@falai/agent 1.1.3 → 1.2.1
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 +9 -0
- package/dist/cjs/core/Agent.d.ts +17 -1
- package/dist/cjs/core/Agent.d.ts.map +1 -1
- package/dist/cjs/core/Agent.js +47 -0
- package/dist/cjs/core/Agent.js.map +1 -1
- package/dist/cjs/core/BatchPromptBuilder.d.ts +3 -0
- package/dist/cjs/core/BatchPromptBuilder.d.ts.map +1 -1
- package/dist/cjs/core/BatchPromptBuilder.js +4 -1
- package/dist/cjs/core/BatchPromptBuilder.js.map +1 -1
- package/dist/cjs/core/CompactionEngine.d.ts +65 -0
- package/dist/cjs/core/CompactionEngine.d.ts.map +1 -0
- package/dist/cjs/core/CompactionEngine.js +251 -0
- package/dist/cjs/core/CompactionEngine.js.map +1 -0
- package/dist/cjs/core/PromptComposer.d.ts +8 -1
- package/dist/cjs/core/PromptComposer.d.ts.map +1 -1
- package/dist/cjs/core/PromptComposer.js +238 -126
- package/dist/cjs/core/PromptComposer.js.map +1 -1
- package/dist/cjs/core/PromptSectionCache.d.ts +57 -0
- package/dist/cjs/core/PromptSectionCache.d.ts.map +1 -0
- package/dist/cjs/core/PromptSectionCache.js +108 -0
- package/dist/cjs/core/PromptSectionCache.js.map +1 -0
- package/dist/cjs/core/ResponseEngine.d.ts +3 -2
- package/dist/cjs/core/ResponseEngine.d.ts.map +1 -1
- package/dist/cjs/core/ResponseEngine.js +8 -8
- package/dist/cjs/core/ResponseEngine.js.map +1 -1
- package/dist/cjs/core/ResponseModal.d.ts.map +1 -1
- package/dist/cjs/core/ResponseModal.js +120 -70
- package/dist/cjs/core/ResponseModal.js.map +1 -1
- package/dist/cjs/core/ResponsePipeline.d.ts +2 -1
- package/dist/cjs/core/ResponsePipeline.d.ts.map +1 -1
- package/dist/cjs/core/ResponsePipeline.js +17 -19
- package/dist/cjs/core/ResponsePipeline.js.map +1 -1
- package/dist/cjs/core/RoutingEngine.d.ts +10 -0
- package/dist/cjs/core/RoutingEngine.d.ts.map +1 -1
- package/dist/cjs/core/RoutingEngine.js +5 -4
- package/dist/cjs/core/RoutingEngine.js.map +1 -1
- package/dist/cjs/core/SessionManager.d.ts.map +1 -1
- package/dist/cjs/core/SessionManager.js +20 -0
- package/dist/cjs/core/SessionManager.js.map +1 -1
- package/dist/cjs/core/StreamingToolExecutor.d.ts +142 -0
- package/dist/cjs/core/StreamingToolExecutor.d.ts.map +1 -0
- package/dist/cjs/core/StreamingToolExecutor.js +455 -0
- package/dist/cjs/core/StreamingToolExecutor.js.map +1 -0
- package/dist/cjs/core/ToolManager.d.ts +18 -1
- package/dist/cjs/core/ToolManager.d.ts.map +1 -1
- package/dist/cjs/core/ToolManager.js +91 -0
- package/dist/cjs/core/ToolManager.js.map +1 -1
- package/dist/cjs/index.d.ts +5 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +8 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/providers/AnthropicProvider.d.ts +7 -0
- package/dist/cjs/providers/AnthropicProvider.d.ts.map +1 -1
- package/dist/cjs/providers/AnthropicProvider.js +109 -19
- package/dist/cjs/providers/AnthropicProvider.js.map +1 -1
- package/dist/cjs/providers/GeminiProvider.d.ts +32 -0
- package/dist/cjs/providers/GeminiProvider.d.ts.map +1 -1
- package/dist/cjs/providers/GeminiProvider.js +160 -53
- package/dist/cjs/providers/GeminiProvider.js.map +1 -1
- package/dist/cjs/providers/OpenAIProvider.d.ts +5 -0
- package/dist/cjs/providers/OpenAIProvider.d.ts.map +1 -1
- package/dist/cjs/providers/OpenAIProvider.js +65 -18
- package/dist/cjs/providers/OpenAIProvider.js.map +1 -1
- package/dist/cjs/providers/OpenRouterProvider.d.ts +5 -0
- package/dist/cjs/providers/OpenRouterProvider.d.ts.map +1 -1
- package/dist/cjs/providers/OpenRouterProvider.js +57 -18
- package/dist/cjs/providers/OpenRouterProvider.js.map +1 -1
- package/dist/cjs/types/agent.d.ts +44 -0
- package/dist/cjs/types/agent.d.ts.map +1 -1
- package/dist/cjs/types/agent.js.map +1 -1
- package/dist/cjs/types/ai.d.ts +2 -2
- package/dist/cjs/types/ai.d.ts.map +1 -1
- package/dist/cjs/types/compaction.d.ts +50 -0
- package/dist/cjs/types/compaction.d.ts.map +1 -0
- package/dist/cjs/types/compaction.js +6 -0
- package/dist/cjs/types/compaction.js.map +1 -0
- package/dist/cjs/types/index.d.ts +4 -2
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/types/tool.d.ts +84 -0
- package/dist/cjs/types/tool.d.ts.map +1 -1
- package/dist/core/Agent.d.ts +17 -1
- package/dist/core/Agent.d.ts.map +1 -1
- package/dist/core/Agent.js +47 -0
- package/dist/core/Agent.js.map +1 -1
- package/dist/core/BatchPromptBuilder.d.ts +3 -0
- package/dist/core/BatchPromptBuilder.d.ts.map +1 -1
- package/dist/core/BatchPromptBuilder.js +4 -1
- package/dist/core/BatchPromptBuilder.js.map +1 -1
- package/dist/core/CompactionEngine.d.ts +65 -0
- package/dist/core/CompactionEngine.d.ts.map +1 -0
- package/dist/core/CompactionEngine.js +244 -0
- package/dist/core/CompactionEngine.js.map +1 -0
- package/dist/core/PromptComposer.d.ts +8 -1
- package/dist/core/PromptComposer.d.ts.map +1 -1
- package/dist/core/PromptComposer.js +238 -126
- package/dist/core/PromptComposer.js.map +1 -1
- package/dist/core/PromptSectionCache.d.ts +57 -0
- package/dist/core/PromptSectionCache.d.ts.map +1 -0
- package/dist/core/PromptSectionCache.js +104 -0
- package/dist/core/PromptSectionCache.js.map +1 -0
- package/dist/core/ResponseEngine.d.ts +3 -2
- package/dist/core/ResponseEngine.d.ts.map +1 -1
- package/dist/core/ResponseEngine.js +8 -8
- package/dist/core/ResponseEngine.js.map +1 -1
- package/dist/core/ResponseModal.d.ts.map +1 -1
- package/dist/core/ResponseModal.js +121 -71
- package/dist/core/ResponseModal.js.map +1 -1
- package/dist/core/ResponsePipeline.d.ts +2 -1
- package/dist/core/ResponsePipeline.d.ts.map +1 -1
- package/dist/core/ResponsePipeline.js +18 -20
- package/dist/core/ResponsePipeline.js.map +1 -1
- package/dist/core/RoutingEngine.d.ts +10 -0
- package/dist/core/RoutingEngine.d.ts.map +1 -1
- package/dist/core/RoutingEngine.js +6 -5
- package/dist/core/RoutingEngine.js.map +1 -1
- package/dist/core/SessionManager.d.ts.map +1 -1
- package/dist/core/SessionManager.js +17 -0
- package/dist/core/SessionManager.js.map +1 -1
- package/dist/core/StreamingToolExecutor.d.ts +142 -0
- package/dist/core/StreamingToolExecutor.d.ts.map +1 -0
- package/dist/core/StreamingToolExecutor.js +448 -0
- package/dist/core/StreamingToolExecutor.js.map +1 -0
- package/dist/core/ToolManager.d.ts +18 -1
- package/dist/core/ToolManager.d.ts.map +1 -1
- package/dist/core/ToolManager.js +91 -0
- package/dist/core/ToolManager.js.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/providers/AnthropicProvider.d.ts +7 -0
- package/dist/providers/AnthropicProvider.d.ts.map +1 -1
- package/dist/providers/AnthropicProvider.js +109 -19
- package/dist/providers/AnthropicProvider.js.map +1 -1
- package/dist/providers/GeminiProvider.d.ts +32 -0
- package/dist/providers/GeminiProvider.d.ts.map +1 -1
- package/dist/providers/GeminiProvider.js +160 -53
- package/dist/providers/GeminiProvider.js.map +1 -1
- package/dist/providers/OpenAIProvider.d.ts +5 -0
- package/dist/providers/OpenAIProvider.d.ts.map +1 -1
- package/dist/providers/OpenAIProvider.js +65 -18
- package/dist/providers/OpenAIProvider.js.map +1 -1
- package/dist/providers/OpenRouterProvider.d.ts +5 -0
- package/dist/providers/OpenRouterProvider.d.ts.map +1 -1
- package/dist/providers/OpenRouterProvider.js +57 -18
- package/dist/providers/OpenRouterProvider.js.map +1 -1
- package/dist/types/agent.d.ts +44 -0
- package/dist/types/agent.d.ts.map +1 -1
- package/dist/types/agent.js.map +1 -1
- package/dist/types/ai.d.ts +2 -2
- package/dist/types/ai.d.ts.map +1 -1
- package/dist/types/compaction.d.ts +50 -0
- package/dist/types/compaction.d.ts.map +1 -0
- package/dist/types/compaction.js +5 -0
- package/dist/types/compaction.js.map +1 -0
- package/dist/types/index.d.ts +4 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/tool.d.ts +84 -0
- package/dist/types/tool.d.ts.map +1 -1
- package/docs/api/overview.md +140 -0
- package/docs/core/tools/enhanced-tool.md +186 -0
- package/docs/core/tools/streaming-execution.md +161 -0
- package/docs/guides/context-compaction.md +96 -0
- package/docs/guides/prompt-optimization.md +164 -0
- package/examples/advanced-patterns/context-compaction.ts +223 -0
- package/examples/advanced-patterns/streaming-responses.ts +85 -7
- package/examples/tools/enhanced-tool-metadata.ts +268 -0
- package/examples/tools/streaming-tool-execution.ts +283 -0
- package/package.json +1 -1
- package/src/core/Agent.ts +58 -2
- package/src/core/BatchPromptBuilder.ts +4 -1
- package/src/core/CompactionEngine.ts +318 -0
- package/src/core/PromptComposer.ts +259 -156
- package/src/core/PromptSectionCache.ts +136 -0
- package/src/core/ResponseEngine.ts +7 -11
- package/src/core/ResponseModal.ts +133 -83
- package/src/core/ResponsePipeline.ts +22 -22
- package/src/core/RoutingEngine.ts +16 -5
- package/src/core/SessionManager.ts +19 -0
- package/src/core/StreamingToolExecutor.ts +572 -0
- package/src/core/ToolManager.ts +151 -41
- package/src/index.ts +14 -0
- package/src/providers/AnthropicProvider.ts +121 -24
- package/src/providers/GeminiProvider.ts +174 -54
- package/src/providers/OpenAIProvider.ts +77 -25
- package/src/providers/OpenRouterProvider.ts +68 -25
- package/src/types/agent.ts +45 -0
- package/src/types/ai.ts +2 -2
- package/src/types/compaction.ts +52 -0
- package/src/types/index.ts +35 -14
- package/src/types/tool.ts +108 -0
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.ResponseModal = exports.ResponseGenerationError = void 0;
|
|
8
|
-
const types_1 = require("../types");
|
|
9
8
|
const Step_1 = require("./Step");
|
|
10
9
|
const ResponseEngine_1 = require("./ResponseEngine");
|
|
11
10
|
const ResponsePipeline_1 = require("./ResponsePipeline");
|
|
@@ -51,14 +50,14 @@ class ResponseModal {
|
|
|
51
50
|
this.agent = agent;
|
|
52
51
|
this.options = options;
|
|
53
52
|
// Initialize response engine
|
|
54
|
-
this.responseEngine = new ResponseEngine_1.ResponseEngine();
|
|
53
|
+
this.responseEngine = new ResponseEngine_1.ResponseEngine(this.agent.promptSectionCache);
|
|
55
54
|
// Initialize response pipeline with agent dependencies
|
|
56
55
|
this.responsePipeline = new ResponsePipeline_1.ResponsePipeline(this.agent.getAgentOptions(), () => this.agent.getRoutes(), // Pass a function to get routes dynamically
|
|
57
56
|
this.agent.getTools(), this.agent.getRoutingEngine(), this.agent.updateContext.bind(this.agent), this.agent.getUpdateDataMethod(), this.agent.updateCollectedData.bind(this.agent), this.getToolManager());
|
|
58
57
|
// Initialize batch executor for multi-step execution
|
|
59
58
|
this.batchExecutor = new BatchExecutor_1.BatchExecutor();
|
|
60
59
|
// Initialize batch prompt builder for combined prompts
|
|
61
|
-
this.batchPromptBuilder = new BatchPromptBuilder_1.BatchPromptBuilder();
|
|
60
|
+
this.batchPromptBuilder = new BatchPromptBuilder_1.BatchPromptBuilder(this.agent.promptSectionCache);
|
|
62
61
|
}
|
|
63
62
|
/**
|
|
64
63
|
* Generate a non-streaming response using unified logic
|
|
@@ -447,12 +446,14 @@ class ResponseModal {
|
|
|
447
446
|
extractionPrompt.push(`Optional fields: ${route.optionalFields.join(', ')}`);
|
|
448
447
|
}
|
|
449
448
|
extractionPrompt.push(``, `Return ONLY the extracted data as JSON. If no data can be extracted, return an empty object {}.`);
|
|
449
|
+
// Convert Event[] to HistoryItem[] for provider call
|
|
450
|
+
const historyItems = (0, utils_1.eventsToHistory)(history);
|
|
450
451
|
// Call AI to extract data
|
|
451
452
|
const agentOptions = this.agent.getAgentOptions();
|
|
452
453
|
try {
|
|
453
454
|
const result = await agentOptions.provider.generateMessage({
|
|
454
455
|
prompt: extractionPrompt.join('\n'),
|
|
455
|
-
history,
|
|
456
|
+
history: historyItems,
|
|
456
457
|
context: {}, // Passed as empty object so AI doesn't "extract" from context
|
|
457
458
|
// NOTE: context is intentionally NOT passed here.
|
|
458
459
|
// Passing context caused the AI to "extract" data from the lead's context
|
|
@@ -480,7 +481,6 @@ class ResponseModal {
|
|
|
480
481
|
// Get last user message (needed for both route and completion handling)
|
|
481
482
|
// Convert HistoryItem[] to Event[] for internal processing
|
|
482
483
|
const historyEvents = (0, utils_1.historyToEvents)(history);
|
|
483
|
-
const lastMessageText = (0, utils_1.getLastMessageFromHistory)(historyEvents);
|
|
484
484
|
let message;
|
|
485
485
|
let toolCalls = undefined;
|
|
486
486
|
let executedSteps;
|
|
@@ -515,7 +515,6 @@ class ResponseModal {
|
|
|
515
515
|
session,
|
|
516
516
|
history,
|
|
517
517
|
context: effectiveContext,
|
|
518
|
-
lastMessageText,
|
|
519
518
|
historyEvents,
|
|
520
519
|
signal: undefined,
|
|
521
520
|
});
|
|
@@ -540,7 +539,7 @@ class ResponseModal {
|
|
|
540
539
|
selectedRoute,
|
|
541
540
|
session,
|
|
542
541
|
context: effectiveContext,
|
|
543
|
-
|
|
542
|
+
history,
|
|
544
543
|
historyEvents,
|
|
545
544
|
signal: undefined,
|
|
546
545
|
});
|
|
@@ -560,7 +559,7 @@ class ResponseModal {
|
|
|
560
559
|
else {
|
|
561
560
|
// Fallback: No routes defined, generate a simple response
|
|
562
561
|
message = await this.generateFallbackResponse({
|
|
563
|
-
history
|
|
562
|
+
history,
|
|
564
563
|
context: effectiveContext,
|
|
565
564
|
session,
|
|
566
565
|
});
|
|
@@ -646,7 +645,7 @@ class ResponseModal {
|
|
|
646
645
|
const agentOptions = this.agent.getAgentOptions();
|
|
647
646
|
const result = await agentOptions.provider.generateMessage({
|
|
648
647
|
prompt: batchPromptResult.prompt,
|
|
649
|
-
history
|
|
648
|
+
history, // Use HistoryItem[] for AI provider
|
|
650
649
|
context,
|
|
651
650
|
tools: availableTools,
|
|
652
651
|
signal,
|
|
@@ -802,7 +801,6 @@ class ResponseModal {
|
|
|
802
801
|
// Get last user message (needed for both route and completion handling)
|
|
803
802
|
// Convert HistoryItem[] to Event[] for internal processing
|
|
804
803
|
const historyEvents = (0, utils_1.historyToEvents)(history);
|
|
805
|
-
const lastMessageText = (0, utils_1.getLastMessageFromHistory)(historyEvents);
|
|
806
804
|
if (selectedRoute && !isRouteComplete) {
|
|
807
805
|
// Check if we have batch steps to execute
|
|
808
806
|
if (batchSteps && batchSteps.length > 0) {
|
|
@@ -829,7 +827,6 @@ class ResponseModal {
|
|
|
829
827
|
session,
|
|
830
828
|
history,
|
|
831
829
|
context: effectiveContext,
|
|
832
|
-
lastMessageText,
|
|
833
830
|
historyEvents,
|
|
834
831
|
});
|
|
835
832
|
}
|
|
@@ -840,14 +837,14 @@ class ResponseModal {
|
|
|
840
837
|
selectedRoute,
|
|
841
838
|
session,
|
|
842
839
|
context: effectiveContext,
|
|
843
|
-
|
|
840
|
+
history,
|
|
844
841
|
historyEvents,
|
|
845
842
|
});
|
|
846
843
|
}
|
|
847
844
|
else {
|
|
848
845
|
// Fallback: No routes defined, stream a simple response
|
|
849
846
|
yield* this.streamFallbackResponse({
|
|
850
|
-
history
|
|
847
|
+
history,
|
|
851
848
|
context: effectiveContext,
|
|
852
849
|
session,
|
|
853
850
|
});
|
|
@@ -861,7 +858,7 @@ class ResponseModal {
|
|
|
861
858
|
* @private
|
|
862
859
|
*/
|
|
863
860
|
async *streamBatchResponse(params) {
|
|
864
|
-
const { selectedRoute, batchSteps, context, historyEvents, batchStoppedReason, signal } = params;
|
|
861
|
+
const { selectedRoute, batchSteps, history, context, historyEvents, batchStoppedReason, signal } = params;
|
|
865
862
|
let session = params.session;
|
|
866
863
|
// Create hook executor function
|
|
867
864
|
const executeHook = async (hook, hookContext, data, step) => {
|
|
@@ -902,7 +899,7 @@ class ResponseModal {
|
|
|
902
899
|
const agentOptions = this.agent.getAgentOptions();
|
|
903
900
|
const stream = agentOptions.provider.generateMessageStream({
|
|
904
901
|
prompt: batchPromptResult.prompt,
|
|
905
|
-
history
|
|
902
|
+
history, // Use HistoryItem[] for AI provider
|
|
906
903
|
context,
|
|
907
904
|
tools: availableTools,
|
|
908
905
|
signal,
|
|
@@ -998,7 +995,7 @@ class ResponseModal {
|
|
|
998
995
|
* @private
|
|
999
996
|
*/
|
|
1000
997
|
async processRouteResponse(params) {
|
|
1001
|
-
const { selectedRoute, selectedStep, responseDirectives, history, context,
|
|
998
|
+
const { selectedRoute, selectedStep, responseDirectives, history, context, historyEvents, signal } = params;
|
|
1002
999
|
let session = params.session;
|
|
1003
1000
|
// Determine next step
|
|
1004
1001
|
let nextStep;
|
|
@@ -1065,8 +1062,7 @@ class ResponseModal {
|
|
|
1065
1062
|
rules: selectedRoute.getRules(),
|
|
1066
1063
|
prohibitions: selectedRoute.getProhibitions(),
|
|
1067
1064
|
directives: responseDirectives,
|
|
1068
|
-
history: historyEvents,
|
|
1069
|
-
lastMessage: lastMessageText, // Use string for buildResponsePrompt
|
|
1065
|
+
history: historyEvents,
|
|
1070
1066
|
agentOptions: this.agent.getAgentOptions(),
|
|
1071
1067
|
combinedGuidelines: [...this.agent.getGuidelines(), ...selectedRoute.getGuidelines()],
|
|
1072
1068
|
combinedTerms: this.mergeTerms(this.agent.getTerms(), selectedRoute.getTerms()),
|
|
@@ -1080,7 +1076,7 @@ class ResponseModal {
|
|
|
1080
1076
|
const agentOptions = this.agent.getAgentOptions();
|
|
1081
1077
|
const result = await agentOptions.provider.generateMessage({
|
|
1082
1078
|
prompt: responsePrompt,
|
|
1083
|
-
history
|
|
1079
|
+
history, // Use HistoryItem[] for AI provider
|
|
1084
1080
|
context,
|
|
1085
1081
|
tools: availableTools,
|
|
1086
1082
|
signal,
|
|
@@ -1122,7 +1118,7 @@ class ResponseModal {
|
|
|
1122
1118
|
* @private
|
|
1123
1119
|
*/
|
|
1124
1120
|
async *processRouteStreamingResponse(params) {
|
|
1125
|
-
const { selectedRoute, selectedStep, responseDirectives, history, context,
|
|
1121
|
+
const { selectedRoute, selectedStep, responseDirectives, history, context, historyEvents, signal } = params;
|
|
1126
1122
|
let session = params.session;
|
|
1127
1123
|
// Determine next step (same logic as non-streaming)
|
|
1128
1124
|
let nextStep;
|
|
@@ -1183,8 +1179,7 @@ class ResponseModal {
|
|
|
1183
1179
|
rules: selectedRoute.getRules(),
|
|
1184
1180
|
prohibitions: selectedRoute.getProhibitions(),
|
|
1185
1181
|
directives: responseDirectives,
|
|
1186
|
-
history: historyEvents,
|
|
1187
|
-
lastMessage: lastMessageText, // Use string for buildResponsePrompt
|
|
1182
|
+
history: historyEvents,
|
|
1188
1183
|
agentOptions: this.agent.getAgentOptions(),
|
|
1189
1184
|
combinedGuidelines: [...this.agent.getGuidelines(), ...selectedRoute.getGuidelines()],
|
|
1190
1185
|
combinedTerms: this.mergeTerms(this.agent.getTerms(), selectedRoute.getTerms()),
|
|
@@ -1198,7 +1193,7 @@ class ResponseModal {
|
|
|
1198
1193
|
const agentOptions = this.agent.getAgentOptions();
|
|
1199
1194
|
const stream = agentOptions.provider.generateMessageStream({
|
|
1200
1195
|
prompt: responsePrompt,
|
|
1201
|
-
history
|
|
1196
|
+
history, // Use HistoryItem[] for AI provider
|
|
1202
1197
|
context,
|
|
1203
1198
|
tools: availableTools,
|
|
1204
1199
|
signal,
|
|
@@ -1210,20 +1205,79 @@ class ResponseModal {
|
|
|
1210
1205
|
// Extract tool calls from AI response on final chunk
|
|
1211
1206
|
if (chunk.done && chunk.structured?.toolCalls) {
|
|
1212
1207
|
toolCalls = chunk.structured.toolCalls;
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1208
|
+
const toolManager = this.getToolManager();
|
|
1209
|
+
// Use concurrent execution for the initial batch of tool calls
|
|
1210
|
+
if (toolManager && typeof toolManager.executeWithConcurrency === 'function') {
|
|
1211
|
+
const toolCallRequests = toolCalls.map((tc, i) => ({
|
|
1212
|
+
id: `${tc.toolName}-${i}-${Date.now()}`,
|
|
1213
|
+
toolName: tc.toolName,
|
|
1214
|
+
arguments: tc.arguments,
|
|
1215
|
+
}));
|
|
1216
|
+
const historyEvents = (0, utils_1.historyToEvents)(history);
|
|
1217
|
+
try {
|
|
1218
|
+
for await (const update of toolManager.executeWithConcurrency({
|
|
1219
|
+
toolCalls: toolCallRequests,
|
|
1220
|
+
context,
|
|
1221
|
+
data: session.data,
|
|
1222
|
+
history: historyEvents,
|
|
1223
|
+
signal,
|
|
1224
|
+
route: selectedRoute,
|
|
1225
|
+
step: nextStep,
|
|
1226
|
+
})) {
|
|
1227
|
+
// Apply context updates
|
|
1228
|
+
if (update.contextUpdate) {
|
|
1229
|
+
try {
|
|
1230
|
+
await this.agent.updateContext(update.contextUpdate);
|
|
1231
|
+
}
|
|
1232
|
+
catch (error) {
|
|
1233
|
+
utils_1.logger.error(`[ResponseModal] Failed to update context from concurrent tool:`, error);
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
// Apply data updates
|
|
1237
|
+
if (update.dataUpdate) {
|
|
1238
|
+
try {
|
|
1239
|
+
const updateDataMethod = this.agent.getUpdateDataMethod();
|
|
1240
|
+
session = await updateDataMethod(session, update.dataUpdate);
|
|
1241
|
+
}
|
|
1242
|
+
catch (error) {
|
|
1243
|
+
utils_1.logger.error(`[ResponseModal] Failed to update data from concurrent tool:`, error);
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
// Yield progress updates immediately
|
|
1247
|
+
if (update.progress) {
|
|
1248
|
+
yield {
|
|
1249
|
+
delta: '',
|
|
1250
|
+
accumulated: chunk.accumulated,
|
|
1251
|
+
done: false,
|
|
1252
|
+
session,
|
|
1253
|
+
toolCalls: undefined,
|
|
1254
|
+
isRouteComplete: false,
|
|
1255
|
+
metadata: { toolProgress: update.progress, toolCallId: update.toolCallId },
|
|
1256
|
+
};
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
utils_1.logger.debug(`[ResponseModal] Concurrent tool execution completed for ${toolCallRequests.length} tools`);
|
|
1260
|
+
}
|
|
1261
|
+
catch (error) {
|
|
1262
|
+
utils_1.logger.error(`[ResponseModal] Concurrent tool execution failed, falling back to sequential:`, error);
|
|
1263
|
+
// Fall back to the unified tool loop on failure
|
|
1264
|
+
const toolResult = await this.executeUnifiedToolLoop({
|
|
1265
|
+
toolCalls, context, session, history, selectedRoute,
|
|
1266
|
+
responsePrompt, availableTools, responseSchema, signal,
|
|
1267
|
+
});
|
|
1268
|
+
session = toolResult.session;
|
|
1269
|
+
toolCalls = toolResult.finalToolCalls;
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
else {
|
|
1273
|
+
// Fallback: no ToolManager or no executeWithConcurrency, use unified tool loop
|
|
1274
|
+
const toolResult = await this.executeUnifiedToolLoop({
|
|
1275
|
+
toolCalls, context, session, history, selectedRoute,
|
|
1276
|
+
responsePrompt, availableTools, responseSchema, signal,
|
|
1277
|
+
});
|
|
1278
|
+
session = toolResult.session;
|
|
1279
|
+
toolCalls = toolResult.finalToolCalls;
|
|
1280
|
+
}
|
|
1227
1281
|
}
|
|
1228
1282
|
// Extract collected data on final chunk
|
|
1229
1283
|
if (chunk.done && chunk.structured && nextStep.collect) {
|
|
@@ -1340,33 +1394,33 @@ class ResponseModal {
|
|
|
1340
1394
|
while (hasToolCalls && toolLoopCount < MAX_TOOL_LOOPS) {
|
|
1341
1395
|
toolLoopCount++;
|
|
1342
1396
|
utils_1.logger.debug(`[ResponseModal] Starting tool loop ${toolLoopCount}/${MAX_TOOL_LOOPS} with ${toolCalls?.length || 0} tool calls`);
|
|
1343
|
-
// Create tool result
|
|
1344
|
-
const
|
|
1397
|
+
// Create tool result history items
|
|
1398
|
+
const toolResultHistoryItems = [];
|
|
1345
1399
|
for (const toolCall of toolCalls || []) {
|
|
1346
1400
|
const tool = this.findAvailableTool(toolCall.toolName, selectedRoute);
|
|
1347
1401
|
if (tool) {
|
|
1348
|
-
// Create
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1402
|
+
// Create HistoryItem format for tool results
|
|
1403
|
+
// Add assistant message with tool_calls
|
|
1404
|
+
toolResultHistoryItems.push({
|
|
1405
|
+
role: "assistant",
|
|
1406
|
+
content: null,
|
|
1407
|
+
tool_calls: [{
|
|
1408
|
+
id: toolCall.toolName,
|
|
1409
|
+
name: toolCall.toolName,
|
|
1410
|
+
arguments: toolCall.arguments,
|
|
1411
|
+
}],
|
|
1412
|
+
});
|
|
1413
|
+
// Add tool result
|
|
1414
|
+
toolResultHistoryItems.push({
|
|
1415
|
+
role: "tool",
|
|
1416
|
+
tool_call_id: toolCall.toolName,
|
|
1417
|
+
name: toolCall.toolName,
|
|
1418
|
+
content: "Tool executed successfully",
|
|
1419
|
+
});
|
|
1366
1420
|
}
|
|
1367
1421
|
}
|
|
1368
|
-
// Create updated history with tool results
|
|
1369
|
-
const
|
|
1422
|
+
// Create updated history with tool results
|
|
1423
|
+
const updatedHistory = [...history, ...toolResultHistoryItems];
|
|
1370
1424
|
// Make follow-up AI call to see if more tools are needed
|
|
1371
1425
|
// After first iteration, don't provide tools to force a text response
|
|
1372
1426
|
const agentOptions = this.agent.getAgentOptions();
|
|
@@ -1378,7 +1432,7 @@ class ResponseModal {
|
|
|
1378
1432
|
});
|
|
1379
1433
|
const followUpResult = await agentOptions.provider.generateMessage({
|
|
1380
1434
|
prompt: responsePrompt + (toolLoopCount > 1 ? "\n\nProvide a text response to the user based on the tool results." : ""),
|
|
1381
|
-
history:
|
|
1435
|
+
history: updatedHistory, // Use HistoryItem[] for AI provider
|
|
1382
1436
|
context,
|
|
1383
1437
|
tools: shouldProvideTools ? availableTools : [], // Only provide tools on first iteration
|
|
1384
1438
|
parameters: responseSchema ? {
|
|
@@ -1416,7 +1470,7 @@ class ResponseModal {
|
|
|
1416
1470
|
context,
|
|
1417
1471
|
updateContext: this.agent.updateContext.bind(this.agent),
|
|
1418
1472
|
updateData: this.agent.updateCollectedData.bind(this.agent),
|
|
1419
|
-
history:
|
|
1473
|
+
history: (0, utils_1.historyToEvents)(updatedHistory), // Convert to Event[] for tool execution
|
|
1420
1474
|
data: session.data,
|
|
1421
1475
|
toolArguments: toolCall.arguments,
|
|
1422
1476
|
});
|
|
@@ -1571,7 +1625,7 @@ class ResponseModal {
|
|
|
1571
1625
|
* @private
|
|
1572
1626
|
*/
|
|
1573
1627
|
async handleRouteCompletion(params) {
|
|
1574
|
-
const { selectedRoute, session, context,
|
|
1628
|
+
const { selectedRoute, session, context, history, historyEvents, signal } = params;
|
|
1575
1629
|
// Get endStep spec from route
|
|
1576
1630
|
const endStepSpec = selectedRoute.endStepSpec;
|
|
1577
1631
|
// Create a temporary step for completion message generation using endStep configuration
|
|
@@ -1621,7 +1675,6 @@ class ResponseModal {
|
|
|
1621
1675
|
completitionPrompt,
|
|
1622
1676
|
],
|
|
1623
1677
|
history: historyEvents,
|
|
1624
|
-
lastMessage: lastMessageText,
|
|
1625
1678
|
agentOptions: this.agent.getAgentOptions(),
|
|
1626
1679
|
combinedGuidelines: alwaysActiveGuidelines, // Only non-conditional guidelines
|
|
1627
1680
|
combinedTerms: this.mergeTerms(this.agent.getTerms(), selectedRoute.getTerms()),
|
|
@@ -1634,7 +1687,7 @@ class ResponseModal {
|
|
|
1634
1687
|
utils_1.logger.debug(`[ResponseModal] Calling AI provider for completion message...`);
|
|
1635
1688
|
const completionResult = await agentOptions.provider.generateMessage({
|
|
1636
1689
|
prompt: completionPrompt,
|
|
1637
|
-
history
|
|
1690
|
+
history, // Use HistoryItem[] for AI provider
|
|
1638
1691
|
context,
|
|
1639
1692
|
signal,
|
|
1640
1693
|
parameters: { jsonSchema: completionSchema, schemaName: "completion_message" },
|
|
@@ -1668,7 +1721,7 @@ class ResponseModal {
|
|
|
1668
1721
|
* @private
|
|
1669
1722
|
*/
|
|
1670
1723
|
async *streamRouteCompletion(params) {
|
|
1671
|
-
const { selectedRoute, context,
|
|
1724
|
+
const { selectedRoute, context, history, historyEvents, signal } = params;
|
|
1672
1725
|
let session = params.session;
|
|
1673
1726
|
// Get endStep spec from route
|
|
1674
1727
|
const endStepSpec = selectedRoute.endStepSpec;
|
|
@@ -1690,8 +1743,7 @@ class ResponseModal {
|
|
|
1690
1743
|
rules: selectedRoute.getRules(),
|
|
1691
1744
|
prohibitions: selectedRoute.getProhibitions(),
|
|
1692
1745
|
directives: undefined, // No directives for completion
|
|
1693
|
-
history: historyEvents,
|
|
1694
|
-
lastMessage: lastMessageText, // Use string for buildResponsePrompt
|
|
1746
|
+
history: historyEvents,
|
|
1695
1747
|
agentOptions: this.agent.getAgentOptions(),
|
|
1696
1748
|
combinedGuidelines: [...this.agent.getGuidelines(), ...selectedRoute.getGuidelines()],
|
|
1697
1749
|
combinedTerms: this.mergeTerms(this.agent.getTerms(), selectedRoute.getTerms()),
|
|
@@ -1703,7 +1755,7 @@ class ResponseModal {
|
|
|
1703
1755
|
const agentOptions = this.agent.getAgentOptions();
|
|
1704
1756
|
const stream = agentOptions.provider.generateMessageStream({
|
|
1705
1757
|
prompt: completionPrompt,
|
|
1706
|
-
history
|
|
1758
|
+
history, // Use HistoryItem[] for AI provider
|
|
1707
1759
|
context,
|
|
1708
1760
|
signal,
|
|
1709
1761
|
parameters: { jsonSchema: responseSchema, schemaName: "completion_message_stream" },
|
|
@@ -1767,7 +1819,6 @@ class ResponseModal {
|
|
|
1767
1819
|
utils_1.logger.debug(`[ResponseModal] No route selected, generating basic response`);
|
|
1768
1820
|
// Build basic response prompt without route context
|
|
1769
1821
|
const fallbackPrompt = await this.responseEngine.buildFallbackPrompt({
|
|
1770
|
-
history,
|
|
1771
1822
|
agentOptions: this.agent.getAgentOptions(),
|
|
1772
1823
|
terms: this.agent.getTerms(),
|
|
1773
1824
|
guidelines: this.agent.getGuidelines(),
|
|
@@ -1799,7 +1850,6 @@ class ResponseModal {
|
|
|
1799
1850
|
async *streamFallbackResponse(params) {
|
|
1800
1851
|
const { history, context, session, signal } = params;
|
|
1801
1852
|
const fallbackPrompt = await this.responseEngine.buildFallbackPrompt({
|
|
1802
|
-
history,
|
|
1803
1853
|
agentOptions: this.agent.getAgentOptions(),
|
|
1804
1854
|
terms: this.agent.getTerms(),
|
|
1805
1855
|
guidelines: this.agent.getGuidelines(),
|