@falai/agent 2.0.1 → 2.1.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/dist/cjs/core/Agent.d.ts +1 -1
- package/dist/cjs/core/Agent.js +5 -5
- 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 +5 -5
- 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 +7 -7
- 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
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @falai/agent
|
|
2
|
+
* @falai/agent — Conversational state engine for TypeScript
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* The AI understands. The code is in control.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
// Core
|
|
@@ -173,7 +173,6 @@ export type {
|
|
|
173
173
|
StoppedReason,
|
|
174
174
|
PrepareResult,
|
|
175
175
|
Directive,
|
|
176
|
-
PreDirective,
|
|
177
176
|
BranchEntry,
|
|
178
177
|
BranchMap,
|
|
179
178
|
BranchPredicate,
|
package/src/types/agent.ts
CHANGED
|
@@ -146,7 +146,7 @@ export interface AgentOptions<TContext = unknown, TData = unknown> {
|
|
|
146
146
|
contextProvider?: ContextProvider<TContext>;
|
|
147
147
|
/** Lifecycle hooks for context management */
|
|
148
148
|
hooks?: ContextLifecycleHooks<TContext, TData>;
|
|
149
|
-
/** AI provider
|
|
149
|
+
/** AI provider for generating responses */
|
|
150
150
|
provider: AiProvider;
|
|
151
151
|
/** Initial terms for domain glossary */
|
|
152
152
|
terms?: Term<TContext, TData>[];
|
package/src/types/ai.ts
CHANGED
package/src/types/flow.ts
CHANGED
|
@@ -79,54 +79,61 @@ export interface PrepareResult {
|
|
|
79
79
|
// ─── Branch types ────────────────────────────────────────────────────────────
|
|
80
80
|
|
|
81
81
|
/**
|
|
82
|
-
* A programmatic transition directive.
|
|
83
|
-
*
|
|
84
|
-
*
|
|
82
|
+
* A programmatic transition directive. The single shape any tool, hook, branch,
|
|
83
|
+
* or signal handler returns to write state, redirect the conversation, or speak
|
|
84
|
+
* verbatim.
|
|
85
85
|
*
|
|
86
86
|
* At most one position field (`goTo`, `goToStep`, `complete`, `abort`, `reset`)
|
|
87
87
|
* may be set per Directive. Non-position fields (`reply`, `contextUpdate`,
|
|
88
88
|
* `dataUpdate`) may accompany any position field.
|
|
89
|
+
*
|
|
90
|
+
* Pre-LLM augmentation fields (`appendPrompt`, `injectTools`, `halt`) are
|
|
91
|
+
* one-turn-lifetime: they only take effect in pre-LLM hooks (`onEnter`,
|
|
92
|
+
* `prepare`). When emitted from post-LLM hooks (`finalize`, `onComplete`) or
|
|
93
|
+
* persisted to `session.pendingDirective`, these fields are ignored and a WARN
|
|
94
|
+
* log is emitted. They are never serialized across turns.
|
|
89
95
|
*/
|
|
90
96
|
export interface Directive<TContext = unknown, TData = unknown> {
|
|
97
|
+
// ── Position fields (mutually exclusive: at most one) ────────────
|
|
91
98
|
/** Jump to a flow (string) or a flow with options (object). */
|
|
92
99
|
goTo?: string | { flow?: string; step?: string; data?: Partial<TData>; reason?: string; carry?: 'preserve' | 'reset'; };
|
|
93
100
|
/** Jump to a specific step, optionally in another flow. */
|
|
94
101
|
goToStep?: string | { step: string; flow?: string; data?: Partial<TData>; reason?: string; };
|
|
95
102
|
/** Mark the current flow as complete. */
|
|
96
|
-
complete?: true | { next?: Directive<
|
|
103
|
+
complete?: true | { next?: Directive<unknown, unknown>; reason?: string; };
|
|
97
104
|
/** Abort the current flow. */
|
|
98
105
|
abort?: string | { reason: string; clearSession?: boolean; };
|
|
99
106
|
/** Reset the current flow (or jump to a step within it). */
|
|
100
107
|
reset?: true | { step?: string; clearData?: boolean; reason?: string; };
|
|
108
|
+
|
|
109
|
+
// ── Verbatim utterance ───────────────────────────────────────────
|
|
101
110
|
/** Verbatim reply text to send to the user. */
|
|
102
111
|
reply?: string;
|
|
112
|
+
|
|
113
|
+
// ── State writes ─────────────────────────────────────────────────
|
|
103
114
|
/** Partial context update applied before the next turn. */
|
|
104
115
|
contextUpdate?: Partial<TContext>;
|
|
105
116
|
/** Partial data update applied before the next turn. */
|
|
106
117
|
dataUpdate?: Partial<TData>;
|
|
107
|
-
}
|
|
108
118
|
|
|
109
|
-
|
|
110
|
-
* PreDirective — what `prepare` hooks and `onEnter` hooks return.
|
|
111
|
-
* Adds prompt/tool shaping that only makes sense BEFORE the LLM call this turn.
|
|
112
|
-
* Extends Directive — anything a Directive can do, a PreDirective can do too.
|
|
113
|
-
*
|
|
114
|
-
* NOT persistable: contains live Tool references and a halt flag that
|
|
115
|
-
* has no meaning across turns. This type is transient (one-turn lifetime).
|
|
116
|
-
*/
|
|
117
|
-
export interface PreDirective<TContext = unknown, TData = unknown>
|
|
118
|
-
extends Directive<TContext, TData> {
|
|
119
|
+
// ── Pre-LLM augmentation (one-turn lifetime) ─────────────────────
|
|
119
120
|
/**
|
|
120
121
|
* Sentences to append to the system prompt for THIS turn only.
|
|
121
122
|
* Wired through PromptComposer's per-turn appendage slot.
|
|
123
|
+
*
|
|
124
|
+
* Only meaningful in pre-LLM hooks (`onEnter`, `prepare`). Ignored with
|
|
125
|
+
* a WARN log when emitted from post-LLM hooks or persisted.
|
|
122
126
|
*/
|
|
123
127
|
appendPrompt?: string[];
|
|
124
128
|
|
|
125
129
|
/**
|
|
126
130
|
* Tools available for THIS turn only. Stacked on top of agent/flow/step
|
|
127
131
|
* tool scopes via ToolManager's transient layer.
|
|
132
|
+
*
|
|
133
|
+
* Only meaningful in pre-LLM hooks (`onEnter`, `prepare`). Ignored with
|
|
134
|
+
* a WARN log when emitted from post-LLM hooks or persisted.
|
|
128
135
|
*/
|
|
129
|
-
injectTools?:
|
|
136
|
+
injectTools?: Tool[];
|
|
130
137
|
|
|
131
138
|
/**
|
|
132
139
|
* If true, skip the LLM call entirely this turn.
|
|
@@ -135,10 +142,15 @@ export interface PreDirective<TContext = unknown, TData = unknown>
|
|
|
135
142
|
* assistant output (`stoppedReason: 'reply'`). When `halt` is true
|
|
136
143
|
* without `reply`, the turn produces an empty assistant message
|
|
137
144
|
* (`stoppedReason: 'halt'`).
|
|
145
|
+
*
|
|
146
|
+
* Only meaningful in pre-LLM hooks (`onEnter`, `prepare`). Ignored with
|
|
147
|
+
* a WARN log when emitted from post-LLM hooks or persisted.
|
|
138
148
|
*/
|
|
139
149
|
halt?: boolean;
|
|
140
150
|
}
|
|
141
151
|
|
|
152
|
+
|
|
153
|
+
|
|
142
154
|
/**
|
|
143
155
|
* Context passed to a `BranchPredicate` function.
|
|
144
156
|
*
|
|
@@ -241,7 +253,7 @@ export interface StepRef {
|
|
|
241
253
|
/**
|
|
242
254
|
* Flow lifecycle hooks for managing flow-specific data and behavior.
|
|
243
255
|
*
|
|
244
|
-
* Pre-LLM hooks (`onEnter`) return `void |
|
|
256
|
+
* Pre-LLM hooks (`onEnter`) return `void | Directive`.
|
|
245
257
|
* Post-LLM hooks (`onComplete`) return `void | Directive`.
|
|
246
258
|
* Informational hooks (`onExit`) return `void`.
|
|
247
259
|
* Data hooks (`onDataUpdate`, `onContextUpdate`) retain their v1 signatures.
|
|
@@ -249,11 +261,12 @@ export interface StepRef {
|
|
|
249
261
|
export interface FlowLifecycleHooks<TContext = unknown, TData = unknown> {
|
|
250
262
|
/**
|
|
251
263
|
* Called when the flow is first entered.
|
|
252
|
-
* May return a
|
|
264
|
+
* May return a Directive to augment the prompt, inject tools, halt, or redirect.
|
|
265
|
+
* Pre-LLM fields (`appendPrompt`, `injectTools`, `halt`) are honored here.
|
|
253
266
|
*/
|
|
254
267
|
onEnter?: (
|
|
255
268
|
ctx: HookContext<TContext, TData>
|
|
256
|
-
) => void |
|
|
269
|
+
) => void | Directive<TContext, TData> | Promise<void | Directive<TContext, TData>>;
|
|
257
270
|
|
|
258
271
|
/**
|
|
259
272
|
* Called when the flow is exited. Informational only — cannot influence flow control.
|
|
@@ -401,18 +414,19 @@ export interface FlowOptions<TContext = unknown, TData = unknown> {
|
|
|
401
414
|
/**
|
|
402
415
|
* Step lifecycle hooks for managing step-specific behavior.
|
|
403
416
|
*
|
|
404
|
-
* Pre-LLM hooks (`onEnter`, `prepare`) return `void |
|
|
417
|
+
* Pre-LLM hooks (`onEnter`, `prepare`) return `void | Directive`.
|
|
405
418
|
* Post-LLM hooks (`finalize`) return `void | Directive`.
|
|
406
419
|
* Informational hooks (`onExit`) return `void`.
|
|
407
420
|
*/
|
|
408
421
|
export interface StepLifecycleHooks<TContext = unknown, TData = unknown> {
|
|
409
422
|
/**
|
|
410
|
-
* Called on step entry. May return a
|
|
411
|
-
* inject tools, or
|
|
423
|
+
* Called on step entry. May return a Directive to augment the prompt,
|
|
424
|
+
* inject tools, halt, or redirect.
|
|
425
|
+
* Pre-LLM fields (`appendPrompt`, `injectTools`, `halt`) are honored here.
|
|
412
426
|
*/
|
|
413
427
|
onEnter?: (
|
|
414
428
|
ctx: HookContext<TContext, TData>
|
|
415
|
-
) => void |
|
|
429
|
+
) => void | Directive<TContext, TData> | Promise<void | Directive<TContext, TData>>;
|
|
416
430
|
|
|
417
431
|
/**
|
|
418
432
|
* Called when the step is exited. Informational only — cannot influence flow control.
|
|
@@ -424,12 +438,13 @@ export interface StepLifecycleHooks<TContext = unknown, TData = unknown> {
|
|
|
424
438
|
) => void | Promise<void>;
|
|
425
439
|
|
|
426
440
|
/**
|
|
427
|
-
* Called pre-LLM. May return a
|
|
441
|
+
* Called pre-LLM. May return a Directive to augment the prompt,
|
|
428
442
|
* inject tools, or halt the LLM call.
|
|
443
|
+
* Pre-LLM fields (`appendPrompt`, `injectTools`, `halt`) are honored here.
|
|
429
444
|
*/
|
|
430
445
|
prepare?: (
|
|
431
446
|
ctx: HookContext<TContext, TData>
|
|
432
|
-
) => void |
|
|
447
|
+
) => void | Directive<TContext, TData> | Promise<void | Directive<TContext, TData>>;
|
|
433
448
|
|
|
434
449
|
/**
|
|
435
450
|
* Called post-LLM. May return a Directive to redirect flow, write state,
|
|
@@ -524,7 +539,7 @@ export interface StepOptions<TContext = unknown, TData = unknown> {
|
|
|
524
539
|
* at construction time.
|
|
525
540
|
*
|
|
526
541
|
* `onEnter` and `prepare` hooks fire normally before the reply is rendered.
|
|
527
|
-
* If `prepare` returns a
|
|
542
|
+
* If `prepare` returns a Directive with its own `reply` field, the
|
|
528
543
|
* hook-emitted reply wins (last-emission-wins per Algorithm 4).
|
|
529
544
|
*
|
|
530
545
|
* After emission, `onExit` fires and `branches` are resolved for next step.
|
package/src/types/index.ts
CHANGED
package/src/types/signals.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import type { SessionState } from "./session";
|
|
11
|
-
import type {
|
|
11
|
+
import type { Directive } from "./flow";
|
|
12
12
|
import type { Event } from "./history";
|
|
13
13
|
|
|
14
14
|
// ──────────────────────────────────────────────────────────────────────────────
|
|
@@ -100,19 +100,18 @@ export type SignalPredicate<TContext = unknown, TData = unknown> = (
|
|
|
100
100
|
|
|
101
101
|
/**
|
|
102
102
|
* SignalDirective — what signal handlers return.
|
|
103
|
-
* Extends `
|
|
104
|
-
* fields: `stopOtherSignals` and `replyWith`.
|
|
103
|
+
* Extends `Directive`. Adds signal-specific fields: `stopOtherSignals` and `replyWith`.
|
|
105
104
|
*
|
|
106
105
|
* All position-control (`goTo`, `goToStep`, `complete`, `abort`, `reset`),
|
|
107
106
|
* state writes (`dataUpdate`, `contextUpdate`), prompt augmentation
|
|
108
107
|
* (`appendPrompt`, `injectTools`), `reply`, and `halt` are inherited unchanged.
|
|
109
108
|
*
|
|
110
109
|
* Post-phase drop rules: when returned in the post-phase, `appendPrompt`,
|
|
111
|
-
* `injectTools`, and `halt` are dropped with a
|
|
110
|
+
* `injectTools`, and `halt` are dropped with a WARN log — they have
|
|
112
111
|
* no meaning after the LLM call has already completed.
|
|
113
112
|
*/
|
|
114
113
|
export interface SignalDirective<TContext = unknown, TData = unknown>
|
|
115
|
-
extends
|
|
114
|
+
extends Directive<TContext, TData> {
|
|
116
115
|
/**
|
|
117
116
|
* Stop processing remaining signals for this phase after this handler.
|
|
118
117
|
* Does not affect the other phase.
|
package/src/utils/session.ts
CHANGED
|
@@ -262,7 +262,7 @@ export function sessionStepToData<TData = Record<string, unknown>>(
|
|
|
262
262
|
currentStep?: string;
|
|
263
263
|
collectedData: CollectedStateData<TData>;
|
|
264
264
|
} {
|
|
265
|
-
// Strip
|
|
265
|
+
// Strip pre-LLM-only fields before persisting pendingDirective
|
|
266
266
|
let pendingDirective: SessionState<TData>["pendingDirective"] | undefined;
|
|
267
267
|
if (session.pendingDirective) {
|
|
268
268
|
pendingDirective = stripPreDirectiveFields(session.pendingDirective);
|
|
@@ -354,7 +354,7 @@ export function sessionDataToStep<TData = Record<string, unknown>>(
|
|
|
354
354
|
|
|
355
355
|
|
|
356
356
|
/**
|
|
357
|
-
* Strip
|
|
357
|
+
* Strip pre-LLM-only fields from a directive before persistence.
|
|
358
358
|
*
|
|
359
359
|
* `appendPrompt`, `injectTools`, and `halt` are transient (one-turn lifetime)
|
|
360
360
|
* and must not be serialized. This is a belt-and-suspenders safety net —
|
|
@@ -380,8 +380,8 @@ function stripPreDirectiveFields<TData>(
|
|
|
380
380
|
].filter(Boolean);
|
|
381
381
|
|
|
382
382
|
if (droppedFields.length > 0) {
|
|
383
|
-
logger.
|
|
384
|
-
`[createPersistedState]
|
|
383
|
+
logger.warn(
|
|
384
|
+
`[createPersistedState] Ignoring pre-LLM-only fields before persistence (these have no effect outside pre-LLM hooks): ${droppedFields.join(", ")}`
|
|
385
385
|
);
|
|
386
386
|
}
|
|
387
387
|
|
|
@@ -393,7 +393,7 @@ function stripPreDirectiveFields<TData>(
|
|
|
393
393
|
*
|
|
394
394
|
* This is the shared helper that every persistence adapter should call before
|
|
395
395
|
* writing session state. It ensures:
|
|
396
|
-
* - `pendingDirective` has
|
|
396
|
+
* - `pendingDirective` has pre-LLM-only fields (`appendPrompt`,
|
|
397
397
|
* `injectTools`, `halt`) stripped (these are one-turn-lifetime and not
|
|
398
398
|
* serializable across turns).
|
|
399
399
|
* - `pendingDirective` is omitted from the result when `undefined` (adapters
|