@flink-app/flink 1.0.0 → 2.0.0-alpha.48

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 (109) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/cli/build.ts +8 -1
  3. package/cli/run.ts +8 -1
  4. package/dist/cli/build.js +8 -1
  5. package/dist/cli/run.js +8 -1
  6. package/dist/src/FlinkApp.d.ts +33 -0
  7. package/dist/src/FlinkApp.js +247 -27
  8. package/dist/src/FlinkContext.d.ts +21 -0
  9. package/dist/src/FlinkHttpHandler.d.ts +90 -1
  10. package/dist/src/TypeScriptCompiler.d.ts +42 -0
  11. package/dist/src/TypeScriptCompiler.js +346 -4
  12. package/dist/src/TypeScriptUtils.js +4 -0
  13. package/dist/src/ai/AgentRunner.d.ts +39 -0
  14. package/dist/src/ai/AgentRunner.js +625 -0
  15. package/dist/src/ai/FlinkAgent.d.ts +446 -0
  16. package/dist/src/ai/FlinkAgent.js +633 -0
  17. package/dist/src/ai/FlinkTool.d.ts +37 -0
  18. package/dist/src/ai/FlinkTool.js +2 -0
  19. package/dist/src/ai/LLMAdapter.d.ts +119 -0
  20. package/dist/src/ai/LLMAdapter.js +2 -0
  21. package/dist/src/ai/SubAgentExecutor.d.ts +36 -0
  22. package/dist/src/ai/SubAgentExecutor.js +220 -0
  23. package/dist/src/ai/ToolExecutor.d.ts +35 -0
  24. package/dist/src/ai/ToolExecutor.js +237 -0
  25. package/dist/src/ai/index.d.ts +5 -0
  26. package/dist/src/ai/index.js +21 -0
  27. package/dist/src/handlers/StreamWriterFactory.d.ts +20 -0
  28. package/dist/src/handlers/StreamWriterFactory.js +83 -0
  29. package/dist/src/index.d.ts +4 -0
  30. package/dist/src/index.js +4 -0
  31. package/dist/src/utils.d.ts +30 -0
  32. package/dist/src/utils.js +52 -0
  33. package/package.json +14 -2
  34. package/readme.md +425 -0
  35. package/spec/AgentDuplicateDetection.spec.ts +112 -0
  36. package/spec/AgentRunner.spec.ts +527 -0
  37. package/spec/ConversationHooks.spec.ts +290 -0
  38. package/spec/FlinkAgent.spec.ts +310 -0
  39. package/spec/FlinkApp.onError.spec.ts +1 -2
  40. package/spec/StreamingIntegration.spec.ts +138 -0
  41. package/spec/SubAgentSupport.spec.ts +941 -0
  42. package/spec/ToolExecutor.spec.ts +360 -0
  43. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCar.js +57 -0
  44. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCar2.js +59 -0
  45. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCarWithArraySchema.js +53 -0
  46. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCarWithArraySchema2.js +53 -0
  47. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCarWithArraySchema3.js +53 -0
  48. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCarWithLiteralSchema.js +55 -0
  49. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCarWithLiteralSchema2.js +55 -0
  50. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCarWithSchemaInFile.js +58 -0
  51. package/spec/mock-project/dist/spec/mock-project/src/handlers/GetCarWithSchemaInFile2.js +58 -0
  52. package/spec/mock-project/dist/spec/mock-project/src/handlers/ManuallyAddedHandler.js +53 -0
  53. package/spec/mock-project/dist/spec/mock-project/src/handlers/ManuallyAddedHandler2.js +55 -0
  54. package/spec/mock-project/dist/spec/mock-project/src/handlers/PatchCar.js +58 -0
  55. package/spec/mock-project/dist/spec/mock-project/src/handlers/PatchOnboardingSession.js +76 -0
  56. package/spec/mock-project/dist/spec/mock-project/src/handlers/PatchOrderWithComplexTypes.js +58 -0
  57. package/spec/mock-project/dist/spec/mock-project/src/handlers/PatchProductWithIntersection.js +59 -0
  58. package/spec/mock-project/dist/spec/mock-project/src/handlers/PatchUserWithUnion.js +59 -0
  59. package/spec/mock-project/dist/spec/mock-project/src/handlers/PostCar.js +55 -0
  60. package/spec/mock-project/dist/spec/mock-project/src/handlers/PostLogin.js +56 -0
  61. package/spec/mock-project/dist/spec/mock-project/src/handlers/PostLogout.js +55 -0
  62. package/spec/mock-project/dist/spec/mock-project/src/handlers/PutCar.js +55 -0
  63. package/spec/mock-project/dist/spec/mock-project/src/index.js +83 -0
  64. package/spec/mock-project/dist/spec/mock-project/src/repos/CarRepo.js +26 -0
  65. package/spec/mock-project/dist/spec/mock-project/src/schemas/Car.js +2 -0
  66. package/spec/mock-project/dist/spec/mock-project/src/schemas/DefaultExportSchema.js +2 -0
  67. package/spec/mock-project/dist/spec/mock-project/src/schemas/FileWithTwoSchemas.js +2 -0
  68. package/spec/mock-project/dist/src/FlinkApp.js +1012 -0
  69. package/spec/mock-project/dist/src/FlinkContext.js +2 -0
  70. package/spec/mock-project/dist/src/FlinkErrors.js +143 -0
  71. package/spec/mock-project/dist/src/FlinkHttpHandler.js +47 -0
  72. package/spec/mock-project/dist/src/FlinkJob.js +2 -0
  73. package/spec/mock-project/dist/src/FlinkLog.js +26 -0
  74. package/spec/mock-project/dist/src/FlinkPlugin.js +2 -0
  75. package/spec/mock-project/dist/src/FlinkRepo.js +224 -0
  76. package/spec/mock-project/dist/src/FlinkResponse.js +2 -0
  77. package/spec/mock-project/dist/src/ai/AgentExecutor.js +279 -0
  78. package/spec/mock-project/dist/src/ai/AgentRunner.js +625 -0
  79. package/spec/mock-project/dist/src/ai/FlinkAgent.js +633 -0
  80. package/spec/mock-project/dist/src/ai/FlinkTool.js +2 -0
  81. package/spec/mock-project/dist/src/ai/LLMAdapter.js +2 -0
  82. package/spec/mock-project/dist/src/ai/SubAgentExecutor.js +220 -0
  83. package/spec/mock-project/dist/src/ai/ToolExecutor.js +237 -0
  84. package/spec/mock-project/dist/src/auth/FlinkAuthPlugin.js +2 -0
  85. package/spec/mock-project/dist/src/auth/FlinkAuthUser.js +2 -0
  86. package/spec/mock-project/dist/src/handlers/StreamWriterFactory.js +83 -0
  87. package/spec/mock-project/dist/src/index.js +17 -69
  88. package/spec/mock-project/dist/src/mock-data-generator.js +9 -0
  89. package/spec/mock-project/dist/src/utils.js +290 -0
  90. package/spec/mock-project/tsconfig.json +6 -1
  91. package/spec/testHelpers.ts +49 -0
  92. package/spec/utils.caseConversion.spec.ts +80 -0
  93. package/spec/utils.spec.ts +13 -13
  94. package/src/FlinkApp.ts +251 -7
  95. package/src/FlinkContext.ts +22 -0
  96. package/src/FlinkHttpHandler.ts +100 -2
  97. package/src/TypeScriptCompiler.ts +398 -7
  98. package/src/TypeScriptUtils.ts +5 -0
  99. package/src/ai/AgentRunner.ts +549 -0
  100. package/src/ai/FlinkAgent.ts +770 -0
  101. package/src/ai/FlinkTool.ts +40 -0
  102. package/src/ai/LLMAdapter.ts +96 -0
  103. package/src/ai/SubAgentExecutor.ts +199 -0
  104. package/src/ai/ToolExecutor.ts +193 -0
  105. package/src/ai/index.ts +5 -0
  106. package/src/handlers/StreamWriterFactory.ts +84 -0
  107. package/src/index.ts +4 -0
  108. package/src/utils.ts +52 -0
  109. package/tsconfig.json +6 -1
@@ -0,0 +1,138 @@
1
+ import { FlinkAgent, StreamChunk } from "../src/ai/FlinkAgent";
2
+ import { FlinkContext } from "../src/FlinkContext";
3
+ import { LLMAdapter, LLMStreamChunk } from "../src/ai/LLMAdapter";
4
+
5
+ describe("Streaming Integration", () => {
6
+ let mockCtx: FlinkContext;
7
+
8
+ beforeEach(() => {
9
+ mockCtx = {
10
+ repos: {},
11
+ plugins: {},
12
+ };
13
+ });
14
+
15
+ class StreamingTestAgent extends FlinkAgent<FlinkContext> {
16
+ description = "Test agent for streaming";
17
+ instructions = "You are a test agent";
18
+ tools = [];
19
+
20
+ async query(message: string) {
21
+ const response = this.execute({ message });
22
+ return response;
23
+ }
24
+
25
+ setContext(ctx: FlinkContext) {
26
+ (this as any).ctx = ctx;
27
+ }
28
+
29
+ setMockLLMAdapter(adapter: LLMAdapter) {
30
+ this.__init(new Map([["default", adapter]]), {});
31
+ }
32
+ }
33
+
34
+ it("should stream text deltas in real-time", async () => {
35
+ const agent = new StreamingTestAgent();
36
+ agent.setContext(mockCtx);
37
+
38
+ // Mock LLM that streams chunks
39
+ const mockAdapter: LLMAdapter = {
40
+ stream: async function* () {
41
+ yield { type: "text", delta: "Hello " } as LLMStreamChunk;
42
+ yield { type: "text", delta: "world " } as LLMStreamChunk;
43
+ yield { type: "text", delta: "from agent" } as LLMStreamChunk;
44
+ yield { type: "usage", usage: { inputTokens: 10, outputTokens: 5 } } as LLMStreamChunk;
45
+ yield { type: "done", stopReason: "end_turn" } as LLMStreamChunk;
46
+ },
47
+ };
48
+
49
+ agent.setMockLLMAdapter(mockAdapter);
50
+
51
+ const response = agent.query("Hello");
52
+
53
+ const textChunks: string[] = [];
54
+ for await (const text of (await response).textStream) {
55
+ textChunks.push(text);
56
+ }
57
+
58
+ expect(textChunks.length).toBe(3);
59
+ expect(textChunks.join("")).toBe("Hello world from agent");
60
+ });
61
+
62
+ it("should emit text_delta events during streaming", async () => {
63
+ const agent = new StreamingTestAgent();
64
+ agent.setContext(mockCtx);
65
+
66
+ const mockAdapter: LLMAdapter = {
67
+ stream: async function* () {
68
+ yield { type: "text", delta: "Pro" } as LLMStreamChunk;
69
+ yield { type: "text", delta: "gressive " } as LLMStreamChunk;
70
+ yield { type: "text", delta: "text" } as LLMStreamChunk;
71
+ yield { type: "usage", usage: { inputTokens: 10, outputTokens: 5 } } as LLMStreamChunk;
72
+ yield { type: "done", stopReason: "end_turn" } as LLMStreamChunk;
73
+ },
74
+ };
75
+
76
+ agent.setMockLLMAdapter(mockAdapter);
77
+
78
+ const response = agent.query("Hello");
79
+
80
+ const events: StreamChunk[] = [];
81
+ for await (const chunk of (await response).fullStream) {
82
+ events.push(chunk);
83
+ }
84
+
85
+ const textDeltas = events.filter((e) => e.type === "text_delta");
86
+ expect(textDeltas.length).toBe(3);
87
+ expect((textDeltas[0] as any).delta).toBe("Pro");
88
+ expect((textDeltas[1] as any).delta).toBe("gressive ");
89
+ expect((textDeltas[2] as any).delta).toBe("text");
90
+ });
91
+
92
+ it("should allow consuming result after streaming completes", async () => {
93
+ const agent = new StreamingTestAgent();
94
+ agent.setContext(mockCtx);
95
+
96
+ const mockAdapter: LLMAdapter = {
97
+ stream: async function* () {
98
+ yield { type: "text", delta: "Complete " } as LLMStreamChunk;
99
+ yield { type: "text", delta: "response" } as LLMStreamChunk;
100
+ yield { type: "usage", usage: { inputTokens: 10, outputTokens: 5 } } as LLMStreamChunk;
101
+ yield { type: "done", stopReason: "end_turn" } as LLMStreamChunk;
102
+ },
103
+ };
104
+
105
+ agent.setMockLLMAdapter(mockAdapter);
106
+
107
+ const response = agent.query("Hello");
108
+
109
+ // Consume stream first
110
+ const textChunks: string[] = [];
111
+ for await (const text of (await response).textStream) {
112
+ textChunks.push(text);
113
+ }
114
+
115
+ // Then await result
116
+ const result = await (await response).result;
117
+
118
+ expect(result.message).toContain("Complete response");
119
+ expect(textChunks.join("")).toBe("Complete response");
120
+ });
121
+
122
+ it("should handle streaming errors gracefully", async () => {
123
+ const agent = new StreamingTestAgent();
124
+ agent.setContext(mockCtx);
125
+
126
+ const mockAdapter: LLMAdapter = {
127
+ stream: async function* () {
128
+ throw new Error("Streaming failed");
129
+ },
130
+ };
131
+
132
+ agent.setMockLLMAdapter(mockAdapter);
133
+
134
+ const response = agent.query("Hello");
135
+
136
+ await expectAsync((await response).result).toBeRejectedWithError("Streaming failed");
137
+ });
138
+ });