@falai/agent 0.9.0-alpha-2 → 0.9.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 (75) hide show
  1. package/dist/cjs/src/core/Agent.d.ts +31 -41
  2. package/dist/cjs/src/core/Agent.d.ts.map +1 -1
  3. package/dist/cjs/src/core/Agent.js +72 -1075
  4. package/dist/cjs/src/core/Agent.js.map +1 -1
  5. package/dist/cjs/src/core/ResponseModal.d.ts +205 -0
  6. package/dist/cjs/src/core/ResponseModal.d.ts.map +1 -0
  7. package/dist/cjs/src/core/ResponseModal.js +1328 -0
  8. package/dist/cjs/src/core/ResponseModal.js.map +1 -0
  9. package/dist/cjs/src/core/ResponsePipeline.js +1 -1
  10. package/dist/cjs/src/core/ResponsePipeline.js.map +1 -1
  11. package/dist/cjs/src/index.d.ts +3 -1
  12. package/dist/cjs/src/index.d.ts.map +1 -1
  13. package/dist/cjs/src/index.js +7 -1
  14. package/dist/cjs/src/index.js.map +1 -1
  15. package/dist/cjs/src/types/agent.d.ts +1 -0
  16. package/dist/cjs/src/types/agent.d.ts.map +1 -1
  17. package/dist/cjs/src/types/ai.d.ts +1 -1
  18. package/dist/cjs/src/types/ai.d.ts.map +1 -1
  19. package/dist/cjs/src/utils/clone.d.ts.map +1 -1
  20. package/dist/cjs/src/utils/clone.js +0 -4
  21. package/dist/cjs/src/utils/clone.js.map +1 -1
  22. package/dist/cjs/src/utils/history.d.ts +30 -1
  23. package/dist/cjs/src/utils/history.d.ts.map +1 -1
  24. package/dist/cjs/src/utils/history.js +169 -23
  25. package/dist/cjs/src/utils/history.js.map +1 -1
  26. package/dist/cjs/src/utils/index.d.ts +1 -1
  27. package/dist/cjs/src/utils/index.d.ts.map +1 -1
  28. package/dist/cjs/src/utils/index.js +5 -1
  29. package/dist/cjs/src/utils/index.js.map +1 -1
  30. package/dist/src/core/Agent.d.ts +31 -41
  31. package/dist/src/core/Agent.d.ts.map +1 -1
  32. package/dist/src/core/Agent.js +73 -1076
  33. package/dist/src/core/Agent.js.map +1 -1
  34. package/dist/src/core/ResponseModal.d.ts +205 -0
  35. package/dist/src/core/ResponseModal.d.ts.map +1 -0
  36. package/dist/src/core/ResponseModal.js +1323 -0
  37. package/dist/src/core/ResponseModal.js.map +1 -0
  38. package/dist/src/core/ResponsePipeline.js +1 -1
  39. package/dist/src/core/ResponsePipeline.js.map +1 -1
  40. package/dist/src/index.d.ts +3 -1
  41. package/dist/src/index.d.ts.map +1 -1
  42. package/dist/src/index.js +2 -1
  43. package/dist/src/index.js.map +1 -1
  44. package/dist/src/types/agent.d.ts +1 -0
  45. package/dist/src/types/agent.d.ts.map +1 -1
  46. package/dist/src/types/ai.d.ts +1 -1
  47. package/dist/src/types/ai.d.ts.map +1 -1
  48. package/dist/src/utils/clone.d.ts.map +1 -1
  49. package/dist/src/utils/clone.js +0 -4
  50. package/dist/src/utils/clone.js.map +1 -1
  51. package/dist/src/utils/history.d.ts +30 -1
  52. package/dist/src/utils/history.d.ts.map +1 -1
  53. package/dist/src/utils/history.js +165 -23
  54. package/dist/src/utils/history.js.map +1 -1
  55. package/dist/src/utils/index.d.ts +1 -1
  56. package/dist/src/utils/index.d.ts.map +1 -1
  57. package/dist/src/utils/index.js +1 -1
  58. package/dist/src/utils/index.js.map +1 -1
  59. package/docs/README.md +2 -1
  60. package/docs/api/README.md +160 -0
  61. package/docs/api/overview.md +66 -1
  62. package/docs/guides/migration/README.md +72 -0
  63. package/docs/guides/migration/response-modal-refactor.md +518 -0
  64. package/examples/advanced-patterns/streaming-responses.ts +169 -96
  65. package/examples/core-concepts/modern-streaming-api.ts +309 -0
  66. package/package.json +1 -1
  67. package/src/core/Agent.ts +95 -1488
  68. package/src/core/ResponseModal.ts +1722 -0
  69. package/src/core/ResponsePipeline.ts +1 -1
  70. package/src/index.ts +11 -0
  71. package/src/types/agent.ts +1 -0
  72. package/src/types/ai.ts +1 -1
  73. package/src/utils/clone.ts +6 -8
  74. package/src/utils/history.ts +190 -27
  75. package/src/utils/index.ts +4 -0
@@ -1,9 +1,12 @@
1
1
  /**
2
2
  * Example: Streaming Responses
3
- * Updated for v2 architecture with session step management
3
+ * Updated for ResponseModal architecture with modern streaming APIs
4
4
  *
5
- * This example demonstrates how to use the respondStream method
6
- * to stream AI responses in real-time for better user experience
5
+ * This example demonstrates both the new modern stream() API and the legacy
6
+ * respondStream() method for streaming AI responses in real-time.
7
+ *
8
+ * NEW: The modern stream() API provides automatic session management and
9
+ * a simpler interface similar to chat() but with streaming.
7
10
  */
8
11
 
9
12
  import {
@@ -22,8 +25,8 @@ interface ConversationContext {
22
25
  };
23
26
  }
24
27
 
25
- async function streamingWithAnthropic() {
26
- console.log("\nšŸ¤– Example 1: Streaming with Anthropic (Claude)\n");
28
+ async function modernStreamingWithAnthropic() {
29
+ console.log("\nšŸ¤– Example 1: Modern Streaming API with Anthropic (Claude)\n");
27
30
 
28
31
  // Initialize Anthropic provider
29
32
  const provider = new AnthropicProvider({
@@ -56,34 +59,91 @@ async function streamingWithAnthropic() {
56
59
  enabled: true,
57
60
  });
58
61
 
59
- // Create conversation history
60
- const history = [
61
- {
62
- role: "user" as const,
63
- content: "Explain quantum computing in simple terms.",
64
- name: "User",
62
+ try {
63
+ console.log("šŸ“¤ Modern streaming API - automatic session management...\n");
64
+ console.log("Response: ");
65
+
66
+ // Session is automatically managed by the agent
67
+ console.log("✨ Session ready:", agent.session.id);
68
+
69
+ // NEW: Modern stream() API - automatically manages session history
70
+ for await (const chunk of agent.stream("Explain quantum computing in simple terms.")) {
71
+ // chunk.delta contains the new text
72
+ // chunk.accumulated contains the full text so far
73
+ // chunk.done indicates if this is the final chunk
74
+
75
+ if (chunk.delta) {
76
+ process.stdout.write(chunk.delta);
77
+ }
78
+
79
+ if (chunk.done) {
80
+ console.log("\n\nāœ… Stream complete!");
81
+ console.log(`\nšŸ“Š Metadata:`);
82
+ console.log(
83
+ ` - Route: ${chunk.session?.currentRoute?.title || "None"}`
84
+ );
85
+ console.log(` - Data:`, agent.session.getData() || "None");
86
+ console.log(` - Tool Calls: ${chunk.toolCalls?.length || 0}`);
87
+
88
+ // Session history is automatically updated - no manual management needed!
89
+ console.log(` - Session Messages: ${agent.session.getHistory().length}`);
90
+ }
91
+ }
92
+
93
+ console.log("\nšŸ’” Benefits of modern stream() API:");
94
+ console.log(" - Automatic session management");
95
+ console.log(" - Simple interface: agent.stream('message')");
96
+ console.log(" - No need to manually manage history");
97
+ console.log(" - Same performance as respondStream()");
98
+
99
+ } catch (error) {
100
+ console.error("āŒ Error:", error);
101
+ }
102
+ }
103
+
104
+ async function legacyStreamingWithAnthropic() {
105
+ console.log("\nšŸ¤– Example 2: Legacy Streaming API (respondStream) - Still Supported\n");
106
+
107
+ // Initialize Anthropic provider
108
+ const provider = new AnthropicProvider({
109
+ apiKey: process.env.ANTHROPIC_API_KEY || "",
110
+ model: "claude-sonnet-4-5",
111
+ config: {
112
+ temperature: 0.7,
113
+ max_tokens: 1000,
65
114
  },
66
- ];
115
+ });
116
+
117
+ // Create agent
118
+ const agent = new Agent<ConversationContext, unknown>({
119
+ name: "LegacyStreamingAssistant",
120
+ description: "An AI assistant using legacy streaming API",
121
+ goal: "Demonstrate backward compatibility",
122
+ context: {
123
+ userId: "user123",
124
+ sessionId: "session456-legacy",
125
+ preferences: {
126
+ streamingEnabled: true,
127
+ },
128
+ },
129
+ provider: provider,
130
+ });
67
131
 
68
132
  try {
69
- console.log("šŸ“¤ Streaming response from Claude...\n");
133
+ console.log("šŸ“¤ Legacy respondStream API - manual session management...\n");
70
134
  console.log("Response: ");
71
135
 
72
136
  // Session is automatically managed by the agent
73
137
  console.log("✨ Session ready:", agent.session.id);
74
138
 
75
- // Add user message to session history
139
+ // Add user message to session history manually
76
140
  await agent.session.addMessage("user", "What's the weather like today?");
77
141
 
78
- // Use respondStream for real-time streaming with session history
142
+ // Legacy respondStream API - requires manual session management
79
143
  let fullMessage = "";
80
144
  for await (const chunk of agent.respondStream({
81
145
  history: agent.session.getHistory()
82
146
  })) {
83
- // chunk.delta contains the new text
84
- // chunk.accumulated contains the full text so far
85
- // chunk.done indicates if this is the final chunk
86
-
87
147
  if (chunk.delta) {
88
148
  process.stdout.write(chunk.delta);
89
149
  fullMessage += chunk.delta;
@@ -97,24 +157,30 @@ async function streamingWithAnthropic() {
97
157
  );
98
158
  console.log(` - Data:`, agent.session.getData() || "None");
99
159
  console.log(` - Tool Calls: ${chunk.toolCalls?.length || 0}`);
100
-
101
- console.log(` - Full Message: ${fullMessage}`);
102
160
 
103
- // Add assistant response to session history
161
+ // Manual session history management required
104
162
  await agent.session.addMessage("assistant", fullMessage);
163
+ console.log(` - Session Messages: ${agent.session.getHistory().length}`);
105
164
  }
106
165
  }
166
+
167
+ console.log("\nšŸ’” Legacy respondStream() API characteristics:");
168
+ console.log(" - Manual session management required");
169
+ console.log(" - More complex parameter structure");
170
+ console.log(" - Full backward compatibility maintained");
171
+ console.log(" - Still fully supported for existing code");
172
+
107
173
  } catch (error) {
108
174
  console.error("āŒ Error:", error);
109
175
  }
110
176
  }
111
177
 
112
- async function streamingWithOpenAI() {
113
- console.log("\nšŸ¤– Example 2: Streaming with OpenAI\n");
178
+ async function modernStreamingWithOpenAI() {
179
+ console.log("\nšŸ¤– Example 3: Modern Streaming with OpenAI\n");
114
180
 
115
181
  const provider = new OpenAIProvider({
116
182
  apiKey: process.env.OPENAI_API_KEY || "",
117
- model: "gpt-5",
183
+ model: "gpt-4",
118
184
  config: {
119
185
  temperature: 0.8,
120
186
  },
@@ -133,26 +199,16 @@ async function streamingWithOpenAI() {
133
199
  provider: provider,
134
200
  });
135
201
 
136
- const history = [
137
- {
138
- role: "user" as const,
139
- content: "Write a short poem about TypeScript",
140
- name: "User",
141
- },
142
- ];
143
-
144
202
  try {
145
- console.log("šŸ“¤ Streaming response from OpenAI...\n");
203
+ console.log("šŸ“¤ Modern streaming with OpenAI...\n");
146
204
  console.log("Response: ");
147
205
 
148
206
  // Session is automatically managed by the agent
149
207
  console.log("✨ Session ready:", agent.session.id);
150
208
 
151
- // Add user message to session history
152
- await agent.session.addMessage("user", "What's the weather like today?");
153
-
154
- for await (const chunk of agent.respondStream({
155
- history: agent.session.getHistory()
209
+ // NEW: Modern stream() API with context override
210
+ for await (const chunk of agent.stream("Write a short poem about TypeScript", {
211
+ contextOverride: { preferences: { streamingEnabled: true } }
156
212
  })) {
157
213
  if (chunk.delta) {
158
214
  process.stdout.write(chunk.delta);
@@ -164,9 +220,9 @@ async function streamingWithOpenAI() {
164
220
  ` - Route: ${chunk.session?.currentRoute?.title || "None"}`
165
221
  );
166
222
  console.log(` - Data:`, agent.session.getData() || "None");
167
-
168
- // Add assistant response to session history
169
- await agent.session.addMessage("assistant", chunk.accumulated || "");
223
+
224
+ // Session automatically updated - no manual work needed!
225
+ console.log(` - Session Messages: ${agent.session.getHistory().length}`);
170
226
  }
171
227
  }
172
228
  } catch (error) {
@@ -174,8 +230,8 @@ async function streamingWithOpenAI() {
174
230
  }
175
231
  }
176
232
 
177
- async function streamingWithGemini() {
178
- console.log("\nšŸ¤– Example 3: Streaming with Google Gemini\n");
233
+ async function modernStreamingComparison() {
234
+ console.log("\nšŸ¤– Example 4: Side-by-Side API Comparison\n");
179
235
 
180
236
  const provider = new GeminiProvider({
181
237
  apiKey: process.env.GEMINI_API_KEY || "",
@@ -186,8 +242,8 @@ async function streamingWithGemini() {
186
242
  });
187
243
 
188
244
  const agent = new Agent<ConversationContext, unknown>({
189
- name: "AnalyticalAssistant",
190
- description: "An analytical AI assistant",
245
+ name: "ComparisonAssistant",
246
+ description: "Demonstrates API differences",
191
247
  context: {
192
248
  userId: "user123",
193
249
  sessionId: "session101",
@@ -198,49 +254,73 @@ async function streamingWithGemini() {
198
254
  provider: provider,
199
255
  });
200
256
 
201
- const history = [
202
- {
203
- role: "user" as const,
204
- content: "What are the key differences between REST and GraphQL?",
205
- name: "User",
206
- },
207
- ];
257
+ const userMessage = "What are the key differences between REST and GraphQL?";
208
258
 
209
259
  try {
210
- console.log("šŸ“¤ Streaming response from Gemini...\n");
211
- console.log("Response: ");
260
+ console.log("šŸ“¤ Comparing old vs new streaming APIs...\n");
212
261
 
213
- // Session is automatically managed by the agent
214
- console.log("✨ Session ready:", agent.session.id);
215
-
216
- // Add user message to session history
217
- await agent.session.addMessage("user", "What are the key differences between REST and GraphQL?");
262
+ // ========================================================================
263
+ // OLD WAY: respondStream() - Manual session management
264
+ // ========================================================================
265
+ console.log("šŸ”ø OLD WAY: respondStream() with manual session management");
266
+ console.log("Response: ");
218
267
 
268
+ // Manual session management
269
+ await agent.session.addMessage("user", userMessage);
270
+
271
+ let oldWayMessage = "";
219
272
  for await (const chunk of agent.respondStream({
220
273
  history: agent.session.getHistory()
221
274
  })) {
222
275
  if (chunk.delta) {
223
276
  process.stdout.write(chunk.delta);
277
+ oldWayMessage += chunk.delta;
224
278
  }
225
279
 
226
280
  if (chunk.done) {
227
- console.log("\n\nāœ… Stream complete!");
228
- console.log(
229
- ` - Route: ${chunk.session?.currentRoute?.title || "None"}`
230
- );
231
- console.log(` - Data:`, agent.session.getData() || "None");
281
+ // Manual history update required
282
+ await agent.session.addMessage("assistant", oldWayMessage);
283
+ console.log("\n āœ… Manual session update completed");
284
+ }
285
+ }
286
+
287
+ console.log("\n" + "=".repeat(60));
288
+
289
+ // ========================================================================
290
+ // NEW WAY: stream() - Automatic session management
291
+ // ========================================================================
292
+ console.log("šŸ”ø NEW WAY: stream() with automatic session management");
293
+ console.log("Response: ");
232
294
 
233
- // Add assistant response to session history
234
- await agent.session.addMessage("assistant", chunk.accumulated || "");
295
+ // Automatic session management - just pass the message!
296
+ for await (const chunk of agent.stream("Can you explain that in more detail?")) {
297
+ if (chunk.delta) {
298
+ process.stdout.write(chunk.delta);
299
+ }
300
+
301
+ if (chunk.done) {
302
+ console.log("\n āœ… Automatic session update - no manual work needed!");
303
+ console.log(` šŸ“Š Total messages in session: ${agent.session.getHistory().length}`);
235
304
  }
236
305
  }
306
+
307
+ console.log("\nšŸ’” Key Differences:");
308
+ console.log(" OLD: agent.respondStream({ history: agent.session.getHistory() })");
309
+ console.log(" NEW: agent.stream('message')");
310
+ console.log(" ");
311
+ console.log(" OLD: Manual session.addMessage() calls required");
312
+ console.log(" NEW: Automatic session management");
313
+ console.log(" ");
314
+ console.log(" OLD: Complex parameter structure");
315
+ console.log(" NEW: Simple message + optional options");
316
+
237
317
  } catch (error) {
238
318
  console.error("āŒ Error:", error);
239
319
  }
240
320
  }
241
321
 
242
- async function streamingWithRoutes() {
243
- console.log("\nšŸ¤– Example 4: Streaming with Routes and Steps\n");
322
+ async function modernStreamingWithRoutes() {
323
+ console.log("\nšŸ¤– Example 5: Modern Streaming with Routes and Steps\n");
244
324
 
245
325
  const provider = new AnthropicProvider({
246
326
  apiKey: process.env.ANTHROPIC_API_KEY || "",
@@ -327,12 +407,8 @@ async function streamingWithRoutes() {
327
407
  // Session is automatically managed by the agent
328
408
  console.log("✨ Session ready:", agent.session.id);
329
409
 
330
- // Add user message to session history
331
- await agent.session.addMessage("user", "How do I reset my password?");
332
-
333
- for await (const chunk of agent.respondStream({
334
- history: agent.session.getHistory()
335
- })) {
410
+ // NEW: Modern stream() API with routes - automatic session management
411
+ for await (const chunk of agent.stream("How do I reset my password?")) {
336
412
  if (chunk.delta) {
337
413
  process.stdout.write(chunk.delta);
338
414
  }
@@ -353,7 +429,7 @@ async function streamingWithRoutes() {
353
429
  }
354
430
  }
355
431
 
356
- // Session is automatically updated by the agent
432
+ // Session is automatically updated by the modern stream() API
357
433
  }
358
434
  }
359
435
  } catch (error) {
@@ -361,8 +437,8 @@ async function streamingWithRoutes() {
361
437
  }
362
438
  }
363
439
 
364
- async function streamingWithAbortSignal() {
365
- console.log("\nšŸ¤– Example 5: Streaming with Abort Control\n");
440
+ async function modernStreamingWithAbortSignal() {
441
+ console.log("\nšŸ¤– Example 6: Modern Streaming with Abort Control\n");
366
442
 
367
443
  const provider = new AnthropicProvider({
368
444
  apiKey: process.env.ANTHROPIC_API_KEY || "",
@@ -406,11 +482,8 @@ async function streamingWithAbortSignal() {
406
482
  // Session is automatically managed by the agent
407
483
  console.log("✨ Session ready:", agent.session.id);
408
484
 
409
- // Add user message to session history
410
- await agent.session.addMessage("user", "Tell me a very long story about space exploration.");
411
-
412
- for await (const chunk of agent.respondStream({
413
- history: agent.session.getHistory(),
485
+ // NEW: Modern stream() API with abort signal
486
+ for await (const chunk of agent.stream("Tell me a very long story about space exploration.", {
414
487
  signal: abortController.signal,
415
488
  })) {
416
489
  if (chunk.delta) {
@@ -425,10 +498,7 @@ async function streamingWithAbortSignal() {
425
498
  );
426
499
  console.log(` - Data:`, agent.session.getData() || "None");
427
500
 
428
- // Add assistant response to session history
429
- await agent.session.addMessage("assistant", chunk.accumulated || "");
430
-
431
- // Session is automatically updated by the agent
501
+ // Session is automatically updated by the modern stream() API - no manual work needed!
432
502
 
433
503
  clearTimeout(timeout);
434
504
  }
@@ -463,11 +533,12 @@ async function main() {
463
533
  console.log("=".repeat(60));
464
534
 
465
535
  const examples = [
466
- { name: "Anthropic Streaming", fn: streamingWithAnthropic },
467
- { name: "OpenAI Streaming", fn: streamingWithOpenAI },
468
- { name: "Gemini Streaming", fn: streamingWithGemini },
469
- { name: "Streaming with Routes", fn: streamingWithRoutes },
470
- { name: "Streaming with Abort", fn: streamingWithAbortSignal },
536
+ { name: "Modern Streaming API (Anthropic)", fn: modernStreamingWithAnthropic },
537
+ { name: "Legacy Streaming API (Anthropic)", fn: legacyStreamingWithAnthropic },
538
+ { name: "Modern Streaming (OpenAI)", fn: modernStreamingWithOpenAI },
539
+ { name: "API Comparison (Gemini)", fn: modernStreamingComparison },
540
+ { name: "Modern Streaming with Routes", fn: modernStreamingWithRoutes },
541
+ { name: "Modern Streaming with Abort", fn: modernStreamingWithAbortSignal },
471
542
  ];
472
543
 
473
544
  console.log("\nAvailable Examples:");
@@ -477,19 +548,21 @@ async function main() {
477
548
 
478
549
  console.log("\nšŸ’” Tips:");
479
550
  console.log(" - Set ANTHROPIC_API_KEY, OPENAI_API_KEY, or GEMINI_API_KEY");
551
+ console.log(" - NEW: Use agent.stream('message') for automatic session management");
552
+ console.log(" - OLD: agent.respondStream() still supported for backward compatibility");
480
553
  console.log(" - Streaming provides real-time responses for better UX");
481
554
  console.log(" - Use AbortSignal to cancel long-running streams");
482
555
  console.log(" - Access chunk.route and chunk.step for flow information");
483
556
 
484
557
  console.log("\n" + "=".repeat(60));
485
558
 
486
- // Run first example if API key is available
559
+ // Run modern streaming example if API key is available
487
560
  if (process.env.ANTHROPIC_API_KEY) {
488
- await streamingWithAnthropic();
561
+ await modernStreamingWithAnthropic();
489
562
  } else if (process.env.OPENAI_API_KEY) {
490
- await streamingWithOpenAI();
563
+ await modernStreamingWithOpenAI();
491
564
  } else if (process.env.GEMINI_API_KEY) {
492
- await streamingWithGemini();
565
+ await modernStreamingComparison();
493
566
  } else {
494
567
  console.log(
495
568
  "\nāš ļø No API keys found. Set one of the environment variables to run examples."