@ouro.bot/cli 0.0.1-alpha.0 → 0.1.0-alpha.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/AdoptionSpecialist.ouro/agent.json +20 -0
- package/AdoptionSpecialist.ouro/psyche/SOUL.md +22 -0
- package/AdoptionSpecialist.ouro/psyche/identities/basilisk.md +31 -0
- package/AdoptionSpecialist.ouro/psyche/identities/jafar.md +31 -0
- package/AdoptionSpecialist.ouro/psyche/identities/jormungandr.md +31 -0
- package/AdoptionSpecialist.ouro/psyche/identities/kaa.md +31 -0
- package/AdoptionSpecialist.ouro/psyche/identities/medusa.md +31 -0
- package/AdoptionSpecialist.ouro/psyche/identities/monty.md +31 -0
- package/AdoptionSpecialist.ouro/psyche/identities/nagini.md +31 -0
- package/AdoptionSpecialist.ouro/psyche/identities/ouroboros.md +31 -0
- package/AdoptionSpecialist.ouro/psyche/identities/python.md +31 -0
- package/AdoptionSpecialist.ouro/psyche/identities/quetzalcoatl.md +31 -0
- package/AdoptionSpecialist.ouro/psyche/identities/sir-hiss.md +31 -0
- package/AdoptionSpecialist.ouro/psyche/identities/the-serpent.md +31 -0
- package/AdoptionSpecialist.ouro/psyche/identities/the-snake.md +31 -0
- package/README.md +224 -6
- package/dist/heart/agent-entry.js +17 -0
- package/dist/heart/api-error.js +34 -0
- package/dist/heart/config.js +296 -0
- package/dist/heart/core.js +515 -0
- package/dist/heart/daemon/daemon-cli.js +675 -0
- package/dist/heart/daemon/daemon-entry.js +74 -0
- package/dist/heart/daemon/daemon.js +313 -0
- package/dist/heart/daemon/hatch-flow.js +285 -0
- package/dist/heart/daemon/hatch-specialist.js +107 -0
- package/dist/heart/daemon/health-monitor.js +79 -0
- package/dist/heart/daemon/log-tailer.js +146 -0
- package/dist/heart/daemon/message-router.js +98 -0
- package/dist/heart/daemon/os-cron.js +260 -0
- package/dist/heart/daemon/ouro-bot-entry.js +23 -0
- package/dist/heart/daemon/ouro-bot-wrapper.js +90 -0
- package/dist/heart/daemon/ouro-entry.js +23 -0
- package/dist/heart/daemon/ouro-uti.js +212 -0
- package/dist/heart/daemon/process-manager.js +237 -0
- package/dist/heart/daemon/runtime-logging.js +98 -0
- package/dist/heart/daemon/subagent-installer.js +125 -0
- package/dist/heart/daemon/task-scheduler.js +240 -0
- package/dist/heart/harness.js +26 -0
- package/dist/heart/identity.js +281 -0
- package/dist/heart/kicks.js +144 -0
- package/dist/heart/primitives.js +4 -0
- package/dist/heart/providers/anthropic.js +329 -0
- package/dist/heart/providers/azure.js +66 -0
- package/dist/heart/providers/minimax.js +53 -0
- package/dist/heart/providers/openai-codex.js +162 -0
- package/dist/heart/streaming.js +412 -0
- package/dist/heart/turn-coordinator.js +62 -0
- package/dist/inner-worker-entry.js +4 -0
- package/dist/mind/associative-recall.js +197 -0
- package/dist/mind/bundle-manifest.js +118 -0
- package/dist/mind/context.js +302 -0
- package/dist/mind/first-impressions.js +43 -0
- package/dist/mind/format.js +56 -0
- package/dist/mind/friends/channel.js +41 -0
- package/dist/mind/friends/resolver.js +84 -0
- package/dist/mind/friends/store-file.js +171 -0
- package/dist/mind/friends/store.js +4 -0
- package/dist/mind/friends/tokens.js +26 -0
- package/dist/mind/friends/types.js +21 -0
- package/dist/mind/memory.js +388 -0
- package/dist/mind/pending.js +93 -0
- package/dist/mind/phrases.js +43 -0
- package/dist/mind/prompt-refresh.js +20 -0
- package/dist/mind/prompt.js +352 -0
- package/dist/mind/token-estimate.js +119 -0
- package/dist/nerves/cli-logging.js +31 -0
- package/dist/nerves/coverage/audit-rules.js +81 -0
- package/dist/nerves/coverage/audit.js +200 -0
- package/dist/nerves/coverage/cli-main.js +5 -0
- package/dist/nerves/coverage/cli.js +51 -0
- package/dist/nerves/coverage/contract.js +23 -0
- package/dist/nerves/coverage/file-completeness.js +56 -0
- package/dist/nerves/coverage/run-artifacts.js +77 -0
- package/dist/nerves/coverage/source-scanner.js +34 -0
- package/dist/nerves/index.js +152 -0
- package/dist/nerves/runtime.js +38 -0
- package/dist/repertoire/ado-client.js +211 -0
- package/dist/repertoire/ado-context.js +73 -0
- package/dist/repertoire/ado-semantic.js +841 -0
- package/dist/repertoire/ado-templates.js +146 -0
- package/dist/repertoire/coding/index.js +36 -0
- package/dist/repertoire/coding/manager.js +489 -0
- package/dist/repertoire/coding/monitor.js +60 -0
- package/dist/repertoire/coding/reporter.js +45 -0
- package/dist/repertoire/coding/spawner.js +102 -0
- package/dist/repertoire/coding/tools.js +167 -0
- package/dist/repertoire/coding/types.js +2 -0
- package/dist/repertoire/data/ado-endpoints.json +122 -0
- package/dist/repertoire/data/graph-endpoints.json +212 -0
- package/dist/repertoire/github-client.js +64 -0
- package/dist/repertoire/graph-client.js +118 -0
- package/dist/repertoire/skills.js +156 -0
- package/dist/repertoire/tasks/board.js +122 -0
- package/dist/repertoire/tasks/index.js +210 -0
- package/dist/repertoire/tasks/lifecycle.js +80 -0
- package/dist/repertoire/tasks/middleware.js +65 -0
- package/dist/repertoire/tasks/parser.js +173 -0
- package/dist/repertoire/tasks/scanner.js +132 -0
- package/dist/repertoire/tasks/transitions.js +145 -0
- package/dist/repertoire/tasks/types.js +2 -0
- package/dist/repertoire/tools-base.js +714 -0
- package/dist/repertoire/tools-github.js +53 -0
- package/dist/repertoire/tools-teams.js +308 -0
- package/dist/repertoire/tools.js +199 -0
- package/dist/senses/cli-entry.js +15 -0
- package/dist/senses/cli.js +604 -0
- package/dist/senses/commands.js +98 -0
- package/dist/senses/inner-dialog-worker.js +61 -0
- package/dist/senses/inner-dialog.js +231 -0
- package/dist/senses/session-lock.js +119 -0
- package/dist/senses/teams-entry.js +15 -0
- package/dist/senses/teams.js +696 -0
- package/dist/senses/trust-gate.js +150 -0
- package/package.json +34 -11
- package/subagents/README.md +73 -0
- package/subagents/work-doer.md +233 -0
- package/subagents/work-merger.md +624 -0
- package/subagents/work-planner.md +373 -0
- package/bin/ouro.js +0 -6
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hasToolIntent = exports.buildSystem = exports.toResponsesTools = exports.toResponsesInput = exports.streamResponsesApi = exports.streamChatCompletion = exports.getToolsForChannel = exports.summarizeArgs = exports.execTool = exports.tools = void 0;
|
|
4
|
+
exports.createProviderRegistry = createProviderRegistry;
|
|
5
|
+
exports.getModel = getModel;
|
|
6
|
+
exports.getProvider = getProvider;
|
|
7
|
+
exports.createSummarize = createSummarize;
|
|
8
|
+
exports.getProviderDisplayLabel = getProviderDisplayLabel;
|
|
9
|
+
exports.stripLastToolCalls = stripLastToolCalls;
|
|
10
|
+
exports.isTransientError = isTransientError;
|
|
11
|
+
exports.classifyTransientError = classifyTransientError;
|
|
12
|
+
exports.runAgent = runAgent;
|
|
13
|
+
const config_1 = require("./config");
|
|
14
|
+
const identity_1 = require("./identity");
|
|
15
|
+
const tools_1 = require("../repertoire/tools");
|
|
16
|
+
const channel_1 = require("../mind/friends/channel");
|
|
17
|
+
// Kick detection preserved but disabled — see comment in agent loop below.
|
|
18
|
+
// import { detectKick } from "./kicks";
|
|
19
|
+
// import type { KickReason } from "./kicks";
|
|
20
|
+
const runtime_1 = require("../nerves/runtime");
|
|
21
|
+
const context_1 = require("../mind/context");
|
|
22
|
+
const prompt_1 = require("../mind/prompt");
|
|
23
|
+
const associative_recall_1 = require("../mind/associative-recall");
|
|
24
|
+
const anthropic_1 = require("./providers/anthropic");
|
|
25
|
+
const azure_1 = require("./providers/azure");
|
|
26
|
+
const minimax_1 = require("./providers/minimax");
|
|
27
|
+
const openai_codex_1 = require("./providers/openai-codex");
|
|
28
|
+
let _providerRuntime = null;
|
|
29
|
+
function createProviderRegistry() {
|
|
30
|
+
const factories = {
|
|
31
|
+
azure: azure_1.createAzureProviderRuntime,
|
|
32
|
+
anthropic: anthropic_1.createAnthropicProviderRuntime,
|
|
33
|
+
minimax: minimax_1.createMinimaxProviderRuntime,
|
|
34
|
+
"openai-codex": openai_codex_1.createOpenAICodexProviderRuntime,
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
resolve() {
|
|
38
|
+
const provider = (0, identity_1.loadAgentConfig)().provider;
|
|
39
|
+
return factories[provider]();
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function getProviderRuntime() {
|
|
44
|
+
if (!_providerRuntime) {
|
|
45
|
+
try {
|
|
46
|
+
_providerRuntime = createProviderRegistry().resolve();
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
50
|
+
(0, runtime_1.emitNervesEvent)({
|
|
51
|
+
level: "error",
|
|
52
|
+
event: "engine.provider_init_error",
|
|
53
|
+
component: "engine",
|
|
54
|
+
message: msg,
|
|
55
|
+
meta: {},
|
|
56
|
+
});
|
|
57
|
+
// eslint-disable-next-line no-console -- pre-boot guard: provider init failure
|
|
58
|
+
console.error(`\n[fatal] ${msg}\n`);
|
|
59
|
+
process.exit(1);
|
|
60
|
+
throw new Error("unreachable");
|
|
61
|
+
}
|
|
62
|
+
if (!_providerRuntime) {
|
|
63
|
+
(0, runtime_1.emitNervesEvent)({
|
|
64
|
+
level: "error",
|
|
65
|
+
event: "engine.provider_init_error",
|
|
66
|
+
component: "engine",
|
|
67
|
+
message: "provider runtime could not be initialized.",
|
|
68
|
+
meta: {},
|
|
69
|
+
});
|
|
70
|
+
process.exit(1);
|
|
71
|
+
throw new Error("unreachable");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return _providerRuntime;
|
|
75
|
+
}
|
|
76
|
+
function getModel() {
|
|
77
|
+
return getProviderRuntime().model;
|
|
78
|
+
}
|
|
79
|
+
function getProvider() {
|
|
80
|
+
return getProviderRuntime().id;
|
|
81
|
+
}
|
|
82
|
+
function createSummarize() {
|
|
83
|
+
return async (transcript, instruction) => {
|
|
84
|
+
const runtime = getProviderRuntime();
|
|
85
|
+
const client = runtime.client;
|
|
86
|
+
const response = await client.chat.completions.create({
|
|
87
|
+
model: runtime.model,
|
|
88
|
+
messages: [
|
|
89
|
+
{ role: "system", content: instruction },
|
|
90
|
+
{ role: "user", content: transcript },
|
|
91
|
+
],
|
|
92
|
+
max_tokens: 500,
|
|
93
|
+
});
|
|
94
|
+
return response.choices?.[0]?.message?.content ?? transcript;
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function getProviderDisplayLabel() {
|
|
98
|
+
const model = getModel();
|
|
99
|
+
const providerLabelBuilders = {
|
|
100
|
+
azure: () => `azure openai (${(0, config_1.getAzureConfig)().deployment || "default"}, model: ${model})`,
|
|
101
|
+
anthropic: () => `anthropic (${model})`,
|
|
102
|
+
minimax: () => `minimax (${model})`,
|
|
103
|
+
"openai-codex": () => `openai codex (${model})`,
|
|
104
|
+
};
|
|
105
|
+
return providerLabelBuilders[getProvider()]();
|
|
106
|
+
}
|
|
107
|
+
// Re-export tools, execTool, summarizeArgs from ./tools for backward compat
|
|
108
|
+
var tools_2 = require("../repertoire/tools");
|
|
109
|
+
Object.defineProperty(exports, "tools", { enumerable: true, get: function () { return tools_2.tools; } });
|
|
110
|
+
Object.defineProperty(exports, "execTool", { enumerable: true, get: function () { return tools_2.execTool; } });
|
|
111
|
+
Object.defineProperty(exports, "summarizeArgs", { enumerable: true, get: function () { return tools_2.summarizeArgs; } });
|
|
112
|
+
Object.defineProperty(exports, "getToolsForChannel", { enumerable: true, get: function () { return tools_2.getToolsForChannel; } });
|
|
113
|
+
// Re-export streaming functions for backward compat
|
|
114
|
+
var streaming_1 = require("./streaming");
|
|
115
|
+
Object.defineProperty(exports, "streamChatCompletion", { enumerable: true, get: function () { return streaming_1.streamChatCompletion; } });
|
|
116
|
+
Object.defineProperty(exports, "streamResponsesApi", { enumerable: true, get: function () { return streaming_1.streamResponsesApi; } });
|
|
117
|
+
Object.defineProperty(exports, "toResponsesInput", { enumerable: true, get: function () { return streaming_1.toResponsesInput; } });
|
|
118
|
+
Object.defineProperty(exports, "toResponsesTools", { enumerable: true, get: function () { return streaming_1.toResponsesTools; } });
|
|
119
|
+
// Re-export prompt functions for backward compat
|
|
120
|
+
var prompt_2 = require("../mind/prompt");
|
|
121
|
+
Object.defineProperty(exports, "buildSystem", { enumerable: true, get: function () { return prompt_2.buildSystem; } });
|
|
122
|
+
// Re-export kick utilities for backward compat
|
|
123
|
+
var kicks_1 = require("./kicks");
|
|
124
|
+
Object.defineProperty(exports, "hasToolIntent", { enumerable: true, get: function () { return kicks_1.hasToolIntent; } });
|
|
125
|
+
function upsertSystemPrompt(messages, systemText) {
|
|
126
|
+
const systemMessage = { role: "system", content: systemText };
|
|
127
|
+
if (messages[0]?.role === "system") {
|
|
128
|
+
messages[0] = systemMessage;
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
messages.unshift(systemMessage);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// Remove orphan tool_calls from the last assistant message and any
|
|
135
|
+
// trailing tool-result messages that lack a matching tool_call.
|
|
136
|
+
// This keeps the conversation valid after an abort or tool-loop limit.
|
|
137
|
+
function stripLastToolCalls(messages) {
|
|
138
|
+
// Pop any trailing tool-result messages
|
|
139
|
+
while (messages.length && messages[messages.length - 1].role === "tool") {
|
|
140
|
+
messages.pop();
|
|
141
|
+
}
|
|
142
|
+
// Strip tool_calls from the last assistant message
|
|
143
|
+
const last = messages[messages.length - 1];
|
|
144
|
+
if (last?.role === "assistant") {
|
|
145
|
+
const asst = last;
|
|
146
|
+
if (asst.tool_calls) {
|
|
147
|
+
delete asst.tool_calls;
|
|
148
|
+
// If the assistant message is now empty, remove it entirely
|
|
149
|
+
if (!asst.content)
|
|
150
|
+
messages.pop();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// Detect context overflow errors from Azure or MiniMax
|
|
155
|
+
function isContextOverflow(err) {
|
|
156
|
+
if (!(err instanceof Error))
|
|
157
|
+
return false;
|
|
158
|
+
const code = err.code;
|
|
159
|
+
const msg = err.message || "";
|
|
160
|
+
if (code === "context_length_exceeded")
|
|
161
|
+
return true;
|
|
162
|
+
if (msg.includes("context_length_exceeded"))
|
|
163
|
+
return true;
|
|
164
|
+
if (msg.includes("context window exceeds limit"))
|
|
165
|
+
return true;
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
// Detect transient network errors worth retrying
|
|
169
|
+
function isTransientError(err) {
|
|
170
|
+
if (!(err instanceof Error))
|
|
171
|
+
return false;
|
|
172
|
+
const msg = err.message || "";
|
|
173
|
+
const code = err.code || "";
|
|
174
|
+
// Node.js network error codes
|
|
175
|
+
if (["ECONNRESET", "ECONNREFUSED", "ENOTFOUND", "ETIMEDOUT", "EPIPE",
|
|
176
|
+
"EAI_AGAIN", "EHOSTUNREACH", "ENETUNREACH", "ECONNABORTED"].includes(code))
|
|
177
|
+
return true;
|
|
178
|
+
// OpenAI SDK / fetch errors
|
|
179
|
+
if (msg.includes("fetch failed"))
|
|
180
|
+
return true;
|
|
181
|
+
if (msg.includes("network") && !msg.includes("context"))
|
|
182
|
+
return true;
|
|
183
|
+
if (msg.includes("ECONNRESET") || msg.includes("ETIMEDOUT"))
|
|
184
|
+
return true;
|
|
185
|
+
if (msg.includes("socket hang up"))
|
|
186
|
+
return true;
|
|
187
|
+
if (msg.includes("getaddrinfo"))
|
|
188
|
+
return true;
|
|
189
|
+
// HTTP 429 / 500 / 502 / 503 / 504
|
|
190
|
+
const status = err.status;
|
|
191
|
+
if (status === 429 || status === 500 || status === 502 || status === 503 || status === 504)
|
|
192
|
+
return true;
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
function classifyTransientError(err) {
|
|
196
|
+
if (!(err instanceof Error))
|
|
197
|
+
return "unknown error";
|
|
198
|
+
const status = err.status;
|
|
199
|
+
if (status === 429)
|
|
200
|
+
return "rate limited";
|
|
201
|
+
if (status === 401 || status === 403)
|
|
202
|
+
return "auth error";
|
|
203
|
+
if (status && status >= 500)
|
|
204
|
+
return "server error";
|
|
205
|
+
return "network error";
|
|
206
|
+
}
|
|
207
|
+
const MAX_RETRIES = 3;
|
|
208
|
+
const RETRY_BASE_MS = 2000;
|
|
209
|
+
async function runAgent(messages, callbacks, channel, signal, options) {
|
|
210
|
+
const providerRuntime = getProviderRuntime();
|
|
211
|
+
const provider = providerRuntime.id;
|
|
212
|
+
const toolChoiceRequired = options?.toolChoiceRequired ?? true;
|
|
213
|
+
const traceId = options?.traceId;
|
|
214
|
+
(0, runtime_1.emitNervesEvent)({
|
|
215
|
+
event: "engine.turn_start",
|
|
216
|
+
trace_id: traceId,
|
|
217
|
+
component: "engine",
|
|
218
|
+
message: "runAgent turn started",
|
|
219
|
+
meta: { channel: channel ?? "unknown", provider },
|
|
220
|
+
});
|
|
221
|
+
// Per-turn friend refresh: re-read friend record from disk for fresh context
|
|
222
|
+
const friendStore = options?.toolContext?.friendStore;
|
|
223
|
+
const friendId = options?.toolContext?.context?.friend?.id;
|
|
224
|
+
let currentContext = options?.toolContext?.context;
|
|
225
|
+
if (friendStore && friendId) {
|
|
226
|
+
const freshFriend = await friendStore.get(friendId);
|
|
227
|
+
if (freshFriend) {
|
|
228
|
+
currentContext = { ...currentContext, friend: freshFriend };
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
// Refresh system prompt at start of each turn when channel is provided.
|
|
232
|
+
// If refresh fails, keep existing system prompt (or inject a minimal safe fallback)
|
|
233
|
+
// so turn execution remains consistent and non-fatal.
|
|
234
|
+
if (channel) {
|
|
235
|
+
try {
|
|
236
|
+
const refreshed = await (0, prompt_1.buildSystem)(channel, options, currentContext);
|
|
237
|
+
upsertSystemPrompt(messages, refreshed);
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
const hadExistingSystemPrompt = messages[0]?.role === "system" && typeof messages[0].content === "string";
|
|
241
|
+
const existingSystemText = hadExistingSystemPrompt ? messages[0].content : undefined;
|
|
242
|
+
const fallback = existingSystemText ?? "You are a helpful assistant.";
|
|
243
|
+
upsertSystemPrompt(messages, fallback);
|
|
244
|
+
(0, runtime_1.emitNervesEvent)({
|
|
245
|
+
level: "warn",
|
|
246
|
+
event: "mind.step_error",
|
|
247
|
+
trace_id: traceId,
|
|
248
|
+
component: "mind",
|
|
249
|
+
message: "buildSystem refresh failed; using fallback prompt",
|
|
250
|
+
meta: {
|
|
251
|
+
channel,
|
|
252
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
253
|
+
used_existing_prompt: hadExistingSystemPrompt,
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
await (0, associative_recall_1.injectAssociativeRecall)(messages);
|
|
259
|
+
// kickCount and lastKickReason preserved but unused while kick detection is disabled.
|
|
260
|
+
// let kickCount = 0;
|
|
261
|
+
// let lastKickReason: KickReason | null = null;
|
|
262
|
+
let done = false;
|
|
263
|
+
let lastUsage;
|
|
264
|
+
let overflowRetried = false;
|
|
265
|
+
let retryCount = 0;
|
|
266
|
+
// Prevent MaxListenersExceeded warning — each iteration adds a listener
|
|
267
|
+
try {
|
|
268
|
+
require("events").setMaxListeners(50, signal);
|
|
269
|
+
}
|
|
270
|
+
catch { /* unsupported */ }
|
|
271
|
+
const toolPreferences = currentContext?.friend?.toolPreferences;
|
|
272
|
+
const baseTools = (0, tools_1.getToolsForChannel)(channel ? (0, channel_1.getChannelCapabilities)(channel) : undefined, toolPreferences && Object.keys(toolPreferences).length > 0 ? toolPreferences : undefined);
|
|
273
|
+
// Rebase provider-owned turn state from canonical messages at user-turn start.
|
|
274
|
+
// This prevents stale provider caches from replaying prior-turn context.
|
|
275
|
+
providerRuntime.resetTurnState(messages);
|
|
276
|
+
while (!done) {
|
|
277
|
+
// When toolChoiceRequired is true (the default), include final_answer
|
|
278
|
+
// so the model can signal completion. With tool_choice: required, the
|
|
279
|
+
// model must call a tool every turn — final_answer is how it exits.
|
|
280
|
+
// Overridable via options.toolChoiceRequired = false (e.g. CLI).
|
|
281
|
+
const activeTools = toolChoiceRequired ? [...baseTools, tools_1.finalAnswerTool] : baseTools;
|
|
282
|
+
const steeringFollowUps = options?.drainSteeringFollowUps?.() ?? [];
|
|
283
|
+
if (steeringFollowUps.length > 0) {
|
|
284
|
+
for (const followUp of steeringFollowUps) {
|
|
285
|
+
messages.push({ role: "user", content: followUp.text });
|
|
286
|
+
}
|
|
287
|
+
providerRuntime.resetTurnState(messages);
|
|
288
|
+
}
|
|
289
|
+
// Yield so pending I/O (stdin Ctrl-C) can be processed between iterations
|
|
290
|
+
await new Promise((r) => setImmediate(r));
|
|
291
|
+
if (signal?.aborted)
|
|
292
|
+
break;
|
|
293
|
+
try {
|
|
294
|
+
callbacks.onModelStart();
|
|
295
|
+
const result = await providerRuntime.streamTurn({
|
|
296
|
+
messages,
|
|
297
|
+
activeTools,
|
|
298
|
+
callbacks,
|
|
299
|
+
signal,
|
|
300
|
+
traceId,
|
|
301
|
+
toolChoiceRequired,
|
|
302
|
+
});
|
|
303
|
+
// Track usage from the latest API call
|
|
304
|
+
if (result.usage)
|
|
305
|
+
lastUsage = result.usage;
|
|
306
|
+
retryCount = 0; // reset on success
|
|
307
|
+
// SHARED: build CC-format assistant message from TurnResult
|
|
308
|
+
const msg = {
|
|
309
|
+
role: "assistant",
|
|
310
|
+
};
|
|
311
|
+
if (result.content)
|
|
312
|
+
msg.content = result.content;
|
|
313
|
+
if (result.toolCalls.length)
|
|
314
|
+
msg.tool_calls = result.toolCalls.map((tc) => ({
|
|
315
|
+
id: tc.id,
|
|
316
|
+
type: "function",
|
|
317
|
+
function: { name: tc.name, arguments: tc.arguments },
|
|
318
|
+
}));
|
|
319
|
+
// Store reasoning items from the API response on the assistant message
|
|
320
|
+
// so they persist through session save/load and can be restored in toResponsesInput
|
|
321
|
+
const reasoningItems = result.outputItems.filter((item) => "type" in item && item.type === "reasoning");
|
|
322
|
+
if (reasoningItems.length > 0) {
|
|
323
|
+
msg._reasoning_items = reasoningItems;
|
|
324
|
+
}
|
|
325
|
+
if (!result.toolCalls.length) {
|
|
326
|
+
// Kick detection is disabled while tool_choice: required + final_answer
|
|
327
|
+
// is the primary loop control mechanism. The model should never reach
|
|
328
|
+
// this path (tool_choice: required forces a tool call), but if it does,
|
|
329
|
+
// accept the response as-is rather than risk false-positive kicks.
|
|
330
|
+
//
|
|
331
|
+
// Preserved for future use — re-enable by uncommenting:
|
|
332
|
+
// const kick = detectKick(result.content, options);
|
|
333
|
+
// if (kick) {
|
|
334
|
+
// kickCount++;
|
|
335
|
+
// lastKickReason = kick.reason;
|
|
336
|
+
// callbacks.onKick?.();
|
|
337
|
+
// const kickContent = result.content
|
|
338
|
+
// ? result.content + "\n\n" + kick.message
|
|
339
|
+
// : kick.message;
|
|
340
|
+
// messages.push({ role: "assistant", content: kickContent });
|
|
341
|
+
// providerRuntime.resetTurnState(messages);
|
|
342
|
+
// continue;
|
|
343
|
+
// }
|
|
344
|
+
messages.push(msg);
|
|
345
|
+
done = true;
|
|
346
|
+
}
|
|
347
|
+
else {
|
|
348
|
+
// Check for final_answer sole call: intercept before tool execution
|
|
349
|
+
const isSoleFinalAnswer = result.toolCalls.length === 1 && result.toolCalls[0].name === "final_answer";
|
|
350
|
+
if (isSoleFinalAnswer) {
|
|
351
|
+
// Extract answer from the tool call arguments.
|
|
352
|
+
// Supports: {"answer":"text"}, "text" (JSON string), retry on failure.
|
|
353
|
+
let answer;
|
|
354
|
+
try {
|
|
355
|
+
const parsed = JSON.parse(result.toolCalls[0].arguments);
|
|
356
|
+
if (typeof parsed === "string") {
|
|
357
|
+
answer = parsed;
|
|
358
|
+
}
|
|
359
|
+
else if (parsed.answer != null) {
|
|
360
|
+
answer = parsed.answer;
|
|
361
|
+
}
|
|
362
|
+
// else: valid JSON but no answer field — answer stays undefined (retry)
|
|
363
|
+
}
|
|
364
|
+
catch {
|
|
365
|
+
// JSON parsing failed (e.g. truncated output) — answer stays undefined (retry)
|
|
366
|
+
}
|
|
367
|
+
if (answer != null) {
|
|
368
|
+
if (result.finalAnswerStreamed) {
|
|
369
|
+
// The streaming layer already parsed and emitted the answer
|
|
370
|
+
// progressively via FinalAnswerParser. Skip clearing and
|
|
371
|
+
// re-emitting to avoid double-delivery.
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
// Clear any streamed noise (e.g. refusal text) before emitting.
|
|
375
|
+
callbacks.onClearText?.();
|
|
376
|
+
// Emit the answer through the callback pipeline so channels receive it.
|
|
377
|
+
// Never truncate -- channel adapters handle splitting long messages.
|
|
378
|
+
callbacks.onTextChunk(answer);
|
|
379
|
+
}
|
|
380
|
+
// Keep the full assistant message (with tool_calls) for debuggability,
|
|
381
|
+
// plus a synthetic tool response so the conversation stays valid on resume.
|
|
382
|
+
messages.push(msg);
|
|
383
|
+
messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: "(delivered)" });
|
|
384
|
+
providerRuntime.appendToolOutput(result.toolCalls[0].id, "(delivered)");
|
|
385
|
+
done = true;
|
|
386
|
+
}
|
|
387
|
+
else {
|
|
388
|
+
// Answer is undefined -- the model's final_answer was incomplete or
|
|
389
|
+
// malformed. Clear any partial streamed text or noise, then push the
|
|
390
|
+
// assistant msg + error tool result and let the model try again.
|
|
391
|
+
callbacks.onClearText?.();
|
|
392
|
+
const retryError = "your final_answer was incomplete or malformed. call final_answer again with your complete response.";
|
|
393
|
+
messages.push(msg);
|
|
394
|
+
messages.push({ role: "tool", tool_call_id: result.toolCalls[0].id, content: retryError });
|
|
395
|
+
providerRuntime.appendToolOutput(result.toolCalls[0].id, retryError);
|
|
396
|
+
}
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
messages.push(msg);
|
|
400
|
+
// SHARED: execute tools (final_answer in mixed calls is rejected inline)
|
|
401
|
+
for (const tc of result.toolCalls) {
|
|
402
|
+
if (signal?.aborted)
|
|
403
|
+
break;
|
|
404
|
+
// Intercept final_answer in mixed call: reject it
|
|
405
|
+
if (tc.name === "final_answer") {
|
|
406
|
+
const rejection = "rejected: final_answer must be the only tool call. Finish your work first, then call final_answer alone.";
|
|
407
|
+
messages.push({ role: "tool", tool_call_id: tc.id, content: rejection });
|
|
408
|
+
providerRuntime.appendToolOutput(tc.id, rejection);
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
let args = {};
|
|
412
|
+
try {
|
|
413
|
+
args = JSON.parse(tc.arguments);
|
|
414
|
+
}
|
|
415
|
+
catch {
|
|
416
|
+
/* ignore */
|
|
417
|
+
}
|
|
418
|
+
const argSummary = (0, tools_1.summarizeArgs)(tc.name, args);
|
|
419
|
+
// Confirmation check for mutate tools
|
|
420
|
+
if ((0, tools_1.isConfirmationRequired)(tc.name) && !options?.skipConfirmation) {
|
|
421
|
+
let decision = "denied";
|
|
422
|
+
if (callbacks.onConfirmAction) {
|
|
423
|
+
decision = await callbacks.onConfirmAction(tc.name, args);
|
|
424
|
+
}
|
|
425
|
+
if (decision !== "confirmed") {
|
|
426
|
+
const cancelled = "Action cancelled by user.";
|
|
427
|
+
callbacks.onToolStart(tc.name, args);
|
|
428
|
+
callbacks.onToolEnd(tc.name, argSummary, false);
|
|
429
|
+
messages.push({ role: "tool", tool_call_id: tc.id, content: cancelled });
|
|
430
|
+
providerRuntime.appendToolOutput(tc.id, cancelled);
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
callbacks.onToolStart(tc.name, args);
|
|
435
|
+
let toolResult;
|
|
436
|
+
let success;
|
|
437
|
+
try {
|
|
438
|
+
toolResult = await (0, tools_1.execTool)(tc.name, args, options?.toolContext);
|
|
439
|
+
success = true;
|
|
440
|
+
}
|
|
441
|
+
catch (e) {
|
|
442
|
+
toolResult = `error: ${e}`;
|
|
443
|
+
success = false;
|
|
444
|
+
}
|
|
445
|
+
callbacks.onToolEnd(tc.name, argSummary, success);
|
|
446
|
+
messages.push({ role: "tool", tool_call_id: tc.id, content: toolResult });
|
|
447
|
+
providerRuntime.appendToolOutput(tc.id, toolResult);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
catch (e) {
|
|
452
|
+
// Abort is not an error — just stop cleanly
|
|
453
|
+
if (signal?.aborted) {
|
|
454
|
+
stripLastToolCalls(messages);
|
|
455
|
+
break;
|
|
456
|
+
}
|
|
457
|
+
// Context overflow: trim aggressively and retry once
|
|
458
|
+
if (isContextOverflow(e) && !overflowRetried) {
|
|
459
|
+
overflowRetried = true;
|
|
460
|
+
stripLastToolCalls(messages);
|
|
461
|
+
const { maxTokens, contextMargin } = (0, config_1.getContextConfig)();
|
|
462
|
+
const trimmed = (0, context_1.trimMessages)(messages, maxTokens, contextMargin, maxTokens * 2);
|
|
463
|
+
messages.splice(0, messages.length, ...trimmed);
|
|
464
|
+
providerRuntime.resetTurnState(messages);
|
|
465
|
+
callbacks.onError(new Error("context trimmed, retrying..."), "transient");
|
|
466
|
+
continue;
|
|
467
|
+
}
|
|
468
|
+
// Transient errors: retry with exponential backoff
|
|
469
|
+
if (isTransientError(e) && retryCount < MAX_RETRIES) {
|
|
470
|
+
retryCount++;
|
|
471
|
+
const delay = RETRY_BASE_MS * Math.pow(2, retryCount - 1);
|
|
472
|
+
const cause = classifyTransientError(e);
|
|
473
|
+
callbacks.onError(new Error(`${cause}, retrying in ${delay / 1000}s (${retryCount}/${MAX_RETRIES})...`), "transient");
|
|
474
|
+
// Wait with abort support
|
|
475
|
+
const aborted = await new Promise((resolve) => {
|
|
476
|
+
const timer = setTimeout(() => resolve(false), delay);
|
|
477
|
+
if (signal) {
|
|
478
|
+
const onAbort = () => { clearTimeout(timer); resolve(true); };
|
|
479
|
+
if (signal.aborted) {
|
|
480
|
+
clearTimeout(timer);
|
|
481
|
+
resolve(true);
|
|
482
|
+
return;
|
|
483
|
+
}
|
|
484
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
if (aborted) {
|
|
488
|
+
stripLastToolCalls(messages);
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
providerRuntime.resetTurnState(messages);
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
callbacks.onError(e instanceof Error ? e : new Error(String(e)), "terminal");
|
|
495
|
+
(0, runtime_1.emitNervesEvent)({
|
|
496
|
+
level: "error",
|
|
497
|
+
event: "engine.error",
|
|
498
|
+
trace_id: traceId,
|
|
499
|
+
component: "engine",
|
|
500
|
+
message: e instanceof Error ? e.message : String(e),
|
|
501
|
+
meta: {},
|
|
502
|
+
});
|
|
503
|
+
stripLastToolCalls(messages);
|
|
504
|
+
done = true;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
(0, runtime_1.emitNervesEvent)({
|
|
508
|
+
event: "engine.turn_end",
|
|
509
|
+
trace_id: traceId,
|
|
510
|
+
component: "engine",
|
|
511
|
+
message: "runAgent turn completed",
|
|
512
|
+
meta: { done },
|
|
513
|
+
});
|
|
514
|
+
return { usage: lastUsage };
|
|
515
|
+
}
|