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
+58
View File
@@ -0,0 +1,58 @@
import { mkdtempSync, rmSync, writeFileSync } from "fs";
import { tmpdir } from "os";
import { join } from "path";
import { ModelRuntime } from "@earendil-works/pi-coding-agent";
export interface ModelDiscoveryAuth {
apiKey?: string;
headers: Record<string, string>;
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function stringRecord(value: unknown): Record<string, string> {
if (!isRecord(value)) return {};
return Object.fromEntries(Object.entries(value).filter((entry): entry is [string, string] => typeof entry[1] === "string"));
}
export async function resolveModelDiscoveryAuth(
providerName: string,
provider: Record<string, unknown>,
): Promise<ModelDiscoveryAuth> {
let tempDir: string | undefined;
try {
tempDir = mkdtempSync(join(tmpdir(), "pi-web-model-discovery-"));
const modelsPath = join(tempDir, "models.json");
const discoveryModelId = "__pi_web_model_discovery__";
writeFileSync(modelsPath, JSON.stringify({
providers: {
[providerName]: {
...provider,
models: [{ id: discoveryModelId }],
},
},
}, null, 2), "utf8");
const modelRuntime = await ModelRuntime.create({ modelsPath });
const loadError = modelRuntime.getError();
if (loadError) throw new Error(loadError);
const model = modelRuntime.getModel(providerName, discoveryModelId);
if (!model) throw new Error(`Unable to load provider "${providerName}"`);
const resolved = await modelRuntime.getAuth(model);
if (resolved) {
return {
apiKey: resolved.auth.apiKey,
headers: stringRecord(resolved.auth.headers),
};
}
return {
headers: stringRecord(modelRuntime.getCompatibilityRequestConfig(model).headers),
};
} finally {
if (tempDir) rmSync(tempDir, { recursive: true, force: true });
}
}