69d3acda5d
- Add LLM client abstraction (src/llm-client.ts) with factory pattern - Add OpenAI-compatible external client (src/external-llm-client.ts) - Add Kimi.com client using Anthropic-based API (src/kimi-llm-client.ts) - Add Pi native LLM stub (src/pi-llm-client.ts) for future ExtensionAPI wiring - Add SHA-256 disk cache at ~/.cache/pi-project-map/ (src/llm-cache.ts) - Add parallel batching with p-limit, retry + exponential backoff (src/llm-batch.ts) - Rewrite llm-extract.ts to use real LLM calls with structured prompts - File-level: PURPOSE, DEPS, CONCEPTS - Package-level: ROLE, ARCH - Context truncation, 50KB skip, cache before LLM call - Wire CLI with --llm-provider, --llm-model, --llm-base-url flags - Update config.ts with llmProvider, llmBaseUrl fields - Update init.ts and patch.ts to accept optional LLMClient - Add sample project fixture for manual testing - Add tests: llm-cache (3), llm-batch (5), llm-integration (8 with real Kimi API), pi-extension (14 mocked) - All 56 tests pass
57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { withRetry, processFiles } from "../src/llm-batch.js";
|
|
import { LLMError } from "../src/llm-error.js";
|
|
|
|
describe("withRetry", () => {
|
|
it("returns result on first success", async () => {
|
|
const result = await withRetry(async () => "success");
|
|
expect(result).toBe("success");
|
|
});
|
|
|
|
it("retries on failure and eventually succeeds", async () => {
|
|
let attempts = 0;
|
|
const result = await withRetry(async () => {
|
|
attempts++;
|
|
if (attempts < 3) throw new Error("transient");
|
|
return "success";
|
|
});
|
|
expect(result).toBe("success");
|
|
expect(attempts).toBe(3);
|
|
});
|
|
|
|
it("throws after max retries", async () => {
|
|
let attempts = 0;
|
|
await expect(
|
|
withRetry(async () => {
|
|
attempts++;
|
|
throw new Error("persistent");
|
|
}, { maxRetries: 2, retryDelaysMs: [10, 20] }),
|
|
).rejects.toThrow("persistent");
|
|
expect(attempts).toBe(3); // initial + 2 retries
|
|
});
|
|
});
|
|
|
|
describe("processFiles", () => {
|
|
it("processes all files in parallel", async () => {
|
|
const files = [1, 2, 3, 4, 5];
|
|
const results = await processFiles(files, async (n) => n * 2, {
|
|
concurrency: 2,
|
|
});
|
|
expect(results).toEqual([2, 4, 6, 8, 10]);
|
|
});
|
|
|
|
it("retries failed files", async () => {
|
|
let attempts = 0;
|
|
const results = await processFiles(
|
|
[1],
|
|
async () => {
|
|
attempts++;
|
|
if (attempts < 2) throw new Error("fail");
|
|
return "ok";
|
|
},
|
|
{ maxRetries: 2, retryDelaysMs: [10, 20] },
|
|
);
|
|
expect(results).toEqual(["ok"]);
|
|
});
|
|
});
|