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