u-foo 4.0.0 → 4.0.2
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/OBSERVABILITY.md +65 -0
- package/README.md +90 -30
- package/README.zh-CN.md +76 -26
- package/SKILLS/ufoo/SKILL.md +25 -1
- package/SKILLS/ufoo-bus/SKILL.md +25 -2
- package/bin/ucode.js +4 -0
- package/bin/ufoo.js +3 -3
- package/dist/tui/darwin-arm64/ufoo-tui +0 -0
- package/dist/tui/darwin-x64/ufoo-tui +0 -0
- package/dist/tui/linux-arm64/ufoo-tui +0 -0
- package/dist/tui/linux-x64/ufoo-tui +0 -0
- package/package.json +6 -6
- package/src/agents/activity/activityDetector.js +1 -1
- package/src/agents/capabilities/agentManagement/service.js +40 -13
- package/src/agents/capabilities/coordination/index.js +1 -1
- package/src/agents/capabilities/taskReports/index.js +1 -1
- package/src/agents/controller/controllerToolExecutor.js +36 -20
- package/src/agents/controller/index.js +1 -6
- package/src/agents/internal/internalRunner.js +90 -56
- package/src/agents/launch/index.js +0 -1
- package/src/agents/launch/launcher.js +38 -29
- package/src/agents/launch/launcherReadiness.js +35 -0
- package/src/agents/launch/notifier.js +2 -2
- package/src/agents/launch/ptyWrapper.js +5 -2
- package/src/agents/launch/readyDetector.js +2 -2
- package/src/agents/profiles/main.js +1 -1
- package/src/agents/prompts/cooperation.js +14 -0
- package/src/agents/prompts/defaultBootstrap.js +1 -1
- package/src/agents/prompts/groupBootstrap.js +23 -7
- package/src/agents/prompts/mainAgent.js +11 -2
- package/src/agents/prompts/native/system.js +4 -1
- package/src/agents/prompts/native/ufoo.js +11 -3
- package/src/agents/prompts/promptProfiles.js +2 -0
- package/src/agents/prompts/reporting.js +16 -0
- package/src/agents/providers/claudeEventTranslator.js +3 -0
- package/src/agents/providers/claudeThreadProvider.js +3 -2
- package/src/agents/providers/clientIdentity.js +75 -0
- package/src/agents/providers/codexEventTranslator.js +23 -2
- package/src/agents/providers/codexThreadProvider.js +15 -6
- package/src/agents/providers/credentials/index.js +2 -0
- package/src/agents/providers/directAuthStatus.js +1 -1
- package/src/agents/providers/index.js +1 -1
- package/src/agents/providers/modelSettings.js +34 -0
- package/src/agents/providers/modelsCatalog.js +379 -0
- package/src/agents/providers/nativeTransport.js +58 -40
- package/src/agents/providers/runtimeConfig.js +10 -8
- package/src/agents/providers/thinkingLevels.js +10 -36
- package/src/agents/providers/transports/responsesProtocol.js +11 -6
- package/src/agents/providers/upstreamRuntime.js +135 -0
- package/src/agents/runtime/context/checkpointStore.js +38 -0
- package/src/agents/runtime/context/compaction.js +151 -0
- package/src/agents/runtime/context/contextBudget.js +93 -0
- package/src/agents/runtime/context/replayHistory.js +28 -0
- package/src/agents/runtime/context/runtimeStore.js +2 -1
- package/src/agents/runtime/core/agentLoop.js +99 -18
- package/src/agents/runtime/core/executionBudget.js +36 -0
- package/src/agents/runtime/createAgentRuntime.js +16 -4
- package/src/agents/runtime/prompts/responseLanguage.js +12 -0
- package/src/agents/runtime/protocol/runObservation.js +24 -0
- package/src/agents/runtime/protocol/traceContext.js +9 -0
- package/src/app/chat/ChatController.js +26 -53
- package/src/app/chat/agentDirectory.js +1 -16
- package/src/app/chat/bootstrap.js +1 -2
- package/src/app/chat/commandExecutor.js +93 -285
- package/src/app/chat/commands.js +32 -45
- package/src/app/chat/daemonMessageRouter.js +0 -45
- package/src/app/chat/historyStore.js +12 -48
- package/src/app/chat/inputSubmitHandler.js +7 -33
- package/src/app/chat/internalAgentLogHistory.js +19 -60
- package/src/app/chat/multiWindow/paneManager.js +10 -3
- package/src/app/chat/streamState.js +0 -4
- package/src/app/cli/features/init.js +5 -5
- package/src/app/cli/metricsCommands.js +68 -0
- package/src/app/cli/run.js +42 -3
- package/src/app/cli/traceCommands.js +36 -0
- package/src/code/agent.js +18 -13
- package/src/code/busConsumer.js +1 -3
- package/src/code/commands.js +1 -1
- package/src/code/context/assembler.js +7 -8
- package/src/code/context/stateCommit.js +2 -1
- package/src/code/internalThread.js +10 -3
- package/src/code/launcher/ucode.js +1 -1
- package/src/code/launcher/ucodeBootstrap.js +1 -1
- package/src/code/modelCommand.js +11 -35
- package/src/code/nativeRunner.js +55 -17
- package/src/code/providers/modelsCatalog.js +1 -317
- package/src/code/repl.js +4 -11
- package/src/code/sessionStore.js +3 -3
- package/src/code/taskDecomposer.js +18 -0
- package/src/code/toolBudget.js +1 -17
- package/src/config.js +22 -72
- package/src/coordination/bus/index.js +12 -3
- package/src/coordination/bus/inject.js +2 -2
- package/src/coordination/bus/message.js +15 -1
- package/src/coordination/bus/nickname.js +1 -1
- package/src/coordination/bus/subscriber.js +3 -2
- package/src/coordination/bus/utils.js +1 -1
- package/src/coordination/history/agentPerformance.js +279 -0
- package/src/coordination/history/agentTrace.js +249 -0
- package/src/coordination/state/workspaceAccess.js +1 -1
- package/src/orchestration/controller/index.js +1 -8
- package/src/orchestration/controller/launchRouting.js +1 -1
- package/src/orchestration/solo/commands.js +3 -1
- package/src/runtime/contracts/agentMode.js +11 -2
- package/src/runtime/contracts/uiProtocol.js +18 -19
- package/src/runtime/daemon/agentHost.js +57 -25
- package/src/runtime/daemon/agentRecoveryGroups.js +70 -0
- package/src/runtime/daemon/busBridge.js +1 -1
- package/src/runtime/daemon/controlPlaneService.js +1 -1
- package/src/runtime/daemon/groupOrchestrator.js +20 -5
- package/src/runtime/daemon/groupStatus.js +20 -0
- package/src/runtime/daemon/index.js +19 -44
- package/src/runtime/daemon/mcpHttpServer.js +39 -10
- package/src/runtime/daemon/mcpServer.js +1 -1
- package/src/runtime/daemon/nicknameScope.js +1 -1
- package/src/runtime/daemon/ops.js +46 -23
- package/src/runtime/daemon/projectRuntimeGateway.js +2 -0
- package/src/runtime/daemon/promptRequest.js +92 -589
- package/src/runtime/daemon/status.js +5 -6
- package/src/tools/controllerSchemas.js +6 -2
- package/src/tools/handlers/tier2.js +4 -2
- package/src/tools/registry.js +4 -0
- package/src/tools/schemaFixtures.js +26 -2
- package/src/tools/tier0/readAgentMetrics.js +14 -0
- package/src/tools/tier0/readAgentTrace.js +12 -0
- package/src/ui/agentSettingsController.js +107 -0
- package/src/ui/agentSurface.js +1 -1
- package/src/ui/dashboardBridge.js +1 -1
- package/src/ui/format/banner.js +1 -4
- package/src/ui/format/index.js +14 -10
- package/src/ui/format/loopSummary.js +50 -0
- package/src/ui/format/markdownRenderer.js +3 -4
- package/src/ui/rustChatHost.js +191 -206
- package/src/ui/rustMultiSession.js +77 -36
- package/src/ui/rustUcodeHost.js +6 -1
- package/src/ui/settingsBridge.js +33 -16
- package/src/ui/toolMergeBridge.js +0 -4
- package/src/ui/uiHostServer.js +5 -8
- package/src/agents/controller/loopObservability.js +0 -200
- package/src/agents/controller/loopRuntime.js +0 -336
- package/src/agents/controller/ufooAgent.js +0 -830
- package/src/agents/launch/ptyRunner.js +0 -1135
- package/src/agents/providers/controllerJsonTransport.js +0 -28
- package/src/agents/providers/upstreamTransport.js +0 -698
- package/src/app/chat/agentBar.js +0 -135
- package/src/app/chat/agentSockets.js +0 -225
- package/src/app/chat/dashboardView.js +0 -405
- package/src/app/chat/rawKeyMap.js +0 -42
- package/src/app/chat/settingsController.js +0 -111
- package/src/coordination/history/controllerSummary.js +0 -23
- package/src/orchestration/controller/flags.js +0 -167
- package/src/orchestration/controller/gateRouter.js +0 -211
- package/src/orchestration/controller/routerFastPath.js +0 -22
- package/src/orchestration/controller/routerFinalize.js +0 -222
- package/src/orchestration/controller/shadowGuard.js +0 -280
- package/src/runtime/daemon/executionPaths.js +0 -30
- package/src/runtime/daemon/promptLoop.js +0 -136
- package/src/runtime/privacy/shadowDiff.js +0 -174
|
@@ -1,336 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { executeControllerTool } = require("./controllerToolExecutor");
|
|
4
|
-
const { createAgentRuntime } = require("../runtime");
|
|
5
|
-
const { createControllerJsonTransport } = require("../providers/controllerJsonTransport");
|
|
6
|
-
const { createCoordinationCapability } = require("../capabilities/coordination");
|
|
7
|
-
const { createLoopObserver } = require("./loopObservability");
|
|
8
|
-
const { finalizeRouterPayload } = require("../../orchestration/controller/routerFinalize");
|
|
9
|
-
|
|
10
|
-
const DEFAULT_LOOP_OPTIONS = {
|
|
11
|
-
enabled: false,
|
|
12
|
-
maxRounds: 3,
|
|
13
|
-
maxToolCalls: 3,
|
|
14
|
-
maxToolErrors: 2,
|
|
15
|
-
maxPromptChars: 12000,
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
const TERMINAL_REASONS = Object.freeze({
|
|
19
|
-
FINAL_ANSWER: "final_answer",
|
|
20
|
-
BUDGET_EXCEEDED: "budget_exceeded",
|
|
21
|
-
TOOL_FAILURE: "tool_failure",
|
|
22
|
-
USER_CANCEL: "user_cancel",
|
|
23
|
-
PROVIDER_ERROR: "provider_error",
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
const FALLBACK_USED_VALUES = Object.freeze({
|
|
27
|
-
NONE: "none",
|
|
28
|
-
ASSISTANT_CALL: "assistant_call",
|
|
29
|
-
LEGACY_ROUTER: "legacy_router",
|
|
30
|
-
HELPER_AGENT: "helper_agent",
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
function normalizeTerminalReason(value) {
|
|
34
|
-
const raw = String(value || "").trim();
|
|
35
|
-
if (!raw) return TERMINAL_REASONS.FINAL_ANSWER;
|
|
36
|
-
if (Object.values(TERMINAL_REASONS).includes(raw)) return raw;
|
|
37
|
-
return TERMINAL_REASONS.FINAL_ANSWER;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function normalizeFallbackUsed(value) {
|
|
41
|
-
const raw = String(value || "").trim();
|
|
42
|
-
if (!raw) return FALLBACK_USED_VALUES.NONE;
|
|
43
|
-
if (Object.values(FALLBACK_USED_VALUES).includes(raw)) return raw;
|
|
44
|
-
return FALLBACK_USED_VALUES.NONE;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function toNonNegativeInt(value) {
|
|
48
|
-
const num = Number(value);
|
|
49
|
-
if (!Number.isFinite(num) || num < 0) return 0;
|
|
50
|
-
return Math.floor(num);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function extractModelMetrics(result) {
|
|
54
|
-
const meta = result && result.meta && typeof result.meta === "object" ? result.meta : null;
|
|
55
|
-
const payloadMeta = result && result.payload && typeof result.payload === "object"
|
|
56
|
-
&& result.payload.meta && typeof result.payload.meta === "object"
|
|
57
|
-
? result.payload.meta
|
|
58
|
-
: null;
|
|
59
|
-
const source = { ...(payloadMeta || {}), ...(meta || {}) };
|
|
60
|
-
return {
|
|
61
|
-
input_tokens: toNonNegativeInt(source.input_tokens),
|
|
62
|
-
output_tokens: toNonNegativeInt(source.output_tokens),
|
|
63
|
-
cache_read_tokens: toNonNegativeInt(source.cache_read_tokens),
|
|
64
|
-
cache_creation_tokens: toNonNegativeInt(source.cache_creation_tokens),
|
|
65
|
-
cache_semistatic_hit: toNonNegativeInt(source.cache_semistatic_hit),
|
|
66
|
-
cache_semistatic_miss: toNonNegativeInt(source.cache_semistatic_miss),
|
|
67
|
-
memory_prefix_tokens: toNonNegativeInt(source.memory_prefix_tokens),
|
|
68
|
-
dynamic_memory_tokens: toNonNegativeInt(source.dynamic_memory_tokens),
|
|
69
|
-
latency_ms: toNonNegativeInt(source.latency_ms),
|
|
70
|
-
first_token_ms: toNonNegativeInt(source.first_token_ms),
|
|
71
|
-
stop_reason: String(source.stop_reason || "").trim(),
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function normalizePositiveInt(value, fallback) {
|
|
76
|
-
const num = Number.parseInt(value, 10);
|
|
77
|
-
if (Number.isFinite(num) && num > 0) return num;
|
|
78
|
-
return fallback;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function resolveLoopRuntimeOptions(env = process.env) {
|
|
82
|
-
const mode = String(env.UFOO_AGENT_RUNTIME_MODE || env.UFOO_AGENT_LOOP_MODE || "").trim().toLowerCase();
|
|
83
|
-
const enabled = mode === "loop" || String(env.UFOO_AGENT_ENABLE_LOOP || "").trim() === "1";
|
|
84
|
-
return {
|
|
85
|
-
enabled,
|
|
86
|
-
maxRounds: normalizePositiveInt(env.UFOO_AGENT_LOOP_MAX_ROUNDS, DEFAULT_LOOP_OPTIONS.maxRounds),
|
|
87
|
-
maxToolCalls: normalizePositiveInt(env.UFOO_AGENT_LOOP_MAX_TOOL_CALLS, DEFAULT_LOOP_OPTIONS.maxToolCalls),
|
|
88
|
-
maxToolErrors: normalizePositiveInt(env.UFOO_AGENT_LOOP_MAX_TOOL_ERRORS, DEFAULT_LOOP_OPTIONS.maxToolErrors),
|
|
89
|
-
maxPromptChars: normalizePositiveInt(env.UFOO_AGENT_LOOP_MAX_PROMPT_CHARS, DEFAULT_LOOP_OPTIONS.maxPromptChars),
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function normalizePayload(payload) {
|
|
94
|
-
if (!payload || typeof payload !== "object") {
|
|
95
|
-
return { reply: "", dispatch: [], ops: [], done: true };
|
|
96
|
-
}
|
|
97
|
-
return {
|
|
98
|
-
...payload,
|
|
99
|
-
reply: typeof payload.reply === "string" ? payload.reply : "",
|
|
100
|
-
dispatch: Array.isArray(payload.dispatch) ? payload.dispatch : [],
|
|
101
|
-
ops: Array.isArray(payload.ops) ? payload.ops : [],
|
|
102
|
-
done: payload.done !== false,
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function buildLoopContinuationPrompt({
|
|
107
|
-
originalPrompt,
|
|
108
|
-
toolResults,
|
|
109
|
-
lastReply,
|
|
110
|
-
loopState,
|
|
111
|
-
}) {
|
|
112
|
-
const lines = [];
|
|
113
|
-
lines.push(String(originalPrompt || ""));
|
|
114
|
-
lines.push("");
|
|
115
|
-
if (lastReply) {
|
|
116
|
-
lines.push("Previous draft reply:");
|
|
117
|
-
lines.push(String(lastReply || ""));
|
|
118
|
-
lines.push("");
|
|
119
|
-
}
|
|
120
|
-
lines.push("Controller loop state (JSON):");
|
|
121
|
-
lines.push(JSON.stringify(loopState, null, 2));
|
|
122
|
-
lines.push("");
|
|
123
|
-
lines.push("Controller tool results so far (JSON):");
|
|
124
|
-
lines.push(JSON.stringify(toolResults, null, 2));
|
|
125
|
-
lines.push("");
|
|
126
|
-
lines.push("Use these results to decide the next tool_call or final JSON response.");
|
|
127
|
-
return lines.join("\n");
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
async function finalizeLoopRun({
|
|
131
|
-
projectRoot,
|
|
132
|
-
payload,
|
|
133
|
-
prompt = "",
|
|
134
|
-
processManager,
|
|
135
|
-
dispatchMessages,
|
|
136
|
-
handleOps,
|
|
137
|
-
markPending,
|
|
138
|
-
finalizeLocally = true,
|
|
139
|
-
}) {
|
|
140
|
-
return finalizeRouterPayload({
|
|
141
|
-
projectRoot,
|
|
142
|
-
payload,
|
|
143
|
-
prompt,
|
|
144
|
-
processManager: processManager || null,
|
|
145
|
-
dispatchMessages,
|
|
146
|
-
handleOps,
|
|
147
|
-
markPending,
|
|
148
|
-
finalizeLocally,
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function buildTerminalPayload(reason, lastPayload, rounds, toolCalls, toolErrors, totals = {}) {
|
|
153
|
-
const payload = normalizePayload(lastPayload);
|
|
154
|
-
const canonicalReason = normalizeTerminalReason(reason);
|
|
155
|
-
if (!payload.reply) {
|
|
156
|
-
payload.reply = `Controller loop stopped: ${canonicalReason}.`;
|
|
157
|
-
}
|
|
158
|
-
payload.dispatch = [];
|
|
159
|
-
payload.ops = [];
|
|
160
|
-
payload.loop = {
|
|
161
|
-
terminal_reason: canonicalReason,
|
|
162
|
-
rounds,
|
|
163
|
-
tool_calls: toolCalls,
|
|
164
|
-
tool_errors: toolErrors,
|
|
165
|
-
fallback_used: normalizeFallbackUsed(totals.fallback_used),
|
|
166
|
-
total_tokens: toNonNegativeInt(totals.total_tokens),
|
|
167
|
-
total_latency_ms: toNonNegativeInt(totals.total_latency_ms),
|
|
168
|
-
dynamic_memory_tokens: toNonNegativeInt(totals.dynamic_memory_tokens),
|
|
169
|
-
};
|
|
170
|
-
return payload;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
async function runPromptWithControllerLoop({
|
|
174
|
-
projectRoot,
|
|
175
|
-
prompt,
|
|
176
|
-
provider,
|
|
177
|
-
model,
|
|
178
|
-
processManager = null,
|
|
179
|
-
runUfooAgent,
|
|
180
|
-
dispatchMessages,
|
|
181
|
-
handleOps,
|
|
182
|
-
ackBus,
|
|
183
|
-
markPending = () => {},
|
|
184
|
-
log = () => {},
|
|
185
|
-
ufooAgentOptions = {},
|
|
186
|
-
finalizeLocally = true,
|
|
187
|
-
loopRuntime = DEFAULT_LOOP_OPTIONS,
|
|
188
|
-
observer: providedObserver = null,
|
|
189
|
-
observabilityDefaults = {},
|
|
190
|
-
now = () => Date.now(),
|
|
191
|
-
isCancelled = null,
|
|
192
|
-
}) {
|
|
193
|
-
const options = { ...DEFAULT_LOOP_OPTIONS, ...(loopRuntime || {}) };
|
|
194
|
-
const observer = providedObserver || createLoopObserver({
|
|
195
|
-
projectRoot,
|
|
196
|
-
enabled: options.enabled !== false,
|
|
197
|
-
defaults: observabilityDefaults,
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
let currentPrompt = String(prompt || "");
|
|
201
|
-
let lastPayload = null;
|
|
202
|
-
let toolCalls = 0;
|
|
203
|
-
let toolErrors = 0;
|
|
204
|
-
let totalTokens = 0;
|
|
205
|
-
let totalLatencyMs = 0;
|
|
206
|
-
let dynamicMemoryTokens = 0;
|
|
207
|
-
const toolResults = [];
|
|
208
|
-
|
|
209
|
-
const checkCancellation = () => {
|
|
210
|
-
if (typeof isCancelled !== "function") return false;
|
|
211
|
-
try {
|
|
212
|
-
return isCancelled() === true;
|
|
213
|
-
} catch {
|
|
214
|
-
return false;
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
const totals = () => ({
|
|
219
|
-
fallback_used: FALLBACK_USED_VALUES.NONE,
|
|
220
|
-
total_tokens: totalTokens,
|
|
221
|
-
total_latency_ms: totalLatencyMs,
|
|
222
|
-
dynamic_memory_tokens: dynamicMemoryTokens,
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
const terminate = (reason, payloadBase, roundsCount) => {
|
|
226
|
-
const finalPayload = buildTerminalPayload(
|
|
227
|
-
reason,
|
|
228
|
-
payloadBase,
|
|
229
|
-
roundsCount,
|
|
230
|
-
toolCalls,
|
|
231
|
-
toolErrors,
|
|
232
|
-
totals()
|
|
233
|
-
);
|
|
234
|
-
observer.emit("loop_terminal", finalPayload.loop);
|
|
235
|
-
return finalPayload;
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
let rounds = 0;
|
|
239
|
-
const host = {
|
|
240
|
-
grantedPermissions: ["coordination.read", "coordination.write", "agents.manage", "schedules.manage", "memory.write"],
|
|
241
|
-
coordination: {
|
|
242
|
-
projectRoot, processManager, dispatchMessages, handleOps, ackBus, markPending, observer,
|
|
243
|
-
async execute(ctx, call) {
|
|
244
|
-
const started = now();
|
|
245
|
-
const result = await executeControllerTool({ ...ctx, turnId: `loop-round-${rounds}` }, call);
|
|
246
|
-
let size = 0;
|
|
247
|
-
let memory = 0;
|
|
248
|
-
try {
|
|
249
|
-
size = result.result === undefined ? 0 : JSON.stringify(result.result).length;
|
|
250
|
-
memory = toNonNegativeInt(result.result && result.result.dynamic_memory_tokens);
|
|
251
|
-
} catch { /* diagnostic metadata only */ }
|
|
252
|
-
dynamicMemoryTokens += memory;
|
|
253
|
-
observer.emit("tool_call", {
|
|
254
|
-
round: rounds, tool_name: result.name || call.name,
|
|
255
|
-
tool_call_id: result.tool_call_id || "", turn_id: result.turn_id || `loop-round-${rounds}`,
|
|
256
|
-
duration_ms: Math.max(0, now() - started), result_size: size,
|
|
257
|
-
dynamic_memory_tokens: memory, retry_count: 0, final_status: result.ok ? "ok" : "error",
|
|
258
|
-
});
|
|
259
|
-
return result;
|
|
260
|
-
},
|
|
261
|
-
},
|
|
262
|
-
};
|
|
263
|
-
const policy = {
|
|
264
|
-
beforeTurn() {
|
|
265
|
-
if (checkCancellation()) return TERMINAL_REASONS.USER_CANCEL;
|
|
266
|
-
if (rounds >= options.maxRounds || currentPrompt.length > options.maxPromptChars) return TERMINAL_REASONS.BUDGET_EXCEEDED;
|
|
267
|
-
return null;
|
|
268
|
-
},
|
|
269
|
-
beforeTool() {
|
|
270
|
-
return toolCalls >= options.maxToolCalls ? TERMINAL_REASONS.BUDGET_EXCEEDED : null;
|
|
271
|
-
},
|
|
272
|
-
afterTool({ call, result }) {
|
|
273
|
-
toolCalls += 1;
|
|
274
|
-
if (!result || !result.ok) toolErrors += 1;
|
|
275
|
-
toolResults.push(result && result.name ? result : {
|
|
276
|
-
...result, name: call.name, tool_call_id: call.source.id, turn_id: `loop-round-${rounds}`,
|
|
277
|
-
});
|
|
278
|
-
if (toolErrors >= options.maxToolErrors) return TERMINAL_REASONS.TOOL_FAILURE;
|
|
279
|
-
currentPrompt = buildLoopContinuationPrompt({
|
|
280
|
-
originalPrompt: prompt, toolResults, lastReply: lastPayload.reply,
|
|
281
|
-
loopState: { round: rounds, max_rounds: options.maxRounds, tool_calls_used: toolCalls,
|
|
282
|
-
tool_calls_remaining: Math.max(options.maxToolCalls - toolCalls, 0), tool_errors: toolErrors },
|
|
283
|
-
});
|
|
284
|
-
return null;
|
|
285
|
-
},
|
|
286
|
-
};
|
|
287
|
-
const transport = createControllerJsonTransport({ async invoke() {
|
|
288
|
-
rounds += 1;
|
|
289
|
-
const started = now();
|
|
290
|
-
observer.emit("model_call_started", { round: rounds, provider: String(provider || ""),
|
|
291
|
-
model: String(model || ""), prompt_chars: currentPrompt.length });
|
|
292
|
-
const result = await runUfooAgent({ projectRoot, prompt: currentPrompt, provider, model,
|
|
293
|
-
...ufooAgentOptions, loopRuntime: { enabled: true, round: rounds, maxRounds: options.maxRounds,
|
|
294
|
-
maxToolCalls: options.maxToolCalls, remainingToolCalls: Math.max(options.maxToolCalls - toolCalls, 0) } });
|
|
295
|
-
const metrics = extractModelMetrics(result);
|
|
296
|
-
const latency = metrics.latency_ms || Math.max(0, now() - started);
|
|
297
|
-
totalTokens += metrics.input_tokens + metrics.output_tokens;
|
|
298
|
-
totalLatencyMs += latency;
|
|
299
|
-
observer.emit("model_call", { round: rounds, provider: String(provider || ""), model: String(model || ""),
|
|
300
|
-
...metrics, latency_ms: latency, ok: result && result.ok === true,
|
|
301
|
-
tool_call_count: result && result.payload && result.payload.tool_call ? 1 : 0,
|
|
302
|
-
error: result && result.ok === false ? String(result.error || "") : "" });
|
|
303
|
-
observer.emit("model_call_finished", { round: rounds, ok: result && result.ok === true,
|
|
304
|
-
error: result && result.ok === false ? String(result.error || "") : "" });
|
|
305
|
-
if (result && result.ok) lastPayload = normalizePayload(result.payload);
|
|
306
|
-
return result;
|
|
307
|
-
} });
|
|
308
|
-
const runtime = createAgentRuntime({ profile: { capabilities: ["controller"] }, transport, host,
|
|
309
|
-
capabilities: [createCoordinationCapability({ host, id: "controller", policy })] });
|
|
310
|
-
let run;
|
|
311
|
-
try {
|
|
312
|
-
run = await runtime.run({ workspaceRoot: projectRoot, prompt, model: model || "controller",
|
|
313
|
-
toolBudget: { maxToolErrors: Infinity } });
|
|
314
|
-
} catch (error) {
|
|
315
|
-
const payload = terminate(TERMINAL_REASONS.PROVIDER_ERROR, lastPayload, rounds);
|
|
316
|
-
return { ok: false, error: error.message, payload };
|
|
317
|
-
}
|
|
318
|
-
const payload = run.stopReason
|
|
319
|
-
? terminate(run.stopReason, lastPayload, rounds)
|
|
320
|
-
: { ...lastPayload, loop: { terminal_reason: TERMINAL_REASONS.FINAL_ANSWER, rounds,
|
|
321
|
-
tool_calls: toolCalls, tool_errors: toolErrors, ...totals() } };
|
|
322
|
-
if (!run.stopReason) observer.emit("loop_terminal", payload.loop);
|
|
323
|
-
return finalizeLoopRun({ projectRoot, payload, prompt: currentPrompt, processManager,
|
|
324
|
-
dispatchMessages, handleOps, markPending, finalizeLocally });
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
module.exports = {
|
|
328
|
-
DEFAULT_LOOP_OPTIONS,
|
|
329
|
-
FALLBACK_USED_VALUES,
|
|
330
|
-
TERMINAL_REASONS,
|
|
331
|
-
buildLoopContinuationPrompt,
|
|
332
|
-
normalizeFallbackUsed,
|
|
333
|
-
normalizeTerminalReason,
|
|
334
|
-
resolveLoopRuntimeOptions,
|
|
335
|
-
runPromptWithControllerLoop,
|
|
336
|
-
};
|