@copilotkit/runtime 1.8.12-next.3 → 1.8.12-next.5

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 (47) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/{chunk-LZBWDTON.mjs → chunk-BILRAIYB.mjs} +2 -2
  3. package/dist/{chunk-CF5VXJC6.mjs → chunk-JUBPE6KE.mjs} +2 -2
  4. package/dist/{chunk-Y4H3U52G.mjs → chunk-MVKCCH5U.mjs} +216 -173
  5. package/dist/chunk-MVKCCH5U.mjs.map +1 -0
  6. package/dist/{chunk-2L6VOEJ4.mjs → chunk-RVYUQL6I.mjs} +4 -3
  7. package/dist/chunk-RVYUQL6I.mjs.map +1 -0
  8. package/dist/{chunk-KVRZ3PWO.mjs → chunk-Y3WEJHDT.mjs} +2 -2
  9. package/dist/index.js +217 -173
  10. package/dist/index.js.map +1 -1
  11. package/dist/index.mjs +5 -5
  12. package/dist/lib/index.js +109 -82
  13. package/dist/lib/index.js.map +1 -1
  14. package/dist/lib/index.mjs +5 -5
  15. package/dist/lib/integrations/index.js +2 -1
  16. package/dist/lib/integrations/index.js.map +1 -1
  17. package/dist/lib/integrations/index.mjs +5 -5
  18. package/dist/lib/integrations/nest/index.js +2 -1
  19. package/dist/lib/integrations/nest/index.js.map +1 -1
  20. package/dist/lib/integrations/nest/index.mjs +3 -3
  21. package/dist/lib/integrations/node-express/index.js +2 -1
  22. package/dist/lib/integrations/node-express/index.js.map +1 -1
  23. package/dist/lib/integrations/node-express/index.mjs +3 -3
  24. package/dist/lib/integrations/node-http/index.js +2 -1
  25. package/dist/lib/integrations/node-http/index.js.map +1 -1
  26. package/dist/lib/integrations/node-http/index.mjs +2 -2
  27. package/dist/service-adapters/index.js +215 -172
  28. package/dist/service-adapters/index.js.map +1 -1
  29. package/dist/service-adapters/index.mjs +1 -1
  30. package/jest.config.js +8 -3
  31. package/package.json +3 -2
  32. package/src/service-adapters/anthropic/anthropic-adapter.ts +124 -66
  33. package/src/service-adapters/anthropic/utils.ts +0 -19
  34. package/src/service-adapters/openai/openai-adapter.ts +107 -69
  35. package/tests/global.d.ts +13 -0
  36. package/tests/service-adapters/anthropic/allowlist-approach.test.ts +226 -0
  37. package/tests/service-adapters/anthropic/anthropic-adapter.test.ts +604 -0
  38. package/tests/service-adapters/openai/allowlist-approach.test.ts +238 -0
  39. package/tests/service-adapters/openai/openai-adapter.test.ts +301 -0
  40. package/tests/setup.jest.ts +21 -0
  41. package/tests/tsconfig.json +10 -0
  42. package/tsconfig.json +1 -1
  43. package/dist/chunk-2L6VOEJ4.mjs.map +0 -1
  44. package/dist/chunk-Y4H3U52G.mjs.map +0 -1
  45. /package/dist/{chunk-LZBWDTON.mjs.map → chunk-BILRAIYB.mjs.map} +0 -0
  46. /package/dist/{chunk-CF5VXJC6.mjs.map → chunk-JUBPE6KE.mjs.map} +0 -0
  47. /package/dist/{chunk-KVRZ3PWO.mjs.map → chunk-Y3WEJHDT.mjs.map} +0 -0
package/dist/index.mjs CHANGED
@@ -3,13 +3,13 @@ import {
3
3
  config,
4
4
  copilotRuntimeNextJSAppRouterEndpoint,
5
5
  copilotRuntimeNextJSPagesRouterEndpoint
6
- } from "./chunk-KVRZ3PWO.mjs";
6
+ } from "./chunk-Y3WEJHDT.mjs";
7
7
  import {
8
8
  copilotRuntimeNestEndpoint
9
- } from "./chunk-LZBWDTON.mjs";
9
+ } from "./chunk-BILRAIYB.mjs";
10
10
  import {
11
11
  copilotRuntimeNodeExpressEndpoint
12
- } from "./chunk-CF5VXJC6.mjs";
12
+ } from "./chunk-JUBPE6KE.mjs";
13
13
  import {
14
14
  CopilotRuntime,
15
15
  addCustomHeaderPlugin,
@@ -25,7 +25,7 @@ import {
25
25
  getCommonConfig,
26
26
  langGraphPlatformEndpoint,
27
27
  resolveEndpointType
28
- } from "./chunk-2L6VOEJ4.mjs";
28
+ } from "./chunk-RVYUQL6I.mjs";
29
29
  import {
30
30
  AnthropicAdapter,
31
31
  EmptyAdapter,
@@ -38,7 +38,7 @@ import {
38
38
  OpenAIAssistantAdapter,
39
39
  RemoteChain,
40
40
  UnifyAdapter
41
- } from "./chunk-Y4H3U52G.mjs";
41
+ } from "./chunk-MVKCCH5U.mjs";
42
42
  import {
43
43
  GuardrailsValidationFailureResponse,
44
44
  MessageStreamInterruptedResponse,
package/dist/lib/index.js CHANGED
@@ -44,7 +44,7 @@ var require_package = __commonJS({
44
44
  publishConfig: {
45
45
  access: "public"
46
46
  },
47
- version: "1.8.12-next.3",
47
+ version: "1.8.12-next.5",
48
48
  sideEffects: false,
49
49
  main: "./dist/index.js",
50
50
  module: "./dist/index.mjs",
@@ -64,6 +64,7 @@ var require_package = __commonJS({
64
64
  "unlink:global": "pnpm unlink --global"
65
65
  },
66
66
  devDependencies: {
67
+ "@jest/globals": "^29.7.0",
67
68
  "@swc/core": "1.5.28",
68
69
  "@types/express": "^4.17.21",
69
70
  "@types/jest": "^29.5.12",
@@ -365,7 +366,23 @@ var OpenAIAdapter = class {
365
366
  const { threadId: threadIdFromRequest, model = this.model, messages, actions, eventSource, forwardedParameters } = request;
366
367
  const tools = actions.map(convertActionInputToOpenAITool);
367
368
  const threadId = threadIdFromRequest ?? (0, import_shared2.randomUUID)();
368
- let openaiMessages = messages.map((m) => convertMessageToOpenAIMessage(m, {
369
+ const validToolUseIds = /* @__PURE__ */ new Set();
370
+ for (const message of messages) {
371
+ if (message.isActionExecutionMessage()) {
372
+ validToolUseIds.add(message.id);
373
+ }
374
+ }
375
+ const filteredMessages = messages.filter((message) => {
376
+ if (message.isResultMessage()) {
377
+ if (!validToolUseIds.has(message.actionExecutionId)) {
378
+ return false;
379
+ }
380
+ validToolUseIds.delete(message.actionExecutionId);
381
+ return true;
382
+ }
383
+ return true;
384
+ });
385
+ let openaiMessages = filteredMessages.map((m) => convertMessageToOpenAIMessage(m, {
369
386
  keepSystemRole: this.keepSystemRole
370
387
  }));
371
388
  openaiMessages = limitMessagesToTokenCount(openaiMessages, tools, model);
@@ -378,91 +395,101 @@ var OpenAIAdapter = class {
378
395
  }
379
396
  };
380
397
  }
381
- const stream = this.openai.beta.chat.completions.stream({
382
- model,
383
- stream: true,
384
- messages: openaiMessages,
385
- ...tools.length > 0 && {
386
- tools
387
- },
388
- ...(forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) && {
389
- max_tokens: forwardedParameters.maxTokens
390
- },
391
- ...(forwardedParameters == null ? void 0 : forwardedParameters.stop) && {
392
- stop: forwardedParameters.stop
393
- },
394
- ...toolChoice && {
395
- tool_choice: toolChoice
396
- },
397
- ...this.disableParallelToolCalls && {
398
- parallel_tool_calls: false
399
- },
400
- ...(forwardedParameters == null ? void 0 : forwardedParameters.temperature) && {
401
- temperature: forwardedParameters.temperature
402
- }
403
- });
404
- eventSource.stream(async (eventStream$) => {
405
- var _a, _b;
406
- let mode = null;
407
- let currentMessageId;
408
- let currentToolCallId;
409
- for await (const chunk of stream) {
410
- if (chunk.choices.length === 0) {
411
- continue;
412
- }
413
- const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
414
- const content = chunk.choices[0].delta.content;
415
- if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
416
- mode = null;
417
- eventStream$.sendTextMessageEnd({
418
- messageId: currentMessageId
419
- });
420
- } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
421
- mode = null;
422
- eventStream$.sendActionExecutionEnd({
423
- actionExecutionId: currentToolCallId
424
- });
398
+ try {
399
+ const stream = this.openai.beta.chat.completions.stream({
400
+ model,
401
+ stream: true,
402
+ messages: openaiMessages,
403
+ ...tools.length > 0 && {
404
+ tools
405
+ },
406
+ ...(forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) && {
407
+ max_tokens: forwardedParameters.maxTokens
408
+ },
409
+ ...(forwardedParameters == null ? void 0 : forwardedParameters.stop) && {
410
+ stop: forwardedParameters.stop
411
+ },
412
+ ...toolChoice && {
413
+ tool_choice: toolChoice
414
+ },
415
+ ...this.disableParallelToolCalls && {
416
+ parallel_tool_calls: false
417
+ },
418
+ ...(forwardedParameters == null ? void 0 : forwardedParameters.temperature) && {
419
+ temperature: forwardedParameters.temperature
425
420
  }
426
- if (mode === null) {
427
- if (toolCall == null ? void 0 : toolCall.id) {
428
- mode = "function";
429
- currentToolCallId = toolCall.id;
430
- eventStream$.sendActionExecutionStart({
431
- actionExecutionId: currentToolCallId,
432
- parentMessageId: chunk.id,
433
- actionName: toolCall.function.name
434
- });
435
- } else if (content) {
436
- mode = "message";
437
- currentMessageId = chunk.id;
438
- eventStream$.sendTextMessageStart({
421
+ });
422
+ eventSource.stream(async (eventStream$) => {
423
+ var _a, _b;
424
+ let mode = null;
425
+ let currentMessageId;
426
+ let currentToolCallId;
427
+ try {
428
+ for await (const chunk of stream) {
429
+ if (chunk.choices.length === 0) {
430
+ continue;
431
+ }
432
+ const toolCall = (_a = chunk.choices[0].delta.tool_calls) == null ? void 0 : _a[0];
433
+ const content = chunk.choices[0].delta.content;
434
+ if (mode === "message" && (toolCall == null ? void 0 : toolCall.id)) {
435
+ mode = null;
436
+ eventStream$.sendTextMessageEnd({
437
+ messageId: currentMessageId
438
+ });
439
+ } else if (mode === "function" && (toolCall === void 0 || (toolCall == null ? void 0 : toolCall.id))) {
440
+ mode = null;
441
+ eventStream$.sendActionExecutionEnd({
442
+ actionExecutionId: currentToolCallId
443
+ });
444
+ }
445
+ if (mode === null) {
446
+ if (toolCall == null ? void 0 : toolCall.id) {
447
+ mode = "function";
448
+ currentToolCallId = toolCall.id;
449
+ eventStream$.sendActionExecutionStart({
450
+ actionExecutionId: currentToolCallId,
451
+ parentMessageId: chunk.id,
452
+ actionName: toolCall.function.name
453
+ });
454
+ } else if (content) {
455
+ mode = "message";
456
+ currentMessageId = chunk.id;
457
+ eventStream$.sendTextMessageStart({
458
+ messageId: currentMessageId
459
+ });
460
+ }
461
+ }
462
+ if (mode === "message" && content) {
463
+ eventStream$.sendTextMessageContent({
464
+ messageId: currentMessageId,
465
+ content
466
+ });
467
+ } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
468
+ eventStream$.sendActionExecutionArgs({
469
+ actionExecutionId: currentToolCallId,
470
+ args: toolCall.function.arguments
471
+ });
472
+ }
473
+ }
474
+ if (mode === "message") {
475
+ eventStream$.sendTextMessageEnd({
439
476
  messageId: currentMessageId
440
477
  });
478
+ } else if (mode === "function") {
479
+ eventStream$.sendActionExecutionEnd({
480
+ actionExecutionId: currentToolCallId
481
+ });
441
482
  }
483
+ } catch (error) {
484
+ console.error("[OpenAI] Error processing stream:", error);
485
+ throw error;
442
486
  }
443
- if (mode === "message" && content) {
444
- eventStream$.sendTextMessageContent({
445
- messageId: currentMessageId,
446
- content
447
- });
448
- } else if (mode === "function" && ((_b = toolCall == null ? void 0 : toolCall.function) == null ? void 0 : _b.arguments)) {
449
- eventStream$.sendActionExecutionArgs({
450
- actionExecutionId: currentToolCallId,
451
- args: toolCall.function.arguments
452
- });
453
- }
454
- }
455
- if (mode === "message") {
456
- eventStream$.sendTextMessageEnd({
457
- messageId: currentMessageId
458
- });
459
- } else if (mode === "function") {
460
- eventStream$.sendActionExecutionEnd({
461
- actionExecutionId: currentToolCallId
462
- });
463
- }
464
- eventStream$.complete();
465
- });
487
+ eventStream$.complete();
488
+ });
489
+ } catch (error) {
490
+ console.error("[OpenAI] Error during API call:", error);
491
+ throw error;
492
+ }
466
493
  return {
467
494
  threadId
468
495
  };