@oyasmi/pipiclaw 0.3.4 → 0.4.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 (78) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/LICENSE +184 -0
  3. package/README.md +230 -231
  4. package/dist/agent.d.ts.map +1 -1
  5. package/dist/agent.js +2 -19
  6. package/dist/agent.js.map +1 -1
  7. package/dist/command-extension.d.ts.map +1 -1
  8. package/dist/command-extension.js.map +1 -1
  9. package/dist/commands.d.ts.map +1 -1
  10. package/dist/commands.js.map +1 -1
  11. package/dist/config-loader.d.ts.map +1 -1
  12. package/dist/config-loader.js.map +1 -1
  13. package/dist/context.d.ts.map +1 -1
  14. package/dist/context.js +0 -2
  15. package/dist/context.js.map +1 -1
  16. package/dist/delivery.d.ts.map +1 -1
  17. package/dist/delivery.js +11 -14
  18. package/dist/delivery.js.map +1 -1
  19. package/dist/dingtalk.d.ts.map +1 -1
  20. package/dist/dingtalk.js +26 -26
  21. package/dist/dingtalk.js.map +1 -1
  22. package/dist/events.d.ts.map +1 -1
  23. package/dist/events.js +5 -8
  24. package/dist/events.js.map +1 -1
  25. package/dist/index.d.ts +20 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +20 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/log.d.ts.map +1 -1
  30. package/dist/log.js.map +1 -1
  31. package/dist/main.d.ts.map +1 -1
  32. package/dist/main.js.map +1 -1
  33. package/dist/memory-consolidation.d.ts.map +1 -1
  34. package/dist/memory-consolidation.js.map +1 -1
  35. package/dist/memory-files.d.ts.map +1 -1
  36. package/dist/memory-files.js.map +1 -1
  37. package/dist/memory-lifecycle.d.ts.map +1 -1
  38. package/dist/memory-lifecycle.js +1 -2
  39. package/dist/memory-lifecycle.js.map +1 -1
  40. package/dist/model-utils.d.ts.map +1 -1
  41. package/dist/model-utils.js.map +1 -1
  42. package/dist/paths.d.ts.map +1 -1
  43. package/dist/prompt-builder.d.ts.map +1 -1
  44. package/dist/prompt-builder.js.map +1 -1
  45. package/dist/sandbox.d.ts.map +1 -1
  46. package/dist/sandbox.js +0 -1
  47. package/dist/sandbox.js.map +1 -1
  48. package/dist/shell-escape.d.ts.map +1 -1
  49. package/dist/shell-escape.js.map +1 -1
  50. package/dist/store.d.ts.map +1 -1
  51. package/dist/store.js +2 -3
  52. package/dist/store.js.map +1 -1
  53. package/dist/sub-agents.d.ts.map +1 -1
  54. package/dist/sub-agents.js +42 -10
  55. package/dist/sub-agents.js.map +1 -1
  56. package/dist/tools/attach.d.ts.map +1 -1
  57. package/dist/tools/attach.js.map +1 -1
  58. package/dist/tools/bash.d.ts.map +1 -1
  59. package/dist/tools/bash.js.map +1 -1
  60. package/dist/tools/edit.d.ts.map +1 -1
  61. package/dist/tools/edit.js.map +1 -1
  62. package/dist/tools/index.d.ts.map +1 -1
  63. package/dist/tools/index.js.map +1 -1
  64. package/dist/tools/read.d.ts.map +1 -1
  65. package/dist/tools/read.js.map +1 -1
  66. package/dist/tools/subagent.d.ts.map +1 -1
  67. package/dist/tools/subagent.js.map +1 -1
  68. package/dist/tools/truncate.d.ts.map +1 -1
  69. package/dist/tools/truncate.js.map +1 -1
  70. package/dist/tools/write-content.d.ts.map +1 -1
  71. package/dist/tools/write-content.js.map +1 -1
  72. package/dist/tools/write.d.ts.map +1 -1
  73. package/dist/tools/write.js.map +1 -1
  74. package/docs/memory-rfc.md +291 -0
  75. package/docs/subagent/pi-subagent-analyse.txt +190 -0
  76. package/docs/subagent/pi-subagent-design.txt +266 -0
  77. package/docs/subagent/pi-subagent-phase1-plan.txt +529 -0
  78. package/package.json +69 -53
@@ -1 +1 @@
1
- {"version":3,"file":"model-utils.js","sourceRoot":"","sources":["../src/model-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,QAAQ,EAAc,MAAM,qBAAqB,CAAC;AAIrE,sFAAsF;AACtF,MAAM,YAAY,GAAG,QAAQ,CAAC,WAAW,EAAE,mB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,eAAe,CAAC,CAAC,CAAC,CAAC;IAC3B,CAAC;IAED,OAAO,YAAY,CAAC;AAAA,CACpB","sourcesContent":["import { type Api, getModel, type Model } from \"@mariozechner/pi-ai\";\nimport type { ModelRegistry } from \"@mariozechner/pi-coding-agent\";\nimport type { PipiclawSettingsManager } from \"./context.js\";\n\n// Default model - will be overridden by ModelRegistry if custom models are configured\nconst defaultModel = getModel(\"anthropic\", \"claude-sonnet-4-5\");\n\nexport function formatModelReference(model: Model<Api>): string {\n\treturn `${model.provider}/${model.id}`;\n}\n\nexport function findExactModelReferenceMatch(\n\tmodelReference: string,\n\tavailableModels: Model<Api>[],\n): { match?: Model<Api>; ambiguous: boolean } {\n\tconst trimmedReference = modelReference.trim();\n\tif (!trimmedReference) {\n\t\treturn { ambiguous: false };\n\t}\n\n\tconst normalizedReference = trimmedReference.toLowerCase();\n\n\tconst canonicalMatches = availableModels.filter(\n\t\t(model) => `${model.provider}/${model.id}`.toLowerCase() === normalizedReference,\n\t);\n\tif (canonicalMatches.length === 1) {\n\t\treturn { match: canonicalMatches[0], ambiguous: false };\n\t}\n\tif (canonicalMatches.length > 1) {\n\t\treturn { ambiguous: true };\n\t}\n\n\tconst slashIndex = trimmedReference.indexOf(\"/\");\n\tif (slashIndex !== -1) {\n\t\tconst provider = trimmedReference.substring(0, slashIndex).trim();\n\t\tconst modelId = trimmedReference.substring(slashIndex + 1).trim();\n\t\tif (provider && modelId) {\n\t\t\tconst providerMatches = availableModels.filter(\n\t\t\t\t(model) =>\n\t\t\t\t\tmodel.provider.toLowerCase() === provider.toLowerCase() &&\n\t\t\t\t\tmodel.id.toLowerCase() === modelId.toLowerCase(),\n\t\t\t);\n\t\t\tif (providerMatches.length === 1) {\n\t\t\t\treturn { match: providerMatches[0], ambiguous: false };\n\t\t\t}\n\t\t\tif (providerMatches.length > 1) {\n\t\t\t\treturn { ambiguous: true };\n\t\t\t}\n\t\t}\n\t}\n\n\tconst idMatches = availableModels.filter((model) => model.id.toLowerCase() === normalizedReference);\n\tif (idMatches.length === 1) {\n\t\treturn { match: idMatches[0], ambiguous: false };\n\t}\n\n\treturn { ambiguous: idMatches.length > 1 };\n}\n\nexport function formatModelList(\n\tmodels: Model<Api>[],\n\tcurrentModel: Model<Api> | undefined,\n\tlimit: number = 20,\n): string {\n\tconst refs = models\n\t\t.slice()\n\t\t.sort((a, b) => formatModelReference(a).localeCompare(formatModelReference(b)))\n\t\t.map((model) => {\n\t\t\tconst ref = formatModelReference(model);\n\t\t\tconst marker =\n\t\t\t\tcurrentModel && currentModel.provider === model.provider && currentModel.id === model.id\n\t\t\t\t\t? \" (current)\"\n\t\t\t\t\t: \"\";\n\t\t\treturn `- \\`${ref}\\`${marker}`;\n\t\t});\n\n\tif (refs.length <= limit) {\n\t\treturn refs.join(\"\\n\");\n\t}\n\n\treturn `${refs.slice(0, limit).join(\"\\n\")}\\n- ... and ${refs.length - limit} more`;\n}\n\nexport function resolveInitialModel(\n\tmodelRegistry: ModelRegistry,\n\tsettingsManager: PipiclawSettingsManager,\n): Model<Api> {\n\tconst savedProvider = settingsManager.getDefaultProvider();\n\tconst savedModelId = settingsManager.getDefaultModel();\n\tconst availableModels = modelRegistry.getAvailable();\n\tif (savedProvider && savedModelId) {\n\t\tconst savedModel = modelRegistry.find(savedProvider, savedModelId);\n\t\tif (\n\t\t\tsavedModel &&\n\t\t\tavailableModels.some((model) => model.provider === savedModel.provider && model.id === savedModel.id)\n\t\t) {\n\t\t\treturn savedModel;\n\t\t}\n\t}\n\n\tif (availableModels.length > 0) {\n\t\treturn availableModels[0];\n\t}\n\n\treturn defaultModel;\n}\n"]}
1
+ {"version":3,"file":"model-utils.js","sourceRoot":"","sources":["../src/model-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,QAAQ,EAAc,MAAM,qBAAqB,CAAC;AAIrE,sFAAsF;AACtF,MAAM,YAAY,GAAG,QAAQ,CAAC,WAAW,EAAE,mBAAmB,CAAC,CAAC;AAEhE,MAAM,UAAU,oBAA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C","sourcesContent":["import { type Api, getModel, type Model } from \"@mariozechner/pi-ai\";\nimport type { ModelRegistry } from \"@mariozechner/pi-coding-agent\";\nimport type { PipiclawSettingsManager } from \"./context.js\";\n\n// Default model - will be overridden by ModelRegistry if custom models are configured\nconst defaultModel = getModel(\"anthropic\", \"claude-sonnet-4-5\");\n\nexport function formatModelReference(model: Model<Api>): string {\n\treturn `${model.provider}/${model.id}`;\n}\n\nexport function findExactModelReferenceMatch(\n\tmodelReference: string,\n\tavailableModels: Model<Api>[],\n): { match?: Model<Api>; ambiguous: boolean } {\n\tconst trimmedReference = modelReference.trim();\n\tif (!trimmedReference) {\n\t\treturn { ambiguous: false };\n\t}\n\n\tconst normalizedReference = trimmedReference.toLowerCase();\n\n\tconst canonicalMatches = availableModels.filter(\n\t\t(model) => `${model.provider}/${model.id}`.toLowerCase() === normalizedReference,\n\t);\n\tif (canonicalMatches.length === 1) {\n\t\treturn { match: canonicalMatches[0], ambiguous: false };\n\t}\n\tif (canonicalMatches.length > 1) {\n\t\treturn { ambiguous: true };\n\t}\n\n\tconst slashIndex = trimmedReference.indexOf(\"/\");\n\tif (slashIndex !== -1) {\n\t\tconst provider = trimmedReference.substring(0, slashIndex).trim();\n\t\tconst modelId = trimmedReference.substring(slashIndex + 1).trim();\n\t\tif (provider && modelId) {\n\t\t\tconst providerMatches = availableModels.filter(\n\t\t\t\t(model) =>\n\t\t\t\t\tmodel.provider.toLowerCase() === provider.toLowerCase() &&\n\t\t\t\t\tmodel.id.toLowerCase() === modelId.toLowerCase(),\n\t\t\t);\n\t\t\tif (providerMatches.length === 1) {\n\t\t\t\treturn { match: providerMatches[0], ambiguous: false };\n\t\t\t}\n\t\t\tif (providerMatches.length > 1) {\n\t\t\t\treturn { ambiguous: true };\n\t\t\t}\n\t\t}\n\t}\n\n\tconst idMatches = availableModels.filter((model) => model.id.toLowerCase() === normalizedReference);\n\tif (idMatches.length === 1) {\n\t\treturn { match: idMatches[0], ambiguous: false };\n\t}\n\n\treturn { ambiguous: idMatches.length > 1 };\n}\n\nexport function formatModelList(\n\tmodels: Model<Api>[],\n\tcurrentModel: Model<Api> | undefined,\n\tlimit: number = 20,\n): string {\n\tconst refs = models\n\t\t.slice()\n\t\t.sort((a, b) => formatModelReference(a).localeCompare(formatModelReference(b)))\n\t\t.map((model) => {\n\t\t\tconst ref = formatModelReference(model);\n\t\t\tconst marker =\n\t\t\t\tcurrentModel && currentModel.provider === model.provider && currentModel.id === model.id\n\t\t\t\t\t? \" (current)\"\n\t\t\t\t\t: \"\";\n\t\t\treturn `- \\`${ref}\\`${marker}`;\n\t\t});\n\n\tif (refs.length <= limit) {\n\t\treturn refs.join(\"\\n\");\n\t}\n\n\treturn `${refs.slice(0, limit).join(\"\\n\")}\\n- ... and ${refs.length - limit} more`;\n}\n\nexport function resolveInitialModel(\n\tmodelRegistry: ModelRegistry,\n\tsettingsManager: PipiclawSettingsManager,\n): Model<Api> {\n\tconst savedProvider = settingsManager.getDefaultProvider();\n\tconst savedModelId = settingsManager.getDefaultModel();\n\tconst availableModels = modelRegistry.getAvailable();\n\tif (savedProvider && savedModelId) {\n\t\tconst savedModel = modelRegistry.find(savedProvider, savedModelId);\n\t\tif (\n\t\t\tsavedModel &&\n\t\t\tavailableModels.some((model) => model.provider === savedModel.provider && model.id === savedModel.id)\n\t\t) {\n\t\t\treturn savedModel;\n\t\t}\n\t}\n\n\tif (availableModels.length > 0) {\n\t\treturn availableModels[0];\n\t}\n\n\treturn defaultModel;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../src/paths.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,QAAQ,aAAa,CAAC;AACnC,eAAO,MAAM,YAAY,QAAmC,CAAC;AAC7D,eAAO,MAAM,aAAa,QAAkC,CAAC;AAC7D,eAAO,MAAM,mBAAmB,eAAe,CAAC;AAChD,eAAO,MAAM,cAAc,QAA2C,CAAC;AACvE,eAAO,MAAM,mBAAmB,QAAqC,CAAC;AACtE,eAAO,MAAM,gBAAgB,QAAkC,CAAC;AAChE,eAAO,MAAM,kBAAkB,QAAoC,CAAC;AACpE,eAAO,MAAM,oBAAoB,QAAsC,CAAC","sourcesContent":["import { homedir } from \"os\";\nimport { join } from \"path\";\n\nexport const APP_NAME = \"pipiclaw\";\nexport const APP_HOME_DIR = join(homedir(), \".pi\", APP_NAME);\nexport const WORKSPACE_DIR = join(APP_HOME_DIR, \"workspace\");\nexport const SUB_AGENTS_DIR_NAME = \"sub-agents\";\nexport const SUB_AGENTS_DIR = join(WORKSPACE_DIR, SUB_AGENTS_DIR_NAME);\nexport const CHANNEL_CONFIG_PATH = join(APP_HOME_DIR, \"channel.json\");\nexport const AUTH_CONFIG_PATH = join(APP_HOME_DIR, \"auth.json\");\nexport const MODELS_CONFIG_PATH = join(APP_HOME_DIR, \"models.json\");\nexport const SETTINGS_CONFIG_PATH = join(APP_HOME_DIR, \"settings.json\");\n"]}
1
+ {"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../src/paths.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,QAAQ,aAAa,CAAC;AACnC,eAAO,MAAM,YAAY,QAAmC,CAAC;AAC7D,eAAO,MAAM,aAAa,QAAkC,CAAC;AAC7D,eAAO,MAAM,mBAAmB,eAAe,CAAC;AAChD,eAAO,MAAM,cAAc,QAA2C,CAAC;AACvE,eAAO,MAAM,mBAAmB,QAAqC,CAAC;AACtE,eAAO,MAAM,gBAAgB,QAAkC,CAAC;AAChE,eAAO,MAAM,kBAAkB,QAAoC,CAAC;AACpE,eAAO,MAAM,oBAAoB,QAAsC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"prompt-builder.d.ts","sourceRoot":"","sources":["../src/prompt-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAElD,MAAM,WAAW,yBAAyB;IACzC,YAAY,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,uBAAuB,CACtC,aAAa,EAAE,MAAM,EACrB,SAAS,EAAE,MAAM,EACjB,aAAa,EAAE,aAAa,EAC5B,OAAO,GAAE,yBAA8B,GACrC,MAAM,CAoJR","sourcesContent":["import type { SandboxConfig } from \"./sandbox.js\";\n\nexport interface AppendSystemPromptOptions {\n\tsubAgentList?: string;\n}\n\nexport function buildAppendSystemPrompt(\n\tworkspacePath: string,\n\tchannelId: string,\n\tsandboxConfig: SandboxConfig,\n\toptions: AppendSystemPromptOptions = {},\n): string {\n\tconst channelPath = `${workspacePath}/${channelId}`;\n\tconst subAgentsPath = `${workspacePath}/sub-agents`;\n\tconst isDocker = sandboxConfig.type === \"docker\";\n\n\tconst envDescription = isDocker\n\t\t? `You are running inside a Docker container (Alpine Linux).\n- Bash working directory: / (use cd or absolute paths)\n- Install tools with: apk add <package>\n- Your changes persist across sessions`\n\t\t: `You are running directly on the host machine.\n- Bash working directory: ${process.cwd()}\n- Be careful with system modifications`;\n\n\tconst sections: string[] = [];\n\n\tsections.push(`## Pipiclaw Runtime\nYou are running inside Pipiclaw, a DingTalk-oriented runtime built on top of pi.\n\n## Context\n- For current date/time, use: date\n- You have access to the active session context for this session.\n- Raw transcript files are cold storage. Do not assume they are preloaded.\n\n## Formatting\nUse Markdown for formatting. DingTalk AI Card supports basic Markdown:\nBold: **text**, Italic: *text*, Code: \\`code\\`, Block: \\`\\`\\`code\\`\\`\\`, Links: [text](url)\n\n## Environment\n${envDescription}\n\n## Workspace Layout\n${workspacePath}/\n├── SOUL.md # Your identity/personality (read-only)\n├── AGENTS.md # Custom behavior instructions (read-only)\n├── MEMORY.md # Stable workspace memory (admin-managed, read on demand)\n├── sub-agents/ # Predefined sub-agent definitions\n├── skills/ # Global CLI tools you create\n├── events/ # Scheduled events\n└── ${channelId}/ # This channel\n ├── MEMORY.md # Channel durable memory (read on demand, runtime-managed)\n ├── HISTORY.md # Channel summarized history (read on demand, runtime-managed)\n ├── log.jsonl # Raw message archive (cold storage)\n ├── context.jsonl # Raw session archive (cold storage)\n ├── scratch/ # Your working directory\n └── skills/ # Channel-specific tools`);\n\n\tsections.push(`## Events\nYou can schedule events that wake you up at specific times or when external things happen. Events are JSON files in \\`${workspacePath}/events/\\`.\n\n### Event Types\n\n**Immediate** - Triggers as soon as harness sees the file.\n\\`\\`\\`json\n{\"type\": \"immediate\", \"channelId\": \"${channelId}\", \"text\": \"New event occurred\"}\n\\`\\`\\`\n\n**One-shot** - Triggers once at a specific time.\n\\`\\`\\`json\n{\"type\": \"one-shot\", \"channelId\": \"${channelId}\", \"text\": \"Reminder\", \"at\": \"2025-12-15T09:00:00+08:00\"}\n\\`\\`\\`\n\n**Periodic** - Triggers on a cron schedule.\n\\`\\`\\`json\n{\"type\": \"periodic\", \"channelId\": \"${channelId}\", \"text\": \"Check inbox\", \"schedule\": \"0 9 * * 1-5\", \"timezone\": \"${Intl.DateTimeFormat().resolvedOptions().timeZone}\"}\n\\`\\`\\`\n\n### Cron Format\n\\`minute hour day-of-month month day-of-week\\`\n\n### Creating Events\nCreate a JSON file under \\`${workspacePath}/events/\\` with the appropriate event payload.\nPrefer the file tools for creating or editing the event file. Use shell commands only when they are the clearest option.\n\n### Silent Completion\nFor periodic events where there's nothing to report, respond with just \\`[SILENT]\\`. This deletes the status message. Use this to avoid spam when periodic checks find nothing.\n\n### Limits\nMaximum 5 events can be queued.`);\n\n\tsections.push(`## Memory\nMemory files are not preloaded into session context. Read them explicitly when memory or history matters.\n\n### Files\n- Workspace memory: ${workspacePath}/MEMORY.md\n Stable shared background memory. Admin-managed. Read on demand.\n- Channel memory: ${channelPath}/MEMORY.md\n Durable channel memory. Runtime-managed via consolidation. You may update this file manually when necessary.\n- Channel history: ${channelPath}/HISTORY.md\n Summarized older channel history. Runtime-managed. Read on demand. Do not maintain this file manually during normal work.\n\n### Runtime Behavior\n- The runtime automatically consolidates channel MEMORY.md and HISTORY.md before compaction or session trimming.\n- Workspace MEMORY.md is not updated by normal runtime consolidation.\n\n### Cold Storage\n- ${channelPath}/log.jsonl is a raw archive. It is not normal memory and is not proactively loaded.\n- ${channelPath}/context.jsonl is a raw session archive. It is not normal memory and is not proactively loaded.\n\nWhen a task depends on prior decisions, preferences, or long-running work, read channel MEMORY.md and HISTORY.md first.`);\n\n\tsections.push(`## System Configuration Log\nMaintain ${workspacePath}/SYSTEM.md to log all environment modifications:\n- Installed packages (apk add, npm install, pip install)\n- Environment variables set\n- Config files modified\n- Skill dependencies installed\n\nUpdate this file whenever you modify the environment.`);\n\n\tsections.push(`## Tools\n- read: Read files\n- edit: Surgical file edits\n- write: Create or overwrite files when needed\n- bash: Run shell commands and external programs\n- subagent: Delegate a focused task to a sub-agent with its own isolated context\n\nEach tool requires a \"label\" parameter (shown to user).`);\n\n\tsections.push(`## Sub-Agents\nYou have a \\`subagent\\` tool for delegating focused work to a separate agent with an isolated context window.\n\n### Predefined Sub-Agents\nPredefined sub-agent definitions live in \\`${subAgentsPath}/\\`.\n${options.subAgentList ? `Available predefined sub-agents:\\n${options.subAgentList}` : \"Available predefined sub-agents: none\"}\n\n### Temporary Inline Sub-Agents\nIf no predefined sub-agent fits, you may define a temporary inline sub-agent directly in the \\`subagent\\` tool call by providing a focused \\`systemPrompt\\` plus optional tools, model, and budget settings.\n\nUse sub-agents when:\n- The task can be decomposed into a focused sub-problem\n- You need a fresh context for heavy file reading, shell work, or review\n- A specialized role would produce better results\n- The main conversation has grown long and you want to offload a bounded task\n\nDo not use sub-agents when:\n- The task is simple and direct\n- The task depends heavily on the full current conversation state\n- The task requires frequent user confirmation\n\nImportant rules:\n- Sub-agents cannot see your conversation history unless you include the needed context in \\`task\\`\n- The runtime injects a small fixed execution context (workspace path, channel id, sandbox), but you must still include task-specific context yourself\n- Sub-agents do not receive the \\`subagent\\` tool, so they cannot create nested agents\n- Prefer predefined sub-agents when one clearly fits\n- Use temporary inline sub-agents only when that extra flexibility is genuinely useful`);\n\n\treturn sections.join(\"\\n\\n\");\n}\n"]}
1
+ {"version":3,"file":"prompt-builder.d.ts","sourceRoot":"","sources":["../src/prompt-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAElD,MAAM,WAAW,yBAAyB;IACzC,YAAY,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,uBAAuB,CACtC,aAAa,EAAE,MAAM,EACrB,SAAS,EAAE,MAAM,EACjB,aAAa,EAAE,aAAa,EAC5B,OAAO,GAAE,yBAA8B,GACrC,MAAM,CAoJR"}
@@ -1 +1 @@
1
- {"version":3,"file":"prompt-builder.js","sourceRoot":"","sources":["../src/prompt-builder.ts"],"names":[],"mappings":"AAMA,MAAM,UAAU,uBAAuB,CACtC,aAAqB,EACrB,SAAiB,EACjB,aAA4B,EAC5B,OAAO,GAA8B,EAAE,EAC9B;IACT,MAAM,WAAW,GAAG,GAAG,aAAa,IAAI,SAAS,EAAE,CAAC;IACpD,MAAM,aAAa,GAAG,GAAG,aAAa,aAAa,CAAC;IACpD,MAAM,QAAQ,GAAG,aAAa,CAAC,IAAI,KAAK,QAAQ,CAAC;IAEjD,MAAM,cAAc,GAAG,QAAQ;QAC9B,CAAC,CAAC;;;uCAGmC;QACrC,CAAC,CAAC;4BACwB,OAAO,CAAC,GAAG,EAAE;uCACF,CAAC;IAEvC,MAAM,QAAQ,GAAa,EAAE,CAAC;IAE9B,QAAQ,CAAC,IAAI,CAAC;;;;;;;;;;;;;EAab,cAAc;;;EAGd,aAAa;;;;;;;YAOT,SAAS;;;;;;gEAM2C,CAAC,CAAC;IAE3D,QAAQ,CAAC,IAAI,CAAC;wHACyG,aAAa;;;;;;sCAM/F,SAAS;;;;;qCAKV,SAAS;;;;;qCAKT,SAAS,qEAAqE,IAAI,CAAC,cAAc,EAAE,CAAC,eAAe,EAAE,CAAC,QAAQ;;;;;;;6BAOtI,aAAa;;;;;;;gCAOV,CAAC,CAAC;IAEjC,QAAQ,CAAC,IAAI,CAAC;;;;sBAIO,aAAa;;oBAEf,WAAW;;qBAEV,WAAW;;;;;;;;IAQ5B,WAAW;IACX,WAAW;;wHAEyG,CAAC,CAAC;IAEzH,QAAQ,CAAC,IAAI,CAAC;WACJ,aAAa;;;;;;sDAM8B,CAAC,CAAC;IAEvD,QAAQ,CAAC,IAAI,CAAC;;;;;;;wDAOyC,CAAC,CAAC;IAEzD,QAAQ,CAAC,IAAI,CAAC;;;;6CAI8B,aAAa;EACxD,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,qCAAqC,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,uCAAuC;;;;;;;;;;;;;;;;;;;;;uFAqBvC,CAAC,CAAC;IAExF,OAAO,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAAA,CAC7B","sourcesContent":["import type { SandboxConfig } from \"./sandbox.js\";\n\nexport interface AppendSystemPromptOptions {\n\tsubAgentList?: string;\n}\n\nexport function buildAppendSystemPrompt(\n\tworkspacePath: string,\n\tchannelId: string,\n\tsandboxConfig: SandboxConfig,\n\toptions: AppendSystemPromptOptions = {},\n): string {\n\tconst channelPath = `${workspacePath}/${channelId}`;\n\tconst subAgentsPath = `${workspacePath}/sub-agents`;\n\tconst isDocker = sandboxConfig.type === \"docker\";\n\n\tconst envDescription = isDocker\n\t\t? `You are running inside a Docker container (Alpine Linux).\n- Bash working directory: / (use cd or absolute paths)\n- Install tools with: apk add <package>\n- Your changes persist across sessions`\n\t\t: `You are running directly on the host machine.\n- Bash working directory: ${process.cwd()}\n- Be careful with system modifications`;\n\n\tconst sections: string[] = [];\n\n\tsections.push(`## Pipiclaw Runtime\nYou are running inside Pipiclaw, a DingTalk-oriented runtime built on top of pi.\n\n## Context\n- For current date/time, use: date\n- You have access to the active session context for this session.\n- Raw transcript files are cold storage. Do not assume they are preloaded.\n\n## Formatting\nUse Markdown for formatting. DingTalk AI Card supports basic Markdown:\nBold: **text**, Italic: *text*, Code: \\`code\\`, Block: \\`\\`\\`code\\`\\`\\`, Links: [text](url)\n\n## Environment\n${envDescription}\n\n## Workspace Layout\n${workspacePath}/\n├── SOUL.md # Your identity/personality (read-only)\n├── AGENTS.md # Custom behavior instructions (read-only)\n├── MEMORY.md # Stable workspace memory (admin-managed, read on demand)\n├── sub-agents/ # Predefined sub-agent definitions\n├── skills/ # Global CLI tools you create\n├── events/ # Scheduled events\n└── ${channelId}/ # This channel\n ├── MEMORY.md # Channel durable memory (read on demand, runtime-managed)\n ├── HISTORY.md # Channel summarized history (read on demand, runtime-managed)\n ├── log.jsonl # Raw message archive (cold storage)\n ├── context.jsonl # Raw session archive (cold storage)\n ├── scratch/ # Your working directory\n └── skills/ # Channel-specific tools`);\n\n\tsections.push(`## Events\nYou can schedule events that wake you up at specific times or when external things happen. Events are JSON files in \\`${workspacePath}/events/\\`.\n\n### Event Types\n\n**Immediate** - Triggers as soon as harness sees the file.\n\\`\\`\\`json\n{\"type\": \"immediate\", \"channelId\": \"${channelId}\", \"text\": \"New event occurred\"}\n\\`\\`\\`\n\n**One-shot** - Triggers once at a specific time.\n\\`\\`\\`json\n{\"type\": \"one-shot\", \"channelId\": \"${channelId}\", \"text\": \"Reminder\", \"at\": \"2025-12-15T09:00:00+08:00\"}\n\\`\\`\\`\n\n**Periodic** - Triggers on a cron schedule.\n\\`\\`\\`json\n{\"type\": \"periodic\", \"channelId\": \"${channelId}\", \"text\": \"Check inbox\", \"schedule\": \"0 9 * * 1-5\", \"timezone\": \"${Intl.DateTimeFormat().resolvedOptions().timeZone}\"}\n\\`\\`\\`\n\n### Cron Format\n\\`minute hour day-of-month month day-of-week\\`\n\n### Creating Events\nCreate a JSON file under \\`${workspacePath}/events/\\` with the appropriate event payload.\nPrefer the file tools for creating or editing the event file. Use shell commands only when they are the clearest option.\n\n### Silent Completion\nFor periodic events where there's nothing to report, respond with just \\`[SILENT]\\`. This deletes the status message. Use this to avoid spam when periodic checks find nothing.\n\n### Limits\nMaximum 5 events can be queued.`);\n\n\tsections.push(`## Memory\nMemory files are not preloaded into session context. Read them explicitly when memory or history matters.\n\n### Files\n- Workspace memory: ${workspacePath}/MEMORY.md\n Stable shared background memory. Admin-managed. Read on demand.\n- Channel memory: ${channelPath}/MEMORY.md\n Durable channel memory. Runtime-managed via consolidation. You may update this file manually when necessary.\n- Channel history: ${channelPath}/HISTORY.md\n Summarized older channel history. Runtime-managed. Read on demand. Do not maintain this file manually during normal work.\n\n### Runtime Behavior\n- The runtime automatically consolidates channel MEMORY.md and HISTORY.md before compaction or session trimming.\n- Workspace MEMORY.md is not updated by normal runtime consolidation.\n\n### Cold Storage\n- ${channelPath}/log.jsonl is a raw archive. It is not normal memory and is not proactively loaded.\n- ${channelPath}/context.jsonl is a raw session archive. It is not normal memory and is not proactively loaded.\n\nWhen a task depends on prior decisions, preferences, or long-running work, read channel MEMORY.md and HISTORY.md first.`);\n\n\tsections.push(`## System Configuration Log\nMaintain ${workspacePath}/SYSTEM.md to log all environment modifications:\n- Installed packages (apk add, npm install, pip install)\n- Environment variables set\n- Config files modified\n- Skill dependencies installed\n\nUpdate this file whenever you modify the environment.`);\n\n\tsections.push(`## Tools\n- read: Read files\n- edit: Surgical file edits\n- write: Create or overwrite files when needed\n- bash: Run shell commands and external programs\n- subagent: Delegate a focused task to a sub-agent with its own isolated context\n\nEach tool requires a \"label\" parameter (shown to user).`);\n\n\tsections.push(`## Sub-Agents\nYou have a \\`subagent\\` tool for delegating focused work to a separate agent with an isolated context window.\n\n### Predefined Sub-Agents\nPredefined sub-agent definitions live in \\`${subAgentsPath}/\\`.\n${options.subAgentList ? `Available predefined sub-agents:\\n${options.subAgentList}` : \"Available predefined sub-agents: none\"}\n\n### Temporary Inline Sub-Agents\nIf no predefined sub-agent fits, you may define a temporary inline sub-agent directly in the \\`subagent\\` tool call by providing a focused \\`systemPrompt\\` plus optional tools, model, and budget settings.\n\nUse sub-agents when:\n- The task can be decomposed into a focused sub-problem\n- You need a fresh context for heavy file reading, shell work, or review\n- A specialized role would produce better results\n- The main conversation has grown long and you want to offload a bounded task\n\nDo not use sub-agents when:\n- The task is simple and direct\n- The task depends heavily on the full current conversation state\n- The task requires frequent user confirmation\n\nImportant rules:\n- Sub-agents cannot see your conversation history unless you include the needed context in \\`task\\`\n- The runtime injects a small fixed execution context (workspace path, channel id, sandbox), but you must still include task-specific context yourself\n- Sub-agents do not receive the \\`subagent\\` tool, so they cannot create nested agents\n- Prefer predefined sub-agents when one clearly fits\n- Use temporary inline sub-agents only when that extra flexibility is genuinely useful`);\n\n\treturn sections.join(\"\\n\\n\");\n}\n"]}
1
+ {"version":3,"file":"prompt-builder.js","sourceRoot":"","sources":["../src/prompt-builder.ts"],"names":[],"mappings":"AAMA,MAAM,UAAU,uBAAuB,CACtC,aAAqB,EACrB,SAAiB,EACjB,aAA4B,EAC5B,UAAqC,EAAE;IAEvC,MAAM,WAAW,GAAG,GAAG,aAAa,IAAI,SAAS,EAAE,CAAC;IACpD,MAAM,aAAa,GAAG,GAAG,aAAa,aAAa,CAAC;IACpD,MAAM,QAAQ,GAAG,aAAa,CAAC,IAAI,KAAK,QAAQ,CAAC;IAEjD,MAAM,cAAc,GAAG,QAAQ;QAC9B,CAAC,CAAC;;;uCAGmC;QACrC,CAAC,CAAC;4BACwB,OAAO,CAAC,GAAG,EAAE;uCACF,CAAC;IAEvC,MAAM,QAAQ,GAAa,EAAE,CAAC;IAE9B,QAAQ,CAAC,IAAI,CAAC;;;;;;;;;;;;;EAab,cAAc;;;EAGd,aAAa;;;;;;;MAOT,SAAS;;;;;;0DAM2C,CAAC,CAAC;IAE3D,QAAQ,CAAC,IAAI,CAAC;wHACyG,aAAa;;;;;;sCAM/F,SAAS;;;;;qCAKV,SAAS;;;;;qCAKT,SAAS,qEAAqE,IAAI,CAAC,cAAc,EAAE,CAAC,eAAe,EAAE,CAAC,QAAQ;;;;;;;6BAOtI,aAAa;;;;;;;gCAOV,CAAC,CAAC;IAEjC,QAAQ,CAAC,IAAI,CAAC;;;;sBAIO,aAAa;;oBAEf,WAAW;;qBAEV,WAAW;;;;;;;;IAQ5B,WAAW;IACX,WAAW;;wHAEyG,CAAC,CAAC;IAEzH,QAAQ,CAAC,IAAI,CAAC;WACJ,aAAa;;;;;;sDAM8B,CAAC,CAAC;IAEvD,QAAQ,CAAC,IAAI,CAAC;;;;;;;wDAOyC,CAAC,CAAC;IAEzD,QAAQ,CAAC,IAAI,CAAC;;;;6CAI8B,aAAa;EACxD,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,qCAAqC,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,uCAAuC;;;;;;;;;;;;;;;;;;;;;uFAqBvC,CAAC,CAAC;IAExF,OAAO,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAC9B,CAAC","sourcesContent":["import type { SandboxConfig } from \"./sandbox.js\";\n\nexport interface AppendSystemPromptOptions {\n\tsubAgentList?: string;\n}\n\nexport function buildAppendSystemPrompt(\n\tworkspacePath: string,\n\tchannelId: string,\n\tsandboxConfig: SandboxConfig,\n\toptions: AppendSystemPromptOptions = {},\n): string {\n\tconst channelPath = `${workspacePath}/${channelId}`;\n\tconst subAgentsPath = `${workspacePath}/sub-agents`;\n\tconst isDocker = sandboxConfig.type === \"docker\";\n\n\tconst envDescription = isDocker\n\t\t? `You are running inside a Docker container (Alpine Linux).\n- Bash working directory: / (use cd or absolute paths)\n- Install tools with: apk add <package>\n- Your changes persist across sessions`\n\t\t: `You are running directly on the host machine.\n- Bash working directory: ${process.cwd()}\n- Be careful with system modifications`;\n\n\tconst sections: string[] = [];\n\n\tsections.push(`## Pipiclaw Runtime\nYou are running inside Pipiclaw, a DingTalk-oriented runtime built on top of pi.\n\n## Context\n- For current date/time, use: date\n- You have access to the active session context for this session.\n- Raw transcript files are cold storage. Do not assume they are preloaded.\n\n## Formatting\nUse Markdown for formatting. DingTalk AI Card supports basic Markdown:\nBold: **text**, Italic: *text*, Code: \\`code\\`, Block: \\`\\`\\`code\\`\\`\\`, Links: [text](url)\n\n## Environment\n${envDescription}\n\n## Workspace Layout\n${workspacePath}/\n├── SOUL.md # Your identity/personality (read-only)\n├── AGENTS.md # Custom behavior instructions (read-only)\n├── MEMORY.md # Stable workspace memory (admin-managed, read on demand)\n├── sub-agents/ # Predefined sub-agent definitions\n├── skills/ # Global CLI tools you create\n├── events/ # Scheduled events\n└── ${channelId}/ # This channel\n ├── MEMORY.md # Channel durable memory (read on demand, runtime-managed)\n ├── HISTORY.md # Channel summarized history (read on demand, runtime-managed)\n ├── log.jsonl # Raw message archive (cold storage)\n ├── context.jsonl # Raw session archive (cold storage)\n ├── scratch/ # Your working directory\n └── skills/ # Channel-specific tools`);\n\n\tsections.push(`## Events\nYou can schedule events that wake you up at specific times or when external things happen. Events are JSON files in \\`${workspacePath}/events/\\`.\n\n### Event Types\n\n**Immediate** - Triggers as soon as harness sees the file.\n\\`\\`\\`json\n{\"type\": \"immediate\", \"channelId\": \"${channelId}\", \"text\": \"New event occurred\"}\n\\`\\`\\`\n\n**One-shot** - Triggers once at a specific time.\n\\`\\`\\`json\n{\"type\": \"one-shot\", \"channelId\": \"${channelId}\", \"text\": \"Reminder\", \"at\": \"2025-12-15T09:00:00+08:00\"}\n\\`\\`\\`\n\n**Periodic** - Triggers on a cron schedule.\n\\`\\`\\`json\n{\"type\": \"periodic\", \"channelId\": \"${channelId}\", \"text\": \"Check inbox\", \"schedule\": \"0 9 * * 1-5\", \"timezone\": \"${Intl.DateTimeFormat().resolvedOptions().timeZone}\"}\n\\`\\`\\`\n\n### Cron Format\n\\`minute hour day-of-month month day-of-week\\`\n\n### Creating Events\nCreate a JSON file under \\`${workspacePath}/events/\\` with the appropriate event payload.\nPrefer the file tools for creating or editing the event file. Use shell commands only when they are the clearest option.\n\n### Silent Completion\nFor periodic events where there's nothing to report, respond with just \\`[SILENT]\\`. This deletes the status message. Use this to avoid spam when periodic checks find nothing.\n\n### Limits\nMaximum 5 events can be queued.`);\n\n\tsections.push(`## Memory\nMemory files are not preloaded into session context. Read them explicitly when memory or history matters.\n\n### Files\n- Workspace memory: ${workspacePath}/MEMORY.md\n Stable shared background memory. Admin-managed. Read on demand.\n- Channel memory: ${channelPath}/MEMORY.md\n Durable channel memory. Runtime-managed via consolidation. You may update this file manually when necessary.\n- Channel history: ${channelPath}/HISTORY.md\n Summarized older channel history. Runtime-managed. Read on demand. Do not maintain this file manually during normal work.\n\n### Runtime Behavior\n- The runtime automatically consolidates channel MEMORY.md and HISTORY.md before compaction or session trimming.\n- Workspace MEMORY.md is not updated by normal runtime consolidation.\n\n### Cold Storage\n- ${channelPath}/log.jsonl is a raw archive. It is not normal memory and is not proactively loaded.\n- ${channelPath}/context.jsonl is a raw session archive. It is not normal memory and is not proactively loaded.\n\nWhen a task depends on prior decisions, preferences, or long-running work, read channel MEMORY.md and HISTORY.md first.`);\n\n\tsections.push(`## System Configuration Log\nMaintain ${workspacePath}/SYSTEM.md to log all environment modifications:\n- Installed packages (apk add, npm install, pip install)\n- Environment variables set\n- Config files modified\n- Skill dependencies installed\n\nUpdate this file whenever you modify the environment.`);\n\n\tsections.push(`## Tools\n- read: Read files\n- edit: Surgical file edits\n- write: Create or overwrite files when needed\n- bash: Run shell commands and external programs\n- subagent: Delegate a focused task to a sub-agent with its own isolated context\n\nEach tool requires a \"label\" parameter (shown to user).`);\n\n\tsections.push(`## Sub-Agents\nYou have a \\`subagent\\` tool for delegating focused work to a separate agent with an isolated context window.\n\n### Predefined Sub-Agents\nPredefined sub-agent definitions live in \\`${subAgentsPath}/\\`.\n${options.subAgentList ? `Available predefined sub-agents:\\n${options.subAgentList}` : \"Available predefined sub-agents: none\"}\n\n### Temporary Inline Sub-Agents\nIf no predefined sub-agent fits, you may define a temporary inline sub-agent directly in the \\`subagent\\` tool call by providing a focused \\`systemPrompt\\` plus optional tools, model, and budget settings.\n\nUse sub-agents when:\n- The task can be decomposed into a focused sub-problem\n- You need a fresh context for heavy file reading, shell work, or review\n- A specialized role would produce better results\n- The main conversation has grown long and you want to offload a bounded task\n\nDo not use sub-agents when:\n- The task is simple and direct\n- The task depends heavily on the full current conversation state\n- The task requires frequent user confirmation\n\nImportant rules:\n- Sub-agents cannot see your conversation history unless you include the needed context in \\`task\\`\n- The runtime injects a small fixed execution context (workspace path, channel id, sandbox), but you must still include task-specific context yourself\n- Sub-agents do not receive the \\`subagent\\` tool, so they cannot create nested agents\n- Prefer predefined sub-agents when one clearly fits\n- Use temporary inline sub-agents only when that extra flexibility is genuinely useful`);\n\n\treturn sections.join(\"\\n\\n\");\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"sandbox.d.ts","sourceRoot":"","sources":["../src/sandbox.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,aAAa,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC;AAErF,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,aAAa,CAc5D;AAED,wBAAsB,eAAe,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CA4B1E;AAoBD;;GAEG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,aAAa,GAAG,QAAQ,CAK9D;AAED,MAAM,WAAW,QAAQ;IACxB;;OAEG;IACH,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAElE;;;;OAIG;IACH,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC;CAC3C;AAED,MAAM,WAAW,WAAW;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,UAAU;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACb","sourcesContent":["import { spawn } from \"child_process\";\nimport { shellEscape } from \"./shell-escape.js\";\n\nexport type SandboxConfig = { type: \"host\" } | { type: \"docker\"; container: string };\n\nexport function parseSandboxArg(value: string): SandboxConfig {\n\tif (value === \"host\") {\n\t\treturn { type: \"host\" };\n\t}\n\tif (value.startsWith(\"docker:\")) {\n\t\tconst container = value.slice(\"docker:\".length);\n\t\tif (!container) {\n\t\t\tconsole.error(\"Error: docker sandbox requires container name (e.g., docker:pipiclaw-sandbox)\");\n\t\t\tprocess.exit(1);\n\t\t}\n\t\treturn { type: \"docker\", container };\n\t}\n\tconsole.error(`Error: Invalid sandbox type '${value}'. Use 'host' or 'docker:<container-name>'`);\n\tprocess.exit(1);\n}\n\nexport async function validateSandbox(config: SandboxConfig): Promise<void> {\n\tif (config.type === \"host\") {\n\t\treturn;\n\t}\n\n\t// Check if Docker is available\n\ttry {\n\t\tawait execSimple(\"docker\", [\"--version\"]);\n\t} catch {\n\t\tconsole.error(\"Error: Docker is not installed or not in PATH\");\n\t\tprocess.exit(1);\n\t}\n\n\t// Check if container exists and is running\n\ttry {\n\t\tconst result = await execSimple(\"docker\", [\"inspect\", \"-f\", \"{{.State.Running}}\", config.container]);\n\t\tif (result.trim() !== \"true\") {\n\t\t\tconsole.error(`Error: Container '${config.container}' is not running.`);\n\t\t\tconsole.error(`Start it with: docker start ${config.container}`);\n\t\t\tprocess.exit(1);\n\t\t}\n\t} catch {\n\t\tconsole.error(`Error: Container '${config.container}' does not exist.`);\n\t\tconsole.error(\"Create it with: ./docker.sh create <data-dir>\");\n\t\tprocess.exit(1);\n\t}\n\n\tconsole.log(` Docker container '${config.container}' is running.`);\n}\n\nfunction execSimple(cmd: string, args: string[]): Promise<string> {\n\treturn new Promise((resolve, reject) => {\n\t\tconst child = spawn(cmd, args, { stdio: [\"ignore\", \"pipe\", \"pipe\"] });\n\t\tlet stdout = \"\";\n\t\tlet stderr = \"\";\n\t\tchild.stdout?.on(\"data\", (d) => {\n\t\t\tstdout += d;\n\t\t});\n\t\tchild.stderr?.on(\"data\", (d) => {\n\t\t\tstderr += d;\n\t\t});\n\t\tchild.on(\"close\", (code) => {\n\t\t\tif (code === 0) resolve(stdout);\n\t\t\telse reject(new Error(stderr || `Exit code ${code}`));\n\t\t});\n\t});\n}\n\n/**\n * Create an executor that runs commands either on host or in Docker container\n */\nexport function createExecutor(config: SandboxConfig): Executor {\n\tif (config.type === \"host\") {\n\t\treturn new HostExecutor();\n\t}\n\treturn new DockerExecutor(config.container);\n}\n\nexport interface Executor {\n\t/**\n\t * Execute a bash command\n\t */\n\texec(command: string, options?: ExecOptions): Promise<ExecResult>;\n\n\t/**\n\t * Get the workspace path prefix for this executor\n\t * Host: returns the actual path\n\t * Docker: returns /workspace\n\t */\n\tgetWorkspacePath(hostPath: string): string;\n}\n\nexport interface ExecOptions {\n\ttimeout?: number;\n\tsignal?: AbortSignal;\n\tstdin?: string;\n}\n\nexport interface ExecResult {\n\tstdout: string;\n\tstderr: string;\n\tcode: number;\n}\n\nclass HostExecutor implements Executor {\n\tasync exec(command: string, options?: ExecOptions): Promise<ExecResult> {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst shell = process.platform === \"win32\" ? \"cmd\" : \"sh\";\n\t\t\tconst shellArgs = process.platform === \"win32\" ? [\"/c\"] : [\"-c\"];\n\t\t\tconst child = (() => {\n\t\t\t\ttry {\n\t\t\t\t\treturn spawn(shell, [...shellArgs, command], {\n\t\t\t\t\t\tdetached: true,\n\t\t\t\t\t\tstdio: [\"pipe\", \"pipe\", \"pipe\"],\n\t\t\t\t\t});\n\t\t\t\t} catch (err) {\n\t\t\t\t\treject(err instanceof Error ? err : new Error(String(err)));\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\t\t\t})();\n\n\t\t\tif (!child) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet stdout = \"\";\n\t\t\tlet stderr = \"\";\n\t\t\tlet timedOut = false;\n\t\t\tlet settled = false;\n\n\t\t\tconst cleanup = () => {\n\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\tif (options?.signal) {\n\t\t\t\t\toptions.signal.removeEventListener(\"abort\", onAbort);\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tconst rejectOnce = (err: Error) => {\n\t\t\t\tif (settled) return;\n\t\t\t\tsettled = true;\n\t\t\t\tcleanup();\n\t\t\t\treject(err);\n\t\t\t};\n\n\t\t\tconst resolveOnce = (result: ExecResult) => {\n\t\t\t\tif (settled) return;\n\t\t\t\tsettled = true;\n\t\t\t\tcleanup();\n\t\t\t\tresolve(result);\n\t\t\t};\n\n\t\t\tconst timeoutHandle =\n\t\t\t\toptions?.timeout && options.timeout > 0\n\t\t\t\t\t? setTimeout(() => {\n\t\t\t\t\t\t\ttimedOut = true;\n\t\t\t\t\t\t\tkillProcessTree(child.pid!);\n\t\t\t\t\t\t}, options.timeout * 1000)\n\t\t\t\t\t: undefined;\n\n\t\t\tconst onAbort = () => {\n\t\t\t\tif (child.pid) killProcessTree(child.pid);\n\t\t\t};\n\n\t\t\tif (options?.signal) {\n\t\t\t\tif (options.signal.aborted) {\n\t\t\t\t\tonAbort();\n\t\t\t\t} else {\n\t\t\t\t\toptions.signal.addEventListener(\"abort\", onAbort, { once: true });\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tchild.stdout?.on(\"data\", (data) => {\n\t\t\t\tstdout += data.toString();\n\t\t\t\tif (stdout.length > 10 * 1024 * 1024) {\n\t\t\t\t\tstdout = stdout.slice(0, 10 * 1024 * 1024);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tchild.stderr?.on(\"data\", (data) => {\n\t\t\t\tstderr += data.toString();\n\t\t\t\tif (stderr.length > 10 * 1024 * 1024) {\n\t\t\t\t\tstderr = stderr.slice(0, 10 * 1024 * 1024);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tchild.on(\"error\", (err) => {\n\t\t\t\trejectOnce(err instanceof Error ? err : new Error(String(err)));\n\t\t\t});\n\n\t\t\tchild.on(\"close\", (code) => {\n\t\t\t\tif (options?.signal?.aborted) {\n\t\t\t\t\trejectOnce(new Error(`${stdout}\\n${stderr}\\nCommand aborted`.trim()));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (timedOut) {\n\t\t\t\t\trejectOnce(\n\t\t\t\t\t\tnew Error(`${stdout}\\n${stderr}\\nCommand timed out after ${options?.timeout} seconds`.trim()),\n\t\t\t\t\t);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tresolveOnce({ stdout, stderr, code: code ?? 0 });\n\t\t\t});\n\n\t\t\tif (options?.stdin !== undefined) {\n\t\t\t\tchild.stdin?.on(\"error\", (err) => {\n\t\t\t\t\tif ((err as NodeJS.ErrnoException).code === \"EPIPE\") return;\n\t\t\t\t\trejectOnce(err instanceof Error ? err : new Error(String(err)));\n\t\t\t\t});\n\t\t\t\tchild.stdin?.end(options.stdin);\n\t\t\t} else {\n\t\t\t\tchild.stdin?.end();\n\t\t\t}\n\t\t});\n\t}\n\n\tgetWorkspacePath(hostPath: string): string {\n\t\treturn hostPath;\n\t}\n}\n\nclass DockerExecutor implements Executor {\n\tconstructor(private container: string) {}\n\n\tasync exec(command: string, options?: ExecOptions): Promise<ExecResult> {\n\t\t// Wrap command for docker exec\n\t\tconst interactive = options?.stdin !== undefined ? \"-i \" : \"\";\n\t\tconst dockerCmd = `docker exec ${interactive}${this.container} sh -c ${shellEscape(command)}`;\n\t\tconst hostExecutor = new HostExecutor();\n\t\treturn hostExecutor.exec(dockerCmd, options);\n\t}\n\n\tgetWorkspacePath(_hostPath: string): string {\n\t\t// Docker container sees /workspace\n\t\treturn \"/workspace\";\n\t}\n}\n\nfunction killProcessTree(pid: number): void {\n\tif (process.platform === \"win32\") {\n\t\ttry {\n\t\t\tspawn(\"taskkill\", [\"/F\", \"/T\", \"/PID\", String(pid)], {\n\t\t\t\tstdio: \"ignore\",\n\t\t\t\tdetached: true,\n\t\t\t});\n\t\t} catch {\n\t\t\t// Ignore errors\n\t\t}\n\t} else {\n\t\ttry {\n\t\t\tprocess.kill(-pid, \"SIGKILL\");\n\t\t} catch {\n\t\t\ttry {\n\t\t\t\tprocess.kill(pid, \"SIGKILL\");\n\t\t\t} catch {\n\t\t\t\t// Process already dead\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
1
+ {"version":3,"file":"sandbox.d.ts","sourceRoot":"","sources":["../src/sandbox.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,aAAa,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC;AAErF,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,aAAa,CAc5D;AAED,wBAAsB,eAAe,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CA4B1E;AAoBD;;GAEG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,aAAa,GAAG,QAAQ,CAK9D;AAED,MAAM,WAAW,QAAQ;IACxB;;OAEG;IACH,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAElE;;;;OAIG;IACH,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC;CAC3C;AAED,MAAM,WAAW,WAAW;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,UAAU;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACb"}
package/dist/sandbox.js CHANGED
@@ -178,7 +178,6 @@ class HostExecutor {
178
178
  }
179
179
  }
180
180
  class DockerExecutor {
181
- container;
182
181
  constructor(container) {
183
182
  this.container = container;
184
183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I,CAAC;YACJ,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QAC/B,CAAC;QAAC,MAAM,CAAC;YACR,IAAI,CAAC;gBACJ,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;YAC9B,CAAC;YAAC,MAAM,CAAC;gBACR,uBAAuB;YACxB,CAAC;QACF,CAAC;IACF,CAAC;AAAA,CACD","sourcesContent":["import { spawn } from \"child_process\";\nimport { shellEscape } from \"./shell-escape.js\";\n\nexport type SandboxConfig = { type: \"host\" } | { type: \"docker\"; container: string };\n\nexport function parseSandboxArg(value: string): SandboxConfig {\n\tif (value === \"host\") {\n\t\treturn { type: \"host\" };\n\t}\n\tif (value.startsWith(\"docker:\")) {\n\t\tconst container = value.slice(\"docker:\".length);\n\t\tif (!container) {\n\t\t\tconsole.error(\"Error: docker sandbox requires container name (e.g., docker:pipiclaw-sandbox)\");\n\t\t\tprocess.exit(1);\n\t\t}\n\t\treturn { type: \"docker\", container };\n\t}\n\tconsole.error(`Error: Invalid sandbox type '${value}'. Use 'host' or 'docker:<container-name>'`);\n\tprocess.exit(1);\n}\n\nexport async function validateSandbox(config: SandboxConfig): Promise<void> {\n\tif (config.type === \"host\") {\n\t\treturn;\n\t}\n\n\t// Check if Docker is available\n\ttry {\n\t\tawait execSimple(\"docker\", [\"--version\"]);\n\t} catch {\n\t\tconsole.error(\"Error: Docker is not installed or not in PATH\");\n\t\tprocess.exit(1);\n\t}\n\n\t// Check if container exists and is running\n\ttry {\n\t\tconst result = await execSimple(\"docker\", [\"inspect\", \"-f\", \"{{.State.Running}}\", config.container]);\n\t\tif (result.trim() !== \"true\") {\n\t\t\tconsole.error(`Error: Container '${config.container}' is not running.`);\n\t\t\tconsole.error(`Start it with: docker start ${config.container}`);\n\t\t\tprocess.exit(1);\n\t\t}\n\t} catch {\n\t\tconsole.error(`Error: Container '${config.container}' does not exist.`);\n\t\tconsole.error(\"Create it with: ./docker.sh create <data-dir>\");\n\t\tprocess.exit(1);\n\t}\n\n\tconsole.log(` Docker container '${config.container}' is running.`);\n}\n\nfunction execSimple(cmd: string, args: string[]): Promise<string> {\n\treturn new Promise((resolve, reject) => {\n\t\tconst child = spawn(cmd, args, { stdio: [\"ignore\", \"pipe\", \"pipe\"] });\n\t\tlet stdout = \"\";\n\t\tlet stderr = \"\";\n\t\tchild.stdout?.on(\"data\", (d) => {\n\t\t\tstdout += d;\n\t\t});\n\t\tchild.stderr?.on(\"data\", (d) => {\n\t\t\tstderr += d;\n\t\t});\n\t\tchild.on(\"close\", (code) => {\n\t\t\tif (code === 0) resolve(stdout);\n\t\t\telse reject(new Error(stderr || `Exit code ${code}`));\n\t\t});\n\t});\n}\n\n/**\n * Create an executor that runs commands either on host or in Docker container\n */\nexport function createExecutor(config: SandboxConfig): Executor {\n\tif (config.type === \"host\") {\n\t\treturn new HostExecutor();\n\t}\n\treturn new DockerExecutor(config.container);\n}\n\nexport interface Executor {\n\t/**\n\t * Execute a bash command\n\t */\n\texec(command: string, options?: ExecOptions): Promise<ExecResult>;\n\n\t/**\n\t * Get the workspace path prefix for this executor\n\t * Host: returns the actual path\n\t * Docker: returns /workspace\n\t */\n\tgetWorkspacePath(hostPath: string): string;\n}\n\nexport interface ExecOptions {\n\ttimeout?: number;\n\tsignal?: AbortSignal;\n\tstdin?: string;\n}\n\nexport interface ExecResult {\n\tstdout: string;\n\tstderr: string;\n\tcode: number;\n}\n\nclass HostExecutor implements Executor {\n\tasync exec(command: string, options?: ExecOptions): Promise<ExecResult> {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst shell = process.platform === \"win32\" ? \"cmd\" : \"sh\";\n\t\t\tconst shellArgs = process.platform === \"win32\" ? [\"/c\"] : [\"-c\"];\n\t\t\tconst child = (() => {\n\t\t\t\ttry {\n\t\t\t\t\treturn spawn(shell, [...shellArgs, command], {\n\t\t\t\t\t\tdetached: true,\n\t\t\t\t\t\tstdio: [\"pipe\", \"pipe\", \"pipe\"],\n\t\t\t\t\t});\n\t\t\t\t} catch (err) {\n\t\t\t\t\treject(err instanceof Error ? err : new Error(String(err)));\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\t\t\t})();\n\n\t\t\tif (!child) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet stdout = \"\";\n\t\t\tlet stderr = \"\";\n\t\t\tlet timedOut = false;\n\t\t\tlet settled = false;\n\n\t\t\tconst cleanup = () => {\n\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\tif (options?.signal) {\n\t\t\t\t\toptions.signal.removeEventListener(\"abort\", onAbort);\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tconst rejectOnce = (err: Error) => {\n\t\t\t\tif (settled) return;\n\t\t\t\tsettled = true;\n\t\t\t\tcleanup();\n\t\t\t\treject(err);\n\t\t\t};\n\n\t\t\tconst resolveOnce = (result: ExecResult) => {\n\t\t\t\tif (settled) return;\n\t\t\t\tsettled = true;\n\t\t\t\tcleanup();\n\t\t\t\tresolve(result);\n\t\t\t};\n\n\t\t\tconst timeoutHandle =\n\t\t\t\toptions?.timeout && options.timeout > 0\n\t\t\t\t\t? setTimeout(() => {\n\t\t\t\t\t\t\ttimedOut = true;\n\t\t\t\t\t\t\tkillProcessTree(child.pid!);\n\t\t\t\t\t\t}, options.timeout * 1000)\n\t\t\t\t\t: undefined;\n\n\t\t\tconst onAbort = () => {\n\t\t\t\tif (child.pid) killProcessTree(child.pid);\n\t\t\t};\n\n\t\t\tif (options?.signal) {\n\t\t\t\tif (options.signal.aborted) {\n\t\t\t\t\tonAbort();\n\t\t\t\t} else {\n\t\t\t\t\toptions.signal.addEventListener(\"abort\", onAbort, { once: true });\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tchild.stdout?.on(\"data\", (data) => {\n\t\t\t\tstdout += data.toString();\n\t\t\t\tif (stdout.length > 10 * 1024 * 1024) {\n\t\t\t\t\tstdout = stdout.slice(0, 10 * 1024 * 1024);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tchild.stderr?.on(\"data\", (data) => {\n\t\t\t\tstderr += data.toString();\n\t\t\t\tif (stderr.length > 10 * 1024 * 1024) {\n\t\t\t\t\tstderr = stderr.slice(0, 10 * 1024 * 1024);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tchild.on(\"error\", (err) => {\n\t\t\t\trejectOnce(err instanceof Error ? err : new Error(String(err)));\n\t\t\t});\n\n\t\t\tchild.on(\"close\", (code) => {\n\t\t\t\tif (options?.signal?.aborted) {\n\t\t\t\t\trejectOnce(new Error(`${stdout}\\n${stderr}\\nCommand aborted`.trim()));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (timedOut) {\n\t\t\t\t\trejectOnce(\n\t\t\t\t\t\tnew Error(`${stdout}\\n${stderr}\\nCommand timed out after ${options?.timeout} seconds`.trim()),\n\t\t\t\t\t);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tresolveOnce({ stdout, stderr, code: code ?? 0 });\n\t\t\t});\n\n\t\t\tif (options?.stdin !== undefined) {\n\t\t\t\tchild.stdin?.on(\"error\", (err) => {\n\t\t\t\t\tif ((err as NodeJS.ErrnoException).code === \"EPIPE\") return;\n\t\t\t\t\trejectOnce(err instanceof Error ? err : new Error(String(err)));\n\t\t\t\t});\n\t\t\t\tchild.stdin?.end(options.stdin);\n\t\t\t} else {\n\t\t\t\tchild.stdin?.end();\n\t\t\t}\n\t\t});\n\t}\n\n\tgetWorkspacePath(hostPath: string): string {\n\t\treturn hostPath;\n\t}\n}\n\nclass DockerExecutor implements Executor {\n\tconstructor(private container: string) {}\n\n\tasync exec(command: string, options?: ExecOptions): Promise<ExecResult> {\n\t\t// Wrap command for docker exec\n\t\tconst interactive = options?.stdin !== undefined ? \"-i \" : \"\";\n\t\tconst dockerCmd = `docker exec ${interactive}${this.container} sh -c ${shellEscape(command)}`;\n\t\tconst hostExecutor = new HostExecutor();\n\t\treturn hostExecutor.exec(dockerCmd, options);\n\t}\n\n\tgetWorkspacePath(_hostPath: string): string {\n\t\t// Docker container sees /workspace\n\t\treturn \"/workspace\";\n\t}\n}\n\nfunction killProcessTree(pid: number): void {\n\tif (process.platform === \"win32\") {\n\t\ttry {\n\t\t\tspawn(\"taskkill\", [\"/F\", \"/T\", \"/PID\", String(pid)], {\n\t\t\t\tstdio: \"ignore\",\n\t\t\t\tdetached: true,\n\t\t\t});\n\t\t} catch {\n\t\t\t// Ignore errors\n\t\t}\n\t} else {\n\t\ttry {\n\t\t\tprocess.kill(-pid, \"SIGKILL\");\n\t\t} catch {\n\t\t\ttry {\n\t\t\t\tprocess.kill(pid, \"SIGKILL\");\n\t\t\t} catch {\n\t\t\t\t// Process already dead\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
1
+ {"version":3,"file":"sandbox.js","sourceRoot":"","sources":["../src/sandbox.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAIhD,MAAM,UAAU,eAAe,CAAC,KAAa;IAC5C,IAAI,KAAK,KAAK,MAAM,EAAE,CAAC;QACtB,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;IACzB,CAAC;IACD,IAAI,KAAK,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;QACjC,MAAM,SAAS,GAAG,KAAK,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;QAChD,IAAI,CAAC,SAAS,EAAE,CAAC;YAChB,OAAO,CAAC,KAAK,CAAC,+EAA+E,CAAC,CAAC;YAC/F,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACjB,CAAC;QACD,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC;IACtC,CAAC;IACD,OAAO,CAAC,KAAK,CAAC,gCAAgC,KAAK,4CAA4C,CAAC,CAAC;IACjG,OAAO,CAAC,I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AK,OAAO,EAAE,CAAC;QAClC,IAAI,CAAC;YACJ,KAAK,CAAC,UAAU,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE;gBACpD,KAAK,EAAE,QAAQ;gBACf,QAAQ,EAAE,IAAI;aACd,CAAC,CAAC;QACJ,CAAC;QAAC,MAAM,CAAC;YACR,gBAAgB;QACjB,CAAC;IACF,CAAC;SAAM,CAAC;QACP,IAAI,CAAC;YACJ,OAAO,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QAC/B,CAAC;QAAC,MAAM,CAAC;YACR,IAAI,CAAC;gBACJ,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;YAC9B,CAAC;YAAC,MAAM,CAAC;gBACR,uBAAuB;YACxB,CAAC;QACF,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["import { spawn } from \"child_process\";\nimport { shellEscape } from \"./shell-escape.js\";\n\nexport type SandboxConfig = { type: \"host\" } | { type: \"docker\"; container: string };\n\nexport function parseSandboxArg(value: string): SandboxConfig {\n\tif (value === \"host\") {\n\t\treturn { type: \"host\" };\n\t}\n\tif (value.startsWith(\"docker:\")) {\n\t\tconst container = value.slice(\"docker:\".length);\n\t\tif (!container) {\n\t\t\tconsole.error(\"Error: docker sandbox requires container name (e.g., docker:pipiclaw-sandbox)\");\n\t\t\tprocess.exit(1);\n\t\t}\n\t\treturn { type: \"docker\", container };\n\t}\n\tconsole.error(`Error: Invalid sandbox type '${value}'. Use 'host' or 'docker:<container-name>'`);\n\tprocess.exit(1);\n}\n\nexport async function validateSandbox(config: SandboxConfig): Promise<void> {\n\tif (config.type === \"host\") {\n\t\treturn;\n\t}\n\n\t// Check if Docker is available\n\ttry {\n\t\tawait execSimple(\"docker\", [\"--version\"]);\n\t} catch {\n\t\tconsole.error(\"Error: Docker is not installed or not in PATH\");\n\t\tprocess.exit(1);\n\t}\n\n\t// Check if container exists and is running\n\ttry {\n\t\tconst result = await execSimple(\"docker\", [\"inspect\", \"-f\", \"{{.State.Running}}\", config.container]);\n\t\tif (result.trim() !== \"true\") {\n\t\t\tconsole.error(`Error: Container '${config.container}' is not running.`);\n\t\t\tconsole.error(`Start it with: docker start ${config.container}`);\n\t\t\tprocess.exit(1);\n\t\t}\n\t} catch {\n\t\tconsole.error(`Error: Container '${config.container}' does not exist.`);\n\t\tconsole.error(\"Create it with: ./docker.sh create <data-dir>\");\n\t\tprocess.exit(1);\n\t}\n\n\tconsole.log(` Docker container '${config.container}' is running.`);\n}\n\nfunction execSimple(cmd: string, args: string[]): Promise<string> {\n\treturn new Promise((resolve, reject) => {\n\t\tconst child = spawn(cmd, args, { stdio: [\"ignore\", \"pipe\", \"pipe\"] });\n\t\tlet stdout = \"\";\n\t\tlet stderr = \"\";\n\t\tchild.stdout?.on(\"data\", (d) => {\n\t\t\tstdout += d;\n\t\t});\n\t\tchild.stderr?.on(\"data\", (d) => {\n\t\t\tstderr += d;\n\t\t});\n\t\tchild.on(\"close\", (code) => {\n\t\t\tif (code === 0) resolve(stdout);\n\t\t\telse reject(new Error(stderr || `Exit code ${code}`));\n\t\t});\n\t});\n}\n\n/**\n * Create an executor that runs commands either on host or in Docker container\n */\nexport function createExecutor(config: SandboxConfig): Executor {\n\tif (config.type === \"host\") {\n\t\treturn new HostExecutor();\n\t}\n\treturn new DockerExecutor(config.container);\n}\n\nexport interface Executor {\n\t/**\n\t * Execute a bash command\n\t */\n\texec(command: string, options?: ExecOptions): Promise<ExecResult>;\n\n\t/**\n\t * Get the workspace path prefix for this executor\n\t * Host: returns the actual path\n\t * Docker: returns /workspace\n\t */\n\tgetWorkspacePath(hostPath: string): string;\n}\n\nexport interface ExecOptions {\n\ttimeout?: number;\n\tsignal?: AbortSignal;\n\tstdin?: string;\n}\n\nexport interface ExecResult {\n\tstdout: string;\n\tstderr: string;\n\tcode: number;\n}\n\nclass HostExecutor implements Executor {\n\tasync exec(command: string, options?: ExecOptions): Promise<ExecResult> {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst shell = process.platform === \"win32\" ? \"cmd\" : \"sh\";\n\t\t\tconst shellArgs = process.platform === \"win32\" ? [\"/c\"] : [\"-c\"];\n\t\t\tconst child = (() => {\n\t\t\t\ttry {\n\t\t\t\t\treturn spawn(shell, [...shellArgs, command], {\n\t\t\t\t\t\tdetached: true,\n\t\t\t\t\t\tstdio: [\"pipe\", \"pipe\", \"pipe\"],\n\t\t\t\t\t});\n\t\t\t\t} catch (err) {\n\t\t\t\t\treject(err instanceof Error ? err : new Error(String(err)));\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\t\t\t})();\n\n\t\t\tif (!child) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet stdout = \"\";\n\t\t\tlet stderr = \"\";\n\t\t\tlet timedOut = false;\n\t\t\tlet settled = false;\n\n\t\t\tconst cleanup = () => {\n\t\t\t\tif (timeoutHandle) clearTimeout(timeoutHandle);\n\t\t\t\tif (options?.signal) {\n\t\t\t\t\toptions.signal.removeEventListener(\"abort\", onAbort);\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tconst rejectOnce = (err: Error) => {\n\t\t\t\tif (settled) return;\n\t\t\t\tsettled = true;\n\t\t\t\tcleanup();\n\t\t\t\treject(err);\n\t\t\t};\n\n\t\t\tconst resolveOnce = (result: ExecResult) => {\n\t\t\t\tif (settled) return;\n\t\t\t\tsettled = true;\n\t\t\t\tcleanup();\n\t\t\t\tresolve(result);\n\t\t\t};\n\n\t\t\tconst timeoutHandle =\n\t\t\t\toptions?.timeout && options.timeout > 0\n\t\t\t\t\t? setTimeout(() => {\n\t\t\t\t\t\t\ttimedOut = true;\n\t\t\t\t\t\t\tkillProcessTree(child.pid!);\n\t\t\t\t\t\t}, options.timeout * 1000)\n\t\t\t\t\t: undefined;\n\n\t\t\tconst onAbort = () => {\n\t\t\t\tif (child.pid) killProcessTree(child.pid);\n\t\t\t};\n\n\t\t\tif (options?.signal) {\n\t\t\t\tif (options.signal.aborted) {\n\t\t\t\t\tonAbort();\n\t\t\t\t} else {\n\t\t\t\t\toptions.signal.addEventListener(\"abort\", onAbort, { once: true });\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tchild.stdout?.on(\"data\", (data) => {\n\t\t\t\tstdout += data.toString();\n\t\t\t\tif (stdout.length > 10 * 1024 * 1024) {\n\t\t\t\t\tstdout = stdout.slice(0, 10 * 1024 * 1024);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tchild.stderr?.on(\"data\", (data) => {\n\t\t\t\tstderr += data.toString();\n\t\t\t\tif (stderr.length > 10 * 1024 * 1024) {\n\t\t\t\t\tstderr = stderr.slice(0, 10 * 1024 * 1024);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tchild.on(\"error\", (err) => {\n\t\t\t\trejectOnce(err instanceof Error ? err : new Error(String(err)));\n\t\t\t});\n\n\t\t\tchild.on(\"close\", (code) => {\n\t\t\t\tif (options?.signal?.aborted) {\n\t\t\t\t\trejectOnce(new Error(`${stdout}\\n${stderr}\\nCommand aborted`.trim()));\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tif (timedOut) {\n\t\t\t\t\trejectOnce(\n\t\t\t\t\t\tnew Error(`${stdout}\\n${stderr}\\nCommand timed out after ${options?.timeout} seconds`.trim()),\n\t\t\t\t\t);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tresolveOnce({ stdout, stderr, code: code ?? 0 });\n\t\t\t});\n\n\t\t\tif (options?.stdin !== undefined) {\n\t\t\t\tchild.stdin?.on(\"error\", (err) => {\n\t\t\t\t\tif ((err as NodeJS.ErrnoException).code === \"EPIPE\") return;\n\t\t\t\t\trejectOnce(err instanceof Error ? err : new Error(String(err)));\n\t\t\t\t});\n\t\t\t\tchild.stdin?.end(options.stdin);\n\t\t\t} else {\n\t\t\t\tchild.stdin?.end();\n\t\t\t}\n\t\t});\n\t}\n\n\tgetWorkspacePath(hostPath: string): string {\n\t\treturn hostPath;\n\t}\n}\n\nclass DockerExecutor implements Executor {\n\tconstructor(private container: string) {}\n\n\tasync exec(command: string, options?: ExecOptions): Promise<ExecResult> {\n\t\t// Wrap command for docker exec\n\t\tconst interactive = options?.stdin !== undefined ? \"-i \" : \"\";\n\t\tconst dockerCmd = `docker exec ${interactive}${this.container} sh -c ${shellEscape(command)}`;\n\t\tconst hostExecutor = new HostExecutor();\n\t\treturn hostExecutor.exec(dockerCmd, options);\n\t}\n\n\tgetWorkspacePath(_hostPath: string): string {\n\t\t// Docker container sees /workspace\n\t\treturn \"/workspace\";\n\t}\n}\n\nfunction killProcessTree(pid: number): void {\n\tif (process.platform === \"win32\") {\n\t\ttry {\n\t\t\tspawn(\"taskkill\", [\"/F\", \"/T\", \"/PID\", String(pid)], {\n\t\t\t\tstdio: \"ignore\",\n\t\t\t\tdetached: true,\n\t\t\t});\n\t\t} catch {\n\t\t\t// Ignore errors\n\t\t}\n\t} else {\n\t\ttry {\n\t\t\tprocess.kill(-pid, \"SIGKILL\");\n\t\t} catch {\n\t\t\ttry {\n\t\t\t\tprocess.kill(pid, \"SIGKILL\");\n\t\t\t} catch {\n\t\t\t\t// Process already dead\n\t\t\t}\n\t\t}\n\t}\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"shell-escape.d.ts","sourceRoot":"","sources":["../src/shell-escape.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAE7C","sourcesContent":["/**\n * Shell-escape a string for safe use in sh -c commands.\n * Wraps in single quotes and escapes internal single quotes.\n */\nexport function shellEscape(s: string): string {\n\treturn `'${s.replace(/'/g, \"'\\\\''\")}'`;\n}\n"]}
1
+ {"version":3,"file":"shell-escape.d.ts","sourceRoot":"","sources":["../src/shell-escape.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAE7C"}
@@ -1 +1 @@
1
- {"version":3,"file":"shell-escape.js","sourceRoot":"","sources":["../src/shell-escape.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,CAAS,EAAU;IAC9C,OAAO,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC;AAAA,CACvC","sourcesContent":["/**\n * Shell-escape a string for safe use in sh -c commands.\n * Wraps in single quotes and escapes internal single quotes.\n */\nexport function shellEscape(s: string): string {\n\treturn `'${s.replace(/'/g, \"'\\\\''\")}'`;\n}\n"]}
1
+ {"version":3,"file":"shell-escape.js","sourceRoot":"","sources":["../src/shell-escape.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,CAAS;IACpC,OAAO,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC;AACxC,CAAC","sourcesContent":["/**\n * Shell-escape a string for safe use in sh -c commands.\n * Wraps in single quotes and escapes internal single quotes.\n */\nexport function shellEscape(s: string): string {\n\treturn `'${s.replace(/'/g, \"'\\\\''\")}'`;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,aAAa;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,OAAO,CAAC;IACf,YAAY,CAAC,EAAE,OAAO,GAAG,UAAU,CAAC;IACpC,eAAe,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED,MAAM,WAAW,kBAAkB;IAClC,UAAU,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,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cAAc;IAmBtB;;OAEG;IACG,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAQ/E;IAED;;;OAGG;IACH,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAiEjD;CACD","sourcesContent":["import { closeSync, existsSync, mkdirSync, openSync, readSync, renameSync, statSync } from \"fs\";\nimport { appendFile, writeFile } from \"fs/promises\";\nimport { dirname, join } from \"path\";\n\nexport interface LoggedMessage {\n\tdate: string;\n\tts: string;\n\tuser: string;\n\tuserName?: string;\n\tdisplayName?: string;\n\ttext: string;\n\tisBot: boolean;\n\tdeliveryMode?: \"steer\" | \"followUp\";\n\tskipContextSync?: boolean;\n}\n\nexport interface ChannelStoreConfig {\n\tworkingDir: string;\n}\n\nexport interface LoggedSubAgentRun {\n\tdate: string;\n\ttoolCallId: string;\n\tlabel: string;\n\tagent: string;\n\tsource: \"predefined\" | \"inline\";\n\tmodel: string;\n\ttools: string[];\n\tturns: number;\n\ttoolCalls: number;\n\tdurationMs: number;\n\tfailed: boolean;\n\tfailureReason?: string;\n\toutput: string;\n\toutputTruncated: boolean;\n\tusage: {\n\t\tinput: number;\n\t\toutput: number;\n\t\tcacheRead: number;\n\t\tcacheWrite: number;\n\t\ttotal: number;\n\t\tcost: {\n\t\t\tinput: number;\n\t\t\toutput: number;\n\t\t\tcacheRead: number;\n\t\t\tcacheWrite: number;\n\t\t\ttotal: number;\n\t\t};\n\t};\n}\n\nexport class ChannelStore {\n\tprivate workingDir: string;\n\t// Track recently logged message timestamps to prevent duplicates\n\tprivate recentlyLogged = new Map<string, number>();\n\n\tconstructor(config: ChannelStoreConfig) {\n\t\tthis.workingDir = config.workingDir;\n\n\t\t// Ensure working directory exists\n\t\tif (!existsSync(this.workingDir)) {\n\t\t\tmkdirSync(this.workingDir, { recursive: true });\n\t\t}\n\t}\n\n\t/**\n\t * Get or create the directory for a channel/DM\n\t */\n\tgetChannelDir(channelId: string): string {\n\t\tconst dir = join(this.workingDir, channelId);\n\t\tif (!existsSync(dir)) {\n\t\t\tmkdirSync(dir, { recursive: true });\n\t\t}\n\t\treturn dir;\n\t}\n\n\t/**\n\t * Log a message to the channel's log.jsonl raw archive.\n\t * This file is cold storage and is not proactively loaded into memory context.\n\t * Returns false if message was already logged (duplicate)\n\t */\n\tasync logMessage(channelId: string, message: LoggedMessage): Promise<boolean> {\n\t\t// Check for duplicate (same channel + timestamp)\n\t\tconst dedupeKey = `${channelId}:${message.ts}`;\n\t\tif (this.recentlyLogged.has(dedupeKey)) {\n\t\t\treturn false; // Already logged\n\t\t}\n\n\t\t// Mark as logged and schedule cleanup after 60 seconds\n\t\tthis.recentlyLogged.set(dedupeKey, Date.now());\n\t\tsetTimeout(() => this.recentlyLogged.delete(dedupeKey), 60000);\n\n\t\tconst logPath = join(this.getChannelDir(channelId), \"log.jsonl\");\n\n\t\t// Rotate if file exceeds size limit\n\t\tthis.rotateIfNeeded(logPath);\n\n\t\t// Ensure message has a date field\n\t\tif (!message.date) {\n\t\t\tmessage.date = new Date().toISOString();\n\t\t}\n\n\t\tconst line = `${JSON.stringify(message)}\\n`;\n\t\tawait appendFile(logPath, line, \"utf-8\");\n\t\treturn true;\n\t}\n\n\tasync logSubAgentRun(channelId: string, run: LoggedSubAgentRun): Promise<void> {\n\t\tconst logPath = join(this.getChannelDir(channelId), \"subagent-runs.jsonl\");\n\t\tthis.rotateIfNeeded(logPath);\n\t\tconst line = `${JSON.stringify(run)}\\n`;\n\t\tawait appendFile(logPath, line, \"utf-8\");\n\t}\n\n\t/**\n\t * Rotate log file if it exceeds 1MB.\n\t * Keeps one backup (log.jsonl.1) and resets the sync offset.\n\t */\n\tprivate rotateIfNeeded(logPath: string): void {\n\t\ttry {\n\t\t\tif (!existsSync(logPath)) return;\n\t\t\tconst stats = statSync(logPath);\n\t\t\tif (stats.size > 1_000_000) {\n\t\t\t\trenameSync(logPath, `${logPath}.1`);\n\t\t\t\t// Reset sync offset since log.jsonl was replaced\n\t\t\t\tconst syncOffsetPath = join(dirname(logPath), \".sync-offset\");\n\t\t\t\ttry {\n\t\t\t\t\twriteFile(syncOffsetPath, \"0\", \"utf-8\").catch(() => {});\n\t\t\t\t} catch {\n\t\t\t\t\t/* ignore */\n\t\t\t\t}\n\t\t\t}\n\t\t} catch {\n\t\t\t// Ignore rotation errors\n\t\t}\n\t}\n\n\t/**\n\t * Log a bot response\n\t */\n\tasync logBotResponse(channelId: string, text: string, ts: string): Promise<void> {\n\t\tawait this.logMessage(channelId, {\n\t\t\tdate: new Date().toISOString(),\n\t\t\tts,\n\t\t\tuser: \"bot\",\n\t\t\ttext,\n\t\t\tisBot: true,\n\t\t});\n\t}\n\n\t/**\n\t * Get the timestamp of the last logged message for a channel\n\t * Returns null if no log exists\n\t */\n\tgetLastTimestamp(channelId: string): string | null {\n\t\tconst logPath = join(this.workingDir, channelId, \"log.jsonl\");\n\t\tif (!existsSync(logPath)) {\n\t\t\treturn null;\n\t\t}\n\n\t\ttry {\n\t\t\tconst stats = statSync(logPath);\n\t\t\tif (stats.size === 0) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst fd = openSync(logPath, \"r\");\n\t\t\ttry {\n\t\t\t\tlet end = stats.size;\n\t\t\t\tconst trailing = Buffer.alloc(1);\n\t\t\t\twhile (end > 0) {\n\t\t\t\t\treadSync(fd, trailing, 0, 1, end - 1);\n\t\t\t\t\tif (trailing[0] !== 0x0a && trailing[0] !== 0x0d) {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tend--;\n\t\t\t\t}\n\n\t\t\t\tif (end === 0) {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\n\t\t\t\tconst chunkSize = 4096;\n\t\t\t\tconst buffer = Buffer.alloc(chunkSize);\n\t\t\t\tlet lineStart = 0;\n\t\t\t\tlet position = end;\n\n\t\t\t\twhile (position > 0) {\n\t\t\t\t\tconst bytesToRead = Math.min(chunkSize, position);\n\t\t\t\t\tposition -= bytesToRead;\n\t\t\t\t\treadSync(fd, buffer, 0, bytesToRead, position);\n\n\t\t\t\t\tconst newlineIndex = buffer.subarray(0, bytesToRead).lastIndexOf(0x0a);\n\t\t\t\t\tif (newlineIndex !== -1) {\n\t\t\t\t\t\tlineStart = position + newlineIndex + 1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst lineLength = end - lineStart;\n\t\t\t\tif (lineLength <= 0) {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\n\t\t\t\tconst lineBuffer = Buffer.alloc(lineLength);\n\t\t\t\treadSync(fd, lineBuffer, 0, lineLength, lineStart);\n\t\t\t\tconst lastLine = lineBuffer.toString(\"utf-8\").replace(/\\r+$/, \"\");\n\t\t\t\tif (!lastLine) {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\n\t\t\t\tconst message = JSON.parse(lastLine) as LoggedMessage;\n\t\t\t\treturn message.ts;\n\t\t\t} finally {\n\t\t\t\tcloseSync(fd);\n\t\t\t}\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n}\n"]}
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,aAAa;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,OAAO,CAAC;IACf,YAAY,CAAC,EAAE,OAAO,GAAG,UAAU,CAAC;IACpC,eAAe,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED,MAAM,WAAW,kBAAkB;IAClC,UAAU,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,iBAAiB;IAC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,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAUhF;;;OAGG;IACH,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;CAkElD"}
package/dist/store.js CHANGED
@@ -2,10 +2,9 @@ import { closeSync, existsSync, mkdirSync, openSync, readSync, renameSync, statS
2
2
  import { appendFile, writeFile } from "fs/promises";
3
3
  import { dirname, join } from "path";
4
4
  export class ChannelStore {
5
- workingDir;
6
- // Track recently logged message timestamps to prevent duplicates
7
- recentlyLogged = new Map();
8
5
  constructor(config) {
6
+ // Track recently logged message timestamps to prevent duplicates
7
+ this.recentlyLogged = new Map();
9
8
  this.workingDir = config.workingDir;
10
9
  // Ensure working directory exists
11
10
  if (!existsSync(this.workingDir)) {
package/dist/store.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAChG,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAiDrC,MAAM,OAAO,YAAY;IAChB,UAAU,CAAS;IAC3B,iEAAiE;IACzD,cAAc,GAAG,IAAI,GAAG,EAAkB,CAAC;IAEnD,YAAY,MAA0B,EAAE;QACvC,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;QAEpC,kCAAkC;QAClC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;YAClC,SAAS,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACjD,CAAC;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AAC;YACR,OAAO,IAAI,CAAC;QACb,CAAC;IAAA,CACD;CACD","sourcesContent":["import { closeSync, existsSync, mkdirSync, openSync, readSync, renameSync, statSync } from \"fs\";\nimport { appendFile, writeFile } from \"fs/promises\";\nimport { dirname, join } from \"path\";\n\nexport interface LoggedMessage {\n\tdate: string;\n\tts: string;\n\tuser: string;\n\tuserName?: string;\n\tdisplayName?: string;\n\ttext: string;\n\tisBot: boolean;\n\tdeliveryMode?: \"steer\" | \"followUp\";\n\tskipContextSync?: boolean;\n}\n\nexport interface ChannelStoreConfig {\n\tworkingDir: string;\n}\n\nexport interface LoggedSubAgentRun {\n\tdate: string;\n\ttoolCallId: string;\n\tlabel: string;\n\tagent: string;\n\tsource: \"predefined\" | \"inline\";\n\tmodel: string;\n\ttools: string[];\n\tturns: number;\n\ttoolCalls: number;\n\tdurationMs: number;\n\tfailed: boolean;\n\tfailureReason?: string;\n\toutput: string;\n\toutputTruncated: boolean;\n\tusage: {\n\t\tinput: number;\n\t\toutput: number;\n\t\tcacheRead: number;\n\t\tcacheWrite: number;\n\t\ttotal: number;\n\t\tcost: {\n\t\t\tinput: number;\n\t\t\toutput: number;\n\t\t\tcacheRead: number;\n\t\t\tcacheWrite: number;\n\t\t\ttotal: number;\n\t\t};\n\t};\n}\n\nexport class ChannelStore {\n\tprivate workingDir: string;\n\t// Track recently logged message timestamps to prevent duplicates\n\tprivate recentlyLogged = new Map<string, number>();\n\n\tconstructor(config: ChannelStoreConfig) {\n\t\tthis.workingDir = config.workingDir;\n\n\t\t// Ensure working directory exists\n\t\tif (!existsSync(this.workingDir)) {\n\t\t\tmkdirSync(this.workingDir, { recursive: true });\n\t\t}\n\t}\n\n\t/**\n\t * Get or create the directory for a channel/DM\n\t */\n\tgetChannelDir(channelId: string): string {\n\t\tconst dir = join(this.workingDir, channelId);\n\t\tif (!existsSync(dir)) {\n\t\t\tmkdirSync(dir, { recursive: true });\n\t\t}\n\t\treturn dir;\n\t}\n\n\t/**\n\t * Log a message to the channel's log.jsonl raw archive.\n\t * This file is cold storage and is not proactively loaded into memory context.\n\t * Returns false if message was already logged (duplicate)\n\t */\n\tasync logMessage(channelId: string, message: LoggedMessage): Promise<boolean> {\n\t\t// Check for duplicate (same channel + timestamp)\n\t\tconst dedupeKey = `${channelId}:${message.ts}`;\n\t\tif (this.recentlyLogged.has(dedupeKey)) {\n\t\t\treturn false; // Already logged\n\t\t}\n\n\t\t// Mark as logged and schedule cleanup after 60 seconds\n\t\tthis.recentlyLogged.set(dedupeKey, Date.now());\n\t\tsetTimeout(() => this.recentlyLogged.delete(dedupeKey), 60000);\n\n\t\tconst logPath = join(this.getChannelDir(channelId), \"log.jsonl\");\n\n\t\t// Rotate if file exceeds size limit\n\t\tthis.rotateIfNeeded(logPath);\n\n\t\t// Ensure message has a date field\n\t\tif (!message.date) {\n\t\t\tmessage.date = new Date().toISOString();\n\t\t}\n\n\t\tconst line = `${JSON.stringify(message)}\\n`;\n\t\tawait appendFile(logPath, line, \"utf-8\");\n\t\treturn true;\n\t}\n\n\tasync logSubAgentRun(channelId: string, run: LoggedSubAgentRun): Promise<void> {\n\t\tconst logPath = join(this.getChannelDir(channelId), \"subagent-runs.jsonl\");\n\t\tthis.rotateIfNeeded(logPath);\n\t\tconst line = `${JSON.stringify(run)}\\n`;\n\t\tawait appendFile(logPath, line, \"utf-8\");\n\t}\n\n\t/**\n\t * Rotate log file if it exceeds 1MB.\n\t * Keeps one backup (log.jsonl.1) and resets the sync offset.\n\t */\n\tprivate rotateIfNeeded(logPath: string): void {\n\t\ttry {\n\t\t\tif (!existsSync(logPath)) return;\n\t\t\tconst stats = statSync(logPath);\n\t\t\tif (stats.size > 1_000_000) {\n\t\t\t\trenameSync(logPath, `${logPath}.1`);\n\t\t\t\t// Reset sync offset since log.jsonl was replaced\n\t\t\t\tconst syncOffsetPath = join(dirname(logPath), \".sync-offset\");\n\t\t\t\ttry {\n\t\t\t\t\twriteFile(syncOffsetPath, \"0\", \"utf-8\").catch(() => {});\n\t\t\t\t} catch {\n\t\t\t\t\t/* ignore */\n\t\t\t\t}\n\t\t\t}\n\t\t} catch {\n\t\t\t// Ignore rotation errors\n\t\t}\n\t}\n\n\t/**\n\t * Log a bot response\n\t */\n\tasync logBotResponse(channelId: string, text: string, ts: string): Promise<void> {\n\t\tawait this.logMessage(channelId, {\n\t\t\tdate: new Date().toISOString(),\n\t\t\tts,\n\t\t\tuser: \"bot\",\n\t\t\ttext,\n\t\t\tisBot: true,\n\t\t});\n\t}\n\n\t/**\n\t * Get the timestamp of the last logged message for a channel\n\t * Returns null if no log exists\n\t */\n\tgetLastTimestamp(channelId: string): string | null {\n\t\tconst logPath = join(this.workingDir, channelId, \"log.jsonl\");\n\t\tif (!existsSync(logPath)) {\n\t\t\treturn null;\n\t\t}\n\n\t\ttry {\n\t\t\tconst stats = statSync(logPath);\n\t\t\tif (stats.size === 0) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst fd = openSync(logPath, \"r\");\n\t\t\ttry {\n\t\t\t\tlet end = stats.size;\n\t\t\t\tconst trailing = Buffer.alloc(1);\n\t\t\t\twhile (end > 0) {\n\t\t\t\t\treadSync(fd, trailing, 0, 1, end - 1);\n\t\t\t\t\tif (trailing[0] !== 0x0a && trailing[0] !== 0x0d) {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tend--;\n\t\t\t\t}\n\n\t\t\t\tif (end === 0) {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\n\t\t\t\tconst chunkSize = 4096;\n\t\t\t\tconst buffer = Buffer.alloc(chunkSize);\n\t\t\t\tlet lineStart = 0;\n\t\t\t\tlet position = end;\n\n\t\t\t\twhile (position > 0) {\n\t\t\t\t\tconst bytesToRead = Math.min(chunkSize, position);\n\t\t\t\t\tposition -= bytesToRead;\n\t\t\t\t\treadSync(fd, buffer, 0, bytesToRead, position);\n\n\t\t\t\t\tconst newlineIndex = buffer.subarray(0, bytesToRead).lastIndexOf(0x0a);\n\t\t\t\t\tif (newlineIndex !== -1) {\n\t\t\t\t\t\tlineStart = position + newlineIndex + 1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst lineLength = end - lineStart;\n\t\t\t\tif (lineLength <= 0) {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\n\t\t\t\tconst lineBuffer = Buffer.alloc(lineLength);\n\t\t\t\treadSync(fd, lineBuffer, 0, lineLength, lineStart);\n\t\t\t\tconst lastLine = lineBuffer.toString(\"utf-8\").replace(/\\r+$/, \"\");\n\t\t\t\tif (!lastLine) {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\n\t\t\t\tconst message = JSON.parse(lastLine) as LoggedMessage;\n\t\t\t\treturn message.ts;\n\t\t\t} finally {\n\t\t\t\tcloseSync(fd);\n\t\t\t}\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n}\n"]}
1
+ {"version":3,"file":"store.js","sourceRoot":"","sources":["../src/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,IAAI,CAAC;AAChG,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAiDrC,MAAM,OAAO,YAAY;IAKxB,YAAY,MAA0B;QAHtC,iEAAiE;QACzD,mBAAc,GAAG,IAAI,GAAG,EAAkB,CAAC;QAGlD,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;QAEpC,kCAAkC;QAClC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;YAClC,SAAS,CAAC,IAAI,CAAC,UAAU,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACjD,CAAC;IACF,CAAC;IAED;;OA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{ closeSync, existsSync, mkdirSync, openSync, readSync, renameSync, statSync } from \"fs\";\nimport { appendFile, writeFile } from \"fs/promises\";\nimport { dirname, join } from \"path\";\n\nexport interface LoggedMessage {\n\tdate: string;\n\tts: string;\n\tuser: string;\n\tuserName?: string;\n\tdisplayName?: string;\n\ttext: string;\n\tisBot: boolean;\n\tdeliveryMode?: \"steer\" | \"followUp\";\n\tskipContextSync?: boolean;\n}\n\nexport interface ChannelStoreConfig {\n\tworkingDir: string;\n}\n\nexport interface LoggedSubAgentRun {\n\tdate: string;\n\ttoolCallId: string;\n\tlabel: string;\n\tagent: string;\n\tsource: \"predefined\" | \"inline\";\n\tmodel: string;\n\ttools: string[];\n\tturns: number;\n\ttoolCalls: number;\n\tdurationMs: number;\n\tfailed: boolean;\n\tfailureReason?: string;\n\toutput: string;\n\toutputTruncated: boolean;\n\tusage: {\n\t\tinput: number;\n\t\toutput: number;\n\t\tcacheRead: number;\n\t\tcacheWrite: number;\n\t\ttotal: number;\n\t\tcost: {\n\t\t\tinput: number;\n\t\t\toutput: number;\n\t\t\tcacheRead: number;\n\t\t\tcacheWrite: number;\n\t\t\ttotal: number;\n\t\t};\n\t};\n}\n\nexport class ChannelStore {\n\tprivate workingDir: string;\n\t// Track recently logged message timestamps to prevent duplicates\n\tprivate recentlyLogged = new Map<string, number>();\n\n\tconstructor(config: ChannelStoreConfig) {\n\t\tthis.workingDir = config.workingDir;\n\n\t\t// Ensure working directory exists\n\t\tif (!existsSync(this.workingDir)) {\n\t\t\tmkdirSync(this.workingDir, { recursive: true });\n\t\t}\n\t}\n\n\t/**\n\t * Get or create the directory for a channel/DM\n\t */\n\tgetChannelDir(channelId: string): string {\n\t\tconst dir = join(this.workingDir, channelId);\n\t\tif (!existsSync(dir)) {\n\t\t\tmkdirSync(dir, { recursive: true });\n\t\t}\n\t\treturn dir;\n\t}\n\n\t/**\n\t * Log a message to the channel's log.jsonl raw archive.\n\t * This file is cold storage and is not proactively loaded into memory context.\n\t * Returns false if message was already logged (duplicate)\n\t */\n\tasync logMessage(channelId: string, message: LoggedMessage): Promise<boolean> {\n\t\t// Check for duplicate (same channel + timestamp)\n\t\tconst dedupeKey = `${channelId}:${message.ts}`;\n\t\tif (this.recentlyLogged.has(dedupeKey)) {\n\t\t\treturn false; // Already logged\n\t\t}\n\n\t\t// Mark as logged and schedule cleanup after 60 seconds\n\t\tthis.recentlyLogged.set(dedupeKey, Date.now());\n\t\tsetTimeout(() => this.recentlyLogged.delete(dedupeKey), 60000);\n\n\t\tconst logPath = join(this.getChannelDir(channelId), \"log.jsonl\");\n\n\t\t// Rotate if file exceeds size limit\n\t\tthis.rotateIfNeeded(logPath);\n\n\t\t// Ensure message has a date field\n\t\tif (!message.date) {\n\t\t\tmessage.date = new Date().toISOString();\n\t\t}\n\n\t\tconst line = `${JSON.stringify(message)}\\n`;\n\t\tawait appendFile(logPath, line, \"utf-8\");\n\t\treturn true;\n\t}\n\n\tasync logSubAgentRun(channelId: string, run: LoggedSubAgentRun): Promise<void> {\n\t\tconst logPath = join(this.getChannelDir(channelId), \"subagent-runs.jsonl\");\n\t\tthis.rotateIfNeeded(logPath);\n\t\tconst line = `${JSON.stringify(run)}\\n`;\n\t\tawait appendFile(logPath, line, \"utf-8\");\n\t}\n\n\t/**\n\t * Rotate log file if it exceeds 1MB.\n\t * Keeps one backup (log.jsonl.1) and resets the sync offset.\n\t */\n\tprivate rotateIfNeeded(logPath: string): void {\n\t\ttry {\n\t\t\tif (!existsSync(logPath)) return;\n\t\t\tconst stats = statSync(logPath);\n\t\t\tif (stats.size > 1_000_000) {\n\t\t\t\trenameSync(logPath, `${logPath}.1`);\n\t\t\t\t// Reset sync offset since log.jsonl was replaced\n\t\t\t\tconst syncOffsetPath = join(dirname(logPath), \".sync-offset\");\n\t\t\t\ttry {\n\t\t\t\t\twriteFile(syncOffsetPath, \"0\", \"utf-8\").catch(() => {});\n\t\t\t\t} catch {\n\t\t\t\t\t/* ignore */\n\t\t\t\t}\n\t\t\t}\n\t\t} catch {\n\t\t\t// Ignore rotation errors\n\t\t}\n\t}\n\n\t/**\n\t * Log a bot response\n\t */\n\tasync logBotResponse(channelId: string, text: string, ts: string): Promise<void> {\n\t\tawait this.logMessage(channelId, {\n\t\t\tdate: new Date().toISOString(),\n\t\t\tts,\n\t\t\tuser: \"bot\",\n\t\t\ttext,\n\t\t\tisBot: true,\n\t\t});\n\t}\n\n\t/**\n\t * Get the timestamp of the last logged message for a channel\n\t * Returns null if no log exists\n\t */\n\tgetLastTimestamp(channelId: string): string | null {\n\t\tconst logPath = join(this.workingDir, channelId, \"log.jsonl\");\n\t\tif (!existsSync(logPath)) {\n\t\t\treturn null;\n\t\t}\n\n\t\ttry {\n\t\t\tconst stats = statSync(logPath);\n\t\t\tif (stats.size === 0) {\n\t\t\t\treturn null;\n\t\t\t}\n\n\t\t\tconst fd = openSync(logPath, \"r\");\n\t\t\ttry {\n\t\t\t\tlet end = stats.size;\n\t\t\t\tconst trailing = Buffer.alloc(1);\n\t\t\t\twhile (end > 0) {\n\t\t\t\t\treadSync(fd, trailing, 0, 1, end - 1);\n\t\t\t\t\tif (trailing[0] !== 0x0a && trailing[0] !== 0x0d) {\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tend--;\n\t\t\t\t}\n\n\t\t\t\tif (end === 0) {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\n\t\t\t\tconst chunkSize = 4096;\n\t\t\t\tconst buffer = Buffer.alloc(chunkSize);\n\t\t\t\tlet lineStart = 0;\n\t\t\t\tlet position = end;\n\n\t\t\t\twhile (position > 0) {\n\t\t\t\t\tconst bytesToRead = Math.min(chunkSize, position);\n\t\t\t\t\tposition -= bytesToRead;\n\t\t\t\t\treadSync(fd, buffer, 0, bytesToRead, position);\n\n\t\t\t\t\tconst newlineIndex = buffer.subarray(0, bytesToRead).lastIndexOf(0x0a);\n\t\t\t\t\tif (newlineIndex !== -1) {\n\t\t\t\t\t\tlineStart = position + newlineIndex + 1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst lineLength = end - lineStart;\n\t\t\t\tif (lineLength <= 0) {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\n\t\t\t\tconst lineBuffer = Buffer.alloc(lineLength);\n\t\t\t\treadSync(fd, lineBuffer, 0, lineLength, lineStart);\n\t\t\t\tconst lastLine = lineBuffer.toString(\"utf-8\").replace(/\\r+$/, \"\");\n\t\t\t\tif (!lastLine) {\n\t\t\t\t\treturn null;\n\t\t\t\t}\n\n\t\t\t\tconst message = JSON.parse(lastLine) as LoggedMessage;\n\t\t\t\treturn message.ts;\n\t\t\t} finally {\n\t\t\t\tcloseSync(fd);\n\t\t\t}\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"sub-agents.d.ts","sourceRoot":"","sources":["../src/sub-agents.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAQtD,QAAA,MAAM,uBAAuB,4CAA6C,CAAC;AAS3E,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,uBAAuB,CAAC,CAAC,MAAM,CAAC,CAAC;AAExE,MAAM,WAAW,cAAc;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,gBAAgB,EAAE,CAAC;IAC1B,KAAK,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,YAAY,GAAG,QAAQ,CAAC;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EAClC,SAAS,EAAE,2BAA2B,GACpC;IAAE,MAAM,CAAC,EAAE,sBAAsB,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAyErD;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,cAAc,EAAE,EAAE,QAAQ,GAAE,MAAW,GAAG,MAAM,CAW1F","sourcesContent":["import type { Api, Model } from \"@mariozechner/pi-ai\";\nimport { parseFrontmatter } from \"@mariozechner/pi-coding-agent\";\nimport type { Dirent } from \"fs\";\nimport { existsSync, readdirSync, readFileSync } from \"fs\";\nimport { join } from \"path\";\nimport { findExactModelReferenceMatch, formatModelReference } from \"./model-utils.js\";\nimport { SUB_AGENTS_DIR_NAME } from \"./paths.js\";\n\nconst ALLOWED_SUB_AGENT_TOOLS = [\"read\", \"bash\", \"edit\", \"write\"] as const;\nconst DEFAULT_SUB_AGENT_TOOLS = [\"read\", \"bash\"] as const;\nconst DEFAULT_MAX_TURNS = 24;\nconst DEFAULT_MAX_TOOL_CALLS = 48;\nconst DEFAULT_MAX_WALL_TIME_SEC = 300;\nconst DEFAULT_BASH_TIMEOUT_SEC = 120;\nconst MAX_SUB_AGENT_TASK_CHARS = 12000;\nconst MAX_SUB_AGENT_SYSTEM_PROMPT_CHARS = 16000;\n\nexport type SubAgentToolName = (typeof ALLOWED_SUB_AGENT_TOOLS)[number];\n\nexport interface SubAgentConfig {\n\tname: string;\n\tdescription: string;\n\tsystemPrompt: string;\n\ttools: SubAgentToolName[];\n\tmodel?: Model<Api>;\n\tmodelRef?: string;\n\tmaxTurns: number;\n\tmaxToolCalls: number;\n\tmaxWallTimeSec: number;\n\tbashTimeoutSec: number;\n\tfilePath?: string;\n\tsource: \"predefined\" | \"inline\";\n}\n\nexport interface ResolvedSubAgentConfig extends Omit<SubAgentConfig, \"model\" | \"modelRef\"> {\n\tmodel: Model<Api>;\n\tmodelRef: string;\n}\n\nexport interface SubAgentDiscoveryResult {\n\tdirectory: string;\n\tagents: SubAgentConfig[];\n\twarnings: string[];\n}\n\nexport interface SubAgentInvocationOverrides {\n\tagent?: string;\n\tname?: string;\n\tsystemPrompt?: string;\n\ttools?: string[];\n\tmodel?: string;\n\tmaxTurns?: number;\n\tmaxToolCalls?: number;\n\tmaxWallTimeSec?: number;\n\tbashTimeoutSec?: number;\n}\n\nfunction validateTextLength(value: string, maxChars: number, label: string): string | undefined {\n\tif (value.length <= maxChars) {\n\t\treturn undefined;\n\t}\n\treturn `${label} exceeds ${maxChars} characters (got ${value.length}).`;\n}\n\nexport function validateSubAgentTask(task: string): string | undefined {\n\treturn validateTextLength(task, MAX_SUB_AGENT_TASK_CHARS, \"Sub-agent task\");\n}\n\nfunction validateSubAgentSystemPrompt(systemPrompt: string, label: string): string | undefined {\n\treturn validateTextLength(systemPrompt, MAX_SUB_AGENT_SYSTEM_PROMPT_CHARS, label);\n}\n\nexport function getSubAgentsDir(workspaceDir: string): string {\n\treturn join(workspaceDir, SUB_AGENTS_DIR_NAME);\n}\n\nfunction parseToolNames(raw: string | undefined): { tools: SubAgentToolName[]; error?: string } {\n\tif (!raw || !raw.trim()) {\n\t\treturn { tools: [...DEFAULT_SUB_AGENT_TOOLS] };\n\t}\n\n\tconst values = raw\n\t\t.split(\",\")\n\t\t.map((value) => value.trim())\n\t\t.filter((value) => value.length > 0);\n\n\treturn validateToolNames(values);\n}\n\nexport function validateToolNames(values: string[] | undefined): { tools: SubAgentToolName[]; error?: string } {\n\tif (!values || values.length === 0) {\n\t\treturn { tools: [...DEFAULT_SUB_AGENT_TOOLS] };\n\t}\n\n\tconst tools: SubAgentToolName[] = [];\n\tconst seen = new Set<string>();\n\tfor (const value of values) {\n\t\tconst normalized = value.trim();\n\t\tif (!normalized || seen.has(normalized)) {\n\t\t\tcontinue;\n\t\t}\n\t\tif (!ALLOWED_SUB_AGENT_TOOLS.includes(normalized as SubAgentToolName)) {\n\t\t\treturn {\n\t\t\t\ttools: [],\n\t\t\t\terror: `Unknown tool \"${normalized}\". Allowed tools: ${ALLOWED_SUB_AGENT_TOOLS.join(\", \")}`,\n\t\t\t};\n\t\t}\n\t\tseen.add(normalized);\n\t\ttools.push(normalized as SubAgentToolName);\n\t}\n\n\treturn { tools: tools.length > 0 ? tools : [...DEFAULT_SUB_AGENT_TOOLS] };\n}\n\nfunction parsePositiveInteger(raw: string | undefined, fallback: number): { value: number; warning?: string } {\n\tif (!raw || !raw.trim()) {\n\t\treturn { value: fallback };\n\t}\n\n\tconst parsed = Number.parseInt(raw.trim(), 10);\n\tif (!Number.isFinite(parsed) || parsed <= 0) {\n\t\treturn { value: fallback, warning: `Invalid numeric value \"${raw}\", using default ${fallback}` };\n\t}\n\n\treturn { value: parsed };\n}\n\nfunction resolvePositiveOverride(value: number | undefined, fallback: number): number {\n\tif (!Number.isFinite(value) || value === undefined || value <= 0) {\n\t\treturn fallback;\n\t}\n\treturn Math.floor(value);\n}\n\nfunction resolveModelReference(\n\tmodelRef: string,\n\tavailableModels: Model<Api>[],\n): { model?: Model<Api>; error?: string } {\n\tconst { match, ambiguous } = findExactModelReferenceMatch(modelRef, availableModels);\n\tif (match) {\n\t\treturn { model: match };\n\t}\n\tif (ambiguous) {\n\t\treturn { error: `Model reference \"${modelRef}\" is ambiguous. Use provider/modelId.` };\n\t}\n\treturn { error: `Model reference \"${modelRef}\" was not found among available models.` };\n}\n\nexport function discoverSubAgents(workspaceDir: string, availableModels: Model<Api>[]): SubAgentDiscoveryResult {\n\tconst directory = getSubAgentsDir(workspaceDir);\n\tif (!existsSync(directory)) {\n\t\treturn { directory, agents: [], warnings: [] };\n\t}\n\n\tconst warnings: string[] = [];\n\tconst agents: SubAgentConfig[] = [];\n\tconst seenNames = new Set<string>();\n\tlet entries: Dirent<string>[];\n\ttry {\n\t\tentries = readdirSync(directory, { withFileTypes: true })\n\t\t\t.filter((entry) => entry.name.endsWith(\".md\") && (entry.isFile() || entry.isSymbolicLink()))\n\t\t\t.sort((a, b) => a.name.localeCompare(b.name));\n\t} catch (error) {\n\t\treturn {\n\t\t\tdirectory,\n\t\t\tagents: [],\n\t\t\twarnings: [`Failed to read sub-agents directory (${error instanceof Error ? error.message : String(error)})`],\n\t\t};\n\t}\n\n\tfor (const entry of entries) {\n\t\tconst filePath = join(directory, entry.name);\n\t\tlet content = \"\";\n\t\ttry {\n\t\t\tcontent = readFileSync(filePath, \"utf-8\");\n\t\t} catch (error) {\n\t\t\twarnings.push(\n\t\t\t\t`${entry.name}: failed to read file (${error instanceof Error ? error.message : String(error)})`,\n\t\t\t);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst { frontmatter, body } = parseFrontmatter<Record<string, string>>(content);\n\t\tconst name = frontmatter.name?.trim();\n\t\tconst description = frontmatter.description?.trim();\n\n\t\tif (!name || !description) {\n\t\t\twarnings.push(`${entry.name}: missing required frontmatter fields \"name\" or \"description\"`);\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (seenNames.has(name)) {\n\t\t\twarnings.push(`${entry.name}: duplicate sub-agent name \"${name}\" ignored`);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst toolParse = parseToolNames(frontmatter.tools);\n\t\tif (toolParse.error) {\n\t\t\twarnings.push(`${entry.name}: ${toolParse.error}`);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst maxTurns = parsePositiveInteger(frontmatter.maxTurns, DEFAULT_MAX_TURNS);\n\t\tconst maxToolCalls = parsePositiveInteger(frontmatter.maxToolCalls, DEFAULT_MAX_TOOL_CALLS);\n\t\tconst maxWallTimeSec = parsePositiveInteger(frontmatter.maxWallTimeSec, DEFAULT_MAX_WALL_TIME_SEC);\n\t\tconst bashTimeoutSec = parsePositiveInteger(frontmatter.bashTimeoutSec, DEFAULT_BASH_TIMEOUT_SEC);\n\n\t\tfor (const warning of [maxTurns.warning, maxToolCalls.warning, maxWallTimeSec.warning, bashTimeoutSec.warning]) {\n\t\t\tif (warning) {\n\t\t\t\twarnings.push(`${entry.name}: ${warning}`);\n\t\t\t}\n\t\t}\n\n\t\tconst modelRef = frontmatter.model?.trim();\n\t\tlet model: Model<Api> | undefined;\n\t\tif (modelRef) {\n\t\t\tconst resolved = resolveModelReference(modelRef, availableModels);\n\t\t\tif (!resolved.model) {\n\t\t\t\twarnings.push(`${entry.name}: ${resolved.error}`);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tmodel = resolved.model;\n\t\t}\n\n\t\tconst trimmedBody = body.trim();\n\t\tif (!trimmedBody) {\n\t\t\twarnings.push(`${entry.name}: empty system prompt body`);\n\t\t\tcontinue;\n\t\t}\n\t\tconst promptLengthError = validateSubAgentSystemPrompt(trimmedBody, \"Sub-agent system prompt\");\n\t\tif (promptLengthError) {\n\t\t\twarnings.push(`${entry.name}: ${promptLengthError}`);\n\t\t\tcontinue;\n\t\t}\n\n\t\tseenNames.add(name);\n\t\tagents.push({\n\t\t\tname,\n\t\t\tdescription,\n\t\t\tsystemPrompt: trimmedBody,\n\t\t\ttools: toolParse.tools,\n\t\t\tmodel,\n\t\t\tmodelRef: modelRef || (model ? formatModelReference(model) : undefined),\n\t\t\tmaxTurns: maxTurns.value,\n\t\t\tmaxToolCalls: maxToolCalls.value,\n\t\t\tmaxWallTimeSec: maxWallTimeSec.value,\n\t\t\tbashTimeoutSec: bashTimeoutSec.value,\n\t\t\tfilePath,\n\t\t\tsource: \"predefined\",\n\t\t});\n\t}\n\n\treturn { directory, agents, warnings };\n}\n\nexport function resolveSubAgentConfig(\n\tavailableModels: Model<Api>[],\n\tcurrentModel: Model<Api>,\n\tpredefinedAgents: SubAgentConfig[],\n\toverrides: SubAgentInvocationOverrides,\n): { config?: ResolvedSubAgentConfig; error?: string } {\n\tconst baseConfig = overrides.agent ? predefinedAgents.find((agent) => agent.name === overrides.agent) : undefined;\n\tif (overrides.agent && !baseConfig) {\n\t\tconst available = predefinedAgents.length > 0 ? predefinedAgents.map((agent) => agent.name).join(\", \") : \"none\";\n\t\treturn { error: `Unknown sub-agent \"${overrides.agent}\". Available sub-agents: ${available}.` };\n\t}\n\n\tif (!baseConfig && (!overrides.systemPrompt || !overrides.systemPrompt.trim())) {\n\t\treturn { error: 'Provide either \"agent\" or \"systemPrompt\" to define the sub-agent.' };\n\t}\n\n\tconst tools = overrides.tools\n\t\t? validateToolNames(overrides.tools)\n\t\t: { tools: baseConfig?.tools ?? [...DEFAULT_SUB_AGENT_TOOLS] };\n\tif (tools.error) {\n\t\treturn { error: tools.error };\n\t}\n\n\tlet model = baseConfig?.model;\n\tlet modelRef = baseConfig?.modelRef;\n\tif (overrides.model?.trim()) {\n\t\tconst resolved = resolveModelReference(overrides.model.trim(), availableModels);\n\t\tif (!resolved.model) {\n\t\t\treturn { error: resolved.error };\n\t\t}\n\t\tmodel = resolved.model;\n\t\tmodelRef = formatModelReference(resolved.model);\n\t}\n\n\tconst maxTurns = resolvePositiveOverride(overrides.maxTurns, baseConfig?.maxTurns ?? DEFAULT_MAX_TURNS);\n\tconst maxToolCalls = resolvePositiveOverride(\n\t\toverrides.maxToolCalls,\n\t\tbaseConfig?.maxToolCalls ?? DEFAULT_MAX_TOOL_CALLS,\n\t);\n\tconst maxWallTimeSec = resolvePositiveOverride(\n\t\toverrides.maxWallTimeSec,\n\t\tbaseConfig?.maxWallTimeSec ?? DEFAULT_MAX_WALL_TIME_SEC,\n\t);\n\tconst bashTimeoutSec = resolvePositiveOverride(\n\t\toverrides.bashTimeoutSec,\n\t\tbaseConfig?.bashTimeoutSec ?? DEFAULT_BASH_TIMEOUT_SEC,\n\t);\n\n\tconst systemPrompt = overrides.systemPrompt?.trim() || baseConfig?.systemPrompt || \"\";\n\tif (!systemPrompt) {\n\t\treturn { error: \"Sub-agent system prompt cannot be empty.\" };\n\t}\n\tif (overrides.systemPrompt?.trim()) {\n\t\tconst promptLengthError = validateSubAgentSystemPrompt(\n\t\t\toverrides.systemPrompt.trim(),\n\t\t\t\"Inline sub-agent systemPrompt\",\n\t\t);\n\t\tif (promptLengthError) {\n\t\t\treturn { error: promptLengthError };\n\t\t}\n\t}\n\n\treturn {\n\t\tconfig: {\n\t\t\tname: overrides.name?.trim() || baseConfig?.name || \"dynamic-subagent\",\n\t\t\tdescription: baseConfig?.description || \"Inline sub-agent\",\n\t\t\tsystemPrompt,\n\t\t\ttools: tools.tools,\n\t\t\tmodel: model ?? currentModel,\n\t\t\tmodelRef: modelRef ?? formatModelReference(model ?? currentModel),\n\t\t\tmaxTurns,\n\t\t\tmaxToolCalls,\n\t\t\tmaxWallTimeSec,\n\t\t\tbashTimeoutSec,\n\t\t\tfilePath: baseConfig?.filePath,\n\t\t\tsource: baseConfig ? \"predefined\" : \"inline\",\n\t\t},\n\t};\n}\n\nexport function formatSubAgentList(agents: SubAgentConfig[], maxItems: number = 12): string {\n\tif (agents.length === 0) {\n\t\treturn \"none\";\n\t}\n\n\tconst listed = agents.slice(0, maxItems).map((agent) => `- \\`${agent.name}\\`: ${agent.description}`);\n\tif (agents.length <= maxItems) {\n\t\treturn listed.join(\"\\n\");\n\t}\n\n\treturn `${listed.join(\"\\n\")}\\n- ... and ${agents.length - maxItems} more`;\n}\n"]}
1
+ {"version":3,"file":"sub-agents.d.ts","sourceRoot":"","sources":["../src/sub-agents.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAQtD,QAAA,MAAM,uBAAuB,4CAA6C,CAAC;AAS3E,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,uBAAuB,CAAC,CAAC,MAAM,CAAC,CAAC;AAExE,MAAM,WAAW,cAAc;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,gBAAgB,EAAE,CAAC;IAC1B,KAAK,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAA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C,eAAe,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,EAC7B,YAAY,EAAE,KAAK,CAAC,GAAG,CAAC,EACxB,gBAAgB,EAAE,cAAc,EAAE,EAClC,SAAS,EAAE,2BAA2B,GACpC;IAAE,MAAM,CAAC,EAAE,sBAAsB,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAyErD;AAED,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,cAAc,EAAE,EAAE,QAAQ,GAAE,MAAW,GAAG,MAAM,CAW1F"}
@@ -26,15 +26,35 @@ function validateSubAgentSystemPrompt(systemPrompt, label) {
26
26
  export function getSubAgentsDir(workspaceDir) {
27
27
  return join(workspaceDir, SUB_AGENTS_DIR_NAME);
28
28
  }
29
+ function readOptionalTrimmedString(value) {
30
+ if (typeof value !== "string") {
31
+ return undefined;
32
+ }
33
+ const trimmed = value.trim();
34
+ return trimmed ? trimmed : undefined;
35
+ }
29
36
  function parseToolNames(raw) {
30
- if (!raw || !raw.trim()) {
37
+ if (raw === undefined || raw === null) {
31
38
  return { tools: [...DEFAULT_SUB_AGENT_TOOLS] };
32
39
  }
33
- const values = raw
34
- .split(",")
35
- .map((value) => value.trim())
36
- .filter((value) => value.length > 0);
37
- return validateToolNames(values);
40
+ if (typeof raw === "string") {
41
+ if (!raw.trim()) {
42
+ return { tools: [...DEFAULT_SUB_AGENT_TOOLS] };
43
+ }
44
+ const values = raw
45
+ .split(",")
46
+ .map((value) => value.trim())
47
+ .filter((value) => value.length > 0);
48
+ return validateToolNames(values);
49
+ }
50
+ if (Array.isArray(raw)) {
51
+ const invalidValue = raw.find((value) => typeof value !== "string");
52
+ if (invalidValue !== undefined) {
53
+ return { tools: [], error: 'Invalid "tools" frontmatter: expected a string or string[]' };
54
+ }
55
+ return validateToolNames(raw);
56
+ }
57
+ return { tools: [], error: 'Invalid "tools" frontmatter: expected a string or string[]' };
38
58
  }
39
59
  export function validateToolNames(values) {
40
60
  if (!values || values.length === 0) {
@@ -59,7 +79,19 @@ export function validateToolNames(values) {
59
79
  return { tools: tools.length > 0 ? tools : [...DEFAULT_SUB_AGENT_TOOLS] };
60
80
  }
61
81
  function parsePositiveInteger(raw, fallback) {
62
- if (!raw || !raw.trim()) {
82
+ if (raw === undefined || raw === null) {
83
+ return { value: fallback };
84
+ }
85
+ if (typeof raw === "number") {
86
+ if (!Number.isFinite(raw) || raw <= 0) {
87
+ return { value: fallback, warning: `Invalid numeric value "${String(raw)}", using default ${fallback}` };
88
+ }
89
+ return { value: Math.floor(raw) };
90
+ }
91
+ if (typeof raw !== "string") {
92
+ return { value: fallback, warning: `Invalid numeric value "${String(raw)}", using default ${fallback}` };
93
+ }
94
+ if (!raw.trim()) {
63
95
  return { value: fallback };
64
96
  }
65
97
  const parsed = Number.parseInt(raw.trim(), 10);
@@ -116,8 +148,8 @@ export function discoverSubAgents(workspaceDir, availableModels) {
116
148
  continue;
117
149
  }
118
150
  const { frontmatter, body } = parseFrontmatter(content);
119
- const name = frontmatter.name?.trim();
120
- const description = frontmatter.description?.trim();
151
+ const name = readOptionalTrimmedString(frontmatter.name);
152
+ const description = readOptionalTrimmedString(frontmatter.description);
121
153
  if (!name || !description) {
122
154
  warnings.push(`${entry.name}: missing required frontmatter fields "name" or "description"`);
123
155
  continue;
@@ -140,7 +172,7 @@ export function discoverSubAgents(workspaceDir, availableModels) {
140
172
  warnings.push(`${entry.name}: ${warning}`);
141
173
  }
142
174
  }
143
- const modelRef = frontmatter.model?.trim();
175
+ const modelRef = readOptionalTrimmedString(frontmatter.model);
144
176
  let model;
145
177
  if (modelRef) {
146
178
  const resolved = resolveModelReference(modelRef, availableModels);
@@ -1 +1 @@
1
- {"version":3,"file":"sub-agents.js","sourceRoot":"","sources":["../src/sub-agents.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AAEjE,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;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IAAI,YAAY,CAAC;YACjE,QAAQ;YACR,YAAY;YACZ,cAAc;YACd,cAAc;YACd,QAAQ,EAAE,UAAU,EAAE,QAAQ;YAC9B,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,QAAQ;SAC5C;KACD,CAAC;AAAA,CACF;AAED,MAAM,UAAU,kBAAkB,CAAC,MAAwB,EAAE,QAAQ,GAAW,EAAE,EAAU;IAC3F,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,MAAM,CAAC;IACf,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,KAAK,CAAC,IAAI,OAAO,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC;IACrG,IAAI,MAAM,CAAC,MAAM,IAAI,QAAQ,EAAE,CAAC;QAC/B,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;IAED,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,eAAe,MAAM,CAAC,MAAM,GAAG,QAAQ,OAAO,CAAC;AAAA,CAC1E","sourcesContent":["import type { Api, Model } from \"@mariozechner/pi-ai\";\nimport { parseFrontmatter } from \"@mariozechner/pi-coding-agent\";\nimport type { Dirent } from \"fs\";\nimport { existsSync, readdirSync, readFileSync } from \"fs\";\nimport { join } from \"path\";\nimport { findExactModelReferenceMatch, formatModelReference } from \"./model-utils.js\";\nimport { SUB_AGENTS_DIR_NAME } from \"./paths.js\";\n\nconst ALLOWED_SUB_AGENT_TOOLS = [\"read\", \"bash\", \"edit\", \"write\"] as const;\nconst DEFAULT_SUB_AGENT_TOOLS = [\"read\", \"bash\"] as const;\nconst DEFAULT_MAX_TURNS = 24;\nconst DEFAULT_MAX_TOOL_CALLS = 48;\nconst DEFAULT_MAX_WALL_TIME_SEC = 300;\nconst DEFAULT_BASH_TIMEOUT_SEC = 120;\nconst MAX_SUB_AGENT_TASK_CHARS = 12000;\nconst MAX_SUB_AGENT_SYSTEM_PROMPT_CHARS = 16000;\n\nexport type SubAgentToolName = (typeof ALLOWED_SUB_AGENT_TOOLS)[number];\n\nexport interface SubAgentConfig {\n\tname: string;\n\tdescription: string;\n\tsystemPrompt: string;\n\ttools: SubAgentToolName[];\n\tmodel?: Model<Api>;\n\tmodelRef?: string;\n\tmaxTurns: number;\n\tmaxToolCalls: number;\n\tmaxWallTimeSec: number;\n\tbashTimeoutSec: number;\n\tfilePath?: string;\n\tsource: \"predefined\" | \"inline\";\n}\n\nexport interface ResolvedSubAgentConfig extends Omit<SubAgentConfig, \"model\" | \"modelRef\"> {\n\tmodel: Model<Api>;\n\tmodelRef: string;\n}\n\nexport interface SubAgentDiscoveryResult {\n\tdirectory: string;\n\tagents: SubAgentConfig[];\n\twarnings: string[];\n}\n\nexport interface SubAgentInvocationOverrides {\n\tagent?: string;\n\tname?: string;\n\tsystemPrompt?: string;\n\ttools?: string[];\n\tmodel?: string;\n\tmaxTurns?: number;\n\tmaxToolCalls?: number;\n\tmaxWallTimeSec?: number;\n\tbashTimeoutSec?: number;\n}\n\nfunction validateTextLength(value: string, maxChars: number, label: string): string | undefined {\n\tif (value.length <= maxChars) {\n\t\treturn undefined;\n\t}\n\treturn `${label} exceeds ${maxChars} characters (got ${value.length}).`;\n}\n\nexport function validateSubAgentTask(task: string): string | undefined {\n\treturn validateTextLength(task, MAX_SUB_AGENT_TASK_CHARS, \"Sub-agent task\");\n}\n\nfunction validateSubAgentSystemPrompt(systemPrompt: string, label: string): string | undefined {\n\treturn validateTextLength(systemPrompt, MAX_SUB_AGENT_SYSTEM_PROMPT_CHARS, label);\n}\n\nexport function getSubAgentsDir(workspaceDir: string): string {\n\treturn join(workspaceDir, SUB_AGENTS_DIR_NAME);\n}\n\nfunction parseToolNames(raw: string | undefined): { tools: SubAgentToolName[]; error?: string } {\n\tif (!raw || !raw.trim()) {\n\t\treturn { tools: [...DEFAULT_SUB_AGENT_TOOLS] };\n\t}\n\n\tconst values = raw\n\t\t.split(\",\")\n\t\t.map((value) => value.trim())\n\t\t.filter((value) => value.length > 0);\n\n\treturn validateToolNames(values);\n}\n\nexport function validateToolNames(values: string[] | undefined): { tools: SubAgentToolName[]; error?: string } {\n\tif (!values || values.length === 0) {\n\t\treturn { tools: [...DEFAULT_SUB_AGENT_TOOLS] };\n\t}\n\n\tconst tools: SubAgentToolName[] = [];\n\tconst seen = new Set<string>();\n\tfor (const value of values) {\n\t\tconst normalized = value.trim();\n\t\tif (!normalized || seen.has(normalized)) {\n\t\t\tcontinue;\n\t\t}\n\t\tif (!ALLOWED_SUB_AGENT_TOOLS.includes(normalized as SubAgentToolName)) {\n\t\t\treturn {\n\t\t\t\ttools: [],\n\t\t\t\terror: `Unknown tool \"${normalized}\". Allowed tools: ${ALLOWED_SUB_AGENT_TOOLS.join(\", \")}`,\n\t\t\t};\n\t\t}\n\t\tseen.add(normalized);\n\t\ttools.push(normalized as SubAgentToolName);\n\t}\n\n\treturn { tools: tools.length > 0 ? tools : [...DEFAULT_SUB_AGENT_TOOLS] };\n}\n\nfunction parsePositiveInteger(raw: string | undefined, fallback: number): { value: number; warning?: string } {\n\tif (!raw || !raw.trim()) {\n\t\treturn { value: fallback };\n\t}\n\n\tconst parsed = Number.parseInt(raw.trim(), 10);\n\tif (!Number.isFinite(parsed) || parsed <= 0) {\n\t\treturn { value: fallback, warning: `Invalid numeric value \"${raw}\", using default ${fallback}` };\n\t}\n\n\treturn { value: parsed };\n}\n\nfunction resolvePositiveOverride(value: number | undefined, fallback: number): number {\n\tif (!Number.isFinite(value) || value === undefined || value <= 0) {\n\t\treturn fallback;\n\t}\n\treturn Math.floor(value);\n}\n\nfunction resolveModelReference(\n\tmodelRef: string,\n\tavailableModels: Model<Api>[],\n): { model?: Model<Api>; error?: string } {\n\tconst { match, ambiguous } = findExactModelReferenceMatch(modelRef, availableModels);\n\tif (match) {\n\t\treturn { model: match };\n\t}\n\tif (ambiguous) {\n\t\treturn { error: `Model reference \"${modelRef}\" is ambiguous. Use provider/modelId.` };\n\t}\n\treturn { error: `Model reference \"${modelRef}\" was not found among available models.` };\n}\n\nexport function discoverSubAgents(workspaceDir: string, availableModels: Model<Api>[]): SubAgentDiscoveryResult {\n\tconst directory = getSubAgentsDir(workspaceDir);\n\tif (!existsSync(directory)) {\n\t\treturn { directory, agents: [], warnings: [] };\n\t}\n\n\tconst warnings: string[] = [];\n\tconst agents: SubAgentConfig[] = [];\n\tconst seenNames = new Set<string>();\n\tlet entries: Dirent<string>[];\n\ttry {\n\t\tentries = readdirSync(directory, { withFileTypes: true })\n\t\t\t.filter((entry) => entry.name.endsWith(\".md\") && (entry.isFile() || entry.isSymbolicLink()))\n\t\t\t.sort((a, b) => a.name.localeCompare(b.name));\n\t} catch (error) {\n\t\treturn {\n\t\t\tdirectory,\n\t\t\tagents: [],\n\t\t\twarnings: [`Failed to read sub-agents directory (${error instanceof Error ? error.message : String(error)})`],\n\t\t};\n\t}\n\n\tfor (const entry of entries) {\n\t\tconst filePath = join(directory, entry.name);\n\t\tlet content = \"\";\n\t\ttry {\n\t\t\tcontent = readFileSync(filePath, \"utf-8\");\n\t\t} catch (error) {\n\t\t\twarnings.push(\n\t\t\t\t`${entry.name}: failed to read file (${error instanceof Error ? error.message : String(error)})`,\n\t\t\t);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst { frontmatter, body } = parseFrontmatter<Record<string, string>>(content);\n\t\tconst name = frontmatter.name?.trim();\n\t\tconst description = frontmatter.description?.trim();\n\n\t\tif (!name || !description) {\n\t\t\twarnings.push(`${entry.name}: missing required frontmatter fields \"name\" or \"description\"`);\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (seenNames.has(name)) {\n\t\t\twarnings.push(`${entry.name}: duplicate sub-agent name \"${name}\" ignored`);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst toolParse = parseToolNames(frontmatter.tools);\n\t\tif (toolParse.error) {\n\t\t\twarnings.push(`${entry.name}: ${toolParse.error}`);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst maxTurns = parsePositiveInteger(frontmatter.maxTurns, DEFAULT_MAX_TURNS);\n\t\tconst maxToolCalls = parsePositiveInteger(frontmatter.maxToolCalls, DEFAULT_MAX_TOOL_CALLS);\n\t\tconst maxWallTimeSec = parsePositiveInteger(frontmatter.maxWallTimeSec, DEFAULT_MAX_WALL_TIME_SEC);\n\t\tconst bashTimeoutSec = parsePositiveInteger(frontmatter.bashTimeoutSec, DEFAULT_BASH_TIMEOUT_SEC);\n\n\t\tfor (const warning of [maxTurns.warning, maxToolCalls.warning, maxWallTimeSec.warning, bashTimeoutSec.warning]) {\n\t\t\tif (warning) {\n\t\t\t\twarnings.push(`${entry.name}: ${warning}`);\n\t\t\t}\n\t\t}\n\n\t\tconst modelRef = frontmatter.model?.trim();\n\t\tlet model: Model<Api> | undefined;\n\t\tif (modelRef) {\n\t\t\tconst resolved = resolveModelReference(modelRef, availableModels);\n\t\t\tif (!resolved.model) {\n\t\t\t\twarnings.push(`${entry.name}: ${resolved.error}`);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tmodel = resolved.model;\n\t\t}\n\n\t\tconst trimmedBody = body.trim();\n\t\tif (!trimmedBody) {\n\t\t\twarnings.push(`${entry.name}: empty system prompt body`);\n\t\t\tcontinue;\n\t\t}\n\t\tconst promptLengthError = validateSubAgentSystemPrompt(trimmedBody, \"Sub-agent system prompt\");\n\t\tif (promptLengthError) {\n\t\t\twarnings.push(`${entry.name}: ${promptLengthError}`);\n\t\t\tcontinue;\n\t\t}\n\n\t\tseenNames.add(name);\n\t\tagents.push({\n\t\t\tname,\n\t\t\tdescription,\n\t\t\tsystemPrompt: trimmedBody,\n\t\t\ttools: toolParse.tools,\n\t\t\tmodel,\n\t\t\tmodelRef: modelRef || (model ? formatModelReference(model) : undefined),\n\t\t\tmaxTurns: maxTurns.value,\n\t\t\tmaxToolCalls: maxToolCalls.value,\n\t\t\tmaxWallTimeSec: maxWallTimeSec.value,\n\t\t\tbashTimeoutSec: bashTimeoutSec.value,\n\t\t\tfilePath,\n\t\t\tsource: \"predefined\",\n\t\t});\n\t}\n\n\treturn { directory, agents, warnings };\n}\n\nexport function resolveSubAgentConfig(\n\tavailableModels: Model<Api>[],\n\tcurrentModel: Model<Api>,\n\tpredefinedAgents: SubAgentConfig[],\n\toverrides: SubAgentInvocationOverrides,\n): { config?: ResolvedSubAgentConfig; error?: string } {\n\tconst baseConfig = overrides.agent ? predefinedAgents.find((agent) => agent.name === overrides.agent) : undefined;\n\tif (overrides.agent && !baseConfig) {\n\t\tconst available = predefinedAgents.length > 0 ? predefinedAgents.map((agent) => agent.name).join(\", \") : \"none\";\n\t\treturn { error: `Unknown sub-agent \"${overrides.agent}\". Available sub-agents: ${available}.` };\n\t}\n\n\tif (!baseConfig && (!overrides.systemPrompt || !overrides.systemPrompt.trim())) {\n\t\treturn { error: 'Provide either \"agent\" or \"systemPrompt\" to define the sub-agent.' };\n\t}\n\n\tconst tools = overrides.tools\n\t\t? validateToolNames(overrides.tools)\n\t\t: { tools: baseConfig?.tools ?? [...DEFAULT_SUB_AGENT_TOOLS] };\n\tif (tools.error) {\n\t\treturn { error: tools.error };\n\t}\n\n\tlet model = baseConfig?.model;\n\tlet modelRef = baseConfig?.modelRef;\n\tif (overrides.model?.trim()) {\n\t\tconst resolved = resolveModelReference(overrides.model.trim(), availableModels);\n\t\tif (!resolved.model) {\n\t\t\treturn { error: resolved.error };\n\t\t}\n\t\tmodel = resolved.model;\n\t\tmodelRef = formatModelReference(resolved.model);\n\t}\n\n\tconst maxTurns = resolvePositiveOverride(overrides.maxTurns, baseConfig?.maxTurns ?? DEFAULT_MAX_TURNS);\n\tconst maxToolCalls = resolvePositiveOverride(\n\t\toverrides.maxToolCalls,\n\t\tbaseConfig?.maxToolCalls ?? DEFAULT_MAX_TOOL_CALLS,\n\t);\n\tconst maxWallTimeSec = resolvePositiveOverride(\n\t\toverrides.maxWallTimeSec,\n\t\tbaseConfig?.maxWallTimeSec ?? DEFAULT_MAX_WALL_TIME_SEC,\n\t);\n\tconst bashTimeoutSec = resolvePositiveOverride(\n\t\toverrides.bashTimeoutSec,\n\t\tbaseConfig?.bashTimeoutSec ?? DEFAULT_BASH_TIMEOUT_SEC,\n\t);\n\n\tconst systemPrompt = overrides.systemPrompt?.trim() || baseConfig?.systemPrompt || \"\";\n\tif (!systemPrompt) {\n\t\treturn { error: \"Sub-agent system prompt cannot be empty.\" };\n\t}\n\tif (overrides.systemPrompt?.trim()) {\n\t\tconst promptLengthError = validateSubAgentSystemPrompt(\n\t\t\toverrides.systemPrompt.trim(),\n\t\t\t\"Inline sub-agent systemPrompt\",\n\t\t);\n\t\tif (promptLengthError) {\n\t\t\treturn { error: promptLengthError };\n\t\t}\n\t}\n\n\treturn {\n\t\tconfig: {\n\t\t\tname: overrides.name?.trim() || baseConfig?.name || \"dynamic-subagent\",\n\t\t\tdescription: baseConfig?.description || \"Inline sub-agent\",\n\t\t\tsystemPrompt,\n\t\t\ttools: tools.tools,\n\t\t\tmodel: model ?? currentModel,\n\t\t\tmodelRef: modelRef ?? formatModelReference(model ?? currentModel),\n\t\t\tmaxTurns,\n\t\t\tmaxToolCalls,\n\t\t\tmaxWallTimeSec,\n\t\t\tbashTimeoutSec,\n\t\t\tfilePath: baseConfig?.filePath,\n\t\t\tsource: baseConfig ? \"predefined\" : \"inline\",\n\t\t},\n\t};\n}\n\nexport function formatSubAgentList(agents: SubAgentConfig[], maxItems: number = 12): string {\n\tif (agents.length === 0) {\n\t\treturn \"none\";\n\t}\n\n\tconst listed = agents.slice(0, maxItems).map((agent) => `- \\`${agent.name}\\`: ${agent.description}`);\n\tif (agents.length <= maxItems) {\n\t\treturn listed.join(\"\\n\");\n\t}\n\n\treturn `${listed.join(\"\\n\")}\\n- ... and ${agents.length - maxItems} more`;\n}\n"]}
1
+ {"version":3,"file":"sub-agents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nimport { parseFrontmatter } from \"@mariozechner/pi-coding-agent\";\nimport type { Dirent } from \"fs\";\nimport { existsSync, readdirSync, readFileSync } from \"fs\";\nimport { join } from \"path\";\nimport { findExactModelReferenceMatch, formatModelReference } from \"./model-utils.js\";\nimport { SUB_AGENTS_DIR_NAME } from \"./paths.js\";\n\nconst ALLOWED_SUB_AGENT_TOOLS = [\"read\", \"bash\", \"edit\", \"write\"] as const;\nconst DEFAULT_SUB_AGENT_TOOLS = [\"read\", \"bash\"] as const;\nconst DEFAULT_MAX_TURNS = 24;\nconst DEFAULT_MAX_TOOL_CALLS = 48;\nconst DEFAULT_MAX_WALL_TIME_SEC = 300;\nconst DEFAULT_BASH_TIMEOUT_SEC = 120;\nconst MAX_SUB_AGENT_TASK_CHARS = 12000;\nconst MAX_SUB_AGENT_SYSTEM_PROMPT_CHARS = 16000;\n\nexport type SubAgentToolName = (typeof ALLOWED_SUB_AGENT_TOOLS)[number];\n\nexport interface SubAgentConfig {\n\tname: string;\n\tdescription: string;\n\tsystemPrompt: string;\n\ttools: SubAgentToolName[];\n\tmodel?: Model<Api>;\n\tmodelRef?: string;\n\tmaxTurns: number;\n\tmaxToolCalls: number;\n\tmaxWallTimeSec: number;\n\tbashTimeoutSec: number;\n\tfilePath?: string;\n\tsource: \"predefined\" | \"inline\";\n}\n\nexport interface ResolvedSubAgentConfig extends Omit<SubAgentConfig, \"model\" | \"modelRef\"> {\n\tmodel: Model<Api>;\n\tmodelRef: string;\n}\n\nexport interface SubAgentDiscoveryResult {\n\tdirectory: string;\n\tagents: SubAgentConfig[];\n\twarnings: string[];\n}\n\nexport interface SubAgentInvocationOverrides {\n\tagent?: string;\n\tname?: string;\n\tsystemPrompt?: string;\n\ttools?: string[];\n\tmodel?: string;\n\tmaxTurns?: number;\n\tmaxToolCalls?: number;\n\tmaxWallTimeSec?: number;\n\tbashTimeoutSec?: number;\n}\n\nfunction validateTextLength(value: string, maxChars: number, label: string): string | undefined {\n\tif (value.length <= maxChars) {\n\t\treturn undefined;\n\t}\n\treturn `${label} exceeds ${maxChars} characters (got ${value.length}).`;\n}\n\nexport function validateSubAgentTask(task: string): string | undefined {\n\treturn validateTextLength(task, MAX_SUB_AGENT_TASK_CHARS, \"Sub-agent task\");\n}\n\nfunction validateSubAgentSystemPrompt(systemPrompt: string, label: string): string | undefined {\n\treturn validateTextLength(systemPrompt, MAX_SUB_AGENT_SYSTEM_PROMPT_CHARS, label);\n}\n\nexport function getSubAgentsDir(workspaceDir: string): string {\n\treturn join(workspaceDir, SUB_AGENTS_DIR_NAME);\n}\n\nfunction readOptionalTrimmedString(value: unknown): string | undefined {\n\tif (typeof value !== \"string\") {\n\t\treturn undefined;\n\t}\n\n\tconst trimmed = value.trim();\n\treturn trimmed ? trimmed : undefined;\n}\n\nfunction parseToolNames(raw: unknown): { tools: SubAgentToolName[]; error?: string } {\n\tif (raw === undefined || raw === null) {\n\t\treturn { tools: [...DEFAULT_SUB_AGENT_TOOLS] };\n\t}\n\n\tif (typeof raw === \"string\") {\n\t\tif (!raw.trim()) {\n\t\t\treturn { tools: [...DEFAULT_SUB_AGENT_TOOLS] };\n\t\t}\n\n\t\tconst values = raw\n\t\t\t.split(\",\")\n\t\t\t.map((value) => value.trim())\n\t\t\t.filter((value) => value.length > 0);\n\n\t\treturn validateToolNames(values);\n\t}\n\n\tif (Array.isArray(raw)) {\n\t\tconst invalidValue = raw.find((value) => typeof value !== \"string\");\n\t\tif (invalidValue !== undefined) {\n\t\t\treturn { tools: [], error: 'Invalid \"tools\" frontmatter: expected a string or string[]' };\n\t\t}\n\t\treturn validateToolNames(raw);\n\t}\n\n\treturn { tools: [], error: 'Invalid \"tools\" frontmatter: expected a string or string[]' };\n}\n\nexport function validateToolNames(values: string[] | undefined): { tools: SubAgentToolName[]; error?: string } {\n\tif (!values || values.length === 0) {\n\t\treturn { tools: [...DEFAULT_SUB_AGENT_TOOLS] };\n\t}\n\n\tconst tools: SubAgentToolName[] = [];\n\tconst seen = new Set<string>();\n\tfor (const value of values) {\n\t\tconst normalized = value.trim();\n\t\tif (!normalized || seen.has(normalized)) {\n\t\t\tcontinue;\n\t\t}\n\t\tif (!ALLOWED_SUB_AGENT_TOOLS.includes(normalized as SubAgentToolName)) {\n\t\t\treturn {\n\t\t\t\ttools: [],\n\t\t\t\terror: `Unknown tool \"${normalized}\". Allowed tools: ${ALLOWED_SUB_AGENT_TOOLS.join(\", \")}`,\n\t\t\t};\n\t\t}\n\t\tseen.add(normalized);\n\t\ttools.push(normalized as SubAgentToolName);\n\t}\n\n\treturn { tools: tools.length > 0 ? tools : [...DEFAULT_SUB_AGENT_TOOLS] };\n}\n\nfunction parsePositiveInteger(raw: unknown, fallback: number): { value: number; warning?: string } {\n\tif (raw === undefined || raw === null) {\n\t\treturn { value: fallback };\n\t}\n\n\tif (typeof raw === \"number\") {\n\t\tif (!Number.isFinite(raw) || raw <= 0) {\n\t\t\treturn { value: fallback, warning: `Invalid numeric value \"${String(raw)}\", using default ${fallback}` };\n\t\t}\n\t\treturn { value: Math.floor(raw) };\n\t}\n\n\tif (typeof raw !== \"string\") {\n\t\treturn { value: fallback, warning: `Invalid numeric value \"${String(raw)}\", using default ${fallback}` };\n\t}\n\n\tif (!raw.trim()) {\n\t\treturn { value: fallback };\n\t}\n\n\tconst parsed = Number.parseInt(raw.trim(), 10);\n\tif (!Number.isFinite(parsed) || parsed <= 0) {\n\t\treturn { value: fallback, warning: `Invalid numeric value \"${raw}\", using default ${fallback}` };\n\t}\n\n\treturn { value: parsed };\n}\n\nfunction resolvePositiveOverride(value: number | undefined, fallback: number): number {\n\tif (!Number.isFinite(value) || value === undefined || value <= 0) {\n\t\treturn fallback;\n\t}\n\treturn Math.floor(value);\n}\n\nfunction resolveModelReference(\n\tmodelRef: string,\n\tavailableModels: Model<Api>[],\n): { model?: Model<Api>; error?: string } {\n\tconst { match, ambiguous } = findExactModelReferenceMatch(modelRef, availableModels);\n\tif (match) {\n\t\treturn { model: match };\n\t}\n\tif (ambiguous) {\n\t\treturn { error: `Model reference \"${modelRef}\" is ambiguous. Use provider/modelId.` };\n\t}\n\treturn { error: `Model reference \"${modelRef}\" was not found among available models.` };\n}\n\nexport function discoverSubAgents(workspaceDir: string, availableModels: Model<Api>[]): SubAgentDiscoveryResult {\n\tconst directory = getSubAgentsDir(workspaceDir);\n\tif (!existsSync(directory)) {\n\t\treturn { directory, agents: [], warnings: [] };\n\t}\n\n\tconst warnings: string[] = [];\n\tconst agents: SubAgentConfig[] = [];\n\tconst seenNames = new Set<string>();\n\tlet entries: Dirent<string>[];\n\ttry {\n\t\tentries = readdirSync(directory, { withFileTypes: true })\n\t\t\t.filter((entry) => entry.name.endsWith(\".md\") && (entry.isFile() || entry.isSymbolicLink()))\n\t\t\t.sort((a, b) => a.name.localeCompare(b.name));\n\t} catch (error) {\n\t\treturn {\n\t\t\tdirectory,\n\t\t\tagents: [],\n\t\t\twarnings: [`Failed to read sub-agents directory (${error instanceof Error ? error.message : String(error)})`],\n\t\t};\n\t}\n\n\tfor (const entry of entries) {\n\t\tconst filePath = join(directory, entry.name);\n\t\tlet content = \"\";\n\t\ttry {\n\t\t\tcontent = readFileSync(filePath, \"utf-8\");\n\t\t} catch (error) {\n\t\t\twarnings.push(\n\t\t\t\t`${entry.name}: failed to read file (${error instanceof Error ? error.message : String(error)})`,\n\t\t\t);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst { frontmatter, body } = parseFrontmatter<Record<string, unknown>>(content);\n\t\tconst name = readOptionalTrimmedString(frontmatter.name);\n\t\tconst description = readOptionalTrimmedString(frontmatter.description);\n\n\t\tif (!name || !description) {\n\t\t\twarnings.push(`${entry.name}: missing required frontmatter fields \"name\" or \"description\"`);\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (seenNames.has(name)) {\n\t\t\twarnings.push(`${entry.name}: duplicate sub-agent name \"${name}\" ignored`);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst toolParse = parseToolNames(frontmatter.tools);\n\t\tif (toolParse.error) {\n\t\t\twarnings.push(`${entry.name}: ${toolParse.error}`);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst maxTurns = parsePositiveInteger(frontmatter.maxTurns, DEFAULT_MAX_TURNS);\n\t\tconst maxToolCalls = parsePositiveInteger(frontmatter.maxToolCalls, DEFAULT_MAX_TOOL_CALLS);\n\t\tconst maxWallTimeSec = parsePositiveInteger(frontmatter.maxWallTimeSec, DEFAULT_MAX_WALL_TIME_SEC);\n\t\tconst bashTimeoutSec = parsePositiveInteger(frontmatter.bashTimeoutSec, DEFAULT_BASH_TIMEOUT_SEC);\n\n\t\tfor (const warning of [maxTurns.warning, maxToolCalls.warning, maxWallTimeSec.warning, bashTimeoutSec.warning]) {\n\t\t\tif (warning) {\n\t\t\t\twarnings.push(`${entry.name}: ${warning}`);\n\t\t\t}\n\t\t}\n\n\t\tconst modelRef = readOptionalTrimmedString(frontmatter.model);\n\t\tlet model: Model<Api> | undefined;\n\t\tif (modelRef) {\n\t\t\tconst resolved = resolveModelReference(modelRef, availableModels);\n\t\t\tif (!resolved.model) {\n\t\t\t\twarnings.push(`${entry.name}: ${resolved.error}`);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tmodel = resolved.model;\n\t\t}\n\n\t\tconst trimmedBody = body.trim();\n\t\tif (!trimmedBody) {\n\t\t\twarnings.push(`${entry.name}: empty system prompt body`);\n\t\t\tcontinue;\n\t\t}\n\t\tconst promptLengthError = validateSubAgentSystemPrompt(trimmedBody, \"Sub-agent system prompt\");\n\t\tif (promptLengthError) {\n\t\t\twarnings.push(`${entry.name}: ${promptLengthError}`);\n\t\t\tcontinue;\n\t\t}\n\n\t\tseenNames.add(name);\n\t\tagents.push({\n\t\t\tname,\n\t\t\tdescription,\n\t\t\tsystemPrompt: trimmedBody,\n\t\t\ttools: toolParse.tools,\n\t\t\tmodel,\n\t\t\tmodelRef: modelRef || (model ? formatModelReference(model) : undefined),\n\t\t\tmaxTurns: maxTurns.value,\n\t\t\tmaxToolCalls: maxToolCalls.value,\n\t\t\tmaxWallTimeSec: maxWallTimeSec.value,\n\t\t\tbashTimeoutSec: bashTimeoutSec.value,\n\t\t\tfilePath,\n\t\t\tsource: \"predefined\",\n\t\t});\n\t}\n\n\treturn { directory, agents, warnings };\n}\n\nexport function resolveSubAgentConfig(\n\tavailableModels: Model<Api>[],\n\tcurrentModel: Model<Api>,\n\tpredefinedAgents: SubAgentConfig[],\n\toverrides: SubAgentInvocationOverrides,\n): { config?: ResolvedSubAgentConfig; error?: string } {\n\tconst baseConfig = overrides.agent ? predefinedAgents.find((agent) => agent.name === overrides.agent) : undefined;\n\tif (overrides.agent && !baseConfig) {\n\t\tconst available = predefinedAgents.length > 0 ? predefinedAgents.map((agent) => agent.name).join(\", \") : \"none\";\n\t\treturn { error: `Unknown sub-agent \"${overrides.agent}\". Available sub-agents: ${available}.` };\n\t}\n\n\tif (!baseConfig && (!overrides.systemPrompt || !overrides.systemPrompt.trim())) {\n\t\treturn { error: 'Provide either \"agent\" or \"systemPrompt\" to define the sub-agent.' };\n\t}\n\n\tconst tools = overrides.tools\n\t\t? validateToolNames(overrides.tools)\n\t\t: { tools: baseConfig?.tools ?? [...DEFAULT_SUB_AGENT_TOOLS] };\n\tif (tools.error) {\n\t\treturn { error: tools.error };\n\t}\n\n\tlet model = baseConfig?.model;\n\tlet modelRef = baseConfig?.modelRef;\n\tif (overrides.model?.trim()) {\n\t\tconst resolved = resolveModelReference(overrides.model.trim(), availableModels);\n\t\tif (!resolved.model) {\n\t\t\treturn { error: resolved.error };\n\t\t}\n\t\tmodel = resolved.model;\n\t\tmodelRef = formatModelReference(resolved.model);\n\t}\n\n\tconst maxTurns = resolvePositiveOverride(overrides.maxTurns, baseConfig?.maxTurns ?? DEFAULT_MAX_TURNS);\n\tconst maxToolCalls = resolvePositiveOverride(\n\t\toverrides.maxToolCalls,\n\t\tbaseConfig?.maxToolCalls ?? DEFAULT_MAX_TOOL_CALLS,\n\t);\n\tconst maxWallTimeSec = resolvePositiveOverride(\n\t\toverrides.maxWallTimeSec,\n\t\tbaseConfig?.maxWallTimeSec ?? DEFAULT_MAX_WALL_TIME_SEC,\n\t);\n\tconst bashTimeoutSec = resolvePositiveOverride(\n\t\toverrides.bashTimeoutSec,\n\t\tbaseConfig?.bashTimeoutSec ?? DEFAULT_BASH_TIMEOUT_SEC,\n\t);\n\n\tconst systemPrompt = overrides.systemPrompt?.trim() || baseConfig?.systemPrompt || \"\";\n\tif (!systemPrompt) {\n\t\treturn { error: \"Sub-agent system prompt cannot be empty.\" };\n\t}\n\tif (overrides.systemPrompt?.trim()) {\n\t\tconst promptLengthError = validateSubAgentSystemPrompt(\n\t\t\toverrides.systemPrompt.trim(),\n\t\t\t\"Inline sub-agent systemPrompt\",\n\t\t);\n\t\tif (promptLengthError) {\n\t\t\treturn { error: promptLengthError };\n\t\t}\n\t}\n\n\treturn {\n\t\tconfig: {\n\t\t\tname: overrides.name?.trim() || baseConfig?.name || \"dynamic-subagent\",\n\t\t\tdescription: baseConfig?.description || \"Inline sub-agent\",\n\t\t\tsystemPrompt,\n\t\t\ttools: tools.tools,\n\t\t\tmodel: model ?? currentModel,\n\t\t\tmodelRef: modelRef ?? formatModelReference(model ?? currentModel),\n\t\t\tmaxTurns,\n\t\t\tmaxToolCalls,\n\t\t\tmaxWallTimeSec,\n\t\t\tbashTimeoutSec,\n\t\t\tfilePath: baseConfig?.filePath,\n\t\t\tsource: baseConfig ? \"predefined\" : \"inline\",\n\t\t},\n\t};\n}\n\nexport function formatSubAgentList(agents: SubAgentConfig[], maxItems: number = 12): string {\n\tif (agents.length === 0) {\n\t\treturn \"none\";\n\t}\n\n\tconst listed = agents.slice(0, maxItems).map((agent) => `- \\`${agent.name}\\`: ${agent.description}`);\n\tif (agents.length <= maxItems) {\n\t\treturn listed.join(\"\\n\");\n\t}\n\n\treturn `${listed.join(\"\\n\")}\\n- ... and ${agents.length - maxItems} more`;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"attach.d.ts","sourceRoot":"","sources":["../../src/tools/attach.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAI7D,QAAA,MAAM,YAAY;;;;EAIhB,CAAC;AAEH,MAAM,MAAM,cAAc,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAEjF;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,CAAC,EAAE,cAAc,GAAG,SAAS,CAAC,OAAO,YAAY,CAAC,CAiC1F","sourcesContent":["import type { AgentTool } from \"@mariozechner/pi-agent-core\";\nimport { Type } from \"@sinclair/typebox\";\nimport { basename, resolve as resolvePath } from \"path\";\n\nconst attachSchema = Type.Object({\n\tlabel: Type.String({ description: \"Brief description of what you're sharing (shown to user)\" }),\n\tpath: Type.String({ description: \"Path to the file to attach\" }),\n\ttitle: Type.Optional(Type.String({ description: \"Title for the file (defaults to filename)\" })),\n});\n\nexport type UploadFunction = (filePath: string, title?: string) => Promise<void>;\n\n/**\n * Create the attach tool. If no uploadFn is provided, the tool will throw\n * an informative error guiding the LLM to use alternative approaches.\n */\nexport function createAttachTool(uploadFn?: UploadFunction): AgentTool<typeof attachSchema> {\n\treturn {\n\t\tname: \"attach\",\n\t\tlabel: \"attach\",\n\t\tdescription:\n\t\t\t\"Attach a file to your response. Use this to share files, images, or documents with the user. Only files from /workspace/ can be attached.\",\n\t\tparameters: attachSchema,\n\t\texecute: async (\n\t\t\t_toolCallId: string,\n\t\t\t{ path, title }: { label: string; path: string; title?: string },\n\t\t\tsignal?: AbortSignal,\n\t\t) => {\n\t\t\tif (!uploadFn) {\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"File upload is not supported in DingTalk mode. Output file content as text instead, or use bash to host files.\",\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tif (signal?.aborted) {\n\t\t\t\tthrow new Error(\"Operation aborted\");\n\t\t\t}\n\n\t\t\tconst absolutePath = resolvePath(path);\n\t\t\tconst fileName = title || basename(absolutePath);\n\n\t\t\tawait uploadFn(absolutePath, fileName);\n\n\t\t\treturn {\n\t\t\t\tcontent: [{ type: \"text\" as const, text: `Attached file: ${fileName}` }],\n\t\t\t\tdetails: undefined,\n\t\t\t};\n\t\t},\n\t};\n}\n"]}
1
+ {"version":3,"file":"attach.d.ts","sourceRoot":"","sources":["../../src/tools/attach.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAI7D,QAAA,MAAM,YAAY;;;;EAIhB,CAAC;AAEH,MAAM,MAAM,cAAc,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAEjF;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,CAAC,EAAE,cAAc,GAAG,SAAS,CAAC,OAAO,YAAY,CAAC,CAiC1F"}