2f151fea9a
- Added `close()` method to SessionManager and AuthStorage for proper resource cleanup and finalization of prepared statements. - Added `initiatorOverride` option support in OpenAI and Anthropic providers for message attribution control. - Fixed resource leaks in RpcClient timeout handling by centralizing timeout creation with unref() and adding explicit clearTimeout() calls. - Fixed AgentSession disposal to call SessionManager's `close()` method for guaranteed resource cleanup instead of fallback flush. - Updated all test suites to properly dispose AuthStorage instances in cleanup hooks to prevent resource leaks between tests.
74 lines
2.7 KiB
TypeScript
74 lines
2.7 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
import * as fs from "node:fs";
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
import { ModelRegistry } from "@oh-my-pi/pi-coding-agent/config/model-registry";
|
|
import { AuthStorage } from "@oh-my-pi/pi-coding-agent/session/auth-storage";
|
|
import { hookFetch, Snowflake } from "@oh-my-pi/pi-utils";
|
|
|
|
describe("ModelRegistry LM Studio Fixes", () => {
|
|
let tempDir: string;
|
|
let modelsJsonPath: string;
|
|
let authStorage: AuthStorage;
|
|
|
|
beforeEach(async () => {
|
|
tempDir = path.join(os.tmpdir(), `pi-test-lm-studio-fixes-${Snowflake.next()}`);
|
|
fs.mkdirSync(tempDir, { recursive: true });
|
|
modelsJsonPath = path.join(tempDir, "models.json");
|
|
authStorage = await AuthStorage.create(path.join(tempDir, "testauth.db"));
|
|
});
|
|
|
|
afterEach(() => {
|
|
authStorage.close();
|
|
if (tempDir && fs.existsSync(tempDir)) {
|
|
fs.rmSync(tempDir, { recursive: true });
|
|
}
|
|
});
|
|
|
|
test("auto-discovers both ollama and lm-studio models independently", async () => {
|
|
using _hook = hookFetch(input => {
|
|
const url = String(input);
|
|
if (url.includes(":11434/api/tags")) {
|
|
return new Response(JSON.stringify({ models: [{ name: "ollama-model" }] }), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
if (url.includes(":1234/v1/models")) {
|
|
return new Response(JSON.stringify({ data: [{ id: "lm-studio-model" }] }), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
return new Response(null, { status: 404 });
|
|
});
|
|
|
|
const registry = new ModelRegistry(authStorage, modelsJsonPath);
|
|
await registry.refresh();
|
|
|
|
const allModels = registry.getAll();
|
|
expect(allModels.some(m => m.provider === "ollama" && m.id === "ollama-model")).toBe(true);
|
|
expect(allModels.some(m => m.provider === "lm-studio" && m.id === "lm-studio-model")).toBe(true);
|
|
|
|
const available = registry.getAvailable();
|
|
expect(available.some(m => m.provider === "ollama")).toBe(true);
|
|
expect(available.some(m => m.provider === "lm-studio")).toBe(true);
|
|
});
|
|
|
|
test("lm-studio discovery handles trailing slashes in baseUrl correctly", async () => {
|
|
let _requestedUrl = "";
|
|
using _hook = hookFetch(input => {
|
|
const url = String(input);
|
|
// Only track URLs from our test endpoints; ignore concurrent built-in provider discovery
|
|
if (url.includes("127.0.0.1:1234") || url.includes("127.0.0.1:9999") || url.startsWith("not a url")) {
|
|
_requestedUrl = url;
|
|
return new Response(JSON.stringify({ data: [{ id: "model-1" }] }), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
});
|
|
}
|
|
return new Response(null, { status: 404 });
|
|
});
|
|
});
|
|
});
|