@mcp-abap-adt/llm-agent-libs 33.1.0 → 35.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 +72 -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 +51 -34
- package/dist/adapters/llm-adapter.js.map +1 -1
- package/dist/adapters/llm-provider-bridge.d.ts.map +1 -1
- package/dist/adapters/llm-provider-bridge.js +13 -20
- package/dist/adapters/llm-provider-bridge.js.map +1 -1
- package/dist/adapters/normalize-finish-reason.d.ts +3 -0
- package/dist/adapters/normalize-finish-reason.d.ts.map +1 -0
- package/dist/adapters/normalize-finish-reason.js +17 -0
- package/dist/adapters/normalize-finish-reason.js.map +1 -0
- 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 -43
- 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 +12 -3
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +178 -245
- 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 +16 -26
- package/dist/builder.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/adaptive-interpreter.d.ts +2 -1
- package/dist/coordinator/adaptive/adaptive-interpreter.d.ts.map +1 -1
- package/dist/coordinator/adaptive/adaptive-interpreter.js +46 -22
- package/dist/coordinator/adaptive/adaptive-interpreter.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 +2 -2
- package/dist/coordinator/adaptive/index.d.ts.map +1 -1
- package/dist/coordinator/adaptive/index.js +2 -2
- 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 +14 -20
- package/dist/coordinator/adaptive/llm-adaptive-controller.js.map +1 -1
- package/dist/coordinator/adaptive/parse-plan.d.ts +0 -2
- package/dist/coordinator/adaptive/parse-plan.d.ts.map +1 -1
- package/dist/coordinator/adaptive/parse-plan.js +4 -6
- package/dist/coordinator/adaptive/parse-plan.js.map +1 -1
- package/dist/coordinator/dag/dag-plan-interpreter.d.ts.map +1 -1
- package/dist/coordinator/dag/dag-plan-interpreter.js +7 -1
- package/dist/coordinator/dag/dag-plan-interpreter.js.map +1 -1
- package/dist/coordinator/dag/llm-finalizer.d.ts.map +1 -1
- package/dist/coordinator/dag/llm-finalizer.js +5 -2
- package/dist/coordinator/dag/llm-finalizer.js.map +1 -1
- package/dist/coordinator/dialogue/dialogue-session.d.ts +2 -3
- package/dist/coordinator/dialogue/dialogue-session.d.ts.map +1 -1
- package/dist/coordinator/dialogue/dialogue-session.js +103 -64
- 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-retrieval.d.ts.map +1 -1
- package/dist/coordinator/dialogue/planner-retrieval.js +13 -4
- package/dist/coordinator/dialogue/planner-retrieval.js.map +1 -1
- package/dist/coordinator/dialogue/planner-scope.d.ts +4 -1
- package/dist/coordinator/dialogue/planner-scope.d.ts.map +1 -1
- package/dist/coordinator/dialogue/planner-scope.js.map +1 -1
- package/dist/coordinator/dialogue/planner-tools-client.d.ts.map +1 -1
- package/dist/coordinator/dialogue/planner-tools-client.js +47 -26
- package/dist/coordinator/dialogue/planner-tools-client.js.map +1 -1
- package/dist/coordinator/dialogue/profile-plan-author.d.ts.map +1 -1
- package/dist/coordinator/dialogue/profile-plan-author.js +5 -5
- package/dist/coordinator/dialogue/profile-plan-author.js.map +1 -1
- package/dist/coordinator/dialogue/profile-resolver.d.ts.map +1 -1
- package/dist/coordinator/dialogue/profile-resolver.js +8 -0
- package/dist/coordinator/dialogue/profile-resolver.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 +11 -3
- package/dist/coordinator/dialogue/task-board.d.ts.map +1 -1
- package/dist/coordinator/dialogue/task-board.js +42 -12
- 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 +41 -26
- package/dist/coordinator/dispatch/self.js.map +1 -1
- package/dist/coordinator/dispatch/subagent.d.ts.map +1 -1
- package/dist/coordinator/dispatch/subagent.js +3 -0
- package/dist/coordinator/dispatch/subagent.js.map +1 -1
- package/dist/coordinator/llm-output-truncation.d.ts +3 -0
- package/dist/coordinator/llm-output-truncation.d.ts.map +1 -0
- package/dist/coordinator/llm-output-truncation.js +6 -0
- package/dist/coordinator/llm-output-truncation.js.map +1 -0
- package/dist/coordinator/planning/one-shot.d.ts.map +1 -1
- package/dist/coordinator/planning/one-shot.js +4 -0
- package/dist/coordinator/planning/one-shot.js.map +1 -1
- package/dist/coordinator/planning/replan-on-error.d.ts.map +1 -1
- package/dist/coordinator/planning/replan-on-error.js +5 -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 +51 -37
- package/dist/coordinator/stepper/cyclic-react-executor.js.map +1 -1
- package/dist/coordinator/stepper/llm-evaluator.d.ts.map +1 -1
- package/dist/coordinator/stepper/llm-evaluator.js +12 -4
- package/dist/coordinator/stepper/llm-evaluator.js.map +1 -1
- package/dist/coordinator/stepper/llm-stepper-planner.d.ts.map +1 -1
- package/dist/coordinator/stepper/llm-stepper-planner.js +16 -5
- package/dist/coordinator/stepper/llm-stepper-planner.js.map +1 -1
- package/dist/coordinator/stepper/llm-task-formalizer.d.ts +2 -3
- package/dist/coordinator/stepper/llm-task-formalizer.d.ts.map +1 -1
- package/dist/coordinator/stepper/llm-task-formalizer.js +14 -8
- package/dist/coordinator/stepper/llm-task-formalizer.js.map +1 -1
- package/dist/coordinator/stepper/need-resolver.d.ts.map +1 -1
- package/dist/coordinator/stepper/need-resolver.js +10 -1
- package/dist/coordinator/stepper/need-resolver.js.map +1 -1
- package/dist/coordinator/stepper/root-finalizer.d.ts.map +1 -1
- package/dist/coordinator/stepper/root-finalizer.js +8 -3
- package/dist/coordinator/stepper/root-finalizer.js.map +1 -1
- package/dist/coordinator/stepper/stepper-interpreter.d.ts.map +1 -1
- package/dist/coordinator/stepper/stepper-interpreter.js +8 -19
- 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/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 +1 -1
- package/dist/pipeline/handlers/adaptive-coordinator.d.ts.map +1 -1
- package/dist/pipeline/handlers/adaptive-coordinator.js +42 -46
- 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/coordinator.d.ts +1 -0
- package/dist/pipeline/handlers/coordinator.d.ts.map +1 -1
- package/dist/pipeline/handlers/coordinator.js +39 -7
- package/dist/pipeline/handlers/coordinator.js.map +1 -1
- package/dist/pipeline/handlers/dag-coordinator.d.ts.map +1 -1
- package/dist/pipeline/handlers/dag-coordinator.js +17 -13
- 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 +5 -3
- package/dist/pipeline/handlers/pass-through.d.ts.map +1 -1
- package/dist/pipeline/handlers/pass-through.js +165 -91
- package/dist/pipeline/handlers/pass-through.js.map +1 -1
- package/dist/pipeline/handlers/subagent.d.ts.map +1 -1
- package/dist/pipeline/handlers/subagent.js +4 -1
- package/dist/pipeline/handlers/subagent.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 -54
- 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 +120 -298
- package/dist/pipeline/handlers/tool-loop.js.map +1 -1
- package/dist/pipeline/handlers/tool-response-round.d.ts +48 -0
- package/dist/pipeline/handlers/tool-response-round.d.ts.map +1 -0
- package/dist/pipeline/handlers/tool-response-round.js +104 -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/pipeline-to-stream.d.ts.map +1 -1
- package/dist/pipeline/pipeline-to-stream.js +13 -6
- package/dist/pipeline/pipeline-to-stream.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/resilience/retry-llm.d.ts +7 -5
- package/dist/resilience/retry-llm.d.ts.map +1 -1
- package/dist/resilience/retry-llm.js +93 -58
- package/dist/resilience/retry-llm.js.map +1 -1
- package/dist/subagent/default-context-builder.d.ts.map +1 -1
- package/dist/subagent/default-context-builder.js +9 -2
- package/dist/subagent/default-context-builder.js.map +1 -1
- package/dist/subagent/direct-llm-subagent.d.ts +1 -1
- package/dist/subagent/direct-llm-subagent.d.ts.map +1 -1
- package/dist/subagent/direct-llm-subagent.js +12 -3
- 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 +8 -5
- package/dist/subagent/smart-agent-subagent.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 { addCacheTokenUsage,
|
|
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() {
|
|
@@ -424,15 +460,33 @@ export class SmartAgent {
|
|
|
424
460
|
detectedAdapter = this.deps.clientAdapters.find((a) => a.detect(typeof systemMsg.content === 'string' ? systemMsg.content : ''));
|
|
425
461
|
}
|
|
426
462
|
}
|
|
427
|
-
const ephemeralSession = options?.sessionId === undefined
|
|
428
|
-
|
|
463
|
+
const ephemeralSession = options?.sessionId === undefined &&
|
|
464
|
+
options?.caller?.sessionId === undefined;
|
|
465
|
+
const sessionId = options?.sessionId ?? options?.caller?.sessionId ?? randomUUID();
|
|
429
466
|
const pipelinePendingResults = opts.pendingToolResults instanceof PendingToolResultsRegistry
|
|
430
467
|
? opts.pendingToolResults
|
|
431
468
|
: this.pendingToolResults;
|
|
432
469
|
const pipelineToolAvailability = opts.toolAvailability instanceof ToolAvailabilityRegistry
|
|
433
470
|
? opts.toolAvailability
|
|
434
471
|
: this.toolAvailabilityRegistry;
|
|
435
|
-
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
|
+
}
|
|
436
490
|
const externalTools = normalizeExternalTools(options?.externalTools);
|
|
437
491
|
opts?.sessionLogger?.logStep('external_tools_normalized', {
|
|
438
492
|
rawCount: options?.externalTools?.length ?? 0,
|
|
@@ -442,12 +496,20 @@ export class SmartAgent {
|
|
|
442
496
|
opts?.sessionLogger?.logStep('pipeline_start', { mode, textOrMessages });
|
|
443
497
|
this.requestLogger.startRequest(traceId);
|
|
444
498
|
try {
|
|
499
|
+
if (identity.kind === 'caller')
|
|
500
|
+
textOrMessages = await (this.deps.pipeline
|
|
501
|
+
? pipelinePendingResults
|
|
502
|
+
: this.pendingToolResults).prepareInput(identity.caller, textOrMessages, options?.externalResults);
|
|
445
503
|
if (mode === 'pass') {
|
|
446
|
-
const
|
|
504
|
+
const passPendingResults = this.deps.pipeline
|
|
505
|
+
? pipelinePendingResults
|
|
506
|
+
: this.pendingToolResults;
|
|
507
|
+
const inputMessages = typeof textOrMessages === 'string'
|
|
447
508
|
? [{ role: 'user', content: textOrMessages }]
|
|
448
509
|
: textOrMessages;
|
|
510
|
+
const passMessages = await injectPendingResults(withRequestTime(inputMessages, opts.requestStartedAt, this.config.timeZone), passPendingResults, identity, opts, options?.externalResults, textOrMessages);
|
|
449
511
|
opts?.sessionLogger?.logStep('client_request', { textOrMessages });
|
|
450
|
-
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)) {
|
|
451
513
|
yield chunk;
|
|
452
514
|
}
|
|
453
515
|
rootSpan.setStatus('ok');
|
|
@@ -492,6 +554,7 @@ export class SmartAgent {
|
|
|
492
554
|
});
|
|
493
555
|
const orchResult = await orchestrator.orchestrate(textOrMessages, {
|
|
494
556
|
opts,
|
|
557
|
+
identity,
|
|
495
558
|
rootSpan,
|
|
496
559
|
sessionId,
|
|
497
560
|
mode,
|
|
@@ -508,21 +571,21 @@ export class SmartAgent {
|
|
|
508
571
|
// it unused would trip noUnusedLocals.
|
|
509
572
|
const { retrieved, finalTools, toolsAvailable, assembledMessages, mainAction, toolClientMap, } = orchResult.value;
|
|
510
573
|
// 4. Single Streaming Loop
|
|
511
|
-
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);
|
|
512
575
|
for await (const chunk of stream)
|
|
513
576
|
yield chunk;
|
|
514
577
|
rootSpan.setStatus('ok');
|
|
515
578
|
}
|
|
516
579
|
finally {
|
|
517
|
-
if (ephemeralSession) {
|
|
580
|
+
if (ephemeralSession && identity.kind === 'caller') {
|
|
518
581
|
const pendingResults = this.deps.pipeline
|
|
519
582
|
? pipelinePendingResults
|
|
520
583
|
: this.pendingToolResults;
|
|
521
584
|
const toolAvailability = this.deps.pipeline
|
|
522
585
|
? pipelineToolAvailability
|
|
523
586
|
: this.toolAvailabilityRegistry;
|
|
524
|
-
pendingResults.clear(
|
|
525
|
-
toolAvailability.clear(
|
|
587
|
+
pendingResults.clear(identity.caller);
|
|
588
|
+
toolAvailability.clear(callerIdentityKey(identity.caller));
|
|
526
589
|
}
|
|
527
590
|
this.requestLogger.endRequest(traceId);
|
|
528
591
|
rootSpan.end();
|
|
@@ -530,7 +593,7 @@ export class SmartAgent {
|
|
|
530
593
|
this.metrics.requestLatency.record(Date.now() - requestStart);
|
|
531
594
|
}
|
|
532
595
|
}
|
|
533
|
-
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) {
|
|
534
597
|
const toolLoopSpan = this.tracer.startSpan('smart_agent.tool_loop', {
|
|
535
598
|
parent: parentSpan,
|
|
536
599
|
});
|
|
@@ -551,29 +614,19 @@ export class SmartAgent {
|
|
|
551
614
|
const timingLog = [];
|
|
552
615
|
const loopStart = Date.now();
|
|
553
616
|
let currentTools = activeTools;
|
|
617
|
+
const localBlocks = new Set();
|
|
554
618
|
messages = injectToolPriority(messages, externalTools);
|
|
555
619
|
// staticPrefix is immutable across all iterations — re-emitted every round.
|
|
556
|
-
const staticPrefix = messages;
|
|
620
|
+
const staticPrefix = await injectPendingResults(messages, this.pendingToolResults, identity, opts, opts?.externalResults, sourceInput);
|
|
557
621
|
const strategy = (this.deps.toolLoopContextStrategyFactory ??
|
|
558
622
|
(() => new LegacyAccumulateContextStrategy()))({ run: undefined });
|
|
559
|
-
//
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
const withPending = await injectPendingResults(messages, this.pendingToolResults, sessionId, opts);
|
|
563
|
-
if (withPending.length > staticPrefix.length) {
|
|
564
|
-
// Injected pending results form an assistant+tool group → record as round.
|
|
565
|
-
const injected = withPending.slice(staticPrefix.length);
|
|
566
|
-
const pendingRound = {
|
|
567
|
-
assistant: injected[0],
|
|
568
|
-
results: injected.slice(1),
|
|
569
|
-
};
|
|
570
|
-
await strategy.record(pendingRound);
|
|
571
|
-
}
|
|
572
|
-
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({
|
|
573
626
|
prefix: staticPrefix,
|
|
574
627
|
toolTrust: this.config.toolTrust,
|
|
575
628
|
queryText: _action.text,
|
|
576
|
-
}))
|
|
629
|
+
}));
|
|
577
630
|
const finalize = async function* (content, finishReason) {
|
|
578
631
|
opts?.sessionLogger?.logStep('final_response', { content, usage });
|
|
579
632
|
// onBeforeStream hook — consumer transforms content before streaming
|
|
@@ -796,7 +849,7 @@ export class SmartAgent {
|
|
|
796
849
|
}
|
|
797
850
|
}
|
|
798
851
|
currentTools = await withSubmitReport(currentTools, toolClientMap, opts, externalTools);
|
|
799
|
-
currentTools = filterAvailableTools(this.toolAvailabilityRegistry,
|
|
852
|
+
currentTools = filterAvailableTools(this.toolAvailabilityRegistry, identity, currentTools, iteration, opts, localBlocks);
|
|
800
853
|
if (this.config.maxToolCalls !== undefined &&
|
|
801
854
|
toolCallCount >= this.config.maxToolCalls) {
|
|
802
855
|
currentTools = [];
|
|
@@ -853,17 +906,13 @@ export class SmartAgent {
|
|
|
853
906
|
const toolCallsMap = new Map();
|
|
854
907
|
// Track which streaming indices belong to external tools so that
|
|
855
908
|
// argument-only continuation deltas (no name field) are forwarded too.
|
|
856
|
-
const externalToolIndices = new Set();
|
|
857
909
|
for await (const chunkResult of stream) {
|
|
858
910
|
if (!chunkResult.ok) {
|
|
859
911
|
llmSpan.setStatus('error', chunkResult.error.message);
|
|
860
912
|
llmSpan.end();
|
|
861
913
|
toolLoopSpan.setStatus('error', chunkResult.error.message);
|
|
862
914
|
toolLoopSpan.end();
|
|
863
|
-
yield
|
|
864
|
-
ok: false,
|
|
865
|
-
error: new OrchestratorError(chunkResult.error.message, 'LLM_ERROR'),
|
|
866
|
-
};
|
|
915
|
+
yield chunkResult;
|
|
867
916
|
return;
|
|
868
917
|
}
|
|
869
918
|
const chunk = chunkResult.value;
|
|
@@ -874,7 +923,6 @@ export class SmartAgent {
|
|
|
874
923
|
content = '';
|
|
875
924
|
iterationBuffer = '';
|
|
876
925
|
toolCallsMap.clear();
|
|
877
|
-
externalToolIndices.clear();
|
|
878
926
|
finishReason = undefined;
|
|
879
927
|
continue;
|
|
880
928
|
}
|
|
@@ -891,24 +939,7 @@ export class SmartAgent {
|
|
|
891
939
|
}
|
|
892
940
|
}
|
|
893
941
|
if (chunk.toolCalls) {
|
|
894
|
-
//
|
|
895
|
-
for (const tc of chunk.toolCalls) {
|
|
896
|
-
const name = getStreamToolCallName(tc);
|
|
897
|
-
if (name && externalToolNames.has(name)) {
|
|
898
|
-
const delta = toToolCallDelta(tc, 0);
|
|
899
|
-
externalToolIndices.add(delta.index);
|
|
900
|
-
}
|
|
901
|
-
}
|
|
902
|
-
const externalDeltas = chunk.toolCalls.filter((tc) => {
|
|
903
|
-
const delta = toToolCallDelta(tc, 0);
|
|
904
|
-
return externalToolIndices.has(delta.index);
|
|
905
|
-
});
|
|
906
|
-
if (!opts?.toolRoundGuard && externalDeltas.length > 0) {
|
|
907
|
-
yield {
|
|
908
|
-
ok: true,
|
|
909
|
-
value: { content: '', toolCalls: externalDeltas },
|
|
910
|
-
};
|
|
911
|
-
}
|
|
942
|
+
// Buffer calls until the complete response can be classified atomically.
|
|
912
943
|
for (const [fallbackIndex, rawToolCall,] of chunk.toolCalls.entries()) {
|
|
913
944
|
const tc = toToolCallDelta(rawToolCall, fallbackIndex);
|
|
914
945
|
if (!toolCallsMap.has(tc.index)) {
|
|
@@ -949,10 +980,21 @@ export class SmartAgent {
|
|
|
949
980
|
phase: `llm_call_${iteration + 1}`,
|
|
950
981
|
duration: llmCallDuration,
|
|
951
982
|
});
|
|
952
|
-
const
|
|
983
|
+
const hasToolCalls = finishReason === 'tool_calls' && toolCallsMap.size > 0;
|
|
984
|
+
const responseRound = new ToolResponseRound(content, toolCallsMap.values(), (name) => externalToolNames.has(name) && !toolClientMap.has(name));
|
|
985
|
+
const toolCalls = hasToolCalls ? responseRound.executableCalls() : [];
|
|
986
|
+
opts?.sessionLogger?.logStep(`llm_response_iter_${iteration + 1}`, { content, ...responseRound.diagnostics(toolCalls), finishReason }, 'llm');
|
|
987
|
+
if (finishReason === 'length') {
|
|
988
|
+
const error = new OrchestratorError('LLM response was truncated at a token limit.', 'LLM_OUTPUT_TRUNCATED');
|
|
989
|
+
toolLoopSpan.setStatus('error', error.message);
|
|
990
|
+
toolLoopSpan.end();
|
|
991
|
+
yield { ok: false, error };
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
const val = hasToolCalls
|
|
953
995
|
? { reprompt: false, messages }
|
|
954
996
|
: await runOutputValidationReprompt(this.outputValidator, content, messages, currentTools, opts, this.config.toolTrust);
|
|
955
|
-
const iterationEnd =
|
|
997
|
+
const iterationEnd = hasToolCalls
|
|
956
998
|
? 'tool_calls'
|
|
957
999
|
: val.reprompt
|
|
958
1000
|
? 'retry'
|
|
@@ -975,182 +1017,97 @@ export class SmartAgent {
|
|
|
975
1017
|
if (finishReason === 'tool_calls' &&
|
|
976
1018
|
(clientAdapter || this.config.streamMode === 'final'))
|
|
977
1019
|
stoppedContent += content;
|
|
978
|
-
|
|
979
|
-
let args = {};
|
|
980
|
-
try {
|
|
981
|
-
args = JSON.parse(tc.arguments);
|
|
982
|
-
}
|
|
983
|
-
catch {
|
|
984
|
-
args = {};
|
|
985
|
-
}
|
|
986
|
-
return { id: tc.id, name: tc.name, arguments: args };
|
|
987
|
-
});
|
|
988
|
-
opts?.sessionLogger?.logStep(`llm_response_iter_${iteration + 1}`, {
|
|
989
|
-
content,
|
|
990
|
-
toolCalls,
|
|
991
|
-
finishReason,
|
|
992
|
-
}, 'llm');
|
|
993
|
-
if (finishReason !== 'tool_calls' || toolCalls.length === 0) {
|
|
1020
|
+
if (!hasToolCalls) {
|
|
994
1021
|
// Output validation
|
|
995
1022
|
if (val.reprompt) {
|
|
996
|
-
//
|
|
997
|
-
// into the strategy — they are ephemeral and survive only until the
|
|
998
|
-
// next recorded tool round, at which point controlTail is pruned.
|
|
1023
|
+
// Anchor the rejected reply and correction before any newer tool rounds.
|
|
999
1024
|
const appended = val.messages.slice(messages.length);
|
|
1000
|
-
|
|
1001
|
-
messages = (await strategy.form({
|
|
1025
|
+
controlHistory.append(appended);
|
|
1026
|
+
messages = controlHistory.restore(await strategy.form({
|
|
1002
1027
|
prefix: staticPrefix,
|
|
1003
1028
|
toolTrust: this.config.toolTrust,
|
|
1004
1029
|
queryText: _action.text,
|
|
1005
|
-
}))
|
|
1030
|
+
}));
|
|
1006
1031
|
continue;
|
|
1007
1032
|
}
|
|
1008
1033
|
yield* finalize(content, finishReason);
|
|
1009
1034
|
return;
|
|
1010
1035
|
}
|
|
1011
1036
|
const roundRejections = guardToolRound(toolCalls, opts) ?? new Map();
|
|
1012
|
-
const { internalCalls, validExternalCalls, blockedCalls, hallucinations, } = classifyToolCalls(toolCalls, toolClientMap, externalToolNames, this.toolAvailabilityRegistry,
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
if (rejectedExternal.length > 0) {
|
|
1016
|
-
await strategy.record({
|
|
1017
|
-
assistant: {
|
|
1018
|
-
role: 'assistant',
|
|
1019
|
-
content: content || null,
|
|
1020
|
-
tool_calls: rejectedExternal.map((tc) => ({
|
|
1021
|
-
id: tc.id,
|
|
1022
|
-
type: 'function',
|
|
1023
|
-
function: {
|
|
1024
|
-
name: tc.name,
|
|
1025
|
-
arguments: JSON.stringify(tc.arguments),
|
|
1026
|
-
},
|
|
1027
|
-
})),
|
|
1028
|
-
},
|
|
1029
|
-
results: rejectedExternal.map((tc) => ({
|
|
1030
|
-
role: 'tool',
|
|
1031
|
-
tool_call_id: tc.id,
|
|
1032
|
-
content: `Error: ${roundRejections.get(toolCalls.indexOf(tc))}`,
|
|
1033
|
-
})),
|
|
1034
|
-
meta: rejectedExternal.map(() => ({ isError: true })),
|
|
1035
|
-
});
|
|
1036
|
-
for (const tc of rejectedExternal) {
|
|
1037
|
-
validExternalCalls.splice(validExternalCalls.indexOf(tc), 1);
|
|
1038
|
-
}
|
|
1039
|
-
controlTail.length = 0;
|
|
1040
|
-
messages = (await strategy.form({
|
|
1041
|
-
prefix: staticPrefix,
|
|
1042
|
-
toolTrust: this.config.toolTrust,
|
|
1043
|
-
queryText: _action.text,
|
|
1044
|
-
})).concat(controlTail);
|
|
1045
|
-
if (validExternalCalls.length === 0 &&
|
|
1046
|
-
internalCalls.length === 0 &&
|
|
1047
|
-
blockedCalls.length === 0 &&
|
|
1048
|
-
hallucinations.length === 0)
|
|
1049
|
-
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);
|
|
1050
1040
|
}
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
await strategy.record({
|
|
1054
|
-
assistant: group.assistant,
|
|
1055
|
-
results: group.results,
|
|
1056
|
-
});
|
|
1057
|
-
controlTail.length = 0;
|
|
1058
|
-
messages = (await strategy.form({
|
|
1059
|
-
prefix: staticPrefix,
|
|
1060
|
-
toolTrust: this.config.toolTrust,
|
|
1061
|
-
queryText: _action.text,
|
|
1062
|
-
})).concat(controlTail);
|
|
1063
|
-
continue;
|
|
1041
|
+
for (const tc of hallucinations) {
|
|
1042
|
+
responseRound.resolve(tc, `Error: Tool "${tc.name}" not found.`, true);
|
|
1064
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
|
+
});
|
|
1065
1051
|
// Truncate batch to remaining budget
|
|
1066
1052
|
const remaining = this.config.maxToolCalls !== undefined
|
|
1067
1053
|
? this.config.maxToolCalls - toolCallCount
|
|
1068
1054
|
: internalCalls.length;
|
|
1069
1055
|
const batch = internalCalls.slice(0, Math.max(0, remaining));
|
|
1070
1056
|
const skipped = internalCalls.slice(batch.length);
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
const
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
results: unknownCalls.map((tc) => ({
|
|
1088
|
-
role: 'tool',
|
|
1089
|
-
tool_call_id: tc.id,
|
|
1090
|
-
content: `Error: Tool "${tc.name}" not found.`,
|
|
1091
|
-
})),
|
|
1092
|
-
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,
|
|
1093
1073
|
});
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
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) {
|
|
1103
1085
|
yield {
|
|
1104
1086
|
ok: true,
|
|
1105
1087
|
value: {
|
|
1106
1088
|
content: '',
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
name: tc.name,
|
|
1111
|
-
arguments: JSON.stringify(tc.arguments),
|
|
1112
|
-
})),
|
|
1089
|
+
budgetExhausted: true,
|
|
1090
|
+
toolCallCount,
|
|
1091
|
+
maxToolCalls: this.config.maxToolCalls,
|
|
1113
1092
|
},
|
|
1114
1093
|
};
|
|
1115
1094
|
}
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
}
|
|
1133
|
-
catch {
|
|
1134
|
-
/* Observers cannot affect execution. */
|
|
1135
|
-
}
|
|
1136
|
-
}
|
|
1137
|
-
if (this.config.maxToolCalls !== undefined &&
|
|
1138
|
-
toolCallCount >= this.config.maxToolCalls) {
|
|
1139
|
-
yield {
|
|
1140
|
-
ok: true,
|
|
1141
|
-
value: {
|
|
1142
|
-
content: '',
|
|
1143
|
-
budgetExhausted: true,
|
|
1144
|
-
toolCallCount,
|
|
1145
|
-
maxToolCalls: this.config.maxToolCalls,
|
|
1146
|
-
},
|
|
1147
|
-
};
|
|
1148
|
-
}
|
|
1149
|
-
opts?.sessionLogger?.logStep('mixed_tool_calls', {
|
|
1150
|
-
internal: internalCalls.map((tc) => tc.name),
|
|
1151
|
-
external: validExternalCalls.map((tc) => tc.name),
|
|
1152
|
-
});
|
|
1153
|
-
}
|
|
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
|
+
};
|
|
1154
1111
|
timingLog.push({ phase: 'total', duration: Date.now() - loopStart });
|
|
1155
1112
|
toolLoopSpan.setStatus('ok');
|
|
1156
1113
|
toolLoopSpan.end();
|
|
@@ -1174,20 +1131,6 @@ export class SmartAgent {
|
|
|
1174
1131
|
// Capture the assistant message now; record the full round (assistant +
|
|
1175
1132
|
// tool results) atomically after the batch completes so the strategy
|
|
1176
1133
|
// always sees a complete ToolRound.
|
|
1177
|
-
const batchAssistantMsg = {
|
|
1178
|
-
role: 'assistant',
|
|
1179
|
-
content: content || null,
|
|
1180
|
-
tool_calls: toolCalls
|
|
1181
|
-
.filter((tc) => internalCalls.includes(tc) || hallucinations.includes(tc))
|
|
1182
|
-
.map((tc) => ({
|
|
1183
|
-
id: tc.id,
|
|
1184
|
-
type: 'function',
|
|
1185
|
-
function: {
|
|
1186
|
-
name: tc.name,
|
|
1187
|
-
arguments: JSON.stringify(tc.arguments),
|
|
1188
|
-
},
|
|
1189
|
-
})),
|
|
1190
|
-
};
|
|
1191
1134
|
const heartbeatMs = normalizeHeartbeatMs(this.config.heartbeatIntervalMs);
|
|
1192
1135
|
// Yield all progress messages before execution
|
|
1193
1136
|
for (const tc of batch) {
|
|
@@ -1201,6 +1144,8 @@ export class SmartAgent {
|
|
|
1201
1144
|
}
|
|
1202
1145
|
// Execute all tool calls concurrently with heartbeat (shared core).
|
|
1203
1146
|
const outcome = yield* executeToolBatchWithHeartbeat({
|
|
1147
|
+
callerResolution: identity,
|
|
1148
|
+
localBlocks,
|
|
1204
1149
|
batch,
|
|
1205
1150
|
roundCalls: toolCalls,
|
|
1206
1151
|
roundRejections,
|
|
@@ -1217,6 +1162,16 @@ export class SmartAgent {
|
|
|
1217
1162
|
timingLog,
|
|
1218
1163
|
heartbeatMs,
|
|
1219
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
|
+
},
|
|
1220
1175
|
mcpFailureClassifier: this.deps.mcpFailureClassifier,
|
|
1221
1176
|
maxToolCalls: this.config.maxToolCalls,
|
|
1222
1177
|
onToolExecuted: (r) => {
|
|
@@ -1260,33 +1215,11 @@ export class SmartAgent {
|
|
|
1260
1215
|
},
|
|
1261
1216
|
};
|
|
1262
1217
|
}
|
|
1263
|
-
|
|
1264
|
-
assistant: batchAssistantMsg,
|
|
1265
|
-
results: [
|
|
1266
|
-
...outcome.toolMessages,
|
|
1267
|
-
...hallucinations.map((tc) => ({
|
|
1268
|
-
role: 'tool',
|
|
1269
|
-
tool_call_id: tc.id,
|
|
1270
|
-
content: `Error: Tool "${tc.name}" not found.`,
|
|
1271
|
-
})),
|
|
1272
|
-
...skipped.map((tc) => ({
|
|
1273
|
-
role: 'tool',
|
|
1274
|
-
tool_call_id: tc.id,
|
|
1275
|
-
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`,
|
|
1276
|
-
})),
|
|
1277
|
-
],
|
|
1278
|
-
meta: [
|
|
1279
|
-
...outcome.resultMeta,
|
|
1280
|
-
...[...hallucinations, ...skipped].map(() => ({ isError: true })),
|
|
1281
|
-
],
|
|
1282
|
-
};
|
|
1283
|
-
await strategy.record(batchRound);
|
|
1284
|
-
controlTail.length = 0;
|
|
1285
|
-
messages = (await strategy.form({
|
|
1218
|
+
messages = controlHistory.restore(await strategy.form({
|
|
1286
1219
|
prefix: staticPrefix,
|
|
1287
1220
|
toolTrust: this.config.toolTrust,
|
|
1288
1221
|
queryText: _action.text,
|
|
1289
|
-
}))
|
|
1222
|
+
}));
|
|
1290
1223
|
}
|
|
1291
1224
|
}
|
|
1292
1225
|
}
|