feat: integrate Pi backend and Pi Web

This commit is contained in:
luckyyzh
2026-07-30 19:37:53 +08:00
commit 7392ab9dd7
1390 changed files with 337197 additions and 0 deletions
+73
View File
@@ -0,0 +1,73 @@
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const rootDir = dirname(dirname(fileURLToPath(import.meta.url)));
const baseUrl = process.env.PI_WEB_URL ?? "http://127.0.0.1:30141";
const cwd = resolve(process.env.PI_SMOKE_CWD ?? rootDir);
async function readJson(path, init) {
const response = await fetch(`${baseUrl}${path}`, init);
const text = await response.text();
let data;
try {
data = JSON.parse(text);
} catch {
data = { error: text };
}
if (!response.ok) {
throw new Error(`${init?.method ?? "GET"} ${path} returned ${response.status}: ${data.error ?? text}`);
}
return data;
}
const pageResponse = await fetch(baseUrl);
const pageHtml = await pageResponse.text();
if (!pageResponse.ok || !pageHtml.includes("Pi Web")) {
throw new Error(`Pi Web page check failed with status ${pageResponse.status}`);
}
const jsonHeaders = { "Content-Type": "application/json" };
const validated = await readJson("/api/cwd/validate", {
method: "POST",
headers: jsonHeaders,
body: JSON.stringify({ cwd }),
});
const models = await readJson(`/api/models?cwd=${encodeURIComponent(cwd)}`);
const runtime = await readJson("/api/runtime");
const created = await readJson("/api/agent/new", {
method: "POST",
headers: jsonHeaders,
body: JSON.stringify({ cwd, type: "ensure_session", toolNames: [] }),
});
const state = await readJson(`/api/agent/${encodeURIComponent(created.sessionId)}`);
if (
!validated.success ||
!runtime.managed ||
resolve(runtime.appRoot) !== rootDir ||
resolve(runtime.agentDir) !== resolve(rootDir, "data", "agent") ||
!created.success ||
!created.sessionId ||
!state.running ||
!state.state
) {
throw new Error("Pi AgentSession smoke check returned an incomplete state");
}
console.log(
JSON.stringify(
{
pageStatus: pageResponse.status,
cwdAccepted: validated.success,
managedRuntime: runtime.managed,
agentDir: runtime.agentDir,
modelCount: Array.isArray(models.modelList) ? models.modelList.length : 0,
defaultModelConfigured: Boolean(models.defaultModel),
sessionCreated: created.success,
runtimeRunning: state.running,
llmRequestSent: false,
},
null,
2,
),
);