File size: 5,539 Bytes
bc575bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | import { existsSync, mkdirSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type { Provider } from "@earendil-works/pi-ai";
import { getModel } from "@earendil-works/pi-ai/compat";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { AuthStorage } from "../src/core/auth-storage.ts";
import { ModelRuntime } from "../src/core/model-runtime.ts";
import { DefaultResourceLoader } from "../src/core/resource-loader.ts";
import type { ExtensionFactory } from "../src/core/sdk.ts";
import { createAgentSession } from "../src/core/sdk.ts";
import { SessionManager } from "../src/core/session-manager.ts";
import { SettingsManager } from "../src/core/settings-manager.ts";
function nativeAnthropicProvider(baseUrl: string): Provider {
const model = { ...getModel("anthropic", "claude-sonnet-4-5")!, baseUrl };
return {
id: "anthropic",
name: "Native Anthropic",
baseUrl,
auth: {
apiKey: {
name: "Test API key",
resolve: async () => ({ auth: { apiKey: "test-key" }, source: "test" }),
},
},
getModels: () => [model],
stream: () => {
throw new Error("unused");
},
streamSimple: () => {
throw new Error("unused");
},
};
}
describe("AgentSession dynamic provider registration", () => {
let tempDir: string;
let agentDir: string;
beforeEach(() => {
tempDir = join(tmpdir(), `pi-dynamic-provider-test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
agentDir = join(tempDir, "agent");
mkdirSync(agentDir, { recursive: true });
});
afterEach(() => {
if (tempDir && existsSync(tempDir)) {
rmSync(tempDir, { recursive: true, force: true });
}
});
async function createSession(extensionFactories: ExtensionFactory[]) {
const settingsManager = SettingsManager.create(tempDir, agentDir);
const sessionManager = SessionManager.inMemory();
const authStorage = AuthStorage.create(join(agentDir, "auth.json"));
await authStorage.modify("anthropic", async () => ({ type: "api_key", key: "test-key" }));
const modelRuntime = await ModelRuntime.create({
credentials: authStorage,
modelsPath: join(agentDir, "models.json"),
});
const resourceLoader = new DefaultResourceLoader({
cwd: tempDir,
agentDir,
settingsManager,
extensionFactories,
});
await resourceLoader.reload();
const { session } = await createAgentSession({
cwd: tempDir,
agentDir,
model: getModel("anthropic", "claude-sonnet-4-5")!,
settingsManager,
sessionManager,
modelRuntime,
resourceLoader,
});
return session;
}
async function capturePromptBaseUrl(
session: Awaited<ReturnType<typeof createSession>>,
): Promise<string | undefined> {
let baseUrl: string | undefined;
session.agent.streamFunction = async (model) => {
baseUrl = model.baseUrl;
throw new Error("stop");
};
await session.prompt("hello");
return baseUrl;
}
it("applies top-level registerProvider overrides to the active model", async () => {
const session = await createSession([
(pi) => {
pi.registerProvider("anthropic", { baseUrl: "http://localhost:8080/top-level" });
},
]);
expect(session.model?.baseUrl).toBe("http://localhost:8080/top-level");
expect(await capturePromptBaseUrl(session)).toBe("http://localhost:8080/top-level");
session.dispose();
});
it("applies session_start registerProvider overrides to the active model", async () => {
const session = await createSession([
(pi) => {
pi.on("session_start", () => {
pi.registerProvider("anthropic", { baseUrl: "http://localhost:8080/session-start" });
});
},
]);
await session.bindExtensions({});
expect(session.model?.baseUrl).toBe("http://localhost:8080/session-start");
expect(await capturePromptBaseUrl(session)).toBe("http://localhost:8080/session-start");
session.dispose();
});
it("registers native pi-ai providers during extension loading", async () => {
const session = await createSession([
(pi) => {
pi.registerProvider(nativeAnthropicProvider("http://localhost:8080/native-top-level"));
},
]);
expect(session.model?.baseUrl).toBe("http://localhost:8080/native-top-level");
expect(await capturePromptBaseUrl(session)).toBe("http://localhost:8080/native-top-level");
session.dispose();
});
it("applies command-time registerProvider overrides without reload", async () => {
const session = await createSession([
(pi) => {
pi.registerCommand("use-proxy", {
description: "Use proxy",
handler: async () => {
pi.registerProvider("anthropic", { baseUrl: "http://localhost:8080/command" });
},
});
},
]);
await session.bindExtensions({});
await session.prompt("/use-proxy");
expect(session.model?.baseUrl).toBe("http://localhost:8080/command");
expect(await capturePromptBaseUrl(session)).toBe("http://localhost:8080/command");
session.dispose();
});
it("registers native pi-ai providers at command time", async () => {
const session = await createSession([
(pi) => {
pi.registerCommand("use-native", {
description: "Use native provider",
handler: async () => {
pi.registerProvider(nativeAnthropicProvider("http://localhost:8080/native-command"));
},
});
},
]);
await session.bindExtensions({});
await session.prompt("/use-native");
expect(session.model?.baseUrl).toBe("http://localhost:8080/native-command");
expect(await capturePromptBaseUrl(session)).toBe("http://localhost:8080/native-command");
session.dispose();
});
});
|