@copilotkit/runtime 1.10.0-next.6 → 1.10.0-next.7

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 (40) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/{chunk-6K5MA7C2.mjs → chunk-4NCZWO3B.mjs} +2 -2
  3. package/dist/{chunk-NE5XONJ5.mjs → chunk-CB66TAVJ.mjs} +4 -4
  4. package/dist/{chunk-NE5XONJ5.mjs.map → chunk-CB66TAVJ.mjs.map} +1 -1
  5. package/dist/{chunk-RZHXGTUZ.mjs → chunk-F7IANE7Z.mjs} +65 -5
  6. package/dist/chunk-F7IANE7Z.mjs.map +1 -0
  7. package/dist/{chunk-S73GSB6E.mjs → chunk-H2LB46NL.mjs} +2 -2
  8. package/dist/{chunk-5UCKTDUR.mjs → chunk-HRQPO53N.mjs} +2 -2
  9. package/dist/{chunk-L3IUQRK2.mjs → chunk-OQZOCZO2.mjs} +2 -2
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.js +66 -6
  12. package/dist/index.js.map +1 -1
  13. package/dist/index.mjs +6 -6
  14. package/dist/lib/index.js +2 -2
  15. package/dist/lib/index.js.map +1 -1
  16. package/dist/lib/index.mjs +6 -6
  17. package/dist/lib/integrations/index.js +2 -2
  18. package/dist/lib/integrations/index.js.map +1 -1
  19. package/dist/lib/integrations/index.mjs +5 -5
  20. package/dist/lib/integrations/nest/index.js +2 -2
  21. package/dist/lib/integrations/nest/index.js.map +1 -1
  22. package/dist/lib/integrations/nest/index.mjs +3 -3
  23. package/dist/lib/integrations/node-express/index.js +2 -2
  24. package/dist/lib/integrations/node-express/index.js.map +1 -1
  25. package/dist/lib/integrations/node-express/index.mjs +3 -3
  26. package/dist/lib/integrations/node-http/index.js +2 -2
  27. package/dist/lib/integrations/node-http/index.js.map +1 -1
  28. package/dist/lib/integrations/node-http/index.mjs +2 -2
  29. package/dist/service-adapters/index.d.ts +32 -2
  30. package/dist/service-adapters/index.js +64 -4
  31. package/dist/service-adapters/index.js.map +1 -1
  32. package/dist/service-adapters/index.mjs +1 -1
  33. package/package.json +3 -3
  34. package/src/service-adapters/anthropic/anthropic-adapter.ts +106 -5
  35. package/src/service-adapters/anthropic/utils.ts +1 -1
  36. package/dist/chunk-RZHXGTUZ.mjs.map +0 -1
  37. /package/dist/{chunk-6K5MA7C2.mjs.map → chunk-4NCZWO3B.mjs.map} +0 -0
  38. /package/dist/{chunk-S73GSB6E.mjs.map → chunk-H2LB46NL.mjs.map} +0 -0
  39. /package/dist/{chunk-5UCKTDUR.mjs.map → chunk-HRQPO53N.mjs.map} +0 -0
  40. /package/dist/{chunk-L3IUQRK2.mjs.map → chunk-OQZOCZO2.mjs.map} +0 -0
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  copilotRuntimeNodeHttpEndpoint
3
- } from "../../../chunk-NE5XONJ5.mjs";
3
+ } from "../../../chunk-CB66TAVJ.mjs";
4
4
  import "../../../chunk-SHBDMA63.mjs";
5
- import "../../../chunk-RZHXGTUZ.mjs";
5
+ import "../../../chunk-F7IANE7Z.mjs";
6
6
  import "../../../chunk-XWBDEXDA.mjs";
7
7
  import "../../../chunk-AMUJQ6IR.mjs";
8
8
  import "../../../chunk-2OZAGFV3.mjs";
@@ -29,10 +29,26 @@ import 'groq-sdk';
29
29
  * apiKey: "<your-api-key>",
30
30
  * });
31
31
  *
32
- * return new AnthropicAdapter({ anthropic });
32
+ * return new AnthropicAdapter({
33
+ * anthropic,
34
+ * promptCaching: {
35
+ * enabled: true,
36
+ * debug: true
37
+ * }
38
+ * });
33
39
  * ```
34
40
  */
35
41
 
42
+ interface AnthropicPromptCachingConfig {
43
+ /**
44
+ * Whether to enable prompt caching.
45
+ */
46
+ enabled: boolean;
47
+ /**
48
+ * Whether to enable debug logging for cache operations.
49
+ */
50
+ debug?: boolean;
51
+ }
36
52
  interface AnthropicAdapterParams {
37
53
  /**
38
54
  * An optional Anthropic instance to use. If not provided, a new instance will be
@@ -43,12 +59,26 @@ interface AnthropicAdapterParams {
43
59
  * The model to use.
44
60
  */
45
61
  model?: string;
62
+ /**
63
+ * Configuration for prompt caching.
64
+ * See: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
65
+ */
66
+ promptCaching?: AnthropicPromptCachingConfig;
46
67
  }
47
68
  declare class AnthropicAdapter implements CopilotServiceAdapter {
48
69
  private model;
70
+ private promptCaching;
49
71
  private _anthropic;
50
72
  get anthropic(): Anthropic;
51
73
  constructor(params?: AnthropicAdapterParams);
74
+ /**
75
+ * Adds cache control to system prompt
76
+ */
77
+ private addSystemPromptCaching;
78
+ /**
79
+ * Adds cache control to the final message
80
+ */
81
+ private addIncrementalMessageCaching;
52
82
  private shouldGenerateFallbackResponse;
53
83
  process(request: CopilotRuntimeChatCompletionRequest): Promise<CopilotRuntimeChatCompletionResponse>;
54
84
  }
@@ -129,4 +159,4 @@ declare class EmptyAdapter implements CopilotServiceAdapter {
129
159
  }
130
160
  declare const ExperimentalEmptyAdapter: typeof EmptyAdapter;
131
161
 
132
- export { AnthropicAdapter, AnthropicAdapterParams, BedrockAdapter, BedrockAdapterParams, CopilotRuntimeChatCompletionRequest, CopilotRuntimeChatCompletionResponse, CopilotServiceAdapter, EmptyAdapter, ExperimentalEmptyAdapter, ExperimentalOllamaAdapter, LangChainAdapter };
162
+ export { AnthropicAdapter, AnthropicAdapterParams, AnthropicPromptCachingConfig, BedrockAdapter, BedrockAdapterParams, CopilotRuntimeChatCompletionRequest, CopilotRuntimeChatCompletionResponse, CopilotServiceAdapter, EmptyAdapter, ExperimentalEmptyAdapter, ExperimentalOllamaAdapter, LangChainAdapter };
@@ -1403,6 +1403,7 @@ var import_shared10 = require("@copilotkit/shared");
1403
1403
  var DEFAULT_MODEL3 = "claude-3-5-sonnet-latest";
1404
1404
  var AnthropicAdapter = class {
1405
1405
  model = DEFAULT_MODEL3;
1406
+ promptCaching;
1406
1407
  _anthropic;
1407
1408
  get anthropic() {
1408
1409
  return this._anthropic;
@@ -1412,6 +1413,63 @@ var AnthropicAdapter = class {
1412
1413
  if (params == null ? void 0 : params.model) {
1413
1414
  this.model = params.model;
1414
1415
  }
1416
+ this.promptCaching = (params == null ? void 0 : params.promptCaching) || {
1417
+ enabled: false
1418
+ };
1419
+ }
1420
+ /**
1421
+ * Adds cache control to system prompt
1422
+ */
1423
+ addSystemPromptCaching(system, debug = false) {
1424
+ if (!this.promptCaching.enabled || !system) {
1425
+ return system;
1426
+ }
1427
+ const originalTextLength = system.length;
1428
+ if (debug) {
1429
+ console.log(`[ANTHROPIC CACHE DEBUG] Added cache control to system prompt (${originalTextLength} chars).`);
1430
+ }
1431
+ return [
1432
+ {
1433
+ type: "text",
1434
+ text: system,
1435
+ cache_control: {
1436
+ type: "ephemeral"
1437
+ }
1438
+ }
1439
+ ];
1440
+ }
1441
+ /**
1442
+ * Adds cache control to the final message
1443
+ */
1444
+ addIncrementalMessageCaching(messages, debug = false) {
1445
+ if (!this.promptCaching.enabled || messages.length === 0) {
1446
+ return messages;
1447
+ }
1448
+ const finalMessage = messages[messages.length - 1];
1449
+ const messageNumber = messages.length;
1450
+ if (Array.isArray(finalMessage.content) && finalMessage.content.length > 0) {
1451
+ const finalBlock = finalMessage.content[finalMessage.content.length - 1];
1452
+ const updatedMessages = [
1453
+ ...messages.slice(0, -1),
1454
+ {
1455
+ ...finalMessage,
1456
+ content: [
1457
+ ...finalMessage.content.slice(0, -1),
1458
+ {
1459
+ ...finalBlock,
1460
+ cache_control: {
1461
+ type: "ephemeral"
1462
+ }
1463
+ }
1464
+ ]
1465
+ }
1466
+ ];
1467
+ if (debug) {
1468
+ console.log(`[ANTHROPIC CACHE DEBUG] Added cache control to final message (message ${messageNumber}).`);
1469
+ }
1470
+ return updatedMessages;
1471
+ }
1472
+ return messages;
1415
1473
  }
1416
1474
  shouldGenerateFallbackResponse(messages) {
1417
1475
  var _a, _b, _c;
@@ -1473,6 +1531,8 @@ var AnthropicAdapter = class {
1473
1531
  return true;
1474
1532
  });
1475
1533
  const limitedMessages = limitMessagesToTokenCount2(anthropicMessages, tools, model);
1534
+ const cachedSystemPrompt = this.addSystemPromptCaching(instructions, this.promptCaching.debug);
1535
+ const cachedMessages = this.addIncrementalMessageCaching(limitedMessages, this.promptCaching.debug);
1476
1536
  let toolChoice = forwardedParameters == null ? void 0 : forwardedParameters.toolChoice;
1477
1537
  if ((forwardedParameters == null ? void 0 : forwardedParameters.toolChoice) === "function") {
1478
1538
  toolChoice = {
@@ -1482,9 +1542,9 @@ var AnthropicAdapter = class {
1482
1542
  }
1483
1543
  try {
1484
1544
  const createParams = {
1485
- system: instructions,
1545
+ system: cachedSystemPrompt,
1486
1546
  model: this.model,
1487
- messages: limitedMessages,
1547
+ messages: cachedMessages,
1488
1548
  max_tokens: (forwardedParameters == null ? void 0 : forwardedParameters.maxTokens) || 1024,
1489
1549
  ...(forwardedParameters == null ? void 0 : forwardedParameters.temperature) ? {
1490
1550
  temperature: forwardedParameters.temperature
@@ -1562,9 +1622,9 @@ var AnthropicAdapter = class {
1562
1622
  } catch (error) {
1563
1623
  throw convertServiceAdapterError(error, "Anthropic");
1564
1624
  }
1565
- if (!hasReceivedContent && this.shouldGenerateFallbackResponse(limitedMessages)) {
1625
+ if (!hasReceivedContent && this.shouldGenerateFallbackResponse(cachedMessages)) {
1566
1626
  let fallbackContent = "Task completed successfully.";
1567
- const lastMessage = limitedMessages[limitedMessages.length - 1];
1627
+ const lastMessage = cachedMessages[cachedMessages.length - 1];
1568
1628
  if ((lastMessage == null ? void 0 : lastMessage.role) === "user" && Array.isArray(lastMessage.content)) {
1569
1629
  const toolResult = lastMessage.content.find((c) => c.type === "tool_result");
1570
1630
  if ((toolResult == null ? void 0 : toolResult.content) && toolResult.content !== "Action completed successfully") {