@falai/agent 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -4
- package/dist/cjs/core/Agent.d.ts +10 -0
- package/dist/cjs/core/Agent.d.ts.map +1 -1
- package/dist/cjs/core/Agent.js +22 -3
- package/dist/cjs/core/Agent.js.map +1 -1
- package/dist/cjs/core/BatchExecutor.d.ts.map +1 -1
- package/dist/cjs/core/BatchExecutor.js +8 -0
- package/dist/cjs/core/BatchExecutor.js.map +1 -1
- package/dist/cjs/core/BatchPromptBuilder.d.ts.map +1 -1
- package/dist/cjs/core/BatchPromptBuilder.js +16 -0
- package/dist/cjs/core/BatchPromptBuilder.js.map +1 -1
- package/dist/cjs/core/ResponseEngine.d.ts.map +1 -1
- package/dist/cjs/core/ResponseEngine.js +71 -62
- 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 +71 -13
- package/dist/cjs/core/ResponseModal.js.map +1 -1
- package/dist/cjs/core/RoutingEngine.d.ts +8 -9
- package/dist/cjs/core/RoutingEngine.d.ts.map +1 -1
- package/dist/cjs/core/RoutingEngine.js +29 -23
- package/dist/cjs/core/RoutingEngine.js.map +1 -1
- package/dist/cjs/types/agent.d.ts +11 -0
- package/dist/cjs/types/agent.d.ts.map +1 -1
- package/dist/cjs/types/routing.d.ts +0 -4
- package/dist/cjs/types/routing.d.ts.map +1 -1
- package/dist/core/Agent.d.ts +10 -0
- package/dist/core/Agent.d.ts.map +1 -1
- package/dist/core/Agent.js +22 -3
- package/dist/core/Agent.js.map +1 -1
- package/dist/core/BatchExecutor.d.ts.map +1 -1
- package/dist/core/BatchExecutor.js +8 -0
- package/dist/core/BatchExecutor.js.map +1 -1
- package/dist/core/BatchPromptBuilder.d.ts.map +1 -1
- package/dist/core/BatchPromptBuilder.js +17 -1
- package/dist/core/BatchPromptBuilder.js.map +1 -1
- package/dist/core/ResponseEngine.d.ts.map +1 -1
- package/dist/core/ResponseEngine.js +71 -62
- package/dist/core/ResponseEngine.js.map +1 -1
- package/dist/core/ResponseModal.d.ts.map +1 -1
- package/dist/core/ResponseModal.js +71 -13
- package/dist/core/ResponseModal.js.map +1 -1
- package/dist/core/RoutingEngine.d.ts +8 -9
- package/dist/core/RoutingEngine.d.ts.map +1 -1
- package/dist/core/RoutingEngine.js +29 -23
- package/dist/core/RoutingEngine.js.map +1 -1
- package/dist/types/agent.d.ts +11 -0
- package/dist/types/agent.d.ts.map +1 -1
- package/dist/types/routing.d.ts +0 -4
- package/dist/types/routing.d.ts.map +1 -1
- package/docs/README.md +7 -17
- package/docs/api/README.md +28 -16
- package/docs/api/overview.md +4 -0
- package/docs/architecture/data-extraction-flow.md +0 -1
- package/docs/core/agent/README.md +36 -0
- package/docs/core/agent/context-management.md +6 -6
- package/docs/core/agent/rules-and-prohibitions.md +113 -0
- package/docs/core/agent/session-management.md +3 -4
- package/docs/core/routing/intelligent-routing.md +12 -8
- package/docs/guides/getting-started/README.md +10 -13
- package/docs/guides/migration/README.md +6 -2
- package/docs/guides/migration/multi-step-execution.md +70 -0
- package/docs/guides/migration/response-modal-refactor.md +2 -2
- package/package.json +1 -1
- package/src/core/Agent.ts +25 -3
- package/src/core/BatchExecutor.ts +10 -0
- package/src/core/BatchPromptBuilder.ts +19 -1
- package/src/core/ResponseEngine.ts +91 -91
- package/src/core/ResponseModal.ts +85 -27
- package/src/core/RoutingEngine.ts +63 -50
- package/src/types/agent.ts +11 -0
- package/src/types/routing.ts +0 -5
|
@@ -2,6 +2,76 @@
|
|
|
2
2
|
|
|
3
3
|
This guide covers the behavioral changes from single-step to multi-step execution and provides migration guidance for existing routes.
|
|
4
4
|
|
|
5
|
+
## Breaking Changes in v1.0.0
|
|
6
|
+
|
|
7
|
+
### History API Simplified
|
|
8
|
+
|
|
9
|
+
The `createMessageEvent` and `EventSource` exports have been replaced with simpler helper functions:
|
|
10
|
+
|
|
11
|
+
**Before (v0.x):**
|
|
12
|
+
```typescript
|
|
13
|
+
import { createMessageEvent, EventSource } from "@falai/agent";
|
|
14
|
+
|
|
15
|
+
const history = [
|
|
16
|
+
createMessageEvent(EventSource.CUSTOMER, "Hello"),
|
|
17
|
+
createMessageEvent(EventSource.AI_AGENT, "Hi there!"),
|
|
18
|
+
];
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**After (v1.0.0):**
|
|
22
|
+
```typescript
|
|
23
|
+
import { userMessage, assistantMessage } from "@falai/agent";
|
|
24
|
+
|
|
25
|
+
const history = [
|
|
26
|
+
userMessage("Hello"),
|
|
27
|
+
assistantMessage("Hi there!"),
|
|
28
|
+
];
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The history is now a simple array of objects with `role` and `content` properties:
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
// Available helper functions
|
|
35
|
+
import {
|
|
36
|
+
userMessage, // (content, name?) => { role: "user", content, name? }
|
|
37
|
+
assistantMessage, // (content, toolCalls?) => { role: "assistant", content, tool_calls? }
|
|
38
|
+
toolMessage, // (toolCallId, name, content) => { role: "tool", ... }
|
|
39
|
+
systemMessage, // (content) => { role: "system", content }
|
|
40
|
+
} from "@falai/agent";
|
|
41
|
+
|
|
42
|
+
// Or create history items directly
|
|
43
|
+
const history = [
|
|
44
|
+
{ role: "user", content: "Hello" },
|
|
45
|
+
{ role: "assistant", content: "Hi there!" },
|
|
46
|
+
];
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### StepOptions: `instructions` → `prompt`
|
|
50
|
+
|
|
51
|
+
The `instructions` property in `StepOptions` has been renamed to `prompt`:
|
|
52
|
+
|
|
53
|
+
**Before:**
|
|
54
|
+
```typescript
|
|
55
|
+
steps: [
|
|
56
|
+
{
|
|
57
|
+
id: "greeting",
|
|
58
|
+
instructions: "Greet the user warmly",
|
|
59
|
+
}
|
|
60
|
+
]
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
**After:**
|
|
64
|
+
```typescript
|
|
65
|
+
steps: [
|
|
66
|
+
{
|
|
67
|
+
id: "greeting",
|
|
68
|
+
prompt: "Greet the user warmly",
|
|
69
|
+
}
|
|
70
|
+
]
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
5
75
|
## Overview
|
|
6
76
|
|
|
7
77
|
Multi-step execution is a **major behavioral change** that allows multiple consecutive steps to execute in a single LLM call. While the public API shape remains compatible, the execution semantics differ from the previous single-step model.
|
|
@@ -513,6 +513,6 @@ The modern `stream()` API is the recommended approach for new streaming implemen
|
|
|
513
513
|
---
|
|
514
514
|
|
|
515
515
|
**Need Help?**
|
|
516
|
-
- Check the [examples](
|
|
516
|
+
- Check the [examples](../../../examples/) directory for complete working examples
|
|
517
517
|
- Review the [API documentation](../../api/) for detailed method signatures
|
|
518
|
-
- Look at the [streaming examples](
|
|
518
|
+
- Look at the [streaming examples](../../../examples/advanced-patterns/streaming-responses.ts) for side-by-side comparisons
|
package/package.json
CHANGED
package/src/core/Agent.ts
CHANGED
|
@@ -67,6 +67,8 @@ export class Agent<TContext = any, TData = any> {
|
|
|
67
67
|
private guidelines: Guideline<TContext, TData>[] = [];
|
|
68
68
|
private tools: Tool<TContext, TData>[] = [];
|
|
69
69
|
private routes: Route<TContext, TData>[] = [];
|
|
70
|
+
private agentRules: Template<TContext, TData>[] = [];
|
|
71
|
+
private agentProhibitions: Template<TContext, TData>[] = [];
|
|
70
72
|
private context: TContext | undefined;
|
|
71
73
|
private persistenceManager: PersistenceManager<TData> | undefined;
|
|
72
74
|
private routingEngine: RoutingEngine<TContext, TData>;
|
|
@@ -124,9 +126,7 @@ export class Agent<TContext = any, TData = any> {
|
|
|
124
126
|
|
|
125
127
|
// Initialize routing engine
|
|
126
128
|
this.routingEngine = new RoutingEngine<TContext, TData>({
|
|
127
|
-
|
|
128
|
-
allowRouteSwitch: true,
|
|
129
|
-
switchThreshold: 70,
|
|
129
|
+
routeSwitchMargin: options.routeSwitchMargin,
|
|
130
130
|
});
|
|
131
131
|
|
|
132
132
|
// Initialize ResponseModal for handling all response generation
|
|
@@ -185,6 +185,14 @@ export class Agent<TContext = any, TData = any> {
|
|
|
185
185
|
});
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
+
// Initialize agent-level rules and prohibitions
|
|
189
|
+
if (options.rules) {
|
|
190
|
+
this.agentRules = [...options.rules];
|
|
191
|
+
}
|
|
192
|
+
if (options.prohibitions) {
|
|
193
|
+
this.agentProhibitions = [...options.prohibitions];
|
|
194
|
+
}
|
|
195
|
+
|
|
188
196
|
if (options.routes) {
|
|
189
197
|
options.routes.forEach((routeOptions) => {
|
|
190
198
|
this.createRoute(routeOptions);
|
|
@@ -677,6 +685,20 @@ export class Agent<TContext = any, TData = any> {
|
|
|
677
685
|
return [...this.guidelines];
|
|
678
686
|
}
|
|
679
687
|
|
|
688
|
+
/**
|
|
689
|
+
* Get agent-level rules
|
|
690
|
+
*/
|
|
691
|
+
getRules(): Template<TContext, TData>[] {
|
|
692
|
+
return [...this.agentRules];
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
* Get agent-level prohibitions
|
|
697
|
+
*/
|
|
698
|
+
getProhibitions(): Template<TContext, TData>[] {
|
|
699
|
+
return [...this.agentProhibitions];
|
|
700
|
+
}
|
|
701
|
+
|
|
680
702
|
/**
|
|
681
703
|
* Evaluate and match active guidelines based on their conditions
|
|
682
704
|
* Returns guidelines that should be active given the current context
|
|
@@ -326,6 +326,16 @@ export class BatchExecutor<TContext = unknown, TData = unknown> {
|
|
|
326
326
|
field => (sessionData as Record<string, unknown>)[String(field)] === undefined
|
|
327
327
|
) || [];
|
|
328
328
|
const collectFields = step.collect || [];
|
|
329
|
+
|
|
330
|
+
// Warn about missing required fields from previous steps
|
|
331
|
+
if (missingRequires.length > 0) {
|
|
332
|
+
const warning = `[Agent] Step "${step.description || step.id}" requires data [${missingRequires.join(', ')}] that was not collected by previous steps. ` +
|
|
333
|
+
`Ensure earlier steps collect these fields before this step can proceed.`;
|
|
334
|
+
logger.warn(warning);
|
|
335
|
+
// Also log to console for developer visibility
|
|
336
|
+
console.warn(warning);
|
|
337
|
+
}
|
|
338
|
+
|
|
329
339
|
logger.debug(`[BatchExecutor] Step ${step.id} needs input, stopping batch. Missing requires: [${missingRequires.join(', ')}], Collect fields: [${collectFields.join(', ')}]`);
|
|
330
340
|
|
|
331
341
|
// Emit batch_stop event (Requirement 11.3)
|
|
@@ -12,7 +12,7 @@ import type { AgentOptions } from '../types/agent';
|
|
|
12
12
|
import type { SessionState } from '../types/session';
|
|
13
13
|
import type { Event } from '../types/history';
|
|
14
14
|
import type { Route } from './Route';
|
|
15
|
-
import { render, createTemplateContext } from '../utils/template';
|
|
15
|
+
import { render, renderMany, createTemplateContext } from '../utils/template';
|
|
16
16
|
import { PromptComposer } from './PromptComposer';
|
|
17
17
|
|
|
18
18
|
/**
|
|
@@ -120,6 +120,24 @@ export class BatchPromptBuilder<TContext = unknown, TData = unknown> {
|
|
|
120
120
|
const allGuidelines = [...(agentOptions.guidelines || []), ...route.getGuidelines()];
|
|
121
121
|
await composer.addGuidelines(allGuidelines);
|
|
122
122
|
|
|
123
|
+
// Add combined rules (agent + route)
|
|
124
|
+
const allRules = [...(agentOptions.rules || []), ...route.getRules()];
|
|
125
|
+
if (allRules.length > 0) {
|
|
126
|
+
const renderedRules = await renderMany(allRules, templateContext);
|
|
127
|
+
if (renderedRules.length > 0) {
|
|
128
|
+
await composer.addInstruction(`Rules:\n- ${renderedRules.join('\n- ')}`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Add combined prohibitions (agent + route)
|
|
133
|
+
const allProhibitions = [...(agentOptions.prohibitions || []), ...route.getProhibitions()];
|
|
134
|
+
if (allProhibitions.length > 0) {
|
|
135
|
+
const renderedProhibitions = await renderMany(allProhibitions, templateContext);
|
|
136
|
+
if (renderedProhibitions.length > 0) {
|
|
137
|
+
await composer.addInstruction(`Prohibitions:\n- ${renderedProhibitions.join('\n- ')}`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
123
141
|
// Add interaction history
|
|
124
142
|
await composer.addInteractionHistory(history, 'Recent conversation context:');
|
|
125
143
|
|
|
@@ -44,36 +44,47 @@ export interface BuildFallbackPromptParams<TContext = unknown, TData = unknown>
|
|
|
44
44
|
|
|
45
45
|
export class ResponseEngine<TContext = unknown, TData = unknown> {
|
|
46
46
|
responseSchemaForRoute(
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
47
|
+
route: Route<TContext, TData>,
|
|
48
|
+
currentStep?: Step<TContext, TData>,
|
|
49
|
+
agentSchema?: StructuredSchema
|
|
50
|
+
): StructuredSchema {
|
|
51
|
+
const base: StructuredSchema = {
|
|
52
|
+
type: "object",
|
|
53
|
+
properties: {
|
|
54
|
+
message: { type: "string", description: "Final user-facing message" },
|
|
55
|
+
},
|
|
56
|
+
required: ["message"],
|
|
57
|
+
additionalProperties: false,
|
|
58
|
+
};
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
// Add data field only if route has responseOutputSchema
|
|
61
|
+
if (route.responseOutputSchema) {
|
|
62
|
+
base.properties!.data = route.responseOutputSchema;
|
|
63
|
+
}
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
65
|
+
// Add collect fields from current step
|
|
66
|
+
if (currentStep?.collect) {
|
|
67
|
+
if (agentSchema?.properties) {
|
|
68
|
+
// Use agent schema definitions for collect fields
|
|
69
|
+
for (const field of currentStep.collect) {
|
|
70
|
+
const fieldSchema = agentSchema.properties[field as string];
|
|
71
|
+
if (fieldSchema) {
|
|
72
|
+
base.properties![field as string] = fieldSchema;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
// No agent schema - generate dynamic schema from collect fields
|
|
77
|
+
for (const field of currentStep.collect) {
|
|
78
|
+
base.properties![field as string] = {
|
|
79
|
+
type: "string",
|
|
80
|
+
description: `Collected value for ${String(field)}`,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
71
83
|
}
|
|
72
84
|
}
|
|
73
|
-
}
|
|
74
85
|
|
|
75
|
-
|
|
76
|
-
|
|
86
|
+
return base;
|
|
87
|
+
}
|
|
77
88
|
|
|
78
89
|
async buildResponsePrompt(
|
|
79
90
|
params: BuildResponsePromptParams<TContext, TData>
|
|
@@ -146,30 +157,25 @@ export class ResponseEngine<TContext = unknown, TData = unknown> {
|
|
|
146
157
|
await pc.addLastMessage(lastMessage);
|
|
147
158
|
|
|
148
159
|
// Add data collection instructions - include ALL route fields, not just current step
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
currentStep.collect.forEach(field => allRouteFields.add(String(field)));
|
|
166
|
-
}
|
|
160
|
+
// Collect all fields from route's required and optional fields
|
|
161
|
+
const allRouteFields = new Set<string>();
|
|
162
|
+
|
|
163
|
+
if (route.requiredFields) {
|
|
164
|
+
route.requiredFields.forEach(field => allRouteFields.add(String(field)));
|
|
165
|
+
}
|
|
166
|
+
if (route.optionalFields) {
|
|
167
|
+
route.optionalFields.forEach(field => allRouteFields.add(String(field)));
|
|
168
|
+
}
|
|
169
|
+
if (currentStep?.collect) {
|
|
170
|
+
currentStep.collect.forEach(field => allRouteFields.add(String(field)));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (allRouteFields.size > 0) {
|
|
174
|
+
const stepCollectFields = new Set(currentStep?.collect?.map(f => String(f)) || []);
|
|
175
|
+
const fieldDescriptions: string[] = [];
|
|
167
176
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
const fieldDescriptions: string[] = [];
|
|
171
|
-
|
|
172
|
-
for (const field of allRouteFields) {
|
|
177
|
+
for (const field of allRouteFields) {
|
|
178
|
+
if (agentSchema?.properties) {
|
|
173
179
|
const fieldSchema = agentSchema.properties[field];
|
|
174
180
|
if (fieldSchema) {
|
|
175
181
|
const fieldName = field;
|
|
@@ -193,33 +199,40 @@ export class ResponseEngine<TContext = unknown, TData = unknown> {
|
|
|
193
199
|
|
|
194
200
|
fieldDescriptions.push(fieldInfo);
|
|
195
201
|
}
|
|
202
|
+
} else {
|
|
203
|
+
// No agent schema - generate dynamic description from field name
|
|
204
|
+
let fieldInfo = ` • ${field} (string): ${field}`;
|
|
205
|
+
if (stepCollectFields.has(field)) {
|
|
206
|
+
fieldInfo += ` ← FOCUS FOR THIS STEP`;
|
|
207
|
+
}
|
|
208
|
+
fieldDescriptions.push(fieldInfo);
|
|
196
209
|
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (fieldDescriptions.length > 0) {
|
|
213
|
+
const instruction = [
|
|
214
|
+
`## Data Collection Rules`,
|
|
215
|
+
``,
|
|
216
|
+
`CRITICAL: You MUST extract ALL relevant information from the user's message, not just what this step asks for.`,
|
|
217
|
+
``,
|
|
218
|
+
`Available fields to extract:`,
|
|
219
|
+
...fieldDescriptions,
|
|
220
|
+
``,
|
|
221
|
+
`**How to collect data:**`,
|
|
222
|
+
`1. Read the user's message carefully`,
|
|
223
|
+
`2. Extract EVERY piece of information that matches ANY field above`,
|
|
224
|
+
`3. Users often provide multiple details at once (e.g., "I need a checkup next Tuesday at 2 PM")`,
|
|
225
|
+
`4. Include ALL extracted fields in your JSON response as top-level properties`,
|
|
226
|
+
`5. Field names must match EXACTLY as shown above`,
|
|
227
|
+
`6. Only include fields that the user actually mentioned`,
|
|
228
|
+
``,
|
|
229
|
+
`**Example:** If user says "I need a checkup next Tuesday at 2 PM", extract:`,
|
|
230
|
+
`- appointmentType: "checkup"`,
|
|
231
|
+
`- preferredDate: "next Tuesday"`,
|
|
232
|
+
`- preferredTime: "2 PM"`,
|
|
233
|
+
].join('\n');
|
|
197
234
|
|
|
198
|
-
|
|
199
|
-
const instruction = [
|
|
200
|
-
`## Data Collection Rules`,
|
|
201
|
-
``,
|
|
202
|
-
`CRITICAL: You MUST extract ALL relevant information from the user's message, not just what this step asks for.`,
|
|
203
|
-
``,
|
|
204
|
-
`Available fields to extract:`,
|
|
205
|
-
...fieldDescriptions,
|
|
206
|
-
``,
|
|
207
|
-
`**How to collect data:**`,
|
|
208
|
-
`1. Read the user's message carefully`,
|
|
209
|
-
`2. Extract EVERY piece of information that matches ANY field above`,
|
|
210
|
-
`3. Users often provide multiple details at once (e.g., "I need a checkup next Tuesday at 2 PM")`,
|
|
211
|
-
`4. Include ALL extracted fields in your JSON response as top-level properties`,
|
|
212
|
-
`5. Field names must match EXACTLY as shown above`,
|
|
213
|
-
`6. Only include fields that the user actually mentioned`,
|
|
214
|
-
``,
|
|
215
|
-
`**Example:** If user says "I need a checkup next Tuesday at 2 PM", extract:`,
|
|
216
|
-
`- appointmentType: "checkup"`,
|
|
217
|
-
`- preferredDate: "next Tuesday"`,
|
|
218
|
-
`- preferredTime: "2 PM"`,
|
|
219
|
-
].join('\n');
|
|
220
|
-
|
|
221
|
-
await pc.addInstruction(instruction);
|
|
222
|
-
}
|
|
235
|
+
await pc.addInstruction(instruction);
|
|
223
236
|
}
|
|
224
237
|
}
|
|
225
238
|
|
|
@@ -227,24 +240,8 @@ export class ResponseEngine<TContext = unknown, TData = unknown> {
|
|
|
227
240
|
// Generate example JSON based on actual schema fields
|
|
228
241
|
const exampleFields: string[] = [' "message": "your response to the user"'];
|
|
229
242
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
const allRouteFields = new Set<string>();
|
|
233
|
-
|
|
234
|
-
if (route.requiredFields) {
|
|
235
|
-
route.requiredFields.forEach(field => allRouteFields.add(String(field)));
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
if (route.optionalFields) {
|
|
239
|
-
route.optionalFields.forEach(field => allRouteFields.add(String(field)));
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
if (currentStep?.collect) {
|
|
243
|
-
currentStep.collect.forEach(field => allRouteFields.add(String(field)));
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// Generate example values for each field
|
|
247
|
-
for (const field of allRouteFields) {
|
|
243
|
+
for (const field of allRouteFields) {
|
|
244
|
+
if (agentSchema?.properties) {
|
|
248
245
|
const fieldSchema = agentSchema.properties[field];
|
|
249
246
|
if (fieldSchema) {
|
|
250
247
|
const fieldType = Array.isArray(fieldSchema.type) ? fieldSchema.type[0] : fieldSchema.type;
|
|
@@ -263,6 +260,9 @@ export class ResponseEngine<TContext = unknown, TData = unknown> {
|
|
|
263
260
|
|
|
264
261
|
exampleFields.push(` "${field}": ${exampleValue}`);
|
|
265
262
|
}
|
|
263
|
+
} else {
|
|
264
|
+
// No agent schema - use string as default
|
|
265
|
+
exampleFields.push(` "${field}": "extracted value"`);
|
|
266
266
|
}
|
|
267
267
|
}
|
|
268
268
|
|
|
@@ -1028,29 +1028,36 @@ export class ResponseModal<TContext = unknown, TData = unknown> {
|
|
|
1028
1028
|
* @private
|
|
1029
1029
|
*/
|
|
1030
1030
|
private buildBatchResponseSchema(collectFields: string[]): Record<string, unknown> {
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1031
|
+
const properties: Record<string, unknown> = {
|
|
1032
|
+
message: {
|
|
1033
|
+
type: "string",
|
|
1034
|
+
description: "Your response to the user",
|
|
1035
|
+
},
|
|
1036
|
+
};
|
|
1037
|
+
|
|
1038
|
+
const agentSchema = this.agent.getSchema();
|
|
1039
|
+
|
|
1040
|
+
// Add collect fields to schema, using agent schema definitions when available
|
|
1041
|
+
for (const field of collectFields) {
|
|
1042
|
+
if (agentSchema?.properties && agentSchema.properties[field]) {
|
|
1043
|
+
properties[field] = agentSchema.properties[field];
|
|
1044
|
+
} else {
|
|
1045
|
+
// Dynamic fallback when no agent schema is defined
|
|
1046
|
+
properties[field] = {
|
|
1047
|
+
type: "string",
|
|
1048
|
+
description: `Collected value for ${field}`,
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1037
1052
|
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1053
|
+
return {
|
|
1054
|
+
type: "object",
|
|
1055
|
+
properties,
|
|
1056
|
+
required: ["message"],
|
|
1057
|
+
additionalProperties: true,
|
|
1043
1058
|
};
|
|
1044
1059
|
}
|
|
1045
1060
|
|
|
1046
|
-
return {
|
|
1047
|
-
type: "object",
|
|
1048
|
-
properties,
|
|
1049
|
-
required: ["message"],
|
|
1050
|
-
additionalProperties: true,
|
|
1051
|
-
};
|
|
1052
|
-
}
|
|
1053
|
-
|
|
1054
1061
|
/**
|
|
1055
1062
|
* Collect available tools from all steps in the batch
|
|
1056
1063
|
* @private
|
|
@@ -1424,8 +1431,34 @@ export class ResponseModal<TContext = unknown, TData = unknown> {
|
|
|
1424
1431
|
}
|
|
1425
1432
|
|
|
1426
1433
|
// Update session with next step
|
|
1427
|
-
|
|
1428
|
-
|
|
1434
|
+
// If the next step has requires fields that are missing, stay at the previous step
|
|
1435
|
+
if (nextStep.requires && nextStep.requires.length > 0) {
|
|
1436
|
+
const sessionData = session.data || {};
|
|
1437
|
+
const missingRequires = nextStep.requires.filter(
|
|
1438
|
+
field => (sessionData as Record<string, unknown>)[String(field)] === undefined
|
|
1439
|
+
);
|
|
1440
|
+
if (missingRequires.length > 0) {
|
|
1441
|
+
const warning = `[Agent] Cannot advance to step "${nextStep.description || nextStep.id}": ` +
|
|
1442
|
+
`missing required fields [${missingRequires.join(', ')}]. Staying at current step.`;
|
|
1443
|
+
logger.warn(warning);
|
|
1444
|
+
console.warn(warning);
|
|
1445
|
+
// Stay at the current step - don't enter the next one
|
|
1446
|
+
const currentStepId = session.currentStep?.id;
|
|
1447
|
+
if (currentStepId) {
|
|
1448
|
+
const currentStepInstance = selectedRoute.getStep(currentStepId);
|
|
1449
|
+
if (currentStepInstance) {
|
|
1450
|
+
nextStep = currentStepInstance;
|
|
1451
|
+
logger.debug(`[ResponseModal] Staying at current step: ${nextStep.id} due to missing requires`);
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
} else {
|
|
1455
|
+
session = enterStep(session, nextStep.id, nextStep.description);
|
|
1456
|
+
logger.debug(`[ResponseModal] Entered step: ${nextStep.id}`);
|
|
1457
|
+
}
|
|
1458
|
+
} else {
|
|
1459
|
+
session = enterStep(session, nextStep.id, nextStep.description);
|
|
1460
|
+
logger.debug(`[ResponseModal] Entered step: ${nextStep.id}`);
|
|
1461
|
+
}
|
|
1429
1462
|
|
|
1430
1463
|
// Build response schema for this route (with collect fields from step)
|
|
1431
1464
|
const responseSchema = this.responseEngine.responseSchemaForRoute(selectedRoute, nextStep, this.agent.getSchema());
|
|
@@ -1434,8 +1467,8 @@ export class ResponseModal<TContext = unknown, TData = unknown> {
|
|
|
1434
1467
|
const responsePrompt = await this.responseEngine.buildResponsePrompt({
|
|
1435
1468
|
route: selectedRoute,
|
|
1436
1469
|
currentStep: nextStep,
|
|
1437
|
-
rules: selectedRoute.getRules(),
|
|
1438
|
-
prohibitions: selectedRoute.getProhibitions(),
|
|
1470
|
+
rules: [...this.agent.getRules(), ...selectedRoute.getRules()],
|
|
1471
|
+
prohibitions: [...this.agent.getProhibitions(), ...selectedRoute.getProhibitions()],
|
|
1439
1472
|
directives: responseDirectives,
|
|
1440
1473
|
history: historyEvents, // Use Event[] for buildResponsePrompt
|
|
1441
1474
|
lastMessage: lastMessageText, // Use string for buildResponsePrompt
|
|
@@ -1544,16 +1577,41 @@ export class ResponseModal<TContext = unknown, TData = unknown> {
|
|
|
1544
1577
|
}
|
|
1545
1578
|
|
|
1546
1579
|
// Update session with next step
|
|
1547
|
-
|
|
1548
|
-
|
|
1580
|
+
// If the next step has requires fields that are missing, stay at the previous step
|
|
1581
|
+
if (nextStep.requires && nextStep.requires.length > 0) {
|
|
1582
|
+
const sessionData = session.data || {};
|
|
1583
|
+
const missingRequires = nextStep.requires.filter(
|
|
1584
|
+
field => (sessionData as Record<string, unknown>)[String(field)] === undefined
|
|
1585
|
+
);
|
|
1586
|
+
if (missingRequires.length > 0) {
|
|
1587
|
+
const warning = `[Agent] Cannot advance to step "${nextStep.description || nextStep.id}": ` +
|
|
1588
|
+
`missing required fields [${missingRequires.join(', ')}]. Staying at current step.`;
|
|
1589
|
+
logger.warn(warning);
|
|
1590
|
+
console.warn(warning);
|
|
1591
|
+
const currentStepId = session.currentStep?.id;
|
|
1592
|
+
if (currentStepId) {
|
|
1593
|
+
const currentStepInstance = selectedRoute.getStep(currentStepId);
|
|
1594
|
+
if (currentStepInstance) {
|
|
1595
|
+
nextStep = currentStepInstance;
|
|
1596
|
+
logger.debug(`[ResponseModal] Staying at current step: ${nextStep.id} due to missing requires`);
|
|
1597
|
+
}
|
|
1598
|
+
}
|
|
1599
|
+
} else {
|
|
1600
|
+
session = enterStep(session, nextStep.id, nextStep.description);
|
|
1601
|
+
logger.debug(`[ResponseModal] Entered step: ${nextStep.id}`);
|
|
1602
|
+
}
|
|
1603
|
+
} else {
|
|
1604
|
+
session = enterStep(session, nextStep.id, nextStep.description);
|
|
1605
|
+
logger.debug(`[ResponseModal] Entered step: ${nextStep.id}`);
|
|
1606
|
+
}
|
|
1549
1607
|
|
|
1550
1608
|
// Build response schema and prompt (same as non-streaming)
|
|
1551
1609
|
const responseSchema = this.responseEngine.responseSchemaForRoute(selectedRoute, nextStep, this.agent.getSchema());
|
|
1552
1610
|
const responsePrompt = await this.responseEngine.buildResponsePrompt({
|
|
1553
1611
|
route: selectedRoute,
|
|
1554
1612
|
currentStep: nextStep,
|
|
1555
|
-
rules: selectedRoute.getRules(),
|
|
1556
|
-
prohibitions: selectedRoute.getProhibitions(),
|
|
1613
|
+
rules: [...this.agent.getRules(), ...selectedRoute.getRules()],
|
|
1614
|
+
prohibitions: [...this.agent.getProhibitions(), ...selectedRoute.getProhibitions()],
|
|
1557
1615
|
directives: responseDirectives,
|
|
1558
1616
|
history: historyEvents, // Use Event[] for buildResponsePrompt
|
|
1559
1617
|
lastMessage: lastMessageText, // Use string for buildResponsePrompt
|