@flink-app/flink 0.14.3 → 2.0.0-alpha.100

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 (280) hide show
  1. package/CHANGELOG.md +1051 -0
  2. package/SCHEMA_EXTRACTION_ANALYSIS.md +494 -0
  3. package/SIMPLE_AST_FEASIBILITY.md +570 -0
  4. package/bin/flink.ts +13 -2
  5. package/cli/build.ts +24 -44
  6. package/cli/clean.ts +13 -25
  7. package/cli/cli-utils.ts +190 -17
  8. package/cli/dev.ts +252 -0
  9. package/cli/loadEnvFiles.ts +116 -0
  10. package/cli/run.ts +45 -62
  11. package/dist/bin/flink.js +61 -2
  12. package/dist/cli/build.js +20 -25
  13. package/dist/cli/clean.js +12 -10
  14. package/dist/cli/cli-utils.d.ts +34 -3
  15. package/dist/cli/cli-utils.js +193 -12
  16. package/dist/cli/dev.d.ts +2 -0
  17. package/dist/cli/dev.js +279 -0
  18. package/dist/cli/loadEnvFiles.d.ts +30 -0
  19. package/dist/cli/loadEnvFiles.js +113 -0
  20. package/dist/cli/run.js +47 -46
  21. package/dist/src/DependencyTracker.d.ts +44 -0
  22. package/dist/src/DependencyTracker.js +239 -0
  23. package/dist/src/FlinkApp.d.ts +163 -10
  24. package/dist/src/FlinkApp.js +847 -184
  25. package/dist/src/FlinkContext.d.ts +41 -0
  26. package/dist/src/FlinkErrors.d.ts +19 -6
  27. package/dist/src/FlinkErrors.js +36 -42
  28. package/dist/src/FlinkHttpHandler.d.ts +219 -26
  29. package/dist/src/FlinkHttpHandler.js +37 -1
  30. package/dist/src/FlinkJob.d.ts +10 -0
  31. package/dist/src/FlinkLog.d.ts +82 -18
  32. package/dist/src/FlinkLog.js +165 -13
  33. package/dist/src/FlinkLogFactory.d.ts +288 -0
  34. package/dist/src/FlinkLogFactory.js +619 -0
  35. package/dist/src/FlinkRepo.d.ts +10 -2
  36. package/dist/src/FlinkRepo.js +11 -1
  37. package/dist/src/FlinkRequestContext.d.ts +63 -0
  38. package/dist/src/FlinkRequestContext.js +74 -0
  39. package/dist/src/FlinkResponse.d.ts +6 -0
  40. package/dist/src/FlinkService.d.ts +38 -0
  41. package/dist/src/FlinkService.js +46 -0
  42. package/dist/src/LeaderElection.d.ts +45 -0
  43. package/dist/src/LeaderElection.js +269 -0
  44. package/dist/src/SchemaCache.d.ts +84 -0
  45. package/dist/src/SchemaCache.js +289 -0
  46. package/dist/src/TypeScriptCompiler.d.ts +161 -51
  47. package/dist/src/TypeScriptCompiler.js +1253 -617
  48. package/dist/src/TypeScriptUtils.js +4 -0
  49. package/dist/src/ai/AgentRunner.d.ts +39 -0
  50. package/dist/src/ai/AgentRunner.js +760 -0
  51. package/dist/src/ai/ConversationAgent.d.ts +279 -0
  52. package/dist/src/ai/ConversationAgent.js +404 -0
  53. package/dist/src/ai/ConversationFlinkAgent.d.ts +278 -0
  54. package/dist/src/ai/ConversationFlinkAgent.js +404 -0
  55. package/dist/src/ai/FlinkAgent.d.ts +690 -0
  56. package/dist/src/ai/FlinkAgent.js +729 -0
  57. package/dist/src/ai/FlinkTool.d.ts +135 -0
  58. package/dist/src/ai/FlinkTool.js +2 -0
  59. package/dist/src/ai/InMemoryConversationAgent.d.ts +121 -0
  60. package/dist/src/ai/InMemoryConversationAgent.js +209 -0
  61. package/dist/src/ai/LLMAdapter.d.ts +148 -0
  62. package/dist/src/ai/LLMAdapter.js +2 -0
  63. package/dist/src/ai/PersistentFlinkAgent.d.ts +278 -0
  64. package/dist/src/ai/PersistentFlinkAgent.js +403 -0
  65. package/dist/src/ai/SubAgentExecutor.d.ts +38 -0
  66. package/dist/src/ai/SubAgentExecutor.js +223 -0
  67. package/dist/src/ai/ToolExecutor.d.ts +64 -0
  68. package/dist/src/ai/ToolExecutor.js +497 -0
  69. package/dist/src/ai/agentInstructions.d.ts +68 -0
  70. package/dist/src/ai/agentInstructions.js +286 -0
  71. package/dist/src/ai/index.d.ts +8 -0
  72. package/dist/src/ai/index.js +26 -0
  73. package/dist/src/ai/instructionFileLoader.d.ts +44 -0
  74. package/dist/src/ai/instructionFileLoader.js +179 -0
  75. package/dist/src/auth/FlinkAuthPlugin.d.ts +1 -1
  76. package/dist/src/handlers/StreamWriterFactory.d.ts +20 -0
  77. package/dist/src/handlers/StreamWriterFactory.js +83 -0
  78. package/dist/src/index.d.ts +14 -0
  79. package/dist/src/index.js +17 -0
  80. package/dist/src/loadPluginSchemas.d.ts +45 -0
  81. package/dist/src/loadPluginSchemas.js +143 -0
  82. package/dist/src/schema-extraction/ComplexTypeDetection.d.ts +40 -0
  83. package/dist/src/schema-extraction/ComplexTypeDetection.js +75 -0
  84. package/dist/src/schema-extraction/TypeScriptSourceParser.d.ts +321 -0
  85. package/dist/src/schema-extraction/TypeScriptSourceParser.js +925 -0
  86. package/dist/src/schema-extraction/TypeScriptSourceParser.spec.d.ts +1 -0
  87. package/dist/src/schema-extraction/TypeScriptSourceParser.spec.js +233 -0
  88. package/dist/src/schema-extraction/TypeScriptTokenizer.d.ts +57 -0
  89. package/dist/src/schema-extraction/TypeScriptTokenizer.js +177 -0
  90. package/dist/src/schema-extraction/index.d.ts +2 -0
  91. package/dist/src/schema-extraction/index.js +20 -0
  92. package/dist/src/schema-extraction/types.d.ts +31 -0
  93. package/dist/src/schema-extraction/types.js +2 -0
  94. package/dist/src/utils/loadFlinkConfig.d.ts +53 -0
  95. package/dist/src/utils/loadFlinkConfig.js +77 -0
  96. package/dist/src/utils.d.ts +30 -0
  97. package/dist/src/utils.js +52 -0
  98. package/dist/src/workers/SchemaGeneratorWorker.d.ts +1 -0
  99. package/dist/src/workers/SchemaGeneratorWorker.js +49 -0
  100. package/dist/src/workers/WorkerPool.d.ts +60 -0
  101. package/dist/src/workers/WorkerPool.js +306 -0
  102. package/examples/logging-hierarchical-example.ts +125 -0
  103. package/package.json +29 -4
  104. package/readme.md +499 -0
  105. package/spec/AgentDescendantDetection.spec.ts +335 -0
  106. package/spec/AgentDuplicateDetection.spec.ts +112 -0
  107. package/spec/AgentObserver.spec.ts +266 -0
  108. package/spec/AgentRunner.spec.ts +1062 -0
  109. package/spec/AsyncLocalStorageContext.spec.ts +223 -0
  110. package/spec/ConversationHooks.spec.ts +257 -0
  111. package/spec/FlinkAgent.spec.ts +681 -0
  112. package/spec/FlinkApp.htmlResponse.spec.ts +260 -0
  113. package/spec/FlinkApp.onError.invocation.spec.ts +151 -0
  114. package/spec/FlinkApp.onError.spec.ts +1 -2
  115. package/spec/FlinkApp.query.spec.ts +107 -0
  116. package/spec/FlinkApp.routeOrdering.spec.ts +61 -0
  117. package/spec/FlinkApp.undefinedResponse.spec.ts +123 -0
  118. package/spec/FlinkApp.validationMode.spec.ts +155 -0
  119. package/spec/FlinkJob.spec.ts +171 -0
  120. package/spec/FlinkLogFactory.spec.ts +337 -0
  121. package/spec/FlinkRepo.spec.ts +1 -1
  122. package/spec/LeaderElection.spec.ts +174 -0
  123. package/spec/StreamingIntegration.spec.ts +139 -0
  124. package/spec/ToolExecutor.spec.ts +465 -0
  125. package/spec/TypeScriptCompiler.spec.ts +1 -1
  126. package/spec/TypeScriptSourceParser.spec.ts +1215 -0
  127. package/spec/TypeScriptTokenizer.spec.ts +366 -0
  128. package/spec/ai/ContextCompaction.spec.ts +405 -0
  129. package/spec/ai/ConversationAgent.spec.ts +520 -0
  130. package/spec/ai/InMemoryConversationAgent.spec.ts +144 -0
  131. package/spec/ai/agentInstructions.spec.ts +358 -0
  132. package/spec/fixtures/agent-instructions/TestAgent.ts +24 -0
  133. package/spec/fixtures/agent-instructions/simple.md +3 -0
  134. package/spec/fixtures/agent-instructions/template.md +18 -0
  135. package/spec/fixtures/agent-instructions/yaml-format.yaml +9 -0
  136. package/spec/mock-project/dist/.tsbuildinfo +1 -0
  137. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCar.js +56 -0
  138. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCar2.js +58 -0
  139. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCarWithArraySchema.js +52 -0
  140. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCarWithArraySchema2.js +52 -0
  141. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCarWithArraySchema3.js +52 -0
  142. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCarWithLiteralSchema.js +54 -0
  143. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCarWithLiteralSchema2.js +54 -0
  144. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCarWithSchemaInFile.js +57 -0
  145. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCarWithSchemaInFile2.js +57 -0
  146. package/spec/mock-project/dist/spec/mock-project/src/handlers/ManuallyAddedHandler.js +53 -0
  147. package/spec/mock-project/dist/spec/mock-project/src/handlers/ManuallyAddedHandler2.js +55 -0
  148. package/spec/mock-project/dist/spec/mock-project/src/handlers/PatchCar.js +57 -0
  149. package/spec/mock-project/dist/spec/mock-project/src/handlers/PatchOnboardingSession.js +75 -0
  150. package/spec/mock-project/dist/spec/mock-project/src/handlers/PatchOrderWithComplexTypes.js +57 -0
  151. package/spec/mock-project/dist/spec/mock-project/src/handlers/PatchProductWithIntersection.js +58 -0
  152. package/spec/mock-project/dist/spec/mock-project/src/handlers/PatchUserWithUnion.js +58 -0
  153. package/spec/mock-project/dist/spec/mock-project/src/handlers/PostCar.js +54 -0
  154. package/spec/mock-project/dist/spec/mock-project/src/handlers/PostLogin.js +55 -0
  155. package/spec/mock-project/dist/spec/mock-project/src/handlers/PostLogout.js +54 -0
  156. package/spec/mock-project/dist/spec/mock-project/src/handlers/PutCar.js +54 -0
  157. package/spec/mock-project/dist/spec/mock-project/src/index.js +83 -0
  158. package/spec/mock-project/dist/spec/mock-project/src/repos/CarRepo.js +26 -0
  159. package/spec/mock-project/dist/spec/mock-project/src/schemas/Car.js +2 -0
  160. package/spec/mock-project/dist/spec/mock-project/src/schemas/DefaultExportSchema.js +2 -0
  161. package/spec/mock-project/dist/spec/mock-project/src/schemas/FileWithTwoSchemas.js +2 -0
  162. package/spec/mock-project/dist/src/FlinkApp.js +1000 -0
  163. package/spec/mock-project/dist/src/FlinkContext.js +2 -0
  164. package/spec/mock-project/dist/src/FlinkErrors.js +143 -0
  165. package/spec/mock-project/dist/src/FlinkHttpHandler.js +47 -0
  166. package/spec/mock-project/dist/src/FlinkJob.js +2 -0
  167. package/spec/mock-project/dist/src/FlinkLog.js +119 -0
  168. package/spec/mock-project/dist/src/FlinkLogFactory.js +617 -0
  169. package/spec/mock-project/dist/src/FlinkPlugin.js +2 -0
  170. package/spec/mock-project/dist/src/FlinkRepo.js +224 -0
  171. package/spec/mock-project/dist/src/FlinkRequestContext.js +74 -0
  172. package/spec/mock-project/dist/src/FlinkResponse.js +2 -0
  173. package/spec/mock-project/dist/src/ai/AgentExecutor.js +279 -0
  174. package/spec/mock-project/dist/src/ai/AgentRunner.js +632 -0
  175. package/spec/mock-project/dist/src/ai/ConversationAgent.js +402 -0
  176. package/spec/mock-project/dist/src/ai/ConversationFlinkAgent.js +422 -0
  177. package/spec/mock-project/dist/src/ai/FlinkAgent.js +699 -0
  178. package/spec/mock-project/dist/src/ai/FlinkTool.js +2 -0
  179. package/spec/mock-project/dist/src/ai/InMemoryConversationAgent.js +209 -0
  180. package/spec/mock-project/dist/src/ai/LLMAdapter.js +2 -0
  181. package/spec/mock-project/dist/src/ai/SubAgentExecutor.js +223 -0
  182. package/spec/mock-project/dist/src/ai/ToolExecutor.js +412 -0
  183. package/spec/mock-project/dist/src/ai/agentInstructions.js +246 -0
  184. package/spec/mock-project/dist/src/auth/FlinkAuthPlugin.js +2 -0
  185. package/spec/mock-project/dist/src/auth/FlinkAuthUser.js +2 -0
  186. package/spec/mock-project/dist/src/handlers/GetCar.js +26 -52
  187. package/spec/mock-project/dist/src/handlers/GetCar.js.map +1 -0
  188. package/spec/mock-project/dist/src/handlers/GetCar2.js +32 -54
  189. package/spec/mock-project/dist/src/handlers/GetCar2.js.map +1 -0
  190. package/spec/mock-project/dist/src/handlers/GetCarWithArraySchema.js +26 -48
  191. package/spec/mock-project/dist/src/handlers/GetCarWithArraySchema.js.map +1 -0
  192. package/spec/mock-project/dist/src/handlers/GetCarWithArraySchema2.js +28 -48
  193. package/spec/mock-project/dist/src/handlers/GetCarWithArraySchema2.js.map +1 -0
  194. package/spec/mock-project/dist/src/handlers/GetCarWithArraySchema3.js +29 -48
  195. package/spec/mock-project/dist/src/handlers/GetCarWithArraySchema3.js.map +1 -0
  196. package/spec/mock-project/dist/src/handlers/GetCarWithLiteralSchema.js +26 -50
  197. package/spec/mock-project/dist/src/handlers/GetCarWithLiteralSchema.js.map +1 -0
  198. package/spec/mock-project/dist/src/handlers/GetCarWithLiteralSchema2.js +28 -50
  199. package/spec/mock-project/dist/src/handlers/GetCarWithLiteralSchema2.js.map +1 -0
  200. package/spec/mock-project/dist/src/handlers/GetCarWithSchemaInFile.js +27 -53
  201. package/spec/mock-project/dist/src/handlers/GetCarWithSchemaInFile.js.map +1 -0
  202. package/spec/mock-project/dist/src/handlers/GetCarWithSchemaInFile2.js +29 -53
  203. package/spec/mock-project/dist/src/handlers/GetCarWithSchemaInFile2.js.map +1 -0
  204. package/spec/mock-project/dist/src/handlers/ManuallyAddedHandler.js +16 -49
  205. package/spec/mock-project/dist/src/handlers/ManuallyAddedHandler.js.map +1 -0
  206. package/spec/mock-project/dist/src/handlers/ManuallyAddedHandler2.js +25 -50
  207. package/spec/mock-project/dist/src/handlers/ManuallyAddedHandler2.js.map +1 -0
  208. package/spec/mock-project/dist/src/handlers/PatchCar.js +27 -53
  209. package/spec/mock-project/dist/src/handlers/PatchCar.js.map +1 -0
  210. package/spec/mock-project/dist/src/handlers/PatchOnboardingSession.js +44 -70
  211. package/spec/mock-project/dist/src/handlers/PatchOnboardingSession.js.map +1 -0
  212. package/spec/mock-project/dist/src/handlers/PatchOrderWithComplexTypes.js +27 -53
  213. package/spec/mock-project/dist/src/handlers/PatchOrderWithComplexTypes.js.map +1 -0
  214. package/spec/mock-project/dist/src/handlers/PatchProductWithIntersection.js +28 -54
  215. package/spec/mock-project/dist/src/handlers/PatchProductWithIntersection.js.map +1 -0
  216. package/spec/mock-project/dist/src/handlers/PatchUserWithUnion.js +28 -54
  217. package/spec/mock-project/dist/src/handlers/PatchUserWithUnion.js.map +1 -0
  218. package/spec/mock-project/dist/src/handlers/PostCar.js +24 -50
  219. package/spec/mock-project/dist/src/handlers/PostCar.js.map +1 -0
  220. package/spec/mock-project/dist/src/handlers/PostLogin.js +25 -51
  221. package/spec/mock-project/dist/src/handlers/PostLogin.js.map +1 -0
  222. package/spec/mock-project/dist/src/handlers/PostLogout.js +24 -50
  223. package/spec/mock-project/dist/src/handlers/PostLogout.js.map +1 -0
  224. package/spec/mock-project/dist/src/handlers/PutCar.js +24 -50
  225. package/spec/mock-project/dist/src/handlers/PutCar.js.map +1 -0
  226. package/spec/mock-project/dist/src/handlers/StreamWriterFactory.js +83 -0
  227. package/spec/mock-project/dist/src/index.js +52 -76
  228. package/spec/mock-project/dist/src/index.js.map +1 -0
  229. package/spec/mock-project/dist/src/mock-data-generator.js +9 -0
  230. package/spec/mock-project/dist/src/repos/CarRepo.js +12 -24
  231. package/spec/mock-project/dist/src/repos/CarRepo.js.map +1 -0
  232. package/spec/mock-project/dist/src/schemas/Car.js +3 -1
  233. package/spec/mock-project/dist/src/schemas/Car.js.map +1 -0
  234. package/spec/mock-project/dist/src/schemas/DefaultExportSchema.js +3 -1
  235. package/spec/mock-project/dist/src/schemas/DefaultExportSchema.js.map +1 -0
  236. package/spec/mock-project/dist/src/schemas/FileWithTwoSchemas.js +3 -1
  237. package/spec/mock-project/dist/src/schemas/FileWithTwoSchemas.js.map +1 -0
  238. package/spec/mock-project/dist/src/utils.js +290 -0
  239. package/spec/mock-project/tsconfig.json +6 -1
  240. package/spec/schema-generation-nested-objects.spec.ts +97 -0
  241. package/spec/testHelpers.ts +49 -0
  242. package/spec/utils.caseConversion.spec.ts +78 -0
  243. package/spec/utils.spec.ts +13 -13
  244. package/src/DependencyTracker.ts +166 -0
  245. package/src/FlinkApp.ts +919 -155
  246. package/src/FlinkContext.ts +43 -0
  247. package/src/FlinkErrors.ts +32 -12
  248. package/src/FlinkHttpHandler.ts +246 -28
  249. package/src/FlinkJob.ts +11 -0
  250. package/src/FlinkLog.ts +119 -12
  251. package/src/FlinkLogFactory.ts +699 -0
  252. package/src/FlinkRepo.ts +10 -3
  253. package/src/FlinkRequestContext.ts +95 -0
  254. package/src/FlinkResponse.ts +6 -0
  255. package/src/FlinkService.ts +49 -0
  256. package/src/LeaderElection.ts +203 -0
  257. package/src/SchemaCache.ts +232 -0
  258. package/src/TypeScriptCompiler.ts +1347 -610
  259. package/src/TypeScriptUtils.ts +5 -0
  260. package/src/ai/AgentRunner.ts +646 -0
  261. package/src/ai/ConversationAgent.ts +413 -0
  262. package/src/ai/FlinkAgent.ts +1069 -0
  263. package/src/ai/FlinkTool.ts +165 -0
  264. package/src/ai/InMemoryConversationAgent.ts +149 -0
  265. package/src/ai/LLMAdapter.ts +126 -0
  266. package/src/ai/ToolExecutor.ts +485 -0
  267. package/src/ai/agentInstructions.ts +245 -0
  268. package/src/ai/index.ts +8 -0
  269. package/src/ai/instructionFileLoader.ts +156 -0
  270. package/src/auth/FlinkAuthPlugin.ts +2 -1
  271. package/src/handlers/StreamWriterFactory.ts +84 -0
  272. package/src/index.ts +14 -0
  273. package/src/loadPluginSchemas.ts +141 -0
  274. package/src/schema-extraction/TypeScriptSourceParser.ts +1058 -0
  275. package/src/schema-extraction/TypeScriptTokenizer.ts +205 -0
  276. package/src/schema-extraction/index.ts +2 -0
  277. package/src/schema-extraction/types.ts +34 -0
  278. package/src/utils/loadFlinkConfig.ts +89 -0
  279. package/src/utils.ts +52 -0
  280. package/tsconfig.json +6 -1
@@ -0,0 +1,135 @@
1
+ import { z } from "zod";
2
+ import { FlinkContext } from "../FlinkContext";
3
+ /**
4
+ * Standardized tool result format for better error handling
5
+ */
6
+ export type ToolResult<T = any> = {
7
+ success: true;
8
+ data: T;
9
+ } | {
10
+ success: false;
11
+ error: string;
12
+ code?: string;
13
+ };
14
+ /**
15
+ * JSON Schema types
16
+ */
17
+ export type JSONSchemaType = "string" | "number" | "integer" | "boolean" | "object" | "array" | "null";
18
+ /**
19
+ * JSON Schema for object types
20
+ */
21
+ export interface JSONSchemaObject {
22
+ type: "object";
23
+ properties?: Record<string, ToolJSONSchema>;
24
+ required?: string[];
25
+ additionalProperties?: boolean | ToolJSONSchema;
26
+ description?: string;
27
+ [key: string]: any;
28
+ }
29
+ /**
30
+ * JSON Schema for array types
31
+ */
32
+ export interface JSONSchemaArray {
33
+ type: "array";
34
+ items?: ToolJSONSchema | ToolJSONSchema[];
35
+ minItems?: number;
36
+ maxItems?: number;
37
+ uniqueItems?: boolean;
38
+ description?: string;
39
+ [key: string]: any;
40
+ }
41
+ /**
42
+ * JSON Schema for primitive types
43
+ */
44
+ export interface JSONSchemaPrimitive {
45
+ type: "string" | "number" | "integer" | "boolean" | "null";
46
+ description?: string;
47
+ enum?: any[];
48
+ const?: any;
49
+ minLength?: number;
50
+ maxLength?: number;
51
+ pattern?: string;
52
+ format?: string;
53
+ minimum?: number;
54
+ maximum?: number;
55
+ exclusiveMinimum?: number;
56
+ exclusiveMaximum?: number;
57
+ multipleOf?: number;
58
+ [key: string]: any;
59
+ }
60
+ /**
61
+ * Recursive JSON Schema type for tools
62
+ * Used when you can't provide Zod schemas (e.g., stuck on Zod 3.x)
63
+ */
64
+ export type ToolJSONSchema = JSONSchemaObject | JSONSchemaArray | JSONSchemaPrimitive;
65
+ /**
66
+ * FlinkToolProps - defines a Flink AI tool
67
+ *
68
+ * You can provide schemas in three ways (in order of precedence):
69
+ * 1. Zod 4.x schemas: `inputSchema` + `outputSchema` (manual Zod validation)
70
+ * 2. JSON schemas: `inputJsonSchema` + `outputJsonSchema` (manual JSON Schema validation)
71
+ * 3. TypeScript types: Auto-generated from FlinkTool type parameters (no manual schemas needed!)
72
+ *
73
+ * If no manual schemas are provided, the compiler will automatically extract schemas
74
+ * from the FlinkTool<Ctx, Input, Output> type parameters.
75
+ */
76
+ export interface FlinkToolProps {
77
+ /**
78
+ * Unique identifier for the tool (kebab-case)
79
+ * Used to reference the tool in agents and for registration
80
+ * Example: "get-cars-tool", "search-users-tool"
81
+ */
82
+ id: string;
83
+ description: string;
84
+ /**
85
+ * Zod schema for input validation and type inference
86
+ * Requires Zod 4.x for proper type inference
87
+ * OPTIONAL - if not provided, schema will be auto-generated from type parameters
88
+ *
89
+ * @example
90
+ * inputSchema: z.object({ name: z.string(), age: z.number() })
91
+ */
92
+ inputSchema?: z.ZodType<any>;
93
+ /**
94
+ * Optional Zod schema for output validation (validates the .data field)
95
+ * Requires Zod 4.x for proper type inference
96
+ * OPTIONAL - if not provided, schema will be auto-generated from type parameters
97
+ *
98
+ * @example
99
+ * outputSchema: z.string()
100
+ */
101
+ outputSchema?: z.ZodType<any>;
102
+ /**
103
+ * JSON Schema for input (alternative to inputSchema)
104
+ * Use this if you're stuck on Zod 3.x and can't upgrade
105
+ * OPTIONAL - if not provided, schema will be auto-generated from type parameters
106
+ * If both inputSchema and inputJsonSchema are provided, inputSchema takes precedence
107
+ */
108
+ inputJsonSchema?: ToolJSONSchema;
109
+ /**
110
+ * JSON Schema for output (alternative to outputSchema)
111
+ * Use this if you're stuck on Zod 3.x and can't upgrade
112
+ * OPTIONAL - if not provided, schema will be auto-generated from type parameters
113
+ * If both outputSchema and outputJsonSchema are provided, outputSchema takes precedence
114
+ */
115
+ outputJsonSchema?: ToolJSONSchema;
116
+ permissions?: string | string[] | ((input: any, user?: any) => boolean | Promise<boolean>);
117
+ }
118
+ export interface FlinkTool<Ctx extends FlinkContext, Input = any, Output = any, ConversationCtx = any> {
119
+ (params: {
120
+ input: Input;
121
+ ctx: Ctx;
122
+ user?: any;
123
+ permissions?: string[];
124
+ conversationCtx?: ConversationCtx;
125
+ }): Promise<ToolResult<Output>>;
126
+ }
127
+ export type FlinkToolFile = {
128
+ default: FlinkTool<any, any, any>;
129
+ Tool: FlinkToolProps;
130
+ /**
131
+ * Typescript source file path (relative to project root), set at compile time by Flink compiler.
132
+ * Used to look up tool metadata from schema-manifest.json at runtime.
133
+ */
134
+ __file?: string;
135
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,121 @@
1
+ import { FlinkContext } from "../FlinkContext";
2
+ import { ConversationAgent, ConversationData } from "./ConversationAgent";
3
+ import { AgentExecuteResult, AgentFinishContext } from "./FlinkAgent";
4
+ /**
5
+ * Opinionated base class for agents with in-memory conversation persistence.
6
+ *
7
+ * Provides ready-to-use Map-based storage for rapid prototyping and testing.
8
+ * Storage methods (loadConversation/saveConversation) are already implemented - just extend and configure!
9
+ *
10
+ * ⚠️ **WARNING**: Data is stored in memory and lost on process restart.
11
+ * Only use for:
12
+ * - Rapid prototyping
13
+ * - Development environments
14
+ * - Integration testing
15
+ * - Demo applications
16
+ *
17
+ * For production, extend `ConversationAgent` with persistent storage (MongoDB, Redis, etc.)
18
+ *
19
+ * @example Basic Usage
20
+ * ```typescript
21
+ * export default class MyAgent extends InMemoryConversationAgent<AppContext> {
22
+ * id = "my-agent";
23
+ * description = "Quick prototype agent";
24
+ * instructions = "You are a helpful assistant...";
25
+ * tools = ["search-knowledge"];
26
+ * // That's it! No loadConversation or saveConversation needed
27
+ * }
28
+ * ```
29
+ *
30
+ * @example With Custom Behavior
31
+ * ```typescript
32
+ * export default class CustomAgent extends InMemoryConversationAgent<AppContext> {
33
+ * id = "custom-agent";
34
+ * description = "Custom agent with hooks";
35
+ * instructions = "...";
36
+ *
37
+ * protected async afterRun(result, context) {
38
+ * await super.afterRun(result, context); // Still saves to memory
39
+ * // Add custom logic (logging, analytics, etc.)
40
+ * }
41
+ * }
42
+ * ```
43
+ *
44
+ * @example Testing
45
+ * ```typescript
46
+ * beforeEach(() => {
47
+ * InMemoryConversationAgent.clearAll(); // Reset between tests
48
+ * });
49
+ *
50
+ * it("should maintain conversation context", async () => {
51
+ * const agent = new MyAgent();
52
+ * await agent.execute({ message: "Hello", conversationId: "test-1" });
53
+ *
54
+ * expect(InMemoryConversationAgent.getConversationCount()).toBe(1);
55
+ * expect(InMemoryConversationAgent.getAllConversationIds()).toContain("test-1");
56
+ * });
57
+ * ```
58
+ *
59
+ * @template Context - Application context type extending FlinkContext
60
+ */
61
+ export declare abstract class InMemoryConversationAgent<Context extends FlinkContext = FlinkContext> extends ConversationAgent<Context> {
62
+ /**
63
+ * Load conversation from in-memory Map.
64
+ *
65
+ * @internal Implemented by InMemoryConversationAgent - no need to override
66
+ */
67
+ protected loadConversation(conversationId: string): Promise<ConversationData | null>;
68
+ /**
69
+ * Save conversation to in-memory Map.
70
+ *
71
+ * @internal Implemented by InMemoryConversationAgent - no need to override
72
+ */
73
+ protected saveConversation(conversationId: string, data: ConversationData, result: AgentExecuteResult, context: AgentFinishContext): Promise<void>;
74
+ /**
75
+ * Clear all stored conversations.
76
+ * Useful for testing and resetting state.
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * beforeEach(() => {
81
+ * InMemoryConversationAgent.clearAll();
82
+ * });
83
+ * ```
84
+ */
85
+ static clearAll(): void;
86
+ /**
87
+ * Get the number of stored conversations.
88
+ * Useful for testing and debugging.
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * expect(InMemoryConversationAgent.getConversationCount()).toBe(2);
93
+ * ```
94
+ */
95
+ static getConversationCount(): number;
96
+ /**
97
+ * Get a specific conversation by ID.
98
+ * Useful for testing and debugging.
99
+ *
100
+ * @param conversationId - The conversation ID to retrieve
101
+ * @returns Conversation data or undefined if not found
102
+ *
103
+ * @example
104
+ * ```typescript
105
+ * const conv = InMemoryConversationAgent.getConversation("conv-123");
106
+ * expect(conv?.messages.length).toBe(4);
107
+ * ```
108
+ */
109
+ static getConversation(conversationId: string): ConversationData | undefined;
110
+ /**
111
+ * Get all stored conversation IDs.
112
+ * Useful for testing and debugging.
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * const ids = InMemoryConversationAgent.getAllConversationIds();
117
+ * expect(ids).toContain("conv-123");
118
+ * ```
119
+ */
120
+ static getAllConversationIds(): string[];
121
+ }
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
28
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ Object.defineProperty(exports, "__esModule", { value: true });
54
+ exports.InMemoryConversationAgent = void 0;
55
+ var ConversationAgent_1 = require("./ConversationAgent");
56
+ /**
57
+ * In-memory conversation storage (development/testing only).
58
+ *
59
+ * ⚠️ WARNING: Data is lost when the process restarts.
60
+ * Use MongoDB, Redis, or another persistent backend for production.
61
+ */
62
+ var conversationStore = new Map();
63
+ /**
64
+ * Opinionated base class for agents with in-memory conversation persistence.
65
+ *
66
+ * Provides ready-to-use Map-based storage for rapid prototyping and testing.
67
+ * Storage methods (loadConversation/saveConversation) are already implemented - just extend and configure!
68
+ *
69
+ * ⚠️ **WARNING**: Data is stored in memory and lost on process restart.
70
+ * Only use for:
71
+ * - Rapid prototyping
72
+ * - Development environments
73
+ * - Integration testing
74
+ * - Demo applications
75
+ *
76
+ * For production, extend `ConversationAgent` with persistent storage (MongoDB, Redis, etc.)
77
+ *
78
+ * @example Basic Usage
79
+ * ```typescript
80
+ * export default class MyAgent extends InMemoryConversationAgent<AppContext> {
81
+ * id = "my-agent";
82
+ * description = "Quick prototype agent";
83
+ * instructions = "You are a helpful assistant...";
84
+ * tools = ["search-knowledge"];
85
+ * // That's it! No loadConversation or saveConversation needed
86
+ * }
87
+ * ```
88
+ *
89
+ * @example With Custom Behavior
90
+ * ```typescript
91
+ * export default class CustomAgent extends InMemoryConversationAgent<AppContext> {
92
+ * id = "custom-agent";
93
+ * description = "Custom agent with hooks";
94
+ * instructions = "...";
95
+ *
96
+ * protected async afterRun(result, context) {
97
+ * await super.afterRun(result, context); // Still saves to memory
98
+ * // Add custom logic (logging, analytics, etc.)
99
+ * }
100
+ * }
101
+ * ```
102
+ *
103
+ * @example Testing
104
+ * ```typescript
105
+ * beforeEach(() => {
106
+ * InMemoryConversationAgent.clearAll(); // Reset between tests
107
+ * });
108
+ *
109
+ * it("should maintain conversation context", async () => {
110
+ * const agent = new MyAgent();
111
+ * await agent.execute({ message: "Hello", conversationId: "test-1" });
112
+ *
113
+ * expect(InMemoryConversationAgent.getConversationCount()).toBe(1);
114
+ * expect(InMemoryConversationAgent.getAllConversationIds()).toContain("test-1");
115
+ * });
116
+ * ```
117
+ *
118
+ * @template Context - Application context type extending FlinkContext
119
+ */
120
+ var InMemoryConversationAgent = /** @class */ (function (_super) {
121
+ __extends(InMemoryConversationAgent, _super);
122
+ function InMemoryConversationAgent() {
123
+ return _super !== null && _super.apply(this, arguments) || this;
124
+ }
125
+ /**
126
+ * Load conversation from in-memory Map.
127
+ *
128
+ * @internal Implemented by InMemoryConversationAgent - no need to override
129
+ */
130
+ InMemoryConversationAgent.prototype.loadConversation = function (conversationId) {
131
+ return __awaiter(this, void 0, void 0, function () {
132
+ return __generator(this, function (_a) {
133
+ ConversationAgent_1.logger.debug("Loading conversation ".concat(conversationId));
134
+ return [2 /*return*/, conversationStore.get(conversationId) || null];
135
+ });
136
+ });
137
+ };
138
+ /**
139
+ * Save conversation to in-memory Map.
140
+ *
141
+ * @internal Implemented by InMemoryConversationAgent - no need to override
142
+ */
143
+ InMemoryConversationAgent.prototype.saveConversation = function (conversationId, data, result, context) {
144
+ return __awaiter(this, void 0, void 0, function () {
145
+ return __generator(this, function (_a) {
146
+ ConversationAgent_1.logger.debug("Saving ".concat(data.messages.length, " messages"));
147
+ conversationStore.set(conversationId, data);
148
+ return [2 /*return*/];
149
+ });
150
+ });
151
+ };
152
+ /**
153
+ * Clear all stored conversations.
154
+ * Useful for testing and resetting state.
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * beforeEach(() => {
159
+ * InMemoryConversationAgent.clearAll();
160
+ * });
161
+ * ```
162
+ */
163
+ InMemoryConversationAgent.clearAll = function () {
164
+ conversationStore.clear();
165
+ };
166
+ /**
167
+ * Get the number of stored conversations.
168
+ * Useful for testing and debugging.
169
+ *
170
+ * @example
171
+ * ```typescript
172
+ * expect(InMemoryConversationAgent.getConversationCount()).toBe(2);
173
+ * ```
174
+ */
175
+ InMemoryConversationAgent.getConversationCount = function () {
176
+ return conversationStore.size;
177
+ };
178
+ /**
179
+ * Get a specific conversation by ID.
180
+ * Useful for testing and debugging.
181
+ *
182
+ * @param conversationId - The conversation ID to retrieve
183
+ * @returns Conversation data or undefined if not found
184
+ *
185
+ * @example
186
+ * ```typescript
187
+ * const conv = InMemoryConversationAgent.getConversation("conv-123");
188
+ * expect(conv?.messages.length).toBe(4);
189
+ * ```
190
+ */
191
+ InMemoryConversationAgent.getConversation = function (conversationId) {
192
+ return conversationStore.get(conversationId);
193
+ };
194
+ /**
195
+ * Get all stored conversation IDs.
196
+ * Useful for testing and debugging.
197
+ *
198
+ * @example
199
+ * ```typescript
200
+ * const ids = InMemoryConversationAgent.getAllConversationIds();
201
+ * expect(ids).toContain("conv-123");
202
+ * ```
203
+ */
204
+ InMemoryConversationAgent.getAllConversationIds = function () {
205
+ return Array.from(conversationStore.keys());
206
+ };
207
+ return InMemoryConversationAgent;
208
+ }(ConversationAgent_1.ConversationAgent));
209
+ exports.InMemoryConversationAgent = InMemoryConversationAgent;
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Generic message format for LLM conversations
3
+ * Compatible with most LLM providers (Anthropic, OpenAI, etc.)
4
+ */
5
+ export type LLMMessage = {
6
+ role: "system";
7
+ content: string;
8
+ } | {
9
+ role: "user";
10
+ content: string | LLMContentBlock[];
11
+ } | {
12
+ role: "assistant";
13
+ content: string | LLMContentBlock[];
14
+ };
15
+ export type LLMContentBlock = {
16
+ type: "text";
17
+ text: string;
18
+ } | {
19
+ type: "image";
20
+ url: string;
21
+ } | {
22
+ type: "tool_use";
23
+ id: string;
24
+ name: string;
25
+ input: any;
26
+ } | {
27
+ type: "tool_result";
28
+ tool_use_id: string;
29
+ content: string;
30
+ is_error?: boolean;
31
+ };
32
+ /**
33
+ * Standard Flink tool schema format
34
+ * This is the format generated by ToolExecutor.getToolSchema()
35
+ * Adapters convert this to provider-specific formats
36
+ */
37
+ export interface FlinkToolSchema {
38
+ /**
39
+ * Tool name (kebab-case identifier)
40
+ */
41
+ name: string;
42
+ /**
43
+ * Human-readable description for the LLM
44
+ */
45
+ description: string;
46
+ /**
47
+ * JSON Schema for tool input validation
48
+ * Generated from Zod schema via z.toJSONSchema()
49
+ */
50
+ inputSchema: Record<string, any>;
51
+ }
52
+ /**
53
+ * LLM Adapter interface for provider abstraction
54
+ * Enables support for multiple AI providers (Anthropic, OpenAI, Mistral, etc.)
55
+ *
56
+ * All adapters use streaming as the primary method since:
57
+ * - All modern LLM providers support streaming
58
+ * - Better user experience (time-to-first-token <500ms)
59
+ * - Prevents HTTP timeouts for long responses
60
+ * - FlinkAgent's lazy generator pattern allows both streaming and awaiting final result
61
+ *
62
+ * @template ToolSchema - The tool schema format accepted by this adapter (defaults to FlinkToolSchema)
63
+ */
64
+ export interface LLMAdapter<ToolSchema = FlinkToolSchema> {
65
+ /**
66
+ * Optional debug flag to enable detailed logging
67
+ * When enabled, adapters will log:
68
+ * - Full request parameters sent to the LLM provider
69
+ * - Tool call decisions made by the LLM
70
+ * - Token usage and performance metrics
71
+ * - Error details and response metadata
72
+ *
73
+ * Useful for development and troubleshooting.
74
+ * Default: false
75
+ */
76
+ debug?: boolean;
77
+ /**
78
+ * Stream a completion with tool calling support
79
+ * Returns async generator for streaming responses
80
+ *
81
+ * Even if you want non-streaming UX, use this method and await the final result:
82
+ * ```typescript
83
+ * const response = agent.execute({ message: "Hello" });
84
+ * const result = await response.result; // Non-streaming consumption
85
+ * ```
86
+ */
87
+ stream(params: {
88
+ /**
89
+ * Instructions that define agent behavior and personality.
90
+ * Adapters should prepend this as a system message to the conversation.
91
+ * Following Vercel AI SDK pattern.
92
+ */
93
+ instructions: string;
94
+ messages: LLMMessage[];
95
+ tools: ToolSchema[];
96
+ maxTokens: number;
97
+ temperature: number;
98
+ /**
99
+ * Provider-specific metadata for tracking and future use
100
+ * Example: { openai: { responseId: "resp_123" } }
101
+ */
102
+ providerMetadata?: Record<string, any>;
103
+ }): AsyncGenerator<LLMStreamChunk>;
104
+ }
105
+ /**
106
+ * Token usage information from LLM providers
107
+ *
108
+ * Includes cache-related metrics for cost optimization and performance tracking:
109
+ * - cachedInputTokens: Tokens served from cache (OpenAI, Anthropic)
110
+ * - cacheCreationInputTokens: Tokens written to cache (Anthropic only)
111
+ *
112
+ * Cache economics:
113
+ * - OpenAI: Cached tokens cost ~10% of regular tokens
114
+ * - Anthropic: Cache reads cost 10%, cache writes cost 125% of regular tokens
115
+ */
116
+ export interface LLMUsage {
117
+ /** Total input tokens (including cached) */
118
+ inputTokens: number;
119
+ /** Total output tokens */
120
+ outputTokens: number;
121
+ /** Input tokens served from cache (lower cost) */
122
+ cachedInputTokens?: number;
123
+ /** Input tokens written to cache (Anthropic only, higher cost) */
124
+ cacheCreationInputTokens?: number;
125
+ }
126
+ /**
127
+ * Streaming chunk types
128
+ */
129
+ export type LLMStreamChunk = {
130
+ type: "text";
131
+ delta: string;
132
+ } | {
133
+ type: "tool_call";
134
+ toolCall: {
135
+ id: string;
136
+ name: string;
137
+ input: any;
138
+ };
139
+ } | {
140
+ type: "usage";
141
+ usage: LLMUsage;
142
+ } | {
143
+ type: "metadata";
144
+ metadata: Record<string, any>;
145
+ } | {
146
+ type: "done";
147
+ stopReason: string;
148
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });