@falai/agent 1.1.3 → 1.2.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.
Files changed (193) hide show
  1. package/README.md +9 -0
  2. package/dist/cjs/core/Agent.d.ts +17 -1
  3. package/dist/cjs/core/Agent.d.ts.map +1 -1
  4. package/dist/cjs/core/Agent.js +47 -0
  5. package/dist/cjs/core/Agent.js.map +1 -1
  6. package/dist/cjs/core/BatchPromptBuilder.d.ts +3 -0
  7. package/dist/cjs/core/BatchPromptBuilder.d.ts.map +1 -1
  8. package/dist/cjs/core/BatchPromptBuilder.js +4 -1
  9. package/dist/cjs/core/BatchPromptBuilder.js.map +1 -1
  10. package/dist/cjs/core/CompactionEngine.d.ts +65 -0
  11. package/dist/cjs/core/CompactionEngine.d.ts.map +1 -0
  12. package/dist/cjs/core/CompactionEngine.js +251 -0
  13. package/dist/cjs/core/CompactionEngine.js.map +1 -0
  14. package/dist/cjs/core/PromptComposer.d.ts +8 -1
  15. package/dist/cjs/core/PromptComposer.d.ts.map +1 -1
  16. package/dist/cjs/core/PromptComposer.js +238 -126
  17. package/dist/cjs/core/PromptComposer.js.map +1 -1
  18. package/dist/cjs/core/PromptSectionCache.d.ts +57 -0
  19. package/dist/cjs/core/PromptSectionCache.d.ts.map +1 -0
  20. package/dist/cjs/core/PromptSectionCache.js +108 -0
  21. package/dist/cjs/core/PromptSectionCache.js.map +1 -0
  22. package/dist/cjs/core/ResponseEngine.d.ts +3 -2
  23. package/dist/cjs/core/ResponseEngine.d.ts.map +1 -1
  24. package/dist/cjs/core/ResponseEngine.js +8 -8
  25. package/dist/cjs/core/ResponseEngine.js.map +1 -1
  26. package/dist/cjs/core/ResponseModal.d.ts.map +1 -1
  27. package/dist/cjs/core/ResponseModal.js +120 -70
  28. package/dist/cjs/core/ResponseModal.js.map +1 -1
  29. package/dist/cjs/core/ResponsePipeline.d.ts +2 -1
  30. package/dist/cjs/core/ResponsePipeline.d.ts.map +1 -1
  31. package/dist/cjs/core/ResponsePipeline.js +17 -19
  32. package/dist/cjs/core/ResponsePipeline.js.map +1 -1
  33. package/dist/cjs/core/RoutingEngine.d.ts +10 -0
  34. package/dist/cjs/core/RoutingEngine.d.ts.map +1 -1
  35. package/dist/cjs/core/RoutingEngine.js +5 -4
  36. package/dist/cjs/core/RoutingEngine.js.map +1 -1
  37. package/dist/cjs/core/SessionManager.d.ts.map +1 -1
  38. package/dist/cjs/core/SessionManager.js +20 -0
  39. package/dist/cjs/core/SessionManager.js.map +1 -1
  40. package/dist/cjs/core/StreamingToolExecutor.d.ts +142 -0
  41. package/dist/cjs/core/StreamingToolExecutor.d.ts.map +1 -0
  42. package/dist/cjs/core/StreamingToolExecutor.js +455 -0
  43. package/dist/cjs/core/StreamingToolExecutor.js.map +1 -0
  44. package/dist/cjs/core/ToolManager.d.ts +18 -1
  45. package/dist/cjs/core/ToolManager.d.ts.map +1 -1
  46. package/dist/cjs/core/ToolManager.js +91 -0
  47. package/dist/cjs/core/ToolManager.js.map +1 -1
  48. package/dist/cjs/index.d.ts +5 -1
  49. package/dist/cjs/index.d.ts.map +1 -1
  50. package/dist/cjs/index.js +8 -2
  51. package/dist/cjs/index.js.map +1 -1
  52. package/dist/cjs/providers/AnthropicProvider.d.ts +7 -0
  53. package/dist/cjs/providers/AnthropicProvider.d.ts.map +1 -1
  54. package/dist/cjs/providers/AnthropicProvider.js +109 -19
  55. package/dist/cjs/providers/AnthropicProvider.js.map +1 -1
  56. package/dist/cjs/providers/GeminiProvider.d.ts +32 -0
  57. package/dist/cjs/providers/GeminiProvider.d.ts.map +1 -1
  58. package/dist/cjs/providers/GeminiProvider.js +160 -53
  59. package/dist/cjs/providers/GeminiProvider.js.map +1 -1
  60. package/dist/cjs/providers/OpenAIProvider.d.ts +5 -0
  61. package/dist/cjs/providers/OpenAIProvider.d.ts.map +1 -1
  62. package/dist/cjs/providers/OpenAIProvider.js +65 -18
  63. package/dist/cjs/providers/OpenAIProvider.js.map +1 -1
  64. package/dist/cjs/providers/OpenRouterProvider.d.ts +5 -0
  65. package/dist/cjs/providers/OpenRouterProvider.d.ts.map +1 -1
  66. package/dist/cjs/providers/OpenRouterProvider.js +57 -18
  67. package/dist/cjs/providers/OpenRouterProvider.js.map +1 -1
  68. package/dist/cjs/types/agent.d.ts +44 -0
  69. package/dist/cjs/types/agent.d.ts.map +1 -1
  70. package/dist/cjs/types/agent.js.map +1 -1
  71. package/dist/cjs/types/ai.d.ts +2 -2
  72. package/dist/cjs/types/ai.d.ts.map +1 -1
  73. package/dist/cjs/types/compaction.d.ts +50 -0
  74. package/dist/cjs/types/compaction.d.ts.map +1 -0
  75. package/dist/cjs/types/compaction.js +6 -0
  76. package/dist/cjs/types/compaction.js.map +1 -0
  77. package/dist/cjs/types/index.d.ts +4 -2
  78. package/dist/cjs/types/index.d.ts.map +1 -1
  79. package/dist/cjs/types/index.js.map +1 -1
  80. package/dist/cjs/types/tool.d.ts +84 -0
  81. package/dist/cjs/types/tool.d.ts.map +1 -1
  82. package/dist/core/Agent.d.ts +17 -1
  83. package/dist/core/Agent.d.ts.map +1 -1
  84. package/dist/core/Agent.js +47 -0
  85. package/dist/core/Agent.js.map +1 -1
  86. package/dist/core/BatchPromptBuilder.d.ts +3 -0
  87. package/dist/core/BatchPromptBuilder.d.ts.map +1 -1
  88. package/dist/core/BatchPromptBuilder.js +4 -1
  89. package/dist/core/BatchPromptBuilder.js.map +1 -1
  90. package/dist/core/CompactionEngine.d.ts +65 -0
  91. package/dist/core/CompactionEngine.d.ts.map +1 -0
  92. package/dist/core/CompactionEngine.js +244 -0
  93. package/dist/core/CompactionEngine.js.map +1 -0
  94. package/dist/core/PromptComposer.d.ts +8 -1
  95. package/dist/core/PromptComposer.d.ts.map +1 -1
  96. package/dist/core/PromptComposer.js +238 -126
  97. package/dist/core/PromptComposer.js.map +1 -1
  98. package/dist/core/PromptSectionCache.d.ts +57 -0
  99. package/dist/core/PromptSectionCache.d.ts.map +1 -0
  100. package/dist/core/PromptSectionCache.js +104 -0
  101. package/dist/core/PromptSectionCache.js.map +1 -0
  102. package/dist/core/ResponseEngine.d.ts +3 -2
  103. package/dist/core/ResponseEngine.d.ts.map +1 -1
  104. package/dist/core/ResponseEngine.js +8 -8
  105. package/dist/core/ResponseEngine.js.map +1 -1
  106. package/dist/core/ResponseModal.d.ts.map +1 -1
  107. package/dist/core/ResponseModal.js +121 -71
  108. package/dist/core/ResponseModal.js.map +1 -1
  109. package/dist/core/ResponsePipeline.d.ts +2 -1
  110. package/dist/core/ResponsePipeline.d.ts.map +1 -1
  111. package/dist/core/ResponsePipeline.js +18 -20
  112. package/dist/core/ResponsePipeline.js.map +1 -1
  113. package/dist/core/RoutingEngine.d.ts +10 -0
  114. package/dist/core/RoutingEngine.d.ts.map +1 -1
  115. package/dist/core/RoutingEngine.js +6 -5
  116. package/dist/core/RoutingEngine.js.map +1 -1
  117. package/dist/core/SessionManager.d.ts.map +1 -1
  118. package/dist/core/SessionManager.js +17 -0
  119. package/dist/core/SessionManager.js.map +1 -1
  120. package/dist/core/StreamingToolExecutor.d.ts +142 -0
  121. package/dist/core/StreamingToolExecutor.d.ts.map +1 -0
  122. package/dist/core/StreamingToolExecutor.js +448 -0
  123. package/dist/core/StreamingToolExecutor.js.map +1 -0
  124. package/dist/core/ToolManager.d.ts +18 -1
  125. package/dist/core/ToolManager.d.ts.map +1 -1
  126. package/dist/core/ToolManager.js +91 -0
  127. package/dist/core/ToolManager.js.map +1 -1
  128. package/dist/index.d.ts +5 -1
  129. package/dist/index.d.ts.map +1 -1
  130. package/dist/index.js +3 -0
  131. package/dist/index.js.map +1 -1
  132. package/dist/providers/AnthropicProvider.d.ts +7 -0
  133. package/dist/providers/AnthropicProvider.d.ts.map +1 -1
  134. package/dist/providers/AnthropicProvider.js +109 -19
  135. package/dist/providers/AnthropicProvider.js.map +1 -1
  136. package/dist/providers/GeminiProvider.d.ts +32 -0
  137. package/dist/providers/GeminiProvider.d.ts.map +1 -1
  138. package/dist/providers/GeminiProvider.js +160 -53
  139. package/dist/providers/GeminiProvider.js.map +1 -1
  140. package/dist/providers/OpenAIProvider.d.ts +5 -0
  141. package/dist/providers/OpenAIProvider.d.ts.map +1 -1
  142. package/dist/providers/OpenAIProvider.js +65 -18
  143. package/dist/providers/OpenAIProvider.js.map +1 -1
  144. package/dist/providers/OpenRouterProvider.d.ts +5 -0
  145. package/dist/providers/OpenRouterProvider.d.ts.map +1 -1
  146. package/dist/providers/OpenRouterProvider.js +57 -18
  147. package/dist/providers/OpenRouterProvider.js.map +1 -1
  148. package/dist/types/agent.d.ts +44 -0
  149. package/dist/types/agent.d.ts.map +1 -1
  150. package/dist/types/agent.js.map +1 -1
  151. package/dist/types/ai.d.ts +2 -2
  152. package/dist/types/ai.d.ts.map +1 -1
  153. package/dist/types/compaction.d.ts +50 -0
  154. package/dist/types/compaction.d.ts.map +1 -0
  155. package/dist/types/compaction.js +5 -0
  156. package/dist/types/compaction.js.map +1 -0
  157. package/dist/types/index.d.ts +4 -2
  158. package/dist/types/index.d.ts.map +1 -1
  159. package/dist/types/index.js.map +1 -1
  160. package/dist/types/tool.d.ts +84 -0
  161. package/dist/types/tool.d.ts.map +1 -1
  162. package/docs/api/overview.md +140 -0
  163. package/docs/core/tools/enhanced-tool.md +186 -0
  164. package/docs/core/tools/streaming-execution.md +161 -0
  165. package/docs/guides/context-compaction.md +96 -0
  166. package/docs/guides/prompt-optimization.md +164 -0
  167. package/examples/advanced-patterns/context-compaction.ts +223 -0
  168. package/examples/advanced-patterns/streaming-responses.ts +85 -7
  169. package/examples/tools/enhanced-tool-metadata.ts +268 -0
  170. package/examples/tools/streaming-tool-execution.ts +283 -0
  171. package/package.json +1 -1
  172. package/src/core/Agent.ts +58 -2
  173. package/src/core/BatchPromptBuilder.ts +4 -1
  174. package/src/core/CompactionEngine.ts +318 -0
  175. package/src/core/PromptComposer.ts +259 -156
  176. package/src/core/PromptSectionCache.ts +136 -0
  177. package/src/core/ResponseEngine.ts +7 -11
  178. package/src/core/ResponseModal.ts +133 -83
  179. package/src/core/ResponsePipeline.ts +22 -22
  180. package/src/core/RoutingEngine.ts +16 -5
  181. package/src/core/SessionManager.ts +19 -0
  182. package/src/core/StreamingToolExecutor.ts +572 -0
  183. package/src/core/ToolManager.ts +151 -41
  184. package/src/index.ts +14 -0
  185. package/src/providers/AnthropicProvider.ts +121 -24
  186. package/src/providers/GeminiProvider.ts +174 -54
  187. package/src/providers/OpenAIProvider.ts +77 -25
  188. package/src/providers/OpenRouterProvider.ts +68 -25
  189. package/src/types/agent.ts +45 -0
  190. package/src/types/ai.ts +2 -2
  191. package/src/types/compaction.ts +52 -0
  192. package/src/types/index.ts +35 -14
  193. package/src/types/tool.ts +108 -0
@@ -0,0 +1,268 @@
1
+ /**
2
+ * Enhanced Tool Metadata Example
3
+ *
4
+ * Demonstrates the EnhancedTool interface with rich metadata for concurrency
5
+ * control, input validation, permission gating, and result size budgeting.
6
+ *
7
+ * Key concepts:
8
+ * - `isConcurrencySafe` — classify tools for parallel vs serial execution
9
+ * - `validateInput` — reject bad inputs before the handler runs
10
+ * - `checkPermissions` — gate execution behind authorization checks
11
+ * - `maxResultSizeChars` — cap result size to prevent context overflow
12
+ * - `interruptBehavior` — control abort signal handling ('cancel' vs 'block')
13
+ * - Backward compatibility: plain `Tool` objects work without any metadata
14
+ */
15
+
16
+ import {
17
+ Agent,
18
+ GeminiProvider,
19
+ type EnhancedTool,
20
+ type Tool,
21
+ type ToolContext,
22
+ } from "../../src/index";
23
+
24
+ // --- Context type for permission checks ---
25
+
26
+ interface AppContext {
27
+ userRole: "admin" | "editor" | "viewer";
28
+ userId: string;
29
+ }
30
+
31
+ // --- 1. Read-only tool with concurrency metadata ---
32
+
33
+ const fetchUserTool: EnhancedTool<AppContext> = {
34
+ id: "fetch_user",
35
+ name: "Fetch User",
36
+ description: "Fetch user profile by ID",
37
+ parameters: {
38
+ type: "object",
39
+ properties: { userId: { type: "string" } },
40
+ required: ["userId"],
41
+ },
42
+ handler: async (_ctx, args) => {
43
+ const userId = args?.userId as string;
44
+ return { data: `User ${userId}: Alice (admin)`, success: true };
45
+ },
46
+
47
+ // Concurrency metadata — safe to run alongside other reads
48
+ isConcurrencySafe: () => true,
49
+ isReadOnly: () => true,
50
+ isDestructive: () => false,
51
+ interruptBehavior: () => "cancel",
52
+
53
+ // Cap result size to 10k chars
54
+ maxResultSizeChars: 10_000,
55
+ };
56
+
57
+ // --- 2. Write tool with validation and permissions ---
58
+
59
+ const deleteResourceTool: EnhancedTool<AppContext> = {
60
+ id: "delete_resource",
61
+ name: "Delete Resource",
62
+ description: "Permanently delete a resource",
63
+ parameters: {
64
+ type: "object",
65
+ properties: { resourceId: { type: "string" } },
66
+ required: ["resourceId"],
67
+ },
68
+ handler: async (_ctx, args) => {
69
+ const id = args?.resourceId as string;
70
+ return { data: `Resource ${id} deleted`, success: true };
71
+ },
72
+
73
+ // Not concurrency-safe — must run exclusively
74
+ isConcurrencySafe: () => false,
75
+ isReadOnly: () => false,
76
+ isDestructive: () => true,
77
+ interruptBehavior: () => "block", // don't abort mid-delete
78
+
79
+ maxResultSizeChars: 500,
80
+
81
+ // Input validation — runs before handler
82
+ validateInput: (input) => {
83
+ const id = input.resourceId;
84
+ if (!id || typeof id !== "string" || id.trim().length === 0) {
85
+ return { valid: false, error: "resourceId must be a non-empty string" };
86
+ }
87
+ // Suggest correction for common prefix mistake
88
+ if (typeof id === "string" && !id.startsWith("res_")) {
89
+ return {
90
+ valid: false,
91
+ error: "resourceId must start with 'res_'",
92
+ correctedInput: { resourceId: `res_${id}` },
93
+ };
94
+ }
95
+ return { valid: true };
96
+ },
97
+
98
+ // Permission check — runs before handler
99
+ checkPermissions: (_input, ctx) => {
100
+ if (ctx.context.userRole !== "admin") {
101
+ return {
102
+ allowed: false,
103
+ reason: "Only admins can delete resources",
104
+ canOverride: false,
105
+ };
106
+ }
107
+ return { allowed: true };
108
+ },
109
+ };
110
+
111
+ // --- 3. Tool with input-dependent concurrency ---
112
+
113
+ const queryDatabaseTool: EnhancedTool<AppContext> = {
114
+ id: "query_database",
115
+ name: "Query Database",
116
+ description: "Run a database query (SELECT is concurrent-safe, mutations are not)",
117
+ parameters: {
118
+ type: "object",
119
+ properties: {
120
+ sql: { type: "string" },
121
+ readonly: { type: "boolean" },
122
+ },
123
+ required: ["sql"],
124
+ },
125
+ handler: async (_ctx, args) => {
126
+ const sql = args?.sql as string;
127
+ return { data: `Query result for: ${sql}`, success: true };
128
+ },
129
+
130
+ // Concurrency depends on the query type
131
+ isConcurrencySafe: (input) => input?.readonly === true,
132
+ isReadOnly: (input) => input?.readonly === true,
133
+ isDestructive: (input) => {
134
+ const sql = (input?.sql as string)?.toUpperCase() ?? "";
135
+ return sql.includes("DROP") || sql.includes("DELETE") || sql.includes("TRUNCATE");
136
+ },
137
+ interruptBehavior: () => "block",
138
+
139
+ // Validate SQL isn't empty
140
+ validateInput: (input) => {
141
+ if (!input.sql || typeof input.sql !== "string") {
142
+ return { valid: false, error: "sql must be a non-empty string" };
143
+ }
144
+ return { valid: true };
145
+ },
146
+
147
+ // Large query results get truncated
148
+ maxResultSizeChars: 50_000,
149
+ };
150
+
151
+ // --- 4. Plain Tool (backward compatible, no metadata) ---
152
+
153
+ const echoTool: Tool<AppContext> = {
154
+ id: "echo",
155
+ name: "Echo",
156
+ description: "Echo back the input (plain Tool, no EnhancedTool metadata)",
157
+ parameters: {
158
+ type: "object",
159
+ properties: { message: { type: "string" } },
160
+ required: ["message"],
161
+ },
162
+ handler: async (_ctx, args) => {
163
+ return { data: args?.message as string, success: true };
164
+ },
165
+ // No isConcurrencySafe, validateInput, checkPermissions, etc.
166
+ // Defaults: isConcurrencySafe → false, interruptBehavior → 'block'
167
+ };
168
+
169
+ // --- Demo functions ---
170
+
171
+ function demonstrateMetadata() {
172
+ console.log("=== EnhancedTool Metadata ===\n");
173
+
174
+ const tools = [fetchUserTool, deleteResourceTool, queryDatabaseTool, echoTool];
175
+
176
+ for (const tool of tools) {
177
+ const enhanced = tool as EnhancedTool;
178
+ console.log(`${tool.id}:`);
179
+ console.log(` concurrencySafe: ${enhanced.isConcurrencySafe?.() ?? "default (false)"}`);
180
+ console.log(` readOnly: ${enhanced.isReadOnly?.() ?? "default (false)"}`);
181
+ console.log(` destructive: ${enhanced.isDestructive?.() ?? "default (false)"}`);
182
+ console.log(` interruptBehavior: ${enhanced.interruptBehavior?.() ?? "default (block)"}`);
183
+ console.log(` maxResultSizeChars: ${enhanced.maxResultSizeChars ?? "none"}`);
184
+ console.log(` hasValidateInput: ${!!enhanced.validateInput}`);
185
+ console.log(` hasCheckPermissions: ${!!enhanced.checkPermissions}`);
186
+ console.log();
187
+ }
188
+ }
189
+
190
+ function demonstrateInputDependentConcurrency() {
191
+ console.log("=== Input-Dependent Concurrency ===\n");
192
+
193
+ const selectInput = { sql: "SELECT * FROM users", readonly: true };
194
+ const insertInput = { sql: "INSERT INTO users VALUES (...)", readonly: false };
195
+ const dropInput = { sql: "DROP TABLE users", readonly: false };
196
+
197
+ console.log(`SELECT query: concurrencySafe=${queryDatabaseTool.isConcurrencySafe!(selectInput)}`);
198
+ console.log(`INSERT query: concurrencySafe=${queryDatabaseTool.isConcurrencySafe!(insertInput)}`);
199
+ console.log(`DROP query: destructive=${queryDatabaseTool.isDestructive!(dropInput)}`);
200
+ console.log();
201
+ }
202
+
203
+ async function demonstrateValidation() {
204
+ console.log("=== Input Validation ===\n");
205
+
206
+ const cases = [
207
+ { label: "valid ID", input: { resourceId: "res_123" } },
208
+ { label: "missing prefix", input: { resourceId: "123" } },
209
+ { label: "empty string", input: { resourceId: "" } },
210
+ ];
211
+
212
+ for (const { label, input } of cases) {
213
+ const result = deleteResourceTool.validateInput!(input, {} as any);
214
+ const resolved = result instanceof Promise ? await result : result;
215
+ console.log(` ${label}: valid=${resolved.valid}${resolved.error ? `, error="${resolved.error}"` : ""}${resolved.correctedInput ? `, corrected=${JSON.stringify(resolved.correctedInput)}` : ""}`);
216
+ }
217
+ console.log();
218
+ }
219
+
220
+ async function demonstratePermissions() {
221
+ console.log("=== Permission Gating ===\n");
222
+
223
+ const roles: Array<"admin" | "editor" | "viewer"> = ["admin", "editor", "viewer"];
224
+
225
+ for (const role of roles) {
226
+ const mockCtx = { context: { userRole: role, userId: "u1" } } as ToolContext<AppContext>;
227
+ const result = deleteResourceTool.checkPermissions!({ resourceId: "res_1" }, mockCtx);
228
+ const resolved = result instanceof Promise ? await result : result;
229
+ console.log(` role=${role}: allowed=${resolved.allowed}${resolved.reason ? `, reason="${resolved.reason}"` : ""}`);
230
+ }
231
+ console.log();
232
+ }
233
+
234
+ function demonstrateAgentRegistration() {
235
+ console.log("=== Agent Registration ===\n");
236
+
237
+ const agent = new Agent<AppContext>({
238
+ name: "MetadataDemo",
239
+ description: "Demonstrates EnhancedTool metadata",
240
+ provider: new GeminiProvider({
241
+ apiKey: process.env.GEMINI_API_KEY || "demo-key",
242
+ model: "models/gemini-2.5-flash",
243
+ }),
244
+ context: { userRole: "admin", userId: "u1" },
245
+ // Mix of EnhancedTool and plain Tool — both accepted seamlessly
246
+ tools: [fetchUserTool, deleteResourceTool, queryDatabaseTool, echoTool],
247
+ });
248
+
249
+ console.log("Registered tools:");
250
+ for (const t of agent.getTools()) {
251
+ console.log(` - ${t.id}`);
252
+ }
253
+ console.log("\nPlain Tool objects work alongside EnhancedTool without changes.");
254
+ }
255
+
256
+ async function main() {
257
+ demonstrateMetadata();
258
+ demonstrateInputDependentConcurrency();
259
+ await demonstrateValidation();
260
+ await demonstratePermissions();
261
+ demonstrateAgentRegistration();
262
+ }
263
+
264
+ if (import.meta.url === `file://${process.argv[1]}`) {
265
+ main().catch(console.error);
266
+ }
267
+
268
+ export { main };
@@ -0,0 +1,283 @@
1
+ /**
2
+ * Streaming Tool Execution Example
3
+ *
4
+ * Demonstrates the StreamingToolExecutor with mixed read-only and write tools,
5
+ * showing how read-only tools execute in parallel while write tools run serially.
6
+ *
7
+ * Key concepts:
8
+ * - EnhancedTool with `isConcurrencySafe` metadata
9
+ * - Parallel execution of concurrent-safe (read-only) tools
10
+ * - Serial execution of non-concurrent-safe (write) tools
11
+ * - Result ordering preserved regardless of completion order
12
+ * - Progress reporting from long-running tools
13
+ * - Abort signal support
14
+ */
15
+
16
+ import {
17
+ Agent,
18
+ GeminiProvider,
19
+ StreamingToolExecutor,
20
+ type EnhancedTool,
21
+ type ToolCallRequest,
22
+ } from "../../src/index";
23
+
24
+ // --- Read-only tools (concurrency-safe, run in parallel) ---
25
+
26
+ const readFileTool: EnhancedTool = {
27
+ id: "read_file",
28
+ name: "Read File",
29
+ description: "Read a file from disk",
30
+ parameters: {
31
+ type: "object",
32
+ properties: { path: { type: "string" } },
33
+ required: ["path"],
34
+ },
35
+ handler: async (_ctx, args) => {
36
+ const path = args?.path as string;
37
+ // Simulate file read latency
38
+ await new Promise((r) => setTimeout(r, 200));
39
+ return { data: `Contents of ${path}: [mock file data]`, success: true };
40
+ },
41
+ isConcurrencySafe: () => true,
42
+ isReadOnly: () => true,
43
+ isDestructive: () => false,
44
+ interruptBehavior: () => "cancel",
45
+ maxResultSizeChars: 50_000,
46
+ };
47
+
48
+ const listDirectoryTool: EnhancedTool = {
49
+ id: "list_directory",
50
+ name: "List Directory",
51
+ description: "List files in a directory",
52
+ parameters: {
53
+ type: "object",
54
+ properties: { path: { type: "string" } },
55
+ required: ["path"],
56
+ },
57
+ handler: async (_ctx, args) => {
58
+ const path = args?.path as string;
59
+ await new Promise((r) => setTimeout(r, 150));
60
+ return {
61
+ data: `Files in ${path}: index.ts, utils.ts, types.ts`,
62
+ success: true,
63
+ };
64
+ },
65
+ isConcurrencySafe: () => true,
66
+ isReadOnly: () => true,
67
+ isDestructive: () => false,
68
+ interruptBehavior: () => "cancel",
69
+ };
70
+
71
+ const searchCodeTool: EnhancedTool = {
72
+ id: "search_code",
73
+ name: "Search Code",
74
+ description: "Search for patterns in the codebase",
75
+ parameters: {
76
+ type: "object",
77
+ properties: { query: { type: "string" } },
78
+ required: ["query"],
79
+ },
80
+ handler: async (_ctx, args) => {
81
+ const query = args?.query as string;
82
+ await new Promise((r) => setTimeout(r, 300));
83
+ return {
84
+ data: `Found 3 matches for "${query}" in src/`,
85
+ success: true,
86
+ };
87
+ },
88
+ isConcurrencySafe: () => true,
89
+ isReadOnly: () => true,
90
+ isDestructive: () => false,
91
+ interruptBehavior: () => "cancel",
92
+ };
93
+
94
+ // --- Write tools (NOT concurrency-safe, run serially) ---
95
+
96
+ const writeFileTool: EnhancedTool = {
97
+ id: "write_file",
98
+ name: "Write File",
99
+ description: "Write content to a file",
100
+ parameters: {
101
+ type: "object",
102
+ properties: {
103
+ path: { type: "string" },
104
+ content: { type: "string" },
105
+ },
106
+ required: ["path", "content"],
107
+ },
108
+ handler: async (_ctx, args) => {
109
+ const path = args?.path as string;
110
+ await new Promise((r) => setTimeout(r, 250));
111
+ return { data: `Wrote to ${path}`, success: true };
112
+ },
113
+ isConcurrencySafe: () => false,
114
+ isReadOnly: () => false,
115
+ isDestructive: (input) => true,
116
+ interruptBehavior: () => "block",
117
+ maxResultSizeChars: 1_000,
118
+ };
119
+
120
+ const deleteFileTool: EnhancedTool = {
121
+ id: "delete_file",
122
+ name: "Delete File",
123
+ description: "Delete a file from disk",
124
+ parameters: {
125
+ type: "object",
126
+ properties: { path: { type: "string" } },
127
+ required: ["path"],
128
+ },
129
+ handler: async (_ctx, args) => {
130
+ const path = args?.path as string;
131
+ await new Promise((r) => setTimeout(r, 100));
132
+ return { data: `Deleted ${path}`, success: true };
133
+ },
134
+ isConcurrencySafe: () => false,
135
+ isReadOnly: () => false,
136
+ isDestructive: () => true,
137
+ interruptBehavior: () => "block",
138
+ };
139
+
140
+ // Helper: create a minimal ToolContext for standalone executor usage
141
+ function createMockToolContext() {
142
+ return {
143
+ context: {},
144
+ data: {},
145
+ history: [],
146
+ updateContext: async () => { },
147
+ updateData: async () => { },
148
+ getField: () => undefined,
149
+ setField: async () => { },
150
+ hasField: () => false,
151
+ } as any;
152
+ }
153
+
154
+ // --- Direct StreamingToolExecutor usage ---
155
+
156
+ async function demonstrateDirectExecutor() {
157
+ console.log("=== Direct StreamingToolExecutor Demo ===\n");
158
+
159
+ const toolMap = new Map<string, EnhancedTool>([
160
+ ["read_file", readFileTool],
161
+ ["list_directory", listDirectoryTool],
162
+ ["search_code", searchCodeTool],
163
+ ["write_file", writeFileTool],
164
+ ]);
165
+
166
+ // Simulate a sequence of tool calls from an LLM response:
167
+ // 3 reads (parallel) followed by 1 write (serial)
168
+ const toolCalls: ToolCallRequest[] = [
169
+ { id: "call_1", toolName: "read_file", arguments: { path: "src/index.ts" } },
170
+ { id: "call_2", toolName: "list_directory", arguments: { path: "src/" } },
171
+ { id: "call_3", toolName: "search_code", arguments: { query: "import" } },
172
+ { id: "call_4", toolName: "write_file", arguments: { path: "out.ts", content: "// generated" } },
173
+ ];
174
+
175
+ const executor = new StreamingToolExecutor<unknown, unknown>(
176
+ createMockToolContext(),
177
+ { maxParallel: 5 },
178
+ );
179
+
180
+ console.log("Queueing tools as they arrive from the LLM stream...\n");
181
+
182
+ for (const call of toolCalls) {
183
+ const tool = toolMap.get(call.toolName)!;
184
+ const safe = tool.isConcurrencySafe?.() ? "parallel" : "serial";
185
+ console.log(` + Queued: ${call.toolName} (${safe})`);
186
+ executor.addTool(call, tool);
187
+ }
188
+
189
+ console.log("\nResults (yielded in original request order):\n");
190
+
191
+ for await (const update of executor.getRemainingResults()) {
192
+ if (update.progress) {
193
+ console.log(` [progress] ${update.toolCallId}: ${update.progress}`);
194
+ }
195
+ if (update.result) {
196
+ console.log(` [result] ${update.toolCallId}: ${update.result.data}`);
197
+ }
198
+ }
199
+
200
+ console.log("\nAll tools complete.");
201
+ }
202
+
203
+ // --- Agent-level integration ---
204
+
205
+ async function demonstrateAgentIntegration() {
206
+ console.log("\n=== Agent Integration Demo ===\n");
207
+
208
+ const agent = new Agent({
209
+ name: "CodeAssistant",
210
+ description: "An assistant with streaming tool execution",
211
+ provider: new GeminiProvider({
212
+ apiKey: process.env.GEMINI_API_KEY || "demo-key",
213
+ model: "models/gemini-2.5-flash",
214
+ }),
215
+ tools: [readFileTool, listDirectoryTool, searchCodeTool, writeFileTool, deleteFileTool],
216
+ });
217
+
218
+ console.log("Tools registered:");
219
+ for (const t of agent.getTools()) {
220
+ const enhanced = t as EnhancedTool;
221
+ const safe = enhanced.isConcurrencySafe?.() ?? false;
222
+ console.log(` - ${t.id} (concurrencySafe: ${safe})`);
223
+ }
224
+
225
+ console.log("\nStreaming tool execution happens automatically during respondStream().");
226
+ console.log("Read-only tools run in parallel; write tools wait for exclusive access.\n");
227
+
228
+ // In a real scenario you'd call agent.stream() or agent.respondStream()
229
+ // and the StreamingToolExecutor handles concurrency internally.
230
+ console.log("Example streaming usage:");
231
+ console.log(' for await (const chunk of agent.stream("Read index.ts and list src/")) {');
232
+ console.log(" process.stdout.write(chunk.delta);");
233
+ console.log(" }");
234
+ }
235
+
236
+ // --- Abort signal demo ---
237
+
238
+ async function demonstrateAbortSignal() {
239
+ console.log("\n=== Abort Signal Demo ===\n");
240
+
241
+ const controller = new AbortController();
242
+ const executor = new StreamingToolExecutor<unknown, unknown>(
243
+ createMockToolContext(),
244
+ { maxParallel: 5, signal: controller.signal },
245
+ );
246
+
247
+ // Queue a slow read and a write
248
+ executor.addTool(
249
+ { id: "slow_read", toolName: "search_code", arguments: { query: "TODO" } },
250
+ searchCodeTool
251
+ );
252
+ executor.addTool(
253
+ { id: "slow_write", toolName: "write_file", arguments: { path: "tmp.ts", content: "x" } },
254
+ writeFileTool
255
+ );
256
+
257
+ // Abort after 100ms — 'cancel' tools abort immediately, 'block' tools finish
258
+ setTimeout(() => {
259
+ console.log(" Aborting...");
260
+ controller.abort();
261
+ }, 100);
262
+
263
+ for await (const update of executor.getRemainingResults()) {
264
+ if (update.result) {
265
+ const status = update.result.success ? "ok" : "aborted";
266
+ console.log(` ${update.toolCallId}: ${status} — ${update.result.data ?? update.result.error}`);
267
+ }
268
+ }
269
+
270
+ console.log(" Done (abort handled gracefully).");
271
+ }
272
+
273
+ async function main() {
274
+ await demonstrateDirectExecutor();
275
+ await demonstrateAgentIntegration();
276
+ await demonstrateAbortSignal();
277
+ }
278
+
279
+ if (import.meta.url === `file://${process.argv[1]}`) {
280
+ main().catch(console.error);
281
+ }
282
+
283
+ export { main };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@falai/agent",
3
- "version": "1.1.3",
3
+ "version": "1.2.1",
4
4
  "description": "Standalone, strongly-typed AI Agent framework with route DSL and AI provider strategy",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",