skilld 1.5.2 → 1.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/THIRD-PARTY-LICENSES.md +38 -0
  2. package/dist/_chunks/agent.mjs +281 -147
  3. package/dist/_chunks/agent.mjs.map +1 -1
  4. package/dist/_chunks/assemble.mjs +2 -1
  5. package/dist/_chunks/assemble.mjs.map +1 -1
  6. package/dist/_chunks/author.mjs +26 -2
  7. package/dist/_chunks/author.mjs.map +1 -1
  8. package/dist/_chunks/cli-helpers.mjs +22 -5
  9. package/dist/_chunks/cli-helpers.mjs.map +1 -1
  10. package/dist/_chunks/cli-helpers2.mjs +2 -1
  11. package/dist/_chunks/index3.d.mts.map +1 -1
  12. package/dist/_chunks/install.mjs +6 -2
  13. package/dist/_chunks/install.mjs.map +1 -1
  14. package/dist/_chunks/libs/@sinclair/typebox.mjs +2748 -0
  15. package/dist/_chunks/libs/@sinclair/typebox.mjs.map +1 -0
  16. package/dist/_chunks/list.mjs +2 -1
  17. package/dist/_chunks/list.mjs.map +1 -1
  18. package/dist/_chunks/prepare2.mjs +2 -1
  19. package/dist/_chunks/prepare2.mjs.map +1 -1
  20. package/dist/_chunks/prompts.mjs +10 -15
  21. package/dist/_chunks/prompts.mjs.map +1 -1
  22. package/dist/_chunks/rolldown-runtime.mjs +13 -0
  23. package/dist/_chunks/sanitize.mjs +3 -0
  24. package/dist/_chunks/sanitize.mjs.map +1 -1
  25. package/dist/_chunks/search-interactive.mjs +2 -1
  26. package/dist/_chunks/search-interactive.mjs.map +1 -1
  27. package/dist/_chunks/search.mjs +2 -1
  28. package/dist/_chunks/setup.mjs +2 -1
  29. package/dist/_chunks/setup.mjs.map +1 -1
  30. package/dist/_chunks/sources.mjs +20 -7
  31. package/dist/_chunks/sources.mjs.map +1 -1
  32. package/dist/_chunks/sync-shared.mjs +2 -1
  33. package/dist/_chunks/sync-shared2.mjs +8 -4
  34. package/dist/_chunks/sync-shared2.mjs.map +1 -1
  35. package/dist/_chunks/sync.mjs +1 -1
  36. package/dist/_chunks/sync2.mjs +2 -1
  37. package/dist/_chunks/uninstall.mjs +2 -1
  38. package/dist/_chunks/uninstall.mjs.map +1 -1
  39. package/dist/_chunks/wizard.mjs +1 -1
  40. package/dist/agent/index.d.mts.map +1 -1
  41. package/dist/agent/index.mjs +2 -1
  42. package/dist/cli.mjs +2 -1
  43. package/dist/cli.mjs.map +1 -1
  44. package/package.json +7 -1
@@ -1 +1 @@
1
- {"version":3,"file":"agent.mjs","names":["cli","agentId","models","buildArgs","parseLine","cli","agentId","models","buildArgs","parseLine","claude","gemini","codex","claude.buildArgs","gemini.buildArgs","codex.buildArgs","claude.parseLine","gemini.parseLine","codex.parseLine","agents","delay"],"sources":["../../src/agent/clis/claude.ts","../../src/agent/clis/codex.ts","../../src/agent/clis/gemini.ts","../../src/agent/clis/pi-ai.ts","../../src/agent/clis/index.ts","../../src/agent/detect-presets.ts","../../src/agent/detect-imports.ts"],"sourcesContent":["/**\n * Claude Code CLI — token-level streaming via --include-partial-messages\n *\n * Write permission: Claude Code has hardcoded .claude/ write protection and\n * --allowedTools glob patterns are broken (github.com/anthropics/claude-code/issues/6881).\n * Instead of fighting the permission system, we let Write be auto-denied in pipe mode\n * and capture the content via writeContent fallback in parseLine().\n */\n\nimport type { CliModelEntry, ParsedEvent } from './types.ts'\n\nexport const cli = 'claude' as const\nexport const agentId = 'claude-code' as const\n\nexport const models: Record<string, CliModelEntry> = {\n opus: { model: 'opus', name: 'Opus 4.6', hint: 'Most capable for complex work' },\n sonnet: { model: 'sonnet', name: 'Sonnet 4.6', hint: 'Best for everyday tasks' },\n haiku: { model: 'haiku', name: 'Haiku 4.5', hint: 'Fastest for quick answers', recommended: true },\n}\n\nexport function buildArgs(model: string, skillDir: string, symlinkDirs: string[]): string[] {\n const allowedTools = [\n // Bare tool names — --add-dir already scopes visibility\n 'Read',\n 'Glob',\n 'Grep',\n 'Bash(*skilld search*)',\n 'Bash(*skilld validate*)',\n // Write intentionally omitted — auto-denied in pipe mode, content\n // captured via writeContent fallback (see parseLine + index.ts:373)\n ].join(' ')\n return [\n '-p',\n '--model',\n model,\n '--output-format',\n 'stream-json',\n '--verbose',\n '--include-partial-messages',\n '--allowedTools',\n allowedTools,\n '--disallowedTools',\n 'WebSearch WebFetch Task',\n '--add-dir',\n skillDir,\n ...symlinkDirs.flatMap(d => ['--add-dir', d]),\n '--no-session-persistence',\n ]\n}\n\n/**\n * Parse claude stream-json events\n *\n * Event types:\n * - stream_event/content_block_delta/text_delta → token streaming\n * - stream_event/content_block_start/tool_use → tool invocation starting\n * - assistant message with tool_use content → tool name + input\n * - assistant message with text content → full text (non-streaming fallback)\n * - result → usage, cost, turns\n */\nexport function parseLine(line: string): ParsedEvent {\n try {\n const obj = JSON.parse(line)\n\n // Token-level streaming (--include-partial-messages)\n if (obj.type === 'stream_event') {\n const evt = obj.event\n if (!evt)\n return {}\n\n // Text delta — the main streaming path\n if (evt.type === 'content_block_delta' && evt.delta?.type === 'text_delta') {\n return { textDelta: evt.delta.text }\n }\n\n return {}\n }\n\n // Full assistant message (complete turn, after streaming)\n if (obj.type === 'assistant' && obj.message?.content) {\n const content = obj.message.content as any[]\n\n // Extract tool uses with inputs for progress hints\n const tools = content.filter((c: any) => c.type === 'tool_use')\n if (tools.length) {\n const names = tools.map((t: any) => t.name)\n // Extract useful hint from tool input (file path, query, etc)\n const hint = tools.map((t: any) => {\n const input = t.input || {}\n return input.file_path || input.path || input.pattern || input.query || input.command || ''\n }).filter(Boolean).join(', ')\n // Capture Write content — primary output path since Write is auto-denied\n const writeTool = tools.find((t: any) => t.name === 'Write' && t.input?.content)\n return { toolName: names.join(', '), toolHint: hint || undefined, writeContent: writeTool?.input?.content }\n }\n\n // Text content (fallback for non-partial mode)\n const text = content\n .filter((c: any) => c.type === 'text')\n .map((c: any) => c.text)\n .join('')\n if (text)\n return { fullText: text }\n }\n\n // Final result\n if (obj.type === 'result') {\n const u = obj.usage\n return {\n done: true,\n usage: u ? { input: u.input_tokens ?? u.inputTokens ?? 0, output: u.output_tokens ?? u.outputTokens ?? 0 } : undefined,\n cost: obj.total_cost_usd,\n turns: obj.num_turns,\n }\n }\n }\n catch {}\n return {}\n}\n","/**\n * OpenAI Codex CLI — exec subcommand with JSON output\n * Prompt passed via stdin with `-` sentinel\n *\n * Event types:\n * - turn.started / turn.completed → turn lifecycle + usage\n * - item.started → command_execution in progress\n * - item.completed → agent_message (text), command_execution (result), file_change (apply_patch)\n * - error / turn.failed → errors\n */\n\nimport type { CliModelEntry, ParsedEvent } from './types.ts'\n\nexport const cli = 'codex' as const\nexport const agentId = 'codex' as const\n\nexport const models: Record<string, CliModelEntry> = {\n 'gpt-5.3-codex': { model: 'gpt-5.3-codex', name: 'GPT-5.3 Codex', hint: 'Latest frontier Codex model' },\n 'gpt-5.3-codex-spark': { model: 'gpt-5.3-codex-spark', name: 'GPT-5.3 Codex Spark', hint: 'Faster GPT-5.3 Codex variant', recommended: true },\n 'gpt-5.2-codex': { model: 'gpt-5.2-codex', name: 'GPT-5.2 Codex', hint: 'Frontier agentic coding model' },\n}\n\nexport function buildArgs(model: string, _skillDir: string, _symlinkDirs: string[]): string[] {\n return [\n 'exec',\n '--json',\n '--ephemeral',\n '--model',\n model,\n // Permissions aligned with Claude's scoped model:\n // --full-auto = --sandbox workspace-write + --ask-for-approval on-request\n // → writes scoped to CWD (.skilld/, set in spawn), reads unrestricted, network blocked\n // Shell remains enabled for `skilld` / `npx -y skilld` search/validate (no per-command allowlist in Codex)\n // --ephemeral → no session persistence (equivalent to Claude's --no-session-persistence)\n '--full-auto',\n '-',\n ]\n}\n\nexport function parseLine(line: string): ParsedEvent {\n try {\n const obj = JSON.parse(line)\n\n if (obj.type === 'item.completed' && obj.item) {\n const item = obj.item\n // Agent message — the main text output\n if (item.type === 'agent_message' && item.text)\n return { fullText: item.text }\n // Command execution completed — log as tool progress\n // If the command writes to a file (redirect or cat >), capture output as writeContent fallback\n if (item.type === 'command_execution' && item.aggregated_output) {\n const cmd = item.command || ''\n const writeContent = (/^cat\\s*>|>/.test(cmd)) ? item.aggregated_output : undefined\n return { toolName: 'Bash', toolHint: `(${item.aggregated_output.length} chars output)`, writeContent }\n }\n // apply_patch completed — file written directly to disk\n if (item.type === 'file_change' && item.changes?.length) {\n const paths = item.changes.map((c: { path: string, kind: string }) => c.path).join(', ')\n return { toolName: 'Write', toolHint: paths }\n }\n }\n\n // Command starting — show progress\n if (obj.type === 'item.started' && obj.item?.type === 'command_execution') {\n return { toolName: 'Bash', toolHint: obj.item.command }\n }\n\n // Turn completed — usage stats\n if (obj.type === 'turn.completed' && obj.usage) {\n return {\n done: true,\n usage: {\n input: obj.usage.input_tokens ?? 0,\n output: obj.usage.output_tokens ?? 0,\n },\n }\n }\n\n // Error events\n if (obj.type === 'turn.failed' || obj.type === 'error') {\n return { done: true }\n }\n }\n catch {}\n return {}\n}\n","/**\n * Gemini CLI — turn-level streaming via -o stream-json\n * Write scoping: relies on cwd being set to .skilld/ (no native --writeable-dirs)\n */\n\nimport type { CliModelEntry, ParsedEvent } from './types.ts'\nimport { resolveSkilldCommand } from '../../core/shared.ts'\n\nexport const cli = 'gemini' as const\nexport const agentId = 'gemini-cli' as const\n\nexport const models: Record<string, CliModelEntry> = {\n 'gemini-3.1-pro': { model: 'gemini-3.1-pro', name: 'Gemini 3.1 Pro', hint: 'Most capable' },\n 'gemini-3-flash': { model: 'gemini-3-flash', name: 'Gemini 3 Flash', hint: 'Balanced', recommended: true },\n}\n\nexport function buildArgs(model: string, skillDir: string, symlinkDirs: string[]): string[] {\n return [\n '-o',\n 'stream-json',\n '-m',\n model,\n '--allowed-tools',\n `read_file,write_file,glob_tool,list_directory,search_file_content,run_shell_command(${resolveSkilldCommand()}),run_shell_command(grep),run_shell_command(head)`,\n '--include-directories',\n skillDir,\n ...symlinkDirs.flatMap(d => ['--include-directories', d]),\n ]\n}\n\n/** Parse gemini stream-json events — turn level (full message per event) */\nexport function parseLine(line: string): ParsedEvent {\n try {\n const obj = JSON.parse(line)\n\n // Text message (delta or full)\n if (obj.type === 'message' && obj.role === 'assistant' && obj.content) {\n return obj.delta ? { textDelta: obj.content } : { fullText: obj.content }\n }\n\n // Tool invocation\n if (obj.type === 'tool_use' || obj.type === 'tool_call') {\n const name = obj.tool_name || obj.name || obj.tool || 'tool'\n const params = obj.parameters || obj.args || obj.input || {}\n const hint = params.file_path || params.path || params.dir_path || params.pattern || params.query || params.command || ''\n // Capture write_file content as fallback (matches Claude's Write tool behavior)\n if (name === 'write_file' && params.content) {\n return { toolName: name, toolHint: hint || undefined, writeContent: params.content }\n }\n return { toolName: name, toolHint: hint || undefined }\n }\n\n // Final result\n if (obj.type === 'result') {\n const s = obj.stats\n return {\n done: true,\n usage: s ? { input: s.input_tokens ?? s.input ?? 0, output: s.output_tokens ?? s.output ?? 0 } : undefined,\n turns: s?.tool_calls,\n }\n }\n }\n catch {}\n return {}\n}\n","/**\n * pi-ai adapter — direct LLM API calls via @mariozechner/pi-ai\n *\n * Optional alternative to CLI spawning. Supports:\n * - Env-var API keys (ANTHROPIC_API_KEY, GEMINI_API_KEY, OPENAI_API_KEY, etc.)\n * - OAuth login to piggyback on existing subscriptions (Claude Pro, ChatGPT Plus, Copilot)\n *\n * Models are enumerated dynamically from pi-ai's registry — no hardcoded list.\n * Reference content is inlined into the prompt via portabilizePrompt().\n */\n\nimport type { SkillSection } from '../prompts/index.ts'\nimport type { StreamProgress } from './types.ts'\nimport { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'\nimport { homedir } from 'node:os'\nimport { getEnvApiKey, getModel, getModels, getProviders, streamSimple } from '@mariozechner/pi-ai'\nimport { getOAuthApiKey, getOAuthProvider, getOAuthProviders } from '@mariozechner/pi-ai/oauth'\nimport { join } from 'pathe'\nimport { sanitizeMarkdown } from '../../core/sanitize.ts'\nimport { portabilizePrompt } from '../prompts/index.ts'\n\nexport function isPiAiModel(model: string): boolean {\n return model.startsWith('pi:')\n}\n\n/** Parse a pi:provider/model-id string → { provider, modelId } */\nexport function parsePiAiModelId(model: string): { provider: string, modelId: string } | null {\n if (!model.startsWith('pi:'))\n return null\n const rest = model.slice(3)\n const slashIdx = rest.indexOf('/')\n if (slashIdx === -1)\n return null\n return { provider: rest.slice(0, slashIdx), modelId: rest.slice(slashIdx + 1) }\n}\n\n// ── OAuth credentials ────────────────────────────────────────────────\n\n/** pi coding agent stores auth here; env var can override */\nconst PI_AGENT_AUTH_PATH = join(\n process.env.PI_CODING_AGENT_DIR || join(homedir(), '.pi', 'agent'),\n 'auth.json',\n)\n/** skilld's own auth file — used when user logs in via skilld */\nconst SKILLD_AUTH_PATH = join(homedir(), '.skilld', 'pi-ai-auth.json')\n\ninterface OAuthCredentials {\n type: 'oauth'\n refresh: string\n access: string\n expires: number\n [key: string]: unknown\n}\n\nfunction readAuthFile(path: string): Record<string, OAuthCredentials> {\n if (!existsSync(path))\n return {}\n try {\n return JSON.parse(readFileSync(path, 'utf-8'))\n }\n catch { return {} }\n}\n\n/** Load auth from pi coding agent first (~/.pi/agent/auth.json), then skilld's own */\nfunction loadAuth(): Record<string, OAuthCredentials> {\n const piAuth = readAuthFile(PI_AGENT_AUTH_PATH)\n const skilldAuth = readAuthFile(SKILLD_AUTH_PATH)\n // pi agent credentials take precedence (user's primary auth)\n return { ...skilldAuth, ...piAuth }\n}\n\n/** Save auth to skilld's own file — never writes to pi agent's auth */\nfunction saveAuth(auth: Record<string, OAuthCredentials>): void {\n mkdirSync(join(homedir(), '.skilld'), { recursive: true, mode: 0o700 })\n writeFileSync(SKILLD_AUTH_PATH, JSON.stringify(auth, null, 2), { mode: 0o600 })\n}\n\n/**\n * Overrides for model-provider → OAuth-provider mapping.\n * Most providers share the same ID in both systems (auto-matched).\n * Only list exceptions where the IDs diverge.\n */\nconst OAUTH_PROVIDER_OVERRIDES: Record<string, string> = {\n google: 'google-gemini-cli',\n openai: 'openai-codex',\n}\n\n/** Resolve model provider ID → OAuth provider ID */\nfunction resolveOAuthProviderId(modelProvider: string): string | null {\n if (OAUTH_PROVIDER_OVERRIDES[modelProvider])\n return OAUTH_PROVIDER_OVERRIDES[modelProvider]\n // Auto-match: if the model provider ID is also an OAuth provider, use it directly\n const oauthIds = new Set(getOAuthProviders().map((p: any) => p.id))\n if (oauthIds.has(modelProvider))\n return modelProvider\n return null\n}\n\n// ── Auth resolution ──────────────────────────────────────────────────\n\n/** Resolve API key for a provider — checks env vars first, then OAuth credentials */\nasync function resolveApiKey(provider: string): Promise<string | null> {\n // 1. Check env vars via pi-ai's own resolver\n const envKey = getEnvApiKey(provider)\n if (envKey)\n return envKey\n\n // 2. Check stored OAuth credentials\n const oauthProviderId = resolveOAuthProviderId(provider)\n if (!oauthProviderId)\n return null\n\n const auth = loadAuth()\n if (!auth[oauthProviderId])\n return null\n\n const result = await getOAuthApiKey(oauthProviderId, auth)\n if (!result)\n return null\n\n // Save refreshed credentials to skilld's own file only (never leak pi-agent tokens)\n const skilldAuth = readAuthFile(SKILLD_AUTH_PATH)\n skilldAuth[oauthProviderId] = { type: 'oauth', ...result.newCredentials }\n saveAuth(skilldAuth)\n return result.apiKey\n}\n\n// ── OAuth login flow ─────────────────────────────────────────────────\n\nexport interface LoginCallbacks {\n /** Called with the URL the user needs to open in their browser */\n onAuth: (url: string, instructions?: string) => void\n /** Called when pi-ai needs text input from the user */\n onPrompt: (message: string, placeholder?: string) => Promise<string>\n /** Status updates during the login flow */\n onProgress?: (message: string) => void\n}\n\n/** Get available OAuth providers for login */\nexport function getOAuthProviderList(): Array<{ id: string, name: string, loggedIn: boolean }> {\n const auth = loadAuth()\n const providers = getOAuthProviders() as Array<{ id: string, name: string }>\n return providers.map((p: any) => ({\n id: p.id,\n name: p.name ?? p.id,\n loggedIn: !!auth[p.id],\n }))\n}\n\n/** Run OAuth login for a provider, saving credentials to ~/.skilld/ */\nexport async function loginOAuthProvider(providerId: string, callbacks: LoginCallbacks): Promise<boolean> {\n const provider = getOAuthProvider(providerId)\n if (!provider)\n return false\n\n const credentials = await provider.login({\n onAuth: (info: any) => callbacks.onAuth(info.url, info.instructions),\n onPrompt: async (prompt: any) => callbacks.onPrompt(prompt.message, prompt.placeholder),\n onProgress: (msg: string) => callbacks.onProgress?.(msg),\n })\n\n const auth = loadAuth()\n auth[providerId] = { type: 'oauth', ...credentials }\n saveAuth(auth)\n return true\n}\n\n/** Remove OAuth credentials for a provider */\nexport function logoutOAuthProvider(providerId: string): void {\n const auth = loadAuth()\n delete auth[providerId]\n saveAuth(auth)\n}\n\n// ── Dynamic model enumeration ────────────────────────────────────────\n\nconst MIN_CONTEXT_WINDOW = 32_000\n\n/** Legacy model patterns — old generations that clutter the model list */\nconst LEGACY_MODEL_PATTERNS = [\n // Anthropic: claude 3.x family\n /^claude-3-/,\n /^claude-3\\.5-/,\n /^claude-3\\.7-/,\n // OpenAI: pre-gpt-5\n /^gpt-4(?!\\.\\d)/, // gpt-4, gpt-4-turbo, gpt-4o but not gpt-4.1\n /^o1/,\n /^o3-mini/,\n // Google: old gemini generations + non-text models\n /^gemini-1\\./,\n /^gemini-2\\.0/,\n /^gemini-live-/,\n // Preview snapshots with date suffixes (e.g. -preview-04-17, -preview-05-06)\n /-preview-\\d{2}-\\d{2,4}$/,\n // Dated model snapshots (e.g. -20240307, -20241022)\n /-\\d{8}$/,\n]\n\nfunction isLegacyModel(modelId: string): boolean {\n return LEGACY_MODEL_PATTERNS.some(p => p.test(modelId))\n}\n\n/** Preferred model per provider for auto-selection (cheapest reliable option) */\nconst RECOMMENDED_MODELS: Record<string, RegExp> = {\n anthropic: /haiku/,\n google: /flash/,\n openai: /gpt-4\\.1-mini/,\n}\n\nexport interface PiAiModelInfo {\n /** Full model ID: pi:provider/model-id */\n id: string\n /** Human-readable name */\n name: string\n /** Provider + context info */\n hint: string\n /** Auth source: 'env', 'oauth', or 'none' */\n authSource: 'env' | 'oauth' | 'none'\n /** Whether this is the recommended model for its provider */\n recommended: boolean\n}\n\n/** Get all pi-ai models for providers with auth configured */\nexport function getAvailablePiAiModels(): PiAiModelInfo[] {\n const providers: string[] = getProviders()\n const auth = loadAuth()\n const available: PiAiModelInfo[] = []\n const recommendedPicked = new Set<string>()\n\n for (const provider of providers) {\n let authSource: 'env' | 'oauth' | 'none' = 'none'\n if (getEnvApiKey(provider)) {\n authSource = 'env'\n }\n else {\n const oauthId = resolveOAuthProviderId(provider)\n if (oauthId && auth[oauthId])\n authSource = 'oauth'\n }\n\n if (authSource === 'none')\n continue\n\n const models: any[] = getModels(provider as any)\n // First pass: find the recommended model for this provider\n const recPattern = RECOMMENDED_MODELS[provider]\n let recModelId: string | null = null\n if (recPattern) {\n for (const model of models) {\n if (!isLegacyModel(model.id) && recPattern.test(model.id)) {\n recModelId = model.id\n break\n }\n }\n }\n\n for (const model of models) {\n if (model.contextWindow && model.contextWindow < MIN_CONTEXT_WINDOW)\n continue\n if (isLegacyModel(model.id))\n continue\n\n const id = `pi:${provider}/${model.id}`\n const ctx = model.contextWindow ? ` · ${Math.round(model.contextWindow / 1000)}k ctx` : ''\n const cost = model.cost?.input ? ` · $${model.cost.input}/Mtok` : ''\n const isRecommended = model.id === recModelId && !recommendedPicked.has(provider)\n\n if (isRecommended)\n recommendedPicked.add(provider)\n\n available.push({\n id,\n name: model.name || model.id,\n hint: `${authSource === 'oauth' ? 'OAuth' : 'API key'}${ctx}${cost}`,\n authSource,\n recommended: isRecommended,\n })\n }\n }\n\n return available\n}\n\n// ── Reference inlining ───────────────────────────────────────────────\n\nconst REFERENCE_SUBDIRS = ['docs', 'issues', 'discussions', 'releases']\nconst MAX_REFERENCE_CHARS = 150_000 // ~37k tokens\n\n/** Read reference files from .skilld/ and format as inline context */\nfunction collectReferenceContent(skillDir: string): string {\n const skilldDir = join(skillDir, '.skilld')\n if (!existsSync(skilldDir))\n return ''\n\n const files: Array<{ path: string, content: string }> = []\n let totalChars = 0\n\n for (const subdir of REFERENCE_SUBDIRS) {\n const dirPath = join(skilldDir, subdir)\n if (!existsSync(dirPath))\n continue\n\n const indexPath = join(dirPath, '_INDEX.md')\n if (existsSync(indexPath) && totalChars < MAX_REFERENCE_CHARS) {\n const content = sanitizeMarkdown(readFileSync(indexPath, 'utf-8'))\n if (totalChars + content.length <= MAX_REFERENCE_CHARS) {\n files.push({ path: `references/${subdir}/_INDEX.md`, content })\n totalChars += content.length\n }\n }\n\n const entries = readdirSync(dirPath, { recursive: true })\n for (const entry of entries) {\n if (totalChars >= MAX_REFERENCE_CHARS)\n break\n const entryStr = String(entry)\n if (!entryStr.endsWith('.md') || entryStr === '_INDEX.md')\n continue\n const fullPath = join(dirPath, entryStr)\n if (!existsSync(fullPath))\n continue\n try {\n const content = sanitizeMarkdown(readFileSync(fullPath, 'utf-8'))\n if (totalChars + content.length > MAX_REFERENCE_CHARS)\n continue\n files.push({ path: `references/${subdir}/${entryStr}`, content })\n totalChars += content.length\n }\n catch {}\n }\n }\n\n // Read pkg/README.md if available and within budget\n const readmePath = join(skilldDir, 'pkg', 'README.md')\n if (existsSync(readmePath) && totalChars < MAX_REFERENCE_CHARS) {\n const content = sanitizeMarkdown(readFileSync(readmePath, 'utf-8'))\n if (totalChars + content.length <= MAX_REFERENCE_CHARS) {\n files.push({ path: 'references/pkg/README.md', content })\n totalChars += content.length\n }\n }\n\n // Read type definitions if present\n const pkgDir = join(skilldDir, 'pkg')\n if (existsSync(pkgDir) && totalChars < MAX_REFERENCE_CHARS) {\n try {\n const pkgEntries = readdirSync(pkgDir, { recursive: true })\n for (const entry of pkgEntries) {\n const entryStr = String(entry)\n if (!entryStr.endsWith('.d.ts'))\n continue\n const fullPath = join(pkgDir, entryStr)\n if (!existsSync(fullPath))\n continue\n const content = sanitizeMarkdown(readFileSync(fullPath, 'utf-8'))\n if (totalChars + content.length <= MAX_REFERENCE_CHARS) {\n files.push({ path: `references/pkg/${entryStr}`, content })\n totalChars += content.length\n }\n break // only first .d.ts\n }\n }\n catch {}\n }\n\n if (files.length === 0)\n return ''\n\n const blocks = files.map(f => `<file path=\"${f.path}\">\\n${f.content.replaceAll('</file>', '&lt;/file&gt;').replaceAll('</reference-files>', '&lt;/reference-files&gt;')}\\n</file>`)\n return `<reference-files>\\n${blocks.join('\\n')}\\n</reference-files>`\n}\n\n// ── Section optimization ─────────────────────────────────────────────\n\nexport interface PiAiSectionOptions {\n section: SkillSection\n prompt: string\n skillDir: string\n model: string\n onProgress?: (progress: StreamProgress) => void\n signal?: AbortSignal\n}\n\nexport interface PiAiSectionResult {\n text: string\n usage?: { input: number, output: number }\n cost?: number\n}\n\n/** Optimize a single section using pi-ai direct API */\nexport async function optimizeSectionPiAi(opts: PiAiSectionOptions): Promise<PiAiSectionResult> {\n const parsed = parsePiAiModelId(opts.model)\n if (!parsed)\n throw new Error(`Invalid pi-ai model ID: ${opts.model}. Expected format: pi:provider/model-id`)\n\n const model = getModel(parsed.provider as any, parsed.modelId as any)\n const apiKey = await resolveApiKey(parsed.provider)\n\n // Build non-agentic prompt with inlined references\n const portablePrompt = portabilizePrompt(opts.prompt, opts.section)\n const references = collectReferenceContent(opts.skillDir)\n const fullPrompt = references\n ? `${portablePrompt}\\n\\n## Reference Content\\n\\nThe following files are provided inline for your reference:\\n\\n${references}`\n : portablePrompt\n\n opts.onProgress?.({ chunk: '[starting...]', type: 'reasoning', text: '', reasoning: '', section: opts.section })\n\n const stream = streamSimple(model, {\n systemPrompt: 'You are a technical documentation expert generating SKILL.md sections for AI agent skills. Output clean, structured markdown following the format instructions exactly.',\n messages: [{\n role: 'user' as const,\n content: [{ type: 'text' as const, text: fullPrompt }],\n timestamp: Date.now(),\n }],\n }, {\n reasoning: 'medium',\n maxTokens: 16_384,\n ...(apiKey ? { apiKey } : {}),\n })\n\n let text = ''\n let usage: { input: number, output: number } | undefined\n let cost: number | undefined\n\n for await (const event of stream) {\n if (opts.signal?.aborted)\n throw new Error('pi-ai request timed out')\n switch (event.type) {\n case 'text_delta':\n text += event.delta\n opts.onProgress?.({ chunk: event.delta, type: 'text', text, reasoning: '', section: opts.section })\n break\n case 'done':\n if (event.message?.usage) {\n usage = { input: event.message.usage.input, output: event.message.usage.output }\n cost = event.message.usage.cost?.total\n }\n break\n case 'error':\n throw new Error(event.error?.errorMessage ?? 'pi-ai stream error')\n }\n }\n\n return { text, usage, cost }\n}\n","/**\n * CLI orchestrator — spawns per-CLI processes for skill generation\n * Each CLI (claude, gemini, codex) has its own buildArgs + parseLine in separate files\n */\n\nimport type { SkillSection } from '../prompts/index.ts'\nimport type { AgentType } from '../types.ts'\nimport type { CliModelConfig, CliName, OptimizeDocsOptions, OptimizeModel, OptimizeResult, ParsedEvent, SectionResult, StreamProgress, ValidationWarning } from './types.ts'\nimport { exec, spawn } from 'node:child_process'\nimport { createHash } from 'node:crypto'\nimport { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, unlinkSync, writeFileSync } from 'node:fs'\nimport { homedir } from 'node:os'\nimport { setTimeout as delay } from 'node:timers/promises'\nimport { promisify } from 'node:util'\nimport { dirname, join } from 'pathe'\nimport { isWindows } from 'std-env'\nimport { readCachedSection, writeSections } from '../../cache/index.ts'\nimport { sanitizeMarkdown } from '../../core/sanitize.ts'\nimport { detectInstalledAgents } from '../detect.ts'\nimport { buildAllSectionPrompts, getSectionValidator, SECTION_MERGE_ORDER, SECTION_OUTPUT_FILES, wrapSection } from '../prompts/index.ts'\nimport { agents } from '../registry.ts'\nimport * as claude from './claude.ts'\nimport * as codex from './codex.ts'\nimport * as gemini from './gemini.ts'\nimport { getAvailablePiAiModels, isPiAiModel, optimizeSectionPiAi, parsePiAiModelId } from './pi-ai.ts'\n\nexport { buildAllSectionPrompts, buildSectionPrompt, SECTION_MERGE_ORDER, SECTION_OUTPUT_FILES } from '../prompts/index.ts'\nexport type { CustomPrompt, SkillSection } from '../prompts/index.ts'\nexport type { CliModelConfig, CliName, ModelInfo, OptimizeDocsOptions, OptimizeModel, OptimizeResult, StreamProgress } from './types.ts'\n\n// ── Tool progress display ────────────────────────────────────────────\n\nconst TOOL_VERBS: Record<string, string> = {\n // Claude\n Read: 'Reading',\n Glob: 'Searching',\n Grep: 'Searching',\n Write: 'Writing',\n Bash: 'Running',\n // Gemini\n read_file: 'Reading',\n glob_tool: 'Searching',\n write_file: 'Writing',\n list_directory: 'Listing',\n search_file_content: 'Searching',\n run_shell_command: 'Running',\n}\n\ninterface ToolProgressLog {\n message: (msg: string) => void\n}\n\n/** Create a progress callback that emits one line per tool call, Claude Code style */\nexport function createToolProgress(log: ToolProgressLog): (progress: StreamProgress) => void {\n let lastMsg = ''\n let repeatCount = 0\n /** Per-section timestamp of last \"Writing...\" emission — throttles text_delta spam */\n const lastTextEmit = new Map<string, number>()\n const TEXT_THROTTLE_MS = 2000\n\n function emit(msg: string) {\n if (msg === lastMsg) {\n repeatCount++\n log.message(`${msg} \\x1B[90m(+${repeatCount})\\x1B[0m`)\n }\n else {\n lastMsg = msg\n repeatCount = 0\n log.message(msg)\n }\n }\n\n return ({ type, chunk, section }) => {\n if (type === 'text') {\n const key = section ?? ''\n const now = Date.now()\n const last = lastTextEmit.get(key) ?? 0\n if (now - last < TEXT_THROTTLE_MS)\n return\n lastTextEmit.set(key, now)\n emit(`${section ? `\\x1B[90m[${section}]\\x1B[0m ` : ''}Writing...`)\n return\n }\n if (type !== 'reasoning' || !chunk.startsWith('['))\n return\n\n // Parse individual tool names and hints from \"[Read: path]\" or \"[Read, Glob: path1, path2]\"\n const match = chunk.match(/^\\[([^:[\\]]+)(?::\\s(.+))?\\]$/)\n if (!match)\n return\n\n const names = match[1]!.split(',').map(n => n.trim())\n const hints = match[2]?.split(',').map(h => h.trim()) ?? []\n\n for (let i = 0; i < names.length; i++) {\n const rawName = names[i]!\n const hint = hints[i] ?? hints[0] ?? ''\n const verb = TOOL_VERBS[rawName] ?? rawName\n const prefix = section ? `\\x1B[90m[${section}]\\x1B[0m ` : ''\n\n if ((rawName === 'Bash' || rawName === 'run_shell_command') && hint) {\n const searchMatch = hint.match(/skilld search\\s+\"([^\"]+)\"/)\n if (searchMatch) {\n emit(`${prefix}Searching \\x1B[36m\"${searchMatch[1]}\"\\x1B[0m`)\n }\n else if (hint.includes('skilld validate')) {\n emit(`${prefix}Validating...`)\n }\n else {\n const shortened = shortenCommand(hint)\n emit(`${prefix}Running ${shortened.length > 50 ? `${shortened.slice(0, 47)}...` : shortened}`)\n }\n }\n else {\n const path = shortenPath(hint || '...')\n emit(`${prefix}${verb} \\x1B[90m${path}\\x1B[0m`)\n }\n }\n }\n}\n\n// ── Per-CLI dispatch ─────────────────────────────────────────────────\n\nconst CLI_DEFS = [claude, gemini, codex] as const\n\nconst CLI_BUILD_ARGS: Record<CliName, (model: string, skillDir: string, symlinkDirs: string[]) => string[]> = {\n claude: claude.buildArgs,\n gemini: gemini.buildArgs,\n codex: codex.buildArgs,\n}\n\nconst CLI_PARSE_LINE: Record<CliName, (line: string) => ParsedEvent> = {\n claude: claude.parseLine,\n gemini: gemini.parseLine,\n codex: codex.parseLine,\n}\n\n// ── Provider display names ────────────────────────────────────────────\n\n/** Map CLI agent IDs to their LLM provider name (not the agent/tool name) */\nconst CLI_PROVIDER_NAMES: Record<string, string> = {\n 'claude-code': 'Anthropic',\n 'gemini-cli': 'Google',\n 'codex': 'OpenAI',\n}\n\nconst PI_PROVIDER_NAMES: Record<string, string> = {\n 'anthropic': 'Anthropic',\n 'google': 'Google',\n 'google-antigravity': 'Antigravity',\n 'google-gemini-cli': 'Google Gemini',\n 'google-vertex': 'Google Vertex',\n 'openai': 'OpenAI',\n 'openai-codex': 'OpenAI Codex',\n 'github-copilot': 'GitHub Copilot',\n 'groq': 'Groq',\n 'mistral': 'Mistral',\n 'xai': 'xAI',\n}\n\n// ── Assemble CLI_MODELS from per-CLI model definitions ───────────────\n\nexport const CLI_MODELS: Partial<Record<OptimizeModel, CliModelConfig>> = Object.fromEntries(\n CLI_DEFS.flatMap(def =>\n Object.entries(def.models).map(([id, entry]) => [\n id,\n { ...entry, cli: def.cli, agentId: def.agentId },\n ]),\n ),\n)\n\n// ── Model helpers ────────────────────────────────────────────────────\n\nexport function getModelName(id: OptimizeModel): string {\n if (isPiAiModel(id)) {\n const parsed = parsePiAiModelId(id)\n return parsed?.modelId ?? id\n }\n return CLI_MODELS[id]?.name ?? id\n}\n\nexport function getModelLabel(id: OptimizeModel): string {\n if (isPiAiModel(id)) {\n const parsed = parsePiAiModelId(id)\n return parsed ? `${PI_PROVIDER_NAMES[parsed.provider] ?? parsed.provider} · ${parsed.modelId}` : id\n }\n const config = CLI_MODELS[id]\n if (!config)\n return id\n const providerName = CLI_PROVIDER_NAMES[config.agentId] ?? config.cli\n return `${providerName} · ${config.name}`\n}\n\nexport async function getAvailableModels(): Promise<import('./types.ts').ModelInfo[]> {\n const execAsync = promisify(exec)\n const lookupCmd = isWindows ? 'where' : 'which'\n\n const installedAgents = detectInstalledAgents()\n const agentsWithCli = installedAgents.filter(id => agents[id].cli)\n\n const cliChecks = await Promise.all(\n agentsWithCli.map(async (agentId) => {\n const cli = agents[agentId].cli!\n try {\n await execAsync(`${lookupCmd} ${cli}`)\n return agentId\n }\n catch { return null }\n }),\n )\n const availableAgentIds = new Set(cliChecks.filter((id): id is AgentType => id != null))\n\n const cliModels = (Object.entries(CLI_MODELS) as [OptimizeModel, CliModelConfig][])\n .filter(([_, config]) => availableAgentIds.has(config.agentId))\n .map(([id, config]) => {\n const providerName = CLI_PROVIDER_NAMES[config.agentId] ?? agents[config.agentId]?.displayName ?? config.agentId\n return {\n id,\n name: config.name,\n hint: config.hint,\n recommended: config.recommended,\n agentId: config.agentId,\n agentName: providerName,\n provider: config.agentId,\n providerName: `${providerName} (via ${config.cli} CLI)`,\n vendorGroup: providerName,\n }\n })\n\n // Append pi-ai direct API models (providers with auth configured)\n const piAiModels = getAvailablePiAiModels()\n const piAiEntries = piAiModels.map((m) => {\n const parsed = parsePiAiModelId(m.id)\n const piProvider = parsed?.provider ?? 'pi-ai'\n const displayName = PI_PROVIDER_NAMES[piProvider] ?? piProvider\n const authLabel = m.authSource === 'env' ? 'API' : 'OAuth'\n return {\n id: m.id as OptimizeModel,\n name: m.name,\n hint: m.hint,\n recommended: m.recommended,\n agentId: 'pi-ai',\n agentName: `pi-ai (${m.authSource})`,\n provider: `pi:${piProvider}:${m.authSource}`,\n providerName: `${displayName} (${authLabel})`,\n vendorGroup: displayName,\n }\n })\n\n return [...cliModels, ...piAiEntries]\n}\n\n// ── Reference dirs ───────────────────────────────────────────────────\n\n/** Resolve symlinks in .skilld/ to get real paths for --add-dir */\nfunction resolveReferenceDirs(skillDir: string): string[] {\n const refsDir = join(skillDir, '.skilld')\n if (!existsSync(refsDir))\n return []\n const resolved = readdirSync(refsDir)\n .map(entry => join(refsDir, entry))\n .filter(p => lstatSync(p).isSymbolicLink() && existsSync(p))\n .map(p => realpathSync(p))\n\n // Include parent directories so CLIs can search across all references at once\n // (e.g. Gemini's sandbox requires the parent dir to be explicitly included)\n const parents = new Set<string>()\n for (const p of resolved) {\n const parent = dirname(p)\n if (!resolved.includes(parent))\n parents.add(parent)\n }\n\n return [...resolved, ...parents]\n}\n\n// ── Cache ────────────────────────────────────────────────────────────\n\nconst CACHE_DIR = join(homedir(), '.skilld', 'llm-cache')\n\n/** Strip absolute paths from prompt so the hash is project-independent */\nfunction normalizePromptForHash(prompt: string): string {\n return prompt.replace(/\\/[^\\s`]*\\.(?:claude|codex|gemini)\\/skills\\/[^\\s/`]+/g, '<SKILL_DIR>')\n}\n\nfunction hashPrompt(prompt: string, model: OptimizeModel, section: SkillSection): string {\n return createHash('sha256').update(`exec:${model}:${section}:${normalizePromptForHash(prompt)}`).digest('hex').slice(0, 16)\n}\n\nfunction getCached(prompt: string, model: OptimizeModel, section: SkillSection, maxAge = 7 * 24 * 60 * 60 * 1000): string | null {\n const path = join(CACHE_DIR, `${hashPrompt(prompt, model, section)}.json`)\n if (!existsSync(path))\n return null\n try {\n const { text, timestamp } = JSON.parse(readFileSync(path, 'utf-8'))\n return Date.now() - timestamp > maxAge ? null : text\n }\n catch { return null }\n}\n\nfunction setCache(prompt: string, model: OptimizeModel, section: SkillSection, text: string): void {\n mkdirSync(CACHE_DIR, { recursive: true, mode: 0o700 })\n writeFileSync(\n join(CACHE_DIR, `${hashPrompt(prompt, model, section)}.json`),\n JSON.stringify({ text, model, section, timestamp: Date.now() }),\n { mode: 0o600 },\n )\n}\n\n// ── pi-ai direct API path ────────────────────────────────────────────\n\nasync function optimizeSectionViaPiAi(opts: {\n section: SkillSection\n prompt: string\n outputFile: string\n skillDir: string\n model: OptimizeModel\n onProgress?: (progress: StreamProgress) => void\n timeout: number\n debug?: boolean\n}): Promise<SectionResult> {\n const { section, prompt, outputFile, skillDir, model, onProgress, timeout, debug } = opts\n const skilldDir = join(skillDir, '.skilld')\n const outputPath = join(skilldDir, outputFile)\n\n // Write prompt for debugging (same as CLI path)\n writeFileSync(join(skilldDir, `PROMPT_${section}.md`), prompt)\n\n try {\n const ac = new AbortController()\n const timer = setTimeout(() => ac.abort(), timeout)\n const result = await optimizeSectionPiAi({ section, prompt, skillDir, model, onProgress, signal: ac.signal })\n .finally(() => clearTimeout(timer))\n\n const raw = result.text.trim()\n\n // Debug logging\n if (debug) {\n const logsDir = join(skilldDir, 'logs')\n const logName = section.toUpperCase().replace(/-/g, '_')\n mkdirSync(logsDir, { recursive: true })\n if (raw)\n writeFileSync(join(logsDir, `${logName}.md`), raw)\n }\n\n if (!raw) {\n return { section, content: '', wasOptimized: false, error: 'pi-ai returned empty response' }\n }\n\n const content = cleanSectionOutput(raw)\n\n if (content)\n writeFileSync(outputPath, content)\n\n const validator = getSectionValidator(section)\n const rawWarnings = content && validator ? validator(content) : []\n const warnings: ValidationWarning[] = rawWarnings.map(w => ({ section, warning: w.warning }))\n\n return {\n section,\n content,\n wasOptimized: !!content,\n warnings: warnings?.length ? warnings : undefined,\n usage: result.usage,\n cost: result.cost,\n }\n }\n catch (err) {\n return { section, content: '', wasOptimized: false, error: (err as Error).message }\n }\n}\n\n// ── Per-section spawn ────────────────────────────────────────────────\n\ninterface OptimizeSectionOptions {\n section: SkillSection\n prompt: string\n outputFile: string\n skillDir: string\n model: OptimizeModel\n packageName: string\n onProgress?: (progress: StreamProgress) => void\n timeout: number\n debug?: boolean\n preExistingFiles: Set<string>\n}\n\n/** Spawn a single CLI process for one section, or call pi-ai directly */\nfunction optimizeSection(opts: OptimizeSectionOptions): Promise<SectionResult> {\n const { section, prompt, outputFile, skillDir, model, onProgress, timeout, debug, preExistingFiles } = opts\n\n // pi-ai direct API path — no CLI spawning\n if (isPiAiModel(model)) {\n return optimizeSectionViaPiAi({ section, prompt, outputFile, skillDir, model, onProgress, timeout, debug })\n }\n\n const cliConfig = CLI_MODELS[model]\n if (!cliConfig) {\n return Promise.resolve({ section, content: '', wasOptimized: false, error: `No CLI mapping for model: ${model}` })\n }\n\n const { cli, model: cliModel } = cliConfig\n const symlinkDirs = resolveReferenceDirs(skillDir)\n const args = CLI_BUILD_ARGS[cli](cliModel, skillDir, symlinkDirs)\n const parseLine = CLI_PARSE_LINE[cli]\n\n const skilldDir = join(skillDir, '.skilld')\n const outputPath = join(skilldDir, outputFile)\n\n // Remove stale output so we don't read a leftover from a previous run\n if (existsSync(outputPath))\n unlinkSync(outputPath)\n\n // Write prompt for debugging\n writeFileSync(join(skilldDir, `PROMPT_${section}.md`), prompt)\n\n return new Promise<SectionResult>((resolve) => {\n const proc = spawn(cli, args, {\n cwd: skilldDir,\n stdio: ['pipe', 'pipe', 'pipe'],\n timeout,\n env: { ...process.env, NO_COLOR: '1' },\n shell: isWindows,\n })\n\n let buffer = ''\n let accumulatedText = ''\n let lastWriteContent = ''\n let usage: { input: number, output: number } | undefined\n let cost: number | undefined\n const rawLines: string[] = []\n\n onProgress?.({ chunk: '[starting...]', type: 'reasoning', text: '', reasoning: '', section })\n\n proc.stdin.write(prompt)\n proc.stdin.end()\n\n proc.stdout.on('data', (chunk: Buffer) => {\n buffer += chunk.toString()\n const lines = buffer.split('\\n')\n buffer = lines.pop() || ''\n\n for (const line of lines) {\n if (!line.trim())\n continue\n if (debug)\n rawLines.push(line)\n const evt = parseLine(line)\n\n if (evt.textDelta)\n accumulatedText += evt.textDelta\n if (evt.fullText)\n accumulatedText = evt.fullText\n\n if (evt.writeContent)\n lastWriteContent = evt.writeContent\n\n if (evt.toolName) {\n const hint = evt.toolHint\n ? `[${evt.toolName}: ${evt.toolHint}]`\n : `[${evt.toolName}]`\n onProgress?.({ chunk: hint, type: 'reasoning', text: '', reasoning: hint, section })\n }\n\n if (evt.usage)\n usage = evt.usage\n if (evt.cost != null)\n cost = evt.cost\n }\n })\n\n let stderr = ''\n proc.stderr.on('data', (chunk: Buffer) => {\n stderr += chunk.toString()\n })\n\n proc.on('close', (code) => {\n // Drain remaining buffer for metadata\n if (buffer.trim()) {\n const evt = parseLine(buffer)\n if (evt.textDelta)\n accumulatedText += evt.textDelta\n if (evt.fullText)\n accumulatedText = evt.fullText\n if (evt.writeContent)\n lastWriteContent = evt.writeContent\n if (evt.usage)\n usage = evt.usage\n if (evt.cost != null)\n cost = evt.cost\n }\n\n // Remove unexpected files the LLM may have written (prompt injection defense)\n // Only clean files not in the pre-existing snapshot and not our expected output\n for (const entry of readdirSync(skilldDir)) {\n if (entry !== outputFile && !preExistingFiles.has(entry)) {\n // Allow other section output files and debug prompts\n if (Object.values(SECTION_OUTPUT_FILES).includes(entry))\n continue\n if (entry.startsWith('PROMPT_') || entry === 'logs')\n continue\n try {\n unlinkSync(join(skilldDir, entry))\n }\n catch {}\n }\n }\n\n // Prefer file written by LLM, fall back to Write tool content (if denied), then accumulated stdout\n const raw = (existsSync(outputPath) ? readFileSync(outputPath, 'utf-8') : lastWriteContent || accumulatedText).trim()\n\n // Always write stderr on failure; write all logs in debug mode\n const logsDir = join(skilldDir, 'logs')\n const logName = section.toUpperCase().replace(/-/g, '_')\n if (debug || (stderr && (!raw || code !== 0))) {\n mkdirSync(logsDir, { recursive: true })\n if (stderr)\n writeFileSync(join(logsDir, `${logName}.stderr.log`), stderr)\n }\n if (debug) {\n mkdirSync(logsDir, { recursive: true })\n if (rawLines.length)\n writeFileSync(join(logsDir, `${logName}.jsonl`), rawLines.join('\\n'))\n if (raw)\n writeFileSync(join(logsDir, `${logName}.md`), raw)\n }\n\n if (!raw && code !== 0) {\n resolve({ section, content: '', wasOptimized: false, error: stderr.trim() || `CLI exited with code ${code}` })\n return\n }\n\n // Clean the section output (strip markdown fences, frontmatter, sanitize)\n const content = raw ? cleanSectionOutput(raw) : ''\n\n if (content) {\n // Write cleaned content back to the output file for debugging\n writeFileSync(outputPath, content)\n }\n\n const validator = getSectionValidator(section)\n const rawWarnings = content && validator ? validator(content) : []\n const warnings: ValidationWarning[] = rawWarnings.map(w => ({ section, warning: w.warning }))\n\n resolve({\n section,\n content,\n wasOptimized: !!content,\n warnings: warnings?.length ? warnings : undefined,\n usage,\n cost,\n })\n })\n\n proc.on('error', (err) => {\n resolve({ section, content: '', wasOptimized: false, error: err.message })\n })\n })\n}\n\n// ── Main orchestrator ────────────────────────────────────────────────\n\nexport async function optimizeDocs(opts: OptimizeDocsOptions): Promise<OptimizeResult> {\n const { packageName, skillDir, model = 'sonnet', version, hasGithub, hasReleases, hasChangelog, docFiles, docsType, hasShippedDocs, onProgress, timeout = 180000, debug, noCache, sections, customPrompt, features, pkgFiles, overheadLines } = opts\n\n const selectedSections = sections ?? ['api-changes', 'best-practices'] as SkillSection[]\n\n // Build all section prompts\n const sectionPrompts = buildAllSectionPrompts({\n packageName,\n skillDir,\n version,\n hasIssues: hasGithub,\n hasDiscussions: hasGithub,\n hasReleases,\n hasChangelog,\n docFiles,\n docsType,\n hasShippedDocs,\n customPrompt,\n features,\n pkgFiles,\n overheadLines,\n sections: selectedSections,\n })\n\n if (sectionPrompts.size === 0) {\n return { optimized: '', wasOptimized: false, error: 'No valid sections to generate' }\n }\n\n if (isPiAiModel(model)) {\n const available = new Set(getAvailablePiAiModels().map(m => m.id as OptimizeModel))\n if (!available.has(model))\n return { optimized: '', wasOptimized: false, error: `Pi model unavailable or not authenticated: ${model}` }\n }\n else if (!CLI_MODELS[model]) {\n return { optimized: '', wasOptimized: false, error: `No CLI mapping for model: ${model}` }\n }\n\n // Check per-section cache: references dir first (version-keyed), then LLM cache (prompt-hashed)\n const cachedResults: SectionResult[] = []\n const uncachedSections: Array<{ section: SkillSection, prompt: string }> = []\n\n for (const [section, prompt] of sectionPrompts) {\n if (!noCache) {\n // Check global references dir (cross-project, version-keyed)\n if (version) {\n const outputFile = SECTION_OUTPUT_FILES[section]\n const refCached = readCachedSection(packageName, version, outputFile)\n if (refCached) {\n onProgress?.({ chunk: `[${section}: cached]`, type: 'text', text: refCached, reasoning: '', section })\n cachedResults.push({ section, content: refCached, wasOptimized: true })\n continue\n }\n }\n\n // Check LLM prompt-hash cache\n const cached = getCached(prompt, model, section)\n if (cached) {\n onProgress?.({ chunk: `[${section}: cached]`, type: 'text', text: cached, reasoning: '', section })\n cachedResults.push({ section, content: cached, wasOptimized: true })\n continue\n }\n }\n uncachedSections.push({ section, prompt })\n }\n\n // Prepare .skilld/ dir and snapshot before spawns\n const skilldDir = join(skillDir, '.skilld')\n mkdirSync(skilldDir, { recursive: true })\n\n // Pre-flight: warn about broken symlinks in .skilld/ (avoids wasting tokens on missing refs)\n for (const entry of readdirSync(skilldDir)) {\n const entryPath = join(skilldDir, entry)\n try {\n if (lstatSync(entryPath).isSymbolicLink() && !existsSync(entryPath))\n onProgress?.({ chunk: `[warn: broken symlink .skilld/${entry}]`, type: 'reasoning', text: '', reasoning: '' })\n }\n catch {}\n }\n\n const preExistingFiles = new Set(readdirSync(skilldDir))\n\n // Spawn uncached sections with staggered starts to avoid rate-limit collisions\n const STAGGER_MS = 3000\n const spawnResults = uncachedSections.length > 0\n ? await Promise.allSettled(\n uncachedSections.map(({ section, prompt }, i) => {\n const outputFile = SECTION_OUTPUT_FILES[section]\n const run = () => optimizeSection({\n section,\n prompt,\n outputFile,\n skillDir,\n model,\n packageName,\n onProgress,\n timeout,\n debug,\n preExistingFiles,\n })\n // Stagger: first section starts immediately, rest delayed\n if (i === 0)\n return run()\n return delay(i * STAGGER_MS).then(run)\n }),\n )\n : []\n\n // Collect results, retry failed sections once\n const allResults: SectionResult[] = [...cachedResults]\n let totalUsage: { input: number, output: number } | undefined\n let totalCost = 0\n const retryQueue: Array<{ index: number, section: SkillSection, prompt: string }> = []\n\n for (let i = 0; i < spawnResults.length; i++) {\n const r = spawnResults[i]!\n const { section, prompt } = uncachedSections[i]!\n if (r.status === 'fulfilled' && r.value.wasOptimized) {\n allResults.push(r.value)\n if (r.value.usage) {\n totalUsage = totalUsage ?? { input: 0, output: 0 }\n totalUsage.input += r.value.usage.input\n totalUsage.output += r.value.usage.output\n }\n if (r.value.cost != null)\n totalCost += r.value.cost\n if (!noCache)\n setCache(prompt, model, section, r.value.content)\n }\n else {\n retryQueue.push({ index: i, section, prompt })\n }\n }\n\n // Retry failed sections once (sequential to avoid rate limits)\n for (const { section, prompt } of retryQueue) {\n onProgress?.({ chunk: `[${section}: retrying...]`, type: 'reasoning', text: '', reasoning: '', section })\n await delay(STAGGER_MS)\n const result = await optimizeSection({\n section,\n prompt,\n outputFile: SECTION_OUTPUT_FILES[section],\n skillDir,\n model,\n packageName,\n onProgress,\n timeout,\n debug,\n preExistingFiles,\n }).catch((err: Error) => ({ section, content: '', wasOptimized: false, error: err.message }) as SectionResult)\n\n allResults.push(result)\n if (result.wasOptimized && !noCache)\n setCache(prompt, model, section, result.content)\n if (result.usage) {\n totalUsage = totalUsage ?? { input: 0, output: 0 }\n totalUsage.input += result.usage.input\n totalUsage.output += result.usage.output\n }\n if (result.cost != null)\n totalCost += result.cost\n }\n\n // Write successful sections to global references dir for cross-project reuse\n if (version) {\n const sectionFiles = allResults\n .filter(r => r.wasOptimized && r.content)\n .map(r => ({ file: SECTION_OUTPUT_FILES[r.section], content: r.content }))\n if (sectionFiles.length > 0) {\n writeSections(packageName, version, sectionFiles)\n }\n }\n\n // Merge results in SECTION_MERGE_ORDER, wrapped with comment markers\n const mergedParts: string[] = []\n for (const section of SECTION_MERGE_ORDER) {\n const result = allResults.find(r => r.section === section)\n if (result?.wasOptimized && result.content) {\n mergedParts.push(wrapSection(section, result.content))\n }\n }\n\n const optimized = mergedParts.join('\\n\\n')\n const wasOptimized = mergedParts.length > 0\n\n const usageResult = totalUsage\n ? { inputTokens: totalUsage.input, outputTokens: totalUsage.output, totalTokens: totalUsage.input + totalUsage.output }\n : undefined\n\n // Collect errors and warnings from sections\n const errors = allResults.filter(r => r.error).map(r => `${r.section}: ${r.error}`)\n const warnings = allResults.flatMap(r => r.warnings ?? []).map(w => `${w.section}: ${w.warning}`)\n\n const debugLogsDir = debug && uncachedSections.length > 0\n ? join(skillDir, '.skilld', 'logs')\n : undefined\n\n return {\n optimized,\n wasOptimized,\n error: errors.length > 0 ? errors.join('; ') : undefined,\n warnings: warnings.length > 0 ? warnings : undefined,\n finishReason: wasOptimized ? 'stop' : 'error',\n usage: usageResult,\n cost: totalCost || undefined,\n debugLogsDir,\n }\n}\n\n// ── Helpers ──────────────────────────────────────────────────────────\n\n/** Shorten absolute paths for display: /home/user/project/.claude/skills/vue/SKILL.md → .claude/.../SKILL.md */\nfunction shortenPath(p: string): string {\n const refIdx = p.indexOf('.skilld/')\n if (refIdx !== -1)\n return p.slice(refIdx + '.skilld/'.length)\n // Keep just filename for other paths\n const parts = p.split('/')\n return parts.length > 2 ? `.../${parts.slice(-2).join('/')}` : p\n}\n\n/** Replace absolute paths in a command string with shortened versions */\nfunction shortenCommand(cmd: string): string {\n return cmd.replace(/\\/[^\\s\"']+/g, (match) => {\n // Only shorten paths that look like they're inside a project\n if (match.includes('.claude/') || match.includes('.skilld/') || match.includes('node_modules/'))\n return `.../${match.split('/').slice(-2).join('/')}`\n return match\n })\n}\n\n/** Clean a single section's LLM output: strip markdown fences, frontmatter, sanitize */\nexport function cleanSectionOutput(content: string): string {\n let cleaned = content.trim()\n\n // Strip wrapping fences if output is wrapped in ```markdown, ```md, or bare ```\n // Requires matched open+close pair to avoid stripping internal code blocks\n const wrapMatch = cleaned.match(/^```(?:markdown|md)?[^\\S\\n]*\\n([\\s\\S]+)\\n```[^\\S\\n]*$/)\n if (wrapMatch) {\n const inner = wrapMatch[1]!.trim()\n // For bare ``` wrappers (no markdown/md tag), verify inner looks like section output\n const isExplicitWrapper = /^```(?:markdown|md)/.test(cleaned)\n if (isExplicitWrapper || /^##\\s/m.test(inner) || /^- (?:BREAKING|DEPRECATED|NEW): /m.test(inner)) {\n cleaned = inner\n }\n }\n\n // Normalize h1 headers to h2 — LLMs sometimes use # instead of ##\n cleaned = cleaned.replace(/^# (?!#)/gm, '## ')\n\n // Strip accidental frontmatter or leading horizontal rules\n const fmMatch = cleaned.match(/^-{3,}\\n/)\n if (fmMatch) {\n const afterOpen = fmMatch[0].length\n const closeMatch = cleaned.slice(afterOpen).match(/\\n-{3,}/)\n if (closeMatch) {\n cleaned = cleaned.slice(afterOpen + closeMatch.index! + closeMatch[0].length).trim()\n }\n else {\n cleaned = cleaned.slice(afterOpen).trim()\n }\n }\n\n // Strip raw code preamble before first section marker (defense against LLMs dumping source)\n // Section markers: ## heading, BREAKING/DEPRECATED/NEW labels\n const firstMarker = cleaned.match(/^(##\\s|- (?:BREAKING|DEPRECATED|NEW): )/m)\n if (firstMarker?.index && firstMarker.index > 0) {\n const preamble = cleaned.slice(0, firstMarker.index)\n // Only strip if preamble looks like code (contains function/const/export/return patterns)\n if (/\\b(?:function|const |let |var |export |return |import |async |class )\\b/.test(preamble)) {\n cleaned = cleaned.slice(firstMarker.index).trim()\n }\n }\n\n // Strip duplicate section headings (LLM echoing the format example before real content)\n // Handles headings separated by blank lines or boilerplate text\n const headingMatch = cleaned.match(/^(## .+)\\n/)\n if (headingMatch) {\n const heading = headingMatch[1]!\n const afterFirst = headingMatch[0].length\n const secondIdx = cleaned.indexOf(heading, afterFirst)\n if (secondIdx !== -1) {\n // Only strip if the gap between duplicates is small (< 200 chars of boilerplate)\n if (secondIdx - afterFirst < 200)\n cleaned = cleaned.slice(secondIdx).trim()\n }\n }\n\n // Normalize citation link text to [source] — LLMs sometimes use the path as link text\n // e.g. [./references/docs/api.md](./references/docs/api.md) or [`./references/...`](...)\n // Also handles paren-wrapped variants: ([`path`](url))\n cleaned = cleaned.replace(\n /\\(?\\[`?\\.\\/(?:\\.skilld\\/|references\\/)[^)\\]]*\\]\\(([^)]+)\\)\\)?/g,\n (match, url: string) => {\n // Only normalize if the URL points to a reference path\n if (/^\\.\\/(?:\\.skilld\\/|references\\/)/.test(url))\n return `[source](${url})`\n return match\n },\n )\n\n // Normalize source link paths: ensure .skilld/ prefix is present\n // LLMs sometimes emit [source](./docs/...) instead of [source](./.skilld/docs/...)\n cleaned = cleaned.replace(\n /\\[source\\]\\(\\.\\/((docs|issues|discussions|releases|pkg|guide)\\/)/g,\n '[source](./.skilld/$1',\n )\n\n cleaned = sanitizeMarkdown(cleaned)\n\n // Reject content that lacks any section structure — likely leaked LLM reasoning/narration\n // Valid sections contain headings (##), API change labels, or source-linked items\n if (!/^##\\s/m.test(cleaned) && !/^- (?:BREAKING|DEPRECATED|NEW): /m.test(cleaned) && !/\\[source\\]/.test(cleaned)) {\n return ''\n }\n\n return cleaned\n}\n","/**\n * Detect packages from framework presets (e.g., Nuxt modules in nuxt.config)\n * These are string literals in config arrays, not imports — the import scanner misses them.\n */\n\nimport type { PackageUsage } from './detect-imports.ts'\nimport { readFile } from 'node:fs/promises'\nimport { parseSync } from 'oxc-parser'\nimport { join } from 'pathe'\n\nconst NUXT_CONFIG_FILES = ['nuxt.config.ts', 'nuxt.config.js', 'nuxt.config.mjs']\nconst NUXT_ECOSYSTEM = ['vue', 'nitro', 'h3']\n\nasync function findNuxtConfig(cwd: string): Promise<{ path: string, content: string } | null> {\n for (const name of NUXT_CONFIG_FILES) {\n const path = join(cwd, name)\n const content = await readFile(path, 'utf8').catch(() => null)\n if (content)\n return { path, content }\n }\n return null\n}\n\n/**\n * Walk AST node to find all string values inside a `modules` array property.\n * Handles: defineNuxtConfig({ modules: [...] }) and export default { modules: [...] }\n */\nexport function extractModuleStrings(node: any): string[] {\n if (!node || typeof node !== 'object')\n return []\n\n // Found a Property with key \"modules\" and an ArrayExpression value\n if (node.type === 'Property' && !node.computed\n && (node.key?.type === 'Identifier' && node.key.name === 'modules')\n && node.value?.type === 'ArrayExpression') { return node.value.elements.filter((el: any) => el?.type === 'Literal' && typeof el.value === 'string').map((el: any) => el.value as string) }\n\n // Recurse into arrays and object values\n const results: string[] = []\n if (Array.isArray(node)) {\n for (const child of node)\n results.push(...extractModuleStrings(child))\n }\n else {\n for (const key of Object.keys(node)) {\n if (key === 'start' || key === 'end' || key === 'type')\n continue\n const val = node[key]\n if (val && typeof val === 'object')\n results.push(...extractModuleStrings(val))\n }\n }\n return results\n}\n\n/**\n * Detect Nuxt modules from nuxt.config.{ts,js,mjs}\n */\nexport async function detectNuxtModules(cwd: string): Promise<PackageUsage[]> {\n const config = await findNuxtConfig(cwd)\n if (!config)\n return []\n\n const result = parseSync(config.path, config.content)\n const modules = extractModuleStrings(result.program)\n\n // Dedupe and build results\n const seen = new Set<string>()\n const packages: PackageUsage[] = []\n\n for (const mod of modules) {\n if (!seen.has(mod)) {\n seen.add(mod)\n packages.push({ name: mod, count: 0, source: 'preset' })\n }\n }\n\n // Add core ecosystem packages\n for (const pkg of NUXT_ECOSYSTEM) {\n if (!seen.has(pkg)) {\n seen.add(pkg)\n packages.push({ name: pkg, count: 0, source: 'preset' })\n }\n }\n\n return packages\n}\n\n/**\n * Run all preset detectors and merge results\n */\nexport async function detectPresetPackages(cwd: string): Promise<PackageUsage[]> {\n // Currently only Nuxt, but extensible for other frameworks\n return detectNuxtModules(cwd)\n}\n","/**\n * Detect directly-used npm packages by scanning source files\n * Uses mlly for proper ES module parsing + tinyglobby for file discovery\n */\n\nimport { readFile } from 'node:fs/promises'\nimport { findDynamicImports, findStaticImports } from 'mlly'\nimport { glob } from 'tinyglobby'\nimport { detectPresetPackages } from './detect-presets.ts'\n\nexport interface PackageUsage {\n name: string\n count: number\n source?: 'import' | 'preset'\n}\n\nexport interface DetectResult {\n packages: PackageUsage[]\n error?: string\n}\n\nconst PATTERNS = ['**/*.{ts,js,vue,mjs,cjs,tsx,jsx,mts,cts}']\nconst IGNORE = ['**/node_modules/**', '**/dist/**', '**/.nuxt/**', '**/.output/**', '**/coverage/**']\n\nfunction addPackage(counts: Map<string, number>, specifier: string | undefined) {\n if (!specifier || specifier.startsWith('.') || specifier.startsWith('/'))\n return\n\n // Extract package name (handle subpaths like 'pkg/subpath')\n const name = specifier.startsWith('@')\n ? specifier.split('/').slice(0, 2).join('/')\n : specifier.split('/')[0]!\n\n if (!isNodeBuiltin(name)) {\n counts.set(name, (counts.get(name) || 0) + 1)\n }\n}\n\n/**\n * Scan source files to detect all directly-imported npm packages\n * Async with gitignore support for proper spinner animation\n */\nexport async function detectImportedPackages(cwd: string = process.cwd()): Promise<DetectResult> {\n try {\n const counts = new Map<string, number>()\n\n const files = await glob(PATTERNS, {\n cwd,\n ignore: IGNORE,\n absolute: true,\n expandDirectories: false,\n })\n\n await Promise.all(files.map(async (file) => {\n const content = await readFile(file, 'utf8')\n\n // Static: import x from 'pkg'\n for (const imp of findStaticImports(content)) {\n addPackage(counts, imp.specifier)\n }\n\n // Dynamic: import('pkg') - expression is the string literal\n for (const imp of findDynamicImports(content)) {\n // expression includes quotes, extract string value\n const match = imp.expression.match(/^['\"]([^'\"]+)['\"]$/)\n if (match)\n addPackage(counts, match[1]!)\n }\n }))\n\n // Sort by usage count (descending), then alphabetically\n const packages: PackageUsage[] = Array.from(counts.entries(), ([name, count]) => ({ name, count, source: 'import' as const }))\n .sort((a, b) => b.count - a.count || a.name.localeCompare(b.name))\n\n // Merge preset-detected packages (imports take priority)\n const presets = await detectPresetPackages(cwd)\n const importNames = new Set(packages.map(p => p.name))\n for (const preset of presets) {\n if (!importNames.has(preset.name))\n packages.push(preset)\n }\n\n return { packages }\n }\n catch (err) {\n return { packages: [], error: String(err) }\n }\n}\n\nconst NODE_BUILTINS = new Set([\n 'assert',\n 'async_hooks',\n 'buffer',\n 'child_process',\n 'cluster',\n 'console',\n 'constants',\n 'crypto',\n 'dgram',\n 'diagnostics_channel',\n 'dns',\n 'domain',\n 'events',\n 'fs',\n 'http',\n 'http2',\n 'https',\n 'inspector',\n 'module',\n 'net',\n 'os',\n 'path',\n 'perf_hooks',\n 'process',\n 'punycode',\n 'querystring',\n 'readline',\n 'repl',\n 'sea',\n 'sqlite',\n 'stream',\n 'string_decoder',\n 'sys',\n 'test',\n 'timers',\n 'tls',\n 'trace_events',\n 'tty',\n 'url',\n 'util',\n 'v8',\n 'vm',\n 'wasi',\n 'worker_threads',\n 'zlib',\n])\n\nfunction isNodeBuiltin(pkg: string): boolean {\n const base = pkg.startsWith('node:') ? pkg.slice(5) : pkg\n return NODE_BUILTINS.has(base.split('/')[0]!)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,MAAaA,QAAM;AACnB,MAAaC,YAAU;AAEvB,MAAaC,WAAwC;CACnD,MAAM;EAAE,OAAO;EAAQ,MAAM;EAAY,MAAM;EAAiC;CAChF,QAAQ;EAAE,OAAO;EAAU,MAAM;EAAc,MAAM;EAA2B;CAChF,OAAO;EAAE,OAAO;EAAS,MAAM;EAAa,MAAM;EAA6B,aAAa;;CAC7F;AAED,SAAgBC,YAAU,OAAe,UAAkB,aAAiC;AAW1F,QAAO;EACL;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EAlBmB;GAEnB;GACA;GACA;GACA;GACA;GAGD,CAAC,KAAK,IAAI;EAWT;EACA;EACA;EACA;EACA,GAAG,YAAY,SAAQ,MAAK,CAAC,aAAa,EAAE,CAAC;EAC7C;EACD;;;;;;;;;;;;AAaH,SAAgBC,YAAU,MAA2B;AACnD,KAAI;EACF,MAAM,MAAM,KAAK,MAAM,KAAK;AAG5B,MAAI,IAAI,SAAS,gBAAgB;GAC/B,MAAM,MAAM,IAAI;AAChB,OAAI,CAAC,IACH,QAAO,EAAE;AAGX,O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,cAAc,IAAI,IAAI,SAAS,KAAI,MAAK,EAAE,KAAK,CAAC;AACtD,OAAK,MAAM,UAAU,QACnB,KAAI,CAAC,YAAY,IAAI,OAAO,KAAK,CAC/B,UAAS,KAAK,OAAO;AAGzB,SAAO,EAAE,UAAU;UAEd,KAAK;AACV,SAAO;GAAE,UAAU,EAAE;GAAE,OAAO,OAAO,IAAA;GAAM;;;AAI/C,MAAM,gBAAgB,IAAI,IAAI;CAC5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD,CAAC;AAEF,SAAS,cAAc,KAAsB;CAC3C,MAAM,OAAO,IAAI,WAAW,QAAQ,GAAG,IAAI,MAAM,EAAE,GAAG;AACtD,QAAO,cAAc,IAAI,KAAK,MAAM,IAAI,CAAC,GAAI"}
1
+ {"version":3,"file":"agent.mjs","names":["cli","agentId","models","buildArgs","parseLine","cli","agentId","models","buildArgs","parseLine","resolve","claude","gemini","codex","claude.buildArgs","gemini.buildArgs","codex.buildArgs","claude.parseLine","gemini.parseLine","codex.parseLine","agents","delay"],"sources":["../../src/agent/clis/claude.ts","../../src/agent/clis/codex.ts","../../src/agent/clis/gemini.ts","../../src/agent/clis/pi-ai.ts","../../src/agent/clis/index.ts","../../src/agent/detect-presets.ts","../../src/agent/detect-imports.ts"],"sourcesContent":["/**\n * Claude Code CLI — token-level streaming via --include-partial-messages\n *\n * Write permission: Claude Code has hardcoded .claude/ write protection and\n * --allowedTools glob patterns are broken (github.com/anthropics/claude-code/issues/6881).\n * Instead of fighting the permission system, we let Write be auto-denied in pipe mode\n * and capture the content via writeContent fallback in parseLine().\n */\n\nimport type { CliModelEntry, ParsedEvent } from './types.ts'\n\nexport const cli = 'claude' as const\nexport const agentId = 'claude-code' as const\n\nexport const models: Record<string, CliModelEntry> = {\n opus: { model: 'opus', name: 'Opus 4.6', hint: 'Most capable for complex work' },\n sonnet: { model: 'sonnet', name: 'Sonnet 4.6', hint: 'Best for everyday tasks' },\n haiku: { model: 'haiku', name: 'Haiku 4.5', hint: 'Fastest for quick answers', recommended: true },\n}\n\nexport function buildArgs(model: string, skillDir: string, symlinkDirs: string[]): string[] {\n const allowedTools = [\n // Bare tool names — --add-dir already scopes visibility\n 'Read',\n 'Glob',\n 'Grep',\n 'Bash(*skilld search*)',\n 'Bash(*skilld validate*)',\n // Write intentionally omitted — auto-denied in pipe mode, content\n // captured via writeContent fallback (see parseLine + index.ts:373)\n ].join(' ')\n return [\n '-p',\n '--model',\n model,\n '--output-format',\n 'stream-json',\n '--verbose',\n '--include-partial-messages',\n '--allowedTools',\n allowedTools,\n '--disallowedTools',\n 'WebSearch WebFetch Task',\n '--add-dir',\n skillDir,\n ...symlinkDirs.flatMap(d => ['--add-dir', d]),\n '--no-session-persistence',\n ]\n}\n\n/**\n * Parse claude stream-json events\n *\n * Event types:\n * - stream_event/content_block_delta/text_delta → token streaming\n * - stream_event/content_block_start/tool_use → tool invocation starting\n * - assistant message with tool_use content → tool name + input\n * - assistant message with text content → full text (non-streaming fallback)\n * - result → usage, cost, turns\n */\nexport function parseLine(line: string): ParsedEvent {\n try {\n const obj = JSON.parse(line)\n\n // Token-level streaming (--include-partial-messages)\n if (obj.type === 'stream_event') {\n const evt = obj.event\n if (!evt)\n return {}\n\n // Text delta — the main streaming path\n if (evt.type === 'content_block_delta' && evt.delta?.type === 'text_delta') {\n return { textDelta: evt.delta.text }\n }\n\n return {}\n }\n\n // Full assistant message (complete turn, after streaming)\n if (obj.type === 'assistant' && obj.message?.content) {\n const content = obj.message.content as any[]\n\n // Extract tool uses with inputs for progress hints\n const tools = content.filter((c: any) => c.type === 'tool_use')\n if (tools.length) {\n const names = tools.map((t: any) => t.name)\n // Extract useful hint from tool input (file path, query, etc)\n const hint = tools.map((t: any) => {\n const input = t.input || {}\n return input.file_path || input.path || input.pattern || input.query || input.command || ''\n }).filter(Boolean).join(', ')\n // Capture Write content — primary output path since Write is auto-denied\n const writeTool = tools.find((t: any) => t.name === 'Write' && t.input?.content)\n return { toolName: names.join(', '), toolHint: hint || undefined, writeContent: writeTool?.input?.content }\n }\n\n // Text content (fallback for non-partial mode)\n const text = content\n .filter((c: any) => c.type === 'text')\n .map((c: any) => c.text)\n .join('')\n if (text)\n return { fullText: text }\n }\n\n // Final result\n if (obj.type === 'result') {\n const u = obj.usage\n return {\n done: true,\n usage: u ? { input: u.input_tokens ?? u.inputTokens ?? 0, output: u.output_tokens ?? u.outputTokens ?? 0 } : undefined,\n cost: obj.total_cost_usd,\n turns: obj.num_turns,\n }\n }\n }\n catch {}\n return {}\n}\n","/**\n * OpenAI Codex CLI — exec subcommand with JSON output\n * Prompt passed via stdin with `-` sentinel\n *\n * Event types:\n * - turn.started / turn.completed → turn lifecycle + usage\n * - item.started → command_execution in progress\n * - item.completed → agent_message (text), command_execution (result), file_change (apply_patch)\n * - error / turn.failed → errors\n */\n\nimport type { CliModelEntry, ParsedEvent } from './types.ts'\n\nexport const cli = 'codex' as const\nexport const agentId = 'codex' as const\n\nexport const models: Record<string, CliModelEntry> = {\n 'gpt-5.3-codex': { model: 'gpt-5.3-codex', name: 'GPT-5.3 Codex', hint: 'Latest frontier Codex model' },\n 'gpt-5.3-codex-spark': { model: 'gpt-5.3-codex-spark', name: 'GPT-5.3 Codex Spark', hint: 'Faster GPT-5.3 Codex variant', recommended: true },\n 'gpt-5.2-codex': { model: 'gpt-5.2-codex', name: 'GPT-5.2 Codex', hint: 'Frontier agentic coding model' },\n}\n\nexport function buildArgs(model: string, _skillDir: string, _symlinkDirs: string[]): string[] {\n return [\n 'exec',\n '--json',\n '--ephemeral',\n '--model',\n model,\n // Permissions aligned with Claude's scoped model:\n // --full-auto = --sandbox workspace-write + --ask-for-approval on-request\n // → writes scoped to CWD (.skilld/, set in spawn), reads unrestricted, network blocked\n // Shell remains enabled for `skilld` / `npx -y skilld` search/validate (no per-command allowlist in Codex)\n // --ephemeral → no session persistence (equivalent to Claude's --no-session-persistence)\n '--full-auto',\n '-',\n ]\n}\n\nexport function parseLine(line: string): ParsedEvent {\n try {\n const obj = JSON.parse(line)\n\n if (obj.type === 'item.completed' && obj.item) {\n const item = obj.item\n // Agent message — the main text output\n if (item.type === 'agent_message' && item.text)\n return { fullText: item.text }\n // Command execution completed — log as tool progress\n // If the command writes to a file (redirect or cat >), capture output as writeContent fallback\n if (item.type === 'command_execution' && item.aggregated_output) {\n const cmd = item.command || ''\n const writeContent = (/^cat\\s*>|>/.test(cmd)) ? item.aggregated_output : undefined\n return { toolName: 'Bash', toolHint: `(${item.aggregated_output.length} chars output)`, writeContent }\n }\n // apply_patch completed — file written directly to disk\n if (item.type === 'file_change' && item.changes?.length) {\n const paths = item.changes.map((c: { path: string, kind: string }) => c.path).join(', ')\n return { toolName: 'Write', toolHint: paths }\n }\n }\n\n // Command starting — show progress\n if (obj.type === 'item.started' && obj.item?.type === 'command_execution') {\n return { toolName: 'Bash', toolHint: obj.item.command }\n }\n\n // Turn completed — usage stats\n if (obj.type === 'turn.completed' && obj.usage) {\n return {\n done: true,\n usage: {\n input: obj.usage.input_tokens ?? 0,\n output: obj.usage.output_tokens ?? 0,\n },\n }\n }\n\n // Error events\n if (obj.type === 'turn.failed' || obj.type === 'error') {\n return { done: true }\n }\n }\n catch {}\n return {}\n}\n","/**\n * Gemini CLI — turn-level streaming via -o stream-json\n * Write scoping: relies on cwd being set to .skilld/ (no native --writeable-dirs)\n */\n\nimport type { CliModelEntry, ParsedEvent } from './types.ts'\nimport { resolveSkilldCommand } from '../../core/shared.ts'\n\nexport const cli = 'gemini' as const\nexport const agentId = 'gemini-cli' as const\n\nexport const models: Record<string, CliModelEntry> = {\n 'gemini-3.1-pro': { model: 'gemini-3.1-pro', name: 'Gemini 3.1 Pro', hint: 'Most capable' },\n 'gemini-3-flash': { model: 'gemini-3-flash', name: 'Gemini 3 Flash', hint: 'Balanced', recommended: true },\n}\n\nexport function buildArgs(model: string, skillDir: string, symlinkDirs: string[]): string[] {\n return [\n '-o',\n 'stream-json',\n '-m',\n model,\n '--allowed-tools',\n `read_file,write_file,glob_tool,list_directory,search_file_content,run_shell_command(${resolveSkilldCommand()}),run_shell_command(grep),run_shell_command(head)`,\n '--include-directories',\n skillDir,\n ...symlinkDirs.flatMap(d => ['--include-directories', d]),\n ]\n}\n\n/** Parse gemini stream-json events — turn level (full message per event) */\nexport function parseLine(line: string): ParsedEvent {\n try {\n const obj = JSON.parse(line)\n\n // Text message (delta or full)\n if (obj.type === 'message' && obj.role === 'assistant' && obj.content) {\n return obj.delta ? { textDelta: obj.content } : { fullText: obj.content }\n }\n\n // Tool invocation\n if (obj.type === 'tool_use' || obj.type === 'tool_call') {\n const name = obj.tool_name || obj.name || obj.tool || 'tool'\n const params = obj.parameters || obj.args || obj.input || {}\n const hint = params.file_path || params.path || params.dir_path || params.pattern || params.query || params.command || ''\n // Capture write_file content as fallback (matches Claude's Write tool behavior)\n if (name === 'write_file' && params.content) {\n return { toolName: name, toolHint: hint || undefined, writeContent: params.content }\n }\n return { toolName: name, toolHint: hint || undefined }\n }\n\n // Final result\n if (obj.type === 'result') {\n const s = obj.stats\n return {\n done: true,\n usage: s ? { input: s.input_tokens ?? s.input ?? 0, output: s.output_tokens ?? s.output ?? 0 } : undefined,\n turns: s?.tool_calls,\n }\n }\n }\n catch {}\n return {}\n}\n","/**\n * pi-ai adapter — direct LLM API calls via @mariozechner/pi-ai\n *\n * Optional alternative to CLI spawning. Supports:\n * - Env-var API keys (ANTHROPIC_API_KEY, GEMINI_API_KEY, OPENAI_API_KEY, etc.)\n * - OAuth login to piggyback on existing subscriptions (Claude Pro, ChatGPT Plus, Copilot)\n *\n * Models are enumerated dynamically from pi-ai's registry — no hardcoded list.\n * Reference content is inlined into the prompt via portabilizePrompt().\n */\n\nimport type { AssistantMessage, Message, ToolCall } from '@mariozechner/pi-ai'\nimport type { SkillSection } from '../prompts/index.ts'\nimport type { StreamProgress } from './types.ts'\nimport { execFileSync } from 'node:child_process'\nimport { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'\nimport { homedir } from 'node:os'\nimport { resolve } from 'node:path'\nimport { getEnvApiKey, getModel, getModels, getProviders, streamSimple } from '@mariozechner/pi-ai'\nimport { getOAuthApiKey, getOAuthProvider, getOAuthProviders } from '@mariozechner/pi-ai/oauth'\nimport { Type } from '@sinclair/typebox'\nimport { join } from 'pathe'\nimport { sanitizeMarkdown } from '../../core/sanitize.ts'\n\nexport function isPiAiModel(model: string): boolean {\n return model.startsWith('pi:')\n}\n\n/** Parse a pi:provider/model-id string → { provider, modelId } */\nexport function parsePiAiModelId(model: string): { provider: string, modelId: string } | null {\n if (!model.startsWith('pi:'))\n return null\n const rest = model.slice(3)\n const slashIdx = rest.indexOf('/')\n if (slashIdx === -1)\n return null\n return { provider: rest.slice(0, slashIdx), modelId: rest.slice(slashIdx + 1) }\n}\n\n// ── OAuth credentials ────────────────────────────────────────────────\n\n/** pi coding agent stores auth here; env var can override */\nconst PI_AGENT_AUTH_PATH = join(\n process.env.PI_CODING_AGENT_DIR || join(homedir(), '.pi', 'agent'),\n 'auth.json',\n)\n/** skilld's own auth file — used when user logs in via skilld */\nconst SKILLD_AUTH_PATH = join(homedir(), '.skilld', 'pi-ai-auth.json')\n\ninterface OAuthCredentials {\n type: 'oauth'\n refresh: string\n access: string\n expires: number\n [key: string]: unknown\n}\n\nfunction readAuthFile(path: string): Record<string, OAuthCredentials> {\n if (!existsSync(path))\n return {}\n try {\n return JSON.parse(readFileSync(path, 'utf-8'))\n }\n catch { return {} }\n}\n\n/** Load auth from pi coding agent first (~/.pi/agent/auth.json), then skilld's own */\nfunction loadAuth(): Record<string, OAuthCredentials> {\n const piAuth = readAuthFile(PI_AGENT_AUTH_PATH)\n const skilldAuth = readAuthFile(SKILLD_AUTH_PATH)\n // pi agent credentials take precedence (user's primary auth)\n return { ...skilldAuth, ...piAuth }\n}\n\n/** Save auth to skilld's own file — never writes to pi agent's auth */\nfunction saveAuth(auth: Record<string, OAuthCredentials>): void {\n mkdirSync(join(homedir(), '.skilld'), { recursive: true, mode: 0o700 })\n writeFileSync(SKILLD_AUTH_PATH, JSON.stringify(auth, null, 2), { mode: 0o600 })\n}\n\n/**\n * Overrides for model-provider → OAuth-provider mapping.\n * Most providers share the same ID in both systems (auto-matched).\n * Only list exceptions where the IDs diverge.\n */\nconst OAUTH_PROVIDER_OVERRIDES: Record<string, string> = {\n google: 'google-gemini-cli',\n openai: 'openai-codex',\n}\n\n/** Resolve model provider ID → OAuth provider ID */\nfunction resolveOAuthProviderId(modelProvider: string): string | null {\n if (OAUTH_PROVIDER_OVERRIDES[modelProvider])\n return OAUTH_PROVIDER_OVERRIDES[modelProvider]\n // Auto-match: if the model provider ID is also an OAuth provider, use it directly\n const oauthIds = new Set(getOAuthProviders().map((p: any) => p.id))\n if (oauthIds.has(modelProvider))\n return modelProvider\n return null\n}\n\n// ── Auth resolution ──────────────────────────────────────────────────\n\n/** Resolve API key for a provider — checks env vars first, then OAuth credentials */\nasync function resolveApiKey(provider: string): Promise<string | null> {\n // 1. Check env vars via pi-ai's own resolver\n const envKey = getEnvApiKey(provider)\n if (envKey)\n return envKey\n\n // 2. Check stored OAuth credentials\n const oauthProviderId = resolveOAuthProviderId(provider)\n if (!oauthProviderId)\n return null\n\n const auth = loadAuth()\n if (!auth[oauthProviderId])\n return null\n\n const result = await getOAuthApiKey(oauthProviderId, auth)\n if (!result)\n return null\n\n // Save refreshed credentials to skilld's own file only (never leak pi-agent tokens)\n const skilldAuth = readAuthFile(SKILLD_AUTH_PATH)\n skilldAuth[oauthProviderId] = { type: 'oauth', ...result.newCredentials }\n saveAuth(skilldAuth)\n return result.apiKey\n}\n\n// ── OAuth login flow ─────────────────────────────────────────────────\n\nexport interface LoginCallbacks {\n /** Called with the URL the user needs to open in their browser */\n onAuth: (url: string, instructions?: string) => void\n /** Called when pi-ai needs text input from the user */\n onPrompt: (message: string, placeholder?: string) => Promise<string>\n /** Status updates during the login flow */\n onProgress?: (message: string) => void\n}\n\n/** Get available OAuth providers for login */\nexport function getOAuthProviderList(): Array<{ id: string, name: string, loggedIn: boolean }> {\n const auth = loadAuth()\n const providers = getOAuthProviders() as Array<{ id: string, name: string }>\n return providers.map((p: any) => ({\n id: p.id,\n name: p.name ?? p.id,\n loggedIn: !!auth[p.id],\n }))\n}\n\n/** Run OAuth login for a provider, saving credentials to ~/.skilld/ */\nexport async function loginOAuthProvider(providerId: string, callbacks: LoginCallbacks): Promise<boolean> {\n const provider = getOAuthProvider(providerId)\n if (!provider)\n return false\n\n const credentials = await provider.login({\n onAuth: (info: any) => callbacks.onAuth(info.url, info.instructions),\n onPrompt: async (prompt: any) => callbacks.onPrompt(prompt.message, prompt.placeholder),\n onProgress: (msg: string) => callbacks.onProgress?.(msg),\n })\n\n const auth = loadAuth()\n auth[providerId] = { type: 'oauth', ...credentials }\n saveAuth(auth)\n return true\n}\n\n/** Remove OAuth credentials for a provider */\nexport function logoutOAuthProvider(providerId: string): void {\n const auth = loadAuth()\n delete auth[providerId]\n saveAuth(auth)\n}\n\n// ── Dynamic model enumeration ────────────────────────────────────────\n\nconst MIN_CONTEXT_WINDOW = 32_000\n\n/** Legacy model patterns — old generations that clutter the model list */\nconst LEGACY_MODEL_PATTERNS = [\n // Anthropic: claude 3.x family\n /^claude-3-/,\n /^claude-3\\.5-/,\n /^claude-3\\.7-/,\n // OpenAI: pre-gpt-5\n /^gpt-4(?!\\.\\d)/, // gpt-4, gpt-4-turbo, gpt-4o but not gpt-4.1\n /^o1/,\n /^o3-mini/,\n // Google: old gemini generations + non-text models\n /^gemini-1\\./,\n /^gemini-2\\.0/,\n /^gemini-live-/,\n // Preview snapshots with date suffixes (e.g. -preview-04-17, -preview-05-06)\n /-preview-\\d{2}-\\d{2,4}$/,\n // Dated model snapshots (e.g. -20240307, -20241022)\n /-\\d{8}$/,\n]\n\nfunction isLegacyModel(modelId: string): boolean {\n return LEGACY_MODEL_PATTERNS.some(p => p.test(modelId))\n}\n\n/** Preferred model per provider for auto-selection (cheapest reliable option) */\nconst RECOMMENDED_MODELS: Record<string, RegExp> = {\n anthropic: /haiku/,\n google: /flash/,\n openai: /gpt-4\\.1-mini/,\n}\n\nexport interface PiAiModelInfo {\n /** Full model ID: pi:provider/model-id */\n id: string\n /** Human-readable name */\n name: string\n /** Provider + context info */\n hint: string\n /** Auth source: 'env', 'oauth', or 'none' */\n authSource: 'env' | 'oauth' | 'none'\n /** Whether this is the recommended model for its provider */\n recommended: boolean\n}\n\n/** Get all pi-ai models for providers with auth configured */\nexport function getAvailablePiAiModels(): PiAiModelInfo[] {\n const providers: string[] = getProviders()\n const auth = loadAuth()\n const available: PiAiModelInfo[] = []\n const recommendedPicked = new Set<string>()\n\n for (const provider of providers) {\n let authSource: 'env' | 'oauth' | 'none' = 'none'\n if (getEnvApiKey(provider)) {\n authSource = 'env'\n }\n else {\n const oauthId = resolveOAuthProviderId(provider)\n if (oauthId && auth[oauthId])\n authSource = 'oauth'\n }\n\n if (authSource === 'none')\n continue\n\n const models: any[] = getModels(provider as any)\n // First pass: find the recommended model for this provider\n const recPattern = RECOMMENDED_MODELS[provider]\n let recModelId: string | null = null\n if (recPattern) {\n for (const model of models) {\n if (!isLegacyModel(model.id) && recPattern.test(model.id)) {\n recModelId = model.id\n break\n }\n }\n }\n\n for (const model of models) {\n if (model.contextWindow && model.contextWindow < MIN_CONTEXT_WINDOW)\n continue\n if (isLegacyModel(model.id))\n continue\n\n const id = `pi:${provider}/${model.id}`\n const ctx = model.contextWindow ? ` · ${Math.round(model.contextWindow / 1000)}k ctx` : ''\n const cost = model.cost?.input ? ` · $${model.cost.input}/Mtok` : ''\n const isRecommended = model.id === recModelId && !recommendedPicked.has(provider)\n\n if (isRecommended)\n recommendedPicked.add(provider)\n\n available.push({\n id,\n name: model.name || model.id,\n hint: `${authSource === 'oauth' ? 'OAuth' : 'API key'}${ctx}${cost}`,\n authSource,\n recommended: isRecommended,\n })\n }\n }\n\n return available\n}\n\n// ── Tool definitions for agentic mode ────────────────────────────────\n\nconst TOOLS = [\n {\n name: 'Read',\n description: 'Read a file. Path is relative to the working directory (e.g. \"./.skilld/docs/api.md\").',\n parameters: Type.Object({ path: Type.String({ description: 'File path to read' }) }),\n },\n {\n name: 'Glob',\n description: 'List files matching a glob pattern (e.g. \"./.skilld/docs/*.md\"). Returns newline-separated paths.',\n parameters: Type.Object({\n pattern: Type.String({ description: 'Glob pattern' }),\n no_ignore: Type.Optional(Type.Boolean({ description: 'Include gitignored files' })),\n }),\n },\n {\n name: 'Write',\n description: 'Write content to a file.',\n parameters: Type.Object({\n path: Type.String({ description: 'File path to write' }),\n content: Type.String({ description: 'File content' }),\n }),\n },\n {\n name: 'Bash',\n description: 'Run a shell command. Use for `skilld search`, `skilld validate`, etc.',\n parameters: Type.Object({ command: Type.String({ description: 'Shell command to run' }) }),\n },\n]\n\nconst MAX_TOOL_TURNS = 30\nconst SAFE_COMMANDS = new Set(['skilld', 'ls', 'cat', 'find'])\nconst SHELL_META_RE = /[;&|`$()<>]/\n\n/** Resolve a path safely within skilldDir, blocking traversal */\nfunction resolveSandboxedPath(p: string, skilldDir: string): string {\n const cleaned = String(p).replace(/^\\.\\/\\.skilld\\//, './').replace(/^\\.skilld\\//, './').replace(/^\\.\\//, '')\n const resolved = resolve(skilldDir, cleaned)\n if (!resolved.startsWith(`${skilldDir}/`) && resolved !== skilldDir)\n throw new Error(`Path traversal blocked: ${p}`)\n return resolved\n}\n\n/** Match a file path against a glob pattern using simple segment matching (no regex from user input) */\nfunction globMatch(filePath: string, pattern: string): boolean {\n const segments = pattern.split('**')\n if (segments.length === 1) {\n // No **, simple wildcard match: split on * and check containment in order\n const parts = pattern.split('*')\n if (parts.length === 1)\n return filePath === pattern\n let pos = 0\n for (let i = 0; i < parts.length; i++) {\n const part = parts[i]!\n if (!part)\n continue\n const idx = filePath.indexOf(part, pos)\n if (idx === -1)\n return false\n if (i === 0 && idx !== 0)\n return false // first segment must match from start\n pos = idx + part.length\n }\n if (parts.at(-1) !== '')\n return pos === filePath.length // last segment must match to end\n return true\n }\n // ** present: match any depth between segments\n let remaining = filePath\n for (let i = 0; i < segments.length; i++) {\n const seg = segments[i]!\n if (!seg)\n continue\n // Replace single * within each segment for matching\n const segParts = seg.split('*')\n let pos = 0\n let matched = false\n for (let attempt = remaining.indexOf(segParts[0]!, 0); attempt !== -1; attempt = remaining.indexOf(segParts[0]!, attempt + 1)) {\n pos = attempt\n matched = true\n for (const sp of segParts) {\n if (!sp)\n continue\n const idx = remaining.indexOf(sp, pos)\n if (idx === -1) {\n matched = false\n break\n }\n pos = idx + sp.length\n }\n if (matched)\n break\n }\n if (!matched)\n return false\n remaining = remaining.slice(pos)\n }\n return true\n}\n\n/** Execute a tool call against the .skilld/ directory */\nfunction executeTool(toolCall: ToolCall, skilldDir: string): string {\n const args = toolCall.arguments as Record<string, unknown>\n\n switch (toolCall.name) {\n case 'Read': {\n const filePath = resolveSandboxedPath(args.path as string, skilldDir)\n if (!existsSync(filePath))\n return `Error: file not found: ${args.path}`\n return sanitizeMarkdown(readFileSync(filePath, 'utf-8'))\n }\n case 'Glob': {\n const pattern = String(args.pattern).replace(/^\\.\\/\\.skilld\\//, './').replace(/^\\.skilld\\//, './').replace(/^\\.\\//, '')\n const results: string[] = []\n const walkDir = (dir: string, prefix: string) => {\n if (!existsSync(dir))\n return\n for (const entry of readdirSync(dir, { withFileTypes: true })) {\n const relPath = prefix ? `${prefix}/${entry.name}` : entry.name\n if (entry.isDirectory())\n walkDir(join(dir, entry.name), relPath)\n else results.push(`./.skilld/${relPath}`)\n }\n }\n const baseDir = pattern.split('*')[0]?.replace(/\\/$/, '') ?? ''\n walkDir(join(skilldDir, baseDir), baseDir)\n const matched = results.filter(r => globMatch(r.replace(/^\\.\\/\\.skilld\\//, ''), pattern))\n return matched.length > 0 ? matched.join('\\n') : `No files matching: ${args.pattern}`\n }\n case 'Write': {\n const filePath = resolveSandboxedPath(args.path as string, skilldDir)\n writeFileSync(filePath, sanitizeMarkdown(String(args.content)))\n return 'File written successfully.'\n }\n case 'Bash': {\n const cmd = String(args.command).trim()\n const parts = cmd.split(/\\s+/)\n const bin = parts[0] ?? ''\n if (!SAFE_COMMANDS.has(bin) || SHELL_META_RE.test(cmd))\n return `Error: command not allowed. Only skilld, ls, cat, find commands are permitted.`\n try {\n return execFileSync(bin, parts.slice(1), { cwd: skilldDir, timeout: 15_000, encoding: 'utf-8', maxBuffer: 512 * 1024 }).trim()\n }\n catch (err) {\n return `Error: ${(err as Error).message}`\n }\n }\n default:\n return `Unknown tool: ${toolCall.name}`\n }\n}\n\n// ── Section optimization ─────────────────────────────────────────────\n\nexport interface PiAiSectionOptions {\n section: SkillSection\n prompt: string\n skillDir: string\n model: string\n onProgress?: (progress: StreamProgress) => void\n signal?: AbortSignal\n}\n\nexport interface PiAiSectionResult {\n text: string\n /** The raw prompt sent to the model */\n fullPrompt: string\n usage?: { input: number, output: number }\n cost?: number\n}\n\n/** Optimize a single section using pi-ai agentic API with tool use */\nexport async function optimizeSectionPiAi(opts: PiAiSectionOptions): Promise<PiAiSectionResult> {\n const parsed = parsePiAiModelId(opts.model)\n if (!parsed)\n throw new Error(`Invalid pi-ai model ID: ${opts.model}. Expected format: pi:provider/model-id`)\n\n const model = getModel(parsed.provider as any, parsed.modelId as any)\n const apiKey = await resolveApiKey(parsed.provider)\n const skilldDir = join(opts.skillDir, '.skilld')\n\n // Use the raw prompt (references tool names like Read, Glob, Write, Bash)\n const fullPrompt = opts.prompt\n\n opts.onProgress?.({ chunk: '[starting...]', type: 'reasoning', text: '', reasoning: '', section: opts.section })\n\n const messages: Message[] = [{\n role: 'user' as const,\n content: [{ type: 'text' as const, text: fullPrompt }],\n timestamp: Date.now(),\n }]\n\n let text = ''\n let completed = false\n let totalUsage: { input: number, output: number } | undefined\n let totalCost: number | undefined\n let lastWriteContent = ''\n\n for (let turn = 0; turn < MAX_TOOL_TURNS; turn++) {\n if (opts.signal?.aborted)\n throw new Error('pi-ai request timed out')\n\n const eventStream = streamSimple(model, {\n systemPrompt: 'You are a technical documentation expert generating SKILL.md sections for AI agent skills. Follow the format instructions exactly. Use the provided tools to explore reference files in ./.skilld/ before writing your output.',\n messages,\n tools: TOOLS,\n }, {\n reasoning: turn === 0 ? 'medium' : undefined,\n maxTokens: 16_384,\n ...(apiKey ? { apiKey } : {}),\n })\n\n let assistantMessage: AssistantMessage | undefined\n let turnText = ''\n\n for await (const event of eventStream) {\n if (opts.signal?.aborted)\n throw new Error('pi-ai request timed out')\n\n switch (event.type) {\n case 'text_delta':\n turnText += event.delta\n opts.onProgress?.({ chunk: event.delta, type: 'text', text: turnText, reasoning: '', section: opts.section })\n break\n case 'toolcall_end': {\n const tc = event.toolCall\n const hint = tc.name === 'Read' || tc.name === 'Write'\n ? `[${tc.name}: ${tc.arguments.path}]`\n : tc.name === 'Bash'\n ? `[${tc.name}: ${tc.arguments.command}]`\n : `[${tc.name}: ${tc.arguments.pattern}]`\n opts.onProgress?.({ chunk: hint, type: 'reasoning', text: '', reasoning: hint, section: opts.section })\n break\n }\n case 'done':\n assistantMessage = event.message\n break\n case 'error':\n throw new Error(event.error?.errorMessage ?? 'pi-ai stream error')\n }\n }\n\n if (!assistantMessage)\n throw new Error('pi-ai stream ended without a message')\n\n // Accumulate usage across turns\n if (assistantMessage.usage) {\n if (totalUsage) {\n totalUsage.input += assistantMessage.usage.input\n totalUsage.output += assistantMessage.usage.output\n }\n else {\n totalUsage = { input: assistantMessage.usage.input, output: assistantMessage.usage.output }\n }\n totalCost = (totalCost ?? 0) + (assistantMessage.usage.cost?.total ?? 0)\n }\n\n // Add assistant message to conversation\n messages.push(assistantMessage)\n\n // Check if there are tool calls to execute\n const toolCalls = assistantMessage.content.filter((c): c is ToolCall => c.type === 'toolCall')\n if (toolCalls.length === 0) {\n text = turnText\n completed = true\n break\n }\n\n // Execute tool calls and add results\n for (const tc of toolCalls) {\n const result = executeTool(tc, skilldDir)\n // Track Write tool content for output fallback\n if (tc.name === 'Write')\n lastWriteContent = String(tc.arguments.content)\n messages.push({\n role: 'toolResult' as const,\n toolCallId: tc.id,\n toolName: tc.name,\n content: [{ type: 'text' as const, text: result }],\n isError: result.startsWith('Error:'),\n timestamp: Date.now(),\n })\n }\n }\n\n if (!completed)\n throw new Error(`pi-ai exceeded ${MAX_TOOL_TURNS} tool turns without completing`)\n\n // Prefer text output, fall back to last Write content\n const finalText = text || lastWriteContent\n\n return { text: finalText, fullPrompt, usage: totalUsage, cost: totalCost }\n}\n","/**\n * CLI orchestrator — spawns per-CLI processes for skill generation\n * Each CLI (claude, gemini, codex) has its own buildArgs + parseLine in separate files\n */\n\nimport type { SkillSection } from '../prompts/index.ts'\nimport type { AgentType } from '../types.ts'\nimport type { CliModelConfig, CliName, OptimizeDocsOptions, OptimizeModel, OptimizeResult, ParsedEvent, SectionResult, StreamProgress, ValidationWarning } from './types.ts'\nimport { exec, spawn } from 'node:child_process'\nimport { createHash } from 'node:crypto'\nimport { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, unlinkSync, writeFileSync } from 'node:fs'\nimport { homedir } from 'node:os'\nimport { setTimeout as delay } from 'node:timers/promises'\nimport { promisify } from 'node:util'\nimport { dirname, join } from 'pathe'\nimport { isWindows } from 'std-env'\nimport { readCachedSection, writeSections } from '../../cache/index.ts'\nimport { sanitizeMarkdown } from '../../core/sanitize.ts'\nimport { detectInstalledAgents } from '../detect.ts'\nimport { buildAllSectionPrompts, getSectionValidator, SECTION_MERGE_ORDER, SECTION_OUTPUT_FILES, wrapSection } from '../prompts/index.ts'\nimport { agents } from '../registry.ts'\nimport * as claude from './claude.ts'\nimport * as codex from './codex.ts'\nimport * as gemini from './gemini.ts'\nimport { getAvailablePiAiModels, isPiAiModel, optimizeSectionPiAi, parsePiAiModelId } from './pi-ai.ts'\n\nexport { buildAllSectionPrompts, buildSectionPrompt, SECTION_MERGE_ORDER, SECTION_OUTPUT_FILES } from '../prompts/index.ts'\nexport type { CustomPrompt, SkillSection } from '../prompts/index.ts'\nexport type { CliModelConfig, CliName, ModelInfo, OptimizeDocsOptions, OptimizeModel, OptimizeResult, StreamProgress } from './types.ts'\n\n// ── Tool progress display ────────────────────────────────────────────\n\nconst TOOL_VERBS: Record<string, string> = {\n // Claude\n Read: 'Reading',\n Glob: 'Searching',\n Grep: 'Searching',\n Write: 'Writing',\n Bash: 'Running',\n // Gemini\n read_file: 'Reading',\n glob_tool: 'Searching',\n write_file: 'Writing',\n list_directory: 'Listing',\n search_file_content: 'Searching',\n run_shell_command: 'Running',\n}\n\ninterface ToolProgressLog {\n message: (msg: string) => void\n}\n\n/** Create a progress callback that emits one line per tool call, Claude Code style */\nexport function createToolProgress(log: ToolProgressLog): (progress: StreamProgress) => void {\n let lastMsg = ''\n let repeatCount = 0\n /** Per-section timestamp of last \"Writing...\" emission — throttles text_delta spam */\n const lastTextEmit = new Map<string, number>()\n const TEXT_THROTTLE_MS = 2000\n\n function emit(msg: string) {\n if (msg === lastMsg) {\n repeatCount++\n log.message(`${msg} \\x1B[90m(+${repeatCount})\\x1B[0m`)\n }\n else {\n lastMsg = msg\n repeatCount = 0\n log.message(msg)\n }\n }\n\n return ({ type, chunk, text, section }) => {\n if (type === 'text') {\n const key = section ?? ''\n const now = Date.now()\n const last = lastTextEmit.get(key) ?? 0\n if (now - last < TEXT_THROTTLE_MS)\n return\n lastTextEmit.set(key, now)\n const prefix = section ? `\\x1B[90m[${section}]\\x1B[0m ` : ''\n // Count bullet items in accumulated text for meaningful progress\n const items = text ? (text.match(/^- (?:BREAKING|DEPRECATED|NEW|CHANGED|REMOVED|Use |Do |Set |Add |Avoid |Always |Never |Prefer |Check |Ensure )/gm)?.length ?? 0) : 0\n emit(items > 0 ? `${prefix}Writing... \\x1B[90m(${items} items)\\x1B[0m` : `${prefix}Writing...`)\n return\n }\n if (type !== 'reasoning' || !chunk.startsWith('['))\n return\n\n // Handle status messages like [starting...], [retrying...], [cached]\n if (/^\\[(?:starting|retrying|cached)/.test(chunk)) {\n const prefix = section ? `\\x1B[90m[${section}]\\x1B[0m ` : ''\n emit(`${prefix}${chunk.slice(1, -1)}`)\n return\n }\n\n // Parse individual tool names and hints from \"[Read: path]\" or \"[Read, Glob: path1, path2]\"\n const match = chunk.match(/^\\[([^:[\\]]+)(?::\\s(.+))?\\]$/)\n if (!match)\n return\n\n const names = match[1]!.split(',').map(n => n.trim())\n const hints = match[2]?.split(',').map(h => h.trim()) ?? []\n\n for (let i = 0; i < names.length; i++) {\n const rawName = names[i]!\n const hint = hints[i] ?? hints[0] ?? ''\n const verb = TOOL_VERBS[rawName] ?? rawName\n const prefix = section ? `\\x1B[90m[${section}]\\x1B[0m ` : ''\n\n if ((rawName === 'Bash' || rawName === 'run_shell_command') && hint) {\n const searchMatch = hint.match(/skilld search\\s+\"([^\"]+)\"/)\n if (searchMatch) {\n emit(`${prefix}Searching \\x1B[36m\"${searchMatch[1]}\"\\x1B[0m`)\n }\n else if (hint.includes('skilld validate')) {\n emit(`${prefix}Validating...`)\n }\n else {\n const shortened = shortenCommand(hint)\n emit(`${prefix}Running ${shortened.length > 50 ? `${shortened.slice(0, 47)}...` : shortened}`)\n }\n }\n else {\n const path = shortenPath(hint || '...')\n emit(`${prefix}${verb} \\x1B[90m${path}\\x1B[0m`)\n }\n }\n }\n}\n\n// ── Per-CLI dispatch ─────────────────────────────────────────────────\n\nconst CLI_DEFS = [claude, gemini, codex] as const\n\nconst CLI_BUILD_ARGS: Record<CliName, (model: string, skillDir: string, symlinkDirs: string[]) => string[]> = {\n claude: claude.buildArgs,\n gemini: gemini.buildArgs,\n codex: codex.buildArgs,\n}\n\nconst CLI_PARSE_LINE: Record<CliName, (line: string) => ParsedEvent> = {\n claude: claude.parseLine,\n gemini: gemini.parseLine,\n codex: codex.parseLine,\n}\n\n// ── Provider display names ────────────────────────────────────────────\n\n/** Map CLI agent IDs to their LLM provider name (not the agent/tool name) */\nconst CLI_PROVIDER_NAMES: Record<string, string> = {\n 'claude-code': 'Anthropic',\n 'gemini-cli': 'Google',\n 'codex': 'OpenAI',\n}\n\nconst PI_PROVIDER_NAMES: Record<string, string> = {\n 'anthropic': 'Anthropic',\n 'google': 'Google',\n 'google-antigravity': 'Antigravity',\n 'google-gemini-cli': 'Google Gemini',\n 'google-vertex': 'Google Vertex',\n 'openai': 'OpenAI',\n 'openai-codex': 'OpenAI Codex',\n 'github-copilot': 'GitHub Copilot',\n 'groq': 'Groq',\n 'mistral': 'Mistral',\n 'xai': 'xAI',\n}\n\n// ── Assemble CLI_MODELS from per-CLI model definitions ───────────────\n\nexport const CLI_MODELS: Partial<Record<OptimizeModel, CliModelConfig>> = Object.fromEntries(\n CLI_DEFS.flatMap(def =>\n Object.entries(def.models).map(([id, entry]) => [\n id,\n { ...entry, cli: def.cli, agentId: def.agentId },\n ]),\n ),\n)\n\n// ── Model helpers ────────────────────────────────────────────────────\n\nexport function getModelName(id: OptimizeModel): string {\n if (isPiAiModel(id)) {\n const parsed = parsePiAiModelId(id)\n return parsed?.modelId ?? id\n }\n return CLI_MODELS[id]?.name ?? id\n}\n\nexport function getModelLabel(id: OptimizeModel): string {\n if (isPiAiModel(id)) {\n const parsed = parsePiAiModelId(id)\n return parsed ? `${PI_PROVIDER_NAMES[parsed.provider] ?? parsed.provider} · ${parsed.modelId}` : id\n }\n const config = CLI_MODELS[id]\n if (!config)\n return id\n const providerName = CLI_PROVIDER_NAMES[config.agentId] ?? config.cli\n return `${providerName} · ${config.name}`\n}\n\nexport async function getAvailableModels(): Promise<import('./types.ts').ModelInfo[]> {\n const execAsync = promisify(exec)\n const lookupCmd = isWindows ? 'where' : 'which'\n\n const installedAgents = detectInstalledAgents()\n const agentsWithCli = installedAgents.filter(id => agents[id].cli)\n\n const cliChecks = await Promise.all(\n agentsWithCli.map(async (agentId) => {\n const cli = agents[agentId].cli!\n try {\n await execAsync(`${lookupCmd} ${cli}`)\n return agentId\n }\n catch { return null }\n }),\n )\n const availableAgentIds = new Set(cliChecks.filter((id): id is AgentType => id != null))\n\n const cliModels = (Object.entries(CLI_MODELS) as [OptimizeModel, CliModelConfig][])\n .filter(([_, config]) => availableAgentIds.has(config.agentId))\n .map(([id, config]) => {\n const providerName = CLI_PROVIDER_NAMES[config.agentId] ?? agents[config.agentId]?.displayName ?? config.agentId\n return {\n id,\n name: config.name,\n hint: config.hint,\n recommended: config.recommended,\n agentId: config.agentId,\n agentName: providerName,\n provider: config.agentId,\n providerName: `${providerName} (via ${config.cli} CLI)`,\n vendorGroup: providerName,\n }\n })\n\n // Append pi-ai direct API models (providers with auth configured)\n const piAiModels = getAvailablePiAiModels()\n const piAiEntries = piAiModels.map((m) => {\n const parsed = parsePiAiModelId(m.id)\n const piProvider = parsed?.provider ?? 'pi-ai'\n const displayName = PI_PROVIDER_NAMES[piProvider] ?? piProvider\n const authLabel = m.authSource === 'env' ? 'API' : 'OAuth'\n return {\n id: m.id as OptimizeModel,\n name: m.name,\n hint: m.hint,\n recommended: m.recommended,\n agentId: 'pi-ai',\n agentName: `pi-ai (${m.authSource})`,\n provider: `pi:${piProvider}:${m.authSource}`,\n providerName: `${displayName} (${authLabel})`,\n vendorGroup: displayName,\n }\n })\n\n return [...cliModels, ...piAiEntries]\n}\n\n// ── Reference dirs ───────────────────────────────────────────────────\n\n/** Resolve symlinks in .skilld/ to get real paths for --add-dir */\nfunction resolveReferenceDirs(skillDir: string): string[] {\n const refsDir = join(skillDir, '.skilld')\n if (!existsSync(refsDir))\n return []\n const resolved = readdirSync(refsDir)\n .map(entry => join(refsDir, entry))\n .filter(p => lstatSync(p).isSymbolicLink() && existsSync(p))\n .map(p => realpathSync(p))\n\n // Include parent directories so CLIs can search across all references at once\n // (e.g. Gemini's sandbox requires the parent dir to be explicitly included)\n const parents = new Set<string>()\n for (const p of resolved) {\n const parent = dirname(p)\n if (!resolved.includes(parent))\n parents.add(parent)\n }\n\n return [...resolved, ...parents]\n}\n\n// ── Cache ────────────────────────────────────────────────────────────\n\nconst CACHE_DIR = join(homedir(), '.skilld', 'llm-cache')\n\n/** Strip absolute paths from prompt so the hash is project-independent */\nfunction normalizePromptForHash(prompt: string): string {\n return prompt.replace(/\\/[^\\s`]*\\.(?:claude|codex|gemini)\\/skills\\/[^\\s/`]+/g, '<SKILL_DIR>')\n}\n\nfunction hashPrompt(prompt: string, model: OptimizeModel, section: SkillSection): string {\n return createHash('sha256').update(`exec:${model}:${section}:${normalizePromptForHash(prompt)}`).digest('hex').slice(0, 16)\n}\n\nfunction getCached(prompt: string, model: OptimizeModel, section: SkillSection, maxAge = 7 * 24 * 60 * 60 * 1000): string | null {\n const path = join(CACHE_DIR, `${hashPrompt(prompt, model, section)}.json`)\n if (!existsSync(path))\n return null\n try {\n const { text, timestamp } = JSON.parse(readFileSync(path, 'utf-8'))\n return Date.now() - timestamp > maxAge ? null : text\n }\n catch { return null }\n}\n\nfunction setCache(prompt: string, model: OptimizeModel, section: SkillSection, text: string): void {\n mkdirSync(CACHE_DIR, { recursive: true, mode: 0o700 })\n writeFileSync(\n join(CACHE_DIR, `${hashPrompt(prompt, model, section)}.json`),\n JSON.stringify({ text, model, section, timestamp: Date.now() }),\n { mode: 0o600 },\n )\n}\n\n// ── pi-ai direct API path ────────────────────────────────────────────\n\nasync function optimizeSectionViaPiAi(opts: {\n section: SkillSection\n prompt: string\n outputFile: string\n skillDir: string\n model: OptimizeModel\n onProgress?: (progress: StreamProgress) => void\n timeout: number\n debug?: boolean\n}): Promise<SectionResult> {\n const { section, prompt, outputFile, skillDir, model, onProgress, timeout, debug } = opts\n const skilldDir = join(skillDir, '.skilld')\n const outputPath = join(skilldDir, outputFile)\n const logsDir = join(skilldDir, 'logs')\n const logName = section.toUpperCase().replace(/-/g, '_')\n\n // Remove stale output so we don't read a leftover from a previous run\n if (existsSync(outputPath))\n unlinkSync(outputPath)\n\n // Write prompt for debugging (same as CLI path)\n writeFileSync(join(skilldDir, `PROMPT_${section}.md`), prompt)\n\n try {\n const ac = new AbortController()\n const timer = setTimeout(() => ac.abort(), timeout)\n const result = await optimizeSectionPiAi({ section, prompt, skillDir, model, onProgress, signal: ac.signal })\n .finally(() => clearTimeout(timer))\n\n const raw = result.text.trim()\n\n // Debug logging — match CLI path: actual prompt sent + raw output\n if (debug) {\n mkdirSync(logsDir, { recursive: true })\n writeFileSync(join(skilldDir, `PROMPT_${section}.md`), result.fullPrompt)\n if (raw)\n writeFileSync(join(logsDir, `${logName}.md`), raw)\n }\n\n if (!raw) {\n return { section, content: '', wasOptimized: false, error: 'pi-ai returned empty response' }\n }\n\n const content = cleanSectionOutput(raw)\n\n // Always write cleaned content to output file (matches CLI path)\n if (content)\n writeFileSync(outputPath, content)\n\n const validator = getSectionValidator(section)\n const rawWarnings = content && validator ? validator(content) : []\n const warnings: ValidationWarning[] = rawWarnings.map(w => ({ section, warning: w.warning }))\n\n return {\n section,\n content,\n wasOptimized: !!content,\n warnings: warnings?.length ? warnings : undefined,\n usage: result.usage,\n cost: result.cost,\n }\n }\n catch (err) {\n // Write error to logs on failure (matches CLI stderr logging)\n const errMsg = (err as Error).message\n if (debug || errMsg) {\n mkdirSync(logsDir, { recursive: true })\n writeFileSync(join(logsDir, `${logName}.stderr.log`), errMsg)\n }\n return { section, content: '', wasOptimized: false, error: errMsg }\n }\n}\n\n// ── Per-section spawn ────────────────────────────────────────────────\n\ninterface OptimizeSectionOptions {\n section: SkillSection\n prompt: string\n outputFile: string\n skillDir: string\n model: OptimizeModel\n packageName: string\n onProgress?: (progress: StreamProgress) => void\n timeout: number\n debug?: boolean\n preExistingFiles: Set<string>\n}\n\n/** Spawn a single CLI process for one section, or call pi-ai directly */\nfunction optimizeSection(opts: OptimizeSectionOptions): Promise<SectionResult> {\n const { section, prompt, outputFile, skillDir, model, onProgress, timeout, debug, preExistingFiles } = opts\n\n // pi-ai direct API path — no CLI spawning\n if (isPiAiModel(model)) {\n return optimizeSectionViaPiAi({ section, prompt, outputFile, skillDir, model, onProgress, timeout, debug })\n }\n\n const cliConfig = CLI_MODELS[model]\n if (!cliConfig) {\n return Promise.resolve({ section, content: '', wasOptimized: false, error: `No CLI mapping for model: ${model}` })\n }\n\n const { cli, model: cliModel } = cliConfig\n const symlinkDirs = resolveReferenceDirs(skillDir)\n const args = CLI_BUILD_ARGS[cli](cliModel, skillDir, symlinkDirs)\n const parseLine = CLI_PARSE_LINE[cli]\n\n const skilldDir = join(skillDir, '.skilld')\n const outputPath = join(skilldDir, outputFile)\n\n // Remove stale output so we don't read a leftover from a previous run\n if (existsSync(outputPath))\n unlinkSync(outputPath)\n\n // Write prompt for debugging\n writeFileSync(join(skilldDir, `PROMPT_${section}.md`), prompt)\n\n return new Promise<SectionResult>((resolve) => {\n const proc = spawn(cli, args, {\n cwd: skilldDir,\n stdio: ['pipe', 'pipe', 'pipe'],\n timeout,\n env: { ...process.env, NO_COLOR: '1' },\n shell: isWindows,\n })\n\n let buffer = ''\n let accumulatedText = ''\n let lastWriteContent = ''\n let usage: { input: number, output: number } | undefined\n let cost: number | undefined\n const rawLines: string[] = []\n\n onProgress?.({ chunk: '[starting...]', type: 'reasoning', text: '', reasoning: '', section })\n\n proc.stdin.write(prompt)\n proc.stdin.end()\n\n proc.stdout.on('data', (chunk: Buffer) => {\n buffer += chunk.toString()\n const lines = buffer.split('\\n')\n buffer = lines.pop() || ''\n\n for (const line of lines) {\n if (!line.trim())\n continue\n if (debug)\n rawLines.push(line)\n const evt = parseLine(line)\n\n if (evt.textDelta)\n accumulatedText += evt.textDelta\n if (evt.fullText)\n accumulatedText = evt.fullText\n\n if (evt.writeContent)\n lastWriteContent = evt.writeContent\n\n if (evt.toolName) {\n const hint = evt.toolHint\n ? `[${evt.toolName}: ${evt.toolHint}]`\n : `[${evt.toolName}]`\n onProgress?.({ chunk: hint, type: 'reasoning', text: '', reasoning: hint, section })\n }\n\n if (evt.usage)\n usage = evt.usage\n if (evt.cost != null)\n cost = evt.cost\n }\n })\n\n let stderr = ''\n proc.stderr.on('data', (chunk: Buffer) => {\n stderr += chunk.toString()\n })\n\n proc.on('close', (code) => {\n // Drain remaining buffer for metadata\n if (buffer.trim()) {\n const evt = parseLine(buffer)\n if (evt.textDelta)\n accumulatedText += evt.textDelta\n if (evt.fullText)\n accumulatedText = evt.fullText\n if (evt.writeContent)\n lastWriteContent = evt.writeContent\n if (evt.usage)\n usage = evt.usage\n if (evt.cost != null)\n cost = evt.cost\n }\n\n // Remove unexpected files the LLM may have written (prompt injection defense)\n // Only clean files not in the pre-existing snapshot and not our expected output\n for (const entry of readdirSync(skilldDir)) {\n if (entry !== outputFile && !preExistingFiles.has(entry)) {\n // Allow other section output files and debug prompts\n if (Object.values(SECTION_OUTPUT_FILES).includes(entry))\n continue\n if (entry.startsWith('PROMPT_') || entry === 'logs')\n continue\n try {\n unlinkSync(join(skilldDir, entry))\n }\n catch {}\n }\n }\n\n // Prefer file written by LLM, fall back to Write tool content (if denied), then accumulated stdout\n const raw = (existsSync(outputPath) ? readFileSync(outputPath, 'utf-8') : lastWriteContent || accumulatedText).trim()\n\n // Always write stderr on failure; write all logs in debug mode\n const logsDir = join(skilldDir, 'logs')\n const logName = section.toUpperCase().replace(/-/g, '_')\n if (debug || (stderr && (!raw || code !== 0))) {\n mkdirSync(logsDir, { recursive: true })\n if (stderr)\n writeFileSync(join(logsDir, `${logName}.stderr.log`), stderr)\n }\n if (debug) {\n mkdirSync(logsDir, { recursive: true })\n if (rawLines.length)\n writeFileSync(join(logsDir, `${logName}.jsonl`), rawLines.join('\\n'))\n if (raw)\n writeFileSync(join(logsDir, `${logName}.md`), raw)\n }\n\n if (!raw && code !== 0) {\n resolve({ section, content: '', wasOptimized: false, error: stderr.trim() || `CLI exited with code ${code}` })\n return\n }\n\n // Clean the section output (strip markdown fences, frontmatter, sanitize)\n const content = raw ? cleanSectionOutput(raw) : ''\n\n if (content) {\n // Write cleaned content back to the output file for debugging\n writeFileSync(outputPath, content)\n }\n\n const validator = getSectionValidator(section)\n const rawWarnings = content && validator ? validator(content) : []\n const warnings: ValidationWarning[] = rawWarnings.map(w => ({ section, warning: w.warning }))\n\n resolve({\n section,\n content,\n wasOptimized: !!content,\n warnings: warnings?.length ? warnings : undefined,\n usage,\n cost,\n })\n })\n\n proc.on('error', (err) => {\n resolve({ section, content: '', wasOptimized: false, error: err.message })\n })\n })\n}\n\n// ── Main orchestrator ────────────────────────────────────────────────\n\nexport async function optimizeDocs(opts: OptimizeDocsOptions): Promise<OptimizeResult> {\n const { packageName, skillDir, model = 'sonnet', version, hasGithub, hasReleases, hasChangelog, docFiles, docsType, hasShippedDocs, onProgress, timeout = 180000, debug, noCache, sections, customPrompt, features, pkgFiles, overheadLines } = opts\n\n const selectedSections = sections ?? ['api-changes', 'best-practices'] as SkillSection[]\n\n // Build all section prompts\n const sectionPrompts = buildAllSectionPrompts({\n packageName,\n skillDir,\n version,\n hasIssues: hasGithub,\n hasDiscussions: hasGithub,\n hasReleases,\n hasChangelog,\n docFiles,\n docsType,\n hasShippedDocs,\n customPrompt,\n features,\n pkgFiles,\n overheadLines,\n sections: selectedSections,\n })\n\n if (sectionPrompts.size === 0) {\n return { optimized: '', wasOptimized: false, error: 'No valid sections to generate' }\n }\n\n if (isPiAiModel(model)) {\n const available = new Set(getAvailablePiAiModels().map(m => m.id as OptimizeModel))\n if (!available.has(model))\n return { optimized: '', wasOptimized: false, error: `Pi model unavailable or not authenticated: ${model}` }\n }\n else if (!CLI_MODELS[model]) {\n return { optimized: '', wasOptimized: false, error: `No CLI mapping for model: ${model}` }\n }\n\n // Check per-section cache: references dir first (version-keyed), then LLM cache (prompt-hashed)\n const cachedResults: SectionResult[] = []\n const uncachedSections: Array<{ section: SkillSection, prompt: string }> = []\n\n for (const [section, prompt] of sectionPrompts) {\n if (!noCache) {\n // Check global references dir (cross-project, version-keyed)\n if (version) {\n const outputFile = SECTION_OUTPUT_FILES[section]\n const refCached = readCachedSection(packageName, version, outputFile)\n if (refCached) {\n onProgress?.({ chunk: `[${section}: cached]`, type: 'text', text: refCached, reasoning: '', section })\n cachedResults.push({ section, content: refCached, wasOptimized: true })\n continue\n }\n }\n\n // Check LLM prompt-hash cache\n const cached = getCached(prompt, model, section)\n if (cached) {\n onProgress?.({ chunk: `[${section}: cached]`, type: 'text', text: cached, reasoning: '', section })\n cachedResults.push({ section, content: cached, wasOptimized: true })\n continue\n }\n }\n uncachedSections.push({ section, prompt })\n }\n\n // Prepare .skilld/ dir and snapshot before spawns\n const skilldDir = join(skillDir, '.skilld')\n mkdirSync(skilldDir, { recursive: true })\n\n // Pre-flight: warn about broken symlinks in .skilld/ (avoids wasting tokens on missing refs)\n for (const entry of readdirSync(skilldDir)) {\n const entryPath = join(skilldDir, entry)\n try {\n if (lstatSync(entryPath).isSymbolicLink() && !existsSync(entryPath))\n onProgress?.({ chunk: `[warn: broken symlink .skilld/${entry}]`, type: 'reasoning', text: '', reasoning: '' })\n }\n catch {}\n }\n\n const preExistingFiles = new Set(readdirSync(skilldDir))\n\n // Spawn uncached sections with staggered starts to avoid rate-limit collisions\n const STAGGER_MS = 3000\n const spawnResults = uncachedSections.length > 0\n ? await Promise.allSettled(\n uncachedSections.map(({ section, prompt }, i) => {\n const outputFile = SECTION_OUTPUT_FILES[section]\n const run = () => optimizeSection({\n section,\n prompt,\n outputFile,\n skillDir,\n model,\n packageName,\n onProgress,\n timeout,\n debug,\n preExistingFiles,\n })\n // Stagger: first section starts immediately, rest delayed\n if (i === 0)\n return run()\n return delay(i * STAGGER_MS).then(run)\n }),\n )\n : []\n\n // Collect results, retry failed sections once\n const allResults: SectionResult[] = [...cachedResults]\n let totalUsage: { input: number, output: number } | undefined\n let totalCost = 0\n const retryQueue: Array<{ index: number, section: SkillSection, prompt: string }> = []\n\n for (let i = 0; i < spawnResults.length; i++) {\n const r = spawnResults[i]!\n const { section, prompt } = uncachedSections[i]!\n if (r.status === 'fulfilled' && r.value.wasOptimized) {\n allResults.push(r.value)\n if (r.value.usage) {\n totalUsage = totalUsage ?? { input: 0, output: 0 }\n totalUsage.input += r.value.usage.input\n totalUsage.output += r.value.usage.output\n }\n if (r.value.cost != null)\n totalCost += r.value.cost\n if (!noCache)\n setCache(prompt, model, section, r.value.content)\n }\n else {\n retryQueue.push({ index: i, section, prompt })\n }\n }\n\n // Retry failed sections (sequential, with rate-limit aware backoff)\n for (const { index, section, prompt } of retryQueue) {\n const prevError = getRetryError(spawnResults[index]!)\n const rateLimitDelay = parseRateLimitDelay(prevError)\n\n if (rateLimitDelay != null) {\n const waitSec = Math.max(rateLimitDelay, 5)\n onProgress?.({ chunk: `[${section}] Rate limited, waiting ${waitSec}s...`, type: 'reasoning', text: '', reasoning: '', section })\n await delay(waitSec * 1000)\n }\n else {\n onProgress?.({ chunk: `[${section}: retrying...]`, type: 'reasoning', text: '', reasoning: '', section })\n await delay(STAGGER_MS)\n }\n\n const result = await optimizeSection({\n section,\n prompt,\n outputFile: SECTION_OUTPUT_FILES[section],\n skillDir,\n model,\n packageName,\n onProgress,\n timeout,\n debug,\n preExistingFiles,\n }).catch((err: Error) => ({ section, content: '', wasOptimized: false, error: err.message }) as SectionResult)\n\n allResults.push(result)\n if (result.wasOptimized && !noCache)\n setCache(prompt, model, section, result.content)\n if (result.usage) {\n totalUsage = totalUsage ?? { input: 0, output: 0 }\n totalUsage.input += result.usage.input\n totalUsage.output += result.usage.output\n }\n if (result.cost != null)\n totalCost += result.cost\n }\n\n // Write successful sections to global references dir for cross-project reuse\n if (version) {\n const sectionFiles = allResults\n .filter(r => r.wasOptimized && r.content)\n .map(r => ({ file: SECTION_OUTPUT_FILES[r.section], content: r.content }))\n if (sectionFiles.length > 0) {\n writeSections(packageName, version, sectionFiles)\n }\n }\n\n // Merge results in SECTION_MERGE_ORDER, wrapped with comment markers\n const mergedParts: string[] = []\n for (const section of SECTION_MERGE_ORDER) {\n const result = allResults.find(r => r.section === section)\n if (result?.wasOptimized && result.content) {\n mergedParts.push(wrapSection(section, result.content))\n }\n }\n\n const optimized = mergedParts.join('\\n\\n')\n const wasOptimized = mergedParts.length > 0\n\n const usageResult = totalUsage\n ? { inputTokens: totalUsage.input, outputTokens: totalUsage.output, totalTokens: totalUsage.input + totalUsage.output }\n : undefined\n\n // Collect errors and warnings from sections\n const errors = allResults.filter(r => r.error).map(r => `${r.section}: ${r.error}`)\n const warnings = allResults.flatMap(r => r.warnings ?? []).map(w => `${w.section}: ${w.warning}`)\n\n const debugLogsDir = debug && uncachedSections.length > 0\n ? join(skillDir, '.skilld', 'logs')\n : undefined\n\n return {\n optimized,\n wasOptimized,\n error: errors.length > 0 ? errors.join('; ') : undefined,\n warnings: warnings.length > 0 ? warnings : undefined,\n finishReason: wasOptimized ? 'stop' : 'error',\n usage: usageResult,\n cost: totalCost || undefined,\n debugLogsDir,\n }\n}\n\n// ── Helpers ──────────────────────────────────────────────────────────\n\n/** Check if an error string indicates a rate limit (429) */\nfunction isRateLimitError(error: string | undefined): boolean {\n if (!error)\n return false\n return /\\b429\\b/.test(error)\n || /rate.?limit/i.test(error)\n || /exhausted.*capacity/i.test(error)\n || /quota.*reset/i.test(error)\n}\n\n/** Parse delay hint from rate limit error (e.g. \"reset after 5s\" → 5). Returns undefined if not a rate limit. */\nfunction parseRateLimitDelay(error: string | undefined): number | undefined {\n if (!error || !isRateLimitError(error))\n return undefined\n const match = error.match(/reset\\s+after\\s+(\\d+)s/i)\n return match ? Number(match[1]) : 10 // default 10s if no hint\n}\n\n/** Extract error string from a PromiseSettledResult */\nfunction getRetryError(result: PromiseSettledResult<SectionResult>): string | undefined {\n if (result.status === 'rejected')\n return String(result.reason)\n return result.value.error\n}\n\n/** Shorten absolute paths for display: /home/user/project/.claude/skills/vue/SKILL.md → .claude/.../SKILL.md */\nfunction shortenPath(p: string): string {\n const refIdx = p.indexOf('.skilld/')\n if (refIdx !== -1)\n return p.slice(refIdx + '.skilld/'.length)\n // Keep just filename for other paths\n const parts = p.split('/')\n return parts.length > 2 ? `.../${parts.slice(-2).join('/')}` : p\n}\n\n/** Replace absolute paths in a command string with shortened versions */\nfunction shortenCommand(cmd: string): string {\n return cmd.replace(/\\/[^\\s\"']+/g, (match) => {\n // Only shorten paths that look like they're inside a project\n if (match.includes('.claude/') || match.includes('.skilld/') || match.includes('node_modules/'))\n return `.../${match.split('/').slice(-2).join('/')}`\n return match\n })\n}\n\n/** Clean a single section's LLM output: strip markdown fences, frontmatter, sanitize */\nexport function cleanSectionOutput(content: string): string {\n let cleaned = content.trim()\n\n // Strip wrapping fences if output is wrapped in ```markdown, ```md, or bare ```\n // Requires matched open+close pair to avoid stripping internal code blocks\n const wrapMatch = cleaned.match(/^```(?:markdown|md)?[^\\S\\n]*\\n([\\s\\S]+)\\n```[^\\S\\n]*$/)\n if (wrapMatch) {\n const inner = wrapMatch[1]!.trim()\n // For bare ``` wrappers (no markdown/md tag), verify inner looks like section output\n const isExplicitWrapper = /^```(?:markdown|md)/.test(cleaned)\n if (isExplicitWrapper || /^##\\s/m.test(inner) || /^- (?:BREAKING|DEPRECATED|NEW): /m.test(inner)) {\n cleaned = inner\n }\n }\n\n // Normalize h1 headers to h2 — LLMs sometimes use # instead of ##\n cleaned = cleaned.replace(/^# (?!#)/gm, '## ')\n\n // Strip accidental frontmatter or leading horizontal rules\n const fmMatch = cleaned.match(/^-{3,}\\n/)\n if (fmMatch) {\n const afterOpen = fmMatch[0].length\n const closeMatch = cleaned.slice(afterOpen).match(/\\n-{3,}/)\n if (closeMatch) {\n cleaned = cleaned.slice(afterOpen + closeMatch.index! + closeMatch[0].length).trim()\n }\n else {\n cleaned = cleaned.slice(afterOpen).trim()\n }\n }\n\n // Strip preamble before first section marker (LLM reasoning, fake tool calls, code dumps)\n // Section markers: ## heading, BREAKING/DEPRECATED/NEW labels\n const firstMarker = cleaned.match(/^(##\\s|- (?:BREAKING|DEPRECATED|NEW): )/m)\n if (firstMarker?.index && firstMarker.index > 0) {\n cleaned = cleaned.slice(firstMarker.index).trim()\n }\n\n // Strip duplicate section headings (LLM echoing the format example before real content)\n // Handles headings separated by blank lines or boilerplate text\n const headingMatch = cleaned.match(/^(## .+)\\n/)\n if (headingMatch) {\n const heading = headingMatch[1]!\n const afterFirst = headingMatch[0].length\n const secondIdx = cleaned.indexOf(heading, afterFirst)\n if (secondIdx !== -1) {\n // Only strip if the gap between duplicates is small (< 200 chars of boilerplate)\n if (secondIdx - afterFirst < 200)\n cleaned = cleaned.slice(secondIdx).trim()\n }\n }\n\n // Normalize citation link text to [source] — LLMs sometimes use the path as link text\n // e.g. [./references/docs/api.md](./references/docs/api.md) or [`./references/...`](...)\n // Also handles paren-wrapped variants: ([`path`](url))\n cleaned = cleaned.replace(\n /\\(?\\[`?\\.\\/(?:\\.skilld\\/|references\\/)[^)\\]]*\\]\\(([^)]+)\\)\\)?/g,\n (match, url: string) => {\n // Only normalize if the URL points to a reference path\n if (/^\\.\\/(?:\\.skilld\\/|references\\/)/.test(url))\n return `[source](${url})`\n return match\n },\n )\n\n // Normalize source link paths: ensure .skilld/ prefix is present\n // LLMs sometimes emit [source](./docs/...) instead of [source](./.skilld/docs/...)\n cleaned = cleaned.replace(\n /\\[source\\]\\(\\.\\/((docs|issues|discussions|releases|pkg|guide)\\/)/g,\n '[source](./.skilld/$1',\n )\n\n cleaned = sanitizeMarkdown(cleaned)\n\n // Reject content that lacks any section structure — likely leaked LLM reasoning/narration\n // Valid sections contain headings (##), API change labels, or source-linked items\n if (!/^##\\s/m.test(cleaned) && !/^- (?:BREAKING|DEPRECATED|NEW): /m.test(cleaned) && !/\\[source\\]/.test(cleaned)) {\n return ''\n }\n\n return cleaned\n}\n","/**\n * Detect packages from framework presets (e.g., Nuxt modules in nuxt.config)\n * These are string literals in config arrays, not imports — the import scanner misses them.\n */\n\nimport type { PackageUsage } from './detect-imports.ts'\nimport { readFile } from 'node:fs/promises'\nimport { parseSync } from 'oxc-parser'\nimport { join } from 'pathe'\n\nconst NUXT_CONFIG_FILES = ['nuxt.config.ts', 'nuxt.config.js', 'nuxt.config.mjs']\nconst NUXT_ECOSYSTEM = ['vue', 'nitro', 'h3']\n\nasync function findNuxtConfig(cwd: string): Promise<{ path: string, content: string } | null> {\n for (const name of NUXT_CONFIG_FILES) {\n const path = join(cwd, name)\n const content = await readFile(path, 'utf8').catch(() => null)\n if (content)\n return { path, content }\n }\n return null\n}\n\n/**\n * Walk AST node to find all string values inside a `modules` array property.\n * Handles: defineNuxtConfig({ modules: [...] }) and export default { modules: [...] }\n */\nexport function extractModuleStrings(node: any): string[] {\n if (!node || typeof node !== 'object')\n return []\n\n // Found a Property with key \"modules\" and an ArrayExpression value\n if (node.type === 'Property' && !node.computed\n && (node.key?.type === 'Identifier' && node.key.name === 'modules')\n && node.value?.type === 'ArrayExpression') { return node.value.elements.filter((el: any) => el?.type === 'Literal' && typeof el.value === 'string').map((el: any) => el.value as string) }\n\n // Recurse into arrays and object values\n const results: string[] = []\n if (Array.isArray(node)) {\n for (const child of node)\n results.push(...extractModuleStrings(child))\n }\n else {\n for (const key of Object.keys(node)) {\n if (key === 'start' || key === 'end' || key === 'type')\n continue\n const val = node[key]\n if (val && typeof val === 'object')\n results.push(...extractModuleStrings(val))\n }\n }\n return results\n}\n\n/**\n * Detect Nuxt modules from nuxt.config.{ts,js,mjs}\n */\nexport async function detectNuxtModules(cwd: string): Promise<PackageUsage[]> {\n const config = await findNuxtConfig(cwd)\n if (!config)\n return []\n\n const result = parseSync(config.path, config.content)\n const modules = extractModuleStrings(result.program)\n\n // Dedupe and build results\n const seen = new Set<string>()\n const packages: PackageUsage[] = []\n\n for (const mod of modules) {\n if (!seen.has(mod)) {\n seen.add(mod)\n packages.push({ name: mod, count: 0, source: 'preset' })\n }\n }\n\n // Add core ecosystem packages\n for (const pkg of NUXT_ECOSYSTEM) {\n if (!seen.has(pkg)) {\n seen.add(pkg)\n packages.push({ name: pkg, count: 0, source: 'preset' })\n }\n }\n\n return packages\n}\n\n/**\n * Run all preset detectors and merge results\n */\nexport async function detectPresetPackages(cwd: string): Promise<PackageUsage[]> {\n // Currently only Nuxt, but extensible for other frameworks\n return detectNuxtModules(cwd)\n}\n","/**\n * Detect directly-used npm packages by scanning source files\n * Uses mlly for proper ES module parsing + tinyglobby for file discovery\n */\n\nimport { readFile } from 'node:fs/promises'\nimport { findDynamicImports, findStaticImports } from 'mlly'\nimport { glob } from 'tinyglobby'\nimport { detectPresetPackages } from './detect-presets.ts'\n\nexport interface PackageUsage {\n name: string\n count: number\n source?: 'import' | 'preset'\n}\n\nexport interface DetectResult {\n packages: PackageUsage[]\n error?: string\n}\n\nconst PATTERNS = ['**/*.{ts,js,vue,mjs,cjs,tsx,jsx,mts,cts}']\nconst IGNORE = ['**/node_modules/**', '**/dist/**', '**/.nuxt/**', '**/.output/**', '**/coverage/**']\n\nfunction addPackage(counts: Map<string, number>, specifier: string | undefined) {\n if (!specifier || specifier.startsWith('.') || specifier.startsWith('/'))\n return\n\n // Extract package name (handle subpaths like 'pkg/subpath')\n const name = specifier.startsWith('@')\n ? specifier.split('/').slice(0, 2).join('/')\n : specifier.split('/')[0]!\n\n if (!isNodeBuiltin(name)) {\n counts.set(name, (counts.get(name) || 0) + 1)\n }\n}\n\n/**\n * Scan source files to detect all directly-imported npm packages\n * Async with gitignore support for proper spinner animation\n */\nexport async function detectImportedPackages(cwd: string = process.cwd()): Promise<DetectResult> {\n try {\n const counts = new Map<string, number>()\n\n const files = await glob(PATTERNS, {\n cwd,\n ignore: IGNORE,\n absolute: true,\n expandDirectories: false,\n })\n\n await Promise.all(files.map(async (file) => {\n const content = await readFile(file, 'utf8')\n\n // Static: import x from 'pkg'\n for (const imp of findStaticImports(content)) {\n addPackage(counts, imp.specifier)\n }\n\n // Dynamic: import('pkg') - expression is the string literal\n for (const imp of findDynamicImports(content)) {\n // expression includes quotes, extract string value\n const match = imp.expression.match(/^['\"]([^'\"]+)['\"]$/)\n if (match)\n addPackage(counts, match[1]!)\n }\n }))\n\n // Sort by usage count (descending), then alphabetically\n const packages: PackageUsage[] = Array.from(counts.entries(), ([name, count]) => ({ name, count, source: 'import' as const }))\n .sort((a, b) => b.count - a.count || a.name.localeCompare(b.name))\n\n // Merge preset-detected packages (imports take priority)\n const presets = await detectPresetPackages(cwd)\n const importNames = new Set(packages.map(p => p.name))\n for (const preset of presets) {\n if (!importNames.has(preset.name))\n packages.push(preset)\n }\n\n return { packages }\n }\n catch (err) {\n return { packages: [], error: String(err) }\n }\n}\n\nconst NODE_BUILTINS = new Set([\n 'assert',\n 'async_hooks',\n 'buffer',\n 'child_process',\n 'cluster',\n 'console',\n 'constants',\n 'crypto',\n 'dgram',\n 'diagnostics_channel',\n 'dns',\n 'domain',\n 'events',\n 'fs',\n 'http',\n 'http2',\n 'https',\n 'inspector',\n 'module',\n 'net',\n 'os',\n 'path',\n 'perf_hooks',\n 'process',\n 'punycode',\n 'querystring',\n 'readline',\n 'repl',\n 'sea',\n 'sqlite',\n 'stream',\n 'string_decoder',\n 'sys',\n 'test',\n 'timers',\n 'tls',\n 'trace_events',\n 'tty',\n 'url',\n 'util',\n 'v8',\n 'vm',\n 'wasi',\n 'worker_threads',\n 'zlib',\n])\n\nfunction isNodeBuiltin(pkg: string): boolean {\n const base = pkg.startsWith('node:') ? pkg.slice(5) : pkg\n return NODE_BUILTINS.has(base.split('/')[0]!)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,MAAaA,QAAM;AACnB,MAAaC,YAAU;AAEvB,MAAaC,WAAwC;CACnD,MAAM;EAAE,OAAO;EAAQ,MAAM;EAAY,MAAM;EAAiC;CAChF,QAAQ;EAAE,OAAO;EAAU,MAAM;EAAc,MAAM;EAA2B;CAChF,OAAO;EAAE,OAAO;EAAS,MAAM;EAAa,MAAM;EAA6B,aAAa;;CAC7F;AAED,SAAgBC,YAAU,OAAe,UAAkB,aAAiC;AAW1F,QAAO;EACL;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EAlBmB;GAEnB;GACA;GACA;GACA;GACA;GAGD,CAAC,KAAK,IAAI;EAWT;EACA;EACA;EACA;EACA,GAAG,YAAY,SAAQ,M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I"}
@@ -1,4 +1,3 @@
1
- import { n as cleanSectionOutput } from "./agent.mjs";
2
1
  import "./config.mjs";
3
2
  import "./package-json.mjs";
4
3
  import "./prepare.mjs";
@@ -10,6 +9,8 @@ import "./shared.mjs";
10
9
  import "./sources.mjs";
11
10
  import "./detect.mjs";
12
11
  import { c as SECTION_OUTPUT_FILES, d as extractMarkedSections, f as getSectionValidator, m as wrapSection, s as SECTION_MERGE_ORDER } from "./prompts.mjs";
12
+ import { n as cleanSectionOutput } from "./agent.mjs";
13
+ import "./libs/@sinclair/typebox.mjs";
13
14
  import "./lockfile.mjs";
14
15
  import { i as iterateSkills } from "./skills.mjs";
15
16
  import { join, relative, resolve } from "pathe";
@@ -1 +1 @@
1
- {"version":3,"file":"assemble.mjs","names":[],"sources":["../../src/commands/assemble.ts"],"sourcesContent":["/**\n * `skilld assemble` — merge pasted LLM output back into SKILL.md\n *\n * Auto-discovers skill directories with pending output files when run without arguments.\n */\n\nimport type { SkillSection } from '../agent/index.ts'\nimport { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'\nimport * as p from '@clack/prompts'\nimport { defineCommand } from 'citty'\nimport { join, relative, resolve } from 'pathe'\nimport { cleanSectionOutput } from '../agent/clis/index.ts'\nimport {\n extractMarkedSections,\n getSectionValidator,\n SECTION_MERGE_ORDER,\n SECTION_OUTPUT_FILES,\n wrapSection,\n} from '../agent/index.ts'\nimport { iterateSkills } from '../core/skills.ts'\n\nconst OUTPUT_FILE_SET = new Set(Object.values(SECTION_OUTPUT_FILES))\n\n/**\n * Find installed skill dirs that have pending section output files.\n */\nfunction discoverSkillDirsWithOutputs(): string[] {\n const dirs: string[] = []\n for (const skill of iterateSkills({})) {\n if (readdirSync(skill.dir).some(f => OUTPUT_FILE_SET.has(f)))\n dirs.push(skill.dir)\n }\n return dirs\n}\n\nexport async function assembleCommand(dir: string | undefined): Promise<void> {\n const cwd = process.cwd()\n\n let dirs: string[]\n if (dir) {\n dirs = [resolve(cwd, dir)]\n }\n else {\n // Check cwd first — if it has SKILL.md + output files, use it\n if (existsSync(join(cwd, 'SKILL.md'))\n && readdirSync(cwd).some(f => OUTPUT_FILE_SET.has(f))) {\n dirs = [cwd]\n }\n else {\n dirs = discoverSkillDirsWithOutputs()\n if (dirs.length === 0) {\n p.log.error('No skill directories with output files found. Run `skilld add` first.')\n return\n }\n }\n }\n\n for (const targetDir of dirs)\n assembleDir(targetDir, cwd)\n}\n\nfunction assembleDir(targetDir: string, cwd: string): void {\n if (!existsSync(targetDir)) {\n p.log.error(`Directory not found: ${targetDir}`)\n return\n }\n\n const skillMdPath = join(targetDir, 'SKILL.md')\n if (!existsSync(skillMdPath)) {\n p.log.error(`No SKILL.md found in ${targetDir}`)\n return\n }\n\n const existingSkillMd = readFileSync(skillMdPath, 'utf-8')\n\n // Find and read section output files\n const sections: Array<{ section: SkillSection, content: string }> = []\n const warnings: string[] = []\n\n for (const [section, outputFile] of Object.entries(SECTION_OUTPUT_FILES) as Array<[SkillSection, string]>) {\n const filePath = join(targetDir, outputFile)\n if (!existsSync(filePath))\n continue\n\n const raw = readFileSync(filePath, 'utf-8').trim()\n if (!raw) {\n p.log.warn(`Empty file: ${outputFile}`)\n continue\n }\n\n const cleaned = cleanSectionOutput(raw)\n if (!cleaned) {\n const missing: string[] = []\n if (!/^##\\s/m.test(raw))\n missing.push('h2 heading (## ...)')\n if (!/^- (?:BREAKING|DEPRECATED|NEW): /m.test(raw))\n missing.push('change label (- BREAKING/DEPRECATED/NEW: ...)')\n if (!/\\[source\\]/.test(raw))\n missing.push('[source] link')\n p.log.warn(`${outputFile}: content rejected — missing ${missing.join(', ')}`)\n continue\n }\n\n const validator = getSectionValidator(section)\n if (validator) {\n for (const w of validator(cleaned))\n warnings.push(`${section}: ${w.warning}`)\n }\n\n sections.push({ section, content: cleaned })\n p.log.success(`Loaded ${outputFile}`)\n }\n\n if (sections.length === 0) {\n p.log.warn(`No section output files in ${relative(cwd, targetDir)}. Expected: ${Object.values(SECTION_OUTPUT_FILES).join(', ')}`)\n return\n }\n\n for (const w of warnings)\n p.log.warn(`\\x1B[33m${w}\\x1B[0m`)\n\n // Wrap each section with comment markers\n const wrappedSections: Array<{ section: SkillSection, wrapped: string }> = []\n for (const section of SECTION_MERGE_ORDER) {\n const result = sections.find(s => s.section === section)\n if (result)\n wrappedSections.push({ section, wrapped: wrapSection(section, result.content) })\n }\n\n // Try marker-based replacement first (re-assembly of previously assembled SKILL.md)\n const existingMarkers = extractMarkedSections(existingSkillMd)\n let newSkillMd: string\n\n if (existingMarkers.size > 0) {\n // Replace existing marked sections in-place, append new ones at the end\n newSkillMd = existingSkillMd\n // Process in reverse offset order to preserve indices\n const replacements = wrappedSections\n .filter(s => existingMarkers.has(s.section))\n .sort((a, b) => existingMarkers.get(b.section)!.start - existingMarkers.get(a.section)!.start)\n for (const { section, wrapped } of replacements) {\n const { start, end } = existingMarkers.get(section)!\n newSkillMd = newSkillMd.slice(0, start) + wrapped + newSkillMd.slice(end)\n }\n // Append sections that don't have existing markers\n const newSections = wrappedSections.filter(s => !existingMarkers.has(s.section))\n if (newSections.length > 0)\n newSkillMd = `${newSkillMd.trimEnd()}\\n\\n${newSections.map(s => s.wrapped).join('\\n\\n')}\\n`\n }\n else {\n // First assembly — find header boundary and append all sections\n const body = wrappedSections.map(s => s.wrapped).join('\\n\\n')\n const fmEnd = existingSkillMd.indexOf('\\n---\\n', 4)\n const afterFm = fmEnd !== -1 ? existingSkillMd.slice(fmEnd + 5) : existingSkillMd\n\n const firstLlmHeading = body.match(/^## .+$/m)?.[0]\n let headerPart = afterFm\n if (firstLlmHeading) {\n const idx = afterFm.indexOf(firstLlmHeading)\n if (idx !== -1)\n headerPart = afterFm.slice(0, idx)\n }\n\n const fmPart = fmEnd !== -1 ? existingSkillMd.slice(0, fmEnd + 5) : ''\n const cleanHeader = headerPart.trimEnd()\n newSkillMd = fmPart\n ? `${fmPart}${cleanHeader}\\n\\n${body}\\n`\n : `${cleanHeader}\\n\\n${body}\\n`\n }\n\n writeFileSync(skillMdPath, newSkillMd)\n p.log.success(`Updated ${relative(cwd, skillMdPath)} with ${sections.length} section(s)`)\n}\n\nexport const assembleCommandDef = defineCommand({\n meta: { name: 'assemble', description: 'Merge enhancement output files into SKILL.md' },\n args: {\n dir: {\n type: 'positional',\n description: 'Skill directory with output files (auto-discovers installed skills)',\n required: false,\n },\n },\n async run({ args }) {\n await assembleCommand(args.dir)\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAqBA,MAAM,kBAAkB,IAAI,IAAI,OAAO,OAAO,qBAAqB,CAAC;;;;AAKpE,SAAS,+BAAyC;CAChD,MAAM,OAAiB,EAAE;AACzB,MAAK,MAAM,SAAS,cAAc,EAAE,CAAC,CACnC,KAAI,YAAY,MAAM,IAAI,CAAC,MAAK,MAAK,gBAAgB,IAAI,EAAE,CAAC,CAC1D,MAAK,KAAK,MAAM,IAAI;AAExB,QAAO;;AAGT,eAAsB,gBAAgB,KAAwC;CAC5E,MAAM,MAAM,QAAQ,KAAK;CAEzB,IAAI;AACJ,KAAI,IACF,QAAO,CAAC,QAAQ,KAAK,IAAI,CAAC;UAItB,WAAW,KAAK,KAAK,WAAW,CAAC,IAChC,YAAY,IAAI,CAAC,MAAK,MAAK,gBAAgB,IAAI,EAAE,CAAC,CACrD,QAAO,CAAC,IAAI;MAET;AACH,SAAO,8BAA8B;AACrC,MAAI,KAAK,WAAW,GAAG;AACrB,KAAE,IAAI,MAAM,wEAAwE;AACpF;;;AAKN,MAAK,MAAM,aAAa,KACtB,aAAY,WAAW,IAAI;;AAG/B,SAAS,YAAY,WAAmB,KAAmB;AACzD,KAAI,CAAC,WAAW,UAAU,EAAE;AAC1B,IAAE,IAAI,MAAM,wBAAwB,YAAY;AAChD;;CAGF,MAAM,cAAc,KAAK,WAAW,WAAW;AAC/C,KAAI,CAAC,WAAW,YAAY,EAAE;AAC5B,IAAE,IAAI,MAAM,wBAAwB,YAAY;AAChD;;CAGF,MAAM,kBAAkB,aAAa,aAAa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AK,MACnC,GAAG,YAAY,MAAM,KAAK;;AAGhC,eAAc,aAAa,WAAW;AACtC,GAAE,IAAI,QAAQ,WAAW,SAAS,KAAK,YAAY,CAAC,QAAQ,SAAS,OAAO,aAAa;;AAG3F,MAAa,qBAAqB,cAAc;CAC9C,MAAM;EAAE,MAAM;EAAY,aAAa;EAAgD;CACvF,MAAM,EACJ,KAAK;EACH,MAAM;EACN,aAAa;EACb,UAAU;EACX,EACF;CACD,MAAM,IAAI,EAAE,QAAQ;AAClB,QAAM,gBAAgB,KAAK,IAAI;;CAElC,CAAC"}
1
+ {"version":3,"file":"assemble.mjs","names":[],"sources":["../../src/commands/assemble.ts"],"sourcesContent":["/**\n * `skilld assemble` — merge pasted LLM output back into SKILL.md\n *\n * Auto-discovers skill directories with pending output files when run without arguments.\n */\n\nimport type { SkillSection } from '../agent/index.ts'\nimport { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'\nimport * as p from '@clack/prompts'\nimport { defineCommand } from 'citty'\nimport { join, relative, resolve } from 'pathe'\nimport { cleanSectionOutput } from '../agent/clis/index.ts'\nimport {\n extractMarkedSections,\n getSectionValidator,\n SECTION_MERGE_ORDER,\n SECTION_OUTPUT_FILES,\n wrapSection,\n} from '../agent/index.ts'\nimport { iterateSkills } from '../core/skills.ts'\n\nconst OUTPUT_FILE_SET = new Set(Object.values(SECTION_OUTPUT_FILES))\n\n/**\n * Find installed skill dirs that have pending section output files.\n */\nfunction discoverSkillDirsWithOutputs(): string[] {\n const dirs: string[] = []\n for (const skill of iterateSkills({})) {\n if (readdirSync(skill.dir).some(f => OUTPUT_FILE_SET.has(f)))\n dirs.push(skill.dir)\n }\n return dirs\n}\n\nexport async function assembleCommand(dir: string | undefined): Promise<void> {\n const cwd = process.cwd()\n\n let dirs: string[]\n if (dir) {\n dirs = [resolve(cwd, dir)]\n }\n else {\n // Check cwd first — if it has SKILL.md + output files, use it\n if (existsSync(join(cwd, 'SKILL.md'))\n && readdirSync(cwd).some(f => OUTPUT_FILE_SET.has(f))) {\n dirs = [cwd]\n }\n else {\n dirs = discoverSkillDirsWithOutputs()\n if (dirs.length === 0) {\n p.log.error('No skill directories with output files found. Run `skilld add` first.')\n return\n }\n }\n }\n\n for (const targetDir of dirs)\n assembleDir(targetDir, cwd)\n}\n\nfunction assembleDir(targetDir: string, cwd: string): void {\n if (!existsSync(targetDir)) {\n p.log.error(`Directory not found: ${targetDir}`)\n return\n }\n\n const skillMdPath = join(targetDir, 'SKILL.md')\n if (!existsSync(skillMdPath)) {\n p.log.error(`No SKILL.md found in ${targetDir}`)\n return\n }\n\n const existingSkillMd = readFileSync(skillMdPath, 'utf-8')\n\n // Find and read section output files\n const sections: Array<{ section: SkillSection, content: string }> = []\n const warnings: string[] = []\n\n for (const [section, outputFile] of Object.entries(SECTION_OUTPUT_FILES) as Array<[SkillSection, string]>) {\n const filePath = join(targetDir, outputFile)\n if (!existsSync(filePath))\n continue\n\n const raw = readFileSync(filePath, 'utf-8').trim()\n if (!raw) {\n p.log.warn(`Empty file: ${outputFile}`)\n continue\n }\n\n const cleaned = cleanSectionOutput(raw)\n if (!cleaned) {\n const missing: string[] = []\n if (!/^##\\s/m.test(raw))\n missing.push('h2 heading (## ...)')\n if (!/^- (?:BREAKING|DEPRECATED|NEW): /m.test(raw))\n missing.push('change label (- BREAKING/DEPRECATED/NEW: ...)')\n if (!/\\[source\\]/.test(raw))\n missing.push('[source] link')\n p.log.warn(`${outputFile}: content rejected — missing ${missing.join(', ')}`)\n continue\n }\n\n const validator = getSectionValidator(section)\n if (validator) {\n for (const w of validator(cleaned))\n warnings.push(`${section}: ${w.warning}`)\n }\n\n sections.push({ section, content: cleaned })\n p.log.success(`Loaded ${outputFile}`)\n }\n\n if (sections.length === 0) {\n p.log.warn(`No section output files in ${relative(cwd, targetDir)}. Expected: ${Object.values(SECTION_OUTPUT_FILES).join(', ')}`)\n return\n }\n\n for (const w of warnings)\n p.log.warn(`\\x1B[33m${w}\\x1B[0m`)\n\n // Wrap each section with comment markers\n const wrappedSections: Array<{ section: SkillSection, wrapped: string }> = []\n for (const section of SECTION_MERGE_ORDER) {\n const result = sections.find(s => s.section === section)\n if (result)\n wrappedSections.push({ section, wrapped: wrapSection(section, result.content) })\n }\n\n // Try marker-based replacement first (re-assembly of previously assembled SKILL.md)\n const existingMarkers = extractMarkedSections(existingSkillMd)\n let newSkillMd: string\n\n if (existingMarkers.size > 0) {\n // Replace existing marked sections in-place, append new ones at the end\n newSkillMd = existingSkillMd\n // Process in reverse offset order to preserve indices\n const replacements = wrappedSections\n .filter(s => existingMarkers.has(s.section))\n .sort((a, b) => existingMarkers.get(b.section)!.start - existingMarkers.get(a.section)!.start)\n for (const { section, wrapped } of replacements) {\n const { start, end } = existingMarkers.get(section)!\n newSkillMd = newSkillMd.slice(0, start) + wrapped + newSkillMd.slice(end)\n }\n // Append sections that don't have existing markers\n const newSections = wrappedSections.filter(s => !existingMarkers.has(s.section))\n if (newSections.length > 0)\n newSkillMd = `${newSkillMd.trimEnd()}\\n\\n${newSections.map(s => s.wrapped).join('\\n\\n')}\\n`\n }\n else {\n // First assembly — find header boundary and append all sections\n const body = wrappedSections.map(s => s.wrapped).join('\\n\\n')\n const fmEnd = existingSkillMd.indexOf('\\n---\\n', 4)\n const afterFm = fmEnd !== -1 ? existingSkillMd.slice(fmEnd + 5) : existingSkillMd\n\n const firstLlmHeading = body.match(/^## .+$/m)?.[0]\n let headerPart = afterFm\n if (firstLlmHeading) {\n const idx = afterFm.indexOf(firstLlmHeading)\n if (idx !== -1)\n headerPart = afterFm.slice(0, idx)\n }\n\n const fmPart = fmEnd !== -1 ? existingSkillMd.slice(0, fmEnd + 5) : ''\n const cleanHeader = headerPart.trimEnd()\n newSkillMd = fmPart\n ? `${fmPart}${cleanHeader}\\n\\n${body}\\n`\n : `${cleanHeader}\\n\\n${body}\\n`\n }\n\n writeFileSync(skillMdPath, newSkillMd)\n p.log.success(`Updated ${relative(cwd, skillMdPath)} with ${sections.length} section(s)`)\n}\n\nexport const assembleCommandDef = defineCommand({\n meta: { name: 'assemble', description: 'Merge enhancement output files into SKILL.md' },\n args: {\n dir: {\n type: 'positional',\n description: 'Skill directory with output files (auto-discovers installed skills)',\n required: false,\n },\n },\n async run({ args }) {\n await assembleCommand(args.dir)\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAqBA,MAAM,kBAAkB,IAAI,IAAI,OAAO,OAAO,qBAAqB,CAAC;;;;AAKpE,SAAS,+BAAyC;CAChD,MAAM,OAAiB,EAAE;AACzB,MAAK,MAAM,SAAS,cAAc,EAAE,CAAC,CACnC,KAAI,YAAY,MAAM,IAAI,CAAC,MAAK,MAAK,gBAAgB,IAAI,EAAE,CAAC,CAC1D,MAAK,KAAK,MAAM,IAAI;AAExB,QAAO;;AAGT,eAAsB,gBAAgB,KAAwC;CAC5E,MAAM,MAAM,QAAQ,KAAK;CAEzB,IAAI;AACJ,KAAI,IACF,QAAO,CAAC,QAAQ,KAAK,IAAI,CAAC;UAItB,WAAW,KAAK,KAAK,WAAW,CAAC,IAChC,YAAY,IAAI,CAAC,MAAK,MAAK,gBAAgB,IAAI,EAAE,CAAC,CACrD,QAAO,CAAC,IAAI;MAET;AACH,SAAO,8BAA8B;AACrC,MAAI,KAAK,WAAW,GAAG;AACrB,KAAE,IAAI,MAAM,wEAAwE;AACpF;;;AAKN,MAAK,MAAM,aAAa,KACtB,aAAY,WAAW,IAAI;;AAG/B,SAAS,YAAY,WAAmB,KAAmB;AACzD,KAAI,CAAC,WAAW,UAAU,EAAE;AAC1B,IAAE,IAAI,MAAM,wBAAwB,YAAY;AAChD;;CAGF,MAAM,cAAc,KAAK,WAAW,WAAW;AAC/C,KAAI,CAAC,WAAW,YAAY,EAAE;AAC5B,IAAE,IAAI,MAAM,wBAAwB,YAAY;AAChD;;CAGF,MAAM,kBAAkB,aAAa,aAAa,QAAQ;CAG1D,MAAM,WAA8D,EAAE;CACtE,MAAM,WAAqB,EAAE;AAE7B,MAAK,MAAM,CAAC,SA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