@copilotkit/runtime 1.10.6 → 1.50.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/dist/index.d.ts +1655 -27
  2. package/dist/index.js +2172 -5049
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.mjs +5441 -99
  5. package/dist/index.mjs.map +1 -1
  6. package/dist/v2/index.d.ts +1 -0
  7. package/dist/v2/index.js +15 -0
  8. package/dist/v2/index.js.map +1 -0
  9. package/dist/v2/index.mjs +4 -0
  10. package/dist/v2/index.mjs.map +1 -0
  11. package/package.json +33 -21
  12. package/src/graphql/message-conversion/agui-to-gql.test.ts +1263 -0
  13. package/src/graphql/message-conversion/agui-to-gql.ts +333 -0
  14. package/src/graphql/message-conversion/gql-to-agui.test.ts +1578 -0
  15. package/src/graphql/message-conversion/gql-to-agui.ts +278 -0
  16. package/src/graphql/message-conversion/index.ts +2 -0
  17. package/src/graphql/message-conversion/roundtrip-conversion.test.ts +526 -0
  18. package/src/graphql/resolvers/copilot.resolver.ts +3 -48
  19. package/src/graphql/resolvers/state.resolver.ts +3 -2
  20. package/src/graphql/types/converted/index.ts +32 -6
  21. package/src/graphql/types/enums.ts +2 -2
  22. package/src/graphql/types/message-status.type.ts +3 -1
  23. package/src/lib/index.ts +1 -1
  24. package/src/lib/integrations/nextjs/app-router.ts +10 -11
  25. package/src/lib/integrations/nextjs/pages-router.ts +4 -11
  26. package/src/lib/integrations/node-http/index.ts +64 -5
  27. package/src/lib/integrations/shared.ts +1 -1
  28. package/src/lib/observability.ts +87 -0
  29. package/src/lib/runtime/{langgraph/langgraph-agent.ts → agent-integrations/langgraph.agent.ts} +5 -0
  30. package/src/lib/runtime/copilot-runtime.ts +346 -1333
  31. package/src/lib/runtime/types.ts +49 -0
  32. package/src/lib/runtime/utils.ts +87 -0
  33. package/src/lib/telemetry-client.ts +6 -5
  34. package/src/service-adapters/anthropic/anthropic-adapter.ts +5 -1
  35. package/src/service-adapters/bedrock/bedrock-adapter.ts +6 -1
  36. package/src/service-adapters/empty/empty-adapter.ts +3 -0
  37. package/src/service-adapters/events.ts +0 -254
  38. package/src/service-adapters/experimental/ollama/ollama-adapter.ts +5 -1
  39. package/src/service-adapters/google/google-genai-adapter.ts +7 -1
  40. package/src/service-adapters/groq/groq-adapter.ts +5 -1
  41. package/src/service-adapters/langchain/langchain-adapter.ts +3 -0
  42. package/src/service-adapters/openai/openai-adapter.ts +5 -1
  43. package/src/service-adapters/openai/openai-assistant-adapter.ts +4 -0
  44. package/src/service-adapters/service-adapter.ts +3 -0
  45. package/src/service-adapters/unify/unify-adapter.ts +6 -1
  46. package/src/v2/index.ts +2 -0
  47. package/tsup.config.ts +2 -1
  48. package/dist/chunk-27JKTS6P.mjs +0 -1704
  49. package/dist/chunk-27JKTS6P.mjs.map +0 -1
  50. package/dist/chunk-2OZAGFV3.mjs +0 -43
  51. package/dist/chunk-2OZAGFV3.mjs.map +0 -1
  52. package/dist/chunk-AMUJQ6IR.mjs +0 -50
  53. package/dist/chunk-AMUJQ6IR.mjs.map +0 -1
  54. package/dist/chunk-CEOMFPJU.mjs +0 -6020
  55. package/dist/chunk-CEOMFPJU.mjs.map +0 -1
  56. package/dist/chunk-DTPRUTNV.mjs +0 -25
  57. package/dist/chunk-DTPRUTNV.mjs.map +0 -1
  58. package/dist/chunk-FHD4JECV.mjs +0 -33
  59. package/dist/chunk-FHD4JECV.mjs.map +0 -1
  60. package/dist/chunk-I27F2UPA.mjs +0 -175
  61. package/dist/chunk-I27F2UPA.mjs.map +0 -1
  62. package/dist/chunk-LPEPX6NH.mjs +0 -25
  63. package/dist/chunk-LPEPX6NH.mjs.map +0 -1
  64. package/dist/chunk-PTYRVXXP.mjs +0 -80
  65. package/dist/chunk-PTYRVXXP.mjs.map +0 -1
  66. package/dist/chunk-SHBDMA63.mjs +0 -141
  67. package/dist/chunk-SHBDMA63.mjs.map +0 -1
  68. package/dist/chunk-XWBDEXDA.mjs +0 -153
  69. package/dist/chunk-XWBDEXDA.mjs.map +0 -1
  70. package/dist/graphql/types/base/index.d.ts +0 -6
  71. package/dist/graphql/types/base/index.js +0 -63
  72. package/dist/graphql/types/base/index.js.map +0 -1
  73. package/dist/graphql/types/base/index.mjs +0 -8
  74. package/dist/graphql/types/base/index.mjs.map +0 -1
  75. package/dist/graphql/types/converted/index.d.ts +0 -2
  76. package/dist/graphql/types/converted/index.js +0 -200
  77. package/dist/graphql/types/converted/index.js.map +0 -1
  78. package/dist/graphql/types/converted/index.mjs +0 -19
  79. package/dist/graphql/types/converted/index.mjs.map +0 -1
  80. package/dist/groq-adapter-c8aec5c5.d.ts +0 -321
  81. package/dist/index-96b330da.d.ts +0 -119
  82. package/dist/langserve-0c6100e3.d.ts +0 -257
  83. package/dist/lib/cloud/index.d.ts +0 -6
  84. package/dist/lib/cloud/index.js +0 -18
  85. package/dist/lib/cloud/index.js.map +0 -1
  86. package/dist/lib/cloud/index.mjs +0 -1
  87. package/dist/lib/cloud/index.mjs.map +0 -1
  88. package/dist/lib/index.d.ts +0 -212
  89. package/dist/lib/index.js +0 -7843
  90. package/dist/lib/index.js.map +0 -1
  91. package/dist/lib/index.mjs +0 -76
  92. package/dist/lib/index.mjs.map +0 -1
  93. package/dist/lib/integrations/index.d.ts +0 -34
  94. package/dist/lib/integrations/index.js +0 -3052
  95. package/dist/lib/integrations/index.js.map +0 -1
  96. package/dist/lib/integrations/index.mjs +0 -37
  97. package/dist/lib/integrations/index.mjs.map +0 -1
  98. package/dist/lib/integrations/nest/index.d.ts +0 -15
  99. package/dist/lib/integrations/nest/index.js +0 -2959
  100. package/dist/lib/integrations/nest/index.js.map +0 -1
  101. package/dist/lib/integrations/nest/index.mjs +0 -14
  102. package/dist/lib/integrations/nest/index.mjs.map +0 -1
  103. package/dist/lib/integrations/node-express/index.d.ts +0 -15
  104. package/dist/lib/integrations/node-express/index.js +0 -2959
  105. package/dist/lib/integrations/node-express/index.js.map +0 -1
  106. package/dist/lib/integrations/node-express/index.mjs +0 -14
  107. package/dist/lib/integrations/node-express/index.mjs.map +0 -1
  108. package/dist/lib/integrations/node-http/index.d.ts +0 -15
  109. package/dist/lib/integrations/node-http/index.js +0 -2945
  110. package/dist/lib/integrations/node-http/index.js.map +0 -1
  111. package/dist/lib/integrations/node-http/index.mjs +0 -13
  112. package/dist/lib/integrations/node-http/index.mjs.map +0 -1
  113. package/dist/service-adapters/index.d.ts +0 -162
  114. package/dist/service-adapters/index.js +0 -1787
  115. package/dist/service-adapters/index.js.map +0 -1
  116. package/dist/service-adapters/index.mjs +0 -34
  117. package/dist/service-adapters/index.mjs.map +0 -1
  118. package/dist/service-adapters/shared/index.d.ts +0 -9
  119. package/dist/service-adapters/shared/index.js +0 -72
  120. package/dist/service-adapters/shared/index.js.map +0 -1
  121. package/dist/service-adapters/shared/index.mjs +0 -8
  122. package/dist/service-adapters/shared/index.mjs.map +0 -1
  123. package/dist/shared-0a7346ce.d.ts +0 -466
  124. package/dist/utils/index.d.ts +0 -65
  125. package/dist/utils/index.js +0 -175
  126. package/dist/utils/index.js.map +0 -1
  127. package/dist/utils/index.mjs +0 -12
  128. package/dist/utils/index.mjs.map +0 -1
  129. package/src/lib/runtime/__tests__/remote-action-constructors.test.ts +0 -246
  130. package/src/lib/runtime/agui-action.ts +0 -180
  131. package/src/lib/runtime/remote-action-constructors.ts +0 -331
  132. package/src/lib/runtime/remote-actions.ts +0 -217
  133. package/src/lib/runtime/remote-lg-action.ts +0 -1006
@@ -0,0 +1,1578 @@
1
+ import { describe, test, expect, vi } from "vitest";
2
+ import * as gql from "../types/converted/index";
3
+ import { MessageStatusCode } from "../types/message-status.type";
4
+ import {
5
+ gqlToAGUI,
6
+ gqlTextMessageToAGUIMessage,
7
+ gqlResultMessageToAGUIMessage,
8
+ gqlImageMessageToAGUIMessage,
9
+ gqlActionExecutionMessageToAGUIMessage,
10
+ } from "./gql-to-agui";
11
+ import { AIMessage } from "@copilotkit/shared";
12
+
13
+ describe("message-conversion", () => {
14
+ describe("gqlTextMessageToAGUIMessage", () => {
15
+ test("should convert developer message", () => {
16
+ const gqlMessage = new gql.TextMessage({
17
+ id: "dev-message-id",
18
+ content: "Hello from developer",
19
+ role: gql.Role.developer,
20
+ });
21
+
22
+ const result = gqlTextMessageToAGUIMessage(gqlMessage);
23
+
24
+ expect(result).toEqual({
25
+ id: "dev-message-id",
26
+ role: "developer",
27
+ content: "Hello from developer",
28
+ });
29
+ });
30
+
31
+ test("should convert system message", () => {
32
+ const gqlMessage = new gql.TextMessage({
33
+ id: "system-message-id",
34
+ content: "System instruction",
35
+ role: gql.Role.system,
36
+ });
37
+
38
+ const result = gqlTextMessageToAGUIMessage(gqlMessage);
39
+
40
+ expect(result).toEqual({
41
+ id: "system-message-id",
42
+ role: "system",
43
+ content: "System instruction",
44
+ });
45
+ });
46
+
47
+ test("should convert assistant message", () => {
48
+ const gqlMessage = new gql.TextMessage({
49
+ id: "assistant-message-id",
50
+ content: "Assistant response",
51
+ role: gql.Role.assistant,
52
+ });
53
+
54
+ const result = gqlTextMessageToAGUIMessage(gqlMessage);
55
+
56
+ expect(result).toEqual({
57
+ id: "assistant-message-id",
58
+ role: "assistant",
59
+ content: "Assistant response",
60
+ });
61
+ });
62
+
63
+ test("should throw error for unknown role", () => {
64
+ const gqlMessage = new gql.TextMessage({
65
+ id: "unknown-message-id",
66
+ content: "Unknown message",
67
+ role: "unknown" as any,
68
+ });
69
+
70
+ expect(() => gqlTextMessageToAGUIMessage(gqlMessage)).toThrow("Unknown message role");
71
+ });
72
+ });
73
+
74
+ describe("gqlResultMessageToAGUIMessage", () => {
75
+ test("should convert result message to tool message", () => {
76
+ const gqlMessage = new gql.ResultMessage({
77
+ id: "result-id",
78
+ result: "Function result data",
79
+ actionExecutionId: "action-exec-123",
80
+ actionName: "testAction",
81
+ });
82
+
83
+ const result = gqlResultMessageToAGUIMessage(gqlMessage);
84
+
85
+ expect(result).toEqual({
86
+ id: "result-id",
87
+ role: "tool",
88
+ content: "Function result data",
89
+ toolCallId: "action-exec-123",
90
+ toolName: "testAction",
91
+ });
92
+ });
93
+ });
94
+
95
+ describe("gqlToAGUI", () => {
96
+ test("should convert an array of text messages", () => {
97
+ const gqlMessages = [
98
+ new gql.TextMessage({
99
+ id: "dev-1",
100
+ content: "Hello",
101
+ role: gql.Role.developer,
102
+ }),
103
+ new gql.TextMessage({
104
+ id: "assistant-1",
105
+ content: "Hi there",
106
+ role: gql.Role.assistant,
107
+ }),
108
+ ];
109
+
110
+ const result = gqlToAGUI(gqlMessages);
111
+
112
+ expect(result).toHaveLength(2);
113
+ expect(result[0]).toEqual({
114
+ id: "dev-1",
115
+ role: "developer",
116
+ content: "Hello",
117
+ });
118
+ expect(result[1]).toEqual({
119
+ id: "assistant-1",
120
+ role: "assistant",
121
+ content: "Hi there",
122
+ });
123
+ });
124
+
125
+ test("should handle agent state messages", () => {
126
+ const gqlMessages = [new gql.AgentStateMessage({ id: "agent-state-1" })];
127
+
128
+ const result = gqlToAGUI(gqlMessages);
129
+
130
+ expect(result).toHaveLength(1);
131
+ expect(result[0]).toEqual({
132
+ id: "agent-state-1",
133
+ role: "assistant",
134
+ });
135
+ });
136
+
137
+ // test("should throw error for unknown message type", () => {
138
+ // // Create a message with unknown type
139
+ // const unknownMessage = new gql.Message({ id: "unknown-1" });
140
+ // // Override the type checking methods to simulate unknown type
141
+ // unknownMessage.isTextMessage = () => false as any;
142
+ // unknownMessage.isResultMessage = () => false as any;
143
+ // unknownMessage.isActionExecutionMessage = () => false as any;
144
+ // unknownMessage.isAgentStateMessage = () => false as any;
145
+ // unknownMessage.isImageMessage = () => false as any;
146
+
147
+ // expect(() => gqlToAGUI([unknownMessage])).toThrow("Unknown message type");
148
+ // });
149
+
150
+ test("should handle a mix of message types", () => {
151
+ const gqlMessages = [
152
+ new gql.TextMessage({
153
+ id: "dev-1",
154
+ content: "Run action",
155
+ role: gql.Role.developer,
156
+ }),
157
+ new gql.TextMessage({
158
+ id: "assistant-1",
159
+ content: "I'll run the action",
160
+ role: gql.Role.assistant,
161
+ }),
162
+ new gql.ResultMessage({
163
+ id: "result-1",
164
+ result: "Action result",
165
+ actionExecutionId: "action-exec-1",
166
+ actionName: "testAction",
167
+ }),
168
+ ];
169
+
170
+ const result = gqlToAGUI(gqlMessages);
171
+
172
+ expect(result).toHaveLength(3);
173
+ expect(result[0]).toEqual({
174
+ id: "dev-1",
175
+ role: "developer",
176
+ content: "Run action",
177
+ });
178
+ expect(result[1]).toEqual({
179
+ id: "assistant-1",
180
+ role: "assistant",
181
+ content: "I'll run the action",
182
+ });
183
+ expect(result[2]).toEqual({
184
+ id: "result-1",
185
+ role: "tool",
186
+ content: "Action result",
187
+ toolCallId: "action-exec-1",
188
+ toolName: "testAction",
189
+ });
190
+ });
191
+
192
+ test("should handle action execution messages with parent messages", () => {
193
+ const assistantMsg = new gql.TextMessage({
194
+ id: "assistant-1",
195
+ content: "I'll execute an action",
196
+ role: gql.Role.assistant,
197
+ });
198
+
199
+ const actionExecMsg = new gql.ActionExecutionMessage({
200
+ id: "action-1",
201
+ name: "testAction",
202
+ arguments: { param: "value" },
203
+ parentMessageId: "assistant-1",
204
+ });
205
+
206
+ const result = gqlToAGUI([assistantMsg, actionExecMsg]);
207
+
208
+ // Now we expect 2 messages: the original assistant message and the action execution message
209
+ expect(result).toHaveLength(2);
210
+ expect(result[0]).toEqual({
211
+ id: "assistant-1",
212
+ role: "assistant",
213
+ content: "I'll execute an action",
214
+ });
215
+ expect(result[1]).toEqual({
216
+ id: "action-1",
217
+ role: "assistant",
218
+ name: "testAction",
219
+ toolCalls: [
220
+ {
221
+ id: "action-1",
222
+ function: {
223
+ name: "testAction",
224
+ arguments: JSON.stringify({ param: "value" }),
225
+ },
226
+ type: "function",
227
+ },
228
+ ],
229
+ });
230
+ });
231
+
232
+ test("should handle multiple action execution messages for the same parent", () => {
233
+ const assistantMsg = new gql.TextMessage({
234
+ id: "assistant-1",
235
+ content: "I'll execute multiple actions",
236
+ role: gql.Role.assistant,
237
+ });
238
+
239
+ const action1 = new gql.ActionExecutionMessage({
240
+ id: "action-1",
241
+ name: "firstAction",
242
+ arguments: { param: "value1" },
243
+ parentMessageId: "assistant-1",
244
+ });
245
+
246
+ const action2 = new gql.ActionExecutionMessage({
247
+ id: "action-2",
248
+ name: "secondAction",
249
+ arguments: { param: "value2" },
250
+ parentMessageId: "assistant-1",
251
+ });
252
+
253
+ const result = gqlToAGUI([assistantMsg, action1, action2]);
254
+
255
+ // Now we expect 3 messages: the original assistant message and 2 separate action execution messages
256
+ expect(result).toHaveLength(3);
257
+ expect(result[0]).toEqual({
258
+ id: "assistant-1",
259
+ role: "assistant",
260
+ content: "I'll execute multiple actions",
261
+ });
262
+ expect(result[1]).toEqual({
263
+ id: "action-1",
264
+ role: "assistant",
265
+ name: "firstAction",
266
+ toolCalls: [
267
+ {
268
+ id: "action-1",
269
+ function: {
270
+ name: "firstAction",
271
+ arguments: JSON.stringify({ param: "value1" }),
272
+ },
273
+ type: "function",
274
+ },
275
+ ],
276
+ });
277
+ expect(result[2]).toEqual({
278
+ id: "action-2",
279
+ role: "assistant",
280
+ name: "secondAction",
281
+ toolCalls: [
282
+ {
283
+ id: "action-2",
284
+ function: {
285
+ name: "secondAction",
286
+ arguments: JSON.stringify({ param: "value2" }),
287
+ },
288
+ type: "function",
289
+ },
290
+ ],
291
+ });
292
+ });
293
+
294
+ test("should not add toolCalls to non-assistant messages", () => {
295
+ const developerMsg = new gql.TextMessage({
296
+ id: "dev-1",
297
+ content: "Developer message",
298
+ role: gql.Role.developer,
299
+ });
300
+
301
+ const actionExecMsg = new gql.ActionExecutionMessage({
302
+ id: "action-1",
303
+ name: "testAction",
304
+ arguments: { param: "value" },
305
+ parentMessageId: "dev-1", // This should be ignored since parent is not assistant
306
+ });
307
+
308
+ const result = gqlToAGUI([developerMsg, actionExecMsg]);
309
+
310
+ // Now we expect 2 messages: the developer message and the action execution as assistant message
311
+ expect(result).toHaveLength(2);
312
+ expect(result[0]).toEqual({
313
+ id: "dev-1",
314
+ role: "developer",
315
+ content: "Developer message",
316
+ });
317
+ // The action execution becomes its own assistant message regardless of parent
318
+ expect(result[1]).toEqual({
319
+ id: "action-1",
320
+ role: "assistant",
321
+ name: "testAction",
322
+ toolCalls: [
323
+ {
324
+ id: "action-1",
325
+ function: {
326
+ name: "testAction",
327
+ arguments: JSON.stringify({ param: "value" }),
328
+ },
329
+ type: "function",
330
+ },
331
+ ],
332
+ });
333
+ });
334
+
335
+ test("should handle action execution messages without actions context", () => {
336
+ const actionExecMsg = new gql.ActionExecutionMessage({
337
+ id: "action-1",
338
+ name: "testAction",
339
+ arguments: { param: "value" },
340
+ });
341
+
342
+ const result = gqlToAGUI([actionExecMsg]);
343
+
344
+ expect(result).toHaveLength(1);
345
+ expect(result[0]).toEqual({
346
+ id: "action-1",
347
+ role: "assistant",
348
+ name: "testAction",
349
+ toolCalls: [
350
+ {
351
+ id: "action-1",
352
+ function: {
353
+ name: "testAction",
354
+ arguments: JSON.stringify({ param: "value" }),
355
+ },
356
+ type: "function",
357
+ },
358
+ ],
359
+ });
360
+ // Should not have render functions without actions context
361
+ expect(result[0]).not.toHaveProperty("render");
362
+ expect(result[0]).not.toHaveProperty("renderAndWaitForResponse");
363
+ });
364
+
365
+ test("should handle action execution messages with actions context and render functions", () => {
366
+ const actionExecMsg = new gql.ActionExecutionMessage({
367
+ id: "action-1",
368
+ name: "testAction",
369
+ arguments: { param: "value" },
370
+ status: { code: MessageStatusCode.Pending },
371
+ });
372
+
373
+ const mockRender = vi.fn();
374
+ const mockRenderAndWaitForResponse = (props: any) => "Test Render With Response";
375
+
376
+ const actions = {
377
+ testAction: {
378
+ name: "testAction",
379
+ render: mockRender,
380
+ renderAndWaitForResponse: mockRenderAndWaitForResponse,
381
+ },
382
+ };
383
+
384
+ const result = gqlToAGUI([actionExecMsg], actions);
385
+
386
+ expect(result).toHaveLength(1);
387
+ expect(result[0]).toMatchObject({
388
+ id: "action-1",
389
+ role: "assistant",
390
+ name: "testAction",
391
+ content: "",
392
+ toolCalls: [
393
+ {
394
+ id: "action-1",
395
+ function: {
396
+ name: "testAction",
397
+ arguments: JSON.stringify({ param: "value" }),
398
+ },
399
+ type: "function",
400
+ },
401
+ ],
402
+ });
403
+
404
+ // Should have generativeUI function
405
+ expect(result[0]).toHaveProperty("generativeUI");
406
+ expect(typeof (result[0] as any).generativeUI).toBe("function");
407
+ });
408
+
409
+ test("should provide correct status in generativeUI function props", () => {
410
+ const actionExecMsg = new gql.ActionExecutionMessage({
411
+ id: "action-1",
412
+ name: "testAction",
413
+ arguments: { param: "value" },
414
+ status: { code: MessageStatusCode.Pending },
415
+ });
416
+
417
+ const mockRender = vi.fn();
418
+ const actions = {
419
+ testAction: {
420
+ name: "testAction",
421
+ render: mockRender,
422
+ },
423
+ };
424
+
425
+ const result = gqlToAGUI([actionExecMsg], actions);
426
+
427
+ // Call the generativeUI function
428
+ (result[0] as any).generativeUI?.();
429
+
430
+ expect(mockRender).toHaveBeenCalledWith({
431
+ status: "inProgress",
432
+ args: { param: "value" },
433
+ result: undefined,
434
+ respond: expect.any(Function),
435
+ messageId: "action-1",
436
+ // Regular actions should NOT have the name property
437
+ });
438
+ });
439
+
440
+ test("should provide executing status when not pending", () => {
441
+ const actionExecMsg = new gql.ActionExecutionMessage({
442
+ id: "action-1",
443
+ name: "testAction",
444
+ arguments: { param: "value" },
445
+ status: { code: MessageStatusCode.Success },
446
+ });
447
+
448
+ const mockRender = vi.fn();
449
+ const actions = {
450
+ testAction: {
451
+ name: "testAction",
452
+ render: mockRender,
453
+ },
454
+ };
455
+
456
+ const result = gqlToAGUI([actionExecMsg], actions);
457
+
458
+ // Call the generativeUI function
459
+ (result[0] as any).generativeUI?.();
460
+
461
+ expect(mockRender).toHaveBeenCalledWith({
462
+ status: "executing",
463
+ args: { param: "value" },
464
+ result: undefined,
465
+ respond: expect.any(Function),
466
+ messageId: "action-1",
467
+ // Regular actions should NOT have the name property
468
+ });
469
+ });
470
+
471
+ test("should provide complete status when result is available", () => {
472
+ const actionExecMsg = new gql.ActionExecutionMessage({
473
+ id: "action-1",
474
+ name: "testAction",
475
+ arguments: { param: "value" },
476
+ status: { code: MessageStatusCode.Success },
477
+ });
478
+
479
+ const resultMsg = new gql.ResultMessage({
480
+ id: "result-1",
481
+ result: "Action completed successfully",
482
+ actionExecutionId: "action-1",
483
+ actionName: "testAction",
484
+ });
485
+
486
+ const mockRender = vi.fn();
487
+ const actions = {
488
+ testAction: {
489
+ name: "testAction",
490
+ render: mockRender,
491
+ },
492
+ };
493
+
494
+ const result = gqlToAGUI([actionExecMsg, resultMsg], actions);
495
+
496
+ // Find the action execution message result (not the tool result)
497
+ const actionMessage = result.find((msg) => msg.role === "assistant" && "toolCalls" in msg);
498
+
499
+ // Call the generativeUI function
500
+ (actionMessage as any)?.generativeUI?.();
501
+
502
+ expect(mockRender).toHaveBeenCalledWith({
503
+ status: "complete",
504
+ args: { param: "value" },
505
+ result: "Action completed successfully",
506
+ respond: expect.any(Function),
507
+ messageId: "action-1",
508
+ // Regular actions should NOT have the name property
509
+ });
510
+ });
511
+
512
+ test("should handle generativeUI function props override", () => {
513
+ const actionExecMsg = new gql.ActionExecutionMessage({
514
+ id: "action-1",
515
+ name: "testAction",
516
+ arguments: { param: "value" },
517
+ status: { code: MessageStatusCode.Pending },
518
+ });
519
+
520
+ const mockRender = vi.fn();
521
+ const actions = {
522
+ testAction: {
523
+ name: "testAction",
524
+ render: mockRender,
525
+ },
526
+ };
527
+
528
+ const result = gqlToAGUI([actionExecMsg], actions);
529
+
530
+ // Call with custom props
531
+ (result[0] as any).generativeUI?.({
532
+ status: "custom",
533
+ customProp: "test",
534
+ respond: () => "custom respond",
535
+ });
536
+
537
+ expect(mockRender).toHaveBeenCalledWith({
538
+ status: "custom",
539
+ args: { param: "value" },
540
+ result: undefined,
541
+ respond: expect.any(Function),
542
+ customProp: "test",
543
+ messageId: "action-1",
544
+ // Regular actions should NOT have the name property
545
+ });
546
+ });
547
+
548
+ test("should handle missing render functions gracefully", () => {
549
+ const actionExecMsg = new gql.ActionExecutionMessage({
550
+ id: "action-1",
551
+ name: "testAction",
552
+ arguments: { param: "value" },
553
+ });
554
+
555
+ const actions = {
556
+ testAction: {
557
+ name: "testAction",
558
+ // No render functions provided
559
+ },
560
+ };
561
+
562
+ const result = gqlToAGUI([actionExecMsg], actions);
563
+
564
+ expect(result[0]).toMatchObject({
565
+ id: "action-1",
566
+ role: "assistant",
567
+ name: "testAction",
568
+ content: "",
569
+ toolCalls: [
570
+ {
571
+ id: "action-1",
572
+ function: {
573
+ name: "testAction",
574
+ arguments: JSON.stringify({ param: "value" }),
575
+ },
576
+ type: "function",
577
+ },
578
+ ],
579
+ });
580
+
581
+ // Should have undefined generativeUI functions
582
+ expect((result[0] as any).generativeUI).toBeUndefined();
583
+ });
584
+
585
+ test("should handle action not found in actions context", () => {
586
+ const actionExecMsg = new gql.ActionExecutionMessage({
587
+ id: "action-1",
588
+ name: "unknownAction",
589
+ arguments: { param: "value" },
590
+ });
591
+
592
+ const actions = {
593
+ testAction: {
594
+ name: "testAction",
595
+ render: () => "Test",
596
+ },
597
+ };
598
+
599
+ const result = gqlToAGUI([actionExecMsg], actions);
600
+
601
+ expect(result).toHaveLength(1);
602
+ expect(result[0]).toEqual({
603
+ id: "action-1",
604
+ role: "assistant",
605
+ name: "unknownAction",
606
+ toolCalls: [
607
+ {
608
+ id: "action-1",
609
+ function: {
610
+ name: "unknownAction",
611
+ arguments: JSON.stringify({ param: "value" }),
612
+ },
613
+ type: "function",
614
+ },
615
+ ],
616
+ });
617
+
618
+ // Should not have generativeUI functions when action not found
619
+ expect(result[0]).not.toHaveProperty("generativeUI");
620
+ });
621
+
622
+ test("should handle agent state messages with coAgentStateRenders", () => {
623
+ const agentStateMsg = new gql.AgentStateMessage({
624
+ id: "agent-state-1",
625
+ agentName: "testAgent",
626
+ state: { status: "running", data: "test data" },
627
+ role: gql.Role.assistant,
628
+ });
629
+
630
+ const mockRender = vi.fn();
631
+ const coAgentStateRenders = {
632
+ testAgent: {
633
+ name: "testAgent",
634
+ render: mockRender,
635
+ },
636
+ };
637
+
638
+ const result = gqlToAGUI([agentStateMsg], undefined, coAgentStateRenders);
639
+
640
+ expect(result).toHaveLength(1);
641
+ expect(result[0]).toEqual({
642
+ id: "agent-state-1",
643
+ role: "assistant",
644
+ agentName: "testAgent",
645
+ state: { status: "running", data: "test data" },
646
+ generativeUI: expect.any(Function),
647
+ });
648
+
649
+ // Should have generativeUI function
650
+ expect(result[0]).toHaveProperty("generativeUI");
651
+ expect(typeof (result[0] as any).generativeUI).toBe("function");
652
+
653
+ // Call the generativeUI function
654
+ (result[0] as any).generativeUI?.();
655
+
656
+ expect(mockRender).toHaveBeenCalledWith({
657
+ state: { status: "running", data: "test data" },
658
+ });
659
+ });
660
+
661
+ test("should handle agent state messages without coAgentStateRenders", () => {
662
+ const agentStateMsg = new gql.AgentStateMessage({
663
+ id: "agent-state-1",
664
+ agentName: "testAgent",
665
+ state: { status: "running", data: "test data" },
666
+ role: gql.Role.assistant,
667
+ });
668
+
669
+ const result = gqlToAGUI([agentStateMsg]);
670
+
671
+ expect(result).toHaveLength(1);
672
+ expect(result[0]).toEqual({
673
+ id: "agent-state-1",
674
+ role: "assistant",
675
+ agentName: "testAgent",
676
+ state: { status: "running", data: "test data" },
677
+ });
678
+
679
+ // Should not have generativeUI functions without coAgentStateRenders
680
+ expect(result[0]).not.toHaveProperty("generativeUI");
681
+ });
682
+
683
+ test("should handle agent state messages with agent not found in coAgentStateRenders", () => {
684
+ const agentStateMsg = new gql.AgentStateMessage({
685
+ id: "agent-state-1",
686
+ agentName: "unknownAgent",
687
+ state: { status: "running", data: "test data" },
688
+ role: gql.Role.assistant,
689
+ });
690
+
691
+ const coAgentStateRenders = {
692
+ testAgent: {
693
+ name: "testAgent",
694
+ render: () => "Test",
695
+ },
696
+ };
697
+
698
+ const result = gqlToAGUI([agentStateMsg], undefined, coAgentStateRenders);
699
+
700
+ expect(result).toHaveLength(1);
701
+ expect(result[0]).toEqual({
702
+ id: "agent-state-1",
703
+ role: "assistant",
704
+ agentName: "unknownAgent",
705
+ state: { status: "running", data: "test data" },
706
+ });
707
+
708
+ // Should not have generativeUI functions when agent not found
709
+ expect(result[0]).not.toHaveProperty("generativeUI");
710
+ });
711
+
712
+ test("should handle user role messages", () => {
713
+ const userMsg = new gql.TextMessage({
714
+ id: "user-1",
715
+ content: "Hello from user",
716
+ role: gql.Role.user,
717
+ });
718
+
719
+ const result = gqlToAGUI([userMsg]);
720
+
721
+ expect(result).toHaveLength(1);
722
+ expect(result[0]).toEqual({
723
+ id: "user-1",
724
+ role: "user",
725
+ content: "Hello from user",
726
+ });
727
+ });
728
+
729
+ test("should handle mixed message types including agent state messages", () => {
730
+ const textMsg = new gql.TextMessage({
731
+ id: "text-1",
732
+ content: "Hello",
733
+ role: gql.Role.assistant,
734
+ });
735
+
736
+ const agentStateMsg = new gql.AgentStateMessage({
737
+ id: "agent-state-1",
738
+ agentName: "testAgent",
739
+ state: { status: "running" },
740
+ role: gql.Role.assistant,
741
+ });
742
+
743
+ const mockRender = vi.fn();
744
+ const coAgentStateRenders = {
745
+ testAgent: {
746
+ name: "testAgent",
747
+ render: mockRender,
748
+ },
749
+ };
750
+
751
+ const result = gqlToAGUI([textMsg, agentStateMsg], undefined, coAgentStateRenders);
752
+
753
+ expect(result).toHaveLength(2);
754
+ expect(result[0]).toEqual({
755
+ id: "text-1",
756
+ role: "assistant",
757
+ content: "Hello",
758
+ });
759
+ expect(result[1]).toMatchObject({
760
+ id: "agent-state-1",
761
+ role: "assistant",
762
+ agentName: "testAgent",
763
+ state: { status: "running" },
764
+ generativeUI: expect.any(Function),
765
+ });
766
+ expect(result[1]).toHaveProperty("generativeUI");
767
+ });
768
+ });
769
+
770
+ describe("gqlImageMessageToAGUIMessage", () => {
771
+ test("should throw error for invalid image format", () => {
772
+ const invalidImageMsg = new gql.ImageMessage({
773
+ id: "img-1",
774
+ format: "bmp", // not in VALID_IMAGE_FORMATS
775
+ bytes: "somebase64string",
776
+ role: gql.Role.user,
777
+ });
778
+ expect(() => gqlImageMessageToAGUIMessage(invalidImageMsg)).toThrow("Invalid image format");
779
+ });
780
+
781
+ test("should throw error for empty image bytes", () => {
782
+ const invalidImageMsg = new gql.ImageMessage({
783
+ id: "img-2",
784
+ format: "jpeg",
785
+ bytes: "",
786
+ role: gql.Role.user,
787
+ });
788
+ expect(() => gqlImageMessageToAGUIMessage(invalidImageMsg)).toThrow(
789
+ "Image bytes must be a non-empty string",
790
+ );
791
+ });
792
+
793
+ test("should convert valid image message", () => {
794
+ const validImageMsg = new gql.ImageMessage({
795
+ id: "img-3",
796
+ format: "jpeg",
797
+ bytes: "somebase64string",
798
+ role: gql.Role.user,
799
+ });
800
+ const result = gqlImageMessageToAGUIMessage(validImageMsg);
801
+ expect(result).toMatchObject({
802
+ id: "img-3",
803
+ role: "user",
804
+ content: "",
805
+ image: {
806
+ format: "jpeg",
807
+ bytes: "somebase64string",
808
+ },
809
+ });
810
+ });
811
+
812
+ test("should convert valid user image message", () => {
813
+ const validImageMsg = new gql.ImageMessage({
814
+ id: "img-user-1",
815
+ format: "jpeg",
816
+ bytes: "userbase64string",
817
+ role: gql.Role.user,
818
+ });
819
+ const result = gqlImageMessageToAGUIMessage(validImageMsg);
820
+ expect(result).toMatchObject({
821
+ id: "img-user-1",
822
+ role: "user",
823
+ content: "",
824
+ image: {
825
+ format: "jpeg",
826
+ bytes: "userbase64string",
827
+ },
828
+ });
829
+ });
830
+
831
+ test("should convert valid assistant image message", () => {
832
+ const validImageMsg = new gql.ImageMessage({
833
+ id: "img-assistant-1",
834
+ format: "png",
835
+ bytes: "assistantbase64string",
836
+ role: gql.Role.assistant,
837
+ });
838
+ const result = gqlImageMessageToAGUIMessage(validImageMsg);
839
+ expect(result).toMatchObject({
840
+ id: "img-assistant-1",
841
+ role: "assistant",
842
+ content: "",
843
+ image: {
844
+ format: "png",
845
+ bytes: "assistantbase64string",
846
+ },
847
+ });
848
+ });
849
+ });
850
+
851
+ describe("Wild Card Actions", () => {
852
+ test("should handle action execution with specific action", () => {
853
+ const actions = {
854
+ testAction: {
855
+ name: "testAction",
856
+ render: vi.fn((props) => `Rendered: ${props.args.test}`),
857
+ },
858
+ };
859
+
860
+ const actionExecMsg = new gql.ActionExecutionMessage({
861
+ id: "action-1",
862
+ name: "testAction",
863
+ arguments: { test: "value" },
864
+ parentMessageId: "parent-1",
865
+ });
866
+
867
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions);
868
+
869
+ expect(result).toMatchObject({
870
+ id: "action-1",
871
+ role: "assistant",
872
+ content: "",
873
+ toolCalls: [
874
+ {
875
+ id: "action-1",
876
+ function: {
877
+ name: "testAction",
878
+ arguments: '{"test":"value"}',
879
+ },
880
+ type: "function",
881
+ },
882
+ ],
883
+ generativeUI: expect.any(Function),
884
+ name: "testAction",
885
+ });
886
+ });
887
+
888
+ test("should handle action execution with wild card action", () => {
889
+ const actions = {
890
+ "*": {
891
+ name: "*",
892
+ render: vi.fn((props) => `Wildcard rendered: ${props.args.test}`),
893
+ },
894
+ };
895
+
896
+ const actionExecMsg = new gql.ActionExecutionMessage({
897
+ id: "action-2",
898
+ name: "unknownAction",
899
+ arguments: { test: "wildcard-value" },
900
+ parentMessageId: "parent-2",
901
+ });
902
+
903
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions);
904
+
905
+ expect(result).toMatchObject({
906
+ id: "action-2",
907
+ role: "assistant",
908
+ content: "",
909
+ toolCalls: [
910
+ {
911
+ id: "action-2",
912
+ function: {
913
+ name: "unknownAction",
914
+ arguments: '{"test":"wildcard-value"}',
915
+ },
916
+ type: "function",
917
+ },
918
+ ],
919
+ generativeUI: expect.any(Function),
920
+ name: "unknownAction",
921
+ });
922
+ });
923
+
924
+ test("should pass tool name to wildcard action render function", () => {
925
+ const mockRender = vi.fn(
926
+ (props) => `Wildcard rendered: ${props.name} with args: ${JSON.stringify(props.args)}`,
927
+ );
928
+ const actions = {
929
+ "*": {
930
+ name: "*",
931
+ render: mockRender,
932
+ },
933
+ };
934
+
935
+ const actionExecMsg = new gql.ActionExecutionMessage({
936
+ id: "action-wildcard-name",
937
+ name: "testTool",
938
+ arguments: { param: "value" },
939
+ parentMessageId: "parent-wildcard-name",
940
+ });
941
+
942
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions);
943
+
944
+ // Call the generativeUI function to trigger the render
945
+ (result as any).generativeUI?.();
946
+
947
+ // Verify that the render function was called with the name property
948
+ expect(mockRender).toHaveBeenCalledWith(
949
+ expect.objectContaining({
950
+ name: "testTool",
951
+ args: { param: "value" },
952
+ }),
953
+ );
954
+ });
955
+
956
+ test("should pass tool name to regular action render function", () => {
957
+ const mockRender = vi.fn((props) => `Regular action rendered: ${JSON.stringify(props.args)}`);
958
+ const actions = {
959
+ testAction: {
960
+ name: "testAction",
961
+ render: mockRender,
962
+ },
963
+ };
964
+
965
+ const actionExecMsg = new gql.ActionExecutionMessage({
966
+ id: "action-regular-name",
967
+ name: "testAction",
968
+ arguments: { param: "value" },
969
+ parentMessageId: "parent-regular-name",
970
+ });
971
+
972
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions);
973
+
974
+ // Call the generativeUI function to trigger the render
975
+ (result as any).generativeUI?.();
976
+
977
+ // Verify that the render function was called with the correct props
978
+ // Regular actions should NOT have the name property
979
+ expect(mockRender).toHaveBeenCalledWith(
980
+ expect.objectContaining({
981
+ args: { param: "value" },
982
+ // name property should NOT be present for regular actions
983
+ }),
984
+ );
985
+
986
+ // Verify that the name property is NOT present
987
+ const callArgs = mockRender.mock.calls[0][0];
988
+ expect(callArgs).not.toHaveProperty("name");
989
+ });
990
+
991
+ test("should prioritize specific action over wild card action", () => {
992
+ const actions = {
993
+ specificAction: {
994
+ name: "specificAction",
995
+ render: vi.fn((props) => "Specific action rendered"),
996
+ },
997
+ "*": {
998
+ name: "*",
999
+ render: vi.fn((props) => "Wildcard action rendered"),
1000
+ },
1001
+ };
1002
+
1003
+ const actionExecMsg = new gql.ActionExecutionMessage({
1004
+ id: "action-3",
1005
+ name: "specificAction",
1006
+ arguments: { test: "value" },
1007
+ parentMessageId: "parent-3",
1008
+ });
1009
+
1010
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions);
1011
+
1012
+ expect(result).toMatchObject({
1013
+ id: "action-3",
1014
+ role: "assistant",
1015
+ content: "",
1016
+ toolCalls: [
1017
+ {
1018
+ id: "action-3",
1019
+ function: {
1020
+ name: "specificAction",
1021
+ arguments: '{"test":"value"}',
1022
+ },
1023
+ type: "function",
1024
+ },
1025
+ ],
1026
+ generativeUI: expect.any(Function),
1027
+ name: "specificAction",
1028
+ });
1029
+ });
1030
+
1031
+ test("should handle action execution without any matching actions", () => {
1032
+ const actions = {
1033
+ otherAction: {
1034
+ name: "otherAction",
1035
+ render: vi.fn(),
1036
+ },
1037
+ };
1038
+
1039
+ const actionExecMsg = new gql.ActionExecutionMessage({
1040
+ id: "action-4",
1041
+ name: "unmatchedAction",
1042
+ arguments: { test: "value" },
1043
+ parentMessageId: "parent-4",
1044
+ });
1045
+
1046
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions);
1047
+
1048
+ expect(result).toMatchObject({
1049
+ id: "action-4",
1050
+ role: "assistant",
1051
+ toolCalls: [
1052
+ {
1053
+ id: "action-4",
1054
+ function: {
1055
+ name: "unmatchedAction",
1056
+ arguments: '{"test":"value"}',
1057
+ },
1058
+ type: "function",
1059
+ },
1060
+ ],
1061
+ name: "unmatchedAction",
1062
+ });
1063
+ expect(result).not.toHaveProperty("generativeUI");
1064
+ });
1065
+
1066
+ test("should handle action execution with no actions provided", () => {
1067
+ const actionExecMsg = new gql.ActionExecutionMessage({
1068
+ id: "action-5",
1069
+ name: "anyAction",
1070
+ arguments: { test: "value" },
1071
+ parentMessageId: "parent-5",
1072
+ });
1073
+
1074
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg);
1075
+
1076
+ expect(result).toMatchObject({
1077
+ id: "action-5",
1078
+ role: "assistant",
1079
+ toolCalls: [
1080
+ {
1081
+ id: "action-5",
1082
+ function: {
1083
+ name: "anyAction",
1084
+ arguments: '{"test":"value"}',
1085
+ },
1086
+ type: "function",
1087
+ },
1088
+ ],
1089
+ name: "anyAction",
1090
+ });
1091
+ expect(result).not.toHaveProperty("generativeUI");
1092
+ });
1093
+
1094
+ test("should handle action execution with completed result", () => {
1095
+ const actions = {
1096
+ "*": {
1097
+ name: "*",
1098
+ render: vi.fn((props) => `Result: ${props.result}`),
1099
+ },
1100
+ };
1101
+
1102
+ const actionExecMsg = new gql.ActionExecutionMessage({
1103
+ id: "action-6",
1104
+ name: "testAction",
1105
+ arguments: { test: "value" },
1106
+ parentMessageId: "parent-6",
1107
+ });
1108
+
1109
+ const actionResults = new Map([["action-6", "completed result"]]);
1110
+
1111
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions, actionResults);
1112
+
1113
+ expect(result).toMatchObject({
1114
+ id: "action-6",
1115
+ role: "assistant",
1116
+ content: "",
1117
+ toolCalls: [
1118
+ {
1119
+ id: "action-6",
1120
+ function: {
1121
+ name: "testAction",
1122
+ arguments: '{"test":"value"}',
1123
+ },
1124
+ type: "function",
1125
+ },
1126
+ ],
1127
+ generativeUI: expect.any(Function),
1128
+ name: "testAction",
1129
+ });
1130
+ });
1131
+
1132
+ test("should handle action execution with executing status", () => {
1133
+ const actions = {
1134
+ "*": {
1135
+ name: "*",
1136
+ render: vi.fn((props) => `Status: ${props.status}`),
1137
+ },
1138
+ };
1139
+
1140
+ const actionExecMsg = new gql.ActionExecutionMessage({
1141
+ id: "action-7",
1142
+ name: "testAction",
1143
+ arguments: { test: "value" },
1144
+ parentMessageId: "parent-7",
1145
+ status: { code: MessageStatusCode.Success },
1146
+ });
1147
+
1148
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions);
1149
+
1150
+ expect(result).toMatchObject({
1151
+ id: "action-7",
1152
+ role: "assistant",
1153
+ content: "",
1154
+ toolCalls: [
1155
+ {
1156
+ id: "action-7",
1157
+ function: {
1158
+ name: "testAction",
1159
+ arguments: '{"test":"value"}',
1160
+ },
1161
+ type: "function",
1162
+ },
1163
+ ],
1164
+ generativeUI: expect.any(Function),
1165
+ name: "testAction",
1166
+ });
1167
+ });
1168
+
1169
+ test("should handle action execution with pending status", () => {
1170
+ const actions = {
1171
+ "*": {
1172
+ name: "*",
1173
+ render: vi.fn((props) => `Status: ${props.status}`),
1174
+ },
1175
+ };
1176
+
1177
+ const actionExecMsg = new gql.ActionExecutionMessage({
1178
+ id: "action-8",
1179
+ name: "testAction",
1180
+ arguments: { test: "value" },
1181
+ parentMessageId: "parent-8",
1182
+ status: { code: MessageStatusCode.Pending },
1183
+ });
1184
+
1185
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions);
1186
+
1187
+ expect(result).toMatchObject({
1188
+ id: "action-8",
1189
+ role: "assistant",
1190
+ content: "",
1191
+ toolCalls: [
1192
+ {
1193
+ id: "action-8",
1194
+ function: {
1195
+ name: "testAction",
1196
+ arguments: '{"test":"value"}',
1197
+ },
1198
+ type: "function",
1199
+ },
1200
+ ],
1201
+ generativeUI: expect.any(Function),
1202
+ name: "testAction",
1203
+ });
1204
+ });
1205
+
1206
+ test("should handle action execution with failed status", () => {
1207
+ const actions = {
1208
+ "*": {
1209
+ name: "*",
1210
+ render: vi.fn((props) => `Status: ${props.status}`),
1211
+ },
1212
+ };
1213
+
1214
+ const actionExecMsg = new gql.ActionExecutionMessage({
1215
+ id: "action-9",
1216
+ name: "testAction",
1217
+ arguments: { test: "value" },
1218
+ parentMessageId: "parent-9",
1219
+ status: { code: MessageStatusCode.Failed },
1220
+ });
1221
+
1222
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions);
1223
+
1224
+ expect(result).toMatchObject({
1225
+ id: "action-9",
1226
+ role: "assistant",
1227
+ content: "",
1228
+ toolCalls: [
1229
+ {
1230
+ id: "action-9",
1231
+ function: {
1232
+ name: "testAction",
1233
+ arguments: '{"test":"value"}',
1234
+ },
1235
+ type: "function",
1236
+ },
1237
+ ],
1238
+ generativeUI: expect.any(Function),
1239
+ name: "testAction",
1240
+ });
1241
+ });
1242
+
1243
+ test("should handle action execution with undefined status", () => {
1244
+ const actions = {
1245
+ "*": {
1246
+ name: "*",
1247
+ render: vi.fn((props) => `Status: ${props.status}`),
1248
+ },
1249
+ };
1250
+
1251
+ const actionExecMsg = new gql.ActionExecutionMessage({
1252
+ id: "action-10",
1253
+ name: "testAction",
1254
+ arguments: { test: "value" },
1255
+ parentMessageId: "parent-10",
1256
+ // No status field
1257
+ });
1258
+
1259
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions);
1260
+
1261
+ expect(result).toMatchObject({
1262
+ id: "action-10",
1263
+ role: "assistant",
1264
+ content: "",
1265
+ toolCalls: [
1266
+ {
1267
+ id: "action-10",
1268
+ function: {
1269
+ name: "testAction",
1270
+ arguments: '{"test":"value"}',
1271
+ },
1272
+ type: "function",
1273
+ },
1274
+ ],
1275
+ generativeUI: expect.any(Function),
1276
+ name: "testAction",
1277
+ });
1278
+ });
1279
+
1280
+ test("should handle action execution with empty arguments", () => {
1281
+ const actions = {
1282
+ "*": {
1283
+ name: "*",
1284
+ render: vi.fn((props) => `Args: ${JSON.stringify(props.args)}`),
1285
+ },
1286
+ };
1287
+
1288
+ const actionExecMsg = new gql.ActionExecutionMessage({
1289
+ id: "action-11",
1290
+ name: "testAction",
1291
+ arguments: {},
1292
+ parentMessageId: "parent-11",
1293
+ });
1294
+
1295
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions);
1296
+
1297
+ expect(result).toMatchObject({
1298
+ id: "action-11",
1299
+ role: "assistant",
1300
+ content: "",
1301
+ toolCalls: [
1302
+ {
1303
+ id: "action-11",
1304
+ function: {
1305
+ name: "testAction",
1306
+ arguments: "{}",
1307
+ },
1308
+ type: "function",
1309
+ },
1310
+ ],
1311
+ generativeUI: expect.any(Function),
1312
+ name: "testAction",
1313
+ });
1314
+ });
1315
+
1316
+ test("should handle action execution with null arguments", () => {
1317
+ const actions = {
1318
+ "*": {
1319
+ name: "*",
1320
+ render: vi.fn((props) => `Args: ${JSON.stringify(props.args)}`),
1321
+ },
1322
+ };
1323
+
1324
+ const actionExecMsg = new gql.ActionExecutionMessage({
1325
+ id: "action-12",
1326
+ name: "testAction",
1327
+ arguments: null as any,
1328
+ parentMessageId: "parent-12",
1329
+ });
1330
+
1331
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions);
1332
+
1333
+ expect(result).toMatchObject({
1334
+ id: "action-12",
1335
+ role: "assistant",
1336
+ content: "",
1337
+ toolCalls: [
1338
+ {
1339
+ id: "action-12",
1340
+ function: {
1341
+ name: "testAction",
1342
+ arguments: "null",
1343
+ },
1344
+ type: "function",
1345
+ },
1346
+ ],
1347
+ generativeUI: expect.any(Function),
1348
+ name: "testAction",
1349
+ });
1350
+ });
1351
+
1352
+ test("should handle action execution with complex nested arguments", () => {
1353
+ const actions = {
1354
+ "*": {
1355
+ name: "*",
1356
+ render: vi.fn((props) => `Complex: ${JSON.stringify(props.args)}`),
1357
+ },
1358
+ };
1359
+
1360
+ const complexArgs = {
1361
+ nested: {
1362
+ array: [1, 2, 3],
1363
+ object: { key: "value" },
1364
+ nullValue: null,
1365
+ undefinedValue: undefined,
1366
+ },
1367
+ string: "test",
1368
+ number: 42,
1369
+ boolean: true,
1370
+ };
1371
+
1372
+ const actionExecMsg = new gql.ActionExecutionMessage({
1373
+ id: "action-13",
1374
+ name: "testAction",
1375
+ arguments: complexArgs,
1376
+ parentMessageId: "parent-13",
1377
+ });
1378
+
1379
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions);
1380
+
1381
+ expect(result).toMatchObject({
1382
+ id: "action-13",
1383
+ role: "assistant",
1384
+ content: "",
1385
+ toolCalls: [
1386
+ {
1387
+ id: "action-13",
1388
+ function: {
1389
+ name: "testAction",
1390
+ arguments: JSON.stringify(complexArgs),
1391
+ },
1392
+ type: "function",
1393
+ },
1394
+ ],
1395
+ generativeUI: expect.any(Function),
1396
+ name: "testAction",
1397
+ });
1398
+ });
1399
+
1400
+ test("should handle multiple wild card actions (should use first one)", () => {
1401
+ const actions = {
1402
+ wildcard1: {
1403
+ name: "*",
1404
+ render: vi.fn((props) => "First wildcard"),
1405
+ },
1406
+ wildcard2: {
1407
+ name: "*",
1408
+ render: vi.fn((props) => "Second wildcard"),
1409
+ },
1410
+ };
1411
+
1412
+ const actionExecMsg = new gql.ActionExecutionMessage({
1413
+ id: "action-14",
1414
+ name: "unknownAction",
1415
+ arguments: { test: "value" },
1416
+ parentMessageId: "parent-14",
1417
+ });
1418
+
1419
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions);
1420
+
1421
+ expect(result).toMatchObject({
1422
+ id: "action-14",
1423
+ role: "assistant",
1424
+ content: "",
1425
+ toolCalls: [
1426
+ {
1427
+ id: "action-14",
1428
+ function: {
1429
+ name: "unknownAction",
1430
+ arguments: '{"test":"value"}',
1431
+ },
1432
+ type: "function",
1433
+ },
1434
+ ],
1435
+ generativeUI: expect.any(Function),
1436
+ name: "unknownAction",
1437
+ });
1438
+ });
1439
+
1440
+ test("should parse string results in generativeUI props", () => {
1441
+ const actions = {
1442
+ "*": {
1443
+ name: "*",
1444
+ render: vi.fn((props) => `Result: ${JSON.stringify(props.result)}`),
1445
+ },
1446
+ };
1447
+
1448
+ const actionExecMsg = new gql.ActionExecutionMessage({
1449
+ id: "action-string-result",
1450
+ name: "stringResultAction",
1451
+ arguments: { test: "value" },
1452
+ parentMessageId: "parent-string",
1453
+ });
1454
+
1455
+ const actionResults = new Map<string, string>();
1456
+ actionResults.set("action-string-result", '{"parsed": true, "value": 42}');
1457
+
1458
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions, actionResults);
1459
+
1460
+ expect((result as AIMessage).generativeUI).toBeDefined();
1461
+ // Call the render function to test the result parsing
1462
+ const renderResult = (result as AIMessage).generativeUI!({
1463
+ result: '{"from": "props", "data": "test"}',
1464
+ });
1465
+
1466
+ // Verify the render function was called and the result was parsed
1467
+ expect(actions["*"].render).toHaveBeenCalledWith(
1468
+ expect.objectContaining({
1469
+ result: { from: "props", data: "test" }, // Should be parsed from string
1470
+ args: { test: "value" },
1471
+ status: "complete",
1472
+ messageId: "action-string-result",
1473
+ }),
1474
+ );
1475
+ });
1476
+
1477
+ test("should handle malformed JSON strings gracefully in results", () => {
1478
+ const actions = {
1479
+ "*": {
1480
+ name: "*",
1481
+ render: vi.fn((props) => `Result: ${JSON.stringify(props.result)}`),
1482
+ },
1483
+ };
1484
+
1485
+ const actionExecMsg = new gql.ActionExecutionMessage({
1486
+ id: "action-malformed",
1487
+ name: "malformedAction",
1488
+ arguments: { test: "value" },
1489
+ parentMessageId: "parent-malformed",
1490
+ });
1491
+
1492
+ const actionResults = new Map<string, string>();
1493
+ actionResults.set("action-malformed", "invalid json {");
1494
+
1495
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions, actionResults);
1496
+
1497
+ expect((result as AIMessage).generativeUI).toBeDefined();
1498
+ // Call the render function to test malformed JSON handling
1499
+ const renderResult = (result as AIMessage).generativeUI!({
1500
+ result: "invalid json {",
1501
+ });
1502
+
1503
+ // Verify the render function was called with the original string (unparsed)
1504
+ expect(actions["*"].render).toHaveBeenCalledWith(
1505
+ expect.objectContaining({
1506
+ result: "invalid json {", // Should remain as string due to parse error
1507
+ args: { test: "value" },
1508
+ status: "complete",
1509
+ messageId: "action-malformed",
1510
+ }),
1511
+ );
1512
+ });
1513
+
1514
+ test("should handle non-string results without parsing", () => {
1515
+ const actions = {
1516
+ "*": {
1517
+ name: "*",
1518
+ render: vi.fn((props) => `Result: ${JSON.stringify(props.result)}`),
1519
+ },
1520
+ };
1521
+
1522
+ const actionExecMsg = new gql.ActionExecutionMessage({
1523
+ id: "action-object-result",
1524
+ name: "objectResultAction",
1525
+ arguments: { test: "value" },
1526
+ parentMessageId: "parent-object",
1527
+ });
1528
+
1529
+ const actionResults = new Map<string, string>();
1530
+ actionResults.set("action-object-result", '{"already": "parsed"}');
1531
+
1532
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg, actions, actionResults);
1533
+
1534
+ expect((result as AIMessage).generativeUI).toBeDefined();
1535
+ // Call the render function with an object result
1536
+ const renderResult = (result as AIMessage).generativeUI!({
1537
+ result: { from: "props", data: "object" },
1538
+ });
1539
+
1540
+ // Verify the render function was called with the object as-is
1541
+ expect(actions["*"].render).toHaveBeenCalledWith(
1542
+ expect.objectContaining({
1543
+ result: { from: "props", data: "object" }, // Should remain as object
1544
+ args: { test: "value" },
1545
+ status: "complete",
1546
+ messageId: "action-object-result",
1547
+ }),
1548
+ );
1549
+ });
1550
+
1551
+ test("should handle action execution arguments correctly with simplified conversion", () => {
1552
+ const actionExecMsg = new gql.ActionExecutionMessage({
1553
+ id: "action-simplified",
1554
+ name: "simplifiedAction",
1555
+ arguments: { complex: { nested: "value" }, array: [1, 2, 3] },
1556
+ parentMessageId: "parent-simplified",
1557
+ });
1558
+
1559
+ const result = gqlActionExecutionMessageToAGUIMessage(actionExecMsg);
1560
+
1561
+ expect(result).toMatchObject({
1562
+ id: "action-simplified",
1563
+ role: "assistant",
1564
+ toolCalls: [
1565
+ {
1566
+ id: "action-simplified",
1567
+ function: {
1568
+ name: "simplifiedAction",
1569
+ arguments: '{"complex":{"nested":"value"},"array":[1,2,3]}',
1570
+ },
1571
+ type: "function",
1572
+ },
1573
+ ],
1574
+ name: "simplifiedAction",
1575
+ });
1576
+ });
1577
+ });
1578
+ });