@selesai/code 0.5.20 → 0.5.21
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.
- package/dist/extensions/handoff-new.test.ts +8 -9
- package/dist/extensions/handoff-new.ts +6 -8
- package/dist/extensions/pi-subagents/CHANGELOG.md +1367 -0
- package/dist/extensions/pi-subagents/README.md +602 -220
- package/dist/extensions/pi-subagents/banner.png +0 -0
- package/dist/extensions/pi-subagents/index.ts +1 -0
- package/dist/extensions/pi-subagents/install.mjs +3 -3
- package/dist/extensions/pi-subagents/package-lock.json +3405 -0
- package/dist/extensions/pi-subagents/package.json +26 -14
- package/dist/extensions/pi-subagents/prompts/gather-context-and-clarify.md +1 -1
- package/dist/extensions/pi-subagents/prompts/parallel-cleanup.md +9 -9
- package/dist/extensions/pi-subagents/prompts/parallel-context-build.md +3 -3
- package/dist/extensions/pi-subagents/prompts/parallel-handoff-plan.md +7 -7
- package/dist/extensions/pi-subagents/prompts/parallel-research.md +6 -6
- package/dist/extensions/pi-subagents/prompts/parallel-review.md +8 -8
- package/dist/extensions/pi-subagents/prompts/review-loop.md +13 -11
- package/dist/extensions/pi-subagents/skills/pi-subagents/SKILL.md +267 -176
- package/dist/extensions/pi-subagents/src/agents/agent-management.ts +89 -16
- package/dist/extensions/pi-subagents/src/agents/agent-serializer.ts +19 -0
- package/dist/extensions/pi-subagents/src/agents/agents.ts +218 -120
- package/dist/extensions/pi-subagents/src/agents/frontmatter.ts +67 -13
- package/dist/extensions/pi-subagents/src/agents/proactive-skills.ts +2 -2
- package/dist/extensions/pi-subagents/src/agents/skills.ts +25 -12
- package/dist/extensions/pi-subagents/src/api/background-work.ts +197 -0
- package/dist/extensions/pi-subagents/src/api/capability-ceiling.ts +17 -0
- package/dist/extensions/pi-subagents/src/api/delegation.ts +285 -0
- package/dist/extensions/pi-subagents/src/api/preflight.ts +399 -0
- package/dist/extensions/pi-subagents/src/extension/chain-validation.ts +165 -0
- package/dist/extensions/pi-subagents/src/extension/config.ts +7 -1
- package/dist/extensions/pi-subagents/src/extension/doctor.ts +15 -0
- package/dist/extensions/pi-subagents/src/extension/fanout-child.ts +3 -1
- package/dist/extensions/pi-subagents/src/extension/index.ts +113 -157
- package/dist/extensions/pi-subagents/src/extension/rpc.ts +41 -4
- package/dist/extensions/pi-subagents/src/extension/schemas.ts +45 -15
- package/dist/extensions/pi-subagents/src/extension/steering-notices.ts +35 -0
- package/dist/extensions/pi-subagents/src/extension/tool-description.ts +28 -15
- package/dist/extensions/pi-subagents/src/intercom/intercom-bridge.ts +4 -3
- package/dist/extensions/pi-subagents/src/intercom/native-supervisor-channel.ts +225 -29
- package/dist/extensions/pi-subagents/src/intercom/result-intercom.ts +11 -0
- package/dist/extensions/pi-subagents/src/profiles/profiles.ts +8 -10
- package/dist/extensions/pi-subagents/src/runs/background/async-execution.ts +452 -62
- package/dist/extensions/pi-subagents/src/runs/background/async-job-tracker.ts +60 -9
- package/dist/extensions/pi-subagents/src/runs/background/async-resume.ts +198 -52
- package/dist/extensions/pi-subagents/src/runs/background/async-status.ts +119 -21
- package/dist/extensions/pi-subagents/src/runs/background/auto-drain.ts +67 -0
- package/dist/extensions/pi-subagents/src/runs/background/chain-append.ts +2 -0
- package/dist/extensions/pi-subagents/src/runs/background/chain-root-attachment.ts +16 -8
- package/dist/extensions/pi-subagents/src/runs/background/completion-batcher.ts +6 -4
- package/dist/extensions/pi-subagents/src/runs/background/completion-dedupe.ts +2 -11
- package/dist/extensions/pi-subagents/src/runs/background/control-channel.ts +260 -13
- package/dist/extensions/pi-subagents/src/runs/background/fleet-view.ts +32 -6
- package/dist/extensions/pi-subagents/src/runs/background/notify.ts +171 -90
- package/dist/extensions/pi-subagents/src/runs/background/process-terminal.ts +280 -0
- package/dist/extensions/pi-subagents/src/runs/background/result-watcher.ts +149 -88
- package/dist/extensions/pi-subagents/src/runs/background/run-id-resolver.ts +14 -2
- package/dist/extensions/pi-subagents/src/runs/background/run-status.ts +33 -17
- package/dist/extensions/pi-subagents/src/runs/background/scheduled-runs.ts +9 -1
- package/dist/extensions/pi-subagents/src/runs/background/stale-run-reconciler.ts +38 -10
- package/dist/extensions/pi-subagents/src/runs/background/steering.ts +237 -0
- package/dist/extensions/pi-subagents/src/runs/background/subagent-runner.ts +1297 -307
- package/dist/extensions/pi-subagents/src/runs/background/subagent-wait.ts +610 -0
- package/dist/extensions/pi-subagents/src/runs/background/top-level-async.ts +2 -1
- package/dist/extensions/pi-subagents/src/runs/background/wait-config.ts +36 -0
- package/dist/extensions/pi-subagents/src/runs/background/wait-tool.ts +26 -0
- package/dist/extensions/pi-subagents/src/runs/foreground/async-steering-action.ts +230 -0
- package/dist/extensions/pi-subagents/src/runs/foreground/chain-clarify.ts +22 -6
- package/dist/extensions/pi-subagents/src/runs/foreground/chain-execution.ts +228 -140
- package/dist/extensions/pi-subagents/src/runs/foreground/execution.ts +452 -138
- package/dist/extensions/pi-subagents/src/runs/foreground/foreground-control.ts +90 -0
- package/dist/extensions/pi-subagents/src/runs/foreground/subagent-executor.ts +1001 -416
- package/dist/extensions/pi-subagents/src/runs/shared/acceptance.ts +443 -132
- package/dist/extensions/pi-subagents/src/runs/shared/agent-contract.ts +38 -0
- package/dist/extensions/pi-subagents/src/runs/shared/capability-ceiling.ts +178 -0
- package/dist/extensions/pi-subagents/src/runs/shared/child-protocol.ts +121 -0
- package/dist/extensions/pi-subagents/src/runs/shared/completion-guard.ts +41 -93
- package/dist/extensions/pi-subagents/src/runs/shared/context-mode.ts +44 -0
- package/dist/extensions/pi-subagents/src/runs/shared/dynamic-fanout.ts +11 -9
- package/dist/extensions/pi-subagents/src/runs/shared/long-running-guard.ts +4 -0
- package/dist/extensions/pi-subagents/src/runs/shared/mcp-direct-tool-allowlist.ts +12 -6
- package/dist/extensions/pi-subagents/src/runs/shared/model-fallback.ts +36 -0
- package/dist/extensions/pi-subagents/src/runs/shared/nested-events.ts +44 -7
- package/dist/extensions/pi-subagents/src/runs/shared/nested-render.ts +4 -1
- package/dist/extensions/pi-subagents/src/runs/shared/parallel-handoff.ts +154 -0
- package/dist/extensions/pi-subagents/src/runs/shared/parallel-utils.ts +18 -0
- package/dist/extensions/pi-subagents/src/runs/shared/pi-args.ts +174 -54
- package/dist/extensions/pi-subagents/src/runs/shared/pi-spawn.ts +38 -31
- package/dist/extensions/pi-subagents/src/runs/shared/run-history.ts +90 -5
- package/dist/extensions/pi-subagents/src/runs/shared/session-lease.ts +299 -0
- package/dist/extensions/pi-subagents/src/runs/shared/single-output.ts +61 -6
- package/dist/extensions/pi-subagents/src/runs/shared/spawn-budget.ts +128 -0
- package/dist/extensions/pi-subagents/src/runs/shared/structured-output.ts +112 -7
- package/dist/extensions/pi-subagents/src/runs/shared/subagent-control.ts +14 -6
- package/dist/extensions/pi-subagents/src/runs/shared/subagent-prompt-runtime.ts +149 -36
- package/dist/extensions/pi-subagents/src/runs/shared/task-intent.ts +181 -0
- package/dist/extensions/pi-subagents/src/runs/shared/tool-availability.ts +83 -0
- package/dist/extensions/pi-subagents/src/runs/shared/tool-budget.ts +11 -5
- package/dist/extensions/pi-subagents/src/runs/shared/turn-budget.ts +50 -4
- package/dist/extensions/pi-subagents/src/runs/shared/worktree.ts +63 -14
- package/dist/extensions/pi-subagents/src/shared/accessible-dir.ts +25 -0
- package/dist/extensions/pi-subagents/src/shared/artifacts.ts +37 -7
- package/dist/extensions/pi-subagents/src/shared/atomic-json.ts +18 -43
- package/dist/extensions/pi-subagents/src/shared/child-transcript.ts +52 -0
- package/dist/extensions/pi-subagents/src/shared/env.ts +16 -0
- package/dist/extensions/pi-subagents/src/shared/file-system-retry.ts +47 -0
- package/dist/extensions/pi-subagents/src/shared/fork-context.ts +28 -3
- package/dist/extensions/pi-subagents/src/shared/launch-contract.ts +123 -0
- package/dist/extensions/pi-subagents/src/shared/model-info.ts +8 -5
- package/dist/extensions/pi-subagents/src/shared/settings.ts +9 -1
- package/dist/extensions/pi-subagents/src/shared/status-format.ts +7 -1
- package/dist/extensions/pi-subagents/src/shared/types.ts +522 -51
- package/dist/extensions/pi-subagents/src/shared/utils.ts +59 -53
- package/dist/extensions/pi-subagents/src/slash/delegation-adapters.ts +604 -0
- package/dist/extensions/pi-subagents/src/slash/delegation-json.ts +108 -0
- package/dist/extensions/pi-subagents/src/slash/delegation-request.ts +249 -0
- package/dist/extensions/pi-subagents/src/slash/prompt-template-bridge.ts +353 -345
- package/dist/extensions/pi-subagents/src/slash/prompt-workflows.ts +2 -2
- package/dist/extensions/pi-subagents/src/slash/selector.ts +147 -0
- package/dist/extensions/pi-subagents/src/slash/slash-commands.ts +265 -23
- package/dist/extensions/pi-subagents/src/slash/slash-live-state.ts +2 -2
- package/dist/extensions/pi-subagents/src/slash/subagents-admin.ts +428 -0
- package/dist/extensions/pi-subagents/src/tui/fleet-status.ts +362 -0
- package/dist/extensions/pi-subagents/src/tui/fleet-transcript.ts +472 -0
- package/dist/extensions/pi-subagents/src/tui/fleet.ts +664 -0
- package/dist/extensions/pi-subagents/src/tui/render.ts +115 -31
- package/dist/extensions/pi-subagents/src/watchdog/change-signature.ts +220 -0
- package/dist/extensions/pi-subagents/src/watchdog/child-status.ts +205 -0
- package/dist/extensions/pi-subagents/src/watchdog/emission-guard.ts +123 -0
- package/dist/extensions/pi-subagents/src/watchdog/lsp-diagnostics.ts +532 -0
- package/dist/extensions/pi-subagents/src/watchdog/model-selection.ts +167 -0
- package/dist/extensions/pi-subagents/src/watchdog/register-child.ts +117 -0
- package/dist/extensions/pi-subagents/src/watchdog/register-main.ts +433 -0
- package/dist/extensions/pi-subagents/src/watchdog/render.ts +54 -0
- package/dist/extensions/pi-subagents/src/watchdog/review.ts +298 -0
- package/dist/extensions/pi-subagents/src/watchdog/runtime.ts +714 -0
- package/dist/extensions/pi-subagents/src/watchdog/settings.ts +528 -0
- package/dist/extensions/pi-subagents/src/watchdog/tool-actions.ts +155 -0
- package/dist/extensions/pi-subagents/src/watchdog/turn-delta.ts +161 -0
- package/dist/extensions/pi-subagents/src/watchdog/types.ts +188 -0
- package/dist/extensions/pi-subagents/src/watchdog/warning-format.ts +73 -0
- package/dist/extensions/pi-subagents/test/e2e/real-session-subagent.test.ts +130 -2
- package/dist/extensions/pi-subagents/test/integration/acceptance-file-report.test.ts +439 -0
- package/dist/extensions/pi-subagents/test/integration/async-execution.test.ts +1632 -61
- package/dist/extensions/pi-subagents/test/integration/async-job-tracker.test.ts +54 -1
- package/dist/extensions/pi-subagents/test/integration/async-status.test.ts +112 -0
- package/dist/extensions/pi-subagents/test/integration/chain-clarify.test.ts +98 -1
- package/dist/extensions/pi-subagents/test/integration/chain-execution.test.ts +179 -9
- package/dist/extensions/pi-subagents/test/integration/detect-error.test.ts +16 -9
- package/dist/extensions/pi-subagents/test/integration/error-handling.test.ts +6 -5
- package/dist/extensions/pi-subagents/test/integration/fork-context-execution.test.ts +589 -9
- package/dist/extensions/pi-subagents/test/integration/intercom-result-delivery.test.ts +528 -21
- package/dist/extensions/pi-subagents/test/integration/parallel-execution.test.ts +227 -3
- package/dist/extensions/pi-subagents/test/integration/render-fork-badge.test.ts +235 -7
- package/dist/extensions/pi-subagents/test/integration/render-widget.test.ts +27 -0
- package/dist/extensions/pi-subagents/test/integration/result-watcher.test.ts +172 -2
- package/dist/extensions/pi-subagents/test/integration/single-execution.test.ts +1502 -18
- package/dist/extensions/pi-subagents/test/integration/slash-commands.test.ts +993 -4
- package/dist/extensions/pi-subagents/test/integration/slash-live-state.test.ts +27 -0
- package/dist/extensions/pi-subagents/test/integration/top-level-async.test.ts +7 -1
- package/dist/extensions/pi-subagents/test/support/helpers.ts +32 -0
- package/dist/extensions/pi-subagents/test/support/mock-pi-script.mjs +59 -2
- package/dist/extensions/pi-subagents/test/support/mock-pi.ts +32 -6
- package/dist/extensions/pi-subagents/test/support/real-session-child-cli.mjs +25 -24
- package/dist/extensions/pi-subagents/test/support/real-session-runner.ts +59 -35
- package/dist/extensions/pi-subagents/test/support/session-lease-child.mjs +42 -0
- package/dist/extensions/pi-subagents/test/unit/acceptance.test.ts +703 -69
- package/dist/extensions/pi-subagents/test/unit/accessible-dir.test.ts +82 -0
- package/dist/extensions/pi-subagents/test/unit/agent-disabled.test.ts +15 -15
- package/dist/extensions/pi-subagents/test/unit/agent-eject-disable.test.ts +66 -66
- package/dist/extensions/pi-subagents/test/unit/agent-frontmatter.test.ts +478 -19
- package/dist/extensions/pi-subagents/test/unit/agent-management.test.ts +228 -7
- package/dist/extensions/pi-subagents/test/unit/agent-memory.test.ts +1 -1
- package/dist/extensions/pi-subagents/test/unit/agent-overrides.test.ts +198 -78
- package/dist/extensions/pi-subagents/test/unit/async-execution.test.ts +15 -1
- package/dist/extensions/pi-subagents/test/unit/async-interrupt-action.test.ts +102 -7
- package/dist/extensions/pi-subagents/test/unit/async-resume.test.ts +439 -6
- package/dist/extensions/pi-subagents/test/unit/atomic-json.test.ts +41 -1
- package/dist/extensions/pi-subagents/test/unit/auto-drain.test.ts +79 -0
- package/dist/extensions/pi-subagents/test/unit/background-work.test.ts +285 -0
- package/dist/extensions/pi-subagents/test/unit/capability-ceiling-pi-args.test.ts +84 -0
- package/dist/extensions/pi-subagents/test/unit/capability-ceiling.test.ts +68 -0
- package/dist/extensions/pi-subagents/test/unit/chain-validation.test.ts +326 -0
- package/dist/extensions/pi-subagents/test/unit/child-protocol.test.ts +82 -0
- package/dist/extensions/pi-subagents/test/unit/child-transcript.test.ts +46 -4
- package/dist/extensions/pi-subagents/test/unit/completion-batcher.test.ts +3 -1
- package/dist/extensions/pi-subagents/test/unit/completion-dedupe.test.ts +9 -17
- package/dist/extensions/pi-subagents/test/unit/completion-guard.test.ts +175 -3
- package/dist/extensions/pi-subagents/test/unit/config-dir-runtime.test.ts +10 -5
- package/dist/extensions/pi-subagents/test/unit/control-channel.test.ts +126 -6
- package/dist/extensions/pi-subagents/test/unit/default-extensions.test.ts +219 -0
- package/dist/extensions/pi-subagents/test/unit/delegation-api.test.ts +913 -0
- package/dist/extensions/pi-subagents/test/unit/doctor.test.ts +13 -2
- package/dist/extensions/pi-subagents/test/unit/dynamic-fanout.test.ts +3 -3
- package/dist/extensions/pi-subagents/test/unit/fleet-status.test.ts +351 -0
- package/dist/extensions/pi-subagents/test/unit/fleet-transcript.test.ts +280 -0
- package/dist/extensions/pi-subagents/test/unit/fleet.test.ts +575 -0
- package/dist/extensions/pi-subagents/test/unit/foreground-control.test.ts +65 -0
- package/dist/extensions/pi-subagents/test/unit/fork-context.test.ts +94 -1
- package/dist/extensions/pi-subagents/test/unit/get-final-output.test.ts +23 -0
- package/dist/extensions/pi-subagents/test/unit/host-peer-runtime-imports.test.ts +157 -0
- package/dist/extensions/pi-subagents/test/unit/index-child-registration.test.ts +367 -5
- package/dist/extensions/pi-subagents/test/unit/intercom-bridge.test.ts +11 -0
- package/dist/extensions/pi-subagents/test/unit/model-fallback.test.ts +36 -0
- package/dist/extensions/pi-subagents/test/unit/model-info.test.ts +25 -3
- package/dist/extensions/pi-subagents/test/unit/model-scope.test.ts +1 -1
- package/dist/extensions/pi-subagents/test/unit/native-supervisor-channel.test.ts +341 -10
- package/dist/extensions/pi-subagents/test/unit/nested-control.test.ts +16 -0
- package/dist/extensions/pi-subagents/test/unit/nested-events.test.ts +52 -0
- package/dist/extensions/pi-subagents/test/unit/notify.test.ts +168 -7
- package/dist/extensions/pi-subagents/test/unit/package-manifest.test.ts +85 -7
- package/dist/extensions/pi-subagents/test/unit/parallel-handoff.test.ts +175 -0
- package/dist/extensions/pi-subagents/test/unit/path-resolution.test.ts +1 -1
- package/dist/extensions/pi-subagents/test/unit/pi-args.test.ts +243 -16
- package/dist/extensions/pi-subagents/test/unit/pi-coding-agent-dir.test.ts +79 -36
- package/dist/extensions/pi-subagents/test/unit/pi-spawn.test.ts +174 -56
- package/dist/extensions/pi-subagents/test/unit/preflight.test.ts +322 -0
- package/dist/extensions/pi-subagents/test/unit/process-terminal.test.ts +204 -0
- package/dist/extensions/pi-subagents/test/unit/recursion-guard.test.ts +20 -12
- package/dist/extensions/pi-subagents/test/unit/rpc.test.ts +117 -4
- package/dist/extensions/pi-subagents/test/unit/run-status.test.ts +40 -7
- package/dist/extensions/pi-subagents/test/unit/scheduled-runs.test.ts +48 -1
- package/dist/extensions/pi-subagents/test/unit/schemas.test.ts +21 -3
- package/dist/extensions/pi-subagents/test/unit/selector.test.ts +57 -0
- package/dist/extensions/pi-subagents/test/unit/session-lease.test.ts +326 -0
- package/dist/extensions/pi-subagents/test/unit/single-output.test.ts +101 -2
- package/dist/extensions/pi-subagents/test/unit/skills-fallback.test.ts +111 -1
- package/dist/extensions/pi-subagents/test/unit/spawn-budget.test.ts +124 -0
- package/dist/extensions/pi-subagents/test/unit/stale-run-reconciler.test.ts +6 -4
- package/dist/extensions/pi-subagents/test/unit/status-format.test.ts +7 -0
- package/dist/extensions/pi-subagents/test/unit/steering-action.test.ts +397 -0
- package/dist/extensions/pi-subagents/test/unit/steering-notices.test.ts +58 -0
- package/dist/extensions/pi-subagents/test/unit/steering.test.ts +173 -0
- package/dist/extensions/pi-subagents/test/unit/subagent-control.test.ts +25 -4
- package/dist/extensions/pi-subagents/test/unit/subagent-prompt-runtime.test.ts +390 -25
- package/dist/extensions/pi-subagents/test/unit/subagent-wait.test.ts +837 -0
- package/dist/extensions/pi-subagents/test/unit/task-intent.test.ts +86 -0
- package/dist/extensions/pi-subagents/test/unit/tool-budget.test.ts +15 -0
- package/dist/extensions/pi-subagents/test/unit/tool-description.test.ts +47 -1
- package/dist/extensions/pi-subagents/test/unit/turn-budget.test.ts +56 -10
- package/dist/extensions/pi-subagents/test/unit/watchdog-change-signature.test.ts +387 -0
- package/dist/extensions/pi-subagents/test/unit/watchdog-child-status.test.ts +110 -0
- package/dist/extensions/pi-subagents/test/unit/watchdog-emission-guard.test.ts +62 -0
- package/dist/extensions/pi-subagents/test/unit/watchdog-lsp-diagnostics.test.ts +144 -0
- package/dist/extensions/pi-subagents/test/unit/watchdog-model-selection.test.ts +121 -0
- package/dist/extensions/pi-subagents/test/unit/watchdog-render.test.ts +76 -0
- package/dist/extensions/pi-subagents/test/unit/watchdog-review.test.ts +327 -0
- package/dist/extensions/pi-subagents/test/unit/watchdog-runtime.test.ts +863 -0
- package/dist/extensions/pi-subagents/test/unit/watchdog-settings.test.ts +290 -0
- package/dist/extensions/pi-subagents/test/unit/watchdog-tool-actions.test.ts +91 -0
- package/dist/extensions/pi-subagents/test/unit/watchdog-turn-delta.test.ts +130 -0
- package/dist/extensions/pi-subagents/test/unit/widget-nested-render.test.ts +32 -4
- package/dist/extensions/pi-subagents/test/unit/worktree.test.ts +5 -1
- package/dist/extensions/pi-subagents/test/unit/writer-budget-guidance.test.ts +30 -0
- package/dist/extensions/question/batch.ts +39 -0
- package/dist/extensions/question/dialog-adapter.ts +23 -1
- package/dist/extensions/question/index.ts +237 -7
- package/dist/extensions/question/schemas.ts +28 -15
- package/dist/extensions/question/tests/batch.test.ts +26 -0
- package/dist/extensions/question/tests/ui-protocol.test.ts +23 -3
- package/dist/extensions/question/tui-adapter.ts +2 -3
- package/dist/extensions/question/types.ts +37 -0
- package/dist/extensions/question/ui-protocol.ts +3 -3
- package/dist/skills/batch-grill-me/SKILL.md +3 -1
- package/package.json +1 -1
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SUBAGENT_DELEGATION_PROTOCOL_VERSION,
|
|
3
|
+
SUBAGENT_DELEGATION_V2_PROTOCOL_VERSION,
|
|
4
|
+
type SubagentDelegationAcceptanceResult,
|
|
5
|
+
type SubagentDelegationEffectsResult,
|
|
6
|
+
type SubagentDelegationExecutionResult,
|
|
7
|
+
type SubagentDelegationRequest,
|
|
8
|
+
type SubagentDelegationResponse,
|
|
9
|
+
type SubagentDelegationReviewResult,
|
|
10
|
+
type SubagentDelegationStatus,
|
|
11
|
+
type SubagentDelegationUpdate,
|
|
12
|
+
type SubagentDelegationV2Request,
|
|
13
|
+
type SubagentDelegationV2Response,
|
|
14
|
+
type SubagentDelegationV2Thinking,
|
|
15
|
+
type SubagentDelegationV2Value,
|
|
16
|
+
type SubagentDelegationV2Update,
|
|
17
|
+
} from "../api/delegation.ts";
|
|
18
|
+
import type { AcceptanceInput, AgentContract, EffectsProjection, ExecutionProjection, JsonSchemaObject, ReviewProjection, ToolBudgetConfig, TurnBudgetConfig, Usage } from "../shared/types.ts";
|
|
19
|
+
import { isAgentContractV1 } from "../runs/shared/agent-contract.ts";
|
|
20
|
+
import { cloneJsonWithinByteLimit } from "./delegation-json.ts";
|
|
21
|
+
|
|
22
|
+
export interface PromptTemplateDelegationTask {
|
|
23
|
+
agent: string;
|
|
24
|
+
task: string;
|
|
25
|
+
model?: string;
|
|
26
|
+
cwd?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface PromptTemplateDelegationParallelResult {
|
|
30
|
+
agent: string;
|
|
31
|
+
messages: unknown[];
|
|
32
|
+
isError: boolean;
|
|
33
|
+
errorText?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface PromptTemplateDelegationRequest {
|
|
37
|
+
requestId: string;
|
|
38
|
+
agent: string;
|
|
39
|
+
task: string;
|
|
40
|
+
tasks?: PromptTemplateDelegationTask[];
|
|
41
|
+
context: "fresh" | "fork";
|
|
42
|
+
model: string;
|
|
43
|
+
cwd: string;
|
|
44
|
+
worktree?: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface PromptTemplateDelegationResponse extends PromptTemplateDelegationRequest {
|
|
48
|
+
messages: unknown[];
|
|
49
|
+
parallelResults?: PromptTemplateDelegationParallelResult[];
|
|
50
|
+
contentText?: string;
|
|
51
|
+
isError: boolean;
|
|
52
|
+
errorText?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface PromptTemplateDelegationTaskProgress {
|
|
56
|
+
index?: number;
|
|
57
|
+
agent: string;
|
|
58
|
+
status?: string;
|
|
59
|
+
currentTool?: string;
|
|
60
|
+
currentToolArgs?: string;
|
|
61
|
+
recentOutput?: string;
|
|
62
|
+
recentOutputLines?: string[];
|
|
63
|
+
recentTools?: Array<{ tool: string; args: string }>;
|
|
64
|
+
model?: string;
|
|
65
|
+
toolCount?: number;
|
|
66
|
+
durationMs?: number;
|
|
67
|
+
tokens?: number;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface PromptTemplateDelegationUpdate {
|
|
71
|
+
requestId: string;
|
|
72
|
+
currentTool?: string;
|
|
73
|
+
currentToolArgs?: string;
|
|
74
|
+
recentOutput?: string;
|
|
75
|
+
recentOutputLines?: string[];
|
|
76
|
+
recentTools?: Array<{ tool: string; args: string }>;
|
|
77
|
+
model?: string;
|
|
78
|
+
toolCount?: number;
|
|
79
|
+
durationMs?: number;
|
|
80
|
+
tokens?: number;
|
|
81
|
+
taskProgress?: PromptTemplateDelegationTaskProgress[];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface PromptTemplateBridgeResult {
|
|
85
|
+
isError?: boolean;
|
|
86
|
+
content?: unknown;
|
|
87
|
+
details?: {
|
|
88
|
+
mode?: "single" | "parallel" | "chain" | "management";
|
|
89
|
+
runId?: string;
|
|
90
|
+
timedOut?: boolean;
|
|
91
|
+
stopped?: boolean;
|
|
92
|
+
results?: Array<{
|
|
93
|
+
agent?: string;
|
|
94
|
+
messages?: unknown[];
|
|
95
|
+
finalOutput?: string;
|
|
96
|
+
toolCalls?: Array<{ text?: string; expandedText?: string }>;
|
|
97
|
+
exitCode?: number;
|
|
98
|
+
error?: string;
|
|
99
|
+
model?: string;
|
|
100
|
+
thinking?: string;
|
|
101
|
+
structuredOutput?: unknown;
|
|
102
|
+
interrupted?: boolean;
|
|
103
|
+
timedOut?: boolean;
|
|
104
|
+
stopped?: boolean;
|
|
105
|
+
turnBudgetExceeded?: boolean;
|
|
106
|
+
toolBudgetBlocked?: boolean;
|
|
107
|
+
structuredOutputFailed?: boolean;
|
|
108
|
+
savedOutputPath?: string;
|
|
109
|
+
sessionFile?: string;
|
|
110
|
+
agentContract?: AgentContract;
|
|
111
|
+
execution?: ExecutionProjection;
|
|
112
|
+
acceptance?: SubagentDelegationAcceptanceResult;
|
|
113
|
+
review?: ReviewProjection;
|
|
114
|
+
effects?: EffectsProjection;
|
|
115
|
+
usage?: Usage;
|
|
116
|
+
progressSummary?: { toolCount?: number; durationMs?: number; tokens?: number };
|
|
117
|
+
skillsWarning?: string;
|
|
118
|
+
outputSaveError?: string;
|
|
119
|
+
transcriptError?: string;
|
|
120
|
+
}>;
|
|
121
|
+
progress?: Array<{
|
|
122
|
+
index?: number;
|
|
123
|
+
agent?: string;
|
|
124
|
+
status?: string;
|
|
125
|
+
currentTool?: string;
|
|
126
|
+
currentToolArgs?: string;
|
|
127
|
+
recentOutput?: string[];
|
|
128
|
+
recentTools?: Array<{ tool?: string; args?: string }>;
|
|
129
|
+
toolCount?: number;
|
|
130
|
+
durationMs?: number;
|
|
131
|
+
tokens?: number;
|
|
132
|
+
}>;
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export interface DelegatedSubagentExecutionParams {
|
|
137
|
+
agent?: string;
|
|
138
|
+
task?: string;
|
|
139
|
+
tasks?: PromptTemplateDelegationTask[];
|
|
140
|
+
context: "fresh" | "fork";
|
|
141
|
+
model?: string;
|
|
142
|
+
cwd: string;
|
|
143
|
+
worktree?: boolean;
|
|
144
|
+
timeoutMs?: number;
|
|
145
|
+
turnBudget?: TurnBudgetConfig;
|
|
146
|
+
/** Internal-only strict turn-boundary enforcement for versioned foreground delegation. */
|
|
147
|
+
enforceHardTurnLimit?: boolean;
|
|
148
|
+
toolBudget?: ToolBudgetConfig;
|
|
149
|
+
skill?: string | string[] | boolean;
|
|
150
|
+
output?: string | boolean;
|
|
151
|
+
outputMode?: "inline" | "file-only";
|
|
152
|
+
outputSchema?: JsonSchemaObject;
|
|
153
|
+
agentContract?: AgentContract;
|
|
154
|
+
acceptance?: AcceptanceInput;
|
|
155
|
+
artifacts?: boolean;
|
|
156
|
+
/** Internal-only thinking override accepted by executeDelegated. */
|
|
157
|
+
delegatedThinkingOverride?: SubagentDelegationV2Thinking;
|
|
158
|
+
/** Internal-only V2 capability accepted and stripped by executeDelegated. */
|
|
159
|
+
delegatedAllowZeroToolBudget?: true;
|
|
160
|
+
async: false;
|
|
161
|
+
foregroundOnly: true;
|
|
162
|
+
clarify: false;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function parseDelegationTasks(tasks: unknown): PromptTemplateDelegationTask[] {
|
|
166
|
+
if (!Array.isArray(tasks)) return [];
|
|
167
|
+
const parsed: PromptTemplateDelegationTask[] = [];
|
|
168
|
+
for (const item of tasks) {
|
|
169
|
+
if (!item || typeof item !== "object") return [];
|
|
170
|
+
const value = item as Partial<PromptTemplateDelegationTask>;
|
|
171
|
+
if (typeof value.agent !== "string" || !value.agent.trim()) return [];
|
|
172
|
+
if (typeof value.task !== "string" || !value.task.trim()) return [];
|
|
173
|
+
const model = typeof value.model === "string" && value.model.trim().length > 0 ? value.model : undefined;
|
|
174
|
+
const cwd = typeof value.cwd === "string" && value.cwd.trim().length > 0 ? value.cwd : undefined;
|
|
175
|
+
parsed.push({
|
|
176
|
+
agent: value.agent,
|
|
177
|
+
task: value.task,
|
|
178
|
+
...(model ? { model } : {}),
|
|
179
|
+
...(cwd ? { cwd } : {}),
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
return parsed;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function parsePromptTemplateRequest(data: unknown): PromptTemplateDelegationRequest | undefined {
|
|
186
|
+
if (!data || typeof data !== "object") return undefined;
|
|
187
|
+
const value = data as Partial<PromptTemplateDelegationRequest> & { tasks?: unknown };
|
|
188
|
+
if (typeof value.requestId !== "string" || !value.requestId) return undefined;
|
|
189
|
+
if (typeof value.model !== "string" || !value.model) return undefined;
|
|
190
|
+
if (typeof value.cwd !== "string" || !value.cwd) return undefined;
|
|
191
|
+
if (value.context !== "fresh" && value.context !== "fork") return undefined;
|
|
192
|
+
const tasks = parseDelegationTasks(value.tasks);
|
|
193
|
+
const worktree = value.worktree === true ? true : undefined;
|
|
194
|
+
const hasSingle =
|
|
195
|
+
typeof value.agent === "string" &&
|
|
196
|
+
value.agent.length > 0 &&
|
|
197
|
+
typeof value.task === "string" &&
|
|
198
|
+
value.task.length > 0;
|
|
199
|
+
if (!hasSingle && tasks.length === 0) return undefined;
|
|
200
|
+
|
|
201
|
+
const fallbackTask = tasks[0];
|
|
202
|
+
return {
|
|
203
|
+
requestId: value.requestId,
|
|
204
|
+
agent: hasSingle ? value.agent! : fallbackTask!.agent,
|
|
205
|
+
task: hasSingle ? value.task! : fallbackTask!.task,
|
|
206
|
+
...(tasks.length > 0 ? { tasks } : {}),
|
|
207
|
+
context: value.context,
|
|
208
|
+
model: value.model,
|
|
209
|
+
cwd: value.cwd,
|
|
210
|
+
...(worktree ? { worktree } : {}),
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function firstTextContent(content: unknown): string | undefined {
|
|
215
|
+
if (!Array.isArray(content)) return undefined;
|
|
216
|
+
for (const part of content) {
|
|
217
|
+
if (!part || typeof part !== "object") continue;
|
|
218
|
+
if ((part as { type?: string }).type !== "text") continue;
|
|
219
|
+
const text = (part as { text?: unknown }).text;
|
|
220
|
+
if (typeof text === "string" && text.trim()) return text.trim();
|
|
221
|
+
}
|
|
222
|
+
return undefined;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function filterRecentOutput(lines: string[] | undefined): string[] | undefined {
|
|
226
|
+
if (!lines || lines.length === 0) return undefined;
|
|
227
|
+
const filtered = lines.filter((line) => typeof line === "string" && line.trim() && line.trim() !== "(running...)");
|
|
228
|
+
if (filtered.length === 0) return undefined;
|
|
229
|
+
return filtered;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function sanitizeRecentTools(
|
|
233
|
+
tools: Array<{ tool?: string; args?: string }> | undefined,
|
|
234
|
+
): Array<{ tool: string; args: string }> | undefined {
|
|
235
|
+
if (!tools || tools.length === 0) return undefined;
|
|
236
|
+
const sanitized = tools.flatMap((entry) => {
|
|
237
|
+
if (typeof entry.tool !== "string" || entry.tool.trim().length === 0) return [];
|
|
238
|
+
return [{
|
|
239
|
+
tool: entry.tool,
|
|
240
|
+
args: typeof entry.args === "string" ? entry.args : String(entry.args ?? ""),
|
|
241
|
+
}];
|
|
242
|
+
});
|
|
243
|
+
return sanitized.length > 0 ? sanitized : undefined;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function resolveProgressModel(
|
|
247
|
+
update: PromptTemplateBridgeResult,
|
|
248
|
+
entry: { index?: number; agent?: string },
|
|
249
|
+
): string | undefined {
|
|
250
|
+
const results = update.details?.results;
|
|
251
|
+
if (!results || results.length === 0) return undefined;
|
|
252
|
+
if (typeof entry.index === "number" && entry.index >= 0) {
|
|
253
|
+
const byIndex = results[entry.index];
|
|
254
|
+
if (typeof byIndex?.model === "string") return byIndex.model;
|
|
255
|
+
}
|
|
256
|
+
if (entry.agent) {
|
|
257
|
+
const byAgent = results.find((result) => result.agent === entry.agent && typeof result.model === "string");
|
|
258
|
+
if (byAgent?.model) return byAgent.model;
|
|
259
|
+
}
|
|
260
|
+
const firstWithModel = results.find((result) => typeof result.model === "string");
|
|
261
|
+
return firstWithModel?.model;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function toolCallNameFromSummary(summary: { text?: string; expandedText?: string }): string | undefined {
|
|
265
|
+
const text = typeof summary.expandedText === "string" && summary.expandedText.trim().length > 0
|
|
266
|
+
? summary.expandedText.trim()
|
|
267
|
+
: typeof summary.text === "string"
|
|
268
|
+
? summary.text.trim()
|
|
269
|
+
: "";
|
|
270
|
+
if (!text) return undefined;
|
|
271
|
+
if (text.startsWith("$ ")) return "bash";
|
|
272
|
+
return text.match(/^[A-Za-z_][\w.-]*/)?.[0];
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function buildDelegationMessages(
|
|
276
|
+
result: { messages?: unknown[]; finalOutput?: string; toolCalls?: Array<{ text?: string; expandedText?: string }> },
|
|
277
|
+
fallbackText?: string,
|
|
278
|
+
): unknown[] {
|
|
279
|
+
if (Array.isArray(result.messages) && result.messages.length > 0) return result.messages;
|
|
280
|
+
const toolCallParts = (result.toolCalls ?? []).flatMap((summary) => {
|
|
281
|
+
const name = toolCallNameFromSummary(summary);
|
|
282
|
+
return name ? [{ type: "toolCall", name, arguments: { summary: summary.expandedText ?? summary.text ?? "" } }] : [];
|
|
283
|
+
});
|
|
284
|
+
const text = typeof result.finalOutput === "string" && result.finalOutput.trim().length > 0
|
|
285
|
+
? result.finalOutput.trim()
|
|
286
|
+
: fallbackText;
|
|
287
|
+
const content = [
|
|
288
|
+
...toolCallParts,
|
|
289
|
+
...(text ? [{ type: "text", text }] : []),
|
|
290
|
+
];
|
|
291
|
+
if (content.length === 0) return [];
|
|
292
|
+
return [{ role: "assistant", content }];
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export function toDelegationUpdate(requestId: string, update: PromptTemplateBridgeResult): PromptTemplateDelegationUpdate | undefined {
|
|
296
|
+
const progress = update.details?.progress?.[0];
|
|
297
|
+
const taskProgress = update.details?.progress?.map((entry) => {
|
|
298
|
+
const lastOutput = entry.recentOutput?.[entry.recentOutput.length - 1];
|
|
299
|
+
const safeLastOutput =
|
|
300
|
+
typeof lastOutput === "string" && lastOutput.trim() && lastOutput !== "(running...)"
|
|
301
|
+
? lastOutput
|
|
302
|
+
: undefined;
|
|
303
|
+
return {
|
|
304
|
+
index: entry.index,
|
|
305
|
+
agent: entry.agent ?? "builder",
|
|
306
|
+
status: entry.status,
|
|
307
|
+
currentTool: entry.currentTool,
|
|
308
|
+
currentToolArgs: entry.currentToolArgs,
|
|
309
|
+
recentOutput: safeLastOutput,
|
|
310
|
+
recentOutputLines: filterRecentOutput(entry.recentOutput),
|
|
311
|
+
recentTools: sanitizeRecentTools(entry.recentTools),
|
|
312
|
+
model: resolveProgressModel(update, entry),
|
|
313
|
+
toolCount: entry.toolCount,
|
|
314
|
+
durationMs: entry.durationMs,
|
|
315
|
+
tokens: entry.tokens,
|
|
316
|
+
};
|
|
317
|
+
});
|
|
318
|
+
if (!progress && (!taskProgress || taskProgress.length === 0)) return undefined;
|
|
319
|
+
const lastOutput = progress?.recentOutput?.[progress.recentOutput.length - 1];
|
|
320
|
+
const safeLastOutput =
|
|
321
|
+
typeof lastOutput === "string" && lastOutput.trim() && lastOutput !== "(running...)"
|
|
322
|
+
? lastOutput
|
|
323
|
+
: undefined;
|
|
324
|
+
return {
|
|
325
|
+
requestId,
|
|
326
|
+
currentTool: progress?.currentTool,
|
|
327
|
+
currentToolArgs: progress?.currentToolArgs,
|
|
328
|
+
recentOutput: safeLastOutput,
|
|
329
|
+
recentOutputLines: filterRecentOutput(progress?.recentOutput),
|
|
330
|
+
recentTools: sanitizeRecentTools(progress?.recentTools),
|
|
331
|
+
model: progress ? resolveProgressModel(update, progress) : undefined,
|
|
332
|
+
toolCount: progress?.toolCount,
|
|
333
|
+
durationMs: progress?.durationMs,
|
|
334
|
+
tokens: progress?.tokens,
|
|
335
|
+
taskProgress,
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export function toLegacyExecutionParams(request: PromptTemplateDelegationRequest): DelegatedSubagentExecutionParams {
|
|
340
|
+
if (request.tasks && request.tasks.length > 0) {
|
|
341
|
+
return {
|
|
342
|
+
tasks: request.tasks,
|
|
343
|
+
context: request.context,
|
|
344
|
+
cwd: request.cwd,
|
|
345
|
+
worktree: request.worktree,
|
|
346
|
+
async: false,
|
|
347
|
+
foregroundOnly: true,
|
|
348
|
+
clarify: false,
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
return {
|
|
352
|
+
agent: request.agent,
|
|
353
|
+
task: request.task,
|
|
354
|
+
context: request.context,
|
|
355
|
+
model: request.model,
|
|
356
|
+
cwd: request.cwd,
|
|
357
|
+
async: false,
|
|
358
|
+
foregroundOnly: true,
|
|
359
|
+
clarify: false,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export function toSubagentDelegationExecutionParams(request: SubagentDelegationRequest): DelegatedSubagentExecutionParams {
|
|
364
|
+
return {
|
|
365
|
+
agent: request.agent,
|
|
366
|
+
task: request.task,
|
|
367
|
+
context: request.context,
|
|
368
|
+
cwd: request.cwd,
|
|
369
|
+
model: request.model,
|
|
370
|
+
timeoutMs: request.timeoutMs,
|
|
371
|
+
turnBudget: request.turnBudget,
|
|
372
|
+
enforceHardTurnLimit: true,
|
|
373
|
+
toolBudget: request.toolBudget,
|
|
374
|
+
skill: request.skill,
|
|
375
|
+
output: request.output,
|
|
376
|
+
outputMode: request.outputMode,
|
|
377
|
+
outputSchema: request.outputSchema,
|
|
378
|
+
agentContract: request.agentContract,
|
|
379
|
+
acceptance: request.acceptance,
|
|
380
|
+
artifacts: request.artifacts,
|
|
381
|
+
async: false,
|
|
382
|
+
foregroundOnly: true,
|
|
383
|
+
clarify: false,
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export function toSubagentDelegationV2ExecutionParams(request: SubagentDelegationV2Request): DelegatedSubagentExecutionParams {
|
|
388
|
+
return {
|
|
389
|
+
agent: request.agent,
|
|
390
|
+
task: request.task,
|
|
391
|
+
context: request.context,
|
|
392
|
+
cwd: request.cwd,
|
|
393
|
+
model: request.model,
|
|
394
|
+
timeoutMs: request.timeoutMs,
|
|
395
|
+
turnBudget: request.turnBudget,
|
|
396
|
+
enforceHardTurnLimit: true,
|
|
397
|
+
toolBudget: request.toolBudget,
|
|
398
|
+
skill: request.skill,
|
|
399
|
+
output: false,
|
|
400
|
+
...(request.result.kind === "structured" ? { outputSchema: request.result.schema } : {}),
|
|
401
|
+
acceptance: false,
|
|
402
|
+
artifacts: request.artifacts,
|
|
403
|
+
delegatedThinkingOverride: request.thinking,
|
|
404
|
+
delegatedAllowZeroToolBudget: true,
|
|
405
|
+
async: false,
|
|
406
|
+
foregroundOnly: true,
|
|
407
|
+
clarify: false,
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export function toSubagentDelegationUpdate(requestId: string, result: PromptTemplateBridgeResult): SubagentDelegationUpdate | undefined {
|
|
412
|
+
const legacy = toDelegationUpdate(requestId, result);
|
|
413
|
+
if (!legacy) return undefined;
|
|
414
|
+
return {
|
|
415
|
+
version: SUBAGENT_DELEGATION_PROTOCOL_VERSION,
|
|
416
|
+
requestId,
|
|
417
|
+
...(legacy.currentTool ? { currentTool: legacy.currentTool } : {}),
|
|
418
|
+
...(legacy.currentToolArgs ? { currentToolArgs: legacy.currentToolArgs } : {}),
|
|
419
|
+
...(legacy.recentOutput ? { recentOutput: legacy.recentOutput } : {}),
|
|
420
|
+
...(legacy.recentOutputLines ? { recentOutputLines: legacy.recentOutputLines } : {}),
|
|
421
|
+
...(legacy.recentTools ? { recentTools: legacy.recentTools } : {}),
|
|
422
|
+
...(legacy.model ? { model: legacy.model } : {}),
|
|
423
|
+
...(typeof legacy.toolCount === "number" ? { toolCount: legacy.toolCount } : {}),
|
|
424
|
+
...(typeof legacy.durationMs === "number" ? { durationMs: legacy.durationMs } : {}),
|
|
425
|
+
...(typeof legacy.tokens === "number" ? { tokens: legacy.tokens } : {}),
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
export function toSubagentDelegationV2Update(
|
|
430
|
+
request: SubagentDelegationV2Request,
|
|
431
|
+
result: PromptTemplateBridgeResult,
|
|
432
|
+
): SubagentDelegationV2Update | undefined {
|
|
433
|
+
const legacy = toDelegationUpdate(request.requestId, result);
|
|
434
|
+
if (!legacy) return undefined;
|
|
435
|
+
return {
|
|
436
|
+
version: SUBAGENT_DELEGATION_V2_PROTOCOL_VERSION,
|
|
437
|
+
requestId: request.requestId,
|
|
438
|
+
ownerRunId: request.ownerRunId,
|
|
439
|
+
nodeId: request.nodeId,
|
|
440
|
+
...(legacy.currentTool ? { currentTool: legacy.currentTool } : {}),
|
|
441
|
+
...(legacy.currentToolArgs ? { currentToolArgs: legacy.currentToolArgs } : {}),
|
|
442
|
+
...(legacy.recentOutput ? { recentOutput: legacy.recentOutput } : {}),
|
|
443
|
+
...(legacy.recentOutputLines ? { recentOutputLines: legacy.recentOutputLines } : {}),
|
|
444
|
+
...(legacy.recentTools ? { recentTools: legacy.recentTools } : {}),
|
|
445
|
+
...(legacy.model ? { model: legacy.model } : {}),
|
|
446
|
+
...(typeof legacy.toolCount === "number" ? { toolCount: legacy.toolCount } : {}),
|
|
447
|
+
...(typeof legacy.durationMs === "number" ? { durationMs: legacy.durationMs } : {}),
|
|
448
|
+
...(typeof legacy.tokens === "number" ? { tokens: legacy.tokens } : {}),
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
function resolveSubagentDelegationStatus(
|
|
453
|
+
result: PromptTemplateBridgeResult,
|
|
454
|
+
aborted: boolean,
|
|
455
|
+
): SubagentDelegationStatus {
|
|
456
|
+
if (aborted) return "cancelled";
|
|
457
|
+
const child = result.details?.results?.[0];
|
|
458
|
+
if (!child) return "failed";
|
|
459
|
+
if (result.details?.timedOut || child.timedOut) return "timed_out";
|
|
460
|
+
if (child?.structuredOutputFailed) return "structured_output_failed";
|
|
461
|
+
if (child?.turnBudgetExceeded) return "turn_budget_exhausted";
|
|
462
|
+
if (child?.toolBudgetBlocked) return "tool_budget_exhausted";
|
|
463
|
+
if (!isAgentContractV1(child.agentContract) && child?.acceptance?.status === "rejected" && child.acceptance.explicit) return "acceptance_failed";
|
|
464
|
+
if (result.details?.stopped || child?.stopped || child?.interrupted) return "interrupted";
|
|
465
|
+
if (result.isError || child?.error || (typeof child?.exitCode === "number" && child.exitCode !== 0)) return "failed";
|
|
466
|
+
return "completed";
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
export function toSubagentDelegationResponse(
|
|
470
|
+
requestId: string,
|
|
471
|
+
result: PromptTemplateBridgeResult,
|
|
472
|
+
aborted: boolean,
|
|
473
|
+
): SubagentDelegationResponse {
|
|
474
|
+
const child = result.details?.results?.[0];
|
|
475
|
+
const progress = child?.progressSummary ?? result.details?.progress?.[0];
|
|
476
|
+
const warnings = [child?.skillsWarning, child?.outputSaveError, child?.transcriptError]
|
|
477
|
+
.filter((warning): warning is string => typeof warning === "string" && warning.length > 0);
|
|
478
|
+
const status = resolveSubagentDelegationStatus(result, aborted);
|
|
479
|
+
const fallbackError = status === "failed" ? firstTextContent(result.content) : undefined;
|
|
480
|
+
return {
|
|
481
|
+
version: SUBAGENT_DELEGATION_PROTOCOL_VERSION,
|
|
482
|
+
requestId,
|
|
483
|
+
status,
|
|
484
|
+
...(child?.error || fallbackError ? { error: child?.error ?? fallbackError } : {}),
|
|
485
|
+
...(result.details?.runId ? { runId: result.details.runId } : {}),
|
|
486
|
+
...(child ? { childIndex: 0 } : {}),
|
|
487
|
+
...(child?.agent ? { agent: child.agent } : {}),
|
|
488
|
+
...(child?.model ? { model: child.model } : {}),
|
|
489
|
+
...(typeof child?.exitCode === "number" ? { exitCode: child.exitCode } : {}),
|
|
490
|
+
...(child?.execution ? { execution: child.execution as SubagentDelegationExecutionResult } : {}),
|
|
491
|
+
...(child?.finalOutput ? { output: child.finalOutput } : {}),
|
|
492
|
+
...(child?.savedOutputPath ? { outputPath: child.savedOutputPath } : {}),
|
|
493
|
+
...(child?.sessionFile ? { sessionFile: child.sessionFile } : {}),
|
|
494
|
+
...(child?.acceptance ? { acceptance: { status: child.acceptance.status, evidenceStatus: child.acceptance.evidenceStatus, explicit: child.acceptance.explicit } } : {}),
|
|
495
|
+
...(child?.review ? { review: child.review as SubagentDelegationReviewResult } : {}),
|
|
496
|
+
...(child?.effects ? { effects: child.effects as SubagentDelegationEffectsResult } : {}),
|
|
497
|
+
...(typeof child?.usage?.turns === "number" ? { turns: child.usage.turns } : {}),
|
|
498
|
+
...(typeof progress?.toolCount === "number" ? { toolCount: progress.toolCount } : {}),
|
|
499
|
+
...(typeof progress?.durationMs === "number" ? { durationMs: progress.durationMs } : {}),
|
|
500
|
+
...(typeof progress?.tokens === "number" ? { tokens: progress.tokens } : {}),
|
|
501
|
+
...(warnings.length > 0 ? { warnings } : {}),
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const MAX_V2_RESULT_BYTES = 1024 * 1024;
|
|
506
|
+
|
|
507
|
+
export function toSubagentDelegationV2Response(
|
|
508
|
+
request: SubagentDelegationV2Request,
|
|
509
|
+
result: PromptTemplateBridgeResult,
|
|
510
|
+
aborted: boolean,
|
|
511
|
+
): SubagentDelegationV2Response {
|
|
512
|
+
const child = result.details?.results?.[0];
|
|
513
|
+
const progress = child?.progressSummary ?? result.details?.progress?.[0];
|
|
514
|
+
let status = resolveSubagentDelegationStatus(result, aborted);
|
|
515
|
+
let error = child?.error ?? (status === "failed" ? firstTextContent(result.content) : undefined);
|
|
516
|
+
let projectedResult: SubagentDelegationV2Value | undefined;
|
|
517
|
+
if (status === "completed") {
|
|
518
|
+
if (request.result.kind === "text") {
|
|
519
|
+
if (typeof child?.finalOutput !== "string") {
|
|
520
|
+
status = "failed";
|
|
521
|
+
error = "Delegated subagent did not capture a text result.";
|
|
522
|
+
} else if (Buffer.byteLength(child.finalOutput, "utf8") > MAX_V2_RESULT_BYTES) {
|
|
523
|
+
status = "failed";
|
|
524
|
+
error = "Delegated text result exceeds 1 MiB when UTF-8 encoded.";
|
|
525
|
+
} else {
|
|
526
|
+
projectedResult = { kind: "text", text: child.finalOutput };
|
|
527
|
+
}
|
|
528
|
+
} else if (child?.structuredOutput === undefined) {
|
|
529
|
+
status = "failed";
|
|
530
|
+
error = "Delegated subagent did not capture the requested structured result.";
|
|
531
|
+
} else {
|
|
532
|
+
const inspected = cloneJsonWithinByteLimit(child.structuredOutput, MAX_V2_RESULT_BYTES);
|
|
533
|
+
if (!inspected.ok) {
|
|
534
|
+
status = "failed";
|
|
535
|
+
error = inspected.reason === "too_large"
|
|
536
|
+
? "Delegated structured result exceeds 1 MiB when encoded."
|
|
537
|
+
: "Delegated structured result is not plain JSON data.";
|
|
538
|
+
} else {
|
|
539
|
+
projectedResult = { kind: "structured", value: inspected.value };
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
const usage = child?.usage;
|
|
544
|
+
return {
|
|
545
|
+
version: SUBAGENT_DELEGATION_V2_PROTOCOL_VERSION,
|
|
546
|
+
requestId: request.requestId,
|
|
547
|
+
ownerRunId: request.ownerRunId,
|
|
548
|
+
nodeId: request.nodeId,
|
|
549
|
+
status,
|
|
550
|
+
...(error ? { error } : {}),
|
|
551
|
+
...(result.details?.runId ? { runId: result.details.runId } : {}),
|
|
552
|
+
...(child?.agent ? { agent: child.agent } : {}),
|
|
553
|
+
...(child?.model ? { model: child.model } : {}),
|
|
554
|
+
...(child?.thinking ? { thinking: child.thinking } : {}),
|
|
555
|
+
...(typeof child?.exitCode === "number" ? { exitCode: child.exitCode } : {}),
|
|
556
|
+
...(projectedResult ? { result: projectedResult } : {}),
|
|
557
|
+
...(usage ? {
|
|
558
|
+
usage: {
|
|
559
|
+
input: usage.input,
|
|
560
|
+
output: usage.output,
|
|
561
|
+
cacheRead: usage.cacheRead,
|
|
562
|
+
cacheWrite: usage.cacheWrite,
|
|
563
|
+
cost: usage.cost,
|
|
564
|
+
turns: usage.turns,
|
|
565
|
+
toolCalls: progress?.toolCount ?? 0,
|
|
566
|
+
durationMs: progress?.durationMs ?? 0,
|
|
567
|
+
},
|
|
568
|
+
} : {}),
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
export function toPromptTemplateResponse(
|
|
573
|
+
request: PromptTemplateDelegationRequest,
|
|
574
|
+
result: PromptTemplateBridgeResult,
|
|
575
|
+
): PromptTemplateDelegationResponse {
|
|
576
|
+
const contentText = firstTextContent(result.content);
|
|
577
|
+
const messages = buildDelegationMessages(result.details?.results?.[0] ?? {}, contentText);
|
|
578
|
+
const parallelResults = request.tasks?.map<PromptTemplateDelegationParallelResult>((task, index) => {
|
|
579
|
+
const step = result.details?.results?.[index];
|
|
580
|
+
if (!step) {
|
|
581
|
+
return {
|
|
582
|
+
agent: task.agent,
|
|
583
|
+
messages: [],
|
|
584
|
+
isError: true,
|
|
585
|
+
errorText: "Missing result for delegated parallel task.",
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
const exitCode = typeof step.exitCode === "number" ? step.exitCode : undefined;
|
|
589
|
+
return {
|
|
590
|
+
agent: step.agent ?? task.agent,
|
|
591
|
+
messages: buildDelegationMessages(step),
|
|
592
|
+
isError: (exitCode !== undefined && exitCode !== 0) || !!step.error,
|
|
593
|
+
errorText: step.error || undefined,
|
|
594
|
+
};
|
|
595
|
+
});
|
|
596
|
+
return {
|
|
597
|
+
...request,
|
|
598
|
+
messages,
|
|
599
|
+
...(parallelResults ? { parallelResults } : {}),
|
|
600
|
+
...(contentText ? { contentText } : {}),
|
|
601
|
+
isError: result.isError === true,
|
|
602
|
+
errorText: result.isError ? contentText : undefined,
|
|
603
|
+
};
|
|
604
|
+
}
|