@falai/agent 2.4.3 → 2.6.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 (121) hide show
  1. package/dist/cjs/core/FlowRouter.d.ts.map +1 -1
  2. package/dist/cjs/core/FlowRouter.js +0 -11
  3. package/dist/cjs/core/FlowRouter.js.map +1 -1
  4. package/dist/cjs/core/ResponseModal.d.ts +51 -2
  5. package/dist/cjs/core/ResponseModal.d.ts.map +1 -1
  6. package/dist/cjs/core/ResponseModal.js +272 -269
  7. package/dist/cjs/core/ResponseModal.js.map +1 -1
  8. package/dist/cjs/core/ToolLoopExecutor.d.ts +11 -0
  9. package/dist/cjs/core/ToolLoopExecutor.d.ts.map +1 -1
  10. package/dist/cjs/core/ToolLoopExecutor.js +75 -51
  11. package/dist/cjs/core/ToolLoopExecutor.js.map +1 -1
  12. package/dist/cjs/index.d.ts +3 -0
  13. package/dist/cjs/index.d.ts.map +1 -1
  14. package/dist/cjs/index.js +4 -2
  15. package/dist/cjs/index.js.map +1 -1
  16. package/dist/cjs/providers/AnthropicProvider.d.ts.map +1 -1
  17. package/dist/cjs/providers/AnthropicProvider.js +18 -35
  18. package/dist/cjs/providers/AnthropicProvider.js.map +1 -1
  19. package/dist/cjs/providers/DeepSeekProvider.d.ts +1 -12
  20. package/dist/cjs/providers/DeepSeekProvider.d.ts.map +1 -1
  21. package/dist/cjs/providers/DeepSeekProvider.js +3 -20
  22. package/dist/cjs/providers/DeepSeekProvider.js.map +1 -1
  23. package/dist/cjs/providers/GeminiProvider.d.ts.map +1 -1
  24. package/dist/cjs/providers/GeminiProvider.js +22 -37
  25. package/dist/cjs/providers/GeminiProvider.js.map +1 -1
  26. package/dist/cjs/providers/GenericOpenAICompatibleProvider.d.ts +69 -0
  27. package/dist/cjs/providers/GenericOpenAICompatibleProvider.d.ts.map +1 -0
  28. package/dist/cjs/providers/GenericOpenAICompatibleProvider.js +93 -0
  29. package/dist/cjs/providers/GenericOpenAICompatibleProvider.js.map +1 -0
  30. package/dist/cjs/providers/OpenAICompatibleProvider.d.ts +23 -5
  31. package/dist/cjs/providers/OpenAICompatibleProvider.d.ts.map +1 -1
  32. package/dist/cjs/providers/OpenAICompatibleProvider.js +46 -44
  33. package/dist/cjs/providers/OpenAICompatibleProvider.js.map +1 -1
  34. package/dist/cjs/providers/index.d.ts +3 -1
  35. package/dist/cjs/providers/index.d.ts.map +1 -1
  36. package/dist/cjs/providers/index.js +3 -1
  37. package/dist/cjs/providers/index.js.map +1 -1
  38. package/dist/cjs/utils/completion.d.ts +20 -0
  39. package/dist/cjs/utils/completion.d.ts.map +1 -0
  40. package/dist/cjs/utils/completion.js +33 -0
  41. package/dist/cjs/utils/completion.js.map +1 -0
  42. package/dist/cjs/utils/index.d.ts +3 -2
  43. package/dist/cjs/utils/index.d.ts.map +1 -1
  44. package/dist/cjs/utils/index.js +7 -1
  45. package/dist/cjs/utils/index.js.map +1 -1
  46. package/dist/cjs/utils/retry.d.ts +43 -7
  47. package/dist/cjs/utils/retry.d.ts.map +1 -1
  48. package/dist/cjs/utils/retry.js +100 -12
  49. package/dist/cjs/utils/retry.js.map +1 -1
  50. package/dist/cjs/utils/streamingMessage.d.ts +48 -0
  51. package/dist/cjs/utils/streamingMessage.d.ts.map +1 -0
  52. package/dist/cjs/utils/streamingMessage.js +210 -0
  53. package/dist/cjs/utils/streamingMessage.js.map +1 -0
  54. package/dist/core/FlowRouter.d.ts.map +1 -1
  55. package/dist/core/FlowRouter.js +0 -11
  56. package/dist/core/FlowRouter.js.map +1 -1
  57. package/dist/core/ResponseModal.d.ts +51 -2
  58. package/dist/core/ResponseModal.d.ts.map +1 -1
  59. package/dist/core/ResponseModal.js +272 -269
  60. package/dist/core/ResponseModal.js.map +1 -1
  61. package/dist/core/ToolLoopExecutor.d.ts +11 -0
  62. package/dist/core/ToolLoopExecutor.d.ts.map +1 -1
  63. package/dist/core/ToolLoopExecutor.js +76 -52
  64. package/dist/core/ToolLoopExecutor.js.map +1 -1
  65. package/dist/index.d.ts +3 -0
  66. package/dist/index.d.ts.map +1 -1
  67. package/dist/index.js +1 -0
  68. package/dist/index.js.map +1 -1
  69. package/dist/providers/AnthropicProvider.d.ts.map +1 -1
  70. package/dist/providers/AnthropicProvider.js +19 -36
  71. package/dist/providers/AnthropicProvider.js.map +1 -1
  72. package/dist/providers/DeepSeekProvider.d.ts +1 -12
  73. package/dist/providers/DeepSeekProvider.d.ts.map +1 -1
  74. package/dist/providers/DeepSeekProvider.js +3 -20
  75. package/dist/providers/DeepSeekProvider.js.map +1 -1
  76. package/dist/providers/GeminiProvider.d.ts.map +1 -1
  77. package/dist/providers/GeminiProvider.js +23 -38
  78. package/dist/providers/GeminiProvider.js.map +1 -1
  79. package/dist/providers/GenericOpenAICompatibleProvider.d.ts +69 -0
  80. package/dist/providers/GenericOpenAICompatibleProvider.d.ts.map +1 -0
  81. package/dist/providers/GenericOpenAICompatibleProvider.js +87 -0
  82. package/dist/providers/GenericOpenAICompatibleProvider.js.map +1 -0
  83. package/dist/providers/OpenAICompatibleProvider.d.ts +23 -5
  84. package/dist/providers/OpenAICompatibleProvider.d.ts.map +1 -1
  85. package/dist/providers/OpenAICompatibleProvider.js +47 -45
  86. package/dist/providers/OpenAICompatibleProvider.js.map +1 -1
  87. package/dist/providers/index.d.ts +3 -1
  88. package/dist/providers/index.d.ts.map +1 -1
  89. package/dist/providers/index.js +1 -0
  90. package/dist/providers/index.js.map +1 -1
  91. package/dist/utils/completion.d.ts +20 -0
  92. package/dist/utils/completion.d.ts.map +1 -0
  93. package/dist/utils/completion.js +29 -0
  94. package/dist/utils/completion.js.map +1 -0
  95. package/dist/utils/index.d.ts +3 -2
  96. package/dist/utils/index.d.ts.map +1 -1
  97. package/dist/utils/index.js +3 -1
  98. package/dist/utils/index.js.map +1 -1
  99. package/dist/utils/retry.d.ts +43 -7
  100. package/dist/utils/retry.d.ts.map +1 -1
  101. package/dist/utils/retry.js +98 -12
  102. package/dist/utils/retry.js.map +1 -1
  103. package/dist/utils/streamingMessage.d.ts +48 -0
  104. package/dist/utils/streamingMessage.d.ts.map +1 -0
  105. package/dist/utils/streamingMessage.js +205 -0
  106. package/dist/utils/streamingMessage.js.map +1 -0
  107. package/package.json +1 -1
  108. package/src/core/FlowRouter.ts +0 -14
  109. package/src/core/ResponseModal.ts +332 -299
  110. package/src/core/ToolLoopExecutor.ts +108 -52
  111. package/src/index.ts +3 -0
  112. package/src/providers/AnthropicProvider.ts +22 -43
  113. package/src/providers/DeepSeekProvider.ts +3 -35
  114. package/src/providers/GeminiProvider.ts +27 -46
  115. package/src/providers/GenericOpenAICompatibleProvider.ts +138 -0
  116. package/src/providers/OpenAICompatibleProvider.ts +87 -67
  117. package/src/providers/index.ts +4 -0
  118. package/src/utils/completion.ts +38 -0
  119. package/src/utils/index.ts +5 -2
  120. package/src/utils/retry.ts +136 -14
  121. package/src/utils/streamingMessage.ts +220 -0
@@ -5,8 +5,37 @@ export interface RetryOptions<T> {
5
5
  onRetry?: (attempt: number, error: unknown) => void;
6
6
  onFailure?: (error: unknown) => boolean;
7
7
  }
8
+ /** Provider timeout (ms) + retry count, after defaults are applied. */
9
+ export interface RetryConfig {
10
+ timeout: number;
11
+ retries: number;
12
+ }
13
+ /**
14
+ * Normalize a provider's optional retry config against the defaults. `timeout`
15
+ * uses `||` (a 0ms timeout is degenerate — it aborts every call immediately — so
16
+ * fall back to the default), while `retries` uses `??` so an explicit
17
+ * `retries: 0` (disable retries) is honored rather than treated as unset. Single
18
+ * definition so this distinction can't drift between providers.
19
+ */
20
+ export declare function resolveRetryConfig(input?: {
21
+ timeout?: number;
22
+ retries?: number;
23
+ }): RetryConfig;
8
24
  export declare function retry<T>(options: RetryOptions<T>): Promise<T>;
9
- export declare const withTimeoutAndRetry: <T>(operation: () => Promise<T>, timeoutMs?: number, maxRetries?: number, operationName?: string) => Promise<T>;
25
+ /**
26
+ * Combine abort signals into one that aborts as soon as any input aborts.
27
+ * `undefined` inputs are skipped, so a caller can pass an optional caller-signal
28
+ * alongside a required per-attempt one. Returns the sole signal when only one is
29
+ * present (no wrapper), or `undefined` when none are.
30
+ *
31
+ * Prefers the platform `AbortSignal.any`, which cleans up its listeners via weak
32
+ * refs — so merging onto a long-lived caller signal that's reused across many
33
+ * calls can't accumulate listeners. Falls back to a manual controller on older
34
+ * runtimes (< Node 20.3); there the listeners live until a source aborts or is
35
+ * garbage-collected, which is fine for the usual per-request signal.
36
+ */
37
+ export declare function combineAbortSignals(...signals: Array<AbortSignal | undefined>): AbortSignal | undefined;
38
+ export declare const withTimeoutAndRetry: <T>(operation: (signal: AbortSignal) => Promise<T>, timeoutMs?: number, maxRetries?: number, operationName?: string) => Promise<T>;
10
39
  export interface StreamRetryOptions {
11
40
  /** Maximum number of retries after the first attempt. Defaults to 3. */
12
41
  maxRetries?: number;
@@ -14,18 +43,25 @@ export interface StreamRetryOptions {
14
43
  delay?: (attempt: number) => number;
15
44
  /** Label used in retry logs. */
16
45
  operationName?: string;
46
+ /**
47
+ * Max ms to wait for the *first* chunk (time-to-first-token) before treating
48
+ * the attempt as failed. Guards a provider that opens a stream and then
49
+ * stalls. Only the first chunk is bounded — later chunks are unbounded so a
50
+ * long but healthy stream is never cut off. `0`/omitted disables the deadline.
51
+ */
52
+ firstChunkTimeoutMs?: number;
17
53
  }
18
54
  /**
19
55
  * Streaming analog of {@link withTimeoutAndRetry}. Re-runs an async-generator
20
- * factory as long as it fails *before yielding its first chunk* — e.g. an
21
- * empty completion that throws "No response", or an error while establishing
22
- * the stream. Once any chunk has been yielded the stream is committed and
23
- * further errors propagate, so a retry can never double-emit deltas that the
24
- * consumer has already received.
56
+ * factory as long as it fails *before yielding its first chunk* — an empty
57
+ * completion that throws "No response", an error while establishing the stream,
58
+ * or (with `firstChunkTimeoutMs`) a stall before the first token. Once any chunk
59
+ * has been yielded the stream is committed and further errors propagate, so a
60
+ * retry can never double-emit deltas the consumer has already received.
25
61
  *
26
62
  * This mirrors the non-streaming path, where the provider throws on an empty
27
63
  * completion inside `withTimeoutAndRetry` and is retried on the same model
28
64
  * before the caller falls through to backup models.
29
65
  */
30
- export declare function withStreamRetry<T>(factory: () => AsyncGenerator<T>, options?: StreamRetryOptions): AsyncGenerator<T>;
66
+ export declare function withStreamRetry<T>(factory: (signal: AbortSignal) => AsyncGenerator<T>, options?: StreamRetryOptions): AsyncGenerator<T>;
31
67
  //# sourceMappingURL=retry.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../src/utils/retry.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,YAAY,CAAC,CAAC;IAC7B,SAAS,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,CAAC;IACnC,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;IACpD,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC;CACzC;AAED,wBAAsB,KAAK,CAAC,CAAC,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CA8BnE;AAED,eAAO,MAAM,mBAAmB,GAAU,CAAC,EACzC,WAAW,MAAM,OAAO,CAAC,CAAC,CAAC,EAC3B,YAAW,MAAc,EACzB,aAAY,MAAU,EACtB,gBAAe,MAAuB,KACrC,OAAO,CAAC,CAAC,CA8CX,CAAC;AAEF,MAAM,WAAW,kBAAkB;IACjC,wEAAwE;IACxE,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,8EAA8E;IAC9E,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,CAAC;IACpC,gCAAgC;IAChC,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;;;;;;GAWG;AACH,wBAAuB,e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,OAAO,CAAC,CAAC,CAiDX,CAAC;AAEF,MAAM,WAAW,kBAAkB;IACjC,wEAAwE;IACxE,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,8EAA8E;IAC9E,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,MAAM,CAAC;IACpC,gCAAgC;IAChC,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;;;OAKG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;CAC9B;AAuBD;;;;;;;;;;;GAWG;AACH,wBAAuB,eAAe,CAAC,CAAC,EACtC,OAAO,EAAE,CAAC,MAAM,EAAE,WAAW,KAAK,cAAc,CAAC,CAAC,CAAC,EACnD,OAAO,GAAE,kBAAuB,GAC/B,cAAc,CAAC,CAAC,CAAC,CAwDnB"}
@@ -2,6 +2,23 @@
2
2
  * Retry utility with exponential backoff
3
3
  */
4
4
  import { logger } from './logger';
5
+ const DEFAULT_RETRY_CONFIG = {
6
+ timeout: 60000,
7
+ retries: 3,
8
+ };
9
+ /**
10
+ * Normalize a provider's optional retry config against the defaults. `timeout`
11
+ * uses `||` (a 0ms timeout is degenerate — it aborts every call immediately — so
12
+ * fall back to the default), while `retries` uses `??` so an explicit
13
+ * `retries: 0` (disable retries) is honored rather than treated as unset. Single
14
+ * definition so this distinction can't drift between providers.
15
+ */
16
+ export function resolveRetryConfig(input) {
17
+ return {
18
+ timeout: input?.timeout || DEFAULT_RETRY_CONFIG.timeout,
19
+ retries: input?.retries ?? DEFAULT_RETRY_CONFIG.retries,
20
+ };
21
+ }
5
22
  export async function retry(options) {
6
23
  const { operation, maxRetries, delay, onRetry, onFailure } = options;
7
24
  let lastError;
@@ -28,6 +45,38 @@ export async function retry(options) {
28
45
  // eslint-disable-next-line @typescript-eslint/only-throw-error
29
46
  throw lastError ?? new Error("Operation failed");
30
47
  }
48
+ /** Capped exponential backoff (1s, 2s, 4s, … max 5s) shared by the retry helpers. */
49
+ const defaultBackoff = (attempt) => Math.min(1000 * Math.pow(2, attempt), 5000);
50
+ /**
51
+ * Combine abort signals into one that aborts as soon as any input aborts.
52
+ * `undefined` inputs are skipped, so a caller can pass an optional caller-signal
53
+ * alongside a required per-attempt one. Returns the sole signal when only one is
54
+ * present (no wrapper), or `undefined` when none are.
55
+ *
56
+ * Prefers the platform `AbortSignal.any`, which cleans up its listeners via weak
57
+ * refs — so merging onto a long-lived caller signal that's reused across many
58
+ * calls can't accumulate listeners. Falls back to a manual controller on older
59
+ * runtimes (< Node 20.3); there the listeners live until a source aborts or is
60
+ * garbage-collected, which is fine for the usual per-request signal.
61
+ */
62
+ export function combineAbortSignals(...signals) {
63
+ const present = signals.filter((s) => s != null);
64
+ if (present.length <= 1)
65
+ return present[0];
66
+ if (typeof AbortSignal.any === "function")
67
+ return AbortSignal.any(present);
68
+ const controller = new AbortController();
69
+ for (const signal of present) {
70
+ if (signal.aborted) {
71
+ controller.abort(signal.reason);
72
+ break;
73
+ }
74
+ signal.addEventListener("abort", () => controller.abort(signal.reason), {
75
+ once: true,
76
+ });
77
+ }
78
+ return controller.signal;
79
+ }
31
80
  export const withTimeoutAndRetry = async (operation, timeoutMs = 60000, maxRetries = 3, operationName = "AI operation") => {
32
81
  const createTimeoutOperation = () => async () => {
33
82
  const controller = new AbortController();
@@ -35,8 +84,11 @@ export const withTimeoutAndRetry = async (operation, timeoutMs = 60000, maxRetri
35
84
  controller.abort();
36
85
  }, timeoutMs);
37
86
  try {
87
+ // Hand the operation the timeout signal so the in-flight upstream call is
88
+ // actually cancelled when the deadline fires — otherwise the abandoned
89
+ // attempt keeps running while the retry stacks a second concurrent call.
38
90
  const result = await Promise.race([
39
- operation(),
91
+ operation(controller.signal),
40
92
  new Promise((_, reject) => {
41
93
  controller.signal.addEventListener("abort", () => {
42
94
  reject(new Error(`Operation timed out after ${timeoutMs}ms`));
@@ -54,11 +106,11 @@ export const withTimeoutAndRetry = async (operation, timeoutMs = 60000, maxRetri
54
106
  return retry({
55
107
  operation: createTimeoutOperation(),
56
108
  maxRetries,
57
- delay: (attempt) => Math.min(1000 * Math.pow(2, attempt), 5000),
109
+ delay: defaultBackoff,
58
110
  onRetry: (attempt, error) => {
59
111
  const message = error instanceof Error ? error.message : String(error);
60
112
  logger.error(`[${operationName}] Failed attempt ${attempt + 1}:`, message);
61
- const delay = Math.min(1000 * Math.pow(2, attempt), 5000);
113
+ const delay = defaultBackoff(attempt);
62
114
  logger.debug(`[${operationName}] Retrying in ${delay}ms...`);
63
115
  logger.debug(`[${operationName}] Attempt ${attempt + 2}/${maxRetries + 1}`);
64
116
  },
@@ -68,28 +120,49 @@ export const withTimeoutAndRetry = async (operation, timeoutMs = 60000, maxRetri
68
120
  },
69
121
  });
70
122
  };
123
+ /** Reject if `next` hasn't settled within `timeoutMs`; always clears its timer. */
124
+ function raceFirstChunk(next, timeoutMs, operationName) {
125
+ let timer;
126
+ const timeout = new Promise((_, reject) => {
127
+ timer = setTimeout(() => reject(new Error(`[${operationName}] Stream timed out: no first chunk within ${timeoutMs}ms`)), timeoutMs);
128
+ });
129
+ return Promise.race([next, timeout]).finally(() => clearTimeout(timer));
130
+ }
71
131
  /**
72
132
  * Streaming analog of {@link withTimeoutAndRetry}. Re-runs an async-generator
73
- * factory as long as it fails *before yielding its first chunk* — e.g. an
74
- * empty completion that throws "No response", or an error while establishing
75
- * the stream. Once any chunk has been yielded the stream is committed and
76
- * further errors propagate, so a retry can never double-emit deltas that the
77
- * consumer has already received.
133
+ * factory as long as it fails *before yielding its first chunk* — an empty
134
+ * completion that throws "No response", an error while establishing the stream,
135
+ * or (with `firstChunkTimeoutMs`) a stall before the first token. Once any chunk
136
+ * has been yielded the stream is committed and further errors propagate, so a
137
+ * retry can never double-emit deltas the consumer has already received.
78
138
  *
79
139
  * This mirrors the non-streaming path, where the provider throws on an empty
80
140
  * completion inside `withTimeoutAndRetry` and is retried on the same model
81
141
  * before the caller falls through to backup models.
82
142
  */
83
143
  export async function* withStreamRetry(factory, options = {}) {
84
- const { maxRetries = 3, delay = (attempt) => Math.min(1000 * Math.pow(2, attempt), 5000), operationName = "AI stream", } = options;
144
+ const { maxRetries = 3, delay = defaultBackoff, operationName = "AI stream", firstChunkTimeoutMs, } = options;
85
145
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
146
+ // One controller per attempt: aborting it on abandon cancels the upstream
147
+ // SDK call so a retry can't stack a second concurrent stream.
148
+ const controller = new AbortController();
149
+ const iterator = factory(controller.signal);
86
150
  let yielded = false;
151
+ let completed = false;
87
152
  try {
88
- for await (const chunk of factory()) {
153
+ // Drive the iterator by hand so the first chunk can be raced against a
154
+ // deadline; for-await would give no hook for that.
155
+ for (;;) {
156
+ const result = !yielded && firstChunkTimeoutMs
157
+ ? await raceFirstChunk(iterator.next(), firstChunkTimeoutMs, operationName)
158
+ : await iterator.next();
159
+ if (result.done) {
160
+ completed = true;
161
+ return;
162
+ }
89
163
  yielded = true;
90
- yield chunk;
164
+ yield result.value;
91
165
  }
92
- return;
93
166
  }
94
167
  catch (error) {
95
168
  // Can't retry once deltas are out, and don't retry past the budget.
@@ -102,6 +175,19 @@ export async function* withStreamRetry(factory, options = {}) {
102
175
  logger.debug(`[${operationName}] Retrying in ${delayMs}ms... (attempt ${attempt + 2}/${maxRetries + 1})`);
103
176
  await new Promise((resolve) => setTimeout(resolve, delayMs));
104
177
  }
178
+ finally {
179
+ // Abandoning an attempt (first-chunk timeout, pre-yield error before a
180
+ // retry, or a consumer that breaks early): abort the upstream SDK call
181
+ // first so it's actually torn down — not left running while we retry —
182
+ // then dispose the iterator. Aborting also unblocks a stalled
183
+ // iterator.next() so return() can run the generator's cleanup. Both are
184
+ // fire-and-forget: a wedged generator's return() may never settle, so we
185
+ // must not await it.
186
+ if (!completed) {
187
+ controller.abort();
188
+ void iterator.return?.(undefined)?.catch(() => undefined);
189
+ }
190
+ }
105
191
  }
106
192
  }
107
193
  //# sourceMappingURL=retry.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"retry.js","sourceRoot":"","sources":["../../src/utils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the `message` field from a streamed structured
3
+ * JSON response.
4
+ *
5
+ * Providers that enforce a JSON schema stream the raw wrapper object
6
+ * (`{"message":"Hel` → `{"message":"Hello"}`), not the message text. Without
7
+ * this, every streaming consumer has to re-implement partial-JSON unwrapping to
8
+ * recover the user-facing tokens. These helpers do it once, at the framework
9
+ * boundary, so `delta`/`accumulated` carry clean message text and the parsed
10
+ * object is surfaced only when complete.
11
+ *
12
+ * The extractor targets the top-level `message` string field specifically
13
+ * (tracking object depth and string context, so a nested decoy `"message"` key
14
+ * or a `"message"` substring inside another value is never mistaken for it),
15
+ * and is tolerant of truncation at any byte — a dangling escape sequence is
16
+ * held back rather than emitted half-decoded. Input that is not a JSON object
17
+ * (e.g. a provider streaming plain text) is passed through verbatim.
18
+ */
19
+ /**
20
+ * Extract the decoded value of the top-level `message` string field from a
21
+ * (possibly partial) JSON object string, returning the text available so far.
22
+ *
23
+ * Returns `""` while the `message` value has not begun streaming, and passes
24
+ * `accumulated` through unchanged when it is not a JSON object.
25
+ */
26
+ export declare function extractMessageSoFar(accumulated: string): string;
27
+ /**
28
+ * Stateful wrapper over {@link extractMessageSoFar} for a single stream: feed
29
+ * each chunk's accumulated JSON and get back the clean message-so-far plus the
30
+ * newly revealed delta.
31
+ *
32
+ * Each push re-scans the full accumulated buffer (O(n) per chunk, O(n²) over a
33
+ * stream) — deliberately kept simple and stateless: at LLM response sizes (KBs)
34
+ * the cost is negligible, and it avoids carrying cross-chunk parser/escape state.
35
+ */
36
+ export declare class StreamingMessageDecoder {
37
+ private previous;
38
+ /**
39
+ * @param accumulated The provider chunk's full accumulated output so far.
40
+ * @returns `message` (clean text so far) and `delta` (the new text since the
41
+ * previous push).
42
+ */
43
+ push(accumulated: string): {
44
+ message: string;
45
+ delta: string;
46
+ };
47
+ }
48
+ //# sourceMappingURL=streamingMessage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"streamingMessage.d.ts","sourceRoot":"","sources":["../../src/utils/streamingMessage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AA6HH;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAyC/D;AAED;;;;;;;;GAQG;AACH,qBAAa,uBAAuB;IAClC,OAAO,CAAC,QAAQ,CAAM;IAEtB;;;;OAIG;IACH,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE;CAU9D"}
@@ -0,0 +1,205 @@
1
+ /**
2
+ * Incremental extraction of the `message` field from a streamed structured
3
+ * JSON response.
4
+ *
5
+ * Providers that enforce a JSON schema stream the raw wrapper object
6
+ * (`{"message":"Hel` → `{"message":"Hello"}`), not the message text. Without
7
+ * this, every streaming consumer has to re-implement partial-JSON unwrapping to
8
+ * recover the user-facing tokens. These helpers do it once, at the framework
9
+ * boundary, so `delta`/`accumulated` carry clean message text and the parsed
10
+ * object is surfaced only when complete.
11
+ *
12
+ * The extractor targets the top-level `message` string field specifically
13
+ * (tracking object depth and string context, so a nested decoy `"message"` key
14
+ * or a `"message"` substring inside another value is never mistaken for it),
15
+ * and is tolerant of truncation at any byte — a dangling escape sequence is
16
+ * held back rather than emitted half-decoded. Input that is not a JSON object
17
+ * (e.g. a provider streaming plain text) is passed through verbatim.
18
+ */
19
+ const WHITESPACE = " \t\n\r";
20
+ const ESCAPE_CHARS = {
21
+ '"': '"',
22
+ "\\": "\\",
23
+ "/": "/",
24
+ b: "\b",
25
+ f: "\f",
26
+ n: "\n",
27
+ r: "\r",
28
+ t: "\t",
29
+ };
30
+ /**
31
+ * Read a JSON string token whose opening quote is at `s[start]`. Decodes
32
+ * escapes. When the closing quote has not arrived, returns the decoded prefix
33
+ * with any trailing incomplete escape (`\` or a partial `\uXXXX`) held back, so
34
+ * a half-decoded character is never produced.
35
+ */
36
+ function readJsonString(s, start) {
37
+ let out = "";
38
+ const n = s.length;
39
+ let i = start + 1; // skip opening quote
40
+ while (i < n) {
41
+ const ch = s[i];
42
+ if (ch === '"') {
43
+ return { value: out, end: i + 1, complete: true };
44
+ }
45
+ if (ch === "\\") {
46
+ const esc = s[i + 1];
47
+ if (esc === undefined) {
48
+ // Dangling backslash — wait for the rest of the escape.
49
+ return { value: out, end: n, complete: false };
50
+ }
51
+ if (esc === "u") {
52
+ if (i + 6 > n) {
53
+ // Incomplete \uXXXX — hold it back.
54
+ return { value: out, end: n, complete: false };
55
+ }
56
+ const code = parseInt(s.slice(i + 2, i + 6), 16);
57
+ if (Number.isNaN(code)) {
58
+ return { value: out, end: n, complete: false };
59
+ }
60
+ out += String.fromCharCode(code);
61
+ i += 6;
62
+ }
63
+ else if (esc in ESCAPE_CHARS) {
64
+ out += ESCAPE_CHARS[esc];
65
+ i += 2;
66
+ }
67
+ else {
68
+ // Not a valid JSON escape; pass the character through leniently.
69
+ out += esc;
70
+ i += 2;
71
+ }
72
+ continue;
73
+ }
74
+ out += ch;
75
+ i++;
76
+ }
77
+ return { value: out, end: n, complete: false };
78
+ }
79
+ /**
80
+ * Skip one JSON value (string, object, array, or primitive) starting at `s[i]`.
81
+ * Tolerant of truncation: an unfinished value reports `complete: false`.
82
+ */
83
+ function skipJsonValue(s, i) {
84
+ const n = s.length;
85
+ if (i >= n)
86
+ return { end: n, complete: false };
87
+ const ch = s[i];
88
+ if (ch === '"') {
89
+ const r = readJsonString(s, i);
90
+ return { end: r.end, complete: r.complete };
91
+ }
92
+ if (ch === "{" || ch === "[") {
93
+ let depth = 0;
94
+ let j = i;
95
+ while (j < n) {
96
+ const c = s[j];
97
+ if (c === '"') {
98
+ const r = readJsonString(s, j);
99
+ if (!r.complete)
100
+ return { end: n, complete: false };
101
+ j = r.end;
102
+ continue;
103
+ }
104
+ if (c === "{" || c === "[")
105
+ depth++;
106
+ else if (c === "}" || c === "]") {
107
+ depth--;
108
+ if (depth === 0)
109
+ return { end: j + 1, complete: true };
110
+ }
111
+ j++;
112
+ }
113
+ return { end: n, complete: false };
114
+ }
115
+ // Primitive (number, true, false, null): runs until a structural delimiter.
116
+ // Hitting end-of-input first means it may still be streaming.
117
+ let j = i;
118
+ while (j < n && !`,}]${WHITESPACE}`.includes(s[j]))
119
+ j++;
120
+ return j < n ? { end: j, complete: true } : { end: n, complete: false };
121
+ }
122
+ /**
123
+ * Extract the decoded value of the top-level `message` string field from a
124
+ * (possibly partial) JSON object string, returning the text available so far.
125
+ *
126
+ * Returns `""` while the `message` value has not begun streaming, and passes
127
+ * `accumulated` through unchanged when it is not a JSON object.
128
+ */
129
+ export function extractMessageSoFar(accumulated) {
130
+ const s = accumulated;
131
+ const n = s.length;
132
+ let i = 0;
133
+ while (i < n && WHITESPACE.includes(s[i]))
134
+ i++;
135
+ // Not a JSON object — a plain-text stream; emit verbatim.
136
+ if (i >= n || s[i] !== "{")
137
+ return accumulated;
138
+ i++; // skip '{'
139
+ while (i < n) {
140
+ while (i < n && (WHITESPACE.includes(s[i]) || s[i] === ","))
141
+ i++;
142
+ if (i >= n)
143
+ return "";
144
+ if (s[i] === "}")
145
+ return ""; // object closed without a message
146
+ if (s[i] !== '"')
147
+ return ""; // key not (fully) arrived
148
+ const key = readJsonString(s, i);
149
+ if (!key.complete)
150
+ return ""; // key still streaming
151
+ i = key.end;
152
+ while (i < n && WHITESPACE.includes(s[i]))
153
+ i++;
154
+ if (i >= n || s[i] !== ":")
155
+ return ""; // colon not arrived
156
+ i++;
157
+ while (i < n && WHITESPACE.includes(s[i]))
158
+ i++;
159
+ if (i >= n)
160
+ return "";
161
+ if (key.value === "message") {
162
+ // Found it. Only a string value yields text; null/other → no message yet.
163
+ if (s[i] !== '"')
164
+ return "";
165
+ return readJsonString(s, i).value;
166
+ }
167
+ // A field before `message`: skip its value. If it is still streaming we
168
+ // cannot have reached `message` yet.
169
+ const skipped = skipJsonValue(s, i);
170
+ if (!skipped.complete)
171
+ return "";
172
+ i = skipped.end;
173
+ }
174
+ return "";
175
+ }
176
+ /**
177
+ * Stateful wrapper over {@link extractMessageSoFar} for a single stream: feed
178
+ * each chunk's accumulated JSON and get back the clean message-so-far plus the
179
+ * newly revealed delta.
180
+ *
181
+ * Each push re-scans the full accumulated buffer (O(n) per chunk, O(n²) over a
182
+ * stream) — deliberately kept simple and stateless: at LLM response sizes (KBs)
183
+ * the cost is negligible, and it avoids carrying cross-chunk parser/escape state.
184
+ */
185
+ export class StreamingMessageDecoder {
186
+ constructor() {
187
+ this.previous = "";
188
+ }
189
+ /**
190
+ * @param accumulated The provider chunk's full accumulated output so far.
191
+ * @returns `message` (clean text so far) and `delta` (the new text since the
192
+ * previous push).
193
+ */
194
+ push(accumulated) {
195
+ const message = extractMessageSoFar(accumulated);
196
+ // Decoding is monotonic (each push extends the prefix); the guard is a
197
+ // belt-and-braces reset for any non-prefix anomaly.
198
+ const delta = message.startsWith(this.previous)
199
+ ? message.slice(this.previous.length)
200
+ : message;
201
+ this.previous = message;
202
+ return { message, delta };
203
+ }
204
+ }
205
+ //# sourceMappingURL=streamingMessage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"streamingMessage.js","sourceRoot":"","sources":["../../src/utils/streamingMessage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,MAAM,UAAU,GAAG,SAAS,CAAC;AAE7B,MAAM,YAAY,GAA2B;IAC3C,GAAG,EAAE,GAAG;IACR,IAAI,EAAE,IAAI;IACV,GAAG,EAAE,GAAG;IACR,CAAC,EAAE,IAAI;IACP,CAAC,EAAE,IAAI;IACP,CAAC,EAAE,IAAI;IACP,CAAC,EAAE,IAAI;IACP,CAAC,EAAE,IAAI;CACR,CAAC;AAWF;;;;;GAKG;AACH,SAAS,cAAc,CAAC,CAAS,EAAE,KAAa;IAC9C,IAAI,GAAG,GAAG,EAAE,CAAC;IACb,MAAM,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC;IACnB,IAAI,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,qBAAqB;IAExC,OAA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,SAAS,EAAE,CAAC;YAC5B,0EAA0E;YAC1E,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG;gBAAE,OAAO,EAAE,CAAC;YAC5B,OAAO,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;QACpC,CAAC;QAED,wEAAwE;QACxE,qCAAqC;QACrC,MAAM,OAAO,GAAG,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QACpC,IAAI,CAAC,OAAO,CAAC,QAAQ;YAAE,OAAO,EAAE,CAAC;QACjC,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC;IAClB,CAAC;IAED,OAAO,EAAE,CAAC;AACZ,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,OAAO,uBAAuB;IAApC;QACU,aAAQ,GAAG,EAAE,CAAC;IAiBxB,CAAC;IAfC;;;;OAIG;IACH,IAAI,CAAC,WAAmB;QACtB,MAAM,OAAO,GAAG,mBAAmB,CAAC,WAAW,CAAC,CAAC;QACjD,uEAAuE;QACvE,oDAAoD;QACpD,MAAM,KAAK,GAAG,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC;YAC7C,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC;YACrC,CAAC,CAAC,OAAO,CAAC;QACZ,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IAC5B,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@falai/agent",
3
- "version": "2.4.3",
3
+ "version": "2.6.0",
4
4
  "description": "Conversational state engine for TypeScript where the AI understands, but the code is in control",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",
@@ -366,20 +366,6 @@ export class FlowRouter<TContext = unknown, TData = unknown> {
366
366
  }
367
367
  }
368
368
 
369
- // No candidates means flow has no valid next steps (edge case)
370
- if (candidates.length === 0) {
371
- logger.debug(
372
- `[FlowRouter] Single-flow: No valid candidate steps found`
373
- );
374
- return {
375
- selectedFlow,
376
- selectedStep: undefined,
377
- session: updatedSession,
378
- isFlowComplete: false,
379
- completedFlows,
380
- };
381
- }
382
-
383
369
  // Multiple candidates - use AI to select best step
384
370
  const lastUserMessage = getLastMessageFromHistory(history);
385
371