diff options
| author | Adam Malczewski <[email protected]> | 2026-06-04 21:21:20 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-06-04 21:21:20 +0900 |
| commit | 394f1ed37ce860da6fdc385769bf29f9737105cd (patch) | |
| tree | 4b825dc642cb6eb9a060e54bf8d69288fbee4904 /packages/core | |
| parent | 81a9cdbadf8c9d940d4fe9a2a0de607dee1f5f1a (diff) | |
| download | dispatch-394f1ed37ce860da6fdc385769bf29f9737105cd.tar.gz dispatch-394f1ed37ce860da6fdc385769bf29f9737105cd.zip | |
chore: genesis — remove all files to rebuild from scratch (arch rewrite)
Diffstat (limited to 'packages/core')
115 files changed, 0 insertions, 20596 deletions
diff --git a/packages/core/package.json b/packages/core/package.json deleted file mode 100644 index 55dff0f..0000000 --- a/packages/core/package.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "name": "@dispatch/core", - "version": "0.0.1", - "private": true, - "type": "module", - "main": "src/index.ts", - "types": "src/index.ts", - "scripts": { - "test": "vitest run", - "test:watch": "vitest", - "typecheck": "tsc --noEmit" - }, - "dependencies": { - "@ai-sdk/anthropic": "^3.0.79", - "@ai-sdk/openai-compatible": "^2.0.48", - "ai": "^6.0.191", - "chokidar": "^5.0.0", - "smol-toml": "^1.6.1", - "tree-sitter-bash": "^0.25.1", - "vscode-jsonrpc": "8.2.1", - "vscode-languageserver-types": "3.17.5", - "web-tree-sitter": "^0.26.8", - "zod": "^3.23.0", - "zod-to-json-schema": "^3.25.2" - }, - "devDependencies": { - "@types/bun": "latest" - } -} diff --git a/packages/core/src/agent/agent.ts b/packages/core/src/agent/agent.ts Binary files differdeleted file mode 100644 index 2e2dbb2..0000000 --- a/packages/core/src/agent/agent.ts +++ /dev/null diff --git a/packages/core/src/agents/index.ts b/packages/core/src/agents/index.ts deleted file mode 100644 index 4931162..0000000 --- a/packages/core/src/agents/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -export { - deleteAgent, - expandAgentToolNames, - GLOBAL_AGENTS_DIR, - getAgentDirPaths, - getAgentDirs, - getProjectAgentsDir, - loadAgent, - loadAgents, - saveAgent, -} from "./loader.js"; diff --git a/packages/core/src/agents/loader.ts b/packages/core/src/agents/loader.ts deleted file mode 100644 index 9971803..0000000 --- a/packages/core/src/agents/loader.ts +++ /dev/null @@ -1,294 +0,0 @@ -import * as fs from "node:fs"; -import * as os from "node:os"; -import * as path from "node:path"; -import { parse as parseTOML, stringify as stringifyTOML } from "smol-toml"; -import { type AgentDefinition, type AgentModelEntry, isReasoningEffort } from "../types/index.js"; - -// ─── Helpers ───────────────────────────────────────────────────── - -/** Sanitize a slug to prevent path traversal */ -function sanitizeSlug(slug: string): string { - // Strip directory components and ensure only safe characters - const base = path.basename(slug); - const clean = base - .replace(/[^a-zA-Z0-9_-]/g, "-") - .replace(/-+/g, "-") - .replace(/^-|-$/g, ""); - if (!clean) throw new Error("Invalid agent slug"); - return clean; -} - -// ─── Constants ─────────────────────────────────────────────────── - -export const GLOBAL_AGENTS_DIR = path.join(os.homedir(), ".config", "dispatch", "agents"); - -export function getProjectAgentsDir(projectDir: string): string { - return path.join(projectDir, ".dispatch", "agents"); -} - -// ─── Public API ────────────────────────────────────────────────── - -/** - * Returns the agent directories that exist or could exist. - * Always includes global. Includes project dir if projectDir is provided. - */ -export function getAgentDirs( - projectDir?: string, -): Array<{ label: string; path: string; scope: string }> { - const dirs: Array<{ label: string; path: string; scope: string }> = [ - { label: "Global (~/.config/dispatch/agents)", path: GLOBAL_AGENTS_DIR, scope: "global" }, - ]; - if (projectDir) { - dirs.push({ - label: `.dispatch/agents (${path.basename(projectDir)})`, - path: getProjectAgentsDir(projectDir), - scope: projectDir, - }); - } - return dirs; -} - -/** - * Return just the absolute filesystem paths of the agent directories. - * Used by the agent's permission gate to grant read-only access to - * these locations by default (so any agent can list/read agent - * definitions without prompting the user). - */ -export function getAgentDirPaths(projectDir?: string): string[] { - const paths = [GLOBAL_AGENTS_DIR]; - if (projectDir) paths.push(getProjectAgentsDir(projectDir)); - return paths; -} - -/** - * Load a single agent definition by slug. Searches the project-scoped - * directory first (if `projectDir` is provided), then falls back to - * the global directory. Returns `null` if no match is found. - * - * Slug matching is exact and case-sensitive; sanitization mirrors - * `saveAgent` to keep loader and writer symmetric. - */ -export function loadAgent(slug: string, projectDir?: string): AgentDefinition | null { - const safeSlug = sanitizeSlug(slug); - const agents = loadAgents(projectDir); - return agents.find((a) => a.slug === safeSlug) ?? null; -} - -/** - * Translate the short permission-group names used by `AgentDefinition.tools` - * (e.g. `"read"`, `"edit"`, `"bash"`) into the concrete tool-implementation - * names registered with the agent runtime (e.g. `"read_file"`, - * `"list_files"`, `"write_file"`, `"run_shell"`). - * - * The mapping mirrors the per-permission tool-creation paths in - * `AgentManager.getOrCreateAgentForTab` so a subagent summoned with a - * given agent definition ends up with the exact same set of registered - * tools as a top-level tab using that definition. Tool names that aren't - * group aliases (`summon`, `retrieve`, `web_search`, `youtube_transcribe`, - * `todo`) are passed through unchanged. - * - * `"todo"` is auto-included so the summoned agent always has its task list - * available, matching the parent-agent path which always registers `todo`. - */ -export function expandAgentToolNames(tools: string[]): string[] { - const expanded = new Set<string>(); - for (const t of tools) { - switch (t) { - case "read": - expanded.add("read_file"); - expanded.add("read_file_slice"); - expanded.add("list_files"); - break; - case "edit": - expanded.add("write_file"); - break; - case "bash": - expanded.add("run_shell"); - break; - default: - // Pass through tool names that aren't permission-group - // aliases (summon, retrieve, web_search, youtube_transcribe, - // send_to_tab, read_tab, todo, and the granular file tools - // themselves if a user hand-wrote them in a TOML). - expanded.add(t); - } - } - // Always include `todo` — every agent should be able to track its work, - // and the parent-agent path adds it unconditionally. - expanded.add("todo"); - return Array.from(expanded); -} - -/** - * Ensure the default global agent exists. Creates it if missing. - */ -function ensureDefaultAgent(): void { - const filePath = path.join(GLOBAL_AGENTS_DIR, "default.toml"); - if (fs.existsSync(filePath)) return; - - const defaultAgent: AgentDefinition = { - name: "Default", - description: "Default agent with all tools enabled", - skills: [], - tools: ["read", "edit", "bash", "summon"], - models: [], - scope: "global", - slug: "default", - }; - saveAgent(defaultAgent); -} - -/** - * Load all agent definitions from global + project directories. - * Auto-generates the default global agent if it doesn't exist. - */ -export function loadAgents(projectDir?: string): AgentDefinition[] { - ensureDefaultAgent(); - - const agents: AgentDefinition[] = []; - - // Global agents - agents.push(...loadAgentsFromDir(GLOBAL_AGENTS_DIR, "global")); - - // Project-scoped agents - if (projectDir) { - agents.push(...loadAgentsFromDir(getProjectAgentsDir(projectDir), projectDir)); - } - - return agents; -} - -/** - * Save (create or update) an agent definition to a TOML file. - * The scope determines which directory: - * - "global" -> ~/.config/dispatch/agents/ - * - any other string -> that directory path + /.dispatch/agents/ - */ -export function saveAgent(agent: AgentDefinition): void { - if (agent.scope !== "global" && agent.scope.includes("..")) { - throw new Error("Invalid agent scope"); - } - const dir = agent.scope === "global" ? GLOBAL_AGENTS_DIR : getProjectAgentsDir(agent.scope); - - fs.mkdirSync(dir, { recursive: true }); - - const tomlContent: Record<string, unknown> = { - name: agent.name, - description: agent.description, - skills: agent.skills, - tools: agent.tools, - }; - - if (agent.cwd) { - tomlContent.cwd = agent.cwd; - } - - if (agent.is_subagent) { - tomlContent.is_subagent = true; - } - - // smol-toml handles [[models]] array-of-tables - if (agent.models.length > 0) { - tomlContent.models = agent.models.map((m) => ({ - key_id: m.key_id, - model_id: m.model_id, - ...(m.effort ? { effort: m.effort } : {}), - })); - } - - const content = stringifyTOML(tomlContent); - const safeSlug = sanitizeSlug(agent.slug); - const filePath = path.join(dir, `${safeSlug}.toml`); - fs.writeFileSync(filePath, content, "utf-8"); -} - -/** - * Delete an agent TOML file. - */ -export function deleteAgent(slug: string, scope: string): boolean { - if (scope !== "global" && scope.includes("..")) { - throw new Error("Invalid agent scope"); - } - const dir = scope === "global" ? GLOBAL_AGENTS_DIR : getProjectAgentsDir(scope); - - const safeSlug = sanitizeSlug(slug); - const filePath = path.join(dir, `${safeSlug}.toml`); - if (fs.existsSync(filePath)) { - fs.unlinkSync(filePath); - return true; - } - return false; -} - -// ─── Internal ──────────────────────────────────────────────────── - -function loadAgentsFromDir(dir: string, scope: string): AgentDefinition[] { - if (!fs.existsSync(dir)) return []; - - const results: AgentDefinition[] = []; - let entries: fs.Dirent[]; - try { - entries = fs.readdirSync(dir, { withFileTypes: true }); - } catch { - return []; - } - - for (const entry of entries) { - if (!entry.isFile() || !entry.name.endsWith(".toml")) continue; - - const filePath = path.join(dir, entry.name); - const slug = entry.name.slice(0, -5); // remove .toml - - try { - const raw = fs.readFileSync(filePath, "utf-8"); - const parsed = parseTOML(raw); - - const models: AgentModelEntry[] = []; - if (Array.isArray(parsed.models)) { - for (const m of parsed.models) { - if (m && typeof m === "object" && "key_id" in m && "model_id" in m) { - const rawEffort = (m as Record<string, unknown>).effort; - models.push({ - key_id: String((m as Record<string, unknown>).key_id), - model_id: String((m as Record<string, unknown>).model_id), - // Only carry `effort` when it's a recognised level; an - // unset or invalid value falls back to the per-tab / - // default effort at the call site. - ...(isReasoningEffort(rawEffort) ? { effort: rawEffort } : {}), - }); - } - } - } - - const skills: string[] = []; - if (Array.isArray(parsed.skills)) { - for (const s of parsed.skills) { - if (typeof s === "string") skills.push(s); - } - } - - const tools: string[] = []; - if (Array.isArray(parsed.tools)) { - for (const t of parsed.tools) { - if (typeof t === "string") tools.push(t); - } - } - - results.push({ - name: typeof parsed.name === "string" ? parsed.name : slug, - description: typeof parsed.description === "string" ? parsed.description : "", - skills, - tools, - models, - scope, - slug, - ...(typeof parsed.cwd === "string" && parsed.cwd ? { cwd: parsed.cwd } : {}), - ...(parsed.is_subagent === true ? { is_subagent: true } : {}), - }); - } catch { - // Skip unparseable files - } - } - - return results; -} diff --git a/packages/core/src/chunks/append.ts b/packages/core/src/chunks/append.ts deleted file mode 100644 index 4ca6fe1..0000000 --- a/packages/core/src/chunks/append.ts +++ /dev/null @@ -1,314 +0,0 @@ -/** - * Chunk-builder helper. - * - * `appendEventToChunks` is the single source of truth for how a stream of - * `AgentEvent`s collapses into an ordered `Chunk[]` on a message. Both the - * backend (agent + agent-manager) and the frontend store call this helper - * so the wire format stays in lockstep across the boundary. - * - * Open/close rules — see notes/plan-chunk-refactor.md for the full table. - * - * | Chunk | Opens on | Coalesces | - * |---------------|-----------------------------------------------------------------|------------------------------------------------------------| - * | `text` | first `text-delta` after a non-text chunk | consecutive `text-delta` events append to `.text` | - * | `thinking` | first `reasoning-delta` after a non-thinking chunk | consecutive `reasoning-delta` events append to `.text` | - * | | OR after the last thinking chunk was sealed by `reasoning-end` | (only into the most recent UNSEALED thinking chunk) | - * | `tool-batch` | first `tool-call` after a non-tool-batch chunk | consecutive `tool-call` events push a new entry to `.calls`| - * | `error` | every `error` event | NEVER (always single-event) | - * | `system` | every `notice`/`model-changed`/`config-reload`/... | NEVER (two consecutive system events → two chunks) | - * - * Side-effect events (no new chunk): - * - `tool-result` → finds the call by `id` across all `tool-batch` - * chunks (most-recent first) and updates its - * `result` / `isError`. - * - `shell-output` → appends to the most recent entry of the most - * recent `tool-batch` chunk. - * - `reasoning-end` → attaches `metadata` (the AI SDK v6 - * `providerMetadata` blob) to the most recent - * UNSEALED `thinking` chunk. The metadata is also - * the "sealed" marker — subsequent - * `reasoning-delta`s will open a new chunk rather - * than extending this one. Anthropic's signature - * lives inside this blob; round-tripping it on the - * next turn is mandatory for Anthropic to accept - * the conversation. Orphan `reasoning-end` events - * (no unsealed thinking chunk) are dropped. - * - * Ignored events: - * - `status`, `turn-start`, `turn-sealed`, `done`, `usage`, - * `task-list-update`, `tab-created`, `message-queued`, `message-consumed`, - * `message-cancelled` — these are control / lifecycle events, not message - * content. - */ - -import type { - AgentEvent, - ChatMessage, - Chunk, - MessageRole, - SystemChunk, - SystemChunkKind, - ToolBatchChunk, -} from "../types/index.js"; - -/** - * Mutates `chunks` in place based on `event`. - * - * Returns void; the array is the output channel. - */ -export function appendEventToChunks(chunks: Chunk[], event: AgentEvent): void { - switch (event.type) { - case "text-delta": { - // Open or extend the current text chunk. - const last = chunks[chunks.length - 1]; - if (last && last.type === "text") { - last.text += event.delta; - } else { - chunks.push({ type: "text", text: event.delta }); - } - return; - } - - case "reasoning-delta": { - // Open a new thinking chunk if the last chunk is not a thinking - // chunk OR if it's already sealed by metadata. Anthropic emits - // each thinking content block with its own metadata; a fresh - // reasoning-delta after a sealed thinking chunk is the start of - // a new block, not a continuation — extending the sealed chunk - // would corrupt the metadata/text mapping. - const last = chunks[chunks.length - 1]; - if (last && last.type === "thinking" && last.metadata === undefined) { - last.text += event.delta; - } else { - chunks.push({ type: "thinking", text: event.delta }); - } - return; - } - - case "reasoning-end": { - // Attach `providerMetadata` to the most recent unsealed - // thinking chunk. Anthropic's signature lives inside this - // blob; without it on the next request, Anthropic rejects the - // thinking block. The walk-back is a defensive backstop — - // Anthropic's SSE delivers a content block's deltas strictly - // in order and `appendEventToChunks` runs synchronously per - // event, so the most recent thinking chunk is normally the - // last chunk in the array. - if (event.metadata === undefined) return; - for (let i = chunks.length - 1; i >= 0; i--) { - const c = chunks[i]; - if (!c || c.type !== "thinking") continue; - if (c.metadata !== undefined) { - // Already sealed; the orphan metadata has no home. - return; - } - c.metadata = event.metadata; - return; - } - // No thinking chunk found at all — drop silently. - return; - } - - case "tool-call": { - // Open or extend the current tool-batch chunk. - const last = chunks[chunks.length - 1]; - const entry = { - id: event.toolCall.id, - name: event.toolCall.name, - arguments: event.toolCall.arguments, - }; - if (last && last.type === "tool-batch") { - last.calls.push(entry); - } else { - chunks.push({ type: "tool-batch", calls: [entry] }); - } - return; - } - - case "tool-result": { - // Find the matching call (by id) across all tool-batch chunks, - // most-recent first. Tool results can arrive after subsequent - // text-deltas, so we cannot rely on the *last* chunk being the - // tool-batch — we have to search. - for (let i = chunks.length - 1; i >= 0; i--) { - const c = chunks[i]; - if (!c || c.type !== "tool-batch") continue; - const call = c.calls.find((e) => e.id === event.toolResult.toolCallId); - if (call) { - call.result = event.toolResult.result; - call.isError = event.toolResult.isError; - return; - } - } - // Orphan result with no matching call — drop silently. - return; - } - - case "shell-output": { - // Append to the most recent entry of the most recent tool-batch. - // Walk back through chunks to find the latest tool-batch; if there - // are intervening text/thinking/etc chunks (which can happen if - // the model streams text while a shell tool is still running), - // we still want the most recent tool-batch. - for (let i = chunks.length - 1; i >= 0; i--) { - const c = chunks[i]; - if (!c || c.type !== "tool-batch") continue; - const entry = c.calls[c.calls.length - 1]; - if (!entry) return; - const prev = entry.shellOutput ?? { stdout: "", stderr: "" }; - entry.shellOutput = { - stdout: prev.stdout + (event.stream === "stdout" ? event.data : ""), - stderr: prev.stderr + (event.stream === "stderr" ? event.data : ""), - }; - return; - } - // Orphan shell-output with no tool-batch in scope — drop silently. - return; - } - - case "error": { - // Always a fresh single-event chunk — no coalescing. - chunks.push({ - type: "error", - message: event.error, - ...(event.statusCode !== undefined ? { statusCode: event.statusCode } : {}), - }); - return; - } - - case "notice": { - chunks.push({ type: "system", kind: "notice", text: event.message }); - return; - } - - case "model-changed": { - chunks.push({ - type: "system", - kind: "model-changed", - text: `Switched to ${event.modelId} (${event.keyId})`, - }); - return; - } - - case "config-reload": { - chunks.push({ - type: "system", - kind: "config-reload", - text: "Configuration reloaded", - }); - return; - } - - // Lifecycle / control events — no chunk emitted. - case "status": - case "turn-start": - case "turn-sealed": - case "done": - case "usage": - case "task-list-update": - case "tab-created": - case "message-queued": - case "message-consumed": - case "message-cancelled": - case "compaction-started": - case "compaction-complete": - case "compaction-error": - return; - - default: { - // Exhaustiveness check — if a new event variant is added to - // AgentEvent, TypeScript will complain here. - const _exhaustive: never = event; - void _exhaustive; - return; - } - } -} - -// ─── System event routing across messages ──────────────────────── - -/** - * Minimal shape needed by `applySystemEvent`. - * - * The caller (agent-manager / persistence layer) typically tracks message - * id alongside the wire-format `ChatMessage`. This generic constraint - * lets us keep core `ChatMessage` clean while still letting downstream - * pass anything with an `id`. - */ -export interface IdentifiedMessage { - id: string; - role: MessageRole; - chunks: Chunk[]; -} - -/** - * Describes the system event in caller-controlled terms. We let the caller - * decide both the `kind` (so the same helper can record cancellations, - * notices, model swaps, etc.) and the `text` (so the caller controls - * formatting / localization). - */ -export interface SystemEventLike { - kind: SystemChunkKind; - text: string; -} - -/** - * Routes a system event to the right message when *no assistant turn is - * in flight*. (When a turn IS in flight, the caller should instead use - * `appendEventToChunks` against the in-flight message's chunks directly.) - * - * Routing rules (from notes/plan-chunk-refactor.md): - * - * 1. Most recent message is `role: "system"` → append a `system` chunk - * to it. (Note: a second consecutive system event creates a second - * system chunk inside the same system message — chunks themselves - * never coalesce.) - * 2. Otherwise → create a fresh `role: "system"` message containing one - * `system` chunk and push it. - * - * Returns the `messageId` that was used (either the existing system - * message's id or the newly-created one) so the caller can persist / - * emit a diff to subscribers. - * - * `idFactory` defaults to `crypto.randomUUID()`; tests inject a - * deterministic factory. - */ -export function applySystemEvent<M extends IdentifiedMessage>( - messages: M[], - event: SystemEventLike, - idFactory: () => string = defaultIdFactory, -): { messageId: string } { - const chunk: SystemChunk = { type: "system", kind: event.kind, text: event.text }; - - const last = messages[messages.length - 1]; - if (last && last.role === "system") { - last.chunks.push(chunk); - return { messageId: last.id }; - } - - const id = idFactory(); - // We can't fabricate the full `M` shape without knowing its extra - // fields, but `IdentifiedMessage` is the minimum we need to push. - // Callers that extend the shape with extra fields are responsible for - // initializing them via post-hoc patching, or by passing in their own - // message-creation logic. In practice callers either: - // (a) use `ChatMessage` itself (no extra fields beyond IdentifiedMessage), or - // (b) construct messages and look them up by id after this call returns. - const newMessage = { id, role: "system" as const, chunks: [chunk] } as unknown as M; - messages.push(newMessage); - return { messageId: id }; -} - -function defaultIdFactory(): string { - // In Node 19+ / modern browsers, `crypto.randomUUID` is available globally. - if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") { - return crypto.randomUUID(); - } - // Fallback: pseudo-random; not cryptographically secure, but adequate for - // in-memory message identifiers when randomUUID is unavailable. - return `sysmsg-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`; -} - -// ─── Re-exports for convenience ────────────────────────────────── - -export type { ChatMessage, Chunk, SystemChunk, SystemChunkKind, ToolBatchChunk }; diff --git a/packages/core/src/chunks/transform.ts b/packages/core/src/chunks/transform.ts deleted file mode 100644 index e8f4a18..0000000 --- a/packages/core/src/chunks/transform.ts +++ /dev/null @@ -1,271 +0,0 @@ -// Pure, dependency-free transforms between the render-shaped `Chunk[]` / -// `ChatMessage` model and the flat append-only `ChunkRow` log. Kept free of any -// DB (`bun:sqlite`) import so BOTH the backend persistence layer -// (`db/chunks.ts`) and the browser frontend store can share the exact same -// explode/group logic. - -import type { - Chunk, - ChunkRow, - ChunkRowDraft, - ErrorData, - MessageRole, - SystemData, - TextData, - ThinkingData, - ToolBatchChunk, - ToolCallData, - ToolResultData, -} from "../types/index.js"; - -/** - * A DERIVED message — a grouping of contiguous chunk rows reconstructed for the - * agent's in-memory history and for the frontend's render bubbles. NOT a stored - * shape: the source of truth is the flat chunk log. - */ -export interface MessageRow { - id: string; - tabId: string; - seq: number; - /** turn_id of the chunk rows this message was grouped from. */ - turnId: string; - role: MessageRole; - chunks: Chunk[]; - createdAt: number; -} - -// ─── Explode: in-memory turn → flat chunk-row drafts ───────────── -// -// A turn's render-shaped `Chunk[]` is flattened into append-only rows. -// `tool-batch` chunks are split into SEPARATE `tool_call` (role=assistant) and -// `tool_result` (role=tool) rows linked by `callId`, mapping 1:1 to the -// Anthropic wire format. `step` increments after each tool-batch: every LLM -// round-trip emits text/thinking then (optionally) one tool-batch, so a -// tool-batch boundary is exactly a step boundary. - -/** Explode a single user message's text into one row draft. */ -export function explodeUserText(turnId: string, text: string): ChunkRowDraft[] { - return [{ turnId, step: 0, role: "user", type: "text", data: { text } }]; -} - -/** Explode an assistant turn's accumulated chunks into ordered row drafts. */ -export function explodeTurn(turnId: string, chunks: Chunk[]): ChunkRowDraft[] { - const drafts: ChunkRowDraft[] = []; - let step = 0; - for (const chunk of chunks) { - switch (chunk.type) { - case "text": - drafts.push({ turnId, step, role: "assistant", type: "text", data: { text: chunk.text } }); - break; - case "thinking": - drafts.push({ - turnId, - step, - role: "assistant", - type: "thinking", - data: { - text: chunk.text, - ...(chunk.metadata !== undefined ? { metadata: chunk.metadata } : {}), - }, - }); - break; - case "tool-batch": { - for (const call of chunk.calls) { - drafts.push({ - turnId, - step, - role: "assistant", - type: "tool_call", - data: { callId: call.id, name: call.name, arguments: call.arguments }, - }); - } - for (const call of chunk.calls) { - if (call.result === undefined) continue; - drafts.push({ - turnId, - step, - role: "tool", - type: "tool_result", - data: { - callId: call.id, - name: call.name, - result: call.result, - isError: call.isError ?? false, - ...(call.shellOutput !== undefined ? { shellOutput: call.shellOutput } : {}), - }, - }); - } - // A tool-batch ends the current LLM step; subsequent chunks belong - // to the next round-trip. - step++; - break; - } - case "error": - drafts.push({ - turnId, - step, - role: "assistant", - type: "error", - data: { - message: chunk.message, - ...(chunk.statusCode !== undefined ? { statusCode: chunk.statusCode } : {}), - }, - }); - break; - case "system": - drafts.push({ - turnId, - step, - role: "system", - type: "system", - data: { kind: chunk.kind, text: chunk.text }, - }); - break; - } - } - return drafts; -} - -// ─── Group: flat chunk rows → derived render messages ──────────── -// -// The inverse of explode (best-effort over an arbitrary window, so it tolerates -// orphan tool-results whose tool-call was paged out). `tool_result` rows -// (role=tool) merge back into the preceding assistant message's per-step -// `tool-batch` chunk by `callId` rather than forming their own message. - -export function groupRowsToMessages(rows: ChunkRow[]): MessageRow[] { - const messages: MessageRow[] = []; - - let current: { msg: MessageRow; batches: Map<number, ToolBatchChunk> } | null = null; - const flush = () => { - if (current) { - messages.push(current.msg); - current = null; - } - }; - const ensureAssistant = (row: ChunkRow) => { - if (!current) { - current = { - msg: { - id: row.id, - tabId: row.tabId, - seq: row.seq, - turnId: row.turnId, - role: "assistant", - chunks: [], - createdAt: row.createdAt, - }, - batches: new Map(), - }; - } - return current; - }; - const ensureBatch = (step: number): ToolBatchChunk => { - const c = current; - if (!c) throw new Error("ensureBatch called without an assistant message"); - let batch = c.batches.get(step); - if (!batch) { - batch = { type: "tool-batch", calls: [] }; - c.batches.set(step, batch); - c.msg.chunks.push(batch); - } - return batch; - }; - - for (const row of rows) { - if (row.role === "user") { - flush(); - const d = row.data as TextData; - messages.push({ - id: row.id, - tabId: row.tabId, - seq: row.seq, - turnId: row.turnId, - role: "user", - chunks: [{ type: "text", text: d.text }], - createdAt: row.createdAt, - }); - continue; - } - if (row.role === "system") { - // Coalesce consecutive system rows into one system message (multiple - // system chunks), matching the old applySystemEvent behaviour. - const prev = messages[messages.length - 1]; - const d = row.data as SystemData; - if (current === null && prev && prev.role === "system") { - prev.chunks.push({ type: "system", kind: d.kind, text: d.text }); - continue; - } - flush(); - messages.push({ - id: row.id, - tabId: row.tabId, - seq: row.seq, - turnId: row.turnId, - role: "system", - chunks: [{ type: "system", kind: d.kind, text: d.text }], - createdAt: row.createdAt, - }); - continue; - } - - // Usage rows are an invisible side channel (persisted for the backend - // aggregate only). They're already query-excluded from getChunksForTab, - // so this is defensive insurance: never let one leak into render grouping. - if (row.type === "usage") continue; - - // assistant / tool rows → part of the current assistant message - const c = ensureAssistant(row); - switch (row.type) { - case "text": - c.msg.chunks.push({ type: "text", text: (row.data as TextData).text }); - break; - case "thinking": { - const d = row.data as ThinkingData; - c.msg.chunks.push({ - type: "thinking", - text: d.text, - ...(d.metadata !== undefined ? { metadata: d.metadata } : {}), - }); - break; - } - case "error": { - const d = row.data as ErrorData; - c.msg.chunks.push({ - type: "error", - message: d.message, - ...(d.statusCode !== undefined ? { statusCode: d.statusCode } : {}), - }); - break; - } - case "tool_call": { - const d = row.data as ToolCallData; - ensureBatch(row.step).calls.push({ id: d.callId, name: d.name, arguments: d.arguments }); - break; - } - case "tool_result": { - const d = row.data as ToolResultData; - const batch = ensureBatch(row.step); - const entry = batch.calls.find((e) => e.id === d.callId); - if (entry) { - entry.result = d.result; - entry.isError = d.isError; - if (d.shellOutput !== undefined) entry.shellOutput = d.shellOutput; - } else { - // Orphan result (its tool_call was paged out of this window). - batch.calls.push({ - id: d.callId, - name: d.name, - arguments: {}, - result: d.result, - isError: d.isError, - ...(d.shellOutput !== undefined ? { shellOutput: d.shellOutput } : {}), - }); - } - break; - } - } - } - flush(); - return messages; -} diff --git a/packages/core/src/compaction/index.ts b/packages/core/src/compaction/index.ts deleted file mode 100644 index 663ccb2..0000000 --- a/packages/core/src/compaction/index.ts +++ /dev/null @@ -1,245 +0,0 @@ -// Conversation compaction — summarize the older "head" of a conversation into -// a structured anchor while preserving the most recent turns verbatim. -// -// Ported from opencode's `session/compaction.ts` (SUMMARY_TEMPLATE, the -// anchored-summary `buildPrompt`, and the `TOOL_OUTPUT_MAX_CHARS` cap). The -// turn-budget `splitTurn` tail selection is intentionally simplified to a fixed -// recent-turn count (`DEFAULT_TAIL_TURNS`) — Dispatch keeps the last N turns -// verbatim rather than computing a token budget. -// -// This module is pure and DB-free so it can be unit-tested in isolation and -// shared by the API orchestrator. - -import type { ChatMessage } from "../types/index.js"; - -/** Number of trailing turns kept verbatim (opencode's DEFAULT_TAIL_TURNS). */ -export const DEFAULT_TAIL_TURNS = 2; - -/** Max characters of a single tool result fed into the summary request. */ -export const TOOL_OUTPUT_MAX_CHARS = 2_000; - -/** - * Marker prefixing the seeded summary turn in a compacted conversation. Lets a - * subsequent compaction detect a prior summary and re-summarize (anchor) it - * instead of treating it as ordinary conversation. - */ -export const SUMMARY_MARKER = "[CONVERSATION SUMMARY]"; - -/** - * Structured Markdown template the summary must follow. Ported verbatim from - * opencode's `SUMMARY_TEMPLATE`. - */ -export const SUMMARY_TEMPLATE = `Output exactly the Markdown structure shown inside <template> and keep the section order unchanged. Do not include the <template> tags in your response. -<template> -## Goal -- [single-sentence task summary] - -## Constraints & Preferences -- [user constraints, preferences, specs, or "(none)"] - -## Progress -### Done -- [completed work or "(none)"] - -### In Progress -- [current work or "(none)"] - -### Blocked -- [blockers or "(none)"] - -## Key Decisions -- [decision and why, or "(none)"] - -## Next Steps -- [ordered next actions or "(none)"] - -## Critical Context -- [important technical facts, errors, open questions, or "(none)"] - -## Relevant Files -- [file or directory path: why it matters, or "(none)"] -</template> - -Rules: -- Keep every section, even when empty. -- Use terse bullets, not prose paragraphs. -- Preserve exact file paths, commands, error strings, and identifiers when known. -- Do not mention the summary process or that context was compacted.`; - -/** - * Build the compaction instruction. When `previousSummary` is provided, the - * model is asked to UPDATE the anchored summary rather than create a fresh one - * (opencode's `buildPrompt` anchor behaviour). - */ -export function buildCompactionPrompt(input: { previousSummary?: string }): string { - const anchor = input.previousSummary - ? [ - "Update the anchored summary below using the conversation history above.", - "Preserve still-true details, remove stale details, and merge in the new facts.", - "<previous-summary>", - input.previousSummary, - "</previous-summary>", - ].join("\n") - : "Create a new anchored summary from the conversation history above."; - return `${anchor}\n\n${SUMMARY_TEMPLATE}`; -} - -/** - * The first text chunk of a message, trimmed (empty → undefined). Used to read - * the seeded summary out of a compacted conversation's first user turn. - */ -function firstText(message: ChatMessage): string | undefined { - for (const chunk of message.chunks) { - if (chunk.type === "text") { - const t = chunk.text.trim(); - if (t) return t; - } - } - return undefined; -} - -/** - * Extract a prior summary from the conversation head. If the first user message - * is a seeded summary (starts with {@link SUMMARY_MARKER}), return its body - * (marker stripped) so the next compaction can anchor on it. - */ -export function extractPreviousSummary(messages: ChatMessage[]): string | undefined { - const first = messages.find((m) => m.role === "user"); - if (!first) return undefined; - const text = firstText(first); - if (!text?.startsWith(SUMMARY_MARKER)) return undefined; - const body = text.slice(SUMMARY_MARKER.length).trim(); - return body || undefined; -} - -export interface HeadTailSelection<T extends ChatMessage = ChatMessage> { - /** Older messages to be summarized away. */ - head: T[]; - /** Recent messages preserved verbatim in the continuation. */ - tail: T[]; -} - -/** - * Split a conversation into a summarizable `head` and a preserved `tail` of the - * last `tailTurns` turns. A "turn" begins at a user message and runs until the - * next user message. - * - * When the conversation has `tailTurns` or fewer turns, `head` is empty: there - * is nothing to compact (the caller should refuse). - */ -export function selectHeadTail<T extends ChatMessage>( - messages: T[], - tailTurns: number = DEFAULT_TAIL_TURNS, -): HeadTailSelection<T> { - if (tailTurns <= 0) return { head: messages, tail: [] }; - const userIndices: number[] = []; - for (let i = 0; i < messages.length; i++) { - if (messages[i]?.role === "user") userIndices.push(i); - } - if (userIndices.length <= tailTurns) return { head: [], tail: messages }; - const tailStart = userIndices[userIndices.length - tailTurns]; - if (tailStart === undefined || tailStart <= 0) return { head: [], tail: messages }; - return { head: messages.slice(0, tailStart), tail: messages.slice(tailStart) }; -} - -/** Cap a tool result to `max` chars with a truncation marker. */ -function capToolOutput(result: string, max: number): string { - if (result.length <= max) return result; - const omitted = result.length - max; - return `${result.slice(0, max)}\n…[${omitted} chars truncated for summary]`; -} - -/** - * Render conversation messages into a compact, provider-agnostic plain-text - * transcript suitable as summary-request context. Tool results are capped at - * `toolOutputMaxChars` (opencode's `TOOL_OUTPUT_MAX_CHARS`), and a seeded prior - * summary message is skipped (its content is carried by the prompt anchor). - * Thinking/error/system chunks are omitted as summary noise. - */ -export function renderTranscript( - messages: ChatMessage[], - toolOutputMaxChars: number = TOOL_OUTPUT_MAX_CHARS, -): string { - const blocks: string[] = []; - for (const message of messages) { - // Skip a seeded prior-summary user turn — it's represented via the anchor. - if (message.role === "user") { - const t = firstText(message); - if (t?.startsWith(SUMMARY_MARKER)) continue; - } - - const lines: string[] = []; - for (const chunk of message.chunks) { - if (chunk.type === "text") { - const t = chunk.text.trim(); - if (t) lines.push(t); - } else if (chunk.type === "tool-batch") { - for (const call of chunk.calls) { - let args = ""; - try { - args = JSON.stringify(call.arguments ?? {}); - } catch { - args = "{}"; - } - lines.push(`[tool ${call.name} ${args}]`); - if (call.result !== undefined) { - const tag = call.isError ? "tool-error" : "tool-result"; - lines.push(`[${tag}] ${capToolOutput(call.result, toolOutputMaxChars)}`); - } - } - } - } - if (lines.length === 0) continue; - const role = - message.role === "user" ? "User" : message.role === "assistant" ? "Assistant" : "System"; - blocks.push(`## ${role}\n${lines.join("\n")}`); - } - return blocks.join("\n\n"); -} - -export interface CompactionRequest<T extends ChatMessage = ChatMessage> { - /** Messages selected for summarization (older head). */ - head: T[]; - /** Recent messages preserved verbatim (last N turns). */ - tail: T[]; - /** Prior summary anchored on, if the conversation was compacted before. */ - previousSummary?: string; - /** - * The full user-message content for the summary request: rendered head - * transcript followed by the compaction prompt/template. `undefined` when - * there is nothing to compact (`head` empty). - */ - prompt?: string; -} - -/** - * Assemble everything needed to run a compaction: head/tail split, prior-summary - * extraction, and the combined summary-request prompt. Returns `prompt: - * undefined` when the conversation is too short to compact. - */ -export function buildCompactionRequest<T extends ChatMessage>(input: { - messages: T[]; - tailTurns?: number; - toolOutputMaxChars?: number; -}): CompactionRequest<T> { - const tailTurns = input.tailTurns ?? DEFAULT_TAIL_TURNS; - const toolMax = input.toolOutputMaxChars ?? TOOL_OUTPUT_MAX_CHARS; - const { head, tail } = selectHeadTail(input.messages, tailTurns); - const previousSummary = extractPreviousSummary(input.messages); - if (head.length === 0) { - return { head, tail, previousSummary }; - } - const transcript = renderTranscript(head, toolMax); - const instruction = buildCompactionPrompt({ previousSummary }); - const prompt = `${transcript}\n\n${instruction}`; - return { head, tail, previousSummary, prompt }; -} - -/** - * Wrap a generated summary as the seeded user-turn text for the continuation - * conversation. Prefixed with {@link SUMMARY_MARKER} so a later compaction can - * anchor on it. - */ -export function buildSummaryTurnText(summary: string): string { - return `${SUMMARY_MARKER}\n\n${summary.trim()}`; -} diff --git a/packages/core/src/config/index.ts b/packages/core/src/config/index.ts deleted file mode 100644 index 7f76dd7..0000000 --- a/packages/core/src/config/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -export { - configToRuleset, - getGlobalConfigPath, - loadConfig, - loadGlobalConfig, - mergeConfigs, -} from "./loader.js"; -export { validateConfig } from "./schema.js"; -export { createConfigWatcher, watchDirConfig } from "./watcher.js"; diff --git a/packages/core/src/config/loader.ts b/packages/core/src/config/loader.ts deleted file mode 100644 index 66f798b..0000000 --- a/packages/core/src/config/loader.ts +++ /dev/null @@ -1,226 +0,0 @@ -import { readFileSync } from "node:fs"; -import { homedir } from "node:os"; -import { join } from "node:path"; -import { parse } from "smol-toml"; -import type { PermissionRule, Ruleset } from "../permission/index.js"; -import type { DispatchConfig, KeyDefinition, LspServerConfig } from "../types/index.js"; -import { validateConfig } from "./schema.js"; - -const DEFAULT_CONFIG: DispatchConfig = { permissions: {} }; - -const VALID_ACTIONS = new Set(["allow", "deny", "ask"]); - -function validateAction(raw: string): "allow" | "deny" | "ask" { - if (VALID_ACTIONS.has(raw)) return raw as "allow" | "deny" | "ask"; - console.warn(`dispatch: unrecognized action "${raw}", defaulting to "ask"`); - return "ask"; -} - -/** - * Absolute path to the HOME-directory (global) `dispatch.toml`. - * - * Follows the same `~/.config/dispatch/` convention as global agents - * (`~/.config/dispatch/agents`). This file is OPTIONAL; when present its - * contents are merged underneath every project/working-directory config so - * machine-wide settings (e.g. globally available LSP servers) work in any - * repository without per-repo configuration. - * - * The path can be overridden with the `DISPATCH_GLOBAL_CONFIG` environment - * variable, which is primarily useful for tests (point it at a temp file) but - * also lets a user relocate the global config. - */ -export function getGlobalConfigPath(): string { - return ( - process.env.DISPATCH_GLOBAL_CONFIG ?? join(homedir(), ".config", "dispatch", "dispatch.toml") - ); -} - -// Parse + validate a single dispatch.toml. Returns null when the file does not -// exist. Re-throws TOML parse errors so a corrupt LOCAL config surfaces loudly -// (callers that must stay resilient, e.g. the global loader, catch it). -function readConfigFile(tomlPath: string): DispatchConfig | null { - let raw: unknown; - try { - const content = readFileSync(tomlPath, "utf-8"); - raw = parse(content); - } catch (err: unknown) { - if (err instanceof Error && (err as NodeJS.ErrnoException).code === "ENOENT") { - // File doesn't exist — signal "no config here". - return null; - } - console.warn( - `dispatch: failed to parse ${tomlPath}: ${err instanceof Error ? err.message : String(err)}`, - ); - throw err; - } - - const { config, errors } = validateConfig(raw); - for (const e of errors) { - console.warn(`dispatch: config warning at ${e.path}: ${e.message}`); - } - return config; -} - -/** - * Load the HOME-directory global `dispatch.toml` (see {@link getGlobalConfigPath}). - * - * Always resilient: a missing file yields the empty default and a malformed - * file is logged but downgraded to the empty default rather than thrown. A - * broken global config must never break config loading for every repository on - * the machine. - */ -export function loadGlobalConfig(): DispatchConfig { - try { - return readConfigFile(getGlobalConfigPath()) ?? DEFAULT_CONFIG; - } catch (err) { - console.warn( - `dispatch: ignoring global config due to parse error: ${err instanceof Error ? err.message : String(err)}`, - ); - return DEFAULT_CONFIG; - } -} - -/** - * Load the effective config for `dir`: the global config MERGED with the - * project/working-directory `dispatch.toml`, where the LOCAL config takes - * precedence on conflicts (see {@link mergeConfigs}). A missing local file - * yields the global config as-is; a missing global file yields the local - * config as-is. - * - * Note: a malformed LOCAL config still throws (callers may surface it), while a - * malformed GLOBAL config is downgraded to empty by {@link loadGlobalConfig}. - */ -export function loadConfig(dir: string): DispatchConfig { - const global = loadGlobalConfig(); - const local = readConfigFile(join(dir, "dispatch.toml")); - if (local === null) return global; - return mergeConfigs(global, local); -} - -// ─── Merge ─────────────────────────────────────────────────────── - -/** - * Merge two permission blocks. Local takes precedence on conflicts. - * - * - A key present only in one side is carried over verbatim. - * - A key whose value is a string on either side: local replaces global. - * - A key that is a nested `{ pattern -> action }` object on BOTH sides is - * merged pattern-by-pattern: global patterns the local block does NOT also - * define come first (original order), then EVERY local pattern is appended - * last (overriding any same-named global pattern). - * - * Emitting all local patterns after the global ones is essential, not - * cosmetic: `configToRuleset` flattens patterns in iteration order and - * `evaluate` uses `findLast` (last match wins). If an overridden pattern were - * updated in place, a more-general global pattern (e.g. "*") could remain AFTER - * it and silently shadow the local override. Appending local patterns last - * reproduces a clean "global rules then local rules" concatenation so local - * always wins. - */ -function mergePermissions( - global: DispatchConfig["permissions"], - local: DispatchConfig["permissions"], -): DispatchConfig["permissions"] { - const result: DispatchConfig["permissions"] = {}; - for (const [key, value] of Object.entries(global)) { - result[key] = value; - } - for (const [key, value] of Object.entries(local)) { - const existing = result[key]; - if (existing !== undefined && typeof existing !== "string" && typeof value !== "string") { - // Both nested objects — merge patterns so that ALL local patterns - // are emitted AFTER the global ones. This matters because - // `configToRuleset` flattens patterns in insertion order and - // `evaluate` uses `findLast` (last match wins): a naive - // `{ ...existing, ...value }` would update an overridden pattern - // IN PLACE, leaving a more-general global pattern (e.g. "*") sitting - // AFTER it and silently shadowing the local override. We therefore - // drop any global pattern that the local block also defines, keep the - // remaining global patterns in their original order, then append every - // local pattern last — reproducing a clean "global rules then local - // rules" concatenation where local always wins. - const merged: Record<string, string> = {}; - for (const [pattern, action] of Object.entries(existing)) { - if (!(pattern in value)) merged[pattern] = action; - } - for (const [pattern, action] of Object.entries(value)) { - merged[pattern] = action; - } - result[key] = merged; - } else { - // Local string, brand-new key, or a string/object type mismatch: - // local replaces global wholesale. - result[key] = value; - } - } - return result; -} - -/** - * Merge two key lists by `id`. Local keys override global keys sharing the same - * id; non-conflicting ids from both lists survive. Global keys keep their - * relative order (overridden in place) followed by local-only keys. - */ -function mergeKeys(global: KeyDefinition[], local: KeyDefinition[]): KeyDefinition[] { - const byId = new Map<string, KeyDefinition>(); - for (const key of global) byId.set(key.id, key); - for (const key of local) byId.set(key.id, key); - return Array.from(byId.values()); -} - -/** - * Merge two `[lsp]` blocks by server id. Local servers override global servers - * sharing the same id; non-conflicting ids from both sides remain active. This - * is what lets a global config provide LSP servers to every repository while a - * project can still override or add its own. - */ -function mergeLsp( - global: Record<string, LspServerConfig>, - local: Record<string, LspServerConfig>, -): Record<string, LspServerConfig> { - return { ...global, ...local }; -} - -/** - * Deep-merge a `global` config with a `local` (project/working-directory) - * config, with LOCAL taking precedence on every conflict. Pure function — does - * not touch the filesystem and never mutates its inputs. - */ -export function mergeConfigs(global: DispatchConfig, local: DispatchConfig): DispatchConfig { - const merged: DispatchConfig = { - permissions: mergePermissions(global.permissions, local.permissions), - }; - - if (global.keys !== undefined || local.keys !== undefined) { - merged.keys = mergeKeys(global.keys ?? [], local.keys ?? []); - } - - if (global.lsp !== undefined || local.lsp !== undefined) { - merged.lsp = mergeLsp(global.lsp ?? {}, local.lsp ?? {}); - } - - return merged; -} - -// Convert the config's permission block to a Ruleset -export function configToRuleset(config: DispatchConfig): Ruleset { - const home = homedir(); - const rules: PermissionRule[] = []; - - for (const [permission, value] of Object.entries(config.permissions)) { - if (typeof value === "string") { - const action = validateAction(value); - rules.push({ permission, pattern: "*", action }); - } else { - for (const [rawPattern, rawAction] of Object.entries(value)) { - const pattern = rawPattern - .replace(/^\$HOME(?=[/\\]|$)/, home) - .replace(/^~(?=[/\\]|$)/, home); - const action = validateAction(rawAction); - rules.push({ permission, pattern, action }); - } - } - } - - return rules; -} diff --git a/packages/core/src/config/schema.ts b/packages/core/src/config/schema.ts deleted file mode 100644 index 304ee10..0000000 --- a/packages/core/src/config/schema.ts +++ /dev/null @@ -1,239 +0,0 @@ -import type { - ConfigError, - DispatchConfig, - KeyDefinition, - LspServerConfig, -} from "../types/index.js"; - -function isRecord(value: unknown): value is Record<string, unknown> { - return typeof value === "object" && value !== null && !Array.isArray(value); -} - -function isStringRecord(value: unknown): value is Record<string, string> { - if (!isRecord(value)) return false; - return Object.values(value).every((v) => typeof v === "string"); -} - -function isValidAction(value: string): boolean { - return value === "allow" || value === "deny" || value === "ask"; -} - -function isPermissionsValue(value: unknown): value is string | Record<string, string> { - return typeof value === "string" || isStringRecord(value); -} - -function validatePermissions( - raw: unknown, - path: string, - errors: ConfigError[], -): Record<string, string | Record<string, string>> { - if (!isRecord(raw)) { - errors.push({ path, message: "must be an object" }); - return {}; - } - const result: Record<string, string | Record<string, string>> = {}; - for (const [key, value] of Object.entries(raw)) { - if (!isPermissionsValue(value)) { - errors.push({ - path: `${path}.${key}`, - message: "must be a string or a flat string-keyed object", - }); - continue; - } - if (typeof value === "string") { - if (!isValidAction(value)) { - errors.push({ - path: `${path}.${key}`, - message: `invalid action "${value}"; must be "allow", "deny", or "ask"`, - }); - continue; - } - } else { - let hasError = false; - for (const [pattern, action] of Object.entries(value)) { - if (!isValidAction(action)) { - errors.push({ - path: `${path}.${key}.${pattern}`, - message: `invalid action "${action}"; must be "allow", "deny", or "ask"`, - }); - hasError = true; - } - } - if (hasError) continue; - } - result[key] = value; - } - return result; -} - -function validateKey(raw: unknown, path: string, errors: ConfigError[]): KeyDefinition | null { - if (!isRecord(raw)) { - errors.push({ path, message: "must be an object" }); - return null; - } - if (typeof raw.id !== "string") { - errors.push({ path: `${path}.id`, message: "must be a string" }); - return null; - } - if (typeof raw.provider !== "string") { - errors.push({ path: `${path}.provider`, message: "must be a string" }); - return null; - } - if (typeof raw.base_url !== "string") { - errors.push({ path: `${path}.base_url`, message: "must be a string" }); - return null; - } - - // "anthropic" provider uses credentials_file instead of env - if (raw.provider === "anthropic") { - return { - id: raw.id as string, - provider: raw.provider as string, - base_url: raw.base_url as string, - ...(typeof raw.credentials_file === "string" - ? ({ credentials_file: raw.credentials_file } as Pick<KeyDefinition, "credentials_file">) - : {}), - }; - } - - // Other providers: env is optional (keys can be stored in DB) - return { - id: raw.id as string, - provider: raw.provider as string, - base_url: raw.base_url as string, - ...(typeof raw.env === "string" ? { env: raw.env } : {}), - }; -} - -function isStringArray(value: unknown): value is string[] { - return Array.isArray(value) && value.every((v) => typeof v === "string"); -} - -function validateLspServer( - raw: unknown, - path: string, - errors: ConfigError[], -): LspServerConfig | null { - if (!isRecord(raw)) { - errors.push({ path, message: "must be an object" }); - return null; - } - - const disabled = raw.disabled === true; - - // `command` is required and must be a non-empty string array unless the - // entry is explicitly disabled (a disabled entry is skipped wholesale). - if (!disabled) { - if (!isStringArray(raw.command) || raw.command.length === 0) { - errors.push({ - path: `${path}.command`, - message: "must be a non-empty array of strings", - }); - return null; - } - // `extensions` is required for custom servers — without it the client - // cannot know which files should activate the server. - if (!isStringArray(raw.extensions) || raw.extensions.length === 0) { - errors.push({ - path: `${path}.extensions`, - message: 'must be a non-empty array of strings (e.g. [".luau"])', - }); - return null; - } - } else { - // Disabled entries still must not carry a malformed command/extensions - // if present, but we do not require them. - if (raw.command !== undefined && !isStringArray(raw.command)) { - errors.push({ path: `${path}.command`, message: "must be an array of strings" }); - return null; - } - if (raw.extensions !== undefined && !isStringArray(raw.extensions)) { - errors.push({ path: `${path}.extensions`, message: "must be an array of strings" }); - return null; - } - } - - if (raw.env !== undefined && !isStringRecord(raw.env)) { - errors.push({ - path: `${path}.env`, - message: "must be a flat string-keyed object", - }); - return null; - } - - if (raw.initialization !== undefined && !isRecord(raw.initialization)) { - errors.push({ - path: `${path}.initialization`, - message: "must be an object", - }); - return null; - } - - const server: LspServerConfig = { - command: (raw.command as string[] | undefined) ?? [], - extensions: (raw.extensions as string[] | undefined) ?? [], - ...(isStringRecord(raw.env) ? { env: raw.env } : {}), - ...(isRecord(raw.initialization) - ? { initialization: raw.initialization as Record<string, unknown> } - : {}), - ...(disabled ? { disabled: true } : {}), - }; - return server; -} - -function validateLsp( - raw: unknown, - path: string, - errors: ConfigError[], -): Record<string, LspServerConfig> | undefined { - if (!isRecord(raw)) { - errors.push({ path, message: "must be an object" }); - return undefined; - } - const result: Record<string, LspServerConfig> = {}; - for (const [id, value] of Object.entries(raw)) { - const server = validateLspServer(value, `${path}.${id}`, errors); - if (server) result[id] = server; - } - return Object.keys(result).length > 0 ? result : undefined; -} - -export function validateConfig(raw: unknown): { config: DispatchConfig; errors: ConfigError[] } { - const errors: ConfigError[] = []; - - if (!isRecord(raw)) { - errors.push({ path: "", message: "config must be an object" }); - return { config: { permissions: {} }, errors }; - } - - // permissions (required, but can be empty) - const permissions = validatePermissions(raw.permissions ?? {}, "permissions", errors); - - // keys (optional) - let keys: KeyDefinition[] | undefined; - if (raw.keys !== undefined) { - if (!Array.isArray(raw.keys)) { - errors.push({ path: "keys", message: "must be an array" }); - } else { - keys = []; - for (let i = 0; i < raw.keys.length; i++) { - const key = validateKey(raw.keys[i], `keys[${i}]`, errors); - if (key) keys.push(key); - } - } - } - - // lsp (optional) - let lsp: Record<string, LspServerConfig> | undefined; - if (raw.lsp !== undefined) { - lsp = validateLsp(raw.lsp, "lsp", errors); - } - - const config: DispatchConfig = { - permissions, - ...(keys !== undefined && { keys }), - ...(lsp !== undefined && { lsp }), - }; - - return { config, errors }; -} diff --git a/packages/core/src/config/watcher.ts b/packages/core/src/config/watcher.ts deleted file mode 100644 index ad55804..0000000 --- a/packages/core/src/config/watcher.ts +++ /dev/null @@ -1,129 +0,0 @@ -import { join } from "node:path"; -import { watch } from "chokidar"; -import type { DispatchConfig } from "../types/index.js"; -import { getGlobalConfigPath, loadConfig } from "./loader.js"; - -/** - * Watch BOTH the HOME-directory global `dispatch.toml` and the project/working- - * directory `dispatch.toml`. Either file changing triggers a reload that - * re-merges global + local (via {@link loadConfig}), so hot-reload works for - * global defaults and per-project overrides alike. - * - * When the global and local paths coincide (e.g. the working directory IS - * `~/.config/dispatch`, or `DISPATCH_GLOBAL_CONFIG` points at the local file) - * the duplicate is collapsed so chokidar only watches it once. - */ -export function createConfigWatcher( - dir: string, - onChange: (config: DispatchConfig) => void, -): { close(): void } { - const localPath = join(dir, "dispatch.toml"); - const globalPath = getGlobalConfigPath(); - const paths = globalPath === localPath ? [localPath] : [globalPath, localPath]; - let debounceTimer: ReturnType<typeof setTimeout> | null = null; - - const watcher = watch(paths, { - ignoreInitial: true, - persistent: false, - }); - - const handleChange = () => { - if (debounceTimer !== null) { - clearTimeout(debounceTimer); - } - debounceTimer = setTimeout(() => { - debounceTimer = null; - console.log(`dispatch: reloading config (global + ${localPath})`); - try { - const config = loadConfig(dir); - onChange(config); - } catch (err) { - console.warn( - `dispatch: retaining last known config due to parse error: ${err instanceof Error ? err.message : String(err)}`, - ); - } - }, 300); - }; - - watcher.on("change", handleChange); - watcher.on("add", handleChange); - watcher.on("unlink", handleChange); - - watcher.on("error", (err) => { - console.warn( - `dispatch: config watcher error: ${err instanceof Error ? err.message : String(err)}`, - ); - }); - - return { - close() { - if (debounceTimer !== null) { - clearTimeout(debounceTimer); - debounceTimer = null; - } - watcher.close().catch((err) => { - console.warn( - `dispatch: error closing config watcher: ${err instanceof Error ? err.message : String(err)}`, - ); - }); - }, - }; -} - -/** - * Watch a SINGLE directory's `dispatch.toml` (no global merge, no reload — just - * a debounced change signal). Used by the agent manager to invalidate its - * per-directory LSP cache when a tab's effective working directory is a - * SUBDIRECTORY with its own `dispatch.toml`: the main `createConfigWatcher` - * only watches the root + global configs, so without this a nested config edit - * would never clear `lspServersByDir[subdir]` and agents there would keep using - * stale LSP servers until a root-config change or restart. - * - * `onChange` fires (debounced) on add/change/unlink of `<dir>/dispatch.toml`. - */ -export function watchDirConfig(dir: string, onChange: () => void): { close(): void } { - const tomlPath = join(dir, "dispatch.toml"); - let debounceTimer: ReturnType<typeof setTimeout> | null = null; - - const watcher = watch(tomlPath, { - ignoreInitial: true, - persistent: false, - }); - - const handleChange = () => { - if (debounceTimer !== null) clearTimeout(debounceTimer); - debounceTimer = setTimeout(() => { - debounceTimer = null; - try { - onChange(); - } catch (err) { - console.warn( - `dispatch: dir config watcher onChange error: ${err instanceof Error ? err.message : String(err)}`, - ); - } - }, 300); - }; - - watcher.on("change", handleChange); - watcher.on("add", handleChange); - watcher.on("unlink", handleChange); - watcher.on("error", (err) => { - console.warn( - `dispatch: dir config watcher error: ${err instanceof Error ? err.message : String(err)}`, - ); - }); - - return { - close() { - if (debounceTimer !== null) { - clearTimeout(debounceTimer); - debounceTimer = null; - } - watcher.close().catch((err) => { - console.warn( - `dispatch: error closing dir config watcher: ${err instanceof Error ? err.message : String(err)}`, - ); - }); - }, - }; -} diff --git a/packages/core/src/credentials/anthropic-betas.ts b/packages/core/src/credentials/anthropic-betas.ts deleted file mode 100644 index 802ebbd..0000000 --- a/packages/core/src/credentials/anthropic-betas.ts +++ /dev/null @@ -1,24 +0,0 @@ -// ─── Anthropic Beta Headers ─────────────────────────────────── -// -// The set of `anthropic-beta` features the official Claude Code CLI sends on -// every request. Kept in a dependency-free module (no DB / `bun:sqlite` -// import) so the LLM provider layer (`llm/provider.ts`) can pull the list in -// without dragging the whole credentials/DB stack into its import graph. -// -// `prompt-caching-scope-2026-01-05` is load-bearing for cost: without it the -// Anthropic API silently ignores every `cache_control` breakpoint we place, -// producing a 0% cache hit rate (see notes/claude-report.md). `oauth-2025-04-20` -// gates the Bearer/OAuth flow used by Claude Pro/Max subscriptions. - -const BASE_BETAS = [ - "claude-code-20250219", - "oauth-2025-04-20", - "interleaved-thinking-2025-05-14", - "prompt-caching-scope-2026-01-05", - "context-management-2025-06-27", - "advisor-tool-2026-03-01", -]; - -export function getAnthropicBetas(): string[] { - return [...BASE_BETAS]; -} diff --git a/packages/core/src/credentials/api-keys.ts b/packages/core/src/credentials/api-keys.ts deleted file mode 100644 index ef30400..0000000 --- a/packages/core/src/credentials/api-keys.ts +++ /dev/null @@ -1,80 +0,0 @@ -import { getDatabase } from "../db/index.js"; - -export interface StoredApiKey { - keyId: string; - provider: string; - apiKey: string; - importedAt: number; - updatedAt: number; -} - -/** - * Store or update an API key in the database. - */ -export function setApiKey(keyId: string, provider: string, apiKey: string): void { - const db = getDatabase(); - const now = Date.now(); - db.query( - `INSERT INTO api_keys (key_id, provider, api_key, imported_at, updated_at) - VALUES ($keyId, $provider, $apiKey, $now, $now) - ON CONFLICT(key_id) DO UPDATE SET - api_key = $apiKey, - updated_at = $now`, - ).run({ - $keyId: keyId, - $provider: provider, - $apiKey: apiKey, - $now: now, - }); -} - -/** - * Get a stored API key by key ID. Returns the key string or null. - */ -export function getApiKey(keyId: string): string | null { - const db = getDatabase(); - const row = db - .query("SELECT api_key FROM api_keys WHERE key_id = $keyId") - .get({ $keyId: keyId }) as { api_key: string } | null; - return row?.api_key ?? null; -} - -/** - * Resolve an API key from the database, with env var fallback. - * Pass the env var name (e.g. "GOOGLE_API_KEY") to check process.env as well. - */ -export function resolveApiKey(keyId: string, envVar?: string): string | null { - const dbKey = getApiKey(keyId); - if (dbKey) return dbKey; - if (envVar) return process.env[envVar] ?? null; - return null; -} - -/** - * Delete a stored API key. - */ -export function deleteApiKey(keyId: string): void { - const db = getDatabase(); - db.query("DELETE FROM api_keys WHERE key_id = $keyId").run({ $keyId: keyId }); -} - -/** - * List all stored API keys with metadata (key value excluded for security). - */ -export function listApiKeys(): Array<{ - keyId: string; - provider: string; - importedAt: number; - updatedAt: number; -}> { - const db = getDatabase(); - const rows = db - .query("SELECT key_id, provider, imported_at, updated_at FROM api_keys ORDER BY key_id") - .all() as Array<Record<string, unknown>>; - return rows.map((row) => ({ - keyId: row.key_id as string, - provider: row.provider as string, - importedAt: row.imported_at as number, - updatedAt: row.updated_at as number, - })); -} diff --git a/packages/core/src/credentials/claude.ts b/packages/core/src/credentials/claude.ts deleted file mode 100644 index 050a0fc..0000000 --- a/packages/core/src/credentials/claude.ts +++ /dev/null @@ -1,694 +0,0 @@ -import { createHash } from "node:crypto"; -import { - chmodSync, - existsSync, - mkdirSync, - readdirSync, - readFileSync, - writeFileSync, -} from "node:fs"; -import { homedir } from "node:os"; -import { basename, dirname, join } from "node:path"; -import { getDatabase } from "../db/index.js"; -import { getAnthropicBetas } from "./anthropic-betas.js"; -import { getStoredCredentials, listStoredCredentials, updateStoredTokens } from "./store.js"; - -// Re-exported for backward compatibility — `getAnthropicBetas` historically -// lived here and is surfaced through `credentials/index.ts`. The definition -// now lives in the dependency-free `anthropic-betas.ts` module. -export { getAnthropicBetas }; - -export interface ClaudeCredentials { - accessToken: string; - refreshToken: string; - expiresAt: number; - subscriptionType?: string; -} - -export interface ClaudeAccount { - id: string; - label: string; - source: string; - credentials: ClaudeCredentials; -} - -const OAUTH_TOKEN_URL = "https://claude.ai/v1/oauth/token"; -const OAUTH_CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e"; -const CREDENTIAL_CACHE_TTL_MS = 30_000; - -const CREDENTIALS_DIR = join(homedir(), ".claude"); -const PRIMARY_CREDENTIALS_FILE = join(CREDENTIALS_DIR, ".credentials.json"); - -const accountCacheMap = new Map<string, { creds: ClaudeCredentials; cachedAt: number }>(); - -function parseCredentialsFile(raw: string): ClaudeCredentials | null { - let parsed: unknown; - try { - parsed = JSON.parse(raw); - } catch { - return null; - } - - if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null; - - const data = (parsed as Record<string, unknown>).claudeAiOauth ?? parsed; - const creds = data as Record<string, unknown>; - - if ( - (creds as Record<string, unknown>).mcpOAuth && - !(creds as Record<string, unknown>).accessToken - ) { - return null; - } - - if ( - typeof creds.accessToken !== "string" || - typeof creds.refreshToken !== "string" || - typeof creds.expiresAt !== "number" - ) { - return null; - } - - return { - accessToken: creds.accessToken as string, - refreshToken: creds.refreshToken as string, - expiresAt: creds.expiresAt as number, - subscriptionType: - typeof creds.subscriptionType === "string" ? creds.subscriptionType : undefined, - }; -} - -function readCredentialsFile(filePath: string): ClaudeCredentials | null { - try { - if (!existsSync(filePath)) return null; - const raw = readFileSync(filePath, "utf-8").trim(); - if (!raw) return null; - return parseCredentialsFile(raw); - } catch { - return null; - } -} - -function writeCredentialsFile(filePath: string, creds: ClaudeCredentials): void { - let existing: Record<string, unknown> = {}; - try { - if (existsSync(filePath)) { - const raw = readFileSync(filePath, "utf-8").trim(); - if (raw) { - existing = JSON.parse(raw); - } - } - } catch { - existing = {}; - } - - const hasWrapper = "claudeAiOauth" in existing; - const target = hasWrapper ? (existing.claudeAiOauth as Record<string, unknown>) : existing; - target.accessToken = creds.accessToken; - target.refreshToken = creds.refreshToken; - target.expiresAt = creds.expiresAt; - if (creds.subscriptionType) { - target.subscriptionType = creds.subscriptionType; - } - - const dir = dirname(filePath); - if (!existsSync(dir)) { - mkdirSync(dir, { recursive: true, mode: 0o700 }); - } - writeFileSync(filePath, JSON.stringify(existing, null, 2), { encoding: "utf-8", mode: 0o600 }); - if (process.platform !== "win32") { - chmodSync(filePath, 0o600); - } -} - -async function refreshViaOAuth(refreshToken: string): Promise<ClaudeCredentials | null> { - const body = new URLSearchParams({ - grant_type: "refresh_token", - client_id: OAUTH_CLIENT_ID, - refresh_token: refreshToken, - }); - - try { - const response = await fetch(OAUTH_TOKEN_URL, { - method: "POST", - headers: { "Content-Type": "application/x-www-form-urlencoded" }, - body: body.toString(), - }); - - if (!response.ok) { - return null; - } - - const data = (await response.json()) as Record<string, unknown>; - if (!data.access_token || typeof data.access_token !== "string") { - return null; - } - - return { - accessToken: data.access_token as string, - refreshToken: (data.refresh_token as string) ?? refreshToken, - expiresAt: Date.now() + ((data.expires_in as number) ?? 36_000) * 1000, - subscriptionType: - typeof data.subscriptionType === "string" ? data.subscriptionType : undefined, - }; - } catch { - return null; - } -} - -function buildAccountLabels(accounts: ClaudeAccount[]): void { - for (const acct of accounts) { - acct.label = acct.credentials.subscriptionType - ? `Claude ${acct.credentials.subscriptionType.charAt(0).toUpperCase() + acct.credentials.subscriptionType.slice(1)}` - : "Claude"; - } -} - -/** - * Load Claude accounts from the SQLite database. - * Returns accounts for all stored anthropic credentials. - * This is the preferred path — file-based discovery is the fallback. - */ -export function getClaudeAccountsFromDB(): ClaudeAccount[] { - const stored = listStoredCredentials(); - const accounts: ClaudeAccount[] = []; - - for (const cred of stored) { - if (cred.provider !== "anthropic") continue; - accounts.push({ - id: cred.keyId, - label: "", - source: `db:${cred.keyId}`, - credentials: { - accessToken: cred.accessToken, - refreshToken: cred.refreshToken, - expiresAt: cred.expiresAt, - subscriptionType: cred.subscriptionType ?? undefined, - }, - }); - } - - buildAccountLabels(accounts); - return accounts; -} - -export function discoverClaudeAccounts(): ClaudeAccount[] { - const accounts: ClaudeAccount[] = []; - - if (!existsSync(CREDENTIALS_DIR)) { - return accounts; - } - - const primaryCreds = readCredentialsFile(PRIMARY_CREDENTIALS_FILE); - if (primaryCreds) { - accounts.push({ - id: "claude-default", - label: "", - source: PRIMARY_CREDENTIALS_FILE, - credentials: primaryCreds, - }); - } - - try { - const files = readdirSync(CREDENTIALS_DIR); - const credFiles = files.filter( - (f) => f.startsWith(".credentials") && f.endsWith(".json") && f !== ".credentials.json", - ); - for (const file of credFiles) { - const filePath = join(CREDENTIALS_DIR, file); - const creds = readCredentialsFile(filePath); - if (creds) { - const id = basename(file, ".json").replace(/^\.credentials/, "claude") || `claude-${file}`; - accounts.push({ - id, - label: "", - source: filePath, - credentials: creds, - }); - } - } - } catch { - // ignore - } - - buildAccountLabels(accounts); - return accounts; -} - -export function refreshAccountCredentials(account: ClaudeAccount): ClaudeCredentials | null { - const cached = accountCacheMap.get(account.id); - const now = Date.now(); - if ( - cached && - now - cached.cachedAt < CREDENTIAL_CACHE_TTL_MS && - cached.creds.expiresAt > now + 60_000 - ) { - return cached.creds; - } - - // Re-read credentials: from DB for DB-backed accounts, from file otherwise - if (account.source.startsWith("db:")) { - const stored = getStoredCredentials(account.id); - if (stored) { - account.credentials = { - accessToken: stored.accessToken, - refreshToken: stored.refreshToken, - expiresAt: stored.expiresAt, - subscriptionType: stored.subscriptionType ?? undefined, - }; - } - } else { - const onDisk = readCredentialsFile(account.source); - if (onDisk) { - account.credentials = onDisk; - } - } - - if (account.credentials.expiresAt > now + 60_000) { - accountCacheMap.set(account.id, { creds: account.credentials, cachedAt: now }); - return account.credentials; - } - - // Try OAuth refresh - if (account.credentials.refreshToken) { - // Synchronous refresh not available in this context, but the async version will be used - // by getCredentialsForAccount below - return null; - } - - return null; -} - -export async function refreshAccountCredentialsAsync( - account: ClaudeAccount, -): Promise<ClaudeCredentials | null> { - const cached = accountCacheMap.get(account.id); - const now = Date.now(); - if ( - cached && - now - cached.cachedAt < CREDENTIAL_CACHE_TTL_MS && - cached.creds.expiresAt > now + 60_000 - ) { - return cached.creds; - } - - // Re-read credentials: from DB for DB-backed accounts, from file otherwise - if (account.source.startsWith("db:")) { - const stored = getStoredCredentials(account.id); - if (stored) { - account.credentials = { - accessToken: stored.accessToken, - refreshToken: stored.refreshToken, - expiresAt: stored.expiresAt, - subscriptionType: stored.subscriptionType ?? undefined, - }; - } - } else { - const onDisk = readCredentialsFile(account.source); - if (onDisk) { - account.credentials = onDisk; - } - } - - if (account.credentials.expiresAt > now + 60_000) { - accountCacheMap.set(account.id, { creds: account.credentials, cachedAt: now }); - return account.credentials; - } - - // Try OAuth refresh - if (account.credentials.refreshToken) { - const refreshed = await refreshViaOAuth(account.credentials.refreshToken); - if (refreshed && refreshed.expiresAt > now + 60_000) { - account.credentials = refreshed; - // Update DB if this is a DB-backed account, otherwise write to file - if (account.source.startsWith("db:")) { - updateStoredTokens( - account.id, - refreshed.accessToken, - refreshed.refreshToken, - refreshed.expiresAt, - ); - } else { - writeCredentialsFile(account.source, refreshed); - } - accountCacheMap.set(account.id, { creds: refreshed, cachedAt: now }); - return refreshed; - } - } - - return null; -} - -// ─── Billing Header Computation ──────────────────────────────── - -const BILLING_SALT = "59cf53e54c78"; -const CC_VERSION = "2.1.112"; - -function extractFirstUserMessageText(messages: Array<{ role: string; content: string }>): string { - const userMsg = messages.find((m) => m.role === "user"); - if (!userMsg) return ""; - if (typeof userMsg.content === "string") return userMsg.content; - return ""; -} - -function computeCch(messageText: string): string { - return createHash("sha256").update(messageText).digest("hex").slice(0, 5); -} - -function computeVersionSuffix(messageText: string, version: string): string { - const sampled = [4, 7, 20].map((i) => (i < messageText.length ? messageText[i] : "0")).join(""); - const input = `${BILLING_SALT}${sampled}${version}`; - return createHash("sha256").update(input).digest("hex").slice(0, 3); -} - -export function buildBillingHeaderValue( - messages: Array<{ role: string; content: string }>, -): string { - const text = extractFirstUserMessageText(messages); - const version = process.env.ANTHROPIC_CLI_VERSION ?? CC_VERSION; - const suffix = computeVersionSuffix(text, version); - const cch = computeCch(text); - return `x-anthropic-billing-header: cc_version=${version}.${suffix}; cc_entrypoint=sdk-cli; cch=${cch};`; -} - -export const SYSTEM_IDENTITY = "You are Claude Code, Anthropic's official CLI for Claude."; - -/** - * Build the request body for a Claude "wake" probe — a tiny, cheap message - * whose only purpose is to keep the subscription's rate-limit window warm. - * - * This MUST mirror the shape of a genuine Claude Code CLI request, because - * Anthropic validates the `system[]` array on OAuth (Pro/Max) -authenticated, - * Claude-Code-billed requests. A bare `{ model, messages }` body (no system - * identity) is rejected (401/403) — which is exactly how the old probe silently - * failed. The valid shape is: - * - * system: [ - * { type: "text", text: "x-anthropic-billing-header: ..." }, // billing, no cache_control - * { type: "text", text: "You are Claude Code, Anthropic's official CLI for Claude." }, - * ] - * messages: [ { role: "user", content: "hi" } ] - * - * Mirrors the runtime `transformClaudeOAuthBody` output for a single short user - * turn. Pure: deterministic given its inputs (the billing header samples only - * the user text), so it can be unit-tested without touching the network. - */ -export function buildWakeProbeBody(model: string): { - model: string; - max_tokens: number; - system: Array<{ type: "text"; text: string }>; - messages: Array<{ role: "user"; content: string }>; -} { - const messages = [{ role: "user" as const, content: "hi" }]; - return { - model, - max_tokens: 16, - system: [ - { type: "text", text: buildBillingHeaderValue(messages) }, - { type: "text", text: SYSTEM_IDENTITY }, - ], - messages, - }; -} - -// ─── Anthropic Request Headers ──────────────────────────────── - -export function getAnthropicHeaders(accessToken: string): Record<string, string> { - return { - authorization: `Bearer ${accessToken}`, - "anthropic-version": "2023-06-01", - "anthropic-beta": getAnthropicBetas().join(","), - "anthropic-dangerous-direct-browser-access": "true", - "x-app": "cli", - "user-agent": `claude-cli/${CC_VERSION} (external, sdk-cli)`, - }; -} - -// ─── Usage Tracking ─────────────────────────────────────────── - -export interface ClaudeUsageBucket { - utilization?: number; - resetsAt?: number; -} - -export interface ClaudeUsageReport { - fiveHour?: ClaudeUsageBucket; - sevenDay?: ClaudeUsageBucket; - sevenDayOpus?: ClaudeUsageBucket; - sevenDaySonnet?: ClaudeUsageBucket; - accountId?: string; - email?: string; - orgId?: string; -} - -/** - * A usage report paired with provenance: whether it came back from a fresh - * live fetch against Anthropic's `/api/oauth/usage` endpoint or was served - * from the local `usage_cache` table after a failed/skipped live fetch. - * - * `source: "cache"` carries `cachedAt` — the epoch-ms timestamp recording when - * that cached payload was last fetched FROM the source (the `usage_cache.cached_at` - * column). `source: "live"` omits `cachedAt` (the data is current as of now). - */ -export interface ClaudeUsageResult { - report: ClaudeUsageReport; - source: "live" | "cache"; - /** Epoch-ms the cached report was last fetched from source. Only on `source: "cache"`. */ - cachedAt?: number; -} - -// ─── Well-known Anthropic models ────────────────────────────── - -/** - * Fetch the live list of available models from Anthropic's /v1/models endpoint. - * Requires valid OAuth credentials with anthropic-beta headers. - */ -export async function fetchAnthropicModels(accessToken: string): Promise<string[]> { - const headers: Record<string, string> = { - ...getAnthropicHeaders(accessToken), - accept: "application/json", - }; - - try { - const response = await fetch("https://api.anthropic.com/v1/models", { headers }); - if (!response.ok) { - console.warn(`dispatch: Anthropic /v1/models returned ${response.status}`); - return []; - } - - const data = (await response.json()) as { - data?: Array<{ id: string }>; - models?: Array<{ id: string }>; - }; - const entries = data.data ?? data.models ?? []; - return entries.map((m) => m.id).filter(Boolean); - } catch (err) { - console.warn( - `dispatch: failed to fetch Anthropic models: ${err instanceof Error ? err.message : String(err)}`, - ); - return []; - } -} - -/** Fallback list if /v1/models is unreachable. */ -export const ANTHROPIC_MODELS_FALLBACK = [ - "claude-sonnet-4-20250514", - "claude-opus-4-20250514", - "claude-3.5-sonnet-20241022", - "claude-3.5-haiku-20241022", - "claude-3-opus-20240229", -]; - -/** - * Pick the model to use for a Claude "wake" probe from a list of model ids. - * - * The probe only needs a small/cheap model to register activity against the - * subscription, so we target Haiku. Model ids change over time (the old - * hardcoded `claude-3-5-haiku-20241022` started returning HTTP 404), so the - * caller fetches the live list from `/v1/models` and we resolve by substring. - * - * Selection: the FIRST id whose name contains "haiku" (case-insensitive). - * Anthropic's `/v1/models` returns models newest-first, so first-match - * naturally prefers the newest Haiku. Returns `null` when nothing matches so - * the caller can surface a clear error instead of probing an invalid model. - */ -export function selectHaikuModel(models: string[]): string | null { - return models.find((id) => id.toLowerCase().includes("haiku")) ?? null; -} - -// ─── Credential Validation ──────────────────────────────────── - -export interface ClaudeProfile { - accountId?: string; - email?: string; - subscriptionType?: string; -} - -/** - * Validate that Claude credentials are usable by hitting the OAuth profile endpoint. - * Returns the profile info if valid, or null if the token is dead. - */ -export async function validateAccountCredentials( - account: ClaudeAccount, -): Promise<ClaudeProfile | null> { - const creds = await refreshAccountCredentialsAsync(account); - if (!creds) return null; - - const url = "https://api.anthropic.com/api/oauth/profile"; - const headers: Record<string, string> = { - ...getAnthropicHeaders(creds.accessToken), - accept: "application/json, text/plain, */*", - }; - - try { - const response = await fetch(url, { headers }); - if (!response.ok) return null; - - const data = (await response.json()) as Record<string, unknown>; - - const profile: ClaudeProfile = {}; - const uuid = typeof data.uuid === "string" ? data.uuid : undefined; - const email = typeof data.email === "string" ? data.email : undefined; - - if (uuid) profile.accountId = uuid; - if (email) profile.email = email; - - // subscriptionType comes from the credentials file, but profile may also carry it - profile.subscriptionType = account.credentials.subscriptionType; - - return profile; - } catch { - return null; - } -} - -async function fetchClaudeUsage(accessToken: string): Promise<ClaudeUsageReport | null> { - const url = "https://api.anthropic.com/api/oauth/usage"; - const headers: Record<string, string> = { - ...getAnthropicHeaders(accessToken), - accept: "application/json, text/plain, */*", - "content-type": "application/json", - }; - - try { - const response = await fetch(url, { headers }); - if (!response.ok) return null; - - const orgId = response.headers.get("anthropic-organization-id")?.trim() || undefined; - const data = (await response.json()) as Record<string, unknown>; - - const parseBucket = (bucket: unknown): ClaudeUsageBucket | undefined => { - if (!bucket || typeof bucket !== "object" || Array.isArray(bucket)) return undefined; - const b = bucket as Record<string, unknown>; - // API returns utilization as 0-100 percentage; normalize to 0-1 fraction - const rawUtil = typeof b.utilization === "number" ? b.utilization : undefined; - const utilization = rawUtil !== undefined ? rawUtil / 100 : undefined; - const resetsAt = - typeof b.resets_at === "string" ? Date.parse(b.resets_at as string) : undefined; - if (utilization === undefined && resetsAt === undefined) return undefined; - return { utilization, resetsAt }; - }; - - const report: ClaudeUsageReport = { - fiveHour: parseBucket(data.five_hour), - sevenDay: parseBucket(data.seven_day), - sevenDayOpus: parseBucket(data.seven_day_opus), - sevenDaySonnet: parseBucket(data.seven_day_sonnet), - }; - - if (orgId) report.orgId = orgId; - - // Try to extract identity - const accountId = - typeof data.account_id === "string" - ? data.account_id - : typeof data.user_id === "string" - ? data.user_id - : typeof data.org_id === "string" - ? data.org_id - : undefined; - if (accountId) report.accountId = accountId; - - const email = typeof data.email === "string" ? data.email : undefined; - if (email) report.email = email; - - return report; - } catch { - return null; - } -} - -/** - * Read a cached usage report plus the epoch-ms it was last fetched from source. - * Returns `null` when there is no cached row (or on any DB/parse error). - */ -function getCachedUsageWithMeta( - keyId: string, -): { report: ClaudeUsageReport; cachedAt: number } | null { - try { - const db = getDatabase(); - const row = db - .query("SELECT report_json, cached_at FROM usage_cache WHERE key_id = $keyId") - .get({ $keyId: keyId }) as { report_json: string; cached_at: number } | null; - if (!row) return null; - return { - report: JSON.parse(row.report_json) as ClaudeUsageReport, - cachedAt: row.cached_at, - }; - } catch { - return null; - } -} - -function setCachedUsage(keyId: string, provider: string, report: ClaudeUsageReport): void { - try { - const db = getDatabase(); - db.query( - `INSERT INTO usage_cache (key_id, provider, cached_at, report_json) - VALUES ($keyId, $provider, $cachedAt, $reportJson) - ON CONFLICT(key_id) DO UPDATE SET - cached_at = $cachedAt, - report_json = $reportJson`, - ).run({ - $keyId: keyId, - $provider: provider, - $cachedAt: Date.now(), - $reportJson: JSON.stringify(report), - }); - } catch { - // Ignore DB errors - } -} - -/** - * Fetch an account's usage report along with its provenance (live vs cache). - * - * Resolution: refresh credentials and hit the live `/api/oauth/usage` endpoint; - * on success the fresh report is cached and returned as `source: "live"`. If - * credentials cannot be refreshed OR the live fetch returns nothing, fall back - * to the local `usage_cache` row and return it as `source: "cache"` with the - * `cachedAt` timestamp recording when that payload was last fetched from source. - * Returns `null` only when neither a live report nor a cached row is available. - */ -export async function getAccountUsageWithSource( - account: ClaudeAccount, -): Promise<ClaudeUsageResult | null> { - const creds = await refreshAccountCredentialsAsync(account); - if (creds) { - const report = await fetchClaudeUsage(creds.accessToken); - if (report) { - setCachedUsage(account.id, "anthropic", report); - return { report, source: "live" }; - } - } - const cached = getCachedUsageWithMeta(account.id); - if (cached) { - return { report: cached.report, source: "cache", cachedAt: cached.cachedAt }; - } - return null; -} - -export async function getAccountUsage(account: ClaudeAccount): Promise<ClaudeUsageReport | null> { - const result = await getAccountUsageWithSource(account); - return result?.report ?? null; -} diff --git a/packages/core/src/credentials/copilot.ts b/packages/core/src/credentials/copilot.ts deleted file mode 100644 index 8baf6f4..0000000 --- a/packages/core/src/credentials/copilot.ts +++ /dev/null @@ -1,64 +0,0 @@ -// ─── GitHub Copilot Usage Tracking ─────────────────────────── -// Uses the internal GitHub Copilot user endpoint (same one the -// official VS Code Copilot extension calls). - -export interface CopilotUsageReport { - tokensConsumed?: number; - tokensRemaining?: number; - percentUsed?: number; // 0-100 - resetAt?: number; // Unix timestamp ms - plan?: string; -} - -export async function fetchCopilotUsage( - token: string, - _baseUrl: string, -): Promise<CopilotUsageReport | null> { - const url = "https://api.github.com/copilot_internal/user"; - const headers: Record<string, string> = { - authorization: `Bearer ${token}`, - accept: "application/json", - }; - - try { - const response = await fetch(url, { headers }); - if (!response.ok) return null; - - const data = (await response.json()) as Record<string, unknown>; - const plan = typeof data.copilot_plan === "string" ? data.copilot_plan : undefined; - - const resetDate = typeof data.quota_reset_date === "string" ? data.quota_reset_date : undefined; - const resetAt = resetDate ? Date.parse(resetDate) : undefined; - - const qs = data.quota_snapshots as Record<string, unknown> | undefined; - const pi = qs?.premium_interactions as Record<string, unknown> | undefined; - - const entitlement = typeof pi?.entitlement === "number" ? pi.entitlement : undefined; - const remaining = typeof pi?.remaining === "number" ? pi.remaining : undefined; - const percentRemaining = - typeof pi?.percent_remaining === "number" ? pi.percent_remaining : undefined; - - if (entitlement === undefined && remaining === undefined) { - return null; - } - - const tokensConsumed = - entitlement !== undefined && remaining !== undefined ? entitlement - remaining : undefined; - const percentUsed = - percentRemaining !== undefined - ? Math.round((100 - percentRemaining) * 100) / 100 - : tokensConsumed !== undefined && entitlement !== undefined && entitlement > 0 - ? Math.round((tokensConsumed / entitlement) * 10000) / 100 - : undefined; - - return { - tokensConsumed, - tokensRemaining: remaining, - percentUsed, - resetAt: resetAt && !Number.isNaN(resetAt) ? resetAt : undefined, - plan, - }; - } catch { - return null; - } -} diff --git a/packages/core/src/credentials/google.ts b/packages/core/src/credentials/google.ts deleted file mode 100644 index 17bc930..0000000 --- a/packages/core/src/credentials/google.ts +++ /dev/null @@ -1,178 +0,0 @@ -// ─── Google Gemini Usage Tracking ─────────────────────────── -// Two modes: -// 1. API key → queries native Gemini models endpoint for rate limits -// 2. Cookie → scrapes gemini.google.com for current usage % (like OpenCode) -// -// Set GEMINI_COOKIE env var with the value of your __Secure-1PSID cookie -// from gemini.google.com to see current usage percentages. - -export interface GoogleUsageBucket { - percentUsed: number; // 0-100 - resetsAt?: string; // e.g. "22:40" or "30 May at 17:40" -} - -export interface GoogleUsageReport { - // Mode 1: API key rate limits - models?: Array<{ - name: string; - inputTokenLimit: number; - outputTokenLimit: number; - rpm: number; - requestsPerDay: number; - }>; - // Mode 2: Cookie-scraped usage from gemini.google.com - currentUsage?: GoogleUsageBucket; - weeklyUsage?: GoogleUsageBucket; -} - -// Helpers to extract HTML text between markers -function extractBetween(html: string, after: string, before: string): string | null { - const start = html.indexOf(after); - if (start === -1) return null; - const s = start + after.length; - const end = html.indexOf(before, s); - if (end === -1) return null; - return html.slice(s, end).trim(); -} - -function extractPercent(html: string, afterMarker: string): number | null { - const chunk = extractBetween(html, afterMarker, "%"); - if (!chunk) return null; - const digits = chunk.replace(/\D/g, ""); - const n = parseInt(digits, 10); - return Number.isNaN(n) ? null : n; -} - -function extractResetTime(html: string, afterMarker: string): string | null { - // Look for "Resets at HH:MM" or "Resets on DD Mon at HH:MM" - const start = html.indexOf(afterMarker); - if (start === -1) return null; - const chunk = html.slice(start + afterMarker.length); - // Match time patterns - const m = chunk.match(/Resets?\s+(at|on)\s+([^<]+)/i); - if (!m?.[1] || !m[2]) return null; - return `${m[1]} ${m[2].trim()}`; -} - -async function scrapeGeminiWeb(cookie: string): Promise<{ - currentUsage?: GoogleUsageBucket; - weeklyUsage?: GoogleUsageBucket; -} | null> { - try { - const response = await fetch("https://gemini.google.com/app", { - headers: { - cookie: `__Secure-1PSID=${cookie}`, - "accept-language": "en-US,en;q=0.9", - }, - redirect: "follow", - }); - - if (!response.ok) return null; - - const html = await response.text(); - - // Detect auth redirect - if (html.includes("ServiceLogin") || html.includes("sign in")) { - return null; - } - - // Look for usage data in the page - // Pattern: "Current usage" followed by a percentage and reset time - const currentPct = extractPercent(html, "Current usage"); - const currentReset = extractResetTime(html, "Current usage"); - - // Weekly limit section - const weeklyPct = extractPercent(html, "Weekly limit"); - const weeklyReset = extractResetTime(html, "Weekly limit"); - - const result: { - currentUsage?: GoogleUsageBucket; - weeklyUsage?: GoogleUsageBucket; - } = {}; - - if (currentPct !== null) { - result.currentUsage = { - percentUsed: currentPct, - resetsAt: currentReset ?? undefined, - }; - } - - if (weeklyPct !== null) { - result.weeklyUsage = { - percentUsed: weeklyPct, - resetsAt: weeklyReset ?? undefined, - }; - } - - if (result.currentUsage || result.weeklyUsage) return result; - return null; - } catch { - return null; - } -} - -async function fetchModelsViaApiKey(apiKey: string): Promise<Array<{ - name: string; - inputTokenLimit: number; - outputTokenLimit: number; - rpm: number; - requestsPerDay: number; -}> | null> { - const url = `https://generativelanguage.googleapis.com/v1beta/models?key=${encodeURIComponent(apiKey)}`; - - try { - const response = await fetch(url); - if (!response.ok) return null; - - const data = (await response.json()) as { - models?: Array<{ - name: string; - inputTokenLimit?: number; - outputTokenLimit?: number; - rateLimit?: { requestsPerMinute?: number }; - limits?: { requestsPerDay?: number }; - }>; - }; - - return (data.models ?? []) - .filter((m) => { - const name = m.name?.replace(/^models\//, "") ?? ""; - return name.startsWith("gemini-"); - }) - .map((m) => ({ - name: m.name?.replace(/^models\//, "") ?? "", - inputTokenLimit: m.inputTokenLimit ?? 0, - outputTokenLimit: m.outputTokenLimit ?? 0, - rpm: m.rateLimit?.requestsPerMinute ?? 0, - requestsPerDay: m.limits?.requestsPerDay ?? 0, - })); - } catch { - return null; - } -} - -export async function fetchGoogleUsage( - apiKey: string, - _baseUrl: string, -): Promise<GoogleUsageReport | null> { - const results: GoogleUsageReport = {}; - - // Try API key mode: get model rate limits - const models = await fetchModelsViaApiKey(apiKey); - if (models) { - results.models = models; - } - - // Try cookie mode: scrape gemini.google.com usage - const cookie = process.env.GEMINI_COOKIE; - if (cookie) { - const scraped = await scrapeGeminiWeb(cookie); - if (scraped) { - if (scraped.currentUsage) results.currentUsage = scraped.currentUsage; - if (scraped.weeklyUsage) results.weeklyUsage = scraped.weeklyUsage; - } - } - - if (results.models || results.currentUsage || results.weeklyUsage) return results; - return null; -} diff --git a/packages/core/src/credentials/index.ts b/packages/core/src/credentials/index.ts deleted file mode 100644 index 131f035..0000000 --- a/packages/core/src/credentials/index.ts +++ /dev/null @@ -1,52 +0,0 @@ -export { - deleteApiKey, - getApiKey, - listApiKeys, - resolveApiKey, - type StoredApiKey, - setApiKey, -} from "./api-keys.js"; -export { - ANTHROPIC_MODELS_FALLBACK, - buildBillingHeaderValue, - buildWakeProbeBody, - type ClaudeAccount, - type ClaudeCredentials, - type ClaudeProfile, - type ClaudeUsageBucket, - type ClaudeUsageReport, - type ClaudeUsageResult, - discoverClaudeAccounts, - fetchAnthropicModels, - getAccountUsage, - getAccountUsageWithSource, - getAnthropicBetas, - getAnthropicHeaders, - getClaudeAccountsFromDB, - refreshAccountCredentials, - refreshAccountCredentialsAsync, - SYSTEM_IDENTITY, - selectHaikuModel, - validateAccountCredentials, -} from "./claude.js"; -export { - type CopilotUsageReport, - fetchCopilotUsage, -} from "./copilot.js"; -export { - fetchGoogleUsage, - type GoogleUsageReport, -} from "./google.js"; -export { - fetchOpencodeUsage, - type OpencodeUsageBucket, - type OpencodeUsageReport, -} from "./opencode.js"; -export { - deleteStoredCredentials, - getStoredCredentials, - importCredentialsFromFile, - listStoredCredentials, - type StoredCredential, - updateStoredTokens, -} from "./store.js"; diff --git a/packages/core/src/credentials/opencode.ts b/packages/core/src/credentials/opencode.ts deleted file mode 100644 index d4d4851..0000000 --- a/packages/core/src/credentials/opencode.ts +++ /dev/null @@ -1,126 +0,0 @@ -import { resolveApiKey } from "./api-keys.js"; - -// ─── OpenCode Usage Tracking ────────────────────────────────── -// OpenCode has no public usage API. We scrape usage from the -// SolidStart SSR-rendered workspace page using a session cookie. -// Requires OPENCODE_COOKIE env var. -// Workspace IDs: OPENCODE_WS1_ID for opencode-1, OPENCODE_WS2_ID for opencode-2. - -export interface OpencodeUsageBucket { - utilization?: number; // 0-1 fraction - resetsAt?: number; // Unix timestamp ms -} - -export interface OpencodeUsageReport { - fiveHour?: OpencodeUsageBucket; - weekly?: OpencodeUsageBucket; - monthly?: OpencodeUsageBucket; -} - -function getWorkspaceId(keyId: string): string | undefined { - // Check DB for workspace ID: stored as "opencode-ws1", "opencode-ws2", or "opencode-ws" - const match = keyId.match(/opencode-(\d+)$/i); - if (match) { - const num = match[1]; - const specific = resolveApiKey(`opencode-ws${num}`); - if (specific) return specific; - } - return resolveApiKey("opencode-ws") ?? undefined; -} - -function parseOcDouble(html: string, key: string): number { - const idx = html.indexOf(`${key}:`); - if (idx === -1) return 0; - let start = idx + key.length + 1; - while (start < html.length && html[start] === " ") start++; - let end = start; - while (end < html.length && html[end] !== "," && html[end] !== "}") { - end++; - } - const val = parseFloat(html.slice(start, end)); - return Number.isNaN(val) ? 0 : val; -} - -function parseOcInt(html: string, key: string): number { - const idx = html.indexOf(`${key}:`); - if (idx === -1) return 0; - let i = idx + key.length + 1; - while (i < html.length && html[i] === " ") i++; - return parseInt(html.slice(i), 10) || 0; -} - -function parseOcBucket( - html: string, - bucketName: string, -): { utilization: number; resetsAt: number } | null { - const search = `${bucketName}:`; - const pos = html.indexOf(search); - if (pos === -1) return null; - - // Find the opening brace after the bucket name - const brace = html.indexOf("{", pos); - if (brace === -1) return null; - - const resetSecs = parseOcInt(html.slice(brace), "resetInSec"); - const usagePct = parseOcDouble(html.slice(brace), "usagePercent"); - - const utilization = usagePct / 100; // convert 0-100% to 0-1 fraction - const resetsAt = Date.now() + resetSecs * 1000; - - return { utilization, resetsAt }; -} - -export async function fetchOpencodeUsage(keyId: string): Promise<OpencodeUsageReport | null> { - const cookie = resolveApiKey("opencode-cookie"); - const wsId = getWorkspaceId(keyId); - - if (!cookie || !wsId) { - return null; - } - - const url = `https://opencode.ai/workspace/${encodeURIComponent(wsId)}/go`; - - try { - const response = await fetch(url, { - headers: { - accept: "text/html", - cookie: `auth=${cookie}`, - }, - redirect: "follow", - }); - - if (!response.ok) return null; - - const html = await response.text(); - - // Auth redirect check - if (html.includes("/auth/authorize") || html.includes('window.location="/auth/authorize"')) { - return null; - } - - // Find the lite.subscription data block. - // HTML contains: lite.subscription.get[\"<wsId>\"] - // We need literal backslashes; use \x5c (hex for backslash). - const wsKey = `lite.subscription.get[\x5c"${wsId}\x5c"]`; - const wsPos = html.indexOf(wsKey); - if (wsPos === -1) return null; - - // Search for the resolved data starting from the ws key position - const minePos = html.indexOf("mine:", wsPos); - const slice = minePos !== -1 ? html.slice(minePos) : ""; - - const fiveHour = parseOcBucket(slice, "rollingUsage"); - const weekly = parseOcBucket(slice, "weeklyUsage"); - const monthly = parseOcBucket(slice, "monthlyUsage"); - - if (!fiveHour && !weekly && !monthly) return null; - - return { - fiveHour: fiveHour ?? undefined, - weekly: weekly ?? undefined, - monthly: monthly ?? undefined, - }; - } catch { - return null; - } -} diff --git a/packages/core/src/credentials/store.ts b/packages/core/src/credentials/store.ts deleted file mode 100644 index 662b322..0000000 --- a/packages/core/src/credentials/store.ts +++ /dev/null @@ -1,185 +0,0 @@ -import { existsSync, readFileSync } from "node:fs"; -import { getDatabase } from "../db/index.js"; -import type { ClaudeCredentials } from "./claude.js"; - -export interface StoredCredential { - keyId: string; - provider: string; - accessToken: string; - refreshToken: string; - expiresAt: number; - subscriptionType: string | null; - sourceFile: string | null; - importedAt: number; - updatedAt: number; -} - -function parseCredentialsFile(raw: string): ClaudeCredentials | null { - let parsed: unknown; - try { - parsed = JSON.parse(raw); - } catch { - return null; - } - - if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null; - - const data = (parsed as Record<string, unknown>).claudeAiOauth ?? parsed; - const creds = data as Record<string, unknown>; - - if (creds.mcpOAuth && !creds.accessToken) return null; - - if ( - typeof creds.accessToken !== "string" || - typeof creds.refreshToken !== "string" || - typeof creds.expiresAt !== "number" - ) { - return null; - } - - return { - accessToken: creds.accessToken as string, - refreshToken: creds.refreshToken as string, - expiresAt: creds.expiresAt as number, - subscriptionType: - typeof creds.subscriptionType === "string" ? creds.subscriptionType : undefined, - }; -} - -/** - * Import credentials from a file into the database for a specific key. - * Reads the credential file, parses it, and upserts into the credentials table. - */ -export function importCredentialsFromFile( - keyId: string, - provider: string, - filePath: string, -): { success: boolean; error?: string } { - if (!existsSync(filePath)) { - return { success: false, error: `File not found: ${filePath}` }; - } - - let raw: string; - try { - raw = readFileSync(filePath, "utf-8").trim(); - } catch (e) { - return { - success: false, - error: `Failed to read file: ${e instanceof Error ? e.message : String(e)}`, - }; - } - - if (!raw) { - return { success: false, error: "File is empty" }; - } - - const creds = parseCredentialsFile(raw); - if (!creds) { - return { success: false, error: "Invalid credentials format" }; - } - - const db = getDatabase(); - const now = Date.now(); - - db.query( - `INSERT INTO credentials (key_id, provider, access_token, refresh_token, expires_at, subscription_type, source_file, imported_at, updated_at) - VALUES ($keyId, $provider, $accessToken, $refreshToken, $expiresAt, $subscriptionType, $sourceFile, $now, $now) - ON CONFLICT(key_id) DO UPDATE SET - access_token = $accessToken, - refresh_token = $refreshToken, - expires_at = $expiresAt, - subscription_type = $subscriptionType, - source_file = $sourceFile, - updated_at = $now`, - ).run({ - $keyId: keyId, - $provider: provider, - $accessToken: creds.accessToken, - $refreshToken: creds.refreshToken, - $expiresAt: creds.expiresAt, - $subscriptionType: creds.subscriptionType ?? null, - $sourceFile: filePath, - $now: now, - }); - - return { success: true }; -} - -/** - * Get stored credentials for a specific key from the database. - */ -export function getStoredCredentials(keyId: string): StoredCredential | null { - const db = getDatabase(); - const row = db - .query( - "SELECT key_id, provider, access_token, refresh_token, expires_at, subscription_type, source_file, imported_at, updated_at FROM credentials WHERE key_id = $keyId", - ) - .get({ $keyId: keyId }) as Record<string, unknown> | null; - - if (!row) return null; - - return { - keyId: row.key_id as string, - provider: row.provider as string, - accessToken: row.access_token as string, - refreshToken: row.refresh_token as string, - expiresAt: row.expires_at as number, - subscriptionType: row.subscription_type as string | null, - sourceFile: row.source_file as string | null, - importedAt: row.imported_at as number, - updatedAt: row.updated_at as number, - }; -} - -/** - * Update tokens in the database after a refresh. - */ -export function updateStoredTokens( - keyId: string, - accessToken: string, - refreshToken: string, - expiresAt: number, -): void { - const db = getDatabase(); - db.query( - `UPDATE credentials SET access_token = $accessToken, refresh_token = $refreshToken, expires_at = $expiresAt, updated_at = $now WHERE key_id = $keyId`, - ).run({ - $keyId: keyId, - $accessToken: accessToken, - $refreshToken: refreshToken, - $expiresAt: expiresAt, - $now: Date.now(), - }); -} - -/** - * Delete stored credentials for a key. - */ -export function deleteStoredCredentials(keyId: string): void { - const db = getDatabase(); - db.query("DELETE FROM credentials WHERE key_id = $keyId").run({ $keyId: keyId }); -} - -/** - * List all keys that have imported credentials, with their status. - */ -export function listStoredCredentials(): StoredCredential[] { - const db = getDatabase(); - const rows = db - .query( - "SELECT key_id, provider, access_token, refresh_token, expires_at, subscription_type, source_file, imported_at, updated_at FROM credentials ORDER BY key_id", - ) - .all() as Array<Record<string, unknown>>; - - return rows.map((row) => ({ - keyId: row.key_id as string, - provider: row.provider as string, - accessToken: row.access_token as string, - refreshToken: row.refresh_token as string, - expiresAt: row.expires_at as number, - subscriptionType: row.subscription_type as string | null, - sourceFile: row.source_file as string | null, - importedAt: row.imported_at as number, - updatedAt: row.updated_at as number, - })); -} diff --git a/packages/core/src/db/chunks.ts b/packages/core/src/db/chunks.ts deleted file mode 100644 index b434a47..0000000 --- a/packages/core/src/db/chunks.ts +++ /dev/null @@ -1,246 +0,0 @@ -import { randomUUID } from "node:crypto"; -import { - explodeTurn, - explodeUserText, - groupRowsToMessages, - type MessageRow, -} from "../chunks/transform.js"; -import type { - ChunkData, - ChunkRow, - ChunkRowDraft, - TextData, - UsageData, - UsageStats, -} from "../types/index.js"; -import { getDatabase } from "./index.js"; - -// Re-export the DB-free transforms so existing barrel consumers -// (`@dispatch/core`) keep importing them from here. The browser frontend deep- -// imports them directly from `chunks/transform.js` to avoid the DB dependency. -export { explodeTurn, explodeUserText, groupRowsToMessages, type MessageRow }; - -// ─── Persistence ───────────────────────────────────────────────── - -function mapRow(row: Record<string, unknown>): ChunkRow { - let data: ChunkData; - try { - data = JSON.parse(row.data_json as string) as ChunkData; - } catch { - data = { text: "" } as TextData; - } - return { - id: row.id as string, - tabId: row.tab_id as string, - seq: row.seq as number, - turnId: row.turn_id as string, - step: row.step as number, - role: row.role as ChunkRow["role"], - type: row.type as ChunkRow["type"], - data, - createdAt: row.created_at as number, - }; -} - -/** - * Append one or more chunk-row drafts to a tab, assigning a monotonic per-tab - * `seq` and a fresh id/timestamp to each. Returns the inserted rows in order. - */ -export function appendChunks(tabId: string, drafts: ChunkRowDraft[]): ChunkRow[] { - if (drafts.length === 0) return []; - const db = getDatabase(); - const maxSeq = db - .query("SELECT COALESCE(MAX(seq), -1) as max_seq FROM chunks WHERE tab_id = $tabId") - .get({ $tabId: tabId }) as { max_seq: number }; - let seq = (maxSeq?.max_seq ?? -1) + 1; - const now = Date.now(); - const insert = db.query( - `INSERT INTO chunks (id, tab_id, seq, turn_id, step, role, type, data_json, created_at) - VALUES ($id, $tabId, $seq, $turnId, $step, $role, $type, $dataJson, $now)`, - ); - const out: ChunkRow[] = []; - // Wrap the whole batch in one transaction: a turn's chunks are persisted in - // a single `appendChunks` call, so this is one fsync per turn instead of one - // per row — the chosen low-IO write strategy for constrained backends. - const insertAll = db.transaction(() => { - for (const draft of drafts) { - const id = randomUUID(); - insert.run({ - $id: id, - $tabId: tabId, - $seq: seq, - $turnId: draft.turnId, - $step: draft.step, - $role: draft.role, - $type: draft.type, - $dataJson: JSON.stringify(draft.data), - $now: now, - }); - out.push({ - id, - tabId, - seq, - turnId: draft.turnId, - step: draft.step, - role: draft.role, - type: draft.type, - data: draft.data, - createdAt: now, - }); - seq++; - } - }); - insertAll(); - return out; -} - -/** - * Read chunk rows for a tab in `seq` order (ASC). Pagination mirrors the old - * message pagination but at chunk granularity: - * - no options → all rows; - * - `before` → rows with `seq < before`, most-recent-first then reversed; - * - `limit` → most recent `limit` rows, reversed to ASC. - */ -export function getChunksForTab( - tabId: string, - options?: { limit?: number; before?: number }, -): ChunkRow[] { - const db = getDatabase(); - if (!options) { - const rows = db - .query("SELECT * FROM chunks WHERE tab_id = $tabId AND type != 'usage' ORDER BY seq ASC") - .all({ $tabId: tabId }) as Array<Record<string, unknown>>; - return rows.map(mapRow); - } - const { limit, before } = options; - if (before !== undefined) { - if (limit !== undefined) { - const rows = db - .query( - "SELECT * FROM chunks WHERE tab_id = $tabId AND type != 'usage' AND seq < $before ORDER BY seq DESC LIMIT $limit", - ) - .all({ $tabId: tabId, $before: before, $limit: limit }) as Array<Record<string, unknown>>; - return rows.map(mapRow).reverse(); - } - const rows = db - .query( - "SELECT * FROM chunks WHERE tab_id = $tabId AND type != 'usage' AND seq < $before ORDER BY seq DESC", - ) - .all({ $tabId: tabId, $before: before }) as Array<Record<string, unknown>>; - return rows.map(mapRow).reverse(); - } - if (limit !== undefined) { - const rows = db - .query( - "SELECT * FROM chunks WHERE tab_id = $tabId AND type != 'usage' ORDER BY seq DESC LIMIT $limit", - ) - .all({ $tabId: tabId, $limit: limit }) as Array<Record<string, unknown>>; - return rows.map(mapRow).reverse(); - } - const rows = db - .query("SELECT * FROM chunks WHERE tab_id = $tabId AND type != 'usage' ORDER BY seq ASC") - .all({ $tabId: tabId }) as Array<Record<string, unknown>>; - return rows.map(mapRow); -} - -/** - * Derived, grouped view of a tab's full history as messages. Used to - * pre-populate the agent's in-memory `ChatMessage[]` history when an Agent is - * (re)constructed. Always reads the full log (grouping a partial window would - * be lossy for the rebuild path). - */ -export function getMessagesForTab(tabId: string): MessageRow[] { - return groupRowsToMessages(getChunksForTab(tabId)); -} - -export function getTotalChunkCount(tabId: string): number { - const db = getDatabase(); - const row = db - .query("SELECT COUNT(*) as count FROM chunks WHERE tab_id = $tabId AND type != 'usage'") - .get({ $tabId: tabId }) as { count: number } | null; - return row?.count ?? 0; -} - -/** - * Aggregate per-tab token/cache usage across ALL persisted `usage` chunk rows. - * - * Usage rows are written as an invisible side channel (one row per `usage` - * AgentEvent) and are query-excluded from `getChunksForTab`/`getTotalChunkCount`, - * so this aggregate is the read path. Because it sums server-side over every - * row, it stays complete even after the frontend evicts/pages out old turns - * (eviction is in-memory only). The return shape is structurally identical to - * the frontend `CacheStats`, so reload can seed it directly. - * - * - cumulative `inputTokens`/`outputTokens`/`cacheReadTokens`/`cacheWriteTokens` - * = SUM over all usage rows; - * - `requests` = COUNT of usage rows; - * - `last` = the highest-seq usage row's split (most recent request); - * - `null` when the tab has no usage rows. - * - * Sums in JS after selecting the rows (mirroring `mapRow`) to avoid relying on - * `json_extract` over the freeform `data_json`. - */ -export function getUsageStatsForTab(tabId: string): UsageStats | null { - const db = getDatabase(); - const rows = db - .query("SELECT data_json FROM chunks WHERE tab_id = $tabId AND type = 'usage' ORDER BY seq ASC") - .all({ $tabId: tabId }) as Array<{ data_json: string }>; - if (rows.length === 0) return null; - - let inputTokens = 0; - let outputTokens = 0; - let cacheReadTokens = 0; - let cacheWriteTokens = 0; - let last: UsageData | null = null; - for (const row of rows) { - let u: UsageData; - try { - u = JSON.parse(row.data_json) as UsageData; - } catch { - continue; - } - inputTokens += u.inputTokens ?? 0; - outputTokens += u.outputTokens ?? 0; - cacheReadTokens += u.cacheReadTokens ?? 0; - cacheWriteTokens += u.cacheWriteTokens ?? 0; - last = { - inputTokens: u.inputTokens ?? 0, - outputTokens: u.outputTokens ?? 0, - cacheReadTokens: u.cacheReadTokens ?? 0, - cacheWriteTokens: u.cacheWriteTokens ?? 0, - }; - } - - return { - inputTokens, - outputTokens, - cacheReadTokens, - cacheWriteTokens, - requests: rows.length, - last, - }; -} - -export function clearChunksForTab(tabId: string): void { - const db = getDatabase(); - db.query("DELETE FROM chunks WHERE tab_id = $tabId").run({ $tabId: tabId }); -} - -/** - * Relocate every chunk row from one tab to another (compaction backup path). - * - * Used by conversation compaction to move the FULL pre-compaction history off - * the canonical tab id (`fromTabId`) onto a freshly-created backup tab id - * (`toTabId`), leaving the canonical id free to be re-seeded with the summary + - * preserved tail. `seq` values are preserved (they remain per-tab monotonic for - * the destination since it starts empty), as are turn ids, so the relocated - * history groups identically under its new tab. Returns the number of rows - * moved. - */ -export function rekeyChunks(fromTabId: string, toTabId: string): number { - const db = getDatabase(); - const result = db - .query("UPDATE chunks SET tab_id = $to WHERE tab_id = $from") - .run({ $from: fromTabId, $to: toTabId }); - return Number(result.changes ?? 0); -} diff --git a/packages/core/src/db/index.ts b/packages/core/src/db/index.ts deleted file mode 100644 index 93ec1f9..0000000 --- a/packages/core/src/db/index.ts +++ /dev/null @@ -1,177 +0,0 @@ -import { Database } from "bun:sqlite"; -import { existsSync, mkdirSync } from "node:fs"; -import { homedir } from "node:os"; -import { isAbsolute, join } from "node:path"; - -/** - * Returns the directory for persistent Dispatch data, following XDG Base - * Directory spec on Linux: `$XDG_DATA_HOME/dispatch` (defaults to - * `~/.local/share/dispatch`). - */ -function getDataDir(): string { - const xdg = process.env.XDG_DATA_HOME; - const base = xdg && isAbsolute(xdg) ? xdg : join(homedir(), ".local", "share"); - return join(base, "dispatch"); -} - -let _db: Database | null = null; - -/** - * Get (or create) the singleton SQLite database. - * - * - Creates the data directory if it doesn't exist. - * - Creates `dispatch.db` if it doesn't exist. - * - Enables WAL journal mode for concurrent read performance. - */ -export function getDatabase(): Database { - if (_db) return _db; - - const dir = getDataDir(); - if (!existsSync(dir)) { - mkdirSync(dir, { recursive: true }); - } - - const dbPath = join(dir, "dispatch.db"); - _db = new Database(dbPath, { create: true }); - - // WAL mode: better concurrent read performance, safe for single-writer - _db.run("PRAGMA journal_mode = WAL;"); - // Recommended for WAL: normal synchronous is safe and faster - _db.run("PRAGMA synchronous = NORMAL;"); - // Enable foreign keys - _db.run("PRAGMA foreign_keys = ON;"); - - // Create tables - _db.run(`CREATE TABLE IF NOT EXISTS credentials ( - key_id TEXT PRIMARY KEY, - provider TEXT NOT NULL, - access_token TEXT NOT NULL, - refresh_token TEXT NOT NULL, - expires_at INTEGER NOT NULL, - subscription_type TEXT, - source_file TEXT, - imported_at INTEGER NOT NULL, - updated_at INTEGER NOT NULL - )`); - - // Wake schedule: 4 rows per marked hour (one per :00 / :15 / :30 / :45 probe - // slot). The PK is (hour, slot_minute). Destructive migration off the legacy - // single-row-per-hour schema: detect by absence of the `slot_minute` column - // and drop the old table. Other tables (credentials, api_keys, usage_cache, - // settings, tabs, chunks) are NOT touched. - const legacyWakeSchema = (() => { - try { - const cols = _db.query("PRAGMA table_info(wake_schedule)").all() as Array<{ name: string }>; - if (cols.length === 0) return false; // table doesn't exist yet - return !cols.some((c) => c.name === "slot_minute"); - } catch { - return false; - } - })(); - if (legacyWakeSchema) { - _db.run("DROP TABLE IF EXISTS wake_schedule"); - } - _db.run(`CREATE TABLE IF NOT EXISTS wake_schedule ( - hour INTEGER NOT NULL CHECK (hour BETWEEN 0 AND 23), - slot_minute INTEGER NOT NULL CHECK (slot_minute IN (0, 15, 30, 45)), - next_wake_at INTEGER NOT NULL, - PRIMARY KEY (hour, slot_minute) - )`); - - _db.run(`CREATE TABLE IF NOT EXISTS usage_cache ( - key_id TEXT PRIMARY KEY, - provider TEXT NOT NULL, - cached_at INTEGER NOT NULL, - report_json TEXT NOT NULL - )`); - - _db.run(`CREATE TABLE IF NOT EXISTS api_keys ( - key_id TEXT PRIMARY KEY, - provider TEXT NOT NULL, - api_key TEXT NOT NULL, - imported_at INTEGER NOT NULL, - updated_at INTEGER NOT NULL - )`); - - _db.run(`CREATE TABLE IF NOT EXISTS tabs ( - id TEXT PRIMARY KEY, - title TEXT NOT NULL, - key_id TEXT, - model_id TEXT, - parent_tab_id TEXT, - status TEXT NOT NULL DEFAULT 'idle', - is_open INTEGER NOT NULL DEFAULT 1, - position INTEGER NOT NULL DEFAULT 0, - created_at INTEGER NOT NULL, - updated_at INTEGER NOT NULL - )`); - - try { - _db.run("ALTER TABLE tabs ADD COLUMN parent_tab_id TEXT"); - } catch { - // Column already exists — ignore - } - - // ─── Append-only chunk log (replaces the old `messages` blob table) ── - // - // A conversation is stored as a flat, append-only stream of chunk rows - // keyed by a per-tab monotonic `seq`. "Message" and "turn" are DERIVED - // groupings (see db/chunks.ts), never stored containers. This is what - // powers per-chunk frontend pagination AND the stable per-step wire - // format that fixes Anthropic prompt-cache churn (see notes/plan-chunk-log.md). - // - // role : 'user' | 'assistant' | 'tool' | 'system' - // type : 'text' | 'thinking' | 'tool_call' | 'tool_result' | 'error' | 'system' - // step : LLM round-trip index within a turn (user/system rows = 0) - // data_json: the type-specific payload (see ChunkData in types) - _db.run(`CREATE TABLE IF NOT EXISTS chunks ( - id TEXT PRIMARY KEY, - tab_id TEXT NOT NULL, - seq INTEGER NOT NULL, - turn_id TEXT NOT NULL, - step INTEGER NOT NULL DEFAULT 0, - role TEXT NOT NULL, - type TEXT NOT NULL, - data_json TEXT NOT NULL, - created_at INTEGER NOT NULL - )`); - - _db.run(`CREATE INDEX IF NOT EXISTS idx_chunks_tab_seq ON chunks(tab_id, seq)`); - - // One-shot migration off the legacy `messages` blob model. Beta software, - // no backward compatibility: the old chat history is destroyed (tabs + - // messages), while settings / credentials / api_keys / usage_cache / - // wake_schedule are preserved. Detect the old schema by the presence of - // the `messages` table; once dropped, this branch never runs again. - const hasLegacyMessages = _db - .query("SELECT name FROM sqlite_master WHERE type='table' AND name='messages'") - .get() as { name: string } | null; - if (hasLegacyMessages) { - _db.run("DROP TABLE IF EXISTS messages"); - // Clear conversation containers too (fresh slate for the new model). - _db.run("DELETE FROM tabs"); - _db.run("DELETE FROM chunks"); - } - - _db.run(`CREATE TABLE IF NOT EXISTS settings ( - key TEXT PRIMARY KEY, - value TEXT NOT NULL - )`); - - return _db; -} - -/** Close the database connection (e.g. on shutdown). */ -export function closeDatabase(): void { - if (_db) { - _db.close(); - _db = null; - } -} - -/** Returns the path where the database file lives (or will live). */ -export function getDatabasePath(): string { - if (_db) return _db.filename; - const dir = getDataDir(); - return join(dir, "dispatch.db"); -} diff --git a/packages/core/src/db/settings.ts b/packages/core/src/db/settings.ts deleted file mode 100644 index f9d152e..0000000 --- a/packages/core/src/db/settings.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { getDatabase } from "./index.js"; - -export function getSetting(key: string): string | null { - const db = getDatabase(); - const row = db.query("SELECT value FROM settings WHERE key = $key").get({ $key: key }) as { - value: string; - } | null; - return row?.value ?? null; -} - -export function setSetting(key: string, value: string): void { - const db = getDatabase(); - db.query( - `INSERT INTO settings (key, value) VALUES ($key, $value) - ON CONFLICT(key) DO UPDATE SET value = $value`, - ).run({ $key: key, $value: value }); -} - -export function deleteSetting(key: string): void { - const db = getDatabase(); - db.query("DELETE FROM settings WHERE key = $key").run({ $key: key }); -} diff --git a/packages/core/src/db/tabs.ts b/packages/core/src/db/tabs.ts deleted file mode 100644 index f719a01..0000000 --- a/packages/core/src/db/tabs.ts +++ /dev/null @@ -1,250 +0,0 @@ -import { getDatabase } from "./index.js"; - -export interface TabRow { - id: string; - title: string; - keyId: string | null; - modelId: string | null; - parentTabId: string | null; - status: string; - isOpen: boolean; - position: number; - createdAt: number; - updatedAt: number; -} - -function rowToTab(row: Record<string, unknown>): TabRow { - return { - id: row.id as string, - title: row.title as string, - keyId: row.key_id as string | null, - modelId: row.model_id as string | null, - parentTabId: (row.parent_tab_id as string) ?? null, - status: row.status as string, - isOpen: (row.is_open as number) === 1, - position: row.position as number, - createdAt: row.created_at as number, - updatedAt: row.updated_at as number, - }; -} - -export function createTab( - id: string, - title: string, - options?: { keyId?: string | null; modelId?: string | null; parentTabId?: string | null }, -): TabRow { - const db = getDatabase(); - const now = Date.now(); - const maxPos = db - .query("SELECT COALESCE(MAX(position), -1) as max_pos FROM tabs WHERE is_open = 1") - .get() as { max_pos: number }; - const position = (maxPos?.max_pos ?? -1) + 1; - const keyId = options?.keyId ?? null; - const modelId = options?.modelId ?? null; - const parentTabId = options?.parentTabId ?? null; - db.query( - `INSERT INTO tabs (id, title, key_id, model_id, parent_tab_id, status, is_open, position, created_at, updated_at) - VALUES ($id, $title, $keyId, $modelId, $parentTabId, 'idle', 1, $position, $now, $now)`, - ).run({ - $id: id, - $title: title, - $keyId: keyId, - $modelId: modelId, - $parentTabId: parentTabId, - $position: position, - $now: now, - }); - return { - id, - title, - keyId, - modelId, - parentTabId, - status: "idle", - isOpen: true, - position, - createdAt: now, - updatedAt: now, - }; -} - -export function getTab(id: string): TabRow | null { - const db = getDatabase(); - const row = db.query("SELECT * FROM tabs WHERE id = $id").get({ $id: id }) as Record< - string, - unknown - > | null; - return row ? rowToTab(row) : null; -} - -export function listOpenTabs(): TabRow[] { - const db = getDatabase(); - const rows = db - .query("SELECT * FROM tabs WHERE is_open = 1 ORDER BY position ASC") - .all() as Array<Record<string, unknown>>; - return rows.map(rowToTab); -} - -export function updateTabTitle(id: string, title: string): void { - const db = getDatabase(); - db.query("UPDATE tabs SET title = $title, updated_at = $now WHERE id = $id").run({ - $id: id, - $title: title, - $now: Date.now(), - }); -} - -export function updateTabModel(id: string, keyId: string | null, modelId: string | null): void { - const db = getDatabase(); - db.query( - "UPDATE tabs SET key_id = $keyId, model_id = $modelId, updated_at = $now WHERE id = $id", - ).run({ - $id: id, - $keyId: keyId, - $modelId: modelId, - $now: Date.now(), - }); -} - -export function updateTabStatus(id: string, status: string): void { - const db = getDatabase(); - db.query("UPDATE tabs SET status = $status, updated_at = $now WHERE id = $id").run({ - $id: id, - $status: status, - $now: Date.now(), - }); -} - -export function updateTabPositions(idsInOrder: string[]): void { - const db = getDatabase(); - const now = Date.now(); - const update = db.query("UPDATE tabs SET position = $position, updated_at = $now WHERE id = $id"); - // One transaction so a reorder is atomic: either every tab lands at its new - // slot or none does, never a half-applied ordering. - const applyAll = db.transaction(() => { - idsInOrder.forEach((id, index) => { - update.run({ $id: id, $position: index, $now: now }); - }); - }); - applyAll(); -} - -export function archiveTab(id: string): void { - const db = getDatabase(); - db.query("UPDATE tabs SET is_open = 0, updated_at = $now WHERE id = $id").run({ - $id: id, - $now: Date.now(), - }); -} - -/** - * Return the IDs of `rootId` plus every OPEN descendant tab, in leaf-first - * order (children before their parent). Archived descendants - * (`is_open = 0`) and their sub-trees are skipped — closing a parent - * shouldn't drag archived branches back into view. - * - * The starting `rootId` is always included in the result, even if no row - * with that id exists in the `tabs` table (graceful handling for stale - * references). - * - * Order matters for the cascade-close path: callers archive descendants - * leaf-first so foreign-key cleanup (messages, etc.) doesn't fail on - * partially-deleted parents. - * - * Cycle-safe: a `visited` set guards against accidental `parent_tab_id` - * loops that would otherwise spin forever. - */ -export function getDescendantIds(rootId: string): string[] { - const db = getDatabase(); - const visited = new Set<string>(); - const order: string[] = []; - const queue: string[] = [rootId]; - while (queue.length > 0) { - const id = queue.shift() as string; - if (visited.has(id)) continue; - visited.add(id); - order.push(id); - const children = db - .query("SELECT id FROM tabs WHERE parent_tab_id = $id AND is_open = 1") - .all({ $id: id }) as Array<{ id: string }>; - for (const child of children) { - if (!visited.has(child.id)) queue.push(child.id); - } - } - return order.reverse(); -} - -/** - * Minimum length of a tab-handle prefix accepted by `resolveTabPrefix`. - * Mirrors the frontend's minimum DISPLAY length (4 hex chars). Anything - * shorter is rejected as too broad — an agent must echo at least the 4-char - * handle shown in the UI. - */ -export const MIN_TAB_PREFIX_LENGTH = 4; - -/** - * Outcome of resolving a short tab handle (a git-style prefix of a tab's - * UUID) back to a concrete open tab. - * - * - `ok` — exactly one open tab matched; `tab` is it. - * - `none` — no open tab matched (bad/stale handle, or too-short prefix). - * - `ambiguous` — more than one open tab shares the prefix; `matches` lists - * them so the caller can ask for one more character (the same - * UX as `git checkout <ambiguous-sha>`). - */ -export type ResolveTabPrefixResult = - | { status: "ok"; tab: TabRow } - | { status: "none" } - | { status: "ambiguous"; matches: TabRow[] }; - -/** - * Resolve a short tab handle to a single OPEN tab by prefix match — the - * git-short-hash model. The handle is NEVER stored: it is always derived from - * (and matched against) the canonical lowercase UUID in `tabs.id`. - * - * Sanitization is mandatory because the SQLite `LIKE` operator treats `%` and - * `_` as wildcards: an unsanitized prefix like `a%` would match broadly. We - * lowercase the input (UUIDs are canonical lowercase; SQLite `LIKE` is also - * ASCII-case-insensitive by default) and strip everything outside the UUID - * alphabet `[0-9a-f-]` so no wildcard can survive into the query. - * - * A prefix shorter than `MIN_TAB_PREFIX_LENGTH` after sanitization returns - * `none` rather than matching a large swath of tabs. - * - * Only OPEN tabs (`is_open = 1`) are addressable — a closed tab's UUID prefix - * must not cause phantom ambiguity or resolve to a dead conversation. - */ -export function resolveTabPrefix(prefix: string): ResolveTabPrefixResult { - const sanitized = (prefix ?? "").toLowerCase().replace(/[^0-9a-f-]/g, ""); - if (sanitized.length < MIN_TAB_PREFIX_LENGTH) { - return { status: "none" }; - } - const db = getDatabase(); - const rows = db - .query("SELECT * FROM tabs WHERE is_open = 1 AND id LIKE $prefix ORDER BY position ASC") - .all({ $prefix: `${sanitized}%` }) as Array<Record<string, unknown>>; - if (rows.length === 0) return { status: "none" }; - if (rows.length === 1) return { status: "ok", tab: rowToTab(rows[0] as Record<string, unknown>) }; - return { status: "ambiguous", matches: rows.map(rowToTab) }; -} - -/** - * Compute the shortest unique prefix (minimum `MIN_TAB_PREFIX_LENGTH` chars) - * that identifies `tabId` among the currently OPEN tabs — the backend twin of - * the frontend's display helper. Used when a tool needs to echo a tab's own - * handle (e.g. provenance prefixes, "available tabs" hints) without trusting a - * value from the wire. - * - * Returns the full id if no shorter unique prefix exists (degenerate — only if - * two open tabs share an entire id, which UUID uniqueness precludes). - */ -export function shortestUniquePrefix(tabId: string): string { - const db = getDatabase(); - const rows = db.query("SELECT id FROM tabs WHERE is_open = 1").all() as Array<{ id: string }>; - const others = rows.map((r) => r.id).filter((id) => id !== tabId); - for (let len = MIN_TAB_PREFIX_LENGTH; len < tabId.length; len++) { - const candidate = tabId.slice(0, len); - if (!others.some((id) => id.startsWith(candidate))) return candidate; - } - return tabId; -} diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts deleted file mode 100644 index e951d08..0000000 --- a/packages/core/src/index.ts +++ /dev/null @@ -1,172 +0,0 @@ -// @dispatch/core — Agent runtime, LLM integration, tools - -// Agent & LLM -export { Agent } from "./agent/agent.js"; -export { - deleteAgent, - expandAgentToolNames, - GLOBAL_AGENTS_DIR, - getAgentDirPaths, - getAgentDirs, - getProjectAgentsDir, - loadAgent, - loadAgents, - saveAgent, -} from "./agents/index.js"; -// Chunk helpers -export { - appendEventToChunks, - applySystemEvent, - type IdentifiedMessage, - type SystemEventLike, -} from "./chunks/append.js"; -// Compaction -export { - buildCompactionPrompt, - buildCompactionRequest, - buildSummaryTurnText, - type CompactionRequest, - DEFAULT_TAIL_TURNS, - extractPreviousSummary, - type HeadTailSelection, - renderTranscript, - SUMMARY_MARKER, - SUMMARY_TEMPLATE, - selectHeadTail, - TOOL_OUTPUT_MAX_CHARS, -} from "./compaction/index.js"; -// Config -export { - configToRuleset, - createConfigWatcher, - getGlobalConfigPath, - loadConfig, - loadGlobalConfig, - mergeConfigs, - validateConfig, - watchDirConfig, -} from "./config/index.js"; -// Credentials -export * from "./credentials/index.js"; -export { - appendChunks, - clearChunksForTab, - explodeTurn, - explodeUserText, - getChunksForTab, - getMessagesForTab, - getTotalChunkCount, - getUsageStatsForTab, - groupRowsToMessages, - type MessageRow, - rekeyChunks, -} from "./db/chunks.js"; -// Database -export { closeDatabase, getDatabase, getDatabasePath } from "./db/index.js"; -export { deleteSetting, getSetting, setSetting } from "./db/settings.js"; -// Tabs & Messages -export { - archiveTab, - createTab, - getTab, - listOpenTabs, - MIN_TAB_PREFIX_LENGTH, - type ResolveTabPrefixResult, - resolveTabPrefix, - shortestUniquePrefix, - type TabRow, - updateTabModel, - updateTabPositions, - updateTabStatus, - updateTabTitle, -} from "./db/tabs.js"; -export { - debugVerbosity, - isDebugEnabled, - logAgentLoop, - logStepLifecycle, - logStreamEvent, -} from "./llm/debug-logger.js"; -export { createProvider } from "./llm/provider.js"; -// LSP (Language Server Protocol) -export { - createLspClient, - type Diagnostic as LspDiagnostic, - type LspClient, - LspManager, - type LspServerHandle, - pretty as prettyDiagnostic, - type ResolvedLspServer, - report as reportDiagnostics, - resolveServersFromConfig, -} from "./lsp/index.js"; -// Models -export { - ACCEPTED_ATTACHMENT_MEDIA_TYPES, - ACCEPTED_IMAGE_MEDIA_TYPES, - ACCEPTED_PDF_MEDIA_TYPE, - type AttachmentValidationError, - type AttachmentValidationResult, - base64ByteLength, - getModelsCatalog, - hasAttachments, - isAcceptedAttachmentMediaType, - isImageMediaType, - isPdfMediaType, - MAX_ATTACHMENTS, - MAX_IMAGE_BYTES, - MAX_PDF_BYTES, - MAX_TOTAL_ATTACHMENT_BYTES, - type ModelInputCapabilities, - ModelRegistry, - resolveContextLimit, - resolveModelCapabilities, - validateUserContent, -} from "./models/index.js"; -// Notifications (ntfy.sh) -export * from "./notifications/index.js"; -export * from "./permission/index.js"; -// Skills -export { - createSkillsWatcher, - getSkillByName, - loadSkills, - parseSkillFile, - resolveSkillsForAgent, -} from "./skills/index.js"; -export { prefix as bashArityPrefix } from "./tools/bash-arity.js"; -// Tools -export { createKeyUsageTool, type KeyUsageCallbacks } from "./tools/key-usage.js"; -export { createListFilesTool } from "./tools/list-files.js"; -export { createLspTool, type LspToolContext } from "./tools/lsp.js"; -export { createReadFileTool } from "./tools/read-file.js"; -export { createReadFileSliceTool } from "./tools/read-file-slice.js"; -export { createReadTabTool, type ReadTabCallbacks } from "./tools/read-tab.js"; -export { createToolRegistry } from "./tools/registry.js"; -export { createRetrieveTool, type RetrieveCallbacks } from "./tools/retrieve.js"; -export { BackgroundShellStore, createRunShellTool } from "./tools/run-shell.js"; -export { createSearchCodeTool } from "./tools/search-code.js"; -export { - createSendToTabTool, - type ResolvedTabRef, - type SendToTabCallbacks, - type TabResolution, -} from "./tools/send-to-tab.js"; -export { analyzeCommand } from "./tools/shell-analyze.js"; -export { - type AvailableAgent, - createSummonTool, - type SummonCallbacks, - toAvailableSubagents, - toAvailableUserAgents, -} from "./tools/summon.js"; -export { createTaskListTool, TaskList, TODO_DESCRIPTION } from "./tools/task-list.js"; -export { clearSpillForTab } from "./tools/truncate.js"; -export { createWebSearchTool } from "./tools/web-search.js"; -export { type AfterWriteHook, createWriteFileTool } from "./tools/write-file.js"; -export { - BackgroundTranscriptStore, - createYoutubeTranscribeTool, -} from "./tools/youtube-transcribe.js"; -// Types & Permissions -export * from "./types/index.js"; diff --git a/packages/core/src/llm/anthropic-oauth-transform.ts b/packages/core/src/llm/anthropic-oauth-transform.ts deleted file mode 100644 index 467a307..0000000 --- a/packages/core/src/llm/anthropic-oauth-transform.ts +++ /dev/null @@ -1,153 +0,0 @@ -/** - * Wire-level request restructuring for the Claude OAuth (Pro/Max) flow. - * - * Anthropic validates the `system` array on OAuth-authenticated, Claude-Code- - * billed requests. A genuine Claude Code request looks like: - * - * system: [ - * { type: "text", text: "x-anthropic-billing-header: ..." }, // system[0], NO cache_control - * { type: "text", text: "You are Claude Code, Anthropic's official CLI for Claude.", - * cache_control: { type: "ephemeral" } }, // identity, separate block - * ] - * messages: [ { role: "user", content: "<the real system prompt>\n\n<user text>" }, ... ] - * - * i.e. ONLY the billing header and the verbatim identity string may live in - * `system[]`. Any third-party system prompt (Dispatch's tool/agent instructions) - * MUST be relocated into the first user message. When third-party content stays - * in `system[]` next to the identity, Anthropic bills it as premium "extra - * usage" (token burn) and refuses to apply the Claude Code prompt-cache scope — - * producing the 0% cache hit rate and ballooning cost we were seeing. - * - * Dispatch builds its system prompt as ONE concatenated block - * (`<billing>\n<identity>\n\n<systemPrompt>`); `@ai-sdk/anthropic` serializes - * that into a single `system[]` text entry. This transform runs at fetch time - * on the already-serialized JSON body and reshapes it into the structure above. - * - * Mirrors `references/opencode-claude-auth/src/transforms.ts` (`transformBody`), - * adapted to Dispatch: tool names are already PascalCase-`mcp_`-prefixed by the - * agent, so this transform leaves tools and messages (other than the relocation) - * untouched. It is defensive: any parse/shape surprise returns the body - * unchanged so a transform bug can never break a request. - */ - -const SYSTEM_IDENTITY = "You are Claude Code, Anthropic's official CLI for Claude."; -const BILLING_PREFIX = "x-anthropic-billing-header"; - -type SystemBlock = { type: "text"; text: string; cache_control?: unknown } & Record< - string, - unknown ->; - -interface AnthropicRequestBody { - system?: string | Array<{ type?: string; text?: string } & Record<string, unknown>>; - messages?: Array<{ - role?: string; - content?: string | Array<{ type?: string; text?: string } & Record<string, unknown>>; - }>; - [key: string]: unknown; -} - -/** - * Restructure a serialized Anthropic request body string for the Claude Code - * OAuth flow. Returns the (possibly rewritten) body, or the original input - * unchanged when it isn't a JSON string we recognize. - */ -export function transformClaudeOAuthBody( - body: BodyInit | null | undefined, -): BodyInit | null | undefined { - if (typeof body !== "string") return body; - let parsed: AnthropicRequestBody; - try { - parsed = JSON.parse(body) as AnthropicRequestBody; - } catch { - return body; - } - if (!parsed || typeof parsed !== "object") return body; - try { - const changed = restructureSystem(parsed); - return changed ? JSON.stringify(parsed) : body; - } catch { - // Never let a transform bug break a real request. - return body; - } -} - -/** - * In-place restructure of `parsed.system` / `parsed.messages`. Returns true if - * anything changed (so the caller knows to re-serialize). - */ -function restructureSystem(parsed: AnthropicRequestBody): boolean { - const raw = parsed.system; - let entries: SystemBlock[]; - if (typeof raw === "string") { - entries = [{ type: "text", text: raw }]; - } else if (Array.isArray(raw)) { - entries = raw.map((e) => - typeof e === "string" - ? { type: "text", text: e } - : ({ ...e, type: "text", text: typeof e.text === "string" ? e.text : "" } as SystemBlock), - ); - } else { - return false; // no system field — nothing to do - } - - const combined = entries.map((e) => e.text).join("\n\n"); - - // Only act on Claude Code shaped requests (must carry the identity string). - if (!combined.includes(SYSTEM_IDENTITY)) return false; - - const hadCacheControl = entries.some((e) => e.cache_control != null); - - // Peel the billing-header line out (it is a single line with no newlines). - const lines = combined.split("\n"); - const billingIdx = lines.findIndex((l) => l.startsWith(BILLING_PREFIX)); - let billingLine: string | null = null; - if (billingIdx !== -1) { - billingLine = lines[billingIdx] ?? null; - lines.splice(billingIdx, 1); - } - const afterBilling = lines.join("\n").replace(/^\n+/, ""); - - // Split the identity prefix from the rest (Dispatch's real system prompt). - let rest = ""; - if (afterBilling.startsWith(SYSTEM_IDENTITY)) { - rest = afterBilling.slice(SYSTEM_IDENTITY.length).replace(/^\n+/, ""); - } else { - // Identity is present but not at the front (unexpected) — still isolate it. - rest = afterBilling.replace(SYSTEM_IDENTITY, "").replace(/^\n+/, ""); - } - - // Rebuild system[]: billing (no cache_control) then identity (cached). - const newSystem: SystemBlock[] = []; - if (billingLine) newSystem.push({ type: "text", text: billingLine }); - const identityBlock: SystemBlock = { type: "text", text: SYSTEM_IDENTITY }; - if (hadCacheControl) identityBlock.cache_control = { type: "ephemeral" }; - newSystem.push(identityBlock); - - // Relocate the third-party system prompt into the first user message. - if (rest.length > 0) { - const firstUser = Array.isArray(parsed.messages) - ? parsed.messages.find((m) => m.role === "user") - : undefined; - if (firstUser) { - if (typeof firstUser.content === "string") { - firstUser.content = `${rest}\n\n${firstUser.content}`; - } else if (Array.isArray(firstUser.content)) { - firstUser.content.unshift({ type: "text", text: rest }); - } else { - firstUser.content = rest; - } - } else { - // No user message to host it — keep it as a (cached) system block so - // the request still carries the instructions. - const restBlock: SystemBlock = { type: "text", text: rest }; - if (hadCacheControl) restBlock.cache_control = { type: "ephemeral" }; - newSystem.push(restBlock); - } - } - - parsed.system = newSystem; - return true; -} - -export const __test = { restructureSystem, SYSTEM_IDENTITY, BILLING_PREFIX }; diff --git a/packages/core/src/llm/debug-logger.ts b/packages/core/src/llm/debug-logger.ts deleted file mode 100644 index 072a7a1..0000000 --- a/packages/core/src/llm/debug-logger.ts +++ /dev/null @@ -1,448 +0,0 @@ -/** - * Debug logger for LLM API requests and responses. - * - * Enable via environment variable: DISPATCH_DEBUG_LLM=1 - * - * Logs every outgoing request body and incoming response body to timestamped - * files under `DISPATCH_DEBUG_LLM_DIR` (default: /tmp/dispatch/llm-debug/). - * - * Each request/response pair shares a sequence number for easy correlation. - * Files are named: `{seq}_{timestamp}_{direction}_{model}.json` - * - * For streaming responses (SSE), the raw chunks are captured as they arrive - * and written out as a JSON array when the stream completes. - * - * Additional logging layers: - * - Stream events: every AI SDK stream event (text-delta, tool-call, etc.) - * - Step lifecycle: step start/end, tool execution timing - * - Agent loop: step count, break conditions, tool call counts - * - * All output goes to stderr (console.error) for stream event logs, and to - * files for request/response bodies (too large for console). - */ - -import { mkdirSync, writeFileSync } from "node:fs"; -import { join } from "node:path"; - -const ENABLED = !!process.env.DISPATCH_DEBUG_LLM; -const LOG_DIR = process.env.DISPATCH_DEBUG_LLM_DIR || "/tmp/dispatch/llm-debug"; -let seq = 0; - -/** Verbosity levels: - * 1 = requests/responses only (files) - * 2 = + stream events to stderr - * 3 = + step lifecycle + agent loop details to stderr - */ -const VERBOSITY = Math.max(1, Number(process.env.DISPATCH_DEBUG_LLM_VERBOSITY) || 1); - -function ensureDir(): void { - try { - mkdirSync(LOG_DIR, { recursive: true }); - } catch { - // best effort - } -} - -function ts(): string { - return new Date().toISOString().replace(/[:.]/g, "-"); -} - -function sanitizeModel(model: string): string { - return model.replace(/[^a-zA-Z0-9_.-]/g, "_").slice(0, 60); -} - -function sanitizeTab(tabId?: string): string { - if (!tabId) return "notab"; - return tabId.replace(/[^a-zA-Z0-9_.-]/g, "_").slice(0, 40); -} - -export function isDebugEnabled(): boolean { - return ENABLED; -} - -export function debugVerbosity(): number { - return ENABLED ? VERBOSITY : 0; -} - -/** - * Allocate a fresh sequence number. Used by the fetch wrapper so the request - * and the response share the same id without needing a separate `logRequest` - * call from the agent loop (which doesn't see the actual HTTP body anyway). - */ -export function nextDebugSeq(): number { - return ++seq; -} - -/** - * Log an outgoing request to the AI model endpoint. - * Returns a request ID for correlating with the response. - */ -export function logRequest(data: { - model: string; - url?: string; - method?: string; - headers?: Record<string, string>; - body: unknown; - tabId?: string; - step?: number; - provider?: string; -}): number { - if (!ENABLED) return -1; - ensureDir(); - const id = ++seq; - const filename = `${String(id).padStart(5, "0")}_${ts()}_tab-${sanitizeTab(data.tabId)}_REQ_${sanitizeModel(data.model)}.json`; - const payload = { - _debug: { - seq: id, - direction: "request", - timestamp: new Date().toISOString(), - tabId: data.tabId, - step: data.step, - provider: data.provider, - }, - url: data.url, - method: data.method ?? "POST", - headers: data.headers, - body: data.body, - }; - try { - writeFileSync(join(LOG_DIR, filename), JSON.stringify(payload, null, 2)); - } catch (err) { - console.error(`[dispatch-debug] Failed to write request log: ${err}`); - } - console.error( - `[dispatch-debug] REQ #${id} → ${data.model} (step=${data.step ?? "?"}, tab=${data.tabId ?? "?"})`, - ); - return id; -} - -/** - * Log the raw fetch-level request (the actual HTTP body sent to the provider). - * Called from the instrumented fetch wrapper. - */ -export function logRawFetchRequest(data: { - requestId: number; - url: string; - method: string; - headers: Record<string, string>; - body: string | null; - tabId?: string; -}): void { - if (!ENABLED) return; - ensureDir(); - const filename = `${String(data.requestId).padStart(5, "0")}_${ts()}_tab-${sanitizeTab(data.tabId)}_RAW_REQ.json`; - const payload = { - _debug: { - seq: data.requestId, - direction: "raw-request", - timestamp: new Date().toISOString(), - tabId: data.tabId, - }, - url: data.url, - method: data.method, - headers: data.headers, - body: tryParseJson(data.body), - }; - try { - writeFileSync(join(LOG_DIR, filename), JSON.stringify(payload, null, 2)); - } catch (err) { - console.error(`[dispatch-debug] Failed to write raw request log: ${err}`); - } -} - -/** - * Log the raw fetch-level response (HTTP status, headers, body). - */ -export function logRawFetchResponse(data: { - requestId: number; - url: string; - status: number; - statusText: string; - headers: Record<string, string>; - body: string | null; - isStreaming: boolean; - tabId?: string; -}): void { - if (!ENABLED) return; - ensureDir(); - const filename = `${String(data.requestId).padStart(5, "0")}_${ts()}_tab-${sanitizeTab(data.tabId)}_RAW_RES_${data.status}.json`; - const payload = { - _debug: { - seq: data.requestId, - direction: "raw-response", - timestamp: new Date().toISOString(), - isStreaming: data.isStreaming, - tabId: data.tabId, - }, - url: data.url, - status: data.status, - statusText: data.statusText, - headers: data.headers, - body: tryParseJson(data.body), - }; - try { - writeFileSync(join(LOG_DIR, filename), JSON.stringify(payload, null, 2)); - } catch (err) { - console.error(`[dispatch-debug] Failed to write raw response log: ${err}`); - } -} - -/** - * Accumulator for streaming response chunks. Call `addChunk()` as SSE events - * arrive, then `flush()` when the stream ends to write them all to disk. - */ -export class StreamResponseLogger { - private requestId: number; - private model: string; - private tabId?: string; - private chunks: Array<{ timestamp: string; data: string }> = []; - private startTime: number; - - constructor(requestId: number, model: string, tabId?: string) { - this.requestId = requestId; - this.model = model; - this.tabId = tabId; - this.startTime = Date.now(); - } - - addChunk(rawLine: string): void { - if (!ENABLED) return; - this.chunks.push({ - timestamp: new Date().toISOString(), - data: rawLine, - }); - } - - flush(meta?: { finishReason?: string; error?: string }): void { - if (!ENABLED) return; - ensureDir(); - const elapsed = Date.now() - this.startTime; - const filename = `${String(this.requestId).padStart(5, "0")}_${ts()}_tab-${sanitizeTab(this.tabId)}_STREAM_RES_${sanitizeModel(this.model)}.json`; - const payload = { - _debug: { - seq: this.requestId, - direction: "stream-response", - timestamp: new Date().toISOString(), - tabId: this.tabId, - model: this.model, - elapsedMs: elapsed, - chunkCount: this.chunks.length, - ...meta, - }, - chunks: this.chunks, - }; - try { - writeFileSync(join(LOG_DIR, filename), JSON.stringify(payload, null, 2)); - } catch (err) { - console.error(`[dispatch-debug] Failed to write stream response log: ${err}`); - } - console.error( - `[dispatch-debug] STREAM #${this.requestId} complete: ${this.chunks.length} chunks in ${elapsed}ms (${this.model})`, - ); - } -} - -/** - * Log an AI SDK stream event (text-delta, tool-call, finish-step, etc.). - * Only logs at verbosity >= 2. - */ -export function logStreamEvent(data: { - requestId: number; - step: number; - eventType: string; - detail?: unknown; - tabId?: string; -}): void { - if (!ENABLED || VERBOSITY < 2) return; - const detail = data.detail !== undefined ? ` ${JSON.stringify(data.detail)}` : ""; - console.error( - `[dispatch-debug] STREAM_EVENT #${data.requestId} step=${data.step} ${data.eventType}${detail}`, - ); -} - -/** - * Log step lifecycle events (step start, tool execution, step end). - * Only logs at verbosity >= 3. - */ -export function logStepLifecycle(data: { - tabId?: string; - step: number; - event: string; - detail?: unknown; -}): void { - if (!ENABLED || VERBOSITY < 3) return; - const detail = data.detail !== undefined ? ` ${JSON.stringify(data.detail)}` : ""; - console.error( - `[dispatch-debug] STEP tab=${data.tabId ?? "?"} step=${data.step} ${data.event}${detail}`, - ); -} - -/** - * Log agent loop-level events (loop start, break conditions, etc.). - * Only logs at verbosity >= 3. - */ -export function logAgentLoop(data: { tabId?: string; event: string; detail?: unknown }): void { - if (!ENABLED || VERBOSITY < 3) return; - const detail = data.detail !== undefined ? ` ${JSON.stringify(data.detail)}` : ""; - console.error(`[dispatch-debug] AGENT tab=${data.tabId ?? "?"} ${data.event}${detail}`); -} - -/** - * Wrap a fetch function so every request/response pair is logged to disk - * under `DISPATCH_DEBUG_LLM_DIR` when `DISPATCH_DEBUG_LLM` is set. When - * disabled, returns the input fetch unchanged (zero overhead). - * - * Critical implementation note — SSE bodies: the AI SDK consumes - * `response.body` as a `ReadableStream`. Reading it from anywhere else - * (e.g. calling `.text()`) drains the stream and the SDK gets an empty - * body. We therefore `response.clone()` the response and tee its body via - * a `TransformStream` so each SSE line is forwarded to the SDK AND - * captured into a `StreamResponseLogger`. The clone returns its own - * Response object whose body the SDK reads normally. - * - * For non-streaming responses (`content-type` is not `text/event-stream`) - * we just clone and read once via `.text()` — simpler and safe because - * non-streaming bodies are bounded. - */ -export function wrapFetchWithLogging<F extends (...args: never[]) => Promise<Response> | Response>( - baseFetch: F, - opts: { tabId?: string; modelHint?: string }, -): F { - if (!ENABLED) return baseFetch; - const wrapped = async (...args: Parameters<F>) => { - const requestId = ++seq; - const [input, init] = args as unknown as [RequestInfo | URL, RequestInit | undefined]; - const url = - typeof input === "string" - ? input - : input instanceof URL - ? input.toString() - : (input as Request).url; - const method = - init?.method ?? - (typeof input === "object" && "method" in input ? (input as Request).method : "POST"); - - // Snapshot headers as a plain object for logging. - const headerObj: Record<string, string> = {}; - try { - const h = new Headers(init?.headers); - h.forEach((v, k) => { - // Redact bearer / api-key headers — useful in shared logs. - if (/^(authorization|x-api-key|cookie)$/i.test(k)) { - headerObj[k] = "<redacted>"; - } else { - headerObj[k] = v; - } - }); - } catch { - // best effort - } - - // Capture request body. Most providers send a JSON string here; if it's - // a stream/blob/etc. we skip body logging (rare in our codebase). - let bodyStr: string | null = null; - if (typeof init?.body === "string") { - bodyStr = init.body; - } else if (init?.body instanceof Uint8Array) { - bodyStr = new TextDecoder().decode(init.body); - } - - logRawFetchRequest({ - requestId, - url, - method, - headers: headerObj, - body: bodyStr, - tabId: opts.tabId, - }); - - const response = await (baseFetch as unknown as typeof fetch)(input, init); - - const respHeaders: Record<string, string> = {}; - response.headers.forEach((v, k) => { - respHeaders[k] = v; - }); - const contentType = response.headers.get("content-type") ?? ""; - const isStreaming = contentType.includes("text/event-stream"); - - if (!isStreaming) { - // Clone so we don't drain the SDK's copy. Bounded body — safe to read. - try { - const cloned = response.clone(); - const text = await cloned.text(); - logRawFetchResponse({ - requestId, - url, - status: response.status, - statusText: response.statusText, - headers: respHeaders, - body: text, - isStreaming: false, - tabId: opts.tabId, - }); - } catch (err) { - console.error(`[dispatch-debug] Failed to clone non-stream response: ${err}`); - } - return response; - } - - // Streaming path: write a header file with status + headers immediately - // (the body file comes later via StreamResponseLogger.flush). - logRawFetchResponse({ - requestId, - url, - status: response.status, - statusText: response.statusText, - headers: respHeaders, - body: null, - isStreaming: true, - tabId: opts.tabId, - }); - - // Tee the body through a TransformStream so each SSE chunk is captured - // without consuming the stream the SDK needs. - const streamLogger = new StreamResponseLogger( - requestId, - opts.modelHint ?? "stream", - opts.tabId, - ); - const decoder = new TextDecoder(); - const tee = new TransformStream<Uint8Array, Uint8Array>({ - transform(chunk, controller) { - try { - streamLogger.addChunk(decoder.decode(chunk, { stream: true })); - } catch { - // never let logging break the stream - } - controller.enqueue(chunk); - }, - flush() { - try { - streamLogger.flush(); - } catch { - // best effort - } - }, - }); - - // `response.body` is `ReadableStream<Uint8Array> | null`. If null (no - // body), there's nothing to tee — return as-is. - if (!response.body) return response; - const teed = response.body.pipeThrough(tee); - return new Response(teed, { - status: response.status, - statusText: response.statusText, - headers: response.headers, - }); - }; - return wrapped as unknown as F; -} - -function tryParseJson(s: string | null): unknown { - if (s === null) return null; - try { - return JSON.parse(s); - } catch { - return s; - } -} diff --git a/packages/core/src/llm/provider.ts b/packages/core/src/llm/provider.ts deleted file mode 100644 index ca734f9..0000000 --- a/packages/core/src/llm/provider.ts +++ /dev/null @@ -1,180 +0,0 @@ -import { randomUUID } from "node:crypto"; -import { createAnthropic } from "@ai-sdk/anthropic"; -import { createOpenAICompatible } from "@ai-sdk/openai-compatible"; -import type { LanguageModelV3 } from "@ai-sdk/provider"; -import type { FetchFunction } from "@ai-sdk/provider-utils"; -import { getAnthropicBetas } from "../credentials/anthropic-betas.js"; -import { transformClaudeOAuthBody } from "./anthropic-oauth-transform.js"; -import { wrapFetchWithLogging } from "./debug-logger.js"; - -export interface ProviderConfig { - apiKey: string; - baseURL: string; - provider?: string; - claudeCredentials?: { - accessToken: string; - }; - /** Optional tab id for labelling debug logs. No effect when - * `DISPATCH_DEBUG_LLM` is unset. */ - tabId?: string; -} - -const MCP_PREFIX = "mcp_"; - -function prefixToolName(name: string): string { - return `${MCP_PREFIX}${name.charAt(0).toUpperCase()}${name.slice(1)}`; -} - -function unprefixToolName(name: string): string { - if (name.startsWith(MCP_PREFIX)) { - const rest = name.slice(MCP_PREFIX.length); - return `${rest.charAt(0).toLowerCase()}${rest.slice(1)}`; - } - return name; -} - -// Explicit factory return type so the inferred type doesn't leak references -// into transitive `@ai-sdk/provider` paths (which would trip TS2742). -// `@ai-sdk/anthropic` v3.x and `@ai-sdk/openai-compatible` v2.x both return -// `LanguageModelV3`-spec models; `wrapLanguageModel` likewise. -export type ModelFactory = (modelId: string) => LanguageModelV3; - -export function createProvider(config: ProviderConfig): ModelFactory { - if (config.provider === "anthropic") { - return createClaudeOAuthProvider(config); - } - - if (config.provider === "opencode-anthropic") { - return createApiKeyAnthropicProvider(config); - } - - // Default: OpenAI-compatible provider (OpenCode Zen — DeepSeek, GLM, - // Kimi, MiniMax, etc.). - // - // `@ai-sdk/[email protected]` handles reasoning round-tripping - // natively: it reads `{ type: "reasoning", text }` parts from each - // assistant message's content and emits them as `reasoning_content` - // on the wire (see node_modules/@ai-sdk/openai-compatible/dist/index.mjs - // lines 215-216 and 245). Our `toModelMessages` in agent.ts already - // emits reasoning parts from `ThinkingChunk`s, so no middleware is - // needed. - // - // (The v4-era `normalizeMessages` middleware that lived here was - // actively breaking DeepSeek: it stripped reasoning parts from - // content AND wrote them under `providerMetadata` — wrong key in v3 - // prompts, which use `providerOptions`. The result was that - // reasoning_content never reached the wire and DeepSeek rejected the - // follow-up turn with "must be passed back".) - // - // Debug logging: when DISPATCH_DEBUG_LLM is set, wrap the base fetch - // so every wire request/response (including SSE chunks) is captured. - // When disabled, `wrapFetchWithLogging` returns the input unchanged - // (zero overhead). - const loggingFetch = wrapFetchWithLogging(globalThis.fetch, { - tabId: config.tabId, - modelHint: "opencode-zen", - }) as unknown as FetchFunction; - - const provider = createOpenAICompatible({ - name: "opencode-zen", - apiKey: config.apiKey, - baseURL: config.baseURL, - fetch: loggingFetch, - }); - - return (modelId: string) => provider(modelId); -} - -/** - * Claude OAuth provider. Used by Dispatch's `anthropic` provider keys - * (claude-pro, claude-max). Uses `authToken` to send `Authorization: Bearer` - * (natively supported by `@ai-sdk/anthropic` v3.x), and mimics Claude Code CLI - * request headers so the request bills against the user's Claude subscription. - * - * The `anthropic-beta` header is REQUIRED here. `@ai-sdk/anthropic` only emits - * an `anthropic-beta` header for betas it auto-derives from tool definitions - * (computer-use, structured-outputs, etc.) — it does NOT add the prompt-caching - * or oauth betas on its own. Without `prompt-caching-scope-2026-01-05` the API - * silently ignores every `cache_control` breakpoint we attach to messages, - * giving a 0% cache hit rate and a massive token burn (see notes/claude-report.md). - * The SDK folds any `anthropic-beta` it finds on the provider's config headers - * back into its own beta set (via `getBetasFromHeaders`), so the values here - * are merged — not overwritten — with any tool-derived betas. - */ -function createClaudeOAuthProvider(config: ProviderConfig): ModelFactory { - // Stable per-provider session id — mirrors the Claude Code CLI, which sends - // the same `X-Claude-Code-Session-Id` across a session's requests. - const sessionId = randomUUID(); - - // Wrap the base fetch FIRST so the logging wrapper sees the genuine - // outgoing HTTP body — i.e. AFTER the OAuth body transform and AFTER the - // Claude-Code session headers have been stamped on. Order matters: if we - // wrapped the inner `baseFetch` instead, the logs would show the pre- - // transform body and miss the session headers, defeating the point of - // capturing the wire for cache/billing debugging. - const baseFetch = wrapFetchWithLogging(globalThis.fetch, { - tabId: config.tabId, - modelHint: "claude-oauth", - }); - - // Custom fetch that (1) restructures the request body into the genuine - // Claude Code system layout — required for Anthropic to bill correctly and - // apply the prompt-cache scope (see anthropic-oauth-transform.ts) — and - // (2) stamps the Claude Code session/request id headers the real CLI sends. - // Cast through `unknown`: `FetchFunction` is `typeof globalThis.fetch`, whose - // (Bun) type carries a `preconnect` member a plain wrapper can't satisfy. - const oauthFetch = (async ( - input: Parameters<FetchFunction>[0], - init?: Parameters<FetchFunction>[1], - ) => { - const nextInit: RequestInit = { ...init }; - if (init?.body != null) { - nextInit.body = transformClaudeOAuthBody(init.body) ?? init.body; - } - const headers = new Headers(init?.headers); - headers.set("X-Claude-Code-Session-Id", sessionId); - if (!headers.has("x-client-request-id")) { - headers.set("x-client-request-id", randomUUID()); - } - nextInit.headers = headers; - return baseFetch(input, nextInit); - }) as unknown as FetchFunction; - - const anthropic = createAnthropic({ - baseURL: config.baseURL || "https://api.anthropic.com/v1", - authToken: config.claudeCredentials?.accessToken ?? config.apiKey, - fetch: oauthFetch, - headers: { - "anthropic-beta": getAnthropicBetas().join(","), - "anthropic-dangerous-direct-browser-access": "true", - "x-app": "cli", - "user-agent": "claude-cli/2.1.112 (external, sdk-cli)", - }, - }); - return (modelId: string) => anthropic(modelId); -} - -/** - * Plain-API-key Anthropic-format provider. Used to hit gateways that speak - * Anthropic's `/messages` protocol with a standard `x-api-key` header — most - * importantly OpenCode Go's MiniMax and Qwen routes. Unlike the Claude OAuth - * variant, no `claudeCredentials` are present, no Claude Code mimicry headers - * are sent, and the API key is passed verbatim through the SDK's default - * authentication path. - */ -function createApiKeyAnthropicProvider(config: ProviderConfig): ModelFactory { - const loggingFetch = wrapFetchWithLogging(globalThis.fetch, { - tabId: config.tabId, - modelHint: "opencode-anthropic", - }) as unknown as FetchFunction; - - const anthropic = createAnthropic({ - apiKey: config.apiKey, - baseURL: config.baseURL || "https://opencode.ai/zen/go/v1", - fetch: loggingFetch, - }); - - return (modelId: string) => anthropic(modelId); -} - -export { prefixToolName, unprefixToolName }; diff --git a/packages/core/src/lsp/client.ts b/packages/core/src/lsp/client.ts deleted file mode 100644 index da0c916..0000000 --- a/packages/core/src/lsp/client.ts +++ /dev/null @@ -1,658 +0,0 @@ -import type { ChildProcessWithoutNullStreams } from "node:child_process"; -import { readFile } from "node:fs/promises"; -import { extname, isAbsolute, resolve } from "node:path"; -import { fileURLToPath, pathToFileURL } from "node:url"; -import { - createMessageConnection, - type MessageConnection, - StreamMessageReader, - StreamMessageWriter, -} from "vscode-jsonrpc/node"; -import type { Diagnostic } from "vscode-languageserver-types"; -import { languageIdForExtension } from "./language.js"; - -export type { Diagnostic } from "vscode-languageserver-types"; - -// ─── Timing constants (mirrors opencode) ───────────────────────── -const DIAGNOSTICS_DEBOUNCE_MS = 150; -const DIAGNOSTICS_DOCUMENT_WAIT_TIMEOUT_MS = 5_000; -const DIAGNOSTICS_FULL_WAIT_TIMEOUT_MS = 10_000; -const DIAGNOSTICS_REQUEST_TIMEOUT_MS = 3_000; -const INITIALIZE_TIMEOUT_MS = 45_000; - -// ─── LSP spec constants ────────────────────────────────────────── -const FILE_CHANGE_CREATED = 1; -const FILE_CHANGE_CHANGED = 2; -const TEXT_DOCUMENT_SYNC_INCREMENTAL = 2; - -/** - * A live spawned language-server process plus the `initializationOptions` to - * hand it. Produced by the server-spawning layer (`server.ts`) and consumed by - * `createLspClient`. - */ -export interface LspServerHandle { - process: ChildProcessWithoutNullStreams; - initialization?: Record<string, unknown>; -} - -interface ServerCapabilities { - textDocumentSync?: number | { change?: number }; - diagnosticProvider?: unknown; - [key: string]: unknown; -} - -interface DiagnosticRequestResult { - handled: boolean; - matched: boolean; - byFile: Map<string, Diagnostic[]>; -} - -interface CapabilityRegistration { - id: string; - method: string; - registerOptions?: { - identifier?: string; - workspaceDiagnostics?: boolean; - }; -} - -type DocumentDiagnosticReport = { - items?: Diagnostic[]; - relatedDocuments?: Record<string, DocumentDiagnosticReport>; -}; - -type WorkspaceDiagnosticReport = { - items?: { uri?: string; items?: Diagnostic[] }[]; -}; - -/** Public shape of a connected LSP client. */ -export interface LspClient { - readonly serverID: string; - readonly root: string; - readonly connection: MessageConnection; - /** - * Open (or re-sync) a file with the server. Returns the document version - * sent — pass it to `waitForDiagnostics` to wait for diagnostics matching - * this exact sync. - */ - notifyOpen(path: string): Promise<number>; - /** Snapshot of all known diagnostics keyed by absolute file path. */ - readonly diagnostics: Map<string, Diagnostic[]>; - /** Wait until diagnostics for `path` settle (push and/or pull). */ - waitForDiagnostics(request: { - path: string; - version: number; - mode?: "document" | "full"; - after?: number; - }): Promise<void>; - /** Generic LSP request passthrough (hover, definition, references, …). */ - request<T = unknown>(method: string, params: unknown): Promise<T | null>; - /** Shut the connection and child process down. */ - shutdown(): Promise<void>; -} - -function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> { - return new Promise<T>((resolvePromise, reject) => { - const timer = setTimeout(() => reject(new Error(`LSP request timed out after ${ms}ms`)), ms); - promise.then( - (value) => { - clearTimeout(timer); - resolvePromise(value); - }, - (err) => { - clearTimeout(timer); - reject(err); - }, - ); - }); -} - -function getFilePath(uri: string): string | undefined { - if (!uri.startsWith("file://")) return undefined; - return fileURLToPath(uri); -} - -function getSyncKind(capabilities?: ServerCapabilities): number | undefined { - if (!capabilities) return undefined; - const sync = capabilities.textDocumentSync; - if (typeof sync === "number") return sync; - return sync?.change; -} - -function endPosition(text: string) { - const lines = text.split(/\r\n|\r|\n/); - return { line: lines.length - 1, character: lines.at(-1)?.length ?? 0 }; -} - -function dedupeDiagnostics(items: Diagnostic[]): Diagnostic[] { - const seen = new Set<string>(); - return items.filter((item) => { - const key = JSON.stringify({ - code: item.code, - severity: item.severity, - message: item.message, - source: item.source, - range: item.range, - }); - if (seen.has(key)) return false; - seen.add(key); - return true; - }); -} - -function configurationValue(settings: unknown, section?: string): unknown { - if (!section) return settings ?? null; - const result = section.split(".").reduce<unknown>((acc, key) => { - if (!acc || typeof acc !== "object" || !(key in acc)) return undefined; - return (acc as Record<string, unknown>)[key]; - }, settings); - return result ?? null; -} - -/** - * Create and initialize an LSP client over a spawned server's stdio. - * - * Performs the full `initialize`/`initialized` handshake (with a 45s timeout), - * wires push (`textDocument/publishDiagnostics`) and pull - * (`textDocument/diagnostic`, `workspace/diagnostic`) diagnostics, answers the - * `workspace/configuration`, `workspaceFolders`, and capability-registration - * requests servers commonly make, and returns a small client surface used by - * the manager and tools. Plain-TypeScript port of opencode's `lsp/client.ts`. - */ -export async function createLspClient(input: { - serverID: string; - server: LspServerHandle; - root: string; - directory: string; -}): Promise<LspClient> { - const { serverID, server, root, directory } = input; - - const connection = createMessageConnection( - new StreamMessageReader(server.process.stdout), - new StreamMessageWriter(server.process.stdin), - ); - - // Server stderr is routine for many tools (luau-lsp logs sourcemap status - // there). Keep it quiet unless debugging. - server.process.stderr?.on("data", () => { - /* swallowed — see opencode: stderr is mostly informational */ - }); - - // ─── Connection state ─── - const pushDiagnostics = new Map<string, Diagnostic[]>(); - const pullDiagnostics = new Map<string, Diagnostic[]>(); - const published = new Map<string, { at: number; version?: number }>(); - const diagnosticRegistrations = new Map<string, CapabilityRegistration>(); - const registrationListeners = new Set<() => void>(); - const diagnosticListeners = new Set<(input: { path: string; serverID: string }) => void>(); - const files: Record<string, { version: number; text: string }> = {}; - - const mergedDiagnostics = (filePath: string) => - dedupeDiagnostics([ - ...(pushDiagnostics.get(filePath) ?? []), - ...(pullDiagnostics.get(filePath) ?? []), - ]); - const updatePushDiagnostics = (filePath: string, next: Diagnostic[]) => { - pushDiagnostics.set(filePath, next); - for (const listener of diagnosticListeners) listener({ path: filePath, serverID }); - }; - const updatePullDiagnostics = (filePath: string, next: Diagnostic[]) => { - pullDiagnostics.set(filePath, next); - }; - const emitRegistrationChange = () => { - for (const listener of [...registrationListeners]) listener(); - }; - - // ─── Notification / request handlers ─── - connection.onNotification( - "textDocument/publishDiagnostics", - (params: { uri: string; diagnostics: Diagnostic[]; version?: number }) => { - const filePath = getFilePath(params.uri); - if (!filePath) return; - published.set(filePath, { - at: Date.now(), - version: typeof params.version === "number" ? params.version : undefined, - }); - updatePushDiagnostics(filePath, params.diagnostics); - }, - ); - connection.onRequest("window/workDoneProgress/create", () => null); - connection.onRequest("workspace/configuration", (params: { items?: { section?: string }[] }) => { - const items = params.items ?? []; - return items.map((item) => configurationValue(server.initialization, item.section)); - }); - connection.onRequest( - "client/registerCapability", - (params: { registrations?: CapabilityRegistration[] }) => { - const registrations = params.registrations ?? []; - let changed = false; - for (const registration of registrations) { - if (registration.method !== "textDocument/diagnostic") continue; - diagnosticRegistrations.set(registration.id, registration); - changed = true; - } - if (changed) emitRegistrationChange(); - return null; - }, - ); - connection.onRequest( - "client/unregisterCapability", - (params: { unregisterations?: { id: string; method: string }[] }) => { - const registrations = params.unregisterations ?? []; - let changed = false; - for (const registration of registrations) { - if (registration.method !== "textDocument/diagnostic") continue; - diagnosticRegistrations.delete(registration.id); - changed = true; - } - if (changed) emitRegistrationChange(); - return null; - }, - ); - connection.onRequest("workspace/workspaceFolders", () => [ - { name: "workspace", uri: pathToFileURL(root).href }, - ]); - connection.onRequest("workspace/diagnostic/refresh", () => null); - connection.listen(); - - // ─── Initialize handshake ─── - const initialized = await withTimeout( - connection.sendRequest<{ capabilities?: ServerCapabilities }>("initialize", { - rootUri: pathToFileURL(root).href, - processId: server.process.pid ?? null, - workspaceFolders: [{ name: "workspace", uri: pathToFileURL(root).href }], - initializationOptions: { ...server.initialization }, - capabilities: { - window: { workDoneProgress: true }, - workspace: { - configuration: true, - didChangeWatchedFiles: { dynamicRegistration: true }, - diagnostics: { refreshSupport: false }, - }, - textDocument: { - synchronization: { didOpen: true, didChange: true }, - diagnostic: { dynamicRegistration: true, relatedDocumentSupport: true }, - publishDiagnostics: { versionSupport: false }, - }, - }, - }), - INITIALIZE_TIMEOUT_MS, - ); - - const syncKind = getSyncKind(initialized.capabilities); - const hasStaticPullDiagnostics = Boolean(initialized.capabilities?.diagnosticProvider); - - await connection.sendNotification("initialized", {}); - if (server.initialization) { - await connection.sendNotification("workspace/didChangeConfiguration", { - settings: server.initialization, - }); - } - - // ─── Pull-diagnostics helpers ─── - const mergeResults = (filePath: string, results: DiagnosticRequestResult[]) => { - const handled = results.some((r) => r.handled); - const matched = results.some((r) => r.matched); - if (!handled) return { handled: false, matched: false }; - - const merged = new Map<string, Diagnostic[]>(); - for (const result of results) { - for (const [target, items] of result.byFile.entries()) { - merged.set(target, (merged.get(target) ?? []).concat(items)); - } - } - if (matched && !merged.has(filePath)) merged.set(filePath, []); - for (const [target, items] of merged.entries()) { - updatePullDiagnostics(target, dedupeDiagnostics(items)); - } - return { handled, matched }; - }; - - async function requestDiagnosticReport( - filePath: string, - identifier?: string, - ): Promise<DiagnosticRequestResult> { - const report = await withTimeout( - connection.sendRequest<DocumentDiagnosticReport | null>("textDocument/diagnostic", { - ...(identifier ? { identifier } : {}), - textDocument: { uri: pathToFileURL(filePath).href }, - }), - DIAGNOSTICS_REQUEST_TIMEOUT_MS, - ).catch(() => null); - const empty: DiagnosticRequestResult = { - handled: false, - matched: false, - byFile: new Map(), - }; - if (!report) return empty; - - const byFile = new Map<string, Diagnostic[]>(); - const push = (target: string, items: Diagnostic[]) => { - byFile.set(target, (byFile.get(target) ?? []).concat(items)); - }; - let handled = false; - let matched = false; - if (Array.isArray(report.items)) { - push(filePath, report.items); - handled = true; - matched = true; - } - for (const [uri, related] of Object.entries(report.relatedDocuments ?? {})) { - const relatedPath = getFilePath(uri); - if (!relatedPath || !Array.isArray(related.items)) continue; - push(relatedPath, related.items); - handled = true; - matched = matched || relatedPath === filePath; - } - return { handled, matched, byFile }; - } - - async function requestWorkspaceDiagnosticReport( - filePath: string, - identifier?: string, - ): Promise<DiagnosticRequestResult> { - const report = await withTimeout( - connection.sendRequest<WorkspaceDiagnosticReport | null>("workspace/diagnostic", { - ...(identifier ? { identifier } : {}), - previousResultIds: [], - }), - DIAGNOSTICS_REQUEST_TIMEOUT_MS, - ).catch(() => null); - if (!report) return { handled: false, matched: false, byFile: new Map() }; - - const byFile = new Map<string, Diagnostic[]>(); - let matched = false; - for (const item of report.items ?? []) { - const relatedPath = item.uri ? getFilePath(item.uri) : undefined; - if (!relatedPath || !Array.isArray(item.items)) continue; - byFile.set(relatedPath, (byFile.get(relatedPath) ?? []).concat(item.items)); - matched = matched || relatedPath === filePath; - } - return { handled: true, matched, byFile }; - } - - function documentPullState() { - const documentRegistrations = [...diagnosticRegistrations.values()].filter( - (r) => r.registerOptions?.workspaceDiagnostics !== true, - ); - return { - documentIdentifiers: [ - ...new Set(documentRegistrations.flatMap((r) => r.registerOptions?.identifier ?? [])), - ], - supported: hasStaticPullDiagnostics || documentRegistrations.length > 0, - }; - } - - function workspacePullState() { - const workspaceRegistrations = [...diagnosticRegistrations.values()].filter( - (r) => r.registerOptions?.workspaceDiagnostics === true, - ); - return { - workspaceIdentifiers: [ - ...new Set(workspaceRegistrations.flatMap((r) => r.registerOptions?.identifier ?? [])), - ], - supported: workspaceRegistrations.length > 0, - }; - } - - const hasCurrentFileDiagnostics = (filePath: string, results: DiagnosticRequestResult[]) => - results.some((r) => (r.byFile.get(filePath)?.length ?? 0) > 0); - - async function requestDiagnostics( - filePath: string, - requests: Promise<DiagnosticRequestResult>[], - done: (results: DiagnosticRequestResult[]) => boolean, - ) { - if (!requests.length) return { handled: false, matched: false }; - const results: DiagnosticRequestResult[] = []; - return new Promise<{ handled: boolean; matched: boolean }>((resolvePromise) => { - let pending = requests.length; - let resolved = false; - const finish = (merged: { handled: boolean; matched: boolean }, force = false) => { - if (resolved) return; - if (!force && !done(results)) return; - resolved = true; - resolvePromise(merged); - }; - for (const request of requests) { - request.then((result) => { - results.push(result); - pending -= 1; - const merged = mergeResults(filePath, results); - finish(merged); - if (pending === 0) finish(merged, true); - }); - } - }); - } - - async function requestDocumentDiagnostics(filePath: string) { - const state = documentPullState(); - if (!state.supported) return { handled: false, matched: false }; - return requestDiagnostics( - filePath, - [ - requestDiagnosticReport(filePath), - ...state.documentIdentifiers.map((id) => requestDiagnosticReport(filePath, id)), - ], - (results) => hasCurrentFileDiagnostics(filePath, results), - ); - } - - async function requestFullDiagnostics(filePath: string) { - const documentState = documentPullState(); - const workspaceState = workspacePullState(); - if (!documentState.supported && !workspaceState.supported) { - return { handled: false, matched: false }; - } - return mergeResults( - filePath, - await Promise.all([ - ...(documentState.supported ? [requestDiagnosticReport(filePath)] : []), - ...documentState.documentIdentifiers.map((id) => requestDiagnosticReport(filePath, id)), - ...(workspaceState.supported ? [requestWorkspaceDiagnosticReport(filePath)] : []), - ...workspaceState.workspaceIdentifiers.map((id) => - requestWorkspaceDiagnosticReport(filePath, id), - ), - ]), - ); - } - - function waitForRegistrationChange(timeout: number) { - if (timeout <= 0) return Promise.resolve(false); - return new Promise<boolean>((resolvePromise) => { - let finished = false; - let timer: ReturnType<typeof setTimeout> | undefined; - const finish = (result: boolean) => { - if (finished) return; - finished = true; - if (timer) clearTimeout(timer); - registrationListeners.delete(listener); - resolvePromise(result); - }; - const listener = () => finish(true); - registrationListeners.add(listener); - timer = setTimeout(() => finish(false), timeout); - }); - } - - function waitForFreshPush(request: { - path: string; - version: number; - after: number; - timeout: number; - }) { - if (request.timeout <= 0) return Promise.resolve(false); - return new Promise<boolean>((resolvePromise) => { - let finished = false; - let debounceTimer: ReturnType<typeof setTimeout> | undefined; - let timeoutTimer: ReturnType<typeof setTimeout> | undefined; - let unsub: (() => void) | undefined; - const finish = (result: boolean) => { - if (finished) return; - finished = true; - if (debounceTimer) clearTimeout(debounceTimer); - if (timeoutTimer) clearTimeout(timeoutTimer); - unsub?.(); - resolvePromise(result); - }; - const schedule = () => { - const hit = published.get(request.path); - if (!hit) return; - if (typeof hit.version === "number" && hit.version !== request.version) return; - if (hit.at < request.after && hit.version !== request.version) return; - if (debounceTimer) clearTimeout(debounceTimer); - debounceTimer = setTimeout( - () => finish(true), - Math.max(0, DIAGNOSTICS_DEBOUNCE_MS - (Date.now() - hit.at)), - ); - }; - timeoutTimer = setTimeout(() => finish(false), request.timeout); - const listener = (event: { path: string; serverID: string }) => { - if (event.path !== request.path || event.serverID !== serverID) return; - schedule(); - }; - diagnosticListeners.add(listener); - unsub = () => diagnosticListeners.delete(listener); - schedule(); - }); - } - - async function waitForDocumentDiagnostics(request: { - path: string; - version: number; - after?: number; - }) { - const startedAt = request.after ?? Date.now(); - const pushWait = waitForFreshPush({ - path: request.path, - version: request.version, - after: startedAt, - timeout: DIAGNOSTICS_DOCUMENT_WAIT_TIMEOUT_MS, - }); - while (Date.now() - startedAt < DIAGNOSTICS_DOCUMENT_WAIT_TIMEOUT_MS) { - const result = await requestDocumentDiagnostics(request.path); - if (result.matched) return; - const remaining = DIAGNOSTICS_DOCUMENT_WAIT_TIMEOUT_MS - (Date.now() - startedAt); - if (remaining <= 0) return; - const next = await Promise.race([ - pushWait.then((ready) => (ready ? "push" : "timeout")), - waitForRegistrationChange(remaining).then((c) => (c ? "registration" : "timeout")), - ]); - if (next !== "registration") return; - } - } - - async function waitForFullDiagnostics(request: { - path: string; - version: number; - after?: number; - }) { - const startedAt = request.after ?? Date.now(); - const pushWait = waitForFreshPush({ - path: request.path, - version: request.version, - after: startedAt, - timeout: DIAGNOSTICS_FULL_WAIT_TIMEOUT_MS, - }); - while (Date.now() - startedAt < DIAGNOSTICS_FULL_WAIT_TIMEOUT_MS) { - const result = await requestFullDiagnostics(request.path); - if (result.handled || result.matched) return; - const remaining = DIAGNOSTICS_FULL_WAIT_TIMEOUT_MS - (Date.now() - startedAt); - if (remaining <= 0) return; - const next = await Promise.race([ - pushWait.then((ready) => (ready ? "push" : "timeout")), - waitForRegistrationChange(remaining).then((c) => (c ? "registration" : "timeout")), - ]); - if (next !== "registration") return; - } - } - - const normalize = (p: string) => (isAbsolute(p) ? p : resolve(directory, p)); - - // ─── Public surface ─── - const client: LspClient = { - serverID, - root, - connection, - async notifyOpen(path: string) { - const filePath = normalize(path); - const text = await readFile(filePath, "utf8"); - const languageId = languageIdForExtension(extname(filePath)); - const uri = pathToFileURL(filePath).href; - const document = files[filePath]; - - if (document !== undefined) { - await connection.sendNotification("workspace/didChangeWatchedFiles", { - changes: [{ uri, type: FILE_CHANGE_CHANGED }], - }); - const next = document.version + 1; - files[filePath] = { version: next, text }; - await connection.sendNotification("textDocument/didChange", { - textDocument: { uri, version: next }, - contentChanges: - syncKind === TEXT_DOCUMENT_SYNC_INCREMENTAL - ? [ - { - range: { - start: { line: 0, character: 0 }, - end: endPosition(document.text), - }, - text, - }, - ] - : [{ text }], - }); - return next; - } - - await connection.sendNotification("workspace/didChangeWatchedFiles", { - changes: [{ uri, type: FILE_CHANGE_CREATED }], - }); - pushDiagnostics.delete(filePath); - pullDiagnostics.delete(filePath); - await connection.sendNotification("textDocument/didOpen", { - textDocument: { uri, languageId, version: 0, text }, - }); - files[filePath] = { version: 0, text }; - return 0; - }, - get diagnostics() { - const result = new Map<string, Diagnostic[]>(); - for (const key of new Set([...pushDiagnostics.keys(), ...pullDiagnostics.keys()])) { - result.set(key, mergedDiagnostics(key)); - } - return result; - }, - async waitForDiagnostics(request) { - const normalizedPath = normalize(request.path); - if (request.mode === "document") { - await waitForDocumentDiagnostics({ - path: normalizedPath, - version: request.version, - after: request.after, - }); - return; - } - await waitForFullDiagnostics({ - path: normalizedPath, - version: request.version, - after: request.after, - }); - }, - async request<T = unknown>(method: string, params: unknown): Promise<T | null> { - return connection.sendRequest<T>(method, params).catch(() => null); - }, - async shutdown() { - try { - connection.end(); - connection.dispose(); - } catch { - /* connection may already be closed */ - } - server.process.kill(); - }, - }; - - return client; -} diff --git a/packages/core/src/lsp/diagnostic.ts b/packages/core/src/lsp/diagnostic.ts deleted file mode 100644 index 1ad4d0f..0000000 --- a/packages/core/src/lsp/diagnostic.ts +++ /dev/null @@ -1,41 +0,0 @@ -import type { Diagnostic } from "vscode-languageserver-types"; - -/** - * Diagnostic formatting helpers. Ported from opencode's `lsp/diagnostic.ts`. - * - * LSP positions are 0-based on the wire; we render them 1-based (editor-style) - * so they line up with what `read_file` shows and what editors report. - */ - -/** Max diagnostics rendered per file before truncating with a "… and N more". */ -const MAX_PER_FILE = 20; - -const SEVERITY_LABEL: Record<number, string> = { - 1: "ERROR", - 2: "WARN", - 3: "INFO", - 4: "HINT", -}; - -/** Render a single diagnostic as `SEVERITY [line:col] message` (1-based). */ -export function pretty(diagnostic: Diagnostic): string { - const severity = SEVERITY_LABEL[diagnostic.severity ?? 1] ?? "ERROR"; - const line = diagnostic.range.start.line + 1; - const col = diagnostic.range.start.character + 1; - return `${severity} [${line}:${col}] ${diagnostic.message}`; -} - -/** - * Build a `<diagnostics file="…">` block for a file's ERROR-severity - * diagnostics, or `""` when there are none. Errors only — warnings/info/hints - * are intentionally omitted so the model is nudged toward the things that - * actually break the build (matching opencode's behavior). - */ -export function report(file: string, issues: Diagnostic[]): string { - const errors = issues.filter((item) => item.severity === 1); - if (errors.length === 0) return ""; - const limited = errors.slice(0, MAX_PER_FILE); - const more = errors.length - MAX_PER_FILE; - const suffix = more > 0 ? `\n... and ${more} more` : ""; - return `<diagnostics file="${file}">\n${limited.map(pretty).join("\n")}${suffix}\n</diagnostics>`; -} diff --git a/packages/core/src/lsp/index.ts b/packages/core/src/lsp/index.ts deleted file mode 100644 index fd43c2f..0000000 --- a/packages/core/src/lsp/index.ts +++ /dev/null @@ -1,18 +0,0 @@ -// LSP (Language Server Protocol) integration. -// -// Config-driven only: servers are declared in `dispatch.toml`'s `[lsp.<id>]` -// block (see `LspServerConfig` in `../types`). There is no builtin server -// registry and no auto-download. The primary model-facing surface is -// diagnostics-on-write (the host passes a write hook that calls `touchFile` + -// `report`); an on-demand `lsp` tool exposes hover/definition/references too. - -export { - createLspClient, - type Diagnostic, - type LspClient, - type LspServerHandle, -} from "./client.js"; -export { pretty, report } from "./diagnostic.js"; -export { LANGUAGE_EXTENSIONS, languageIdForExtension } from "./language.js"; -export { LspManager } from "./manager.js"; -export { type ResolvedLspServer, resolveServersFromConfig } from "./server.js"; diff --git a/packages/core/src/lsp/language.ts b/packages/core/src/lsp/language.ts deleted file mode 100644 index 3e9fe68..0000000 --- a/packages/core/src/lsp/language.ts +++ /dev/null @@ -1,72 +0,0 @@ -/** - * File-extension → LSP `languageId` map. - * - * The LSP `textDocument/didOpen` notification carries a `languageId` string - * that tells the server how to parse the document. This table is a trimmed - * port of opencode's `lsp/language.ts`, with one critical addition for this - * project: `.luau` → `"luau"`. Roblox Luau sources use the `.luau` extension, - * which standard Lua tooling does not recognise — luau-lsp expects the - * `"luau"` languageId. - * - * Extensions are looked up with their leading dot (e.g. `".luau"`). Unknown - * extensions fall back to `"plaintext"` at the call site. - */ -export const LANGUAGE_EXTENSIONS: Record<string, string> = { - // Luau (Roblox) — the reason this module exists. Keep first for visibility. - ".luau": "luau", - ".lua": "lua", - // A pragmatic subset of common languages, mirroring opencode's table so a - // user can point an arbitrary LSP server at this codebase and have the - // right languageId reported. - ".c": "c", - ".cpp": "cpp", - ".cc": "cpp", - ".cxx": "cpp", - ".h": "c", - ".hpp": "cpp", - ".cs": "csharp", - ".css": "css", - ".dart": "dart", - ".go": "go", - ".html": "html", - ".htm": "html", - ".java": "java", - ".js": "javascript", - ".jsx": "javascriptreact", - ".json": "json", - ".jsonc": "jsonc", - ".kt": "kotlin", - ".kts": "kotlin", - ".md": "markdown", - ".markdown": "markdown", - ".php": "php", - ".py": "python", - ".rb": "ruby", - ".rs": "rust", - ".scss": "scss", - ".sass": "sass", - ".sh": "shellscript", - ".bash": "shellscript", - ".zsh": "shellscript", - ".sql": "sql", - ".svelte": "svelte", - ".swift": "swift", - ".toml": "toml", - ".ts": "typescript", - ".tsx": "typescriptreact", - ".mts": "typescript", - ".cts": "typescript", - ".vue": "vue", - ".xml": "xml", - ".yaml": "yaml", - ".yml": "yaml", - ".zig": "zig", -}; - -/** - * Resolve the LSP `languageId` for a file path's extension, falling back to - * `"plaintext"` when the extension is unknown. - */ -export function languageIdForExtension(extension: string): string { - return LANGUAGE_EXTENSIONS[extension] ?? "plaintext"; -} diff --git a/packages/core/src/lsp/manager.ts b/packages/core/src/lsp/manager.ts deleted file mode 100644 index db8b68e..0000000 --- a/packages/core/src/lsp/manager.ts +++ /dev/null @@ -1,220 +0,0 @@ -import { extname } from "node:path"; -import { createLspClient, type Diagnostic, type LspClient } from "./client.js"; -import type { ResolvedLspServer } from "./server.js"; - -/** - * Process-wide owner of LSP client lifecycles. - * - * Clients are keyed by `root + serverID` and spawned lazily on the first file - * that matches a server's extensions, then reused. Concurrent spawns for the - * same key are de-duplicated via an in-flight map, and servers that fail to - * start are remembered in `broken` so we don't spawn-spam. Modeled on - * opencode's `lsp/lsp.ts` `getClients` flow, minus the Effect machinery. - * - * The manager is config-agnostic: callers resolve `ResolvedLspServer[]` from a - * tab's working-directory config (`resolveServersFromConfig`) and pass them in - * alongside the `root`. This keeps per-working-directory config out of the - * manager while letting it own all the long-lived processes for the process. - */ -export class LspManager { - private clients = new Map<string, LspClient>(); - private spawning = new Map<string, Promise<LspClient | undefined>>(); - private broken = new Set<string>(); - - private key(root: string, serverID: string): string { - return `${root}\u0000${serverID}`; - } - - private serversForFile(file: string, servers: ResolvedLspServer[]): ResolvedLspServer[] { - const extension = extname(file) || file; - return servers.filter( - (server) => server.extensions.length === 0 || server.extensions.includes(extension), - ); - } - - /** - * True if any provided server is configured to attach to this file's - * extension (regardless of whether it has spawned yet). Used to decide - * whether an LSP operation is even applicable to a file. - */ - hasServerForFile(file: string, servers: ResolvedLspServer[]): boolean { - return this.serversForFile(file, servers).length > 0; - } - - /** - * Get (spawning if needed) all clients that should attach to `file` at - * `root`. Spawn failures are swallowed (logged via `broken`) and simply - * yield fewer clients — callers degrade gracefully to "no diagnostics". - */ - async getClients(input: { - file: string; - root: string; - servers: ResolvedLspServer[]; - }): Promise<LspClient[]> { - const { file, root, servers } = input; - const matching = this.serversForFile(file, servers); - const result: LspClient[] = []; - - for (const server of matching) { - const key = this.key(root, server.id); - if (this.broken.has(key)) continue; - - const existing = this.clients.get(key); - if (existing) { - result.push(existing); - continue; - } - - const inflight = this.spawning.get(key); - if (inflight) { - const client = await inflight; - if (client) result.push(client); - continue; - } - - const task = this.spawn(server, root, key); - this.spawning.set(key, task); - task.finally(() => { - if (this.spawning.get(key) === task) this.spawning.delete(key); - }); - const client = await task; - if (client) result.push(client); - } - - return result; - } - - private async spawn( - server: ResolvedLspServer, - root: string, - key: string, - ): Promise<LspClient | undefined> { - let handle: ReturnType<ResolvedLspServer["spawn"]>; - try { - handle = server.spawn(root); - } catch (err) { - this.broken.add(key); - console.warn( - `dispatch: failed to spawn LSP server "${server.id}": ${err instanceof Error ? err.message : String(err)}`, - ); - return undefined; - } - - // A spawn that fails asynchronously (e.g. ENOENT — binary not on PATH) - // emits `error` on the child process; mark broken so we don't retry it. - handle.process.on("error", (err) => { - this.broken.add(key); - console.warn(`dispatch: LSP server "${server.id}" process error: ${err.message}`); - }); - - try { - const client = await createLspClient({ - serverID: server.id, - server: handle, - root, - directory: root, - }); - // A racing caller may have created the same client; prefer the - // existing one and discard ours. - const existing = this.clients.get(key); - if (existing) { - await client.shutdown(); - return existing; - } - this.clients.set(key, client); - return client; - } catch (err) { - this.broken.add(key); - try { - handle.process.kill(); - } catch { - /* already dead */ - } - console.warn( - `dispatch: failed to initialize LSP client "${server.id}": ${err instanceof Error ? err.message : String(err)}`, - ); - return undefined; - } - } - - /** - * Open/sync a file with its clients and (optionally) wait for diagnostics - * to settle. `mode: "document"` waits for the file's own diagnostics; - * `"full"` also waits on workspace diagnostics; omitted just syncs. - */ - async touchFile(input: { - file: string; - root: string; - servers: ResolvedLspServer[]; - mode?: "document" | "full"; - }): Promise<void> { - const clients = await this.getClients(input); - await Promise.all( - clients.map(async (client) => { - const after = Date.now(); - const version = await client.notifyOpen(input.file); - if (!input.mode) return; - await client.waitForDiagnostics({ - path: input.file, - version, - mode: input.mode, - after, - }); - }), - ).catch((err) => { - console.warn( - `dispatch: failed to touch file for LSP: ${err instanceof Error ? err.message : String(err)}`, - ); - }); - } - - /** - * Merged diagnostics for a single file across all of its clients, keyed by - * absolute file path. Includes related-file diagnostics a client surfaced - * (e.g. workspace pulls), so the result map may contain more than `file`. - */ - getDiagnostics(input: { - root: string; - servers: ResolvedLspServer[]; - file: string; - }): Record<string, Diagnostic[]> { - const results: Record<string, Diagnostic[]> = {}; - const matching = this.serversForFile(input.file, input.servers); - for (const server of matching) { - const client = this.clients.get(this.key(input.root, server.id)); - if (!client) continue; - for (const [path, diags] of client.diagnostics.entries()) { - results[path] = (results[path] ?? []).concat(diags); - } - } - return results; - } - - /** - * Run a positional LSP request (hover/definition/references/etc.) against - * every client for the file and flatten the (non-null) results. `line`/ - * `character` are 0-based here — the caller converts from editor 1-based. - */ - async request(input: { - file: string; - root: string; - servers: ResolvedLspServer[]; - method: string; - params: Record<string, unknown>; - }): Promise<unknown[]> { - const clients = await this.getClients(input); - const results = await Promise.all( - clients.map((client) => client.request(input.method, input.params)), - ); - return results.filter((r) => r !== null && r !== undefined); - } - - /** Shut down every live client and clear all state. */ - async shutdownAll(): Promise<void> { - const clients = [...this.clients.values()]; - this.clients.clear(); - this.spawning.clear(); - this.broken.clear(); - await Promise.all(clients.map((client) => client.shutdown().catch(() => {}))); - } -} diff --git a/packages/core/src/lsp/server.ts b/packages/core/src/lsp/server.ts deleted file mode 100644 index 1fb002e..0000000 --- a/packages/core/src/lsp/server.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { type ChildProcessWithoutNullStreams, spawn } from "node:child_process"; -import type { LspServerConfig } from "../types/index.js"; -import type { LspServerHandle } from "./client.js"; - -/** - * A resolved, ready-to-spawn LSP server derived from a `dispatch.toml` - * `[lsp.<id>]` entry. Config-driven only — dispatch ships no builtin server - * registry and performs no auto-download (unlike opencode). The declared - * executable (`command[0]`) must already be on PATH. - */ -export interface ResolvedLspServer { - id: string; - /** Extensions (with leading dot) this server attaches to, e.g. `".luau"`. */ - extensions: string[]; - /** Launch the server over stdio rooted at `root`. */ - spawn(root: string): LspServerHandle; -} - -/** - * Spawn a child process for an LSP server over stdio. Inherits `process.env` - * (so a PATH-resident `rojo` is visible to luau-lsp's sourcemap autogenerate) - * and merges any `env` from the server config on top. - */ -function spawnServer( - command: string[], - cwd: string, - env: Record<string, string> | undefined, - initialization: Record<string, unknown> | undefined, -): LspServerHandle { - const [cmd, ...args] = command; - if (!cmd) throw new Error("LSP server command is empty"); - const proc = spawn(cmd, args, { - cwd, - env: { ...process.env, ...env }, - stdio: ["pipe", "pipe", "pipe"], - }) as ChildProcessWithoutNullStreams; - return { - process: proc, - ...(initialization ? { initialization } : {}), - }; -} - -/** - * Turn the parsed `dispatch.toml` `lsp` block into a list of spawnable - * servers. Disabled entries are dropped. Entries with no `command`/`extensions` - * are skipped defensively (the config validator already enforces these, but we - * guard here too so a hand-built config object can't crash the manager). - */ -export function resolveServersFromConfig( - lsp: Record<string, LspServerConfig> | undefined, -): ResolvedLspServer[] { - if (!lsp) return []; - const servers: ResolvedLspServer[] = []; - for (const [id, entry] of Object.entries(lsp)) { - if (entry.disabled) continue; - if (!entry.command || entry.command.length === 0) continue; - if (!entry.extensions || entry.extensions.length === 0) continue; - const command = entry.command; - const env = entry.env; - const initialization = entry.initialization; - servers.push({ - id, - extensions: entry.extensions, - spawn: (root: string) => spawnServer(command, root, env, initialization), - }); - } - return servers; -} diff --git a/packages/core/src/models/attachments.ts b/packages/core/src/models/attachments.ts deleted file mode 100644 index 5c98db4..0000000 --- a/packages/core/src/models/attachments.ts +++ /dev/null @@ -1,151 +0,0 @@ -// Validation + limits for multimodal user attachments (images / PDFs). -// -// Kept dependency-free (no DB / `bun:sqlite` import) so both the API layer -// (`/chat` request validation) and any future caller can share the exact same -// allowlist and size/count ceilings. The limits mirror Anthropic's documented -// vision/PDF API constraints (the only image-capable providers Dispatch maps), -// so a request that passes here won't be rejected by the provider for size. - -import type { UserAttachmentPart, UserContentPart } from "../types/index.js"; - -/** Accepted image media types. */ -export const ACCEPTED_IMAGE_MEDIA_TYPES = [ - "image/png", - "image/jpeg", - "image/webp", - "image/gif", -] as const; - -/** Accepted document media types. */ -export const ACCEPTED_PDF_MEDIA_TYPE = "application/pdf"; - -/** Every media type we accept as an attachment. */ -export const ACCEPTED_ATTACHMENT_MEDIA_TYPES = [ - ...ACCEPTED_IMAGE_MEDIA_TYPES, - ACCEPTED_PDF_MEDIA_TYPE, -] as const; - -/** Per-image byte ceiling (Anthropic: 5 MB/image). */ -export const MAX_IMAGE_BYTES = 5 * 1024 * 1024; - -/** Per-PDF byte ceiling (Anthropic: 32 MB/PDF). */ -export const MAX_PDF_BYTES = 32 * 1024 * 1024; - -/** Max attachments per message (Anthropic: 20 images/request). */ -export const MAX_ATTACHMENTS = 20; - -/** - * Total attachment payload ceiling for a single request (decoded bytes). Bounds - * the overall request size even when each individual file is within its limit. - */ -export const MAX_TOTAL_ATTACHMENT_BYTES = 32 * 1024 * 1024; - -/** Whether a media type is an accepted image type. */ -export function isImageMediaType(mediaType: string): boolean { - return (ACCEPTED_IMAGE_MEDIA_TYPES as readonly string[]).includes(mediaType); -} - -/** Whether a media type is the accepted PDF type. */ -export function isPdfMediaType(mediaType: string): boolean { - return mediaType === ACCEPTED_PDF_MEDIA_TYPE; -} - -/** Whether a media type is an accepted attachment type at all. */ -export function isAcceptedAttachmentMediaType(mediaType: string): boolean { - return (ACCEPTED_ATTACHMENT_MEDIA_TYPES as readonly string[]).includes(mediaType); -} - -/** - * Decoded byte length of a base64 string, computed WITHOUT allocating the - * decoded buffer. Tolerates an optional `data:<mediaType>;base64,` prefix and - * any embedded whitespace/newlines. Returns 0 for an empty/whitespace string. - */ -export function base64ByteLength(b64: string): number { - // Strip a data-URI prefix if present. - const comma = b64.indexOf(","); - const body = b64.startsWith("data:") && comma !== -1 ? b64.slice(comma + 1) : b64; - let len = 0; - let pad = 0; - for (let i = 0; i < body.length; i++) { - const ch = body.charCodeAt(i); - // Skip whitespace (space, \t, \n, \r). - if (ch === 32 || ch === 9 || ch === 10 || ch === 13) continue; - len++; - if (body[i] === "=") pad++; - } - if (len === 0) return 0; - // 4 base64 chars → 3 bytes, minus padding. - return Math.floor((len * 3) / 4) - pad; -} - -export type AttachmentValidationError = - | { code: "unsupported-type"; mediaType: string } - | { code: "image-too-large"; mediaType: string; bytes: number; limit: number } - | { code: "pdf-too-large"; bytes: number; limit: number } - | { code: "too-many"; count: number; limit: number } - | { code: "total-too-large"; bytes: number; limit: number } - | { code: "empty"; mediaType: string }; - -export interface AttachmentValidationResult { - ok: boolean; - errors: AttachmentValidationError[]; -} - -/** Extract just the attachment parts from a mixed content list. */ -function attachmentsOf(content: UserContentPart[]): UserAttachmentPart[] { - return content.filter((p): p is UserAttachmentPart => p.type === "attachment"); -} - -/** - * Validate the attachments in a multimodal user content list against the - * media-type allowlist and the size/count ceilings. Pure: never throws, - * collects every violation so the caller can report them all at once. - * - * Text parts are ignored (always valid). An empty content list is valid (it's - * just a text-only message expressed as parts). - */ -export function validateUserContent(content: UserContentPart[]): AttachmentValidationResult { - const errors: AttachmentValidationError[] = []; - const attachments = attachmentsOf(content); - - if (attachments.length > MAX_ATTACHMENTS) { - errors.push({ code: "too-many", count: attachments.length, limit: MAX_ATTACHMENTS }); - } - - let total = 0; - for (const att of attachments) { - if (!isAcceptedAttachmentMediaType(att.mediaType)) { - errors.push({ code: "unsupported-type", mediaType: att.mediaType }); - continue; - } - const bytes = base64ByteLength(att.data); - total += bytes; - if (bytes === 0) { - errors.push({ code: "empty", mediaType: att.mediaType }); - continue; - } - if (isPdfMediaType(att.mediaType)) { - if (bytes > MAX_PDF_BYTES) { - errors.push({ code: "pdf-too-large", bytes, limit: MAX_PDF_BYTES }); - } - } else if (bytes > MAX_IMAGE_BYTES) { - errors.push({ - code: "image-too-large", - mediaType: att.mediaType, - bytes, - limit: MAX_IMAGE_BYTES, - }); - } - } - - if (total > MAX_TOTAL_ATTACHMENT_BYTES) { - errors.push({ code: "total-too-large", bytes: total, limit: MAX_TOTAL_ATTACHMENT_BYTES }); - } - - return { ok: errors.length === 0, errors }; -} - -/** Convenience: does the content list contain at least one attachment? */ -export function hasAttachments(content: UserContentPart[] | undefined | null): boolean { - return !!content && content.some((p) => p.type === "attachment"); -} diff --git a/packages/core/src/models/catalog.ts b/packages/core/src/models/catalog.ts deleted file mode 100644 index ac310b1..0000000 --- a/packages/core/src/models/catalog.ts +++ /dev/null @@ -1,229 +0,0 @@ -import { mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs"; -import { dirname } from "node:path"; - -/** - * models.dev-backed model catalog. Resolves a model's MAXIMUM context window - * (`limit.context`) dynamically from the public models.dev API, mirroring how - * opencode determines per-model context limits — no hardcoded table. - * - * The catalog is fetched once, cached on disk with a short TTL, and reused. On - * fetch failure we fall back to a stale-but-present cache so the lookup keeps - * working offline. Lookups never throw: an unknown/unreachable model resolves - * to `null`, which the UI renders as "max unknown". - */ - -/** Shape of the slice of models.dev's `/api.json` we consume. */ -interface ModelsDevModel { - limit?: { - context?: number; - output?: number; - }; - /** - * Input/output modalities the model accepts. We read `input` to decide - * whether the model can take image / pdf attachments. Absent on older - * catalog entries — treated as "unknown" (capability resolves to `null`). - */ - modalities?: { - input?: string[]; - output?: string[]; - }; -} - -interface ModelsDevProvider { - id: string; - models: Record<string, ModelsDevModel | undefined>; -} - -type ModelsDevCatalog = Record<string, ModelsDevProvider | undefined>; - -/** Where models.dev's API lives. Overridable for tests / private mirrors. */ -const MODELS_URL = process.env.DISPATCH_MODELS_URL || "https://models.dev"; - -/** Disk cache path (reuses the repo's `/tmp/dispatch` convention). */ -const CACHE_PATH = "/tmp/dispatch/models-dev.json"; - -/** How long a cached catalog stays fresh before we re-fetch. */ -const CACHE_TTL_MS = 5 * 60 * 1000; - -/** Network timeout for the catalog fetch. */ -const FETCH_TIMEOUT_MS = 10_000; - -/** - * After a failed fetch we memoize the fallback for this long before retrying, - * so a sustained outage doesn't make every lookup hang on a fresh timeout. - */ -const FETCH_PENALTY_MS = 60_000; - -/** - * Dispatch provider id → models.dev provider ids to search, in priority order. - * We only support Claude-backed providers (per product scope). `anthropic` and - * `opencode-anthropic` are both Claude; we try the first-party `anthropic` - * catalog first, then the `opencode` gateway catalog as a fallback. - */ -const PROVIDER_MAP: Record<string, string[]> = { - anthropic: ["anthropic", "opencode"], - "opencode-anthropic": ["anthropic", "opencode"], -}; - -/** In-process memoized catalog promise (one fetch/parse per TTL window). */ -let cached: { catalog: ModelsDevCatalog; fetchedAt: number } | null = null; -let inflight: Promise<ModelsDevCatalog> | null = null; - -function readDiskCache(): { catalog: ModelsDevCatalog; mtimeMs: number } | null { - try { - const stat = statSync(CACHE_PATH); - const text = readFileSync(CACHE_PATH, "utf-8"); - return { catalog: JSON.parse(text) as ModelsDevCatalog, mtimeMs: stat.mtimeMs }; - } catch { - return null; - } -} - -function writeDiskCache(text: string): void { - try { - mkdirSync(dirname(CACHE_PATH), { recursive: true }); - // Write-then-rename so a concurrent reader never sees a half-written - // file (rename is atomic on the same filesystem). The temp name is - // process-scoped to avoid two writers clobbering each other's temp. - const tmp = `${CACHE_PATH}.${process.pid}.tmp`; - writeFileSync(tmp, text, "utf-8"); - renameSync(tmp, CACHE_PATH); - } catch { - // Best-effort: a read-only /tmp shouldn't break lookups. - } -} - -async function fetchCatalog(): Promise<ModelsDevCatalog> { - const controller = new AbortController(); - const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS); - try { - const res = await fetch(`${MODELS_URL}/api.json`, { signal: controller.signal }); - if (!res.ok) throw new Error(`models.dev returned HTTP ${res.status}`); - const text = await res.text(); - const catalog = JSON.parse(text) as ModelsDevCatalog; - writeDiskCache(text); - return catalog; - } finally { - clearTimeout(timer); - } -} - -/** - * Load the models.dev catalog, preferring in-process memo, then a fresh disk - * cache, then a network fetch. On network failure, falls back to any stale - * disk cache; if nothing is available, returns an empty catalog. - */ -export async function getModelsCatalog(): Promise<ModelsDevCatalog> { - if (process.env.DISPATCH_DISABLE_MODELS_FETCH) { - const disk = readDiskCache(); - return disk?.catalog ?? {}; - } - - const now = Date.now(); - if (cached && now - cached.fetchedAt < CACHE_TTL_MS) return cached.catalog; - - // Fresh disk cache satisfies the request without a network round-trip. - const disk = readDiskCache(); - if (disk && now - disk.mtimeMs < CACHE_TTL_MS) { - // Inherit the file's mtime as `fetchedAt` so loading a disk cache into - // a fresh process doesn't reset its TTL (which would otherwise double - // the worst-case staleness across process boundaries). - cached = { catalog: disk.catalog, fetchedAt: disk.mtimeMs }; - return disk.catalog; - } - - if (!inflight) { - inflight = fetchCatalog() - .then((catalog) => { - cached = { catalog, fetchedAt: Date.now() }; - return catalog; - }) - .catch((err) => { - // Network failed — serve a stale cache if we have one. - console.warn( - `dispatch: failed to fetch models.dev catalog: ${err instanceof Error ? err.message : String(err)}`, - ); - const fallback = disk?.catalog ?? ({} as ModelsDevCatalog); - // Memoize the fallback with a short "penalty" TTL so a sustained - // outage doesn't make every lookup hang on a fresh 10s timeout. - // `fetchedAt` is backdated so the memo expires after FETCH_PENALTY_MS. - cached = { - catalog: fallback, - fetchedAt: Date.now() - CACHE_TTL_MS + FETCH_PENALTY_MS, - }; - return fallback; - }) - .finally(() => { - inflight = null; - }); - } - return inflight; -} - -/** - * Resolve a model's maximum context window (in tokens) for the given Dispatch - * provider + model id. Returns `null` when the provider is unsupported, the - * model is unknown, or the catalog is unavailable — callers should render that - * as "max unknown" (no denominator / percentage). - */ -export async function resolveContextLimit( - provider: string, - modelId: string, -): Promise<number | null> { - const candidates = PROVIDER_MAP[provider]; - if (!candidates || !modelId) return null; - - const catalog = await getModelsCatalog(); - for (const providerId of candidates) { - const ctx = catalog[providerId]?.models?.[modelId]?.limit?.context; - if (typeof ctx === "number" && ctx > 0) return ctx; - } - return null; -} - -/** - * Image / PDF input capabilities for a model, resolved from the models.dev - * catalog's `modalities.input` list. - */ -export interface ModelInputCapabilities { - /** Model accepts image input (vision). */ - image: boolean; - /** Model accepts PDF/document input. */ - pdf: boolean; -} - -/** - * Resolve whether a model accepts image / pdf input for the given Dispatch - * provider + model id. Returns `null` when the capability is UNKNOWN — i.e. the - * provider is unsupported/unmapped, the model is absent from the catalog, the - * entry predates the `modalities` field, or the catalog is unavailable. Callers - * should treat `null` as "can't verify" (optimistic allow) rather than a - * definitive "no", so a temporary catalog outage never disables a known-good - * vision model. - * - * A non-null result means the catalog DID describe the model's input modalities - * — `{ image, pdf }` then reflects exactly what it advertises (a definitive - * yes/no for each). - */ -export async function resolveModelCapabilities( - provider: string, - modelId: string, -): Promise<ModelInputCapabilities | null> { - const candidates = PROVIDER_MAP[provider]; - if (!candidates || !modelId) return null; - - const catalog = await getModelsCatalog(); - for (const providerId of candidates) { - const input = catalog[providerId]?.models?.[modelId]?.modalities?.input; - if (Array.isArray(input)) { - return { image: input.includes("image"), pdf: input.includes("pdf") }; - } - } - return null; -} - -/** Test-only: reset the in-process memo so a test can re-exercise loading. */ -export function __resetCatalogCacheForTests(): void { - cached = null; - inflight = null; -} diff --git a/packages/core/src/models/index.ts b/packages/core/src/models/index.ts deleted file mode 100644 index 15d1ee2..0000000 --- a/packages/core/src/models/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -export { - ACCEPTED_ATTACHMENT_MEDIA_TYPES, - ACCEPTED_IMAGE_MEDIA_TYPES, - ACCEPTED_PDF_MEDIA_TYPE, - type AttachmentValidationError, - type AttachmentValidationResult, - base64ByteLength, - hasAttachments, - isAcceptedAttachmentMediaType, - isImageMediaType, - isPdfMediaType, - MAX_ATTACHMENTS, - MAX_IMAGE_BYTES, - MAX_PDF_BYTES, - MAX_TOTAL_ATTACHMENT_BYTES, - validateUserContent, -} from "./attachments.js"; -export { - getModelsCatalog, - type ModelInputCapabilities, - resolveContextLimit, - resolveModelCapabilities, -} from "./catalog.js"; -export { ModelRegistry } from "./registry.js"; diff --git a/packages/core/src/models/registry.ts b/packages/core/src/models/registry.ts deleted file mode 100644 index 4a24a51..0000000 --- a/packages/core/src/models/registry.ts +++ /dev/null @@ -1,86 +0,0 @@ -import type { KeyDefinition, KeyState } from "../types/index.js"; - -export class ModelRegistry { - private keyStates: Map<string, KeyState>; - private keyOrder: string[]; - - constructor(keys: KeyDefinition[]) { - this.keyStates = new Map(); - this.keyOrder = []; - this._initConfig(keys, new Map()); - } - - private _initConfig(keys: KeyDefinition[], existingStates: Map<string, KeyState>): void { - this.keyOrder = keys.map((k) => k.id); - - const newStates = new Map<string, KeyState>(); - for (const key of keys) { - const existing = existingStates.get(key.id); - if (existing) { - // Preserve existing state but update definition - newStates.set(key.id, { ...existing, definition: key }); - } else { - newStates.set(key.id, { definition: key, status: "active" }); - } - } - this.keyStates = newStates; - } - - getKeys(): KeyState[] { - return this.keyOrder - .map((id) => this.keyStates.get(id)) - .filter((state): state is KeyState => state !== undefined); - } - - markKeyExhausted(keyId: string, error?: string): void { - const state = this.keyStates.get(keyId); - if (!state) return; - this.keyStates.set(keyId, { - ...state, - status: "exhausted", - lastError: error, - exhaustedAt: Date.now(), - }); - } - - markKeyActive(keyId: string): void { - const state = this.keyStates.get(keyId); - if (!state) return; - const updated: KeyState = { - definition: state.definition, - status: "active", - }; - this.keyStates.set(keyId, updated); - } - - hasAvailableKey(provider: string): boolean { - for (const state of this.keyStates.values()) { - if (state.definition.provider === provider && state.status === "active") { - return true; - } - } - return false; - } - - allKeysExhausted(): boolean { - for (const state of this.keyStates.values()) { - if (state.status === "active") { - return false; - } - } - return true; - } - - updateConfig(keys: KeyDefinition[]): void { - this._initConfig(keys, this.keyStates); - } - - // Internal: get ordered key states for a specific provider - getOrderedKeysForProvider(provider: string): KeyState[] { - return this.keyOrder - .map((id) => this.keyStates.get(id)) - .filter( - (state): state is KeyState => state !== undefined && state.definition.provider === provider, - ); - } -} diff --git a/packages/core/src/notifications/config.ts b/packages/core/src/notifications/config.ts deleted file mode 100644 index 49e6ff4..0000000 --- a/packages/core/src/notifications/config.ts +++ /dev/null @@ -1,77 +0,0 @@ -// Persisted ntfy config — single global JSON blob under one settings key. -// -// One global config (no per-user split): the rest of Dispatch's settings -// table is global today (cf. `title_model_*`, `perm_*`), so notification -// config follows the same pattern. - -import { deleteSetting, getSetting, setSetting } from "../db/settings.js"; -import type { NotificationEventType, NtfyConfig } from "./types.js"; -import { NTFY_DEFAULT_EVENTS, NTFY_EVENT_TYPES } from "./types.js"; - -export const NTFY_CONFIG_KEY = "ntfy_config"; - -/** Defaults returned when nothing is persisted yet. */ -export function defaultNtfyConfig(): NtfyConfig { - return { - enabled: false, - topic: "", - authToken: "", - events: { ...NTFY_DEFAULT_EVENTS }, - notifySubagents: false, - }; -} - -/** - * Normalize an arbitrary parsed JSON value into a complete `NtfyConfig`. - * Tolerant of missing / unexpected fields so a config from an older build - * never throws — missing event toggles fall back to defaults. - */ -export function normalizeNtfyConfig(raw: unknown): NtfyConfig { - const base = defaultNtfyConfig(); - if (!raw || typeof raw !== "object") return base; - const obj = raw as Record<string, unknown>; - const out: NtfyConfig = { - enabled: typeof obj.enabled === "boolean" ? obj.enabled : base.enabled, - topic: typeof obj.topic === "string" ? obj.topic : base.topic, - authToken: typeof obj.authToken === "string" ? obj.authToken : base.authToken, - events: { ...base.events }, - notifySubagents: - typeof obj.notifySubagents === "boolean" ? obj.notifySubagents : base.notifySubagents, - }; - const rawEvents = obj.events; - if (rawEvents && typeof rawEvents === "object") { - const evObj = rawEvents as Record<string, unknown>; - for (const key of NTFY_EVENT_TYPES) { - const v = evObj[key]; - if (typeof v === "boolean") out.events[key as NotificationEventType] = v; - } - } - return out; -} - -/** Load the persisted config (or defaults if none/corrupt). */ -export function loadNtfyConfig(): NtfyConfig { - const raw = getSetting(NTFY_CONFIG_KEY); - if (!raw) return defaultNtfyConfig(); - try { - return normalizeNtfyConfig(JSON.parse(raw)); - } catch { - return defaultNtfyConfig(); - } -} - -/** Persist a complete config (after server-side normalization). */ -export function saveNtfyConfig(config: NtfyConfig): void { - const normalized = normalizeNtfyConfig(config); - setSetting(NTFY_CONFIG_KEY, JSON.stringify(normalized)); -} - -/** Wipe the persisted config (revert to defaults on next load). */ -export function clearNtfyConfig(): void { - deleteSetting(NTFY_CONFIG_KEY); -} - -/** Strip the auth token from a config before returning it over the API. */ -export function redactNtfyConfig(config: NtfyConfig): NtfyConfig & { hasAuthToken: boolean } { - return { ...config, authToken: "", hasAuthToken: config.authToken.trim().length > 0 }; -} diff --git a/packages/core/src/notifications/dispatcher.ts b/packages/core/src/notifications/dispatcher.ts deleted file mode 100644 index 01ce00c..0000000 --- a/packages/core/src/notifications/dispatcher.ts +++ /dev/null @@ -1,287 +0,0 @@ -// NotificationDispatcher — turns high-level Dispatch events into -// `sendNtfy(...)` calls, gated by the persisted user config. -// -// The dispatcher is transport-agnostic at the `notify(event)` interface -// boundary: only `sendNtfy` is wired today, but adding another transport -// (email, webhook, etc.) means changing this one file, not the call sites. -// -// All sends are non-blocking (fire-and-forget). A 10-second timeout in -// `sendNtfy` bounds the worst case; the dispatcher additionally guards -// every send in a try/catch so a transport bug can never propagate into -// the agent loop. - -import { loadNtfyConfig } from "./config.js"; -import { type FetchLike, sendNtfy } from "./ntfy.js"; -import type { NotificationEvent, NtfyConfig } from "./types.js"; - -/** Minimal shape of an `AgentManager`-style event stream we hook into. */ -export interface AgentEventSource { - onEvent( - listener: (event: { type: string; tabId: string; [key: string]: unknown }) => void, - ): () => void; -} - -/** Minimal shape of a `PermissionManager`-style prompt source. */ -export interface PermissionPromptSource { - onPromptAdded( - listener: (prompt: { id: string; permission: string; description: string }) => void, - ): () => void; -} - -/** Look up a human-readable tab title for nicer notification text. */ -export type TabTitleLookup = (tabId: string) => string | null; - -/** - * Look up a tab's `parentTabId`. Returns `null` for top-level tabs (no - * parent) and `undefined` when the lookup can't be performed (no DB, tab - * not found). Both non-strings cause the dispatcher to fall back to - * "treat as top-level" to avoid silently dropping notifications when the - * lookup is broken. - */ -export type TabParentLookup = (tabId: string) => string | null | undefined; - -export interface DispatcherOptions { - /** Override the config loader (tests). Defaults to `loadNtfyConfig`. */ - loadConfig?: () => NtfyConfig; - /** Override the transport (tests). Defaults to the real `sendNtfy`. */ - send?: (config: NtfyConfig, event: NotificationEvent) => Promise<unknown>; - /** Optional fetch override (forwarded to `sendNtfy` when `send` not set). */ - fetchImpl?: FetchLike; - /** Look up a tab title for richer titles. */ - getTabTitle?: TabTitleLookup; - /** - * Look up a tab's `parentTabId`. Used to honour the - * `notifySubagents` config flag — when false, `turn-completed` / - * `turn-error` from subagent tabs (those with a parent) are - * suppressed. - */ - getTabParentId?: TabParentLookup; - /** - * How long (ms) a dedupeKey is suppressed for. Permission prompts re-emit - * the whole pending list on every change, so dedupe is essential. - */ - dedupeWindowMs?: number; -} - -export class NotificationDispatcher { - private loadConfig: () => NtfyConfig; - private send: (config: NtfyConfig, event: NotificationEvent) => Promise<unknown>; - private getTabTitle: TabTitleLookup | undefined; - private getTabParentId: TabParentLookup | undefined; - private dedupeWindowMs: number; - /** Recently-sent dedupeKey → expiresAt epoch ms. */ - private recentlySent = new Map<string, number>(); - private unsubs: Array<() => void> = []; - - constructor(opts: DispatcherOptions = {}) { - this.loadConfig = opts.loadConfig ?? loadNtfyConfig; - this.send = - opts.send ?? ((config, event) => sendNtfy(config, event, opts.fetchImpl ?? undefined)); - this.getTabTitle = opts.getTabTitle; - this.getTabParentId = opts.getTabParentId; - this.dedupeWindowMs = opts.dedupeWindowMs ?? 5_000; - } - - /** - * Single internal entry point — every public hook funnels through here. - * Public so a future caller can synthesize an arbitrary notification - * (e.g. a CLI `dispatch notify` command); kept narrow. - */ - notify(event: NotificationEvent): void { - const config = this.loadConfig(); - if (!config.enabled) return; - if (!config.events[event.type]) return; - if (event.dedupeKey && this.isDuplicate(event.dedupeKey)) return; - if (event.dedupeKey) this.markSent(event.dedupeKey); - - // Fire-and-forget: never await, never throw. - try { - void Promise.resolve(this.send(config, event)).catch((err) => { - console.warn( - `[ntfy] send failed for ${event.type}: ${err instanceof Error ? err.message : String(err)}`, - ); - }); - } catch (err) { - // Guard the synchronous portion of `send` too. - console.warn( - `[ntfy] dispatch threw for ${event.type}: ${err instanceof Error ? err.message : String(err)}`, - ); - } - } - - /** - * Hook into an `AgentManager`-style event stream. - * - * Maps: - * - `done` → `turn-completed` - * - `error` → `turn-error` - * - `tab-created` → `agent-spawned` (only top-level user-agent tabs) - * - * `status` events are ignored — they fire on every transition and we'd - * either spam or duplicate the `done`/`error` notifications. - * - * Turn events from subagent tabs are suppressed when - * `config.notifySubagents === false` (the default). A parent agent - * spawning 8 subagents would otherwise produce 9 "Turn complete" - * pushes per round; almost always noise. Permission prompts are NOT - * gated this way — a subagent's permission request still needs human - * input to proceed, so suppressing those would silently hang the - * subagent. - */ - attachToAgentManager(source: AgentEventSource): () => void { - const unsub = source.onEvent((event) => { - if (event.type === "done") { - if (this.isSubagentSuppressed(event.tabId)) return; - this.notify(this.buildTurnCompleted(event)); - } else if (event.type === "error") { - if (this.isSubagentSuppressed(event.tabId)) return; - this.notify(this.buildTurnError(event)); - } else if (event.type === "tab-created") { - const ev = event as unknown as { - tabId: string; - id: string; - title: string; - parentTabId: string | null; - agentSlug?: string | null; - }; - // Only notify for top-level user-agent tabs spawned via `summon`. - // Filtering on `agentSlug` skips "blank" new tabs the user opened - // manually, which would be noisy. - if (ev.parentTabId === null && ev.agentSlug) { - this.notify(this.buildAgentSpawned(ev)); - } - } - }); - this.unsubs.push(unsub); - return unsub; - } - - /** Hook into a `PermissionManager`-style prompt source. */ - attachToPermissionManager(source: PermissionPromptSource): () => void { - const unsub = source.onPromptAdded((prompt) => { - this.notify(this.buildPermissionRequired(prompt)); - }); - this.unsubs.push(unsub); - return unsub; - } - - /** Release all hooks acquired via `attachTo*`. */ - dispose(): void { - for (const u of this.unsubs) { - try { - u(); - } catch { - // best-effort - } - } - this.unsubs = []; - this.recentlySent.clear(); - } - - // ─── Event builders (internal) ──────────────────────────────── - - private buildTurnCompleted(event: { tabId: string }): NotificationEvent { - const tabLabel = this.tabLabel(event.tabId); - return { - type: "turn-completed", - title: `Turn complete — ${tabLabel}`, - message: `Assistant finished a turn in ${tabLabel}.`, - tabId: event.tabId, - }; - } - - private buildTurnError(event: { - tabId: string; - error?: unknown; - statusCode?: unknown; - }): NotificationEvent { - const tabLabel = this.tabLabel(event.tabId); - const errText = typeof event.error === "string" ? event.error : "Unknown error"; - const statusText = typeof event.statusCode === "number" ? ` (status ${event.statusCode})` : ""; - return { - type: "turn-error", - title: `Turn failed — ${tabLabel}`, - message: `${errText}${statusText}`, - tabId: event.tabId, - }; - } - - private buildPermissionRequired(prompt: { - id: string; - permission: string; - description: string; - }): NotificationEvent { - return { - type: "permission-required", - title: `Permission required: ${prompt.permission}`, - message: prompt.description || `Agent is requesting ${prompt.permission} permission.`, - // Permission prompts can re-emit (e.g. another prompt arrives while - // this one is still pending) — dedupe on the prompt id. - dedupeKey: `permission:${prompt.id}`, - }; - } - - private buildAgentSpawned(ev: { - tabId: string; - id: string; - title: string; - agentSlug?: string | null; - }): NotificationEvent { - return { - type: "agent-spawned", - title: `User agent spawned — ${ev.agentSlug ?? "agent"}`, - message: ev.title, - tabId: ev.tabId ?? ev.id, - }; - } - - private tabLabel(tabId: string): string { - const title = this.getTabTitle?.(tabId); - if (title?.trim()) return title.trim(); - return `tab ${tabId.slice(0, 8)}`; - } - - /** - * Returns true when this `tabId` belongs to a subagent AND the user has - * opted out of subagent turn notifications. On lookup failure - * (`getTabParentId` returns `undefined` or throws) we err on the side - * of "not a subagent" — better to over-notify than to silently drop - * legitimate top-level events when the DB is briefly unreadable. - */ - private isSubagentSuppressed(tabId: string): boolean { - const config = this.loadConfig(); - if (config.notifySubagents) return false; - if (!this.getTabParentId) return false; - let parent: string | null | undefined; - try { - parent = this.getTabParentId(tabId); - } catch { - return false; - } - // Only a non-empty string parent id means "this tab is a subagent". - return typeof parent === "string" && parent.length > 0; - } - - // ─── Dedupe helpers ─────────────────────────────────────────── - - private isDuplicate(key: string): boolean { - const expires = this.recentlySent.get(key); - if (expires === undefined) return false; - if (expires <= Date.now()) { - this.recentlySent.delete(key); - return false; - } - return true; - } - - private markSent(key: string): void { - // Lazy-evict expired entries when the map gets large. - if (this.recentlySent.size > 256) { - const now = Date.now(); - for (const [k, exp] of this.recentlySent) { - if (exp <= now) this.recentlySent.delete(k); - } - } - this.recentlySent.set(key, Date.now() + this.dedupeWindowMs); - } -} diff --git a/packages/core/src/notifications/index.ts b/packages/core/src/notifications/index.ts deleted file mode 100644 index d1e7891..0000000 --- a/packages/core/src/notifications/index.ts +++ /dev/null @@ -1,36 +0,0 @@ -// @dispatch/core — ntfy.sh push notifications - -export { - clearNtfyConfig, - defaultNtfyConfig, - loadNtfyConfig, - NTFY_CONFIG_KEY, - normalizeNtfyConfig, - redactNtfyConfig, - saveNtfyConfig, -} from "./config.js"; -export { - type AgentEventSource, - type DispatcherOptions, - NotificationDispatcher, - type PermissionPromptSource, - type TabParentLookup, - type TabTitleLookup, -} from "./dispatcher.js"; -export { - buildNtfyUrl, - type FetchLike, - NTFY_BASE_URL, - type NtfySendResult, - sendNtfy, -} from "./ntfy.js"; -export { - type NotificationEvent, - type NotificationEventType, - NTFY_DEFAULT_EVENTS, - NTFY_DEFAULT_PRIORITIES, - NTFY_DEFAULT_TAGS, - NTFY_EVENT_TYPES, - type NtfyConfig, - type NtfyPriority, -} from "./types.js"; diff --git a/packages/core/src/notifications/ntfy.ts b/packages/core/src/notifications/ntfy.ts deleted file mode 100644 index eb5de9e..0000000 --- a/packages/core/src/notifications/ntfy.ts +++ /dev/null @@ -1,149 +0,0 @@ -// ntfy.sh HTTP transport. -// -// ntfy's API is a simple POST to `https://ntfy.sh/<topic>` with the body -// as the message and metadata passed via HTTP headers: -// Title: notification title -// Priority: 1..5 (3 = default) -// Tags: comma-separated emoji shortcodes -// Click: URL opened when the notification is tapped -// -// The server is hardcoded to the public ntfy.sh instance; the user only -// configures a topic name. We intentionally use `fetch` directly — no -// SDK, no extra deps. - -import type { NotificationEvent, NtfyConfig } from "./types.js"; -import { NTFY_DEFAULT_PRIORITIES, NTFY_DEFAULT_TAGS } from "./types.js"; - -export interface NtfySendResult { - ok: boolean; - status?: number; - error?: string; -} - -/** - * Lightweight fetch shape so callers (and tests) can inject a mock without - * pulling in the DOM `fetch` type from a `Headers` instance. - */ -export type FetchLike = ( - input: string, - init: { method: string; headers: Record<string, string>; body: string; signal?: AbortSignal }, -) => Promise<{ ok: boolean; status: number; statusText?: string; text(): Promise<string> }>; - -/** Base URL of the public ntfy.sh server. */ -export const NTFY_BASE_URL = "https://ntfy.sh"; - -/** - * Build the publish URL for a topic name. - * - * No client-side validation of the topic content: ntfy.sh's accepted - * character set has changed over time and a regex here only locks users - * out of legitimate topics. The topic is URL-encoded so the resulting - * URL is always syntactically valid; if ntfy rejects the name the HTTP - * error surfaces on the first send / `Send test`. - */ -export function buildNtfyUrl(topic: string): string { - return `${NTFY_BASE_URL}/${encodeURIComponent(topic.trim())}`; -} - -/** - * Send a single notification to the configured ntfy topic. - * - * Fire-and-forget at call sites: the dispatcher uses - * `void sendNtfy(...).catch(...)` so a slow/broken ntfy server never blocks - * a turn. We still return a structured result so the explicit - * `POST /notifications/test` route can surface failures back to the UI. - * - * Pure with respect to `config` / `event` — no DB, no module state. - */ -export async function sendNtfy( - config: NtfyConfig, - event: NotificationEvent, - fetchImpl: FetchLike = globalThis.fetch as unknown as FetchLike, - timeoutMs = 10_000, -): Promise<NtfySendResult> { - if (!config.enabled) return { ok: false, error: "Notifications are disabled" }; - if (!config.topic.trim()) return { ok: false, error: "Topic is required" }; - const targetUrl = buildNtfyUrl(config.topic); - - const priority = event.priority ?? NTFY_DEFAULT_PRIORITIES[event.type] ?? 3; - const baseTags = event.tags ?? NTFY_DEFAULT_TAGS[event.type] ?? []; - const tags = [...baseTags]; - if (event.tabId) { - // Short, ASCII-only tag so ntfy's comma-separated header parser is happy. - tags.push(`tab-${event.tabId.slice(0, 8)}`); - } - - const headers: Record<string, string> = { - // ntfy is tolerant of non-ASCII in the Title header but many proxies - // aren't — sanitizeHeader strips CR/LF/control chars (injection guard) - // and leaves UTF-8 in place. Body is sent verbatim as UTF-8. - Title: sanitizeHeader(event.title), - Priority: String(priority), - "Content-Type": "text/plain; charset=utf-8", - }; - if (tags.length > 0) headers.Tags = tags.map(sanitizeHeader).join(","); - if (event.clickUrl) headers.Click = sanitizeHeader(event.clickUrl); - const authValue = buildAuthHeaderValue(config.authToken); - if (authValue) headers.Authorization = authValue; - - // Per-request abort so a hung server doesn't pin a Bun worker forever. - const controller = new AbortController(); - const timer = setTimeout(() => controller.abort(), timeoutMs); - - try { - const res = await fetchImpl(targetUrl, { - method: "POST", - headers, - body: event.message, - signal: controller.signal, - }); - if (!res.ok) { - const text = await safeReadText(res); - return { - ok: false, - status: res.status, - error: `ntfy responded ${res.status} ${res.statusText ?? ""}: ${text}`.trim(), - }; - } - return { ok: true, status: res.status }; - } catch (err) { - const msg = err instanceof Error ? err.message : String(err); - return { ok: false, error: msg }; - } finally { - clearTimeout(timer); - } -} - -/** - * Build the `Authorization` header value from the user-configured token. - * - * - Empty/blank ⇒ no header. - * - Already starts with `Bearer ` / `Basic ` / etc. (RFC 7235 scheme + space) - * ⇒ used verbatim. Lets users paste a complete header for Basic auth or - * any other scheme their private ntfy server supports. - * - Otherwise ⇒ prefixed with `Bearer ` (the common case). - */ -function buildAuthHeaderValue(rawToken: string): string | null { - const trimmed = (rawToken ?? "").trim(); - if (!trimmed) return null; - if (/^[A-Za-z][A-Za-z0-9._~+/-]*\s+\S/.test(trimmed)) { - // Already includes a scheme token (e.g. "Bearer xyz", "Basic dXNlcjpw"). - return sanitizeHeader(trimmed); - } - return `Bearer ${sanitizeHeader(trimmed)}`; -} - -function sanitizeHeader(value: string): string { - // Strip CR/LF (header injection guard) and other control chars, then trim. - // biome-ignore lint/suspicious/noControlCharactersInRegex: intentional - return value.replace(/[\r\n\u0000-\u001f]+/g, " ").trim(); -} - -async function safeReadText(res: { text(): Promise<string> }): Promise<string> { - try { - const t = await res.text(); - return t.length > 200 ? `${t.slice(0, 200)}…` : t; - } catch { - return ""; - } -} diff --git a/packages/core/src/notifications/types.ts b/packages/core/src/notifications/types.ts deleted file mode 100644 index ef6c059..0000000 --- a/packages/core/src/notifications/types.ts +++ /dev/null @@ -1,108 +0,0 @@ -// ntfy.sh push notifications — types - -/** - * Catalog of notification-worthy events. - * - * Kept intentionally small and stable: each entry is something a human - * actually wants pushed to their phone. New event types should be added - * with a sensible default (`NTFY_DEFAULT_EVENTS`) and a mapping in the - * dispatcher. - */ -export type NotificationEventType = - | "turn-completed" - | "turn-error" - | "permission-required" - | "agent-spawned"; - -/** ntfy priority levels (1=min … 5=max). */ -export type NtfyPriority = 1 | 2 | 3 | 4 | 5; - -/** - * A single notification request. Synthesised by the dispatcher from a - * higher-level event source (AgentManager / PermissionManager); fed to - * the ntfy transport. - * - * `dedupeKey` lets the dispatcher suppress duplicate sends (e.g. the - * permission system re-emits the pending list on every change). - */ -export interface NotificationEvent { - type: NotificationEventType; - /** Notification title (short). */ - title: string; - /** Notification body. */ - message: string; - /** Optional ntfy tags (emoji shortcodes — e.g. `["white_check_mark"]`). */ - tags?: string[]; - /** Optional priority override. Defaults are per-event-type. */ - priority?: NtfyPriority; - /** Optional URL the notification deep-links to when tapped. */ - clickUrl?: string; - /** Origin tab id (informational; included in tags as `tab:<short>`). */ - tabId?: string; - /** - * Stable key for suppressing duplicates. Same key + same type within a - * short window ⇒ dropped silently. - */ - dedupeKey?: string; -} - -/** - * Persisted ntfy configuration. Lives in the `settings` table under a - * single key (`ntfy_config`) — one global config, matching the codebase's - * existing single-user assumption (cf. `title_model_*`, `perm_*`). - * - * - `enabled` — master switch. Off ⇒ dispatcher never sends. - * - `topic` — bare ntfy.sh topic name, e.g. `my-secret-topic`. The - * server is hardcoded to https://ntfy.sh; the user only picks a topic. - * Missing ⇒ dispatcher never sends. - * - `authToken` — optional bearer token (rarely needed against ntfy.sh - * directly; preserved for users behind an auth-protected proxy). - * - `events` — per-event-type enable map. Missing entries default to OFF - * so a newly-added event type doesn't silently start firing. - * - `notifySubagents` — when false (default), `turn-completed` and - * `turn-error` notifications from subagent tabs (tabs with a - * `parentTabId`) are suppressed. A parent agent that spawns 8 - * subagents would otherwise push 9 "Turn complete" notifications per - * round — usually noise. `permission-required` is NOT gated: even a - * subagent's permission prompt needs a human tap to proceed. - * `agent-spawned` is already top-level-only by construction. - */ -export interface NtfyConfig { - enabled: boolean; - topic: string; - authToken: string; - events: Record<NotificationEventType, boolean>; - notifySubagents: boolean; -} - -/** All event types this build knows about (the source of truth for UI). */ -export const NTFY_EVENT_TYPES: NotificationEventType[] = [ - "turn-completed", - "turn-error", - "permission-required", - "agent-spawned", -]; - -/** Default per-event-type toggles. */ -export const NTFY_DEFAULT_EVENTS: Record<NotificationEventType, boolean> = { - "turn-completed": true, - "turn-error": true, - "permission-required": true, - "agent-spawned": false, -}; - -/** Default priority per event type (when the event itself doesn't override). */ -export const NTFY_DEFAULT_PRIORITIES: Record<NotificationEventType, NtfyPriority> = { - "turn-completed": 3, - "turn-error": 4, - "permission-required": 4, - "agent-spawned": 2, -}; - -/** Default tag (emoji) per event type. */ -export const NTFY_DEFAULT_TAGS: Record<NotificationEventType, string[]> = { - "turn-completed": ["white_check_mark"], - "turn-error": ["rotating_light"], - "permission-required": ["lock"], - "agent-spawned": ["sparkles"], -}; diff --git a/packages/core/src/permission/evaluate.ts b/packages/core/src/permission/evaluate.ts deleted file mode 100644 index 7a0d235..0000000 --- a/packages/core/src/permission/evaluate.ts +++ /dev/null @@ -1,15 +0,0 @@ -import type { PermissionRule, Ruleset } from "./index.js"; -import { Wildcard } from "./wildcard.js"; - -export function evaluate( - permission: string, - pattern: string, - ...rulesets: Ruleset[] -): PermissionRule { - const flat = ([] as PermissionRule[]).concat(...rulesets); - const match = flat.findLast( - (rule) => Wildcard.match(rule.permission, permission) && Wildcard.match(rule.pattern, pattern), - ); - if (match) return match; - return { action: "ask", permission, pattern }; -} diff --git a/packages/core/src/permission/index.ts b/packages/core/src/permission/index.ts deleted file mode 100644 index bf6efaf..0000000 --- a/packages/core/src/permission/index.ts +++ /dev/null @@ -1,26 +0,0 @@ -export interface PermissionRule { - permission: string; - pattern: string; - action: "allow" | "deny" | "ask"; -} - -export type Ruleset = PermissionRule[]; - -export type PermissionReply = "once" | "always" | "reject"; - -export interface PermissionRequest { - permission: string; - patterns: string[]; - always: string[]; - description: string; - metadata: Record<string, unknown>; -} - -export interface PermissionChecker { - ask(request: PermissionRequest, rulesets: Ruleset[]): Promise<PermissionReply>; - getPending(): Array<{ id: string; request: PermissionRequest }>; -} - -export { evaluate } from "./evaluate.js"; -export { PermissionService } from "./service.js"; -export { Wildcard } from "./wildcard.js"; diff --git a/packages/core/src/permission/service.ts b/packages/core/src/permission/service.ts deleted file mode 100644 index 1c8c6f0..0000000 --- a/packages/core/src/permission/service.ts +++ /dev/null @@ -1,76 +0,0 @@ -import { evaluate } from "./evaluate.js"; -import type { PermissionReply, PermissionRequest, PermissionRule, Ruleset } from "./index.js"; - -export class PermissionService { - private pending: Map< - string, - { - request: PermissionRequest; - resolve: (reply: PermissionReply) => void; - reject: (err: Error) => void; - } - > = new Map(); - private approved: Ruleset = []; - private idCounter = 0; - - approve(rules: PermissionRule[]): void { - this.approved.push(...rules); - } - - async ask(request: PermissionRequest, rulesets: Ruleset[]): Promise<PermissionReply> { - // Evaluate against all rulesets + approved (approved overrides) for ALL patterns - const patterns = request.always.length > 0 ? request.always : ["*"]; - const results = patterns.map((pattern) => - evaluate(request.permission, pattern, ...rulesets, this.approved), - ); - - // Any deny → deny - const denied = results.find((r) => r.action === "deny"); - if (denied) { - throw new Error(`Permission denied: ${request.permission} ${denied.pattern}`); - } - - // All allow → allow - if (results.every((r) => r.action === "allow")) { - return "once"; - } - - // action === "ask" — create a pending request - const id = String(++this.idCounter); - return new Promise<PermissionReply>((resolve, reject) => { - this.pending.set(id, { request, resolve, reject }); - }); - } - - reply(id: string, reply: PermissionReply): void { - if (reply === "reject") { - // Reject cascade — reject all pending - for (const [pendingId, entry] of this.pending) { - entry.reject(new Error(`Permission rejected: ${entry.request.permission}`)); - this.pending.delete(pendingId); - } - return; - } - - this.resolvePending(id, reply); - } - - getPending(): Array<{ id: string; request: PermissionRequest }> { - return Array.from(this.pending.entries()).map(([id, { request }]) => ({ id, request })); - } - - private resolvePending(id: string, reply: PermissionReply): void { - const entry = this.pending.get(id); - if (!entry) return; - - if (reply === "always") { - // Add rules for each pattern in request.patterns - for (const pattern of entry.request.patterns) { - this.approved.push({ permission: entry.request.permission, pattern, action: "allow" }); - } - } - - entry.resolve(reply); - this.pending.delete(id); - } -} diff --git a/packages/core/src/permission/wildcard.ts b/packages/core/src/permission/wildcard.ts deleted file mode 100644 index 7874680..0000000 --- a/packages/core/src/permission/wildcard.ts +++ /dev/null @@ -1,10 +0,0 @@ -export const Wildcard = { - match(pattern: string, value: string): boolean { - // Escape regex special chars except * and ? - const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&"); - // Convert wildcards - const regexStr = escaped.replace(/\*/g, ".*").replace(/\?/g, "."); - const regex = new RegExp(`^${regexStr}$`, "s"); - return regex.test(value); - }, -}; diff --git a/packages/core/src/skills/index.ts b/packages/core/src/skills/index.ts deleted file mode 100644 index a3fac70..0000000 --- a/packages/core/src/skills/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -export { - createSkillsWatcher, - getSkillByName, - loadSkills, - resolveSkillsForAgent, -} from "./loader.js"; -export { parseSkillFile } from "./parser.js"; diff --git a/packages/core/src/skills/loader.ts b/packages/core/src/skills/loader.ts deleted file mode 100644 index 5d043dd..0000000 --- a/packages/core/src/skills/loader.ts +++ /dev/null @@ -1,248 +0,0 @@ -import * as fs from "node:fs"; -import * as os from "node:os"; -import * as path from "node:path"; -import chokidar from "chokidar"; -import type { AgentSkillMapping, SkillDefinition, SkillScope } from "../types/index.js"; -import { parseSkillFile } from "./parser.js"; - -// ─── Internal Helpers ──────────────────────────────────────────── - -/** - * Recursively scan a directory for .md skill files. - * The `directory` field on each skill is the relative path from `baseDir` to the file's parent. - * Skips the `agents/` subdirectory (handled separately). - */ -function scanSkillsRecursive(baseDir: string, scope: SkillScope): SkillDefinition[] { - if (!fs.existsSync(baseDir)) return []; - - const results: SkillDefinition[] = []; - - function walk(dir: string) { - let entries: fs.Dirent[]; - try { - entries = fs.readdirSync(dir, { withFileTypes: true }); - } catch { - return; - } - - for (const entry of entries) { - const fullPath = path.join(dir, entry.name); - - if (entry.isDirectory()) { - // Skip agents/ at the top level (handled by loadAgentMappings) - const relFromBase = path.relative(baseDir, fullPath); - if (relFromBase === "agents") continue; - walk(fullPath); - } else if (entry.isFile() && entry.name.endsWith(".md")) { - const relDir = path.relative(baseDir, dir); - // relDir is "" for root, "general" for general/, "general/webapps" for nested - const directory = relDir === "." ? "" : relDir; - try { - const content = fs.readFileSync(fullPath, "utf-8"); - const skill = parseSkillFile(fullPath, content, scope, directory); - results.push(skill); - } catch { - // Skip unreadable files - } - } - } - } - - walk(baseDir); - return results; -} - -function loadAgentMappings(agentsDir: string, scope: SkillScope): AgentSkillMapping[] { - if (!fs.existsSync(agentsDir)) { - return []; - } - - const results: AgentSkillMapping[] = []; - let entries: fs.Dirent[]; - try { - entries = fs.readdirSync(agentsDir, { withFileTypes: true }); - } catch { - return []; - } - - for (const entry of entries) { - if (!entry.isFile() || !entry.name.endsWith(".txt")) { - continue; - } - - const fileName = entry.name; - let isOrchestrator = false; - let agentType: string; - - if (fileName.endsWith(".o.txt")) { - isOrchestrator = true; - agentType = fileName.slice(0, -6); // remove ".o.txt" - } else { - agentType = fileName.slice(0, -4); // remove ".txt" - } - - const filePath = path.join(agentsDir, fileName); - try { - const content = fs.readFileSync(filePath, "utf-8"); - const skills = content - .split("\n") - .map((line) => line.trim()) - .filter((line) => line.length > 0 && !line.startsWith("#")); - - results.push({ agentType, isOrchestrator, skills, scope }); - } catch { - // Skip unreadable mapping files - } - } - - return results; -} - -// ─── Public API ────────────────────────────────────────────────── - -export function loadSkills(projectDir: string): { - skills: SkillDefinition[]; - mappings: AgentSkillMapping[]; -} { - const globalBase = path.join(os.homedir(), ".skills"); - const projectBase = path.join(projectDir, ".skills"); - - const skills: SkillDefinition[] = []; - const mappings: AgentSkillMapping[] = []; - - // 1. Scan all global skills recursively (skipping agents/) - skills.push(...scanSkillsRecursive(globalBase, "global")); - - // 2. Scan all project skills recursively (skipping agents/) - skills.push(...scanSkillsRecursive(projectBase, "project")); - - // 3. Agent mappings — global then project - mappings.push(...loadAgentMappings(path.join(globalBase, "agents"), "global")); - mappings.push(...loadAgentMappings(path.join(projectBase, "agents"), "project")); - - return { skills, mappings }; -} - -export function resolveSkillsForAgent( - agentType: string, - isOrchestrator: boolean, - skills: SkillDefinition[], - mappings: AgentSkillMapping[], -): SkillDefinition[] { - // Helper: project overrides global for same-named skills - const dedupeByName = (list: SkillDefinition[]): SkillDefinition[] => { - const seen = new Map<string, SkillDefinition>(); - for (const skill of list) { - const existing = seen.get(skill.name); - if (!existing || skill.scope === "project") { - seen.set(skill.name, skill); - } - } - return Array.from(seen.values()); - }; - - // All default-directory skills (global first, then project — dedupe preserves project) - const defaultSkills = skills.filter((s) => s.directory === "default"); - - // Skills mapped to this agent type - const relevantMappings = mappings.filter( - (m) => m.agentType === agentType && m.isOrchestrator === isOrchestrator, - ); - - // Gather agent-specific skills in order: global mappings first, then project - const globalMappings = relevantMappings.filter((m) => m.scope === "global"); - const projectMappings = relevantMappings.filter((m) => m.scope === "project"); - - const agentSkillNames: string[] = []; - for (const mapping of [...globalMappings, ...projectMappings]) { - for (const skillFile of mapping.skills) { - const skillName = path.basename(skillFile, path.extname(skillFile)); - agentSkillNames.push(skillName); - } - } - - const agentSpecificSkills = agentSkillNames - .map((name) => getSkillByName(name, skills, "project")) - .filter((s): s is SkillDefinition => s !== undefined); - - const combined = [...defaultSkills, ...agentSpecificSkills]; - return dedupeByName(combined); -} - -export function getSkillByName( - name: string, - skills: SkillDefinition[], - preferScope?: SkillScope, -): SkillDefinition | undefined { - const matches = skills.filter((s) => s.name === name); - if (matches.length === 0) { - return undefined; - } - - if (preferScope) { - const preferred = matches.find((s) => s.scope === preferScope); - if (preferred) { - return preferred; - } - } - - // Default: project takes precedence - const projectMatch = matches.find((s) => s.scope === "project"); - return projectMatch ?? matches[0]; -} - -export function createSkillsWatcher( - projectDir: string, - onChange: (result: { skills: SkillDefinition[]; mappings: AgentSkillMapping[] }) => void, -): { close(): void } { - const globalBase = path.join(os.homedir(), ".skills"); - const projectBase = path.join(projectDir, ".skills"); - - let debounceTimer: ReturnType<typeof setTimeout> | null = null; - - const reload = () => { - if (debounceTimer !== null) { - clearTimeout(debounceTimer); - } - debounceTimer = setTimeout(() => { - debounceTimer = null; - const result = loadSkills(projectDir); - onChange(result); - }, 300); - }; - - const watchPaths = [globalBase, projectBase]; - - const watcher = chokidar.watch(watchPaths, { - ignoreInitial: true, - persistent: true, - }); - - watcher.on("add", (filePath: string) => { - if (filePath.endsWith(".md") || filePath.endsWith(".txt")) { - reload(); - } - }); - - watcher.on("change", (filePath: string) => { - if (filePath.endsWith(".md") || filePath.endsWith(".txt")) { - reload(); - } - }); - - watcher.on("unlink", (filePath: string) => { - if (filePath.endsWith(".md") || filePath.endsWith(".txt")) { - reload(); - } - }); - - return { - close() { - if (debounceTimer !== null) { - clearTimeout(debounceTimer); - debounceTimer = null; - } - watcher.close(); - }, - }; -} diff --git a/packages/core/src/skills/parser.ts b/packages/core/src/skills/parser.ts deleted file mode 100644 index bd1205e..0000000 --- a/packages/core/src/skills/parser.ts +++ /dev/null @@ -1,60 +0,0 @@ -import * as path from "node:path"; -import { parse } from "smol-toml"; -import type { SkillDefinition, SkillDirectory, SkillScope } from "../types/index.js"; - -const FRONTMATTER_DELIMITER = "+++"; - -export function parseSkillFile( - filePath: string, - content: string, - scope: SkillScope, - directory: SkillDirectory, -): SkillDefinition { - const defaultName = path.basename(filePath, path.extname(filePath)); - - let name = defaultName; - let description = ""; - let tags: string[] = []; - let body = content; - - // Check for TOML frontmatter - const trimmed = content.trimStart(); - if (trimmed.startsWith(FRONTMATTER_DELIMITER)) { - const afterOpen = trimmed.slice(FRONTMATTER_DELIMITER.length); - // Find the closing +++ - const closeIndex = afterOpen.indexOf(FRONTMATTER_DELIMITER); - if (closeIndex !== -1) { - const tomlSource = afterOpen.slice(0, closeIndex); - body = afterOpen.slice(closeIndex + FRONTMATTER_DELIMITER.length); - - try { - const frontmatter = parse(tomlSource); - - if (typeof frontmatter.name === "string") { - name = frontmatter.name; - } - if (typeof frontmatter.description === "string") { - description = frontmatter.description; - } - if (Array.isArray(frontmatter.tags)) { - tags = (frontmatter.tags as unknown[]).filter((t): t is string => typeof t === "string"); - } - } catch { - // Malformed TOML — fall through with defaults - } - } - } - - // Trim leading newline from body (handles both LF and CRLF) - body = body.replace(/^\r?\n/, ""); - - return { - name, - description, - tags, - content: body, - scope, - source: filePath, - directory, - }; -} diff --git a/packages/core/src/tools/bash-arity.ts b/packages/core/src/tools/bash-arity.ts deleted file mode 100644 index 1aaba8e..0000000 --- a/packages/core/src/tools/bash-arity.ts +++ /dev/null @@ -1,48 +0,0 @@ -// Hardcoded dictionary of well-known commands and their arity -// (number of tokens that form the "human-understandable" prefix) -const ARITY: Record<string, number> = { - git: 2, // "git checkout", "git commit", etc. - npm: 3, // "npm run dev", "npm install -g" - docker: 2, // "docker compose", "docker build" - kubectl: 2, // "kubectl get", "kubectl apply" - bun: 2, // "bun install", "bun test" - cargo: 2, // "cargo build", "cargo test" - go: 2, // "go build", "go test" - python: 2, // "python -m", "python script.py" - python3: 2, - pip: 2, - pip3: 2, - brew: 2, - apt: 2, - "apt-get": 2, - dnf: 2, - yum: 2, - pacman: 2, - systemctl: 2, - journalctl: 2, - ssh: 2, - scp: 2, - rsync: 2, - curl: 2, // "curl -X", "curl https://" - wget: 2, - tar: 2, // "tar -xzf", "tar -czf" - zip: 2, - unzip: 2, - chown: 2, - chmod: 2, - mount: 2, - umount: 2, - // Default: all other commands are arity 1 -}; - -// Get the normalized prefix for a list of tokens -export function prefix(tokens: string[]): string[] { - if (tokens.length === 0) return []; - const first = tokens[0]; - if (!first) return []; - const arity = ARITY[first.toLowerCase()]; - if (arity === undefined) { - return [first]; - } - return tokens.slice(0, arity); -} diff --git a/packages/core/src/tools/key-usage.ts b/packages/core/src/tools/key-usage.ts deleted file mode 100644 index 0655ad7..0000000 --- a/packages/core/src/tools/key-usage.ts +++ /dev/null @@ -1,322 +0,0 @@ -import { z } from "zod"; -import type { ClaudeAccount, ClaudeUsageReport, ClaudeUsageResult } from "../credentials/claude.js"; -import { getAccountUsageWithSource } from "../credentials/claude.js"; -import type { OpencodeUsageReport } from "../credentials/opencode.js"; -import { fetchOpencodeUsage as defaultFetchOpencodeUsage } from "../credentials/opencode.js"; -import type { KeyState, ToolDefinition } from "../types/index.js"; - -/** - * Collaborators the `key_usage` tool needs from the API layer (which owns the - * live `ModelRegistry` and the discovered Claude accounts). The two `fetch*` - * hooks default to the real credential fetchers but are injectable so tests can - * exercise the tool without network or DB access. - */ -export interface KeyUsageCallbacks { - /** Current key states from the model registry (definition + active/exhausted status). */ - listKeys(): KeyState[]; - /** Discovered Claude accounts, used to resolve `anthropic` keys to credentials. */ - listClaudeAccounts(): ClaudeAccount[]; - /** - * Fetch an anthropic account's usage with provenance (live vs cache). - * Defaults to `getAccountUsageWithSource`. - */ - fetchAnthropicUsage?: (account: ClaudeAccount) => Promise<ClaudeUsageResult | null>; - /** - * Fetch an opencode-go key's usage (always a live scrape — OpenCode keeps no - * local cache). Defaults to `fetchOpencodeUsage`. - */ - fetchOpencodeUsage?: (keyId: string) => Promise<OpencodeUsageReport | null>; -} - -/** A single normalized usage window (5-hour / week / month). */ -interface UsageWindow { - label: string; - /** Remaining headroom as a 0–100 percentage. Omitted when the source gives no utilization. */ - remainingPercent?: number; - /** Epoch-ms the window resets. Omitted when the source gives no reset time. */ - resetsAt?: number; -} - -/** Fully normalized per-key usage, ready for rendering. */ -interface KeyUsageEntry { - keyId: string; - provider: string; - status: "active" | "exhausted"; - lastError?: string; - exhaustedAt?: number; - /** Provenance of the usage figures: a fresh live fetch or a cached payload. */ - dataSource?: "live" | "cache"; - /** Epoch-ms the cached payload was last fetched from source (only on `dataSource: "cache"`). */ - cachedAt?: number; - windows: UsageWindow[]; - /** Set when no usage figures could be obtained for an otherwise-supported key. */ - unavailableReason?: string; - /** Set when the provider has no usage-reporting support. */ - unsupported?: boolean; -} - -function clampPercent(value: number): number { - if (value < 0) return 0; - if (value > 100) return 100; - return value; -} - -/** Convert a raw `{ utilization, resetsAt }` bucket into a normalized window. */ -function toWindow( - label: string, - bucket?: { utilization?: number; resetsAt?: number }, -): UsageWindow | null { - if (!bucket) return null; - const hasUtil = typeof bucket.utilization === "number"; - const hasReset = typeof bucket.resetsAt === "number"; - if (!hasUtil && !hasReset) return null; - return { - label, - ...(hasUtil - ? { remainingPercent: clampPercent(Math.round((1 - (bucket.utilization as number)) * 100)) } - : {}), - ...(hasReset ? { resetsAt: bucket.resetsAt } : {}), - }; -} - -function anthropicWindows(report: ClaudeUsageReport): UsageWindow[] { - const windows: UsageWindow[] = []; - const fiveHour = toWindow("5-hour", report.fiveHour); - if (fiveHour) windows.push(fiveHour); - const week = toWindow("week", report.sevenDay); - if (week) windows.push(week); - return windows; -} - -function opencodeWindows(report: OpencodeUsageReport): UsageWindow[] { - const windows: UsageWindow[] = []; - const fiveHour = toWindow("5-hour", report.fiveHour); - if (fiveHour) windows.push(fiveHour); - const week = toWindow("week", report.weekly); - if (week) windows.push(week); - const month = toWindow("month", report.monthly); - if (month) windows.push(month); - return windows; -} - -/** - * Resolve which Claude account backs an `anthropic` key. Matches by key id or by - * the account's source file (the key's `credentials_file`), falling back to the - * first available account — mirrors the existing `/models/key-usage` route. - */ -function matchAnthropicAccount( - accounts: ClaudeAccount[], - keyId: string, - credFile?: string, -): ClaudeAccount | undefined { - const matched = accounts.find( - (a) => a.id === keyId || (credFile != null && a.source === credFile), - ); - return matched ?? accounts[0]; -} - -function iso(ms: number): string { - return new Date(ms).toISOString(); -} - -/** Human-readable coarse duration, e.g. "3h 12m", "5d 8h", "0m". */ -function formatDuration(ms: number): string { - const totalSec = Math.round(Math.abs(ms) / 1000); - const days = Math.floor(totalSec / 86400); - const hours = Math.floor((totalSec % 86400) / 3600); - const minutes = Math.floor((totalSec % 3600) / 60); - const parts: string[] = []; - if (days > 0) parts.push(`${days}d`); - if (hours > 0) parts.push(`${hours}h`); - if (minutes > 0 || parts.length === 0) parts.push(`${minutes}m`); - return parts.join(" "); -} - -function formatRelative(targetMs: number, nowMs: number): string { - const delta = targetMs - nowMs; - return delta >= 0 ? `in ${formatDuration(delta)}` : `${formatDuration(delta)} ago`; -} - -function formatWindow(window: UsageWindow, now: number): string { - const parts: string[] = []; - if (typeof window.remainingPercent === "number") { - parts.push(`${window.remainingPercent}% remaining`); - } - if (typeof window.resetsAt === "number") { - parts.push(`resets ${iso(window.resetsAt)} (${formatRelative(window.resetsAt, now)})`); - } - return `${window.label}: ${parts.join(", ")}`; -} - -/** - * Render normalized usage entries into an AI-friendly text block. Pure — `now` - * is injected so relative timestamps are deterministic under test. - */ -export function formatKeyUsage(entries: KeyUsageEntry[], now: number): string { - if (entries.length === 0) return "No API keys matched."; - - const lines: string[] = []; - lines.push(`API key usage — ${entries.length} key${entries.length === 1 ? "" : "s"}:`); - - for (const entry of entries) { - lines.push(""); - lines.push(`[${entry.keyId}] provider: ${entry.provider}`); - - if (entry.status === "exhausted") { - const since = - typeof entry.exhaustedAt === "number" - ? ` (since ${iso(entry.exhaustedAt)}, ${formatRelative(entry.exhaustedAt, now)})` - : ""; - lines.push(`status: EXHAUSTED${since}`); - if (entry.lastError) lines.push(`last error: ${entry.lastError}`); - } else { - lines.push("status: active"); - } - - if (entry.unsupported) { - lines.push( - `usage: not supported for provider "${entry.provider}" (only anthropic and opencode-go report usage)`, - ); - continue; - } - - if (entry.dataSource === "live") { - lines.push("data: live (fetched just now)"); - } else if (entry.dataSource === "cache") { - lines.push( - typeof entry.cachedAt === "number" - ? `data: cached — last fetched from source ${iso(entry.cachedAt)} (${formatRelative(entry.cachedAt, now)})` - : "data: cached (source timestamp unknown)", - ); - } - - for (const window of entry.windows) { - lines.push(formatWindow(window, now)); - } - - if (entry.unavailableReason) { - lines.push(`usage: unavailable — ${entry.unavailableReason}`); - } - } - - return lines.join("\n"); -} - -async function buildEntry( - key: KeyState, - accounts: ClaudeAccount[], - fetchAnthropic: (account: ClaudeAccount) => Promise<ClaudeUsageResult | null>, - fetchOpencode: (keyId: string) => Promise<OpencodeUsageReport | null>, -): Promise<KeyUsageEntry> { - const def = key.definition; - const entry: KeyUsageEntry = { - keyId: def.id, - provider: def.provider, - status: key.status, - windows: [], - ...(key.lastError ? { lastError: key.lastError } : {}), - ...(typeof key.exhaustedAt === "number" ? { exhaustedAt: key.exhaustedAt } : {}), - }; - - if (def.provider === "anthropic") { - const account = matchAnthropicAccount(accounts, def.id, def.credentials_file); - if (!account) { - entry.unavailableReason = "no Claude account credentials available for this key"; - return entry; - } - let result: ClaudeUsageResult | null = null; - try { - result = await fetchAnthropic(account); - } catch { - result = null; - } - if (!result) { - entry.unavailableReason = "no live usage data and no cached usage available"; - return entry; - } - entry.dataSource = result.source; - if (typeof result.cachedAt === "number") entry.cachedAt = result.cachedAt; - entry.windows = anthropicWindows(result.report); - if (entry.windows.length === 0) { - entry.unavailableReason = "usage endpoint returned no window data"; - } - return entry; - } - - if (def.provider === "opencode-go") { - let report: OpencodeUsageReport | null = null; - try { - report = await fetchOpencode(def.id); - } catch { - report = null; - } - if (!report) { - entry.unavailableReason = - "live usage unavailable (requires OPENCODE_COOKIE and a workspace id, or the source returned no data; OpenCode keeps no local cache)"; - return entry; - } - entry.dataSource = "live"; - entry.windows = opencodeWindows(report); - if (entry.windows.length === 0) { - entry.unavailableReason = "usage source returned no window data"; - } - return entry; - } - - entry.unsupported = true; - return entry; -} - -export function createKeyUsageTool(callbacks: KeyUsageCallbacks): ToolDefinition { - const fetchAnthropic = callbacks.fetchAnthropicUsage ?? getAccountUsageWithSource; - const fetchOpencode = callbacks.fetchOpencodeUsage ?? defaultFetchOpencodeUsage; - - return { - name: "key_usage", - description: [ - "Report current usage levels for configured API keys so you can pick a key with", - "headroom, warn before hitting a rate limit, or diagnose an exhausted-key failure.", - "", - "For each key it returns: provider, active/exhausted status (with the last error when", - "exhausted), remaining rate-limit headroom per window (5-hour, weekly, and monthly where", - "the provider exposes it), each window's reset timestamp, and whether the figures are", - "live or served from cache (with the cache's last-fetched time).", - "", - "Pass a key_id to inspect one key; omit it to report all keys. Usage reporting is", - "supported for anthropic and opencode-go keys.", - ].join("\n"), - parameters: z.object({ - key_id: z - .string() - .optional() - .describe( - 'The id of a single key to report (as configured in dispatch.toml, e.g. "claude-max"). Omit to report all configured keys.', - ), - }), - execute: async (args: Record<string, unknown>): Promise<string> => { - const requestedKeyId = (args.key_id as string | undefined)?.trim() || undefined; - - const allKeys = callbacks.listKeys(); - if (allKeys.length === 0) { - return "No API keys are configured."; - } - - let keys = allKeys; - if (requestedKeyId) { - keys = allKeys.filter((k) => k.definition.id === requestedKeyId); - if (keys.length === 0) { - const available = allKeys.map((k) => k.definition.id).join(", "); - return `Error: no key found with id "${requestedKeyId}". Available keys: ${available}.`; - } - } - - const accounts = callbacks.listClaudeAccounts(); - const entries: KeyUsageEntry[] = []; - for (const key of keys) { - entries.push(await buildEntry(key, accounts, fetchAnthropic, fetchOpencode)); - } - - return formatKeyUsage(entries, Date.now()); - }, - }; -} diff --git a/packages/core/src/tools/list-files.ts b/packages/core/src/tools/list-files.ts deleted file mode 100644 index e003099..0000000 --- a/packages/core/src/tools/list-files.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { readdir } from "node:fs/promises"; -import { z } from "zod"; -import type { ToolDefinition } from "../types/index.js"; -import { canonicalize } from "./path-utils.js"; - -export function createListFilesTool(workingDirectory: string): ToolDefinition { - return { - name: "list_files", - description: "List files and directories at a path relative to the working directory.", - parameters: z.object({ - path: z - .string() - .optional() - .describe("Path to list, relative to the working directory. Defaults to '.'"), - }), - execute: async (args: Record<string, unknown>): Promise<string> => { - const relPath = (args.path as string | undefined) ?? "."; - // Canonicalize so a symlink-in-workdir pointing outside is detected. - // See `canonicalize` in ./path-utils.ts for the resolution semantics. - const absolutePath = await canonicalize(workingDirectory, relPath); - const absoluteWorkDir = await canonicalize(workingDirectory); - - if (absolutePath !== absoluteWorkDir && !absolutePath.startsWith(`${absoluteWorkDir}/`)) { - return `Error: Path "${relPath}" is outside the working directory.`; - } - - try { - const entries = await readdir(absolutePath, { withFileTypes: true }); - if (entries.length === 0) { - return "(empty directory)"; - } - return entries - .map((entry) => (entry.isDirectory() ? `${entry.name}/` : entry.name)) - .join("\n"); - } catch (err) { - return `Error listing files: ${err instanceof Error ? err.message : String(err)}`; - } - }, - }; -} diff --git a/packages/core/src/tools/lsp.ts b/packages/core/src/tools/lsp.ts deleted file mode 100644 index 3842cd4..0000000 --- a/packages/core/src/tools/lsp.ts +++ /dev/null @@ -1,135 +0,0 @@ -import { isAbsolute, resolve } from "node:path"; -import { pathToFileURL } from "node:url"; -import { z } from "zod"; -import { report as reportDiagnostics } from "../lsp/diagnostic.js"; -import type { LspManager } from "../lsp/manager.js"; -import type { ResolvedLspServer } from "../lsp/server.js"; -import type { ToolDefinition } from "../types/index.js"; - -const OPERATIONS = ["diagnostics", "hover", "definition", "references", "documentSymbol"] as const; -type Operation = (typeof OPERATIONS)[number]; - -/** - * Context the LSP tool needs from the host: the live manager, the tab's - * effective working directory (used as the LSP `root`), and the servers - * resolved from that directory's `dispatch.toml`. - */ -export interface LspToolContext { - manager: LspManager; - workingDirectory: string; - servers: ResolvedLspServer[]; -} - -/** - * On-demand LSP query tool. Exposes diagnostics plus the navigation - * capabilities (hover/definition/references/documentSymbol) for a file at a - * position. Gated behind `perm_lsp` by the host. - * - * Coordinates are **1-based** in this tool's API (editor-style, matching what - * `read_file` shows); they are converted to the LSP wire's 0-based positions - * before the request. - */ -export function createLspTool(getContext: () => LspToolContext): ToolDefinition { - return { - name: "lsp", - description: - "Query the configured Language Server (e.g. luau-lsp for Roblox Luau) about a file. " + - "Operations: 'diagnostics' (type/lint errors for a file), 'hover' (type/docs at a position), " + - "'definition' (where a symbol is defined), 'references' (all uses of a symbol), " + - "'documentSymbol' (outline of a file). Line and character are 1-based (as shown in editors). " + - "Returns JSON. Requires an [lsp] server configured in dispatch.toml that matches the file's extension.", - parameters: z.object({ - operation: z.enum(OPERATIONS).describe("The LSP operation to perform"), - path: z.string().describe("Path to the file, relative to the working directory"), - line: z - .number() - .int() - .min(1) - .optional() - .describe( - "Line number, 1-based (as shown in editors). Required for hover/definition/references.", - ), - character: z - .number() - .int() - .min(1) - .optional() - .describe( - "Character/column, 1-based (as shown in editors). Required for hover/definition/references.", - ), - }), - execute: async (args: Record<string, unknown>): Promise<string> => { - const { manager, workingDirectory, servers } = getContext(); - const operation = args.operation as Operation; - const pathArg = typeof args.path === "string" ? args.path : ""; - if (!pathArg) return "Error: 'path' is required."; - - const file = isAbsolute(pathArg) ? pathArg : resolve(workingDirectory, pathArg); - - if (servers.length === 0) { - return "Error: no LSP servers are configured. Add an [lsp] entry to dispatch.toml."; - } - if (!manager.hasServerForFile(file, servers)) { - return `Error: no configured LSP server matches "${pathArg}" (check the server's extensions in dispatch.toml).`; - } - - // Sync the file so the server has current content, then act. - await manager.touchFile({ file, root: workingDirectory, servers, mode: "document" }); - - if (operation === "diagnostics") { - const all = manager.getDiagnostics({ root: workingDirectory, servers, file }); - const block = reportDiagnostics(file, all[file] ?? []); - return block || `No errors reported for ${pathArg}.`; - } - - if (operation === "documentSymbol") { - const uri = pathToFileURL(file).href; - const results = await manager.request({ - file, - root: workingDirectory, - servers, - method: "textDocument/documentSymbol", - params: { textDocument: { uri } }, - }); - const flat = results.flat().filter(Boolean); - return flat.length === 0 - ? `No symbols found in ${pathArg}.` - : JSON.stringify(flat, null, 2); - } - - // Positional operations need line + character. - const line = typeof args.line === "number" ? Math.floor(args.line) : undefined; - const character = typeof args.character === "number" ? Math.floor(args.character) : undefined; - if (line === undefined || character === undefined) { - return `Error: '${operation}' requires both 'line' and 'character' (1-based).`; - } - - const uri = pathToFileURL(file).href; - // Convert editor 1-based → LSP wire 0-based. - const position = { line: line - 1, character: character - 1 }; - const method = - operation === "hover" - ? "textDocument/hover" - : operation === "definition" - ? "textDocument/definition" - : "textDocument/references"; - const params: Record<string, unknown> = { - textDocument: { uri }, - position, - ...(operation === "references" ? { context: { includeDeclaration: true } } : {}), - }; - - const results = await manager.request({ - file, - root: workingDirectory, - servers, - method, - params, - }); - const flat = results.flat().filter(Boolean); - return flat.length === 0 - ? `No results found for ${operation} at ${pathArg}:${line}:${character}.` - : JSON.stringify(flat, null, 2); - }, - }; -} diff --git a/packages/core/src/tools/path-utils.ts b/packages/core/src/tools/path-utils.ts deleted file mode 100644 index 3bba0d3..0000000 --- a/packages/core/src/tools/path-utils.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { realpath } from "node:fs/promises"; -import { basename, dirname, join, resolve } from "node:path"; - -/** - * Resolve a path to its canonical absolute form, following symlinks at - * every level. - * - * When the leaf does not exist (common for `write_file` creating a new - * file), walks up to the nearest existing ancestor, canonicalizes that, - * then re-appends the missing trailing segments — ensuring a symlink in - * the *middle* of the path is still resolved. Without this, a - * `workdir/escape-link/new-file.txt` write where `escape-link` points - * outside the workdir would slip the containment check. - * - * Used everywhere we compare a user-supplied path against a trusted - * root (workdir, SPILL_ROOT). Resolving symlinks consistently is the - * only reliable way to detect a symlink-in-workdir-pointing-outside - * escape; lexical-only checks let those through. - * - * Argument semantics match `path.resolve(...paths)`: later absolute - * segments override earlier ones, relative segments are joined. - */ -export async function canonicalize(...paths: string[]): Promise<string> { - const lexical = resolve(...paths); - - // Fast path: full path exists, realpath resolves all symlinks. - try { - return await realpath(lexical); - } catch { - // Path doesn't exist — fall through to ancestor walk. - } - - // Walk up until we hit an existing ancestor we can realpath, then - // re-append the missing trailing segments. This handles cases like - // write_file creating /workdir/symlink/new/dir/file.txt where the - // "symlink" segment exists but the rest doesn't. - let current = lexical; - const trailing: string[] = []; - while (true) { - const parent = dirname(current); - if (parent === current) break; // hit filesystem root - trailing.unshift(basename(current)); - try { - const realParent = await realpath(parent); - return join(realParent, ...trailing); - } catch { - current = parent; - } - } - - // No existing ancestor (pathological — e.g. the entire mount is gone). - // Return the lexical path; downstream containment checks will still - // catch obvious escapes via `..` etc. - return lexical; -} diff --git a/packages/core/src/tools/read-file-slice.ts b/packages/core/src/tools/read-file-slice.ts deleted file mode 100644 index 0f83bcb..0000000 --- a/packages/core/src/tools/read-file-slice.ts +++ /dev/null @@ -1,90 +0,0 @@ -import { readFile } from "node:fs/promises"; -import { z } from "zod"; -import type { ToolDefinition } from "../types/index.js"; -import { canonicalize } from "./path-utils.js"; -import { SPILL_ROOT } from "./truncate.js"; - -const DEFAULT_LENGTH = 5000; - -export function createReadFileSliceTool(workingDirectory: string): ToolDefinition { - return { - name: "read_file_slice", - description: - "Read a character-range slice of a single line in a file. Use this when read_file returns a truncated line marker like '[line N truncated, total X chars; use read_file_slice ...]', or when you need precise byte-ish access into a minified file (huge JSON, base64 blob, etc.). For normal line-oriented reading use read_file with offset/limit instead.", - parameters: z.object({ - path: z.string().describe("Path to the file, relative to the working directory."), - line: z.number().int().min(1).describe("1-indexed line number to slice into."), - charOffset: z - .number() - .int() - .min(0) - .optional() - .describe("0-indexed character offset within the line. Default: 0 (start of line)."), - charLength: z - .number() - .int() - .min(1) - .optional() - .describe( - `Max characters to return from the slice. Default: ${DEFAULT_LENGTH}. The universal tool-output truncator will still spill if the result is huge.`, - ), - }), - execute: async (args: Record<string, unknown>): Promise<string> => { - const filePath = args.path as string; - const lineNumber = args.line as number; - const charOffset = - typeof args.charOffset === "number" ? Math.max(0, Math.floor(args.charOffset)) : 0; - const charLength = - typeof args.charLength === "number" - ? Math.max(1, Math.floor(args.charLength)) - : DEFAULT_LENGTH; - - // Canonicalize all three so symlink-in-workdir escapes are detected. - // See `canonicalize` in ./path-utils.ts for the resolution semantics. - const absolutePath = await canonicalize(workingDirectory, filePath); - const absoluteWorkDir = await canonicalize(workingDirectory); - const absoluteSpillRoot = await canonicalize(SPILL_ROOT); - const isUnderWorkdir = - absolutePath === absoluteWorkDir || absolutePath.startsWith(`${absoluteWorkDir}/`); - const isSpillFile = - absolutePath === absoluteSpillRoot || absolutePath.startsWith(`${absoluteSpillRoot}/`); - - if (!isUnderWorkdir && !isSpillFile) { - return `Error: Path "${filePath}" is outside the working directory.`; - } - - let raw: string; - try { - raw = await readFile(absolutePath, "utf8"); - } catch (err) { - const code = (err as NodeJS.ErrnoException).code; - if (code === "ENOENT") { - return `Error: File "${filePath}" not found.`; - } - return `Error reading file: ${err instanceof Error ? err.message : String(err)}`; - } - - const lines = raw.split("\n"); - const trailingNewline = raw.endsWith("\n"); - const totalLines = trailingNewline ? lines.length - 1 : lines.length; - - if (lineNumber > totalLines) { - return `Error: line ${lineNumber} exceeds file length (${totalLines} lines).`; - } - - const line = lines[lineNumber - 1] ?? ""; - const lineLength = line.length; - - if (charOffset >= lineLength) { - return `Error: charOffset ${charOffset} exceeds line length (${lineLength} chars).`; - } - - const sliceEnd = Math.min(charOffset + charLength, lineLength); - const slice = line.slice(charOffset, sliceEnd); - const remaining = lineLength - sliceEnd; - - const header = `[file: ${filePath} — line ${lineNumber}, chars ${charOffset}-${sliceEnd} of ${lineLength}${remaining > 0 ? ` (${remaining.toLocaleString()} chars remain after this slice)` : ""}]`; - return `${header}\n${slice}`; - }, - }; -} diff --git a/packages/core/src/tools/read-file.ts b/packages/core/src/tools/read-file.ts deleted file mode 100644 index fa82dce..0000000 --- a/packages/core/src/tools/read-file.ts +++ /dev/null @@ -1,129 +0,0 @@ -import { readFile } from "node:fs/promises"; -import { z } from "zod"; -import type { ToolDefinition } from "../types/index.js"; -import { canonicalize } from "./path-utils.js"; -import { MAX_LINES, SPILL_ROOT } from "./truncate.js"; - -// Per-line truncation: any single line longer than MAX_LINE_CHARS is cut and -// replaced with a marker indicating the total length. This protects against -// minified files / base64 blobs / massive single-line JSON. The AI can use -// `read_file_slice` to inspect a specific char range within a long line. -const MAX_LINE_CHARS = 2000; - -// Aligned with the universal truncator's MAX_LINES so a default-args read -// returns a response that fits under the truncator's line ceiling. Without -// this alignment, every default read of a >500-line file got returned by -// the tool and then immediately spilled by the truncator — wasted work. -// Char-dense files can still spill via MAX_CHARS, but that's content- -// dependent rather than guaranteed. -const DEFAULT_LIMIT = MAX_LINES; -// Hard cap on lines per request even if `limit` is larger or omitted. -// Prevents a `read_file(huge.log)` with no params from returning a million -// lines. The universal truncator at the agent level will spill anything -// over its own threshold, but this is a tighter first line of defense -// scoped to the read tool itself. -const HARD_LIMIT = 5000; - -export function createReadFileTool(workingDirectory: string): ToolDefinition { - return { - name: "read_file", - description: - "Read a file relative to the working directory. Returns up to `limit` lines starting at line `offset` (1-indexed). Lines longer than 2000 chars are truncated mid-line with a marker showing the total length — use the `read_file_slice` tool to read a specific char range within a long line. If the response is still too large, the dispatch tool-output truncator may spill the full content to /tmp/dispatch/tool-results/.", - parameters: z.object({ - path: z.string().describe("Path to the file, relative to the working directory"), - offset: z - .number() - .int() - .min(1) - .optional() - .describe("1-indexed start line. Default: 1 (start of file)."), - limit: z - .number() - .int() - .min(1) - .optional() - .describe( - `Max lines to return. Default: ${DEFAULT_LIMIT}. Hard cap: ${HARD_LIMIT}. Use a small limit when exploring a large file.`, - ), - }), - execute: async (args: Record<string, unknown>): Promise<string> => { - const filePath = args.path as string; - const offset = typeof args.offset === "number" ? Math.max(1, Math.floor(args.offset)) : 1; - const requestedLimit = - typeof args.limit === "number" ? Math.max(1, Math.floor(args.limit)) : DEFAULT_LIMIT; - const limit = Math.min(requestedLimit, HARD_LIMIT); - - // Canonicalize all three so symlink-in-workdir escapes are detected: - // a workdir-relative path that resolves through symlinks to /etc must - // fail the containment check below. - const absolutePath = await canonicalize(workingDirectory, filePath); - const absoluteWorkDir = await canonicalize(workingDirectory); - const absoluteSpillRoot = await canonicalize(SPILL_ROOT); - const isUnderWorkdir = - absolutePath === absoluteWorkDir || absolutePath.startsWith(`${absoluteWorkDir}/`); - const isSpillFile = - absolutePath === absoluteSpillRoot || absolutePath.startsWith(`${absoluteSpillRoot}/`); - - if (!isUnderWorkdir && !isSpillFile) { - return `Error: Path "${filePath}" is outside the working directory.`; - } - - let raw: string; - try { - raw = await readFile(absolutePath, "utf8"); - } catch (err) { - const code = (err as NodeJS.ErrnoException).code; - if (code === "ENOENT") { - return `Error: File "${filePath}" not found.`; - } - return `Error reading file: ${err instanceof Error ? err.message : String(err)}`; - } - - // A truly empty file (0 bytes) would otherwise slip through the - // line-counting below: `"".split("\n")` is `[""]` and there's no - // trailing newline, yielding a spurious `totalLines === 1`. - if (raw === "") { - return `(empty file: ${filePath})`; - } - - const allLines = raw.split("\n"); - // `split("\n")` produces an extra empty entry when the file ends with a - // newline. The total line count we report to the caller should match - // the human-visible line count (lines that have content or terminate - // with \n). - const trailingNewline = raw.endsWith("\n"); - const totalLines = trailingNewline ? allLines.length - 1 : allLines.length; - - if (totalLines === 0) { - return `(empty file: ${filePath})`; - } - - if (offset > totalLines) { - return `Error: offset ${offset} exceeds file length (${totalLines} lines).`; - } - - const startIdx = offset - 1; // 0-indexed - const endIdx = Math.min(startIdx + limit, totalLines); - const slice = allLines.slice(startIdx, endIdx); - - // Apply per-line truncation. We tag truncated lines with the line - // number and total chars so the AI knows how to call read_file_slice. - const rendered: string[] = []; - for (let i = 0; i < slice.length; i++) { - const lineNumber = startIdx + i + 1; - const line = slice[i] ?? ""; - if (line.length > MAX_LINE_CHARS) { - const visible = line.slice(0, MAX_LINE_CHARS); - rendered.push( - `${visible}...[line ${lineNumber} truncated, total ${line.length.toLocaleString()} chars; use read_file_slice with path="${filePath}" line=${lineNumber} to read more]`, - ); - } else { - rendered.push(line); - } - } - - const header = `[file: ${filePath} — lines ${offset}-${endIdx} of ${totalLines}]`; - return `${header}\n${rendered.join("\n")}`; - }, - }; -} diff --git a/packages/core/src/tools/read-tab.ts b/packages/core/src/tools/read-tab.ts deleted file mode 100644 index e80dbd0..0000000 --- a/packages/core/src/tools/read-tab.ts +++ /dev/null @@ -1,95 +0,0 @@ -import { z } from "zod"; -import type { AgentStatus, ToolDefinition } from "../types/index.js"; -import type { TabResolution } from "./send-to-tab.js"; - -export interface ReadTabCallbacks { - /** Resolve a (possibly short) handle to one open tab. */ - resolveShortId(prefix: string): TabResolution; - /** - * Return the target tab's most recent COMPLETED assistant turn as plain - * text, plus its current status. `text` is null when the tab has no - * completed assistant turn yet. - */ - getLastResponse(tabId: string): { text: string | null; status: AgentStatus }; - /** Snapshot of currently-open tabs, for "available tabs" error hints. */ - listOpenHandles(): Array<{ handle: string; title: string }>; -} - -/** Render the "available tabs" hint shared by the none/ambiguous branches. */ -function renderOpenHandles(handles: Array<{ handle: string; title: string }>): string { - if (handles.length === 0) return "No other tabs are currently open."; - const lines = handles.map((h) => ` - ${h.handle}: ${h.title}`); - return ["Currently open tabs:", ...lines].join("\n"); -} - -export function createReadTabTool(callbacks: ReadTabCallbacks): ToolDefinition { - return { - name: "read_tab", - description: [ - "Read the most recent completed response from another tab (agent) by its short ID.", - "", - "Returns a SNAPSHOT — it does NOT block or wait for the target to finish.", - " - If the target is idle, you get its just-finished turn.", - " - If the target is still running, you get its PREVIOUS completed turn (if any);", - " call read_tab again later to get the newest one.", - "", - "Use this after send_to_tab to collect another agent's reply. IDs are git-style", - "prefixes: pass any length that uniquely identifies the target (min 4 chars).", - ].join("\n"), - parameters: z.object({ - tab_id: z - .string() - .describe( - "The short ID (handle) of the tab to read, as shown in the tab bar. Any unique-length prefix works (min 4 chars).", - ), - }), - execute: async (args: Record<string, unknown>): Promise<string> => { - const rawId = (args.tab_id as string | undefined)?.trim() ?? ""; - - if (!rawId) { - return `Error: tab_id is required.\n\n${renderOpenHandles(callbacks.listOpenHandles())}`; - } - - const resolution = callbacks.resolveShortId(rawId); - - if (resolution.status === "none") { - return [ - `Error: no open tab matches the ID "${rawId}".`, - "", - renderOpenHandles(callbacks.listOpenHandles()), - ].join("\n"); - } - if (resolution.status === "ambiguous") { - const matches = resolution.matches.map((m) => ` - ${m.handle}: ${m.title}`).join("\n"); - return [ - `Error: the ID "${rawId}" is ambiguous — it matches multiple open tabs:`, - matches, - "", - "Add one or more characters to disambiguate.", - ].join("\n"); - } - - const target = resolution.tab; - const { text, status } = callbacks.getLastResponse(target.id); - - const runningNote = - status === "running" - ? " (this tab is still running; the response below is its previous completed turn — read again later for the newest)" - : ""; - - if (text === null) { - const reason = - status === "running" - ? "it is still working on its first turn" - : "it has no assistant responses yet"; - return `Tab ${target.handle} (${target.title}) has no completed response — ${reason}.`; - } - - return [ - `<tab_response tab="${target.handle}" status="${status}"${runningNote}>`, - text, - "</tab_response>", - ].join("\n"); - }, - }; -} diff --git a/packages/core/src/tools/registry.ts b/packages/core/src/tools/registry.ts deleted file mode 100644 index ff6f4d1..0000000 --- a/packages/core/src/tools/registry.ts +++ /dev/null @@ -1,91 +0,0 @@ -import type { Tool } from "ai"; -import { jsonSchema, tool } from "ai"; -import { zodToJsonSchema } from "zod-to-json-schema"; -import type { ToolDefinition } from "../types/index.js"; - -/** - * Strip JSON Schema fields that Anthropic's API does not accept from a - * `zodToJsonSchema()` output. The Anthropic `/messages` API rejects (or - * silently ignores) tools whose `input_schema` contains `$schema`, - * `additionalProperties`, `default`, or `nullable` — when this happens - * Claude never sees the tool and the model "thinks forever" instead of - * calling it. - * - * The stripped fields are also harmless to remove for OpenAI-compatible - * endpoints, so we apply this unconditionally. - */ -function normalizeForAnthropic(schema: Record<string, unknown>): Record<string, unknown> { - delete schema.$schema; - delete schema.additionalProperties; - delete schema.default; - delete schema.nullable; - - const properties = schema.properties; - if (properties && typeof properties === "object") { - for (const key of Object.keys(properties as Record<string, unknown>)) { - const prop = (properties as Record<string, unknown>)[key]; - if (prop && typeof prop === "object") { - normalizeForAnthropic(prop as Record<string, unknown>); - } - } - } - - const items = schema.items; - if (items && typeof items === "object") { - normalizeForAnthropic(items as Record<string, unknown>); - } - - return schema; -} - -/** - * Convert an internal `ToolDefinition` (Zod-parameterised) to an AI SDK v6 - * `Tool` object. - * - * Critically, NO `execute` function is attached. The agent's manual tool - * loop (see agent.ts) handles execution itself — for permission prompts, - * shell-output streaming, and queued-message injection. Without `execute`, - * the SDK never auto-runs tools; it only surfaces `tool-call` events from - * `fullStream` that agent.ts collects and dispatches. - */ -function toAISDKTool(def: ToolDefinition): Tool { - const raw = zodToJsonSchema(def.parameters) as Record<string, unknown>; - const normalized = normalizeForAnthropic(raw); - return tool({ - description: def.description, - inputSchema: jsonSchema(normalized), - }); -} - -export function createToolRegistry(tools: ToolDefinition[]) { - const toolMap = new Map<string, ToolDefinition>(tools.map((t) => [t.name, t])); - - return { - getTools(): ToolDefinition[] { - return [...toolMap.values()]; - }, - - getTool(name: string): ToolDefinition | undefined { - return toolMap.get(name); - }, - - /** - * Returns AI SDK v6 `Tool` objects keyed by tool name, for passing - * directly to `streamText({ tools })`. - * - * Each tool has: - * - `description` — forwarded verbatim from the internal definition. - * - `inputSchema` — Zod schema converted to JSONSchema7 via - * `zod-to-json-schema`, then wrapped with the v6 - * `jsonSchema()` helper. - * - NO `execute` — intentional; see `toAISDKTool` above. - */ - getAISDKTools(): Record<string, Tool> { - const result: Record<string, Tool> = {}; - for (const [name, def] of toolMap) { - result[name] = toAISDKTool(def); - } - return result; - }, - }; -} diff --git a/packages/core/src/tools/retrieve.ts b/packages/core/src/tools/retrieve.ts deleted file mode 100644 index 80c3715..0000000 --- a/packages/core/src/tools/retrieve.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { z } from "zod"; -import type { ToolDefinition, ToolExecuteContext } from "../types/index.js"; - -export interface RetrieveCallbacks { - getResult( - agentId: string, - ): Promise<{ status: "done"; result: string } | { status: "error"; error: string }>; -} - -export function createRetrieveTool(callbacks: RetrieveCallbacks): ToolDefinition { - return { - name: "retrieve", - description: [ - "Wait for a child agent or backgrounded shell command to finish and retrieve its result. This tool BLOCKS until completion.", - "", - "Pass the ID returned by summon (agent_id) or by an interrupted run_shell (job_id). Once it finishes, the output is returned.", - "If an error occurred, the error message is returned instead.", - "", - "Typical usage:", - ' 1. summon({ task: "...", tools: [...] }) -> get agent_id', - " 2. ... do other work or summon more agents ...", - ' 3. retrieve({ agent_id: "..." }) -> blocks until done, returns result', - "", - "Also used for backgrounded shell commands:", - " If run_shell is interrupted by a user message, it returns a job_id (run_shell_...).", - ' Use retrieve({ agent_id: "run_shell_..." }) to get the final output when ready.', - ].join("\n"), - parameters: z.object({ - agent_id: z.string().describe("The agent_id returned by a previous summon call."), - }), - execute: async ( - args: Record<string, unknown>, - context?: ToolExecuteContext, - ): Promise<string> => { - const agentId = args.agent_id as string; - const queueCallbacks = context?.queueCallbacks; - - try { - let outcome: { status: "done"; result: string } | { status: "error"; error: string }; - - if (queueCallbacks) { - const childPromise = callbacks.getResult(agentId); - const { promise: queuePromise, cancel: cancelQueueWait } = - queueCallbacks.waitForQueuedMessage(); - const queueSignal = queuePromise.then(() => "QUEUE_INTERRUPT" as const); - - const raceResult = await Promise.race([childPromise, queueSignal]); - - if (raceResult === "QUEUE_INTERRUPT") { - const queuedMsgs = queueCallbacks.dequeueMessages(); - const userMessages = queuedMsgs.map((m) => m.message).join("\n---\n"); - return `The subagent (agent_id: ${agentId}) has not completed its task yet. You will need to call retrieve with this agent_id again later to get the result.\n\n[USER INTERRUPT]\nThe user has sent you message(s) while you were working. You MUST address these before continuing with your current task:\n\n${userMessages}`; - } - - // Child finished first — clean up the queue listener - cancelQueueWait(); - outcome = raceResult; - } else { - outcome = await callbacks.getResult(agentId); - } - - if (outcome.status === "done") { - return ["<agent_result>", outcome.result, "</agent_result>"].join("\n"); - } - return `Agent error: ${outcome.error}`; - } catch (err) { - return `Error retrieving result: ${err instanceof Error ? err.message : String(err)}`; - } - }, - }; -} diff --git a/packages/core/src/tools/run-shell.ts b/packages/core/src/tools/run-shell.ts deleted file mode 100644 index ec2db9c..0000000 --- a/packages/core/src/tools/run-shell.ts +++ /dev/null @@ -1,176 +0,0 @@ -import { spawn } from "node:child_process"; -import { randomUUID } from "node:crypto"; -import { z } from "zod"; -import type { ToolDefinition, ToolExecuteContext } from "../types/index.js"; - -const DEFAULT_TIMEOUT = 2 * 60 * 1000; // 2 minutes - -export interface BackgroundShellJob { - command: string; - stdout: string; - stderr: string; - /** Resolves when the process exits */ - completion: Promise<{ stdout: string; stderr: string; exitCode: number; error?: string }>; -} - -/** Shared store for shell commands that were backgrounded due to user interrupt */ -export class BackgroundShellStore { - private jobs = new Map<string, BackgroundShellJob>(); - - register(job: BackgroundShellJob): string { - const id = `run_shell_${randomUUID()}`; - this.jobs.set(id, job); - // Auto-cleanup after completion + 10 minutes - job.completion.finally(() => { - setTimeout(() => this.jobs.delete(id), 10 * 60 * 1000); - }); - return id; - } - - async getResult( - id: string, - ): Promise<{ status: "done"; result: string } | { status: "error"; error: string }> { - const job = this.jobs.get(id); - if (!job) { - return { status: "error", error: `No background shell job found with id '${id}'` }; - } - const result = await job.completion; - return { status: "done", result: JSON.stringify(result) }; - } - - has(id: string): boolean { - return this.jobs.has(id); - } -} - -export function createRunShellTool( - workingDirectory: string, - shellStore?: BackgroundShellStore, -): ToolDefinition { - return { - name: "run_shell", - description: - "Execute a shell command in the working directory. Returns stdout, stderr, and exit code. Use for running tests, builds, git operations, package management, and other development tasks. If the user interrupts while a command is running, the command continues in the background and you receive a job ID. Use the retrieve tool with that ID to get the result later.", - parameters: z.object({ - command: z.string().describe("The shell command to execute"), - timeout: z.number().optional().describe("Timeout in milliseconds (default 2 minutes)"), - background: z - .boolean() - .optional() - .describe( - "If true, the command starts in the background and a job_id is returned immediately. Use the retrieve tool with the job_id to get the result later.", - ), - }), - execute: async ( - args: Record<string, unknown>, - context?: ToolExecuteContext, - ): Promise<string> => { - const command = args.command as string; - const timeout = (args.timeout as number | undefined) ?? DEFAULT_TIMEOUT; - const background = (args.background as boolean | undefined) ?? false; - - const [shell, shellArgs] = getShell(); - const child = spawn(shell, [...shellArgs, command], { - cwd: workingDirectory, - env: process.env, - timeout, - stdio: ["ignore", "pipe", "pipe"], - }); - - let stdout = ""; - let stderr = ""; - - const completionPromise = new Promise<{ - stdout: string; - stderr: string; - exitCode: number; - error?: string; - }>((resolve) => { - child.stdout?.on("data", (data: Buffer) => { - const chunk = data.toString(); - stdout += chunk; - context?.onOutput?.(chunk, "stdout"); - }); - child.stderr?.on("data", (data: Buffer) => { - const chunk = data.toString(); - stderr += chunk; - context?.onOutput?.(chunk, "stderr"); - }); - - child.on("close", (exitCode) => { - resolve({ stdout, stderr, exitCode: exitCode ?? 1 }); - }); - - child.on("error", (err) => { - resolve({ stdout, stderr, exitCode: 1, error: err.message }); - }); - }); - - // If background mode requested, register immediately and return job ID - if (background && shellStore) { - const jobId = shellStore.register({ - command, - stdout, - stderr, - completion: completionPromise, - }); - return [ - `Command started in background.`, - `job_id: ${jobId}`, - `command: ${command}`, - ``, - `Use the retrieve tool with this job_id to get the result when ready.`, - ].join("\n"); - } - - const queueCallbacks = context?.queueCallbacks; - - if (queueCallbacks && shellStore) { - const { promise: queuePromise, cancel: cancelQueueWait } = - queueCallbacks.waitForQueuedMessage(); - const queueSignal = queuePromise.then(() => "QUEUE_INTERRUPT" as const); - - const raceResult = await Promise.race([completionPromise, queueSignal]); - - if (raceResult === "QUEUE_INTERRUPT") { - // Background the still-running process - const jobId = shellStore.register({ - command, - stdout, - stderr, - completion: completionPromise, - }); - - const queuedMsgs = queueCallbacks.dequeueMessages(); - const userMessages = queuedMsgs.map((m) => m.message).join("\n---\n"); - - return [ - `Command backgrounded — still running.`, - `job_id: ${jobId}`, - `command: ${command}`, - `stdout so far: ${stdout.slice(-500) || "(none)"}`, - `stderr so far: ${stderr.slice(-500) || "(none)"}`, - ``, - `Use the retrieve tool with this job_id to get the final result when ready.`, - ``, - `[USER INTERRUPT]`, - `The user has sent you message(s) while you were working. You MUST address these before continuing with your current task:`, - ``, - userMessages, - ].join("\n"); - } - - // Command finished before interrupt — clean up queue listener - cancelQueueWait(); - return JSON.stringify(raceResult); - } - - const result = await completionPromise; - return JSON.stringify(result); - }, - }; -} - -function getShell(): [string, string[]] { - return process.platform === "win32" ? ["powershell", ["-Command"]] : ["bash", ["-c"]]; -} diff --git a/packages/core/src/tools/search-code.ts b/packages/core/src/tools/search-code.ts deleted file mode 100644 index 4350f6a..0000000 --- a/packages/core/src/tools/search-code.ts +++ /dev/null @@ -1,362 +0,0 @@ -import { spawn } from "node:child_process"; -import { stat } from "node:fs/promises"; -import { relative, sep } from "node:path"; -import { z } from "zod"; -import type { ToolDefinition } from "../types/index.js"; -import { canonicalize } from "./path-utils.js"; - -// Resolve the `cs` binary: an explicit override wins, otherwise rely on PATH. -// The deployed images build a patched, statically-linked `cs` into -// /usr/local/bin/cs (see Dockerfile); local dev can point DISPATCH_CS_BIN at a -// custom build. Read at call time so the environment can change at runtime -// (and so tests can point it at a stub or temp build). -function resolveCsBin(): string { - return process.env.DISPATCH_CS_BIN || "cs"; -} - -const DEFAULT_RESULT_LIMIT = 20; -const MAX_RESULT_LIMIT = 100; -const MAX_CONTEXT = 20; -const MIN_SNIPPET_LENGTH = 50; -const MAX_SNIPPET_LENGTH = 2000; -const TIMEOUT_MS = 30_000; -// Hard cap on any single rendered snippet line. Mirrors read-file.ts so a -// matched minified/generated line (e.g. a 2 MB bundle line) can't blow up the -// payload. The universal truncator bounds total output; this bounds per-line. -const MAX_LINE_CHARS = 500; - -/** Maps the `only` enum to the corresponding cs flag. */ -const ONLY_FLAGS: Record<string, string> = { - code: "--only-code", - comments: "--only-comments", - strings: "--only-strings", - declarations: "--only-declarations", - usages: "--only-usages", -}; - -/** One line within a cs JSON match result. */ -interface CsLine { - line_number: number; - content: string; - match_positions?: Array<[number, number]>; -} - -/** A single file result in cs `-f json` output. */ -interface CsResult { - filename: string; - location: string; - score: number; - /** Present in "lines"/"grep" snippet modes. */ - lines?: CsLine[]; - /** - * Present instead of `lines` in cs's "snippet" mode (the default "auto" - * mode selects it for prose). We force a lines-based mode (see buildFlags), - * but this is kept as a defensive fallback so a content-shape result is - * still rendered rather than shown as a bare header. - */ - content?: string; - matchlocations?: Array<[number, number]>; - language?: string; - total_lines?: number; -} - -export function createSearchCodeTool(workingDirectory: string): ToolDefinition { - return { - name: "search_code", - description: - "Search the codebase by query using `cs` (code spelunker) — a fast, relevance-ranked code search engine. " + - "Prefer this over grep/find for EXPLORATORY 'where is X / how does Y work' searches: it ranks the most " + - "relevant files first and returns matching snippets with line numbers, so you spend fewer turns and tokens. " + - "It respects .gitignore and skips hidden/binary files. " + - 'Query syntax: space-separated terms are AND\'d; supports OR, NOT, "exact phrases", fuzzy~1, /regex/, and ' + - "metadata filters like lang:Go, file:test, path:src. " + - "It is a ranked text search, NOT a semantic/LSP index: it won't resolve types or imports. For an EXHAUSTIVE " + - "list of every exact match (e.g. before a rename), use run_shell with ripgrep (rg) instead.", - parameters: z.object({ - query: z - .string() - .describe( - 'The search query. Terms are AND\'d by default. Supports OR, NOT, "phrases", fuzzy~1, /regex/, and filters like lang:Go, file:test, path:src.', - ), - path: z - .string() - .optional() - .describe( - "Subdirectory to scope the search to, relative to the working directory. Defaults to the whole working directory.", - ), - case_sensitive: z - .boolean() - .optional() - .describe("Make the search case-sensitive. Default: false (case-insensitive)."), - include_ext: z - .string() - .optional() - .describe( - 'Comma-separated list of file extensions to limit the search to (case-sensitive), e.g. "go,ts,lua".', - ), - exclude_pattern: z - .string() - .optional() - .describe( - 'Comma-separated list of path patterns to exclude (case-sensitive), e.g. "vendor,_test.go".', - ), - context: z - .number() - .int() - .min(0) - .optional() - .describe( - `Lines of context to show before and after each matching line (0-${MAX_CONTEXT}). When set, switches to a grep-style per-line window.`, - ), - result_limit: z - .number() - .int() - .min(1) - .optional() - .describe( - `Maximum number of file results to return. Default: ${DEFAULT_RESULT_LIMIT}, max: ${MAX_RESULT_LIMIT}.`, - ), - snippet_length: z - .number() - .int() - .min(MIN_SNIPPET_LENGTH) - .optional() - .describe( - `Snippet size in bytes for prose/text files (${MIN_SNIPPET_LENGTH}-${MAX_SNIPPET_LENGTH}). Has little effect on code files, which use a fixed line window — use 'context' to widen code snippets.`, - ), - only: z - .enum(["code", "comments", "strings", "declarations", "usages"]) - .optional() - .describe( - "Restrict matches structurally: code, comments, strings, declarations (definitions like func/class/type), " + - "or usages (call sites). Best-effort and language-dependent — strong for Go/TypeScript/Python/Lua/Luau, " + - "unavailable for unsupported languages (which fall back to plain text ranking).", - ), - }), - execute: async (args: Record<string, unknown>): Promise<string> => { - const query = typeof args.query === "string" ? args.query : ""; - if (query.trim() === "") { - return "Error: query is required (a non-empty string)."; - } - - // Resolve and contain the optional search path within the workdir. - // Canonicalize so a symlink-in-workdir pointing outside is detected, - // matching the containment semantics of list_files / read_file. - const relPath = asString(args.path) ?? "."; - const absoluteWorkDir = await canonicalize(workingDirectory); - const searchDir = await canonicalize(workingDirectory, relPath); - if (searchDir !== absoluteWorkDir && !searchDir.startsWith(`${absoluteWorkDir}/`)) { - return `Error: Path "${relPath}" is outside the working directory.`; - } - - // cs's --dir expects a directory; pointing it at a file silently - // returns no matches. Catch that and give an actionable hint instead - // of a misleading "No matches found". - if (relPath !== ".") { - try { - const st = await stat(searchDir); - if (!st.isDirectory()) { - return `Error: Path "${relPath}" is a file, not a directory. The 'path' parameter scopes the search to a directory; use read_file to read a single file.`; - } - } catch { - return `Error: Path "${relPath}" does not exist in the working directory.`; - } - } - - const flags = buildFlags(args, searchDir); - // `--` terminates cs flag parsing so a query that begins with "-" - // (e.g. "-hello" or "--foo") is treated as the positional search term - // rather than parsed as a (possibly invalid) cs flag. - const spawnArgs = [...flags, "--", query]; - - let stdout = ""; - let stderr = ""; - const result = await new Promise<{ - code: number | null; - signal: NodeJS.Signals | null; - error?: string; - errorCode?: string; - }>((resolve) => { - const child = spawn(resolveCsBin(), spawnArgs, { - cwd: workingDirectory, - env: process.env, - timeout: TIMEOUT_MS, - stdio: ["ignore", "pipe", "pipe"], - }); - child.stdout?.on("data", (d: Buffer) => { - stdout += d.toString(); - }); - child.stderr?.on("data", (d: Buffer) => { - stderr += d.toString(); - }); - child.on("close", (code, signal) => resolve({ code, signal })); - child.on("error", (err) => - resolve({ - code: null, - signal: null, - error: err.message, - errorCode: (err as NodeJS.ErrnoException).code, - }), - ); - }); - - if (result.error) { - // The binary is missing or not executable — give an actionable hint. - if (result.errorCode === "ENOENT" || result.error.includes("ENOENT")) { - return missingBinaryError(); - } - return `Error: failed to run cs: ${result.error}`; - } - - // A signal kill (e.g. SIGTERM from the spawn timeout) or a non-zero - // exit means cs failed — surface it (with stderr) instead of silently - // reporting "No matches found". cs exits 0 even when there are no - // matches, so a clean exit always falls through to the parsing below. - if (result.signal) { - const detail = stderr.trim() ? `\n${stderr.trim()}` : ""; - if (result.signal === "SIGTERM") { - return `Error: cs search timed out after ${TIMEOUT_MS / 1000}s. Try a narrower query or a smaller path.${detail}`; - } - return `Error: cs was terminated by signal ${result.signal}.${detail}`; - } - if (result.code !== 0) { - const detail = stderr.trim() ? `\n${stderr.trim()}` : ""; - return `Error: cs exited with code ${result.code}.${detail}`; - } - - // cs prints `null` (and exit 0) when there are no matches. - const trimmed = stdout.trim(); - if (trimmed === "" || trimmed === "null") { - return "No matches found."; - } - - let parsed: CsResult[]; - try { - parsed = JSON.parse(trimmed) as CsResult[]; - } catch { - // Couldn't parse — surface what cs produced so the caller isn't blind. - const detail = stderr.trim() ? `\nstderr: ${stderr.trim()}` : ""; - return `Error: could not parse cs output as JSON.${detail}\n\nRaw output:\n${trimmed.slice(0, 2000)}`; - } - - if (!Array.isArray(parsed) || parsed.length === 0) { - return "No matches found."; - } - - return formatResults(parsed, absoluteWorkDir); - }, - }; -} - -/** Build the cs CLI flags (everything except the trailing query). */ -function buildFlags(args: Record<string, unknown>, searchDir: string): string[] { - const flags: string[] = ["-f", "json", "--dir", searchDir]; - - if (args.case_sensitive === true) flags.push("-c"); - - const includeExt = asString(args.include_ext); - if (includeExt) flags.push("-i", includeExt); - - const excludePattern = asString(args.exclude_pattern); - if (excludePattern) flags.push("-x", excludePattern); - - // Snippet mode selection. cs's default ("auto") emits a `lines[]` array for - // code but a single `content` string for prose (.md/.html/…), which our - // renderer can't show — so prose results would come back as bare headers. - // It also ignores -C/--context entirely in auto/lines mode. - // - // - No `context` given → force "lines": every file type (code AND prose) - // returns a `lines[]` window, so prose snippets render too. - // - `context` given → use "grep": the only mode where -C actually widens - // the window; it likewise returns `lines[]` for all file types. - if (typeof args.context === "number") { - const context = clamp(Math.floor(args.context), 0, MAX_CONTEXT); - flags.push("--snippet-mode", "grep", "-C", String(context)); - } else { - flags.push("--snippet-mode", "lines"); - } - - const requestedLimit = - typeof args.result_limit === "number" - ? clamp(Math.floor(args.result_limit), 1, MAX_RESULT_LIMIT) - : DEFAULT_RESULT_LIMIT; - flags.push("--result-limit", String(requestedLimit)); - - if (typeof args.snippet_length === "number") { - const snippet = clamp(Math.floor(args.snippet_length), MIN_SNIPPET_LENGTH, MAX_SNIPPET_LENGTH); - flags.push("-n", String(snippet)); - } - - const only = asString(args.only); - if (only && ONLY_FLAGS[only]) flags.push(ONLY_FLAGS[only]); - - return flags; -} - -/** Render cs JSON results into compact, readable per-file blocks. */ -function formatResults(results: CsResult[], absoluteWorkDir: string): string { - const blocks: string[] = []; - // Match the workdir only at a path boundary so a sibling dir that merely - // shares the prefix (e.g. workdir "/app" vs "/app-secrets") isn't treated - // as nested and rendered as a "../app-secrets/..." relative path. - const workdirPrefix = absoluteWorkDir.endsWith(sep) ? absoluteWorkDir : absoluteWorkDir + sep; - for (const r of results) { - // Present paths relative to the workdir so output is portable and compact. - const insideWorkdir = r.location === absoluteWorkDir || r.location.startsWith(workdirPrefix); - const rel = insideWorkdir ? relative(absoluteWorkDir, r.location) || r.filename : r.location; - const lang = r.language ? ` [${r.language}]` : ""; - const score = typeof r.score === "number" ? ` (score ${r.score.toFixed(2)})` : ""; - const header = `${rel}${lang}${score}`; - - let body: string[]; - if (r.lines && r.lines.length > 0) { - body = r.lines.map((l) => { - const marker = l.match_positions && l.match_positions.length > 0 ? ">" : " "; - return ` ${marker} ${l.line_number}: ${truncateLine(l.content)}`; - }); - } else if (r.content && r.content.trim() !== "") { - // Fallback for cs's "snippet"-mode shape (no per-line numbers): show - // the snippet text itself so the result isn't a bare header. - body = r.content.split("\n").map((line) => ` ${truncateLine(line)}`); - } else { - body = [" (match in file; no snippet available)"]; - } - - blocks.push([header, ...body].join("\n")); - } - - const count = results.length; - const heading = `Found matches in ${count} file${count === 1 ? "" : "s"} (ranked by relevance):`; - return [heading, "", blocks.join("\n\n")].join("\n"); -} - -function clamp(n: number, min: number, max: number): number { - return Math.min(max, Math.max(min, n)); -} - -/** Cap an individual snippet line so a minified/generated line can't bloat output. */ -function truncateLine(line: string): string { - if (line.length <= MAX_LINE_CHARS) return line; - return `${line.slice(0, MAX_LINE_CHARS)}… [line truncated, ${line.length.toLocaleString()} chars]`; -} - -/** - * Coerce a tool argument to a trimmed string, or undefined. Guards against a - * model hallucinating a non-string (e.g. an array `["ts","go"]`) for a - * string-typed param: returning undefined makes the flag a no-op instead of - * throwing `x.trim is not a function` and crashing the tool call. - */ -function asString(v: unknown): string | undefined { - if (typeof v !== "string") return undefined; - const t = v.trim(); - return t === "" ? undefined : t; -} - -function missingBinaryError(): string { - return [ - "Error: search_code requires the 'cs' (code spelunker) binary, which was not found.", - "Install it with: go install github.com/boyter/cs/[email protected]", - "or set the DISPATCH_CS_BIN environment variable to the path of a cs binary.", - "(In the official Docker images cs is bundled at /usr/local/bin/cs.)", - ].join("\n"); -} diff --git a/packages/core/src/tools/send-to-tab.ts b/packages/core/src/tools/send-to-tab.ts deleted file mode 100644 index eae6bfa..0000000 --- a/packages/core/src/tools/send-to-tab.ts +++ /dev/null @@ -1,198 +0,0 @@ -import { z } from "zod"; -import type { ToolDefinition } from "../types/index.js"; - -/** - * A tab reference surfaced to the `send_to_tab` / `read_tab` tools. The tools - * are intentionally decoupled from the DB `TabRow` shape — the AgentManager - * maps `resolveTabPrefix(...)` results down to this minimal projection so the - * tools (and their unit tests) never depend on the persistence layer. - */ -export interface ResolvedTabRef { - /** The tab's canonical full UUID. */ - id: string; - /** The tab's display title (for disambiguation hints). */ - title: string; - /** The tab's current short handle (shortest unique prefix). */ - handle: string; -} - -/** - * Outcome of resolving a short tab handle. Mirrors core's - * `ResolveTabPrefixResult` but over the minimal `ResolvedTabRef` projection. - */ -export type TabResolution = - | { status: "ok"; tab: ResolvedTabRef } - | { status: "none" } - | { status: "ambiguous"; matches: ResolvedTabRef[] }; - -export interface SendToTabCallbacks { - /** Resolve a (possibly short) handle to one open tab. */ - resolveShortId(prefix: string): TabResolution; - /** - * Deliver `message` to `tabId`. If the target is mid-turn the message is - * queued (same path as a user message); if idle/errored it wakes the tab - * and starts a new turn. Returns quickly — does NOT block on the turn. - */ - deliver( - tabId: string, - message: string, - ): - | Promise<{ status: "queued" | "started" | "suppressed" }> - | { status: "queued" | "started" | "suppressed" }; - /** Snapshot of currently-open tabs, for "available tabs" error hints. */ - listOpenHandles(): Array<{ handle: string; title: string }>; - /** The calling tab's own id + handle — used to block self-sends and to - * stamp provenance onto the delivered message. */ - self: { id: string; handle: string }; - /** - * Whether THIS calling tab also has the `read_tab` tool granted. The - * tab-messaging permissions are split, so a tab can hold `send_to_tab` - * without `read_tab`. When false, the tool must NOT tell the agent to use - * `read_tab` (it doesn't have it) — replies only arrive on their own. - */ - canReadTab: boolean; -} - -/** Render the "available tabs" hint shared by the none/ambiguous branches. */ -function renderOpenHandles(handles: Array<{ handle: string; title: string }>): string { - if (handles.length === 0) return "No other tabs are currently open."; - const lines = handles.map((h) => ` - ${h.handle}: ${h.title}`); - return ["Currently open tabs:", ...lines].join("\n"); -} - -export function createSendToTabTool(callbacks: SendToTabCallbacks): ToolDefinition { - // The `read_tab` follow-up hint is only truthful when this tab actually - // holds the `read_tab` tool (the permissions are split). When it doesn't, - // the only honest guidance is that a reply will wake it as a new message — never tell - // the agent to call a tool it wasn't granted. - const waitLine = callbacks.canReadTab - ? "money. If the target replies it will WAKE you with a new message in a later turn; you" - : "money. If the target replies it will WAKE you with a new message in a later turn."; - const readTabLine = callbacks.canReadTab - ? ["can also call 'read_tab' with the same ID in a FUTURE turn to check. If you have other"] - : []; - const keepGoingLine = callbacks.canReadTab - ? "work to do, keep going; if you are ONLY waiting for the reply, end your turn now." - : "If you have other work to do, keep going; if you are ONLY waiting for the reply, end your turn now."; - return { - name: "send_to_tab", - description: [ - "Send a message to another tab (agent) by its short ID — the handle shown in the tab bar.", - "", - "Behaviour mirrors a user sending a message:", - " - If the target tab is mid-turn (busy), your message is QUEUED and picked up next.", - " - If the target tab is idle, your message WAKES it and starts a new turn.", - "", - "This is fire-and-forget: it returns immediately and does NOT wait for a reply.", - "Do NOT sleep, poll, or run shell commands to wait for a reply — that wastes turns and", - waitLine, - ...readTabLine, - keepGoingLine, - "", - "Your tab ID is auto-added to the top of the message so the recipient knows who to reply", - "to. The recipient must use this same 'send_to_tab' tool (addressed to your ID) to answer;", - "a plain text response reaches only their own user, not you.", - "IDs are git-style prefixes: pass any length that uniquely identifies the target (min 4 chars).", - "If the ID is ambiguous you'll be asked to add a character.", - ].join("\n"), - parameters: z.object({ - tab_id: z - .string() - .describe( - "The short ID (handle) of the target tab, as shown in the tab bar. Any unique-length prefix of the tab's id works (min 4 chars).", - ), - message: z - .string() - .describe("The message to deliver to the target tab, exactly as a user would type it."), - }), - execute: async (args: Record<string, unknown>): Promise<string> => { - const rawId = (args.tab_id as string | undefined)?.trim() ?? ""; - const message = (args.message as string | undefined) ?? ""; - - if (!rawId) { - return `Error: tab_id is required.\n\n${renderOpenHandles(callbacks.listOpenHandles())}`; - } - if (!message.trim()) { - return "Error: message must not be empty."; - } - - const resolution = callbacks.resolveShortId(rawId); - - if (resolution.status === "none") { - return [ - `Error: no open tab matches the ID "${rawId}".`, - "", - renderOpenHandles(callbacks.listOpenHandles()), - ].join("\n"); - } - if (resolution.status === "ambiguous") { - const matches = resolution.matches.map((m) => ` - ${m.handle}: ${m.title}`).join("\n"); - return [ - `Error: the ID "${rawId}" is ambiguous — it matches multiple open tabs:`, - matches, - "", - "Add one or more characters to disambiguate.", - ].join("\n"); - } - - const target = resolution.tab; - - if (target.id === callbacks.self.id) { - return "Error: cannot send a message to your own tab."; - } - - // Stamp provenance so the recipient (and the watching user) can see - // which tab the message came from and how to reply. The header makes - // clear this is a PEER AGENT, not the recipient's own user, and the - // footer states the reply contract: a reply (only if warranted) must - // go back through `send_to_tab`, since a plain text answer reaches - // only the recipient's own user — not this sender. - const delivered = [ - `[message from tab ${callbacks.self.handle} — this is another agent, NOT your user]`, - "", - message, - "", - `[To reply to tab ${callbacks.self.handle}, use the send_to_tab tool with tab_id "${callbacks.self.handle}". ONLY reply if this message asks you to, or your user tells you to — it may just be context or instructions. A plain text response goes to your own user, not to this agent.]`, - ].join("\n"); - - try { - const result = await callbacks.deliver(target.id, delivered); - if (result.status === "suppressed") { - // The target hit its automatic agent-to-agent wake limit. The - // message was preserved (queued) but did NOT start a turn — a - // human must step in. Tell the sender plainly so it stops - // hammering the target and creating a runaway loop. - return [ - `Message HELD for tab ${target.handle} (${target.title}) — it was NOT delivered as a wake.`, - `That tab has reached its automatic agent-to-agent message limit, so it will not`, - `auto-respond again until a human sends it a message. Do not keep resending:`, - `your message is already queued and will be seen when a human resumes that tab.`, - ].join("\n"); - } - const verb = - result.status === "queued" - ? "queued (target is busy; it will be picked up next turn)" - : "delivered (target was idle; a new turn has started)"; - const tail = callbacks.canReadTab - ? [ - "Do NOT sleep, poll, or run commands to wait for a reply. If the target replies it", - `will WAKE you with a new message later; you can also call read_tab with "${target.handle}"`, - "in a FUTURE turn to check. Keep working if you have other tasks; if you are ONLY", - "waiting for this reply, end your turn now.", - ] - : [ - "Do NOT sleep, poll, or run commands to wait for a reply. If the target replies it", - "will WAKE you with a new message later. Keep working if you have other tasks; if", - "you are ONLY waiting for this reply, end your turn now.", - ]; - return [ - `Message ${verb}. Target tab: ${target.handle} (${target.title}).`, - "", - ...tail, - ].join("\n"); - } catch (err) { - return `Error delivering message: ${err instanceof Error ? err.message : String(err)}`; - } - }, - }; -} diff --git a/packages/core/src/tools/shell-analyze.ts b/packages/core/src/tools/shell-analyze.ts deleted file mode 100644 index b70108b..0000000 --- a/packages/core/src/tools/shell-analyze.ts +++ /dev/null @@ -1,166 +0,0 @@ -import { readFile } from "node:fs/promises"; -import { createRequire } from "node:module"; -import { dirname, isAbsolute, relative, resolve, sep } from "node:path"; -import * as BashArity from "./bash-arity.js"; - -// Commands that touch files — triggers external_directory check. -// Includes any command that takes file paths as arguments and could leak -// information about external directories. -// -// Known gaps (not currently checked): -// - Redirections: `echo x > /etc/file` — the redirect target is not inspected -// - `cd` state changes: we don't track cwd mutations across pipeline stages -// - Interpreter escapes: `python -c "open('/etc/passwd')"`, `node -e "..."` bypass this entirely -const FILE_COMMANDS = new Set([ - "rm", - "cp", - "mv", - "mkdir", - "touch", - "chmod", - "chown", - "cat", - "ls", - "find", - "grep", - "head", - "tail", - "less", - "more", - "wc", - "diff", - "file", - "stat", - "du", - "df", -]); - -// Lazy-initialized parser -let parserPromise: Promise<Parsers> | null = null; - -interface Parsers { - bash: import("web-tree-sitter").Parser; -} - -async function getParser(): Promise<Parsers> { - if (parserPromise) return parserPromise; - parserPromise = initParser(); - return parserPromise; -} - -async function initParser(): Promise<Parsers> { - const { Parser, Language } = await import("web-tree-sitter"); - - // Load the main WASM binary from node_modules - const require = createRequire(import.meta.url); - const webTreeSitterPath = require.resolve("web-tree-sitter/web-tree-sitter.wasm"); - const wasmBinary = await readFile(webTreeSitterPath); - await Parser.init({ wasmBinary }); - - const bashWasmPath = require.resolve("tree-sitter-bash/tree-sitter-bash.wasm"); - const bashLang = await Language.load(bashWasmPath); - - const bash = new Parser(); - bash.setLanguage(bashLang); - - return { bash }; -} - -// Analyze a shell command and return permission patterns -export async function analyzeCommand( - command: string, - workingDirectory: string, -): Promise<{ dirs: string[]; patterns: string[]; always: string[] }> { - try { - const parsers = await getParser(); - const tree = parsers.bash.parse(command); - if (!tree) return { dirs: [], patterns: [command], always: [] }; - - return collect(tree.rootNode, command, workingDirectory); - } catch { - // Parse failure — return basic patterns - return { dirs: [], patterns: [command], always: [] }; - } -} - -function collect( - node: import("web-tree-sitter").Node, - _source: string, - wd: string, -): { dirs: string[]; patterns: string[]; always: string[] } { - const dirs: string[] = []; - const patterns: string[] = []; - const always: string[] = []; - - // Walk all command nodes - const commands = node.descendantsOfType("command"); - - for (const cmd of commands) { - const parts = extractParts(cmd); - const name = parts[0]?.toLowerCase(); - if (!name) continue; - - // Get the command source text - const cmdText = cmd.text; - patterns.push(cmdText); - - // Normalize to always pattern - always.push(`${BashArity.prefix(parts).join(" ")} *`); - - // Check if this is a file-touching command - if (FILE_COMMANDS.has(name)) { - // Extract path arguments (skip flags starting with -) - const pathArgs = parts.slice(1).filter((a) => !a.startsWith("-")); - for (const arg of pathArgs) { - const resolved = resolvePath(arg, wd); - if (resolved && !isInsideWorkspace(resolved, wd)) { - const parent = dirname(resolved); - dirs.push(parent); - } - } - } - } - - return { - dirs: [...new Set(dirs)], - patterns: [...new Set(patterns)], - always: [...new Set(always)], - }; -} - -// Helper to extract command parts from a command AST node -function extractParts(cmd: import("web-tree-sitter").Node): string[] { - const parts: string[] = []; - for (const child of cmd.children) { - if ( - child.type === "command_name" || - child.type === "word" || - child.type === "string" || - child.type === "raw_string" - ) { - const text = child.text.replace(/^['"]|['"]$/g, ""); - if (text) parts.push(text); - } - } - return parts; -} - -function resolvePath(arg: string, wd: string): string | null { - try { - if (isAbsolute(arg)) return arg; - return resolve(wd, arg); - } catch { - return null; - } -} - -function isInsideWorkspace(filePath: string, wd: string): boolean { - const normalizedWd = resolve(wd); - const rel = relative(normalizedWd, filePath); - // rel === "" means filePath IS the workspace root — that is inside. - // If relative path starts with "../" or is ".." exactly, or is an absolute path - // (on Windows when drives differ), the file is outside the workspace. - const isOutside = - rel.startsWith(`..${sep}`) || rel.startsWith("../") || rel === ".." || isAbsolute(rel); - return !isOutside; -} diff --git a/packages/core/src/tools/summon.ts b/packages/core/src/tools/summon.ts deleted file mode 100644 index 2a076e6..0000000 --- a/packages/core/src/tools/summon.ts +++ /dev/null @@ -1,447 +0,0 @@ -import { z } from "zod"; -import type { AgentDefinition, ToolDefinition } from "../types/index.js"; - -export interface SummonCallbacks { - spawn(options: { - task: string; - tools: string[]; - workingDirectory?: string; - /** - * Optional slug of an `AgentDefinition` (loaded from - * `~/.config/dispatch/agents/` or `<projectDir>/.dispatch/agents/`) - * to use as the basis for the spawned child. When provided, - * the definition's tools, models, and cwd override the - * `tools` and `workingDirectory` parameters passed alongside. - */ - agentSlug?: string; - /** - * When true, spawn the agent as an independent top-level "user - * agent" tab (no parent, persistent, fire-and-forget) instead of - * a subagent child tab. Only honoured when the spawning agent has - * the `perm_user_agent` permission. - */ - topLevel?: boolean; - }): Promise<string>; - getResult( - agentId: string, - ): Promise<{ status: "done"; result: string } | { status: "error"; error: string }>; -} - -/** - * Summary of an agent definition surfaced to the calling LLM in the - * summon tool's description. The shape is intentionally minimal — full - * TOML inspection is done by reading the definition file directly, - * which all agents are allowed to do by default. - */ -export interface AvailableAgent { - slug: string; - name: string; - description: string; - /** Filesystem path of the TOML the agent can read for full details. */ - path: string; -} - -/** - * Render a labelled list of agents. Returns an empty array when there - * are no agents so callers can omit the group entirely. - */ -function renderAgentGroup(label: string, agents: AvailableAgent[]): string[] { - if (agents.length === 0) return []; - const lines: string[] = [label]; - for (const a of agents) { - const desc = a.description ? ` — ${a.description}` : ""; - lines.push(` - ${a.slug}: ${a.name}${desc}`); - } - return lines; -} - -/** - * Build the prose paragraph that lists available agent definitions plus - * the disk locations where they live, injected into the summon tool's - * description. - * - * `subagentEnabled` and `userAgentEnabled` independently control which - * groups are shown — they mirror the `perm_summon` and `perm_user_agent` - * permissions respectively: - * - subagents only → generic "Available agents" heading; - * - user agents only → a single user-agent group (top_level is implied); - * - both → two labelled groups so the LLM understands which slugs - * require `top_level=true`. - * - * Returns a compact "no agents defined" notice when nothing is visible. - */ -function buildAgentsCatalog( - subagents: AvailableAgent[], - userAgents: AvailableAgent[], - agentDirs: string[], - userAgentEnabled: boolean, - subagentEnabled: boolean, -): string { - const lines: string[] = []; - lines.push(""); - lines.push("Agent definitions live on disk and can be inspected with read_file/list_files:"); - for (const d of agentDirs) { - lines.push(` - ${d}`); - } - - const visibleSubagents = subagentEnabled ? subagents : []; - const visibleUserAgents = userAgentEnabled ? userAgents : []; - if (visibleSubagents.length === 0 && visibleUserAgents.length === 0) { - lines.push(""); - lines.push("No agent definitions are currently defined."); - return lines.join("\n"); - } - - lines.push(""); - lines.push("To summon a specific agent, pass its slug as the 'agent' parameter."); - lines.push("When 'agent' is set, the child inherits that definition's tools, models,"); - lines.push("and working directory; the 'tools' parameter is ignored."); - lines.push(""); - - // User-agent-only mode: list just the user agents. top_level is implied - // (it is the only thing this grant can spawn), so the heading omits it. - if (!subagentEnabled && userAgentEnabled) { - lines.push( - ...renderAgentGroup( - "User agents (spawned as independent top-level tabs):", - visibleUserAgents, - ), - ); - return lines.join("\n"); - } - - // Subagent-only mode: single generic heading. - if (!userAgentEnabled) { - lines.push(...renderAgentGroup("Available agents:", visibleSubagents)); - return lines.join("\n"); - } - - // Both enabled: two labelled groups. - const subagentLines = renderAgentGroup("Subagents (spawned as child tabs):", visibleSubagents); - const userAgentLines = renderAgentGroup( - "User agents (spawned as independent top-level tabs, requires top_level=true):", - visibleUserAgents, - ); - if (subagentLines.length > 0) { - lines.push(...subagentLines); - } - if (userAgentLines.length > 0) { - if (subagentLines.length > 0) lines.push(""); - lines.push(...userAgentLines); - } - return lines.join("\n"); -} - -/** - * Factory for the `summon` tool. Accepts a snapshot of agent definitions - * available at the time the tool is registered so the LLM's view of - * which agents exist matches what `spawnChildAgent` can actually load. - * - * `agentDirs` is the list of filesystem paths the catalog references in - * its description; this is information-only — the runtime resolves - * slugs through `loadAgent` independently. - * - * `userAgentEnabled` mirrors the `perm_user_agent` permission and - * `subagentEnabled` mirrors the `perm_summon` permission. They are - * independent: the tool is registered whenever at least one is granted. - * - subagentEnabled only → spawn ordinary subagents (no `top_level`); - * - userAgentEnabled only → spawn ONLY top-level user agents - * (`top_level` is forced on, the `background` knob is dropped, and - * the catalog lists user agents only); - * - both → full behavior (subagents plus `top_level` user agents). - */ -export function createSummonTool( - _defaultWorkingDirectory: string, - callbacks: SummonCallbacks, - availableSubagents: AvailableAgent[] = [], - availableUserAgents: AvailableAgent[] = [], - agentDirs: string[] = [], - userAgentEnabled = false, - subagentEnabled = true, -): ToolDefinition { - // When only the user-agent permission is granted the tool spawns user - // agents exclusively: `top_level` is implied (and forced), subagent - // mechanics (background, retrieve, parallel work) are irrelevant. - const userAgentOnly = userAgentEnabled && !subagentEnabled; - - const catalog = buildAgentsCatalog( - availableSubagents, - availableUserAgents, - agentDirs, - userAgentEnabled, - subagentEnabled, - ); - const subagentSlugs = availableSubagents.map((a) => a.slug); - const userAgentSlugs = availableUserAgents.map((a) => a.slug); - const allSlugs = userAgentOnly - ? userAgentSlugs - : userAgentEnabled - ? [...subagentSlugs, ...userAgentSlugs] - : subagentSlugs; - - const toolNamesList = [ - "The 'tools' parameter controls what the child can do. Available tool names:", - " - read_file: Read file contents", - " - read_file_slice: Read a character-range slice of a single line", - " - list_files: List files and directories", - " - write_file: Write/edit files", - " - run_shell: Execute shell commands", - " - search_code: Search the codebase with the cs ranked code-search engine", - " - todo: Track work items", - " - summon: Spawn its own child agents (enables nesting)", - " - retrieve: Collect results from its children (required if summon is given)", - " - web_search: Search the web", - " - youtube_transcribe: Fetch YouTube video transcripts", - " - send_to_tab: Send a message to another tab/agent by its ID", - " - read_tab: Read another tab/agent's latest response by its ID", - ]; - - const description = userAgentOnly - ? [ - "Spawn an independent top-level user agent to work on a task.", - "", - "User agents are first-class top-level tabs with no parent. They are", - "fire-and-forget: you get an agent_id back but cannot retrieve their result.", - "The user agent runs in its own tab visible to the user.", - "", - ...toolNamesList, - "", - "The 'agent' parameter is required — every spawned agent must use a definition.", - "Tools default to the agent definition's tools, intersected with your own tools (you can't grant capabilities you don't have).", - catalog, - ].join("\n") - : [ - "Spawn a new child agent to work on a task independently.", - "", - "By default, blocks until the child agent finishes and returns the result directly.", - "Set background=true to return immediately with an agent_id instead — use retrieve to collect the result later.", - "", - "The child agent runs in its own tab visible to the user. Use the 'retrieve' tool with the returned agent_id to get the result when needed.", - "", - "Pattern for parallel work:", - " 1. Call summon multiple times with background=true to start several agents", - " 2. Do your own work or wait", - " 3. Call retrieve for each agent_id to collect results", - ...(userAgentEnabled - ? [ - "", - "Set top_level=true to spawn an independent user agent — a first-class", - "top-level tab with no parent. User agents are fire-and-forget: you get", - "an agent_id back but cannot retrieve their result. top_level requires an", - "'agent' definition listed under 'User agents' below.", - ] - : []), - "", - ...toolNamesList, - "", - "The 'agent' parameter is required — every spawned agent must use a definition.", - "Tools default to the agent definition's tools, intersected with your own tools (you can't grant capabilities you don't have).", - catalog, - ].join("\n"); - - const parametersShape = { - task: z - .string() - .describe( - "Detailed instructions for the child agent. Be specific about what it should do and what it should return.", - ), - agent: z - .string() - .describe( - [ - "Slug of an agent definition to use as the basis for the child agent.", - "Required. The child inherits the definition's tools, models, and", - "working directory; the 'tools' parameter only narrows them further.", - "Inspect the agent directories listed above to discover which slugs", - "are available and what each one does.", - allSlugs.length > 0 ? `Available slugs: ${allSlugs.join(", ")}.` : "", - ] - .filter(Boolean) - .join(" "), - ), - // `top_level` is only an explicit choice when BOTH subagents and user - // agents are available. In user-agent-only mode it is implied (forced - // on), so the knob is omitted entirely. - ...(userAgentEnabled && !userAgentOnly - ? { - top_level: z - .boolean() - .optional() - .describe( - [ - "If true, spawn the agent as an independent top-level user agent tab", - "instead of a child subagent. User agents have no parent, persist on", - "their own, and are fire-and-forget (cannot be retrieved). Requires an", - "'agent' definition listed under 'User agents'. The 'background' option", - "is ignored when top_level is true.", - ].join(" "), - ), - } - : {}), - tools: z - .array( - z.enum([ - "read_file", - "read_file_slice", - "list_files", - "write_file", - "run_shell", - "search_code", - "key_usage", - "todo", - "summon", - "retrieve", - "web_search", - "youtube_transcribe", - "send_to_tab", - "read_tab", - ]), - ) - .optional() - .describe( - "Tool names to give the child. Defaults to the agent definition's tools. Intersected with the spawning agent's tools (you can't grant capabilities you don't have).", - ), - working_directory: z - .string() - .optional() - .describe( - "Absolute path for the child to work in. Defaults to the agent definition's cwd (or the spawning agent's directory).", - ), - // `background` is meaningless for fire-and-forget user agents, so the - // knob is omitted in user-agent-only mode. - ...(userAgentOnly - ? {} - : { - background: z - .boolean() - .optional() - .describe( - "If true, returns immediately with an agent_id for later retrieval. If false (default), blocks until the child agent finishes and returns the result directly. Ignored when top_level is true.", - ), - }), - }; - - return { - name: "summon", - description, - parameters: z.object(parametersShape), - execute: async (args: Record<string, unknown>): Promise<string> => { - const task = args.task as string; - const agentSlug = args.agent as string | undefined; - const tools = args.tools as string[] | undefined; - const workingDirectory = args.working_directory as string | undefined; - const background = (args.background as boolean | undefined) ?? false; - // User-agent-only mode always spawns top-level user agents. When both - // capabilities are present the caller chooses via `top_level`. When - // only subagents are available, top-level spawning is unavailable. - const topLevel = userAgentOnly - ? true - : userAgentEnabled - ? ((args.top_level as boolean | undefined) ?? false) - : false; - - try { - const agentId = await callbacks.spawn({ - task, - tools: tools ?? [], - ...(workingDirectory ? { workingDirectory } : {}), - ...(agentSlug ? { agentSlug } : {}), - ...(topLevel ? { topLevel: true } : {}), - }); - - if (topLevel) { - // User agents are always fire-and-forget — never block on a - // result and make it explicit that retrieve won't work. - return [ - `User agent spawned successfully.`, - `agent_id: ${agentId}`, - ``, - `The user agent is now working independently in its own top-level tab.`, - `It is fire-and-forget — you cannot retrieve its result.`, - ].join("\n"); - } - - if (!background) { - // Block until the child agent completes. Always prefix the - // result with `agent_id: <uuid>` so the frontend's - // ToolCallDisplay regex (`agent_id:\s*([a-f0-9-]+)`) can - // surface the "Open Tab" button for foreground summons too — - // not just background ones. The child's tab still exists and - // holds the full conversation, so the user should always be - // able to open it. - const result = await callbacks.getResult(agentId); - if (result.status === "done") { - return `agent_id: ${agentId}\n\n${result.result}`; - } - return `agent_id: ${agentId}\n\nError from child agent: ${result.error}`; - } - - return [ - `Agent spawned successfully.`, - `agent_id: ${agentId}`, - ``, - `The child agent is now working on the task in its own tab.`, - `Use the retrieve tool with this agent_id to get the result when ready.`, - ].join("\n"); - } catch (err) { - return `Error spawning agent: ${err instanceof Error ? err.message : String(err)}`; - } - }, - }; -} - -/** - * Build the `AvailableAgent[]` projection of an `AgentDefinition` list, - * deriving each entry's readable `path` from its scope+slug. - */ -function toAvailableAgents( - defs: AgentDefinition[], - globalDir: string, - projectDir: string | null, -): AvailableAgent[] { - return defs.map((d) => { - const baseDir = - d.scope === "global" - ? globalDir - : projectDir - ? `${projectDir.replace(/\/$/, "")}/.dispatch/agents` - : globalDir; - return { - slug: d.slug, - name: d.name, - description: d.description, - path: `${baseDir}/${d.slug}.toml`, - }; - }); -} - -/** - * Subagent definitions (`is_subagent === true`) — spawned as child tabs. - */ -export function toAvailableSubagents( - defs: AgentDefinition[], - globalDir: string, - projectDir: string | null, -): AvailableAgent[] { - return toAvailableAgents( - defs.filter((d) => d.is_subagent), - globalDir, - projectDir, - ); -} - -/** - * User-agent definitions (`is_subagent !== true`) — spawnable as - * independent top-level tabs when `perm_user_agent` is granted. - */ -export function toAvailableUserAgents( - defs: AgentDefinition[], - globalDir: string, - projectDir: string | null, -): AvailableAgent[] { - return toAvailableAgents( - defs.filter((d) => d.is_subagent !== true), - globalDir, - projectDir, - ); -} diff --git a/packages/core/src/tools/task-list.ts b/packages/core/src/tools/task-list.ts deleted file mode 100644 index 98dcf01..0000000 --- a/packages/core/src/tools/task-list.ts +++ /dev/null @@ -1,149 +0,0 @@ -import { z } from "zod"; -import type { TaskItem, TaskStatus, ToolDefinition } from "../types/index.js"; - -/** - * Valid task statuses. Matches opencode's todo lifecycle: - * - pending not started - * - in_progress actively working (exactly ONE at a time) - * - completed finished successfully - * - cancelled no longer needed - */ -const VALID_STATUSES: ReadonlySet<TaskStatus> = new Set<TaskStatus>([ - "pending", - "in_progress", - "completed", - "cancelled", -]); - -function normalizeStatus(value: unknown): TaskStatus { - return typeof value === "string" && VALID_STATUSES.has(value as TaskStatus) - ? (value as TaskStatus) - : "pending"; -} - -/** - * Declarative, whole-list task store (ported from opencode's `todowrite`). - * - * The model never sees ids and never issues per-item mutations. Instead it - * sends the ENTIRE desired list on every call and {@link setTasks} rebuilds the - * stored list, assigning fresh positional ids. This is idempotent and - * eliminates the id-bookkeeping / "task not found" / delta-reasoning failure - * modes of the old imperative CRUD interface. - */ -export class TaskList { - private tasks: TaskItem[] = []; - private listeners: Array<(tasks: TaskItem[]) => void> = []; - - private notify(): void { - const snapshot = this.getTasks(); - for (const listener of this.listeners) { - listener(snapshot); - } - } - - getTasks(): TaskItem[] { - return this.tasks.map((t) => ({ ...t })); - } - - /** - * Replace the entire list. Each item is assigned a fresh positional id - * (`task-1`, `task-2`, …). Invalid/missing statuses fall back to - * `pending`; an empty array clears the list. Always notifies listeners. - */ - setTasks(items: Array<{ content: string; status?: unknown }>): TaskItem[] { - this.tasks = items.map((item, index) => ({ - id: `task-${index + 1}`, - content: item.content, - status: normalizeStatus(item.status), - })); - this.notify(); - return this.getTasks(); - } - - onChange(callback: (tasks: TaskItem[]) => void): () => void { - this.listeners.push(callback); - return () => { - this.listeners = this.listeners.filter((l) => l !== callback); - }; - } -} - -/** - * Rich tool description adapted from opencode's `todowrite.txt`. Teaches the - * declarative whole-list cadence and the status lifecycle. - */ -export const TODO_DESCRIPTION = `Create and maintain a structured todo list for the current session to track progress and surface your plan to the user. - -This is a DECLARATIVE, whole-list tool. There are no ids and no per-item actions: every call sends the ENTIRE list in the \`todos\` parameter and REPLACES the previous list. To change one item, resend the whole list with that item changed. To clear the list, send an empty array. - -## When to use -- The task requires 3+ distinct steps and benefits from planning -- The user provides multiple tasks (numbered or comma-separated) or asks for a todo list -- New instructions arrive — capture them as todos -- You start a task — mark it in_progress (only one at a time) before working -- You finish a task — mark it completed and add any follow-ups discovered - -## When NOT to use -- A single, straightforward task (or fewer than 3 trivial steps) -- Purely informational or conversational requests -- When tracking adds no organizational value - -## States -- pending — not started -- in_progress — actively working (exactly ONE at a time) -- completed — finished successfully -- cancelled — no longer needed - -## Rules -- Send the full desired list every time; the tool replaces the stored list -- Update status in real time; do not batch completions -- Mark completed only after the work is actually done (including any required verification), never on intent -- Keep exactly one in_progress while work remains -- If blocked or partial, keep it in_progress and add a follow-up todo describing the blocker -- Items should be specific and actionable; break large work into smaller steps`; - -export function createTaskListTool(taskList: TaskList): ToolDefinition { - return { - name: "todo", - description: TODO_DESCRIPTION, - parameters: z.object({ - todos: z - .array( - z.object({ - content: z.string().describe("Brief, actionable description of the task"), - status: z - .enum(["pending", "in_progress", "completed", "cancelled"]) - .describe("Current status of the task"), - }), - ) - .describe("The complete, updated todo list. Replaces the previous list entirely."), - }), - execute: async (args: Record<string, unknown>): Promise<string> => { - const rawTodos = args.todos; - if (!Array.isArray(rawTodos)) { - return "Error: 'todos' must be an array of { content, status } items (send the whole list)."; - } - - const items: Array<{ content: string; status?: unknown }> = []; - for (const entry of rawTodos) { - if (!entry || typeof entry !== "object") { - return "Error: each todo must be an object with a 'content' string and a 'status'."; - } - const content = (entry as Record<string, unknown>).content; - if (typeof content !== "string" || content.trim() === "") { - return "Error: each todo requires a non-empty 'content' string."; - } - items.push({ - content, - status: (entry as Record<string, unknown>).status, - }); - } - - const stored = taskList.setTasks(items); - // Echo the canonical stored list back WITHOUT ids — the model must - // never start tracking ids; it always resends the whole list. - const echo = stored.map((t) => ({ content: t.content, status: t.status })); - return JSON.stringify(echo); - }, - }; -} diff --git a/packages/core/src/tools/truncate.ts b/packages/core/src/tools/truncate.ts deleted file mode 100644 index 8c62174..0000000 --- a/packages/core/src/tools/truncate.ts +++ /dev/null @@ -1,142 +0,0 @@ -import { mkdirSync, rmSync, writeFileSync } from "node:fs"; -import { dirname, join } from "node:path"; - -// ─── Constants ─────────────────────────────────────────────────── -// -// A tool result that exceeds *either* MAX_CHARS or MAX_LINES is treated -// as oversized: the full content is written to a spill file under -// /tmp/dispatch/tool-results/<tabId>/<callId>.txt and the model receives -// HEAD_CHARS from the start + TAIL_CHARS from the end with a notice -// in between. These are deliberate hardcoded defaults — see the design -// discussion in notes/plan.md for the rationale. - -export const MAX_CHARS = 10_000; -export const MAX_LINES = 500; -export const HEAD_CHARS = 1500; -export const TAIL_CHARS = 1500; - -/** Base directory for all tool-result spill files. Per-tab subdirectories live inside. */ -export const SPILL_ROOT = "/tmp/dispatch/tool-results"; - -// ─── Public API ────────────────────────────────────────────────── - -export interface TruncationContext { - /** Tab the tool call belongs to. Used to scope the spill directory. */ - tabId: string; - /** Tool call ID, used as the spill file basename. */ - callId: string; - /** Tool name, included in the truncation notice for human-readable hints. */ - toolName: string; -} - -export interface TruncationResult { - /** Final string sent to the model. Either the original (when under threshold) or the head+notice+tail excerpt. */ - displayResult: string; - /** When truncation happened, the absolute path the full output was spilled to. Undefined otherwise. */ - spillPath?: string; -} - -/** - * Apply universal truncation to a tool result string. - * - * If the result is under both the character and line caps, returns it - * unchanged with no side effects. - * - * If the result exceeds either cap: - * 1. Writes the full content to `<SPILL_ROOT>/<tabId>/<callId>.txt`. - * 2. Builds a display string consisting of HEAD_CHARS from the start, - * a multi-line truncation notice that includes the spill path, and - * TAIL_CHARS from the end. - * - * The notice instructs the model to use `read_file` (with offset/limit) - * or `read_file_slice` to inspect the full content. Every tool result - * flows through this function via `Agent.executeToolWithStreaming`, so - * any new tool that returns a string automatically gets the protection. - */ -export function applyTruncation(result: string, ctx: TruncationContext): TruncationResult { - const totalChars = result.length; - const totalLines = countLines(result); - - if (totalChars <= MAX_CHARS && totalLines <= MAX_LINES) { - return { displayResult: result }; - } - - const spillPath = join(SPILL_ROOT, ctx.tabId, `${ctx.callId}.txt`); - try { - mkdirSync(dirname(spillPath), { recursive: true, mode: 0o700 }); - writeFileSync(spillPath, result, { encoding: "utf-8", mode: 0o600 }); - } catch (err) { - // If we can't spill (disk full, perms, etc.) fall back to hard-truncating - // the head + tail without a spill path reference. The model loses the - // ability to inspect the middle but won't be blocked outright. - const message = err instanceof Error ? err.message : String(err); - return { - displayResult: buildExcerpt(result, totalChars, totalLines, ctx, { - spillPath: null, - spillError: message, - }), - }; - } - - return { - displayResult: buildExcerpt(result, totalChars, totalLines, ctx, { - spillPath, - spillError: null, - }), - spillPath, - }; -} - -/** Delete the entire spill directory for a tab. Best-effort, errors swallowed. */ -export function clearSpillForTab(tabId: string): void { - const dir = join(SPILL_ROOT, tabId); - try { - rmSync(dir, { recursive: true, force: true }); - } catch { - // Ignore — tab close should not fail on cleanup errors - } -} - -// ─── Internal helpers ──────────────────────────────────────────── - -function countLines(s: string): number { - if (s.length === 0) return 0; - let count = 1; - for (let i = 0; i < s.length; i++) { - if (s.charCodeAt(i) === 10 /* \n */) count++; - } - return count; -} - -function buildExcerpt( - result: string, - totalChars: number, - totalLines: number, - ctx: TruncationContext, - spill: { spillPath: string | null; spillError: string | null }, -): string { - // Guard against pathological case where HEAD+TAIL overlap. If the result - // is between MAX_CHARS and HEAD+TAIL (rare), slice cleanly so we don't - // emit overlapping content. - const head = result.slice(0, HEAD_CHARS); - const tailStart = Math.max(HEAD_CHARS, totalChars - TAIL_CHARS); - const tail = result.slice(tailStart); - - const omittedChars = Math.max(0, totalChars - head.length - tail.length); - - const notice: string[] = [ - "", - `[output truncated by dispatch — tool=${ctx.toolName}, total ${totalChars.toLocaleString()} chars / ${totalLines.toLocaleString()} lines; showing first ${head.length.toLocaleString()} and last ${tail.length.toLocaleString()}, ${omittedChars.toLocaleString()} omitted]`, - ]; - if (spill.spillPath) { - notice.push( - `[full output saved to: ${spill.spillPath}]`, - `[use read_file with offset/limit (lines), or read_file_slice (chars within a single line), to inspect specific sections]`, - ); - } else if (spill.spillError) { - notice.push(`[failed to spill full output to disk: ${spill.spillError}]`); - } - notice.push(""); - - return `${head}${notice.join("\n")}${tail}`; -} diff --git a/packages/core/src/tools/web-search.ts b/packages/core/src/tools/web-search.ts deleted file mode 100644 index 7f061a5..0000000 --- a/packages/core/src/tools/web-search.ts +++ /dev/null @@ -1,106 +0,0 @@ -import { z } from "zod"; -import type { ToolDefinition } from "../types/index.js"; - -const FIRECRAWL_URL = "http://100.102.55.49:31329/v1/search"; -const MAX_OUTPUT_CHARS = 60000; -const TIMEOUT_MS = 30000; - -export function createWebSearchTool(): ToolDefinition { - return { - name: "web_search", - description: - "Search the web via a self-hosted Firecrawl instance. Returns a list of results with titles, URLs, and descriptions. Optionally scrapes the full markdown content of each result page.", - parameters: z.object({ - query: z.string().describe("The search query"), - limit: z - .number() - .optional() - .default(7) - .describe("Maximum number of results to return (default 7)"), - scrape: z - .boolean() - .optional() - .default(false) - .describe("Whether to also scrape the full markdown content of each result page"), - lang: z.string().optional().describe('Language code to filter results (e.g. "en")'), - country: z.string().optional().describe('Country code to filter results (e.g. "us")'), - }), - execute: async (args: Record<string, unknown>): Promise<string> => { - const query = args.query as string; - const limit = (args.limit as number | undefined) ?? 7; - const scrape = (args.scrape as boolean | undefined) ?? false; - const lang = args.lang as string | undefined; - const country = args.country as string | undefined; - - const body: Record<string, unknown> = { query, limit }; - if (lang !== undefined) body.lang = lang; - if (country !== undefined) body.country = country; - if (scrape) { - body.scrapeOptions = { formats: ["markdown"], onlyMainContent: true }; - } - - const controller = new AbortController(); - const timeout = setTimeout(() => controller.abort(), TIMEOUT_MS); - - let response: Response; - try { - response = await fetch(FIRECRAWL_URL, { - method: "POST", - headers: { - "Content-Type": "application/json", - Accept: "application/json", - }, - body: JSON.stringify(body), - signal: controller.signal, - }); - } catch (err) { - if (err instanceof Error && err.name === "AbortError") { - return "Error: Request to Firecrawl timed out after 30 seconds."; - } - if (err instanceof Error && (err as NodeJS.ErrnoException).code === "ECONNREFUSED") { - return `Error: Could not connect to Firecrawl at http://100.102.55.49:31329. Is it running?`; - } - return `Error: ${err instanceof Error ? err.message : String(err)}`; - } finally { - clearTimeout(timeout); - } - - if (!response.ok) { - const text = await response.text().catch(() => ""); - return `Error: Firecrawl returned HTTP ${response.status} ${response.statusText}${text ? `: ${text}` : ""}`; - } - - let json: { - data?: Array<{ title?: string; url?: string; description?: string; markdown?: string }>; - }; - try { - json = await response.json(); - } catch { - return "Error: Failed to parse Firecrawl response as JSON"; - } - - const results = json.data ?? []; - if (results.length === 0) { - return "No results found."; - } - - const parts: string[] = []; - for (const result of results) { - const title = result.title ?? "(no title)"; - const url = result.url ?? ""; - const description = result.description ?? ""; - let section = `### ${title}\n${url}\n\n${description}`; - if (result.markdown) { - section += `\n\n${result.markdown}`; - } - parts.push(section); - } - - let output = parts.join("\n\n---\n\n"); - if (output.length > MAX_OUTPUT_CHARS) { - output = `${output.slice(0, MAX_OUTPUT_CHARS)}\n\n[Output truncated]`; - } - return output; - }, - }; -} diff --git a/packages/core/src/tools/write-file.ts b/packages/core/src/tools/write-file.ts deleted file mode 100644 index 8a73352..0000000 --- a/packages/core/src/tools/write-file.ts +++ /dev/null @@ -1,66 +0,0 @@ -import { mkdir, writeFile } from "node:fs/promises"; -import { dirname } from "node:path"; -import { z } from "zod"; -import type { ToolDefinition } from "../types/index.js"; -import { canonicalize } from "./path-utils.js"; - -/** - * Optional hook invoked AFTER a successful write, with the canonicalized - * absolute path of the file just written. Its returned string (when non-empty) - * is appended to the tool result. This is how LSP diagnostics are surfaced - * back to the model on write without coupling `@dispatch/core`'s tools to the - * API layer or the LSP manager — the host wires an implementation that touches - * the file through the LSP and formats any diagnostics. Errors thrown here are - * swallowed so a flaky LSP never fails the write itself. - */ -export type AfterWriteHook = (absolutePath: string) => Promise<string>; - -export function createWriteFileTool( - workingDirectory: string, - onAfterWrite?: AfterWriteHook, -): ToolDefinition { - return { - name: "write_file", - description: "Write content to a file relative to the working directory.", - parameters: z.object({ - path: z.string().describe("Path to the file, relative to the working directory"), - content: z.string().describe("Content to write to the file"), - }), - execute: async (args: Record<string, unknown>): Promise<string> => { - const filePath = args.path as string; - const content = args.content as string; - // Canonicalize so a workdir-relative path that resolves through - // symlinks to outside the workdir is detected and blocked. The - // canonicalize walks up to the nearest existing ancestor when the - // leaf doesn't exist (typical for write_file), so a path like - // `workdir/escape-link/new-file.txt` where `escape-link` symlinks - // to /etc still resolves through the symlink and is caught here. - const absolutePath = await canonicalize(workingDirectory, filePath); - const absoluteWorkDir = await canonicalize(workingDirectory); - - if (absolutePath !== absoluteWorkDir && !absolutePath.startsWith(`${absoluteWorkDir}/`)) { - return `Error: Path "${filePath}" is outside the working directory.`; - } - - try { - await mkdir(dirname(absolutePath), { recursive: true }); - await writeFile(absolutePath, content, "utf8"); - } catch (err) { - return `Error writing file: ${err instanceof Error ? err.message : String(err)}`; - } - - let result = `Successfully wrote to "${filePath}".`; - // Post-write hook (e.g. LSP diagnostics). Best-effort: never let a - // hook failure turn a successful write into an error. - if (onAfterWrite) { - try { - const extra = await onAfterWrite(absolutePath); - if (extra) result += `\n\n${extra}`; - } catch { - /* ignore — diagnostics are advisory */ - } - } - return result; - }, - }; -} diff --git a/packages/core/src/tools/youtube-transcribe.ts b/packages/core/src/tools/youtube-transcribe.ts deleted file mode 100644 index 3a26d6f..0000000 --- a/packages/core/src/tools/youtube-transcribe.ts +++ /dev/null @@ -1,219 +0,0 @@ -import { randomUUID } from "node:crypto"; -import { z } from "zod"; -import type { ToolDefinition, ToolExecuteContext } from "../types/index.js"; - -const TRANSCRIBER_BASE = "http://100.102.55.49:41090"; -const MAX_OUTPUT_CHARS = 60000; -const REQUEST_TIMEOUT_MS = 30000; -const MAX_WAIT_MS = 10 * 60 * 1000; // give up after 10 minutes of polling - -interface TranscriptResponse { - status: string; - video_id?: string; - full_text?: string; - segments?: Array<{ text: string; start: number; duration: number }>; - position?: number; - estimated_seconds?: number; - error?: string; - error_type?: string; -} - -async function fetchTranscript(url: string): Promise<TranscriptResponse> { - const controller = new AbortController(); - const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS); - try { - const apiUrl = `${TRANSCRIBER_BASE}/api/transcript?url=${encodeURIComponent(url)}`; - const response = await fetch(apiUrl, { signal: controller.signal }); - if (!response.ok) { - throw new Error(`Transcriber returned HTTP ${response.status} ${response.statusText}`); - } - return (await response.json()) as TranscriptResponse; - } finally { - clearTimeout(timeout); - } -} - -function formatTime(seconds: number): string { - const mins = Math.floor(seconds / 60); - const secs = Math.floor(seconds % 60); - return `${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`; -} - -function formatTranscript(data: TranscriptResponse): string { - const segments = data.segments ?? []; - const segmentsText = segments.map((seg) => `[${formatTime(seg.start)}] ${seg.text}`).join("\n"); - - const output = [ - `Video ID: ${data.video_id}`, - "", - "## Transcript", - "", - data.full_text ?? "", - "", - "## Timestamped Segments", - "", - segmentsText, - ].join("\n"); - - return output.length > MAX_OUTPUT_CHARS - ? `${output.slice(0, MAX_OUTPUT_CHARS)}\n\n[Transcript truncated]` - : output; -} - -function sleep(ms: number): Promise<void> { - return new Promise((resolve) => setTimeout(resolve, ms)); -} - -/** Polls until the transcript is ready, fails, or times out. */ -async function pollUntilReady(url: string): Promise<string> { - const startTime = Date.now(); - - while (Date.now() - startTime < MAX_WAIT_MS) { - const data = await fetchTranscript(url); - - if (data.status === "completed") { - return formatTranscript(data); - } - - if (data.status === "failed") { - return `Error: Transcription failed for video ${data.video_id ?? "unknown"}: [${data.error_type ?? "unknown"}] ${data.error ?? "no details"}`; - } - - if (data.status === "queued" || data.status === "processing") { - const estimate = data.estimated_seconds ?? 30; - const waitMs = Math.max((estimate - 2) * 1000, 2000); - await sleep(waitMs); - continue; - } - - return `Error: Unexpected transcriber response status: ${data.status}`; - } - - return "Error: Timed out waiting for transcript after 10 minutes."; -} - -/** Store for transcript polls backgrounded due to user interrupt. */ -export class BackgroundTranscriptStore { - private jobs = new Map<string, { url: string; completion: Promise<string> }>(); - - register(url: string, completion: Promise<string>): string { - const id = `youtube_transcribe_${randomUUID()}`; - this.jobs.set(id, { url, completion }); - // Auto-cleanup 10 minutes after completion - completion.finally(() => { - setTimeout(() => this.jobs.delete(id), 10 * 60 * 1000); - }); - return id; - } - - async getResult( - id: string, - ): Promise<{ status: "done"; result: string } | { status: "error"; error: string }> { - const job = this.jobs.get(id); - if (!job) { - return { status: "error", error: `No background transcript job found with id '${id}'` }; - } - const result = await job.completion; - return { status: "done", result }; - } - - has(id: string): boolean { - return this.jobs.has(id); - } -} - -export function createYoutubeTranscribeTool( - transcriptStore?: BackgroundTranscriptStore, -): ToolDefinition { - return { - name: "youtube_transcribe", - description: [ - "Fetch the transcript/subtitles for a YouTube video. This tool blocks until the transcript is ready.", - "If the video hasn't been transcribed yet, it will be queued and this tool waits for it automatically.", - "If the user interrupts while waiting, the request continues in the background and you receive a job ID.", - "Use the retrieve tool with that ID to get the transcript later.", - "", - "Accepted URL formats:", - " - youtube.com/watch?v=", - " - youtu.be/", - " - youtube.com/embed/", - " - youtube.com/shorts/", - ].join("\n"), - parameters: z.object({ - url: z.string().describe("The YouTube video URL to fetch the transcript for."), - background: z - .boolean() - .optional() - .describe( - "If true, the transcription request starts in the background and a job_id is returned immediately. Use the retrieve tool with the job_id to get the transcript later.", - ), - }), - execute: async ( - args: Record<string, unknown>, - context?: ToolExecuteContext, - ): Promise<string> => { - const url = args.url as string; - const background = (args.background as boolean | undefined) ?? false; - const queueCallbacks = context?.queueCallbacks; - - try { - const pollPromise = pollUntilReady(url); - - // If background mode requested, register immediately and return job ID - if (background && transcriptStore) { - const jobId = transcriptStore.register(url, pollPromise); - return [ - `Transcript request started in background.`, - `job_id: ${jobId}`, - `url: ${url}`, - ``, - `Use the retrieve tool with this job_id to get the transcript when ready.`, - ].join("\n"); - } - - if (queueCallbacks && transcriptStore) { - const { promise: queuePromise, cancel: cancelQueueWait } = - queueCallbacks.waitForQueuedMessage(); - const queueSignal = queuePromise.then(() => "QUEUE_INTERRUPT" as const); - - const raceResult = await Promise.race([pollPromise, queueSignal]); - - if (raceResult === "QUEUE_INTERRUPT") { - // Background the still-polling request - const jobId = transcriptStore.register(url, pollPromise); - - const queuedMsgs = queueCallbacks.dequeueMessages(); - const userMessages = queuedMsgs.map((m) => m.message).join("\n---\n"); - - return [ - `Transcript request backgrounded — still waiting for transcription.`, - `job_id: ${jobId}`, - `url: ${url}`, - ``, - `Use the retrieve tool with this job_id to get the transcript when ready.`, - ``, - `[USER INTERRUPT]`, - `The user has sent you message(s) while you were working. You MUST address these before continuing with your current task:`, - ``, - userMessages, - ].join("\n"); - } - - // Poll finished before interrupt - cancelQueueWait(); - return raceResult; - } - - return await pollPromise; - } catch (err) { - if (err instanceof Error && err.name === "AbortError") { - return "Error: Request to YouTube transcriber timed out."; - } - if (err instanceof Error && (err as NodeJS.ErrnoException).code === "ECONNREFUSED") { - return `Error: Could not connect to YouTube transcriber at ${TRANSCRIBER_BASE}. Is it running?`; - } - return `Error: ${err instanceof Error ? err.message : String(err)}`; - } - }, - }; -} diff --git a/packages/core/src/types/index.ts b/packages/core/src/types/index.ts deleted file mode 100644 index e878813..0000000 --- a/packages/core/src/types/index.ts +++ /dev/null @@ -1,672 +0,0 @@ -import type { ZodType } from "zod"; -import type { PermissionChecker, Ruleset } from "../permission/index.js"; - -// ─── Message Types ─────────────────────────────────────────────── - -export type MessageRole = "user" | "assistant" | "system"; - -/** - * A single ordered chunk of content inside a message. The chunk list - * preserves the actual temporal ordering of text, reasoning, tool calls, - * system notices, and errors as they arrived from the model. - * - * Coalescing rules (see notes/plan-chunk-refactor.md): - * - `text` and `thinking` coalesce on consecutive same-type deltas. - * - `tool-batch` coalesces on consecutive `tool-call` events - * (appends a new entry to `calls`). - * - `error` and `system` are always single-event chunks (no coalescing). - */ -export type Chunk = TextChunk | ThinkingChunk | ToolBatchChunk | ErrorChunk | SystemChunk; - -export interface TextChunk { - type: "text"; - text: string; -} - -export interface ThinkingChunk { - type: "thinking"; - text: string; - /** - * Full Anthropic `providerMetadata` blob captured from the v6 - * `reasoning-end` stream event (typically `{ anthropic: { signature - * } }` plus any other provider-side metadata). Round-tripped verbatim - * as `providerOptions` on the `ReasoningPart` of the next request so - * Anthropic can validate the thinking block's signature. - * - * Also acts as a "sealed" marker for `appendEventToChunks`: once - * `metadata` is set, the next `reasoning-delta` opens a new thinking - * chunk rather than extending this one (each Anthropic content block - * gets its own metadata, so two consecutive thinking blocks must not - * be coalesced). - * - * Optional: non-Anthropic models produce no metadata, and pre-v6 - * persisted chunks have neither field. - */ - metadata?: Record<string, unknown>; -} - -export interface ToolBatchChunk { - type: "tool-batch"; - calls: ToolBatchEntry[]; -} - -export interface ToolBatchEntry { - id: string; - name: string; - arguments: Record<string, unknown>; - result?: string; - isError?: boolean; - shellOutput?: { stdout: string; stderr: string }; -} - -export interface ErrorChunk { - type: "error"; - message: string; - statusCode?: number; -} - -export type SystemChunkKind = "notice" | "model-changed" | "config-reload" | "cancelled"; - -export interface SystemChunk { - type: "system"; - kind: SystemChunkKind; - text: string; -} - -export interface ChatMessage { - role: MessageRole; - chunks: Chunk[]; - /** - * Ephemeral ORDERED multimodal content for a user turn (interleaved text + - * image/pdf attachments). Set ONLY transiently on the in-flight user message - * so `toModelMessages` can emit multimodal `ImagePart`/`FilePart` content to - * the provider. Never persisted (the chunk log stores only the text, with - * `[image]`/`[pdf]` markers), so it's absent on history-rebuilt messages. - * When absent, the message is plain text built from its `chunks`. - */ - content?: UserContentPart[]; -} - -// ─── Multimodal user content (image / PDF attachments) ─────────── -// -// When a user pastes one or more images/PDFs into the chat input, the turn's -// user message carries an ORDERED list of content parts instead of a plain -// string. The ordering is meaningful — the user can interleave text and -// attachments ("here is image A: <A>, here is image B: <B>") and the model -// sees them in exactly that sequence. -// -// These parts are EPHEMERAL: they are forwarded to the model for the turn that -// produced them but are NOT persisted as raw bytes in the chunk log. History -// stores only the user's text (with `[image]` / `[pdf]` markers in place of -// each attachment), so a later reload re-renders the text but never re-sends -// the binary payload. This keeps the persisted log small and avoids re-billing -// image tokens on every subsequent turn. - -/** A plain-text segment of a multimodal user message. */ -export interface UserTextPart { - type: "text"; - text: string; -} - -/** - * A binary attachment (image or PDF) in a multimodal user message. `data` is a - * base64-encoded payload (no `data:` URI prefix); `mediaType` is the IANA media - * type (e.g. `image/png`, `application/pdf`). `name` is an optional original - * filename, used only for PDF `filename` passthrough and diagnostics. - */ -export interface UserAttachmentPart { - type: "attachment"; - /** IANA media type, e.g. `image/png`, `image/jpeg`, `application/pdf`. */ - mediaType: string; - /** Base64-encoded bytes WITHOUT a `data:` URI prefix. */ - data: string; - /** Optional original filename (mainly for PDFs). */ - name?: string; -} - -/** One ordered part of a multimodal user message. */ -export type UserContentPart = UserTextPart | UserAttachmentPart; - -// ─── Append-only chunk log (persisted model) ───────────────────── -// -// The DB stores a conversation as a flat stream of `ChunkRow`s (see -// db/chunks.ts). The render-facing `Chunk`/`ChatMessage` shapes above are -// DERIVED from these rows by grouping (turn_id + step + role). Tool calls -// and their results are SEPARATE rows linked by `callId`, mapping 1:1 to the -// Anthropic wire format. - -/** Role of a persisted chunk row. `tool` rows hold tool results. */ -export type ChunkRole = "user" | "assistant" | "tool" | "system"; - -/** Discriminator for a persisted chunk row's payload. */ -export type ChunkType = - | "text" - | "thinking" - | "tool_call" - | "tool_result" - | "error" - | "system" - | "usage"; - -export interface TextData { - text: string; -} -export interface ThinkingData { - text: string; - metadata?: Record<string, unknown>; -} -export interface ToolCallData { - callId: string; - name: string; - arguments: Record<string, unknown>; -} -export interface ToolResultData { - callId: string; - name: string; - result: string; - isError: boolean; - shellOutput?: { stdout: string; stderr: string }; -} -export interface ErrorData { - message: string; - statusCode?: number; -} -export interface SystemData { - kind: SystemChunkKind; - text: string; -} -/** - * Per-request token usage persisted as a SIDE-CHANNEL chunk row (one row per - * `usage` AgentEvent, i.e. one per LLM round-trip). These rows are deliberately - * EXCLUDED from `getChunksForTab`/`getTotalChunkCount` so they never enter the - * render, pagination, eviction, or agent-history-rebuild paths — they exist - * only to feed the backend aggregate `getUsageStatsForTab`, which seeds the - * frontend's `cacheStats` on reload. `inputTokens` is the TOTAL prompt - * (cached + fresh); `cacheReadTokens`/`cacheWriteTokens` are Anthropic's - * prompt-cache split. Mirrors the `usage` AgentEvent payload. - */ -export interface UsageData { - inputTokens: number; - outputTokens: number; - cacheReadTokens: number; - cacheWriteTokens: number; -} - -/** - * Aggregate per-tab usage telemetry: the cumulative sum across ALL persisted - * `usage` rows, the request count, and the most recent request's split. This is - * the server-side source of truth (complete regardless of frontend - * eviction/pagination) returned by `getUsageStatsForTab`. Structurally - * identical to the frontend `CacheStats` so it can seed it directly. `null` when - * the tab has no usage rows. - */ -export interface UsageStats { - inputTokens: number; - outputTokens: number; - cacheReadTokens: number; - cacheWriteTokens: number; - /** Number of LLM requests (usage rows) counted. */ - requests: number; - last: { - inputTokens: number; - outputTokens: number; - cacheReadTokens: number; - cacheWriteTokens: number; - } | null; -} - -export type ChunkData = - | TextData - | ThinkingData - | ToolCallData - | ToolResultData - | ErrorData - | SystemData - | UsageData; - -/** - * A persisted chunk row — the append-only unit of conversation storage and - * the unit of frontend pagination. `seq` is per-tab monotonic and is both the - * ordering key and the pagination cursor. - */ -export interface ChunkRow { - id: string; - tabId: string; - seq: number; - turnId: string; - step: number; - role: ChunkRole; - type: ChunkType; - data: ChunkData; - createdAt: number; -} - -/** - * A chunk-row draft (no `seq`/`tabId`/`createdAt`/`id` yet) used when - * exploding an in-memory turn into rows for persistence. - */ -export interface ChunkRowDraft { - turnId: string; - step: number; - role: ChunkRole; - type: ChunkType; - data: ChunkData; -} - -export interface ToolCall { - id: string; - name: string; - arguments: Record<string, unknown>; -} - -export interface ToolResult { - toolCallId: string; - toolName: string; - result: string; - isError: boolean; -} - -// ─── Agent Status & Events ─────────────────────────────────────── - -export type AgentStatus = "idle" | "running" | "error" | "waiting_for_key"; - -/** - * Per-tab snapshot of live state, sent on WS connect and via - * `GET /status`. Carries enough information for a freshly-loaded - * frontend to reconstruct any in-flight assistant message. - * - * - `status` — always present; mirrors the in-memory `TabAgent.status`. - * - `currentChunks` — the live in-flight `Chunk[]` for the running - * assistant turn. Present iff `status === "running"` AND - * `TabAgent.currentChunks` is non-null. Defensively copied at - * snapshot time; the consumer owns the array. - * - `currentAssistantId` — DB id of the in-flight assistant message - * (the row that the eventual `flushAssistant` call will write/update). - * Present iff `status === "running"` AND `TabAgent.currentAssistantId` - * is set. The frontend uses this to align its local assistant - * message id with the persisted id so subsequent `done` and reload - * paths line up. - * - * Not part of `AgentEvent` itself: the `statuses` payload is a WS- - * connect-level snapshot, not an event the `Agent` emits. The frontend - * mirrors this type in `packages/frontend/src/lib/types.ts`. - */ -export interface TabStatusSnapshot { - status: AgentStatus; - currentChunks?: Chunk[]; - currentAssistantId?: string; - /** - * `turn_id` of the in-flight turn. Present iff `status === "running"`. - * Lets a frontend that reconnects mid-stream key its live chunks the same - * way `turn-start` would, so they reconcile cleanly when the turn seals. - */ - currentTurnId?: string; - /** - * The tab's current todo list. Included for ALL tabs (not just running - * ones) so a freshly-reloaded frontend rehydrates the Tasks panel from the - * backend instead of blanking it. Omitted when the list is empty. - */ - tasks?: TaskItem[]; -} - -export type AgentEvent = - | { type: "status"; status: AgentStatus } - /** - * Emitted once at the start of a turn (`processMessage`), before any - * content deltas. Carries the `turn_id` shared by this turn's user message - * and every assistant/tool chunk row. The frontend tags its in-flight - * (live) chunks with this id so they key-match the sealed rows on - * turn-completion reconcile (no remount/flicker). Display/sync only — not - * conversation content. - */ - | { type: "turn-start"; turnId: string } - /** - * Emitted once after a turn has fully settled AND its chunks have been - * persisted (after `flushAssistant`). Signals the frontend that the turn's - * rows — with real `seq`s — are now durable and can be reloaded, so it can - * fold its transient live representation into the sealed chunk log. Emitted - * after `status: idle`/`error` (which fire before the DB write). Display/sync - * only — not conversation content. - * - * Carries `usageStats`: the tab's authoritative usage aggregate read from the - * DB AFTER the turn's usage rows were written. The frontend REPLACES (not adds) - * its live `cacheStats` with this, reconciling the live accumulator to the - * persisted truth every turn. This self-heals the live overshoot that occurs - * when a rate-limited fallback attempt's usage is streamed live but then - * discarded server-side (never persisted). `null` ⇒ tab has no usage rows; - * absent ⇒ leave `cacheStats` untouched (back-compat). - */ - | { type: "turn-sealed"; turnId: string; usageStats?: UsageStats | null } - | { type: "text-delta"; delta: string } - | { type: "reasoning-delta"; delta: string } - /** - * Emitted on the v6 `reasoning-end` stream event when it carries - * `providerMetadata`. `appendEventToChunks` attaches the metadata to - * the most recent unsealed `thinking` chunk; `toModelMessages` reads - * it back as `providerOptions` on the next request's `ReasoningPart`. - */ - | { type: "reasoning-end"; metadata?: Record<string, unknown> } - | { type: "tool-call"; toolCall: ToolCall } - | { type: "tool-result"; toolResult: ToolResult } - | { type: "shell-output"; data: string; stream: "stdout" | "stderr" } - /** - * Per-request token usage, emitted once per LLM round-trip (each - * `streamText` step) from the AI SDK `finish` stream event. `inputTokens` - * is the TOTAL prompt size including cached tokens; `cacheReadTokens` is - * the portion served from Anthropic's prompt cache (a cache HIT) and - * `cacheWriteTokens` the portion written to it (a cache seed). The "Cache - * Rate" view aggregates these to show the prompt-cache hit rate. Non- - * caching providers report zero for the cache fields. - */ - | { - type: "usage"; - usage: { - inputTokens: number; - outputTokens: number; - cacheReadTokens: number; - cacheWriteTokens: number; - }; - } - | { type: "error"; error: string; statusCode?: number } - | { type: "notice"; message: string } - | { type: "model-changed"; keyId: string; modelId: string } - | { type: "done"; message: ChatMessage } - | { type: "task-list-update"; tasks: TaskItem[] } - | { type: "config-reload" } - | { - type: "tab-created"; - id: string; - title: string; - keyId: string | null; - modelId: string | null; - parentTabId: string | null; - agentSlug?: string | null; - workingDirectory: string | null; - agentModels?: Array<{ key_id: string; model_id: string }> | null; - } - | { type: "message-queued"; tabId: string; messageId: string; message: string } - | { - type: "message-consumed"; - tabId: string; - messageIds: string[]; - /** - * Why the queue was drained: - * - "interrupt": consumed mid-turn, folded into a running turn's tool - * result as a [USER INTERRUPT]. The optimistic bubble collapses into - * that sealed turn. - * - "continuation": consumed between turns to START a new turn. The - * optimistic bubble becomes that new turn's initiating user row. - * Absent ⇒ treat as "interrupt" (back-compat). - */ - reason?: "interrupt" | "continuation"; - } - | { type: "message-cancelled"; tabId: string; messageId: string } - /** - * Conversation-compaction lifecycle (UI-driven, not an agent tool). A - * compaction summarizes a tab's older history into an anchored summary while - * preserving the most recent turns verbatim. - * - * `compaction-started` fires on the temporary placeholder tab when the - * summary request begins. `compaction-complete` fires when the summary has - * been generated and the history relocated: the compacted continuation now - * lives on `sourceTabId` (the canonical id, with its key/model/working-dir - * preserved), the FULL pre-compaction history was moved to `backupTabId`, and - * `tempTabId` (the placeholder) should be discarded by the frontend. - * `compaction-error` reports a failure (or cancellation) on `tempTabId`. - */ - | { type: "compaction-started"; tempTabId: string; sourceTabId: string } - | { - type: "compaction-complete"; - tempTabId: string; - sourceTabId: string; - backupTabId: string; - backupTitle: string; - } - | { type: "compaction-error"; tempTabId: string; sourceTabId: string; error: string }; - -// ─── Tool Types ────────────────────────────────────────────────── - -export interface ToolExecuteContext { - onOutput?: (data: string, stream: "stdout" | "stderr") => void; - queueCallbacks?: QueueCallbacks; -} - -export interface ToolDefinition { - name: string; - description: string; - parameters: ZodType; - execute: (args: Record<string, unknown>, context?: ToolExecuteContext) => Promise<string>; -} - -// ─── Agent Configuration ───────────────────────────────────────── - -/** - * Canonical, ordered list of reasoning-effort levels — the SINGLE SOURCE OF - * TRUTH for effort values across the whole codebase (core LLM call site, API - * validation, agent TOML persistence, and the frontend UI). Ordered from least - * to most effort. - * - * `none` disables reasoning. `low`/`medium`/`high`/`xhigh` are forwarded - * verbatim to providers that accept them (OpenAI-compatible `reasoning_effort`, - * Anthropic adaptive `effort`) — `xhigh` is accepted by newer OpenAI reasoning - * models. `max` is Dispatch's own top tier, mapped per-provider at the call - * site (e.g. classic-thinking Claude budget tokens). - */ -export const REASONING_EFFORTS = ["none", "low", "medium", "high", "xhigh", "max"] as const; - -export type ReasoningEffort = (typeof REASONING_EFFORTS)[number]; - -/** - * Default effort applied when nothing more specific is configured (no per-model - * effort and no per-tab selection). Resolution order is - * per-model → per-tab → this default. - */ -export const DEFAULT_REASONING_EFFORT: ReasoningEffort = "high"; - -/** Human-readable labels for each effort level (UI display). */ -export const REASONING_EFFORT_LABELS: Record<ReasoningEffort, string> = { - none: "Off", - low: "Low", - medium: "Medium", - high: "High", - xhigh: "X-High", - max: "Max", -}; - -/** Runtime type guard for narrowing an arbitrary value to a `ReasoningEffort`. */ -export function isReasoningEffort(value: unknown): value is ReasoningEffort { - return typeof value === "string" && (REASONING_EFFORTS as readonly string[]).includes(value); -} - -export interface AgentConfig { - model: string; - apiKey: string; - baseURL: string; - systemPrompt: string; - tools: ToolDefinition[]; - workingDirectory: string; - permissionChecker?: PermissionChecker; - ruleset?: Ruleset; - reasoningEffort?: ReasoningEffort; - provider?: string; - claudeCredentials?: { - accessToken: string; - }; - /** - * Tab ID the agent runs on. Used to scope per-tab side effects, namely - * the tool-output spill directory (`/tmp/dispatch/tool-results/<tabId>/`). - * Optional so legacy callers and tests can construct an Agent without one; - * a fallback ID is generated when absent. - */ - tabId?: string; -} - -// ─── Config Types (dispatch.toml) ──────────────────────────────── - -export interface DispatchConfig { - keys?: KeyDefinition[]; - permissions: Record<string, string | Record<string, string>>; - /** - * Language Server Protocol servers, keyed by an arbitrary server id (e.g. - * `"luau-lsp"`). Resolved by merging the HOME-directory global - * `dispatch.toml` (`~/.config/dispatch/dispatch.toml`) underneath the - * `dispatch.toml` in a tab's effective working directory — local entries - * override global ones sharing the same id, and global-only servers stay - * active in every repository. Re-consulted when either config (or the - * directory) changes. Config-driven only — there is no builtin server - * registry and no auto-download; the declared `command[0]` must be on PATH. - */ - lsp?: Record<string, LspServerConfig>; -} - -/** - * A single LSP server entry as expressed in `dispatch.toml`'s `[lsp.<id>]` - * block. Mirrors opencode's custom-server schema so the Roblox Luau config - * (and any other server) is portable between the two. - * - * Example (`dispatch.toml`): - * ```toml - * [lsp.luau-lsp] - * command = ["luau-lsp", "lsp", "--definitions=globalTypes.d.luau", "--docs=api-docs.json"] - * extensions = [".luau"] - * - * [lsp.luau-lsp.initialization.luau-lsp.platform] - * type = "roblox" - * ``` - */ -export interface LspServerConfig { - /** - * Argv to launch the server over stdio. `command[0]` is the executable - * (resolved via PATH); the rest are arguments. Required for every non- - * disabled entry. - */ - command: string[]; - /** - * File extensions (with leading dot, e.g. `".luau"`) this server attaches - * to. Required for custom servers — without it the client never knows which - * files should activate the server. - */ - extensions: string[]; - /** Extra environment variables merged onto `process.env` for the child. */ - env?: Record<string, string>; - /** - * `initializationOptions` forwarded verbatim in the LSP `initialize` - * request (and echoed back for `workspace/configuration` / - * `didChangeConfiguration`). For luau-lsp this carries the - * `{ "luau-lsp": { platform, sourcemap, types, diagnostics, ... } }` block. - */ - initialization?: Record<string, unknown>; - /** When true, the entry is parsed but skipped (no server launched). */ - disabled?: boolean; -} - -export interface KeyDefinition { - id: string; - provider: string; - env?: string; - base_url: string; - /** For "anthropic" provider: path to credentials file (default: ~/.claude/.credentials.json) */ - credentials_file?: string; -} - -export type KeyStatus = "active" | "exhausted"; - -export interface KeyState { - definition: KeyDefinition; - status: KeyStatus; - lastError?: string; - exhaustedAt?: number; -} - -// ─── Skills Types ──────────────────────────────────────────────── - -export type SkillScope = "global" | "project"; -export type SkillDirectory = string; - -export interface SkillDefinition { - name: string; - description: string; - tags: string[]; - content: string; - scope: SkillScope; - source: string; - directory: SkillDirectory; -} - -export interface AgentSkillMapping { - agentType: string; - isOrchestrator: boolean; - skills: string[]; - scope: SkillScope; -} - -// ─── Task List Types ───────────────────────────────────────────── - -export type TaskStatus = "pending" | "in_progress" | "completed" | "cancelled"; - -export interface TaskItem { - /** - * Stable positional id used purely for UI keying and the - * `task-list-update` event contract. It is NEVER exposed to the model: - * the `todo` tool is a declarative whole-list write (the model sends the - * entire desired list every call), so there are no ids for the model to - * track. Ids are reassigned positionally on every `setTasks`. - */ - id: string; - content: string; - status: TaskStatus; -} - -// ─── Config Validation ─────────────────────────────────────────── - -export interface ConfigError { - path: string; - message: string; -} - -// ─── Message Queue Types ───────────────────────────────────────── - -export interface QueuedMessage { - id: string; - message: string; - timestamp: number; -} - -export interface QueueCallbacks { - dequeueMessages: () => QueuedMessage[]; - waitForQueuedMessage: () => { promise: Promise<void>; cancel: () => void }; -} - -// ─── Agent Definition Types ────────────────────────────────────── - -export interface AgentModelEntry { - key_id: string; - model_id: string; - /** - * Per-model/key reasoning effort. When set, overrides the per-tab effort - * selector for generations that use this entry (resolution order: - * per-model → per-tab → DEFAULT_REASONING_EFFORT). Omitted when unset. - */ - effort?: ReasoningEffort; -} - -export interface AgentDefinition { - /** Human-readable name */ - name: string; - /** Short description of what this agent does */ - description: string; - /** Skills to auto-include, as "scope:name" strings */ - skills: string[]; - /** Allowed tools (allowlist) */ - tools: string[]; - /** Key+model fallback hierarchy, tried in order */ - models: AgentModelEntry[]; - /** Where the TOML was loaded from: "global" or a directory path */ - scope: string; - /** The slug (filename without .toml) */ - slug: string; - /** Default working directory for this agent (optional, absolute path) */ - cwd?: string; - /** Whether this agent is a subagent (hidden from Chat Settings) */ - is_subagent?: boolean; -} diff --git a/packages/core/tests/agent/agent.test.ts b/packages/core/tests/agent/agent.test.ts deleted file mode 100644 index 797aea2..0000000 --- a/packages/core/tests/agent/agent.test.ts +++ /dev/null @@ -1,1791 +0,0 @@ -import { describe, expect, it, vi } from "vitest"; -import { z } from "zod"; -import type { AgentConfig, AgentEvent } from "../../src/types/index.js"; - -// Mock bun:sqlite to avoid Bun-only import in vitest/Node -vi.mock("../../src/db/index.js", () => ({ - getDatabase: vi.fn(() => ({})), -})); - -// Mock the credentials module that depends on the DB -vi.mock("../../src/credentials/claude.js", () => ({ - buildBillingHeaderValue: vi.fn(() => ""), - SYSTEM_IDENTITY: "You are a test agent.", -})); - -// Mock the ai module's streamText -vi.mock("ai", async () => { - const actual = await import("ai"); - return { - ...actual, - streamText: vi.fn(), - }; -}); - -// Mock the provider -vi.mock("@ai-sdk/openai-compatible", () => ({ - createOpenAICompatible: vi.fn(() => (_model: string) => ({ - type: "language-model", - modelId: _model, - })), -})); - -const { Agent, anthropicThinkingProviderOptions } = await import("../../src/agent/agent.js"); -const { streamText } = await import("ai"); - -function makeConfig(overrides: Partial<AgentConfig> = {}): AgentConfig { - return { - model: "test-model", - apiKey: "test-key", - baseURL: "https://example.com/v1", - systemPrompt: "You are a helpful assistant.", - tools: [], - workingDirectory: "/tmp", - ...overrides, - }; -} - -async function* makeFullStream( - events: Array<{ type: string; [key: string]: unknown }>, -): AsyncGenerator<{ type: string; [key: string]: unknown }> { - for (const event of events) { - yield event; - } -} - -function makeMockStreamResult(events: Array<{ type: string; [key: string]: unknown }>) { - return { - fullStream: makeFullStream(events), - } as ReturnType<typeof import("ai").streamText>; -} - -// v6 finish event — only finishReason, rawFinishReason, totalUsage (no usage/providerMetadata/response) -const finishStop = { - type: "finish", - finishReason: "stop", - rawFinishReason: "stop", - totalUsage: { inputTokens: 10, outputTokens: 5 }, -}; - -const finishToolCalls = { - type: "finish", - finishReason: "tool-calls", - rawFinishReason: "tool_use", - totalUsage: { inputTokens: 10, outputTokens: 5 }, -}; - -describe("Agent", () => { - it("starts in idle status", () => { - const agent = new Agent(makeConfig()); - expect(agent.status).toBe("idle"); - }); - - it("has empty messages initially", () => { - const agent = new Agent(makeConfig()); - expect(agent.messages).toHaveLength(0); - }); - - it("yields running then idle status events around a simple message", async () => { - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([ - // v6: text-delta uses `text` (not `textDelta`) - { type: "text-delta", id: "t0", text: "Hello!" }, - finishStop, - ]), - ); - - const agent = new Agent(makeConfig()); - const events = []; - for await (const event of agent.run("hi")) { - events.push(event); - } - - const types = events.map((e) => e.type); - expect(types[0]).toBe("status"); - expect(events[0]).toMatchObject({ type: "status", status: "running" }); - - const lastStatusEvent = events.filter((e) => e.type === "status").at(-1); - expect(lastStatusEvent).toMatchObject({ type: "status", status: "idle" }); - }); - - it("yields text-delta events", async () => { - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([ - // v6: text-delta uses `text` (not `textDelta`) - { type: "text-delta", id: "t0", text: "Hello" }, - { type: "text-delta", id: "t0", text: " world" }, - finishStop, - ]), - ); - - const agent = new Agent(makeConfig()); - const events = []; - for await (const event of agent.run("test")) { - events.push(event); - } - - const textDeltas = events.filter((e) => e.type === "text-delta"); - expect(textDeltas).toHaveLength(2); - expect(textDeltas[0]).toMatchObject({ delta: "Hello" }); - expect(textDeltas[1]).toMatchObject({ delta: " world" }); - }); - - it("adds user message and assistant message to history", async () => { - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "Response" }, finishStop]), - ); - - const agent = new Agent(makeConfig()); - for await (const _ of agent.run("my question")) { - // consume generator - } - - expect(agent.messages).toHaveLength(2); - expect(agent.messages[0]).toMatchObject({ - role: "user", - chunks: [{ type: "text", text: "my question" }], - }); - expect(agent.messages[1]).toMatchObject({ - role: "assistant", - chunks: [{ type: "text", text: "Response" }], - }); - }); - - it("yields done event with final message", async () => { - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "Done!" }, finishStop]), - ); - - const agent = new Agent(makeConfig()); - const events = []; - for await (const event of agent.run("test")) { - events.push(event); - } - - const doneEvent = events.find((e) => e.type === "done"); - expect(doneEvent).toBeDefined(); - expect(doneEvent).toMatchObject({ - type: "done", - message: { role: "assistant", chunks: [{ type: "text", text: "Done!" }] }, - }); - }); - - it("yields tool-call and tool-result events", async () => { - // First call: LLM emits a tool-call - // Second call (after tool execution): LLM emits text response with no tool calls - vi.mocked(streamText) - .mockReturnValueOnce( - makeMockStreamResult([ - { - type: "tool-call", - toolCallId: "tc1", - toolName: "read_file", - // v6: `input` replaces `args` - input: { path: "hello.txt" }, - }, - finishToolCalls, - ]), - ) - .mockReturnValueOnce( - makeMockStreamResult([ - { type: "text-delta", id: "t0", text: "Here is the file." }, - finishStop, - ]), - ); - - const toolDef = { - name: "read_file", - description: "reads a file", - parameters: z.object({ path: z.string() }), - execute: async (_args: Record<string, unknown>) => "file contents", - }; - - const agent = new Agent(makeConfig({ tools: [toolDef] })); - const events = []; - for await (const event of agent.run("read the file")) { - events.push(event); - } - - const toolCallEvent = events.find((e) => e.type === "tool-call"); - expect(toolCallEvent).toMatchObject({ - type: "tool-call", - toolCall: { id: "tc1", name: "read_file" }, - }); - - const toolResultEvent = events.find((e) => e.type === "tool-result"); - expect(toolResultEvent).toMatchObject({ - type: "tool-result", - toolResult: { toolCallId: "tc1", result: "file contents" }, - }); - }); - - it("does NOT swallow trailing queued messages into history at turn end", async () => { - // Regression for the "queue not consumed after the turn ends" bug. A - // message that lands on the queue after the last tool call (here: a - // no-tool turn) must be LEFT on the queue for the orchestrator to start - // a new turn — not silently appended to history with no response. - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "done" }, finishStop]), - ); - - const queue = [{ id: "q1", message: "answer me next", timestamp: 1 }]; - const dequeueMessages = vi.fn(() => queue.splice(0, queue.length)); - const agent = new Agent(makeConfig(), { - dequeueMessages, - waitForQueuedMessage: () => ({ promise: Promise.resolve(), cancel: () => {} }), - }); - - const before = agent.messages.length; - for await (const _ of agent.run("hello")) { - // consume - } - - // The agent appended exactly the user turn + its own assistant reply; - // it did NOT drain the queue or append a trailing user message for it. - expect(dequeueMessages).not.toHaveBeenCalled(); - expect(queue).toHaveLength(1); - const added = agent.messages.slice(before); - expect(added.map((m) => m.role)).toEqual(["user", "assistant"]); - expect( - added.some((m) => m.chunks.some((c) => c.type === "text" && c.text === "answer me next")), - ).toBe(false); - }); - - it("still injects a mid-turn queued message into the last tool result", async () => { - // The interrupt path (site 1) must be untouched by the turn-end fix: a - // message present DURING a tool batch is folded into that batch's last - // tool result as a [USER INTERRUPT], and the agent loops back to the LLM. - vi.mocked(streamText) - .mockReturnValueOnce( - makeMockStreamResult([ - { type: "tool-call", toolCallId: "tc1", toolName: "read_file", input: { path: "a.txt" } }, - finishToolCalls, - ]), - ) - .mockReturnValueOnce( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "ok" }, finishStop]), - ); - - const queue = [{ id: "q1", message: "stop and do X", timestamp: 1 }]; - const dequeueMessages = vi.fn(() => queue.splice(0, queue.length)); - const toolDef = { - name: "read_file", - description: "reads a file", - parameters: z.object({ path: z.string() }), - execute: async () => "file contents", - }; - const agent = new Agent(makeConfig({ tools: [toolDef] }), { - dequeueMessages, - waitForQueuedMessage: () => ({ promise: Promise.resolve(), cancel: () => {} }), - }); - - const events: AgentEvent[] = []; - for await (const event of agent.run("read it")) { - events.push(event); - } - - expect(dequeueMessages).toHaveBeenCalled(); - const toolResult = events.find((e) => e.type === "tool-result") as - | (AgentEvent & { toolResult: { result: string } }) - | undefined; - expect(toolResult?.toolResult.result).toContain("[USER INTERRUPT]"); - expect(toolResult?.toolResult.result).toContain("stop and do X"); - }); - - it("yields reasoning-delta events", async () => { - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([ - // v6: reasoning-delta uses `text` (not `textDelta`) - { type: "reasoning-delta", id: "r0", text: "thinking about this..." }, - { type: "reasoning-delta", id: "r0", text: " more thoughts" }, - { type: "text-delta", id: "t0", text: "Answer" }, - finishStop, - ]), - ); - - const agent = new Agent(makeConfig()); - const events = []; - for await (const event of agent.run("think")) { - events.push(event); - } - - const reasoningDeltas = events.filter((e) => e.type === "reasoning-delta"); - expect(reasoningDeltas).toHaveLength(2); - expect(reasoningDeltas[0]).toMatchObject({ delta: "thinking about this..." }); - expect(reasoningDeltas[1]).toMatchObject({ delta: " more thoughts" }); - }); - - it("yields reasoning-end event when providerMetadata is present", async () => { - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([ - { type: "reasoning-delta", id: "r0", text: "some reasoning" }, - { - type: "reasoning-end", - id: "r0", - providerMetadata: { anthropic: { signature: "sig-1" } }, - }, - { type: "text-delta", id: "t0", text: "Answer" }, - finishStop, - ]), - ); - - const agent = new Agent(makeConfig()); - const events = []; - for await (const event of agent.run("think")) { - events.push(event); - } - - const reasoningEndEvent = events.find((e) => e.type === "reasoning-end"); - expect(reasoningEndEvent).toBeDefined(); - expect(reasoningEndEvent).toMatchObject({ - type: "reasoning-end", - metadata: { anthropic: { signature: "sig-1" } }, - }); - }); - - it("does NOT yield reasoning-end event when providerMetadata is absent", async () => { - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([ - { type: "reasoning-delta", id: "r0", text: "some reasoning" }, - { - type: "reasoning-end", - id: "r0", - // No providerMetadata — non-Anthropic model - }, - { type: "text-delta", id: "t0", text: "Answer" }, - finishStop, - ]), - ); - - const agent = new Agent(makeConfig()); - const events = []; - for await (const event of agent.run("think")) { - events.push(event); - } - - const reasoningEndEvent = events.find((e) => e.type === "reasoning-end"); - expect(reasoningEndEvent).toBeUndefined(); - }); - - // ─── New v6 round-trip tests ────────────────────────────────────────────── - - it("signed thinking round-trip: ThinkingChunk.metadata → ReasoningPart.providerOptions", async () => { - // Pre-seed the agent with a prior assistant message containing a ThinkingChunk - // with metadata (the Anthropic signature blob). - // Anthropic-path provider — for openai-compatible the metadata - // would be lifted into providerOptions.openaiCompatible instead; - // that path is covered by the DeepSeek tests further down. - const agent = new Agent(makeConfig({ provider: "opencode-anthropic" })); - agent.messages.push({ - role: "user", - chunks: [{ type: "text", text: "prior user message" }], - }); - agent.messages.push({ - role: "assistant", - chunks: [ - { - type: "thinking", - text: "I thought about it", - metadata: { anthropic: { signature: "S" } }, - }, - { type: "text", text: "prior response" }, - ], - }); - - // Next turn: just return a simple text response - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "New answer" }, finishStop]), - ); - - for await (const _ of agent.run("follow-up")) { - // consume - } - - // Inspect the messages passed to streamText in this (last) call - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - expect(callArgs).toBeDefined(); - const messages = callArgs?.messages as Array<{ - role: string; - content: unknown; - }>; - - // Find the assistant message in the rebuilt ModelMessage[] - const assistantMsg = messages.find((m) => m.role === "assistant"); - expect(assistantMsg).toBeDefined(); - const content = assistantMsg?.content as Array<Record<string, unknown>>; - const reasoningPart = content.find((p) => p.type === "reasoning"); - expect(reasoningPart).toBeDefined(); - expect(reasoningPart).toMatchObject({ - type: "reasoning", - text: "I thought about it", - providerOptions: { anthropic: { signature: "S" } }, - }); - }); - - it("tool-call input round-trip: ToolBatchEntry.arguments → ToolCallPart.input (not args)", async () => { - // Pre-seed the agent with a prior assistant message containing a tool-batch chunk - const agent = new Agent(makeConfig()); - agent.messages.push({ - role: "user", - chunks: [{ type: "text", text: "run a tool" }], - }); - agent.messages.push({ - role: "assistant", - chunks: [ - { - type: "tool-batch", - calls: [ - { - id: "call-1", - name: "read_file", - arguments: { path: "/foo/bar.txt" }, - result: "file contents", - }, - ], - }, - ], - }); - - // Next turn: just return a simple text response - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "Done" }, finishStop]), - ); - - for await (const _ of agent.run("follow-up")) { - // consume - } - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ role: string; content: unknown }>; - - // The assistant message should contain a tool-call part with `input` (not `args`) - const assistantMsg = messages.find((m) => m.role === "assistant"); - expect(assistantMsg).toBeDefined(); - const content = assistantMsg?.content as Array<Record<string, unknown>>; - const toolCallPart = content.find((p) => p.type === "tool-call"); - expect(toolCallPart).toBeDefined(); - expect(toolCallPart).toMatchObject({ - type: "tool-call", - toolCallId: "call-1", - toolName: "read_file", - input: { path: "/foo/bar.txt" }, // v6: input not args - }); - // Explicitly assert `args` is NOT present - expect(toolCallPart).not.toHaveProperty("args"); - }); - - it("tool-result output round-trip: result string → { type: 'text', value } ToolResultOutput", async () => { - // Pre-seed the agent with a prior assistant message containing a tool-batch chunk - const agent = new Agent(makeConfig()); - agent.messages.push({ - role: "user", - chunks: [{ type: "text", text: "run a tool" }], - }); - agent.messages.push({ - role: "assistant", - chunks: [ - { - type: "tool-batch", - calls: [ - { - id: "call-2", - name: "read_file", - arguments: { path: "/foo/baz.txt" }, - result: "the file content here", - }, - ], - }, - ], - }); - - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "Done" }, finishStop]), - ); - - for await (const _ of agent.run("follow-up")) { - // consume - } - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ role: string; content: unknown }>; - - // The tool message should contain a tool-result part with `output` (ToolResultOutput) - const toolMsg = messages.find((m) => m.role === "tool"); - expect(toolMsg).toBeDefined(); - const toolContent = toolMsg?.content as Array<Record<string, unknown>>; - expect(toolContent[0]).toMatchObject({ - type: "tool-result", - toolCallId: "call-2", - toolName: "read_file", - output: { type: "text", value: "the file content here" }, - }); - // Explicitly assert `result` (v4 raw string) is NOT present - expect(toolContent[0]).not.toHaveProperty("result"); - }); - - it("per-step segmentation: a [tool-batch, text] turn becomes [assistant(tool-call), tool(result), assistant(text)]", async () => { - // `toModelMessages` segments a turn at each tool-batch boundary, so the - // tool-batch (step 0) and the trailing text (step 1) land in SEPARATE - // assistant messages — never a single invalid [tool_use, text] block. - // This is the cache-stability fix and is applied for every provider. - const agent = new Agent(makeConfig({ provider: "opencode-anthropic" })); - agent.messages.push({ - role: "user", - chunks: [{ type: "text", text: "run a tool and explain" }], - }); - agent.messages.push({ - role: "assistant", - chunks: [ - // Note: tool-batch appears BEFORE text in chunks — this is the - // problematic ordering that Anthropic rejects - { - type: "tool-batch", - calls: [ - { - id: "call-3", - name: "read_file", - arguments: { path: "/tmp/x.txt" }, - result: "x contents", - }, - ], - }, - { type: "text", text: "Here is my explanation." }, - ], - }); - - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "ok" }, finishStop]), - ); - - for await (const _ of agent.run("follow-up")) { - // consume - } - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ role: string; content: unknown }>; - - // No assistant message may mix tool-call and non-tool-call parts (the - // invalid shape Anthropic rejects); segmentation guarantees this. - const assistantMsgs = messages.filter((m) => m.role === "assistant"); - for (const m of assistantMsgs) { - const c = m.content as Array<Record<string, unknown>>; - if (!Array.isArray(c)) continue; - const hasToolCall = c.some((p) => p.type === "tool-call"); - const hasNonToolCall = c.some((p) => p.type !== "tool-call"); - expect(hasToolCall && hasNonToolCall).toBe(false); - } - - // The seeded turn yields a tool-call assistant message immediately - // followed by its tool-result message (valid tool_use → tool_result). - const toolOnlyIdx = messages.findIndex((m) => { - const c = m.content as Array<Record<string, unknown>>; - return m.role === "assistant" && Array.isArray(c) && c.some((p) => p.type === "tool-call"); - }); - expect(toolOnlyIdx).toBeGreaterThanOrEqual(0); - expect(messages[toolOnlyIdx + 1]?.role).toBe("tool"); - }); - - it("per-step segmentation also applies to the openai-compatible provider", async () => { - // Segmentation is provider-agnostic: a [tool-batch, text] turn is split - // into separate assistant messages for openai-compatible too, with the - // tool result in its own tool message (the standard OpenAI shape). - const agent = new Agent(makeConfig()); - agent.messages.push({ - role: "user", - chunks: [{ type: "text", text: "run a tool and explain" }], - }); - agent.messages.push({ - role: "assistant", - chunks: [ - { - type: "tool-batch", - calls: [ - { - id: "call-4", - name: "read_file", - arguments: { path: "/tmp/y.txt" }, - result: "y contents", - }, - ], - }, - { type: "text", text: "Here is my explanation." }, - ], - }); - - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "ok" }, finishStop]), - ); - - for await (const _ of agent.run("follow-up")) { - // consume - } - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ role: string; content: unknown }>; - - // The seeded [tool-batch, text] turn is segmented: a tool-call-only - // assistant message, its tool message, and a separate text assistant - // message (the new turn's "ok" reply adds one more). No assistant - // message mixes tool-call and non-tool-call parts. - const assistantMsgs = messages.filter((m) => m.role === "assistant"); - for (const m of assistantMsgs) { - const c = m.content as Array<Record<string, unknown>>; - if (!Array.isArray(c)) continue; - expect(c.some((p) => p.type === "tool-call") && c.some((p) => p.type !== "tool-call")).toBe( - false, - ); - } - expect(messages.some((m) => m.role === "tool")).toBe(true); - const toolCallMsg = assistantMsgs.find((m) => { - const c = m.content as Array<Record<string, unknown>>; - return Array.isArray(c) && c.some((p) => p.type === "tool-call"); - }); - expect(toolCallMsg).toBeDefined(); - }); - - it("empty-text-part filter (Anthropic): empty text chunk is not sent", async () => { - // Pre-seed an assistant message where a text chunk has empty text. - const agent = new Agent(makeConfig({ provider: "opencode-anthropic" })); - agent.messages.push({ - role: "user", - chunks: [{ type: "text", text: "hello" }], - }); - agent.messages.push({ - role: "assistant", - chunks: [ - { type: "text", text: "" }, // empty text — should be filtered out - { type: "text", text: "non-empty response" }, - ], - }); - - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "ok" }, finishStop]), - ); - - for await (const _ of agent.run("follow-up")) { - // consume - } - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ role: string; content: unknown }>; - - const assistantMsg = messages.find((m) => m.role === "assistant"); - expect(assistantMsg).toBeDefined(); - const content = assistantMsg?.content as Array<Record<string, unknown>>; - - // Empty text part should have been filtered out - const emptyTextParts = content.filter((p) => p.type === "text" && p.text === ""); - expect(emptyTextParts).toHaveLength(0); - - // The non-empty text part should still be there - const nonEmptyTextParts = content.filter((p) => p.type === "text" && p.text !== ""); - expect(nonEmptyTextParts).toHaveLength(1); - expect(nonEmptyTextParts[0]).toMatchObject({ text: "non-empty response" }); - }); - - it("empty-reasoning-part filter (Anthropic): empty reasoning chunk is not sent", async () => { - // Anthropic's adaptive thinking mode occasionally produces a signed- - // but-empty thinking block. We persist it (for signature round-trip - // fidelity) but strip the empty `reasoning` part before sending it - // back, or Anthropic rejects with "thinking block must have content". - const agent = new Agent(makeConfig({ provider: "opencode-anthropic" })); - agent.messages.push({ role: "user", chunks: [{ type: "text", text: "hi" }] }); - agent.messages.push({ - role: "assistant", - chunks: [ - // Signed-but-empty thinking block - { type: "thinking", text: "", metadata: { anthropic: { signature: "sig-empty" } } }, - { type: "text", text: "answer" }, - ], - }); - - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "ok" }, finishStop]), - ); - - for await (const _ of agent.run("follow-up")) { - /* consume */ - } - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ role: string; content: unknown }>; - const assistantMsg = messages.find((m) => m.role === "assistant"); - const content = assistantMsg?.content as Array<Record<string, unknown>>; - - // Empty reasoning part must have been filtered out by the - // Anthropic structural normalisation pass. - const emptyReasoning = content.filter((p) => p.type === "reasoning" && p.text === ""); - expect(emptyReasoning).toHaveLength(0); - - // The text part should still be there - expect(content.some((p) => p.type === "text" && p.text === "answer")).toBe(true); - }); - - it("toolCallId scrubbing (Anthropic): non-[a-zA-Z0-9_-] chars in tool IDs are sanitised", async () => { - // Anthropic rejects toolCallId outside [a-zA-Z0-9_-]. Our internal - // crypto.randomUUID IDs are safe, but defensively scrub for any - // upstream-assigned IDs (subagent retrieval, provider-executed - // tools, MCP, etc.). Mirrors opencode transform.ts:96-122. - const agent = new Agent(makeConfig({ provider: "opencode-anthropic" })); - agent.messages.push({ role: "user", chunks: [{ type: "text", text: "do the thing" }] }); - agent.messages.push({ - role: "assistant", - chunks: [ - { - type: "tool-batch", - calls: [ - { - id: "call.with/dots:and:slashes", // invalid chars - name: "fake_tool", - arguments: { x: 1 }, - result: "ok", - isError: false, - }, - ], - }, - ], - }); - - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "ok" }, finishStop]), - ); - - for await (const _ of agent.run("follow-up")) { - /* consume */ - } - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ role: string; content: unknown }>; - - // Assistant tool-call part must have scrubbed ID - const assistantMsg = messages.find((m) => m.role === "assistant"); - const assistantContent = assistantMsg?.content as Array<Record<string, unknown>>; - const toolCallPart = assistantContent.find((p) => p.type === "tool-call"); - expect(toolCallPart).toBeDefined(); - expect(toolCallPart?.toolCallId).toBe("call_with_dots_and_slashes"); - - // Matching tool-result message must use the SAME scrubbed ID - // so Anthropic can pair them. - const toolMsg = messages.find((m) => m.role === "tool"); - const toolContent = toolMsg?.content as Array<Record<string, unknown>>; - expect(toolContent?.[0]?.toolCallId).toBe("call_with_dots_and_slashes"); - }); - - it("reasoning metadata captured from stream is round-tripped on the next turn", async () => { - // End-to-end integrity for the providerMetadata round-trip — the - // bug that prompted the entire migration. Stream a turn that - // emits reasoning-delta + reasoning-end with metadata, then run - // ANOTHER turn and verify the metadata reaches the model via - // ReasoningPart.providerOptions. - const agent = new Agent(makeConfig({ provider: "opencode-anthropic" })); - const sig = { anthropic: { signature: "round-trip-sig-1" } }; - - // Turn 1: model emits reasoning + signed reasoning-end - vi.mocked(streamText).mockReturnValueOnce( - makeMockStreamResult([ - { type: "reasoning-delta", id: "r0", text: "let me think" }, - { type: "reasoning-end", id: "r0", providerMetadata: sig }, - { type: "text-delta", id: "t0", text: "answer" }, - finishStop, - ]), - ); - - for await (const _ of agent.run("first question")) { - /* consume */ - } - - // After turn 1, the persisted chunks should include a ThinkingChunk - // with the captured metadata. The agent's messages array IS the - // canonical persisted shape (the DB just JSON-stringifies it). - const turn1Assistant = agent.messages.find( - (m, i) => m.role === "assistant" && i === agent.messages.length - 1, - ); - expect(turn1Assistant).toBeDefined(); - const thinkingChunk = turn1Assistant?.chunks.find((c) => c.type === "thinking"); - expect(thinkingChunk).toBeDefined(); - expect(thinkingChunk).toMatchObject({ text: "let me think", metadata: sig }); - - // Turn 2: drive another turn, capture what streamText receives. - vi.mocked(streamText).mockReturnValueOnce( - makeMockStreamResult([ - { type: "text-delta", id: "t1", text: "follow-up answer" }, - finishStop, - ]), - ); - for await (const _ of agent.run("second question")) { - /* consume */ - } - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ role: string; content: unknown }>; - const turn2Assistant = messages - .filter((m) => m.role === "assistant") - .find((m) => Array.isArray(m.content)); - const turn2Content = turn2Assistant?.content as Array<Record<string, unknown>>; - const reasoningPart = turn2Content.find((p) => p.type === "reasoning"); - expect(reasoningPart).toBeDefined(); - expect(reasoningPart).toMatchObject({ - type: "reasoning", - text: "let me think", - providerOptions: sig, - }); - }); - - it("tool-error stream event yields a synthetic tool-result + error chunk and continues the turn", async () => { - // Provider-executed tools (Anthropic server tools) bypass our - // manual executor and surface as a `tool-error` stream event. - // We must: - // 1. Synthesize a tool-result with isError=true so the chunks - // reflect that the tool ran and failed — this keeps the - // tool-call/tool-result pairing complete and avoids the AI SDK - // throwing MissingToolResultsError on the next round-trip. - // 2. Emit an error chunk so the UI shows the failure. - // 3. NOT transition to "error" status — the step breaks out of the - // stream loop and the turn ends normally (here, with no further - // tool calls pending, the agent completes to idle). - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([ - { - type: "tool-error", - toolCallId: "tc_server", - toolName: "server_tool", - error: new Error("upstream tool failure"), - }, - finishStop, - ]), - ); - - const agent = new Agent(makeConfig()); - const events: AgentEvent[] = []; - for await (const event of agent.run("trigger")) { - events.push(event); - } - - // Synthetic tool-result with the upstream error - const trEvent = events.find((e) => e.type === "tool-result"); - expect(trEvent).toBeDefined(); - expect(trEvent).toMatchObject({ - type: "tool-result", - toolResult: { toolCallId: "tc_server", isError: true }, - }); - - // Error chunk for visibility - const errEvent = events.find((e) => e.type === "error"); - expect(errEvent).toBeDefined(); - const errMsg = errEvent && "error" in errEvent ? errEvent.error : ""; - expect(typeof errMsg).toBe("string"); - expect((errMsg as string).includes("upstream tool failure")).toBe(true); - - // Status does NOT transition to error — the turn completes to idle. - const lastStatus = events.filter((e) => e.type === "status").at(-1); - expect(lastStatus).toMatchObject({ type: "status", status: "idle" }); - - // The turn produced a `done` event (it did not abort). - expect(events.some((e) => e.type === "done")).toBe(true); - }); - - it("tool-error leaves sibling tool calls to be resolved by the executor (not orphaned)", async () => { - // When one tool in a batch errors, its siblings — whose tool-call - // events were already yielded — must still receive a result, otherwise - // the tool-call IDs are orphaned in the chunks (no matching result) - // and the next LLM round-trip throws MissingToolResultsError. The - // tool-error handler breaks out of the stream loop WITHOUT executing - // the unresolved siblings inline; the normal manual-executor pass then - // runs them. Here `sibling_tool` is not a registered tool, so the - // executor returns an "Unknown tool" error result — completing the - // tool-call/tool-result pairing with `isError: true`. - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([ - { - type: "tool-call", - toolCallId: "tc_sibling", - toolName: "sibling_tool", - input: {}, - }, - { - type: "tool-error", - toolCallId: "tc_failed", - toolName: "failed_tool", - error: new Error("boom"), - }, - finishStop, - ]), - ); - - const agent = new Agent(makeConfig()); - const events: AgentEvent[] = []; - for await (const event of agent.run("trigger")) { - events.push(event); - } - - const toolResults = events.filter((e) => e.type === "tool-result"); - // One for the failed tool, one for the sibling resolved by the executor. - const siblingResult = toolResults.find( - (e) => "toolResult" in e && e.toolResult.toolCallId === "tc_sibling", - ); - expect(siblingResult).toBeDefined(); - expect(siblingResult).toMatchObject({ - type: "tool-result", - toolResult: { toolCallId: "tc_sibling", isError: true }, - }); - const siblingMsg = - siblingResult && "toolResult" in siblingResult ? siblingResult.toolResult.result : ""; - expect((siblingMsg as string).includes("sibling_tool")).toBe(true); - - // Status completes to idle (the turn continued, not aborted). - const lastStatus = events.filter((e) => e.type === "status").at(-1); - expect(lastStatus).toMatchObject({ type: "status", status: "idle" }); - }); - - it("abort stream event surfaces as an error event and stops the turn", async () => { - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([ - { type: "text-delta", id: "t0", text: "starting..." }, - { type: "abort", reason: "user cancelled" }, - ]), - ); - - const agent = new Agent(makeConfig()); - const events: AgentEvent[] = []; - for await (const event of agent.run("hi")) { - events.push(event); - } - - const errEvent = events.find((e) => e.type === "error"); - expect(errEvent).toBeDefined(); - const errMsg = errEvent && "error" in errEvent ? errEvent.error : ""; - expect( - (errMsg as string).toLowerCase().includes("aborted") || - (errMsg as string).includes("user cancelled"), - ).toBe(true); - - const lastStatus = events.filter((e) => e.type === "status").at(-1); - expect(lastStatus).toMatchObject({ type: "status", status: "error" }); - }); - - it("openai-compatible reasoning round-trip: ThinkingChunk -> providerOptions.openaiCompatible.reasoning_content (DeepSeek scenario)", async () => { - // Reproducer for the "reasoning_content must be passed back" error - // from DeepSeek via OpenCode Go. - // - // applyOpenAICompatibleReasoningNormalisation strips the - // `{ type: "reasoning", text }` parts and lifts the concatenated - // text into `providerOptions.openaiCompatible.reasoning_content`. - // The v6 SDK provider serializes the message-level - // `providerOptions.openaiCompatible.*` into the wire `assistant` - // message via its `metadata` spread (line 247 of the SDK dist). - // This route emits `reasoning_content` regardless of empty/non- - // empty text — which is what DeepSeek requires. - const agent = new Agent( - makeConfig({ - model: "deepseek-v4-pro", - // no provider field → default openai-compatible path - }), - ); - agent.messages.push( - { role: "user", chunks: [{ type: "text", text: "ping" }] }, - { - role: "assistant", - chunks: [ - { type: "thinking", text: "let me reason about this" }, - { type: "text", text: "ok done" }, - ], - }, - ); - - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "ok" }, finishStop]), - ); - - for await (const _ of agent.run("follow-up")) { - /* consume */ - } - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ - role: string; - content: unknown; - providerOptions?: { openaiCompatible?: { reasoning_content?: string } }; - }>; - const assistantMsg = messages.find((m) => m.role === "assistant"); - if (!assistantMsg || !Array.isArray(assistantMsg.content)) { - throw new Error("expected structured assistant content"); - } - const content = assistantMsg.content as Array<Record<string, unknown>>; - - // Reasoning parts have been stripped from content (lifted into - // providerOptions instead). - expect(content.find((p) => p.type === "reasoning")).toBeUndefined(); - - // reasoning_content is set on providerOptions.openaiCompatible. - // This is what reaches DeepSeek and prevents the rejection. - expect(assistantMsg.providerOptions?.openaiCompatible?.reasoning_content).toBe( - "let me reason about this", - ); - - // The text part still survives in content. - const textPart = content.find((p) => p.type === "text"); - expect(textPart).toMatchObject({ type: "text", text: "ok done" }); - - // And critically, the message must NOT carry a providerMetadata - // key (the v4-era misnamed key). v3 prompts use `providerOptions`. - expect((assistantMsg as Record<string, unknown>).providerMetadata).toBeUndefined(); - }); - - it("openai-compatible empty-reasoning edge case: forces reasoning_content='' so DeepSeek does not reject", async () => { - // DeepSeek will reject the follow-up turn with "must be passed - // back" if a prior assistant turn emitted reasoning AND the - // follow-up doesn't include `reasoning_content` (even empty). - // The v6 SDK's content-side path skips emission when reasoning - // is empty (see `dist/index.mjs:245`); our normalisation routes - // it via providerOptions instead, which fires unconditionally. - const agent = new Agent(makeConfig({ model: "deepseek-v4-pro" })); - agent.messages.push( - { role: "user", chunks: [{ type: "text", text: "ping" }] }, - { - role: "assistant", - chunks: [ - // Empty thinking — captured but produced no actual text. - { type: "thinking", text: "" }, - { type: "text", text: "answer" }, - ], - }, - ); - - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "ok" }, finishStop]), - ); - - for await (const _ of agent.run("follow-up")) { - /* consume */ - } - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ - role: string; - content: unknown; - providerOptions?: { openaiCompatible?: { reasoning_content?: string } }; - }>; - const assistantMsg = messages.find((m) => m.role === "assistant"); - if (!assistantMsg) throw new Error("expected assistant message"); - - // The empty-string reasoning_content is explicitly set on - // providerOptions. (`""` is intentional and required — - // `assistantMsg.providerOptions?.openaiCompatible?.reasoning_content` - // must not be `undefined`.) - const rc = assistantMsg.providerOptions?.openaiCompatible?.reasoning_content; - expect(rc).toBeDefined(); - expect(rc).toBe(""); - }); - - it("openai-compatible normalisation does NOT run for messages without any reasoning parts", async () => { - // DeepSeek only requires `reasoning_content` AFTER a thinking - // turn. For purely-text assistant messages, we should leave - // providerOptions alone. - const agent = new Agent(makeConfig({ model: "deepseek-v4-pro" })); - agent.messages.push( - { role: "user", chunks: [{ type: "text", text: "hi" }] }, - { - role: "assistant", - chunks: [{ type: "text", text: "hello back" }], - }, - ); - - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "ok" }, finishStop]), - ); - - for await (const _ of agent.run("again")) { - /* consume */ - } - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ - role: string; - providerOptions?: { openaiCompatible?: { reasoning_content?: string } }; - }>; - const assistantMsg = messages.find((m) => m.role === "assistant"); - - // No reasoning chunks → no providerOptions injection. (May still - // be undefined entirely if nothing else set it.) - const rc = assistantMsg?.providerOptions?.openaiCompatible?.reasoning_content; - expect(rc).toBeUndefined(); - }); - - // ─── Prompt-caching: tool-result grouping & breakpoints (notes/claude-report.md) ── - - it("groups a turn's tool results into a SINGLE role:'tool' message (Root Cause 2)", async () => { - // The agent batches three distinct read_file calls in one step. The - // rebuilt ModelMessage[] must contain exactly ONE `role: "tool"` message - // holding all three results (not three separate tool messages). Per- - // result messages would strand the rolling cache breakpoints on the last - // two adjacent tool results, wasting a breakpoint. - const toolDef = { - name: "read_file", - description: "reads a file", - parameters: z.object({ path: z.string() }), - execute: async (args: Record<string, unknown>) => `contents of ${String(args.path)}`, - }; - vi.mocked(streamText) - .mockReturnValueOnce( - makeMockStreamResult([ - { type: "tool-call", toolCallId: "b1", toolName: "read_file", input: { path: "a.txt" } }, - { type: "tool-call", toolCallId: "b2", toolName: "read_file", input: { path: "b.txt" } }, - { type: "tool-call", toolCallId: "b3", toolName: "read_file", input: { path: "c.txt" } }, - finishToolCalls, - ]), - ) - .mockReturnValueOnce( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "done" }, finishStop]), - ); - - const agent = new Agent(makeConfig({ provider: "opencode-anthropic", tools: [toolDef] })); - for await (const _ of agent.run("read three files")) { - /* consume */ - } - - // Inspect the step-1 request (sent after the batch executed) — its tail - // is the assistant tool-calls + the grouped tool results. - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ role: string; content: unknown }>; - - const toolMsgs = messages.filter((m) => m.role === "tool"); - expect(toolMsgs).toHaveLength(1); - const toolContent = toolMsgs[0]?.content as Array<Record<string, unknown>>; - expect(toolContent).toHaveLength(3); - expect(toolContent.every((p) => p.type === "tool-result")).toBe(true); - // IDs preserved per result. - expect(toolContent.map((p) => p.toolCallId)).toEqual(["b1", "b2", "b3"]); - }); - - it("places cache breakpoints on [assistant, grouped-tool], not adjacent tool results (Root Cause 2)", async () => { - // With grouping, the last two non-system messages of a mid-turn request - // are [assistant(tool-calls), tool(all results)]. Both — plus the system - // message — must carry an ephemeral cacheControl marker. The pre-fix bug - // put both rolling breakpoints on two adjacent tool-result messages and - // never marked the assistant turn. - const toolDef = { - name: "read_file", - description: "reads a file", - parameters: z.object({ path: z.string() }), - execute: async (args: Record<string, unknown>) => `contents of ${String(args.path)}`, - }; - vi.mocked(streamText) - .mockReturnValueOnce( - makeMockStreamResult([ - { type: "tool-call", toolCallId: "c1", toolName: "read_file", input: { path: "a.txt" } }, - { type: "tool-call", toolCallId: "c2", toolName: "read_file", input: { path: "b.txt" } }, - { type: "tool-call", toolCallId: "c3", toolName: "read_file", input: { path: "c.txt" } }, - finishToolCalls, - ]), - ) - .mockReturnValueOnce( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "done" }, finishStop]), - ); - - const agent = new Agent(makeConfig({ provider: "opencode-anthropic", tools: [toolDef] })); - for await (const _ of agent.run("read three files")) { - /* consume */ - } - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ - role: string; - content: unknown; - providerOptions?: { anthropic?: { cacheControl?: { type?: string } } }; - }>; - - const isCached = (m?: { - providerOptions?: { anthropic?: { cacheControl?: { type?: string } } }; - }) => m?.providerOptions?.anthropic?.cacheControl?.type === "ephemeral"; - - // Exactly one tool message — no adjacent tool-result breakpoints. - expect(messages.filter((m) => m.role === "tool")).toHaveLength(1); - - const systemMsg = messages.find((m) => m.role === "system"); - const assistantMsg = messages.find((m) => m.role === "assistant"); - const toolMsg = messages.find((m) => m.role === "tool"); - - expect(isCached(systemMsg)).toBe(true); - expect(isCached(assistantMsg)).toBe(true); - expect(isCached(toolMsg)).toBe(true); - }); - - it("does NOT attach cacheControl for the openai-compatible (non-Anthropic) path", async () => { - // Sanity check: caching markers are Anthropic-only. The OpenAI-compatible - // endpoints do automatic server-side prefix caching and reject explicit - // cache_control, so no marker should be attached. - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "hi" }, finishStop]), - ); - const agent = new Agent(makeConfig()); // default → openai-compatible - for await (const _ of agent.run("hello")) { - /* consume */ - } - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ - role: string; - providerOptions?: { anthropic?: { cacheControl?: unknown } }; - }>; - for (const m of messages) { - expect(m.providerOptions?.anthropic?.cacheControl).toBeUndefined(); - } - }); - - // ─── Tool-call dedup (notes/tool-runner-duplication-incident.md) ───────────────── - - it("deduplicates byte-identical tool calls within a single batch", async () => { - // Claude can degenerate and emit the same tool call (same name + args) - // many times in one batch. Each copy keeps its own id (and still gets its - // own result), but the tool must execute only ONCE — re-running identical - // idempotent reads wastes time/money and floods the context. - let execCount = 0; - const toolDef = { - name: "read_file", - description: "reads a file", - parameters: z.object({ path: z.string() }), - execute: async (args: Record<string, unknown>) => { - execCount++; - return `contents of ${String(args.path)}`; - }, - }; - vi.mocked(streamText) - .mockReturnValueOnce( - makeMockStreamResult([ - { - type: "tool-call", - toolCallId: "d1", - toolName: "read_file", - input: { path: "package.json" }, - }, - { - type: "tool-call", - toolCallId: "d2", - toolName: "read_file", - input: { path: "package.json" }, - }, - { - type: "tool-call", - toolCallId: "d3", - toolName: "read_file", - input: { path: "package.json" }, - }, - finishToolCalls, - ]), - ) - .mockReturnValueOnce( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "done" }, finishStop]), - ); - - const agent = new Agent(makeConfig({ tools: [toolDef] })); - const events: AgentEvent[] = []; - for await (const e of agent.run("read it thrice")) { - events.push(e); - } - - // Executed exactly once despite three identical calls. - expect(execCount).toBe(1); - - // Every call id still received its own result, all with identical content. - const results = events.filter( - (e): e is Extract<AgentEvent, { type: "tool-result" }> => e.type === "tool-result", - ); - expect(results).toHaveLength(3); - expect(results.map((e) => e.toolResult.toolCallId).sort()).toEqual(["d1", "d2", "d3"]); - for (const r of results) { - expect(r.toolResult.result).toBe("contents of package.json"); - } - }); - - it("does NOT deduplicate tool calls with differing arguments", async () => { - // Dedup is keyed on name + serialized arguments. Distinct args must each - // execute — only byte-identical calls collapse. - let execCount = 0; - const toolDef = { - name: "read_file", - description: "reads a file", - parameters: z.object({ path: z.string() }), - execute: async (args: Record<string, unknown>) => { - execCount++; - return `contents of ${String(args.path)}`; - }, - }; - vi.mocked(streamText) - .mockReturnValueOnce( - makeMockStreamResult([ - { type: "tool-call", toolCallId: "e1", toolName: "read_file", input: { path: "a.txt" } }, - { type: "tool-call", toolCallId: "e2", toolName: "read_file", input: { path: "b.txt" } }, - { type: "tool-call", toolCallId: "e3", toolName: "read_file", input: { path: "a.txt" } }, - finishToolCalls, - ]), - ) - .mockReturnValueOnce( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "done" }, finishStop]), - ); - - const agent = new Agent(makeConfig({ tools: [toolDef] })); - for await (const _ of agent.run("read a, b, a")) { - /* consume */ - } - - // a.txt + b.txt are distinct → two executions; the repeated a.txt reuses - // the first result. - expect(execCount).toBe(2); - }); - - // ─── Cache stability: per-step wire prefix is immutable ───────────────────── - - it("keeps earlier steps' wire messages byte-identical across requests (cache prefix is stable)", async () => { - // A 3-step tool turn. The messages for steps 0 and 1 must serialize - // identically in the step-2 request and the step-3 request — that - // byte-stability is what lets Anthropic's rolling prompt cache extend - // instead of re-writing the whole prefix every step (notes/cache-miss-report.md). - // Uses the openai-compatible provider so no cacheControl markers (which - // intentionally move each step) obscure the content comparison. - let n = 0; - // mock.calls accumulates across tests in this file — reset so our - // `calls.length` assertions count only this run's requests. - vi.mocked(streamText).mockClear(); - const toolDef = { - name: "read_file", - description: "reads a file", - parameters: z.object({ path: z.string() }), - execute: async (args: Record<string, unknown>) => `contents of ${String(args.path)}`, - }; - const toolStep = (id: string, path: string) => - makeMockStreamResult([ - { type: "reasoning-delta", id: `r${id}`, text: `thinking ${id}` }, - { type: "text-delta", id: `t${id}`, text: `step ${id}` }, - { type: "tool-call", toolCallId: id, toolName: "read_file", input: { path } }, - finishToolCalls, - ]); - vi.mocked(streamText).mockImplementation(() => { - n++; - if (n === 1) return toolStep("s0", "a.txt"); - if (n === 2) return toolStep("s1", "b.txt"); - if (n === 3) return toolStep("s2", "c.txt"); - return makeMockStreamResult([{ type: "text-delta", id: "tf", text: "done" }, finishStop]); - }); - - const agent = new Agent(makeConfig({ tools: [toolDef] })); - for await (const _ of agent.run("go")) { - /* consume */ - } - - // 4 streamText calls (steps 0..3). Compare the step-2 request (call idx 2) - // and step-3 request (call idx 3). - const calls = vi.mocked(streamText).mock.calls; - expect(calls.length).toBe(4); - const req2 = calls[2]?.[0]?.messages as unknown[]; - const req3 = calls[3]?.[0]?.messages as unknown[]; - - // Step-2 request = [system, user, a(s0), tool(s0), a(s1), tool(s1)] (6). - // Step-3 request appends a(s2), tool(s2). The shared 6-message prefix - // must be byte-identical. - expect(req2).toHaveLength(6); - expect(req3).toHaveLength(8); - expect(JSON.stringify(req3.slice(0, 6))).toBe(JSON.stringify(req2)); - - // And each step really is its own [assistant, tool] pair (not one merged - // assistant message with all tool calls bunched together). - const roles = (req3 as Array<{ role: string }>).map((m) => m.role); - expect(roles).toEqual([ - "system", - "user", - "assistant", - "tool", - "assistant", - "tool", - "assistant", - "tool", - ]); - }); - - // ─── Usage / cache-rate telemetry ────────────────────────────────────────── - - it("emits a usage event from the finish-step part with the cache read/write split", async () => { - // The per-step `usage` (with Anthropic's cache read/write split in - // `inputTokenDetails`) rides on the `finish-step` part — NOT the terminal - // `finish` part, which only carries the aggregate `totalUsage`. The agent - // re-emits it as a `usage` AgentEvent that powers the Cache Rate view. - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([ - { type: "text-delta", id: "t0", text: "hi" }, - { - type: "finish-step", - finishReason: "stop", - rawFinishReason: "stop", - usage: { - inputTokens: 1000, - outputTokens: 50, - inputTokenDetails: { - noCacheTokens: 200, - cacheReadTokens: 750, - cacheWriteTokens: 50, - }, - }, - }, - finishStop, - ]), - ); - - const agent = new Agent(makeConfig()); - const events: AgentEvent[] = []; - for await (const e of agent.run("hi")) { - events.push(e); - } - - const usageEvents = events.filter( - (e): e is Extract<AgentEvent, { type: "usage" }> => e.type === "usage", - ); - // Exactly one usage event (from finish-step) — the terminal `finish` - // part must NOT double-count. - expect(usageEvents).toHaveLength(1); - expect(usageEvents[0]?.usage).toEqual({ - inputTokens: 1000, - outputTokens: 50, - cacheReadTokens: 750, - cacheWriteTokens: 50, - }); - }); - - it("does NOT emit a usage event when no finish-step usage is present", async () => { - // `finishStop` (type `finish`, aggregate `totalUsage` only) must not - // trigger a usage event — and with no `finish-step` part there is no - // per-step usage to emit. - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "hi" }, finishStop]), - ); - - const agent = new Agent(makeConfig()); - const events: AgentEvent[] = []; - for await (const e of agent.run("hi")) { - events.push(e); - } - - expect(events.some((e) => e.type === "usage")).toBe(false); - }); -}); - -describe("anthropicThinkingProviderOptions — adaptive-thinking model detection", () => { - // Pure function: no provider construction, no streamText, no network I/O. - // Mirrors opencode's transform.ts detection — Opus 4.7+ AND Opus/Sonnet 4.6 - // are adaptive; only Opus 4.7+ needs display:"summarized" to surface thinking. - - it("Opus 4.8 → adaptive + display:summarized (the reported bug)", () => { - expect(anthropicThinkingProviderOptions("claude-opus-4-8", "max")).toEqual({ - thinking: { type: "adaptive", display: "summarized" }, - effort: "max", - }); - }); - - it("Opus 4.7 → adaptive + display:summarized (dash and dot id forms)", () => { - const expected = { thinking: { type: "adaptive", display: "summarized" }, effort: "high" }; - expect(anthropicThinkingProviderOptions("claude-opus-4-7", "high")).toEqual(expected); - expect(anthropicThinkingProviderOptions("claude-opus-4.7", "high")).toEqual(expected); - }); - - it("Sonnet 4.6 → adaptive WITHOUT display (dash and dot id forms)", () => { - const expected = { thinking: { type: "adaptive" }, effort: "medium" }; - expect(anthropicThinkingProviderOptions("claude-sonnet-4-6", "medium")).toEqual(expected); - expect(anthropicThinkingProviderOptions("claude-sonnet-4.6", "medium")).toEqual(expected); - }); - - it("Opus 4.6 → adaptive WITHOUT display", () => { - expect(anthropicThinkingProviderOptions("claude-opus-4-6", "high")).toEqual({ - thinking: { type: "adaptive" }, - effort: "high", - }); - }); - - it("older Claude (Opus 4.5, dated Sonnet) → classic enabled thinking", () => { - expect(anthropicThinkingProviderOptions("claude-opus-4-5", "max")).toEqual({ - thinking: { type: "enabled", budgetTokens: 31999 }, - }); - expect(anthropicThinkingProviderOptions("claude-sonnet-4-20250514", "high")).toEqual({ - thinking: { type: "enabled", budgetTokens: 16000 }, - }); - }); - - it("uses a version parse, not a hardcoded string (future Opus 4.9 is adaptive)", () => { - expect(anthropicThinkingProviderOptions("claude-opus-4-9", "high")).toEqual({ - thinking: { type: "adaptive", display: "summarized" }, - effort: "high", - }); - }); - - it("maps reasoning effort → budgetTokens for enabled (non-adaptive) models", () => { - const budget = (e: "low" | "medium" | "high" | "xhigh" | "max") => { - const opts = anthropicThinkingProviderOptions("claude-3-7-sonnet", e) as { - thinking: { type: "enabled"; budgetTokens: number }; - }; - return opts.thinking.budgetTokens; - }; - expect(budget("low")).toBe(2000); - expect(budget("medium")).toBe(5000); - expect(budget("high")).toBe(16000); - expect(budget("xhigh")).toBe(24000); - expect(budget("max")).toBe(31999); - }); - - it("xhigh budget sits strictly between high and max (ordering invariant)", () => { - const budget = (e: "high" | "xhigh" | "max") => { - const opts = anthropicThinkingProviderOptions("claude-3-7-sonnet", e) as { - thinking: { type: "enabled"; budgetTokens: number }; - }; - return opts.thinking.budgetTokens; - }; - expect(budget("high")).toBeLessThan(budget("xhigh")); - expect(budget("xhigh")).toBeLessThan(budget("max")); - }); - - it("forwards xhigh verbatim as the adaptive effort sibling (Opus 4.7+)", () => { - expect(anthropicThinkingProviderOptions("claude-opus-4-8", "xhigh")).toEqual({ - thinking: { type: "adaptive", display: "summarized" }, - effort: "xhigh", - }); - }); - - describe("multimodal user content", () => { - it("emits ordered text + image parts to the model when content is provided", async () => { - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "ok" }, finishStop]), - ); - - const agent = new Agent(makeConfig()); - for await (const _ of agent.run("here is image A: [image]", { - content: [ - { type: "text", text: "here is image A: " }, - { type: "attachment", mediaType: "image/png", data: "QQ==" }, - ], - })) { - // consume - } - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ role: string; content: unknown }>; - const userMsg = messages.find((m) => m.role === "user"); - expect(userMsg).toBeDefined(); - // Multimodal turn → content is an ordered parts array, not a string. - expect(Array.isArray(userMsg?.content)).toBe(true); - const parts = userMsg?.content as Array<Record<string, unknown>>; - expect(parts[0]).toMatchObject({ type: "text", text: "here is image A: " }); - expect(parts[1]).toMatchObject({ type: "image", mediaType: "image/png" }); - expect(String(parts[1]?.image)).toBe("data:image/png;base64,QQ=="); - }); - - it("emits a FilePart for a PDF attachment with its filename", async () => { - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "ok" }, finishStop]), - ); - - const agent = new Agent(makeConfig()); - for await (const _ of agent.run("see [pdf]", { - content: [ - { type: "text", text: "see " }, - { type: "attachment", mediaType: "application/pdf", data: "QQ==", name: "doc.pdf" }, - ], - })) { - // consume - } - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ role: string; content: unknown }>; - const userMsg = messages.find((m) => m.role === "user"); - const parts = userMsg?.content as Array<Record<string, unknown>>; - const filePart = parts.find((p) => p.type === "file"); - expect(filePart).toMatchObject({ - type: "file", - mediaType: "application/pdf", - filename: "doc.pdf", - }); - expect(String(filePart?.data)).toBe("data:application/pdf;base64,QQ=="); - }); - - it("persists the user turn as text only (no content) for history", async () => { - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "ok" }, finishStop]), - ); - - const agent = new Agent(makeConfig()); - for await (const _ of agent.run("look: [image]", { - content: [ - { type: "text", text: "look: " }, - { type: "attachment", mediaType: "image/png", data: "QQ==" }, - ], - })) { - // consume - } - - // The in-memory user message keeps the text chunk for the render/persist - // path; the ephemeral `content` rides alongside it but isn't a chunk. - const userMsg = agent.messages.find((m) => m.role === "user"); - expect(userMsg?.chunks).toEqual([{ type: "text", text: "look: [image]" }]); - }); - - it("falls back to a plain string when content has no attachment", async () => { - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "ok" }, finishStop]), - ); - - const agent = new Agent(makeConfig()); - for await (const _ of agent.run("plain text", { - content: [{ type: "text", text: "plain text" }], - })) { - // consume - } - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ role: string; content: unknown }>; - const userMsg = messages.find((m) => m.role === "user"); - // No attachment → plain string content (byte-identical to text-only path). - expect(typeof userMsg?.content).toBe("string"); - expect(userMsg?.content).toBe("plain text"); - }); - }); - - describe("warmCache (prompt-cache warming replay)", () => { - function makeWarmStream(usage: { - inputTokens: number; - cacheReadTokens: number; - cacheWriteTokens: number; - }) { - return makeMockStreamResult([ - { type: "text-delta", id: "t0", text: "." }, - { - type: "finish-step", - finishReason: "stop", - rawFinishReason: "stop", - usage: { - inputTokens: usage.inputTokens, - outputTokens: 1, - inputTokenDetails: { - noCacheTokens: usage.inputTokens - usage.cacheReadTokens - usage.cacheWriteTokens, - cacheReadTokens: usage.cacheReadTokens, - cacheWriteTokens: usage.cacheWriteTokens, - }, - }, - }, - finishStop, - ]); - } - - const history = [ - { role: "user" as const, chunks: [{ type: "text" as const, text: "hello" }] }, - { role: "assistant" as const, chunks: [{ type: "text" as const, text: "hi there" }] }, - ]; - - it("returns the request usage (cache read/write split) without throwing", async () => { - vi.mocked(streamText).mockReturnValue( - makeWarmStream({ inputTokens: 1000, cacheReadTokens: 950, cacheWriteTokens: 0 }), - ); - const agent = new Agent(makeConfig({ provider: "anthropic" })); - const usage = await agent.warmCache(history); - expect(usage).toEqual({ - inputTokens: 1000, - outputTokens: 1, - cacheReadTokens: 950, - cacheWriteTokens: 0, - }); - }); - - it("appends a single trivial throwaway user turn at the END of the history", async () => { - vi.mocked(streamText).mockReturnValue( - makeWarmStream({ inputTokens: 10, cacheReadTokens: 5, cacheWriteTokens: 0 }), - ); - const agent = new Agent(makeConfig({ provider: "anthropic" })); - await agent.warmCache(history); - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - const messages = callArgs?.messages as Array<{ role: string; content: unknown }>; - // system + 2 history messages + 1 throwaway user turn. - expect(messages[0]?.role).toBe("system"); - const last = messages.at(-1); - expect(last?.role).toBe("user"); - // The throwaway turn's text must be the trivial probe. - const lastText = JSON.stringify(last?.content); - expect(lastText).toContain("reply with just a ."); - // Exactly one extra user turn beyond the genuine history's single user msg. - const userMsgs = messages.filter((m) => m.role === "user"); - expect(userMsgs).toHaveLength(2); - }); - - it("sends Anthropic cache_control breakpoints with the SAME toolChoice/thinking as a real turn", async () => { - // Anthropic keys the MESSAGE cache on `tool_choice` AND the extended- - // thinking parameters. If warming sent a different value than a real - // turn, it would warm a DIFFERENT message-cache bucket and the user's - // next real message would still miss. So warming MUST mirror run(): - // toolChoice "auto" + the thinking providerOptions for the effort. - vi.mocked(streamText).mockReturnValue( - makeWarmStream({ inputTokens: 10, cacheReadTokens: 5, cacheWriteTokens: 0 }), - ); - const agent = new Agent(makeConfig({ provider: "anthropic" })); - await agent.warmCache(history); - - const callArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - expect(callArgs?.toolChoice).toBe("auto"); - // Thinking providerOptions present (effort defaults to "max"). - expect(callArgs?.providerOptions?.anthropic).toBeDefined(); - const messages = callArgs?.messages as Array<{ - role: string; - providerOptions?: { anthropic?: { cacheControl?: unknown } }; - }>; - const hasBreakpoint = messages.some( - (m) => m.providerOptions?.anthropic?.cacheControl !== undefined, - ); - expect(hasBreakpoint).toBe(true); - }); - - it("warming and a real turn send IDENTICAL cache-affecting params (same bucket)", async () => { - // The core invariant of the whole feature: warmCache() and run() must - // produce the same toolChoice + thinking providerOptions + maxOutputTokens - // so the warming replay refreshes the EXACT cache the next real message - // reads. Drive both and compare the cache-key inputs streamText receives. - const cfg = makeConfig({ provider: "anthropic" }); - - // 1) Real turn for the same history + the probe text as the user msg. - const realAgent = new Agent(cfg); - realAgent.messages.push(...history.map((m) => ({ ...m }))); - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "text-delta", id: "t0", text: "." }, finishStop]), - ); - for await (const _ of realAgent.run("reply with just a .")) { - // consume - } - const realArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - - // 2) Warming replay for the same history. - const warmAgent = new Agent(cfg); - vi.mocked(streamText).mockReturnValue( - makeWarmStream({ inputTokens: 10, cacheReadTokens: 5, cacheWriteTokens: 0 }), - ); - await warmAgent.warmCache(history); - const warmArgs = vi.mocked(streamText).mock.calls.at(-1)?.[0]; - - // The cache-affecting parameters must be byte-identical. - expect(warmArgs?.toolChoice).toEqual(realArgs?.toolChoice); - expect(warmArgs?.maxOutputTokens).toEqual(realArgs?.maxOutputTokens); - expect(warmArgs?.providerOptions).toEqual(realArgs?.providerOptions); - }); - - it("does NOT mutate the agent's own message history", async () => { - vi.mocked(streamText).mockReturnValue( - makeWarmStream({ inputTokens: 10, cacheReadTokens: 5, cacheWriteTokens: 0 }), - ); - const agent = new Agent(makeConfig({ provider: "anthropic" })); - expect(agent.messages).toHaveLength(0); - await agent.warmCache(history); - // warmCache takes history as an argument and never touches `this.messages`. - expect(agent.messages).toHaveLength(0); - // And it must not have flipped the agent into a running state. - expect(agent.status).toBe("idle"); - }); - - it("throws a formatted error when the stream errors", async () => { - vi.mocked(streamText).mockReturnValue( - makeMockStreamResult([{ type: "error", error: new Error("boom") }]), - ); - const agent = new Agent(makeConfig({ provider: "anthropic" })); - await expect(agent.warmCache(history)).rejects.toThrow(/boom/); - }); - }); -}); diff --git a/packages/core/tests/agents/loader.test.ts b/packages/core/tests/agents/loader.test.ts deleted file mode 100644 index a223a4f..0000000 --- a/packages/core/tests/agents/loader.test.ts +++ /dev/null @@ -1,233 +0,0 @@ -import * as fs from "node:fs"; -import * as os from "node:os"; -import * as path from "node:path"; -import { afterEach, beforeEach, describe, expect, it } from "vitest"; -import { - expandAgentToolNames, - getAgentDirPaths, - loadAgent, - saveAgent, -} from "../../src/agents/loader.js"; - -describe("expandAgentToolNames", () => { - it("expands 'read' into the granular read tools", () => { - const out = expandAgentToolNames(["read"]); - expect(out).toContain("read_file"); - expect(out).toContain("read_file_slice"); - expect(out).toContain("list_files"); - }); - - it("expands 'edit' into write_file", () => { - const out = expandAgentToolNames(["edit"]); - expect(out).toContain("write_file"); - }); - - it("expands 'bash' into run_shell", () => { - const out = expandAgentToolNames(["bash"]); - expect(out).toContain("run_shell"); - }); - - it("passes through the tab tools as independent names (no tab_comm group)", () => { - const out = expandAgentToolNames(["send_to_tab", "read_tab"]); - expect(out).toContain("send_to_tab"); - expect(out).toContain("read_tab"); - // Granting only one must not pull in the other. - const onlySend = expandAgentToolNames(["send_to_tab"]); - expect(onlySend).toContain("send_to_tab"); - expect(onlySend).not.toContain("read_tab"); - }); - - it("passes through non-group tool names unchanged", () => { - const out = expandAgentToolNames([ - "summon", - "retrieve", - "web_search", - "youtube_transcribe", - "search_code", - "send_to_tab", - "read_tab", - ]); - expect(out).toEqual( - expect.arrayContaining([ - "summon", - "retrieve", - "web_search", - "youtube_transcribe", - "search_code", - "send_to_tab", - "read_tab", - ]), - ); - }); - - it("always includes 'todo' even when not requested", () => { - expect(expandAgentToolNames([])).toContain("todo"); - expect(expandAgentToolNames(["read"])).toContain("todo"); - expect(expandAgentToolNames(["summon"])).toContain("todo"); - }); - - it("deduplicates when groups overlap with explicit names", () => { - const out = expandAgentToolNames(["read", "read_file"]); - // Each name should appear at most once - const counts = new Map<string, number>(); - for (const t of out) counts.set(t, (counts.get(t) ?? 0) + 1); - for (const [, c] of counts) expect(c).toBe(1); - }); -}); - -describe("getAgentDirPaths", () => { - it("returns just the global dir when no projectDir is supplied", () => { - const paths = getAgentDirPaths(); - expect(paths).toHaveLength(1); - expect(paths[0]).toContain(".config/dispatch/agents"); - }); - - it("appends the project-scoped dir when projectDir is supplied", () => { - const paths = getAgentDirPaths("/some/project"); - expect(paths).toHaveLength(2); - expect(paths[1]).toBe("/some/project/.dispatch/agents"); - }); -}); - -describe("loadAgent — project-scoped sandbox", () => { - // `GLOBAL_AGENTS_DIR` is captured at module load via `os.homedir()` - // and can't be redirected at runtime. The project-scoped path, - // however, is computed per-call from the `projectDir` argument, so - // we exercise that branch instead. This is also the more common - // real-world case (per-project agent definitions). - let tmpProject: string; - - beforeEach(() => { - tmpProject = fs.mkdtempSync(path.join(os.tmpdir(), "dispatch-loader-test-")); - }); - - afterEach(() => { - fs.rmSync(tmpProject, { recursive: true, force: true }); - }); - - function writeAgentToml(slug: string, body: string): void { - const agentsDir = path.join(tmpProject, ".dispatch", "agents"); - fs.mkdirSync(agentsDir, { recursive: true }); - fs.writeFileSync(path.join(agentsDir, `${slug}.toml`), body, "utf-8"); - } - - // Uses a slug unlikely to collide with anything the user might - // already have in ~/.config/dispatch/agents. `loadAgent` returns - // the FIRST match it finds across all scanned directories, and - // the global scope is scanned before the project scope — a slug - // that exists in both would resolve to the global one (which is - // real, not under our control). The "z-dispatch-test-*" prefix - // gives this fixture exclusive ownership of the slug. - const TEST_SLUG = "z-dispatch-test-fixture"; - - it("returns null for an unknown slug within the project scope", () => { - const agent = loadAgent("z-dispatch-test-does-not-exist", tmpProject); - expect(agent).toBeNull(); - }); - - it("loads a TOML definition written to the project's .dispatch/agents", () => { - writeAgentToml( - TEST_SLUG, - [ - 'name = "Fixture"', - 'description = "Sandbox fixture for loadAgent test."', - "skills = []", - 'tools = ["read", "bash"]', - "is_subagent = true", - "", - "[[models]]", - 'key_id = "opencode-1"', - 'model_id = "deepseek-v4-flash"', - "", - ].join("\n"), - ); - - const agent = loadAgent(TEST_SLUG, tmpProject); - expect(agent).not.toBeNull(); - expect(agent?.slug).toBe(TEST_SLUG); - expect(agent?.name).toBe("Fixture"); - expect(agent?.tools).toEqual(["read", "bash"]); - expect(agent?.is_subagent).toBe(true); - expect(agent?.models).toEqual([{ key_id: "opencode-1", model_id: "deepseek-v4-flash" }]); - expect(agent?.scope).toBe(tmpProject); - }); - - it("parses a per-model effort when it is a recognised level", () => { - writeAgentToml( - TEST_SLUG, - [ - 'name = "Fixture"', - "skills = []", - 'tools = ["read"]', - "", - "[[models]]", - 'key_id = "opencode-1"', - 'model_id = "deepseek-v4-flash"', - 'effort = "low"', - "", - "[[models]]", - 'key_id = "claude-max"', - 'model_id = "claude-opus-4-8"', - 'effort = "xhigh"', - "", - ].join("\n"), - ); - - const agent = loadAgent(TEST_SLUG, tmpProject); - expect(agent?.models).toEqual([ - { key_id: "opencode-1", model_id: "deepseek-v4-flash", effort: "low" }, - { key_id: "claude-max", model_id: "claude-opus-4-8", effort: "xhigh" }, - ]); - }); - - it("drops an invalid effort value so the call site falls back to the default", () => { - writeAgentToml( - TEST_SLUG, - [ - 'name = "Fixture"', - "skills = []", - 'tools = ["read"]', - "", - "[[models]]", - 'key_id = "opencode-1"', - 'model_id = "deepseek-v4-flash"', - 'effort = "turbo"', - "", - ].join("\n"), - ); - - const agent = loadAgent(TEST_SLUG, tmpProject); - expect(agent?.models).toEqual([{ key_id: "opencode-1", model_id: "deepseek-v4-flash" }]); - expect(agent?.models[0]).not.toHaveProperty("effort"); - }); - - it("round-trips effort through saveAgent → loadAgent", () => { - saveAgent({ - name: "Fixture", - description: "", - skills: [], - tools: ["read"], - models: [ - { key_id: "opencode-1", model_id: "deepseek-v4-flash", effort: "medium" }, - { key_id: "claude-max", model_id: "claude-opus-4-8" }, - ], - scope: tmpProject, - slug: TEST_SLUG, - }); - - const agent = loadAgent(TEST_SLUG, tmpProject); - expect(agent?.models).toEqual([ - { key_id: "opencode-1", model_id: "deepseek-v4-flash", effort: "medium" }, - { key_id: "claude-max", model_id: "claude-opus-4-8" }, - ]); - }); - - it("sanitizes the slug so path traversal can't reach outside the agents dir", () => { - // Even if a caller passes something gnarly, the lookup is by - // sanitized slug — no file outside the configured dirs should - // ever be opened. The sanitized form ("etc-passwd") obviously - // doesn't exist in the temp project, so the result is null. - const agent = loadAgent("../../../etc/passwd", tmpProject); - expect(agent).toBeNull(); - }); -}); diff --git a/packages/core/tests/chunks/append.test.ts b/packages/core/tests/chunks/append.test.ts deleted file mode 100644 index dc277d2..0000000 --- a/packages/core/tests/chunks/append.test.ts +++ /dev/null @@ -1,534 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { appendEventToChunks, applySystemEvent } from "../../src/chunks/append.js"; -import type { AgentEvent, ChatMessage, Chunk } from "../../src/types/index.js"; - -// ─── helpers ───────────────────────────────────────────────────── - -const td = (delta: string): AgentEvent => ({ type: "text-delta", delta }); -const rd = (delta: string): AgentEvent => ({ type: "reasoning-delta", delta }); -const re = (metadata?: Record<string, unknown>): AgentEvent => ({ - type: "reasoning-end", - ...(metadata !== undefined ? { metadata } : {}), -}); -const tc = (id: string, name = "fake_tool", args: Record<string, unknown> = {}): AgentEvent => ({ - type: "tool-call", - toolCall: { id, name, arguments: args }, -}); -const tr = (toolCallId: string, result: string, isError = false): AgentEvent => ({ - type: "tool-result", - toolResult: { toolCallId, toolName: "fake_tool", result, isError }, -}); -const so = (data: string, stream: "stdout" | "stderr" = "stdout"): AgentEvent => ({ - type: "shell-output", - data, - stream, -}); -const err = (error: string, statusCode?: number): AgentEvent => ({ - type: "error", - error, - ...(statusCode !== undefined ? { statusCode } : {}), -}); -const notice = (message: string): AgentEvent => ({ type: "notice", message }); -const modelChanged = (keyId: string, modelId: string): AgentEvent => ({ - type: "model-changed", - keyId, - modelId, -}); -const configReload: AgentEvent = { type: "config-reload" }; - -function run(events: AgentEvent[]): Chunk[] { - const chunks: Chunk[] = []; - for (const e of events) appendEventToChunks(chunks, e); - return chunks; -} - -// ─── Required cases from the plan ──────────────────────────────── - -describe("appendEventToChunks — required cases from plan", () => { - it("empty chunks + text-delta → one text chunk with the delta", () => { - const chunks = run([td("Hello")]); - expect(chunks).toEqual([{ type: "text", text: "Hello" }]); - }); - - it("two consecutive text-deltas → one text chunk with concatenated text", () => { - const chunks = run([td("Hello, "), td("world!")]); - expect(chunks).toEqual([{ type: "text", text: "Hello, world!" }]); - }); - - it("text-delta then reasoning-delta → two chunks (text, thinking)", () => { - const chunks = run([td("ans: 42"), rd("I should explain")]); - expect(chunks).toEqual([ - { type: "text", text: "ans: 42" }, - { type: "thinking", text: "I should explain" }, - ]); - }); - - it("text-delta then tool-call → two chunks (text, tool-batch with one entry)", () => { - const chunks = run([td("Looking..."), tc("t1", "read_file", { path: "x" })]); - expect(chunks).toEqual([ - { type: "text", text: "Looking..." }, - { - type: "tool-batch", - calls: [{ id: "t1", name: "read_file", arguments: { path: "x" } }], - }, - ]); - }); - - it("two consecutive tool-calls → one tool-batch with two entries", () => { - const chunks = run([tc("t1", "read_file"), tc("t2", "list_files")]); - expect(chunks).toHaveLength(1); - expect(chunks[0]).toMatchObject({ - type: "tool-batch", - calls: [ - { id: "t1", name: "read_file" }, - { id: "t2", name: "list_files" }, - ], - }); - }); - - it("tool-call then tool-call then text → two chunks (tool-batch with 2 entries, text)", () => { - const chunks = run([tc("t1"), tc("t2"), td("done")]); - expect(chunks).toHaveLength(2); - expect(chunks[0]).toMatchObject({ - type: "tool-batch", - calls: [{ id: "t1" }, { id: "t2" }], - }); - expect(chunks[1]).toEqual({ type: "text", text: "done" }); - }); - - it("tool-result arrives → updates matching tool-call entry in the latest tool-batch chunk by id", () => { - const chunks = run([ - tc("t1"), - tc("t2"), - tr("t1", "first-result"), - tr("t2", "second-result", true), - ]); - expect(chunks).toHaveLength(1); - const batch = chunks[0]; - expect(batch.type).toBe("tool-batch"); - if (batch.type !== "tool-batch") throw new Error("type guard"); - expect(batch.calls[0]).toMatchObject({ id: "t1", result: "first-result", isError: false }); - expect(batch.calls[1]).toMatchObject({ id: "t2", result: "second-result", isError: true }); - }); - - it("shell-output arrives → appends to the most recent tool-call's shellOutput", () => { - const chunks = run([ - tc("t1", "run_shell"), - so("hello\n", "stdout"), - so("world\n", "stdout"), - so("err!\n", "stderr"), - ]); - expect(chunks).toHaveLength(1); - const batch = chunks[0]; - if (batch.type !== "tool-batch") throw new Error("type guard"); - expect(batch.calls[0]?.shellOutput).toEqual({ - stdout: "hello\nworld\n", - stderr: "err!\n", - }); - }); - - it("error event → opens an error chunk; subsequent events go to new chunks", () => { - const chunks = run([td("partial..."), err("network failed", 503), td("recovery")]); - expect(chunks).toEqual([ - { type: "text", text: "partial..." }, - { type: "error", message: "network failed", statusCode: 503 }, - { type: "text", text: "recovery" }, - ]); - }); - - it("system event during text run → closes text, opens system, would re-open text on next text-delta", () => { - const chunks = run([td("first "), notice("model swap"), td("second")]); - expect(chunks).toEqual([ - { type: "text", text: "first " }, - { type: "system", kind: "notice", text: "model swap" }, - { type: "text", text: "second" }, - ]); - }); - - it("two consecutive system events → two separate system chunks (no coalescing)", () => { - const chunks = run([notice("a"), notice("b")]); - expect(chunks).toEqual([ - { type: "system", kind: "notice", text: "a" }, - { type: "system", kind: "notice", text: "b" }, - ]); - }); - - it("interleaved think → text → think → tool → think → text → 6 chunks in order", () => { - const chunks = run([ - rd("planning..."), - td("here goes:"), - rd("hmm, actually"), - tc("t1", "read_file"), - rd("ok now"), - td("and so..."), - ]); - expect(chunks.map((c) => c.type)).toEqual([ - "thinking", - "text", - "thinking", - "tool-batch", - "thinking", - "text", - ]); - expect(chunks[0]).toEqual({ type: "thinking", text: "planning..." }); - expect(chunks[1]).toEqual({ type: "text", text: "here goes:" }); - expect(chunks[2]).toEqual({ type: "thinking", text: "hmm, actually" }); - expect(chunks[3]).toMatchObject({ type: "tool-batch", calls: [{ id: "t1" }] }); - expect(chunks[4]).toEqual({ type: "thinking", text: "ok now" }); - expect(chunks[5]).toEqual({ type: "text", text: "and so..." }); - }); -}); - -// ─── Additional transition coverage ────────────────────────────── - -describe("appendEventToChunks — transition matrix", () => { - it("thinking → thinking coalesces", () => { - const chunks = run([rd("a"), rd("b")]); - expect(chunks).toEqual([{ type: "thinking", text: "ab" }]); - }); - - it("thinking → text opens a new text chunk", () => { - const chunks = run([rd("think"), td("speak")]); - expect(chunks).toEqual([ - { type: "thinking", text: "think" }, - { type: "text", text: "speak" }, - ]); - }); - - it("tool-batch → text opens a new text chunk", () => { - const chunks = run([tc("t1"), td("after tool")]); - expect(chunks).toHaveLength(2); - expect(chunks[1]).toEqual({ type: "text", text: "after tool" }); - }); - - it("text → reasoning-delta after a multi-delta text run still splits cleanly", () => { - const chunks = run([td("a"), td("b"), rd("x"), rd("y"), td("c")]); - expect(chunks).toEqual([ - { type: "text", text: "ab" }, - { type: "thinking", text: "xy" }, - { type: "text", text: "c" }, - ]); - }); - - it("error → text opens a fresh text chunk after the error", () => { - const chunks = run([err("boom"), td("recovered")]); - expect(chunks).toEqual([ - { type: "error", message: "boom" }, - { type: "text", text: "recovered" }, - ]); - }); - - it("two consecutive errors stay as two error chunks (no coalescing)", () => { - const chunks = run([err("first"), err("second", 429)]); - expect(chunks).toEqual([ - { type: "error", message: "first" }, - { type: "error", message: "second", statusCode: 429 }, - ]); - }); - - it("system → tool-call opens a new tool-batch (does not extend the system chunk)", () => { - const chunks = run([notice("info"), tc("t1")]); - expect(chunks).toHaveLength(2); - expect(chunks[1]).toMatchObject({ type: "tool-batch", calls: [{ id: "t1" }] }); - }); - - it("tool-result with no matching call is silently dropped", () => { - const chunks = run([td("hi"), tr("no-such-id", "ignored")]); - expect(chunks).toEqual([{ type: "text", text: "hi" }]); - }); - - it("shell-output with no tool-batch in scope is silently dropped", () => { - const chunks = run([td("hi"), so("orphan")]); - expect(chunks).toEqual([{ type: "text", text: "hi" }]); - }); - - it("tool-result for an earlier batch still updates the right call (results can arrive late)", () => { - // Order: tc -> td -> tc(new batch) -> tr(for first batch's id) - const chunks = run([ - tc("t1", "read_file"), - td("midstream text"), - tc("t2", "list_files"), - tr("t1", "late result for first"), - ]); - // Two tool-batches, separated by the text chunk. The result must land - // inside the FIRST batch (the one containing t1), not the most-recent. - expect(chunks.map((c) => c.type)).toEqual(["tool-batch", "text", "tool-batch"]); - const first = chunks[0]; - if (first?.type !== "tool-batch") throw new Error("type guard"); - expect(first.calls[0]).toMatchObject({ id: "t1", result: "late result for first" }); - const second = chunks[2]; - if (second?.type !== "tool-batch") throw new Error("type guard"); - // t2 in the second batch has no result yet. - expect(second.calls[0]?.result).toBeUndefined(); - }); - - it("shell-output goes to the most recent tool-batch's most recent entry, even with intervening chunks", () => { - // First batch's tool runs, emits output, then later a second batch starts and emits output. - const chunks = run([ - tc("t1", "run_shell"), - so("first-stdout\n"), - td("interlude"), - tc("t2", "run_shell"), - so("second-stdout\n"), - ]); - expect(chunks.map((c) => c.type)).toEqual(["tool-batch", "text", "tool-batch"]); - const first = chunks[0]; - const second = chunks[2]; - if (first?.type !== "tool-batch" || second?.type !== "tool-batch") { - throw new Error("type guard"); - } - expect(first.calls[0]?.shellOutput).toEqual({ stdout: "first-stdout\n", stderr: "" }); - expect(second.calls[0]?.shellOutput).toEqual({ stdout: "second-stdout\n", stderr: "" }); - }); - - it("model-changed event opens a system chunk with kind=model-changed", () => { - const chunks = run([modelChanged("anthropic-1", "claude-sonnet-4")]); - expect(chunks).toEqual([ - { - type: "system", - kind: "model-changed", - text: "Switched to claude-sonnet-4 (anthropic-1)", - }, - ]); - }); - - it("config-reload event opens a system chunk with kind=config-reload", () => { - const chunks = run([configReload]); - expect(chunks).toEqual([ - { type: "system", kind: "config-reload", text: "Configuration reloaded" }, - ]); - }); - - // ─── reasoning-end (v6 SDK metadata round-trip) ────────────────── - - it("reasoning-delta then reasoning-end seals the thinking chunk with metadata", () => { - const meta = { anthropic: { signature: "sig-1" } }; - const chunks = run([rd("plan"), re(meta)]); - expect(chunks).toEqual([{ type: "thinking", text: "plan", metadata: meta }]); - }); - - it("two reasoning-deltas then reasoning-end coalesces text and seals once", () => { - const meta = { anthropic: { signature: "abc" } }; - const chunks = run([rd("a"), rd("b"), re(meta)]); - expect(chunks).toEqual([{ type: "thinking", text: "ab", metadata: meta }]); - }); - - it("reasoning-delta → reasoning-end → reasoning-delta opens a NEW chunk", () => { - // Each Anthropic thinking content block gets its own metadata. - // Extending a sealed chunk would corrupt the text/metadata mapping. - const meta1 = { anthropic: { signature: "sig-1" } }; - const chunks = run([rd("first"), re(meta1), rd("second")]); - expect(chunks).toEqual([ - { type: "thinking", text: "first", metadata: meta1 }, - { type: "thinking", text: "second" }, - ]); - }); - - it("rd → re → rd → re produces two independently sealed chunks", () => { - const m1 = { anthropic: { signature: "s1" } }; - const m2 = { anthropic: { signature: "s2" } }; - const chunks = run([rd("first"), re(m1), rd("second"), re(m2)]); - expect(chunks).toEqual([ - { type: "thinking", text: "first", metadata: m1 }, - { type: "thinking", text: "second", metadata: m2 }, - ]); - }); - - it("orphan reasoning-end (no prior thinking chunk) is a no-op", () => { - const chunks = run([re({ anthropic: { signature: "orphan" } })]); - expect(chunks).toEqual([]); - }); - - it("reasoning-end after an already-sealed thinking chunk does NOT overwrite", () => { - const m1 = { anthropic: { signature: "first" } }; - const m2 = { anthropic: { signature: "second" } }; - const chunks = run([rd("a"), re(m1), re(m2)]); - expect(chunks).toEqual([{ type: "thinking", text: "a", metadata: m1 }]); - }); - - it("reasoning-end without metadata is a silent no-op (does not seal)", () => { - // v6 may emit reasoning-end with no providerMetadata for - // non-Anthropic providers. Don't seal those chunks — a subsequent - // reasoning-delta should continue extending. - const chunks = run([rd("hello"), re(), rd(" world")]); - expect(chunks).toEqual([{ type: "thinking", text: "hello world" }]); - }); - - it("re walks back across an intervening text chunk to seal the right thinking", () => { - // Defensive: even if a non-thinking chunk lands between the - // reasoning text and its end-event, the metadata still attaches - // to the unsealed thinking chunk. - const meta = { anthropic: { signature: "late" } }; - const chunks = run([rd("plan"), td("midstream"), re(meta)]); - expect(chunks).toEqual([ - { type: "thinking", text: "plan", metadata: meta }, - { type: "text", text: "midstream" }, - ]); - }); - - it("interleaved rd / re / tool-call / rd / re produces correct chunk sequence", () => { - // Anthropic's interleaved-thinking emits a thinking block, then - // a tool call, then another thinking block. Each thinking block - // gets its own metadata. - const m1 = { anthropic: { signature: "before-tool" } }; - const m2 = { anthropic: { signature: "after-tool" } }; - const chunks = run([ - rd("plan tool"), - re(m1), - tc("t1", "read_file"), - rd("plan response"), - re(m2), - ]); - expect(chunks.map((c) => c.type)).toEqual(["thinking", "tool-batch", "thinking"]); - expect(chunks[0]).toEqual({ - type: "thinking", - text: "plan tool", - metadata: m1, - }); - expect(chunks[2]).toEqual({ - type: "thinking", - text: "plan response", - metadata: m2, - }); - }); - - it("non-content events (status / done / task-list-update / message-queued etc.) are no-ops", () => { - const chunks = run([ - td("hello"), - { type: "status", status: "running" }, - { type: "task-list-update", tasks: [] }, - { - type: "tab-created", - id: "tab1", - title: "x", - keyId: null, - modelId: null, - parentTabId: null, - workingDirectory: null, - }, - { type: "message-queued", tabId: "t", messageId: "m", message: "queued" }, - { type: "message-consumed", tabId: "t", messageIds: ["m"] }, - { type: "message-cancelled", tabId: "t", messageId: "m" }, - { - type: "done", - message: { role: "assistant", chunks: [] }, - }, - td(" world"), - ]); - expect(chunks).toEqual([{ type: "text", text: "hello world" }]); - }); - - it("error chunk omits statusCode when not provided", () => { - const chunks = run([err("boom")]); - expect(chunks).toEqual([{ type: "error", message: "boom" }]); - // And no stray statusCode key: - expect(Object.hasOwn(chunks[0], "statusCode")).toBe(false); - }); - - it("tool-result updates isError=false correctly (default success path)", () => { - const chunks = run([tc("t1"), tr("t1", "ok", false)]); - const batch = chunks[0]; - if (batch?.type !== "tool-batch") throw new Error("type guard"); - expect(batch.calls[0]).toMatchObject({ result: "ok", isError: false }); - }); -}); - -// ─── applySystemEvent routing ──────────────────────────────────── - -describe("applySystemEvent", () => { - type Msg = { id: string; role: "user" | "assistant" | "system"; chunks: Chunk[] }; - - let counter = 0; - const idFactory = () => `gen-${++counter}`; - - it("creates a new role:system message when message list is empty", () => { - counter = 0; - const messages: Msg[] = []; - const result = applySystemEvent(messages, { kind: "notice", text: "hello" }, idFactory); - expect(result.messageId).toBe("gen-1"); - expect(messages).toEqual([ - { - id: "gen-1", - role: "system", - chunks: [{ type: "system", kind: "notice", text: "hello" }], - }, - ]); - }); - - it("creates a new role:system message when last message is user", () => { - counter = 0; - const messages: Msg[] = [{ id: "u1", role: "user", chunks: [{ type: "text", text: "hi" }] }]; - const result = applySystemEvent(messages, { kind: "model-changed", text: "swap" }, idFactory); - expect(result.messageId).toBe("gen-1"); - expect(messages).toHaveLength(2); - expect(messages[1]).toMatchObject({ - id: "gen-1", - role: "system", - chunks: [{ type: "system", kind: "model-changed", text: "swap" }], - }); - }); - - it("creates a new role:system message when last message is assistant", () => { - counter = 0; - const messages: Msg[] = [ - { id: "a1", role: "assistant", chunks: [{ type: "text", text: "done" }] }, - ]; - applySystemEvent(messages, { kind: "config-reload", text: "reloaded" }, idFactory); - expect(messages).toHaveLength(2); - expect(messages[1]?.role).toBe("system"); - }); - - it("appends a chunk to the existing system message when last message is role:system", () => { - counter = 0; - const messages: Msg[] = [ - { - id: "s1", - role: "system", - chunks: [{ type: "system", kind: "notice", text: "first" }], - }, - ]; - const result = applySystemEvent(messages, { kind: "notice", text: "second" }, idFactory); - expect(result.messageId).toBe("s1"); - expect(messages).toHaveLength(1); - expect(messages[0]?.chunks).toEqual([ - { type: "system", kind: "notice", text: "first" }, - { type: "system", kind: "notice", text: "second" }, - ]); - }); - - it("multiple consecutive calls accumulate in the same system message", () => { - counter = 0; - const messages: Msg[] = [{ id: "u1", role: "user", chunks: [{ type: "text", text: "hi" }] }]; - applySystemEvent(messages, { kind: "notice", text: "a" }, idFactory); - applySystemEvent(messages, { kind: "notice", text: "b" }, idFactory); - applySystemEvent(messages, { kind: "model-changed", text: "c" }, idFactory); - expect(messages).toHaveLength(2); - const sys = messages[1]; - expect(sys?.role).toBe("system"); - expect(sys?.chunks).toEqual([ - { type: "system", kind: "notice", text: "a" }, - { type: "system", kind: "notice", text: "b" }, - { type: "system", kind: "model-changed", text: "c" }, - ]); - }); - - it("returns the same messageId across appends to the same system message", () => { - counter = 0; - const messages: Msg[] = []; - const first = applySystemEvent(messages, { kind: "notice", text: "a" }, idFactory); - const second = applySystemEvent(messages, { kind: "notice", text: "b" }, idFactory); - expect(first.messageId).toBe(second.messageId); - }); - - it("works against the core ChatMessage shape (with id added by caller)", () => { - // Sanity: ChatMessage has {role, chunks}; the caller layers id on top. - // This test exists to prove the generic constraint doesn't reject the - // real persistence/in-memory shape we'll see in Phase 5. - counter = 0; - const messages: Array<ChatMessage & { id: string }> = []; - const result = applySystemEvent(messages, { kind: "cancelled", text: "user stop" }, idFactory); - expect(result.messageId).toBe("gen-1"); - expect(messages[0]?.role).toBe("system"); - expect(messages[0]?.chunks[0]).toMatchObject({ kind: "cancelled", text: "user stop" }); - }); -}); diff --git a/packages/core/tests/compaction/compaction.test.ts b/packages/core/tests/compaction/compaction.test.ts deleted file mode 100644 index d6edd59..0000000 --- a/packages/core/tests/compaction/compaction.test.ts +++ /dev/null @@ -1,194 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { - buildCompactionPrompt, - buildCompactionRequest, - buildSummaryTurnText, - DEFAULT_TAIL_TURNS, - extractPreviousSummary, - renderTranscript, - SUMMARY_MARKER, - SUMMARY_TEMPLATE, - selectHeadTail, -} from "../../src/compaction/index.js"; -import type { ChatMessage } from "../../src/types/index.js"; - -function user(text: string): ChatMessage { - return { role: "user", chunks: [{ type: "text", text }] }; -} -function assistant(text: string): ChatMessage { - return { role: "assistant", chunks: [{ type: "text", text }] }; -} - -describe("selectHeadTail", () => { - it("returns empty head when turns <= tailTurns (nothing to compact)", () => { - const msgs = [user("u1"), assistant("a1"), user("u2"), assistant("a2")]; - const { head, tail } = selectHeadTail(msgs, 2); - expect(head).toEqual([]); - expect(tail).toEqual(msgs); - }); - - it("keeps the last N turns verbatim and summarizes the rest", () => { - const msgs = [ - user("u1"), - assistant("a1"), - user("u2"), - assistant("a2"), - user("u3"), - assistant("a3"), - ]; - const { head, tail } = selectHeadTail(msgs, 2); - expect(tail[0]).toBe(msgs[2]); - expect(tail.at(-1)).toBe(msgs[5]); - expect(head).toEqual([msgs[0], msgs[1]]); - }); - - it("a turn includes trailing assistant/tool messages up to the next user", () => { - const msgs = [user("u1"), assistant("a1a"), assistant("a1b"), user("u2"), assistant("a2")]; - const { head, tail } = selectHeadTail(msgs, 1); - expect(head).toEqual([msgs[0], msgs[1], msgs[2]]); - expect(tail).toEqual([msgs[3], msgs[4]]); - }); - - it("tailTurns<=0 → everything is head", () => { - const msgs = [user("u1"), assistant("a1")]; - expect(selectHeadTail(msgs, 0)).toEqual({ head: msgs, tail: [] }); - }); - - it("defaults to DEFAULT_TAIL_TURNS", () => { - const msgs = [user("u1"), user("u2"), user("u3")]; - const def = selectHeadTail(msgs); - const explicit = selectHeadTail(msgs, DEFAULT_TAIL_TURNS); - expect(def).toEqual(explicit); - }); -}); - -describe("buildCompactionPrompt", () => { - it("creates a fresh-summary instruction without a previous summary", () => { - const p = buildCompactionPrompt({}); - expect(p).toContain("Create a new anchored summary"); - expect(p).toContain(SUMMARY_TEMPLATE); - expect(p).not.toContain("<previous-summary>"); - }); - - it("anchors on a previous summary when provided", () => { - const p = buildCompactionPrompt({ previousSummary: "## Goal\n- old" }); - expect(p).toContain("Update the anchored summary"); - expect(p).toContain("<previous-summary>"); - expect(p).toContain("## Goal\n- old"); - expect(p).toContain(SUMMARY_TEMPLATE); - }); -}); - -describe("extractPreviousSummary", () => { - it("returns undefined when the first user message is not a seeded summary", () => { - expect(extractPreviousSummary([user("hello"), assistant("hi")])).toBeUndefined(); - }); - - it("extracts the body of a seeded summary turn (marker stripped)", () => { - const seeded = user(buildSummaryTurnText("## Goal\n- build X")); - expect(extractPreviousSummary([seeded, assistant("ok")])).toBe("## Goal\n- build X"); - }); -}); - -describe("renderTranscript", () => { - it("renders user/assistant text blocks", () => { - const t = renderTranscript([user("hello"), assistant("hi there")]); - expect(t).toContain("## User\nhello"); - expect(t).toContain("## Assistant\nhi there"); - }); - - it("renders tool calls and caps long tool results", () => { - const big = "x".repeat(5000); - const msg: ChatMessage = { - role: "assistant", - chunks: [ - { - type: "tool-batch", - calls: [{ id: "c1", name: "read_file", arguments: { path: "a" }, result: big }], - }, - ], - }; - const t = renderTranscript([msg], 2000); - expect(t).toContain('[tool read_file {"path":"a"}]'); - expect(t).toContain("chars truncated for summary"); - expect(t.length).toBeLessThan(5000 + 200); - }); - - it("skips a seeded prior-summary user turn", () => { - const seeded = user(buildSummaryTurnText("## Goal\n- prior")); - const t = renderTranscript([seeded, assistant("work")]); - expect(t).not.toContain("prior"); - expect(t).toContain("## Assistant\nwork"); - }); - - it("omits thinking/error/system chunks", () => { - const msg: ChatMessage = { - role: "assistant", - chunks: [ - { type: "thinking", text: "secret reasoning" }, - { type: "text", text: "visible" }, - { type: "error", message: "boom" }, - ], - }; - const t = renderTranscript([msg]); - expect(t).toContain("visible"); - expect(t).not.toContain("secret reasoning"); - expect(t).not.toContain("boom"); - }); -}); - -describe("buildCompactionRequest", () => { - it("returns no prompt when there is nothing to compact", () => { - const msgs = [user("u1"), assistant("a1")]; - const req = buildCompactionRequest({ messages: msgs, tailTurns: 2 }); - expect(req.prompt).toBeUndefined(); - expect(req.head).toEqual([]); - expect(req.tail).toEqual(msgs); - }); - - it("builds a prompt with transcript + instruction and exposes head/tail", () => { - const msgs = [ - user("first task"), - assistant("did stuff"), - user("second"), - assistant("more"), - user("third"), - assistant("done"), - ]; - const req = buildCompactionRequest({ messages: msgs, tailTurns: 2 }); - expect(req.prompt).toBeDefined(); - expect(req.prompt).toContain("first task"); - expect(req.prompt).toContain("Create a new anchored summary"); - expect(req.tail[0]).toBe(msgs[2]); - expect(req.head[0]).toBe(msgs[0]); - }); - - it("anchors on a prior seeded summary", () => { - const msgs = [ - user(buildSummaryTurnText("## Goal\n- old goal")), - assistant("ack"), - user("new work"), - assistant("did new"), - user("more work"), - assistant("did more"), - ]; - const req = buildCompactionRequest({ messages: msgs, tailTurns: 2 }); - expect(req.previousSummary).toBe("## Goal\n- old goal"); - expect(req.prompt).toContain("Update the anchored summary"); - // head = [seeded-summary, "ack"]; seeded summary is skipped in the - // transcript, so "ack" represents the summarized head. "new work" lives - // in the preserved tail (last 2 turns), not the summary body. - expect(req.prompt).toContain("ack"); - expect( - req.tail.some((m) => m.chunks.some((c) => c.type === "text" && c.text === "new work")), - ).toBe(true); - }); -}); - -describe("buildSummaryTurnText", () => { - it("prefixes the marker so a later compaction can anchor", () => { - const seeded = buildSummaryTurnText("## Goal\n- x"); - expect(seeded.startsWith(SUMMARY_MARKER)).toBe(true); - expect(extractPreviousSummary([user(seeded)])).toBe("## Goal\n- x"); - }); -}); diff --git a/packages/core/tests/config/loader.test.ts b/packages/core/tests/config/loader.test.ts deleted file mode 100644 index 0d84d0b..0000000 --- a/packages/core/tests/config/loader.test.ts +++ /dev/null @@ -1,176 +0,0 @@ -import { mkdirSync, rmSync, writeFileSync } from "node:fs"; -import { homedir } from "node:os"; -import { join, sep } from "node:path"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { configToRuleset, loadConfig } from "../../src/config/loader.js"; - -const TMP = join("/tmp/opencode", "dispatch-config-test"); - -// Point the global config at a path that does not exist so these tests are -// hermetic — they must not pick up this machine's real -// ~/.config/dispatch/dispatch.toml. -const prevGlobal = process.env.DISPATCH_GLOBAL_CONFIG; - -beforeEach(() => { - mkdirSync(TMP, { recursive: true }); - process.env.DISPATCH_GLOBAL_CONFIG = join(TMP, "__no_such_global__.toml"); -}); - -afterEach(() => { - rmSync(TMP, { recursive: true, force: true }); - if (prevGlobal === undefined) delete process.env.DISPATCH_GLOBAL_CONFIG; - else process.env.DISPATCH_GLOBAL_CONFIG = prevGlobal; -}); - -function writeToml(content: string): void { - writeFileSync(join(TMP, "dispatch.toml"), content, "utf-8"); -} - -describe("loadConfig", () => { - it("returns empty permissions when dispatch.toml is missing", () => { - const config = loadConfig(TMP); - expect(config.permissions).toEqual({}); - }); - - it("parses simple string permissions", () => { - writeToml(`[permissions]\nread = "allow"\nedit = "deny"\n`); - const config = loadConfig(TMP); - expect(config.permissions.read).toBe("allow"); - expect(config.permissions.edit).toBe("deny"); - }); - - it("parses nested pattern permissions", () => { - writeToml(`[permissions.bash]\n"npm test" = "allow"\n"*" = "ask"\n`); - const config = loadConfig(TMP); - const bash = config.permissions.bash as Record<string, string>; - expect(bash["npm test"]).toBe("allow"); - expect(bash["*"]).toBe("ask"); - }); - - it("ignores comment lines", () => { - writeToml(`# this is a comment\n[permissions]\n# another comment\nread = "allow"\n`); - const config = loadConfig(TMP); - expect(config.permissions.read).toBe("allow"); - }); - - it("handles ~ expansion in nested keys", () => { - writeToml(`[permissions.read]\n"~/projects/*" = "allow"\n`); - const config = loadConfig(TMP); - const read = config.permissions.read as Record<string, string>; - expect(read["~/projects/*"]).toBe("allow"); - }); - - it("handles $HOME expansion in nested keys", () => { - writeToml(`[permissions.read]\n"$HOME/docs/*" = "allow"\n`); - const config = loadConfig(TMP); - const read = config.permissions.read as Record<string, string>; - expect(read["$HOME/docs/*"]).toBe("allow"); - }); - - it("parses quoted keys", () => { - writeToml(`[permissions.bash]\n"git commit *" = "allow"\n"rm *" = "deny"\n`); - const config = loadConfig(TMP); - const bash = config.permissions.bash as Record<string, string>; - expect(bash["git commit *"]).toBe("allow"); - expect(bash["rm *"]).toBe("deny"); - }); - - it("handles multiple permission groups", () => { - writeToml( - `[permissions]\nread = "allow"\n\n[permissions.edit]\n"*" = "ask"\n"src/**" = "allow"\n\n[permissions.bash]\n"npm test" = "allow"\n"*" = "ask"\n`, - ); - const config = loadConfig(TMP); - expect(config.permissions.read).toBe("allow"); - const edit = config.permissions.edit as Record<string, string>; - expect(edit["*"]).toBe("ask"); - expect(edit["src/**"]).toBe("allow"); - const bash = config.permissions.bash as Record<string, string>; - expect(bash["npm test"]).toBe("allow"); - expect(bash["*"]).toBe("ask"); - }); - - it("preserves # inside quoted string keys", () => { - writeToml(`[permissions.bash]\n"file#1" = "allow"\n`); - const config = loadConfig(TMP); - const bash = config.permissions.bash as Record<string, string>; - expect(bash["file#1"]).toBe("allow"); - }); - - it("strips inline comments on table headers", () => { - writeToml(`[permissions.bash] # scripts\n"*" = "allow"\n`); - const config = loadConfig(TMP); - const bash = config.permissions.bash as Record<string, string>; - expect(bash["*"]).toBe("allow"); - }); - - it("expands ~ with platform path separator", () => { - // Simulate a path using the OS separator - const pattern = `~${sep}projects${sep}*`; - writeToml(`[permissions.read]\n"${pattern}" = "allow"\n`); - const config = loadConfig(TMP); - const read = config.permissions.read as Record<string, string>; - expect(read[pattern]).toBe("allow"); - }); - - it("throws on TOML parse errors", () => { - writeToml("this is not valid TOML [[["); - expect(() => loadConfig(TMP)).toThrow(); - }); -}); - -describe("configToRuleset — new validations", () => { - it("falls back to ask and warns for invalid action in string value", () => { - const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); - const rules = configToRuleset({ permissions: { read: "allw" } }); - expect(rules[0]?.action).toBe("ask"); - expect(warn).toHaveBeenCalledWith(expect.stringContaining("allw")); - warn.mockRestore(); - }); - - it("falls back to ask and warns for invalid action in nested pattern", () => { - const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); - const rules = configToRuleset({ permissions: { bash: { "*": "INVALID" } } }); - expect(rules[0]?.action).toBe("ask"); - expect(warn).toHaveBeenCalledWith(expect.stringContaining("INVALID")); - warn.mockRestore(); - }); - - it("expands ~ with backslash separator in patterns", () => { - const home = homedir(); - // Force backslash path even on Linux to test the regex - const rules = configToRuleset({ permissions: { read: { "~\\foo\\*": "allow" } } }); - expect(rules[0]?.pattern).toBe(`${home}\\foo\\*`); - }); -}); - -describe("configToRuleset", () => { - it("produces a rule with pattern * for string value", () => { - const rules = configToRuleset({ permissions: { read: "allow" } }); - expect(rules).toEqual([{ permission: "read", pattern: "*", action: "allow" }]); - }); - - it("produces rules for each pattern in an object value", () => { - const rules = configToRuleset({ - permissions: { bash: { "npm test": "allow", "*": "ask" } }, - }); - expect(rules).toContainEqual({ permission: "bash", pattern: "npm test", action: "allow" }); - expect(rules).toContainEqual({ permission: "bash", pattern: "*", action: "ask" }); - }); - - it("expands ~ in patterns", () => { - const home = homedir(); - const rules = configToRuleset({ permissions: { read: { "~/foo/*": "allow" } } }); - expect(rules[0]?.pattern).toBe(`${home}/foo/*`); - }); - - it("expands $HOME in patterns", () => { - const home = homedir(); - const rules = configToRuleset({ permissions: { read: { "$HOME/bar/*": "deny" } } }); - expect(rules[0]?.pattern).toBe(`${home}/bar/*`); - }); - - it("handles empty permissions", () => { - const rules = configToRuleset({ permissions: {} }); - expect(rules).toEqual([]); - }); -}); diff --git a/packages/core/tests/config/lsp-schema.test.ts b/packages/core/tests/config/lsp-schema.test.ts deleted file mode 100644 index 2b71cc2..0000000 --- a/packages/core/tests/config/lsp-schema.test.ts +++ /dev/null @@ -1,110 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { validateConfig } from "../../src/config/schema.js"; - -describe("config schema — [lsp] block", () => { - it("parses a valid custom server entry", () => { - const { config, errors } = validateConfig({ - permissions: {}, - lsp: { - "luau-lsp": { - command: ["luau-lsp", "lsp"], - extensions: [".luau"], - initialization: { "luau-lsp": { platform: { type: "roblox" } } }, - }, - }, - }); - expect(errors).toHaveLength(0); - expect(config.lsp).toBeDefined(); - const entry = config.lsp?.["luau-lsp"]; - expect(entry?.command).toEqual(["luau-lsp", "lsp"]); - expect(entry?.extensions).toEqual([".luau"]); - expect(entry?.initialization).toEqual({ - "luau-lsp": { platform: { type: "roblox" } }, - }); - }); - - it("preserves env and nested initialization verbatim", () => { - const { config } = validateConfig({ - permissions: {}, - lsp: { - "luau-lsp": { - command: ["luau-lsp", "lsp"], - extensions: [".luau"], - env: { PATH: "/custom/bin" }, - initialization: { - "luau-lsp": { - sourcemap: { enabled: true, autogenerate: true }, - diagnostics: { strictDatamodelTypes: false }, - }, - }, - }, - }, - }); - const entry = config.lsp?.["luau-lsp"]; - expect(entry?.env).toEqual({ PATH: "/custom/bin" }); - expect(entry?.initialization).toEqual({ - "luau-lsp": { - sourcemap: { enabled: true, autogenerate: true }, - diagnostics: { strictDatamodelTypes: false }, - }, - }); - }); - - it("rejects a custom server missing command", () => { - const { config, errors } = validateConfig({ - permissions: {}, - lsp: { broken: { extensions: [".luau"] } }, - }); - expect(errors.some((e) => e.path === "lsp.broken.command")).toBe(true); - expect(config.lsp).toBeUndefined(); - }); - - it("rejects a custom server missing extensions", () => { - const { errors } = validateConfig({ - permissions: {}, - lsp: { broken: { command: ["x"] } }, - }); - expect(errors.some((e) => e.path === "lsp.broken.extensions")).toBe(true); - }); - - it("rejects an empty command array", () => { - const { errors } = validateConfig({ - permissions: {}, - lsp: { broken: { command: [], extensions: [".luau"] } }, - }); - expect(errors.some((e) => e.path === "lsp.broken.command")).toBe(true); - }); - - it("keeps a disabled entry without requiring command/extensions", () => { - const { config, errors } = validateConfig({ - permissions: {}, - lsp: { "luau-lsp": { disabled: true } }, - }); - expect(errors).toHaveLength(0); - expect(config.lsp?.["luau-lsp"]?.disabled).toBe(true); - }); - - it("skips a malformed entry but keeps valid siblings", () => { - const { config, errors } = validateConfig({ - permissions: {}, - lsp: { - good: { command: ["a"], extensions: [".luau"] }, - bad: { extensions: [".luau"] }, - }, - }); - expect(config.lsp?.good).toBeDefined(); - expect(config.lsp?.bad).toBeUndefined(); - expect(errors.length).toBeGreaterThan(0); - }); - - it("omits lsp entirely when not present", () => { - const { config, errors } = validateConfig({ permissions: {} }); - expect(errors).toHaveLength(0); - expect(config.lsp).toBeUndefined(); - }); - - it("flags a non-object lsp value", () => { - const { errors } = validateConfig({ permissions: {}, lsp: "nope" }); - expect(errors.some((e) => e.path === "lsp")).toBe(true); - }); -}); diff --git a/packages/core/tests/config/merge.test.ts b/packages/core/tests/config/merge.test.ts deleted file mode 100644 index b9e4bbb..0000000 --- a/packages/core/tests/config/merge.test.ts +++ /dev/null @@ -1,223 +0,0 @@ -import { mkdirSync, rmSync, writeFileSync } from "node:fs"; -import { join } from "node:path"; -import { afterEach, beforeEach, describe, expect, it } from "vitest"; -import { - configToRuleset, - getGlobalConfigPath, - loadConfig, - loadGlobalConfig, - mergeConfigs, -} from "../../src/config/loader.js"; -import { evaluate } from "../../src/permission/evaluate.js"; -import type { DispatchConfig } from "../../src/types/index.js"; - -const TMP = join("/tmp/opencode", "dispatch-config-merge-test"); -const LOCAL_DIR = join(TMP, "project"); -const GLOBAL_PATH = join(TMP, "global", "dispatch.toml"); - -const prevGlobal = process.env.DISPATCH_GLOBAL_CONFIG; - -beforeEach(() => { - mkdirSync(LOCAL_DIR, { recursive: true }); - mkdirSync(join(TMP, "global"), { recursive: true }); - process.env.DISPATCH_GLOBAL_CONFIG = GLOBAL_PATH; -}); - -afterEach(() => { - rmSync(TMP, { recursive: true, force: true }); - if (prevGlobal === undefined) delete process.env.DISPATCH_GLOBAL_CONFIG; - else process.env.DISPATCH_GLOBAL_CONFIG = prevGlobal; -}); - -function writeGlobal(content: string): void { - writeFileSync(GLOBAL_PATH, content, "utf-8"); -} - -function writeLocal(content: string): void { - writeFileSync(join(LOCAL_DIR, "dispatch.toml"), content, "utf-8"); -} - -// ─── mergeConfigs (pure) ───────────────────────────────────────── - -describe("mergeConfigs — lsp by id", () => { - it("keeps non-conflicting servers from both global and local", () => { - const global: DispatchConfig = { - permissions: {}, - lsp: { biome: { command: ["biome"], extensions: [".ts"] } }, - }; - const local: DispatchConfig = { - permissions: {}, - lsp: { luau: { command: ["luau-lsp"], extensions: [".luau"] } }, - }; - const merged = mergeConfigs(global, local); - expect(Object.keys(merged.lsp ?? {}).sort()).toEqual(["biome", "luau"]); - }); - - it("local overrides global for the same server id", () => { - const global: DispatchConfig = { - permissions: {}, - lsp: { biome: { command: ["global-biome"], extensions: [".ts"] } }, - }; - const local: DispatchConfig = { - permissions: {}, - lsp: { biome: { command: ["local-biome"], extensions: [".ts", ".tsx"] } }, - }; - const merged = mergeConfigs(global, local); - expect(merged.lsp?.biome.command).toEqual(["local-biome"]); - expect(merged.lsp?.biome.extensions).toEqual([".ts", ".tsx"]); - }); - - it("omits lsp entirely when neither side declares one", () => { - const merged = mergeConfigs({ permissions: {} }, { permissions: {} }); - expect(merged.lsp).toBeUndefined(); - }); - - it("does not mutate inputs", () => { - const global: DispatchConfig = { - permissions: {}, - lsp: { biome: { command: ["g"], extensions: [".ts"] } }, - }; - const local: DispatchConfig = { - permissions: {}, - lsp: { biome: { command: ["l"], extensions: [".ts"] } }, - }; - mergeConfigs(global, local); - expect(global.lsp?.biome.command).toEqual(["g"]); - expect(local.lsp?.biome.command).toEqual(["l"]); - }); -}); - -describe("mergeConfigs — keys by id", () => { - it("merges keys by id, local overriding global", () => { - const global: DispatchConfig = { - permissions: {}, - keys: [ - { id: "a", provider: "x", base_url: "g-a" }, - { id: "b", provider: "x", base_url: "g-b" }, - ], - }; - const local: DispatchConfig = { - permissions: {}, - keys: [ - { id: "b", provider: "x", base_url: "l-b" }, - { id: "c", provider: "x", base_url: "l-c" }, - ], - }; - const merged = mergeConfigs(global, local); - const byId = Object.fromEntries((merged.keys ?? []).map((k) => [k.id, k.base_url])); - expect(byId).toEqual({ a: "g-a", b: "l-b", c: "l-c" }); - }); - - it("carries global keys through when local has none", () => { - const global: DispatchConfig = { - permissions: {}, - keys: [{ id: "a", provider: "x", base_url: "g-a" }], - }; - const merged = mergeConfigs(global, { permissions: {} }); - expect(merged.keys).toEqual([{ id: "a", provider: "x", base_url: "g-a" }]); - }); -}); - -describe("mergeConfigs — permissions", () => { - it("merges nested groups pattern-by-pattern with local winning", () => { - const global: DispatchConfig = { - permissions: { bash: { "git status": "allow", "*": "ask" } }, - }; - const local: DispatchConfig = { - permissions: { bash: { "*": "allow" } }, - }; - const merged = mergeConfigs(global, local); - const bash = merged.permissions.bash as Record<string, string>; - expect(bash["git status"]).toBe("allow"); - expect(bash["*"]).toBe("allow"); // local override - }); - - it("local string value replaces a global nested group", () => { - const global: DispatchConfig = { - permissions: { read: { "src/**": "allow" } }, - }; - const local: DispatchConfig = { permissions: { read: "deny" } }; - const merged = mergeConfigs(global, local); - expect(merged.permissions.read).toBe("deny"); - }); - - it("keeps global-only permission groups", () => { - const global: DispatchConfig = { permissions: { read: "allow" } }; - const local: DispatchConfig = { permissions: { edit: "ask" } }; - const merged = mergeConfigs(global, local); - expect(merged.permissions.read).toBe("allow"); - expect(merged.permissions.edit).toBe("ask"); - }); - - it("local wins at evaluation time (findLast ordering)", () => { - const merged = mergeConfigs( - { permissions: { bash: { "*": "ask" } } }, - { permissions: { bash: { "*": "allow" } } }, - ); - const ruleset = configToRuleset(merged); - expect(evaluate("bash", "anything", ruleset).action).toBe("allow"); - }); - - // Regression: a SPECIFIC local override must not be shadowed by a more - // GENERAL global pattern (e.g. "*") that happened to be declared lower in - // the global block. `evaluate` uses findLast, so every local pattern must be - // emitted AFTER all global patterns of the same group. - it("specific local override beats a general global wildcard regardless of declaration order", () => { - const merged = mergeConfigs( - { permissions: { bash: { "npm test": "allow", "*": "ask" } } }, - { permissions: { bash: { "npm test": "deny" } } }, - ); - // Local "npm test" must be emitted after global "*". - expect(Object.keys(merged.permissions.bash as object)).toEqual(["*", "npm test"]); - const ruleset = configToRuleset(merged); - expect(evaluate("bash", "npm test", ruleset).action).toBe("deny"); - // And the inherited global wildcard still applies to other commands. - expect(evaluate("bash", "rm -rf /", ruleset).action).toBe("ask"); - }); -}); - -// ─── loadConfig (filesystem integration) ───────────────────────── - -describe("loadConfig — global + local integration", () => { - it("returns global config when local dispatch.toml is missing", () => { - writeGlobal(`[lsp.biome]\ncommand = ["biome"]\nextensions = [".ts"]\n`); - const config = loadConfig(LOCAL_DIR); - expect(config.lsp?.biome.command).toEqual(["biome"]); - }); - - it("returns local config when global is missing", () => { - writeLocal(`[permissions]\nread = "allow"\n`); - const config = loadConfig(LOCAL_DIR); - expect(config.permissions.read).toBe("allow"); - expect(config.lsp).toBeUndefined(); - }); - - it("merges global LSP servers with local ones (local wins on id)", () => { - writeGlobal( - `[lsp.biome]\ncommand = ["global-biome"]\nextensions = [".ts"]\n\n[lsp.luau]\ncommand = ["luau-lsp"]\nextensions = [".luau"]\n`, - ); - writeLocal(`[lsp.biome]\ncommand = ["local-biome"]\nextensions = [".ts"]\n`); - const config = loadConfig(LOCAL_DIR); - expect(config.lsp?.biome.command).toEqual(["local-biome"]); - expect(config.lsp?.luau.command).toEqual(["luau-lsp"]); - }); - - it("a malformed global config is ignored, local still loads", () => { - writeGlobal("not valid toml [[["); - writeLocal(`[permissions]\nread = "allow"\n`); - const config = loadConfig(LOCAL_DIR); - expect(config.permissions.read).toBe("allow"); - }); -}); - -describe("loadGlobalConfig", () => { - it("returns empty default when the global file is missing", () => { - expect(loadGlobalConfig()).toEqual({ permissions: {} }); - }); - - it("loads the file at getGlobalConfigPath()", () => { - writeGlobal(`[permissions]\nedit = "deny"\n`); - expect(getGlobalConfigPath()).toBe(GLOBAL_PATH); - expect(loadGlobalConfig().permissions.edit).toBe("deny"); - }); -}); diff --git a/packages/core/tests/config/watcher.test.ts b/packages/core/tests/config/watcher.test.ts deleted file mode 100644 index 9388c8a..0000000 --- a/packages/core/tests/config/watcher.test.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { mkdirSync, rmSync, writeFileSync } from "node:fs"; -import { join } from "node:path"; -import { afterEach, beforeEach, describe, expect, it } from "vitest"; -import { watchDirConfig } from "../../src/config/watcher.js"; - -const TMP = join("/tmp/opencode", "dispatch-watchdir-test"); - -beforeEach(() => { - mkdirSync(TMP, { recursive: true }); -}); - -afterEach(() => { - rmSync(TMP, { recursive: true, force: true }); -}); - -function wait(ms: number): Promise<void> { - return new Promise((resolve) => setTimeout(resolve, ms)); -} - -describe("watchDirConfig", () => { - it("fires onChange (debounced) when <dir>/dispatch.toml changes", async () => { - let calls = 0; - const handle = watchDirConfig(TMP, () => { - calls++; - }); - // Let chokidar finish its initial scan before mutating the file. - await wait(200); - - writeFileSync(join(TMP, "dispatch.toml"), `[permissions]\nread = "allow"\n`, "utf-8"); - // 300ms debounce + chokidar latency. - await wait(700); - - handle.close(); - expect(calls).toBeGreaterThanOrEqual(1); - }); - - it("does not fire after close()", async () => { - let calls = 0; - const handle = watchDirConfig(TMP, () => { - calls++; - }); - await wait(200); - handle.close(); - - writeFileSync(join(TMP, "dispatch.toml"), `[permissions]\nedit = "deny"\n`, "utf-8"); - await wait(700); - - expect(calls).toBe(0); - }); -}); diff --git a/packages/core/tests/credentials/wake-probe.test.ts b/packages/core/tests/credentials/wake-probe.test.ts deleted file mode 100644 index a97a00c..0000000 --- a/packages/core/tests/credentials/wake-probe.test.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { describe, expect, it, vi } from "vitest"; - -// `claude.ts` transitively imports `db/index.js`, whose top-level -// `import { Database } from "bun:sqlite"` can't resolve under vitest's Node -// runtime. Stub the db module — `buildWakeProbeBody` never touches it. -vi.mock("../../src/db/index.js", () => ({ - getDatabase: vi.fn(() => { - throw new Error("db not available in this test"); - }), -})); - -const { buildWakeProbeBody, selectHaikuModel } = await import("../../src/credentials/claude.js"); - -const IDENTITY = "You are Claude Code, Anthropic's official CLI for Claude."; - -describe("buildWakeProbeBody", () => { - it("targets the requested model with a tiny token budget", () => { - const body = buildWakeProbeBody("claude-3-5-haiku-20241022"); - expect(body.model).toBe("claude-3-5-haiku-20241022"); - expect(body.max_tokens).toBe(16); - }); - - it("emits a Claude-Code-shaped system[]: billing first, identity second", () => { - const body = buildWakeProbeBody("claude-3-5-haiku-20241022"); - expect(body.system).toHaveLength(2); - - // system[0] is the billing header line (no cache_control on a probe). - expect(body.system[0]).toEqual({ - type: "text", - text: expect.stringMatching(/^x-anthropic-billing-header: /), - }); - expect(body.system[0]).not.toHaveProperty("cache_control"); - - // system[1] is the VERBATIM Claude Code identity string. Anthropic - // rejects OAuth (Pro/Max) requests whose system[] lacks this. - expect(body.system[1]).toEqual({ type: "text", text: IDENTITY }); - }); - - it("carries a single short user message", () => { - const body = buildWakeProbeBody("claude-3-5-haiku-20241022"); - expect(body.messages).toEqual([{ role: "user", content: "hi" }]); - }); - - it("is deterministic for a given model (pure)", () => { - const a = buildWakeProbeBody("claude-3-5-haiku-20241022"); - const b = buildWakeProbeBody("claude-3-5-haiku-20241022"); - expect(a).toEqual(b); - }); -}); -describe("selectHaikuModel", () => { - it("returns the id whose name contains 'haiku'", () => { - const models = ["claude-sonnet-4-20250514", "claude-haiku-4-5-20251001"]; - expect(selectHaikuModel(models)).toBe("claude-haiku-4-5-20251001"); - }); - - it("matches case-insensitively", () => { - expect(selectHaikuModel(["Claude-HAIKU-Latest"])).toBe("Claude-HAIKU-Latest"); - }); - - it("returns the FIRST match when several models contain 'haiku'", () => { - // `/v1/models` returns newest-first, so first-match prefers the newest. - const models = ["claude-haiku-4-5-20251001", "claude-3-5-haiku-20241022"]; - expect(selectHaikuModel(models)).toBe("claude-haiku-4-5-20251001"); - }); - - it("returns null when no model contains 'haiku'", () => { - expect(selectHaikuModel(["claude-sonnet-4-20250514", "claude-opus-4-20250514"])).toBeNull(); - }); - - it("returns null for an empty list", () => { - expect(selectHaikuModel([])).toBeNull(); - }); -}); diff --git a/packages/core/tests/db/chunks.db.test.ts b/packages/core/tests/db/chunks.db.test.ts deleted file mode 100644 index 4f7d517..0000000 --- a/packages/core/tests/db/chunks.db.test.ts +++ /dev/null @@ -1,336 +0,0 @@ -import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; -import type { ChunkRowDraft, UsageData } from "../../src/types/index.js"; - -/** - * Internal row shape — matches the production `chunks` table columns. - * Kept loose at the `query()` boundary to mirror bun:sqlite's dynamic - * return type. - */ -interface ChunkRecord { - id: string; - tab_id: string; - seq: number; - turn_id: string; - step: number; - role: string; - type: string; - data_json: string; - created_at: number; -} - -/** - * In-memory fake of `bun:sqlite`'s Database implementing only the queries - * `chunks.ts` actually issues. Same approach as `tabs.test.ts`: match exact - * normalized query strings as fixed branches (no SQL parser), so a query-string - * change fails loudly as "unsupported" instead of silently returning wrong data. - * - * This lets the DB-backed `getChunksForTab` / `getTotalChunkCount` / - * `getUsageStatsForTab` logic run under vitest, where `bun:sqlite` can't load. - */ -class FakeDatabase { - rows: ChunkRecord[] = []; - private idCounter = 0; - - query(sql: string): { - all: (params?: Record<string, unknown>) => unknown[]; - get: (params?: Record<string, unknown>) => unknown; - run: (params?: Record<string, unknown>) => void; - } { - return { - all: (params) => this.execSelect(sql, params), - get: (params) => this.execSelect(sql, params)[0] ?? null, - run: (params) => { - this.execMutation(sql, params); - }, - }; - } - - /** bun:sqlite's `db.transaction(fn)` returns a callable that runs `fn`. */ - transaction(fn: () => void): () => void { - return () => { - fn(); - }; - } - - private execSelect(sql: string, params?: Record<string, unknown>): unknown[] { - const norm = sql.replace(/\s+/g, " ").trim(); - const tabId = params?.$tabId as string | undefined; - const forTab = this.rows.filter((r) => r.tab_id === tabId); - const visible = forTab.filter((r) => r.type !== "usage"); - - // appendChunks: next-seq lookup (counts ALL rows, incl. usage) - if (norm === "SELECT COALESCE(MAX(seq), -1) as max_seq FROM chunks WHERE tab_id = $tabId") { - const seqs = forTab.map((r) => r.seq); - return [{ max_seq: seqs.length > 0 ? Math.max(...seqs) : -1 }]; - } - - // getChunksForTab — no options (usage excluded) - if ( - norm === "SELECT * FROM chunks WHERE tab_id = $tabId AND type != 'usage' ORDER BY seq ASC" - ) { - return [...visible].sort((a, b) => a.seq - b.seq); - } - - // getChunksForTab — before + limit (usage excluded) - if ( - norm === - "SELECT * FROM chunks WHERE tab_id = $tabId AND type != 'usage' AND seq < $before ORDER BY seq DESC LIMIT $limit" - ) { - const before = params?.$before as number; - const limit = params?.$limit as number; - return visible - .filter((r) => r.seq < before) - .sort((a, b) => b.seq - a.seq) - .slice(0, limit); - } - - // getChunksForTab — before only (usage excluded) - if ( - norm === - "SELECT * FROM chunks WHERE tab_id = $tabId AND type != 'usage' AND seq < $before ORDER BY seq DESC" - ) { - const before = params?.$before as number; - return visible.filter((r) => r.seq < before).sort((a, b) => b.seq - a.seq); - } - - // getChunksForTab — limit only (usage excluded) - if ( - norm === - "SELECT * FROM chunks WHERE tab_id = $tabId AND type != 'usage' ORDER BY seq DESC LIMIT $limit" - ) { - const limit = params?.$limit as number; - return [...visible].sort((a, b) => b.seq - a.seq).slice(0, limit); - } - - // getTotalChunkCount (usage excluded) - if (norm === "SELECT COUNT(*) as count FROM chunks WHERE tab_id = $tabId AND type != 'usage'") { - return [{ count: visible.length }]; - } - - // getUsageStatsForTab: usage rows only, in seq order - if ( - norm === - "SELECT data_json FROM chunks WHERE tab_id = $tabId AND type = 'usage' ORDER BY seq ASC" - ) { - return forTab - .filter((r) => r.type === "usage") - .sort((a, b) => a.seq - b.seq) - .map((r) => ({ data_json: r.data_json })); - } - - throw new Error(`FakeDatabase: unsupported SELECT: ${norm}`); - } - - private execMutation(sql: string, params?: Record<string, unknown>): void { - const norm = sql.replace(/\s+/g, " ").trim(); - - // appendChunks: single-row insert - if ( - norm === - "INSERT INTO chunks (id, tab_id, seq, turn_id, step, role, type, data_json, created_at) VALUES ($id, $tabId, $seq, $turnId, $step, $role, $type, $dataJson, $now)" - ) { - this.rows.push({ - id: (params?.$id as string) ?? `c${this.idCounter++}`, - tab_id: params?.$tabId as string, - seq: params?.$seq as number, - turn_id: params?.$turnId as string, - step: (params?.$step as number) ?? 0, - role: params?.$role as string, - type: params?.$type as string, - data_json: params?.$dataJson as string, - created_at: (params?.$now as number) ?? 0, - }); - return; - } - - throw new Error(`FakeDatabase: unsupported mutation: ${norm}`); - } -} - -let fakeDb: FakeDatabase; - -vi.mock("../../src/db/index.js", () => ({ - getDatabase: vi.fn(() => fakeDb), -})); - -const { appendChunks, getChunksForTab, getTotalChunkCount, getUsageStatsForTab } = await import( - "../../src/db/chunks.js" -); - -function usageDraft(turnId: string, u: UsageData): ChunkRowDraft { - return { turnId, step: 0, role: "assistant", type: "usage", data: u }; -} - -beforeAll(() => { - fakeDb = new FakeDatabase(); -}); - -beforeEach(() => { - fakeDb.rows = []; -}); - -// --------------------------------------------------------------------------- -// usage chunk persistence + side-channel invariants -// --------------------------------------------------------------------------- -describe("usage chunk rows (DB-backed)", () => { - const TAB = "tab-usage"; - - it("persists usage rows alongside content rows with contiguous seqs", () => { - appendChunks(TAB, [ - { turnId: "t1", step: 0, role: "user", type: "text", data: { text: "hi" } }, - { turnId: "t1", step: 0, role: "assistant", type: "text", data: { text: "yo" } }, - usageDraft("t1", { - inputTokens: 100, - outputTokens: 10, - cacheReadTokens: 0, - cacheWriteTokens: 90, - }), - ]); - // All three rows landed with contiguous seqs. - expect(fakeDb.rows.map((r) => r.seq)).toEqual([0, 1, 2]); - expect(fakeDb.rows.map((r) => r.type)).toEqual(["text", "text", "usage"]); - }); - - it("excludes usage rows from getChunksForTab (all variants)", () => { - appendChunks(TAB, [ - { turnId: "t1", step: 0, role: "user", type: "text", data: { text: "q" } }, - usageDraft("t1", { - inputTokens: 100, - outputTokens: 10, - cacheReadTokens: 0, - cacheWriteTokens: 90, - }), - { turnId: "t1", step: 0, role: "assistant", type: "text", data: { text: "a" } }, - usageDraft("t1", { - inputTokens: 200, - outputTokens: 20, - cacheReadTokens: 150, - cacheWriteTokens: 0, - }), - ]); - - // no options - const all = getChunksForTab(TAB); - expect(all.every((r) => r.type !== "usage")).toBe(true); - expect(all.map((r) => r.type)).toEqual(["text", "text"]); - - // limit only - const limited = getChunksForTab(TAB, { limit: 10 }); - expect(limited.every((r) => r.type !== "usage")).toBe(true); - expect(limited).toHaveLength(2); - - // before only — `before` is a seq cursor; usage seqs must never surface - const before = getChunksForTab(TAB, { before: 100 }); - expect(before.every((r) => r.type !== "usage")).toBe(true); - expect(before).toHaveLength(2); - - // before + limit - const bl = getChunksForTab(TAB, { before: 100, limit: 10 }); - expect(bl.every((r) => r.type !== "usage")).toBe(true); - expect(bl).toHaveLength(2); - }); - - it("excludes usage rows from getTotalChunkCount", () => { - appendChunks(TAB, [ - { turnId: "t1", step: 0, role: "user", type: "text", data: { text: "q" } }, - { turnId: "t1", step: 0, role: "assistant", type: "text", data: { text: "a" } }, - usageDraft("t1", { - inputTokens: 100, - outputTokens: 10, - cacheReadTokens: 0, - cacheWriteTokens: 90, - }), - ]); - // 3 rows total, but only 2 visible. - expect(getTotalChunkCount(TAB)).toBe(2); - }); -}); - -// --------------------------------------------------------------------------- -// getUsageStatsForTab — backend aggregate -// --------------------------------------------------------------------------- -describe("getUsageStatsForTab", () => { - const TAB = "tab-agg"; - - it("returns null when the tab has no usage rows", () => { - appendChunks(TAB, [ - { turnId: "t1", step: 0, role: "assistant", type: "text", data: { text: "a" } }, - ]); - expect(getUsageStatsForTab(TAB)).toBeNull(); - }); - - it("sums cumulative tokens, counts requests, and reports the last request's split", () => { - appendChunks(TAB, [ - usageDraft("t1", { - inputTokens: 1000, - outputTokens: 40, - cacheReadTokens: 0, - cacheWriteTokens: 900, - }), - usageDraft("t1", { - inputTokens: 1200, - outputTokens: 60, - cacheReadTokens: 1000, - cacheWriteTokens: 100, - }), - ]); - - const stats = getUsageStatsForTab(TAB); - expect(stats).not.toBeNull(); - expect(stats?.requests).toBe(2); - expect(stats?.inputTokens).toBe(2200); - expect(stats?.outputTokens).toBe(100); - expect(stats?.cacheReadTokens).toBe(1000); - expect(stats?.cacheWriteTokens).toBe(1000); - // `last` = the most recent (highest-seq) usage row. - expect(stats?.last).toEqual({ - inputTokens: 1200, - outputTokens: 60, - cacheReadTokens: 1000, - cacheWriteTokens: 100, - }); - }); - - it("is structurally identical to the frontend CacheStats shape (seeds directly)", () => { - appendChunks(TAB, [ - usageDraft("t1", { - inputTokens: 5, - outputTokens: 1, - cacheReadTokens: 2, - cacheWriteTokens: 3, - }), - ]); - const stats = getUsageStatsForTab(TAB); - expect(Object.keys(stats ?? {}).sort()).toEqual( - [ - "cacheReadTokens", - "cacheWriteTokens", - "inputTokens", - "last", - "outputTokens", - "requests", - ].sort(), - ); - }); - - it("is scoped per tab", () => { - appendChunks("tab-a", [ - usageDraft("t1", { - inputTokens: 10, - outputTokens: 1, - cacheReadTokens: 0, - cacheWriteTokens: 0, - }), - ]); - appendChunks("tab-b", [ - usageDraft("t2", { - inputTokens: 20, - outputTokens: 2, - cacheReadTokens: 0, - cacheWriteTokens: 0, - }), - ]); - expect(getUsageStatsForTab("tab-a")?.inputTokens).toBe(10); - expect(getUsageStatsForTab("tab-b")?.inputTokens).toBe(20); - }); -}); diff --git a/packages/core/tests/db/chunks.test.ts b/packages/core/tests/db/chunks.test.ts deleted file mode 100644 index fe54628..0000000 --- a/packages/core/tests/db/chunks.test.ts +++ /dev/null @@ -1,179 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { explodeTurn, explodeUserText, groupRowsToMessages } from "../../src/chunks/transform.js"; -import type { Chunk, ChunkRow, ChunkRowDraft } from "../../src/types/index.js"; - -// These tests cover the pure explode/group transforms — the heart of the flat -// chunk-log storage model. No DB is required. - -/** Promote drafts to rows with synthetic seq/id/createdAt (as appendChunks would). */ -function toRows(drafts: ChunkRowDraft[], tabId = "tab-1", startSeq = 0): ChunkRow[] { - return drafts.map((d, i) => ({ - id: `c${i}`, - tabId, - seq: startSeq + i, - turnId: d.turnId, - step: d.step, - role: d.role, - type: d.type, - data: d.data, - createdAt: 1000 + i, - })); -} - -describe("explodeTurn", () => { - it("splits a tool-batch into separate tool_call (assistant) and tool_result (tool) rows", () => { - const chunks: Chunk[] = [ - { type: "thinking", text: "hmm", metadata: { anthropic: { signature: "S" } } }, - { type: "text", text: "let me read" }, - { - type: "tool-batch", - calls: [ - { id: "a1", name: "read_file", arguments: { path: "x" }, result: "X", isError: false }, - { id: "a2", name: "read_file", arguments: { path: "y" }, result: "Y", isError: false }, - ], - }, - ]; - const drafts = explodeTurn("turn-1", chunks); - - // thinking, text, tool_call×2 (assistant), tool_result×2 (tool) - expect(drafts.map((d) => `${d.role}/${d.type}`)).toEqual([ - "assistant/thinking", - "assistant/text", - "assistant/tool_call", - "assistant/tool_call", - "tool/tool_result", - "tool/tool_result", - ]); - // All in the same step (one round-trip). - expect(drafts.every((d) => d.step === 0)).toBe(true); - expect(drafts.every((d) => d.turnId === "turn-1")).toBe(true); - }); - - it("increments step after each tool-batch (multi-step turn)", () => { - const chunks: Chunk[] = [ - { type: "text", text: "s0" }, - { type: "tool-batch", calls: [{ id: "a", name: "t", arguments: {}, result: "r" }] }, - { type: "text", text: "s1" }, - { type: "tool-batch", calls: [{ id: "b", name: "t", arguments: {}, result: "r" }] }, - { type: "text", text: "final" }, - ]; - const drafts = explodeTurn("turn-1", chunks); - const byStep = (s: number) => drafts.filter((d) => d.step === s).map((d) => d.type); - expect(byStep(0)).toEqual(["text", "tool_call", "tool_result"]); - expect(byStep(1)).toEqual(["text", "tool_call", "tool_result"]); - expect(byStep(2)).toEqual(["text"]); // trailing final-step text, no tool-batch - }); - - it("omits tool_result rows for calls without a result", () => { - const chunks: Chunk[] = [ - { type: "tool-batch", calls: [{ id: "a", name: "t", arguments: {} }] }, - ]; - const drafts = explodeTurn("turn-1", chunks); - expect(drafts.map((d) => d.type)).toEqual(["tool_call"]); - }); -}); - -describe("groupRowsToMessages (round-trip)", () => { - it("reconstructs a user message then an assistant message with a per-step tool-batch", () => { - const rows = [ - ...toRows(explodeUserText("turn-1", "hello"), "tab-1", 0), - ...toRows( - explodeTurn("turn-1", [ - { type: "text", text: "reading" }, - { - type: "tool-batch", - calls: [ - { - id: "a1", - name: "read_file", - arguments: { path: "x" }, - result: "X", - isError: false, - }, - ], - }, - { type: "text", text: "done" }, - ]), - "tab-1", - 1, - ), - ]; - - const msgs = groupRowsToMessages(rows); - expect(msgs.map((m) => m.role)).toEqual(["user", "assistant"]); - expect(msgs[0]?.chunks).toEqual([{ type: "text", text: "hello" }]); - - const a = msgs[1]; - if (!a) throw new Error("no assistant message"); - // reconstructed: text, tool-batch(step0), text(step1) - expect(a.chunks.map((c) => c.type)).toEqual(["text", "tool-batch", "text"]); - const batch = a.chunks.find((c) => c.type === "tool-batch"); - if (batch?.type !== "tool-batch") throw new Error("no batch"); - expect(batch.calls[0]).toMatchObject({ - id: "a1", - name: "read_file", - arguments: { path: "x" }, - result: "X", - isError: false, - }); - }); - - it("keeps each step's tool calls in its own tool-batch chunk", () => { - const rows = toRows( - explodeTurn("turn-1", [ - { type: "tool-batch", calls: [{ id: "a", name: "t", arguments: {}, result: "ra" }] }, - { type: "tool-batch", calls: [{ id: "b", name: "t", arguments: {}, result: "rb" }] }, - ]), - ); - const msgs = groupRowsToMessages(rows); - expect(msgs).toHaveLength(1); - const batches = msgs[0]?.chunks.filter((c) => c.type === "tool-batch") ?? []; - expect(batches).toHaveLength(2); - }); - - it("round-trips a multi-step assistant turn back to its original chunk shape", () => { - const original: Chunk[] = [ - { type: "thinking", text: "plan", metadata: { anthropic: { signature: "S" } } }, - { type: "text", text: "step0" }, - { - type: "tool-batch", - calls: [ - { id: "a", name: "read_file", arguments: { path: "p" }, result: "R", isError: false }, - ], - }, - { type: "text", text: "final" }, - ]; - const rows = toRows(explodeTurn("turn-1", original)); - const msgs = groupRowsToMessages(rows); - expect(msgs).toHaveLength(1); - expect(msgs[0]?.chunks).toEqual(original); - }); - - it("tolerates an orphan tool_result whose tool_call was paged out", () => { - const rows = toRows([ - { - turnId: "turn-1", - step: 0, - role: "tool", - type: "tool_result", - data: { callId: "z", name: "t", result: "R", isError: false }, - }, - ]); - const msgs = groupRowsToMessages(rows); - expect(msgs).toHaveLength(1); - const batch = msgs[0]?.chunks[0]; - if (batch?.type !== "tool-batch") throw new Error("no batch"); - expect(batch.calls[0]).toMatchObject({ id: "z", result: "R" }); - }); - - it("breaks the assistant grouping on a user or system row", () => { - const rows = [ - ...toRows(explodeUserText("t1", "q1"), "tab", 0), - ...toRows(explodeTurn("t1", [{ type: "text", text: "a1" }]), "tab", 1), - ...toRows(explodeUserText("t2", "q2"), "tab", 2), - ...toRows(explodeTurn("t2", [{ type: "system", kind: "notice", text: "n" }]), "tab", 3), - ]; - const msgs = groupRowsToMessages(rows); - expect(msgs.map((m) => m.role)).toEqual(["user", "assistant", "user", "system"]); - }); -}); diff --git a/packages/core/tests/db/rekey-chunks.db.test.ts b/packages/core/tests/db/rekey-chunks.db.test.ts deleted file mode 100644 index 7cdafe3..0000000 --- a/packages/core/tests/db/rekey-chunks.db.test.ts +++ /dev/null @@ -1,129 +0,0 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; - -interface ChunkRecord { - id: string; - tab_id: string; - seq: number; - turn_id: string; - step: number; - role: string; - type: string; - data_json: string; - created_at: number; -} - -/** - * Minimal in-memory fake of bun:sqlite supporting only the queries - * `appendChunks`, `getChunksForTab`, and `rekeyChunks` issue. Mirrors the - * approach in chunks.db.test.ts (exact normalized-string branches). - */ -class FakeDatabase { - rows: ChunkRecord[] = []; - - query(sql: string) { - return { - all: (params?: Record<string, unknown>) => this.execSelect(sql, params), - get: (params?: Record<string, unknown>) => this.execSelect(sql, params)[0] ?? null, - run: (params?: Record<string, unknown>) => this.execMutation(sql, params), - }; - } - - transaction(fn: () => void): () => void { - return () => fn(); - } - - private execSelect(sql: string, params?: Record<string, unknown>): unknown[] { - const norm = sql.replace(/\s+/g, " ").trim(); - const tabId = params?.$tabId as string | undefined; - const forTab = this.rows.filter((r) => r.tab_id === tabId); - const visible = forTab.filter((r) => r.type !== "usage"); - - if (norm === "SELECT COALESCE(MAX(seq), -1) as max_seq FROM chunks WHERE tab_id = $tabId") { - const seqs = forTab.map((r) => r.seq); - return [{ max_seq: seqs.length > 0 ? Math.max(...seqs) : -1 }]; - } - if ( - norm === "SELECT * FROM chunks WHERE tab_id = $tabId AND type != 'usage' ORDER BY seq ASC" - ) { - return [...visible].sort((a, b) => a.seq - b.seq); - } - throw new Error(`FakeDatabase: unsupported SELECT: ${norm}`); - } - - private execMutation(sql: string, params?: Record<string, unknown>): { changes: number } { - const norm = sql.replace(/\s+/g, " ").trim(); - if ( - norm === - "INSERT INTO chunks (id, tab_id, seq, turn_id, step, role, type, data_json, created_at) VALUES ($id, $tabId, $seq, $turnId, $step, $role, $type, $dataJson, $now)" - ) { - this.rows.push({ - id: params?.$id as string, - tab_id: params?.$tabId as string, - seq: params?.$seq as number, - turn_id: params?.$turnId as string, - step: (params?.$step as number) ?? 0, - role: params?.$role as string, - type: params?.$type as string, - data_json: params?.$dataJson as string, - created_at: (params?.$now as number) ?? 0, - }); - return { changes: 1 }; - } - if (norm === "UPDATE chunks SET tab_id = $to WHERE tab_id = $from") { - const from = params?.$from as string; - const to = params?.$to as string; - let changes = 0; - for (const r of this.rows) { - if (r.tab_id === from) { - r.tab_id = to; - changes++; - } - } - return { changes }; - } - throw new Error(`FakeDatabase: unsupported mutation: ${norm}`); - } -} - -let fakeDb: FakeDatabase; -vi.mock("../../src/db/index.js", () => ({ getDatabase: vi.fn(() => fakeDb) })); - -const { appendChunks, getChunksForTab, rekeyChunks } = await import("../../src/db/chunks.js"); - -beforeEach(() => { - fakeDb = new FakeDatabase(); -}); - -describe("rekeyChunks", () => { - it("relocates all rows from one tab to another and reports the count", () => { - appendChunks("src", [ - { turnId: "t1", step: 0, role: "user", type: "text", data: { text: "hi" } }, - { turnId: "t1", step: 0, role: "assistant", type: "text", data: { text: "yo" } }, - ]); - expect(getChunksForTab("src")).toHaveLength(2); - - const moved = rekeyChunks("src", "backup"); - expect(moved).toBe(2); - expect(getChunksForTab("src")).toHaveLength(0); - const dst = getChunksForTab("backup"); - expect(dst).toHaveLength(2); - // turn id + seq preserved on the destination - expect(dst.map((r) => r.turnId)).toEqual(["t1", "t1"]); - expect(dst.map((r) => r.seq)).toEqual([0, 1]); - }); - - it("returns 0 when the source tab has no rows", () => { - expect(rekeyChunks("nope", "backup")).toBe(0); - }); - - it("does not touch unrelated tabs", () => { - appendChunks("src", [ - { turnId: "t1", step: 0, role: "user", type: "text", data: { text: "a" } }, - ]); - appendChunks("other", [ - { turnId: "t9", step: 0, role: "user", type: "text", data: { text: "b" } }, - ]); - rekeyChunks("src", "backup"); - expect(getChunksForTab("other")).toHaveLength(1); - }); -}); diff --git a/packages/core/tests/db/tabs.test.ts b/packages/core/tests/db/tabs.test.ts deleted file mode 100644 index 2cd226b..0000000 --- a/packages/core/tests/db/tabs.test.ts +++ /dev/null @@ -1,418 +0,0 @@ -import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; - -/** - * Internal row shape — matches the production `tabs` table columns. - * Kept loose (`Record`) on the `query()` boundary to mirror bun:sqlite's - * dynamic return type. - */ -interface TabRow { - id: string; - title: string; - key_id: string | null; - model_id: string | null; - parent_tab_id: string | null; - status: string; - is_open: number; - position: number; - created_at: number; - updated_at: number; -} - -/** - * In-memory fake of `bun:sqlite`'s Database that implements only the - * queries actually issued by `tabs.ts`. This sidesteps two problems - * the original test had: - * 1. Vite's resolver can't load `bun:sqlite` (it's a Bun-native - * module with no on-disk file). - * 2. Even under `bun --bun vitest`, `vi.mock` doesn't intercept - * module imports because Bun's loader bypasses Vite's transforms. - * - * By implementing the exact query strings as fixed branches we avoid - * writing an SQL parser; if `tabs.ts` ever changes a query string, - * tests will fail loudly with "Unsupported query" instead of - * silently returning wrong data. - */ -class FakeDatabase { - rows: TabRow[] = []; - - /** Match production's `db.query(sql).get|all|run(params)` shape. */ - query(sql: string): { - all: (params?: Record<string, unknown>) => unknown[]; - get: (params?: Record<string, unknown>) => unknown; - run: (params?: Record<string, unknown>) => void; - } { - return { - all: (params) => this.execSelect(sql, params), - get: (params) => this.execSelect(sql, params)[0] ?? null, - run: (params) => { - this.execMutation(sql, params); - }, - }; - } - - /** - * Match Bun's `db.transaction(fn)` shape: returns a callable that runs - * `fn` synchronously. The fake is in-memory and single-threaded, so we - * don't emulate rollback — callers just need the wrapper to be invocable. - */ - transaction(fn: () => void): () => void { - return () => fn(); - } - - private execSelect(sql: string, params?: Record<string, unknown>): unknown[] { - const norm = sql.replace(/\s+/g, " ").trim(); - - // getDescendantIds: children-of query - if (norm === "SELECT id FROM tabs WHERE parent_tab_id = $id AND is_open = 1") { - return this.rows - .filter((r) => r.parent_tab_id === params?.$id && r.is_open === 1) - .map((r) => ({ id: r.id })); - } - - // getTab: single-row lookup - if (norm === "SELECT * FROM tabs WHERE id = $id") { - const row = this.rows.find((r) => r.id === params?.$id); - return row ? [row] : []; - } - - // createTab: next-position lookup - if (norm === "SELECT COALESCE(MAX(position), -1) as max_pos FROM tabs WHERE is_open = 1") { - const positions = this.rows.filter((r) => r.is_open === 1).map((r) => r.position); - const maxPos = positions.length > 0 ? Math.max(...positions) : -1; - return [{ max_pos: maxPos }]; - } - - // resolveTabPrefix: open tabs whose id starts with a sanitized prefix. - // The production query binds `$prefix` as `<sanitized>%`; emulate SQLite - // LIKE prefix semantics here (case-insensitive, `%` = "rest of string"). - if (norm === "SELECT * FROM tabs WHERE is_open = 1 AND id LIKE $prefix ORDER BY position ASC") { - const raw = String(params?.$prefix ?? ""); - const needle = raw.endsWith("%") ? raw.slice(0, -1) : raw; - return this.rows - .filter((r) => r.is_open === 1 && r.id.toLowerCase().startsWith(needle.toLowerCase())) - .sort((a, b) => a.position - b.position); - } - - // shortestUniquePrefix: all open tab ids. - if (norm === "SELECT id FROM tabs WHERE is_open = 1") { - return this.rows.filter((r) => r.is_open === 1).map((r) => ({ id: r.id })); - } - - // listOpenTabs: every open tab ordered by position. - if (norm === "SELECT * FROM tabs WHERE is_open = 1 ORDER BY position ASC") { - return this.rows.filter((r) => r.is_open === 1).sort((a, b) => a.position - b.position); - } - - throw new Error(`FakeDatabase: unsupported SELECT: ${norm}`); - } - - private execMutation(sql: string, params?: Record<string, unknown>): void { - const norm = sql.replace(/\s+/g, " ").trim(); - - // createTab: full-row insert (every column named, $-bound params) - if ( - norm === - "INSERT INTO tabs (id, title, key_id, model_id, parent_tab_id, status, is_open, position, created_at, updated_at) VALUES ($id, $title, $keyId, $modelId, $parentTabId, 'idle', 1, $position, $now, $now)" - ) { - const id = params?.$id as string; - if (this.rows.some((r) => r.id === id)) { - throw new Error(`UNIQUE constraint failed: tabs.id (${id})`); - } - this.rows.push({ - id, - title: (params?.$title as string) ?? "", - key_id: (params?.$keyId as string | null) ?? null, - model_id: (params?.$modelId as string | null) ?? null, - parent_tab_id: (params?.$parentTabId as string | null) ?? null, - status: "idle", - is_open: 1, - position: (params?.$position as number) ?? 0, - created_at: (params?.$now as number) ?? 0, - updated_at: (params?.$now as number) ?? 0, - }); - return; - } - - // archiveTab: flip is_open to 0 - if (norm === "UPDATE tabs SET is_open = 0, updated_at = $now WHERE id = $id") { - const row = this.rows.find((r) => r.id === params?.$id); - if (row) { - row.is_open = 0; - row.updated_at = (params?.$now as number) ?? Date.now(); - } - return; - } - - // updateTabPositions: rewrite a single tab's position (run per id inside a txn) - if (norm === "UPDATE tabs SET position = $position, updated_at = $now WHERE id = $id") { - const row = this.rows.find((r) => r.id === params?.$id); - if (row) { - row.position = (params?.$position as number) ?? row.position; - row.updated_at = (params?.$now as number) ?? Date.now(); - } - return; - } - - throw new Error(`FakeDatabase: unsupported mutation: ${norm}`); - } -} - -/** - * Shared instance referenced by both the test setup and the - * `vi.mock` factory below. Declared with `let` (not `const`) so the - * factory's closure picks up the value assigned in `beforeAll`. - */ -let fakeDb: FakeDatabase; - -// Mock the db module before importing `tabs.ts` so that `getDatabase()` -// returns our in-memory fake instead of trying to open a real SQLite -// file. Mirrors the same pattern used by `tests/agent/agent.test.ts`. -vi.mock("../../src/db/index.js", () => ({ - getDatabase: vi.fn(() => fakeDb), -})); - -// Dynamic import AFTER `vi.mock` registers (vitest hoists `vi.mock` to -// the very top of the file, so by the time this line runs the mock is -// active for `./index.js` resolution inside `tabs.ts`). -const { - archiveTab, - createTab, - getDescendantIds, - getTab, - listOpenTabs, - resolveTabPrefix, - shortestUniquePrefix, - updateTabPositions, -} = await import("../../src/db/tabs.js"); - -beforeAll(() => { - fakeDb = new FakeDatabase(); -}); - -beforeEach(() => { - fakeDb.rows = []; -}); - -// --------------------------------------------------------------------------- -// getDescendantIds -// --------------------------------------------------------------------------- -describe("getDescendantIds", () => { - it("returns only the id when the tab has no children", () => { - createTab("root", "Root"); - - const ids = getDescendantIds("root"); - expect(ids).toEqual(["root"]); - }); - - it("returns leaf-first order for a linear chain (root → child → grandchild)", () => { - createTab("root", "Root"); - createTab("child", "Child", { parentTabId: "root" }); - createTab("grandchild", "Grandchild", { parentTabId: "child" }); - - const ids = getDescendantIds("root"); - // Leaves first: grandchild, child, root - expect(ids).toEqual(["grandchild", "child", "root"]); - }); - - it("returns leaf-first for a branching tree", () => { - createTab("a", "A"); - createTab("b1", "B1", { parentTabId: "a" }); - createTab("b2", "B2", { parentTabId: "a" }); - createTab("c1", "C1", { parentTabId: "b1" }); - createTab("c2", "C2", { parentTabId: "b1" }); - - const ids = getDescendantIds("a"); - // BFS: a, b1, b2, c1, c2 → reverse: c2, c1, b2, b1, a - expect(ids).toEqual(["c2", "c1", "b2", "b1", "a"]); - }); - - it("skips archived descendants (is_open = 0)", () => { - createTab("root", "Root"); - // Open child of root — should appear - createTab("open-child", "Open", { parentTabId: "root" }); - // Archived child — should be skipped together with its descendants - createTab("archived-child", "Archived", { parentTabId: "root" }); - archiveTab("archived-child"); - // Child of archived — data drift, should NOT appear (parent is archived) - createTab("orphan", "Orphan", { parentTabId: "archived-child" }); - - const ids = getDescendantIds("root"); - expect(ids).toEqual(["open-child", "root"]); - expect(ids).not.toContain("archived-child"); - expect(ids).not.toContain("orphan"); - }); - - it("handles a non-existent id gracefully", () => { - const ids = getDescendantIds("does-not-exist"); - expect(ids).toEqual(["does-not-exist"]); - }); - - it("defends against accidental parent_tab_id cycles", () => { - // Insert x first with a forward reference to y (y doesn't exist - // yet — the schema has no foreign key enforcement). Then insert - // y with parent_tab_id = x. Result: x.parent = y, y.parent = x. - createTab("x", "X", { parentTabId: "y" }); - createTab("y", "Y", { parentTabId: "x" }); - - // Must terminate — no infinite loop - const ids = getDescendantIds("x"); - expect(ids).toContain("x"); - expect(ids).toContain("y"); - expect(ids).toHaveLength(2); - }); - - it("uses createTab helper and asserts is_open flag", () => { - createTab("a1", "A1"); - createTab("b1", "B1", { parentTabId: "a1" }); - createTab("c1", "C1", { parentTabId: "b1" }); - - // All three should be open - expect(getTab("a1")?.isOpen).toBe(true); - expect(getTab("b1")?.isOpen).toBe(true); - expect(getTab("c1")?.isOpen).toBe(true); - - // getDescendantIds sees all three - const ids = getDescendantIds("a1"); - expect(ids).toEqual(["c1", "b1", "a1"]); - - // Archive the leaf, then it should disappear - archiveTab("c1"); - expect(getTab("c1")?.isOpen).toBe(false); - - const ids2 = getDescendantIds("a1"); - expect(ids2).toEqual(["b1", "a1"]); - }); -}); - -// --------------------------------------------------------------------------- -// resolveTabPrefix — git-style short-handle resolution -// --------------------------------------------------------------------------- -describe("resolveTabPrefix", () => { - it("returns none when the prefix is shorter than the minimum length", () => { - createTab("abcd1234-0000-4000-8000-000000000000", "A"); - // 3 chars < MIN_TAB_PREFIX_LENGTH (4) - expect(resolveTabPrefix("abc").status).toBe("none"); - }); - - it("returns none when no open tab matches", () => { - createTab("abcd1234-0000-4000-8000-000000000000", "A"); - expect(resolveTabPrefix("ffff").status).toBe("none"); - }); - - it("resolves a unique 4-char prefix to the single matching tab", () => { - createTab("abcd1234-0000-4000-8000-000000000000", "Alpha"); - createTab("9999aaaa-0000-4000-8000-000000000000", "Beta"); - const res = resolveTabPrefix("abcd"); - expect(res.status).toBe("ok"); - if (res.status === "ok") { - expect(res.tab.id).toBe("abcd1234-0000-4000-8000-000000000000"); - expect(res.tab.title).toBe("Alpha"); - } - }); - - it("resolves the full UUID (a maximal prefix)", () => { - createTab("abcd1234-0000-4000-8000-000000000000", "Alpha"); - const res = resolveTabPrefix("abcd1234-0000-4000-8000-000000000000"); - expect(res.status).toBe("ok"); - }); - - it("reports ambiguity when multiple open tabs share the prefix", () => { - createTab("abcd1111-0000-4000-8000-000000000000", "One"); - createTab("abcd2222-0000-4000-8000-000000000000", "Two"); - const res = resolveTabPrefix("abcd"); - expect(res.status).toBe("ambiguous"); - if (res.status === "ambiguous") { - expect(res.matches).toHaveLength(2); - expect(res.matches.map((m) => m.title).sort()).toEqual(["One", "Two"]); - } - }); - - it("disambiguates when one more character is supplied", () => { - createTab("abcd1111-0000-4000-8000-000000000000", "One"); - createTab("abcd2222-0000-4000-8000-000000000000", "Two"); - const res = resolveTabPrefix("abcd1"); - expect(res.status).toBe("ok"); - if (res.status === "ok") expect(res.tab.title).toBe("One"); - }); - - it("matches case-insensitively (UUIDs are lowercase; LIKE is ASCII-CI)", () => { - createTab("abcd1234-0000-4000-8000-000000000000", "Alpha"); - const res = resolveTabPrefix("ABCD"); - expect(res.status).toBe("ok"); - }); - - it("sanitizes LIKE wildcards so they cannot broaden the match", () => { - createTab("abcd1234-0000-4000-8000-000000000000", "Alpha"); - createTab("9999aaaa-0000-4000-8000-000000000000", "Beta"); - // `%` would match everything if not stripped; after sanitization the - // query is effectively `abcd%` which matches only Alpha. - const res = resolveTabPrefix("ab%d"); - // "ab%d" -> sanitized "abd" (3 chars) -> below min length -> none. - expect(res.status).toBe("none"); - }); - - it("excludes archived (closed) tabs from matches", () => { - createTab("abcd1234-0000-4000-8000-000000000000", "Alpha"); - archiveTab("abcd1234-0000-4000-8000-000000000000"); - expect(resolveTabPrefix("abcd").status).toBe("none"); - }); -}); - -// --------------------------------------------------------------------------- -// shortestUniquePrefix — display-handle derivation -// --------------------------------------------------------------------------- -describe("shortestUniquePrefix", () => { - it("returns a 4-char prefix when no other open tab collides", () => { - createTab("abcd1234-0000-4000-8000-000000000000", "Alpha"); - expect(shortestUniquePrefix("abcd1234-0000-4000-8000-000000000000")).toBe("abcd"); - }); - - it("grows the prefix one char at a time on a collision", () => { - createTab("abcd1111-0000-4000-8000-000000000000", "One"); - createTab("abcd2222-0000-4000-8000-000000000000", "Two"); - // First differing char is at index 4, so a 5-char prefix is unique. - expect(shortestUniquePrefix("abcd1111-0000-4000-8000-000000000000")).toBe("abcd1"); - expect(shortestUniquePrefix("abcd2222-0000-4000-8000-000000000000")).toBe("abcd2"); - }); - - it("ignores closed tabs when computing uniqueness", () => { - createTab("abcd1111-0000-4000-8000-000000000000", "One"); - createTab("abcd2222-0000-4000-8000-000000000000", "Two"); - archiveTab("abcd2222-0000-4000-8000-000000000000"); - // With Two closed, One no longer collides → back to 4 chars. - expect(shortestUniquePrefix("abcd1111-0000-4000-8000-000000000000")).toBe("abcd"); - }); -}); - -// --------------------------------------------------------------------------- -// updateTabPositions — drag-and-drop reorder persistence -// --------------------------------------------------------------------------- -describe("updateTabPositions", () => { - it("rewrites each tab's position to its index in the given order", () => { - createTab("a", "A"); // position 0 - createTab("b", "B"); // position 1 - createTab("c", "C"); // position 2 - - updateTabPositions(["c", "a", "b"]); - - // listOpenTabs orders by position → reflects the new order. - expect(listOpenTabs().map((t) => t.id)).toEqual(["c", "a", "b"]); - expect(getTab("c")?.position).toBe(0); - expect(getTab("a")?.position).toBe(1); - expect(getTab("b")?.position).toBe(2); - }); - - it("is a no-op for an empty list", () => { - createTab("a", "A"); - createTab("b", "B"); - updateTabPositions([]); - expect(listOpenTabs().map((t) => t.id)).toEqual(["a", "b"]); - }); - - it("ignores ids that don't exist without throwing", () => { - createTab("a", "A"); - expect(() => updateTabPositions(["ghost", "a"])).not.toThrow(); - // "a" took index 1 in the requested order. - expect(getTab("a")?.position).toBe(1); - }); -}); diff --git a/packages/core/tests/fixture/lsp/fake-lsp-server.js b/packages/core/tests/fixture/lsp/fake-lsp-server.js deleted file mode 100644 index d771ebd..0000000 --- a/packages/core/tests/fixture/lsp/fake-lsp-server.js +++ /dev/null @@ -1,195 +0,0 @@ -// Minimal JSON-RPC 2.0 LSP-like fake server over stdio, for testing the LSP -// client without a real language server binary. Ported from opencode's -// test/fixture/lsp/fake-lsp-server.js (trimmed to what dispatch's client and -// manager exercise: initialize, didOpen/didChange, push + pull diagnostics). -// -// Test hooks (custom JSON-RPC methods the test driver can call): -// test/get-initialize-params → returns the params sent to `initialize` -// test/get-last-change → returns the last `didChange` params -// test/publish-diagnostics → forwards a `publishDiagnostics` push -// test/configure-pull-diagnostics → sets up pull-diagnostic responses -// test/get-diagnostic-request-count→ how many pull requests were received - -let nextId = 1; -let readBuffer = Buffer.alloc(0); -let lastChange = null; -let initializeParams = null; -let diagnosticRequestCount = 0; -let registeredCapability = false; -let pullConfig = { - registerOn: undefined, - registrations: [], - documentDiagnostics: [], - workspaceDiagnostics: [], - hasDiagnosticProvider: false, -}; - -function encode(message) { - const json = JSON.stringify(message); - const header = `Content-Length: ${Buffer.byteLength(json, "utf8")}\r\n\r\n`; - return Buffer.concat([Buffer.from(header, "utf8"), Buffer.from(json, "utf8")]); -} - -function decodeFrames(buffer) { - const results = []; - while (true) { - const idx = buffer.indexOf("\r\n\r\n"); - if (idx === -1) break; - const header = buffer.slice(0, idx).toString("utf8"); - const match = /Content-Length:\s*(\d+)/i.exec(header); - const length = match ? parseInt(match[1], 10) : 0; - const bodyStart = idx + 4; - const bodyEnd = bodyStart + length; - if (buffer.length < bodyEnd) break; - results.push(buffer.slice(bodyStart, bodyEnd).toString("utf8")); - buffer = buffer.slice(bodyEnd); - } - return { messages: results, rest: buffer }; -} - -function send(message) { - process.stdout.write(encode(message)); -} -function sendRequest(method, params) { - const id = nextId++; - send({ jsonrpc: "2.0", id, method, params }); - return id; -} -function sendResponse(id, result) { - send({ jsonrpc: "2.0", id, result }); -} -function sendNotification(method, params) { - send({ jsonrpc: "2.0", method, params }); -} - -function maybeRegister(method) { - if (pullConfig.registerOn !== method || registeredCapability) return; - registeredCapability = true; - sendRequest("client/registerCapability", { - registrations: pullConfig.registrations.map((registration, index) => ({ - id: registration.id ?? `pull-${index}`, - method: registration.method ?? "textDocument/diagnostic", - registerOptions: registration.registerOptions ?? registration, - })), - }); -} - -function handle(raw) { - let data; - try { - data = JSON.parse(raw); - } catch { - return; - } - - if (data.method === "initialize") { - initializeParams = data.params; - sendResponse(data.id, { - capabilities: { - textDocumentSync: { change: 2, openClose: true }, - ...(pullConfig.hasDiagnosticProvider - ? { - diagnosticProvider: { - identifier: "fake", - interFileDependencies: false, - workspaceDiagnostics: false, - }, - } - : {}), - }, - }); - return; - } - - if (data.method === "test/get-initialize-params") { - sendResponse(data.id, initializeParams); - return; - } - - if (data.method === "initialized" || data.method === "workspace/didChangeConfiguration") { - return; - } - - if (data.method === "textDocument/didOpen") { - maybeRegister("didOpen"); - return; - } - - if (data.method === "textDocument/didChange") { - lastChange = data.params; - maybeRegister("didChange"); - return; - } - - if (data.method === "workspace/didChangeWatchedFiles") { - return; - } - - if (data.method === "test/configure-pull-diagnostics") { - pullConfig = { - registerOn: data.params?.registerOn, - registrations: data.params?.registrations ?? [], - documentDiagnostics: data.params?.documentDiagnostics ?? [], - workspaceDiagnostics: data.params?.workspaceDiagnostics ?? [], - hasDiagnosticProvider: data.params?.hasDiagnosticProvider ?? false, - }; - registeredCapability = false; - sendResponse(data.id, null); - return; - } - - if (data.method === "test/publish-diagnostics") { - sendNotification("textDocument/publishDiagnostics", data.params); - sendResponse(data.id, null); - return; - } - - if (data.method === "test/get-last-change") { - sendResponse(data.id, lastChange); - return; - } - - if (data.method === "test/get-diagnostic-request-count") { - sendResponse(data.id, diagnosticRequestCount); - return; - } - - if (data.method === "textDocument/diagnostic") { - diagnosticRequestCount += 1; - sendResponse(data.id, { kind: "full", items: pullConfig.documentDiagnostics }); - return; - } - - if (data.method === "workspace/diagnostic") { - diagnosticRequestCount += 1; - sendResponse(data.id, { items: pullConfig.workspaceDiagnostics }); - return; - } - - if (data.method === "textDocument/hover") { - sendResponse(data.id, { contents: { kind: "plaintext", value: "fake hover" } }); - return; - } - - if (data.method === "textDocument/definition") { - sendResponse(data.id, [ - { - uri: data.params?.textDocument?.uri, - range: { start: { line: 0, character: 0 }, end: { line: 0, character: 1 } }, - }, - ]); - return; - } - - // Default: respond null to any other request so the client never hangs. - if (typeof data.id !== "undefined") { - sendResponse(data.id, null); - } -} - -process.stdin.on("data", (chunk) => { - readBuffer = Buffer.concat([readBuffer, chunk]); - const { messages, rest } = decodeFrames(readBuffer); - readBuffer = rest; - for (const message of messages) handle(message); -}); diff --git a/packages/core/tests/llm/anthropic-oauth-transform.test.ts b/packages/core/tests/llm/anthropic-oauth-transform.test.ts deleted file mode 100644 index a8bb156..0000000 --- a/packages/core/tests/llm/anthropic-oauth-transform.test.ts +++ /dev/null @@ -1,137 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { transformClaudeOAuthBody } from "../../src/llm/anthropic-oauth-transform.js"; - -const IDENTITY = "You are Claude Code, Anthropic's official CLI for Claude."; -const BILLING = - "x-anthropic-billing-header: cc_version=2.1.112.abc; cc_entrypoint=sdk-cli; cch=12345;"; - -interface WireBody { - system?: Array<{ type: string; text: string; cache_control?: unknown }>; - messages?: Array<{ role: string; content: unknown }>; -} - -/** - * Build the body shape Dispatch produces: ONE system block holding - * `<billing>\n<identity>\n\n<systemPrompt>`, marked with cache_control by - * `applyAnthropicCaching`. - */ -function dispatchBody(systemPrompt: string, firstUser = "hello"): string { - return JSON.stringify({ - model: "claude-opus-4-8", - system: [ - { - type: "text", - text: `${BILLING}\n${IDENTITY}\n\n${systemPrompt}`, - cache_control: { type: "ephemeral" }, - }, - ], - messages: [{ role: "user", content: firstUser }], - }); -} - -function parse(out: BodyInit | null | undefined): WireBody { - return JSON.parse(out as string) as WireBody; -} - -describe("transformClaudeOAuthBody", () => { - it("isolates the billing header as system[0] WITHOUT cache_control", () => { - const result = parse(transformClaudeOAuthBody(dispatchBody("You are Dispatch. Use tools."))); - const sys = result.system ?? []; - expect(sys[0]?.text).toBe(BILLING); - expect(sys[0]?.cache_control).toBeUndefined(); - }); - - it("keeps the identity as a separate system block and carries the cache_control there", () => { - const result = parse(transformClaudeOAuthBody(dispatchBody("You are Dispatch. Use tools."))); - const sys = result.system ?? []; - expect(sys[1]?.text).toBe(IDENTITY); - expect(sys[1]?.cache_control).toEqual({ type: "ephemeral" }); - // Only billing + identity remain in system[] — the third-party prompt was moved out. - expect(sys).toHaveLength(2); - }); - - it("relocates the third-party system prompt into the first user message", () => { - const result = parse( - transformClaudeOAuthBody(dispatchBody("You are Dispatch. Use tools.", "do the thing")), - ); - const sys = result.system ?? []; - // The system prompt must NOT appear anywhere in system[]. - expect(sys.some((b) => b.text.includes("You are Dispatch"))).toBe(false); - // It is prepended to the first user message. - expect(result.messages?.[0]?.content).toBe("You are Dispatch. Use tools.\n\ndo the thing"); - }); - - it("prepends a text block when the first user message uses array content", () => { - const body = JSON.stringify({ - system: [ - { - type: "text", - text: `${BILLING}\n${IDENTITY}\n\nDispatch instructions here.`, - cache_control: { type: "ephemeral" }, - }, - ], - messages: [{ role: "user", content: [{ type: "text", text: "user text" }] }], - }); - const result = parse(transformClaudeOAuthBody(body)); - const content = result.messages?.[0]?.content as Array<{ type: string; text: string }>; - expect(content[0]).toEqual({ type: "text", text: "Dispatch instructions here." }); - expect(content[1]).toEqual({ type: "text", text: "user text" }); - }); - - it("does not carry cache_control to the identity when the source had none", () => { - const body = JSON.stringify({ - system: [{ type: "text", text: `${BILLING}\n${IDENTITY}\n\nInstr.` }], - messages: [{ role: "user", content: "hi" }], - }); - const result = parse(transformClaudeOAuthBody(body)); - expect(result.system?.[1]?.cache_control).toBeUndefined(); - }); - - it("keeps the prompt as a cached system block when there is no user message", () => { - const body = JSON.stringify({ - system: [ - { - type: "text", - text: `${BILLING}\n${IDENTITY}\n\nInstr only.`, - cache_control: { type: "ephemeral" }, - }, - ], - messages: [], - }); - const result = parse(transformClaudeOAuthBody(body)); - const sys = result.system ?? []; - expect(sys[0]?.text).toBe(BILLING); - expect(sys[1]?.text).toBe(IDENTITY); - expect(sys[2]?.text).toBe("Instr only."); - expect(sys[2]?.cache_control).toEqual({ type: "ephemeral" }); - }); - - it("leaves non-Claude-Code bodies (no identity string) untouched", () => { - const body = JSON.stringify({ - system: [{ type: "text", text: "Some unrelated system prompt." }], - messages: [{ role: "user", content: "hi" }], - }); - // Returned unchanged (same reference string, byte-identical). - expect(transformClaudeOAuthBody(body)).toBe(body); - }); - - it("returns non-string bodies unchanged", () => { - const buf = new Uint8Array([1, 2, 3]); - expect(transformClaudeOAuthBody(buf)).toBe(buf); - expect(transformClaudeOAuthBody(undefined)).toBeUndefined(); - expect(transformClaudeOAuthBody(null)).toBeNull(); - }); - - it("returns invalid JSON unchanged", () => { - const garbage = "{not json"; - expect(transformClaudeOAuthBody(garbage)).toBe(garbage); - }); - - it("never emits more than the 4 cache_control breakpoints Anthropic allows", () => { - const result = parse(transformClaudeOAuthBody(dispatchBody("Big system prompt."))); - const all = result.system ?? []; - const cacheBlocks = all.filter((b) => b.cache_control != null); - // Only the identity block is marked here — well under the limit of 4. - expect(cacheBlocks.length).toBeLessThanOrEqual(4); - }); -}); diff --git a/packages/core/tests/llm/provider.test.ts b/packages/core/tests/llm/provider.test.ts deleted file mode 100644 index c8c0877..0000000 --- a/packages/core/tests/llm/provider.test.ts +++ /dev/null @@ -1,271 +0,0 @@ -import { describe, expect, it, vi } from "vitest"; - -// Mock @ai-sdk/anthropic to capture what options createAnthropic is called with -const mockAnthropicInstance = vi.fn((modelId: string) => ({ - specificationVersion: "v3" as const, - provider: "anthropic.messages", - modelId, - supportedUrls: {}, - doGenerate: vi.fn(), - doStream: vi.fn(), -})); -const mockCreateAnthropic = vi.fn(() => mockAnthropicInstance); -vi.mock("@ai-sdk/anthropic", () => ({ - createAnthropic: mockCreateAnthropic, -})); - -// Mock @ai-sdk/openai-compatible to capture what options createOpenAICompatible -// is called with, and what model id the returned factory is called with. -const mockOpenAICompatibleFactory = vi.fn((modelId: string) => ({ - specificationVersion: "v3" as const, - provider: "openai-compatible", - modelId, - supportedUrls: {}, - doGenerate: vi.fn(), - doStream: vi.fn(), -})); -const mockCreateOpenAICompatible = vi.fn(() => mockOpenAICompatibleFactory); -vi.mock("@ai-sdk/openai-compatible", () => ({ - createOpenAICompatible: mockCreateOpenAICompatible, -})); - -const { createProvider } = await import("../../src/llm/provider.js"); - -describe("createProvider (default OpenAI-compatible path)", () => { - it("does not wrap the model in a middleware layer — v6 SDK handles reasoning round-trip natively", () => { - mockCreateOpenAICompatible.mockClear(); - mockOpenAICompatibleFactory.mockClear(); - - const model = createProvider({ - apiKey: "test-key", - baseURL: "https://example.com/v1", - })("deepseek-v4-pro"); - - // The factory should have been invoked with the model id directly, - // without going through `wrapLanguageModel`. If a middleware were - // still in place, the returned object would carry an `_middleware` - // property (set by our test mock pattern). The bare provider model - // has no such property — verifying the v4-era normalizeMessages - // middleware is gone. - expect(mockOpenAICompatibleFactory).toHaveBeenCalledWith("deepseek-v4-pro"); - expect((model as { _middleware?: unknown })._middleware).toBeUndefined(); - }); - - it("passes name, apiKey, baseURL to createOpenAICompatible", () => { - mockCreateOpenAICompatible.mockClear(); - - createProvider({ - apiKey: "zen-key", - baseURL: "https://opencode.ai/zen/v1", - })("deepseek-v4-pro"); - - // We assert by property rather than full-object equality because the - // provider also passes a `fetch:` wrapper (the debug-logger tee). The - // load-bearing wiring is name/apiKey/baseURL; the fetch field is - // tested separately via the wrap-fetch tests. - expect(mockCreateOpenAICompatible).toHaveBeenCalledOnce(); - const zenArgs = mockCreateOpenAICompatible.mock.calls[0]?.[0] as Record<string, unknown>; - expect(zenArgs.name).toBe("opencode-zen"); - expect(zenArgs.apiKey).toBe("zen-key"); - expect(zenArgs.baseURL).toBe("https://opencode.ai/zen/v1"); - expect(typeof zenArgs.fetch).toBe("function"); - }); -}); - -describe("createClaudeOAuthProvider", () => { - it("passes authToken (not apiKey) to createAnthropic for OAuth flow", () => { - mockCreateAnthropic.mockClear(); - - createProvider({ - provider: "anthropic", - apiKey: "fallback-api-key", - baseURL: "", - claudeCredentials: { accessToken: "oauth-access-token" }, - })("claude-opus-4-5"); - - expect(mockCreateAnthropic).toHaveBeenCalledOnce(); - const callArgs = mockCreateAnthropic.mock.calls[0]?.[0] as Record<string, unknown>; - expect(callArgs.authToken).toBe("oauth-access-token"); - expect(callArgs.apiKey).toBeUndefined(); - }); - - it("falls back to apiKey as authToken when claudeCredentials are absent", () => { - mockCreateAnthropic.mockClear(); - - createProvider({ - provider: "anthropic", - apiKey: "sk-ant-api-key", - baseURL: "", - })("claude-opus-4-5"); - - expect(mockCreateAnthropic).toHaveBeenCalledOnce(); - const callArgs = mockCreateAnthropic.mock.calls[0]?.[0] as Record<string, unknown>; - expect(callArgs.authToken).toBe("sk-ant-api-key"); - expect(callArgs.apiKey).toBeUndefined(); - }); - - it("includes required Claude CLI headers", () => { - mockCreateAnthropic.mockClear(); - - createProvider({ - provider: "anthropic", - apiKey: "test-key", - baseURL: "", - claudeCredentials: { accessToken: "tok" }, - })("claude-opus-4-5"); - - const callArgs = mockCreateAnthropic.mock.calls[0]?.[0] as Record< - string, - Record<string, string> - >; - expect(callArgs.headers?.["anthropic-dangerous-direct-browser-access"]).toBe("true"); - expect(callArgs.headers?.["x-app"]).toBe("cli"); - expect(callArgs.headers?.["user-agent"]).toMatch(/claude-cli/); - }); - - it("installs a fetch wrapper that restructures the body and stamps Claude Code session headers", async () => { - mockCreateAnthropic.mockClear(); - - // Capture what global fetch receives after the wrapper runs. - const globalFetchMock = vi.fn(async () => new Response("{}", { status: 200 })); - const prevFetch = globalThis.fetch; - globalThis.fetch = globalFetchMock as unknown as typeof fetch; - try { - createProvider({ - provider: "anthropic", - apiKey: "test-key", - baseURL: "", - claudeCredentials: { accessToken: "tok" }, - })("claude-opus-4-8"); - - const callArgs = mockCreateAnthropic.mock.calls[0]?.[0] as { fetch?: typeof fetch }; - expect(typeof callArgs.fetch).toBe("function"); - - const IDENTITY = "You are Claude Code, Anthropic's official CLI for Claude."; - const BILLING = - "x-anthropic-billing-header: cc_version=2.1.112.x; cc_entrypoint=sdk-cli; cch=abcde;"; - const body = JSON.stringify({ - system: [ - { - type: "text", - text: `${BILLING}\n${IDENTITY}\n\nDispatch system prompt.`, - cache_control: { type: "ephemeral" }, - }, - ], - messages: [{ role: "user", content: "hi there" }], - }); - - await callArgs.fetch?.("https://api.anthropic.com/v1/messages", { - method: "POST", - body, - headers: { "content-type": "application/json" }, - }); - - expect(globalFetchMock).toHaveBeenCalledOnce(); - const [, init] = globalFetchMock.mock.calls[0] as unknown as [unknown, RequestInit]; - - // Body was restructured: billing isolated, third-party prompt moved to user msg. - const sent = JSON.parse(init.body as string) as { - system: Array<{ text: string; cache_control?: unknown }>; - messages: Array<{ content: string }>; - }; - expect(sent.system).toHaveLength(2); - expect(sent.system[0]?.text).toBe(BILLING); - expect(sent.system[0]?.cache_control).toBeUndefined(); - expect(sent.system[1]?.text).toBe(IDENTITY); - expect(sent.messages[0]?.content).toBe("Dispatch system prompt.\n\nhi there"); - - // Claude Code session headers were stamped. - const headers = new Headers(init.headers); - expect(headers.get("X-Claude-Code-Session-Id")).toBeTruthy(); - expect(headers.get("x-client-request-id")).toBeTruthy(); - } finally { - globalThis.fetch = prevFetch; - } - }); - - it("sends the anthropic-beta header so prompt-caching is honored (notes/claude-report.md Root Cause 1)", () => { - // Without `anthropic-beta: ...,prompt-caching-scope-2026-01-05,...` the - // Anthropic API silently ignores every `cache_control` marker we attach - // to messages, producing a 0% cache hit rate. `@ai-sdk/anthropic` does - // NOT inject this beta on its own — it only derives betas from tool - // definitions — so the OAuth provider MUST set it on its config headers. - mockCreateAnthropic.mockClear(); - - createProvider({ - provider: "anthropic", - apiKey: "test-key", - baseURL: "", - claudeCredentials: { accessToken: "tok" }, - })("claude-opus-4-5"); - - const callArgs = mockCreateAnthropic.mock.calls[0]?.[0] as Record< - string, - Record<string, string> - >; - const betaHeader = callArgs.headers?.["anthropic-beta"]; - expect(betaHeader).toBeDefined(); - const betas = (betaHeader ?? "").split(",").map((b) => b.trim()); - // The load-bearing caching + oauth betas must be present. - expect(betas).toContain("prompt-caching-scope-2026-01-05"); - expect(betas).toContain("oauth-2025-04-20"); - }); - - it("uses default Anthropic baseURL when none provided", () => { - mockCreateAnthropic.mockClear(); - - createProvider({ - provider: "anthropic", - apiKey: "test-key", - baseURL: "", - claudeCredentials: { accessToken: "tok" }, - })("claude-opus-4-5"); - - const callArgs = mockCreateAnthropic.mock.calls[0]?.[0] as Record<string, string>; - expect(callArgs.baseURL).toBe("https://api.anthropic.com/v1"); - }); - - it("uses configured baseURL when provided", () => { - mockCreateAnthropic.mockClear(); - - createProvider({ - provider: "anthropic", - apiKey: "test-key", - baseURL: "https://custom.proxy.example.com/v1", - claudeCredentials: { accessToken: "tok" }, - })("claude-opus-4-5"); - - const callArgs = mockCreateAnthropic.mock.calls[0]?.[0] as Record<string, string>; - expect(callArgs.baseURL).toBe("https://custom.proxy.example.com/v1"); - }); -}); - -describe("createApiKeyAnthropicProvider", () => { - it("passes apiKey (not authToken) to createAnthropic", () => { - mockCreateAnthropic.mockClear(); - - createProvider({ - provider: "opencode-anthropic", - apiKey: "zen-api-key", - baseURL: "", - })("minimax-model"); - - expect(mockCreateAnthropic).toHaveBeenCalledOnce(); - const callArgs = mockCreateAnthropic.mock.calls[0]?.[0] as Record<string, unknown>; - expect(callArgs.apiKey).toBe("zen-api-key"); - expect(callArgs.authToken).toBeUndefined(); - }); - - it("uses default OpenCode Zen baseURL when none provided", () => { - mockCreateAnthropic.mockClear(); - - createProvider({ - provider: "opencode-anthropic", - apiKey: "zen-api-key", - baseURL: "", - })("minimax-model"); - - const callArgs = mockCreateAnthropic.mock.calls[0]?.[0] as Record<string, string>; - expect(callArgs.baseURL).toBe("https://opencode.ai/zen/go/v1"); - }); -}); diff --git a/packages/core/tests/lsp/client.test.ts b/packages/core/tests/lsp/client.test.ts deleted file mode 100644 index 8daf8ab..0000000 --- a/packages/core/tests/lsp/client.test.ts +++ /dev/null @@ -1,146 +0,0 @@ -import { spawn } from "node:child_process"; -import { mkdtemp, rm, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { dirname, join } from "node:path"; -import { fileURLToPath, pathToFileURL } from "node:url"; -import { afterEach, beforeEach, describe, expect, it } from "vitest"; -import type { Diagnostic } from "vscode-languageserver-types"; -import { createLspClient, type LspServerHandle } from "../../src/lsp/client.js"; - -const FIXTURE = join(dirname(fileURLToPath(import.meta.url)), "../fixture/lsp/fake-lsp-server.js"); - -function spawnFakeServer(): LspServerHandle { - const proc = spawn(process.execPath, [FIXTURE], { stdio: "pipe" }); - return { process: proc as LspServerHandle["process"] }; -} - -const ERROR_DIAG: Diagnostic = { - range: { start: { line: 0, character: 0 }, end: { line: 0, character: 5 } }, - severity: 1, - message: "fake type error", - source: "Fake", -}; - -describe("lsp/client (fake server)", () => { - let workDir: string; - - beforeEach(async () => { - workDir = await mkdtemp(join(tmpdir(), "dispatch-lsp-")); - }); - afterEach(async () => { - await rm(workDir, { recursive: true, force: true }); - }); - - it("completes the initialize handshake and forwards initializationOptions", async () => { - const handle = spawnFakeServer(); - handle.initialization = { "luau-lsp": { platform: { type: "roblox" } } }; - const client = await createLspClient({ - serverID: "fake", - server: handle, - root: workDir, - directory: workDir, - }); - - const params = await client.connection.sendRequest<{ initializationOptions?: unknown }>( - "test/get-initialize-params", - {}, - ); - expect(params.initializationOptions).toEqual({ - "luau-lsp": { platform: { type: "roblox" } }, - }); - await client.shutdown(); - }); - - it("opens a file and receives push diagnostics", async () => { - const handle = spawnFakeServer(); - const client = await createLspClient({ - serverID: "fake", - server: handle, - root: workDir, - directory: workDir, - }); - - const file = join(workDir, "a.luau"); - await writeFile(file, "local x = 1\n"); - const version = await client.notifyOpen(file); - expect(version).toBe(0); - - // Drive a push from the fake server, then assert it lands in the map. - await client.connection.sendRequest("test/publish-diagnostics", { - uri: pathToFileURL(file).href, - diagnostics: [ERROR_DIAG], - }); - await new Promise((r) => setTimeout(r, 50)); - - expect(client.diagnostics.get(file)?.[0]?.message).toBe("fake type error"); - await client.shutdown(); - }); - - it("bumps the document version on re-open (didChange)", async () => { - const handle = spawnFakeServer(); - const client = await createLspClient({ - serverID: "fake", - server: handle, - root: workDir, - directory: workDir, - }); - const file = join(workDir, "a.luau"); - await writeFile(file, "local x = 1\n"); - expect(await client.notifyOpen(file)).toBe(0); - await writeFile(file, "local x = 2\n"); - expect(await client.notifyOpen(file)).toBe(1); - - const lastChange = await client.connection.sendRequest<{ textDocument?: { version?: number } }>( - "test/get-last-change", - {}, - ); - expect(lastChange?.textDocument?.version).toBe(1); - await client.shutdown(); - }); - - it("waits for pull diagnostics when the server advertises a diagnostic provider", async () => { - const handle = spawnFakeServer(); - const client = await createLspClient({ - serverID: "fake", - server: handle, - root: workDir, - directory: workDir, - }); - // Tell the fake server (before initialize? no — it persists) to answer - // pull requests. We configure AFTER connect; the static provider flag is - // read at initialize, so this test exercises the dynamic registration - // path instead. - await client.connection.sendRequest("test/configure-pull-diagnostics", { - registerOn: "didOpen", - registrations: [{ id: "d1", registerOptions: { identifier: "fake" } }], - documentDiagnostics: [ERROR_DIAG], - }); - - const file = join(workDir, "a.luau"); - await writeFile(file, "bad\n"); - const version = await client.notifyOpen(file); - await client.waitForDiagnostics({ path: file, version, mode: "document" }); - - expect(client.diagnostics.get(file)?.some((d) => d.message === "fake type error")).toBe(true); - await client.shutdown(); - }); - - it("request() passes through to the server (hover)", async () => { - const handle = spawnFakeServer(); - const client = await createLspClient({ - serverID: "fake", - server: handle, - root: workDir, - directory: workDir, - }); - const file = join(workDir, "a.luau"); - await writeFile(file, "local x = 1\n"); - await client.notifyOpen(file); - const hover = await client.request<{ contents?: { value?: string } }>("textDocument/hover", { - textDocument: { uri: pathToFileURL(file).href }, - position: { line: 0, character: 6 }, - }); - expect(hover?.contents?.value).toBe("fake hover"); - await client.shutdown(); - }); -}); diff --git a/packages/core/tests/lsp/diagnostic.test.ts b/packages/core/tests/lsp/diagnostic.test.ts deleted file mode 100644 index 93ffde9..0000000 --- a/packages/core/tests/lsp/diagnostic.test.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { describe, expect, it } from "vitest"; -import type { Diagnostic } from "vscode-languageserver-types"; -import { pretty, report } from "../../src/lsp/diagnostic.js"; - -function diag(partial: Partial<Diagnostic> & { message: string }): Diagnostic { - return { - range: { start: { line: 0, character: 0 }, end: { line: 0, character: 1 } }, - severity: 1, - ...partial, - }; -} - -describe("lsp/diagnostic", () => { - describe("pretty", () => { - it("renders 1-based line/col with severity label", () => { - const out = pretty( - diag({ - message: "Expected number", - range: { start: { line: 4, character: 2 }, end: { line: 4, character: 8 } }, - }), - ); - expect(out).toBe("ERROR [5:3] Expected number"); - }); - - it("maps severities to labels", () => { - expect(pretty(diag({ message: "w", severity: 2 }))).toMatch(/^WARN /); - expect(pretty(diag({ message: "i", severity: 3 }))).toMatch(/^INFO /); - expect(pretty(diag({ message: "h", severity: 4 }))).toMatch(/^HINT /); - }); - - it("defaults missing severity to ERROR", () => { - expect(pretty(diag({ message: "x", severity: undefined }))).toMatch(/^ERROR /); - }); - }); - - describe("report", () => { - it("returns empty string when there are no errors", () => { - expect(report("a.luau", [])).toBe(""); - // Warnings only → still empty (errors-only). - expect(report("a.luau", [diag({ message: "w", severity: 2 })])).toBe(""); - }); - - it("wraps errors in a <diagnostics file> block", () => { - const out = report("src/a.luau", [diag({ message: "boom" })]); - expect(out).toContain('<diagnostics file="src/a.luau">'); - expect(out).toContain("ERROR [1:1] boom"); - expect(out).toContain("</diagnostics>"); - }); - - it("filters out non-error severities", () => { - const out = report("a.luau", [ - diag({ message: "err" }), - diag({ message: "warn", severity: 2 }), - ]); - expect(out).toContain("err"); - expect(out).not.toContain("warn"); - }); - - it("caps at 20 and notes the remainder", () => { - const issues = Array.from({ length: 25 }, (_, i) => diag({ message: `e${i}` })); - const out = report("a.luau", issues); - expect(out).toContain("... and 5 more"); - expect(out).toContain("e0"); - expect(out).not.toContain("e24"); - }); - }); -}); diff --git a/packages/core/tests/lsp/luau-lsp.smoke.test.ts b/packages/core/tests/lsp/luau-lsp.smoke.test.ts deleted file mode 100644 index 381435b..0000000 --- a/packages/core/tests/lsp/luau-lsp.smoke.test.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { execSync } from "node:child_process"; -import { mkdtemp, rm, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { afterEach, beforeEach, describe, expect, it } from "vitest"; -import { LspManager } from "../../src/lsp/manager.js"; -import { resolveServersFromConfig } from "../../src/lsp/server.js"; - -/** - * Opt-in smoke test against the REAL luau-lsp binary. Skipped automatically - * (never fails CI) when `luau-lsp` is not on PATH — mirrors opencode's - * platform-guarded launch test. When the binary IS present, it proves the - * end-to-end path: spawn → initialize handshake → didOpen → real diagnostics. - */ -function hasLuauLsp(): boolean { - try { - execSync("luau-lsp --version", { stdio: "ignore" }); - return true; - } catch { - return false; - } -} - -const RUN = hasLuauLsp(); - -describe.skipIf(!RUN)("luau-lsp real-binary smoke", () => { - let root: string; - let manager: LspManager; - - beforeEach(async () => { - root = await mkdtemp(join(tmpdir(), "dispatch-luau-smoke-")); - manager = new LspManager(); - }); - afterEach(async () => { - await manager.shutdownAll(); - await rm(root, { recursive: true, force: true }); - }); - - it("reports a real type error for a bad .luau file", async () => { - const servers = resolveServersFromConfig({ - "luau-lsp": { - command: ["luau-lsp", "lsp"], - extensions: [".luau"], - initialization: { - "luau-lsp": { - platform: { type: "roblox" }, - diagnostics: { strictDatamodelTypes: false }, - }, - }, - }, - }); - - const file = join(root, "bad.luau"); - await writeFile(file, 'local x: number = "not a number"\nprint(x)\n'); - - await manager.touchFile({ file, root, servers, mode: "document" }); - const diagnostics = manager.getDiagnostics({ root, servers, file }); - const messages = (diagnostics[file] ?? []).map((d) => d.message).join("\n"); - - expect(messages.length).toBeGreaterThan(0); - expect(messages.toLowerCase()).toContain("number"); - }, 60_000); -}); diff --git a/packages/core/tests/lsp/manager.test.ts b/packages/core/tests/lsp/manager.test.ts deleted file mode 100644 index e720413..0000000 --- a/packages/core/tests/lsp/manager.test.ts +++ /dev/null @@ -1,120 +0,0 @@ -import { spawn } from "node:child_process"; -import { mkdtemp, rm, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { dirname, join } from "node:path"; -import { fileURLToPath, pathToFileURL } from "node:url"; -import { afterEach, beforeEach, describe, expect, it } from "vitest"; -import type { Diagnostic } from "vscode-languageserver-types"; -import { LspManager } from "../../src/lsp/manager.js"; -import type { ResolvedLspServer } from "../../src/lsp/server.js"; - -const FIXTURE = join(dirname(fileURLToPath(import.meta.url)), "../fixture/lsp/fake-lsp-server.js"); - -function makeServer(id: string, extensions: string[]) { - const counter = { count: 0 }; - const server: ResolvedLspServer = { - id, - extensions, - spawn() { - counter.count += 1; - const proc = spawn(process.execPath, [FIXTURE], { stdio: "pipe" }); - return { process: proc as never }; - }, - }; - return { server, counter }; -} - -describe("lsp/manager (fake server)", () => { - let root: string; - let manager: LspManager; - - beforeEach(async () => { - root = await mkdtemp(join(tmpdir(), "dispatch-lspmgr-")); - manager = new LspManager(); - }); - afterEach(async () => { - await manager.shutdownAll(); - await rm(root, { recursive: true, force: true }); - }); - - it("hasServerForFile matches by extension", () => { - const { server } = makeServer("fake", [".luau"]); - expect(manager.hasServerForFile(join(root, "a.luau"), [server])).toBe(true); - expect(manager.hasServerForFile(join(root, "a.ts"), [server])).toBe(false); - }); - - it("spawns lazily and reuses the client across calls", async () => { - const { server, counter } = makeServer("fake", [".luau"]); - const file = join(root, "a.luau"); - await writeFile(file, "local x = 1\n"); - - const c1 = await manager.getClients({ file, root, servers: [server] }); - const c2 = await manager.getClients({ file, root, servers: [server] }); - expect(c1).toHaveLength(1); - expect(c2).toHaveLength(1); - expect(c1[0]).toBe(c2[0]); - expect(counter.count).toBe(1); - }); - - it("does not spawn for a non-matching extension", async () => { - const { server, counter } = makeServer("fake", [".luau"]); - const file = join(root, "a.ts"); - await writeFile(file, "const x = 1\n"); - const clients = await manager.getClients({ file, root, servers: [server] }); - expect(clients).toHaveLength(0); - expect(counter.count).toBe(0); - }); - - it("touchFile + getDiagnostics surfaces a pushed diagnostic", async () => { - const { server } = makeServer("fake", [".luau"]); - const file = join(root, "a.luau"); - await writeFile(file, "bad code\n"); - - await manager.touchFile({ file, root, servers: [server] }); - const [client] = await manager.getClients({ file, root, servers: [server] }); - // Drive a push through the fake server. - const diag: Diagnostic = { - range: { start: { line: 0, character: 0 }, end: { line: 0, character: 3 } }, - severity: 1, - message: "manager error", - }; - await client.connection.sendRequest("test/publish-diagnostics", { - uri: pathToFileURL(file).href, - diagnostics: [diag], - }); - await new Promise((r) => setTimeout(r, 50)); - - const result = manager.getDiagnostics({ root, servers: [server], file }); - expect(result[file]?.[0]?.message).toBe("manager error"); - }); - - it("request() forwards to clients and flattens results", async () => { - const { server } = makeServer("fake", [".luau"]); - const file = join(root, "a.luau"); - await writeFile(file, "local x = 1\n"); - await manager.touchFile({ file, root, servers: [server] }); - - const results = await manager.request({ - file, - root, - servers: [server], - method: "textDocument/definition", - params: { - textDocument: { uri: pathToFileURL(file).href }, - position: { line: 0, character: 6 }, - }, - }); - expect(results.length).toBeGreaterThan(0); - }); - - it("shutdownAll clears state so the next call respawns", async () => { - const { server, counter } = makeServer("fake", [".luau"]); - const file = join(root, "a.luau"); - await writeFile(file, "local x = 1\n"); - await manager.getClients({ file, root, servers: [server] }); - expect(counter.count).toBe(1); - await manager.shutdownAll(); - await manager.getClients({ file, root, servers: [server] }); - expect(counter.count).toBe(2); - }); -}); diff --git a/packages/core/tests/lsp/server.test.ts b/packages/core/tests/lsp/server.test.ts deleted file mode 100644 index bdaf83d..0000000 --- a/packages/core/tests/lsp/server.test.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { resolveServersFromConfig } from "../../src/lsp/server.js"; - -describe("lsp/server resolveServersFromConfig", () => { - it("returns [] for undefined config", () => { - expect(resolveServersFromConfig(undefined)).toEqual([]); - }); - - it("resolves a server entry with id + extensions", () => { - const servers = resolveServersFromConfig({ - "luau-lsp": { command: ["luau-lsp", "lsp"], extensions: [".luau"] }, - }); - expect(servers).toHaveLength(1); - expect(servers[0]?.id).toBe("luau-lsp"); - expect(servers[0]?.extensions).toEqual([".luau"]); - expect(typeof servers[0]?.spawn).toBe("function"); - }); - - it("skips disabled entries", () => { - const servers = resolveServersFromConfig({ - "luau-lsp": { command: ["luau-lsp", "lsp"], extensions: [".luau"], disabled: true }, - }); - expect(servers).toEqual([]); - }); - - it("skips entries with empty command or extensions", () => { - const servers = resolveServersFromConfig({ - noCommand: { command: [], extensions: [".luau"] }, - noExt: { command: ["x"], extensions: [] }, - }); - expect(servers).toEqual([]); - }); - - it("resolves multiple servers", () => { - const servers = resolveServersFromConfig({ - a: { command: ["a"], extensions: [".luau"] }, - b: { command: ["b"], extensions: [".lua"] }, - }); - expect(servers.map((s) => s.id).sort()).toEqual(["a", "b"]); - }); -}); diff --git a/packages/core/tests/models/attachments.test.ts b/packages/core/tests/models/attachments.test.ts deleted file mode 100644 index 11a9f82..0000000 --- a/packages/core/tests/models/attachments.test.ts +++ /dev/null @@ -1,136 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { - base64ByteLength, - isAcceptedAttachmentMediaType, - isImageMediaType, - isPdfMediaType, - MAX_ATTACHMENTS, - MAX_IMAGE_BYTES, - MAX_PDF_BYTES, - MAX_TOTAL_ATTACHMENT_BYTES, - validateUserContent, -} from "../../src/models/attachments.js"; -import type { UserContentPart } from "../../src/types/index.js"; - -/** A base64 string that decodes to exactly `bytes` bytes (no padding chars). */ -function base64OfBytes(bytes: number): string { - // 4 base64 chars → 3 bytes. Use a multiple of 3 for clean (unpadded) output. - const groups = Math.ceil(bytes / 3); - return "A".repeat(groups * 4); -} - -function imagePart(data: string, mediaType = "image/png"): UserContentPart { - return { type: "attachment", mediaType, data }; -} - -describe("media-type predicates", () => { - it("classifies image types", () => { - expect(isImageMediaType("image/png")).toBe(true); - expect(isImageMediaType("image/jpeg")).toBe(true); - expect(isImageMediaType("image/webp")).toBe(true); - expect(isImageMediaType("image/gif")).toBe(true); - expect(isImageMediaType("application/pdf")).toBe(false); - expect(isImageMediaType("image/svg+xml")).toBe(false); - }); - - it("classifies pdf + accepted types", () => { - expect(isPdfMediaType("application/pdf")).toBe(true); - expect(isPdfMediaType("image/png")).toBe(false); - expect(isAcceptedAttachmentMediaType("image/gif")).toBe(true); - expect(isAcceptedAttachmentMediaType("application/pdf")).toBe(true); - expect(isAcceptedAttachmentMediaType("text/plain")).toBe(false); - }); -}); - -describe("base64ByteLength", () => { - it("computes decoded length without padding", () => { - // "AAAA" → 3 bytes. - expect(base64ByteLength("AAAA")).toBe(3); - }); - - it("accounts for padding", () => { - // "QQ==" → 1 byte ("A"). - expect(base64ByteLength("QQ==")).toBe(1); - // "QUI=" → 2 bytes ("AB"). - expect(base64ByteLength("QUI=")).toBe(2); - }); - - it("tolerates a data: URI prefix and whitespace", () => { - expect(base64ByteLength("data:image/png;base64,AAAA")).toBe(3); - expect(base64ByteLength("AA\nAA")).toBe(3); - }); - - it("returns 0 for empty input", () => { - expect(base64ByteLength("")).toBe(0); - expect(base64ByteLength(" ")).toBe(0); - }); -}); - -describe("validateUserContent", () => { - it("accepts a small image and ignores text parts", () => { - const content: UserContentPart[] = [ - { type: "text", text: "hi" }, - imagePart(base64OfBytes(1024)), - ]; - expect(validateUserContent(content)).toEqual({ ok: true, errors: [] }); - }); - - it("accepts an empty / text-only content list", () => { - expect(validateUserContent([]).ok).toBe(true); - expect(validateUserContent([{ type: "text", text: "no files" }]).ok).toBe(true); - }); - - it("rejects an unsupported media type", () => { - const res = validateUserContent([imagePart(base64OfBytes(10), "image/svg+xml")]); - expect(res.ok).toBe(false); - expect(res.errors[0]).toMatchObject({ code: "unsupported-type", mediaType: "image/svg+xml" }); - }); - - it("rejects an oversized image but allows a PDF of the same size", () => { - const big = base64OfBytes(MAX_IMAGE_BYTES + 3); - const imgRes = validateUserContent([imagePart(big, "image/png")]); - expect(imgRes.ok).toBe(false); - expect(imgRes.errors.some((e) => e.code === "image-too-large")).toBe(true); - - // Same byte size as a PDF is fine (PDF limit is much higher). - const pdfRes = validateUserContent([imagePart(big, "application/pdf")]); - expect(pdfRes.ok).toBe(true); - }); - - it("rejects an oversized PDF", () => { - const res = validateUserContent([ - imagePart(base64OfBytes(MAX_PDF_BYTES + 3), "application/pdf"), - ]); - expect(res.ok).toBe(false); - expect(res.errors.some((e) => e.code === "pdf-too-large")).toBe(true); - }); - - it("rejects an empty attachment payload", () => { - const res = validateUserContent([imagePart("", "image/png")]); - expect(res.ok).toBe(false); - expect(res.errors.some((e) => e.code === "empty")).toBe(true); - }); - - it("rejects too many attachments", () => { - const content: UserContentPart[] = Array.from({ length: MAX_ATTACHMENTS + 1 }, () => - imagePart(base64OfBytes(8)), - ); - const res = validateUserContent(content); - expect(res.ok).toBe(false); - expect(res.errors.some((e) => e.code === "too-many")).toBe(true); - }); - - it("rejects when the total payload exceeds the request ceiling", () => { - // Several individually-legal PDFs that together exceed the total cap. - const each = Math.floor(MAX_TOTAL_ATTACHMENT_BYTES / 3); - const content: UserContentPart[] = [ - imagePart(base64OfBytes(each), "application/pdf"), - imagePart(base64OfBytes(each), "application/pdf"), - imagePart(base64OfBytes(each), "application/pdf"), - imagePart(base64OfBytes(each), "application/pdf"), - ]; - const res = validateUserContent(content); - expect(res.ok).toBe(false); - expect(res.errors.some((e) => e.code === "total-too-large")).toBe(true); - }); -}); diff --git a/packages/core/tests/models/catalog.test.ts b/packages/core/tests/models/catalog.test.ts deleted file mode 100644 index f4bddc2..0000000 --- a/packages/core/tests/models/catalog.test.ts +++ /dev/null @@ -1,227 +0,0 @@ -import { existsSync, rmSync, utimesSync, writeFileSync } from "node:fs"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { - __resetCatalogCacheForTests, - getModelsCatalog, - resolveContextLimit, - resolveModelCapabilities, -} from "../../src/models/catalog.js"; - -const CACHE_PATH = "/tmp/dispatch/models-dev.json"; - -// A trimmed models.dev-shaped catalog covering the providers we support. -const CATALOG = { - anthropic: { - id: "anthropic", - models: { - "claude-sonnet-4-5": { - limit: { context: 200000, output: 64000 }, - modalities: { input: ["text", "image", "pdf"], output: ["text"] }, - }, - "claude-sonnet-4-6": { - limit: { context: 1000000, output: 64000 }, - modalities: { input: ["text", "image", "pdf"], output: ["text"] }, - }, - // A text-only model: definitively no image/pdf input. - "text-only-model": { - limit: { context: 100000, output: 8192 }, - modalities: { input: ["text"], output: ["text"] }, - }, - // An entry predating the modalities field → capability unknown. - "legacy-model": { limit: { context: 100000, output: 8192 } }, - }, - }, - opencode: { - id: "opencode", - models: { - "glm-4-6": { - limit: { context: 131072, output: 8192 }, - modalities: { input: ["text", "image"], output: ["text"] }, - }, - }, - }, -}; - -function mockFetchOnce(catalog: unknown, ok = true, status = 200) { - const fn = vi.fn(() => - Promise.resolve({ - ok, - status, - text: () => Promise.resolve(JSON.stringify(catalog)), - } as Response), - ); - vi.stubGlobal("fetch", fn); - return fn; -} - -beforeEach(() => { - __resetCatalogCacheForTests(); - if (existsSync(CACHE_PATH)) rmSync(CACHE_PATH); - delete process.env.DISPATCH_DISABLE_MODELS_FETCH; -}); - -afterEach(() => { - vi.unstubAllGlobals(); - if (existsSync(CACHE_PATH)) rmSync(CACHE_PATH); -}); - -describe("resolveContextLimit", () => { - it("resolves a known anthropic model to its context window", async () => { - mockFetchOnce(CATALOG); - expect(await resolveContextLimit("anthropic", "claude-sonnet-4-5")).toBe(200000); - expect(await resolveContextLimit("anthropic", "claude-sonnet-4-6")).toBe(1000000); - }); - - it("maps opencode-anthropic to the anthropic catalog, then opencode fallback", async () => { - mockFetchOnce(CATALOG); - // Present in the anthropic catalog. - expect(await resolveContextLimit("opencode-anthropic", "claude-sonnet-4-5")).toBe(200000); - // Absent in anthropic, found in the opencode gateway catalog. - expect(await resolveContextLimit("opencode-anthropic", "glm-4-6")).toBe(131072); - }); - - it("returns null for an unknown model id", async () => { - mockFetchOnce(CATALOG); - expect(await resolveContextLimit("anthropic", "no-such-model")).toBeNull(); - }); - - it("returns null for an unsupported provider (no network needed)", async () => { - const fetchFn = mockFetchOnce(CATALOG); - expect(await resolveContextLimit("google", "gemini-2.5-pro")).toBeNull(); - expect(await resolveContextLimit("anthropic", "")).toBeNull(); - expect(fetchFn).not.toHaveBeenCalled(); - }); - - it("returns null when the model has no positive context limit", async () => { - mockFetchOnce({ - anthropic: { id: "anthropic", models: { broken: { limit: { context: 0 } } } }, - }); - expect(await resolveContextLimit("anthropic", "broken")).toBeNull(); - }); - - it("does not throw on a malformed provider entry missing `models`", async () => { - // A provider object without a `models` map must degrade to null, not crash. - mockFetchOnce({ anthropic: { id: "anthropic" } }); - expect(await resolveContextLimit("anthropic", "claude-sonnet-4-5")).toBeNull(); - }); - - it("does not throw when limit/context fields are absent", async () => { - mockFetchOnce({ anthropic: { id: "anthropic", models: { m: {} } } }); - expect(await resolveContextLimit("anthropic", "m")).toBeNull(); - }); -}); - -describe("getModelsCatalog caching", () => { - it("fetches once and serves the in-process memo on subsequent calls", async () => { - const fetchFn = mockFetchOnce(CATALOG); - await resolveContextLimit("anthropic", "claude-sonnet-4-5"); - await resolveContextLimit("anthropic", "claude-sonnet-4-6"); - await getModelsCatalog(); - expect(fetchFn).toHaveBeenCalledTimes(1); - }); - - it("reuses a fresh disk cache without re-fetching across processes", async () => { - // Simulate another process having written a fresh cache. - writeFileSync(CACHE_PATH, JSON.stringify(CATALOG), "utf-8"); - const fetchFn = vi.fn(() => Promise.reject(new Error("network should not be hit"))); - vi.stubGlobal("fetch", fetchFn); - expect(await resolveContextLimit("anthropic", "claude-sonnet-4-5")).toBe(200000); - expect(fetchFn).not.toHaveBeenCalled(); - }); - - it("falls back to a STALE disk cache when the network fails", async () => { - writeFileSync(CACHE_PATH, JSON.stringify(CATALOG), "utf-8"); - // Age the cache well past the TTL so the fetch path is taken. - const old = Date.now() / 1000 - 3600; - utimesSync(CACHE_PATH, old, old); - const fetchFn = vi.fn(() => Promise.reject(new Error("offline"))); - vi.stubGlobal("fetch", fetchFn); - const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); - - expect(await resolveContextLimit("anthropic", "claude-sonnet-4-5")).toBe(200000); - expect(fetchFn).toHaveBeenCalledTimes(1); - warn.mockRestore(); - }); - - it("returns null when fetch fails and no cache exists", async () => { - const fetchFn = vi.fn(() => Promise.reject(new Error("offline"))); - vi.stubGlobal("fetch", fetchFn); - const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); - expect(await resolveContextLimit("anthropic", "claude-sonnet-4-5")).toBeNull(); - warn.mockRestore(); - }); - - it("does not hit the network when DISPATCH_DISABLE_MODELS_FETCH is set", async () => { - process.env.DISPATCH_DISABLE_MODELS_FETCH = "1"; - const fetchFn = vi.fn(() => Promise.reject(new Error("should not fetch"))); - vi.stubGlobal("fetch", fetchFn); - expect(await resolveContextLimit("anthropic", "claude-sonnet-4-5")).toBeNull(); - expect(fetchFn).not.toHaveBeenCalled(); - }); - - it("memoizes the fallback after a failed fetch so it does not re-hit the network", async () => { - const fetchFn = vi.fn(() => Promise.reject(new Error("offline"))); - vi.stubGlobal("fetch", fetchFn); - const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); - - // First lookup triggers the (failing) fetch. - expect(await resolveContextLimit("anthropic", "claude-sonnet-4-5")).toBeNull(); - // Subsequent lookups within the penalty window must NOT re-fetch. - expect(await resolveContextLimit("anthropic", "claude-sonnet-4-6")).toBeNull(); - await getModelsCatalog(); - expect(fetchFn).toHaveBeenCalledTimes(1); - warn.mockRestore(); - }); -}); - -describe("resolveModelCapabilities", () => { - it("reports image + pdf for a vision model", async () => { - mockFetchOnce(CATALOG); - expect(await resolveModelCapabilities("anthropic", "claude-sonnet-4-5")).toEqual({ - image: true, - pdf: true, - }); - }); - - it("reports image-only for a model whose modalities omit pdf", async () => { - mockFetchOnce(CATALOG); - // glm-4-6 lists image but not pdf (resolved via the opencode fallback). - expect(await resolveModelCapabilities("opencode-anthropic", "glm-4-6")).toEqual({ - image: true, - pdf: false, - }); - }); - - it("reports a definitive no for a text-only model", async () => { - mockFetchOnce(CATALOG); - expect(await resolveModelCapabilities("anthropic", "text-only-model")).toEqual({ - image: false, - pdf: false, - }); - }); - - it("returns null (unknown) for an entry without modalities", async () => { - mockFetchOnce(CATALOG); - expect(await resolveModelCapabilities("anthropic", "legacy-model")).toBeNull(); - }); - - it("returns null (unknown) for an unknown model id", async () => { - mockFetchOnce(CATALOG); - expect(await resolveModelCapabilities("anthropic", "no-such-model")).toBeNull(); - }); - - it("returns null for an unsupported provider without hitting the network", async () => { - const fetchFn = mockFetchOnce(CATALOG); - expect(await resolveModelCapabilities("google", "gemini-2.5-pro")).toBeNull(); - expect(await resolveModelCapabilities("anthropic", "")).toBeNull(); - expect(fetchFn).not.toHaveBeenCalled(); - }); - - it("returns null (unknown) when the catalog is offline with no cache", async () => { - const fetchFn = vi.fn(() => Promise.reject(new Error("offline"))); - vi.stubGlobal("fetch", fetchFn); - const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); - expect(await resolveModelCapabilities("anthropic", "claude-sonnet-4-5")).toBeNull(); - warn.mockRestore(); - }); -}); diff --git a/packages/core/tests/notifications/config.test.ts b/packages/core/tests/notifications/config.test.ts deleted file mode 100644 index 71dc00c..0000000 --- a/packages/core/tests/notifications/config.test.ts +++ /dev/null @@ -1,158 +0,0 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; - -// In-memory fake for the settings table — mounted before the module under -// test is imported (vi.mock is hoisted). -const fakeSettings = new Map<string, string>(); - -vi.mock("../../src/db/settings.js", () => ({ - getSetting: vi.fn((key: string) => fakeSettings.get(key) ?? null), - setSetting: vi.fn((key: string, value: string) => { - fakeSettings.set(key, value); - }), - deleteSetting: vi.fn((key: string) => { - fakeSettings.delete(key); - }), -})); - -const { - clearNtfyConfig, - defaultNtfyConfig, - loadNtfyConfig, - normalizeNtfyConfig, - NTFY_CONFIG_KEY, - redactNtfyConfig, - saveNtfyConfig, -} = await import("../../src/notifications/config.js"); - -describe("defaultNtfyConfig", () => { - it("disables notifications and ships sane per-event defaults", () => { - const cfg = defaultNtfyConfig(); - expect(cfg.enabled).toBe(false); - expect(cfg.topic).toBe(""); - expect(cfg.authToken).toBe(""); - expect(cfg.events["turn-completed"]).toBe(true); - expect(cfg.events["turn-error"]).toBe(true); - expect(cfg.events["permission-required"]).toBe(true); - expect(cfg.events["agent-spawned"]).toBe(false); - expect(cfg.notifySubagents).toBe(false); - }); -}); - -describe("normalizeNtfyConfig", () => { - it("returns defaults for non-object input", () => { - expect(normalizeNtfyConfig(null)).toEqual(defaultNtfyConfig()); - expect(normalizeNtfyConfig(undefined)).toEqual(defaultNtfyConfig()); - expect(normalizeNtfyConfig(42)).toEqual(defaultNtfyConfig()); - }); - - it("fills in missing event toggles with defaults (newly-added types default OFF)", () => { - const normalized = normalizeNtfyConfig({ - enabled: true, - topic: "https://ntfy.sh/x", - events: { "turn-completed": false }, - }); - expect(normalized.events["turn-completed"]).toBe(false); - // Defaults preserved for fields the persisted blob doesn't have. - expect(normalized.events["turn-error"]).toBe(true); - expect(normalized.events["agent-spawned"]).toBe(false); - }); - - it("ignores extraneous fields and wrong-typed values", () => { - const normalized = normalizeNtfyConfig({ - enabled: "yes", // wrong type ⇒ default - topic: 42, // wrong type ⇒ default - authToken: null, // wrong type ⇒ default - events: { "turn-completed": "no", bogus: true }, - extra: "ignored", - }); - expect(normalized.enabled).toBe(false); - expect(normalized.topic).toBe(""); - expect(normalized.authToken).toBe(""); - expect(normalized.events["turn-completed"]).toBe(true); // default kept - expect((normalized.events as Record<string, boolean>).bogus).toBeUndefined(); - }); -}); - -describe("normalizeNtfyConfig — notifySubagents", () => { - it("defaults notifySubagents to false when absent", () => { - const normalized = normalizeNtfyConfig({ - enabled: true, - topic: "https://ntfy.sh/x", - }); - expect(normalized.notifySubagents).toBe(false); - }); - - it("respects an explicit notifySubagents=true", () => { - const normalized = normalizeNtfyConfig({ - enabled: true, - topic: "https://ntfy.sh/x", - notifySubagents: true, - }); - expect(normalized.notifySubagents).toBe(true); - }); - - it("falls back to default when notifySubagents is wrong-typed", () => { - const normalized = normalizeNtfyConfig({ - enabled: true, - topic: "https://ntfy.sh/x", - notifySubagents: "yes" as unknown, - }); - expect(normalized.notifySubagents).toBe(false); - }); -}); - -describe("load/save round-trip", () => { - beforeEach(() => { - fakeSettings.clear(); - }); - - it("returns defaults when nothing is persisted", () => { - expect(loadNtfyConfig()).toEqual(defaultNtfyConfig()); - }); - - it("round-trips a complete config", () => { - const cfg = { - enabled: true, - topic: "https://ntfy.sh/team", - authToken: "tk_abc", - events: { - "turn-completed": false, - "turn-error": true, - "permission-required": true, - "agent-spawned": true, - }, - notifySubagents: true, - } as const; - saveNtfyConfig({ ...cfg }); - const loaded = loadNtfyConfig(); - expect(loaded).toEqual(cfg); - // Persisted as a JSON string under the documented key. - expect(fakeSettings.has(NTFY_CONFIG_KEY)).toBe(true); - }); - - it("returns defaults when stored JSON is corrupt", () => { - fakeSettings.set(NTFY_CONFIG_KEY, "{ not json"); - expect(loadNtfyConfig()).toEqual(defaultNtfyConfig()); - }); - - it("clearNtfyConfig removes the persisted entry", () => { - saveNtfyConfig({ ...defaultNtfyConfig(), enabled: true, topic: "https://ntfy.sh/x" }); - expect(fakeSettings.has(NTFY_CONFIG_KEY)).toBe(true); - clearNtfyConfig(); - expect(fakeSettings.has(NTFY_CONFIG_KEY)).toBe(false); - }); -}); - -describe("redactNtfyConfig", () => { - it("strips authToken and surfaces a hasAuthToken flag", () => { - const cfg = { ...defaultNtfyConfig(), authToken: "tk_secret" }; - const redacted = redactNtfyConfig(cfg); - expect(redacted.authToken).toBe(""); - expect(redacted.hasAuthToken).toBe(true); - }); - - it("hasAuthToken is false for blank tokens", () => { - expect(redactNtfyConfig({ ...defaultNtfyConfig(), authToken: "" }).hasAuthToken).toBe(false); - expect(redactNtfyConfig({ ...defaultNtfyConfig(), authToken: " " }).hasAuthToken).toBe(false); - }); -}); diff --git a/packages/core/tests/notifications/dispatcher.test.ts b/packages/core/tests/notifications/dispatcher.test.ts deleted file mode 100644 index c2faba6..0000000 --- a/packages/core/tests/notifications/dispatcher.test.ts +++ /dev/null @@ -1,461 +0,0 @@ -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import type { NotificationEvent, NtfyConfig } from "../../src/notifications/types.js"; - -// The dispatcher imports `loadNtfyConfig` from config.ts, which transitively -// pulls in `db/index.js` (bun:sqlite). Stub the DB so vitest under Node can -// load this file. All tests inject `loadConfig` explicitly, so the real -// settings table is never read. -vi.mock("../../src/db/index.js", () => ({ - getDatabase: vi.fn(() => ({ - query: () => ({ get: () => null, run: () => {} }), - run: () => {}, - })), -})); - -const { NotificationDispatcher } = await import("../../src/notifications/dispatcher.js"); - -function makeConfig(overrides: Partial<NtfyConfig> = {}): NtfyConfig { - return { - enabled: true, - topic: "test-topic", - authToken: "", - events: { - "turn-completed": true, - "turn-error": true, - "permission-required": true, - "agent-spawned": true, - }, - // Default to true in the test config so existing tests (which never - // configure a getTabParentId lookup) keep firing for tab-1 / tab-2 / etc. - // Tests of the new subagent gating override this explicitly. - notifySubagents: true, - ...overrides, - }; -} - -interface FakeAgentSource { - onEvent( - listener: (event: { type: string; tabId: string; [k: string]: unknown }) => void, - ): () => void; - emit(event: { type: string; tabId: string; [k: string]: unknown }): void; -} - -function makeAgentSource(): FakeAgentSource { - let l: ((event: { type: string; tabId: string; [k: string]: unknown }) => void) | null = null; - return { - onEvent(listener) { - l = listener; - return () => { - l = null; - }; - }, - emit(event) { - l?.(event); - }, - }; -} - -interface FakePermissionSource { - onPromptAdded( - listener: (prompt: { id: string; permission: string; description: string }) => void, - ): () => void; - emit(prompt: { id: string; permission: string; description: string }): void; -} - -function makePermissionSource(): FakePermissionSource { - let l: ((prompt: { id: string; permission: string; description: string }) => void) | null = null; - return { - onPromptAdded(listener) { - l = listener; - return () => { - l = null; - }; - }, - emit(p) { - l?.(p); - }, - }; -} - -// Microtask flush so the dispatcher's `void Promise.resolve(...).catch(...)` -// has a chance to settle before assertions. -async function flush(): Promise<void> { - await Promise.resolve(); - await Promise.resolve(); -} - -describe("NotificationDispatcher.notify", () => { - let warnSpy: ReturnType<typeof vi.spyOn>; - beforeEach(() => { - warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); - }); - afterEach(() => { - warnSpy.mockRestore(); - }); - - it("does not send when master switch is disabled", async () => { - const send = vi.fn(async () => ({ ok: true })); - const d = new NotificationDispatcher({ - loadConfig: () => makeConfig({ enabled: false }), - send, - }); - d.notify({ type: "turn-completed", title: "x", message: "y" }); - await flush(); - expect(send).not.toHaveBeenCalled(); - }); - - it("does not send when per-event-type toggle is off", async () => { - const send = vi.fn(async () => ({ ok: true })); - const d = new NotificationDispatcher({ - loadConfig: () => - makeConfig({ - events: { - "turn-completed": false, - "turn-error": true, - "permission-required": true, - "agent-spawned": false, - }, - }), - send, - }); - d.notify({ type: "turn-completed", title: "x", message: "y" }); - await flush(); - expect(send).not.toHaveBeenCalled(); - }); - - it("sends when enabled and toggle is on", async () => { - const send = vi.fn(async () => ({ ok: true })); - const d = new NotificationDispatcher({ loadConfig: () => makeConfig(), send }); - d.notify({ type: "turn-completed", title: "x", message: "y" }); - await flush(); - expect(send).toHaveBeenCalledTimes(1); - }); - - it("does not throw or block when the transport rejects", async () => { - const send = vi.fn(async () => { - throw new Error("boom"); - }); - const d = new NotificationDispatcher({ loadConfig: () => makeConfig(), send }); - expect(() => d.notify({ type: "turn-completed", title: "x", message: "y" })).not.toThrow(); - await flush(); - expect(send).toHaveBeenCalledTimes(1); - expect(warnSpy).toHaveBeenCalled(); - }); - - it("dedupes events with the same dedupeKey within the window", async () => { - const send = vi.fn(async () => ({ ok: true })); - const d = new NotificationDispatcher({ - loadConfig: () => makeConfig(), - send, - dedupeWindowMs: 1000, - }); - const event: NotificationEvent = { - type: "permission-required", - title: "p", - message: "p", - dedupeKey: "permission:42", - }; - d.notify(event); - d.notify(event); - d.notify(event); - await flush(); - expect(send).toHaveBeenCalledTimes(1); - }); - - it("does not dedupe events without a dedupeKey", async () => { - const send = vi.fn(async () => ({ ok: true })); - const d = new NotificationDispatcher({ loadConfig: () => makeConfig(), send }); - d.notify({ type: "turn-completed", title: "x", message: "y" }); - d.notify({ type: "turn-completed", title: "x", message: "y" }); - await flush(); - expect(send).toHaveBeenCalledTimes(2); - }); -}); - -describe("NotificationDispatcher.attachToAgentManager", () => { - let warnSpy: ReturnType<typeof vi.spyOn>; - beforeEach(() => { - warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); - }); - afterEach(() => { - warnSpy.mockRestore(); - }); - - it("maps `done` → turn-completed (with tab title in the body)", async () => { - const send = vi.fn(async () => ({ ok: true })); - const source = makeAgentSource(); - const d = new NotificationDispatcher({ - loadConfig: () => makeConfig(), - send, - getTabTitle: (id) => (id === "tab-1" ? "My chat" : null), - }); - d.attachToAgentManager(source); - source.emit({ type: "done", tabId: "tab-1", message: { role: "assistant", chunks: [] } }); - await flush(); - expect(send).toHaveBeenCalledTimes(1); - const event = send.mock.calls[0][1] as NotificationEvent; - expect(event.type).toBe("turn-completed"); - expect(event.title).toContain("My chat"); - expect(event.tabId).toBe("tab-1"); - }); - - it("maps `error` → turn-error and includes the error text", async () => { - const send = vi.fn(async () => ({ ok: true })); - const source = makeAgentSource(); - const d = new NotificationDispatcher({ loadConfig: () => makeConfig(), send }); - d.attachToAgentManager(source); - source.emit({ type: "error", tabId: "tab-1", error: "Rate limit", statusCode: 429 }); - await flush(); - expect(send).toHaveBeenCalledTimes(1); - const event = send.mock.calls[0][1] as NotificationEvent; - expect(event.type).toBe("turn-error"); - expect(event.message).toContain("Rate limit"); - expect(event.message).toContain("429"); - }); - - it("ignores `status` events (would spam every transition)", async () => { - const send = vi.fn(async () => ({ ok: true })); - const source = makeAgentSource(); - const d = new NotificationDispatcher({ loadConfig: () => makeConfig(), send }); - d.attachToAgentManager(source); - source.emit({ type: "status", tabId: "tab-1", status: "running" }); - source.emit({ type: "status", tabId: "tab-1", status: "idle" }); - await flush(); - expect(send).not.toHaveBeenCalled(); - }); - - it("maps `tab-created` to agent-spawned only for top-level user agents (parentTabId=null AND agentSlug set)", async () => { - const send = vi.fn(async () => ({ ok: true })); - const source = makeAgentSource(); - const d = new NotificationDispatcher({ loadConfig: () => makeConfig(), send }); - d.attachToAgentManager(source); - - // Manual "new tab" with no agent slug ⇒ no notification. - source.emit({ - type: "tab-created", - tabId: "tab-1", - id: "tab-1", - title: "New Tab", - parentTabId: null, - agentSlug: null, - }); - // Subagent (has a parent) ⇒ no notification. - source.emit({ - type: "tab-created", - tabId: "tab-2", - id: "tab-2", - title: "Subagent", - parentTabId: "tab-1", - agentSlug: "researcher", - }); - // Top-level user agent ⇒ notify. - source.emit({ - type: "tab-created", - tabId: "tab-3", - id: "tab-3", - title: "Refactor auth code", - parentTabId: null, - agentSlug: "engineer", - }); - await flush(); - expect(send).toHaveBeenCalledTimes(1); - const event = send.mock.calls[0][1] as NotificationEvent; - expect(event.type).toBe("agent-spawned"); - expect(event.message).toBe("Refactor auth code"); - expect(event.title).toContain("engineer"); - }); - - it("respects the per-event-type toggle (turn-completed off ⇒ silent)", async () => { - const send = vi.fn(async () => ({ ok: true })); - const source = makeAgentSource(); - const d = new NotificationDispatcher({ - loadConfig: () => - makeConfig({ - events: { - "turn-completed": false, - "turn-error": true, - "permission-required": true, - "agent-spawned": false, - }, - }), - send, - }); - d.attachToAgentManager(source); - source.emit({ type: "done", tabId: "tab-1", message: { role: "assistant", chunks: [] } }); - await flush(); - expect(send).not.toHaveBeenCalled(); - }); -}); - -describe("NotificationDispatcher.attachToPermissionManager", () => { - let warnSpy: ReturnType<typeof vi.spyOn>; - beforeEach(() => { - warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); - }); - afterEach(() => { - warnSpy.mockRestore(); - }); - - it("notifies once per unique prompt id (dedupes re-emits)", async () => { - const send = vi.fn(async () => ({ ok: true })); - const source = makePermissionSource(); - const d = new NotificationDispatcher({ loadConfig: () => makeConfig(), send }); - d.attachToPermissionManager(source); - - source.emit({ id: "1", permission: "bash", description: "Run git status" }); - source.emit({ id: "1", permission: "bash", description: "Run git status" }); - source.emit({ id: "2", permission: "read", description: "Read /etc/hosts" }); - await flush(); - expect(send).toHaveBeenCalledTimes(2); - const events = send.mock.calls.map((c) => c[1] as NotificationEvent); - expect(events.map((e) => e.type)).toEqual(["permission-required", "permission-required"]); - expect(events.every((e) => e.dedupeKey?.startsWith("permission:"))).toBe(true); - }); -}); - -describe("NotificationDispatcher.dispose", () => { - it("releases attached subscriptions", async () => { - const send = vi.fn(async () => ({ ok: true })); - const source = makeAgentSource(); - const d = new NotificationDispatcher({ loadConfig: () => makeConfig(), send }); - d.attachToAgentManager(source); - d.dispose(); - source.emit({ type: "done", tabId: "tab-1", message: { role: "assistant", chunks: [] } }); - await flush(); - expect(send).not.toHaveBeenCalled(); - }); -}); - -describe("NotificationDispatcher subagent suppression (notifySubagents flag)", () => { - let warnSpy: ReturnType<typeof vi.spyOn>; - beforeEach(() => { - warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); - }); - afterEach(() => { - warnSpy.mockRestore(); - }); - - const parents = new Map<string, string | null>([ - ["top-level", null], - ["subagent", "top-level"], - ]); - const getTabParentId = (id: string): string | null | undefined => parents.get(id); - - it("suppresses turn-completed from subagent tabs when notifySubagents=false (default)", async () => { - const send = vi.fn(async () => ({ ok: true })); - const source = makeAgentSource(); - const d = new NotificationDispatcher({ - loadConfig: () => makeConfig({ notifySubagents: false }), - send, - getTabParentId, - }); - d.attachToAgentManager(source); - - source.emit({ type: "done", tabId: "subagent", message: { role: "assistant", chunks: [] } }); - source.emit({ type: "done", tabId: "top-level", message: { role: "assistant", chunks: [] } }); - await flush(); - - expect(send).toHaveBeenCalledTimes(1); - expect((send.mock.calls[0][1] as NotificationEvent).tabId).toBe("top-level"); - }); - - it("suppresses turn-error from subagent tabs when notifySubagents=false", async () => { - const send = vi.fn(async () => ({ ok: true })); - const source = makeAgentSource(); - const d = new NotificationDispatcher({ - loadConfig: () => makeConfig({ notifySubagents: false }), - send, - getTabParentId, - }); - d.attachToAgentManager(source); - - source.emit({ type: "error", tabId: "subagent", error: "boom" }); - source.emit({ type: "error", tabId: "top-level", error: "boom" }); - await flush(); - - expect(send).toHaveBeenCalledTimes(1); - expect((send.mock.calls[0][1] as NotificationEvent).tabId).toBe("top-level"); - }); - - it("still notifies subagents when notifySubagents=true", async () => { - const send = vi.fn(async () => ({ ok: true })); - const source = makeAgentSource(); - const d = new NotificationDispatcher({ - loadConfig: () => makeConfig({ notifySubagents: true }), - send, - getTabParentId, - }); - d.attachToAgentManager(source); - - source.emit({ type: "done", tabId: "subagent", message: { role: "assistant", chunks: [] } }); - source.emit({ type: "done", tabId: "top-level", message: { role: "assistant", chunks: [] } }); - await flush(); - - expect(send).toHaveBeenCalledTimes(2); - }); - - it("does NOT gate permission-required (subagents must still get human input)", async () => { - const send = vi.fn(async () => ({ ok: true })); - const psource = makePermissionSource(); - const d = new NotificationDispatcher({ - loadConfig: () => makeConfig({ notifySubagents: false }), - send, - getTabParentId, - }); - d.attachToPermissionManager(psource); - - psource.emit({ id: "p1", permission: "bash", description: "git status" }); - await flush(); - - expect(send).toHaveBeenCalledTimes(1); - expect((send.mock.calls[0][1] as NotificationEvent).type).toBe("permission-required"); - }); - - it("falls back to notifying when getTabParentId is not provided (treat as top-level)", async () => { - const send = vi.fn(async () => ({ ok: true })); - const source = makeAgentSource(); - const d = new NotificationDispatcher({ - loadConfig: () => makeConfig({ notifySubagents: false }), - send, - // intentionally NO getTabParentId - }); - d.attachToAgentManager(source); - - source.emit({ type: "done", tabId: "anything", message: { role: "assistant", chunks: [] } }); - await flush(); - - // Without a lookup, the dispatcher can't prove this is a subagent; it - // must err on the side of notifying so legitimate top-level events - // aren't silently dropped. - expect(send).toHaveBeenCalledTimes(1); - }); - - it("falls back to notifying when getTabParentId throws or returns undefined", async () => { - const send = vi.fn(async () => ({ ok: true })); - const source = makeAgentSource(); - const d = new NotificationDispatcher({ - loadConfig: () => makeConfig({ notifySubagents: false }), - send, - getTabParentId: () => { - throw new Error("db unavailable"); - }, - }); - d.attachToAgentManager(source); - - source.emit({ type: "done", tabId: "x", message: { role: "assistant", chunks: [] } }); - await flush(); - expect(send).toHaveBeenCalledTimes(1); - - const send2 = vi.fn(async () => ({ ok: true })); - const source2 = makeAgentSource(); - const d2 = new NotificationDispatcher({ - loadConfig: () => makeConfig({ notifySubagents: false }), - send: send2, - getTabParentId: () => undefined, - }); - d2.attachToAgentManager(source2); - source2.emit({ type: "done", tabId: "x", message: { role: "assistant", chunks: [] } }); - await flush(); - expect(send2).toHaveBeenCalledTimes(1); - }); -}); diff --git a/packages/core/tests/notifications/ntfy.test.ts b/packages/core/tests/notifications/ntfy.test.ts deleted file mode 100644 index 5f14a60..0000000 --- a/packages/core/tests/notifications/ntfy.test.ts +++ /dev/null @@ -1,204 +0,0 @@ -import { describe, expect, it, vi } from "vitest"; -import { buildNtfyUrl, NTFY_BASE_URL, sendNtfy } from "../../src/notifications/ntfy.js"; -import type { NotificationEvent, NtfyConfig } from "../../src/notifications/types.js"; - -function makeConfig(overrides: Partial<NtfyConfig> = {}): NtfyConfig { - return { - enabled: true, - topic: "my-topic", - authToken: "", - events: { - "turn-completed": true, - "turn-error": true, - "permission-required": true, - "agent-spawned": true, - }, - notifySubagents: false, - ...overrides, - }; -} - -function makeEvent(overrides: Partial<NotificationEvent> = {}): NotificationEvent { - return { - type: "turn-completed", - title: "Done", - message: "all good", - ...overrides, - }; -} - -function makeFetch( - response: Partial<{ ok: boolean; status: number; statusText: string; body: string }> = {}, -) { - const fetchImpl = vi.fn(async () => ({ - ok: response.ok ?? true, - status: response.status ?? 200, - statusText: response.statusText ?? "OK", - text: async () => response.body ?? "", - })); - return fetchImpl; -} - -describe("buildNtfyUrl", () => { - it("prefixes the public ntfy.sh host", () => { - expect(buildNtfyUrl("my-topic")).toBe(`${NTFY_BASE_URL}/my-topic`); - }); - - it("trims surrounding whitespace", () => { - expect(buildNtfyUrl(" hello ")).toBe(`${NTFY_BASE_URL}/hello`); - }); - - it("URL-encodes the topic so any string yields a valid URL", () => { - // Spaces, slashes, unicode — all preserved as encoded bytes; the ntfy - // server is the final authority on what it accepts. - expect(buildNtfyUrl("has space")).toBe(`${NTFY_BASE_URL}/has%20space`); - expect(buildNtfyUrl("a/b")).toBe(`${NTFY_BASE_URL}/a%2Fb`); - expect(buildNtfyUrl("日本語")).toBe(`${NTFY_BASE_URL}/${encodeURIComponent("日本語")}`); - }); -}); - -describe("sendNtfy", () => { - it("POSTs to https://ntfy.sh/<topic> with Title/Priority/Tags/Content-Type headers and body", async () => { - const fetchImpl = makeFetch(); - const result = await sendNtfy( - makeConfig(), - makeEvent({ title: "Hello", message: "World", tags: ["bell"], priority: 4 }), - fetchImpl, - ); - expect(result.ok).toBe(true); - expect(fetchImpl).toHaveBeenCalledTimes(1); - const [url, init] = fetchImpl.mock.calls[0]; - expect(url).toBe(`${NTFY_BASE_URL}/my-topic`); - expect(init.method).toBe("POST"); - expect(init.headers.Title).toBe("Hello"); - expect(init.headers.Priority).toBe("4"); - expect(init.headers.Tags).toBe("bell"); - expect(init.headers["Content-Type"]).toMatch(/text\/plain/); - expect(init.body).toBe("World"); - }); - - it("accepts arbitrary topic strings without a client-side pattern check", async () => { - const fetchImpl = makeFetch(); - // Things the old validator would have rejected — dots, spaces, unicode, - // a single-word "any topic". All should POST and let ntfy decide. - await sendNtfy(makeConfig({ topic: "release.notes" }), makeEvent(), fetchImpl); - await sendNtfy(makeConfig({ topic: "with space" }), makeEvent(), fetchImpl); - await sendNtfy(makeConfig({ topic: "Any Topic Whatsoever" }), makeEvent(), fetchImpl); - await sendNtfy(makeConfig({ topic: "日本語" }), makeEvent(), fetchImpl); - expect(fetchImpl).toHaveBeenCalledTimes(4); - expect(fetchImpl.mock.calls[0][0]).toBe(`${NTFY_BASE_URL}/release.notes`); - expect(fetchImpl.mock.calls[1][0]).toBe(`${NTFY_BASE_URL}/with%20space`); - expect(fetchImpl.mock.calls[2][0]).toBe(`${NTFY_BASE_URL}/Any%20Topic%20Whatsoever`); - expect(fetchImpl.mock.calls[3][0]).toBe(`${NTFY_BASE_URL}/${encodeURIComponent("日本語")}`); - }); - - it("uses per-event-type defaults for priority and tags", async () => { - const fetchImpl = makeFetch(); - await sendNtfy(makeConfig(), makeEvent({ type: "turn-error" }), fetchImpl); - const init = fetchImpl.mock.calls[0][1]; - expect(init.headers.Priority).toBe("4"); // NTFY_DEFAULT_PRIORITIES["turn-error"] - expect(init.headers.Tags).toBe("rotating_light"); - }); - - it("attaches Authorization header with Bearer prefix when authToken is a bare token", async () => { - const fetchImpl = makeFetch(); - await sendNtfy(makeConfig({ authToken: "tk_secret " }), makeEvent(), fetchImpl); - const init = fetchImpl.mock.calls[0][1]; - expect(init.headers.Authorization).toBe("Bearer tk_secret"); - }); - - it("passes a pre-prefixed Authorization value (Basic, custom schemes) through verbatim", async () => { - const fetchImpl = makeFetch(); - await sendNtfy(makeConfig({ authToken: "Basic dXNlcjpwYXNz" }), makeEvent(), fetchImpl); - expect(fetchImpl.mock.calls[0][1].headers.Authorization).toBe("Basic dXNlcjpwYXNz"); - - const fetchImpl2 = makeFetch(); - await sendNtfy(makeConfig({ authToken: "Bearer already_prefixed" }), makeEvent(), fetchImpl2); - expect(fetchImpl2.mock.calls[0][1].headers.Authorization).toBe("Bearer already_prefixed"); - }); - - it("omits Authorization when authToken is blank", async () => { - const fetchImpl = makeFetch(); - await sendNtfy(makeConfig({ authToken: " " }), makeEvent(), fetchImpl); - const init = fetchImpl.mock.calls[0][1]; - expect(init.headers.Authorization).toBeUndefined(); - }); - - it("attaches Click header when clickUrl is set", async () => { - const fetchImpl = makeFetch(); - await sendNtfy(makeConfig(), makeEvent({ clickUrl: "https://example.com/tab/abc" }), fetchImpl); - const init = fetchImpl.mock.calls[0][1]; - expect(init.headers.Click).toBe("https://example.com/tab/abc"); - }); - - it("sanitizes Click header (CR/LF injection guard)", async () => { - const fetchImpl = makeFetch(); - await sendNtfy( - makeConfig(), - makeEvent({ clickUrl: "https://example.com/\r\nInjected: yes" }), - fetchImpl, - ); - const v = fetchImpl.mock.calls[0][1].headers.Click; - expect(v).not.toContain("\n"); - expect(v).not.toContain("\r"); - }); - - it("appends short tab tag when tabId is set", async () => { - const fetchImpl = makeFetch(); - await sendNtfy( - makeConfig(), - makeEvent({ tabId: "abcdef0123456789", tags: ["bell"] }), - fetchImpl, - ); - const init = fetchImpl.mock.calls[0][1]; - expect(init.headers.Tags).toBe("bell,tab-abcdef01"); - }); - - it("strips CR/LF/control chars from header values (injection guard)", async () => { - const fetchImpl = makeFetch(); - await sendNtfy(makeConfig(), makeEvent({ title: "line1\r\nInjected: yes" }), fetchImpl); - const init = fetchImpl.mock.calls[0][1]; - expect(init.headers.Title).not.toContain("\n"); - expect(init.headers.Title).not.toContain("\r"); - expect(init.headers.Title).toBe("line1 Injected: yes"); - }); - - it("returns ok:false when notifications are disabled", async () => { - const fetchImpl = makeFetch(); - const result = await sendNtfy(makeConfig({ enabled: false }), makeEvent(), fetchImpl); - expect(result.ok).toBe(false); - expect(result.error).toMatch(/disabled/); - expect(fetchImpl).not.toHaveBeenCalled(); - }); - - it("returns ok:false when topic is empty / whitespace, without calling fetch", async () => { - const fetchImpl = makeFetch(); - const empty = await sendNtfy(makeConfig({ topic: "" }), makeEvent(), fetchImpl); - expect(empty.ok).toBe(false); - expect(empty.error).toMatch(/required/i); - - const ws = await sendNtfy(makeConfig({ topic: " " }), makeEvent(), fetchImpl); - expect(ws.ok).toBe(false); - expect(ws.error).toMatch(/required/i); - - expect(fetchImpl).not.toHaveBeenCalled(); - }); - - it("returns ok:false with status on non-2xx response", async () => { - const fetchImpl = makeFetch({ ok: false, status: 403, statusText: "Forbidden", body: "nope" }); - const result = await sendNtfy(makeConfig(), makeEvent(), fetchImpl); - expect(result.ok).toBe(false); - expect(result.status).toBe(403); - expect(result.error).toMatch(/403/); - expect(result.error).toMatch(/nope/); - }); - - it("returns ok:false with error message on fetch throwing", async () => { - const fetchImpl = vi.fn(async () => { - throw new Error("ECONNREFUSED"); - }); - const result = await sendNtfy(makeConfig(), makeEvent(), fetchImpl); - expect(result.ok).toBe(false); - expect(result.error).toMatch(/ECONNREFUSED/); - }); -}); diff --git a/packages/core/tests/permission/evaluate.test.ts b/packages/core/tests/permission/evaluate.test.ts deleted file mode 100644 index c8f4541..0000000 --- a/packages/core/tests/permission/evaluate.test.ts +++ /dev/null @@ -1,68 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { evaluate } from "../../src/permission/evaluate.js"; -import type { Ruleset } from "../../src/permission/index.js"; - -describe("evaluate", () => { - it("returns default ask when no rules match", () => { - const result = evaluate("bash", "ls -la"); - expect(result.action).toBe("ask"); - expect(result.permission).toBe("bash"); - expect(result.pattern).toBe("ls -la"); - }); - - it("returns allow when matching rule is allow", () => { - const rules: Ruleset = [{ permission: "bash", pattern: "ls *", action: "allow" }]; - const result = evaluate("bash", "ls -la", rules); - expect(result.action).toBe("allow"); - }); - - it("returns deny when matching rule is deny", () => { - const rules: Ruleset = [{ permission: "bash", pattern: "rm *", action: "deny" }]; - const result = evaluate("bash", "rm -rf /", rules); - expect(result.action).toBe("deny"); - }); - - it("last-match-wins: later deny overrides earlier allow", () => { - const rules: Ruleset = [ - { permission: "bash", pattern: "*", action: "allow" }, - { permission: "bash", pattern: "rm *", action: "deny" }, - ]; - const result = evaluate("bash", "rm -rf /", rules); - expect(result.action).toBe("deny"); - }); - - it("last-match-wins: later allow overrides earlier deny", () => { - const rules: Ruleset = [ - { permission: "bash", pattern: "rm *", action: "deny" }, - { permission: "bash", pattern: "*", action: "allow" }, - ]; - const result = evaluate("bash", "rm -rf /", rules); - expect(result.action).toBe("allow"); - }); - - it("matches permission wildcard", () => { - const rules: Ruleset = [{ permission: "*", pattern: "*", action: "allow" }]; - const result = evaluate("read", "anything", rules); - expect(result.action).toBe("allow"); - }); - - it("multiple rulesets are concatenated, last match wins across rulesets", () => { - const baseRules: Ruleset = [{ permission: "bash", pattern: "*", action: "ask" }]; - const overrideRules: Ruleset = [{ permission: "bash", pattern: "git *", action: "allow" }]; - const result = evaluate("bash", "git status", baseRules, overrideRules); - expect(result.action).toBe("allow"); - }); - - it("second ruleset can deny what first ruleset allows", () => { - const baseRules: Ruleset = [{ permission: "bash", pattern: "*", action: "allow" }]; - const overrideRules: Ruleset = [{ permission: "bash", pattern: "rm *", action: "deny" }]; - const result = evaluate("bash", "rm -rf /", baseRules, overrideRules); - expect(result.action).toBe("deny"); - }); - - it("non-matching permission returns default ask", () => { - const rules: Ruleset = [{ permission: "bash", pattern: "*", action: "allow" }]; - const result = evaluate("read", "/some/path", rules); - expect(result.action).toBe("ask"); - }); -}); diff --git a/packages/core/tests/permission/service.test.ts b/packages/core/tests/permission/service.test.ts deleted file mode 100644 index d1b39d9..0000000 --- a/packages/core/tests/permission/service.test.ts +++ /dev/null @@ -1,95 +0,0 @@ -import { describe, expect, it } from "vitest"; -import type { PermissionRequest, Ruleset } from "../../src/permission/index.js"; -import { PermissionService } from "../../src/permission/service.js"; - -function makeRequest(overrides: Partial<PermissionRequest> = {}): PermissionRequest { - return { - permission: "bash", - patterns: ["git *"], - always: ["git status"], - description: "Run git status", - metadata: {}, - ...overrides, - }; -} - -describe("PermissionService", () => { - it("resolves immediately with 'once' when rule is allow", async () => { - const svc = new PermissionService(); - const rules: Ruleset = [{ permission: "bash", pattern: "*", action: "allow" }]; - const reply = await svc.ask(makeRequest(), [rules]); - expect(reply).toBe("once"); - }); - - it("rejects immediately when rule is deny", async () => { - const svc = new PermissionService(); - const rules: Ruleset = [{ permission: "bash", pattern: "*", action: "deny" }]; - await expect(svc.ask(makeRequest(), [rules])).rejects.toThrow("Permission denied"); - }); - - it("creates pending request when rule is ask", () => { - const svc = new PermissionService(); - svc.ask(makeRequest(), []); - expect(svc.getPending()).toHaveLength(1); - }); - - it("reply 'once' resolves the specific pending request", async () => { - const svc = new PermissionService(); - const promise = svc.ask(makeRequest(), []); - const pending = svc.getPending(); - expect(pending).toHaveLength(1); - svc.reply(pending[0].id, "once"); - const result = await promise; - expect(result).toBe("once"); - expect(svc.getPending()).toHaveLength(0); - }); - - it("reply 'always' adds approved rules and resolves", async () => { - const svc = new PermissionService(); - const promise = svc.ask(makeRequest({ patterns: ["git *"] }), []); - const pending = svc.getPending(); - svc.reply(pending[0].id, "always"); - const result = await promise; - expect(result).toBe("always"); - - // Now the same permission should be immediately allowed - const reply2 = await svc.ask(makeRequest({ always: ["git commit"] }), []); - expect(reply2).toBe("once"); - }); - - it("reply 'reject' rejects all pending requests (cascade)", async () => { - const svc = new PermissionService(); - const p1 = svc.ask(makeRequest(), []); - const p2 = svc.ask(makeRequest({ permission: "read" }), []); - - const pending = svc.getPending(); - expect(pending).toHaveLength(2); - - // Reject using the first id — should cascade to all - svc.reply(pending[0].id, "reject"); - - await expect(p1).rejects.toThrow("Permission rejected"); - await expect(p2).rejects.toThrow("Permission rejected"); - expect(svc.getPending()).toHaveLength(0); - }); - - it("approved rules override config rulesets", async () => { - const svc = new PermissionService(); - svc.approve([{ permission: "bash", pattern: "git *", action: "allow" }]); - - // Config says deny, but approved says allow — approved wins (last) - const configRules: Ruleset = [{ permission: "bash", pattern: "*", action: "deny" }]; - const reply = await svc.ask(makeRequest({ always: ["git status"] }), [configRules]); - expect(reply).toBe("once"); - }); - - it("getPending returns all pending requests with id and request", () => { - const svc = new PermissionService(); - const req = makeRequest(); - svc.ask(req, []); - const pending = svc.getPending(); - expect(pending).toHaveLength(1); - expect(pending[0].id).toBeDefined(); - expect(pending[0].request.permission).toBe("bash"); - }); -}); diff --git a/packages/core/tests/permission/wildcard.test.ts b/packages/core/tests/permission/wildcard.test.ts deleted file mode 100644 index 8fa30a7..0000000 --- a/packages/core/tests/permission/wildcard.test.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { Wildcard } from "../../src/permission/wildcard.js"; - -describe("Wildcard.match", () => { - it("matches exact string", () => { - expect(Wildcard.match("bash", "bash")).toBe(true); - expect(Wildcard.match("bash", "read")).toBe(false); - }); - - it("matches * wildcard (any characters)", () => { - expect(Wildcard.match("*", "bash")).toBe(true); - expect(Wildcard.match("*", "anything")).toBe(true); - expect(Wildcard.match("ba*", "bash")).toBe(true); - expect(Wildcard.match("ba*", "ba")).toBe(true); - expect(Wildcard.match("ba*", "read")).toBe(false); - }); - - it("matches ? wildcard (single character)", () => { - expect(Wildcard.match("ba?h", "bash")).toBe(true); - expect(Wildcard.match("ba?h", "bath")).toBe(true); - expect(Wildcard.match("ba?h", "baXXh")).toBe(false); - expect(Wildcard.match("?", "a")).toBe(true); - expect(Wildcard.match("?", "ab")).toBe(false); - }); - - it("matches nested * patterns with path-like strings", () => { - expect(Wildcard.match("/home/*", "/home/user")).toBe(true); - expect(Wildcard.match("/home/*/file.txt", "/home/user/file.txt")).toBe(true); - expect(Wildcard.match("/home/*/file.txt", "/home/user/subdir/file.txt")).toBe(true); - expect(Wildcard.match("/home/*/file.txt", "/tmp/user/file.txt")).toBe(false); - }); - - it("escapes regex special characters in pattern", () => { - expect(Wildcard.match("git add .", "git add .")).toBe(true); - expect(Wildcard.match("git add .", "git add X")).toBe(false); - expect(Wildcard.match("foo(bar)", "foo(bar)")).toBe(true); - expect(Wildcard.match("foo(bar)", "fooXbar")).toBe(false); - }); - - it("is case-sensitive", () => { - expect(Wildcard.match("Bash", "bash")).toBe(false); - expect(Wildcard.match("BASH", "BASH")).toBe(true); - }); - - it("handles empty pattern and value", () => { - expect(Wildcard.match("", "")).toBe(true); - expect(Wildcard.match("", "x")).toBe(false); - expect(Wildcard.match("*", "")).toBe(true); - }); -}); diff --git a/packages/core/tests/tools/bash-arity.test.ts b/packages/core/tests/tools/bash-arity.test.ts deleted file mode 100644 index a01a6a5..0000000 --- a/packages/core/tests/tools/bash-arity.test.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { prefix } from "../../src/tools/bash-arity.js"; - -describe("BashArity.prefix", () => { - it("returns arity-2 prefix for known command 'git'", () => { - expect(prefix(["git", "checkout", "main"])).toEqual(["git", "checkout"]); - }); - - it("returns arity-3 prefix for npm", () => { - expect(prefix(["npm", "run", "dev"])).toEqual(["npm", "run", "dev"]); - }); - - it("returns arity-2 prefix for bun", () => { - expect(prefix(["bun", "install", "--frozen-lockfile"])).toEqual(["bun", "install"]); - }); - - it("returns just the command for unknown command", () => { - expect(prefix(["unknowncmd", "arg1", "arg2"])).toEqual(["unknowncmd"]); - }); - - it("returns empty array for empty tokens", () => { - expect(prefix([])).toEqual([]); - }); - - it("handles single token for unknown command", () => { - expect(prefix(["ls"])).toEqual(["ls"]); - }); - - it("handles git with fewer tokens than arity", () => { - expect(prefix(["git"])).toEqual(["git"]); - }); - - it("handles case-insensitive matching", () => { - expect(prefix(["GIT", "checkout", "main"])).toEqual(["GIT", "checkout"]); - }); -}); diff --git a/packages/core/tests/tools/key-usage.test.ts b/packages/core/tests/tools/key-usage.test.ts deleted file mode 100644 index 643e30e..0000000 --- a/packages/core/tests/tools/key-usage.test.ts +++ /dev/null @@ -1,317 +0,0 @@ -import { describe, expect, it, vi } from "vitest"; - -// The tool imports `getAccountUsageWithSource` from `claude.ts`, which -// transitively imports `db/index.js` (top-level `import { Database } from -// "bun:sqlite"`) — unresolvable under vitest's Node runtime. These tests inject -// stub fetchers and never hit the real fetchers/DB, so stubbing the db module -// is enough to let the import chain resolve. -vi.mock("../../src/db/index.js", () => ({ - getDatabase: vi.fn(() => { - throw new Error("db not available in this test"); - }), -})); - -import type { ClaudeAccount, ClaudeUsageResult } from "../../src/credentials/claude.js"; -import type { OpencodeUsageReport } from "../../src/credentials/opencode.js"; -import { - createKeyUsageTool, - formatKeyUsage, - type KeyUsageCallbacks, -} from "../../src/tools/key-usage.js"; -import type { KeyDefinition, KeyState } from "../../src/types/index.js"; - -// ─── Builders ───────────────────────────────────────────────── - -function keyState( - def: Partial<KeyDefinition> & { id: string; provider: string }, - overrides: Partial<Omit<KeyState, "definition">> = {}, -): KeyState { - return { - definition: { base_url: "https://example.test", ...def }, - status: "active", - ...overrides, - }; -} - -function account(id: string, source = `/creds/${id}.json`): ClaudeAccount { - return { - id, - label: id, - source, - credentials: { accessToken: "tok", refreshToken: "ref", expiresAt: Date.now() + 3_600_000 }, - }; -} - -/** Build the tool with explicit stub fetchers — no network, no DB. */ -function buildTool(opts: { - keys: KeyState[]; - accounts?: ClaudeAccount[]; - anthropic?: (a: ClaudeAccount) => Promise<ClaudeUsageResult | null>; - opencode?: (keyId: string) => Promise<OpencodeUsageReport | null>; -}) { - const callbacks: KeyUsageCallbacks = { - listKeys: () => opts.keys, - listClaudeAccounts: () => opts.accounts ?? [], - fetchAnthropicUsage: opts.anthropic ?? (async () => null), - fetchOpencodeUsage: opts.opencode ?? (async () => null), - }; - return createKeyUsageTool(callbacks); -} - -const HOUR = 3_600_000; - -describe("key_usage tool", () => { - it("reports all keys when no key_id is given", async () => { - const reset5h = Date.now() + 2 * HOUR; - const tool = buildTool({ - keys: [ - keyState({ id: "claude-max", provider: "anthropic", credentials_file: "/creds/max.json" }), - keyState({ id: "opencode-1", provider: "opencode-go" }), - ], - accounts: [account("claude-max", "/creds/max.json")], - anthropic: async () => ({ - source: "live", - report: { - fiveHour: { utilization: 0.25, resetsAt: reset5h }, - sevenDay: { utilization: 0.6 }, - }, - }), - opencode: async () => ({ - fiveHour: { utilization: 0.1 }, - weekly: { utilization: 0.4 }, - monthly: { utilization: 0.7 }, - }), - }); - - const out = await tool.execute({}); - - // Both keys present with providers. - expect(out).toContain("[claude-max] provider: anthropic"); - expect(out).toContain("[opencode-1] provider: opencode-go"); - // Remaining = (1 - utilization) * 100. - expect(out).toContain("5-hour: 75% remaining"); - expect(out).toContain("week: 40% remaining"); - expect(out).toContain("5-hour: 90% remaining"); - expect(out).toContain("week: 60% remaining"); - expect(out).toContain("month: 30% remaining"); - expect(out).toContain("data: live (fetched just now)"); - }); - - it("filters to a single key when key_id is given and does not fetch others", async () => { - const opencodeFetch = vi.fn(async () => ({ fiveHour: { utilization: 0.5 } })); - const tool = buildTool({ - keys: [ - keyState({ id: "claude-max", provider: "anthropic" }), - keyState({ id: "opencode-1", provider: "opencode-go" }), - ], - accounts: [account("claude-max")], - anthropic: async () => ({ - source: "live", - report: { fiveHour: { utilization: 0.2 } }, - }), - opencode: opencodeFetch, - }); - - const out = await tool.execute({ key_id: "claude-max" }); - - expect(out).toContain("[claude-max] provider: anthropic"); - expect(out).not.toContain("opencode-1"); - expect(opencodeFetch).not.toHaveBeenCalled(); - }); - - it("returns a helpful error for an unknown key_id", async () => { - const tool = buildTool({ - keys: [ - keyState({ id: "claude-max", provider: "anthropic" }), - keyState({ id: "opencode-1", provider: "opencode-go" }), - ], - }); - - const out = await tool.execute({ key_id: "nope" }); - - expect(out).toContain('no key found with id "nope"'); - expect(out).toContain("claude-max"); - expect(out).toContain("opencode-1"); - }); - - it("reports cached data with the source's last-fetched timestamp", async () => { - const cachedAt = Date.UTC(2025, 0, 2, 3, 4, 5); - const tool = buildTool({ - keys: [keyState({ id: "claude-max", provider: "anthropic" })], - accounts: [account("claude-max")], - anthropic: async () => ({ - source: "cache", - cachedAt, - report: { fiveHour: { utilization: 0.5 } }, - }), - }); - - const out = await tool.execute({}); - - expect(out).toContain("data: cached — last fetched from source 2025-01-02T03:04:05.000Z"); - expect(out).toContain("5-hour: 50% remaining"); - }); - - it("omits the month window for anthropic (no monthly bucket)", async () => { - const tool = buildTool({ - keys: [keyState({ id: "claude-max", provider: "anthropic" })], - accounts: [account("claude-max")], - anthropic: async () => ({ - source: "live", - report: { fiveHour: { utilization: 0.1 }, sevenDay: { utilization: 0.2 } }, - }), - }); - - const out = await tool.execute({}); - - expect(out).toContain("5-hour:"); - expect(out).toContain("week:"); - expect(out).not.toContain("month:"); - }); - - it("includes the month window for opencode-go", async () => { - const tool = buildTool({ - keys: [keyState({ id: "opencode-1", provider: "opencode-go" })], - opencode: async () => ({ - fiveHour: { utilization: 0.1 }, - weekly: { utilization: 0.2 }, - monthly: { utilization: 0.3 }, - }), - }); - - const out = await tool.execute({}); - - expect(out).toContain("month: 70% remaining"); - }); - - it("surfaces exhausted status with the last error", async () => { - const exhaustedAt = Date.now() - HOUR; - const tool = buildTool({ - keys: [ - keyState( - { id: "opencode-1", provider: "opencode-go" }, - { status: "exhausted", lastError: "429 rate limit exceeded", exhaustedAt }, - ), - ], - opencode: async () => null, - }); - - const out = await tool.execute({}); - - expect(out).toContain("status: EXHAUSTED"); - expect(out).toContain("last error: 429 rate limit exceeded"); - }); - - it("flags providers without usage support", async () => { - const tool = buildTool({ - keys: [keyState({ id: "gem", provider: "google" })], - }); - - const out = await tool.execute({}); - - expect(out).toContain("[gem] provider: google"); - expect(out).toContain("not supported"); - }); - - it("reports unavailable when a supported provider returns no usage", async () => { - const tool = buildTool({ - keys: [keyState({ id: "claude-max", provider: "anthropic" })], - accounts: [account("claude-max")], - anthropic: async () => null, - }); - - const out = await tool.execute({}); - - expect(out).toContain("usage: unavailable"); - expect(out).toContain("no cached usage"); - }); - - it("reports unavailable for anthropic keys with no account credentials", async () => { - const tool = buildTool({ - keys: [keyState({ id: "claude-max", provider: "anthropic" })], - accounts: [], - }); - - const out = await tool.execute({}); - - expect(out).toContain("no Claude account credentials available"); - }); - - it("treats a fetcher that throws as unavailable (does not crash)", async () => { - const tool = buildTool({ - keys: [keyState({ id: "opencode-1", provider: "opencode-go" })], - opencode: async () => { - throw new Error("network down"); - }, - }); - - const out = await tool.execute({}); - - expect(out).toContain("usage: unavailable"); - }); - - it("reports when no keys are configured at all", async () => { - const tool = buildTool({ keys: [] }); - const out = await tool.execute({}); - expect(out).toBe("No API keys are configured."); - }); - - it("clamps out-of-range utilization to 0–100%", async () => { - const tool = buildTool({ - keys: [keyState({ id: "opencode-1", provider: "opencode-go" })], - opencode: async () => ({ - fiveHour: { utilization: 1.2 }, // over 100% used → 0% remaining - weekly: { utilization: -0.5 }, // negative → 100% remaining - }), - }); - - const out = await tool.execute({}); - - expect(out).toContain("5-hour: 0% remaining"); - expect(out).toContain("week: 100% remaining"); - }); -}); - -describe("formatKeyUsage (pure)", () => { - const now = Date.UTC(2025, 5, 1, 12, 0, 0); - - it("formats reset timestamps with ISO + relative time", () => { - const out = formatKeyUsage( - [ - { - keyId: "claude-max", - provider: "anthropic", - status: "active", - dataSource: "live", - windows: [{ label: "5-hour", remainingPercent: 80, resetsAt: now + 90 * 60_000 }], - }, - ], - now, - ); - - expect(out).toContain("5-hour: 80% remaining, resets 2025-06-01T13:30:00.000Z (in 1h 30m)"); - }); - - it("renders a past reset/exhaustion time as 'ago'", () => { - const out = formatKeyUsage( - [ - { - keyId: "opencode-1", - provider: "opencode-go", - status: "exhausted", - exhaustedAt: now - 2 * HOUR, - lastError: "boom", - windows: [], - }, - ], - now, - ); - - expect(out).toContain("status: EXHAUSTED (since 2025-06-01T10:00:00.000Z, 2h ago)"); - expect(out).toContain("last error: boom"); - }); - - it("returns a friendly message when no entries match", () => { - expect(formatKeyUsage([], now)).toBe("No API keys matched."); - }); -}); diff --git a/packages/core/tests/tools/list-files.test.ts b/packages/core/tests/tools/list-files.test.ts deleted file mode 100644 index f371717..0000000 --- a/packages/core/tests/tools/list-files.test.ts +++ /dev/null @@ -1,94 +0,0 @@ -import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { afterEach, beforeEach, describe, expect, it } from "vitest"; -import { createListFilesTool } from "../../src/tools/list-files.js"; - -describe("list_files tool", () => { - let workDir: string; - - beforeEach(async () => { - workDir = await mkdtemp(join(tmpdir(), "dispatch-test-")); - }); - - afterEach(async () => { - await rm(workDir, { recursive: true, force: true }); - }); - - it("lists directory contents", async () => { - const tool = createListFilesTool(workDir); - await writeFile(join(workDir, "file1.txt"), "a"); - await writeFile(join(workDir, "file2.txt"), "b"); - await mkdir(join(workDir, "subdir")); - const result = await tool.execute({ path: "." }); - expect(result).toContain("file1.txt"); - expect(result).toContain("file2.txt"); - expect(result).toContain("subdir/"); - }); - - it("defaults to current directory when path is undefined", async () => { - const tool = createListFilesTool(workDir); - await writeFile(join(workDir, "hello.txt"), "hi"); - const result = await tool.execute({}); - expect(result).toContain("hello.txt"); - }); - - it("blocks path traversal", async () => { - const tool = createListFilesTool(workDir); - const result = await tool.execute({ path: "../" }); - expect(result).toMatch(/outside the working directory/i); - }); - - // Regression for `resolve(join(workingDirectory, relPath))` — when relPath - // is absolute, `join` does NOT short-circuit. The old code silently - // rewrote `/some/path` to `<workdir>/some/path` and either returned an - // ENOENT-style error or, worse, listed an unrelated path. After the fix, - // absolute paths resolve to themselves and the workdir gate behaves correctly. - describe("absolute path handling", () => { - it("lists an absolute path that lives under the workdir", async () => { - const tool = createListFilesTool(workDir); - await writeFile(join(workDir, "alpha.txt"), "a"); - await writeFile(join(workDir, "beta.txt"), "b"); - const result = await tool.execute({ path: workDir }); - expect(result).toContain("alpha.txt"); - expect(result).toContain("beta.txt"); - // "Error listing files" would indicate the path was mangled into a - // non-existent location. - expect(result).not.toMatch(/error listing/i); - }); - - it("rejects absolute paths outside the workdir with the workdir error (not a generic ENOENT)", async () => { - const tool = createListFilesTool(workDir); - // Use a tmpdir path that's definitely not under workDir. Under the - // bug, this got rewritten to `<workdir>/tmp/...` and produced an - // `Error listing files` ENOENT message instead of the workdir error. - const evilPath = join(tmpdir(), `dispatch-evil-${Date.now()}`); - const result = await tool.execute({ path: evilPath }); - expect(result).toMatch(/outside the working directory/i); - }); - }); - - // A directory symlink inside the workdir pointing to an external - // directory is the classic escape vector for a `ls` style tool. - // `canonicalize` must resolve the symlink so the listing is denied. - describe("symlink handling", () => { - let externalDir: string; - - beforeEach(async () => { - externalDir = await mkdtemp(join(tmpdir(), "dispatch-external-")); - await writeFile(join(externalDir, "secret.txt"), "secret"); - }); - - afterEach(async () => { - await rm(externalDir, { recursive: true, force: true }); - }); - - it("blocks listing through a symlinked directory that escapes the workdir", async () => { - const tool = createListFilesTool(workDir); - await symlink(externalDir, join(workDir, "peek")); - const result = await tool.execute({ path: "peek" }); - expect(result).toMatch(/outside the working directory/i); - expect(result).not.toContain("secret.txt"); - }); - }); -}); diff --git a/packages/core/tests/tools/lsp-tool.test.ts b/packages/core/tests/tools/lsp-tool.test.ts deleted file mode 100644 index 7f26522..0000000 --- a/packages/core/tests/tools/lsp-tool.test.ts +++ /dev/null @@ -1,110 +0,0 @@ -import { describe, expect, it, vi } from "vitest"; -import type { LspManager } from "../../src/lsp/manager.js"; -import type { ResolvedLspServer } from "../../src/lsp/server.js"; -import { createLspTool, type LspToolContext } from "../../src/tools/lsp.js"; - -const SERVER: ResolvedLspServer = { - id: "luau-lsp", - extensions: [".luau"], - spawn: () => ({ process: {} as never }), -}; - -function makeManager(overrides: Partial<LspManager> = {}): LspManager { - return { - hasServerForFile: vi.fn(() => true), - touchFile: vi.fn(async () => {}), - getDiagnostics: vi.fn(() => ({})), - request: vi.fn(async () => []), - getClients: vi.fn(async () => []), - shutdownAll: vi.fn(async () => {}), - ...overrides, - } as unknown as LspManager; -} - -function ctx(manager: LspManager, servers = [SERVER]): () => LspToolContext { - return () => ({ manager, workingDirectory: "/work", servers }); -} - -describe("createLspTool", () => { - it("exposes the expected schema/name", () => { - const tool = createLspTool(ctx(makeManager())); - expect(tool.name).toBe("lsp"); - expect(tool.description).toMatch(/luau-lsp/i); - }); - - it("errors when no servers are configured", async () => { - const tool = createLspTool(ctx(makeManager(), [])); - const out = await tool.execute({ operation: "diagnostics", path: "a.luau" }); - expect(out).toMatch(/no LSP servers are configured/i); - }); - - it("errors when no server matches the file", async () => { - const manager = makeManager({ hasServerForFile: vi.fn(() => false) as never }); - const tool = createLspTool(ctx(manager)); - const out = await tool.execute({ operation: "diagnostics", path: "a.ts" }); - expect(out).toMatch(/no configured LSP server matches/i); - }); - - it("diagnostics: touches the file then reports errors", async () => { - const touchFile = vi.fn(async () => {}); - const getDiagnostics = vi.fn(() => ({ - "/work/a.luau": [ - { - range: { start: { line: 2, character: 1 }, end: { line: 2, character: 9 } }, - severity: 1, - message: "bad type", - }, - ], - })); - const manager = makeManager({ - touchFile: touchFile as never, - getDiagnostics: getDiagnostics as never, - }); - const tool = createLspTool(ctx(manager)); - const out = await tool.execute({ operation: "diagnostics", path: "a.luau" }); - expect(touchFile).toHaveBeenCalledOnce(); - expect(out).toContain("ERROR [3:2] bad type"); - }); - - it("diagnostics: reports clean when no errors", async () => { - const tool = createLspTool(ctx(makeManager())); - const out = await tool.execute({ operation: "diagnostics", path: "a.luau" }); - expect(out).toMatch(/No errors reported/i); - }); - - it("hover: requires line and character", async () => { - const tool = createLspTool(ctx(makeManager())); - const out = await tool.execute({ operation: "hover", path: "a.luau" }); - expect(out).toMatch(/requires both 'line' and 'character'/i); - }); - - it("hover: converts 1-based coords to 0-based on the wire", async () => { - const request = vi.fn(async () => [{ contents: "hi" }]); - const manager = makeManager({ request: request as never }); - const tool = createLspTool(ctx(manager)); - await tool.execute({ operation: "hover", path: "a.luau", line: 5, character: 3 }); - expect(request).toHaveBeenCalledOnce(); - const arg = request.mock.calls[0]?.[0] as { method: string; params: { position: unknown } }; - expect(arg.method).toBe("textDocument/hover"); - expect(arg.params.position).toEqual({ line: 4, character: 2 }); - }); - - it("references: includes declaration context", async () => { - const request = vi.fn(async () => []); - const manager = makeManager({ request: request as never }); - const tool = createLspTool(ctx(manager)); - await tool.execute({ operation: "references", path: "a.luau", line: 1, character: 1 }); - const arg = request.mock.calls[0]?.[0] as { params: { context?: unknown } }; - expect(arg.params.context).toEqual({ includeDeclaration: true }); - }); - - it("documentSymbol: does not require a position", async () => { - const request = vi.fn(async () => [{ name: "foo" }]); - const manager = makeManager({ request: request as never }); - const tool = createLspTool(ctx(manager)); - const out = await tool.execute({ operation: "documentSymbol", path: "a.luau" }); - const arg = request.mock.calls[0]?.[0] as { method: string }; - expect(arg.method).toBe("textDocument/documentSymbol"); - expect(out).toContain("foo"); - }); -}); diff --git a/packages/core/tests/tools/read-file.test.ts b/packages/core/tests/tools/read-file.test.ts deleted file mode 100644 index 90165d8..0000000 --- a/packages/core/tests/tools/read-file.test.ts +++ /dev/null @@ -1,142 +0,0 @@ -import { randomBytes } from "node:crypto"; -import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { afterEach, beforeEach, describe, expect, it } from "vitest"; -import { createReadFileTool } from "../../src/tools/read-file.js"; -import { SPILL_ROOT } from "../../src/tools/truncate.js"; - -describe("read_file tool", () => { - let workDir: string; - - beforeEach(async () => { - workDir = await mkdtemp(join(tmpdir(), "dispatch-test-")); - }); - - afterEach(async () => { - await rm(workDir, { recursive: true, force: true }); - }); - - it("reads an existing file", async () => { - const tool = createReadFileTool(workDir); - await writeFile(join(workDir, "hello.txt"), "Hello, world!"); - const result = await tool.execute({ path: "hello.txt" }); - expect(result).toContain("Hello, world!"); - expect(result).toContain("[file: hello.txt — lines 1-1 of 1]"); - }); - - it("returns error for non-existent file", async () => { - const tool = createReadFileTool(workDir); - const result = await tool.execute({ path: "missing.txt" }); - expect(result).toMatch(/not found/i); - }); - - it("blocks path traversal", async () => { - const tool = createReadFileTool(workDir); - const result = await tool.execute({ path: "../etc/passwd" }); - expect(result).toMatch(/outside the working directory/i); - }); - - it("respects offset and limit", async () => { - const tool = createReadFileTool(workDir); - await writeFile(join(workDir, "multi.txt"), "line1\nline2\nline3\nline4\nline5"); - const result = await tool.execute({ path: "multi.txt", offset: 2, limit: 2 }); - expect(result).toContain("line2"); - expect(result).toContain("line3"); - expect(result).not.toContain("line1"); - expect(result).not.toContain("line4"); - expect(result).toContain("[file: multi.txt — lines 2-3 of 5]"); - }); - - it("truncates long lines and points to read_file_slice", async () => { - const tool = createReadFileTool(workDir); - const longLine = "x".repeat(3000); - await writeFile(join(workDir, "wide.txt"), longLine); - const result = await tool.execute({ path: "wide.txt" }); - expect(result).toContain("[line 1 truncated, total 3,000 chars"); - expect(result).toContain("use read_file_slice"); - }); - - // The universal truncator writes oversized tool output to - // `${SPILL_ROOT}/<tabId>/<callId>.txt` and the truncation notice tells - // the AI to read that absolute path back. A previous implementation - // used `resolve(join(workingDirectory, filePath))` which silently - // concatenated the absolute spill path *under* the workdir, producing - // a non-existent path and ENOENT — breaking the entire spill-and-resume - // flow. These tests guard that contract. - describe("absolute path handling (spill-file regression)", () => { - let spillSubdir: string; - - beforeEach(async () => { - spillSubdir = join(SPILL_ROOT, `test-${Date.now()}-${randomBytes(4).toString("hex")}`); - await mkdir(spillSubdir, { recursive: true }); - }); - - afterEach(async () => { - await rm(spillSubdir, { recursive: true, force: true }); - }); - - it("reads a spill file via its absolute path", async () => { - const tool = createReadFileTool(workDir); - const spillFile = join(spillSubdir, "call-abc.txt"); - const payload = "spilled output line 1\nspilled output line 2"; - await writeFile(spillFile, payload); - - const result = await tool.execute({ path: spillFile }); - - expect(result).toContain("spilled output line 1"); - expect(result).toContain("spilled output line 2"); - expect(result).not.toMatch(/not found/i); - expect(result).not.toMatch(/outside the working directory/i); - }); - - it("still rejects absolute paths that are neither in the workdir nor the spill root", async () => { - const tool = createReadFileTool(workDir); - // Path check happens before file read, so /etc/hostname existing - // (or not) is irrelevant — we just need an absolute path outside - // both the workdir and SPILL_ROOT. - const result = await tool.execute({ path: "/etc/hostname" }); - expect(result).toMatch(/outside the working directory/i); - }); - }); - - // Symlinks must resolve consistently across the agent permission gate - // and the tool itself. The containment check operates on the canonical - // path — so a symlink-in-workdir that points outside is treated as - // "outside" and gated like any other external path. Lexical-only - // checks would let these slip through silently. - describe("symlink handling", () => { - let externalDir: string; - - beforeEach(async () => { - externalDir = await mkdtemp(join(tmpdir(), "dispatch-external-")); - }); - - afterEach(async () => { - await rm(externalDir, { recursive: true, force: true }); - }); - - it("follows symlinks that stay inside the workdir", async () => { - const tool = createReadFileTool(workDir); - await writeFile(join(workDir, "real.txt"), "real content"); - await symlink(join(workDir, "real.txt"), join(workDir, "link.txt")); - const result = await tool.execute({ path: "link.txt" }); - expect(result).toContain("real content"); - expect(result).not.toMatch(/outside the working directory/i); - }); - - it("blocks symlinks that escape the workdir", async () => { - const tool = createReadFileTool(workDir); - const secret = join(externalDir, "secret.txt"); - await writeFile(secret, "leaked secret"); - // Create a symlink *inside* workDir pointing to a file *outside* - // workDir. Lexical-only path validation would see "workdir/trap.txt" - // (under workdir) and allow it. Canonical resolution sees the - // symlink's target and correctly rejects. - await symlink(secret, join(workDir, "trap.txt")); - const result = await tool.execute({ path: "trap.txt" }); - expect(result).toMatch(/outside the working directory/i); - expect(result).not.toContain("leaked secret"); - }); - }); -}); diff --git a/packages/core/tests/tools/read-tab.test.ts b/packages/core/tests/tools/read-tab.test.ts deleted file mode 100644 index 71e419c..0000000 --- a/packages/core/tests/tools/read-tab.test.ts +++ /dev/null @@ -1,101 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { createReadTabTool, type ReadTabCallbacks } from "../../src/tools/read-tab.js"; -import type { TabResolution } from "../../src/tools/send-to-tab.js"; - -function makeCallbacks(overrides: Partial<ReadTabCallbacks> = {}): ReadTabCallbacks { - return { - resolveShortId: (): TabResolution => ({ - status: "ok", - tab: { id: "target-id", title: "Target", handle: "targ" }, - }), - getLastResponse: () => ({ text: "the answer is 42", status: "idle" }), - listOpenHandles: () => [{ handle: "targ", title: "Target" }], - ...overrides, - }; -} - -describe("createReadTabTool — schema & description", () => { - it("is a non-blocking snapshot read", () => { - const tool = createReadTabTool(makeCallbacks()); - expect(tool.name).toBe("read_tab"); - expect(tool.description).toContain("SNAPSHOT"); - expect(tool.description.toLowerCase()).toContain("does not block"); - }); -}); - -describe("createReadTabTool — execute()", () => { - it("returns the last assistant response wrapped in a tab_response tag", async () => { - const tool = createReadTabTool(makeCallbacks()); - const out = await tool.execute({ tab_id: "targ" }); - expect(out).toContain("<tab_response"); - expect(out).toContain('tab="targ"'); - expect(out).toContain('status="idle"'); - expect(out).toContain("the answer is 42"); - expect(out).toContain("</tab_response>"); - }); - - it("notes that a running tab's response is its previous completed turn", async () => { - const tool = createReadTabTool( - makeCallbacks({ - getLastResponse: () => ({ text: "older turn", status: "running" }), - }), - ); - const out = await tool.execute({ tab_id: "targ" }); - expect(out).toContain("still running"); - expect(out).toContain("older turn"); - }); - - it("explains when a tab has no completed response yet (idle)", async () => { - const tool = createReadTabTool( - makeCallbacks({ - getLastResponse: () => ({ text: null, status: "idle" }), - }), - ); - const out = await tool.execute({ tab_id: "targ" }); - expect(out).toContain("no completed response"); - expect(out).toContain("no assistant responses yet"); - }); - - it("explains when a tab is still on its first turn (running, no prior text)", async () => { - const tool = createReadTabTool( - makeCallbacks({ - getLastResponse: () => ({ text: null, status: "running" }), - }), - ); - const out = await tool.execute({ tab_id: "targ" }); - expect(out).toContain("no completed response"); - expect(out).toContain("still working on its first turn"); - }); - - it("rejects an empty tab_id and lists open handles", async () => { - const tool = createReadTabTool(makeCallbacks()); - const out = await tool.execute({ tab_id: "" }); - expect(out).toContain("Error"); - expect(out).toContain("targ"); - }); - - it("returns a helpful error when the id is unknown", async () => { - const tool = createReadTabTool(makeCallbacks({ resolveShortId: () => ({ status: "none" }) })); - const out = await tool.execute({ tab_id: "zzzz" }); - expect(out).toContain("no open tab matches"); - expect(out).toContain("Currently open tabs:"); - }); - - it("asks for more characters when the id is ambiguous", async () => { - const tool = createReadTabTool( - makeCallbacks({ - resolveShortId: () => ({ - status: "ambiguous", - matches: [ - { id: "a1", title: "One", handle: "abcd1" }, - { id: "a2", title: "Two", handle: "abcd2" }, - ], - }), - }), - ); - const out = await tool.execute({ tab_id: "abcd" }); - expect(out).toContain("ambiguous"); - expect(out).toContain("abcd1"); - expect(out).toContain("abcd2"); - }); -}); diff --git a/packages/core/tests/tools/registry.test.ts b/packages/core/tests/tools/registry.test.ts deleted file mode 100644 index cad75d2..0000000 --- a/packages/core/tests/tools/registry.test.ts +++ /dev/null @@ -1,143 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { z } from "zod"; -import { createToolRegistry } from "../../src/tools/registry.js"; -import type { ToolDefinition } from "../../src/types/index.js"; - -const mockTool: ToolDefinition = { - name: "mock_tool", - description: "A mock tool for testing", - parameters: z.object({ input: z.string() }), - execute: async (_args) => "mock result", -}; - -const anotherTool: ToolDefinition = { - name: "another_tool", - description: "Another mock tool", - parameters: z.object({ value: z.number() }), - execute: async (_args) => "another result", -}; - -/** A non-trivial tool that exercises nested objects, required fields, and enums. */ -const complexTool: ToolDefinition = { - name: "complex_tool", - description: "A tool with nested parameters", - parameters: z.object({ - command: z.string().describe("Shell command to run"), - options: z.object({ - timeout: z.number().optional().describe("Timeout in milliseconds"), - shell: z.enum(["bash", "sh", "zsh"]).describe("Shell to use"), - }), - flags: z.array(z.string()).optional().describe("Additional flags"), - }), - execute: async (_args) => "complex result", -}; - -describe("createToolRegistry", () => { - it("returns all tools via getTools()", () => { - const registry = createToolRegistry([mockTool, anotherTool]); - const tools = registry.getTools(); - expect(tools).toHaveLength(2); - expect(tools.map((t) => t.name)).toContain("mock_tool"); - expect(tools.map((t) => t.name)).toContain("another_tool"); - }); - - it("retrieves specific tool by name", () => { - const registry = createToolRegistry([mockTool, anotherTool]); - const tool = registry.getTool("mock_tool"); - expect(tool).toBeDefined(); - expect(tool?.name).toBe("mock_tool"); - }); - - it("returns undefined for unknown tool", () => { - const registry = createToolRegistry([mockTool]); - expect(registry.getTool("nonexistent")).toBeUndefined(); - }); - - describe("getAISDKTools", () => { - it("returns correct keys for all tools", () => { - const registry = createToolRegistry([mockTool, anotherTool]); - const aiTools = registry.getAISDKTools(); - expect(aiTools).toHaveProperty("mock_tool"); - expect(aiTools).toHaveProperty("another_tool"); - }); - - it("AI SDK tools have description from ToolDefinition", () => { - const registry = createToolRegistry([mockTool]); - const aiTools = registry.getAISDKTools(); - expect(aiTools.mock_tool.description).toBe("A mock tool for testing"); - }); - - it("AI SDK tools surface schema via inputSchema, not parameters", () => { - const registry = createToolRegistry([mockTool]); - const aiTools = registry.getAISDKTools(); - // v6 uses inputSchema; v4 used parameters — this verifies the migration - expect(aiTools.mock_tool).toHaveProperty("inputSchema"); - expect(aiTools.mock_tool).not.toHaveProperty("parameters"); - }); - - it("AI SDK tools have no execute callback so the SDK does not auto-run", () => { - const registry = createToolRegistry([mockTool, anotherTool, complexTool]); - const aiTools = registry.getAISDKTools(); - for (const [name, sdkTool] of Object.entries(aiTools)) { - expect( - (sdkTool as Record<string, unknown>).execute, - `Tool "${name}" should not have an execute callback`, - ).toBeUndefined(); - } - }); - - it("inputSchema produces valid JSONSchema7 for a simple tool", () => { - const registry = createToolRegistry([mockTool]); - const aiTools = registry.getAISDKTools(); - const schema = aiTools.mock_tool.inputSchema; - // jsonSchema() wraps the raw JSONSchema7; it should expose the schema - // as a `jsonSchema` property on the Schema object - expect(schema).toBeDefined(); - // The wrapped schema object should carry the JSON Schema definition - const schemaObj = schema as { jsonSchema: Record<string, unknown> }; - expect(schemaObj.jsonSchema).toBeDefined(); - expect(schemaObj.jsonSchema.type).toBe("object"); - const props = schemaObj.jsonSchema.properties as Record<string, unknown>; - expect(props).toHaveProperty("input"); - }); - - it("inputSchema produces correct JSONSchema7 for a non-trivial nested tool", () => { - const registry = createToolRegistry([complexTool]); - const aiTools = registry.getAISDKTools(); - const schema = aiTools.complex_tool.inputSchema; - expect(schema).toBeDefined(); - const schemaObj = schema as { jsonSchema: Record<string, unknown> }; - expect(schemaObj.jsonSchema.type).toBe("object"); - - const props = schemaObj.jsonSchema.properties as Record<string, Record<string, unknown>>; - - // Top-level required field "command" - expect(props).toHaveProperty("command"); - expect(props.command.type).toBe("string"); - - // Nested object "options" - expect(props).toHaveProperty("options"); - expect(props.options.type).toBe("object"); - const optProps = props.options.properties as Record<string, Record<string, unknown>>; - expect(optProps).toHaveProperty("shell"); - expect(optProps.shell.enum).toEqual(["bash", "sh", "zsh"]); - - // Optional array "flags" present as a property - expect(props).toHaveProperty("flags"); - expect(props.flags.type).toBe("array"); - - // Required fields should include "command" and "options" - const required = schemaObj.jsonSchema.required as string[]; - expect(required).toContain("command"); - expect(required).toContain("options"); - }); - - it("getTool still returns the original ToolDefinition with execute", () => { - const registry = createToolRegistry([mockTool]); - const def = registry.getTool("mock_tool"); - expect(def).toBeDefined(); - expect(typeof def?.execute).toBe("function"); - expect(def?.name).toBe("mock_tool"); - }); - }); -}); diff --git a/packages/core/tests/tools/run-shell.test.ts b/packages/core/tests/tools/run-shell.test.ts deleted file mode 100644 index cb66d1c..0000000 --- a/packages/core/tests/tools/run-shell.test.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { mkdtemp, rm } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { createRunShellTool } from "../../src/tools/run-shell.js"; - -describe("run_shell tool", () => { - let workDir: string; - - beforeEach(async () => { - workDir = await mkdtemp(join(tmpdir(), "dispatch-test-")); - }); - - afterEach(async () => { - await rm(workDir, { recursive: true, force: true }); - }); - - it("executes a simple echo command", async () => { - const tool = createRunShellTool(workDir); - const raw = await tool.execute({ command: "echo hello" }); - const result = JSON.parse(raw); - expect(result.stdout.trim()).toBe("hello"); - expect(result.exitCode).toBe(0); - }); - - it("returns non-zero exit code on failure", async () => { - const tool = createRunShellTool(workDir); - const raw = await tool.execute({ command: "exit 42" }); - const result = JSON.parse(raw); - expect(result.exitCode).toBe(42); - }); - - it("captures stderr", async () => { - const tool = createRunShellTool(workDir); - const raw = await tool.execute({ command: "echo errormsg >&2" }); - const result = JSON.parse(raw); - expect(result.stderr.trim()).toBe("errormsg"); - }); - - it("handles timeout", async () => { - const tool = createRunShellTool(workDir); - const raw = await tool.execute({ command: "sleep 10", timeout: 100 }); - const result = JSON.parse(raw); - // Either times out (non-zero exit) or returns an error - expect(result.exitCode !== 0 || result.error !== undefined).toBe(true); - }, 5000); - - it("executes in the working directory", async () => { - const tool = createRunShellTool(workDir); - const raw = await tool.execute({ command: "pwd" }); - const result = JSON.parse(raw); - // On macOS /tmp is symlinked; use includes check - expect(result.stdout.trim()).toContain(workDir.replace(/^\/private/, "")); - }); - - it("calls onOutput callback with stdout chunks", async () => { - const tool = createRunShellTool(workDir); - const onOutput = vi.fn(); - const raw = await tool.execute({ command: "echo streaming" }, { onOutput }); - const result = JSON.parse(raw); - expect(result.stdout.trim()).toBe("streaming"); - expect(onOutput).toHaveBeenCalledWith(expect.stringContaining("streaming"), "stdout"); - }); - - it("calls onOutput callback with stderr chunks", async () => { - const tool = createRunShellTool(workDir); - const onOutput = vi.fn(); - await tool.execute({ command: "echo errdata >&2" }, { onOutput }); - expect(onOutput).toHaveBeenCalledWith(expect.stringContaining("errdata"), "stderr"); - }); - - it("works without context (backward compatible)", async () => { - const tool = createRunShellTool(workDir); - const raw = await tool.execute({ command: "echo nocontext" }); - const result = JSON.parse(raw); - expect(result.stdout.trim()).toBe("nocontext"); - }); -}); diff --git a/packages/core/tests/tools/search-code.test.ts b/packages/core/tests/tools/search-code.test.ts deleted file mode 100644 index c4e933c..0000000 --- a/packages/core/tests/tools/search-code.test.ts +++ /dev/null @@ -1,511 +0,0 @@ -import { spawnSync } from "node:child_process"; -import { chmodSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; -import { mkdtemp as mkdtempP, rm as rmP, writeFile as writeFileP } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { afterEach, beforeEach, describe, expect, it } from "vitest"; -import { createSearchCodeTool } from "../../src/tools/search-code.js"; - -// A tiny stub that impersonates `cs`: it ignores its args and prints whatever -// JSON we put in the CS_STUB_OUTPUT env var. This makes JSON→text formatting -// tests fully deterministic without needing a real cs binary in CI. -function writeStub(dir: string, body: string): string { - const stubPath = join(dir, "cs-stub.sh"); - writeFileSync(stubPath, body, { mode: 0o755 }); - chmodSync(stubPath, 0o755); - return stubPath; -} - -const ECHO_ENV_STUB = `#!/usr/bin/env bash -printf '%s' "$CS_STUB_OUTPUT" -`; - -// A stub that writes to stderr and exits non-zero, impersonating a cs failure -// (bad flag, invalid regex, etc.). -const FAIL_STUB = `#!/usr/bin/env bash -echo "cs: simulated failure on stderr" >&2 -exit 3 -`; - -describe("search_code tool", () => { - let workDir: string; - const savedBin = process.env.DISPATCH_CS_BIN; - const savedStubOut = process.env.CS_STUB_OUTPUT; - - beforeEach(async () => { - workDir = await mkdtempP(join(tmpdir(), "dispatch-cs-test-")); - }); - - afterEach(async () => { - await rmP(workDir, { recursive: true, force: true }); - if (savedBin === undefined) delete process.env.DISPATCH_CS_BIN; - else process.env.DISPATCH_CS_BIN = savedBin; - if (savedStubOut === undefined) delete process.env.CS_STUB_OUTPUT; - else process.env.CS_STUB_OUTPUT = savedStubOut; - }); - - it("exposes the expected name and schema", () => { - const tool = createSearchCodeTool(workDir); - expect(tool.name).toBe("search_code"); - expect(tool.description).toContain("cs"); - // query is required; a representative set of optional knobs exist. - const shape = (tool.parameters as unknown as { shape: Record<string, unknown> }).shape; - expect(shape.query).toBeDefined(); - expect(shape.path).toBeDefined(); - expect(shape.only).toBeDefined(); - expect(shape.result_limit).toBeDefined(); - }); - - it("requires a non-empty query", async () => { - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: " " }); - expect(out).toMatch(/^Error:/); - expect(out).toContain("query is required"); - }); - - it("does not crash when params are the wrong type (model hallucination)", async () => { - const tool = createSearchCodeTool(workDir); - // A non-string query must be rejected gracefully, not throw. - const q = await tool.execute({ query: ["a", "b"] as unknown as string }); - expect(q).toMatch(/^Error:/); - expect(q).toContain("query is required"); - // A non-string include_ext (array) must not throw "x.trim is not a function". - const stubDir = await mkdtempP(join(tmpdir(), "dispatch-cs-stub-")); - try { - process.env.DISPATCH_CS_BIN = writeStub(stubDir, ECHO_ENV_STUB); - process.env.CS_STUB_OUTPUT = "null"; - const out = await tool.execute({ - query: "x", - include_ext: ["ts", "go"] as unknown as string, - exclude_pattern: { a: 1 } as unknown as string, - }); - expect(out).toBe("No matches found."); - } finally { - await rmP(stubDir, { recursive: true, force: true }); - } - }); - - it("rejects a path outside the working directory", async () => { - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "anything", path: "../../etc" }); - expect(out).toMatch(/^Error:/); - expect(out).toContain("outside the working directory"); - }); - - it("rejects a path that points at a file, not a directory", async () => { - await writeFileP(join(workDir, "a-file.ts"), "const x = 1;\n"); - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "x", path: "a-file.ts" }); - expect(out).toMatch(/^Error:/); - expect(out).toContain("is a file, not a directory"); - }); - - it("rejects a path that does not exist", async () => { - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "x", path: "no/such/dir" }); - expect(out).toMatch(/^Error:/); - expect(out).toContain("does not exist"); - }); - - it("returns an actionable error when the cs binary is missing", async () => { - process.env.DISPATCH_CS_BIN = "/nonexistent/path/to/cs-binary-xyz"; - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "anything" }); - expect(out).toMatch(/^Error:/); - expect(out).toContain("requires the 'cs'"); - expect(out).toContain("DISPATCH_CS_BIN"); - }); - - it("reports no matches when cs outputs null", async () => { - const stubDir = await mkdtempP(join(tmpdir(), "dispatch-cs-stub-")); - try { - process.env.DISPATCH_CS_BIN = writeStub(stubDir, ECHO_ENV_STUB); - process.env.CS_STUB_OUTPUT = "null"; - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "nothinghere" }); - expect(out).toBe("No matches found."); - } finally { - await rmP(stubDir, { recursive: true, force: true }); - } - }); - - it("formats cs JSON results into readable per-file blocks", async () => { - const stubDir = await mkdtempP(join(tmpdir(), "dispatch-cs-stub-")); - try { - const csJson = JSON.stringify([ - { - filename: "web-search.ts", - location: join(workDir, "packages/core/src/tools/web-search.ts"), - score: 5.24, - language: "TypeScript", - total_lines: 106, - lines: [ - { line_number: 7, content: "" }, - { - line_number: 8, - content: "export function createWebSearchTool(): ToolDefinition {", - match_positions: [[16, 35]], - }, - { line_number: 9, content: "\treturn {" }, - ], - }, - { - filename: "index.ts", - location: join(workDir, "packages/core/src/index.ts"), - score: 1.1, - language: "TypeScript", - lines: [{ line_number: 113, content: 'export { createWebSearchTool } from "./web.js";' }], - }, - ]); - process.env.DISPATCH_CS_BIN = writeStub(stubDir, ECHO_ENV_STUB); - process.env.CS_STUB_OUTPUT = csJson; - - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "createWebSearchTool" }); - - expect(out).toContain("Found matches in 2 files"); - // Paths are rendered relative to the workdir. - expect(out).toContain("packages/core/src/tools/web-search.ts [TypeScript] (score 5.24)"); - expect(out).not.toContain(workDir); - // Matched line is marked with '>'; line numbers + content present. - expect(out).toContain("> 8: export function createWebSearchTool(): ToolDefinition {"); - expect(out).toContain(" 7: "); - expect(out).toContain("packages/core/src/index.ts [TypeScript] (score 1.10)"); - } finally { - await rmP(stubDir, { recursive: true, force: true }); - } - }); - - it("renders cs 'content'-shape (prose) results instead of a bare header", async () => { - const stubDir = await mkdtempP(join(tmpdir(), "dispatch-cs-stub-")); - try { - // cs's snippet mode emits `content` + `matchlocations` and no `lines`. - const csJson = JSON.stringify([ - { - filename: "notes.md", - location: join(workDir, "docs/notes.md"), - score: 0.42, - language: "Markdown", - content: "Some heading\nthe orchestration paragraph that matched", - matchlocations: [[13, 26]], - }, - ]); - process.env.DISPATCH_CS_BIN = writeStub(stubDir, ECHO_ENV_STUB); - process.env.CS_STUB_OUTPUT = csJson; - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "orchestration" }); - expect(out).toContain("docs/notes.md [Markdown] (score 0.42)"); - // The snippet text must be present, not a bare header. - expect(out).toContain("the orchestration paragraph that matched"); - expect(out).not.toContain("no snippet available"); - } finally { - await rmP(stubDir, { recursive: true, force: true }); - } - }); - - it("truncates an excessively long snippet line", async () => { - const stubDir = await mkdtempP(join(tmpdir(), "dispatch-cs-stub-")); - try { - const longContent = `const x = "${"Z".repeat(5000)}";`; - const csJson = JSON.stringify([ - { - filename: "big.ts", - location: join(workDir, "big.ts"), - score: 1, - language: "TypeScript", - lines: [{ line_number: 1, content: longContent, match_positions: [[10, 14]] }], - }, - ]); - process.env.DISPATCH_CS_BIN = writeStub(stubDir, ECHO_ENV_STUB); - process.env.CS_STUB_OUTPUT = csJson; - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "x" }); - expect(out).toContain("line truncated"); - // No single output line should approach the raw 5k length. - const longest = Math.max(...out.split("\n").map((l) => l.length)); - expect(longest).toBeLessThan(700); - } finally { - await rmP(stubDir, { recursive: true, force: true }); - } - }); - - it("surfaces raw output when cs returns unparseable JSON", async () => { - const stubDir = await mkdtempP(join(tmpdir(), "dispatch-cs-stub-")); - try { - process.env.DISPATCH_CS_BIN = writeStub(stubDir, ECHO_ENV_STUB); - process.env.CS_STUB_OUTPUT = "this is not json"; - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "x" }); - expect(out).toMatch(/^Error:/); - expect(out).toContain("could not parse cs output"); - expect(out).toContain("this is not json"); - } finally { - await rmP(stubDir, { recursive: true, force: true }); - } - }); - - it("reports an error (not 'No matches') when cs exits non-zero", async () => { - const stubDir = await mkdtempP(join(tmpdir(), "dispatch-cs-stub-")); - try { - process.env.DISPATCH_CS_BIN = writeStub(stubDir, FAIL_STUB); - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "x" }); - expect(out).toMatch(/^Error:/); - expect(out).toContain("exited with code 3"); - // stderr from cs is surfaced to the caller. - expect(out).toContain("simulated failure on stderr"); - expect(out).not.toContain("No matches found"); - } finally { - await rmP(stubDir, { recursive: true, force: true }); - } - }); - - // ── Live integration: only runs when a real `cs` binary is available. ── - const liveCsBin = findRealCs(); - describe.runIf(liveCsBin)("live cs binary", () => { - it("finds a real match and ranks the defining file", async () => { - process.env.DISPATCH_CS_BIN = liveCsBin as string; - // Seed a small tree with a clear match. - await writeFileP( - join(workDir, "alpha.ts"), - "export function findTheNeedle() {\n return 42;\n}\n", - ); - await writeFileP(join(workDir, "beta.ts"), "const x = 1;\n// nothing relevant here\n"); - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "findTheNeedle" }); - expect(out).toContain("alpha.ts"); - expect(out).toContain("findTheNeedle"); - expect(out).not.toContain("Error:"); - }); - - it("treats a dash-leading query as a search term, not a cs flag", async () => { - process.env.DISPATCH_CS_BIN = liveCsBin as string; - // A literal token beginning with '-' must not be parsed as a flag. - await writeFileP(join(workDir, "dash.ts"), "const dashToken = 1;\n"); - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "-dashToken" }); - // Whether or not cs ranks a hit, it must NOT error out on flag parsing. - expect(out).not.toContain("unknown shorthand flag"); - expect(out).not.toMatch(/^Error: cs exited/); - }); - - it("renders snippet lines for prose (markdown) matches", async () => { - process.env.DISPATCH_CS_BIN = liveCsBin as string; - await writeFileP( - join(workDir, "doc.md"), - "# Title\n\nThis paragraph mentions widgetronics in prose.\n", - ); - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "widgetronics" }); - expect(out).toContain("doc.md"); - // The matching prose text must be shown, not just a bare header. - expect(out).toContain("widgetronics"); - expect(out).not.toContain("no snippet available"); - }); - - it("widens the snippet window when context is given", async () => { - process.env.DISPATCH_CS_BIN = liveCsBin as string; - const body = Array.from({ length: 21 }, (_, i) => `line ${i + 1}`); - body[10] = "const findContextTarget = 1;"; - await writeFileP(join(workDir, "ctx.ts"), `${body.join("\n")}\n`); - const tool = createSearchCodeTool(workDir); - const countSnippetLines = (s: string) => - s.split("\n").filter((l) => /^\s+>?\s*\d+:/.test(l)).length; - const narrow = await tool.execute({ - query: "findContextTarget", - context: 0, - result_limit: 1, - }); - const wide = await tool.execute({ - query: "findContextTarget", - context: 6, - result_limit: 1, - }); - expect(countSnippetLines(wide)).toBeGreaterThan(countSnippetLines(narrow)); - }); - - it("returns 'No matches found.' for a query with no hits", async () => { - process.env.DISPATCH_CS_BIN = liveCsBin as string; - await writeFileP(join(workDir, "alpha.ts"), "export const a = 1;\n"); - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "zzz_nonexistent_token_qqq" }); - expect(out).toBe("No matches found."); - }); - - it("tags .luau files as Luau", async () => { - process.env.DISPATCH_CS_BIN = liveCsBin as string; - await writeFileP(join(workDir, "mod.luau"), "function Mod.doThing()\n\treturn 1\nend\n"); - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "doThing" }); - expect(out).toContain("mod.luau"); - expect(out).toContain("[Luau]"); - }); - }); - - // ── Luau declaration detection: needs a cs built with the Luau patch - // (docker/cs/luau-declarations.patch). Skipped on an unpatched/older cs. ── - const luauCsBin = findLuauCapableCs(liveCsBin); - describe.runIf(luauCsBin)("live cs binary (Luau declaration patch)", () => { - // A small Luau module exercising every declaration form the patch adds. - const LUAU_MODULE = [ - "local Mod = {}", - "", - "export type StuntResult = {", - "\tscore: number,", - "}", - "", - "type LaunchConfig = StuntResult", - "", - "function Mod.getDefaults(): LaunchConfig", - "\treturn { score = 0 }", - "end", - "", - "local function helperThing(x: number): number", - "\treturn x + 1", - "end", - "", - "Mod.live = Mod.getDefaults()", - "local used = helperThing(1)", - "", - ].join("\n"); - - beforeEach(async () => { - process.env.DISPATCH_CS_BIN = luauCsBin as string; - await writeFileP(join(workDir, "Mod.luau"), LUAU_MODULE); - }); - - it("detects `function Mod.x` declarations in .luau files", async () => { - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "getDefaults", only: "declarations" }); - expect(out).toContain("Mod.luau"); - expect(out).toContain("function Mod.getDefaults"); - expect(out).not.toContain("No matches found"); - }); - - it("detects `local function` declarations in .luau files", async () => { - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "helperThing", only: "declarations" }); - expect(out).toContain("Mod.luau"); - expect(out).toContain("local function helperThing"); - }); - - it("detects `type` / `export type` declarations in .luau files", async () => { - const tool = createSearchCodeTool(workDir); - const exportType = await tool.execute({ query: "StuntResult", only: "declarations" }); - expect(exportType).toContain("export type StuntResult"); - const aliasType = await tool.execute({ query: "LaunchConfig", only: "declarations" }); - expect(aliasType).toContain("type LaunchConfig"); - }); - - it("excludes declaration lines when only=usages", async () => { - const tool = createSearchCodeTool(workDir); - const out = await tool.execute({ query: "getDefaults", only: "usages" }); - // The call site is a usage; the `function Mod.getDefaults` definition is not. - expect(out).toContain("Mod.live = Mod.getDefaults()"); - expect(out).not.toContain("function Mod.getDefaults"); - }); - }); - - // ── Fuzzy mid-word matching: needs a cs built with the fuzzy patch - // (docker/cs/fuzzy-distance.patch). Skipped on an unpatched/older cs. ── - const fuzzyCsBin = findFuzzyCapableCs(liveCsBin); - describe.runIf(fuzzyCsBin)("live cs binary (fuzzy edit-distance patch)", () => { - beforeEach(() => { - process.env.DISPATCH_CS_BIN = fuzzyCsBin as string; - }); - - it("matches a mid-word deletion within distance 1", async () => { - await writeFileP( - join(workDir, "phys.ts"), - "export function computeSlipAngle() {\n\treturn 0;\n}\n", - ); - const tool = createSearchCodeTool(workDir); - // "computSlipAngle" drops the 'e' mid-word — edit distance 1. - const out = await tool.execute({ query: "computSlipAngle~1" }); - expect(out).toContain("phys.ts"); - expect(out).toContain("computeSlipAngle"); - expect(out).not.toBe("No matches found."); - }); - - it("matches a mid-word insertion within distance 1", async () => { - await writeFileP(join(workDir, "tire.ts"), "const tireFriction = 1;\n"); - const tool = createSearchCodeTool(workDir); - // "tireFricction" has an extra 'c' — edit distance 1. - const out = await tool.execute({ query: "tireFricction~1" }); - expect(out).toContain("tire.ts"); - expect(out).toContain("tireFriction"); - }); - }); -}); - -/** - * Locate a usable `cs` binary for live tests. Honors DISPATCH_CS_TEST_BIN, then - * a `cs` on PATH. Returns null when none is runnable, so the live suite is - * skipped rather than failing in environments without cs. - */ -function findRealCs(): string | null { - const candidates = [process.env.DISPATCH_CS_TEST_BIN, "cs"].filter(Boolean) as string[]; - for (const bin of candidates) { - try { - const res = spawnSync(bin, ["--version"], { stdio: "ignore" }); - if (res.status === 0) return bin; - } catch { - // try next - } - } - return null; -} - -/** - * Probe a `cs` binary against a throwaway corpus and return its trimmed stdout - * (or "" on any failure). Used by the capability gates below so patch-dependent - * live tests run only on a cs that actually has the patch — and skip (not fail) - * on an unpatched/older binary. - */ -function probeCs(bin: string, files: Record<string, string>, args: string[]): string { - let dir: string | undefined; - try { - dir = mkdtempSync(join(tmpdir(), "dispatch-cs-probe-")); - for (const [name, body] of Object.entries(files)) { - writeFileSync(join(dir, name), body); - } - const res = spawnSync(bin, ["-f", "json", "--dir", dir, ...args], { - encoding: "utf8", - }); - if (res.status !== 0 || !res.stdout) return ""; - return res.stdout.trim(); - } catch { - return ""; - } finally { - if (dir) rmSync(dir, { recursive: true, force: true }); - } -} - -/** - * Return the cs binary only if it recognises Luau declarations (i.e. was built - * with docker/cs/luau-declarations.patch): a `--only-declarations` search for a - * top-level `function` in a .luau file yields a result. Otherwise null → skip. - */ -function findLuauCapableCs(bin: string | null): string | null { - if (!bin) return null; - const out = probeCs(bin, { "probe.luau": "function Probe.thing()\n\treturn 1\nend\n" }, [ - "--only-declarations", - "--", - "thing", - ]); - return out !== "" && out !== "null" ? bin : null; -} - -/** - * Return the cs binary only if its fuzzy matcher honours mid-word edits (i.e. - * was built with docker/cs/fuzzy-distance.patch): a distance-1 deletion matches. - * Otherwise null → skip. - */ -function findFuzzyCapableCs(bin: string | null): string | null { - if (!bin) return null; - const out = probeCs(bin, { "probe.txt": "const x = computeSlipAngle;\n" }, [ - "--", - "computSlipAngle~1", - ]); - return out !== "" && out !== "null" ? bin : null; -} diff --git a/packages/core/tests/tools/send-to-tab.test.ts b/packages/core/tests/tools/send-to-tab.test.ts deleted file mode 100644 index 21d8032..0000000 --- a/packages/core/tests/tools/send-to-tab.test.ts +++ /dev/null @@ -1,185 +0,0 @@ -import { describe, expect, it, vi } from "vitest"; -import { - createSendToTabTool, - type SendToTabCallbacks, - type TabResolution, -} from "../../src/tools/send-to-tab.js"; - -function makeCallbacks(overrides: Partial<SendToTabCallbacks> = {}): SendToTabCallbacks { - return { - resolveShortId: (): TabResolution => ({ - status: "ok", - tab: { id: "target-id", title: "Target", handle: "targ" }, - }), - deliver: () => ({ status: "started" }), - listOpenHandles: () => [{ handle: "targ", title: "Target" }], - self: { id: "self-id", handle: "self" }, - canReadTab: true, - ...overrides, - }; -} - -describe("createSendToTabTool — schema & description", () => { - it("exposes tab_id and message params and a fire-and-forget description", () => { - const tool = createSendToTabTool(makeCallbacks()); - expect(tool.name).toBe("send_to_tab"); - expect(tool.description).toContain("fire-and-forget"); - expect(tool.description.toLowerCase()).toContain("queued"); - // Description must steer the model away from busy-waiting for a reply. - expect(tool.description.toLowerCase()).toContain("do not sleep"); - expect(tool.description.toLowerCase()).toContain("end your turn"); - }); - - it("mentions read_tab in the description only when canReadTab is true", () => { - const tool = createSendToTabTool(makeCallbacks({ canReadTab: true })); - expect(tool.description).toContain("read_tab"); - }); - - it("never mentions read_tab in the description when canReadTab is false", () => { - const tool = createSendToTabTool(makeCallbacks({ canReadTab: false })); - expect(tool.description).not.toContain("read_tab"); - // Still tells the agent a reply will wake it + to end its turn. - expect(tool.description.toLowerCase()).toContain("wake you with a new message"); - expect(tool.description.toLowerCase()).toContain("end your turn"); - }); -}); - -describe("createSendToTabTool — execute()", () => { - it("delivers to a resolved target and reports the started status", async () => { - const deliver = vi.fn(() => ({ status: "started" as const })); - const tool = createSendToTabTool(makeCallbacks({ deliver })); - const out = await tool.execute({ tab_id: "targ", message: "hello there" }); - expect(deliver).toHaveBeenCalledTimes(1); - const [targetId, delivered] = deliver.mock.calls[0] ?? []; - expect(targetId).toBe("target-id"); - // Provenance header names the sending tab's handle and marks it as a - // peer agent (not the recipient's own user). - expect(delivered).toContain("[message from tab self"); - expect(delivered).toContain("another agent"); - expect(delivered).toContain("hello there"); - // Reply contract: the recipient must answer via send_to_tab back to the - // sender's handle, not as a plain text reply to its own user. - expect(delivered).toContain('send_to_tab tool with tab_id "self"'); - expect(delivered).toContain("ONLY reply if"); - expect(out).toContain("idle"); - expect(out).toContain("targ"); - // Sender is steered away from busy-waiting and told to end its turn. - expect(out.toLowerCase()).toContain("do not sleep"); - expect(out.toLowerCase()).toContain("end your turn"); - }); - - it("points the sender at read_tab in the result only when canReadTab is true", async () => { - const deliver = vi.fn(() => ({ status: "started" as const })); - const tool = createSendToTabTool(makeCallbacks({ deliver, canReadTab: true })); - const out = await tool.execute({ tab_id: "targ", message: "hi" }); - expect(out).toContain("read_tab"); - }); - - it("omits read_tab from the result when canReadTab is false", async () => { - const deliver = vi.fn(() => ({ status: "started" as const })); - const tool = createSendToTabTool(makeCallbacks({ deliver, canReadTab: false })); - const out = await tool.execute({ tab_id: "targ", message: "hi" }); - expect(out).not.toContain("read_tab"); - // Still steers away from busy-waiting and toward ending the turn. - expect(out.toLowerCase()).toContain("do not sleep"); - expect(out.toLowerCase()).toContain("end your turn"); - }); - - it("reports the queued status when the target is busy", async () => { - const deliver = vi.fn(() => ({ status: "queued" as const })); - const tool = createSendToTabTool(makeCallbacks({ deliver })); - const out = await tool.execute({ tab_id: "targ", message: "ping" }); - expect(out.toLowerCase()).toContain("queued"); - expect(out.toLowerCase()).toContain("busy"); - }); - - it("reports a HELD message when delivery is suppressed (auto-wake limit hit)", async () => { - const deliver = vi.fn(() => ({ status: "suppressed" as const })); - const tool = createSendToTabTool(makeCallbacks({ deliver })); - const out = await tool.execute({ tab_id: "targ", message: "ping again" }); - expect(out).toContain("HELD"); - expect(out.toLowerCase()).toContain("limit"); - // It must steer the sender away from retrying in a loop. - expect(out.toLowerCase()).toContain("do not keep resending"); - expect(out.toLowerCase()).toContain("human"); - }); - - it("rejects an empty tab_id and lists open handles", async () => { - const tool = createSendToTabTool(makeCallbacks()); - const out = await tool.execute({ tab_id: " ", message: "hi" }); - expect(out).toContain("Error"); - expect(out).toContain("targ"); - }); - - it("rejects an empty message", async () => { - const deliver = vi.fn(() => ({ status: "started" as const })); - const tool = createSendToTabTool(makeCallbacks({ deliver })); - const out = await tool.execute({ tab_id: "targ", message: " " }); - expect(out).toContain("Error"); - expect(deliver).not.toHaveBeenCalled(); - }); - - it("returns a helpful error and open-tab list when the id is unknown", async () => { - const deliver = vi.fn(() => ({ status: "started" as const })); - const tool = createSendToTabTool( - makeCallbacks({ - resolveShortId: () => ({ status: "none" }), - deliver, - }), - ); - const out = await tool.execute({ tab_id: "zzzz", message: "hi" }); - expect(out).toContain("no open tab matches"); - expect(out).toContain("Currently open tabs:"); - expect(deliver).not.toHaveBeenCalled(); - }); - - it("asks for more characters when the id is ambiguous", async () => { - const deliver = vi.fn(() => ({ status: "started" as const })); - const tool = createSendToTabTool( - makeCallbacks({ - resolveShortId: () => ({ - status: "ambiguous", - matches: [ - { id: "a1", title: "One", handle: "abcd1" }, - { id: "a2", title: "Two", handle: "abcd2" }, - ], - }), - deliver, - }), - ); - const out = await tool.execute({ tab_id: "abcd", message: "hi" }); - expect(out).toContain("ambiguous"); - expect(out).toContain("abcd1"); - expect(out).toContain("abcd2"); - expect(deliver).not.toHaveBeenCalled(); - }); - - it("refuses to send to its own tab", async () => { - const deliver = vi.fn(() => ({ status: "started" as const })); - const tool = createSendToTabTool( - makeCallbacks({ - resolveShortId: () => ({ - status: "ok", - tab: { id: "self-id", title: "Me", handle: "self" }, - }), - deliver, - }), - ); - const out = await tool.execute({ tab_id: "self", message: "hi" }); - expect(out).toContain("cannot send a message to your own tab"); - expect(deliver).not.toHaveBeenCalled(); - }); - - it("surfaces a thrown delivery error instead of crashing", async () => { - const tool = createSendToTabTool( - makeCallbacks({ - deliver: () => { - throw new Error("boom"); - }, - }), - ); - const out = await tool.execute({ tab_id: "targ", message: "hi" }); - expect(out).toContain("Error delivering message"); - expect(out).toContain("boom"); - }); -}); diff --git a/packages/core/tests/tools/summon.test.ts b/packages/core/tests/tools/summon.test.ts deleted file mode 100644 index 4885a94..0000000 --- a/packages/core/tests/tools/summon.test.ts +++ /dev/null @@ -1,349 +0,0 @@ -import { describe, expect, it, vi } from "vitest"; -import { - type AvailableAgent, - createSummonTool, - type SummonCallbacks, -} from "../../src/tools/summon.js"; - -const noopCallbacks: SummonCallbacks = { - spawn: async () => "agent-id-stub", - getResult: async () => ({ status: "done", result: "" }), -}; - -describe("createSummonTool — description content", () => { - it("lists the agent directories so the LLM knows where to look", () => { - const tool = createSummonTool( - "/tmp/work", - noopCallbacks, - [], - [], - ["/home/u/.config/dispatch/agents", "/tmp/work/.dispatch/agents"], - ); - expect(tool.description).toContain("/home/u/.config/dispatch/agents"); - expect(tool.description).toContain("/tmp/work/.dispatch/agents"); - expect(tool.description).toContain("read_file"); - }); - - it("includes available agent slugs+names in the description", () => { - const agents: AvailableAgent[] = [ - { - slug: "programmer", - name: "Programmer", - description: "Implements code from a plan.", - path: "/home/u/.config/dispatch/agents/programmer.toml", - }, - { - slug: "researcher", - name: "Researcher", - description: "Investigates topics.", - path: "/home/u/.config/dispatch/agents/researcher.toml", - }, - ]; - const tool = createSummonTool( - "/tmp/work", - noopCallbacks, - agents, - [], - ["/home/u/.config/dispatch/agents"], - ); - expect(tool.description).toContain("programmer"); - expect(tool.description).toContain("Programmer"); - expect(tool.description).toContain("Implements code from a plan"); - expect(tool.description).toContain("researcher"); - expect(tool.description).toContain("Investigates topics"); - }); - - it("emits a 'no agents defined' notice when the catalog is empty", () => { - const tool = createSummonTool( - "/tmp/work", - noopCallbacks, - [], - [], - ["/home/u/.config/dispatch/agents"], - ); - expect(tool.description).toContain("No agent definitions are currently defined"); - }); - - it("shows two groups when userAgentEnabled is true", () => { - const subagents: AvailableAgent[] = [ - { - slug: "programmer", - name: "Programmer", - description: "Codes things", - path: "/agents/programmer.toml", - }, - ]; - const userAgents: AvailableAgent[] = [ - { - slug: "default", - name: "Default", - description: "Default agent", - path: "/agents/default.toml", - }, - ]; - const tool = createSummonTool( - "/tmp/work", - noopCallbacks, - subagents, - userAgents, - ["/agents"], - true, - ); - expect(tool.description).toContain("Subagents (spawned as child tabs):"); - expect(tool.description).toContain( - "User agents (spawned as independent top-level tabs, requires top_level=true):", - ); - expect(tool.description).toContain("programmer"); - expect(tool.description).toContain("default"); - }); - - it("hides user agents group when userAgentEnabled is false", () => { - const subagents: AvailableAgent[] = [ - { - slug: "programmer", - name: "Programmer", - description: "Codes things", - path: "/agents/programmer.toml", - }, - ]; - const userAgents: AvailableAgent[] = [ - { - slug: "default", - name: "Default", - description: "Default agent", - path: "/agents/default.toml", - }, - ]; - const tool = createSummonTool( - "/tmp/work", - noopCallbacks, - subagents, - userAgents, - ["/agents"], - false, - ); - expect(tool.description).toContain("Available agents:"); - expect(tool.description).not.toContain("User agents"); - // "default" appears in generic description text, so check for the slug listing format - expect(tool.description).not.toContain("- default: Default"); - }); -}); - -describe("createSummonTool — execute() argument forwarding", () => { - it("forwards agent slug through to callbacks.spawn", async () => { - const spawn = vi.fn(async () => "tab-xyz"); - const tool = createSummonTool( - "/tmp/work", - { spawn, getResult: async () => ({ status: "done", result: "ok" }) }, - [], - [], - ); - await tool.execute({ - task: "do thing", - agent: "programmer", - background: true, - }); - expect(spawn).toHaveBeenCalledTimes(1); - const callArg = spawn.mock.calls[0]?.[0]; - expect(callArg).toMatchObject({ - task: "do thing", - agentSlug: "programmer", - }); - }); - - it("returns spawned agent_id when background=true (no blocking on result)", async () => { - const getResult = vi.fn(async () => ({ status: "done" as const, result: "should-not-see" })); - const tool = createSummonTool("/tmp/work", { spawn: async () => "id-42", getResult }, [], []); - const out = await tool.execute({ task: "x", agent: "test-agent", background: true }); - expect(out).toContain("id-42"); - // Background mode must not block on getResult - expect(getResult).not.toHaveBeenCalled(); - }); - - it("blocks on result and returns it when background=false (default)", async () => { - const tool = createSummonTool( - "/tmp/work", - { - spawn: async () => "id-1", - getResult: async () => ({ status: "done", result: "child-output" }), - }, - [], - [], - ); - const out = await tool.execute({ task: "x", agent: "test-agent" }); - // Foreground summons prefix the blocked result with `agent_id: <id>` so - // the frontend's ToolCallDisplay regex can surface the "Open Tab" button - // (see summon.ts). Assert both the prefix and the child output survive. - expect(out).toContain("agent_id: id-1"); - expect(out).toBe("agent_id: id-1\n\nchild-output"); - }); - - it("surfaces child errors when blocking", async () => { - const tool = createSummonTool( - "/tmp/work", - { - spawn: async () => "id-1", - getResult: async () => ({ status: "error", error: "boom" }), - }, - [], - [], - ); - const out = await tool.execute({ task: "x", agent: "test-agent" }); - expect(out).toContain("boom"); - }); - - it("returns fire-and-forget message when top_level=true", async () => { - const spawn = vi.fn(async () => "ua-tab-1"); - const getResult = vi.fn(async () => ({ status: "done" as const, result: "nope" })); - const tool = createSummonTool( - "/tmp/work", - { spawn, getResult }, - [], - [], - [], - true, // userAgentEnabled - ); - const out = await tool.execute({ - task: "do stuff", - agent: "default", - top_level: true, - }); - expect(out).toContain("User agent spawned successfully"); - expect(out).toContain("ua-tab-1"); - expect(out).toContain("fire-and-forget"); - expect(getResult).not.toHaveBeenCalled(); - - // Verify topLevel was forwarded to spawn - const callArg = spawn.mock.calls[0]?.[0]; - expect(callArg).toMatchObject({ topLevel: true }); - }); - - it("ignores top_level when userAgentEnabled is false", async () => { - const spawn = vi.fn(async () => "tab-1"); - const getResult = vi.fn(async () => ({ status: "done" as const, result: "result" })); - const tool = createSummonTool( - "/tmp/work", - { spawn, getResult }, - [], - [], - [], - false, // userAgentEnabled - ); - const out = await tool.execute({ - task: "do stuff", - agent: "default", - top_level: true, // should be ignored - }); - // Should behave as a normal foreground summon, not fire-and-forget - expect(out).not.toContain("fire-and-forget"); - expect(getResult).toHaveBeenCalled(); - }); -}); - -describe("createSummonTool — user-agent-only mode (perm_user_agent without perm_summon)", () => { - // userAgentEnabled=true, subagentEnabled=false → the tool spawns ONLY - // top-level user agents. `top_level` is implied (and forced), the - // subagent/parallel-work prose is dropped, and only the user-agent - // catalog group is shown. - const subagents: AvailableAgent[] = [ - { - slug: "programmer", - name: "Programmer", - description: "Codes things", - path: "/agents/programmer.toml", - }, - ]; - const userAgents: AvailableAgent[] = [ - { - slug: "default", - name: "Default", - description: "Default agent", - path: "/agents/default.toml", - }, - ]; - - function userAgentOnlyTool( - spawn = vi.fn(async () => "ua-1"), - getResult = vi.fn(async () => ({ status: "done" as const, result: "nope" })), - ) { - return { - spawn, - getResult, - tool: createSummonTool( - "/tmp/work", - { spawn, getResult }, - subagents, - userAgents, - ["/agents"], - true, // userAgentEnabled - false, // subagentEnabled - ), - }; - } - - it("describes spawning user agents and omits subagent/parallel-work prose", () => { - const { tool } = userAgentOnlyTool(); - expect(tool.description).toContain("Spawn an independent top-level user agent"); - expect(tool.description).toContain("fire-and-forget"); - expect(tool.description).not.toContain("Pattern for parallel work"); - expect(tool.description).not.toContain("Set background=true"); - }); - - it("lists only the user-agent catalog group, not subagents", () => { - const { tool } = userAgentOnlyTool(); - expect(tool.description).toContain("User agents (spawned as independent top-level tabs):"); - expect(tool.description).toContain("default"); - // Subagents must not be advertised in user-agent-only mode. - expect(tool.description).not.toContain("Subagents (spawned as child tabs):"); - expect(tool.description).not.toContain("- programmer: Programmer"); - }); - - it("only lists user-agent slugs in the 'agent' parameter description", () => { - const { tool } = userAgentOnlyTool(); - const agentParam = (tool.parameters as unknown as { shape: { agent: { description: string } } }) - .shape.agent; - expect(agentParam.description).toContain("default"); - expect(agentParam.description).not.toContain("programmer"); - }); - - it("omits the top_level parameter (it is implied)", () => { - const { tool } = userAgentOnlyTool(); - const shape = (tool.parameters as unknown as { shape: Record<string, unknown> }).shape; - expect("top_level" in shape).toBe(false); - }); - - it("omits the background parameter (user agents are fire-and-forget)", () => { - const { tool } = userAgentOnlyTool(); - const shape = (tool.parameters as unknown as { shape: Record<string, unknown> }).shape; - expect("background" in shape).toBe(false); - }); - - it("forces topLevel=true on spawn even when top_level is not passed", async () => { - const spawn = vi.fn(async () => "ua-99"); - const getResult = vi.fn(async () => ({ status: "done" as const, result: "nope" })); - const { tool } = userAgentOnlyTool(spawn, getResult); - const out = await tool.execute({ task: "do stuff", agent: "default" }); - expect(out).toContain("User agent spawned successfully"); - expect(out).toContain("ua-99"); - expect(out).toContain("fire-and-forget"); - // Never blocks on a result for fire-and-forget user agents. - expect(getResult).not.toHaveBeenCalled(); - const callArg = spawn.mock.calls[0]?.[0]; - expect(callArg).toMatchObject({ topLevel: true, agentSlug: "default" }); - }); -}); - -describe("createSummonTool — subagentEnabled defaults preserve legacy behavior", () => { - it("defaults subagentEnabled=true so omitting it keeps subagent spawning", async () => { - const spawn = vi.fn(async () => "tab-1"); - const getResult = vi.fn(async () => ({ status: "done" as const, result: "child" })); - // No userAgentEnabled/subagentEnabled args → legacy subagent-only mode. - const tool = createSummonTool("/tmp/work", { spawn, getResult }, [], []); - const out = await tool.execute({ task: "x", agent: "programmer" }); - // Foreground subagent summon blocks and returns the child result. - expect(out).toBe("agent_id: tab-1\n\nchild"); - expect(getResult).toHaveBeenCalled(); - const callArg = spawn.mock.calls[0]?.[0]; - expect(callArg).not.toHaveProperty("topLevel"); - }); -}); diff --git a/packages/core/tests/tools/task-list.test.ts b/packages/core/tests/tools/task-list.test.ts deleted file mode 100644 index 5903fec..0000000 --- a/packages/core/tests/tools/task-list.test.ts +++ /dev/null @@ -1,158 +0,0 @@ -import { describe, expect, it, vi } from "vitest"; -import { createTaskListTool, TaskList } from "../../src/tools/task-list.js"; -import type { TaskItem } from "../../src/types/index.js"; - -describe("TaskList (declarative store)", () => { - it("starts empty", () => { - const list = new TaskList(); - expect(list.getTasks()).toEqual([]); - }); - - it("setTasks replaces the whole list and assigns positional ids", () => { - const list = new TaskList(); - const result = list.setTasks([ - { content: "first", status: "in_progress" }, - { content: "second", status: "pending" }, - ]); - expect(result).toEqual([ - { id: "task-1", content: "first", status: "in_progress" }, - { id: "task-2", content: "second", status: "pending" }, - ]); - expect(list.getTasks()).toEqual(result); - }); - - it("a second setTasks fully replaces the previous list (no append)", () => { - const list = new TaskList(); - list.setTasks([ - { content: "a", status: "completed" }, - { content: "b", status: "completed" }, - { content: "c", status: "pending" }, - ]); - const next = list.setTasks([{ content: "only", status: "in_progress" }]); - expect(next).toEqual([{ id: "task-1", content: "only", status: "in_progress" }]); - expect(list.getTasks()).toHaveLength(1); - }); - - it("preserves all four statuses", () => { - const list = new TaskList(); - const result = list.setTasks([ - { content: "p", status: "pending" }, - { content: "i", status: "in_progress" }, - { content: "c", status: "completed" }, - { content: "x", status: "cancelled" }, - ]); - expect(result.map((t) => t.status)).toEqual([ - "pending", - "in_progress", - "completed", - "cancelled", - ]); - }); - - it("defaults missing/invalid status to pending", () => { - const list = new TaskList(); - const result = list.setTasks([ - { content: "no status" }, - { content: "bogus", status: "done" }, - { content: "junk", status: 42 }, - ]); - expect(result.map((t) => t.status)).toEqual(["pending", "pending", "pending"]); - }); - - it("an empty array clears the list", () => { - const list = new TaskList(); - list.setTasks([{ content: "x", status: "pending" }]); - expect(list.setTasks([])).toEqual([]); - expect(list.getTasks()).toEqual([]); - }); - - it("getTasks returns copies (no external mutation leaks in)", () => { - const list = new TaskList(); - list.setTasks([{ content: "x", status: "pending" }]); - const snapshot = list.getTasks(); - snapshot[0].content = "mutated"; - expect(list.getTasks()[0].content).toBe("x"); - }); - - it("onChange fires on every setTasks with the new snapshot", () => { - const list = new TaskList(); - const seen: TaskItem[][] = []; - const unsubscribe = list.onChange((tasks) => seen.push(tasks)); - list.setTasks([{ content: "a", status: "pending" }]); - list.setTasks([{ content: "b", status: "completed" }]); - expect(seen).toHaveLength(2); - expect(seen[0]).toEqual([{ id: "task-1", content: "a", status: "pending" }]); - expect(seen[1]).toEqual([{ id: "task-1", content: "b", status: "completed" }]); - unsubscribe(); - list.setTasks([{ content: "c", status: "pending" }]); - expect(seen).toHaveLength(2); - }); -}); - -describe("createTaskListTool", () => { - it("exposes a single declarative `todos` parameter and the name `todo`", () => { - const tool = createTaskListTool(new TaskList()); - expect(tool.name).toBe("todo"); - // One top-level param: the whole-list `todos` array. - const shape = (tool.parameters as { shape: Record<string, unknown> }).shape; - expect(Object.keys(shape)).toEqual(["todos"]); - }); - - it("execute updates the store and echoes the list WITHOUT ids", async () => { - const list = new TaskList(); - const tool = createTaskListTool(list); - const out = await tool.execute({ - todos: [ - { content: "plan", status: "completed" }, - { content: "build", status: "in_progress" }, - ], - }); - expect(JSON.parse(out)).toEqual([ - { content: "plan", status: "completed" }, - { content: "build", status: "in_progress" }, - ]); - // Store has ids; the echo does not. - expect(list.getTasks()).toEqual([ - { id: "task-1", content: "plan", status: "completed" }, - { id: "task-2", content: "build", status: "in_progress" }, - ]); - }); - - it("execute fires onChange so the UI broadcast is wired", async () => { - const list = new TaskList(); - const cb = vi.fn(); - list.onChange(cb); - const tool = createTaskListTool(list); - await tool.execute({ todos: [{ content: "x", status: "pending" }] }); - expect(cb).toHaveBeenCalledTimes(1); - }); - - it("execute with an empty array clears the store", async () => { - const list = new TaskList(); - list.setTasks([{ content: "x", status: "pending" }]); - const tool = createTaskListTool(list); - const out = await tool.execute({ todos: [] }); - expect(JSON.parse(out)).toEqual([]); - expect(list.getTasks()).toEqual([]); - }); - - it("execute defaults invalid status to pending in both store and echo", async () => { - const list = new TaskList(); - const tool = createTaskListTool(list); - const out = await tool.execute({ todos: [{ content: "x", status: "done" }] }); - expect(JSON.parse(out)).toEqual([{ content: "x", status: "pending" }]); - expect(list.getTasks()[0].status).toBe("pending"); - }); - - it("execute rejects a non-array todos param", async () => { - const tool = createTaskListTool(new TaskList()); - const out = await tool.execute({ todos: "nope" }); - expect(out).toMatch(/Error/); - }); - - it("execute rejects items missing a content string", async () => { - const tool = createTaskListTool(new TaskList()); - const out = await tool.execute({ todos: [{ status: "pending" }] }); - expect(out).toMatch(/Error/); - }); -}); diff --git a/packages/core/tests/tools/write-file.test.ts b/packages/core/tests/tools/write-file.test.ts deleted file mode 100644 index 0dedbfc..0000000 --- a/packages/core/tests/tools/write-file.test.ts +++ /dev/null @@ -1,152 +0,0 @@ -import { access, mkdtemp, readdir, readFile, rm, symlink } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { afterEach, beforeEach, describe, expect, it } from "vitest"; -import { createWriteFileTool } from "../../src/tools/write-file.js"; - -describe("write_file tool", () => { - let workDir: string; - - beforeEach(async () => { - workDir = await mkdtemp(join(tmpdir(), "dispatch-test-")); - }); - - afterEach(async () => { - await rm(workDir, { recursive: true, force: true }); - }); - - it("writes a new file", async () => { - const tool = createWriteFileTool(workDir); - const result = await tool.execute({ - path: "output.txt", - content: "test content", - }); - expect(result).toMatch(/successfully wrote/i); - const written = await readFile(join(workDir, "output.txt"), "utf8"); - expect(written).toBe("test content"); - }); - - it("creates parent directories", async () => { - const tool = createWriteFileTool(workDir); - const result = await tool.execute({ - path: "nested/dir/file.txt", - content: "nested", - }); - expect(result).toMatch(/successfully wrote/i); - const written = await readFile(join(workDir, "nested/dir/file.txt"), "utf8"); - expect(written).toBe("nested"); - }); - - it("blocks path traversal", async () => { - const tool = createWriteFileTool(workDir); - const result = await tool.execute({ path: "../evil.txt", content: "bad" }); - expect(result).toMatch(/outside the working directory/i); - }); - - // Regression for `resolve(join(workingDirectory, filePath))` — when filePath - // is absolute, `join` does NOT short-circuit, it concatenates. The old code - // silently rewrote `/etc/foo` to `<workdir>/etc/foo` and "succeeded" by - // writing to the wrong location. After the fix, absolute paths resolve - // to themselves and the workdir gate behaves correctly. - describe("absolute path handling", () => { - it("writes an absolute path that lives under the workdir to the expected location", async () => { - const tool = createWriteFileTool(workDir); - const absoluteTarget = join(workDir, "abs.txt"); - const result = await tool.execute({ path: absoluteTarget, content: "abs content" }); - expect(result).toMatch(/successfully wrote/i); - // File must exist at exactly `absoluteTarget`, NOT at - // `<workdir>/<workdir>/abs.txt` (the old mangled location). - const written = await readFile(absoluteTarget, "utf8"); - expect(written).toBe("abs content"); - }); - - it("rejects absolute paths outside the workdir instead of silently mangling them", async () => { - const tool = createWriteFileTool(workDir); - // Pick a path under tmpdir that's definitely not under workDir. - // Under the bug, this got rewritten to `<workdir>/tmp/...` and the - // write "succeeded" at the wrong location. - const evilPath = join(tmpdir(), `dispatch-evil-${Date.now()}.txt`); - const result = await tool.execute({ path: evilPath, content: "should not land" }); - expect(result).toMatch(/outside the working directory/i); - }); - }); - - // Symlink containment: even when the *leaf* doesn't exist yet (the - // common case for write_file creating a new file), `canonicalize` - // must walk up to the nearest existing ancestor and resolve symlinks - // there. Otherwise, a directory symlink inside workdir pointing - // outside lets a write escape the workspace. - describe("symlink handling", () => { - let externalDir: string; - - beforeEach(async () => { - externalDir = await mkdtemp(join(tmpdir(), "dispatch-external-")); - }); - - afterEach(async () => { - await rm(externalDir, { recursive: true, force: true }); - }); - - it("blocks writes that escape through a parent symlink (leaf does not exist yet)", async () => { - const tool = createWriteFileTool(workDir); - // `escape` is a symlink *inside* workdir to a directory *outside*. - await symlink(externalDir, join(workDir, "escape")); - const result = await tool.execute({ - path: "escape/payload.txt", - content: "malicious payload", - }); - expect(result).toMatch(/outside the working directory/i); - // And the file must NOT exist in externalDir. - await expect(access(join(externalDir, "payload.txt"))).rejects.toThrow(); - // And externalDir should be empty (nothing leaked through). - const entries = await readdir(externalDir); - expect(entries).toEqual([]); - }); - }); - - describe("onAfterWrite hook", () => { - it("appends the hook's returned string to a successful write", async () => { - const tool = createWriteFileTool(workDir, async (abs) => `DIAGNOSTICS for ${abs}`); - const result = await tool.execute({ path: "a.luau", content: "local x = 1" }); - expect(result).toMatch(/successfully wrote/i); - expect(result).toContain("DIAGNOSTICS for"); - expect(result).toContain(join(workDir, "a.luau")); - }); - - it("does not append when the hook returns empty string", async () => { - const tool = createWriteFileTool(workDir, async () => ""); - const result = await tool.execute({ path: "a.luau", content: "local x = 1" }); - expect(result.trim()).toMatch(/^Successfully wrote to "a\.luau"\.$/); - }); - - it("does not run the hook when the write is blocked (traversal)", async () => { - let called = false; - const tool = createWriteFileTool(workDir, async () => { - called = true; - return "should not appear"; - }); - const result = await tool.execute({ path: "../evil.txt", content: "bad" }); - expect(result).toMatch(/outside the working directory/i); - expect(called).toBe(false); - }); - - it("swallows hook errors so a throwing hook never fails the write", async () => { - const tool = createWriteFileTool(workDir, async () => { - throw new Error("lsp blew up"); - }); - const result = await tool.execute({ path: "a.luau", content: "local x = 1" }); - expect(result).toMatch(/successfully wrote/i); - expect(result).not.toContain("lsp blew up"); - }); - - it("passes the canonical absolute path to the hook", async () => { - let seen = ""; - const tool = createWriteFileTool(workDir, async (abs) => { - seen = abs; - return ""; - }); - await tool.execute({ path: "nested/b.luau", content: "x" }); - expect(seen).toBe(join(workDir, "nested/b.luau")); - }); - }); -}); diff --git a/packages/core/tests/types/reasoning-effort.test.ts b/packages/core/tests/types/reasoning-effort.test.ts deleted file mode 100644 index 97cd26c..0000000 --- a/packages/core/tests/types/reasoning-effort.test.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { - DEFAULT_REASONING_EFFORT, - isReasoningEffort, - REASONING_EFFORT_LABELS, - REASONING_EFFORTS, -} from "../../src/types/index.js"; - -describe("REASONING_EFFORTS — canonical effort list (single source of truth)", () => { - it("is ordered least→most and includes xhigh between high and max", () => { - expect(REASONING_EFFORTS).toEqual(["none", "low", "medium", "high", "xhigh", "max"]); - const hi = REASONING_EFFORTS.indexOf("high"); - const xhi = REASONING_EFFORTS.indexOf("xhigh"); - const mx = REASONING_EFFORTS.indexOf("max"); - expect(hi).toBeLessThan(xhi); - expect(xhi).toBeLessThan(mx); - }); - - it("has a human-readable label for every level (no gaps)", () => { - for (const effort of REASONING_EFFORTS) { - expect(REASONING_EFFORT_LABELS[effort]).toBeTruthy(); - } - expect(Object.keys(REASONING_EFFORT_LABELS).sort()).toEqual([...REASONING_EFFORTS].sort()); - }); - - it("defaults to high", () => { - expect(DEFAULT_REASONING_EFFORT).toBe("high"); - expect(REASONING_EFFORTS).toContain(DEFAULT_REASONING_EFFORT); - }); -}); - -describe("isReasoningEffort", () => { - it("accepts every canonical level", () => { - for (const effort of REASONING_EFFORTS) { - expect(isReasoningEffort(effort)).toBe(true); - } - }); - - it("rejects unknown strings and non-strings", () => { - expect(isReasoningEffort("turbo")).toBe(false); - expect(isReasoningEffort("HIGH")).toBe(false); - expect(isReasoningEffort("")).toBe(false); - expect(isReasoningEffort(undefined)).toBe(false); - expect(isReasoningEffort(null)).toBe(false); - expect(isReasoningEffort(3)).toBe(false); - expect(isReasoningEffort({})).toBe(false); - }); -}); diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json deleted file mode 100644 index 2d6fedd..0000000 --- a/packages/core/tsconfig.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "extends": "../../tsconfig.base.json", - "compilerOptions": { - "outDir": "dist", - "rootDir": "src", - "types": ["@types/bun"] - }, - "include": ["src/**/*.ts"], - "exclude": ["node_modules", "dist", "tests"] -} diff --git a/packages/core/vitest.config.ts b/packages/core/vitest.config.ts deleted file mode 100644 index ba60b3c..0000000 --- a/packages/core/vitest.config.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { defineConfig } from "vitest/config"; - -export default defineConfig({ - test: { - include: ["tests/**/*.test.ts"], - server: { - deps: { - // Force inline resolution for packages that break under Bun's - // .bun/ symlink layout in Docker environments - inline: ["zod"], - }, - }, - }, -}); |
