63 lines
3.6 KiB
PHP
63 lines
3.6 KiB
PHP
<?php
|
|
header("Content-Type: application/json");
|
|
header("Access-Control-Allow-Origin: *");
|
|
if ($_SERVER["REQUEST_METHOD"]==="OPTIONS"){http_response_code(200);exit;}
|
|
require_once "/opt/wevads/vault/credentials.php";
|
|
$db = pg_connect("host=127.0.0.1 dbname=adx_system user=admin password=admin123");
|
|
$action = $_GET["action"] ?? $_POST["action"] ?? "";
|
|
$session = $_GET["session"] ?? $_POST["session"] ?? "";
|
|
$input = json_decode(file_get_contents("php://input"), true) ?: [];
|
|
|
|
switch($action) {
|
|
case "save":
|
|
$role = $input["role"] ?? "user";
|
|
$content = $input["content"] ?? "";
|
|
$provider = $input["provider"] ?? "";
|
|
$tokens = (int)($input["tokens"] ?? 0);
|
|
$sid = $input["session"] ?? $session;
|
|
if (!$sid || !$content) { echo json_encode(["error"=>"session+content required"]); exit; }
|
|
pg_query_params($db, "INSERT INTO wevia_conversations(session_id,role,content,provider,tokens) VALUES($1,$2,$3,$4,$5)",
|
|
[$sid, $role, mb_substr($content,0,5000), $provider, $tokens]);
|
|
echo json_encode(["ok"=>true,"session"=>$sid]);
|
|
break;
|
|
case "history":
|
|
if (!$session) { echo json_encode(["error"=>"session required"]); exit; }
|
|
$limit = min((int)($_GET["limit"] ?? 20), 50);
|
|
$r = pg_query_params($db, "SELECT role,content,provider,tokens,created_at FROM wevia_conversations WHERE session_id=$1 ORDER BY created_at DESC LIMIT $2", [$session, $limit]);
|
|
$msgs = [];
|
|
while($row = pg_fetch_assoc($r)) $msgs[] = $row;
|
|
echo json_encode(["session"=>$session,"messages"=>array_reverse($msgs),"count"=>count($msgs)]);
|
|
break;
|
|
case "sessions":
|
|
$r = pg_query($db, "SELECT session_id, count(*) as msgs, max(created_at) as last_at FROM wevia_conversations GROUP BY session_id ORDER BY last_at DESC LIMIT 20");
|
|
$sessions = [];
|
|
while($row = pg_fetch_assoc($r)) $sessions[] = $row;
|
|
echo json_encode(["sessions"=>$sessions]);
|
|
break;
|
|
case "compact":
|
|
// Context compaction: summarize old messages
|
|
if (!$session) { echo json_encode(["error"=>"session required"]); exit; }
|
|
$r = pg_query_params($db, "SELECT count(*) as c FROM wevia_conversations WHERE session_id=$1", [$session]);
|
|
$count = (int)pg_fetch_assoc($r)["c"];
|
|
if ($count > 15) {
|
|
// Keep last 5, summarize older ones
|
|
$old = pg_query_params($db, "SELECT content FROM wevia_conversations WHERE session_id=$1 ORDER BY created_at ASC LIMIT $2",
|
|
[$session, $count - 5]);
|
|
$summary = "CONTEXT COMPACT: ";
|
|
while($row = pg_fetch_assoc($old)) $summary .= mb_substr($row["content"],0,100) . " | ";
|
|
pg_query_params($db, "DELETE FROM wevia_conversations WHERE session_id=$1 AND ctid NOT IN (SELECT ctid FROM wevia_conversations WHERE session_id=$1 ORDER BY created_at DESC LIMIT 5)", [$session, $session]);
|
|
pg_query_params($db, "INSERT INTO wevia_conversations(session_id,role,content,provider) VALUES($1,'system',$2,'compaction')", [$session, mb_substr($summary,0,3000)]);
|
|
echo json_encode(["ok"=>true,"compacted"=>$count-5,"remaining"=>6]);
|
|
} else {
|
|
echo json_encode(["ok"=>true,"no_compaction"=>true,"count"=>$count]);
|
|
}
|
|
break;
|
|
case "stats":
|
|
$r = pg_query($db, "SELECT count(*) as total, count(DISTINCT session_id) as sessions, max(created_at) as last FROM wevia_conversations");
|
|
echo json_encode(pg_fetch_assoc($r));
|
|
break;
|
|
default:
|
|
echo json_encode(["actions"=>["save","history","sessions","compact","stats"],"version"=>"1.0"]);
|
|
}
|
|
pg_close($db);
|