@myagentroam/agent 0.9.141 → 0.9.142
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/agent-core/host/contracts.d.ts +64 -0
- package/dist/{host → agent-core/host}/local-agent-host.d.ts +6 -5
- package/dist/{host → agent-core/host}/local-agent-host.js +4 -4
- package/dist/{model → agent-core/model}/contracts.d.ts +2 -16
- package/dist/agent-core/model/recovery.d.ts +4 -0
- package/dist/agent-core/model/recovery.js +71 -0
- package/dist/{prompts → agent-core/prompts}/core.d.ts +3 -13
- package/dist/{prompts → agent-core/prompts}/index.d.ts +1 -1
- package/dist/{prompts → agent-core/prompts}/modes.d.ts +1 -1
- package/dist/{prompts → agent-core/prompts}/resources.d.ts +1 -1
- package/dist/{runtime/compact.d.ts → agent-core/runtime/compaction/history.d.ts} +2 -23
- package/dist/{runtime/compact.js → agent-core/runtime/compaction/history.js} +2 -92
- package/dist/agent-core/runtime/compaction/operation.d.ts +22 -0
- package/dist/agent-core/runtime/compaction/operation.js +91 -0
- package/dist/agent-core/runtime/context/contracts.d.ts +12 -0
- package/dist/{runtime/environment-context.d.ts → agent-core/runtime/context/environment.d.ts} +2 -2
- package/dist/{runtime/environment-context.js → agent-core/runtime/context/environment.js} +1 -1
- package/dist/{runtime/context-gc-runtime.d.ts → agent-core/runtime/context/gc-runtime.d.ts} +4 -4
- package/dist/{runtime/context-gc-runtime.js → agent-core/runtime/context/gc-runtime.js} +3 -3
- package/dist/{runtime/context-gc-tokens.d.ts → agent-core/runtime/context/gc-tokens.d.ts} +2 -2
- package/dist/{runtime/context-gc-tokens.js → agent-core/runtime/context/gc-tokens.js} +3 -3
- package/dist/{runtime/context-gc.d.ts → agent-core/runtime/context/gc.d.ts} +3 -3
- package/dist/{runtime/context-gc.js → agent-core/runtime/context/gc.js} +6 -6
- package/dist/{runtime → agent-core/runtime/context}/instructions.d.ts +1 -1
- package/dist/{runtime → agent-core/runtime/context}/instructions.js +1 -1
- package/dist/{runtime → agent-core/runtime/context}/normalize.d.ts +1 -1
- package/dist/{runtime/context-projection.d.ts → agent-core/runtime/context/projection.d.ts} +3 -3
- package/dist/{runtime/context-projection.js → agent-core/runtime/context/projection.js} +4 -4
- package/dist/agent-core/runtime/context/request-budget.d.ts +6 -0
- package/dist/agent-core/runtime/context/request-budget.js +22 -0
- package/dist/agent-core/runtime/context/restore.d.ts +15 -0
- package/dist/agent-core/runtime/context/restore.js +85 -0
- package/dist/agent-core/runtime/context/usage.d.ts +18 -0
- package/dist/agent-core/runtime/context/usage.js +138 -0
- package/dist/agent-core/runtime/context/world-state.d.ts +35 -0
- package/dist/agent-core/runtime/context/world-state.js +133 -0
- package/dist/agent-core/runtime/contracts.d.ts +37 -0
- package/dist/agent-core/runtime/event-projection.d.ts +5 -0
- package/dist/agent-core/runtime/event-projection.js +17 -0
- package/dist/agent-core/runtime/events.d.ts +142 -0
- package/dist/agent-core/runtime/execution.d.ts +50 -0
- package/dist/agent-core/runtime/execution.js +393 -0
- package/dist/{runtime → agent-core/runtime}/rollout-budget.d.ts +1 -1
- package/dist/agent-core/sdk/agent.js +209 -0
- package/dist/agent-core/sdk/model-runtime.d.ts +13 -0
- package/dist/agent-core/sdk/model-runtime.js +63 -0
- package/dist/agent-core/sdk/session-title.d.ts +3 -0
- package/dist/agent-core/sdk/session-title.js +42 -0
- package/dist/agent-core/sdk/session.d.ts +62 -0
- package/dist/agent-core/sdk/session.js +1147 -0
- package/dist/agent-core/sdk/subagent-backend.d.ts +3 -0
- package/dist/agent-core/sdk/subagent-backend.js +46 -0
- package/dist/agent-core/sdk/types.d.ts +56 -0
- package/dist/{session → agent-core/session}/catalog.d.ts +2 -1
- package/dist/{session → agent-core/session}/context-gc-checkpoint.js +1 -1
- package/dist/{session → agent-core/session}/jsonl-store.d.ts +2 -2
- package/dist/{session → agent-core/session}/jsonl-store.js +1 -1
- package/dist/agent-core/subagent/contracts.d.ts +91 -0
- package/dist/agent-core/subagent/contracts.js +1 -0
- package/dist/agent-core/subagent/scheduler.d.ts +117 -0
- package/dist/{subagent → agent-core/subagent}/session-controller.d.ts +3 -2
- package/dist/agent-core/tools/agent/agent-cancel.d.ts +7 -0
- package/dist/{tools → agent-core/tools/agent}/agent-cancel.js +1 -1
- package/dist/agent-core/tools/agent/agent-message.d.ts +7 -0
- package/dist/{tools → agent-core/tools/agent}/agent-message.js +2 -2
- package/dist/{tools → agent-core/tools/agent}/agent-start.d.ts +3 -3
- package/dist/{tools → agent-core/tools/agent}/agent-start.js +3 -3
- package/dist/agent-core/tools/agent/agent-wait.d.ts +7 -0
- package/dist/{tools → agent-core/tools/agent}/agent-wait.js +2 -2
- package/dist/{tools → agent-core/tools/agent}/question.d.ts +2 -2
- package/dist/{tools → agent-core/tools/agent}/question.js +2 -2
- package/dist/{tools → agent-core/tools/agent}/skill.d.ts +2 -2
- package/dist/{tools → agent-core/tools/agent}/skill.js +2 -2
- package/dist/{tools → agent-core/tools/agent}/todo-read.d.ts +2 -2
- package/dist/{tools → agent-core/tools/agent}/todo-write.d.ts +2 -2
- package/dist/{tools → agent-core/tools/agent}/todo-write.js +2 -2
- package/dist/{tools → agent-core/tools/agent}/todo.d.ts +2 -9
- package/dist/{tools → agent-core/tools/agent}/todo.js +1 -1
- package/dist/{tools → agent-core/tools}/code-mode/cells.d.ts +1 -1
- package/dist/{tools → agent-core/tools}/code-mode/dispatch.d.ts +2 -4
- package/dist/{tools → agent-core/tools}/code-mode/dispatch.js +5 -4
- package/dist/{tools → agent-core/tools}/code-mode/engine.d.ts +1 -1
- package/dist/agent-core/tools/code-mode/engine.js +1 -0
- package/dist/{tools/code-mode.d.ts → agent-core/tools/code-mode/index.d.ts} +7 -7
- package/dist/{tools/code-mode.js → agent-core/tools/code-mode/index.js} +14 -15
- package/dist/{tools → agent-core/tools}/code-mode/state.d.ts +2 -13
- package/dist/agent-core/tools/code-mode/worker.d.ts +1 -0
- package/dist/agent-core/tools/contracts.d.ts +168 -0
- package/dist/agent-core/tools/contracts.js +1 -0
- package/dist/agent-core/tools/execution.d.ts +34 -0
- package/dist/agent-core/tools/execution.js +102 -0
- package/dist/agent-core/tools/local/apply-patch.d.ts +19 -0
- package/dist/{tools → agent-core/tools/local}/apply-patch.js +8 -2
- package/dist/{tools → agent-core/tools/local}/exec.d.ts +5 -17
- package/dist/{tools → agent-core/tools/local}/exec.js +4 -4
- package/dist/{tools → agent-core/tools/local}/read.d.ts +7 -15
- package/dist/{tools → agent-core/tools/local}/read.js +185 -2
- package/dist/{tools/local-registry.d.ts → agent-core/tools/local/registry.d.ts} +5 -5
- package/dist/agent-core/tools/local/registry.js +101 -0
- package/dist/{tools → agent-core/tools/local}/view-image.d.ts +4 -9
- package/dist/{tools → agent-core/tools/local}/view-image.js +2 -2
- package/dist/agent-core/tools/local/web-fetch.d.ts +4 -0
- package/dist/agent-core/tools/local/web-fetch.js +85 -0
- package/dist/agent-core/tools/local/web-search.d.ts +7 -0
- package/dist/agent-core/tools/local/web-search.js +34 -0
- package/dist/{tools/local-workspace-tools.js → agent-core/tools/local/workspace-tools.js} +1 -1
- package/dist/{sdk/mcp-catalog.d.ts → agent-core/tools/mcp/catalog.d.ts} +1 -2
- package/dist/{sdk/mcp-catalog.js → agent-core/tools/mcp/catalog.js} +1 -1
- package/dist/agent-core/tools/mcp/history.d.ts +4 -0
- package/dist/agent-core/tools/mcp/history.js +33 -0
- package/dist/{tools → agent-core/tools/mcp}/tool-search.d.ts +3 -3
- package/dist/{tools → agent-core/tools/mcp}/tool-search.js +4 -4
- package/dist/agent-core/tools/output-projection.d.ts +14 -0
- package/dist/agent-core/tools/output-projection.js +189 -0
- package/dist/{tools → agent-core/tools}/registry.d.ts +1 -1
- package/dist/agent-core/tools/registry.js +47 -0
- package/dist/{tools → agent-core/tools}/runtime-context.d.ts +8 -7
- package/dist/agent-core/tools/runtime-context.js +1 -0
- package/dist/{tools → agent-core/tools}/runtime.d.ts +1 -1
- package/dist/cli/codex-auth-file.d.ts +1 -1
- package/dist/cli/codex-auth-file.js +2 -2
- package/dist/cli/main.js +9 -9
- package/dist/index.d.ts +17 -15
- package/dist/index.js +8 -8
- package/dist/sandbox/linux/bubblewrap-sandbox-executor.d.ts +1 -1
- package/dist/sandbox/linux/bubblewrap-sandbox-executor.js +1 -1
- package/package.json +3 -3
- package/dist/host/contracts.d.ts +0 -21
- package/dist/sdk/agent.js +0 -2548
- package/dist/sdk/types.d.ts +0 -349
- package/dist/subagent/scheduler.d.ts +0 -206
- package/dist/tools/agent-cancel.d.ts +0 -7
- package/dist/tools/agent-message.d.ts +0 -7
- package/dist/tools/agent-wait.d.ts +0 -7
- package/dist/tools/apply-patch.d.ts +0 -32
- package/dist/tools/local-registry.js +0 -373
- package/dist/tools/registry.js +0 -47
- package/dist/tools/web-fetch.d.ts +0 -2
- package/dist/tools/web-fetch.js +0 -12
- package/dist/tools/web-search.d.ts +0 -2
- package/dist/tools/web-search.js +0 -15
- package/dist/{error.d.ts → agent-core/error.d.ts} +0 -0
- package/dist/{error.js → agent-core/error.js} +0 -0
- package/dist/{host → agent-core/host}/contracts.js +0 -0
- package/dist/{host → agent-core/host}/local-process-executor.d.ts +0 -0
- package/dist/{host → agent-core/host}/local-process-executor.js +0 -0
- package/dist/{host → agent-core/host}/network.d.ts +0 -0
- package/dist/{host → agent-core/host}/network.js +0 -0
- package/dist/{model → agent-core/model}/anthropic-messages.d.ts +0 -0
- package/dist/{model → agent-core/model}/anthropic-messages.js +0 -0
- package/dist/{model → agent-core/model}/anthropic-thinking.d.ts +0 -0
- package/dist/{model → agent-core/model}/anthropic-thinking.js +0 -0
- package/dist/{model → agent-core/model}/catalog-file.d.ts +0 -0
- package/dist/{model → agent-core/model}/catalog-file.js +0 -0
- package/dist/{model → agent-core/model}/configuration.d.ts +0 -0
- package/dist/{model → agent-core/model}/configuration.js +0 -0
- package/dist/{model → agent-core/model}/contracts.js +0 -0
- package/dist/{model → agent-core/model}/http.d.ts +0 -0
- package/dist/{model → agent-core/model}/http.js +0 -0
- package/dist/{model → agent-core/model}/openai-responses.d.ts +0 -0
- package/dist/{model → agent-core/model}/openai-responses.js +0 -0
- package/dist/{model → agent-core/model}/runtime-credential.d.ts +0 -0
- package/dist/{model → agent-core/model}/runtime-credential.js +0 -0
- package/dist/{model → agent-core/model}/sse.d.ts +0 -0
- package/dist/{model → agent-core/model}/sse.js +0 -0
- package/dist/{model → agent-core/model}/transport-test.d.ts +0 -0
- package/dist/{model → agent-core/model}/transport-test.js +0 -0
- package/dist/{prompts → agent-core/prompts}/compact.d.ts +0 -0
- package/dist/{prompts → agent-core/prompts}/compact.js +0 -0
- package/dist/{prompts → agent-core/prompts}/core.js +0 -0
- package/dist/{prompts → agent-core/prompts}/execution-budget.d.ts +0 -0
- package/dist/{prompts → agent-core/prompts}/execution-budget.js +0 -0
- package/dist/{prompts → agent-core/prompts}/index.js +0 -0
- package/dist/{prompts → agent-core/prompts}/modes.js +0 -0
- package/dist/{prompts → agent-core/prompts}/output.d.ts +0 -0
- package/dist/{prompts → agent-core/prompts}/output.js +0 -0
- package/dist/{prompts → agent-core/prompts}/resources.js +0 -0
- package/dist/{prompts → agent-core/prompts}/subagent.d.ts +0 -0
- package/dist/{prompts → agent-core/prompts}/subagent.js +0 -0
- package/dist/{prompts → agent-core/prompts}/title.d.ts +0 -0
- package/dist/{prompts → agent-core/prompts}/title.js +0 -0
- package/dist/{prompts → agent-core/prompts}/workflow.d.ts +0 -0
- package/dist/{prompts → agent-core/prompts}/workflow.js +0 -0
- package/dist/{runtime → agent-core/runtime}/async-queue.d.ts +0 -0
- package/dist/{runtime → agent-core/runtime}/async-queue.js +0 -0
- package/dist/{sdk/types.js → agent-core/runtime/context/contracts.js} +0 -0
- package/dist/{runtime → agent-core/runtime/context}/normalize.js +0 -0
- package/dist/{session/types.js → agent-core/runtime/contracts.js} +0 -0
- package/dist/{tools/code-mode/engine.js → agent-core/runtime/events.js} +0 -0
- package/dist/{runtime → agent-core/runtime}/execution-policy.d.ts +0 -0
- package/dist/{runtime → agent-core/runtime}/execution-policy.js +0 -0
- package/dist/{runtime → agent-core/runtime}/plan-output.d.ts +0 -0
- package/dist/{runtime → agent-core/runtime}/plan-output.js +0 -0
- package/dist/{runtime → agent-core/runtime}/rollout-budget.js +0 -0
- package/dist/{runtime → agent-core/runtime}/token-budget.d.ts +0 -0
- package/dist/{runtime → agent-core/runtime}/token-budget.js +0 -0
- package/dist/{sdk → agent-core/sdk}/agent.d.ts +0 -0
- package/dist/{tools/code-mode/worker.d.ts → agent-core/sdk/types.js} +0 -0
- package/dist/{session → agent-core/session}/catalog.js +0 -0
- package/dist/{session → agent-core/session}/context-gc-checkpoint.d.ts +0 -0
- package/dist/{session → agent-core/session}/context-gc-replacements.d.ts +0 -0
- package/dist/{session → agent-core/session}/context-gc-replacements.js +0 -0
- package/dist/{session → agent-core/session}/context-gc-subagent.d.ts +0 -0
- package/dist/{session → agent-core/session}/context-gc-subagent.js +0 -0
- package/dist/{session → agent-core/session}/transaction-recovery.d.ts +0 -0
- package/dist/{session → agent-core/session}/transaction-recovery.js +0 -0
- package/dist/{session → agent-core/session}/types.d.ts +0 -0
- package/dist/{tools/runtime-context.js → agent-core/session/types.js} +0 -0
- package/dist/{subagent → agent-core/subagent}/scheduler.js +0 -0
- package/dist/{subagent → agent-core/subagent}/session-controller.js +0 -0
- package/dist/{tools → agent-core/tools/agent}/todo-read.js +0 -0
- package/dist/{tools → agent-core/tools}/callers.d.ts +0 -0
- package/dist/{tools → agent-core/tools}/callers.js +0 -0
- package/dist/{tools → agent-core/tools}/code-mode/cells.js +1 -1
- /package/dist/{tools → agent-core/tools}/code-mode/quickjs-engine.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/code-mode/quickjs-engine.js +0 -0
- /package/dist/{tools → agent-core/tools}/code-mode/state.js +0 -0
- /package/dist/{tools → agent-core/tools}/code-mode/text-output.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/code-mode/text-output.js +0 -0
- /package/dist/{tools → agent-core/tools}/code-mode/worker.js +0 -0
- /package/dist/{tools → agent-core/tools}/execution-limits.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/execution-limits.js +0 -0
- /package/dist/{tools → agent-core/tools/local}/read-context.d.ts +0 -0
- /package/dist/{tools → agent-core/tools/local}/read-context.js +0 -0
- /package/dist/{tools/local-workspace-tools.d.ts → agent-core/tools/local/workspace-tools.d.ts} +0 -0
- /package/dist/{tools → agent-core/tools}/runtime.js +0 -0
- /package/dist/{tools → agent-core/tools}/scheduler.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/scheduler.js +0 -0
- /package/dist/{tools → agent-core/tools}/shared/path-resolver.d.ts +0 -0
- /package/dist/{tools → agent-core/tools}/shared/path-resolver.js +0 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type { MarAgentReasoningEffort } from '../model/configuration.js';
|
|
2
|
+
import type { CodeCellEventData, MarAgentToolArtifact } from '../tools/contracts.js';
|
|
3
|
+
import type { ExecutionMode } from './contracts.js';
|
|
4
|
+
export interface MarAgentEventBase {
|
|
5
|
+
eventId: string;
|
|
6
|
+
sequence: number;
|
|
7
|
+
timestamp: string;
|
|
8
|
+
sessionId: string;
|
|
9
|
+
executionId: string;
|
|
10
|
+
turnId?: string;
|
|
11
|
+
}
|
|
12
|
+
export type MarAgentEvent = MarAgentEventBase & ({
|
|
13
|
+
type: 'execution.started';
|
|
14
|
+
mode: ExecutionMode;
|
|
15
|
+
} | {
|
|
16
|
+
type: 'message.delta';
|
|
17
|
+
itemId: string;
|
|
18
|
+
phase: 'commentary' | 'final_answer';
|
|
19
|
+
delta: string;
|
|
20
|
+
} | {
|
|
21
|
+
type: 'message.completed';
|
|
22
|
+
itemId: string;
|
|
23
|
+
phase: 'commentary' | 'final_answer';
|
|
24
|
+
text: string;
|
|
25
|
+
modelId: string;
|
|
26
|
+
modelName: string;
|
|
27
|
+
} | {
|
|
28
|
+
type: 'model.performance';
|
|
29
|
+
latencyMs: number;
|
|
30
|
+
outputTokens?: number | undefined;
|
|
31
|
+
outputDurationMs?: number | undefined;
|
|
32
|
+
tokensPerSecond?: number | undefined;
|
|
33
|
+
} | {
|
|
34
|
+
/** Legacy history event; new executions publish model.performance instead. */
|
|
35
|
+
type: 'message.performance';
|
|
36
|
+
itemId: string;
|
|
37
|
+
ttftMs: number;
|
|
38
|
+
outputTokens?: number | undefined;
|
|
39
|
+
outputDurationMs?: number | undefined;
|
|
40
|
+
tokensPerSecond?: number | undefined;
|
|
41
|
+
} | {
|
|
42
|
+
type: 'plan.completed';
|
|
43
|
+
itemId: string;
|
|
44
|
+
text: string;
|
|
45
|
+
} | {
|
|
46
|
+
type: 'reasoning.summary';
|
|
47
|
+
text: string;
|
|
48
|
+
} | {
|
|
49
|
+
type: 'hosted_tool.updated';
|
|
50
|
+
itemId: string;
|
|
51
|
+
kind: 'WEB_SEARCH' | 'WEB_FETCH';
|
|
52
|
+
status: 'RUNNING' | 'COMPLETED' | 'FAILED';
|
|
53
|
+
summary: {
|
|
54
|
+
query?: string | undefined;
|
|
55
|
+
url?: string | undefined;
|
|
56
|
+
title?: string | undefined;
|
|
57
|
+
sources?: readonly {
|
|
58
|
+
url: string;
|
|
59
|
+
title?: string | undefined;
|
|
60
|
+
}[] | undefined;
|
|
61
|
+
sourcesTruncated?: boolean | undefined;
|
|
62
|
+
};
|
|
63
|
+
} | {
|
|
64
|
+
type: 'citation.added';
|
|
65
|
+
id: string;
|
|
66
|
+
url: string;
|
|
67
|
+
title?: string;
|
|
68
|
+
} | {
|
|
69
|
+
type: 'tool.started';
|
|
70
|
+
toolName: string;
|
|
71
|
+
callId: string;
|
|
72
|
+
parentCallId?: string;
|
|
73
|
+
input?: unknown;
|
|
74
|
+
} | {
|
|
75
|
+
type: 'tool.completed';
|
|
76
|
+
toolName: string;
|
|
77
|
+
callId: string;
|
|
78
|
+
parentCallId?: string;
|
|
79
|
+
summary: string;
|
|
80
|
+
truncated?: boolean;
|
|
81
|
+
artifacts?: readonly MarAgentToolArtifact[];
|
|
82
|
+
data?: CodeCellEventData | import('../tools/contracts.js').ExecResult | Omit<import('../tools/contracts.js').ExecResult, 'stdout' | 'stderr'> | import('../tools/contracts.js').ApplyPatchResult | import('../tools/contracts.js').ReadEventData | import('../tools/contracts.js').ViewImageResult | import('../subagent/contracts.js').SubagentTaskSnapshot;
|
|
83
|
+
} | {
|
|
84
|
+
type: 'tool.failed';
|
|
85
|
+
toolName: string;
|
|
86
|
+
callId: string;
|
|
87
|
+
parentCallId?: string;
|
|
88
|
+
code: string;
|
|
89
|
+
message?: string;
|
|
90
|
+
} | {
|
|
91
|
+
type: 'usage.updated';
|
|
92
|
+
inputTokens?: number | undefined;
|
|
93
|
+
contextInputTokens?: number | undefined;
|
|
94
|
+
outputTokens?: number | undefined;
|
|
95
|
+
cacheReadTokens?: number | undefined;
|
|
96
|
+
cacheWriteTokens?: number | undefined;
|
|
97
|
+
} | {
|
|
98
|
+
type: 'todo.updated';
|
|
99
|
+
revision: number;
|
|
100
|
+
items: readonly import('../tools/contracts.js').TodoItem[];
|
|
101
|
+
} | {
|
|
102
|
+
type: 'user_input.requested';
|
|
103
|
+
requestId: string;
|
|
104
|
+
questions: unknown[];
|
|
105
|
+
} | {
|
|
106
|
+
type: 'user_input.resolved';
|
|
107
|
+
requestId: string;
|
|
108
|
+
} | {
|
|
109
|
+
type: 'subagent.started';
|
|
110
|
+
agentId: string;
|
|
111
|
+
description: string;
|
|
112
|
+
modelId?: string;
|
|
113
|
+
modelName?: string;
|
|
114
|
+
reasoningEffort?: MarAgentReasoningEffort;
|
|
115
|
+
} | {
|
|
116
|
+
type: 'subagent.completed';
|
|
117
|
+
agentId: string;
|
|
118
|
+
description?: string;
|
|
119
|
+
status: string;
|
|
120
|
+
modelId?: string;
|
|
121
|
+
modelName?: string;
|
|
122
|
+
reasoningEffort?: MarAgentReasoningEffort;
|
|
123
|
+
latestMessage?: import('../subagent/contracts.js').SubagentLatestMessage;
|
|
124
|
+
latestActivity?: import('../subagent/contracts.js').SubagentLatestActivity;
|
|
125
|
+
errorCode?: string;
|
|
126
|
+
errorMessage?: string;
|
|
127
|
+
} | {
|
|
128
|
+
type: 'execution.completed';
|
|
129
|
+
} | {
|
|
130
|
+
type: 'execution.failed';
|
|
131
|
+
code: string;
|
|
132
|
+
message?: string;
|
|
133
|
+
} | {
|
|
134
|
+
type: 'execution.cancelled';
|
|
135
|
+
} | {
|
|
136
|
+
type: 'context.compacting';
|
|
137
|
+
compactionId: string;
|
|
138
|
+
} | {
|
|
139
|
+
type: 'context.compacted';
|
|
140
|
+
compactionId: string;
|
|
141
|
+
});
|
|
142
|
+
export type EventPayload<T = MarAgentEvent> = T extends MarAgentEvent ? Omit<T, 'eventId' | 'sequence' | 'timestamp' | 'sessionId' | 'executionId' | 'turnId'> : never;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { MarAgentModelConfiguration } from '../model/configuration.js';
|
|
2
|
+
import type { ModelEvent, ModelMessage, ModelRequest, ModelUsage } from '../model/contracts.js';
|
|
3
|
+
import { type RoundToolScope } from '../tools/execution.js';
|
|
4
|
+
import type { ToolRuntimeImage } from '../tools/runtime-context.js';
|
|
5
|
+
import type { AuthoritativeModelUsage } from './context/usage.js';
|
|
6
|
+
import type { ExecutionMode } from './contracts.js';
|
|
7
|
+
import type { EventPayload } from './events.js';
|
|
8
|
+
import type { RolloutBudget } from './rollout-budget.js';
|
|
9
|
+
export interface PreparedExecutionRound {
|
|
10
|
+
readonly request: ModelRequest;
|
|
11
|
+
readonly toolScope: RoundToolScope;
|
|
12
|
+
}
|
|
13
|
+
/** Internal execution-local ports. No Session/Agent object, OS handles or terminal publisher. */
|
|
14
|
+
export interface ExecutionLoopInput {
|
|
15
|
+
readonly mode: ExecutionMode;
|
|
16
|
+
readonly sourceType: 'user' | 'subagent';
|
|
17
|
+
readonly signal: AbortSignal;
|
|
18
|
+
readonly model: Pick<MarAgentModelConfiguration, 'id' | 'name'>;
|
|
19
|
+
readonly messages: ModelMessage[];
|
|
20
|
+
readonly toolImages: ToolRuntimeImage[];
|
|
21
|
+
readonly rolloutBudget: RolloutBudget | undefined;
|
|
22
|
+
readonly publish: (event: EventPayload) => Promise<void>;
|
|
23
|
+
readonly appendContext: (message: ModelMessage) => Promise<void>;
|
|
24
|
+
readonly prepareRound: (continuation: ModelMessage | undefined) => Promise<PreparedExecutionRound>;
|
|
25
|
+
readonly startModel: (request: ModelRequest) => AsyncIterable<ModelEvent>;
|
|
26
|
+
readonly recoverySignal: () => AbortSignal;
|
|
27
|
+
readonly compactContext: () => Promise<boolean>;
|
|
28
|
+
readonly compactionFallback?: (() => boolean) | undefined;
|
|
29
|
+
readonly observeUsage: (usage: Omit<AuthoritativeModelUsage, 'historyMessages'> | undefined) => void;
|
|
30
|
+
readonly observeContextTokens: (tokens: number) => void;
|
|
31
|
+
readonly checkpoint: (round: PreparedExecutionRound, usage: ModelUsage | undefined) => Promise<void>;
|
|
32
|
+
readonly mailbox: {
|
|
33
|
+
hasPending(): boolean;
|
|
34
|
+
append(): Promise<boolean>;
|
|
35
|
+
close(): void;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** Own the single model/tool loop; the SDK owns its handle, terminal events and cleanup. */
|
|
39
|
+
export declare class ExecutionLoop {
|
|
40
|
+
#private;
|
|
41
|
+
private readonly input;
|
|
42
|
+
private readonly planOutputPrefixes;
|
|
43
|
+
get finalAnswer(): string;
|
|
44
|
+
private set finalAnswer(value);
|
|
45
|
+
constructor(input: ExecutionLoopInput);
|
|
46
|
+
run(): Promise<{
|
|
47
|
+
finalAnswer: string;
|
|
48
|
+
usage?: ModelUsage;
|
|
49
|
+
}>;
|
|
50
|
+
}
|
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import { MarAgentError } from '../error.js';
|
|
2
|
+
import { isProviderContextOverflow, recoverModelStream } from '../model/recovery.js';
|
|
3
|
+
import { modelStreamEventObservedAt } from '../model/sse.js';
|
|
4
|
+
import { modelOutputContinuationMessage } from '../prompts/output.js';
|
|
5
|
+
import { executeRoundTools } from '../tools/execution.js';
|
|
6
|
+
import { publicModelUsage } from './event-projection.js';
|
|
7
|
+
import { PlanOutputPrefix, splitPlanOutput } from './plan-output.js';
|
|
8
|
+
/** Own the single model/tool loop; the SDK owns its handle, terminal events and cleanup. */
|
|
9
|
+
export class ExecutionLoop {
|
|
10
|
+
input;
|
|
11
|
+
#finalAnswer = '';
|
|
12
|
+
planOutputPrefixes = new Map();
|
|
13
|
+
get finalAnswer() {
|
|
14
|
+
return this.#finalAnswer;
|
|
15
|
+
}
|
|
16
|
+
set finalAnswer(value) {
|
|
17
|
+
this.#finalAnswer = value;
|
|
18
|
+
}
|
|
19
|
+
constructor(input) {
|
|
20
|
+
this.input = input;
|
|
21
|
+
}
|
|
22
|
+
async run() {
|
|
23
|
+
const input = this.input;
|
|
24
|
+
const { mode, messages } = input;
|
|
25
|
+
let recoveringFromContextOverflow = false;
|
|
26
|
+
let maxOutputContinuationUsed = false;
|
|
27
|
+
let pendingOutputContinuation;
|
|
28
|
+
const modelRetryState = { attempts: 0 };
|
|
29
|
+
while (true) {
|
|
30
|
+
let stop;
|
|
31
|
+
let sawTool = false;
|
|
32
|
+
let latestUsage;
|
|
33
|
+
let roundServerUsage;
|
|
34
|
+
const pendingTools = [];
|
|
35
|
+
let incompleteToolCall = false;
|
|
36
|
+
const round = await input.prepareRound(pendingOutputContinuation);
|
|
37
|
+
let sawModelSemanticEvent = false;
|
|
38
|
+
let requestStartedAt;
|
|
39
|
+
let firstModelResponseAt;
|
|
40
|
+
let firstOutputDeltaAt;
|
|
41
|
+
let lastOutputDeltaAt;
|
|
42
|
+
let requestCompletedAt;
|
|
43
|
+
let requestOutputTokens;
|
|
44
|
+
const roundMessageCount = messages.length;
|
|
45
|
+
const emitLatency = async () => {
|
|
46
|
+
if (mode === 'compact' || firstModelResponseAt === undefined)
|
|
47
|
+
return;
|
|
48
|
+
await input.publish({
|
|
49
|
+
type: 'model.performance',
|
|
50
|
+
latencyMs: Math.max(0, Math.round(firstModelResponseAt - requestStartedAt))
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
const emitFinalPerformance = async (completedAt) => {
|
|
54
|
+
if (mode === 'compact' ||
|
|
55
|
+
firstModelResponseAt === undefined ||
|
|
56
|
+
firstOutputDeltaAt === undefined ||
|
|
57
|
+
lastOutputDeltaAt === undefined ||
|
|
58
|
+
lastOutputDeltaAt - firstOutputDeltaAt < 1)
|
|
59
|
+
return;
|
|
60
|
+
const outputDuration = completedAt - firstOutputDeltaAt;
|
|
61
|
+
const outputTokens = requestOutputTokens;
|
|
62
|
+
if (outputTokens === undefined || outputDuration < 1 || completedAt < lastOutputDeltaAt)
|
|
63
|
+
return;
|
|
64
|
+
await input.publish({
|
|
65
|
+
type: 'model.performance',
|
|
66
|
+
latencyMs: Math.max(0, Math.round(firstModelResponseAt - requestStartedAt)),
|
|
67
|
+
outputTokens,
|
|
68
|
+
outputDurationMs: Math.round(outputDuration),
|
|
69
|
+
tokensPerSecond: Number(((outputTokens * 1000) / outputDuration).toFixed(2))
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
const persistToolCalls = async (calls) => {
|
|
73
|
+
for (const call of calls) {
|
|
74
|
+
const message = {
|
|
75
|
+
role: 'tool_call',
|
|
76
|
+
callId: call.callId,
|
|
77
|
+
name: call.name,
|
|
78
|
+
arguments: call.arguments,
|
|
79
|
+
...(call.inputMode ? { inputMode: call.inputMode } : {}),
|
|
80
|
+
content: ''
|
|
81
|
+
};
|
|
82
|
+
messages.push(message);
|
|
83
|
+
await input.appendContext(message);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
try {
|
|
87
|
+
requestStartedAt = performance.now();
|
|
88
|
+
for await (const event of input.startModel({
|
|
89
|
+
...round.request,
|
|
90
|
+
retryState: modelRetryState
|
|
91
|
+
})) {
|
|
92
|
+
if (event.type === 'output.reset') {
|
|
93
|
+
firstOutputDeltaAt = undefined;
|
|
94
|
+
lastOutputDeltaAt = undefined;
|
|
95
|
+
requestOutputTokens = undefined;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const modelResponseAt = mode !== 'compact' && isModelResponseEvent(event)
|
|
99
|
+
? Math.max(requestStartedAt, modelStreamEventObservedAt(event) ?? performance.now())
|
|
100
|
+
: undefined;
|
|
101
|
+
const isFirstModelResponse = modelResponseAt !== undefined && firstModelResponseAt === undefined;
|
|
102
|
+
if (modelResponseAt !== undefined) {
|
|
103
|
+
if (event.type !== 'output.delta')
|
|
104
|
+
sawModelSemanticEvent = true;
|
|
105
|
+
firstModelResponseAt ??= modelResponseAt;
|
|
106
|
+
if (event.type === 'output.delta' ||
|
|
107
|
+
(event.type === 'text.delta' && event.delta.length > 0)) {
|
|
108
|
+
firstOutputDeltaAt ??= modelResponseAt;
|
|
109
|
+
lastOutputDeltaAt = modelResponseAt;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (event.type === 'response.item') {
|
|
113
|
+
// Compaction keeps only its successful checkpoint, never intermediate native output.
|
|
114
|
+
if (mode === 'compact')
|
|
115
|
+
continue;
|
|
116
|
+
const message = {
|
|
117
|
+
role: 'provider',
|
|
118
|
+
content: '',
|
|
119
|
+
provider: event.provider,
|
|
120
|
+
providerModelId: input.model.id,
|
|
121
|
+
...(event.provider === 'ANTHROPIC_MESSAGES'
|
|
122
|
+
? { contextKind: 'private' }
|
|
123
|
+
: {}),
|
|
124
|
+
item: event.item
|
|
125
|
+
};
|
|
126
|
+
messages.push(message);
|
|
127
|
+
await input.appendContext(message);
|
|
128
|
+
}
|
|
129
|
+
else if (event.type === 'text.delta') {
|
|
130
|
+
let delta = event.delta;
|
|
131
|
+
if (mode === 'plan' && input.sourceType !== 'subagent') {
|
|
132
|
+
let prefix = this.planOutputPrefixes.get(event.itemId);
|
|
133
|
+
if (!prefix) {
|
|
134
|
+
prefix = new PlanOutputPrefix();
|
|
135
|
+
this.planOutputPrefixes.set(event.itemId, prefix);
|
|
136
|
+
}
|
|
137
|
+
delta = prefix.push(delta);
|
|
138
|
+
}
|
|
139
|
+
if (mode !== 'compact' && delta) {
|
|
140
|
+
await input.publish({
|
|
141
|
+
type: 'message.delta',
|
|
142
|
+
itemId: event.itemId,
|
|
143
|
+
phase: event.phase,
|
|
144
|
+
delta
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
else if (event.type === 'text.completed') {
|
|
149
|
+
const phase = event.phase ?? 'final_answer';
|
|
150
|
+
if (phase === 'final_answer')
|
|
151
|
+
this.finalAnswer = event.text;
|
|
152
|
+
this.planOutputPrefixes.delete(event.itemId);
|
|
153
|
+
if (mode !== 'compact') {
|
|
154
|
+
if (mode === 'plan' && input.sourceType !== 'subagent') {
|
|
155
|
+
const parts = splitPlanOutput(event.text);
|
|
156
|
+
for (const [index, part] of parts.entries()) {
|
|
157
|
+
const itemId = index === 0 ? event.itemId : `${event.itemId}:plan-part:${index}`;
|
|
158
|
+
if (part.kind === 'plan')
|
|
159
|
+
await input.publish({ type: 'plan.completed', itemId, text: part.text });
|
|
160
|
+
else {
|
|
161
|
+
await input.publish({
|
|
162
|
+
type: 'message.completed',
|
|
163
|
+
itemId,
|
|
164
|
+
phase,
|
|
165
|
+
text: part.text,
|
|
166
|
+
modelId: input.model.id,
|
|
167
|
+
modelName: input.model.name
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
await input.publish({
|
|
174
|
+
type: 'message.completed',
|
|
175
|
+
itemId: event.itemId,
|
|
176
|
+
phase,
|
|
177
|
+
text: event.text,
|
|
178
|
+
modelId: input.model.id,
|
|
179
|
+
modelName: input.model.name
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
messages.push({ role: 'assistant', content: event.text });
|
|
184
|
+
}
|
|
185
|
+
else if (event.type === 'usage') {
|
|
186
|
+
latestUsage = event.usage;
|
|
187
|
+
const outputTokens = typeof event.usage.outputTokens === 'number' &&
|
|
188
|
+
Number.isSafeInteger(event.usage.outputTokens) &&
|
|
189
|
+
event.usage.outputTokens >= 0
|
|
190
|
+
? event.usage.outputTokens
|
|
191
|
+
: undefined;
|
|
192
|
+
if (outputTokens !== undefined) {
|
|
193
|
+
requestOutputTokens = outputTokens;
|
|
194
|
+
}
|
|
195
|
+
const reportedContext = event.usage.contextInputTokens ?? event.usage.inputTokens;
|
|
196
|
+
if (reportedContext !== undefined &&
|
|
197
|
+
Number.isSafeInteger(reportedContext) &&
|
|
198
|
+
reportedContext >= 0) {
|
|
199
|
+
input.observeContextTokens(reportedContext);
|
|
200
|
+
roundServerUsage = {
|
|
201
|
+
contextInputTokens: reportedContext,
|
|
202
|
+
outputTokens: typeof event.usage.outputTokens === 'number' &&
|
|
203
|
+
Number.isSafeInteger(event.usage.outputTokens) &&
|
|
204
|
+
event.usage.outputTokens >= 0
|
|
205
|
+
? event.usage.outputTokens
|
|
206
|
+
: undefined
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
await input.publish({ type: 'usage.updated', ...publicModelUsage(event.usage) });
|
|
210
|
+
}
|
|
211
|
+
else if (event.type === 'reasoning.summary') {
|
|
212
|
+
if (mode !== 'compact')
|
|
213
|
+
await input.publish({ type: 'reasoning.summary', text: event.text });
|
|
214
|
+
}
|
|
215
|
+
else if (event.type === 'hosted_tool') {
|
|
216
|
+
if (mode !== 'compact')
|
|
217
|
+
await input.publish({
|
|
218
|
+
type: 'hosted_tool.updated',
|
|
219
|
+
itemId: event.itemId,
|
|
220
|
+
kind: event.kind,
|
|
221
|
+
status: event.status,
|
|
222
|
+
summary: event.summary
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
else if (event.type === 'citation') {
|
|
226
|
+
if (mode !== 'compact')
|
|
227
|
+
await input.publish({
|
|
228
|
+
type: 'citation.added',
|
|
229
|
+
id: event.citation.id,
|
|
230
|
+
url: event.citation.url,
|
|
231
|
+
...(event.citation.title ? { title: event.citation.title } : {})
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
else if (event.type === 'client_tool.call') {
|
|
235
|
+
if (mode === 'compact')
|
|
236
|
+
throw new MarAgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', 'Compaction returned a tool call while tools were disabled.');
|
|
237
|
+
sawTool = true;
|
|
238
|
+
pendingTools.push(event);
|
|
239
|
+
}
|
|
240
|
+
else if (event.type === 'completed') {
|
|
241
|
+
stop = event.reason;
|
|
242
|
+
requestCompletedAt = modelStreamEventObservedAt(event) ?? performance.now();
|
|
243
|
+
}
|
|
244
|
+
if (isFirstModelResponse)
|
|
245
|
+
await emitLatency();
|
|
246
|
+
}
|
|
247
|
+
if (mode !== 'compact' && firstModelResponseAt !== undefined) {
|
|
248
|
+
const completedAt = requestCompletedAt ?? performance.now();
|
|
249
|
+
await emitFinalPerformance(completedAt);
|
|
250
|
+
}
|
|
251
|
+
pendingOutputContinuation = undefined;
|
|
252
|
+
incompleteToolCall = pendingTools.some((call) => call.argumentError !== undefined);
|
|
253
|
+
if (!(stop === 'max_output' && incompleteToolCall)) {
|
|
254
|
+
await persistToolCalls(pendingTools);
|
|
255
|
+
}
|
|
256
|
+
else
|
|
257
|
+
pendingTools.splice(0, pendingTools.length);
|
|
258
|
+
modelRetryState.attempts = 0;
|
|
259
|
+
}
|
|
260
|
+
catch (error) {
|
|
261
|
+
this.planOutputPrefixes.clear();
|
|
262
|
+
if (await recoverModelStream(error, modelRetryState, input.recoverySignal())) {
|
|
263
|
+
this.finalAnswer = '';
|
|
264
|
+
if (mode === 'compact')
|
|
265
|
+
messages.splice(roundMessageCount);
|
|
266
|
+
if (pendingTools.some((call) => call.argumentError !== undefined))
|
|
267
|
+
pendingTools.splice(0, pendingTools.length);
|
|
268
|
+
else if (pendingTools.length > 0)
|
|
269
|
+
await persistToolCalls(pendingTools);
|
|
270
|
+
if (pendingTools.length === 0)
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
else if (mode === 'compact' &&
|
|
274
|
+
!sawModelSemanticEvent &&
|
|
275
|
+
isProviderContextOverflow(error) &&
|
|
276
|
+
input.compactionFallback?.()) {
|
|
277
|
+
modelRetryState.attempts = 0;
|
|
278
|
+
this.finalAnswer = '';
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
else if (mode !== 'compact' &&
|
|
282
|
+
!sawModelSemanticEvent &&
|
|
283
|
+
isProviderContextOverflow(error)) {
|
|
284
|
+
if (!recoveringFromContextOverflow && (await input.compactContext())) {
|
|
285
|
+
modelRetryState.attempts = 0;
|
|
286
|
+
recoveringFromContextOverflow = true;
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
throw new MarAgentError('MAR_AGENT_CONTEXT_LIMIT', 'The model rejected the compacted context as too large.', { cause: error });
|
|
290
|
+
}
|
|
291
|
+
else
|
|
292
|
+
throw error;
|
|
293
|
+
}
|
|
294
|
+
recoveringFromContextOverflow = false;
|
|
295
|
+
input.observeUsage(roundServerUsage);
|
|
296
|
+
if (pendingTools.length === 0 && input.mailbox.hasPending()) {
|
|
297
|
+
await input.mailbox.append();
|
|
298
|
+
this.finalAnswer = '';
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
const toolResults = await executeRoundTools(pendingTools, round.toolScope);
|
|
302
|
+
for (const result of toolResults) {
|
|
303
|
+
if ('images' in result && result.images?.length)
|
|
304
|
+
input.toolImages.push(...result.images.map((image) => ({
|
|
305
|
+
mimeType: image.mimeType,
|
|
306
|
+
data: Buffer.from(image.dataBase64, 'base64')
|
|
307
|
+
})));
|
|
308
|
+
const toolSearchTools = result.call.name === 'tool_search' && !result.failed
|
|
309
|
+
? result.output.toolSearchTools
|
|
310
|
+
: undefined;
|
|
311
|
+
const message = {
|
|
312
|
+
role: 'tool',
|
|
313
|
+
callId: result.call.callId,
|
|
314
|
+
content: result.content,
|
|
315
|
+
...(toolSearchTools === undefined ? {} : { toolSearchTools }),
|
|
316
|
+
...('images' in result && result.images?.length ? { images: result.images } : {})
|
|
317
|
+
};
|
|
318
|
+
messages.push(message);
|
|
319
|
+
await input.appendContext({
|
|
320
|
+
role: message.role,
|
|
321
|
+
callId: result.call.callId,
|
|
322
|
+
content: message.content,
|
|
323
|
+
...(message.toolSearchTools === undefined
|
|
324
|
+
? {}
|
|
325
|
+
: { toolSearchTools: message.toolSearchTools })
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
if (sawTool) {
|
|
329
|
+
modelRetryState.attempts = 0;
|
|
330
|
+
if (input.sourceType === 'subagent')
|
|
331
|
+
this.finalAnswer = '';
|
|
332
|
+
}
|
|
333
|
+
input.rolloutBudget?.recordUsage(latestUsage);
|
|
334
|
+
if (mode !== 'compact' && stop === 'max_output') {
|
|
335
|
+
const needsContinuation = !sawTool || incompleteToolCall;
|
|
336
|
+
if (needsContinuation) {
|
|
337
|
+
if (maxOutputContinuationUsed ||
|
|
338
|
+
(input.rolloutBudget?.active && input.rolloutBudget.stage === 'exhausted'))
|
|
339
|
+
throw new MarAgentError('MAR_AGENT_MODEL_OUTPUT_INCOMPLETE', 'Model output reached its limit before completing.');
|
|
340
|
+
maxOutputContinuationUsed = true;
|
|
341
|
+
pendingOutputContinuation = {
|
|
342
|
+
role: 'user',
|
|
343
|
+
content: modelOutputContinuationMessage
|
|
344
|
+
};
|
|
345
|
+
this.finalAnswer = '';
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
maxOutputContinuationUsed = false;
|
|
349
|
+
}
|
|
350
|
+
else
|
|
351
|
+
maxOutputContinuationUsed = false;
|
|
352
|
+
await input.checkpoint(round, latestUsage);
|
|
353
|
+
if (input.signal.aborted) {
|
|
354
|
+
const aborted = toolResults.find((result) => result.failed)?.error;
|
|
355
|
+
throw aborted ?? new MarAgentError('MAR_AGENT_EXECUTION_CANCELLED', 'Execution cancelled.');
|
|
356
|
+
}
|
|
357
|
+
if (input.mailbox.hasPending()) {
|
|
358
|
+
await input.mailbox.append();
|
|
359
|
+
this.finalAnswer = '';
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
if (stop === 'completed' && !sawTool) {
|
|
363
|
+
input.mailbox.close();
|
|
364
|
+
const compactSummary = mode === 'compact' ? this.finalAnswer.trim() : '';
|
|
365
|
+
if (mode === 'compact' && !compactSummary)
|
|
366
|
+
throw new MarAgentError('MAR_AGENT_COMPACT_FAILED', 'Compaction returned an empty handoff.');
|
|
367
|
+
if (mode !== 'compact' && !this.finalAnswer.trim())
|
|
368
|
+
throw new MarAgentError(input.sourceType === 'subagent'
|
|
369
|
+
? 'MAR_AGENT_SUBAGENT_FAILED'
|
|
370
|
+
: 'MAR_AGENT_MODEL_OUTPUT_INCOMPLETE', input.sourceType === 'subagent'
|
|
371
|
+
? 'Subagent completed without a final summary.'
|
|
372
|
+
: 'Model completed without an answer or tool request.');
|
|
373
|
+
return {
|
|
374
|
+
finalAnswer: this.finalAnswer,
|
|
375
|
+
...(latestUsage === undefined ? {} : { usage: latestUsage })
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
if (!sawTool && stop !== 'pause')
|
|
379
|
+
throw new MarAgentError('MAR_AGENT_MODEL_OUTPUT_INCOMPLETE', 'Model did not complete or request a tool.');
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
function isModelResponseEvent(event) {
|
|
384
|
+
if (event.type === 'text.delta')
|
|
385
|
+
return event.delta.length > 0;
|
|
386
|
+
if (event.type === 'text.completed' || event.type === 'reasoning.summary')
|
|
387
|
+
return event.text.length > 0;
|
|
388
|
+
return (event.type === 'response.item' ||
|
|
389
|
+
event.type === 'output.delta' ||
|
|
390
|
+
event.type === 'client_tool.call' ||
|
|
391
|
+
event.type === 'hosted_tool' ||
|
|
392
|
+
event.type === 'citation');
|
|
393
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ModelUsage } from '../model/contracts.js';
|
|
2
|
-
import type { RolloutBudgetOptions } from '
|
|
2
|
+
import type { RolloutBudgetOptions } from './contracts.js';
|
|
3
3
|
export type RolloutBudgetStage = 'normal' | 'conserve' | 'finalize' | 'exhausted';
|
|
4
4
|
export interface ResolvedRolloutBudgetOptions {
|
|
5
5
|
limitTokens: number;
|