skilld 1.7.3 → 2.0.0

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 (161) hide show
  1. package/dist/_chunks/add.mjs +66 -0
  2. package/dist/_chunks/add.mjs.map +1 -0
  3. package/dist/_chunks/agent-prompt.mjs +88 -0
  4. package/dist/_chunks/agent-prompt.mjs.map +1 -0
  5. package/dist/_chunks/agent.mjs +737 -619
  6. package/dist/_chunks/agent.mjs.map +1 -1
  7. package/dist/_chunks/args.mjs +42 -0
  8. package/dist/_chunks/args.mjs.map +1 -0
  9. package/dist/_chunks/assemble.mjs +11 -8
  10. package/dist/_chunks/assemble.mjs.map +1 -1
  11. package/dist/_chunks/author.mjs +77 -131
  12. package/dist/_chunks/author.mjs.map +1 -1
  13. package/dist/_chunks/cache.mjs +320 -54
  14. package/dist/_chunks/cache.mjs.map +1 -1
  15. package/dist/_chunks/cache2.mjs +7 -6
  16. package/dist/_chunks/cache2.mjs.map +1 -1
  17. package/dist/_chunks/client.mjs +117 -0
  18. package/dist/_chunks/client.mjs.map +1 -0
  19. package/dist/_chunks/core.mjs +7 -4
  20. package/dist/_chunks/detect.mjs +54 -44
  21. package/dist/_chunks/detect.mjs.map +1 -1
  22. package/dist/_chunks/eject.mjs +69 -0
  23. package/dist/_chunks/eject.mjs.map +1 -0
  24. package/dist/_chunks/embedding-cache2.mjs +2 -2
  25. package/dist/_chunks/env.mjs +19 -0
  26. package/dist/_chunks/env.mjs.map +1 -0
  27. package/dist/_chunks/install-many.mjs +376 -0
  28. package/dist/_chunks/install-many.mjs.map +1 -0
  29. package/dist/_chunks/install.mjs +86 -371
  30. package/dist/_chunks/install.mjs.map +1 -1
  31. package/dist/_chunks/intro.mjs +63 -0
  32. package/dist/_chunks/intro.mjs.map +1 -0
  33. package/dist/_chunks/list.mjs +2 -2
  34. package/dist/_chunks/list.mjs.map +1 -1
  35. package/dist/_chunks/lockfile.mjs +31 -7
  36. package/dist/_chunks/lockfile.mjs.map +1 -1
  37. package/dist/_chunks/login.mjs +233 -0
  38. package/dist/_chunks/login.mjs.map +1 -0
  39. package/dist/_chunks/logout.mjs +27 -0
  40. package/dist/_chunks/logout.mjs.map +1 -0
  41. package/dist/_chunks/map.mjs +11 -0
  42. package/dist/_chunks/map.mjs.map +1 -0
  43. package/dist/_chunks/markdown.mjs +79 -54
  44. package/dist/_chunks/markdown.mjs.map +1 -1
  45. package/dist/_chunks/menu.mjs +33 -0
  46. package/dist/_chunks/menu.mjs.map +1 -0
  47. package/dist/_chunks/model-picker.mjs +61 -0
  48. package/dist/_chunks/model-picker.mjs.map +1 -0
  49. package/dist/_chunks/monorepo.mjs +73 -0
  50. package/dist/_chunks/monorepo.mjs.map +1 -0
  51. package/dist/_chunks/package-json.mjs.map +1 -1
  52. package/dist/_chunks/paths.mjs +47 -0
  53. package/dist/_chunks/paths.mjs.map +1 -0
  54. package/dist/_chunks/pipeline.mjs +985 -0
  55. package/dist/_chunks/pipeline.mjs.map +1 -0
  56. package/dist/_chunks/pool2.mjs +2 -2
  57. package/dist/_chunks/portable.mjs +151 -0
  58. package/dist/_chunks/portable.mjs.map +1 -0
  59. package/dist/_chunks/prepare-hook.mjs +2 -0
  60. package/dist/_chunks/prepare-hook2.mjs +61 -0
  61. package/dist/_chunks/prepare-hook2.mjs.map +1 -0
  62. package/dist/_chunks/prepare.mjs +47 -3
  63. package/dist/_chunks/prepare.mjs.map +1 -1
  64. package/dist/_chunks/prepare2.mjs +9 -8
  65. package/dist/_chunks/prepare2.mjs.map +1 -1
  66. package/dist/_chunks/prompts.mjs +784 -26
  67. package/dist/_chunks/prompts.mjs.map +1 -1
  68. package/dist/_chunks/pull.mjs +219 -0
  69. package/dist/_chunks/pull.mjs.map +1 -0
  70. package/dist/_chunks/regex.mjs +19 -0
  71. package/dist/_chunks/regex.mjs.map +1 -0
  72. package/dist/_chunks/retriv.mjs +2 -171
  73. package/dist/_chunks/retriv2.mjs +159 -0
  74. package/dist/_chunks/retriv2.mjs.map +1 -0
  75. package/dist/_chunks/sanitize.mjs +12 -9
  76. package/dist/_chunks/sanitize.mjs.map +1 -1
  77. package/dist/_chunks/search-helpers.mjs +9 -8
  78. package/dist/_chunks/search-helpers.mjs.map +1 -1
  79. package/dist/_chunks/search-interactive.mjs +23 -20
  80. package/dist/_chunks/search-interactive.mjs.map +1 -1
  81. package/dist/_chunks/search.mjs +3 -4
  82. package/dist/_chunks/search.mjs.map +1 -1
  83. package/dist/_chunks/{sources.mjs → semver.mjs} +1128 -838
  84. package/dist/_chunks/semver.mjs.map +1 -0
  85. package/dist/_chunks/skill-installer.mjs +2 -0
  86. package/dist/_chunks/skill-installer2.mjs +154 -0
  87. package/dist/_chunks/skill-installer2.mjs.map +1 -0
  88. package/dist/_chunks/skills.mjs +12 -12
  89. package/dist/_chunks/skills.mjs.map +1 -1
  90. package/dist/_chunks/store.mjs +107 -0
  91. package/dist/_chunks/store.mjs.map +1 -0
  92. package/dist/_chunks/sync.mjs +761 -1349
  93. package/dist/_chunks/sync.mjs.map +1 -1
  94. package/dist/_chunks/sync2.mjs +2 -3
  95. package/dist/_chunks/telemetry.mjs +26 -0
  96. package/dist/_chunks/telemetry.mjs.map +1 -0
  97. package/dist/_chunks/uninstall.mjs +15 -13
  98. package/dist/_chunks/uninstall.mjs.map +1 -1
  99. package/dist/_chunks/update.mjs +171 -0
  100. package/dist/_chunks/update.mjs.map +1 -0
  101. package/dist/_chunks/upload.mjs +4 -4
  102. package/dist/_chunks/validate.mjs +1 -1
  103. package/dist/_chunks/version.mjs +16 -27
  104. package/dist/_chunks/version.mjs.map +1 -1
  105. package/dist/_chunks/whoami.mjs +21 -0
  106. package/dist/_chunks/whoami.mjs.map +1 -0
  107. package/dist/_chunks/wizard.mjs +2 -190
  108. package/dist/_chunks/wizard2.mjs +200 -0
  109. package/dist/_chunks/wizard2.mjs.map +1 -0
  110. package/dist/cli.mjs +77 -59
  111. package/dist/cli.mjs.map +1 -1
  112. package/dist/prepare.mjs +5 -4
  113. package/dist/prepare.mjs.map +1 -1
  114. package/dist/retriv/worker.d.mts +5 -1
  115. package/dist/retriv/worker.d.mts.map +1 -1
  116. package/dist/retriv/worker.mjs +1 -1
  117. package/package.json +20 -29
  118. package/dist/_chunks/author-group.mjs +0 -17
  119. package/dist/_chunks/author-group.mjs.map +0 -1
  120. package/dist/_chunks/cli-helpers.mjs +0 -335
  121. package/dist/_chunks/cli-helpers.mjs.map +0 -1
  122. package/dist/_chunks/cli-helpers2.mjs +0 -2
  123. package/dist/_chunks/config.mjs +0 -122
  124. package/dist/_chunks/config.mjs.map +0 -1
  125. package/dist/_chunks/index.d.mts +0 -151
  126. package/dist/_chunks/index.d.mts.map +0 -1
  127. package/dist/_chunks/index2.d.mts +0 -44
  128. package/dist/_chunks/index2.d.mts.map +0 -1
  129. package/dist/_chunks/index3.d.mts +0 -589
  130. package/dist/_chunks/index3.d.mts.map +0 -1
  131. package/dist/_chunks/prefix.mjs +0 -108
  132. package/dist/_chunks/prefix.mjs.map +0 -1
  133. package/dist/_chunks/retriv.mjs.map +0 -1
  134. package/dist/_chunks/setup.mjs +0 -17
  135. package/dist/_chunks/setup.mjs.map +0 -1
  136. package/dist/_chunks/shared.mjs +0 -503
  137. package/dist/_chunks/shared.mjs.map +0 -1
  138. package/dist/_chunks/skill.mjs +0 -329
  139. package/dist/_chunks/skill.mjs.map +0 -1
  140. package/dist/_chunks/sources.mjs.map +0 -1
  141. package/dist/_chunks/sync-registry.mjs +0 -59
  142. package/dist/_chunks/sync-registry.mjs.map +0 -1
  143. package/dist/_chunks/sync-shared.mjs +0 -2
  144. package/dist/_chunks/sync-shared2.mjs +0 -1020
  145. package/dist/_chunks/sync-shared2.mjs.map +0 -1
  146. package/dist/_chunks/types.d.mts +0 -88
  147. package/dist/_chunks/types.d.mts.map +0 -1
  148. package/dist/_chunks/wizard.mjs.map +0 -1
  149. package/dist/agent/index.d.mts +0 -346
  150. package/dist/agent/index.d.mts.map +0 -1
  151. package/dist/agent/index.mjs +0 -5
  152. package/dist/cache/index.d.mts +0 -2
  153. package/dist/cache/index.mjs +0 -4
  154. package/dist/index.d.mts +0 -5
  155. package/dist/index.mjs +0 -5
  156. package/dist/retriv/index.d.mts +0 -3
  157. package/dist/retriv/index.mjs +0 -2
  158. package/dist/sources/index.d.mts +0 -2
  159. package/dist/sources/index.mjs +0 -3
  160. package/dist/types.d.mts +0 -4
  161. package/dist/types.mjs +0 -1
@@ -1 +1 @@
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 *\n * OAuth providers are blocked by default. Consumer subscription OAuth\n * impersonates official CLI clients and violates provider ToS, risking\n * account bans. Use API keys or native CLI tools (claude, gemini, codex).\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 { join } from 'pathe'\nimport { Type } from 'typebox'\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// ── Blocked OAuth providers ──────────────────────────────────────────\n// These providers use consumer subscription OAuth in ways that violate\n// the service's ToS and are known to result in account bans/revocations.\n// API key access remains fully supported for these services.\nconst BLOCKED_OAUTH_PROVIDERS = new Set([\n 'google-antigravity', // confirmed account bans\n 'google-gemini-cli', // same enforcement posture as Antigravity\n 'github-copilot', // Microsoft restricts to approved integrations only\n 'anthropic', // use `claude` CLI or ANTHROPIC_API_KEY instead\n 'openai-codex', // use `codex` CLI or OPENAI_API_KEY instead\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 (returns null for blocked providers) */\nfunction resolveOAuthProviderId(modelProvider: string): string | null {\n const oauthId = OAUTH_PROVIDER_OVERRIDES[modelProvider] ?? modelProvider\n // Block providers known to ban accounts for unauthorized OAuth usage\n if (BLOCKED_OAUTH_PROVIDERS.has(oauthId))\n return 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 (excludes blocked providers) */\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\n .filter((p: any) => !BLOCKED_OAUTH_PROVIDERS.has(p.id))\n .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;EAAM;CAC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ildArgs","parseEvent","adapter","STATIC_REGEX_1","buildArgs","parseEvent","adapter","getEnvApiKey","STATIC_REGEX_1","STATIC_REGEX_2","STATIC_REGEX_3","STATIC_REGEX_5","resolve","STATIC_REGEX_1","STATIC_REGEX_2","STATIC_REGEX_5","STATIC_REGEX_1","STATIC_REGEX_2","STATIC_REGEX_3","claudeAdapter","geminiAdapter","codexAdapter","agents","delay"],"sources":["../../src/agent/clis/model-registry.ts","../../src/agent/clis/types.ts","../../src/agent/clis/claude.ts","../../src/agent/clis/codex.ts","../../src/agent/clis/gemini.ts","../../src/agent/clis/pi-ai-auth.ts","../../src/agent/clis/pi-ai-models.ts","../../src/agent/clis/pi-ai-tools.ts","../../src/agent/clis/pi-ai-runner.ts","../../src/agent/clis/clean-output.ts","../../src/agent/clis/cli-progress.ts","../../src/agent/clis/index.ts","../../src/agent/detect-presets.ts","../../src/agent/detect-imports.ts","../../src/agent/clis/cli-cache.ts","../../src/agent/clis/runner.ts","../../src/agent/clis/executors.ts","../../src/agent/llm-enhancer.ts"],"sourcesContent":["/**\n * Model registry: sources display names from @earendil-works/pi-ai's auto-generated\n * model list, so CLI adapters don't have to hand-maintain \"Opus 4.6\" / \"GPT-5.3\" labels.\n *\n * Each adapter declares the CLI flag value it passes (e.g. `opus`, `gpt-5.3-codex`)\n * plus an id pattern; we resolve the latest matching pi-ai entry for the human label.\n */\n\nimport type { KnownProvider, Model } from '@earendil-works/pi-ai'\nimport type { CliModelEntry } from './types.ts'\nimport { getModels } from '@earendil-works/pi-ai'\n\nconst STATIC_REGEX_1 = /-\\d{8}$/\nconst STATIC_REGEX_2 = /-\\d{4}-\\d{2}-\\d{2}$/\nconst STATIC_REGEX_3 = /-preview/\n\n/** Strip dated aliases (claude-opus-4-5-20251101) and preview tags. */\nfunction isStableId(id: string): boolean {\n if (STATIC_REGEX_1.test(id))\n return false\n if (STATIC_REGEX_2.test(id))\n return false\n if (STATIC_REGEX_3.test(id))\n return false\n return true\n}\n\n/**\n * Numeric version comparator on the trailing version chunks of an id\n * (e.g. claude-opus-4-7 > claude-opus-4-6, gpt-5.3 > gpt-5.2).\n */\nfunction compareVersions(a: string, b: string): number {\n const versionOf = (id: string): number[] => {\n const nums = id.match(/\\d+(?:\\.\\d+)?/g) ?? []\n return nums.flatMap(n => n.split('.').map(Number))\n }\n const av = versionOf(a)\n const bv = versionOf(b)\n for (let i = 0; i < Math.max(av.length, bv.length); i++) {\n const x = av[i] ?? 0\n const y = bv[i] ?? 0\n if (x !== y)\n return y - x\n }\n return 0\n}\n\ninterface ResolveOpts {\n provider: KnownProvider\n /** Prefix the id must start with (e.g. `claude-opus-`, `gpt-5.`, `gemini-`). */\n prefix: string\n /** Optional substring filter (e.g. `codex`, `flash`). */\n contains?: string\n /** Skip ids containing any of these substrings. */\n exclude?: string[]\n}\n\nfunction resolve(opts: ResolveOpts): Model<any> | undefined {\n const { provider, prefix, contains, exclude } = opts\n const matches = getModels(provider)\n .filter(m => isStableId(m.id))\n .filter(m => m.id.startsWith(prefix))\n .filter(m => !contains || m.id.includes(contains))\n .filter(m => !exclude?.some(e => m.id.includes(e)))\n .sort((a, b) => compareVersions(a.id, b.id))\n return matches[0]\n}\n\n/**\n * Build a CliModelEntry by looking up the latest matching pi-ai model and using its\n * display name. `model` is the literal value passed to the CLI (alias or id).\n */\nexport function buildModelEntry(opts: ResolveOpts & {\n /** Value passed to the CLI's --model flag. Defaults to the resolved pi-ai id. */\n model?: string\n /** Override for the display name. Defaults to pi-ai's `name`. */\n name?: string\n /** Post-process pi-ai's name (e.g. strip \"Claude \" prefix). */\n nameTransform?: (name: string) => string\n hint: string\n recommended?: boolean\n}): CliModelEntry {\n const found = resolve(opts)\n const piName = found?.name\n const transformed = piName && opts.nameTransform ? opts.nameTransform(piName) : piName\n const name = opts.name ?? transformed ?? opts.model ?? opts.prefix\n const model = opts.model ?? found?.id ?? opts.prefix\n return { model, name, hint: opts.hint, recommended: opts.recommended }\n}\n\n/** Build a Record<id, CliModelEntry> from a list of entry specs, keyed by their `model` field. */\nexport function buildModels(entries: Array<Parameters<typeof buildModelEntry>[0]>): Record<string, CliModelEntry> {\n return Object.fromEntries(entries.map((e) => {\n const entry = buildModelEntry(e)\n return [entry.model, entry]\n }))\n}\n","import type { FeaturesConfig } from '../../core/config.ts'\nimport type { CustomPrompt, SkillSection } from '../prompts/index.ts'\nimport type { AgentType } from '../types.ts'\n\n/** Normalized event emitted by every CliAdapter from a single stream-json line. */\nexport type CliEvent\n = | { kind: 'noop' }\n /** Assistant text — delta (partial streaming) or full (turn-level). */\n | { kind: 'text', delta?: string, full?: string }\n /**\n * Tool invocation. `tool` is the adapter's raw name (e.g. 'Read', 'read_file'); look it up in\n * TOOL_NAMES for display verb + canonical role. `writeContent` carries inline file content\n * the LLM tried to write (fallback path when the host denies the Write tool).\n */\n | { kind: 'tool-call', tool: string, hint?: string, writeContent?: string }\n /** Stream finished cleanly. */\n | { kind: 'done', usage?: { input: number, output: number }, cost?: number, turns?: number }\n /** Stream finished with an error. */\n | { kind: 'error', message?: string }\n\n/**\n * Canonical tool registry — one row per raw name across adapters. Drives display (verb) and\n * semantic role (canonical) without per-CLI switching in the dispatcher.\n */\nexport const TOOL_NAMES: Record<string, { canonical: 'read' | 'search' | 'write' | 'list' | 'shell', verb: string }> = {\n // Claude Code\n Read: { canonical: 'read', verb: 'Reading' },\n Glob: { canonical: 'search', verb: 'Searching' },\n Grep: { canonical: 'search', verb: 'Searching' },\n Write: { canonical: 'write', verb: 'Writing' },\n Bash: { canonical: 'shell', verb: 'Running' },\n // Gemini\n read_file: { canonical: 'read', verb: 'Reading' },\n glob_tool: { canonical: 'search', verb: 'Searching' },\n write_file: { canonical: 'write', verb: 'Writing' },\n list_directory: { canonical: 'list', verb: 'Listing' },\n search_file_content: { canonical: 'search', verb: 'Searching' },\n run_shell_command: { canonical: 'shell', verb: 'Running' },\n}\n\n/**\n * Common keys adapters poke at to extract a useful hint (path, query, command) from a tool's\n * argument object — claude, gemini, codex all use overlapping shapes.\n */\nexport function extractToolHint(input: Record<string, unknown> | undefined | null): string | undefined {\n if (!input)\n return undefined\n for (const k of ['file_path', 'path', 'dir_path', 'pattern', 'query', 'command'] as const) {\n const v = input[k]\n if (typeof v === 'string' && v)\n return v\n }\n return undefined\n}\n\nexport type OptimizeModel\n = | 'opus'\n | 'sonnet'\n | 'haiku'\n | 'gemini-3.1-pro'\n | 'gemini-3-flash'\n | 'gpt-5.3-codex'\n | 'gpt-5.3-codex-spark'\n | 'gpt-5.2-codex'\n // pi-ai direct API models — dynamic from pi-ai's model registry\n | `pi:${string}`\n\nexport interface ModelInfo {\n id: OptimizeModel\n name: string\n hint: string\n recommended?: boolean\n agentId: string\n agentName: string\n /** Grouping key for provider selection (e.g. 'claude-code', 'pi:anthropic') */\n provider: string\n /** Human-readable provider name */\n providerName: string\n /** Normalized vendor name for UI grouping (e.g. 'Anthropic') — merges CLI and API entries */\n vendorGroup: string\n}\n\nexport interface StreamProgress {\n chunk: string\n type: 'reasoning' | 'text'\n text: string\n reasoning: string\n section?: SkillSection\n}\n\nexport interface OptimizeDocsOptions {\n packageName: string\n skillDir: string\n model?: OptimizeModel\n version?: string\n hasGithub?: boolean\n hasReleases?: boolean\n hasChangelog?: string | false\n docFiles?: string[]\n docsType?: 'llms.txt' | 'readme' | 'docs'\n hasShippedDocs?: boolean\n onProgress?: (progress: StreamProgress) => void\n timeout?: number\n verbose?: boolean\n debug?: boolean\n noCache?: boolean\n /** Which sections to generate */\n sections?: SkillSection[]\n /** Custom instructions from the user */\n customPrompt?: CustomPrompt\n /** Resolved feature flags */\n features?: FeaturesConfig\n /** Key files from the package (e.g., dist/pkg.d.ts) */\n pkgFiles?: string[]\n /** Lines consumed by SKILL.md overhead (frontmatter + header + search + footer) */\n overheadLines?: number\n}\n\nexport interface OptimizeResult {\n optimized: string\n wasOptimized: boolean\n error?: string\n warnings?: string[]\n reasoning?: string\n finishReason?: string\n usage?: { inputTokens: number, outputTokens: number, totalTokens: number }\n cost?: number\n debugLogsDir?: string\n}\n\nexport interface SectionResult {\n section: SkillSection\n content: string\n wasOptimized: boolean\n error?: string\n warnings?: ValidationWarning[]\n usage?: { input: number, output: number }\n cost?: number\n}\n\nexport interface ValidationWarning {\n section: string\n warning: string\n}\n\n/** Per-model config without redundant cli/agentId (those come from the CLI file) */\nexport interface CliModelEntry {\n /** Model flag passed to the CLI */\n model: string\n /** Human-readable model name */\n name: string\n /** Short description hint */\n hint: string\n /** Whether this is the recommended model for this CLI */\n recommended?: boolean\n}\n\n/** Full model config (assembled from CLI files + their models) */\nexport interface CliModelConfig extends CliModelEntry {\n cli: CliName\n agentId: AgentType\n}\n\nexport type CliName = 'claude' | 'gemini' | 'codex'\n\n/**\n * Per-CLI integration. Adding a new LLM CLI is one new file exporting one of these — no edits\n * to the dispatcher, the model table, or the provider name table.\n */\nexport interface CliAdapter {\n cli: CliName\n agentId: AgentType\n /** Human-readable LLM provider name (e.g. 'Anthropic', 'OpenAI'). */\n providerName: string\n /** Models this adapter can target, keyed by OptimizeModel id. */\n models: Record<string, CliModelEntry>\n /** Build argv for spawning the CLI process. */\n buildArgs: (model: string, skillDir: string, symlinkDirs: string[]) => string[]\n /** Parse one stream-json line into a normalized event. */\n parseEvent: (line: string) => CliEvent\n}\n","/**\n * Claude Code CLI — token-level streaming via --include-partial-messages.\n *\n * Write permission: Claude Code has hardcoded .claude/ write protection and --allowedTools glob\n * patterns are broken (github.com/anthropics/claude-code/issues/6881). Instead of fighting the\n * permission system, Write is auto-denied in pipe mode and we capture the content via a\n * tool-call event's writeContent.\n */\n\nimport type { CliAdapter, CliEvent } from './types.ts'\nimport { buildModels } from './model-registry.ts'\nimport { extractToolHint } from './types.ts'\n\nconst STATIC_REGEX_1 = /^Claude\\s+/\nconst STATIC_REGEX_2 = /\\s*\\(latest\\)\\s*$/i\n\nconst stripClaude = (n: string): string => n.replace(STATIC_REGEX_1, '').replace(STATIC_REGEX_2, '')\n\nfunction 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 captured via writeContent\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 * Event types this parses:\n * - stream_event/content_block_delta/text_delta → text delta (token streaming)\n * - assistant message with tool_use content → tool-call (with writeContent for Write)\n * - assistant message with text content → text full (non-streaming fallback)\n * - result → done with usage/cost/turns\n */\nfunction parseEvent(line: string): CliEvent {\n try {\n const obj = JSON.parse(line)\n\n if (obj.type === 'stream_event') {\n const evt = obj.event\n if (evt?.type === 'content_block_delta' && evt.delta?.type === 'text_delta')\n return { kind: 'text', delta: evt.delta.text }\n return { kind: 'noop' }\n }\n\n if (obj.type === 'assistant' && obj.message?.content) {\n const content = obj.message.content as any[]\n\n const tools = content.filter((c: any) => c.type === 'tool_use')\n if (tools.length) {\n const tool = tools.map((t: any) => t.name).join(', ')\n const hint = tools.map((t: any) => extractToolHint(t.input) ?? '').filter(Boolean).join(', ') || undefined\n const writeTool = tools.find((t: any) => t.name === 'Write' && t.input?.content)\n return { kind: 'tool-call', tool, hint, writeContent: writeTool?.input?.content }\n }\n\n const text = content\n .filter((c: any) => c.type === 'text')\n .map((c: any) => c.text)\n .join('')\n if (text)\n return { kind: 'text', full: text }\n }\n\n if (obj.type === 'result') {\n const u = obj.usage\n return {\n kind: 'done',\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 { kind: 'noop' }\n}\n\nexport const adapter: CliAdapter = {\n cli: 'claude',\n agentId: 'claude-code',\n providerName: 'Anthropic',\n models: buildModels([\n { model: 'opus', provider: 'anthropic', prefix: 'claude-opus-', nameTransform: stripClaude, hint: 'Most capable for complex work' },\n { model: 'sonnet', provider: 'anthropic', prefix: 'claude-sonnet-', nameTransform: stripClaude, hint: 'Best for everyday tasks' },\n { model: 'haiku', provider: 'anthropic', prefix: 'claude-haiku-', nameTransform: stripClaude, hint: 'Fastest for quick answers', recommended: true },\n ]),\n buildArgs,\n parseEvent,\n}\n","/**\n * OpenAI Codex CLI — exec subcommand with JSON output. Prompt passed via stdin (`-` sentinel).\n *\n * Event types:\n * - turn.completed → done with usage\n * - item.started command_execution → tool-call (Bash, in progress)\n * - item.completed agent_message → text full\n * - item.completed command_execution → tool-call (with writeContent if redirected to a file)\n * - item.completed file_change → tool-call (apply_patch wrote a file)\n * - turn.failed / error → done (terminal)\n */\n\nimport type { CliAdapter, CliEvent } from './types.ts'\nimport { buildModels } from './model-registry.ts'\n\nconst STATIC_REGEX_1 = /^cat\\s*>|>/\n\nfunction buildArgs(model: string, _skillDir: string, _symlinkDirs: string[]): string[] {\n return [\n 'exec',\n '--json',\n '--ephemeral',\n '--model',\n model,\n // --full-auto = workspace-write sandbox + on-request approval. Writes scoped to CWD\n // (.skilld/, set in spawn), reads unrestricted, network blocked. Shell remains enabled\n // for `skilld` search/validate (Codex has no per-command allowlist).\n // --ephemeral = no session persistence (mirrors Claude's --no-session-persistence).\n '--full-auto',\n '-',\n ]\n}\n\nfunction parseEvent(line: string): CliEvent {\n try {\n const obj = JSON.parse(line)\n\n if (obj.type === 'item.completed' && obj.item) {\n const item = obj.item\n if (item.type === 'agent_message' && item.text)\n return { kind: 'text', full: item.text }\n if (item.type === 'command_execution' && item.aggregated_output) {\n const cmd = item.command || ''\n const writeContent = (STATIC_REGEX_1.test(cmd)) ? item.aggregated_output : undefined\n return { kind: 'tool-call', tool: 'Bash', hint: `(${item.aggregated_output.length} chars output)`, writeContent }\n }\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 { kind: 'tool-call', tool: 'Write', hint: paths }\n }\n }\n\n if (obj.type === 'item.started' && obj.item?.type === 'command_execution')\n return { kind: 'tool-call', tool: 'Bash', hint: obj.item.command }\n\n if (obj.type === 'turn.completed' && obj.usage) {\n return {\n kind: 'done',\n usage: { input: obj.usage.input_tokens ?? 0, output: obj.usage.output_tokens ?? 0 },\n }\n }\n\n if (obj.type === 'turn.failed' || obj.type === 'error')\n return { kind: 'error', message: obj.message }\n }\n catch {}\n return { kind: 'noop' }\n}\n\nexport const adapter: CliAdapter = {\n cli: 'codex',\n agentId: 'codex',\n providerName: 'OpenAI',\n models: buildModels([\n { provider: 'openai', prefix: 'gpt-', contains: 'codex', exclude: ['spark', 'mini', 'max'], hint: 'Latest frontier Codex model' },\n { provider: 'openai', prefix: 'gpt-', contains: 'codex-spark', hint: 'Faster Codex variant', recommended: true },\n { provider: 'openai', prefix: 'gpt-', contains: 'codex-mini', hint: 'Cheapest Codex variant' },\n ]),\n buildArgs,\n parseEvent,\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 { CliAdapter, CliEvent } from './types.ts'\nimport { resolveSkilldCommand } from '../../core/skilld-command.ts'\nimport { buildModels } from './model-registry.ts'\nimport { extractToolHint } from './types.ts'\n\nfunction 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\nfunction parseEvent(line: string): CliEvent {\n try {\n const obj = JSON.parse(line)\n\n if (obj.type === 'message' && obj.role === 'assistant' && obj.content) {\n return obj.delta ? { kind: 'text', delta: obj.content } : { kind: 'text', full: obj.content }\n }\n\n if (obj.type === 'tool_use' || obj.type === 'tool_call') {\n const tool = obj.tool_name || obj.name || obj.tool || 'tool'\n const params = obj.parameters || obj.args || obj.input || {}\n const hint = extractToolHint(params)\n const writeContent = tool === 'write_file' && typeof params.content === 'string' ? params.content : undefined\n return { kind: 'tool-call', tool, hint, writeContent }\n }\n\n if (obj.type === 'result') {\n const s = obj.stats\n return {\n kind: 'done',\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 { kind: 'noop' }\n}\n\nexport const adapter: CliAdapter = {\n cli: 'gemini',\n agentId: 'gemini-cli',\n providerName: 'Google',\n models: buildModels([\n { provider: 'google', prefix: 'gemini-', contains: 'pro', exclude: ['flash', 'live', 'gemma'], hint: 'Most capable' },\n { provider: 'google', prefix: 'gemini-', contains: 'flash', exclude: ['lite', 'live', 'preview'], hint: 'Balanced', recommended: true },\n ]),\n buildArgs,\n parseEvent,\n}\n","/**\n * pi-ai auth — OAuth credentials, env API keys, login/logout flows.\n *\n * OAuth providers known to ban accounts for unauthorized usage are blocked.\n * API key access (env vars) remains supported for those providers.\n */\n\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'\nimport { homedir } from 'node:os'\nimport { getEnvApiKey, getProviders } from '@earendil-works/pi-ai'\nimport { getOAuthApiKey, getOAuthProvider, getOAuthProviders } from '@earendil-works/pi-ai/oauth'\nimport { join } from 'pathe'\nimport { CACHE_DIR, PI_AI_AUTH_PATH } from '../../core/paths.ts'\n\n/**\n * Consumer-OAuth providers that ban accounts for unauthorized usage.\n * API-key access remains supported for these.\n */\nexport const BLOCKED_OAUTH_PROVIDERS: ReadonlySet<string> = new Set([\n 'google-antigravity',\n 'google-gemini-cli',\n 'github-copilot',\n 'anthropic',\n 'openai-codex',\n])\n\nconst PI_AGENT_AUTH_PATH = join(\n process.env.PI_CODING_AGENT_DIR || join(homedir(), '.pi', 'agent'),\n 'auth.json',\n)\nconst SKILLD_AUTH_PATH = PI_AI_AUTH_PATH\n\n/**\n * Overrides for model-provider → OAuth-provider mapping. Most providers share\n * the same id in both systems (auto-matched); list only divergences.\n */\nconst OAUTH_PROVIDER_OVERRIDES: Record<string, string> = {\n google: 'google-gemini-cli',\n openai: 'openai-codex',\n}\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, then skilld's own. pi agent wins on conflict. */\nexport function loadAuth(): Record<string, OAuthCredentials> {\n const piAuth = readAuthFile(PI_AGENT_AUTH_PATH)\n const skilldAuth = readAuthFile(SKILLD_AUTH_PATH)\n return { ...skilldAuth, ...piAuth }\n}\n\nfunction saveAuth(auth: Record<string, OAuthCredentials>): void {\n mkdirSync(CACHE_DIR, { recursive: true, mode: 0o700 })\n writeFileSync(SKILLD_AUTH_PATH, JSON.stringify(auth, null, 2), { mode: 0o600 })\n}\n\n/** Resolve model provider id → OAuth provider id (returns null for blocked providers). */\nexport function resolveOAuthProviderId(modelProvider: string): string | null {\n const oauthId = OAUTH_PROVIDER_OVERRIDES[modelProvider] ?? modelProvider\n if (BLOCKED_OAUTH_PROVIDERS.has(oauthId))\n return null\n if (OAUTH_PROVIDER_OVERRIDES[modelProvider])\n return OAUTH_PROVIDER_OVERRIDES[modelProvider]!\n const oauthIds = new Set((getOAuthProviders() as Array<{ id: string }>).map(p => p.id))\n if (oauthIds.has(modelProvider))\n return modelProvider\n return null\n}\n\n/** Resolve API key for a provider — env vars first, then stored OAuth credentials. */\nexport async function resolveApiKey(provider: string): Promise<string | null> {\n const envKey = getEnvApiKey(provider)\n if (envKey)\n return envKey\n\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 // Refreshed credentials go 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\nexport interface LoginCallbacks {\n onAuth: (url: string, instructions?: string) => void\n onPrompt: (message: string, placeholder?: string) => Promise<string>\n onProgress?: (message: string) => void\n}\n\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\n .filter(p => !BLOCKED_OAUTH_PROVIDERS.has(p.id))\n .map(p => ({ id: p.id, name: p.name ?? p.id, loggedIn: !!auth[p.id] }))\n}\n\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\nexport function logoutOAuthProvider(providerId: string): void {\n const auth = loadAuth()\n delete auth[providerId]\n saveAuth(auth)\n}\n\n/** Re-export for callers that want pi-ai's provider list without importing from pi-ai directly. */\nexport { getEnvApiKey, getProviders }\n","/**\n * pi-ai model identification + dynamic model enumeration.\n *\n * Models are sourced from pi-ai's registry; legacy generations and tiny\n * context windows are filtered out. A \"recommended\" model is auto-picked\n * per provider for ergonomic defaults.\n */\n\nimport { getEnvApiKey, getModels, getProviders } from '@earendil-works/pi-ai'\nimport { loadAuth, resolveOAuthProviderId } from './pi-ai-auth.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\nconst MIN_CONTEXT_WINDOW = 32_000\n\n/** 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\n /-preview-\\d{2}-\\d{2,4}$/,\n // Dated model snapshots\n /-\\d{8}$/,\n]\n\nfunction isLegacyModel(modelId: string): boolean {\n return LEGACY_MODEL_PATTERNS.some(p => p.test(modelId))\n}\n\n/** Cheapest reliable option per provider, picked for auto-selection. */\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 name: string\n hint: string\n authSource: 'env' | 'oauth' | 'none'\n recommended: boolean\n}\n\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 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 * Sandboxed tool execution for the pi-ai agentic loop.\n *\n * The model can Read/Glob/Write within `.skilld/` and run a tightly\n * allowlisted set of shell commands. All file operations resolve through\n * `resolveSandboxedPath` which blocks traversal outside `skilldDir`.\n */\n\nimport type { ToolCall } from '@earendil-works/pi-ai'\nimport { execFileSync } from 'node:child_process'\nimport { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'\nimport { resolve } from 'node:path'\nimport { join } from 'pathe'\nimport { Type } from 'typebox'\nimport { TRAILING_SLASH_RE } from '../../core/regex.ts'\nimport { sanitizeMarkdown } from '../../core/sanitize.ts'\n\nconst STATIC_REGEX_1 = /^\\.\\/\\.skilld\\//\nconst STATIC_REGEX_2 = /^\\.skilld\\//\nconst STATIC_REGEX_3 = /^\\.\\//\nconst STATIC_REGEX_5 = /\\s+/\n\nexport const 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\nexport const MAX_TOOL_TURNS = 30\n\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(STATIC_REGEX_1, './').replace(STATIC_REGEX_2, './').replace(STATIC_REGEX_3, '')\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 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\n pos = idx + part.length\n }\n if (parts.at(-1) !== '')\n return pos === filePath.length\n return true\n }\n let remaining = filePath\n for (let i = 0; i < segments.length; i++) {\n const seg = segments[i]!\n if (!seg)\n continue\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. */\nexport function 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(STATIC_REGEX_1, './').replace(STATIC_REGEX_2, './').replace(STATIC_REGEX_3, '')\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(TRAILING_SLASH_RE, '') ?? ''\n walkDir(join(skilldDir, baseDir), baseDir)\n const matched = results.filter(r => globMatch(r.replace(STATIC_REGEX_1, ''), 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(STATIC_REGEX_5)\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 * pi-ai section runner — drives the agentic tool-use loop for a single SKILL.md\n * section. Streams reasoning + text to onProgress; tools are sandboxed to .skilld/.\n */\n\nimport type { AssistantMessage, Message, ToolCall } from '@earendil-works/pi-ai'\nimport type { SkillSection } from '../prompts/index.ts'\nimport type { StreamProgress } from './types.ts'\nimport { getModel, streamSimple } from '@earendil-works/pi-ai'\nimport { skillInternalDir } from '../../core/paths.ts'\nimport { resolveApiKey } from './pi-ai-auth.ts'\nimport { parsePiAiModelId } from './pi-ai-models.ts'\nimport { executeTool, MAX_TOOL_TURNS, TOOLS } from './pi-ai-tools.ts'\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\nconst SYSTEM_PROMPT = '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\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 = skillInternalDir(opts.skillDir)\n\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: SYSTEM_PROMPT,\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 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 messages.push(assistantMessage)\n\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 for (const tc of toolCalls) {\n const result = executeTool(tc, skilldDir)\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","import { API_CHANGE_BULLET_RE, SECTION_HEADING_RE, SOURCE_LINK_RE } from '../../core/regex.ts'\nimport { sanitizeMarkdown } from '../../core/sanitize.ts'\n\nconst STATIC_REGEX_1 = /^```(?:markdown|md)?[^\\S\\n]*\\n([\\s\\S]+)\\n```[^\\S\\n]*$/\nconst STATIC_REGEX_2 = /^```(?:markdown|md)/\nconst STATIC_REGEX_5 = /^-{3,}\\n/\nconst STATIC_REGEX_6 = /\\n-{3,}/\nconst STATIC_REGEX_7 = /^(##\\s|- (?:BREAKING|DEPRECATED|NEW): )/m\nconst STATIC_REGEX_8 = /^(## .+)\\n/\nconst STATIC_REGEX_9 = /^\\.\\/(?:\\.skilld\\/|references\\/)/\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(STATIC_REGEX_1)\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 = STATIC_REGEX_2.test(cleaned)\n if (isExplicitWrapper || SECTION_HEADING_RE.test(inner) || API_CHANGE_BULLET_RE.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(STATIC_REGEX_5)\n if (fmMatch) {\n const afterOpen = fmMatch[0].length\n const closeMatch = cleaned.slice(afterOpen).match(STATIC_REGEX_6)\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(STATIC_REGEX_7)\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(STATIC_REGEX_8)\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 (STATIC_REGEX_9.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 (!SECTION_HEADING_RE.test(cleaned) && !API_CHANGE_BULLET_RE.test(cleaned) && !SOURCE_LINK_RE.test(cleaned)) {\n return ''\n }\n\n return cleaned\n}\n","import type { StreamProgress } from './types.ts'\nimport { styleText } from 'node:util'\nimport { TOOL_NAMES } from './types.ts'\n\nconst STATIC_REGEX_1 = /^\\[(?:starting|retrying|cached)/\nconst STATIC_REGEX_2 = /^\\[([^:[\\]]+)(?::\\s(.+))?\\]$/\nconst STATIC_REGEX_3 = /skilld search\\s+\"([^\"]+)\"/\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} ${styleText('gray', `(+${repeatCount})`)}`)\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 ? `${styleText('gray', `[${section}]`)} ` : ''\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... ${styleText('gray', `(${items} items)`)}` : `${prefix}Writing...`)\n return\n }\n if (type !== 'reasoning' || !chunk.startsWith('['))\n return\n\n // Handle status messages like [starting...], [retrying...], [cached]\n if (STATIC_REGEX_1.test(chunk)) {\n const prefix = section ? `${styleText('gray', `[${section}]`)} ` : ''\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(STATIC_REGEX_2)\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_NAMES[rawName]?.verb ?? rawName\n const prefix = section ? `${styleText('gray', `[${section}]`)} ` : ''\n\n if ((rawName === 'Bash' || rawName === 'run_shell_command') && hint) {\n const searchMatch = hint.match(STATIC_REGEX_3)\n if (searchMatch) {\n emit(`${prefix}Searching ${styleText('cyan', `\"${searchMatch[1]}\"`)}`)\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} ${styleText('gray', path)}`)\n }\n }\n }\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 * 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 { AgentType } from '../types.ts'\nimport type { CliAdapter, CliModelConfig, CliName, OptimizeModel } from './types.ts'\nimport { exec } from 'node:child_process'\nimport { promisify } from 'node:util'\nimport { isWindows } from 'std-env'\nimport { detectInstalledAgents } from '../detect.ts'\nimport { agents } from '../targets/index.ts'\nimport { adapter as claudeAdapter } from './claude.ts'\nimport { adapter as codexAdapter } from './codex.ts'\nimport { adapter as geminiAdapter } from './gemini.ts'\nimport { getAvailablePiAiModels, isPiAiModel, 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 { cleanSectionOutput } from './clean-output.ts'\nexport { createToolProgress } from './cli-progress.ts'\nexport type { CliModelConfig, CliName, ModelInfo, OptimizeDocsOptions, OptimizeModel, OptimizeResult, StreamProgress } from './types.ts'\n\n// ── Per-CLI dispatch ─────────────────────────────────────────────────\n\n/** Single source of truth: adding a new CLI adapter here is the only edit needed in this file. */\nexport const CLI_ADAPTERS: Record<CliName, CliAdapter> = {\n claude: claudeAdapter,\n gemini: geminiAdapter,\n codex: codexAdapter,\n}\n\nconst CLI_PROVIDER_NAMES: Record<string, string> = Object.fromEntries(\n Object.values(CLI_ADAPTERS).map(a => [a.agentId, a.providerName]),\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 Object.values(CLI_ADAPTERS).flatMap(adapter =>\n Object.entries(adapter.models).map(([id, entry]) => [\n id,\n { ...entry, cli: adapter.cli, agentId: adapter.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 * 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 */\n\nimport { glob, readFile } from 'node:fs/promises'\nimport { join } from 'pathe'\nimport { detectPresetPackages } from './detect-presets.ts'\n\n// Static: import x from '...' | export ... from '...'\nconst FROM_STATIC_IMPORT_RE = /\\bfrom\\s*['\"]([^'\"\\n]+)['\"]/g\n// Side-effect: import '...'\nconst SIDE_EFFECT_IMPORT_RE = /\\bimport\\s*['\"]([^'\"\\n]+)['\"]/g\n// Dynamic: import('...')\nconst DYNAMIC_IMPORT_RE = /\\bimport\\s*\\(\\s*['\"]([^'\"\\n]+)['\"]\\s*\\)/g\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_DIRS = ['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: string[] = []\n for await (const file of glob(PATTERNS, {\n cwd,\n exclude: (p: string) => IGNORE_DIRS.some(dir => p === dir || p.endsWith(`/${dir}`)),\n })) {\n files.push(join(cwd, file))\n }\n\n await Promise.all(files.map(async (file) => {\n const content = await readFile(file, 'utf8')\n\n for (const m of content.matchAll(FROM_STATIC_IMPORT_RE))\n addPackage(counts, m[1])\n\n for (const m of content.matchAll(SIDE_EFFECT_IMPORT_RE))\n addPackage(counts, m[1])\n\n for (const m of content.matchAll(DYNAMIC_IMPORT_RE))\n addPackage(counts, m[1])\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","import type { SkillSection } from '../prompts/index.ts'\nimport type { OptimizeModel } from './types.ts'\nimport { createHash } from 'node:crypto'\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'\nimport { join } from 'pathe'\nimport { LLM_CACHE_DIR } from '../../core/paths.ts'\n\nconst DEFAULT_MAX_AGE = 7 * 24 * 60 * 60 * 1000\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\nexport function getCached(prompt: string, model: OptimizeModel, section: SkillSection, maxAge = DEFAULT_MAX_AGE): string | null {\n const path = join(LLM_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\nexport function setCache(prompt: string, model: OptimizeModel, section: SkillSection, text: string): void {\n mkdirSync(LLM_CACHE_DIR, { recursive: true, mode: 0o700 })\n writeFileSync(\n join(LLM_CACHE_DIR, `${hashPrompt(prompt, model, section)}.json`),\n JSON.stringify({ text, model, section, timestamp: Date.now() }),\n { mode: 0o600 },\n )\n}\n","/**\n * Section runner — shared seam for \"run a model and turn output into a SectionResult\".\n *\n * Both the spawn-based CLI path (claude/gemini/codex) and the in-process pi-ai path\n * compose three steps: prepareSection → run model → finalizeSection. The model run\n * itself differs (process vs API call); everything around it is shared here.\n */\n\nimport type { SkillSection } from '../prompts/index.ts'\nimport type { CliAdapter, SectionResult, StreamProgress, ValidationWarning } from './types.ts'\nimport { spawn } from 'node:child_process'\nimport { existsSync, mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'\nimport { join } from 'pathe'\nimport { isWindows } from 'std-env'\nimport { getSectionValidator, SECTION_OUTPUT_FILES } from '../prompts/index.ts'\nimport { cleanSectionOutput } from './clean-output.ts'\n\n/**\n * Strategy for running one section through a model. Two real adapters today:\n * `cliExecutor` (subprocess via `spawnCliAndStream`) and `piAiExecutor`\n * (in-process agent loop via `optimizeSectionPiAi`). The lifecycle around\n * `run` (prepareSection → run → finalizeSection) is identical for both.\n */\nexport interface SectionExecutor {\n /** When true, finalizeSection runs the prompt-injection cleanup pass. */\n cliCleanup: boolean\n run: (opts: {\n section: SkillSection\n prompt: string\n skillDir: string\n skilldDir: string\n timeout: number\n debug?: boolean\n onProgress?: (progress: StreamProgress) => void\n }) => Promise<RawRunOutput>\n}\n\n/** What a model run produces, before file/cleanup/validation. */\nexport interface RawRunOutput {\n /** Accumulated text from the run (stdout for CLIs, in-memory for pi-ai). */\n text: string\n /** Content the LLM tried to Write (fallback when host blocks Write tool). */\n writeContent?: string\n usage?: { input: number, output: number }\n cost?: number\n /** Stderr from a process run; undefined for in-memory runs. */\n stderr?: string\n /** Process exit code; undefined or 0 for successful in-memory runs. */\n exitCode?: number\n /** Raw stream-json lines for debug logging; CLI only. */\n rawLines?: string[]\n}\n\n/** Clear stale output and write the prompt file for debugging. */\nexport function prepareSection(opts: {\n section: SkillSection\n prompt: string\n outputPath: string\n skilldDir: string\n}): void {\n if (existsSync(opts.outputPath))\n unlinkSync(opts.outputPath)\n writeFileSync(join(opts.skilldDir, `PROMPT_${opts.section}.md`), opts.prompt)\n}\n\n/**\n * Turn a RawRunOutput into a SectionResult: resolve final text (file > writeContent > stdout),\n * clean, validate, and write debug logs. CLI runs additionally pass `cliCleanup` to enforce\n * prompt-injection defense (delete unexpected files in skilldDir).\n */\nexport function finalizeSection(opts: {\n section: SkillSection\n raw: RawRunOutput\n outputFile: string\n outputPath: string\n skilldDir: string\n debug: boolean\n /** When set, runs the prompt-injection file cleanup pass. CLI-only. */\n cliCleanup?: { preExistingFiles: Set<string> }\n}): SectionResult {\n const { section, raw, outputFile, outputPath, skilldDir, debug, cliCleanup } = opts\n\n if (cliCleanup) {\n for (const entry of readdirSync(skilldDir)) {\n if (entry === outputFile || cliCleanup.preExistingFiles.has(entry))\n continue\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 const logsDir = join(skilldDir, 'logs')\n const logName = section.toUpperCase().replace(/-/g, '_')\n\n const fileText = existsSync(outputPath) ? readFileSync(outputPath, 'utf-8') : ''\n const text = (fileText || raw.writeContent || raw.text).trim()\n\n const stderr = raw.stderr ?? ''\n const code = raw.exitCode ?? 0\n if (debug || (stderr && (!text || 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 (raw.rawLines?.length)\n writeFileSync(join(logsDir, `${logName}.jsonl`), raw.rawLines.join('\\n'))\n if (text)\n writeFileSync(join(logsDir, `${logName}.md`), text)\n }\n\n if (!text && code !== 0) {\n return { section, content: '', wasOptimized: false, error: stderr.trim() || `CLI exited with code ${code}` }\n }\n\n const content = text ? cleanSectionOutput(text) : ''\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: raw.usage,\n cost: raw.cost,\n }\n}\n\n/**\n * Spawn a CLI process, stream stream-json from stdout, parse via the adapter, and resolve\n * with raw run output. The stdin prompt, env, cwd, timeout, and event dispatch all live here;\n * adapters only declare argv shape and event parsing.\n */\nexport function spawnCliAndStream(opts: {\n adapter: CliAdapter\n cliModel: string\n prompt: string\n skillDir: string\n skilldDir: string\n symlinkDirs: string[]\n timeout: number\n debug?: boolean\n section: SkillSection\n onProgress?: (progress: StreamProgress) => void\n}): Promise<RawRunOutput> {\n const { adapter, cliModel, prompt, skillDir, skilldDir, symlinkDirs, timeout, debug, section, onProgress } = opts\n const args = adapter.buildArgs(cliModel, skillDir, symlinkDirs)\n const parseEvent = adapter.parseEvent\n\n return new Promise<RawRunOutput>((resolve) => {\n const proc = spawn(adapter.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 function applyEvent(evt: ReturnType<typeof parseEvent>): void {\n switch (evt.kind) {\n case 'text':\n if (evt.delta)\n accumulatedText += evt.delta\n if (evt.full !== undefined)\n accumulatedText = evt.full\n break\n case 'tool-call': {\n if (evt.writeContent)\n lastWriteContent = evt.writeContent\n const chunk = evt.hint ? `[${evt.tool}: ${evt.hint}]` : `[${evt.tool}]`\n onProgress?.({ chunk, type: 'reasoning', text: '', reasoning: chunk, section })\n break\n }\n case 'done':\n if (evt.usage)\n usage = evt.usage\n if (evt.cost != null)\n cost = evt.cost\n break\n case 'error':\n case 'noop':\n break\n }\n }\n\n proc.stdout.on('data', (chunk: Buffer) => {\n buffer += chunk.toString()\n const lines = buffer.split('\\n')\n buffer = lines.pop() || ''\n for (const line of lines) {\n if (!line.trim())\n continue\n if (debug)\n rawLines.push(line)\n applyEvent(parseEvent(line))\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 if (buffer.trim())\n applyEvent(parseEvent(buffer))\n resolve({\n text: accumulatedText,\n writeContent: lastWriteContent || undefined,\n usage,\n cost,\n stderr,\n exitCode: code ?? 0,\n rawLines: debug ? rawLines : undefined,\n })\n })\n\n proc.on('error', (err) => {\n resolve({ text: '', stderr: err.message, exitCode: 1 })\n })\n })\n}\n","/**\n * Section executors — concrete `SectionExecutor` impls and model→executor selection.\n *\n * Two adapters today:\n * - `cliExecutor` wraps `spawnCliAndStream` (claude/codex/gemini subprocess)\n * - `piAiExecutor` wraps `optimizeSectionPiAi` (in-process agent loop)\n *\n * Adding a new executor (e.g. raw Anthropic SDK) is a new factory plus a branch in\n * `selectExecutor`. The lifecycle in `llm-enhancer.optimizeSection` does not change.\n */\n\nimport type { SectionExecutor } from './runner.ts'\nimport type { OptimizeModel } from './types.ts'\nimport { getSkillReferenceDirs } from '../../cache/index.ts'\nimport { CLI_ADAPTERS, CLI_MODELS } from './index.ts'\nimport { getAvailablePiAiModels, isPiAiModel, optimizeSectionPiAi } from './pi-ai.ts'\nimport { spawnCliAndStream } from './runner.ts'\n\nfunction cliExecutor(model: OptimizeModel): SectionExecutor | { error: string } {\n const cliConfig = CLI_MODELS[model]\n if (!cliConfig)\n return { error: `No CLI mapping for model: ${model}` }\n const adapter = CLI_ADAPTERS[cliConfig.cli]\n return {\n cliCleanup: true,\n run: ({ section, prompt, skillDir, skilldDir, timeout, debug, onProgress }) => spawnCliAndStream({\n adapter,\n cliModel: cliConfig.model,\n prompt,\n skillDir,\n skilldDir,\n symlinkDirs: getSkillReferenceDirs(skillDir),\n timeout,\n debug,\n section,\n onProgress,\n }),\n }\n}\n\nfunction piAiExecutor(model: OptimizeModel): SectionExecutor | { error: string } {\n const available = new Set(getAvailablePiAiModels().map(m => m.id as OptimizeModel))\n if (!available.has(model))\n return { error: `Pi model unavailable or not authenticated: ${model}` }\n\n return {\n cliCleanup: false,\n run: async ({ section, prompt, skillDir, timeout, onProgress }) => {\n const ac = new AbortController()\n const timer = setTimeout(() => ac.abort(), timeout)\n try {\n const result = await optimizeSectionPiAi({ section, prompt, skillDir, model, onProgress, signal: ac.signal })\n return { text: result.text.trim(), usage: result.usage, cost: result.cost }\n }\n catch (err) {\n return { text: '', stderr: (err as Error).message, exitCode: 1 }\n }\n finally {\n clearTimeout(timer)\n }\n },\n }\n}\n\n/** Resolve `model` to an executor, or an error if the model is unavailable/unmapped. */\nexport function selectExecutor(model: OptimizeModel): SectionExecutor | { error: string } {\n return isPiAiModel(model) ? piAiExecutor(model) : cliExecutor(model)\n}\n","/**\n * LLM enhancer — drives CLI adapters (and pi-ai) to generate SKILL.md sections.\n *\n * Owns the section-level lifecycle: cache lookup (references-dir + prompt-hash),\n * parallel spawn with stagger, rate-limit-aware retry, and merge-order assembly.\n * Per-CLI concerns (argv, stream parsing, model registry) live in `./clis/`.\n */\n\nimport type { SectionExecutor } from './clis/runner.ts'\nimport type { OptimizeDocsOptions, OptimizeResult, SectionResult, StreamProgress } from './clis/types.ts'\nimport type { SkillSection } from './prompts/index.ts'\nimport { existsSync, lstatSync, mkdirSync, readdirSync } from 'node:fs'\nimport { setTimeout as delay } from 'node:timers/promises'\nimport { join } from 'pathe'\nimport { createReferenceCache } from '../cache/index.ts'\nimport { skillInternalDir, skillLogDir } from '../core/paths.ts'\nimport { getCached, setCache } from './clis/cli-cache.ts'\nimport { selectExecutor } from './clis/executors.ts'\nimport { finalizeSection, prepareSection } from './clis/runner.ts'\nimport { buildAllSectionPrompts, SECTION_MERGE_ORDER, SECTION_OUTPUT_FILES, wrapSection } from './prompts/index.ts'\n\nconst STATIC_REGEX_1 = /\\b429\\b/\nconst STATIC_REGEX_2 = /rate.?limit/i\nconst STATIC_REGEX_3 = /exhausted.*capacity/i\nconst STATIC_REGEX_4 = /quota.*reset/i\nconst STATIC_REGEX_5 = /reset\\s+after\\s+(\\d+)s/i\n\n// ── Per-section run ──────────────────────────────────────────────────\n\ninterface OptimizeSectionOptions {\n section: SkillSection\n prompt: string\n outputFile: string\n skillDir: string\n executor: SectionExecutor\n onProgress?: (progress: StreamProgress) => void\n timeout: number\n debug?: boolean\n preExistingFiles: Set<string>\n}\n\n/** prepareSection → executor.run → finalizeSection. One linear flow per section. */\nasync function optimizeSection(opts: OptimizeSectionOptions): Promise<SectionResult> {\n const { section, prompt, outputFile, skillDir, executor, onProgress, timeout, debug, preExistingFiles } = opts\n const skilldDir = skillInternalDir(skillDir)\n const outputPath = join(skilldDir, outputFile)\n\n prepareSection({ section, prompt, outputPath, skilldDir })\n\n const raw = await executor.run({ section, prompt, skillDir, skilldDir, timeout, debug, onProgress })\n\n return finalizeSection({\n section,\n raw,\n outputFile,\n outputPath,\n skilldDir,\n debug: !!debug,\n cliCleanup: executor.cliCleanup ? { preExistingFiles } : undefined,\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 const cache = createReferenceCache(packageName, version)\n\n const selectedSections = sections ?? ['api-changes', 'best-practices'] as SkillSection[]\n\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 const executorOrError = selectExecutor(model)\n if ('error' in executorOrError)\n return { optimized: '', wasOptimized: false, error: executorOrError.error }\n const executor = executorOrError\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 if (version) {\n const outputFile = SECTION_OUTPUT_FILES[section]\n const refCached = cache.readSection(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 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 const skilldDir = skillInternalDir(skillDir)\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 executor,\n onProgress,\n timeout,\n debug,\n preExistingFiles,\n })\n if (i === 0)\n return run()\n return delay(i * STAGGER_MS).then(run)\n }),\n )\n : []\n\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 executor,\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 cache.writeSections(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 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 ? skillLogDir(skillDir)\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\nfunction isRateLimitError(error: string | undefined): boolean {\n if (!error)\n return false\n return STATIC_REGEX_1.test(error)\n || STATIC_REGEX_2.test(error)\n || STATIC_REGEX_3.test(error)\n || STATIC_REGEX_4.test(error)\n}\n\nfunction parseRateLimitDelay(error: string | undefined): number | undefined {\n if (!error || !isRateLimitError(error))\n return undefined\n const match = error.match(STATIC_REGEX_5)\n return match ? Number(match[1]) : 10\n}\n\nfunction getRetryError(result: PromiseSettledResult<SectionResult>): string | undefined {\n if (result.status === 'rejected')\n return String(result.reason)\n return result.value.error\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAYA,MAAMA,mBAAiB;AACvB,MAAMC,mBAAiB;AACvB,MAAMC,mBAAiB;AAGvB,SAAS,WAAW,IAAqB;CACvC,IAAIF,iBAAe,KAAK,GAAG,EACzB,OAAO;CACT,IAAIC,iBAAe,KAAK,GAAG,EACzB,OAAO;CACT,IAAIC,iBAAe,KAAK,GAAG,EACzB,OAAO;CACT,OAAO;;;;;;CAQP,MAAM,KAAA,UAAa,EAAA;OAEjB,KADa,UAAS,EAAA;;EAGxB,MAAM,IAAK,GAAA,MAAA;EACX,MAAM,IAAK,GAAA,MAAA;EACX,IAAK,MAAI,GAAI,OAAO,IAAK;;QAEjB;;;CAIR,MAAA,EAAO,UAAA,QAAA,UAAA,YAAA;;;SAeS,gBAAU,MACvB;;;;;;EAYL,OAAgB,KAAA,SAAA,OAAgB,MASd,KAAA;EAChB;EACA,MAAM,KAAA;EACN,aAAM,KAAA;EACN;;SAEgB,YAAA,SAAA;QAAY,OAAK,YAAA,QAAA,KAAA,MAAA;EAAM,MAAA,QAAa,gBAAK,EAAA;EAAa,OAAA,CAAA,MAAA,OAAA,MAAA;;;MAMpE,aAAc;OACd;aACC;;;;;;;CCvEL,MAAa;EAEX,WAAM;EAAE,MAAA;EAAmB;QAAiB;EAC5C,WAAM;EAAE,MAAA;EAAqB;OAAmB;EAChD,WAAM;EAAE,MAAA;EAAqB;YAAmB;EAChD,WAAO;EAAE,MAAA;EAAoB;YAAiB;EAC9C,WAAM;EAAE,MAAA;EAAoB;aAAiB;EAE7C,WAAW;EAAE,MAAA;EAAmB;iBAAiB;EACjD,WAAW;EAAE,MAAA;EAAqB;sBAAmB;EACrD,WAAY;EAAE,MAAA;EAAoB;oBAAiB;EACnD,WAAA;EAAkB,MAAA;EAAmB;;SACd,gBAAW,O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const sharedArgs = {
4
+ global: {
5
+ type: "boolean",
6
+ alias: "g",
7
+ description: "Install globally to ~/<agent>/skills",
8
+ default: false
9
+ },
10
+ agent: {
11
+ type: "enum",
12
+ options: Object.keys(targets),
13
+ alias: "a",
14
+ description: "Target agent — where skills are installed"
15
+ },
16
+ model: {
17
+ type: "string",
18
+ alias: "m",
19
+ description: "Enhancement model for SKILL.md generation",
20
+ valueHint: "id"
21
+ },
22
+ yes: {
23
+ type: "boolean",
24
+ alias: "y",
25
+ description: "Skip prompts, use defaults",
26
+ default: false
27
+ },
28
+ force: {
29
+ type: "boolean",
30
+ alias: "f",
31
+ description: "Ignore all caches, re-fetch docs and regenerate",
32
+ default: false
33
+ },
34
+ debug: {
35
+ type: "boolean",
36
+ description: "Save raw enhancement output to logs/ for each section",
37
+ default: false
38
+ }
39
+ };
40
+ export { sharedArgs as t };
41
+
42
+ //# sourceMappingURL=args.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"args.mjs","names":["agents"],"sources":["../../src/cli/args.ts"],"sourcesContent":["import { agents } from '../agent/index.ts'\n\nexport const sharedArgs = {\n global: {\n type: 'boolean' as const,\n alias: 'g',\n description: 'Install globally to ~/<agent>/skills',\n default: false,\n },\n agent: {\n type: 'enum' as const,\n options: Object.keys(agents),\n alias: 'a',\n description: 'Target agent — where skills are installed',\n },\n model: {\n type: 'string' as const,\n alias: 'm',\n description: 'Enhancement model for SKILL.md generation',\n valueHint: 'id',\n },\n yes: {\n type: 'boolean' as const,\n alias: 'y',\n description: 'Skip prompts, use defaults',\n default: false,\n },\n force: {\n type: 'boolean' as const,\n alias: 'f',\n description: 'Ignore all caches, re-fetch docs and regenerate',\n default: false,\n },\n debug: {\n type: 'boolean' as const,\n description: 'Save raw enhancement output to logs/ for each section',\n default: false,\n },\n}\n"],"mappings":";;AAEA,MAAa,aAAa;CACxB,QAAQ;EACN,MAAM;EACN,OAAO;EACP,aAAa;EACb,SAAS;EACV;CACD,OAAO;EACL,MAAM;EACN,SAAS,OAAO,KAAKA,QAAO;EAC5B,OAAO;EACP,aAAa;EACd;CACD,OAAO;EACL,MAAM;EACN,OAAO;EACP,aAAa;EACb,WAAW;EACZ;CACD,KAAK;EACH,MAAM;EACN,OAAO;EACP,aAAa;EACb,SAAS;EACV;CACD,OAAO;EACL,MAAM;EACN,OAAO;EACP,aAAa;EACb,SAAS;EACV;CACD,OAAO;EACL,MAAM;EACN,aAAa;EACb,SAAS;EACV;CACF"}