@falai/agent 2.0.1 → 2.1.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/dist/cjs/core/Agent.d.ts +1 -1
- package/dist/cjs/core/Agent.js +3 -3
- package/dist/cjs/core/Agent.js.map +1 -1
- package/dist/cjs/core/AutoChainExecutor.d.ts +2 -2
- package/dist/cjs/core/AutoChainExecutor.js +2 -2
- package/dist/cjs/core/AutoChainExecutor.js.map +1 -1
- package/dist/cjs/core/PromptComposer.d.ts +1 -1
- package/dist/cjs/core/PromptComposer.js +2 -2
- package/dist/cjs/core/PromptComposer.js.map +1 -1
- package/dist/cjs/core/ResponseEngine.d.ts +1 -1
- package/dist/cjs/core/ResponseModal.js +6 -6
- package/dist/cjs/core/ResponseModal.js.map +1 -1
- package/dist/cjs/core/ResponsePipeline.d.ts +2 -2
- package/dist/cjs/core/ResponsePipeline.d.ts.map +1 -1
- package/dist/cjs/core/ResponsePipeline.js.map +1 -1
- package/dist/cjs/core/SignalProcessor.d.ts +3 -3
- package/dist/cjs/core/SignalProcessor.d.ts.map +1 -1
- package/dist/cjs/core/SignalProcessor.js +1 -1
- package/dist/cjs/core/SignalProcessor.js.map +1 -1
- package/dist/cjs/core/Step.d.ts +1 -1
- package/dist/cjs/core/Step.js +1 -1
- package/dist/cjs/core/ToolManager.d.ts +1 -1
- package/dist/cjs/core/ToolManager.js +1 -1
- package/dist/cjs/core/flow-namespace.d.ts +4 -4
- package/dist/cjs/core/flow-namespace.d.ts.map +1 -1
- package/dist/cjs/core/flow-namespace.js +14 -25
- package/dist/cjs/core/flow-namespace.js.map +1 -1
- package/dist/cjs/index.d.ts +3 -3
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +2 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/agent.d.ts +1 -1
- package/dist/cjs/types/agent.d.ts.map +1 -1
- package/dist/cjs/types/ai.d.ts +1 -1
- package/dist/cjs/types/ai.js +1 -1
- package/dist/cjs/types/flow.d.ts +33 -25
- package/dist/cjs/types/flow.d.ts.map +1 -1
- package/dist/cjs/types/index.d.ts +1 -1
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/types/signals.d.ts +4 -5
- package/dist/cjs/types/signals.d.ts.map +1 -1
- package/dist/cjs/utils/session.d.ts +1 -1
- package/dist/cjs/utils/session.js +4 -4
- package/dist/cjs/utils/session.js.map +1 -1
- package/dist/core/Agent.d.ts +1 -1
- package/dist/core/Agent.js +3 -3
- package/dist/core/Agent.js.map +1 -1
- package/dist/core/AutoChainExecutor.d.ts +2 -2
- package/dist/core/AutoChainExecutor.js +2 -2
- package/dist/core/AutoChainExecutor.js.map +1 -1
- package/dist/core/PromptComposer.d.ts +1 -1
- package/dist/core/PromptComposer.js +2 -2
- package/dist/core/PromptComposer.js.map +1 -1
- package/dist/core/ResponseEngine.d.ts +1 -1
- package/dist/core/ResponseModal.js +6 -6
- package/dist/core/ResponseModal.js.map +1 -1
- package/dist/core/ResponsePipeline.d.ts +2 -2
- package/dist/core/ResponsePipeline.d.ts.map +1 -1
- package/dist/core/ResponsePipeline.js.map +1 -1
- package/dist/core/SignalProcessor.d.ts +3 -3
- package/dist/core/SignalProcessor.d.ts.map +1 -1
- package/dist/core/SignalProcessor.js +1 -1
- package/dist/core/SignalProcessor.js.map +1 -1
- package/dist/core/Step.d.ts +1 -1
- package/dist/core/Step.js +1 -1
- package/dist/core/ToolManager.d.ts +1 -1
- package/dist/core/ToolManager.js +1 -1
- package/dist/core/flow-namespace.d.ts +4 -4
- package/dist/core/flow-namespace.d.ts.map +1 -1
- package/dist/core/flow-namespace.js +14 -25
- package/dist/core/flow-namespace.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/types/agent.d.ts +1 -1
- package/dist/types/agent.d.ts.map +1 -1
- package/dist/types/ai.d.ts +1 -1
- package/dist/types/ai.js +1 -1
- package/dist/types/flow.d.ts +33 -25
- package/dist/types/flow.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/types/signals.d.ts +4 -5
- package/dist/types/signals.d.ts.map +1 -1
- package/dist/utils/session.d.ts +1 -1
- package/dist/utils/session.js +4 -4
- package/dist/utils/session.js.map +1 -1
- package/docs/concepts/architecture.md +19 -26
- package/docs/concepts/directives.md +53 -84
- package/docs/concepts/pipeline.md +4 -4
- package/docs/guides/flow-control.md +12 -13
- package/docs/guides/streaming.md +1 -1
- package/docs/migration/README.md +1 -1
- package/docs/migration/v1-to-v2.md +507 -5
- package/docs/reference/adapters.md +1 -1
- package/docs/reference/directive.md +9 -10
- package/docs/reference/flow.md +3 -3
- package/docs/reference/instruction.md +1 -1
- package/docs/reference/signals.md +3 -4
- package/docs/reference/step.md +9 -10
- package/docs/reference/tool.md +1 -1
- package/docs/start/02-first-agent.md +1 -2
- package/examples/tsconfig.json +1 -3
- package/package.json +9 -5
- package/src/core/Agent.ts +4 -4
- package/src/core/AutoChainExecutor.ts +3 -3
- package/src/core/PromptComposer.ts +2 -2
- package/src/core/ResponseEngine.ts +1 -1
- package/src/core/ResponseModal.ts +18 -18
- package/src/core/ResponsePipeline.ts +1 -2
- package/src/core/SignalProcessor.ts +7 -7
- package/src/core/Step.ts +1 -1
- package/src/core/ToolManager.ts +1 -1
- package/src/core/flow-namespace.ts +32 -53
- package/src/index.ts +2 -3
- package/src/types/agent.ts +1 -1
- package/src/types/ai.ts +1 -1
- package/src/types/flow.ts +41 -26
- package/src/types/index.ts +0 -1
- package/src/types/signals.ts +4 -5
- package/src/utils/session.ts +5 -5
- package/docs/migration/route-to-flow.md +0 -561
- package/docs/reference/pre-directive.md +0 -131
|
@@ -1,561 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: "Route → Flow rename"
|
|
3
|
-
description: "Rename the Route domain noun to Flow across symbols, config, persistence, and generated IDs, with per-adapter data migration steps."
|
|
4
|
-
type: migration
|
|
5
|
-
order: 2
|
|
6
|
-
sidebar: false
|
|
7
|
-
---
|
|
8
|
-
|
|
9
|
-
# Migration Guide: Route → Flow Rename
|
|
10
|
-
|
|
11
|
-
**Version:** Minor breaking bump (ships before v2 overhaul)
|
|
12
|
-
**CHANGELOG:** See the [breaking-change entry in CHANGELOG.md](../../CHANGELOG.md)
|
|
13
|
-
|
|
14
|
-
## Summary
|
|
15
|
-
|
|
16
|
-
The `Route` domain noun has been renamed to `Flow` across the entire `@falai/agent` package. This is a clean break with no compatibility shims, no dual-naming layer, and no runtime fallback for legacy field names. Every public symbol, configuration option, persisted column/field, adapter method, constant, error class, and utility function that referenced "Route" as a noun now uses "Flow". The verb form `route()` and the gerund "routing" (as used in prose and the `routing.ts` module) are preserved — routing-as-an-action remains the correct verb for selecting a flow.
|
|
17
|
-
|
|
18
|
-
---
|
|
19
|
-
|
|
20
|
-
## Symbol Rename Table
|
|
21
|
-
|
|
22
|
-
| Old | New | Layer | Action |
|
|
23
|
-
|-----|-----|-------|--------|
|
|
24
|
-
| `Route` (class) | `Flow` | Core | Update imports and instantiation |
|
|
25
|
-
| `RouteOptions` | `FlowOptions` | Type | Update type annotations |
|
|
26
|
-
| `RouteRef` | `FlowRef` | Type | Update type annotations |
|
|
27
|
-
| `RouteTransitionConfig` | `FlowTransitionConfig` | Type | Update type annotations |
|
|
28
|
-
| `RouteCompletionHandler` | `FlowCompletionHandler` | Type | Update type annotations |
|
|
29
|
-
| `RouteLifecycleHooks` | `FlowLifecycleHooks` | Type | Update type annotations |
|
|
30
|
-
| `RoutingEngine` | `FlowRouter` | Core | Update imports and references |
|
|
31
|
-
| `RoutingEngineOptions` | `FlowRouterOptions` | Type | Update type annotations |
|
|
32
|
-
| `RoutingDecisionOutput` | `FlowRoutingDecisionOutput` | Type | Update type annotations |
|
|
33
|
-
| `RouteConfigurationError` | `FlowConfigurationError` | Error | Update catch blocks |
|
|
34
|
-
| `END_ROUTE` | Removed | Constant | Implicit terminus — remove all references |
|
|
35
|
-
| `END_ROUTE_ID` | Removed | Constant | Implicit terminus — remove all references |
|
|
36
|
-
| `generateRouteId` | `generateFlowId` | Utility | Update calls |
|
|
37
|
-
| `enterRoute` | `enterFlow` | Utility | Update calls |
|
|
38
|
-
| `StepRef.routeId` | `StepRef.flowId` | Type | Update field access |
|
|
39
|
-
|
|
40
|
-
### Preserved (verb-form carve-outs)
|
|
41
|
-
|
|
42
|
-
These are **not** renamed:
|
|
43
|
-
|
|
44
|
-
- `route()` method on `FlowRouter` (verb form)
|
|
45
|
-
- `RoutingDecision` type (describes the act of routing)
|
|
46
|
-
- `RoutingSchemaOptions` type
|
|
47
|
-
- `buildRoutingPrompt` method
|
|
48
|
-
- `getCandidateStepsWithConditions` method (returns Steps)
|
|
49
|
-
- `src/types/routing.ts` file path
|
|
50
|
-
- All "routing" prose in documentation
|
|
51
|
-
|
|
52
|
-
---
|
|
53
|
-
|
|
54
|
-
## Configuration Rename Table
|
|
55
|
-
|
|
56
|
-
| Old | New | Location |
|
|
57
|
-
|-----|-----|----------|
|
|
58
|
-
| `AgentOptions.routes` | `AgentOptions.flows` | Agent constructor |
|
|
59
|
-
| `AgentOptions.routeSwitchMargin` | `AgentOptions.flowSwitchMargin` | Agent constructor |
|
|
60
|
-
| `agent.routeSwitchMargin` | `agent.flowSwitchMargin` | Getter/setter |
|
|
61
|
-
| `agent.createRoute(...)` | `agent.createFlow(...)` | Method call |
|
|
62
|
-
| `agent.getRoutes()` | `agent.getFlows()` | Method call |
|
|
63
|
-
| `agent.nextStepRoute(...)` | `agent.nextStepFlow(...)` | Method call |
|
|
64
|
-
| `agent.getRoutingEngine()` | `agent.getFlowRouter()` | Method call |
|
|
65
|
-
| `agent.routes` | `agent.flows` | Getter |
|
|
66
|
-
| `Guideline.scope: 'route'` | `Guideline.scope: 'flow'` | Guideline config |
|
|
67
|
-
| `Guideline.route` | `Guideline.flow` | Guideline config |
|
|
68
|
-
| `RoutingDecision.routes` | `RoutingDecision.flows` | Field access |
|
|
69
|
-
|
|
70
|
-
---
|
|
71
|
-
|
|
72
|
-
## Session State Rename Table
|
|
73
|
-
|
|
74
|
-
| Old | New | Notes |
|
|
75
|
-
|-----|-----|-------|
|
|
76
|
-
| `SessionState.currentRoute` | `SessionState.currentFlow` | Runtime shape |
|
|
77
|
-
| `SessionState.routeHistory` | `SessionState.flowHistory` | Runtime shape |
|
|
78
|
-
| `flowHistory[].routeId` | `flowHistory[].flowId` | History item shape |
|
|
79
|
-
| `PendingTransition.targetRouteId` | `PendingTransition.targetFlowId` | Transition config |
|
|
80
|
-
| `PendingTransition.reason: "route_complete"` | `PendingTransition.reason: "flow_complete"` | String literal |
|
|
81
|
-
| `SessionData.currentRoute` | `SessionData.currentFlow` | Persistence shape |
|
|
82
|
-
| `CollectedStateData.routeHistory` | `CollectedStateData.flowHistory` | Persistence shape |
|
|
83
|
-
| `CollectedStateData.currentRouteTitle` | `CollectedStateData.currentFlowTitle` | Persistence shape |
|
|
84
|
-
| `MessageData.route` | `MessageData.flow` | Persistence shape |
|
|
85
|
-
| `SaveMessageOptions.route` | `SaveMessageOptions.flow` | Persistence shape |
|
|
86
|
-
|
|
87
|
-
### StoppedReason Literals
|
|
88
|
-
|
|
89
|
-
| Old | New |
|
|
90
|
-
|-----|-----|
|
|
91
|
-
| `'end_route'` | Removed — use `'last_step'` or `'completed'` |
|
|
92
|
-
| `'route_complete'` | `'last_step'` (no successor) or `'completed'` (explicit directive) |
|
|
93
|
-
|
|
94
|
-
---
|
|
95
|
-
|
|
96
|
-
## Adapter Method Rename Table
|
|
97
|
-
|
|
98
|
-
| Adapter | Old Method | New Method |
|
|
99
|
-
|---------|-----------|------------|
|
|
100
|
-
| `MemoryAdapter` | `updateRouteStep()` | `updateFlowStep()` |
|
|
101
|
-
| `PrismaAdapter` | `updateRouteStep()` | `updateFlowStep()` |
|
|
102
|
-
| `RedisAdapter` | `updateRouteStep()` | `updateFlowStep()` |
|
|
103
|
-
| `MongoAdapter` | `updateRouteStep()` | `updateFlowStep()` |
|
|
104
|
-
| `PostgreSQLAdapter` | `updateRouteStep()` | `updateFlowStep()` |
|
|
105
|
-
| `SQLiteAdapter` | `updateRouteStep()` | `updateFlowStep()` |
|
|
106
|
-
| `OpenSearchAdapter` | `updateRouteStep()` | `updateFlowStep()` |
|
|
107
|
-
| `PersistenceManager` | `updateRouteStep()` | `updateFlowStep()` |
|
|
108
|
-
| `SessionRepository` (interface) | `updateRouteStep()` | `updateFlowStep()` |
|
|
109
|
-
|
|
110
|
-
If you implement a custom adapter, rename your `updateRouteStep` method to `updateFlowStep`.
|
|
111
|
-
|
|
112
|
-
---
|
|
113
|
-
|
|
114
|
-
## Per-Adapter Data Migration
|
|
115
|
-
|
|
116
|
-
The framework no longer reads or writes the legacy field/column names. You must migrate your persisted data before deploying the new version.
|
|
117
|
-
|
|
118
|
-
### PostgreSQL
|
|
119
|
-
|
|
120
|
-
```sql
|
|
121
|
-
-- Sessions table
|
|
122
|
-
ALTER TABLE sessions RENAME COLUMN current_route TO current_flow;
|
|
123
|
-
|
|
124
|
-
-- Messages table
|
|
125
|
-
ALTER TABLE messages RENAME COLUMN route TO flow;
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
### SQLite
|
|
129
|
-
|
|
130
|
-
SQLite 3.25+ supports `ALTER TABLE ... RENAME COLUMN`:
|
|
131
|
-
|
|
132
|
-
```sql
|
|
133
|
-
-- Sessions table
|
|
134
|
-
ALTER TABLE sessions RENAME COLUMN current_route TO current_flow;
|
|
135
|
-
|
|
136
|
-
-- Messages table
|
|
137
|
-
ALTER TABLE messages RENAME COLUMN route TO flow;
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
For SQLite versions older than 3.25, use the copy-and-rename pattern:
|
|
141
|
-
|
|
142
|
-
```sql
|
|
143
|
-
-- 1. Create new table with correct column names
|
|
144
|
-
CREATE TABLE sessions_new (
|
|
145
|
-
id TEXT PRIMARY KEY,
|
|
146
|
-
user_id TEXT,
|
|
147
|
-
agent_name TEXT,
|
|
148
|
-
status TEXT DEFAULT 'active',
|
|
149
|
-
current_flow TEXT,
|
|
150
|
-
current_step TEXT,
|
|
151
|
-
collected_data TEXT,
|
|
152
|
-
message_count INTEGER DEFAULT 0,
|
|
153
|
-
last_message_at TEXT,
|
|
154
|
-
completed_at TEXT,
|
|
155
|
-
created_at TEXT NOT NULL,
|
|
156
|
-
updated_at TEXT NOT NULL
|
|
157
|
-
);
|
|
158
|
-
|
|
159
|
-
-- 2. Copy data
|
|
160
|
-
INSERT INTO sessions_new SELECT
|
|
161
|
-
id, user_id, agent_name, status,
|
|
162
|
-
current_route AS current_flow,
|
|
163
|
-
current_step, collected_data, message_count,
|
|
164
|
-
last_message_at, completed_at, created_at, updated_at
|
|
165
|
-
FROM sessions;
|
|
166
|
-
|
|
167
|
-
-- 3. Drop old table and rename
|
|
168
|
-
DROP TABLE sessions;
|
|
169
|
-
ALTER TABLE sessions_new RENAME TO sessions;
|
|
170
|
-
|
|
171
|
-
-- Repeat for messages table (route → flow column)
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
### Prisma
|
|
175
|
-
|
|
176
|
-
Update your Prisma schema model fields:
|
|
177
|
-
|
|
178
|
-
```diff
|
|
179
|
-
model Session {
|
|
180
|
-
id String @id
|
|
181
|
-
userId String?
|
|
182
|
-
agentName String?
|
|
183
|
-
status String @default("active")
|
|
184
|
-
- currentRoute String? @map("current_route")
|
|
185
|
-
+ currentFlow String? @map("current_flow")
|
|
186
|
-
currentStep String? @map("current_step")
|
|
187
|
-
collectedData Json? @map("collected_data")
|
|
188
|
-
// ...
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
model Message {
|
|
192
|
-
id String @id
|
|
193
|
-
sessionId String @map("session_id")
|
|
194
|
-
// ...
|
|
195
|
-
- route String?
|
|
196
|
-
+ flow String?
|
|
197
|
-
step String?
|
|
198
|
-
// ...
|
|
199
|
-
}
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
Then generate and apply the migration:
|
|
203
|
-
|
|
204
|
-
```bash
|
|
205
|
-
npx prisma migrate dev --name route-to-flow-rename
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
If you use a custom `fieldMappings` config in `PrismaAdapter`, update the key from `currentRoute` to `currentFlow`:
|
|
209
|
-
|
|
210
|
-
```typescript
|
|
211
|
-
// Before
|
|
212
|
-
fieldMappings: { currentRoute: 'currentRoute', ... }
|
|
213
|
-
|
|
214
|
-
// After
|
|
215
|
-
fieldMappings: { currentFlow: 'currentFlow', ... }
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
### MongoDB
|
|
219
|
-
|
|
220
|
-
```javascript
|
|
221
|
-
// Rename session fields
|
|
222
|
-
db.sessions.updateMany({}, {
|
|
223
|
-
$rename: {
|
|
224
|
-
"currentRoute": "currentFlow"
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
// Rename collected state fields (if stored at top level)
|
|
229
|
-
db.sessions.updateMany({}, {
|
|
230
|
-
$rename: {
|
|
231
|
-
"collectedData.routeHistory": "collectedData.flowHistory",
|
|
232
|
-
"collectedData.currentRouteTitle": "collectedData.currentFlowTitle"
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
// Rename message fields
|
|
237
|
-
db.messages.updateMany({}, {
|
|
238
|
-
$rename: {
|
|
239
|
-
"route": "flow"
|
|
240
|
-
}
|
|
241
|
-
});
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
### Redis
|
|
245
|
-
|
|
246
|
-
Redis stores sessions as serialized JSON. Use a Lua script to rewrite the payload in-place:
|
|
247
|
-
|
|
248
|
-
```lua
|
|
249
|
-
-- redis-migrate-route-to-flow.lua
|
|
250
|
-
-- Run with: redis-cli --eval redis-migrate-route-to-flow.lua
|
|
251
|
-
|
|
252
|
-
local cursor = "0"
|
|
253
|
-
repeat
|
|
254
|
-
local result = redis.call("SCAN", cursor, "MATCH", "session:*", "COUNT", 100)
|
|
255
|
-
cursor = result[1]
|
|
256
|
-
local keys = result[2]
|
|
257
|
-
for _, key in ipairs(keys) do
|
|
258
|
-
local val = redis.call("GET", key)
|
|
259
|
-
if val then
|
|
260
|
-
-- Replace field names in JSON payload
|
|
261
|
-
val = val:gsub('"currentRoute"', '"currentFlow"')
|
|
262
|
-
val = val:gsub('"routeHistory"', '"flowHistory"')
|
|
263
|
-
val = val:gsub('"currentRouteTitle"', '"currentFlowTitle"')
|
|
264
|
-
redis.call("SET", key, val)
|
|
265
|
-
end
|
|
266
|
-
end
|
|
267
|
-
until cursor == "0"
|
|
268
|
-
|
|
269
|
-
-- If using hash layout instead of JSON:
|
|
270
|
-
-- Rename hash fields per session key
|
|
271
|
-
local cursor2 = "0"
|
|
272
|
-
repeat
|
|
273
|
-
local result = redis.call("SCAN", cursor2, "MATCH", "session:*", "COUNT", 100)
|
|
274
|
-
cursor2 = result[1]
|
|
275
|
-
local keys = result[2]
|
|
276
|
-
for _, key in ipairs(keys) do
|
|
277
|
-
local typ = redis.call("TYPE", key)["ok"]
|
|
278
|
-
if typ == "hash" then
|
|
279
|
-
local oldVal = redis.call("HGET", key, "currentRoute")
|
|
280
|
-
if oldVal then
|
|
281
|
-
redis.call("HSET", key, "currentFlow", oldVal)
|
|
282
|
-
redis.call("HDEL", key, "currentRoute")
|
|
283
|
-
end
|
|
284
|
-
end
|
|
285
|
-
end
|
|
286
|
-
until cursor2 == "0"
|
|
287
|
-
```
|
|
288
|
-
|
|
289
|
-
For message keys, apply the same pattern replacing `"route"` with `"flow"` in the JSON payload or hash field.
|
|
290
|
-
|
|
291
|
-
### OpenSearch
|
|
292
|
-
|
|
293
|
-
Use the `_reindex` API with a painless script to rename fields:
|
|
294
|
-
|
|
295
|
-
```json
|
|
296
|
-
POST _reindex
|
|
297
|
-
{
|
|
298
|
-
"source": {
|
|
299
|
-
"index": "sessions"
|
|
300
|
-
},
|
|
301
|
-
"dest": {
|
|
302
|
-
"index": "sessions_v2"
|
|
303
|
-
},
|
|
304
|
-
"script": {
|
|
305
|
-
"source": """
|
|
306
|
-
// Rename currentRoute → currentFlow
|
|
307
|
-
if (ctx._source.containsKey('currentRoute')) {
|
|
308
|
-
ctx._source.currentFlow = ctx._source.remove('currentRoute');
|
|
309
|
-
}
|
|
310
|
-
// Rename routeHistory → flowHistory in collectedData
|
|
311
|
-
if (ctx._source.containsKey('collectedData') && ctx._source.collectedData.containsKey('routeHistory')) {
|
|
312
|
-
ctx._source.collectedData.flowHistory = ctx._source.collectedData.remove('routeHistory');
|
|
313
|
-
}
|
|
314
|
-
if (ctx._source.containsKey('collectedData') && ctx._source.collectedData.containsKey('currentRouteTitle')) {
|
|
315
|
-
ctx._source.collectedData.currentFlowTitle = ctx._source.collectedData.remove('currentRouteTitle');
|
|
316
|
-
}
|
|
317
|
-
""",
|
|
318
|
-
"lang": "painless"
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
```
|
|
322
|
-
|
|
323
|
-
Then swap the alias:
|
|
324
|
-
|
|
325
|
-
```json
|
|
326
|
-
POST _aliases
|
|
327
|
-
{
|
|
328
|
-
"actions": [
|
|
329
|
-
{ "remove": { "index": "sessions", "alias": "sessions_active" } },
|
|
330
|
-
{ "add": { "index": "sessions_v2", "alias": "sessions_active" } }
|
|
331
|
-
]
|
|
332
|
-
}
|
|
333
|
-
```
|
|
334
|
-
|
|
335
|
-
For the messages index, apply the same reindex pattern renaming the `route` field to `flow`:
|
|
336
|
-
|
|
337
|
-
```json
|
|
338
|
-
POST _reindex
|
|
339
|
-
{
|
|
340
|
-
"source": {
|
|
341
|
-
"index": "messages"
|
|
342
|
-
},
|
|
343
|
-
"dest": {
|
|
344
|
-
"index": "messages_v2"
|
|
345
|
-
},
|
|
346
|
-
"script": {
|
|
347
|
-
"source": """
|
|
348
|
-
if (ctx._source.containsKey('route')) {
|
|
349
|
-
ctx._source.flow = ctx._source.remove('route');
|
|
350
|
-
}
|
|
351
|
-
""",
|
|
352
|
-
"lang": "painless"
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
```
|
|
356
|
-
|
|
357
|
-
---
|
|
358
|
-
|
|
359
|
-
## ID Prefix Migration
|
|
360
|
-
|
|
361
|
-
The `generateFlowId()` function now produces IDs with the prefix `flow_` instead of `route_`. Existing sessions stored under the legacy `route_*` prefix will not be recognized by the framework's flow-matching logic unless migrated.
|
|
362
|
-
|
|
363
|
-
**Run this migration during a maintenance window.** In-flight sessions will lose their step pointer if the rename is not atomic with the adapter restart.
|
|
364
|
-
|
|
365
|
-
### PostgreSQL / SQLite
|
|
366
|
-
|
|
367
|
-
```sql
|
|
368
|
-
UPDATE sessions
|
|
369
|
-
SET current_flow = REPLACE(current_flow, 'route_', 'flow_')
|
|
370
|
-
WHERE current_flow LIKE 'route\_%' ESCAPE '\';
|
|
371
|
-
```
|
|
372
|
-
|
|
373
|
-
If your `collected_data` JSON contains `flowHistory` entries with old IDs (stored as `routeId` before the field rename), update those as well:
|
|
374
|
-
|
|
375
|
-
```sql
|
|
376
|
-
-- PostgreSQL (JSONB)
|
|
377
|
-
UPDATE sessions
|
|
378
|
-
SET collected_data = REPLACE(collected_data::text, '"route_', '"flow_')::jsonb
|
|
379
|
-
WHERE collected_data::text LIKE '%"route_%';
|
|
380
|
-
```
|
|
381
|
-
|
|
382
|
-
### MongoDB
|
|
383
|
-
|
|
384
|
-
```javascript
|
|
385
|
-
db.sessions.updateMany(
|
|
386
|
-
{ currentFlow: { $regex: "^route_" } },
|
|
387
|
-
[{
|
|
388
|
-
$set: {
|
|
389
|
-
currentFlow: {
|
|
390
|
-
$replaceOne: {
|
|
391
|
-
input: "$currentFlow",
|
|
392
|
-
find: "route_",
|
|
393
|
-
replacement: "flow_"
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
}]
|
|
398
|
-
);
|
|
399
|
-
|
|
400
|
-
// Also update flowHistory entries
|
|
401
|
-
db.sessions.updateMany(
|
|
402
|
-
{ "collectedData.flowHistory.flowId": { $regex: "^route_" } },
|
|
403
|
-
[{
|
|
404
|
-
$set: {
|
|
405
|
-
"collectedData.flowHistory": {
|
|
406
|
-
$map: {
|
|
407
|
-
input: "$collectedData.flowHistory",
|
|
408
|
-
as: "entry",
|
|
409
|
-
in: {
|
|
410
|
-
$mergeObjects: [
|
|
411
|
-
"$$entry",
|
|
412
|
-
{
|
|
413
|
-
flowId: {
|
|
414
|
-
$replaceOne: {
|
|
415
|
-
input: "$$entry.flowId",
|
|
416
|
-
find: "route_",
|
|
417
|
-
replacement: "flow_"
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
]
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
}]
|
|
427
|
-
);
|
|
428
|
-
```
|
|
429
|
-
|
|
430
|
-
### Redis
|
|
431
|
-
|
|
432
|
-
Extend the Lua script above to also replace ID prefixes in the JSON payload:
|
|
433
|
-
|
|
434
|
-
```lua
|
|
435
|
-
-- Add to the existing migration script
|
|
436
|
-
val = val:gsub('"route_', '"flow_')
|
|
437
|
-
```
|
|
438
|
-
|
|
439
|
-
### OpenSearch
|
|
440
|
-
|
|
441
|
-
Include the prefix replacement in the reindex painless script:
|
|
442
|
-
|
|
443
|
-
```json
|
|
444
|
-
POST _reindex
|
|
445
|
-
{
|
|
446
|
-
"source": { "index": "sessions_v2" },
|
|
447
|
-
"dest": { "index": "sessions_v3" },
|
|
448
|
-
"script": {
|
|
449
|
-
"source": """
|
|
450
|
-
if (ctx._source.containsKey('currentFlow') && ctx._source.currentFlow != null && ctx._source.currentFlow.startsWith('route_')) {
|
|
451
|
-
ctx._source.currentFlow = 'flow_' + ctx._source.currentFlow.substring(6);
|
|
452
|
-
}
|
|
453
|
-
""",
|
|
454
|
-
"lang": "painless"
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
```
|
|
458
|
-
|
|
459
|
-
---
|
|
460
|
-
|
|
461
|
-
## Code-Side Migration Recipe
|
|
462
|
-
|
|
463
|
-
For downstream TypeScript consumers, here's a sed/codemod summary covering the most common public-API touchpoints:
|
|
464
|
-
|
|
465
|
-
```bash
|
|
466
|
-
# Symbol renames (imports and references)
|
|
467
|
-
sed -i '' 's/\bRoute\b/Flow/g; s/\bRouteOptions\b/FlowOptions/g; s/\bRouteRef\b/FlowRef/g' src/**/*.ts
|
|
468
|
-
sed -i '' 's/\bRouteTransitionConfig\b/FlowTransitionConfig/g' src/**/*.ts
|
|
469
|
-
sed -i '' 's/\bRouteCompletionHandler\b/FlowCompletionHandler/g' src/**/*.ts
|
|
470
|
-
sed -i '' 's/\bRouteLifecycleHooks\b/FlowLifecycleHooks/g' src/**/*.ts
|
|
471
|
-
sed -i '' 's/\bRouteConfigurationError\b/FlowConfigurationError/g' src/**/*.ts
|
|
472
|
-
sed -i '' 's/\bRoutingEngine\b/FlowRouter/g' src/**/*.ts
|
|
473
|
-
|
|
474
|
-
# Constants (END_ROUTE removed — delete all references)
|
|
475
|
-
# END_ROUTE/END_FLOW are no longer needed. Remove imports and usages.
|
|
476
|
-
# The last step in a flow is the implicit terminus.
|
|
477
|
-
sed -i '' '/END_ROUTE/d; /END_FLOW/d' src/**/*.ts
|
|
478
|
-
|
|
479
|
-
# Methods and fields
|
|
480
|
-
sed -i '' 's/\.createRoute(/\.createFlow(/g' src/**/*.ts
|
|
481
|
-
sed -i '' 's/\.getRoutes(/\.getFlows(/g' src/**/*.ts
|
|
482
|
-
sed -i '' 's/\.nextStepRoute(/\.nextStepFlow(/g' src/**/*.ts
|
|
483
|
-
sed -i '' 's/\.getRoutingEngine(/\.getFlowRouter(/g' src/**/*.ts
|
|
484
|
-
sed -i '' 's/\brouteSwitchMargin\b/flowSwitchMargin/g' src/**/*.ts
|
|
485
|
-
sed -i '' 's/\bgenerateRouteId\b/generateFlowId/g' src/**/*.ts
|
|
486
|
-
sed -i '' 's/\benterRoute\b/enterFlow/g' src/**/*.ts
|
|
487
|
-
|
|
488
|
-
# Session state fields
|
|
489
|
-
sed -i '' 's/\.currentRoute/\.currentFlow/g' src/**/*.ts
|
|
490
|
-
sed -i '' 's/\.routeHistory/\.flowHistory/g' src/**/*.ts
|
|
491
|
-
sed -i '' 's/\btargetRouteId\b/targetFlowId/g' src/**/*.ts
|
|
492
|
-
|
|
493
|
-
# String literals
|
|
494
|
-
sed -i '' "s/'end_route'/'last_step'/g" src/**/*.ts
|
|
495
|
-
sed -i '' "s/'route_complete'/'completed'/g" src/**/*.ts
|
|
496
|
-
|
|
497
|
-
# Configuration
|
|
498
|
-
sed -i '' 's/routes:/flows:/g' src/**/*.ts # Be careful — review matches manually
|
|
499
|
-
|
|
500
|
-
# Import paths (if importing from @falai/agent internals)
|
|
501
|
-
sed -i '' 's/core\/Route/core\/Flow/g' src/**/*.ts
|
|
502
|
-
sed -i '' 's/core\/RoutingEngine/core\/FlowRouter/g' src/**/*.ts
|
|
503
|
-
sed -i '' 's/types\/route/types\/flow/g' src/**/*.ts
|
|
504
|
-
```
|
|
505
|
-
|
|
506
|
-
**Important:** These sed commands are aggressive. Run them, then use `tsc --noEmit` to catch any false positives (e.g., `routes` in an HTTP router context). Review the diff before committing.
|
|
507
|
-
|
|
508
|
-
For a safer approach, use a TypeScript-aware codemod tool like [jscodeshift](https://github.com/facebook/jscodeshift) or rely on your IDE's "Find and Replace with Regex" with word-boundary matching.
|
|
509
|
-
|
|
510
|
-
---
|
|
511
|
-
|
|
512
|
-
## Verification
|
|
513
|
-
|
|
514
|
-
After migrating your code, run this ripgrep command to confirm no legacy references remain:
|
|
515
|
-
|
|
516
|
-
```bash
|
|
517
|
-
rg -n '\b(Route|RouteOptions|RouteRef|RouteConfigurationError|RoutingEngine|END_ROUTE|currentRoute|routeHistory|createRoute|generateRouteId|enterRoute|nextStepRoute|getRoutes|routeSwitchMargin)\b' \
|
|
518
|
-
--glob '**/*.ts' \
|
|
519
|
-
--glob '**/*.tsx' \
|
|
520
|
-
--glob '!node_modules/**' \
|
|
521
|
-
--glob '!dist/**'
|
|
522
|
-
```
|
|
523
|
-
|
|
524
|
-
Expected output: **zero matches**.
|
|
525
|
-
|
|
526
|
-
If you have historical migration notes or changelog entries that reference the old names, exclude them:
|
|
527
|
-
|
|
528
|
-
```bash
|
|
529
|
-
rg -n '\b(Route|RouteOptions|RouteRef|RouteConfigurationError|RoutingEngine|END_ROUTE|currentRoute|routeHistory|createRoute|generateRouteId|enterRoute|nextStepRoute|getRoutes|routeSwitchMargin)\b' \
|
|
530
|
-
--glob '**/*.ts' \
|
|
531
|
-
--glob '**/*.tsx' \
|
|
532
|
-
--glob '!node_modules/**' \
|
|
533
|
-
--glob '!dist/**' \
|
|
534
|
-
--glob '!**/CHANGELOG*' \
|
|
535
|
-
--glob '!**/migration/**'
|
|
536
|
-
```
|
|
537
|
-
|
|
538
|
-
Then run your type checker to confirm everything compiles:
|
|
539
|
-
|
|
540
|
-
```bash
|
|
541
|
-
npx tsc --noEmit
|
|
542
|
-
```
|
|
543
|
-
|
|
544
|
-
---
|
|
545
|
-
|
|
546
|
-
## FAQ
|
|
547
|
-
|
|
548
|
-
**Q: Is there a compatibility shim or deprecation period?**
|
|
549
|
-
No. This is a clean break. The old names are removed entirely.
|
|
550
|
-
|
|
551
|
-
**Q: Do I need to migrate my database before deploying?**
|
|
552
|
-
Yes. The framework no longer reads or writes the legacy column/field names. Deploy the data migration first, then deploy the new code.
|
|
553
|
-
|
|
554
|
-
**Q: What about the `route()` method I see on `FlowRouter`?**
|
|
555
|
-
That's the verb form — it means "to route a message to a flow." It is intentionally preserved.
|
|
556
|
-
|
|
557
|
-
**Q: My tests assert on `'end_route'` or `'route_complete'` — what do I do?**
|
|
558
|
-
`'end_route'` has been removed entirely (implicit terminus replaces it). Update to `'last_step'`. `'route_complete'` becomes `'last_step'` (no successor) or `'completed'` (explicit directive). TypeScript will flag these as type errors if you miss any.
|
|
559
|
-
|
|
560
|
-
**Q: I have custom IDs that don't use the `route_` prefix — do I need to migrate them?**
|
|
561
|
-
Only IDs generated by `generateRouteId()` (now `generateFlowId()`) use the prefix. If you set custom IDs on your flows, they are unaffected by the prefix change.
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: "PreDirective"
|
|
3
|
-
description: "Directive variant returned by pre-LLM hooks; adds prompt and tool shaping for the current turn."
|
|
4
|
-
type: reference
|
|
5
|
-
order: 7
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
# PreDirective
|
|
9
|
-
|
|
10
|
-
> **Where this is introduced:** [Directives](../concepts/directives.md)
|
|
11
|
-
|
|
12
|
-
`PreDirective<TContext, TData>` is the variant of [`Directive`](./directive.md)
|
|
13
|
-
that pre-LLM hooks return. It inherits every Directive field — position writes
|
|
14
|
-
(`goTo`, `goToStep`, `complete`, `abort`, `reset`), the verbatim `reply`, and
|
|
15
|
-
state writes (`dataUpdate`, `contextUpdate`) — and adds three fields that only
|
|
16
|
-
make sense before this turn's LLM call: `appendPrompt`, `injectTools`, and
|
|
17
|
-
`halt`.
|
|
18
|
-
|
|
19
|
-
Lifetime is one turn. PreDirective fields are stripped before
|
|
20
|
-
`session.pendingDirective` is written, so they cannot persist across turns and
|
|
21
|
-
cannot be assigned to `pendingDirective` directly.
|
|
22
|
-
|
|
23
|
-
PreDirective is the return type of:
|
|
24
|
-
|
|
25
|
-
- `flow.hooks.onEnter`
|
|
26
|
-
- `step.hooks.onEnter`
|
|
27
|
-
- `step.hooks.prepare`
|
|
28
|
-
- the merged pre-LLM phase of the directive bus
|
|
29
|
-
- a [`Signal`](./signals.md) firing in the pre-phase (via `SignalDirective`,
|
|
30
|
-
which extends PreDirective)
|
|
31
|
-
|
|
32
|
-
Post-LLM hooks (`step.hooks.finalize`, `flow.hooks.onComplete`) return plain
|
|
33
|
-
`Directive` — the three PreDirective-only fields have no effect post-LLM and
|
|
34
|
-
are dropped with a debug log if present.
|
|
35
|
-
|
|
36
|
-
## Signature
|
|
37
|
-
|
|
38
|
-
```typescript
|
|
39
|
-
interface PreDirective<TContext = unknown, TData = unknown>
|
|
40
|
-
extends Directive<TContext, TData> {
|
|
41
|
-
appendPrompt?: string[];
|
|
42
|
-
injectTools?: Array<Tool<TContext, TData>>;
|
|
43
|
-
halt?: boolean;
|
|
44
|
-
}
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## Fields
|
|
48
|
-
|
|
49
|
-
| Field | Type | Required | Default | Notes |
|
|
50
|
-
|-------|------|----------|---------|-------|
|
|
51
|
-
| `appendPrompt` | `string[]` | no | — | Sentences appended to the system prompt for THIS turn only via `PromptComposer`'s transient appendage slot. Merged across hooks by array-concat (Algorithm 4). Never cached, never persisted. |
|
|
52
|
-
| `injectTools` | `Tool<TContext, TData>[]` | no | — | Tools added to the available tool list for THIS turn only via `ToolManager`'s transient layer. Merged across hooks by concat-then-dedupe by `Tool.id` (last definition wins). Tool references are not serializable, so this field never persists. |
|
|
53
|
-
| `halt` | `boolean` | no | `false` | When `true`, skip the LLM call entirely this turn. Merged by logical-OR. Co-validates with `reply`: if both are set the `reply` is emitted and the turn ends with `stoppedReason: 'reply'`; if `halt` is set without `reply` the turn ends with `stoppedReason: 'halt'` and an empty assistant message. |
|
|
54
|
-
| *…all `Directive` fields* | — | — | — | See [Directive](./directive.md) for `goTo`, `goToStep`, `complete`, `abort`, `reset`, `reply`, `dataUpdate`, `contextUpdate`. Mutually-exclusive position rules apply identically. |
|
|
55
|
-
|
|
56
|
-
## Examples
|
|
57
|
-
|
|
58
|
-
### Append a sentence to the prompt for this turn
|
|
59
|
-
|
|
60
|
-
```typescript
|
|
61
|
-
import type { PreDirective } from '@falai/agent';
|
|
62
|
-
|
|
63
|
-
const flow = {
|
|
64
|
-
title: 'Booking',
|
|
65
|
-
hooks: {
|
|
66
|
-
onEnter: (ctx): PreDirective => {
|
|
67
|
-
if (ctx.context.user.tier === 'vip') {
|
|
68
|
-
return {
|
|
69
|
-
appendPrompt: ['This caller is a VIP — confirm preferences before suggesting options.'],
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
steps: [/* ... */],
|
|
75
|
-
};
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
### Inject a one-shot tool, then halt the LLM call
|
|
79
|
-
|
|
80
|
-
```typescript
|
|
81
|
-
import type { PreDirective, Tool } from '@falai/agent';
|
|
82
|
-
|
|
83
|
-
const lookupAccount: Tool = {
|
|
84
|
-
id: 'lookup_account',
|
|
85
|
-
description: 'Fetch the caller\'s account record.',
|
|
86
|
-
parameters: { type: 'object', properties: {} },
|
|
87
|
-
handler: async (ctx) => ({ content: JSON.stringify(await fetchAccount(ctx.context.user.id)) }),
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
const step = {
|
|
91
|
-
id: 'verify',
|
|
92
|
-
prompt: 'Verify the caller before proceeding.',
|
|
93
|
-
hooks: {
|
|
94
|
-
prepare: async (ctx): Promise<PreDirective> => {
|
|
95
|
-
if (!ctx.data.verified) {
|
|
96
|
-
// Make the tool available for THIS turn only and let the model call it.
|
|
97
|
-
return { injectTools: [lookupAccount] };
|
|
98
|
-
}
|
|
99
|
-
// Already verified — skip the LLM, emit a deterministic reply.
|
|
100
|
-
return { halt: true, reply: 'Verified. How can I help?' };
|
|
101
|
-
},
|
|
102
|
-
},
|
|
103
|
-
};
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
## Errors
|
|
107
|
-
|
|
108
|
-
`PreDirective` shares Directive's construction-time validation. The following
|
|
109
|
-
typed errors may be thrown by `flow.validate(...)` or by the directive bus
|
|
110
|
-
when an invalid PreDirective is emitted:
|
|
111
|
-
|
|
112
|
-
- `FlowConfigurationError` — multiple position fields set (`goTo`, `goToStep`, `complete`, `abort`, `reset`); `reply` co-existing with `abort`; `goTo` set as an empty object.
|
|
113
|
-
|
|
114
|
-
PreDirective-only fields surface no extra construction errors. Two runtime
|
|
115
|
-
behaviors are notable rather than thrown:
|
|
116
|
-
|
|
117
|
-
- Assigning a PreDirective with `appendPrompt`, `injectTools`, or `halt` to
|
|
118
|
-
`session.pendingDirective` causes those fields to be stripped before the
|
|
119
|
-
write (with a debug log). The remaining Directive fields persist normally.
|
|
120
|
-
- Returning a PreDirective from a post-LLM hook (`finalize`, `onComplete`)
|
|
121
|
-
causes the three PreDirective-only fields to be dropped with a debug log;
|
|
122
|
-
the Directive fields are honored.
|
|
123
|
-
|
|
124
|
-
## Related
|
|
125
|
-
|
|
126
|
-
- [Directives](../concepts/directives.md) — the mental model for the Directive → PreDirective → SignalDirective inheritance chain
|
|
127
|
-
- [Directive](./directive.md) — the parent interface and field semantics for position writes, `reply`, and state writes
|
|
128
|
-
- [Step](./step.md) — `hooks.onEnter` and `hooks.prepare` return `PreDirective`
|
|
129
|
-
- [Flow](./flow.md) — `hooks.onEnter` returns `PreDirective`
|
|
130
|
-
- [Signals](./signals.md) — `SignalDirective` extends `PreDirective` for pre-phase signal firings
|
|
131
|
-
- [Tool](./tool.md) — the type of values in `injectTools[]`
|