feat: embed pi agent runtime

This commit is contained in:
Peter Steinberger
2025-12-17 11:29:04 +01:00
parent c5867b2876
commit fece42ce0a
42 changed files with 2076 additions and 4009 deletions

View File

@@ -3,7 +3,7 @@ import fs from "node:fs/promises";
import { type AddressInfo, createServer } from "node:net";
import os from "node:os";
import path from "node:path";
import { describe, expect, test, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { WebSocket } from "ws";
import { agentCommand } from "../commands/agent.js";
import { emitAgentEvent } from "../infra/agent-events.js";
@@ -72,11 +72,9 @@ vi.mock("../config/config.js", () => ({
loadConfig: () => ({
inbound: {
allowFrom: testAllowFrom,
reply: {
mode: "command",
command: ["echo", "ok"],
session: { mainKey: "main", store: testSessionStorePath },
},
workspace: path.join(os.tmpdir(), "clawd-gateway-test"),
agent: { provider: "anthropic", model: "claude-opus-4-5" },
session: { mainKey: "main", store: testSessionStorePath },
},
cron: (() => {
const cron: Record<string, unknown> = {};
@@ -107,6 +105,23 @@ vi.mock("../commands/agent.js", () => ({
process.env.CLAWDIS_SKIP_PROVIDERS = "1";
let previousHome: string | undefined;
let tempHome: string | undefined;
beforeEach(async () => {
previousHome = process.env.HOME;
tempHome = await fs.mkdtemp(path.join(os.tmpdir(), "clawdis-gateway-home-"));
process.env.HOME = tempHome;
});
afterEach(async () => {
process.env.HOME = previousHome;
if (tempHome) {
await fs.rm(tempHome, { recursive: true, force: true });
tempHome = undefined;
}
});
async function getFreePort(): Promise<number> {
return await new Promise((resolve, reject) => {
const server = createServer();

View File

@@ -377,7 +377,7 @@ function capArrayByJsonBytes<T>(
function loadSessionEntry(sessionKey: string) {
const cfg = loadConfig();
const sessionCfg = cfg.inbound?.reply?.session;
const sessionCfg = cfg.inbound?.session;
const storePath = sessionCfg?.store
? resolveStorePath(sessionCfg.store)
: resolveStorePath(undefined);
@@ -394,9 +394,9 @@ function classifySessionKey(key: string): GatewaySessionRow["kind"] {
}
function getSessionDefaults(cfg: ClawdisConfig): GatewaySessionsDefaults {
const model = cfg.inbound?.reply?.agent?.model ?? DEFAULT_MODEL;
const model = cfg.inbound?.agent?.model ?? DEFAULT_MODEL;
const contextTokens =
cfg.inbound?.reply?.agent?.contextTokens ??
cfg.inbound?.agent?.contextTokens ??
lookupContextTokens(model) ??
DEFAULT_CONTEXT_TOKENS;
return { model: model ?? null, contextTokens: contextTokens ?? null };
@@ -886,7 +886,7 @@ export async function startGatewayServer(
).items;
const thinkingLevel =
entry?.thinkingLevel ??
loadConfig().inbound?.reply?.thinkingDefault ??
loadConfig().inbound?.agent?.thinkingDefault ??
"off";
return {
ok: true,
@@ -1864,7 +1864,7 @@ export async function startGatewayServer(
).items;
const thinkingLevel =
entry?.thinkingLevel ??
loadConfig().inbound?.reply?.thinkingDefault ??
loadConfig().inbound?.agent?.thinkingDefault ??
"off";
respond(true, {
sessionKey,
@@ -2192,9 +2192,7 @@ export async function startGatewayServer(
}
const p = params as SessionsListParams;
const cfg = loadConfig();
const storePath = resolveStorePath(
cfg.inbound?.reply?.session?.store,
);
const storePath = resolveStorePath(cfg.inbound?.session?.store);
const store = loadSessionStore(storePath);
const result = listSessionsFromStore({
cfg,
@@ -2230,9 +2228,7 @@ export async function startGatewayServer(
}
const cfg = loadConfig();
const storePath = resolveStorePath(
cfg.inbound?.reply?.session?.store,
);
const storePath = resolveStorePath(cfg.inbound?.session?.store);
const store = loadSessionStore(storePath);
const now = Date.now();
@@ -2867,8 +2863,7 @@ export async function startGatewayServer(
}
resolvedSessionId = sessionId;
const mainKey =
(cfg.inbound?.reply?.session?.mainKey ?? "main").trim() ||
"main";
(cfg.inbound?.session?.mainKey ?? "main").trim() || "main";
if (requestedSessionKey === mainKey) {
chatRunSessions.set(sessionId, {
sessionKey: requestedSessionKey,