@falai/agent 1.0.0 → 1.0.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 +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 +21 -0
- 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/types/agent.d.ts +4 -0
- package/dist/cjs/types/agent.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 +21 -0
- 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/types/agent.d.ts +4 -0
- package/dist/types/agent.d.ts.map +1 -1
- package/docs/README.md +1 -0
- package/docs/api/README.md +16 -0
- package/docs/api/overview.md +4 -0
- package/docs/core/agent/README.md +36 -0
- package/docs/core/agent/rules-and-prohibitions.md +113 -0
- package/docs/guides/migration/README.md +6 -2
- package/docs/guides/migration/multi-step-execution.md +70 -0
- package/package.json +1 -1
- package/src/core/Agent.ts +24 -0
- 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/types/agent.ts +4 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# Agent-Level Rules & Prohibitions
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Rules and prohibitions define hard behavioral boundaries for the agent. Unlike guidelines (which are conditional and advisory), rules and prohibitions are absolute — they are always included in every prompt sent to the AI provider.
|
|
6
|
+
|
|
7
|
+
- **Rules**: Things the agent must always do.
|
|
8
|
+
- **Prohibitions**: Things the agent must never do.
|
|
9
|
+
|
|
10
|
+
Both can be defined at the agent level (applies to all routes) and at the route level (applies only within that route). When both are present, they are merged — agent-level entries appear first, followed by route-level entries.
|
|
11
|
+
|
|
12
|
+
## Agent-Level Configuration
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
const agent = new Agent({
|
|
16
|
+
name: "Support Bot",
|
|
17
|
+
provider: myProvider,
|
|
18
|
+
|
|
19
|
+
// Agent-wide rules — enforced in every route
|
|
20
|
+
rules: [
|
|
21
|
+
"Always respond in the user's language",
|
|
22
|
+
"Include a follow-up question when the conversation is open-ended",
|
|
23
|
+
],
|
|
24
|
+
|
|
25
|
+
// Agent-wide prohibitions — enforced in every route
|
|
26
|
+
prohibitions: [
|
|
27
|
+
"Never share internal system details or error stack traces",
|
|
28
|
+
"Never make up information — say you don't know instead",
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Dynamic Templates
|
|
34
|
+
|
|
35
|
+
Rules and prohibitions accept the same `Template` type used elsewhere in the framework — they can be static strings or context-aware functions:
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
const agent = new Agent<MyContext, MyData>({
|
|
39
|
+
name: "Adaptive Bot",
|
|
40
|
+
provider: myProvider,
|
|
41
|
+
|
|
42
|
+
rules: [
|
|
43
|
+
"Always be polite",
|
|
44
|
+
// Dynamic rule based on context
|
|
45
|
+
({ context }) =>
|
|
46
|
+
context?.locale === "de"
|
|
47
|
+
? "Respond in formal German (Sie-form)"
|
|
48
|
+
: "Use a casual, friendly tone",
|
|
49
|
+
],
|
|
50
|
+
|
|
51
|
+
prohibitions: [
|
|
52
|
+
"Never discuss competitor products",
|
|
53
|
+
// Dynamic prohibition based on collected data
|
|
54
|
+
({ data }) =>
|
|
55
|
+
data?.isMinor
|
|
56
|
+
? "Do not discuss age-restricted topics"
|
|
57
|
+
: "Do not share personal medical advice",
|
|
58
|
+
],
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Merging with Route-Level Rules
|
|
63
|
+
|
|
64
|
+
Route-level rules and prohibitions are additive. The final prompt includes both:
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
const agent = new Agent({
|
|
68
|
+
provider: myProvider,
|
|
69
|
+
name: "Agent",
|
|
70
|
+
rules: ["Always confirm before taking action"],
|
|
71
|
+
prohibitions: ["Never delete data without confirmation"],
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
agent.createRoute({
|
|
75
|
+
title: "Billing",
|
|
76
|
+
rules: ["Always quote prices in the user's currency"],
|
|
77
|
+
prohibitions: ["Never process refunds above $500 without escalation"],
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// During a Billing route response, the prompt will contain:
|
|
81
|
+
// Rules:
|
|
82
|
+
// - Always confirm before taking action (agent)
|
|
83
|
+
// - Always quote prices in the user's currency (route)
|
|
84
|
+
//
|
|
85
|
+
// Prohibitions:
|
|
86
|
+
// - Never delete data without confirmation (agent)
|
|
87
|
+
// - Never process refunds above $500 without escalation (route)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
This merging applies to all execution paths: single-step responses, batch execution, and streaming.
|
|
91
|
+
|
|
92
|
+
## Accessing Rules Programmatically
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
// Agent-level
|
|
96
|
+
const agentRules = agent.getRules();
|
|
97
|
+
const agentProhibitions = agent.getProhibitions();
|
|
98
|
+
|
|
99
|
+
// Route-level (unchanged)
|
|
100
|
+
const routeRules = route.getRules();
|
|
101
|
+
const routeProhibitions = route.getProhibitions();
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## When to Use Rules vs. Guidelines
|
|
105
|
+
|
|
106
|
+
| | Rules / Prohibitions | Guidelines |
|
|
107
|
+
|---|---|---|
|
|
108
|
+
| Scope | Always active | Conditional (can have `condition`) |
|
|
109
|
+
| Purpose | Hard boundaries | Soft behavioral nudges |
|
|
110
|
+
| Enforcement | Included in every prompt | Only when condition matches |
|
|
111
|
+
| Example | "Never reveal API keys" | "When user is frustrated, apologize" |
|
|
112
|
+
|
|
113
|
+
Use rules for non-negotiable constraints. Use guidelines for context-dependent behavior.
|
|
@@ -6,7 +6,11 @@ This directory contains migration guides for major changes and updates to the `@
|
|
|
6
6
|
|
|
7
7
|
### [Multi-Step Execution Migration Guide](./multi-step-execution.md)
|
|
8
8
|
|
|
9
|
-
**
|
|
9
|
+
**v1.0.0 - Major Release** - Guide for understanding and migrating to multi-step batch execution.
|
|
10
|
+
|
|
11
|
+
**Breaking Changes:**
|
|
12
|
+
- � **History API Simplified**: `createMessageEvent`/`EventSource` replaced with `userMessage`/`assistantMessage`
|
|
13
|
+
- 📝 **StepOptions**: `instructions` property renamed to `prompt`
|
|
10
14
|
|
|
11
15
|
**What's New:**
|
|
12
16
|
- 🚀 **Multi-Step Batching**: Multiple steps execute in a single LLM call
|
|
@@ -21,7 +25,7 @@ This directory contains migration guides for major changes and updates to the `@
|
|
|
21
25
|
- SkipIf conditions affect batch determination
|
|
22
26
|
|
|
23
27
|
**Migration Status:**
|
|
24
|
-
-
|
|
28
|
+
- ⚠️ **Breaking Changes**: History API and StepOptions.instructions
|
|
25
29
|
- ⚠️ **Behavioral Change**: Execution semantics differ
|
|
26
30
|
- ✅ **Gradual Migration**: Review hooks and tests
|
|
27
31
|
|
|
@@ -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.
|
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>;
|
|
@@ -185,6 +187,14 @@ export class Agent<TContext = any, TData = any> {
|
|
|
185
187
|
});
|
|
186
188
|
}
|
|
187
189
|
|
|
190
|
+
// Initialize agent-level rules and prohibitions
|
|
191
|
+
if (options.rules) {
|
|
192
|
+
this.agentRules = [...options.rules];
|
|
193
|
+
}
|
|
194
|
+
if (options.prohibitions) {
|
|
195
|
+
this.agentProhibitions = [...options.prohibitions];
|
|
196
|
+
}
|
|
197
|
+
|
|
188
198
|
if (options.routes) {
|
|
189
199
|
options.routes.forEach((routeOptions) => {
|
|
190
200
|
this.createRoute(routeOptions);
|
|
@@ -677,6 +687,20 @@ export class Agent<TContext = any, TData = any> {
|
|
|
677
687
|
return [...this.guidelines];
|
|
678
688
|
}
|
|
679
689
|
|
|
690
|
+
/**
|
|
691
|
+
* Get agent-level rules
|
|
692
|
+
*/
|
|
693
|
+
getRules(): Template<TContext, TData>[] {
|
|
694
|
+
return [...this.agentRules];
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* Get agent-level prohibitions
|
|
699
|
+
*/
|
|
700
|
+
getProhibitions(): Template<TContext, TData>[] {
|
|
701
|
+
return [...this.agentProhibitions];
|
|
702
|
+
}
|
|
703
|
+
|
|
680
704
|
/**
|
|
681
705
|
* Evaluate and match active guidelines based on their conditions
|
|
682
706
|
* 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
|
|