@narumitw/pi-subagents 2.1.4 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +187 -1228
- package/dist/child-communication-bridge.ts +136 -0
- package/dist/child-communication-bridge.ts.map +7 -0
- package/dist/chunks/chunk-UKTWUQRM.js +667 -0
- package/dist/chunks/chunk-UKTWUQRM.js.map +7 -0
- package/dist/index.ts +1448 -1614
- package/dist/index.ts.map +4 -4
- package/docs/design-principles.md +31 -0
- package/docs/ipc-transport-idea.md +103 -0
- package/docs/tools.md +109 -0
- package/package.json +11 -19
- package/src/broker-credentials.ts +71 -0
- package/src/child-communication-bridge.ts +153 -0
- package/src/child-communication-tools.ts +147 -0
- package/src/completion-renderer.ts +60 -0
- package/src/index.ts +1 -1
- package/src/message-broker.ts +628 -0
- package/src/model-output.ts +42 -0
- package/src/process.ts +688 -0
- package/src/runtime.ts +538 -0
- package/src/subagents.ts +33 -34
- package/src/tools.ts +416 -0
- package/src/types.ts +85 -0
- package/src/widget.ts +130 -0
- package/dist/chunks/auto-transport-FUUKFDIG.ts +0 -109
- package/dist/chunks/auto-transport-FUUKFDIG.ts.map +0 -7
- package/dist/chunks/capability-grant-PR72SWWS.ts +0 -19
- package/dist/chunks/capability-grant-PR72SWWS.ts.map +0 -7
- package/dist/chunks/child-peer-bridge.ts +0 -108
- package/dist/chunks/child-peer-bridge.ts.map +0 -7
- package/dist/chunks/chunk-2LMJU25E.ts +0 -1641
- package/dist/chunks/chunk-2LMJU25E.ts.map +0 -7
- package/dist/chunks/chunk-47SK2QYC.ts +0 -56
- package/dist/chunks/chunk-47SK2QYC.ts.map +0 -7
- package/dist/chunks/chunk-4AQSF7AS.ts +0 -29
- package/dist/chunks/chunk-4AQSF7AS.ts.map +0 -7
- package/dist/chunks/chunk-5DGRMKNV.ts +0 -126
- package/dist/chunks/chunk-5DGRMKNV.ts.map +0 -7
- package/dist/chunks/chunk-6H6TBBED.ts +0 -108
- package/dist/chunks/chunk-6H6TBBED.ts.map +0 -7
- package/dist/chunks/chunk-6KJ34M6S.ts +0 -159
- package/dist/chunks/chunk-6KJ34M6S.ts.map +0 -7
- package/dist/chunks/chunk-6NSJVPXX.ts +0 -480
- package/dist/chunks/chunk-6NSJVPXX.ts.map +0 -7
- package/dist/chunks/chunk-7AAJEUSL.ts +0 -238
- package/dist/chunks/chunk-7AAJEUSL.ts.map +0 -7
- package/dist/chunks/chunk-BFK2Z4ZF.ts +0 -189
- package/dist/chunks/chunk-BFK2Z4ZF.ts.map +0 -7
- package/dist/chunks/chunk-BOAXY55Y.ts +0 -153
- package/dist/chunks/chunk-BOAXY55Y.ts.map +0 -7
- package/dist/chunks/chunk-D4CR7T73.ts +0 -353
- package/dist/chunks/chunk-D4CR7T73.ts.map +0 -7
- package/dist/chunks/chunk-FEVPWRMU.ts +0 -300
- package/dist/chunks/chunk-FEVPWRMU.ts.map +0 -7
- package/dist/chunks/chunk-G6VZTCFH.ts +0 -70
- package/dist/chunks/chunk-G6VZTCFH.ts.map +0 -7
- package/dist/chunks/chunk-H3FP6DLR.ts +0 -160
- package/dist/chunks/chunk-H3FP6DLR.ts.map +0 -7
- package/dist/chunks/chunk-HA36LPCO.ts +0 -130
- package/dist/chunks/chunk-HA36LPCO.ts.map +0 -7
- package/dist/chunks/chunk-HOP5FXDT.ts +0 -27
- package/dist/chunks/chunk-HOP5FXDT.ts.map +0 -7
- package/dist/chunks/chunk-IGAMVWFN.ts +0 -139
- package/dist/chunks/chunk-IGAMVWFN.ts.map +0 -7
- package/dist/chunks/chunk-ILEQ27AL.ts +0 -45
- package/dist/chunks/chunk-ILEQ27AL.ts.map +0 -7
- package/dist/chunks/chunk-ITVWPNU4.ts +0 -819
- package/dist/chunks/chunk-ITVWPNU4.ts.map +0 -7
- package/dist/chunks/chunk-IWC32VPY.ts +0 -172
- package/dist/chunks/chunk-IWC32VPY.ts.map +0 -7
- package/dist/chunks/chunk-JSZIP73U.ts +0 -362
- package/dist/chunks/chunk-JSZIP73U.ts.map +0 -7
- package/dist/chunks/chunk-JU6LUNLP.ts +0 -380
- package/dist/chunks/chunk-JU6LUNLP.ts.map +0 -7
- package/dist/chunks/chunk-KMGKCEO4.ts +0 -38
- package/dist/chunks/chunk-KMGKCEO4.ts.map +0 -7
- package/dist/chunks/chunk-LASD73CM.ts +0 -339
- package/dist/chunks/chunk-LASD73CM.ts.map +0 -7
- package/dist/chunks/chunk-LEOYDZI3.ts +0 -22
- package/dist/chunks/chunk-LEOYDZI3.ts.map +0 -7
- package/dist/chunks/chunk-LL4LP2T7.ts +0 -737
- package/dist/chunks/chunk-LL4LP2T7.ts.map +0 -7
- package/dist/chunks/chunk-N2T5IN4X.ts +0 -18
- package/dist/chunks/chunk-N2T5IN4X.ts.map +0 -7
- package/dist/chunks/chunk-N7BLVXKK.ts +0 -73
- package/dist/chunks/chunk-N7BLVXKK.ts.map +0 -7
- package/dist/chunks/chunk-NLT67IZS.ts +0 -322
- package/dist/chunks/chunk-NLT67IZS.ts.map +0 -7
- package/dist/chunks/chunk-NTRPLF46.ts +0 -63
- package/dist/chunks/chunk-NTRPLF46.ts.map +0 -7
- package/dist/chunks/chunk-ONDTY4EL.ts +0 -91
- package/dist/chunks/chunk-ONDTY4EL.ts.map +0 -7
- package/dist/chunks/chunk-OVPHFGKO.ts +0 -1769
- package/dist/chunks/chunk-OVPHFGKO.ts.map +0 -7
- package/dist/chunks/chunk-P7OH4XMF.ts +0 -60
- package/dist/chunks/chunk-P7OH4XMF.ts.map +0 -7
- package/dist/chunks/chunk-PBZMBTNJ.ts +0 -422
- package/dist/chunks/chunk-PBZMBTNJ.ts.map +0 -7
- package/dist/chunks/chunk-PGLSFLYW.ts +0 -52
- package/dist/chunks/chunk-PGLSFLYW.ts.map +0 -7
- package/dist/chunks/chunk-RRR66UWR.ts +0 -28
- package/dist/chunks/chunk-RRR66UWR.ts.map +0 -7
- package/dist/chunks/chunk-RSUXZD6S.ts +0 -275
- package/dist/chunks/chunk-RSUXZD6S.ts.map +0 -7
- package/dist/chunks/chunk-RTCVYIZA.ts +0 -221
- package/dist/chunks/chunk-RTCVYIZA.ts.map +0 -7
- package/dist/chunks/chunk-RY2AEGMZ.ts +0 -34
- package/dist/chunks/chunk-RY2AEGMZ.ts.map +0 -7
- package/dist/chunks/chunk-SWGQLFSD.ts +0 -60
- package/dist/chunks/chunk-SWGQLFSD.ts.map +0 -7
- package/dist/chunks/chunk-TM2R67J3.ts +0 -52
- package/dist/chunks/chunk-TM2R67J3.ts.map +0 -7
- package/dist/chunks/chunk-TMZRHIIK.ts +0 -497
- package/dist/chunks/chunk-TMZRHIIK.ts.map +0 -7
- package/dist/chunks/chunk-TZ34IQ3M.ts +0 -59
- package/dist/chunks/chunk-TZ34IQ3M.ts.map +0 -7
- package/dist/chunks/chunk-UMBPMVJW.ts +0 -22
- package/dist/chunks/chunk-UMBPMVJW.ts.map +0 -7
- package/dist/chunks/chunk-VDG7LTYE.ts +0 -80
- package/dist/chunks/chunk-VDG7LTYE.ts.map +0 -7
- package/dist/chunks/chunk-X4NMONPE.ts +0 -73
- package/dist/chunks/chunk-X4NMONPE.ts.map +0 -7
- package/dist/chunks/chunk-YPJEN6NU.ts +0 -65
- package/dist/chunks/chunk-YPJEN6NU.ts.map +0 -7
- package/dist/chunks/chunk-YU53SHA7.ts +0 -328
- package/dist/chunks/chunk-YU53SHA7.ts.map +0 -7
- package/dist/chunks/completion-delivery-RSJU6BXL.ts +0 -17
- package/dist/chunks/completion-delivery-RSJU6BXL.ts.map +0 -7
- package/dist/chunks/config-status-FKGDECZ3.ts +0 -35
- package/dist/chunks/config-status-FKGDECZ3.ts.map +0 -7
- package/dist/chunks/config-ui-ABHYNGQ7.ts +0 -1212
- package/dist/chunks/config-ui-ABHYNGQ7.ts.map +0 -7
- package/dist/chunks/consult-LJU3IQY5.ts +0 -519
- package/dist/chunks/consult-LJU3IQY5.ts.map +0 -7
- package/dist/chunks/context-QDKLVQXE.ts +0 -11
- package/dist/chunks/context-QDKLVQXE.ts.map +0 -7
- package/dist/chunks/create-stateful-transport-JWC2EFYL.ts +0 -130
- package/dist/chunks/create-stateful-transport-JWC2EFYL.ts.map +0 -7
- package/dist/chunks/cwd-policy-NB6XZ5GF.ts +0 -17
- package/dist/chunks/cwd-policy-NB6XZ5GF.ts.map +0 -7
- package/dist/chunks/delegation-contract-LA56I5DT.ts +0 -24
- package/dist/chunks/delegation-contract-LA56I5DT.ts.map +0 -7
- package/dist/chunks/discovery-MIFB2U4Y.ts +0 -12
- package/dist/chunks/discovery-MIFB2U4Y.ts.map +0 -7
- package/dist/chunks/execution-Q2JZLKJA.ts +0 -4037
- package/dist/chunks/execution-Q2JZLKJA.ts.map +0 -7
- package/dist/chunks/in-process-transport-HJ6TZXC3.ts +0 -39
- package/dist/chunks/in-process-transport-HJ6TZXC3.ts.map +0 -7
- package/dist/chunks/inspect-UH2TKH6E.ts +0 -634
- package/dist/chunks/inspect-UH2TKH6E.ts.map +0 -7
- package/dist/chunks/peer-communication-KL36Z6CO.ts +0 -294
- package/dist/chunks/peer-communication-KL36Z6CO.ts.map +0 -7
- package/dist/chunks/persistence-UY3PY6E5.ts +0 -348
- package/dist/chunks/persistence-UY3PY6E5.ts.map +0 -7
- package/dist/chunks/registry-BT54L6CY.ts +0 -1445
- package/dist/chunks/registry-BT54L6CY.ts.map +0 -7
- package/dist/chunks/retained-semantic-state-GM75FZGE.ts +0 -81
- package/dist/chunks/retained-semantic-state-GM75FZGE.ts.map +0 -7
- package/dist/chunks/rpc-transport-7R7DVCEB.ts +0 -1222
- package/dist/chunks/rpc-transport-7R7DVCEB.ts.map +0 -7
- package/dist/chunks/runtime-policy-5YELCOVC.ts +0 -15
- package/dist/chunks/runtime-policy-5YELCOVC.ts.map +0 -7
- package/dist/chunks/semantic-snapshot-WPJ3UAFW.ts +0 -19
- package/dist/chunks/semantic-snapshot-WPJ3UAFW.ts.map +0 -7
- package/dist/chunks/spawn-idempotency-BNHOSMZW.ts +0 -13
- package/dist/chunks/spawn-idempotency-BNHOSMZW.ts.map +0 -7
- package/dist/chunks/stateful-lifecycle-JAR6K55H.ts +0 -15
- package/dist/chunks/stateful-lifecycle-JAR6K55H.ts.map +0 -7
- package/dist/chunks/subprocess-transport-VHZJRBTW.ts +0 -160
- package/dist/chunks/subprocess-transport-VHZJRBTW.ts.map +0 -7
- package/dist/chunks/usage-recording-store-CW3EH2SZ.ts +0 -164
- package/dist/chunks/usage-recording-store-CW3EH2SZ.ts.map +0 -7
- package/dist/chunks/workspace-WDKLN72J.ts +0 -11
- package/dist/chunks/workspace-WDKLN72J.ts.map +0 -7
- package/docs/async-runtime-protocol.md +0 -84
- package/docs/implementation-notes/pi-subagents-capability-matrix.md +0 -62
- package/docs/implementation-notes/pi-subagents-current-direction.md +0 -99
- package/docs/implementation-notes/pi-subagents-rpc-v1.md +0 -139
- package/docs/pi-subagents-diagrams.md +0 -183
- package/src/adaptive-scheduler.ts +0 -224
- package/src/admission-policy.ts +0 -78
- package/src/agent-projection.ts +0 -53
- package/src/agents/built-ins.ts +0 -71
- package/src/agents/catalog.ts +0 -241
- package/src/agents/discovery.ts +0 -265
- package/src/agents/types.ts +0 -100
- package/src/agents.ts +0 -51
- package/src/auto-transport.ts +0 -121
- package/src/blocking-status.ts +0 -63
- package/src/cached-module-loader.ts +0 -18
- package/src/capabilities.ts +0 -145
- package/src/capability-grant.ts +0 -115
- package/src/capability-router.ts +0 -107
- package/src/child-peer-bridge.ts +0 -124
- package/src/child-peer-tools.ts +0 -132
- package/src/completion-delivery.ts +0 -409
- package/src/completion-render.ts +0 -190
- package/src/completion-requirement.ts +0 -479
- package/src/completion-routing.ts +0 -24
- package/src/config-registration.ts +0 -114
- package/src/config-status.ts +0 -217
- package/src/config-ui.ts +0 -983
- package/src/consult-policy.ts +0 -15
- package/src/consult-registration.ts +0 -125
- package/src/consult-render.ts +0 -194
- package/src/consult-resources.ts +0 -51
- package/src/consult-tool.ts +0 -95
- package/src/consult.ts +0 -566
- package/src/context.ts +0 -126
- package/src/create-stateful-transport.ts +0 -157
- package/src/cwd-policy.ts +0 -183
- package/src/delegation-contract.ts +0 -463
- package/src/execution/budget.ts +0 -56
- package/src/execution/runtime-policy.ts +0 -19
- package/src/execution-plan.ts +0 -322
- package/src/execution-ui.ts +0 -259
- package/src/execution.ts +0 -1679
- package/src/in-process-transport.ts +0 -941
- package/src/inspect-registration.ts +0 -64
- package/src/inspect-render.ts +0 -334
- package/src/inspect-tool.ts +0 -45
- package/src/inspect.ts +0 -769
- package/src/integration-controller.ts +0 -98
- package/src/limits.ts +0 -75
- package/src/orchestration-metrics.ts +0 -116
- package/src/outcome.ts +0 -61
- package/src/panel-child-group.ts +0 -35
- package/src/panel-contract.ts +0 -343
- package/src/panel-evidence.ts +0 -59
- package/src/panel-execution.ts +0 -769
- package/src/panel-failure.ts +0 -56
- package/src/panel-planning.ts +0 -175
- package/src/panel-presets.ts +0 -3
- package/src/panel-prompts.ts +0 -132
- package/src/panel-reconciliation.ts +0 -57
- package/src/panel-render.ts +0 -103
- package/src/parallel-limit-ui.ts +0 -113
- package/src/params.ts +0 -278
- package/src/peer-communication.ts +0 -352
- package/src/peer-transport.ts +0 -49
- package/src/persistence.ts +0 -522
- package/src/pi-args.ts +0 -43
- package/src/pi-invocation.ts +0 -168
- package/src/process-control.ts +0 -43
- package/src/prompt-resources.ts +0 -38
- package/src/prompt-source-safety.ts +0 -48
- package/src/protocol.ts +0 -76
- package/src/registry-types.ts +0 -204
- package/src/registry.ts +0 -1698
- package/src/render-common.ts +0 -252
- package/src/render.ts +0 -704
- package/src/result-contract.ts +0 -431
- package/src/retained-semantic-state.ts +0 -100
- package/src/rpc-timeout-finalization.ts +0 -207
- package/src/rpc-transport-metadata.ts +0 -65
- package/src/rpc-transport.ts +0 -1035
- package/src/rpc-turn-capture.ts +0 -217
- package/src/runner-outcome.ts +0 -31
- package/src/runner-result.ts +0 -55
- package/src/runner-types.ts +0 -102
- package/src/runner-usage.ts +0 -48
- package/src/runner.ts +0 -866
- package/src/safe-text.ts +0 -67
- package/src/semantic-snapshot.ts +0 -214
- package/src/session-guidance-contract.ts +0 -399
- package/src/settings/inspection.ts +0 -302
- package/src/settings/schema.ts +0 -195
- package/src/settings-reader.ts +0 -215
- package/src/settings.ts +0 -452
- package/src/spawn-idempotency.ts +0 -68
- package/src/stateful-agent-view.ts +0 -85
- package/src/stateful-config.ts +0 -13
- package/src/stateful-guidance.ts +0 -29
- package/src/stateful-lifecycle.ts +0 -74
- package/src/stateful-limit-ui.ts +0 -249
- package/src/stateful-limits.ts +0 -96
- package/src/stateful-prompt.ts +0 -49
- package/src/stateful-registration.ts +0 -1320
- package/src/stateful-render.ts +0 -318
- package/src/stateful-safety.ts +0 -47
- package/src/stateful-tool-params.ts +0 -215
- package/src/stateful.ts +0 -12
- package/src/subagent-details.ts +0 -43
- package/src/subagents-extension.ts +0 -372
- package/src/subprocess-transport.ts +0 -145
- package/src/supervision.ts +0 -104
- package/src/task-path.ts +0 -65
- package/src/timeout-checkpoint.ts +0 -305
- package/src/timeout-finalization.ts +0 -75
- package/src/tool-schema-compatibility.ts +0 -73
- package/src/transport-types.ts +0 -74
- package/src/transport-ui.ts +0 -135
- package/src/transport.ts +0 -43
- package/src/turn-budget.ts +0 -109
- package/src/usage-format.ts +0 -42
- package/src/usage-recording-config.ts +0 -13
- package/src/usage-recording-store.ts +0 -183
- package/src/usage-recording.ts +0 -478
- package/src/verification-harness.ts +0 -516
- package/src/verification-policy.ts +0 -67
- package/src/verification-receipt.ts +0 -275
- package/src/verified-execution-benchmark.ts +0 -86
- package/src/verified-execution-contract.ts +0 -191
- package/src/verified-execution-schema.ts +0 -32
- package/src/work-item-ledger.ts +0 -1404
- package/src/work-item-persistence.ts +0 -254
- package/src/workflow-completion-controller.ts +0 -397
- package/src/workflow-planning.ts +0 -172
- package/src/workflow-tree-identity.ts +0 -289
- package/src/workflow-ui.ts +0 -69
- package/src/workflow-verification.ts +0 -296
- package/src/workspace.ts +0 -174
|
@@ -1,737 +0,0 @@
|
|
|
1
|
-
// @generated by scripts/build-runtime.mjs; do not edit.
|
|
2
|
-
// @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader.
|
|
3
|
-
import {
|
|
4
|
-
CHILD_PEER_TOOL_NAMES,
|
|
5
|
-
createInProcessPeerExtension
|
|
6
|
-
} from "./chunk-RY2AEGMZ.ts";
|
|
7
|
-
import {
|
|
8
|
-
resolvePiPromptResources
|
|
9
|
-
} from "./chunk-G6VZTCFH.ts";
|
|
10
|
-
import {
|
|
11
|
-
buildTimeoutFinalizationPrompt,
|
|
12
|
-
resolveTimeoutFinalizationMs
|
|
13
|
-
} from "./chunk-NTRPLF46.ts";
|
|
14
|
-
import {
|
|
15
|
-
TURN_TERMINATION_VERSION,
|
|
16
|
-
TimeoutProgressJournal,
|
|
17
|
-
formatTimeoutCheckpoint,
|
|
18
|
-
formatTurnTerminationMessage,
|
|
19
|
-
journalMessages
|
|
20
|
-
} from "./chunk-RTCVYIZA.ts";
|
|
21
|
-
import {
|
|
22
|
-
TurnBudgetMonitor
|
|
23
|
-
} from "./chunk-ONDTY4EL.ts";
|
|
24
|
-
import {
|
|
25
|
-
formatPeerMessage
|
|
26
|
-
} from "./chunk-5DGRMKNV.ts";
|
|
27
|
-
import {
|
|
28
|
-
discoverAgents
|
|
29
|
-
} from "./chunk-LASD73CM.ts";
|
|
30
|
-
import {
|
|
31
|
-
appendResultInstruction
|
|
32
|
-
} from "./chunk-RSUXZD6S.ts";
|
|
33
|
-
import {
|
|
34
|
-
readSubagentSettings
|
|
35
|
-
} from "./chunk-TMZRHIIK.ts";
|
|
36
|
-
import {
|
|
37
|
-
safeTerminalLine
|
|
38
|
-
} from "./chunk-SWGQLFSD.ts";
|
|
39
|
-
import {
|
|
40
|
-
appendDelegationContract
|
|
41
|
-
} from "./chunk-JSZIP73U.ts";
|
|
42
|
-
import {
|
|
43
|
-
DEFAULT_MAX_CONTEXT_BYTES,
|
|
44
|
-
DEFAULT_MAX_OUTPUT_BYTES,
|
|
45
|
-
redactPrivateText,
|
|
46
|
-
truncateUtf8
|
|
47
|
-
} from "./chunk-H3FP6DLR.ts";
|
|
48
|
-
import {
|
|
49
|
-
resolveDefaultSubagentTimeoutMs
|
|
50
|
-
} from "./chunk-RRR66UWR.ts";
|
|
51
|
-
|
|
52
|
-
// src/in-process-transport.ts
|
|
53
|
-
import {
|
|
54
|
-
getAgentDir,
|
|
55
|
-
SessionManager,
|
|
56
|
-
SettingsManager
|
|
57
|
-
} from "@earendil-works/pi-coding-agent";
|
|
58
|
-
var BUILT_IN_TOOL_NAMES = /* @__PURE__ */ new Set(["read", "bash", "edit", "write", "grep", "find", "ls"]);
|
|
59
|
-
var DEFAULT_ABORT_GRACE_MS = 5e3;
|
|
60
|
-
var InProcessTransport = class {
|
|
61
|
-
constructor(options) {
|
|
62
|
-
this.options = options;
|
|
63
|
-
this.createSession = options.createSession ?? createSdkChildSession;
|
|
64
|
-
this.discoverAgent = options.discoverAgent ?? ((agent) => discoverAgents(agent.cwd, agent.agentScope ?? "user", readSubagentSettings()).agents.find(
|
|
65
|
-
(candidate) => candidate.name === agent.agent
|
|
66
|
-
));
|
|
67
|
-
this.defaultTimeoutMs = options.defaultTimeoutMs ?? resolveDefaultSubagentTimeoutMs();
|
|
68
|
-
this.abortGraceMs = options.abortGraceMs ?? DEFAULT_ABORT_GRACE_MS;
|
|
69
|
-
}
|
|
70
|
-
options;
|
|
71
|
-
kind = "in-process";
|
|
72
|
-
sessions = /* @__PURE__ */ new Map();
|
|
73
|
-
createSession;
|
|
74
|
-
discoverAgent;
|
|
75
|
-
defaultTimeoutMs;
|
|
76
|
-
abortGraceMs;
|
|
77
|
-
async runTurn(agent, task, signal, onProgress) {
|
|
78
|
-
const startedAt = Date.now();
|
|
79
|
-
let telemetry = {
|
|
80
|
-
transport: "in-process",
|
|
81
|
-
phase: "starting",
|
|
82
|
-
updatedAt: startedAt,
|
|
83
|
-
timing: { startedAt, transportStartedAt: startedAt }
|
|
84
|
-
};
|
|
85
|
-
const publish = (patch) => {
|
|
86
|
-
telemetry = {
|
|
87
|
-
...telemetry,
|
|
88
|
-
...patch,
|
|
89
|
-
timing: { ...telemetry.timing, ...patch.timing },
|
|
90
|
-
updatedAt: Date.now()
|
|
91
|
-
};
|
|
92
|
-
onProgress?.({ ...telemetry, timing: { ...telemetry.timing } });
|
|
93
|
-
};
|
|
94
|
-
publish({});
|
|
95
|
-
if (signal.aborted) {
|
|
96
|
-
publish({ phase: "interrupted", failurePhase: "starting" });
|
|
97
|
-
return { ...interruptedOutcome(""), telemetry };
|
|
98
|
-
}
|
|
99
|
-
const agentConfig = this.discoverAgent(agent);
|
|
100
|
-
if (!agentConfig) {
|
|
101
|
-
publish({ phase: "failed", failurePhase: "starting" });
|
|
102
|
-
return {
|
|
103
|
-
output: "",
|
|
104
|
-
exitCode: 1,
|
|
105
|
-
error: `Unknown subagent: ${agent.agent}`,
|
|
106
|
-
telemetry
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
let tools;
|
|
110
|
-
try {
|
|
111
|
-
tools = validateInProcessTools(agent.executionPlan?.effectiveTools ?? agentConfig.tools);
|
|
112
|
-
} catch (error) {
|
|
113
|
-
publish({ phase: "failed", failurePhase: "starting" });
|
|
114
|
-
return { output: "", exitCode: 1, error: errorMessage(error), telemetry };
|
|
115
|
-
}
|
|
116
|
-
let record;
|
|
117
|
-
try {
|
|
118
|
-
record = await this.getOrCreate(agent, agentConfig, tools);
|
|
119
|
-
} catch (error) {
|
|
120
|
-
publish({ phase: "failed", failurePhase: "starting" });
|
|
121
|
-
return { output: "", exitCode: 1, error: errorMessage(error), telemetry };
|
|
122
|
-
}
|
|
123
|
-
publish({
|
|
124
|
-
phase: "ready",
|
|
125
|
-
provider: record.session.provider,
|
|
126
|
-
model: record.session.model,
|
|
127
|
-
thinkingLevel: record.session.thinkingLevel,
|
|
128
|
-
timing: { readyAt: Date.now() }
|
|
129
|
-
});
|
|
130
|
-
if (signal.aborted) {
|
|
131
|
-
await this.releaseById(agent.id).catch(() => void 0);
|
|
132
|
-
publish({ phase: "interrupted", failurePhase: "ready" });
|
|
133
|
-
return { ...interruptedOutcome(""), telemetry };
|
|
134
|
-
}
|
|
135
|
-
const prompt = buildCurrentTurnPrompt(agent, task);
|
|
136
|
-
const timeoutMs = agent.currentTimeoutMs ?? agent.timeoutMs ?? agentConfig.timeoutMs ?? this.defaultTimeoutMs;
|
|
137
|
-
const startingMessageCount = record.session.messages.length;
|
|
138
|
-
record.lastOutput = "";
|
|
139
|
-
publish({ phase: "running", timing: { promptAcceptedAt: Date.now() } });
|
|
140
|
-
const settlement = await this.runPrompt(record, prompt, signal, timeoutMs, {
|
|
141
|
-
idleTimeoutMs: agent.currentIdleTimeoutMs ?? agent.idleTimeoutMs,
|
|
142
|
-
maxTurns: agent.currentMaxTurns ?? agent.maxTurns,
|
|
143
|
-
maxToolCalls: agent.currentMaxToolCalls ?? agent.maxToolCalls
|
|
144
|
-
});
|
|
145
|
-
const final = latestAssistant(record.session.messages.slice(startingMessageCount));
|
|
146
|
-
const output = truncateUtf8(final.output || record.lastOutput, DEFAULT_MAX_OUTPUT_BYTES);
|
|
147
|
-
const truncated = output.truncated || agent.contextTruncated;
|
|
148
|
-
const policy = inProcessPolicy(agentConfig, agent);
|
|
149
|
-
const settledAt = Date.now();
|
|
150
|
-
if (signal.aborted && (settlement.kind === "timeout" || settlement.kind === "limit")) {
|
|
151
|
-
publish({ phase: "interrupted", failurePhase: "running", timing: { settledAt } });
|
|
152
|
-
return {
|
|
153
|
-
...interruptedOutcome(output.text),
|
|
154
|
-
truncated,
|
|
155
|
-
policy,
|
|
156
|
-
telemetry
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
switch (settlement.kind) {
|
|
160
|
-
case "completed":
|
|
161
|
-
publish({
|
|
162
|
-
phase: final.stopReason === "error" ? "failed" : "settled",
|
|
163
|
-
timing: { settledAt }
|
|
164
|
-
});
|
|
165
|
-
if (final.stopReason === "error") {
|
|
166
|
-
return {
|
|
167
|
-
output: output.text,
|
|
168
|
-
exitCode: 1,
|
|
169
|
-
truncated,
|
|
170
|
-
error: final.error || "In-process subagent returned an error",
|
|
171
|
-
policy,
|
|
172
|
-
telemetry
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
if (final.stopReason === "aborted") {
|
|
176
|
-
publish({ phase: "interrupted", failurePhase: "running" });
|
|
177
|
-
return {
|
|
178
|
-
...interruptedOutcome(output.text),
|
|
179
|
-
truncated,
|
|
180
|
-
policy,
|
|
181
|
-
telemetry
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
return {
|
|
185
|
-
output: output.text,
|
|
186
|
-
exitCode: 0,
|
|
187
|
-
truncated,
|
|
188
|
-
policy,
|
|
189
|
-
telemetry
|
|
190
|
-
};
|
|
191
|
-
case "failed":
|
|
192
|
-
publish({ phase: "failed", failurePhase: "running", timing: { settledAt } });
|
|
193
|
-
return {
|
|
194
|
-
output: output.text,
|
|
195
|
-
exitCode: 1,
|
|
196
|
-
truncated,
|
|
197
|
-
error: errorMessage(settlement.error),
|
|
198
|
-
policy,
|
|
199
|
-
telemetry
|
|
200
|
-
};
|
|
201
|
-
case "timeout":
|
|
202
|
-
case "limit": {
|
|
203
|
-
const stop = settlement.kind === "limit" ? settlement.stop : { reason: "work_timeout", limit: timeoutMs };
|
|
204
|
-
const journal = new TimeoutProgressJournal();
|
|
205
|
-
journalMessages(journal, record.session.messages.slice(startingMessageCount));
|
|
206
|
-
const termination = {
|
|
207
|
-
version: TURN_TERMINATION_VERSION,
|
|
208
|
-
reason: stop.reason,
|
|
209
|
-
limit: stop.limit,
|
|
210
|
-
checkpoint: journal.checkpoint(task, output.text),
|
|
211
|
-
finalization: { attempted: false, status: "skipped", durationMs: 0 }
|
|
212
|
-
};
|
|
213
|
-
let finalizedOutput = output.text || formatTimeoutCheckpoint(termination.checkpoint);
|
|
214
|
-
let finalizationError;
|
|
215
|
-
if (!signal.aborted && this.sessions.get(agent.id) === record) {
|
|
216
|
-
publish({ phase: "finalizing", failurePhase: "running" });
|
|
217
|
-
const summaryStart = record.session.messages.length;
|
|
218
|
-
const finalizationStartedAt = Date.now();
|
|
219
|
-
record.lastOutput = "";
|
|
220
|
-
const summarySettlement = await this.runPrompt(
|
|
221
|
-
record,
|
|
222
|
-
buildTimeoutFinalizationPrompt({
|
|
223
|
-
task,
|
|
224
|
-
partialOutput: output.text,
|
|
225
|
-
checkpoint: termination.checkpoint,
|
|
226
|
-
terminationReason: stop.reason,
|
|
227
|
-
resultFormat: agent.resultFormat
|
|
228
|
-
}),
|
|
229
|
-
signal,
|
|
230
|
-
resolveTimeoutFinalizationMs(timeoutMs, this.options.timeoutFinalizationMs)
|
|
231
|
-
);
|
|
232
|
-
const summary = latestAssistant(record.session.messages.slice(summaryStart));
|
|
233
|
-
const boundedSummary = truncateUtf8(
|
|
234
|
-
summary.output || record.lastOutput,
|
|
235
|
-
DEFAULT_MAX_OUTPUT_BYTES
|
|
236
|
-
);
|
|
237
|
-
if (summarySettlement.kind === "completed" && summary.stopReason !== "error" && boundedSummary.text.trim()) {
|
|
238
|
-
finalizedOutput = boundedSummary.text;
|
|
239
|
-
termination.finalization = {
|
|
240
|
-
attempted: true,
|
|
241
|
-
status: "completed",
|
|
242
|
-
durationMs: Date.now() - finalizationStartedAt
|
|
243
|
-
};
|
|
244
|
-
} else {
|
|
245
|
-
finalizationError = summarySettlement.kind === "failed" ? errorMessage(summarySettlement.error) : `timeout summary ${summarySettlement.kind}`;
|
|
246
|
-
termination.finalization = {
|
|
247
|
-
attempted: true,
|
|
248
|
-
status: summarySettlement.kind === "timeout" ? "timed_out" : "failed",
|
|
249
|
-
durationMs: Date.now() - finalizationStartedAt,
|
|
250
|
-
error: finalizationError
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
publish({ phase: "failed", failurePhase: "running", timing: { settledAt: Date.now() } });
|
|
255
|
-
return {
|
|
256
|
-
output: finalizedOutput,
|
|
257
|
-
exitCode: 124,
|
|
258
|
-
truncated,
|
|
259
|
-
error: [
|
|
260
|
-
formatTurnTerminationMessage(stop.reason, stop.limit, "In-process subagent"),
|
|
261
|
-
finalizationError
|
|
262
|
-
].filter(Boolean).join("; "),
|
|
263
|
-
policy,
|
|
264
|
-
termination,
|
|
265
|
-
telemetry
|
|
266
|
-
};
|
|
267
|
-
}
|
|
268
|
-
case "aborted":
|
|
269
|
-
publish({ phase: "interrupted", failurePhase: "running", timing: { settledAt } });
|
|
270
|
-
return {
|
|
271
|
-
...interruptedOutcome(output.text),
|
|
272
|
-
truncated,
|
|
273
|
-
policy,
|
|
274
|
-
telemetry
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
async deliverMessage(agent, message) {
|
|
279
|
-
const record = this.sessions.get(agent.id);
|
|
280
|
-
if (!record || record.disposed || !record.session.steer) return false;
|
|
281
|
-
await record.session.steer(formatPeerMessage(message));
|
|
282
|
-
return true;
|
|
283
|
-
}
|
|
284
|
-
async release(agent) {
|
|
285
|
-
await this.releaseById(agent.id);
|
|
286
|
-
}
|
|
287
|
-
async shutdown() {
|
|
288
|
-
const agentIds = [...this.sessions.keys()];
|
|
289
|
-
const results = await Promise.allSettled(agentIds.map((agentId) => this.releaseById(agentId)));
|
|
290
|
-
const failures = results.flatMap(
|
|
291
|
-
(result) => result.status === "rejected" ? [result.reason] : []
|
|
292
|
-
);
|
|
293
|
-
if (failures.length > 0) {
|
|
294
|
-
throw new AggregateError(
|
|
295
|
-
failures,
|
|
296
|
-
`Failed to dispose ${failures.length} in-process subagent session(s)`
|
|
297
|
-
);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
async releaseById(agentId) {
|
|
301
|
-
const record = this.sessions.get(agentId);
|
|
302
|
-
if (!record) return;
|
|
303
|
-
this.sessions.delete(agentId);
|
|
304
|
-
this.options.peerRuntime?.revoke(agentId);
|
|
305
|
-
if (record.disposed) return;
|
|
306
|
-
record.disposed = true;
|
|
307
|
-
const failures = [];
|
|
308
|
-
try {
|
|
309
|
-
record.unsubscribe();
|
|
310
|
-
} catch (error) {
|
|
311
|
-
failures.push(error);
|
|
312
|
-
}
|
|
313
|
-
if (record.session.messages.length > 0) {
|
|
314
|
-
await settleWithin(record.session.abort(), this.abortGraceMs);
|
|
315
|
-
}
|
|
316
|
-
try {
|
|
317
|
-
record.session.dispose();
|
|
318
|
-
} catch (error) {
|
|
319
|
-
failures.push(error);
|
|
320
|
-
}
|
|
321
|
-
if (failures.length > 0) {
|
|
322
|
-
throw new AggregateError(
|
|
323
|
-
failures,
|
|
324
|
-
`Failed to release in-process subagent ${agentId}: ${failures.map(errorMessage).join("; ")}`
|
|
325
|
-
);
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
async getOrCreate(agent, agentConfig, tools) {
|
|
329
|
-
const existing = this.sessions.get(agent.id);
|
|
330
|
-
if (existing) return existing;
|
|
331
|
-
const session = await this.createSession({
|
|
332
|
-
agent,
|
|
333
|
-
agentConfig,
|
|
334
|
-
context: agent.context,
|
|
335
|
-
history: agent.history.map((turn) => ({ ...turn })),
|
|
336
|
-
modelRegistry: this.options.modelRegistry,
|
|
337
|
-
parentRuntime: this.options.getParentRuntime(),
|
|
338
|
-
tools,
|
|
339
|
-
peerRuntime: this.options.peerRuntime
|
|
340
|
-
});
|
|
341
|
-
const record = {
|
|
342
|
-
session,
|
|
343
|
-
lastOutput: "",
|
|
344
|
-
disposed: false,
|
|
345
|
-
unsubscribe: () => void 0
|
|
346
|
-
};
|
|
347
|
-
try {
|
|
348
|
-
record.unsubscribe = session.subscribe((event) => {
|
|
349
|
-
const message = eventMessage(event);
|
|
350
|
-
if (!message) return;
|
|
351
|
-
const output = assistantText(message);
|
|
352
|
-
if (output) record.lastOutput = truncateUtf8(output, DEFAULT_MAX_OUTPUT_BYTES).text;
|
|
353
|
-
});
|
|
354
|
-
} catch (error) {
|
|
355
|
-
record.disposed = true;
|
|
356
|
-
try {
|
|
357
|
-
session.dispose();
|
|
358
|
-
} catch {
|
|
359
|
-
}
|
|
360
|
-
throw error;
|
|
361
|
-
}
|
|
362
|
-
this.sessions.set(agent.id, record);
|
|
363
|
-
return record;
|
|
364
|
-
}
|
|
365
|
-
async runPrompt(record, prompt, signal, timeoutMs, limits = {}) {
|
|
366
|
-
if (signal.aborted) return { kind: "aborted" };
|
|
367
|
-
let timeout;
|
|
368
|
-
let abortHandler;
|
|
369
|
-
let resolveLimit;
|
|
370
|
-
const limitSettlement = new Promise((resolve) => {
|
|
371
|
-
resolveLimit = resolve;
|
|
372
|
-
});
|
|
373
|
-
const monitor = new TurnBudgetMonitor({
|
|
374
|
-
...limits,
|
|
375
|
-
onExceeded: (stop) => resolveLimit({ kind: "limit", stop })
|
|
376
|
-
});
|
|
377
|
-
const unsubscribeBudget = record.session.subscribe((event) => {
|
|
378
|
-
const type = childEventType(event);
|
|
379
|
-
if (type === "tool_execution_end") monitor.recordActivity();
|
|
380
|
-
if (type !== "message_end") return;
|
|
381
|
-
const message = eventMessage(event);
|
|
382
|
-
if (!message || typeof message !== "object" || Array.isArray(message)) return;
|
|
383
|
-
const value = message;
|
|
384
|
-
if (value.role === "toolResult") {
|
|
385
|
-
monitor.recordActivity();
|
|
386
|
-
return;
|
|
387
|
-
}
|
|
388
|
-
if (value.role !== "assistant") return;
|
|
389
|
-
monitor.recordToolCalls(assistantToolCallCount(value.content));
|
|
390
|
-
monitor.recordAssistantTurn(
|
|
391
|
-
typeof value.stopReason === "string" ? value.stopReason : void 0
|
|
392
|
-
);
|
|
393
|
-
});
|
|
394
|
-
const promptSettlement = Promise.resolve().then(() => {
|
|
395
|
-
if (signal.aborted) throw new Error("In-process subagent prompt aborted before start");
|
|
396
|
-
return record.session.prompt(prompt);
|
|
397
|
-
}).then(() => ({ kind: "completed" })).catch((error) => ({ kind: "failed", error }));
|
|
398
|
-
const timeoutSettlement = new Promise((resolve) => {
|
|
399
|
-
timeout = setTimeout(() => resolve({ kind: "timeout" }), timeoutMs);
|
|
400
|
-
});
|
|
401
|
-
const abortSettlement = new Promise((resolve) => {
|
|
402
|
-
abortHandler = () => resolve({ kind: "aborted" });
|
|
403
|
-
signal.addEventListener("abort", abortHandler, { once: true });
|
|
404
|
-
if (signal.aborted) abortHandler();
|
|
405
|
-
});
|
|
406
|
-
const settlement = await Promise.race([
|
|
407
|
-
promptSettlement,
|
|
408
|
-
timeoutSettlement,
|
|
409
|
-
abortSettlement,
|
|
410
|
-
limitSettlement
|
|
411
|
-
]);
|
|
412
|
-
if (timeout) clearTimeout(timeout);
|
|
413
|
-
if (abortHandler) signal.removeEventListener("abort", abortHandler);
|
|
414
|
-
try {
|
|
415
|
-
unsubscribeBudget();
|
|
416
|
-
} catch {
|
|
417
|
-
}
|
|
418
|
-
monitor.dispose();
|
|
419
|
-
if (settlement.kind === "timeout" || settlement.kind === "limit" || settlement.kind === "aborted") {
|
|
420
|
-
const [, settledAfterAbort] = await Promise.all([
|
|
421
|
-
settleWithin(record.session.abort(), this.abortGraceMs),
|
|
422
|
-
completesWithin(promptSettlement, this.abortGraceMs)
|
|
423
|
-
]);
|
|
424
|
-
if (!settledAfterAbort) this.discardRecord(record);
|
|
425
|
-
}
|
|
426
|
-
return settlement;
|
|
427
|
-
}
|
|
428
|
-
discardRecord(record) {
|
|
429
|
-
for (const [agentId, candidate] of this.sessions) {
|
|
430
|
-
if (candidate === record) this.sessions.delete(agentId);
|
|
431
|
-
}
|
|
432
|
-
if (record.disposed) return;
|
|
433
|
-
record.disposed = true;
|
|
434
|
-
record.unsubscribe();
|
|
435
|
-
record.session.dispose();
|
|
436
|
-
}
|
|
437
|
-
};
|
|
438
|
-
function validateInProcessTools(tools) {
|
|
439
|
-
if (tools === void 0) return void 0;
|
|
440
|
-
const unique = [...new Set(tools)];
|
|
441
|
-
const unsupported = unique.filter((tool) => !BUILT_IN_TOOL_NAMES.has(tool));
|
|
442
|
-
if (unsupported.length > 0) {
|
|
443
|
-
throw new Error(
|
|
444
|
-
`In-process subagents cannot load extension/custom tools: ${unsupported.map((tool) => safeTerminalLine(tool, 256)).join(", ")}. Use stateful.transport "subprocess" for this agent.`
|
|
445
|
-
);
|
|
446
|
-
}
|
|
447
|
-
return unique;
|
|
448
|
-
}
|
|
449
|
-
async function createSdkChildSession(options) {
|
|
450
|
-
const agentDir = getAgentDir();
|
|
451
|
-
const projectTrusted = options.agent.target?.trust.projectTrusted ?? (options.agent.agentScope === "project" || options.agent.agentScope === "both");
|
|
452
|
-
const { services, support: coreSupport } = await prepareInProcessServices(
|
|
453
|
-
options.agent.cwd,
|
|
454
|
-
agentDir,
|
|
455
|
-
options.agentConfig.systemPrompt,
|
|
456
|
-
projectTrusted,
|
|
457
|
-
options.peerRuntime ? createInProcessPeerExtension(options.peerRuntime, options.agent.id) : void 0
|
|
458
|
-
);
|
|
459
|
-
copyRegisteredProviders(
|
|
460
|
-
options.modelRegistry,
|
|
461
|
-
services.modelRuntime
|
|
462
|
-
);
|
|
463
|
-
const modelSupport = {
|
|
464
|
-
modelRuntime: services.modelRuntime,
|
|
465
|
-
resolveCliModel: coreSupport.resolveCliModel
|
|
466
|
-
};
|
|
467
|
-
const resolved = await resolveChildModel(options, modelSupport);
|
|
468
|
-
await transferChildModelAuth(options.modelRegistry, resolved.model, services.modelRuntime);
|
|
469
|
-
const model = resolved.model;
|
|
470
|
-
const sessionManager = SessionManager.inMemory(options.agent.cwd);
|
|
471
|
-
seedChildSessionManager(sessionManager, options, model);
|
|
472
|
-
const selectedTools = options.tools === void 0 ? void 0 : options.peerRuntime ? [...options.tools, ...CHILD_PEER_TOOL_NAMES] : options.tools;
|
|
473
|
-
const created = await coreSupport.createAgentSessionFromServices({
|
|
474
|
-
services,
|
|
475
|
-
sessionManager,
|
|
476
|
-
model,
|
|
477
|
-
thinkingLevel: resolved.thinkingLevel,
|
|
478
|
-
tools: selectedTools,
|
|
479
|
-
noTools: selectedTools?.length === 0 ? "all" : void 0
|
|
480
|
-
});
|
|
481
|
-
const session = created.session;
|
|
482
|
-
if (selectedTools !== void 0) {
|
|
483
|
-
const active = session.getActiveToolNames();
|
|
484
|
-
const expected = [...selectedTools].sort();
|
|
485
|
-
if (active.length !== expected.length || [...active].sort().some((name, index) => name !== expected[index])) {
|
|
486
|
-
session.dispose();
|
|
487
|
-
throw new Error(
|
|
488
|
-
`In-process child activated an unexpected tool set (${active.join(", ") || "none"}); use stateful.transport "subprocess".`
|
|
489
|
-
);
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
return {
|
|
493
|
-
get sessionId() {
|
|
494
|
-
return session.sessionId;
|
|
495
|
-
},
|
|
496
|
-
get messages() {
|
|
497
|
-
return session.messages;
|
|
498
|
-
},
|
|
499
|
-
get provider() {
|
|
500
|
-
return session.model?.provider;
|
|
501
|
-
},
|
|
502
|
-
get model() {
|
|
503
|
-
return session.model?.id;
|
|
504
|
-
},
|
|
505
|
-
get thinkingLevel() {
|
|
506
|
-
return session.thinkingLevel;
|
|
507
|
-
},
|
|
508
|
-
prompt: (text) => session.prompt(text),
|
|
509
|
-
steer: (text) => session.steer(text),
|
|
510
|
-
subscribe: (listener) => session.subscribe((event) => listener(event)),
|
|
511
|
-
abort: () => session.abort(),
|
|
512
|
-
dispose: () => session.dispose(),
|
|
513
|
-
getActiveToolNames: () => session.getActiveToolNames()
|
|
514
|
-
};
|
|
515
|
-
}
|
|
516
|
-
async function loadCoreSessionSupport() {
|
|
517
|
-
const codingAgentModule = await import("@earendil-works/pi-coding-agent");
|
|
518
|
-
if (!codingAgentModule.createAgentSessionFromServices || !codingAgentModule.createAgentSessionServices || !codingAgentModule.resolveCliModel) {
|
|
519
|
-
return void 0;
|
|
520
|
-
}
|
|
521
|
-
return {
|
|
522
|
-
createAgentSessionFromServices: codingAgentModule.createAgentSessionFromServices,
|
|
523
|
-
createAgentSessionServices: codingAgentModule.createAgentSessionServices,
|
|
524
|
-
resolveCliModel: codingAgentModule.resolveCliModel
|
|
525
|
-
};
|
|
526
|
-
}
|
|
527
|
-
async function transferChildModelAuth(modelRegistry, model, modelRuntime) {
|
|
528
|
-
const auth = await modelRegistry.getApiKeyAndHeaders(model);
|
|
529
|
-
if (auth.ok && auth.apiKey) await modelRuntime.setRuntimeApiKey(model.provider, auth.apiKey);
|
|
530
|
-
}
|
|
531
|
-
function copyRegisteredProviders(registeredProviders, modelRuntime) {
|
|
532
|
-
for (const provider of registeredProviders.getRegisteredProviderIds?.() ?? []) {
|
|
533
|
-
const config = registeredProviders.getRegisteredProviderConfig?.(provider);
|
|
534
|
-
if (config !== void 0) {
|
|
535
|
-
modelRuntime.registerProvider(provider, config);
|
|
536
|
-
continue;
|
|
537
|
-
}
|
|
538
|
-
const nativeProvider = registeredProviders.getRegisteredNativeProvider?.(provider);
|
|
539
|
-
if (nativeProvider) modelRuntime.registerNativeProvider?.(nativeProvider);
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
async function resolveChildModel(options, support) {
|
|
543
|
-
if (!support) throw unsupportedInProcessCoreError();
|
|
544
|
-
let model = options.parentRuntime.model;
|
|
545
|
-
let modelThinkingLevel;
|
|
546
|
-
if (options.agentConfig.model !== void 0) {
|
|
547
|
-
const requested = options.agentConfig.model.trim();
|
|
548
|
-
if (!requested) throw new Error("In-process subagent model cannot be empty");
|
|
549
|
-
const resolved = support.resolveCliModel({
|
|
550
|
-
cliModel: requested,
|
|
551
|
-
modelRuntime: support.modelRuntime
|
|
552
|
-
});
|
|
553
|
-
if (resolved.error) throw new Error(resolved.error);
|
|
554
|
-
if (!resolved.model)
|
|
555
|
-
throw new Error(`Unable to resolve in-process subagent model ${requested}`);
|
|
556
|
-
model = resolved.model;
|
|
557
|
-
modelThinkingLevel = resolved.thinkingLevel;
|
|
558
|
-
}
|
|
559
|
-
if (!model) model = options.modelRegistry.getAvailable()[0];
|
|
560
|
-
if (!model)
|
|
561
|
-
throw new Error("No model with configured authentication is available for in-process subagent");
|
|
562
|
-
model = support.modelRuntime.getModel(model.provider, model.id) ?? model;
|
|
563
|
-
return {
|
|
564
|
-
model,
|
|
565
|
-
thinkingLevel: options.agent.thinkingLevel ?? options.agentConfig.thinkingLevel ?? modelThinkingLevel ?? options.parentRuntime.thinkingLevel
|
|
566
|
-
};
|
|
567
|
-
}
|
|
568
|
-
function unsupportedInProcessCoreError() {
|
|
569
|
-
return new Error(
|
|
570
|
-
'In-process subagents require Pi core createAgentSessionServices, createAgentSessionFromServices, and resolveCliModel support; set stateful.transport to "subprocess".'
|
|
571
|
-
);
|
|
572
|
-
}
|
|
573
|
-
async function prepareInProcessServices(cwd, agentDir, agentSystemPrompt, projectTrusted, peerExtension) {
|
|
574
|
-
const promptResources = await resolvePiPromptResources(cwd, projectTrusted, agentDir);
|
|
575
|
-
const settingsManager = SettingsManager.create(cwd, agentDir, { projectTrusted });
|
|
576
|
-
const support = await loadCoreSessionSupport();
|
|
577
|
-
if (!support) throw unsupportedInProcessCoreError();
|
|
578
|
-
const services = await support.createAgentSessionServices({
|
|
579
|
-
cwd,
|
|
580
|
-
agentDir,
|
|
581
|
-
settingsManager,
|
|
582
|
-
resourceLoaderOptions: {
|
|
583
|
-
noExtensions: true,
|
|
584
|
-
...peerExtension ? { extensionFactories: [peerExtension] } : {},
|
|
585
|
-
appendSystemPrompt: [
|
|
586
|
-
...promptResources.appendSystemPromptPaths,
|
|
587
|
-
...agentSystemPrompt.trim() ? [agentSystemPrompt] : []
|
|
588
|
-
]
|
|
589
|
-
}
|
|
590
|
-
});
|
|
591
|
-
return { services, support };
|
|
592
|
-
}
|
|
593
|
-
async function createInProcessServices(cwd, agentDir, agentSystemPrompt, projectTrusted = false) {
|
|
594
|
-
return (await prepareInProcessServices(cwd, agentDir, agentSystemPrompt, projectTrusted)).services;
|
|
595
|
-
}
|
|
596
|
-
function seedChildSessionManager(sessionManager, options, model) {
|
|
597
|
-
let timestamp = Date.now() - (options.history.length * 2 + 1);
|
|
598
|
-
if (options.context?.trim()) {
|
|
599
|
-
sessionManager.appendMessage({
|
|
600
|
-
role: "user",
|
|
601
|
-
content: `Parent context:
|
|
602
|
-
${redactPrivateText(options.context)}`,
|
|
603
|
-
timestamp: timestamp++
|
|
604
|
-
});
|
|
605
|
-
}
|
|
606
|
-
for (const turn of options.history) {
|
|
607
|
-
sessionManager.appendMessage({
|
|
608
|
-
role: "user",
|
|
609
|
-
content: redactPrivateText(turn.task),
|
|
610
|
-
timestamp: timestamp++
|
|
611
|
-
});
|
|
612
|
-
sessionManager.appendMessage({
|
|
613
|
-
role: "assistant",
|
|
614
|
-
content: [{ type: "text", text: redactPrivateText(turn.output) }],
|
|
615
|
-
api: model.api,
|
|
616
|
-
provider: model.provider,
|
|
617
|
-
model: model.id,
|
|
618
|
-
usage: {
|
|
619
|
-
input: 0,
|
|
620
|
-
output: 0,
|
|
621
|
-
cacheRead: 0,
|
|
622
|
-
cacheWrite: 0,
|
|
623
|
-
totalTokens: 0,
|
|
624
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
|
|
625
|
-
},
|
|
626
|
-
stopReason: turn.exitCode === 0 ? "stop" : "error",
|
|
627
|
-
timestamp: timestamp++
|
|
628
|
-
});
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
function buildCurrentTurnPrompt(agent, task) {
|
|
632
|
-
const ids = new Set(agent.currentMailboxMessageIds ?? []);
|
|
633
|
-
const messages = agent.mailbox.filter((message) => ids.has(message.id)).slice(-20).map(
|
|
634
|
-
(message) => formatPeerMessage({ ...message, content: redactPrivateText(message.content) })
|
|
635
|
-
).join("\n\n");
|
|
636
|
-
const base = messages ? `${redactPrivateText(task)}
|
|
637
|
-
|
|
638
|
-
Mailbox messages:
|
|
639
|
-
${messages}` : redactPrivateText(task);
|
|
640
|
-
const contracted = appendDelegationContract(base, agent.contract, DEFAULT_MAX_CONTEXT_BYTES);
|
|
641
|
-
return truncateUtf8(
|
|
642
|
-
appendResultInstruction(contracted.text, agent.resultFormat),
|
|
643
|
-
DEFAULT_MAX_CONTEXT_BYTES
|
|
644
|
-
).text;
|
|
645
|
-
}
|
|
646
|
-
function latestAssistant(messages) {
|
|
647
|
-
for (let index = messages.length - 1; index >= 0; index--) {
|
|
648
|
-
const message = messages[index];
|
|
649
|
-
if (!message || typeof message !== "object") continue;
|
|
650
|
-
const candidate = message;
|
|
651
|
-
if (candidate.role !== "assistant") continue;
|
|
652
|
-
return {
|
|
653
|
-
output: assistantText(message),
|
|
654
|
-
stopReason: candidate.stopReason,
|
|
655
|
-
error: candidate.errorMessage
|
|
656
|
-
};
|
|
657
|
-
}
|
|
658
|
-
return { output: "" };
|
|
659
|
-
}
|
|
660
|
-
function childEventType(event) {
|
|
661
|
-
if (!event || typeof event !== "object" || Array.isArray(event)) return void 0;
|
|
662
|
-
const type = event.type;
|
|
663
|
-
return typeof type === "string" ? type : void 0;
|
|
664
|
-
}
|
|
665
|
-
function assistantToolCallCount(content) {
|
|
666
|
-
if (!Array.isArray(content)) return 0;
|
|
667
|
-
return content.filter(
|
|
668
|
-
(part) => part && typeof part === "object" && !Array.isArray(part) && part.type === "toolCall"
|
|
669
|
-
).length;
|
|
670
|
-
}
|
|
671
|
-
function eventMessage(event) {
|
|
672
|
-
if (!event || typeof event !== "object") return void 0;
|
|
673
|
-
const candidate = event;
|
|
674
|
-
return candidate.type === "message_update" || candidate.type === "message_end" ? candidate.message : void 0;
|
|
675
|
-
}
|
|
676
|
-
function assistantText(message) {
|
|
677
|
-
if (!message || typeof message !== "object") return "";
|
|
678
|
-
const content = message.content;
|
|
679
|
-
if (typeof content === "string") return content;
|
|
680
|
-
if (!Array.isArray(content)) return "";
|
|
681
|
-
return content.filter(
|
|
682
|
-
(part) => Boolean(
|
|
683
|
-
part && typeof part === "object" && part.type === "text" && typeof part.text === "string"
|
|
684
|
-
)
|
|
685
|
-
).map((part) => part.text).join("\n");
|
|
686
|
-
}
|
|
687
|
-
function interruptedOutcome(output) {
|
|
688
|
-
return { output, exitCode: 130, aborted: true, error: "In-process subagent was aborted" };
|
|
689
|
-
}
|
|
690
|
-
function inProcessPolicy(agentConfig, managedAgent) {
|
|
691
|
-
return {
|
|
692
|
-
inherited: ["modelRegistry", "authentication", "cwdResources"],
|
|
693
|
-
overridden: [
|
|
694
|
-
...agentConfig.model ? ["model"] : [],
|
|
695
|
-
...managedAgent.thinkingLevel || agentConfig.thinkingLevel ? ["thinkingLevel"] : [],
|
|
696
|
-
...agentConfig.tools ? ["tools"] : []
|
|
697
|
-
],
|
|
698
|
-
unsupported: ["approvalPolicy", "sandboxProfile", "providerHeaders", "extensionState"]
|
|
699
|
-
};
|
|
700
|
-
}
|
|
701
|
-
async function settleWithin(promise, timeoutMs) {
|
|
702
|
-
await completesWithin(
|
|
703
|
-
promise.catch(() => void 0),
|
|
704
|
-
timeoutMs
|
|
705
|
-
);
|
|
706
|
-
}
|
|
707
|
-
async function completesWithin(promise, timeoutMs) {
|
|
708
|
-
let timeout;
|
|
709
|
-
try {
|
|
710
|
-
return await Promise.race([
|
|
711
|
-
promise.then(() => true),
|
|
712
|
-
new Promise((resolve) => {
|
|
713
|
-
timeout = setTimeout(() => resolve(false), timeoutMs);
|
|
714
|
-
})
|
|
715
|
-
]);
|
|
716
|
-
} finally {
|
|
717
|
-
if (timeout) clearTimeout(timeout);
|
|
718
|
-
}
|
|
719
|
-
}
|
|
720
|
-
function errorMessage(error) {
|
|
721
|
-
return truncateUtf8(
|
|
722
|
-
error instanceof Error ? error.message : String(error),
|
|
723
|
-
DEFAULT_MAX_OUTPUT_BYTES
|
|
724
|
-
).text;
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
export {
|
|
728
|
-
InProcessTransport,
|
|
729
|
-
validateInProcessTools,
|
|
730
|
-
createSdkChildSession,
|
|
731
|
-
copyRegisteredProviders,
|
|
732
|
-
resolveChildModel,
|
|
733
|
-
createInProcessServices,
|
|
734
|
-
seedChildSessionManager,
|
|
735
|
-
buildCurrentTurnPrompt
|
|
736
|
-
};
|
|
737
|
-
//# sourceMappingURL=chunk-LL4LP2T7.ts.map
|