@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.
Files changed (126) hide show
  1. package/dist/cjs/core/Agent.d.ts +1 -1
  2. package/dist/cjs/core/Agent.js +3 -3
  3. package/dist/cjs/core/Agent.js.map +1 -1
  4. package/dist/cjs/core/AutoChainExecutor.d.ts +2 -2
  5. package/dist/cjs/core/AutoChainExecutor.js +2 -2
  6. package/dist/cjs/core/AutoChainExecutor.js.map +1 -1
  7. package/dist/cjs/core/PromptComposer.d.ts +1 -1
  8. package/dist/cjs/core/PromptComposer.js +2 -2
  9. package/dist/cjs/core/PromptComposer.js.map +1 -1
  10. package/dist/cjs/core/ResponseEngine.d.ts +1 -1
  11. package/dist/cjs/core/ResponseModal.js +6 -6
  12. package/dist/cjs/core/ResponseModal.js.map +1 -1
  13. package/dist/cjs/core/ResponsePipeline.d.ts +2 -2
  14. package/dist/cjs/core/ResponsePipeline.d.ts.map +1 -1
  15. package/dist/cjs/core/ResponsePipeline.js.map +1 -1
  16. package/dist/cjs/core/SignalProcessor.d.ts +3 -3
  17. package/dist/cjs/core/SignalProcessor.d.ts.map +1 -1
  18. package/dist/cjs/core/SignalProcessor.js +1 -1
  19. package/dist/cjs/core/SignalProcessor.js.map +1 -1
  20. package/dist/cjs/core/Step.d.ts +1 -1
  21. package/dist/cjs/core/Step.js +1 -1
  22. package/dist/cjs/core/ToolManager.d.ts +1 -1
  23. package/dist/cjs/core/ToolManager.js +1 -1
  24. package/dist/cjs/core/flow-namespace.d.ts +4 -4
  25. package/dist/cjs/core/flow-namespace.d.ts.map +1 -1
  26. package/dist/cjs/core/flow-namespace.js +14 -25
  27. package/dist/cjs/core/flow-namespace.js.map +1 -1
  28. package/dist/cjs/index.d.ts +3 -3
  29. package/dist/cjs/index.d.ts.map +1 -1
  30. package/dist/cjs/index.js +2 -2
  31. package/dist/cjs/index.js.map +1 -1
  32. package/dist/cjs/types/agent.d.ts +1 -1
  33. package/dist/cjs/types/agent.d.ts.map +1 -1
  34. package/dist/cjs/types/ai.d.ts +1 -1
  35. package/dist/cjs/types/ai.js +1 -1
  36. package/dist/cjs/types/flow.d.ts +33 -25
  37. package/dist/cjs/types/flow.d.ts.map +1 -1
  38. package/dist/cjs/types/index.d.ts +1 -1
  39. package/dist/cjs/types/index.d.ts.map +1 -1
  40. package/dist/cjs/types/index.js.map +1 -1
  41. package/dist/cjs/types/signals.d.ts +4 -5
  42. package/dist/cjs/types/signals.d.ts.map +1 -1
  43. package/dist/cjs/utils/session.d.ts +1 -1
  44. package/dist/cjs/utils/session.js +4 -4
  45. package/dist/cjs/utils/session.js.map +1 -1
  46. package/dist/core/Agent.d.ts +1 -1
  47. package/dist/core/Agent.js +3 -3
  48. package/dist/core/Agent.js.map +1 -1
  49. package/dist/core/AutoChainExecutor.d.ts +2 -2
  50. package/dist/core/AutoChainExecutor.js +2 -2
  51. package/dist/core/AutoChainExecutor.js.map +1 -1
  52. package/dist/core/PromptComposer.d.ts +1 -1
  53. package/dist/core/PromptComposer.js +2 -2
  54. package/dist/core/PromptComposer.js.map +1 -1
  55. package/dist/core/ResponseEngine.d.ts +1 -1
  56. package/dist/core/ResponseModal.js +6 -6
  57. package/dist/core/ResponseModal.js.map +1 -1
  58. package/dist/core/ResponsePipeline.d.ts +2 -2
  59. package/dist/core/ResponsePipeline.d.ts.map +1 -1
  60. package/dist/core/ResponsePipeline.js.map +1 -1
  61. package/dist/core/SignalProcessor.d.ts +3 -3
  62. package/dist/core/SignalProcessor.d.ts.map +1 -1
  63. package/dist/core/SignalProcessor.js +1 -1
  64. package/dist/core/SignalProcessor.js.map +1 -1
  65. package/dist/core/Step.d.ts +1 -1
  66. package/dist/core/Step.js +1 -1
  67. package/dist/core/ToolManager.d.ts +1 -1
  68. package/dist/core/ToolManager.js +1 -1
  69. package/dist/core/flow-namespace.d.ts +4 -4
  70. package/dist/core/flow-namespace.d.ts.map +1 -1
  71. package/dist/core/flow-namespace.js +14 -25
  72. package/dist/core/flow-namespace.js.map +1 -1
  73. package/dist/index.d.ts +3 -3
  74. package/dist/index.d.ts.map +1 -1
  75. package/dist/index.js +2 -2
  76. package/dist/index.js.map +1 -1
  77. package/dist/types/agent.d.ts +1 -1
  78. package/dist/types/agent.d.ts.map +1 -1
  79. package/dist/types/ai.d.ts +1 -1
  80. package/dist/types/ai.js +1 -1
  81. package/dist/types/flow.d.ts +33 -25
  82. package/dist/types/flow.d.ts.map +1 -1
  83. package/dist/types/index.d.ts +1 -1
  84. package/dist/types/index.d.ts.map +1 -1
  85. package/dist/types/index.js.map +1 -1
  86. package/dist/types/signals.d.ts +4 -5
  87. package/dist/types/signals.d.ts.map +1 -1
  88. package/dist/utils/session.d.ts +1 -1
  89. package/dist/utils/session.js +4 -4
  90. package/dist/utils/session.js.map +1 -1
  91. package/docs/concepts/architecture.md +19 -26
  92. package/docs/concepts/directives.md +53 -84
  93. package/docs/concepts/pipeline.md +4 -4
  94. package/docs/guides/flow-control.md +12 -13
  95. package/docs/guides/streaming.md +1 -1
  96. package/docs/migration/README.md +1 -1
  97. package/docs/migration/v1-to-v2.md +507 -5
  98. package/docs/reference/adapters.md +1 -1
  99. package/docs/reference/directive.md +9 -10
  100. package/docs/reference/flow.md +3 -3
  101. package/docs/reference/instruction.md +1 -1
  102. package/docs/reference/signals.md +3 -4
  103. package/docs/reference/step.md +9 -10
  104. package/docs/reference/tool.md +1 -1
  105. package/docs/start/02-first-agent.md +1 -2
  106. package/examples/tsconfig.json +1 -3
  107. package/package.json +9 -5
  108. package/src/core/Agent.ts +4 -4
  109. package/src/core/AutoChainExecutor.ts +3 -3
  110. package/src/core/PromptComposer.ts +2 -2
  111. package/src/core/ResponseEngine.ts +1 -1
  112. package/src/core/ResponseModal.ts +18 -18
  113. package/src/core/ResponsePipeline.ts +1 -2
  114. package/src/core/SignalProcessor.ts +7 -7
  115. package/src/core/Step.ts +1 -1
  116. package/src/core/ToolManager.ts +1 -1
  117. package/src/core/flow-namespace.ts +32 -53
  118. package/src/index.ts +2 -3
  119. package/src/types/agent.ts +1 -1
  120. package/src/types/ai.ts +1 -1
  121. package/src/types/flow.ts +41 -26
  122. package/src/types/index.ts +0 -1
  123. package/src/types/signals.ts +4 -5
  124. package/src/utils/session.ts +5 -5
  125. package/docs/migration/route-to-flow.md +0 -561
  126. package/docs/reference/pre-directive.md +0 -131
@@ -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 strategy for generating responses */
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
@@ -1,5 +1,5 @@
1
1
  /**
2
- * AI provider strategy types
2
+ * AI provider types
3
3
  */
4
4
 
5
5
  import type { HistoryItem } from "./history";
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. Encodes where to go and what state
83
- * writes to apply. Used as the `then` target of a `BranchEntry` when the
84
- * destination is more complex than a simple step/flow id string.
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<TContext, TData>; reason?: string; };
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?: Array<Tool<TContext, TData>>;
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 | PreDirective`.
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 PreDirective to augment the prompt, inject tools, or halt.
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 | PreDirective<TContext, TData> | Promise<void | PreDirective<TContext, TData>>;
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 | PreDirective`.
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 PreDirective to augment the prompt,
411
- * inject tools, or halt.
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 | PreDirective<TContext, TData> | Promise<void | PreDirective<TContext, TData>>;
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 PreDirective to augment the prompt,
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 | PreDirective<TContext, TData> | Promise<void | PreDirective<TContext, TData>>;
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 `PreDirective` with its own `reply` field, the
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.
@@ -52,7 +52,6 @@ export type {
52
52
  FlowLifecycleHooks,
53
53
  StepLifecycleHooks,
54
54
  Directive,
55
- PreDirective,
56
55
  BranchEntry,
57
56
  BranchMap,
58
57
  BranchPredicate,
@@ -8,7 +8,7 @@
8
8
  */
9
9
 
10
10
  import type { SessionState } from "./session";
11
- import type { PreDirective } from "./flow";
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 `PreDirective` (which extends `Directive`). Adds signal-specific
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 debug warning — they have
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 PreDirective<TContext, TData> {
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.
@@ -262,7 +262,7 @@ export function sessionStepToData<TData = Record<string, unknown>>(
262
262
  currentStep?: string;
263
263
  collectedData: CollectedStateData<TData>;
264
264
  } {
265
- // Strip PreDirective fields before persisting pendingDirective
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 PreDirective-only fields from a directive before persistence.
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.debug(
384
- `[createPersistedState] Stripped PreDirective-only fields before persistence: ${droppedFields.join(", ")}`
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 PreDirective-only fields (`appendPrompt`,
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