@mcp-abap-adt/llm-agent-libs 33.0.1 → 34.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +67 -22
- package/dist/adapters/llm-adapter.d.ts +2 -2
- package/dist/adapters/llm-adapter.d.ts.map +1 -1
- package/dist/adapters/llm-adapter.js +82 -34
- package/dist/adapters/llm-adapter.js.map +1 -1
- package/dist/adapters/llm-provider-bridge.d.ts +1 -4
- package/dist/adapters/llm-provider-bridge.d.ts.map +1 -1
- package/dist/adapters/llm-provider-bridge.js +16 -13
- package/dist/adapters/llm-provider-bridge.js.map +1 -1
- package/dist/agent/rag-helpers.d.ts +4 -6
- package/dist/agent/rag-helpers.d.ts.map +1 -1
- package/dist/agent/rag-helpers.js +3 -41
- package/dist/agent/rag-helpers.js.map +1 -1
- package/dist/agent/rag-orchestrator-types.d.ts +3 -0
- package/dist/agent/rag-orchestrator-types.d.ts.map +1 -1
- package/dist/agent/rag-orchestrator.d.ts +0 -1
- package/dist/agent/rag-orchestrator.d.ts.map +1 -1
- package/dist/agent/rag-orchestrator.js +9 -29
- package/dist/agent/rag-orchestrator.js.map +1 -1
- package/dist/agent.d.ts +11 -2
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +171 -236
- package/dist/agent.js.map +1 -1
- package/dist/builder-types.d.ts +1 -1
- package/dist/builder-types.d.ts.map +1 -1
- package/dist/builder.d.ts +7 -7
- package/dist/builder.d.ts.map +1 -1
- package/dist/builder.js +8 -17
- package/dist/builder.js.map +1 -1
- package/dist/classifier/llm-classifier.d.ts.map +1 -1
- package/dist/classifier/llm-classifier.js +2 -1
- package/dist/classifier/llm-classifier.js.map +1 -1
- package/dist/config/config-watcher.d.ts +3 -0
- package/dist/config/config-watcher.d.ts.map +1 -1
- package/dist/config/config-watcher.js +1 -0
- package/dist/config/config-watcher.js.map +1 -1
- package/dist/context/context-assembler.d.ts.map +1 -1
- package/dist/context/context-assembler.js +23 -23
- package/dist/context/context-assembler.js.map +1 -1
- package/dist/coordinator/adaptive/compose-step-input.d.ts +1 -5
- package/dist/coordinator/adaptive/compose-step-input.d.ts.map +1 -1
- package/dist/coordinator/adaptive/compose-step-input.js +6 -17
- package/dist/coordinator/adaptive/compose-step-input.js.map +1 -1
- package/dist/coordinator/adaptive/index.d.ts +1 -1
- package/dist/coordinator/adaptive/index.d.ts.map +1 -1
- package/dist/coordinator/adaptive/index.js +1 -1
- package/dist/coordinator/adaptive/index.js.map +1 -1
- package/dist/coordinator/adaptive/llm-adaptive-controller.d.ts.map +1 -1
- package/dist/coordinator/adaptive/llm-adaptive-controller.js +3 -9
- package/dist/coordinator/adaptive/llm-adaptive-controller.js.map +1 -1
- package/dist/coordinator/dag/dag-plan-interpreter.js +1 -1
- package/dist/coordinator/dag/dag-plan-interpreter.js.map +1 -1
- package/dist/coordinator/dialogue/dialogue-session.d.ts +0 -3
- package/dist/coordinator/dialogue/dialogue-session.d.ts.map +1 -1
- package/dist/coordinator/dialogue/dialogue-session.js +15 -59
- package/dist/coordinator/dialogue/dialogue-session.js.map +1 -1
- package/dist/coordinator/dialogue/index.d.ts +2 -2
- package/dist/coordinator/dialogue/index.d.ts.map +1 -1
- package/dist/coordinator/dialogue/index.js +2 -2
- package/dist/coordinator/dialogue/index.js.map +1 -1
- package/dist/coordinator/dialogue/planner-context-builder.d.ts +2 -3
- package/dist/coordinator/dialogue/planner-context-builder.d.ts.map +1 -1
- package/dist/coordinator/dialogue/planner-context-builder.js +13 -73
- package/dist/coordinator/dialogue/planner-context-builder.js.map +1 -1
- package/dist/coordinator/dialogue/planner-tools-client.d.ts.map +1 -1
- package/dist/coordinator/dialogue/planner-tools-client.js +7 -19
- package/dist/coordinator/dialogue/planner-tools-client.js.map +1 -1
- package/dist/coordinator/dialogue/structured-plan.d.ts +1 -1
- package/dist/coordinator/dialogue/structured-plan.d.ts.map +1 -1
- package/dist/coordinator/dialogue/structured-plan.js +6 -7
- package/dist/coordinator/dialogue/structured-plan.js.map +1 -1
- package/dist/coordinator/dialogue/submit-report-tool.d.ts +1 -1
- package/dist/coordinator/dialogue/submit-report-tool.d.ts.map +1 -1
- package/dist/coordinator/dialogue/submit-report-tool.js +4 -17
- package/dist/coordinator/dialogue/submit-report-tool.js.map +1 -1
- package/dist/coordinator/dialogue/task-board.d.ts +0 -1
- package/dist/coordinator/dialogue/task-board.d.ts.map +1 -1
- package/dist/coordinator/dialogue/task-board.js +6 -7
- package/dist/coordinator/dialogue/task-board.js.map +1 -1
- package/dist/coordinator/dialogue/task-reference.d.ts +1 -3
- package/dist/coordinator/dialogue/task-reference.d.ts.map +1 -1
- package/dist/coordinator/dialogue/task-reference.js +3 -10
- package/dist/coordinator/dialogue/task-reference.js.map +1 -1
- package/dist/coordinator/dialogue/tool-trace-render.d.ts.map +1 -1
- package/dist/coordinator/dialogue/tool-trace-render.js +9 -29
- package/dist/coordinator/dialogue/tool-trace-render.js.map +1 -1
- package/dist/coordinator/dispatch/self.d.ts.map +1 -1
- package/dist/coordinator/dispatch/self.js +17 -21
- package/dist/coordinator/dispatch/self.js.map +1 -1
- package/dist/coordinator/planning/replan-on-error.d.ts.map +1 -1
- package/dist/coordinator/planning/replan-on-error.js +1 -1
- package/dist/coordinator/planning/replan-on-error.js.map +1 -1
- package/dist/coordinator/stepper/cyclic-react-executor.d.ts.map +1 -1
- package/dist/coordinator/stepper/cyclic-react-executor.js +35 -34
- package/dist/coordinator/stepper/cyclic-react-executor.js.map +1 -1
- package/dist/coordinator/stepper/logging-llm.d.ts.map +1 -1
- package/dist/coordinator/stepper/logging-llm.js +4 -1
- package/dist/coordinator/stepper/logging-llm.js.map +1 -1
- package/dist/coordinator/stepper/stepper-interpreter.d.ts.map +1 -1
- package/dist/coordinator/stepper/stepper-interpreter.js +5 -17
- package/dist/coordinator/stepper/stepper-interpreter.js.map +1 -1
- package/dist/history/history-memory.d.ts +1 -0
- package/dist/history/history-memory.d.ts.map +1 -1
- package/dist/history/history-memory.js +5 -1
- package/dist/history/history-memory.js.map +1 -1
- package/dist/history/history-summarizer.js +1 -1
- package/dist/history/history-summarizer.js.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/interfaces/pipeline.d.ts +1 -1
- package/dist/interfaces/pipeline.d.ts.map +1 -1
- package/dist/logger/default-request-logger.d.ts.map +1 -1
- package/dist/logger/default-request-logger.js +3 -0
- package/dist/logger/default-request-logger.js.map +1 -1
- package/dist/logger/session-request-logger.d.ts.map +1 -1
- package/dist/logger/session-request-logger.js +8 -1
- package/dist/logger/session-request-logger.js.map +1 -1
- package/dist/pipeline/context/tool-loop-context/window-context-strategy.d.ts +1 -1
- package/dist/pipeline/context/tool-loop-context/window-context-strategy.d.ts.map +1 -1
- package/dist/pipeline/context/tool-loop-context/window-context-strategy.js +6 -3
- package/dist/pipeline/context/tool-loop-context/window-context-strategy.js.map +1 -1
- package/dist/pipeline/context/tool-loop-control-history.d.ts +15 -0
- package/dist/pipeline/context/tool-loop-control-history.d.ts.map +1 -0
- package/dist/pipeline/context/tool-loop-control-history.js +72 -0
- package/dist/pipeline/context/tool-loop-control-history.js.map +1 -0
- package/dist/pipeline/context.d.ts +5 -2
- package/dist/pipeline/context.d.ts.map +1 -1
- package/dist/pipeline/default-pipeline.d.ts +2 -2
- package/dist/pipeline/default-pipeline.d.ts.map +1 -1
- package/dist/pipeline/default-pipeline.js +7 -7
- package/dist/pipeline/default-pipeline.js.map +1 -1
- package/dist/pipeline/executor.d.ts.map +1 -1
- package/dist/pipeline/executor.js +6 -0
- package/dist/pipeline/executor.js.map +1 -1
- package/dist/pipeline/handlers/adaptive-coordinator.d.ts.map +1 -1
- package/dist/pipeline/handlers/adaptive-coordinator.js +9 -6
- package/dist/pipeline/handlers/adaptive-coordinator.js.map +1 -1
- package/dist/pipeline/handlers/assemble.d.ts +0 -9
- package/dist/pipeline/handlers/assemble.d.ts.map +1 -1
- package/dist/pipeline/handlers/assemble.js +7 -11
- package/dist/pipeline/handlers/assemble.js.map +1 -1
- package/dist/pipeline/handlers/dag-coordinator.d.ts.map +1 -1
- package/dist/pipeline/handlers/dag-coordinator.js +2 -1
- package/dist/pipeline/handlers/dag-coordinator.js.map +1 -1
- package/dist/pipeline/handlers/history-upsert.d.ts +2 -0
- package/dist/pipeline/handlers/history-upsert.d.ts.map +1 -1
- package/dist/pipeline/handlers/history-upsert.js +6 -1
- package/dist/pipeline/handlers/history-upsert.js.map +1 -1
- package/dist/pipeline/handlers/pass-through.d.ts +3 -1
- package/dist/pipeline/handlers/pass-through.d.ts.map +1 -1
- package/dist/pipeline/handlers/pass-through.js +75 -5
- package/dist/pipeline/handlers/pass-through.js.map +1 -1
- package/dist/pipeline/handlers/summarize.d.ts +2 -12
- package/dist/pipeline/handlers/summarize.d.ts.map +1 -1
- package/dist/pipeline/handlers/summarize.js +2 -63
- package/dist/pipeline/handlers/summarize.js.map +1 -1
- package/dist/pipeline/handlers/tool-loop-core.d.ts +15 -9
- package/dist/pipeline/handlers/tool-loop-core.d.ts.map +1 -1
- package/dist/pipeline/handlers/tool-loop-core.js +128 -59
- package/dist/pipeline/handlers/tool-loop-core.js.map +1 -1
- package/dist/pipeline/handlers/tool-loop.d.ts.map +1 -1
- package/dist/pipeline/handlers/tool-loop.js +116 -293
- package/dist/pipeline/handlers/tool-loop.js.map +1 -1
- package/dist/pipeline/handlers/tool-response-round.d.ts +30 -0
- package/dist/pipeline/handlers/tool-response-round.d.ts.map +1 -0
- package/dist/pipeline/handlers/tool-response-round.js +85 -0
- package/dist/pipeline/handlers/tool-response-round.js.map +1 -0
- package/dist/pipeline/handlers/tool-select.d.ts.map +1 -1
- package/dist/pipeline/handlers/tool-select.js +5 -1
- package/dist/pipeline/handlers/tool-select.js.map +1 -1
- package/dist/pipeline/handlers/translate.d.ts.map +1 -1
- package/dist/pipeline/handlers/translate.js +2 -10
- package/dist/pipeline/handlers/translate.js.map +1 -1
- package/dist/policy/caller-identity.d.ts +29 -0
- package/dist/policy/caller-identity.d.ts.map +1 -0
- package/dist/policy/caller-identity.js +79 -0
- package/dist/policy/caller-identity.js.map +1 -0
- package/dist/policy/mixed-tool-call-handler.d.ts +9 -3
- package/dist/policy/mixed-tool-call-handler.d.ts.map +1 -1
- package/dist/policy/mixed-tool-call-handler.js +29 -7
- package/dist/policy/mixed-tool-call-handler.js.map +1 -1
- package/dist/policy/pending-tool-results-registry.d.ts +46 -4
- package/dist/policy/pending-tool-results-registry.d.ts.map +1 -1
- package/dist/policy/pending-tool-results-registry.js +203 -18
- package/dist/policy/pending-tool-results-registry.js.map +1 -1
- package/dist/policy/tool-availability-registry.d.ts +6 -5
- package/dist/policy/tool-availability-registry.d.ts.map +1 -1
- package/dist/policy/tool-availability-registry.js +13 -2
- package/dist/policy/tool-availability-registry.js.map +1 -1
- package/dist/reranker/llm-reranker.d.ts.map +1 -1
- package/dist/reranker/llm-reranker.js +2 -1
- package/dist/reranker/llm-reranker.js.map +1 -1
- package/dist/subagent/direct-llm-subagent.js +1 -1
- package/dist/subagent/direct-llm-subagent.js.map +1 -1
- package/dist/subagent/smart-agent-subagent.d.ts.map +1 -1
- package/dist/subagent/smart-agent-subagent.js +6 -5
- package/dist/subagent/smart-agent-subagent.js.map +1 -1
- package/dist/testing/index.d.ts +3 -1
- package/dist/testing/index.d.ts.map +1 -1
- package/dist/testing/index.js +2 -0
- package/dist/testing/index.js.map +1 -1
- package/package.json +2 -2
package/dist/agent.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
|
-
import {
|
|
2
|
+
import { addCacheTokenUsage, isReadinessReporter, mergeOfferedTools, NoopQueryExpander, NoopToolCache, normalizeExternalTools, OrchestratorError, QueryEmbedding, StreamingLlmCallStrategy, TextOnlyEmbedding, toModelToolMessages, toolNameFromRecord, toToolCallDelta, } from '@mcp-abap-adt/llm-agent';
|
|
3
3
|
import { RagOrchestrator } from './agent/rag-orchestrator.js';
|
|
4
4
|
import { normalizeRequestOptions } from './agent-request-options.js';
|
|
5
5
|
import { LlmClassifier } from './classifier/llm-classifier.js';
|
|
@@ -7,6 +7,8 @@ import { normalizeLoopLimit } from './config/normalize-max-iterations.js';
|
|
|
7
7
|
import { validateTimeZone, withRequestTime } from './context/request-time.js';
|
|
8
8
|
import { withSubmitReport } from './coordinator/dialogue/submit-report-tool.js';
|
|
9
9
|
import { buildAgentHealthSnapshot } from './health/agent-health.js';
|
|
10
|
+
import { ToolResponseRound } from './pipeline/handlers/tool-response-round.js';
|
|
11
|
+
import { callerIdentityKey, readCallerResolution, resolveCallerIdentity, withResolvedCaller, } from './policy/caller-identity.js';
|
|
10
12
|
import { denyRagAccess, ownerBoundQuery } from './rag/owner-bound-query.js';
|
|
11
13
|
export { OrchestratorError, } from '@mcp-abap-adt/llm-agent';
|
|
12
14
|
import { NoopRequestLogger } from './logger/noop-request-logger.js';
|
|
@@ -14,9 +16,10 @@ import { summaryToUsage } from './logger/session-request-logger.js';
|
|
|
14
16
|
import { McpToolRegistry } from './mcp/tool-registry.js';
|
|
15
17
|
import { NoopMetrics } from './metrics/noop-metrics.js';
|
|
16
18
|
import { LegacyAccumulateContextStrategy } from './pipeline/context/tool-loop-context/index.js';
|
|
19
|
+
import { ToolLoopControlHistory } from './pipeline/context/tool-loop-control-history.js';
|
|
17
20
|
import { normalizeHeartbeatMs } from './pipeline/handlers/normalize-heartbeat-ms.js';
|
|
18
21
|
import { runPassThrough } from './pipeline/handlers/pass-through.js';
|
|
19
|
-
import {
|
|
22
|
+
import { classifyToolCalls, executeToolBatchWithHeartbeat, filterAvailableTools, guardToolRound, injectPendingResults, injectToolPriority, pendingSourceMessages, readTaskFinish, runOutputValidationReprompt, } from './pipeline/handlers/tool-loop-core.js';
|
|
20
23
|
import { pipelineToStream } from './pipeline/pipeline-to-stream.js';
|
|
21
24
|
import { fireInternalToolsAsync } from './policy/mixed-tool-call-handler.js';
|
|
22
25
|
import { PendingToolResultsRegistry } from './policy/pending-tool-results-registry.js';
|
|
@@ -42,6 +45,9 @@ export class SmartAgent {
|
|
|
42
45
|
outputValidator;
|
|
43
46
|
sessionManager;
|
|
44
47
|
pendingToolResults;
|
|
48
|
+
// Include injected request registries and custom history implementations that
|
|
49
|
+
// only expose clear(key), so the public session lifecycle can release them.
|
|
50
|
+
sessionState = new Map();
|
|
45
51
|
requestLogger;
|
|
46
52
|
defaultLlmCallStrategy;
|
|
47
53
|
mcpToolRegistry;
|
|
@@ -60,7 +66,9 @@ export class SmartAgent {
|
|
|
60
66
|
maxToolCalls: normalizeLoopLimit(config.maxToolCalls, 'maxToolCalls'),
|
|
61
67
|
});
|
|
62
68
|
validateTimeZone(this.config.timeZone ?? 'UTC');
|
|
63
|
-
this.toolAvailabilityRegistry =
|
|
69
|
+
this.toolAvailabilityRegistry =
|
|
70
|
+
deps.toolAvailabilityRegistry ??
|
|
71
|
+
new ToolAvailabilityRegistry(this.config.toolUnavailableTtlMs);
|
|
64
72
|
this.tracer = deps.tracer ?? new NoopTracer();
|
|
65
73
|
this.metrics = deps.metrics ?? new NoopMetrics();
|
|
66
74
|
this.reranker = deps.reranker ?? new NoopReranker();
|
|
@@ -201,10 +209,11 @@ export class SmartAgent {
|
|
|
201
209
|
}
|
|
202
210
|
/**
|
|
203
211
|
* Close a session: remove session-scoped RAG collections for the given
|
|
204
|
-
* sessionId and flush
|
|
212
|
+
* sessionId and flush all caller-scoped in-memory state for that session.
|
|
213
|
+
* Supplying a caller limits in-memory clearing to that identity.
|
|
205
214
|
* Errors from registry cleanup are logged but not thrown — best-effort.
|
|
206
215
|
*/
|
|
207
|
-
async closeSession(sessionId) {
|
|
216
|
+
async closeSession(sessionId, options) {
|
|
208
217
|
if (this.deps.ragRegistry) {
|
|
209
218
|
const res = await this.deps.ragRegistry.closeSession(sessionId);
|
|
210
219
|
if (!res.ok) {
|
|
@@ -215,7 +224,34 @@ export class SmartAgent {
|
|
|
215
224
|
});
|
|
216
225
|
}
|
|
217
226
|
}
|
|
218
|
-
|
|
227
|
+
const identity = resolveCallerIdentity(options, sessionId);
|
|
228
|
+
const key = identity.kind === 'caller'
|
|
229
|
+
? callerIdentityKey(identity.caller)
|
|
230
|
+
: undefined;
|
|
231
|
+
if (identity.kind === 'caller' && key !== undefined) {
|
|
232
|
+
this.deps.historyMemory?.clear(key);
|
|
233
|
+
this.pendingToolResults.clear(identity.caller);
|
|
234
|
+
this.toolAvailabilityRegistry.clear(key);
|
|
235
|
+
const state = this.sessionState.get(sessionId);
|
|
236
|
+
for (const registry of state?.pending ?? [])
|
|
237
|
+
registry.clear(identity.caller);
|
|
238
|
+
for (const registry of state?.availability ?? [])
|
|
239
|
+
registry.clear(key);
|
|
240
|
+
state?.callerKeys.delete(key);
|
|
241
|
+
}
|
|
242
|
+
else if (options === undefined) {
|
|
243
|
+
const state = this.sessionState.get(sessionId);
|
|
244
|
+
this.deps.historyMemory?.clearSession?.(sessionId);
|
|
245
|
+
for (const callerKey of state?.callerKeys ?? [])
|
|
246
|
+
this.deps.historyMemory?.clear(callerKey);
|
|
247
|
+
this.pendingToolResults.clearSession(sessionId);
|
|
248
|
+
this.toolAvailabilityRegistry.clearSession(sessionId);
|
|
249
|
+
for (const registry of state?.pending ?? [])
|
|
250
|
+
registry.clearSession(sessionId);
|
|
251
|
+
for (const registry of state?.availability ?? [])
|
|
252
|
+
registry.clearSession(sessionId);
|
|
253
|
+
this.sessionState.delete(sessionId);
|
|
254
|
+
}
|
|
219
255
|
}
|
|
220
256
|
/** Returns the model identifiers of the currently active LLM instances. */
|
|
221
257
|
getActiveConfig() {
|
|
@@ -351,6 +387,7 @@ export class SmartAgent {
|
|
|
351
387
|
stopReason = 'tool_calls';
|
|
352
388
|
}
|
|
353
389
|
if (chunk.value.usage) {
|
|
390
|
+
Object.assign(totalUsage, addCacheTokenUsage(totalUsage, chunk.value.usage));
|
|
354
391
|
totalUsage.promptTokens += chunk.value.usage.promptTokens;
|
|
355
392
|
totalUsage.completionTokens += chunk.value.usage.completionTokens;
|
|
356
393
|
totalUsage.totalTokens += chunk.value.usage.totalTokens;
|
|
@@ -423,15 +460,33 @@ export class SmartAgent {
|
|
|
423
460
|
detectedAdapter = this.deps.clientAdapters.find((a) => a.detect(typeof systemMsg.content === 'string' ? systemMsg.content : ''));
|
|
424
461
|
}
|
|
425
462
|
}
|
|
426
|
-
const ephemeralSession = options?.sessionId === undefined
|
|
427
|
-
|
|
463
|
+
const ephemeralSession = options?.sessionId === undefined &&
|
|
464
|
+
options?.caller?.sessionId === undefined;
|
|
465
|
+
const sessionId = options?.sessionId ?? options?.caller?.sessionId ?? randomUUID();
|
|
428
466
|
const pipelinePendingResults = opts.pendingToolResults instanceof PendingToolResultsRegistry
|
|
429
467
|
? opts.pendingToolResults
|
|
430
468
|
: this.pendingToolResults;
|
|
431
469
|
const pipelineToolAvailability = opts.toolAvailability instanceof ToolAvailabilityRegistry
|
|
432
470
|
? opts.toolAvailability
|
|
433
471
|
: this.toolAvailabilityRegistry;
|
|
434
|
-
opts =
|
|
472
|
+
opts = withResolvedCaller(opts, sessionId);
|
|
473
|
+
const identity = readCallerResolution(opts);
|
|
474
|
+
if (!ephemeralSession && identity.kind === 'caller') {
|
|
475
|
+
let state = this.sessionState.get(sessionId);
|
|
476
|
+
if (!state) {
|
|
477
|
+
state = {
|
|
478
|
+
callerKeys: new Set(),
|
|
479
|
+
pending: new Set(),
|
|
480
|
+
availability: new Set(),
|
|
481
|
+
};
|
|
482
|
+
this.sessionState.set(sessionId, state);
|
|
483
|
+
}
|
|
484
|
+
const key = callerIdentityKey(identity.caller);
|
|
485
|
+
if (key !== undefined)
|
|
486
|
+
state.callerKeys.add(key);
|
|
487
|
+
state.pending.add(pipelinePendingResults);
|
|
488
|
+
state.availability.add(pipelineToolAvailability);
|
|
489
|
+
}
|
|
435
490
|
const externalTools = normalizeExternalTools(options?.externalTools);
|
|
436
491
|
opts?.sessionLogger?.logStep('external_tools_normalized', {
|
|
437
492
|
rawCount: options?.externalTools?.length ?? 0,
|
|
@@ -441,12 +496,20 @@ export class SmartAgent {
|
|
|
441
496
|
opts?.sessionLogger?.logStep('pipeline_start', { mode, textOrMessages });
|
|
442
497
|
this.requestLogger.startRequest(traceId);
|
|
443
498
|
try {
|
|
499
|
+
if (identity.kind === 'caller')
|
|
500
|
+
textOrMessages = await (this.deps.pipeline
|
|
501
|
+
? pipelinePendingResults
|
|
502
|
+
: this.pendingToolResults).prepareInput(identity.caller, textOrMessages, options?.externalResults);
|
|
444
503
|
if (mode === 'pass') {
|
|
445
|
-
const
|
|
504
|
+
const passPendingResults = this.deps.pipeline
|
|
505
|
+
? pipelinePendingResults
|
|
506
|
+
: this.pendingToolResults;
|
|
507
|
+
const inputMessages = typeof textOrMessages === 'string'
|
|
446
508
|
? [{ role: 'user', content: textOrMessages }]
|
|
447
509
|
: textOrMessages;
|
|
510
|
+
const passMessages = await injectPendingResults(withRequestTime(inputMessages, opts.requestStartedAt, this.config.timeZone), passPendingResults, identity, opts, options?.externalResults, textOrMessages);
|
|
448
511
|
opts?.sessionLogger?.logStep('client_request', { textOrMessages });
|
|
449
|
-
for await (const chunk of runPassThrough(this._mainLlm, this.requestLogger,
|
|
512
|
+
for await (const chunk of runPassThrough(this._mainLlm, this.requestLogger, passMessages, externalTools, opts, this.config.toolTrust, passPendingResults, identity)) {
|
|
450
513
|
yield chunk;
|
|
451
514
|
}
|
|
452
515
|
rootSpan.setStatus('ok');
|
|
@@ -491,6 +554,7 @@ export class SmartAgent {
|
|
|
491
554
|
});
|
|
492
555
|
const orchResult = await orchestrator.orchestrate(textOrMessages, {
|
|
493
556
|
opts,
|
|
557
|
+
identity,
|
|
494
558
|
rootSpan,
|
|
495
559
|
sessionId,
|
|
496
560
|
mode,
|
|
@@ -507,21 +571,21 @@ export class SmartAgent {
|
|
|
507
571
|
// it unused would trip noUnusedLocals.
|
|
508
572
|
const { retrieved, finalTools, toolsAvailable, assembledMessages, mainAction, toolClientMap, } = orchResult.value;
|
|
509
573
|
// 4. Single Streaming Loop
|
|
510
|
-
const stream = this._runStreamingToolLoop(mainAction, retrieved, assembledMessages, toolClientMap, opts, rootSpan, sessionId, externalTools, finalTools, toolsAvailable, detectedAdapter);
|
|
574
|
+
const stream = this._runStreamingToolLoop(mainAction, retrieved, assembledMessages, toolClientMap, opts, identity, rootSpan, sessionId, externalTools, finalTools, toolsAvailable, textOrMessages, detectedAdapter);
|
|
511
575
|
for await (const chunk of stream)
|
|
512
576
|
yield chunk;
|
|
513
577
|
rootSpan.setStatus('ok');
|
|
514
578
|
}
|
|
515
579
|
finally {
|
|
516
|
-
if (ephemeralSession) {
|
|
580
|
+
if (ephemeralSession && identity.kind === 'caller') {
|
|
517
581
|
const pendingResults = this.deps.pipeline
|
|
518
582
|
? pipelinePendingResults
|
|
519
583
|
: this.pendingToolResults;
|
|
520
584
|
const toolAvailability = this.deps.pipeline
|
|
521
585
|
? pipelineToolAvailability
|
|
522
586
|
: this.toolAvailabilityRegistry;
|
|
523
|
-
pendingResults.clear(
|
|
524
|
-
toolAvailability.clear(
|
|
587
|
+
pendingResults.clear(identity.caller);
|
|
588
|
+
toolAvailability.clear(callerIdentityKey(identity.caller));
|
|
525
589
|
}
|
|
526
590
|
this.requestLogger.endRequest(traceId);
|
|
527
591
|
rootSpan.end();
|
|
@@ -529,7 +593,7 @@ export class SmartAgent {
|
|
|
529
593
|
this.metrics.requestLatency.record(Date.now() - requestStart);
|
|
530
594
|
}
|
|
531
595
|
}
|
|
532
|
-
async *_runStreamingToolLoop(_action, _retrieved, initialMessages, toolClientMap, opts, parentSpan, sessionId, externalTools, activeTools, toolsAvailable, clientAdapter) {
|
|
596
|
+
async *_runStreamingToolLoop(_action, _retrieved, initialMessages, toolClientMap, opts, identity, parentSpan, sessionId, externalTools, activeTools, toolsAvailable, sourceInput, clientAdapter) {
|
|
533
597
|
const toolLoopSpan = this.tracer.startSpan('smart_agent.tool_loop', {
|
|
534
598
|
parent: parentSpan,
|
|
535
599
|
});
|
|
@@ -550,29 +614,19 @@ export class SmartAgent {
|
|
|
550
614
|
const timingLog = [];
|
|
551
615
|
const loopStart = Date.now();
|
|
552
616
|
let currentTools = activeTools;
|
|
617
|
+
const localBlocks = new Set();
|
|
553
618
|
messages = injectToolPriority(messages, externalTools);
|
|
554
619
|
// staticPrefix is immutable across all iterations — re-emitted every round.
|
|
555
|
-
const staticPrefix = messages;
|
|
620
|
+
const staticPrefix = await injectPendingResults(messages, this.pendingToolResults, identity, opts, opts?.externalResults, sourceInput);
|
|
556
621
|
const strategy = (this.deps.toolLoopContextStrategyFactory ??
|
|
557
622
|
(() => new LegacyAccumulateContextStrategy()))({ run: undefined });
|
|
558
|
-
//
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
const withPending = await injectPendingResults(messages, this.pendingToolResults, sessionId, opts);
|
|
562
|
-
if (withPending.length > staticPrefix.length) {
|
|
563
|
-
// Injected pending results form an assistant+tool group → record as round.
|
|
564
|
-
const injected = withPending.slice(staticPrefix.length);
|
|
565
|
-
const pendingRound = {
|
|
566
|
-
assistant: injected[0],
|
|
567
|
-
results: injected.slice(1),
|
|
568
|
-
};
|
|
569
|
-
await strategy.record(pendingRound);
|
|
570
|
-
}
|
|
571
|
-
messages = (await strategy.form({
|
|
623
|
+
// Preserve validation exchanges between their original tool rounds.
|
|
624
|
+
const controlHistory = new ToolLoopControlHistory(staticPrefix.length);
|
|
625
|
+
messages = controlHistory.restore(await strategy.form({
|
|
572
626
|
prefix: staticPrefix,
|
|
573
627
|
toolTrust: this.config.toolTrust,
|
|
574
628
|
queryText: _action.text,
|
|
575
|
-
}))
|
|
629
|
+
}));
|
|
576
630
|
const finalize = async function* (content, finishReason) {
|
|
577
631
|
opts?.sessionLogger?.logStep('final_response', { content, usage });
|
|
578
632
|
// onBeforeStream hook — consumer transforms content before streaming
|
|
@@ -795,7 +849,7 @@ export class SmartAgent {
|
|
|
795
849
|
}
|
|
796
850
|
}
|
|
797
851
|
currentTools = await withSubmitReport(currentTools, toolClientMap, opts, externalTools);
|
|
798
|
-
currentTools = filterAvailableTools(this.toolAvailabilityRegistry,
|
|
852
|
+
currentTools = filterAvailableTools(this.toolAvailabilityRegistry, identity, currentTools, iteration, opts, localBlocks);
|
|
799
853
|
if (this.config.maxToolCalls !== undefined &&
|
|
800
854
|
toolCallCount >= this.config.maxToolCalls) {
|
|
801
855
|
currentTools = [];
|
|
@@ -852,7 +906,6 @@ export class SmartAgent {
|
|
|
852
906
|
const toolCallsMap = new Map();
|
|
853
907
|
// Track which streaming indices belong to external tools so that
|
|
854
908
|
// argument-only continuation deltas (no name field) are forwarded too.
|
|
855
|
-
const externalToolIndices = new Set();
|
|
856
909
|
for await (const chunkResult of stream) {
|
|
857
910
|
if (!chunkResult.ok) {
|
|
858
911
|
llmSpan.setStatus('error', chunkResult.error.message);
|
|
@@ -873,7 +926,6 @@ export class SmartAgent {
|
|
|
873
926
|
content = '';
|
|
874
927
|
iterationBuffer = '';
|
|
875
928
|
toolCallsMap.clear();
|
|
876
|
-
externalToolIndices.clear();
|
|
877
929
|
finishReason = undefined;
|
|
878
930
|
continue;
|
|
879
931
|
}
|
|
@@ -890,24 +942,7 @@ export class SmartAgent {
|
|
|
890
942
|
}
|
|
891
943
|
}
|
|
892
944
|
if (chunk.toolCalls) {
|
|
893
|
-
//
|
|
894
|
-
for (const tc of chunk.toolCalls) {
|
|
895
|
-
const name = getStreamToolCallName(tc);
|
|
896
|
-
if (name && externalToolNames.has(name)) {
|
|
897
|
-
const delta = toToolCallDelta(tc, 0);
|
|
898
|
-
externalToolIndices.add(delta.index);
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
const externalDeltas = chunk.toolCalls.filter((tc) => {
|
|
902
|
-
const delta = toToolCallDelta(tc, 0);
|
|
903
|
-
return externalToolIndices.has(delta.index);
|
|
904
|
-
});
|
|
905
|
-
if (!opts?.toolRoundGuard && externalDeltas.length > 0) {
|
|
906
|
-
yield {
|
|
907
|
-
ok: true,
|
|
908
|
-
value: { content: '', toolCalls: externalDeltas },
|
|
909
|
-
};
|
|
910
|
-
}
|
|
945
|
+
// Buffer calls until the complete response can be classified atomically.
|
|
911
946
|
for (const [fallbackIndex, rawToolCall,] of chunk.toolCalls.entries()) {
|
|
912
947
|
const tc = toToolCallDelta(rawToolCall, fallbackIndex);
|
|
913
948
|
if (!toolCallsMap.has(tc.index)) {
|
|
@@ -933,6 +968,7 @@ export class SmartAgent {
|
|
|
933
968
|
if (chunk.finishReason)
|
|
934
969
|
finishReason = chunk.finishReason;
|
|
935
970
|
if (chunk.usage) {
|
|
971
|
+
Object.assign(usage, addCacheTokenUsage(usage, chunk.usage));
|
|
936
972
|
usage.promptTokens += chunk.usage.promptTokens;
|
|
937
973
|
usage.completionTokens += chunk.usage.completionTokens;
|
|
938
974
|
usage.totalTokens += chunk.usage.totalTokens;
|
|
@@ -947,10 +983,11 @@ export class SmartAgent {
|
|
|
947
983
|
phase: `llm_call_${iteration + 1}`,
|
|
948
984
|
duration: llmCallDuration,
|
|
949
985
|
});
|
|
950
|
-
const
|
|
986
|
+
const hasToolCalls = finishReason === 'tool_calls' && toolCallsMap.size > 0;
|
|
987
|
+
const val = hasToolCalls
|
|
951
988
|
? { reprompt: false, messages }
|
|
952
989
|
: await runOutputValidationReprompt(this.outputValidator, content, messages, currentTools, opts, this.config.toolTrust);
|
|
953
|
-
const iterationEnd =
|
|
990
|
+
const iterationEnd = hasToolCalls
|
|
954
991
|
? 'tool_calls'
|
|
955
992
|
: val.reprompt
|
|
956
993
|
? 'retry'
|
|
@@ -973,182 +1010,104 @@ export class SmartAgent {
|
|
|
973
1010
|
if (finishReason === 'tool_calls' &&
|
|
974
1011
|
(clientAdapter || this.config.streamMode === 'final'))
|
|
975
1012
|
stoppedContent += content;
|
|
976
|
-
const
|
|
977
|
-
|
|
978
|
-
try {
|
|
979
|
-
args = JSON.parse(tc.arguments);
|
|
980
|
-
}
|
|
981
|
-
catch {
|
|
982
|
-
args = {};
|
|
983
|
-
}
|
|
984
|
-
return { id: tc.id, name: tc.name, arguments: args };
|
|
985
|
-
});
|
|
1013
|
+
const responseRound = new ToolResponseRound(content, toolCallsMap.values(), (name) => externalToolNames.has(name) && !toolClientMap.has(name));
|
|
1014
|
+
const toolCalls = hasToolCalls ? responseRound.executableCalls() : [];
|
|
986
1015
|
opts?.sessionLogger?.logStep(`llm_response_iter_${iteration + 1}`, {
|
|
987
1016
|
content,
|
|
988
1017
|
toolCalls,
|
|
989
1018
|
finishReason,
|
|
990
1019
|
}, 'llm');
|
|
991
|
-
if (
|
|
1020
|
+
if (!hasToolCalls) {
|
|
992
1021
|
// Output validation
|
|
993
1022
|
if (val.reprompt) {
|
|
994
|
-
//
|
|
995
|
-
// into the strategy — they are ephemeral and survive only until the
|
|
996
|
-
// next recorded tool round, at which point controlTail is pruned.
|
|
1023
|
+
// Anchor the rejected reply and correction before any newer tool rounds.
|
|
997
1024
|
const appended = val.messages.slice(messages.length);
|
|
998
|
-
|
|
999
|
-
messages = (await strategy.form({
|
|
1025
|
+
controlHistory.append(appended);
|
|
1026
|
+
messages = controlHistory.restore(await strategy.form({
|
|
1000
1027
|
prefix: staticPrefix,
|
|
1001
1028
|
toolTrust: this.config.toolTrust,
|
|
1002
1029
|
queryText: _action.text,
|
|
1003
|
-
}))
|
|
1030
|
+
}));
|
|
1004
1031
|
continue;
|
|
1005
1032
|
}
|
|
1006
1033
|
yield* finalize(content, finishReason);
|
|
1007
1034
|
return;
|
|
1008
1035
|
}
|
|
1009
1036
|
const roundRejections = guardToolRound(toolCalls, opts) ?? new Map();
|
|
1010
|
-
const { internalCalls, validExternalCalls, blockedCalls, hallucinations, } = classifyToolCalls(toolCalls, toolClientMap, externalToolNames, this.toolAvailabilityRegistry,
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
if (rejectedExternal.length > 0) {
|
|
1014
|
-
await strategy.record({
|
|
1015
|
-
assistant: {
|
|
1016
|
-
role: 'assistant',
|
|
1017
|
-
content: content || null,
|
|
1018
|
-
tool_calls: rejectedExternal.map((tc) => ({
|
|
1019
|
-
id: tc.id,
|
|
1020
|
-
type: 'function',
|
|
1021
|
-
function: {
|
|
1022
|
-
name: tc.name,
|
|
1023
|
-
arguments: JSON.stringify(tc.arguments),
|
|
1024
|
-
},
|
|
1025
|
-
})),
|
|
1026
|
-
},
|
|
1027
|
-
results: rejectedExternal.map((tc) => ({
|
|
1028
|
-
role: 'tool',
|
|
1029
|
-
tool_call_id: tc.id,
|
|
1030
|
-
content: `Error: ${roundRejections.get(toolCalls.indexOf(tc))}`,
|
|
1031
|
-
})),
|
|
1032
|
-
meta: rejectedExternal.map(() => ({ isError: true })),
|
|
1033
|
-
});
|
|
1034
|
-
for (const tc of rejectedExternal) {
|
|
1035
|
-
validExternalCalls.splice(validExternalCalls.indexOf(tc), 1);
|
|
1036
|
-
}
|
|
1037
|
-
controlTail.length = 0;
|
|
1038
|
-
messages = (await strategy.form({
|
|
1039
|
-
prefix: staticPrefix,
|
|
1040
|
-
toolTrust: this.config.toolTrust,
|
|
1041
|
-
queryText: _action.text,
|
|
1042
|
-
})).concat(controlTail);
|
|
1043
|
-
if (validExternalCalls.length === 0 &&
|
|
1044
|
-
internalCalls.length === 0 &&
|
|
1045
|
-
blockedCalls.length === 0 &&
|
|
1046
|
-
hallucinations.length === 0)
|
|
1047
|
-
continue;
|
|
1037
|
+
const { internalCalls, validExternalCalls, blockedCalls, hallucinations, } = classifyToolCalls(toolCalls, toolClientMap, externalToolNames, this.toolAvailabilityRegistry, identity, localBlocks);
|
|
1038
|
+
for (const tc of blockedCalls) {
|
|
1039
|
+
responseRound.resolve(tc, `Error: Tool "${tc.name}" is temporarily unavailable in this session.`, true);
|
|
1048
1040
|
}
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
await strategy.record({
|
|
1052
|
-
assistant: group.assistant,
|
|
1053
|
-
results: group.results,
|
|
1054
|
-
});
|
|
1055
|
-
controlTail.length = 0;
|
|
1056
|
-
messages = (await strategy.form({
|
|
1057
|
-
prefix: staticPrefix,
|
|
1058
|
-
toolTrust: this.config.toolTrust,
|
|
1059
|
-
queryText: _action.text,
|
|
1060
|
-
})).concat(controlTail);
|
|
1061
|
-
continue;
|
|
1041
|
+
for (const tc of hallucinations) {
|
|
1042
|
+
responseRound.resolve(tc, `Error: Tool "${tc.name}" not found.`, true);
|
|
1062
1043
|
}
|
|
1044
|
+
const externalCalls = validExternalCalls.filter((tc) => {
|
|
1045
|
+
const rejection = roundRejections.get(toolCalls.indexOf(tc));
|
|
1046
|
+
if (rejection === undefined)
|
|
1047
|
+
return true;
|
|
1048
|
+
responseRound.resolve(tc, `Error: ${rejection}`, true);
|
|
1049
|
+
return false;
|
|
1050
|
+
});
|
|
1063
1051
|
// Truncate batch to remaining budget
|
|
1064
1052
|
const remaining = this.config.maxToolCalls !== undefined
|
|
1065
1053
|
? this.config.maxToolCalls - toolCallCount
|
|
1066
1054
|
: internalCalls.length;
|
|
1067
1055
|
const batch = internalCalls.slice(0, Math.max(0, remaining));
|
|
1068
1056
|
const skipped = internalCalls.slice(batch.length);
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
const
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
results: unknownCalls.map((tc) => ({
|
|
1086
|
-
role: 'tool',
|
|
1087
|
-
tool_call_id: tc.id,
|
|
1088
|
-
content: `Error: Tool "${tc.name}" not found.`,
|
|
1089
|
-
})),
|
|
1090
|
-
meta: unknownCalls.map(() => ({ isError: true })),
|
|
1057
|
+
if (externalCalls.length > 0) {
|
|
1058
|
+
// Mixed calls: fire internal tools async, store pending results
|
|
1059
|
+
const pending = fireInternalToolsAsync(content, batch, this.pendingToolResults, identity, {
|
|
1060
|
+
toolClientMap,
|
|
1061
|
+
toolCache: this.toolCache,
|
|
1062
|
+
metrics: this.metrics,
|
|
1063
|
+
options: opts,
|
|
1064
|
+
roundCalls: toolCalls,
|
|
1065
|
+
roundRejections,
|
|
1066
|
+
skippedCalls: skipped,
|
|
1067
|
+
maxToolCalls: this.config.maxToolCalls,
|
|
1068
|
+
assistantMessage: responseRound.assistant,
|
|
1069
|
+
resolvedResults: responseRound.resolved(),
|
|
1070
|
+
precedingMessages: messages,
|
|
1071
|
+
sourceMessages: pendingSourceMessages(sourceInput, staticPrefix, messages),
|
|
1072
|
+
externalCalls,
|
|
1091
1073
|
});
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1074
|
+
toolCallCount += batch.length;
|
|
1075
|
+
if (batch.length > 0) {
|
|
1076
|
+
try {
|
|
1077
|
+
opts?.onToolCallCount?.(toolCallCount, this.config.maxToolCalls);
|
|
1078
|
+
}
|
|
1079
|
+
catch {
|
|
1080
|
+
/* Observers cannot affect execution. */
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
if (this.config.maxToolCalls !== undefined &&
|
|
1084
|
+
toolCallCount >= this.config.maxToolCalls) {
|
|
1101
1085
|
yield {
|
|
1102
1086
|
ok: true,
|
|
1103
1087
|
value: {
|
|
1104
1088
|
content: '',
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
name: tc.name,
|
|
1109
|
-
arguments: JSON.stringify(tc.arguments),
|
|
1110
|
-
})),
|
|
1089
|
+
budgetExhausted: true,
|
|
1090
|
+
toolCallCount,
|
|
1091
|
+
maxToolCalls: this.config.maxToolCalls,
|
|
1111
1092
|
},
|
|
1112
1093
|
};
|
|
1113
1094
|
}
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
}
|
|
1131
|
-
catch {
|
|
1132
|
-
/* Observers cannot affect execution. */
|
|
1133
|
-
}
|
|
1134
|
-
}
|
|
1135
|
-
if (this.config.maxToolCalls !== undefined &&
|
|
1136
|
-
toolCallCount >= this.config.maxToolCalls) {
|
|
1137
|
-
yield {
|
|
1138
|
-
ok: true,
|
|
1139
|
-
value: {
|
|
1140
|
-
content: '',
|
|
1141
|
-
budgetExhausted: true,
|
|
1142
|
-
toolCallCount,
|
|
1143
|
-
maxToolCalls: this.config.maxToolCalls,
|
|
1144
|
-
},
|
|
1145
|
-
};
|
|
1146
|
-
}
|
|
1147
|
-
opts?.sessionLogger?.logStep('mixed_tool_calls', {
|
|
1148
|
-
internal: internalCalls.map((tc) => tc.name),
|
|
1149
|
-
external: validExternalCalls.map((tc) => tc.name),
|
|
1150
|
-
});
|
|
1151
|
-
}
|
|
1095
|
+
opts?.sessionLogger?.logStep('mixed_tool_calls', {
|
|
1096
|
+
internal: internalCalls.map((tc) => tc.name),
|
|
1097
|
+
external: externalCalls.map((tc) => tc.name),
|
|
1098
|
+
});
|
|
1099
|
+
yield {
|
|
1100
|
+
ok: true,
|
|
1101
|
+
value: {
|
|
1102
|
+
content: '',
|
|
1103
|
+
toolCalls: externalCalls.map((tc, index) => ({
|
|
1104
|
+
index,
|
|
1105
|
+
id: pending.externalCalls[index].returnedId,
|
|
1106
|
+
name: tc.name,
|
|
1107
|
+
arguments: JSON.stringify(tc.arguments),
|
|
1108
|
+
})),
|
|
1109
|
+
},
|
|
1110
|
+
};
|
|
1152
1111
|
timingLog.push({ phase: 'total', duration: Date.now() - loopStart });
|
|
1153
1112
|
toolLoopSpan.setStatus('ok');
|
|
1154
1113
|
toolLoopSpan.end();
|
|
@@ -1172,20 +1131,6 @@ export class SmartAgent {
|
|
|
1172
1131
|
// Capture the assistant message now; record the full round (assistant +
|
|
1173
1132
|
// tool results) atomically after the batch completes so the strategy
|
|
1174
1133
|
// always sees a complete ToolRound.
|
|
1175
|
-
const batchAssistantMsg = {
|
|
1176
|
-
role: 'assistant',
|
|
1177
|
-
content: content || null,
|
|
1178
|
-
tool_calls: toolCalls
|
|
1179
|
-
.filter((tc) => internalCalls.includes(tc) || hallucinations.includes(tc))
|
|
1180
|
-
.map((tc) => ({
|
|
1181
|
-
id: tc.id,
|
|
1182
|
-
type: 'function',
|
|
1183
|
-
function: {
|
|
1184
|
-
name: tc.name,
|
|
1185
|
-
arguments: JSON.stringify(tc.arguments),
|
|
1186
|
-
},
|
|
1187
|
-
})),
|
|
1188
|
-
};
|
|
1189
1134
|
const heartbeatMs = normalizeHeartbeatMs(this.config.heartbeatIntervalMs);
|
|
1190
1135
|
// Yield all progress messages before execution
|
|
1191
1136
|
for (const tc of batch) {
|
|
@@ -1199,6 +1144,8 @@ export class SmartAgent {
|
|
|
1199
1144
|
}
|
|
1200
1145
|
// Execute all tool calls concurrently with heartbeat (shared core).
|
|
1201
1146
|
const outcome = yield* executeToolBatchWithHeartbeat({
|
|
1147
|
+
callerResolution: identity,
|
|
1148
|
+
localBlocks,
|
|
1202
1149
|
batch,
|
|
1203
1150
|
roundCalls: toolCalls,
|
|
1204
1151
|
roundRejections,
|
|
@@ -1215,6 +1162,16 @@ export class SmartAgent {
|
|
|
1215
1162
|
timingLog,
|
|
1216
1163
|
heartbeatMs,
|
|
1217
1164
|
options: opts,
|
|
1165
|
+
onBatchComplete: async (outcome) => {
|
|
1166
|
+
outcome.toolMessages.forEach((message, index) => {
|
|
1167
|
+
responseRound.add(message, outcome.resultMeta[index]);
|
|
1168
|
+
});
|
|
1169
|
+
for (const tc of skipped) {
|
|
1170
|
+
responseRound.resolve(tc, `Error: not executed: tool-call budget of ${this.config.maxToolCalls} for this request is exhausted; return what you have and report what is missing`, true);
|
|
1171
|
+
}
|
|
1172
|
+
const batchRound = responseRound.finish();
|
|
1173
|
+
await strategy.record(controlHistory.record(batchRound));
|
|
1174
|
+
},
|
|
1218
1175
|
mcpFailureClassifier: this.deps.mcpFailureClassifier,
|
|
1219
1176
|
maxToolCalls: this.config.maxToolCalls,
|
|
1220
1177
|
onToolExecuted: (r) => {
|
|
@@ -1258,33 +1215,11 @@ export class SmartAgent {
|
|
|
1258
1215
|
},
|
|
1259
1216
|
};
|
|
1260
1217
|
}
|
|
1261
|
-
|
|
1262
|
-
assistant: batchAssistantMsg,
|
|
1263
|
-
results: [
|
|
1264
|
-
...outcome.toolMessages,
|
|
1265
|
-
...hallucinations.map((tc) => ({
|
|
1266
|
-
role: 'tool',
|
|
1267
|
-
tool_call_id: tc.id,
|
|
1268
|
-
content: `Error: Tool "${tc.name}" not found.`,
|
|
1269
|
-
})),
|
|
1270
|
-
...skipped.map((tc) => ({
|
|
1271
|
-
role: 'tool',
|
|
1272
|
-
tool_call_id: tc.id,
|
|
1273
|
-
content: `Error: not executed: tool-call budget of ${this.config.maxToolCalls} for this request is exhausted; return what you have and report what is missing`,
|
|
1274
|
-
})),
|
|
1275
|
-
],
|
|
1276
|
-
meta: [
|
|
1277
|
-
...outcome.resultMeta,
|
|
1278
|
-
...[...hallucinations, ...skipped].map(() => ({ isError: true })),
|
|
1279
|
-
],
|
|
1280
|
-
};
|
|
1281
|
-
await strategy.record(batchRound);
|
|
1282
|
-
controlTail.length = 0;
|
|
1283
|
-
messages = (await strategy.form({
|
|
1218
|
+
messages = controlHistory.restore(await strategy.form({
|
|
1284
1219
|
prefix: staticPrefix,
|
|
1285
1220
|
toolTrust: this.config.toolTrust,
|
|
1286
1221
|
queryText: _action.text,
|
|
1287
|
-
}))
|
|
1222
|
+
}));
|
|
1288
1223
|
}
|
|
1289
1224
|
}
|
|
1290
1225
|
}
|