From e0df88d2a23b02b7fb90c1000d63dc721acec3ed Mon Sep 17 00:00:00 2001 From: Suleman Dawood Date: Tue, 15 Sep 2026 19:29:22 +1000 Subject: [PATCH] feat(providers): add a host provider so no API key is needed The markdown version let the host agent do the reasoning; the TypeScript CLI moved the LLM call into the process, which forced an API key. Add a host provider that drives the installed agentic CLI (claude, opencode, codex, pi) non-interactively and parses the JSON it returns. When no API key is present the CLI falls back to it, so the skill works with zero configuration in Claude Code, opencode, Codex, and pi. Also restore LLM-generated queries: when an LLM is available, retrieve() asks it for the query plan (keywords, product names, topics, preserved terms) and falls back to the heuristic planner on any failure. Disable with GITHUBPILL_LLM_QUERIES=0. - providers/host.ts: HostClient with an injectable runner; resolveHost auto-detects an agent and honours GITHUBPILL_AGENT - structured.ts: extractJson pulls a JSON object out of prose, fences, or ANSI-coloured output - query-plan.ts: planQueriesWithLlm with heuristic fallback - config: host fallback, GITHUBPILL_AGENT, llmQueries 129 tests. Verified end to end with no API key: validate and explore both run through a stub agent CLI. --- README.md | 10 ++- skills/githubpill/SKILL.md | 16 +++- src/cli.ts | 3 + src/config.test.ts | 18 ++++- src/config.ts | 22 ++++-- src/explore.ts | 1 + src/index.ts | 1 + src/retrieval/query-plan.test.ts | 28 ++++++- src/retrieval/query-plan.ts | 42 ++++++++++ src/retrieval/retrieve.ts | 10 ++- src/synthesis/providers/host.test.ts | 52 ++++++++++++ src/synthesis/providers/host.ts | 110 ++++++++++++++++++++++++++ src/synthesis/providers/index.ts | 4 + src/synthesis/providers/structured.ts | 49 ++++++++++++ src/testing/fakes.ts | 1 + src/types.ts | 2 +- src/validate.ts | 1 + 17 files changed, 352 insertions(+), 18 deletions(-) create mode 100644 src/synthesis/providers/host.test.ts create mode 100644 src/synthesis/providers/host.ts diff --git a/README.md b/README.md index ed0b3ba..466febf 100644 --- a/README.md +++ b/README.md @@ -91,9 +91,15 @@ whichever key is present, or set explicitly with `--provider` / | `openai` | `OPENAI_API_KEY` | `gpt-4o` | | `gemini` | `GEMINI_API_KEY` or `GOOGLE_API_KEY` | `gemini-2.0-flash` | | `deepseek` | `DEEPSEEK_API_KEY` | `deepseek-chat` | +| `host` | none — uses an installed agentic CLI | — | + +If no API key is set, GithubPill falls back to the **`host`** provider and +drives the agentic CLI already on your machine (`claude`, `opencode`, `codex`, +or `pi`) to do the reasoning, so it works with **no API key**. Set +`GITHUBPILL_AGENT` to choose one explicitly. ```bash -export ANTHROPIC_API_KEY=... # or OPENAI_API_KEY / GEMINI_API_KEY +export ANTHROPIC_API_KEY=... # or OPENAI_API_KEY / GEMINI_API_KEY / DEEPSEEK_API_KEY export GITHUB_TOKEN=... # optional; raises GitHub rate limits ``` @@ -195,4 +201,4 @@ the Actions token to write, or you must pass a personal access token instead. ## License -MIT — see [LICENSE](./LICENSE). +MIT License diff --git a/skills/githubpill/SKILL.md b/skills/githubpill/SKILL.md index 9f02e6a..514472a 100644 --- a/skills/githubpill/SKILL.md +++ b/skills/githubpill/SKILL.md @@ -44,7 +44,7 @@ Flags: |---|---| | `--deep` | clone top candidates and cite `path:LINE` evidence | | `--explore` | explore a space instead of validating an idea | -| `--provider ` | LLM provider: `anthropic`, `openai`, `gemini`, `deepseek` | +| `--provider ` | LLM provider: `anthropic`, `openai`, `gemini`, `deepseek`, `host` | | `--json` / `--html` | also write machine-readable / self-contained HTML reports | | `--out ` | output directory (default `githubpill-reports`) | | `--sources github,npm,pypi,hackernews` | restrict sources | @@ -54,13 +54,25 @@ Flags: ## Requirements -- One LLM API key — `ANTHROPIC_API_KEY`, `OPENAI_API_KEY`, +An LLM, in one of two ways: + +- **The host agent itself (no API key).** If no API key is set, the CLI falls + back to the `host` provider and drives the installed agentic CLI + (`claude`, `opencode`, `codex`, or `pi`) to do the reasoning. This is the + zero-config path: the user's existing agent does the LLM work. +- **An API key** — `ANTHROPIC_API_KEY`, `OPENAI_API_KEY`, `GEMINI_API_KEY` / `GOOGLE_API_KEY`, or `DEEPSEEK_API_KEY`. Auto-detected, or set with `--provider`. + +Also: + - `GITHUB_TOKEN` or `GH_TOKEN` — optional but strongly recommended; raises the GitHub rate limits. Falls back to `gh auth token`. - Node.js ≥ 20. +Query generation uses the same LLM when one is available; set +`GITHUBPILL_LLM_QUERIES=0` to use the built-in heuristic planner instead. + ## Present the result Reports land in `./githubpill-reports/` (one per idea or topic). Relay to the user: diff --git a/src/cli.ts b/src/cli.ts index c190bb8..a088281 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -42,11 +42,14 @@ Providers and API keys (first key found selects the provider): openai OPENAI_API_KEY default model ${DEFAULT_MODELS.openai} gemini GEMINI_API_KEY or GOOGLE_API_KEY default model ${DEFAULT_MODELS.gemini} deepseek DEEPSEEK_API_KEY default model ${DEFAULT_MODELS.deepseek} + host (no key) uses an installed agentic CLI: claude, opencode, codex, pi Other environment: GITHUB_TOKEN / GH_TOKEN Optional; raises GitHub rate limits (falls back to \`gh auth token\`) GITHUBPILL_PROVIDER Force a provider instead of auto-detecting GITHUBPILL_MODEL Override the model id + GITHUBPILL_AGENT Agentic CLI for the host provider (claude|opencode|codex|pi) + GITHUBPILL_LLM_QUERIES Set to 0 to use the heuristic query planner GITHUBPILL_LOG silent | error | warn | info | debug Examples: diff --git a/src/config.test.ts b/src/config.test.ts index ffc0def..79ab97d 100644 --- a/src/config.test.ts +++ b/src/config.test.ts @@ -39,8 +39,22 @@ describe("loadConfig", () => { ); }); - it("throws when no key is present", () => { - expect(() => loadConfig({}, noGh)).toThrow(ConfigError); + it("falls back to the host provider when no key is present", () => { + expect(loadConfig({}, noGh).llm.provider).toBe("host"); + }); + + it("accepts an explicit host provider without a key", () => { + expect(loadConfig({ GITHUBPILL_PROVIDER: "host" }, noGh).llm.provider).toBe("host"); + }); + + it("reads GITHUBPILL_AGENT for the host provider", () => { + const config = loadConfig({ GITHUBPILL_PROVIDER: "host", GITHUBPILL_AGENT: "opencode" }, noGh); + expect(config.llm.agent).toBe("opencode"); + }); + + it("generates queries with the LLM by default, unless disabled", () => { + expect(loadConfig({ OPENAI_API_KEY: "k" }, noGh).llmQueries).toBe(true); + expect(loadConfig({ OPENAI_API_KEY: "k", GITHUBPILL_LLM_QUERIES: "0" }, noGh).llmQueries).toBe(false); }); it("names the missing key when the provider is explicit", () => { diff --git a/src/config.ts b/src/config.ts index 74de46b..829261e 100644 --- a/src/config.ts +++ b/src/config.ts @@ -9,6 +9,7 @@ export const DEFAULT_MODELS: Record = { openai: "gpt-4o", gemini: "gemini-2.0-flash", deepseek: "deepseek-chat", + host: "host", }; /** Env vars checked, in order, for each provider's API key. */ @@ -17,6 +18,7 @@ const API_KEY_ENV: Record = { openai: ["OPENAI_API_KEY"], gemini: ["GEMINI_API_KEY", "GOOGLE_API_KEY"], deepseek: ["DEEPSEEK_API_KEY"], + host: [], }; const BASE_URL_ENV: Record = { @@ -24,6 +26,7 @@ const BASE_URL_ENV: Record = { openai: "OPENAI_BASE_URL", gemini: "GEMINI_BASE_URL", deepseek: "DEEPSEEK_BASE_URL", + host: "", }; export interface LlmConfig { @@ -31,6 +34,8 @@ export interface LlmConfig { apiKey: string; model: string; baseUrl?: string; + /** For the `host` provider: which agentic CLI to invoke. */ + agent?: string; maxTokens: number; timeoutMs: number; } @@ -45,6 +50,8 @@ export interface Config { concurrency: number; requestTimeoutMs: number; maxQueriesPerSource: number; + /** Generate search queries with the LLM instead of the heuristic planner. */ + llmQueries: boolean; /** Deep mode: how many top candidates to clone and inspect. */ deepCandidates: number; cloneTimeoutMs: number; @@ -83,12 +90,9 @@ function resolveProvider(env: NodeJS.ProcessEnv): ProviderId { return requested as ProviderId; } - const detected = PROVIDER_IDS.find((provider) => apiKeyFor(env, provider)); - if (!detected) { - const keys = PROVIDER_IDS.flatMap((provider) => API_KEY_ENV[provider]).join(", "); - throw new ConfigError(`No LLM API key found. Set one of: ${keys}.`); - } - return detected; + // Prefer a real API key; fall back to the host agent, which needs none. + const detected = PROVIDER_IDS.find((provider) => provider !== "host" && apiKeyFor(env, provider)); + return detected ?? "host"; } function resolveSources(env: NodeJS.ProcessEnv): SourceId[] { @@ -128,8 +132,8 @@ export interface ConfigDeps { export function loadConfig(env: NodeJS.ProcessEnv = process.env, deps: ConfigDeps = {}): Config { const provider = resolveProvider(env); - const apiKey = apiKeyFor(env, provider); - if (!apiKey) { + const apiKey = apiKeyFor(env, provider) ?? ""; + if (provider !== "host" && !apiKey) { throw new ConfigError( `Provider "${provider}" is selected but no API key is set. Set ${API_KEY_ENV[provider].join(" or ")}.`, ); @@ -163,6 +167,7 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env, deps: ConfigDep maxTokens: limits.maxTokens, timeoutMs: limits.requestTimeoutMs, ...(env[BASE_URL_ENV[provider]] ? { baseUrl: env[BASE_URL_ENV[provider]] as string } : {}), + ...(env.GITHUBPILL_AGENT ? { agent: env.GITHUBPILL_AGENT } : {}), }, sources: resolveSources(env), perSourceLimit: limits.perSourceLimit, @@ -170,6 +175,7 @@ export function loadConfig(env: NodeJS.ProcessEnv = process.env, deps: ConfigDep concurrency: limits.concurrency, requestTimeoutMs: limits.requestTimeoutMs, maxQueriesPerSource: limits.maxQueriesPerSource, + llmQueries: env.GITHUBPILL_LLM_QUERIES !== "0", deepCandidates: limits.deepCandidates, cloneTimeoutMs: limits.cloneTimeoutMs, deepMaxFiles: limits.deepMaxFiles, diff --git a/src/explore.ts b/src/explore.ts index 52eda53..96d6ffd 100644 --- a/src/explore.ts +++ b/src/explore.ts @@ -33,6 +33,7 @@ export async function explore(options: ExploreOptions): Promise { const retrieval = await retrieve({ idea: options.topic, config: options.config, + llm: options.llm, adapters: options.adapters, onProgress: progress, signal: options.signal, diff --git a/src/index.ts b/src/index.ts index cda7e96..3e5c9fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,6 +35,7 @@ export type { SearchOptions, SourceAdapter } from "./adapters/index.js"; export { AnthropicClient, GeminiClient, + HostClient, OpenAICompatibleClient, createLLMClient, } from "./synthesis/providers/index.js"; diff --git a/src/retrieval/query-plan.test.ts b/src/retrieval/query-plan.test.ts index 3e54458..6f2281c 100644 --- a/src/retrieval/query-plan.test.ts +++ b/src/retrieval/query-plan.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from "vitest"; -import { extractPreservedTerms, extractProductNames, planQueries } from "./query-plan.js"; +import { extractPreservedTerms, extractProductNames, planQueries, planQueriesWithLlm } from "./query-plan.js"; +import { FakeLLM } from "../testing/fakes.js"; describe("planQueries", () => { it("preserves acronyms and hyphenated jargon", () => { @@ -34,3 +35,28 @@ describe("planQueries", () => { expect(plan.sharpened.length).toBeLessThanOrEqual(200); }); }); + +describe("planQueriesWithLlm", () => { + it("uses the model plan and keeps heuristic preserved terms", async () => { + const llm = new FakeLLM({ + query_plan: { + sharpened: "An NDIS invoice validator", + keywords: ["ndis invoice checker"], + productNames: ["SomeProduct"], + topics: ["ndis"], + preservedTerms: [], + }, + }); + + const plan = await planQueriesWithLlm("An NDIS invoice validator for providers", llm); + + expect(plan.keywords).toEqual(["ndis invoice checker"]); + expect(plan.productNames).toEqual(["SomeProduct"]); + expect(plan.preservedTerms).toContain("NDIS"); + }); + + it("falls back to the heuristic when the model fails", async () => { + const plan = await planQueriesWithLlm("a todo cli", new FakeLLM()); + expect(plan.keywords.length).toBeGreaterThan(0); + }); +}); diff --git a/src/retrieval/query-plan.ts b/src/retrieval/query-plan.ts index 34ba82e..9afb008 100644 --- a/src/retrieval/query-plan.ts +++ b/src/retrieval/query-plan.ts @@ -1,3 +1,5 @@ +import { z } from "zod"; +import type { LLMClient } from "../synthesis/providers/types.js"; import type { QueryPlan } from "../types.js"; const STOPWORDS = new Set( @@ -140,3 +142,43 @@ export function planQueries(idea: string): QueryPlan { preservedTerms, }; } + +const QueryPlanSchema = z.object({ + sharpened: z.string().describe("One sentence restating the idea"), + keywords: z.array(z.string()).describe("Two to four word search phrases"), + productNames: z.array(z.string()).describe("Known product or project names in this space"), + topics: z.array(z.string()).describe("GitHub topic tags, lowercase and hyphenated"), + preservedTerms: z.array(z.string()).describe("Proper nouns and jargon that must survive verbatim"), +}); + +const QUERY_SYSTEM = [ + "You write search queries for a prior-art tool.", + "Given a project idea, produce diverse queries that will surface existing projects in that space.", + "Cover literal phrasing, near synonyms, the user-facing outcome, adjacent domains, and the names of known products or projects.", + "Preserve proper nouns and jargon verbatim. Never invent projects.", +].join(" "); + +/** + * Ask the LLM for a query plan. Falls back to the heuristic plan on any + * failure, so a flaky model never blocks retrieval. + */ +export async function planQueriesWithLlm(idea: string, llm: LLMClient): Promise { + const fallback = planQueries(idea); + try { + const output = await llm.completeStructured({ + system: QUERY_SYSTEM, + prompt: `IDEA: ${idea}\n\nProduce the query plan.`, + schema: QueryPlanSchema, + schemaName: "query_plan", + }); + return { + sharpened: output.sharpened || fallback.sharpened, + keywords: output.keywords.length > 0 ? output.keywords : fallback.keywords, + productNames: output.productNames.length > 0 ? output.productNames : fallback.productNames, + topics: output.topics.length > 0 ? output.topics : fallback.topics, + preservedTerms: [...new Set([...fallback.preservedTerms, ...output.preservedTerms])], + }; + } catch { + return fallback; + } +} diff --git a/src/retrieval/retrieve.ts b/src/retrieval/retrieve.ts index b7501e1..7974e8c 100644 --- a/src/retrieval/retrieve.ts +++ b/src/retrieval/retrieve.ts @@ -1,14 +1,17 @@ import type { Candidate, ProgressHandler, QueryPlan, SourceRun } from "../types.js"; import type { Config } from "../config.js"; import { createAdapters, type SourceAdapter } from "../adapters/index.js"; -import { planQueries } from "./query-plan.js"; +import { planQueries, planQueriesWithLlm } from "./query-plan.js"; import { fanout, type AdapterError } from "./fanout.js"; import { rankCandidates } from "./rank.js"; import { verifyCandidates } from "../verify/citations.js"; +import type { LLMClient } from "../synthesis/providers/types.js"; export interface RetrieveOptions { idea: string; config: Config; + /** When present (and enabled in config), queries are LLM-generated. */ + llm?: LLMClient; /** Defaults to the adapters for `config.sources`. Inject for tests. */ adapters?: SourceAdapter[]; onProgress?: ProgressHandler; @@ -35,7 +38,10 @@ export async function retrieve(options: RetrieveOptions): Promise { const progress = options.onProgress; progress?.({ type: "stage", stage: "plan" }); - const plan = planQueries(options.idea); + const plan = + options.llm && config.llmQueries + ? await planQueriesWithLlm(options.idea, options.llm) + : planQueries(options.idea); progress?.({ type: "stage", stage: "search" }); const { hits, runs, errors } = await fanout(adapters, plan, config, progress, options.signal); diff --git a/src/synthesis/providers/host.test.ts b/src/synthesis/providers/host.test.ts new file mode 100644 index 0000000..b794e03 --- /dev/null +++ b/src/synthesis/providers/host.test.ts @@ -0,0 +1,52 @@ +import { describe, expect, it } from "vitest"; +import { z } from "zod"; +import { HostClient, resolveHost, type HostRunner } from "./host.js"; + +const schema = z.object({ answer: z.string() }); +const options = { apiKey: "", model: "host", maxTokens: 10, timeoutMs: 1_000 }; +const request = { system: "be terse", prompt: "answer", schema, schemaName: "emit" }; + +describe("HostClient", () => { + it("sends the schema and parses JSON out of fenced output", async () => { + let captured = ""; + const runner: HostRunner = async (_command, args) => { + captured = args.join(" "); + return 'Here you go:\n```json\n{"answer":"ok"}\n```\n'; + }; + + const client = new HostClient({ ...options, agent: "claude" }, runner); + const result = await client.completeStructured(request); + + expect(client.provider).toBe("host"); + expect(result).toEqual({ answer: "ok" }); + expect(captured).toContain("JSON Schema"); + expect(captured).toContain('"answer"'); + }); + + it("extracts JSON embedded in prose", async () => { + const runner: HostRunner = async () => 'sure — {"answer":"ok"} hope that helps'; + const client = new HostClient({ ...options, agent: "opencode" }, runner); + await expect(client.completeStructured(request)).resolves.toEqual({ answer: "ok" }); + }); + + it("surfaces a host failure", async () => { + const runner: HostRunner = async () => { + throw new Error("boom"); + }; + const client = new HostClient({ ...options, agent: "claude" }, runner); + await expect(client.completeStructured(request)).rejects.toThrow(/boom/); + }); +}); + +describe("resolveHost", () => { + it("uses a known command for a named agent", () => { + expect(resolveHost("claude").command).toBe("claude"); + expect(resolveHost("codex").command).toBe("codex"); + }); + + it("treats an unknown name as a custom command", () => { + const host = resolveHost("my-agent"); + expect(host.command).toBe("my-agent"); + expect(host.args("hello")).toEqual(["hello"]); + }); +}); diff --git a/src/synthesis/providers/host.ts b/src/synthesis/providers/host.ts new file mode 100644 index 0000000..9d7e34d --- /dev/null +++ b/src/synthesis/providers/host.ts @@ -0,0 +1,110 @@ +import { execFile } from "node:child_process"; +import { existsSync } from "node:fs"; +import { delimiter, join } from "node:path"; +import { ConfigError, ProviderError } from "../../errors.js"; +import type { ProviderId } from "../../types.js"; +import { + extractJson, + validateStructured, + withJsonSchemaInstruction, + withStructuredRetry, +} from "./structured.js"; +import type { LLMClient, ProviderOptions, StructuredRequest } from "./types.js"; + +/** Runs a command and resolves with stdout. Injectable so tests need no CLI. */ +export type HostRunner = (command: string, args: readonly string[], timeoutMs: number) => Promise; + +export interface HostClientOptions extends ProviderOptions { + /** Force a specific agentic CLI instead of auto-detecting one. */ + agent?: string; +} + +interface HostCommand { + name: string; + command: string; + args: (prompt: string) => string[]; +} + +/** Non-interactive invocations for the agentic CLIs we know how to drive. */ +const HOST_COMMANDS: Record = { + claude: { name: "claude", command: "claude", args: (prompt) => ["-p", prompt] }, + opencode: { name: "opencode", command: "opencode", args: (prompt) => ["run", prompt] }, + codex: { name: "codex", command: "codex", args: (prompt) => ["exec", prompt] }, + pi: { name: "pi", command: "pi", args: (prompt) => ["-p", prompt] }, +}; + +const DETECTION_ORDER = ["claude", "opencode", "codex", "pi"] as const; + +export function commandExists(command: string): boolean { + const directories = (process.env.PATH ?? "").split(delimiter).filter(Boolean); + const extensions = process.platform === "win32" ? [".exe", ".cmd", ".bat", ""] : [""]; + return directories.some((directory) => + extensions.some((extension) => existsSync(join(directory, `${command}${extension}`))), + ); +} + +export function resolveHost(agent?: string): HostCommand { + const requested = agent ?? process.env.GITHUBPILL_AGENT; + if (requested) { + return HOST_COMMANDS[requested] ?? { name: requested, command: requested, args: (prompt) => [prompt] }; + } + + const found = DETECTION_ORDER.map((name) => HOST_COMMANDS[name]).find( + (host) => host !== undefined && commandExists(host.command), + ); + if (!found) { + throw new ConfigError( + "No agentic CLI found for the host provider (looked for claude, opencode, codex, pi). Install one, set GITHUBPILL_AGENT, or set an API key instead.", + ); + } + return found; +} + +export const execHost: HostRunner = (command, args, timeoutMs) => + new Promise((resolve, reject) => { + execFile( + command, + [...args], + { timeout: timeoutMs, maxBuffer: 20 * 1024 * 1024 }, + (error, stdout, stderr) => { + if (error) { + const detail = (stderr || error.message).trim().slice(0, 300); + reject(new ProviderError(`host agent failed: ${detail}`, "host")); + return; + } + resolve(stdout); + }, + ); + }); + +/** + * Uses the host agentic CLI as the LLM, so no API key is required. The agent + * is asked for JSON matching the schema and its output is parsed and validated + * the same way as any other provider. + */ +export class HostClient implements LLMClient { + readonly provider: ProviderId = "host"; + readonly model: string; + private readonly host: HostCommand; + + constructor( + private readonly options: HostClientOptions, + private readonly runner: HostRunner = execHost, + ) { + this.model = options.agent ?? "host"; + this.host = resolveHost(options.agent); + } + + async completeStructured(request: StructuredRequest): Promise { + return withStructuredRetry(async () => { + const raw = await this.send(request); + return validateStructured(request.schema, raw, this.provider); + }); + } + + private async send(request: StructuredRequest): Promise { + const prompt = `${request.system}\n\n${withJsonSchemaInstruction(request.prompt, request.schema)}`; + const output = await this.runner(this.host.command, this.host.args(prompt), this.options.timeoutMs); + return extractJson(output, this.provider); + } +} diff --git a/src/synthesis/providers/index.ts b/src/synthesis/providers/index.ts index cbc6244..96f5eba 100644 --- a/src/synthesis/providers/index.ts +++ b/src/synthesis/providers/index.ts @@ -4,6 +4,7 @@ import type { LLMClient, ProviderOptions } from "./types.js"; import { AnthropicClient } from "./anthropic.js"; import { GeminiClient } from "./gemini.js"; import { OpenAICompatibleClient } from "./openai-compatible.js"; +import { HostClient } from "./host.js"; const OPENAI_BASE_URL = "https://api.openai.com/v1"; const DEEPSEEK_BASE_URL = "https://api.deepseek.com"; @@ -41,6 +42,8 @@ export function createLLMClient(config: LlmConfig): LLMClient { jsonMode: "json_object", baseUrl: baseUrl ?? DEEPSEEK_BASE_URL, }); + case "host": + return new HostClient({ ...options, ...(config.agent ? { agent: config.agent } : {}) }); default: { const unsupported: never = config.provider; throw new ConfigError(`Unsupported provider: ${String(unsupported)}`); @@ -52,3 +55,4 @@ export type { LLMClient, ProviderOptions, StructuredRequest } from "./types.js"; export { AnthropicClient } from "./anthropic.js"; export { GeminiClient } from "./gemini.js"; export { OpenAICompatibleClient } from "./openai-compatible.js"; +export { HostClient, resolveHost } from "./host.js"; diff --git a/src/synthesis/providers/structured.ts b/src/synthesis/providers/structured.ts index 70885ee..350e4c7 100644 --- a/src/synthesis/providers/structured.ts +++ b/src/synthesis/providers/structured.ts @@ -66,6 +66,55 @@ export function parseStructuredText(text: string, provider: ProviderId): unknown } } +function tryParse(text: string): unknown | undefined { + try { + return JSON.parse(text); + } catch { + return undefined; + } +} + +/** + * Pull the first JSON object out of text that may include ANSI codes, prose, or + * a fenced code block — agentic CLIs do not guarantee bare JSON on stdout. + */ +export function extractJson(text: string, provider: ProviderId): unknown { + const cleaned = text.replace(/\u001b\[[0-9;]*m/g, "").trim(); + + const fence = cleaned.match(/```(?:json)?\s*([\s\S]*?)```/i)?.[1]?.trim(); + const candidates = [fence, cleaned].filter((value): value is string => Boolean(value)); + for (const candidate of candidates) { + const parsed = tryParse(candidate); + if (parsed !== undefined) return parsed; + } + + const start = cleaned.indexOf("{"); + if (start >= 0) { + let depth = 0; + let inString = false; + let escaped = false; + for (let i = start; i < cleaned.length; i += 1) { + const char = cleaned[i] as string; + if (inString) { + if (escaped) escaped = false; + else if (char === "\\") escaped = true; + else if (char === '"') inString = false; + } else if (char === '"') inString = true; + else if (char === "{") depth += 1; + else if (char === "}") { + depth -= 1; + if (depth === 0) { + const parsed = tryParse(cleaned.slice(start, i + 1)); + if (parsed !== undefined) return parsed; + break; + } + } + } + } + + throw new StructuredOutputError("no JSON object found in the response", provider); +} + /** * Run an attempt, and retry it once if the model returned output that did not * match the schema. A second try usually fixes malformed JSON; auth and diff --git a/src/testing/fakes.ts b/src/testing/fakes.ts index 8efdef3..f169fd1 100644 --- a/src/testing/fakes.ts +++ b/src/testing/fakes.ts @@ -18,6 +18,7 @@ export function testConfig(overrides: Partial = {}): Config { concurrency: 2, requestTimeoutMs: 1_000, maxQueriesPerSource: 2, + llmQueries: false, deepCandidates: 3, cloneTimeoutMs: 1_000, deepMaxFiles: 5, diff --git a/src/types.ts b/src/types.ts index dc29ae8..13e5f53 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,7 +1,7 @@ export const SOURCE_IDS = ["github", "npm", "pypi", "hackernews"] as const; export type SourceId = (typeof SOURCE_IDS)[number]; -export const PROVIDER_IDS = ["anthropic", "openai", "gemini", "deepseek"] as const; +export const PROVIDER_IDS = ["anthropic", "openai", "gemini", "deepseek", "host"] as const; export type ProviderId = (typeof PROVIDER_IDS)[number]; /** diff --git a/src/validate.ts b/src/validate.ts index 031e236..96a0ec3 100644 --- a/src/validate.ts +++ b/src/validate.ts @@ -37,6 +37,7 @@ export async function validate(options: ValidateOptions): Promise