@copilotkit/runtime 1.71.1 → 1.71.2

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 (88) hide show
  1. package/README.md +22 -0
  2. package/dist/agent/index.cjs +14 -2
  3. package/dist/agent/index.cjs.map +1 -1
  4. package/dist/agent/index.d.cts +1 -0
  5. package/dist/agent/index.d.cts.map +1 -1
  6. package/dist/agent/index.d.mts +1 -0
  7. package/dist/agent/index.d.mts.map +1 -1
  8. package/dist/agent/index.mjs +14 -2
  9. package/dist/agent/index.mjs.map +1 -1
  10. package/dist/index.cjs +1 -0
  11. package/dist/index.d.cts +9 -1
  12. package/dist/index.d.mts +9 -1
  13. package/dist/index.mjs +2 -2
  14. package/dist/lib/telemetry-client.cjs.map +1 -1
  15. package/dist/lib/telemetry-client.mjs.map +1 -1
  16. package/dist/package.cjs +6 -4
  17. package/dist/package.mjs +6 -4
  18. package/dist/service-adapters/anthropic/anthropic-adapter.cjs +59 -25
  19. package/dist/service-adapters/anthropic/anthropic-adapter.cjs.map +1 -1
  20. package/dist/service-adapters/anthropic/anthropic-adapter.d.cts +30 -1
  21. package/dist/service-adapters/anthropic/anthropic-adapter.d.cts.map +1 -1
  22. package/dist/service-adapters/anthropic/anthropic-adapter.d.mts +30 -1
  23. package/dist/service-adapters/anthropic/anthropic-adapter.d.mts.map +1 -1
  24. package/dist/service-adapters/anthropic/anthropic-adapter.mjs +59 -26
  25. package/dist/service-adapters/anthropic/anthropic-adapter.mjs.map +1 -1
  26. package/dist/service-adapters/index.d.cts +1 -1
  27. package/dist/service-adapters/index.d.mts +1 -1
  28. package/dist/v2/index.cjs +2 -0
  29. package/dist/v2/index.d.cts +2 -1
  30. package/dist/v2/index.d.mts +2 -1
  31. package/dist/v2/index.mjs +2 -1
  32. package/dist/v2/runtime/core/runtime.cjs.map +1 -1
  33. package/dist/v2/runtime/core/runtime.d.cts +2 -1
  34. package/dist/v2/runtime/core/runtime.d.cts.map +1 -1
  35. package/dist/v2/runtime/core/runtime.d.mts +2 -1
  36. package/dist/v2/runtime/core/runtime.d.mts.map +1 -1
  37. package/dist/v2/runtime/core/runtime.mjs.map +1 -1
  38. package/dist/v2/runtime/handlers/handle-run.cjs +2 -1
  39. package/dist/v2/runtime/handlers/handle-run.cjs.map +1 -1
  40. package/dist/v2/runtime/handlers/handle-run.mjs +2 -1
  41. package/dist/v2/runtime/handlers/handle-run.mjs.map +1 -1
  42. package/dist/v2/runtime/handlers/handle-stop.cjs +41 -1
  43. package/dist/v2/runtime/handlers/handle-stop.cjs.map +1 -1
  44. package/dist/v2/runtime/handlers/handle-stop.mjs +42 -2
  45. package/dist/v2/runtime/handlers/handle-stop.mjs.map +1 -1
  46. package/dist/v2/runtime/handlers/intelligence/run.cjs +1 -1
  47. package/dist/v2/runtime/handlers/intelligence/run.cjs.map +1 -1
  48. package/dist/v2/runtime/handlers/intelligence/run.mjs +1 -1
  49. package/dist/v2/runtime/handlers/intelligence/run.mjs.map +1 -1
  50. package/dist/v2/runtime/handlers/intelligence/threads.cjs +2 -3
  51. package/dist/v2/runtime/handlers/intelligence/threads.cjs.map +1 -1
  52. package/dist/v2/runtime/handlers/intelligence/threads.mjs +2 -3
  53. package/dist/v2/runtime/handlers/intelligence/threads.mjs.map +1 -1
  54. package/dist/v2/runtime/handlers/shared/agent-utils.cjs +8 -2
  55. package/dist/v2/runtime/handlers/shared/agent-utils.cjs.map +1 -1
  56. package/dist/v2/runtime/handlers/shared/agent-utils.mjs +8 -2
  57. package/dist/v2/runtime/handlers/shared/agent-utils.mjs.map +1 -1
  58. package/dist/v2/runtime/index.d.cts +1 -0
  59. package/dist/v2/runtime/index.d.cts.map +1 -1
  60. package/dist/v2/runtime/index.d.mts +1 -0
  61. package/dist/v2/runtime/index.d.mts.map +1 -1
  62. package/dist/v2/runtime/intelligence-platform/client.cjs +106 -1
  63. package/dist/v2/runtime/intelligence-platform/client.cjs.map +1 -1
  64. package/dist/v2/runtime/intelligence-platform/client.d.cts +11 -0
  65. package/dist/v2/runtime/intelligence-platform/client.d.cts.map +1 -1
  66. package/dist/v2/runtime/intelligence-platform/client.d.mts +11 -0
  67. package/dist/v2/runtime/intelligence-platform/client.d.mts.map +1 -1
  68. package/dist/v2/runtime/intelligence-platform/client.mjs +106 -1
  69. package/dist/v2/runtime/intelligence-platform/client.mjs.map +1 -1
  70. package/dist/v2/runtime/intelligence-platform/index.d.cts +1 -0
  71. package/dist/v2/runtime/intelligence-platform/index.d.mts +1 -0
  72. package/dist/v2/runtime/intelligence-platform/learned-skills.cjs +43 -0
  73. package/dist/v2/runtime/intelligence-platform/learned-skills.cjs.map +1 -0
  74. package/dist/v2/runtime/intelligence-platform/learned-skills.d.cts +34 -0
  75. package/dist/v2/runtime/intelligence-platform/learned-skills.d.cts.map +1 -0
  76. package/dist/v2/runtime/intelligence-platform/learned-skills.d.mts +34 -0
  77. package/dist/v2/runtime/intelligence-platform/learned-skills.d.mts.map +1 -0
  78. package/dist/v2/runtime/intelligence-platform/learned-skills.mjs +41 -0
  79. package/dist/v2/runtime/intelligence-platform/learned-skills.mjs.map +1 -0
  80. package/dist/v2/runtime/runner/intelligence.cjs +82 -25
  81. package/dist/v2/runtime/runner/intelligence.cjs.map +1 -1
  82. package/dist/v2/runtime/runner/intelligence.d.cts +3 -0
  83. package/dist/v2/runtime/runner/intelligence.d.cts.map +1 -1
  84. package/dist/v2/runtime/runner/intelligence.d.mts +3 -0
  85. package/dist/v2/runtime/runner/intelligence.d.mts.map +1 -1
  86. package/dist/v2/runtime/runner/intelligence.mjs +83 -26
  87. package/dist/v2/runtime/runner/intelligence.mjs.map +1 -1
  88. package/package.json +8 -4
@@ -1 +1 @@
1
- {"version":3,"file":"telemetry-client.cjs","names":["TelemetryClient","resolveEndpointType","EndpointType"],"sources":["../../../src/v1-deprecated/lib/telemetry-client.ts"],"sourcesContent":["import { TelemetryClient } from \"@copilotkit/shared/telemetry\";\nimport { createHash } from \"node:crypto\";\nimport { CopilotRuntime, resolveEndpointType } from \"./runtime/copilot-runtime\";\nimport { RuntimeInstanceCreatedInfo } from \"@copilotkit/shared/src/telemetry/events\";\nimport { CreateCopilotRuntimeServerOptions } from \"./integrations/shared\";\nimport { EndpointType, LangGraphPlatformEndpoint } from \"./runtime/types\";\nconst packageJson = require(\"../../../package.json\");\n\nconst telemetryClient = new TelemetryClient({\n packageName: packageJson.name,\n packageVersion: packageJson.version,\n});\n\nexport function getRuntimeInstanceTelemetryInfo(\n options: CreateCopilotRuntimeServerOptions,\n): RuntimeInstanceCreatedInfo {\n const runtime = options.runtime;\n const remoteEndpoints = runtime.params?.remoteEndpoints ?? [];\n const endpointsInfo = remoteEndpoints.reduce(\n (acc, endpoint) => {\n let info = { ...acc };\n\n const endpointType = resolveEndpointType(endpoint);\n if (!info.endpointTypes.includes(endpointType)) {\n info = {\n ...info,\n endpointTypes: [...info.endpointTypes, endpointType],\n };\n }\n\n if (endpointType === EndpointType.LangGraphPlatform) {\n // When type is resolved, recreating a const with casting of type\n const ep = endpoint as LangGraphPlatformEndpoint;\n info = {\n ...info,\n agentsAmount: ep.agents.length,\n hashedKey: ep.langsmithApiKey\n ? createHash(\"sha256\").update(ep.langsmithApiKey).digest(\"hex\")\n : null,\n };\n }\n\n return info;\n },\n { endpointTypes: [], agentsAmount: null, hashedKey: null },\n );\n\n // Get public API key from options.cloud.publicApiKey\n const publicApiKey = options.cloud?.publicApiKey;\n const apiKeyProvided = !!publicApiKey && publicApiKey.trim().length > 0;\n\n return {\n actionsAmount: runtime.params?.actions?.length ?? 0,\n endpointsAmount: remoteEndpoints.length,\n endpointTypes: endpointsInfo.endpointTypes,\n agentsAmount: Object.keys(runtime.instance.agents).length,\n hashedLgcKey: endpointsInfo.hashedKey,\n \"cloud.api_key_provided\": apiKeyProvided,\n ...(apiKeyProvided ? { \"cloud.public_api_key\": publicApiKey } : {}),\n ...(options.cloud?.baseUrl\n ? { \"cloud.base_url\": options.cloud.baseUrl }\n : {}),\n } as RuntimeInstanceCreatedInfo;\n}\n\nexport default telemetryClient;\n"],"mappings":";;;;;;;;;AAMA,MAAM;AAEN,MAAM,kBAAkB,IAAIA,6CAAgB;CAC1C,aAAa,YAAY;CACzB,gBAAgB,YAAY;CAC7B,CAAC;AAEF,SAAgB,gCACd,SAC4B;CAC5B,MAAM,UAAU,QAAQ;CACxB,MAAM,kBAAkB,QAAQ,QAAQ,mBAAmB,EAAE;CAC7D,MAAM,gBAAgB,gBAAgB,QACnC,KAAK,aAAa;EACjB,IAAI,OAAO,EAAE,GAAG,KAAK;EAErB,MAAM,eAAeC,4CAAoB,SAAS;AAClD,MAAI,CAAC,KAAK,cAAc,SAAS,aAAa,CAC5C,QAAO;GACL,GAAG;GACH,eAAe,CAAC,GAAG,KAAK,eAAe,aAAa;GACrD;AAGH,MAAI,iBAAiBC,2BAAa,mBAAmB;GAEnD,MAAM,KAAK;AACX,UAAO;IACL,GAAG;IACH,cAAc,GAAG,OAAO;IACxB,WAAW,GAAG,8CACC,SAAS,CAAC,OAAO,GAAG,gBAAgB,CAAC,OAAO,MAAM,GAC7D;IACL;;AAGH,SAAO;IAET;EAAE,eAAe,EAAE;EAAE,cAAc;EAAM,WAAW;EAAM,CAC3D;CAGD,MAAM,eAAe,QAAQ,OAAO;CACpC,MAAM,iBAAiB,CAAC,CAAC,gBAAgB,aAAa,MAAM,CAAC,SAAS;AAEtE,QAAO;EACL,eAAe,QAAQ,QAAQ,SAAS,UAAU;EAClD,iBAAiB,gBAAgB;EACjC,eAAe,cAAc;EAC7B,cAAc,OAAO,KAAK,QAAQ,SAAS,OAAO,CAAC;EACnD,cAAc,cAAc;EAC5B,0BAA0B;EAC1B,GAAI,iBAAiB,EAAE,wBAAwB,cAAc,GAAG,EAAE;EAClE,GAAI,QAAQ,OAAO,UACf,EAAE,kBAAkB,QAAQ,MAAM,SAAS,GAC3C,EAAE;EACP"}
1
+ {"version":3,"file":"telemetry-client.cjs","names":["TelemetryClient","resolveEndpointType","EndpointType"],"sources":["../../../src/v1-deprecated/lib/telemetry-client.ts"],"sourcesContent":["import { TelemetryClient } from \"@copilotkit/shared/telemetry\";\nimport { createHash } from \"node:crypto\";\nimport { CopilotRuntime, resolveEndpointType } from \"./runtime/copilot-runtime\";\nimport type { RuntimeInstanceCreatedInfo } from \"@copilotkit/shared/src/telemetry/events\";\nimport type { CreateCopilotRuntimeServerOptions } from \"./integrations/shared\";\nimport type { LangGraphPlatformEndpoint } from \"./runtime/types\";\nimport { EndpointType } from \"./runtime/types\";\nconst packageJson = require(\"../../../package.json\");\n\nconst telemetryClient = new TelemetryClient({\n packageName: packageJson.name,\n packageVersion: packageJson.version,\n});\n\nexport function getRuntimeInstanceTelemetryInfo(\n options: CreateCopilotRuntimeServerOptions,\n): RuntimeInstanceCreatedInfo {\n const runtime = options.runtime;\n const remoteEndpoints = runtime.params?.remoteEndpoints ?? [];\n const endpointsInfo = remoteEndpoints.reduce(\n (acc, endpoint) => {\n let info = { ...acc };\n\n const endpointType = resolveEndpointType(endpoint);\n if (!info.endpointTypes.includes(endpointType)) {\n info = {\n ...info,\n endpointTypes: [...info.endpointTypes, endpointType],\n };\n }\n\n if (endpointType === EndpointType.LangGraphPlatform) {\n // When type is resolved, recreating a const with casting of type\n const ep = endpoint as LangGraphPlatformEndpoint;\n info = {\n ...info,\n agentsAmount: ep.agents.length,\n hashedKey: ep.langsmithApiKey\n ? createHash(\"sha256\").update(ep.langsmithApiKey).digest(\"hex\")\n : null,\n };\n }\n\n return info;\n },\n { endpointTypes: [], agentsAmount: null, hashedKey: null },\n );\n\n // Get public API key from options.cloud.publicApiKey\n const publicApiKey = options.cloud?.publicApiKey;\n const apiKeyProvided = !!publicApiKey && publicApiKey.trim().length > 0;\n\n return {\n actionsAmount: runtime.params?.actions?.length ?? 0,\n endpointsAmount: remoteEndpoints.length,\n endpointTypes: endpointsInfo.endpointTypes,\n agentsAmount: Object.keys(runtime.instance.agents).length,\n hashedLgcKey: endpointsInfo.hashedKey,\n \"cloud.api_key_provided\": apiKeyProvided,\n ...(apiKeyProvided ? { \"cloud.public_api_key\": publicApiKey } : {}),\n ...(options.cloud?.baseUrl\n ? { \"cloud.base_url\": options.cloud.baseUrl }\n : {}),\n } as RuntimeInstanceCreatedInfo;\n}\n\nexport default telemetryClient;\n"],"mappings":";;;;;;;;;AAOA,MAAM;AAEN,MAAM,kBAAkB,IAAIA,6CAAgB;CAC1C,aAAa,YAAY;CACzB,gBAAgB,YAAY;CAC7B,CAAC;AAEF,SAAgB,gCACd,SAC4B;CAC5B,MAAM,UAAU,QAAQ;CACxB,MAAM,kBAAkB,QAAQ,QAAQ,mBAAmB,EAAE;CAC7D,MAAM,gBAAgB,gBAAgB,QACnC,KAAK,aAAa;EACjB,IAAI,OAAO,EAAE,GAAG,KAAK;EAErB,MAAM,eAAeC,4CAAoB,SAAS;AAClD,MAAI,CAAC,KAAK,cAAc,SAAS,aAAa,CAC5C,QAAO;GACL,GAAG;GACH,eAAe,CAAC,GAAG,KAAK,eAAe,aAAa;GACrD;AAGH,MAAI,iBAAiBC,2BAAa,mBAAmB;GAEnD,MAAM,KAAK;AACX,UAAO;IACL,GAAG;IACH,cAAc,GAAG,OAAO;IACxB,WAAW,GAAG,8CACC,SAAS,CAAC,OAAO,GAAG,gBAAgB,CAAC,OAAO,MAAM,GAC7D;IACL;;AAGH,SAAO;IAET;EAAE,eAAe,EAAE;EAAE,cAAc;EAAM,WAAW;EAAM,CAC3D;CAGD,MAAM,eAAe,QAAQ,OAAO;CACpC,MAAM,iBAAiB,CAAC,CAAC,gBAAgB,aAAa,MAAM,CAAC,SAAS;AAEtE,QAAO;EACL,eAAe,QAAQ,QAAQ,SAAS,UAAU;EAClD,iBAAiB,gBAAgB;EACjC,eAAe,cAAc;EAC7B,cAAc,OAAO,KAAK,QAAQ,SAAS,OAAO,CAAC;EACnD,cAAc,cAAc;EAC5B,0BAA0B;EAC1B,GAAI,iBAAiB,EAAE,wBAAwB,cAAc,GAAG,EAAE;EAClE,GAAI,QAAQ,OAAO,UACf,EAAE,kBAAkB,QAAQ,MAAM,SAAS,GAC3C,EAAE;EACP"}
@@ -1 +1 @@
1
- {"version":3,"file":"telemetry-client.mjs","names":[],"sources":["../../../src/v1-deprecated/lib/telemetry-client.ts"],"sourcesContent":["import { TelemetryClient } from \"@copilotkit/shared/telemetry\";\nimport { createHash } from \"node:crypto\";\nimport { CopilotRuntime, resolveEndpointType } from \"./runtime/copilot-runtime\";\nimport { RuntimeInstanceCreatedInfo } from \"@copilotkit/shared/src/telemetry/events\";\nimport { CreateCopilotRuntimeServerOptions } from \"./integrations/shared\";\nimport { EndpointType, LangGraphPlatformEndpoint } from \"./runtime/types\";\nconst packageJson = require(\"../../../package.json\");\n\nconst telemetryClient = new TelemetryClient({\n packageName: packageJson.name,\n packageVersion: packageJson.version,\n});\n\nexport function getRuntimeInstanceTelemetryInfo(\n options: CreateCopilotRuntimeServerOptions,\n): RuntimeInstanceCreatedInfo {\n const runtime = options.runtime;\n const remoteEndpoints = runtime.params?.remoteEndpoints ?? [];\n const endpointsInfo = remoteEndpoints.reduce(\n (acc, endpoint) => {\n let info = { ...acc };\n\n const endpointType = resolveEndpointType(endpoint);\n if (!info.endpointTypes.includes(endpointType)) {\n info = {\n ...info,\n endpointTypes: [...info.endpointTypes, endpointType],\n };\n }\n\n if (endpointType === EndpointType.LangGraphPlatform) {\n // When type is resolved, recreating a const with casting of type\n const ep = endpoint as LangGraphPlatformEndpoint;\n info = {\n ...info,\n agentsAmount: ep.agents.length,\n hashedKey: ep.langsmithApiKey\n ? createHash(\"sha256\").update(ep.langsmithApiKey).digest(\"hex\")\n : null,\n };\n }\n\n return info;\n },\n { endpointTypes: [], agentsAmount: null, hashedKey: null },\n );\n\n // Get public API key from options.cloud.publicApiKey\n const publicApiKey = options.cloud?.publicApiKey;\n const apiKeyProvided = !!publicApiKey && publicApiKey.trim().length > 0;\n\n return {\n actionsAmount: runtime.params?.actions?.length ?? 0,\n endpointsAmount: remoteEndpoints.length,\n endpointTypes: endpointsInfo.endpointTypes,\n agentsAmount: Object.keys(runtime.instance.agents).length,\n hashedLgcKey: endpointsInfo.hashedKey,\n \"cloud.api_key_provided\": apiKeyProvided,\n ...(apiKeyProvided ? { \"cloud.public_api_key\": publicApiKey } : {}),\n ...(options.cloud?.baseUrl\n ? { \"cloud.base_url\": options.cloud.baseUrl }\n : {}),\n } as RuntimeInstanceCreatedInfo;\n}\n\nexport default telemetryClient;\n"],"mappings":";;;;;;;;AAMA,MAAM;AAEN,MAAM,kBAAkB,IAAI,gBAAgB;CAC1C,aAAa,YAAY;CACzB,gBAAgB,YAAY;CAC7B,CAAC;AAEF,SAAgB,gCACd,SAC4B;CAC5B,MAAM,UAAU,QAAQ;CACxB,MAAM,kBAAkB,QAAQ,QAAQ,mBAAmB,EAAE;CAC7D,MAAM,gBAAgB,gBAAgB,QACnC,KAAK,aAAa;EACjB,IAAI,OAAO,EAAE,GAAG,KAAK;EAErB,MAAM,eAAe,oBAAoB,SAAS;AAClD,MAAI,CAAC,KAAK,cAAc,SAAS,aAAa,CAC5C,QAAO;GACL,GAAG;GACH,eAAe,CAAC,GAAG,KAAK,eAAe,aAAa;GACrD;AAGH,MAAI,iBAAiB,aAAa,mBAAmB;GAEnD,MAAM,KAAK;AACX,UAAO;IACL,GAAG;IACH,cAAc,GAAG,OAAO;IACxB,WAAW,GAAG,kBACV,WAAW,SAAS,CAAC,OAAO,GAAG,gBAAgB,CAAC,OAAO,MAAM,GAC7D;IACL;;AAGH,SAAO;IAET;EAAE,eAAe,EAAE;EAAE,cAAc;EAAM,WAAW;EAAM,CAC3D;CAGD,MAAM,eAAe,QAAQ,OAAO;CACpC,MAAM,iBAAiB,CAAC,CAAC,gBAAgB,aAAa,MAAM,CAAC,SAAS;AAEtE,QAAO;EACL,eAAe,QAAQ,QAAQ,SAAS,UAAU;EAClD,iBAAiB,gBAAgB;EACjC,eAAe,cAAc;EAC7B,cAAc,OAAO,KAAK,QAAQ,SAAS,OAAO,CAAC;EACnD,cAAc,cAAc;EAC5B,0BAA0B;EAC1B,GAAI,iBAAiB,EAAE,wBAAwB,cAAc,GAAG,EAAE;EAClE,GAAI,QAAQ,OAAO,UACf,EAAE,kBAAkB,QAAQ,MAAM,SAAS,GAC3C,EAAE;EACP"}
1
+ {"version":3,"file":"telemetry-client.mjs","names":[],"sources":["../../../src/v1-deprecated/lib/telemetry-client.ts"],"sourcesContent":["import { TelemetryClient } from \"@copilotkit/shared/telemetry\";\nimport { createHash } from \"node:crypto\";\nimport { CopilotRuntime, resolveEndpointType } from \"./runtime/copilot-runtime\";\nimport type { RuntimeInstanceCreatedInfo } from \"@copilotkit/shared/src/telemetry/events\";\nimport type { CreateCopilotRuntimeServerOptions } from \"./integrations/shared\";\nimport type { LangGraphPlatformEndpoint } from \"./runtime/types\";\nimport { EndpointType } from \"./runtime/types\";\nconst packageJson = require(\"../../../package.json\");\n\nconst telemetryClient = new TelemetryClient({\n packageName: packageJson.name,\n packageVersion: packageJson.version,\n});\n\nexport function getRuntimeInstanceTelemetryInfo(\n options: CreateCopilotRuntimeServerOptions,\n): RuntimeInstanceCreatedInfo {\n const runtime = options.runtime;\n const remoteEndpoints = runtime.params?.remoteEndpoints ?? [];\n const endpointsInfo = remoteEndpoints.reduce(\n (acc, endpoint) => {\n let info = { ...acc };\n\n const endpointType = resolveEndpointType(endpoint);\n if (!info.endpointTypes.includes(endpointType)) {\n info = {\n ...info,\n endpointTypes: [...info.endpointTypes, endpointType],\n };\n }\n\n if (endpointType === EndpointType.LangGraphPlatform) {\n // When type is resolved, recreating a const with casting of type\n const ep = endpoint as LangGraphPlatformEndpoint;\n info = {\n ...info,\n agentsAmount: ep.agents.length,\n hashedKey: ep.langsmithApiKey\n ? createHash(\"sha256\").update(ep.langsmithApiKey).digest(\"hex\")\n : null,\n };\n }\n\n return info;\n },\n { endpointTypes: [], agentsAmount: null, hashedKey: null },\n );\n\n // Get public API key from options.cloud.publicApiKey\n const publicApiKey = options.cloud?.publicApiKey;\n const apiKeyProvided = !!publicApiKey && publicApiKey.trim().length > 0;\n\n return {\n actionsAmount: runtime.params?.actions?.length ?? 0,\n endpointsAmount: remoteEndpoints.length,\n endpointTypes: endpointsInfo.endpointTypes,\n agentsAmount: Object.keys(runtime.instance.agents).length,\n hashedLgcKey: endpointsInfo.hashedKey,\n \"cloud.api_key_provided\": apiKeyProvided,\n ...(apiKeyProvided ? { \"cloud.public_api_key\": publicApiKey } : {}),\n ...(options.cloud?.baseUrl\n ? { \"cloud.base_url\": options.cloud.baseUrl }\n : {}),\n } as RuntimeInstanceCreatedInfo;\n}\n\nexport default telemetryClient;\n"],"mappings":";;;;;;;;AAOA,MAAM;AAEN,MAAM,kBAAkB,IAAI,gBAAgB;CAC1C,aAAa,YAAY;CACzB,gBAAgB,YAAY;CAC7B,CAAC;AAEF,SAAgB,gCACd,SAC4B;CAC5B,MAAM,UAAU,QAAQ;CACxB,MAAM,kBAAkB,QAAQ,QAAQ,mBAAmB,EAAE;CAC7D,MAAM,gBAAgB,gBAAgB,QACnC,KAAK,aAAa;EACjB,IAAI,OAAO,EAAE,GAAG,KAAK;EAErB,MAAM,eAAe,oBAAoB,SAAS;AAClD,MAAI,CAAC,KAAK,cAAc,SAAS,aAAa,CAC5C,QAAO;GACL,GAAG;GACH,eAAe,CAAC,GAAG,KAAK,eAAe,aAAa;GACrD;AAGH,MAAI,iBAAiB,aAAa,mBAAmB;GAEnD,MAAM,KAAK;AACX,UAAO;IACL,GAAG;IACH,cAAc,GAAG,OAAO;IACxB,WAAW,GAAG,kBACV,WAAW,SAAS,CAAC,OAAO,GAAG,gBAAgB,CAAC,OAAO,MAAM,GAC7D;IACL;;AAGH,SAAO;IAET;EAAE,eAAe,EAAE;EAAE,cAAc;EAAM,WAAW;EAAM,CAC3D;CAGD,MAAM,eAAe,QAAQ,OAAO;CACpC,MAAM,iBAAiB,CAAC,CAAC,gBAAgB,aAAa,MAAM,CAAC,SAAS;AAEtE,QAAO;EACL,eAAe,QAAQ,QAAQ,SAAS,UAAU;EAClD,iBAAiB,gBAAgB;EACjC,eAAe,cAAc;EAC7B,cAAc,OAAO,KAAK,QAAQ,SAAS,OAAO,CAAC;EACnD,cAAc,cAAc;EAC5B,0BAA0B;EAC1B,GAAI,iBAAiB,EAAE,wBAAwB,cAAc,GAAG,EAAE;EAClE,GAAI,QAAQ,OAAO,UACf,EAAE,kBAAkB,QAAQ,MAAM,SAAS,GAC3C,EAAE;EACP"}
package/dist/package.cjs CHANGED
@@ -5,7 +5,7 @@ const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
5
5
  var require_package = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, module) => {
6
6
  module.exports = {
7
7
  "name": "@copilotkit/runtime",
8
- "version": "1.71.1",
8
+ "version": "1.71.2",
9
9
  "private": false,
10
10
  "keywords": [
11
11
  "ai",
@@ -83,13 +83,14 @@ var require_package = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, m
83
83
  "@ag-ui/core": "0.0.59",
84
84
  "@ag-ui/encoder": "0.0.59",
85
85
  "@ag-ui/langgraph": "0.0.43",
86
- "@ag-ui/mcp-apps-middleware": "0.0.3",
86
+ "@ag-ui/mcp-apps-middleware": "^0.1.1",
87
87
  "@ag-ui/mcp-middleware": "0.0.2",
88
88
  "@ai-sdk/anthropic": "^3.0.49",
89
89
  "@ai-sdk/google": "^3.0.33",
90
90
  "@ai-sdk/google-vertex": "^3.0.97",
91
91
  "@ai-sdk/mcp": "^1.0.21",
92
92
  "@ai-sdk/openai": "^3.0.36",
93
+ "@cfworker/json-schema": "^4.1.1",
93
94
  "@copilotkit/channels-core": "workspace:^",
94
95
  "@copilotkit/channels-intelligence": "workspace:*",
95
96
  "@copilotkit/license-verifier": "~0.5.0",
@@ -115,7 +116,7 @@ var require_package = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, m
115
116
  "openai": "^4.85.1 || >=5.0.0",
116
117
  "partial-json": "^0.1.7",
117
118
  "phoenix": "^1.8.4",
118
- "pino": "^9.2.0",
119
+ "pino": "^10.1.1",
119
120
  "pino-pretty": "^11.2.1",
120
121
  "reflect-metadata": "^0.2.2",
121
122
  "rxjs": "7.8.1",
@@ -169,7 +170,8 @@ var require_package = /* @__PURE__ */ require_runtime.__commonJSMin(((exports, m
169
170
  "langchain": { "optional": true },
170
171
  "openai": { "optional": true }
171
172
  },
172
- "nx": { "targets": { "build": { "dependsOn": ["generate-graphql-schema", "^build"] } } }
173
+ "nx": { "targets": { "build": { "dependsOn": ["generate-graphql-schema", "^build"] } } },
174
+ "engines": { "node": ">=20" }
173
175
  };
174
176
  }));
175
177
 
package/dist/package.mjs CHANGED
@@ -5,7 +5,7 @@ import { __commonJSMin } from "./_virtual/_rolldown/runtime.mjs";
5
5
  var require_package = /* @__PURE__ */ __commonJSMin(((exports, module) => {
6
6
  module.exports = {
7
7
  "name": "@copilotkit/runtime",
8
- "version": "1.71.1",
8
+ "version": "1.71.2",
9
9
  "private": false,
10
10
  "keywords": [
11
11
  "ai",
@@ -83,13 +83,14 @@ var require_package = /* @__PURE__ */ __commonJSMin(((exports, module) => {
83
83
  "@ag-ui/core": "0.0.59",
84
84
  "@ag-ui/encoder": "0.0.59",
85
85
  "@ag-ui/langgraph": "0.0.43",
86
- "@ag-ui/mcp-apps-middleware": "0.0.3",
86
+ "@ag-ui/mcp-apps-middleware": "^0.1.1",
87
87
  "@ag-ui/mcp-middleware": "0.0.2",
88
88
  "@ai-sdk/anthropic": "^3.0.49",
89
89
  "@ai-sdk/google": "^3.0.33",
90
90
  "@ai-sdk/google-vertex": "^3.0.97",
91
91
  "@ai-sdk/mcp": "^1.0.21",
92
92
  "@ai-sdk/openai": "^3.0.36",
93
+ "@cfworker/json-schema": "^4.1.1",
93
94
  "@copilotkit/channels-core": "workspace:^",
94
95
  "@copilotkit/channels-intelligence": "workspace:*",
95
96
  "@copilotkit/license-verifier": "~0.5.0",
@@ -115,7 +116,7 @@ var require_package = /* @__PURE__ */ __commonJSMin(((exports, module) => {
115
116
  "openai": "^4.85.1 || >=5.0.0",
116
117
  "partial-json": "^0.1.7",
117
118
  "phoenix": "^1.8.4",
118
- "pino": "^9.2.0",
119
+ "pino": "^10.1.1",
119
120
  "pino-pretty": "^11.2.1",
120
121
  "reflect-metadata": "^0.2.2",
121
122
  "rxjs": "7.8.1",
@@ -169,7 +170,8 @@ var require_package = /* @__PURE__ */ __commonJSMin(((exports, module) => {
169
170
  "langchain": { "optional": true },
170
171
  "openai": { "optional": true }
171
172
  },
172
- "nx": { "targets": { "build": { "dependsOn": ["generate-graphql-schema", "^build"] } } }
173
+ "nx": { "targets": { "build": { "dependsOn": ["generate-graphql-schema", "^build"] } } },
174
+ "engines": { "node": ">=20" }
173
175
  };
174
176
  }));
175
177
 
@@ -139,6 +139,17 @@ var AnthropicAdapter = class {
139
139
  let currentToolCallId = (0, _copilotkit_shared.randomId)();
140
140
  let filterThinkingTextBuffer = new FilterThinkingTextBuffer();
141
141
  let hasReceivedContent = false;
142
+ const emitText = (text) => {
143
+ if (text.length === 0) return;
144
+ if (!didOutputText) {
145
+ eventStream$.sendTextMessageStart({ messageId: currentMessageId });
146
+ didOutputText = true;
147
+ }
148
+ eventStream$.sendTextMessageContent({
149
+ messageId: currentMessageId,
150
+ content: text
151
+ });
152
+ };
142
153
  try {
143
154
  for await (const chunk of stream) if (chunk.type === "message_start") currentMessageId = chunk.message.id;
144
155
  else if (chunk.type === "content_block_start") {
@@ -163,24 +174,14 @@ var AnthropicAdapter = class {
163
174
  }
164
175
  } else if (chunk.type === "content_block_delta") {
165
176
  if (mode === null) continue;
166
- if (chunk.delta.type === "text_delta") {
167
- const text = filterThinkingTextBuffer.onTextChunk(chunk.delta.text);
168
- if (text.length > 0) {
169
- if (!didOutputText) {
170
- eventStream$.sendTextMessageStart({ messageId: currentMessageId });
171
- didOutputText = true;
172
- }
173
- eventStream$.sendTextMessageContent({
174
- messageId: currentMessageId,
175
- content: text
176
- });
177
- }
178
- } else if (chunk.delta.type === "input_json_delta") eventStream$.sendActionExecutionArgs({
177
+ if (chunk.delta.type === "text_delta") emitText(filterThinkingTextBuffer.onTextChunk(chunk.delta.text));
178
+ else if (chunk.delta.type === "input_json_delta") eventStream$.sendActionExecutionArgs({
179
179
  actionExecutionId: currentToolCallId,
180
180
  args: chunk.delta.partial_json
181
181
  });
182
182
  } else if (chunk.type === "content_block_stop") {
183
183
  if (mode === "message") {
184
+ emitText(filterThinkingTextBuffer.flush());
184
185
  if (didOutputText) eventStream$.sendTextMessageEnd({ messageId: currentMessageId });
185
186
  } else if (mode === "function") eventStream$.sendActionExecutionEnd({ actionExecutionId: currentToolCallId });
186
187
  }
@@ -212,30 +213,63 @@ var AnthropicAdapter = class {
212
213
  };
213
214
  const THINKING_TAG = "<thinking>";
214
215
  const THINKING_TAG_END = "</thinking>";
216
+ /**
217
+ * Strips a `<thinking>…</thinking>` block that opens a streamed text content
218
+ * block, passing everything else through unchanged.
219
+ *
220
+ * Only the START of a content block can be a thinking tag. Text is held back
221
+ * while the block's opening characters could still turn into `<thinking>`;
222
+ * the moment they diverge, everything held back is emitted in one piece and
223
+ * the detector disarms for the rest of the block, so a literal `<thinking>`
224
+ * mentioned later in the answer is never treated as a tag. `flush()` releases
225
+ * whatever is still held back when the content block ends (a block that is
226
+ * just `<`, for example).
227
+ */
215
228
  var FilterThinkingTextBuffer = class {
216
229
  constructor() {
217
- this.didFilterThinkingTag = false;
218
230
  this.buffer = "";
231
+ this.resolved = false;
219
232
  }
220
233
  onTextChunk(text) {
234
+ if (this.resolved) return text;
221
235
  this.buffer += text;
222
- if (this.didFilterThinkingTag) return text;
223
- const potentialTag = this.buffer.slice(0, 10);
224
- if (THINKING_TAG.startsWith(potentialTag)) if (this.buffer.includes(THINKING_TAG_END)) {
225
- const end = this.buffer.indexOf(THINKING_TAG_END);
226
- const filteredText = this.buffer.slice(end + 11);
227
- this.buffer = filteredText;
228
- this.didFilterThinkingTag = true;
229
- return filteredText;
230
- } else return "";
231
- return text;
236
+ if (this.buffer.length < 10) {
237
+ if (THINKING_TAG.startsWith(this.buffer)) return "";
238
+ return this.release();
239
+ }
240
+ if (!this.buffer.startsWith(THINKING_TAG)) return this.release();
241
+ const end = this.buffer.indexOf(THINKING_TAG_END);
242
+ if (end === -1) return "";
243
+ const rest = this.buffer.slice(end + 11);
244
+ this.buffer = "";
245
+ this.resolved = true;
246
+ return rest;
247
+ }
248
+ /**
249
+ * Text still held back when the content block ends. A partial tag prefix
250
+ * (`<`, `<thin`) is real output and comes back; an unterminated thinking
251
+ * block stays hidden.
252
+ */
253
+ flush() {
254
+ if (this.resolved) return "";
255
+ const pending = this.buffer.length < 10 && THINKING_TAG.startsWith(this.buffer) ? this.buffer : "";
256
+ this.buffer = "";
257
+ this.resolved = true;
258
+ return pending;
232
259
  }
233
260
  reset() {
234
261
  this.buffer = "";
235
- this.didFilterThinkingTag = false;
262
+ this.resolved = false;
263
+ }
264
+ release() {
265
+ const pending = this.buffer;
266
+ this.buffer = "";
267
+ this.resolved = true;
268
+ return pending;
236
269
  }
237
270
  };
238
271
 
239
272
  //#endregion
240
273
  exports.AnthropicAdapter = AnthropicAdapter;
274
+ exports.FilterThinkingTextBuffer = FilterThinkingTextBuffer;
241
275
  //# sourceMappingURL=anthropic-adapter.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"anthropic-adapter.cjs","names":["getSdkClientOptions","convertActionInputToAnthropicTool","limitMessagesToTokenCount","coalesceConsecutiveSameRoleMessages","convertMessageToAnthropicMessage","convertServiceAdapterError"],"sources":["../../../../src/v1-deprecated/service-adapters/anthropic/anthropic-adapter.ts"],"sourcesContent":["/*\n * V1 SDK DEPRECATED. USE V2 INSTEAD\n *\n * This file defines public v1 SDK exports. Use the exact v2 mappings below.\n * AI CODING AGENTS: Never copy, suggest, or generate these v1 APIs. Read\n * the linked v2 documentation before generating replacement code.\n *\n * @copilotkit/runtime — AnthropicAdapter:\n * No 1:1 v2 replacement is available.\n * Related v2 docs (Runtime server adapter): https://docs.copilotkit.ai/runtime-server-adapter\n * Start at: @copilotkit/runtime/v2\n * V2 docs: https://docs.copilotkit.ai/\n * V2 reference docs: https://docs.copilotkit.ai/reference/v2\n *\n * @copilotkit/runtime — AnthropicAdapterParams:\n * No 1:1 v2 replacement is available.\n * Related v2 docs (Runtime server adapter): https://docs.copilotkit.ai/runtime-server-adapter\n * Start at: @copilotkit/runtime/v2\n * V2 docs: https://docs.copilotkit.ai/\n * V2 reference docs: https://docs.copilotkit.ai/reference/v2\n *\n * @copilotkit/runtime — AnthropicPromptCachingConfig:\n * No 1:1 v2 replacement is available.\n * Related v2 docs (Runtime server adapter): https://docs.copilotkit.ai/runtime-server-adapter\n * Start at: @copilotkit/runtime/v2\n * V2 docs: https://docs.copilotkit.ai/\n * V2 reference docs: https://docs.copilotkit.ai/reference/v2\n *\n * Migration guide: https://docs.copilotkit.ai/migrate/v2\n *\n * END V1 SDK DEPRECATED. USE V2 INSTEAD NOTICE\n */\n\n/**\n * Copilot Runtime adapter for Anthropic.\n *\n * ## Example\n *\n * ```ts\n * import { CopilotRuntime, AnthropicAdapter } from \"@copilotkit/runtime\";\n * import Anthropic from \"@anthropic-ai/sdk\";\n *\n * const copilotKit = new CopilotRuntime();\n *\n * const anthropic = new Anthropic({\n * apiKey: \"<your-api-key>\",\n * });\n *\n * return new AnthropicAdapter({\n * anthropic,\n * promptCaching: {\n * enabled: true,\n * debug: true\n * }\n * });\n * ```\n */\nimport type { LanguageModel } from \"ai\";\nimport { createAnthropic } from \"@ai-sdk/anthropic\";\nimport type Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n CopilotServiceAdapter,\n CopilotRuntimeChatCompletionRequest,\n CopilotRuntimeChatCompletionResponse,\n} from \"../service-adapter\";\nimport {\n convertActionInputToAnthropicTool,\n convertMessageToAnthropicMessage,\n coalesceConsecutiveSameRoleMessages,\n limitMessagesToTokenCount,\n} from \"./utils\";\n\nimport { randomId, randomUUID } from \"@copilotkit/shared\";\nimport type { SdkClientLike } from \"../shared\";\nimport { convertServiceAdapterError, getSdkClientOptions } from \"../shared\";\n\nconst DEFAULT_MODEL = \"claude-opus-4-8\";\n\nexport interface AnthropicPromptCachingConfig {\n /**\n * Whether to enable prompt caching.\n */\n enabled: boolean;\n\n /**\n * Whether to enable debug logging for cache operations.\n */\n debug?: boolean;\n}\n\nexport interface AnthropicAdapterParams {\n /**\n * An optional Anthropic instance to use. If not provided, a new instance will be\n * created.\n */\n anthropic?: SdkClientLike;\n\n /**\n * The model to use.\n */\n model?: string;\n\n /**\n * Configuration for prompt caching.\n * See: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching\n */\n promptCaching?: AnthropicPromptCachingConfig;\n\n /**\n * Optional maximum input token limit. Overrides the default limit\n * used when trimming messages to fit the context window.\n */\n maxInputTokens?: number;\n}\n\nexport class AnthropicAdapter implements CopilotServiceAdapter {\n public model: string = DEFAULT_MODEL;\n public provider = \"anthropic\";\n private promptCaching: AnthropicPromptCachingConfig;\n private maxInputTokens?: number;\n\n private _anthropic: SdkClientLike;\n public get anthropic(): SdkClientLike {\n return this._anthropic;\n }\n public get name() {\n return \"AnthropicAdapter\";\n }\n\n constructor(params?: AnthropicAdapterParams) {\n if (params?.anthropic) {\n this._anthropic = params.anthropic;\n }\n // If no instance provided, we'll lazy-load in ensureAnthropic()\n if (params?.model) {\n this.model = params.model;\n }\n this.promptCaching = params?.promptCaching || { enabled: false };\n this.maxInputTokens = params?.maxInputTokens;\n }\n\n getLanguageModel(): LanguageModel {\n const anthropic = this.ensureAnthropic();\n const options = getSdkClientOptions(anthropic);\n const provider = createAnthropic({\n baseURL: anthropic.baseURL,\n apiKey: anthropic.apiKey,\n headers: options.defaultHeaders,\n fetch: options.fetch,\n });\n return provider(this.model);\n }\n\n private ensureAnthropic(): SdkClientLike {\n if (!this._anthropic) {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const Anthropic = require(\"@anthropic-ai/sdk\").default;\n this._anthropic = new Anthropic({});\n }\n return this._anthropic;\n }\n\n /**\n * Adds cache control to system prompt\n */\n private addSystemPromptCaching(\n system: string,\n debug: boolean = false,\n ):\n | string\n | Array<{\n type: \"text\";\n text: string;\n cache_control?: { type: \"ephemeral\" };\n }> {\n if (!this.promptCaching.enabled || !system) {\n return system;\n }\n\n const originalTextLength = system.length;\n\n if (debug) {\n console.log(\n `[ANTHROPIC CACHE DEBUG] Added cache control to system prompt (${originalTextLength} chars).`,\n );\n }\n\n return [\n {\n type: \"text\",\n text: system,\n cache_control: { type: \"ephemeral\" },\n },\n ];\n }\n\n /**\n * Adds cache control to the final message\n */\n private addIncrementalMessageCaching(\n messages: Anthropic.Messages.MessageParam[],\n debug: boolean = false,\n ): any[] {\n if (!this.promptCaching.enabled || messages.length === 0) {\n return messages;\n }\n\n const finalMessage = messages[messages.length - 1];\n const messageNumber = messages.length;\n\n if (\n Array.isArray(finalMessage.content) &&\n finalMessage.content.length > 0\n ) {\n const finalBlock = finalMessage.content[finalMessage.content.length - 1];\n\n const updatedMessages = [\n ...messages.slice(0, -1),\n {\n ...finalMessage,\n content: [\n ...finalMessage.content.slice(0, -1),\n { ...finalBlock, cache_control: { type: \"ephemeral\" } } as any,\n ],\n },\n ];\n\n if (debug) {\n console.log(\n `[ANTHROPIC CACHE DEBUG] Added cache control to final message (message ${messageNumber}).`,\n );\n }\n\n return updatedMessages;\n }\n\n return messages;\n }\n\n private shouldGenerateFallbackResponse(\n messages: Anthropic.Messages.MessageParam[],\n ): boolean {\n if (messages.length === 0) return false;\n\n const lastMessage = messages[messages.length - 1];\n\n // Check if the last message is a tool result\n const endsWithToolResult =\n lastMessage.role === \"user\" &&\n Array.isArray(lastMessage.content) &&\n lastMessage.content.some(\n (content: any) => content.type === \"tool_result\",\n );\n\n // Also check if we have a recent pattern of user message -> assistant tool use -> user tool result\n // This indicates a completed action that might not need a response\n if (messages.length >= 3 && endsWithToolResult) {\n const lastThree = messages.slice(-3);\n const hasRecentToolPattern =\n lastThree[0]?.role === \"user\" && // Initial user message\n lastThree[1]?.role === \"assistant\" && // Assistant tool use\n Array.isArray(lastThree[1].content) &&\n lastThree[1].content.some(\n (content: any) => content.type === \"tool_use\",\n ) &&\n lastThree[2]?.role === \"user\" && // Tool result\n Array.isArray(lastThree[2].content) &&\n lastThree[2].content.some(\n (content: any) => content.type === \"tool_result\",\n );\n\n return hasRecentToolPattern;\n }\n\n return endsWithToolResult;\n }\n\n async process(\n request: CopilotRuntimeChatCompletionRequest,\n ): Promise<CopilotRuntimeChatCompletionResponse> {\n const {\n threadId,\n model = this.model,\n messages: rawMessages,\n actions,\n eventSource,\n forwardedParameters,\n } = request;\n const tools = actions.map(convertActionInputToAnthropicTool);\n const knownActionNames = new Set(actions.map((a) => a.name));\n\n const messages = [...rawMessages];\n\n // get the instruction message\n const instructionsMessage = messages.shift();\n const instructions = instructionsMessage.isTextMessage()\n ? instructionsMessage.content\n : \"\";\n\n // ALLOWLIST APPROACH:\n // 1. First, identify all valid tool_use calls (from assistant)\n // 2. Then, only keep tool_result blocks that correspond to these valid tool_use IDs\n // 3. Discard any other tool_result blocks\n\n // Step 1: Extract valid tool_use IDs\n const validToolUseIds = new Set<string>();\n\n for (const message of messages) {\n if (message.isActionExecutionMessage()) {\n validToolUseIds.add(message.id);\n }\n }\n\n // Step 2: Map each message to an Anthropic message, eliminating invalid tool_results\n const processedToolResultIds = new Set<string>();\n const anthropicMessages = messages\n .map((message) => {\n // For tool results, only include if they match a valid tool_use ID AND haven't been processed\n if (message.isResultMessage()) {\n // Skip if there's no corresponding tool_use\n if (!validToolUseIds.has(message.actionExecutionId)) {\n return null; // Will be filtered out later\n }\n\n // Skip if we've already processed a result for this tool_use ID\n if (processedToolResultIds.has(message.actionExecutionId)) {\n return null; // Will be filtered out later\n }\n\n // Mark this tool result as processed\n processedToolResultIds.add(message.actionExecutionId);\n\n return {\n role: \"user\",\n content: [\n {\n type: \"tool_result\",\n content: message.result || \"Action completed successfully\",\n tool_use_id: message.actionExecutionId,\n },\n ],\n };\n }\n\n // For non-tool-result messages, convert normally\n return convertMessageToAnthropicMessage(message);\n })\n .filter(Boolean) // Remove nulls\n .filter((msg) => {\n // Filter out assistant messages with empty text content\n if (msg.role === \"assistant\" && Array.isArray(msg.content)) {\n const hasEmptyTextOnly =\n msg.content.length === 1 &&\n msg.content[0].type === \"text\" &&\n (!(msg.content[0] as any).text ||\n (msg.content[0] as any).text.trim() === \"\");\n\n // Keep messages that have tool_use or non-empty text\n return !hasEmptyTextOnly;\n }\n return true;\n }) as Anthropic.Messages.MessageParam[];\n\n // Coalesce consecutive same-role messages so the Anthropic API receives alternating turns.\n // A mixed text+tool_use response is stored as separate TextMessage / ActionExecutionMessage\n // objects; they must be merged into one assistant message before dispatch.\n const coalescedMessages =\n coalesceConsecutiveSameRoleMessages(anthropicMessages);\n\n // Apply token limits\n const limitedMessages = limitMessagesToTokenCount(\n coalescedMessages,\n tools,\n model,\n this.maxInputTokens,\n );\n\n // Apply prompt caching if enabled\n const cachedSystemPrompt = this.addSystemPromptCaching(\n instructions,\n this.promptCaching.debug,\n );\n const cachedMessages = this.addIncrementalMessageCaching(\n limitedMessages,\n this.promptCaching.debug,\n );\n\n // We'll check if we need a fallback response after seeing what Anthropic returns\n // Role alternation is now enforced by coalesceConsecutiveSameRoleMessages above.\n\n let toolChoice: any = forwardedParameters?.toolChoice;\n if (forwardedParameters?.toolChoice === \"function\") {\n toolChoice = {\n type: \"tool\",\n name: forwardedParameters.toolChoiceFunctionName,\n };\n }\n\n try {\n const createParams = {\n system: cachedSystemPrompt,\n model,\n messages: cachedMessages,\n max_tokens: forwardedParameters?.maxTokens || 4096,\n ...(forwardedParameters?.temperature\n ? { temperature: forwardedParameters.temperature }\n : {}),\n ...(tools.length > 0 && { tools }),\n ...(toolChoice && { tool_choice: toolChoice }),\n stream: true,\n };\n\n // The public surface is structural so consumers who skip the optional\n // peer can still compile (see SdkClientLike); the SDK type is only\n // reachable here, inside a function body, where it is never emitted.\n const anthropic = this.ensureAnthropic() as Anthropic;\n const stream = await anthropic.messages.create(createParams);\n\n eventSource.stream(async (eventStream$) => {\n let mode: \"function\" | \"message\" | null = null;\n let didOutputText = false;\n let currentMessageId = randomId();\n let currentToolCallId = randomId();\n let filterThinkingTextBuffer = new FilterThinkingTextBuffer();\n let hasReceivedContent = false;\n\n try {\n for await (const chunk of stream as AsyncIterable<any>) {\n if (chunk.type === \"message_start\") {\n currentMessageId = chunk.message.id;\n } else if (chunk.type === \"content_block_start\") {\n if (chunk.content_block.type === \"text\") {\n hasReceivedContent = true;\n didOutputText = false;\n filterThinkingTextBuffer.reset();\n mode = \"message\";\n } else if (chunk.content_block.type === \"tool_use\") {\n if (!knownActionNames.has(chunk.content_block.name)) {\n // Unknown tool - skip execution to prevent crashes\n mode = null;\n continue;\n }\n hasReceivedContent = true;\n currentToolCallId = chunk.content_block.id;\n eventStream$.sendActionExecutionStart({\n actionExecutionId: currentToolCallId,\n actionName: chunk.content_block.name,\n parentMessageId: currentMessageId,\n });\n mode = \"function\";\n }\n } else if (chunk.type === \"content_block_delta\") {\n if (mode === null) {\n // Skip deltas for unknown/skipped content blocks\n continue;\n }\n if (chunk.delta.type === \"text_delta\") {\n const text = filterThinkingTextBuffer.onTextChunk(\n chunk.delta.text,\n );\n if (text.length > 0) {\n if (!didOutputText) {\n eventStream$.sendTextMessageStart({\n messageId: currentMessageId,\n });\n didOutputText = true;\n }\n eventStream$.sendTextMessageContent({\n messageId: currentMessageId,\n content: text,\n });\n }\n } else if (chunk.delta.type === \"input_json_delta\") {\n eventStream$.sendActionExecutionArgs({\n actionExecutionId: currentToolCallId,\n args: chunk.delta.partial_json,\n });\n }\n } else if (chunk.type === \"content_block_stop\") {\n if (mode === \"message\") {\n if (didOutputText) {\n eventStream$.sendTextMessageEnd({\n messageId: currentMessageId,\n });\n }\n } else if (mode === \"function\") {\n eventStream$.sendActionExecutionEnd({\n actionExecutionId: currentToolCallId,\n });\n }\n }\n }\n } catch (error) {\n throw convertServiceAdapterError(error, \"Anthropic\");\n }\n\n // Generate fallback response only if Anthropic produced no content\n if (\n !hasReceivedContent &&\n this.shouldGenerateFallbackResponse(cachedMessages)\n ) {\n // Extract the tool result content for a more contextual response\n let fallbackContent = \"Task completed successfully.\";\n const lastMessage = cachedMessages[cachedMessages.length - 1];\n if (\n lastMessage?.role === \"user\" &&\n Array.isArray(lastMessage.content)\n ) {\n const toolResult = lastMessage.content.find(\n (c: any) => c.type === \"tool_result\",\n );\n if (\n toolResult?.content &&\n toolResult.content !== \"Action completed successfully\"\n ) {\n fallbackContent = toolResult.content;\n }\n }\n\n currentMessageId = randomId();\n eventStream$.sendTextMessageStart({ messageId: currentMessageId });\n eventStream$.sendTextMessageContent({\n messageId: currentMessageId,\n content: fallbackContent,\n });\n eventStream$.sendTextMessageEnd({ messageId: currentMessageId });\n }\n\n eventStream$.complete();\n });\n } catch (error) {\n throw convertServiceAdapterError(error, \"Anthropic\");\n }\n\n return {\n threadId: threadId || randomUUID(),\n };\n }\n}\n\nconst THINKING_TAG = \"<thinking>\";\nconst THINKING_TAG_END = \"</thinking>\";\n\nclass FilterThinkingTextBuffer {\n private buffer: string;\n private didFilterThinkingTag: boolean = false;\n\n constructor() {\n this.buffer = \"\";\n }\n\n onTextChunk(text: string): string {\n this.buffer += text;\n if (this.didFilterThinkingTag) {\n return text;\n }\n const potentialTag = this.buffer.slice(0, THINKING_TAG.length);\n if (THINKING_TAG.startsWith(potentialTag)) {\n if (this.buffer.includes(THINKING_TAG_END)) {\n const end = this.buffer.indexOf(THINKING_TAG_END);\n const filteredText = this.buffer.slice(end + THINKING_TAG_END.length);\n this.buffer = filteredText;\n this.didFilterThinkingTag = true;\n return filteredText;\n } else {\n return \"\";\n }\n }\n return text;\n }\n\n reset() {\n this.buffer = \"\";\n this.didFilterThinkingTag = false;\n }\n}\n"],"mappings":";;;;;;;;;AA4EA,MAAM,gBAAgB;AAuCtB,IAAa,mBAAb,MAA+D;CAO7D,IAAW,YAA2B;AACpC,SAAO,KAAK;;CAEd,IAAW,OAAO;AAChB,SAAO;;CAGT,YAAY,QAAiC;eAbtB;kBACL;AAahB,MAAI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,SAAS;AAC3D,SACE,aAAa,SAAS,UACtB,MAAM,QAAQ,YAAY,QAAQ,EAClC;MACA,MAAM,aAAa,YAAY,QAAQ,MACpC,MAAW,EAAE,SAAS,cACxB;AACD,UACE,YAAY,WACZ,WAAW,YAAY,gCAEvB,mBAAkB,WAAW;;AAIjC,0DAA6B;AAC7B,kBAAa,qBAAqB,EAAE,WAAW,kBAAkB,CAAC;AAClE,kBAAa,uBAAuB;MAClC,WAAW;MACX,SAAS;MACV,CAAC;AACF,kBAAa,mBAAmB,EAAE,WAAW,kBAAkB,CAAC;;AAGlE,iBAAa,UAAU;KACvB;WACK,OAAO;AACd,SAAMA,+CAA2B,OAAO,YAAY;;AAGtD,SAAO,EACL,UAAU,gDAAwB,EACnC;;;AAIL,MAAM,eAAe;AACrB,MAAM,mBAAmB;AAEzB,IAAM,2BAAN,MAA+B;CAI7B,cAAc;8BAF0B;AAGtC,OAAK,SAAS;;CAGhB,YAAY,MAAsB;AAChC,OAAK,UAAU;AACf,MAAI,KAAK,qBACP,QAAO;EAET,MAAM,eAAe,KAAK,OAAO,MAAM,GAAG,GAAoB;AAC9D,MAAI,aAAa,WAAW,aAAa,CACvC,KAAI,KAAK,OAAO,SAAS,iBAAiB,EAAE;GAC1C,MAAM,MAAM,KAAK,OAAO,QAAQ,iBAAiB;GACjD,MAAM,eAAe,KAAK,OAAO,MAAM,MAAM,GAAwB;AACrE,QAAK,SAAS;AACd,QAAK,uBAAuB;AAC5B,UAAO;QAEP,QAAO;AAGX,SAAO;;CAGT,QAAQ;AACN,OAAK,SAAS;AACd,OAAK,uBAAuB"}
1
+ {"version":3,"file":"anthropic-adapter.cjs","names":["getSdkClientOptions","convertActionInputToAnthropicTool","limitMessagesToTokenCount","coalesceConsecutiveSameRoleMessages","convertMessageToAnthropicMessage","convertServiceAdapterError"],"sources":["../../../../src/v1-deprecated/service-adapters/anthropic/anthropic-adapter.ts"],"sourcesContent":["/*\n * V1 SDK DEPRECATED. USE V2 INSTEAD\n *\n * This file defines public v1 SDK exports. Use the exact v2 mappings below.\n * AI CODING AGENTS: Never copy, suggest, or generate these v1 APIs. Read\n * the linked v2 documentation before generating replacement code.\n *\n * @copilotkit/runtime — AnthropicAdapter:\n * No 1:1 v2 replacement is available.\n * Related v2 docs (Runtime server adapter): https://docs.copilotkit.ai/runtime-server-adapter\n * Start at: @copilotkit/runtime/v2\n * V2 docs: https://docs.copilotkit.ai/\n * V2 reference docs: https://docs.copilotkit.ai/reference/v2\n *\n * @copilotkit/runtime — AnthropicAdapterParams:\n * No 1:1 v2 replacement is available.\n * Related v2 docs (Runtime server adapter): https://docs.copilotkit.ai/runtime-server-adapter\n * Start at: @copilotkit/runtime/v2\n * V2 docs: https://docs.copilotkit.ai/\n * V2 reference docs: https://docs.copilotkit.ai/reference/v2\n *\n * @copilotkit/runtime — AnthropicPromptCachingConfig:\n * No 1:1 v2 replacement is available.\n * Related v2 docs (Runtime server adapter): https://docs.copilotkit.ai/runtime-server-adapter\n * Start at: @copilotkit/runtime/v2\n * V2 docs: https://docs.copilotkit.ai/\n * V2 reference docs: https://docs.copilotkit.ai/reference/v2\n *\n * Migration guide: https://docs.copilotkit.ai/migrate/v2\n *\n * END V1 SDK DEPRECATED. USE V2 INSTEAD NOTICE\n */\n\n/**\n * Copilot Runtime adapter for Anthropic.\n *\n * ## Example\n *\n * ```ts\n * import { CopilotRuntime, AnthropicAdapter } from \"@copilotkit/runtime\";\n * import Anthropic from \"@anthropic-ai/sdk\";\n *\n * const copilotKit = new CopilotRuntime();\n *\n * const anthropic = new Anthropic({\n * apiKey: \"<your-api-key>\",\n * });\n *\n * return new AnthropicAdapter({\n * anthropic,\n * promptCaching: {\n * enabled: true,\n * debug: true\n * }\n * });\n * ```\n */\nimport type { LanguageModel } from \"ai\";\nimport { createAnthropic } from \"@ai-sdk/anthropic\";\nimport type Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n CopilotServiceAdapter,\n CopilotRuntimeChatCompletionRequest,\n CopilotRuntimeChatCompletionResponse,\n} from \"../service-adapter\";\nimport {\n convertActionInputToAnthropicTool,\n convertMessageToAnthropicMessage,\n coalesceConsecutiveSameRoleMessages,\n limitMessagesToTokenCount,\n} from \"./utils\";\n\nimport { randomId, randomUUID } from \"@copilotkit/shared\";\nimport type { SdkClientLike } from \"../shared\";\nimport { convertServiceAdapterError, getSdkClientOptions } from \"../shared\";\n\nconst DEFAULT_MODEL = \"claude-opus-4-8\";\n\nexport interface AnthropicPromptCachingConfig {\n /**\n * Whether to enable prompt caching.\n */\n enabled: boolean;\n\n /**\n * Whether to enable debug logging for cache operations.\n */\n debug?: boolean;\n}\n\nexport interface AnthropicAdapterParams {\n /**\n * An optional Anthropic instance to use. If not provided, a new instance will be\n * created.\n */\n anthropic?: SdkClientLike;\n\n /**\n * The model to use.\n */\n model?: string;\n\n /**\n * Configuration for prompt caching.\n * See: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching\n */\n promptCaching?: AnthropicPromptCachingConfig;\n\n /**\n * Optional maximum input token limit. Overrides the default limit\n * used when trimming messages to fit the context window.\n */\n maxInputTokens?: number;\n}\n\nexport class AnthropicAdapter implements CopilotServiceAdapter {\n public model: string = DEFAULT_MODEL;\n public provider = \"anthropic\";\n private promptCaching: AnthropicPromptCachingConfig;\n private maxInputTokens?: number;\n\n private _anthropic: SdkClientLike;\n public get anthropic(): SdkClientLike {\n return this._anthropic;\n }\n public get name() {\n return \"AnthropicAdapter\";\n }\n\n constructor(params?: AnthropicAdapterParams) {\n if (params?.anthropic) {\n this._anthropic = params.anthropic;\n }\n // If no instance provided, we'll lazy-load in ensureAnthropic()\n if (params?.model) {\n this.model = params.model;\n }\n this.promptCaching = params?.promptCaching || { enabled: false };\n this.maxInputTokens = params?.maxInputTokens;\n }\n\n getLanguageModel(): LanguageModel {\n const anthropic = this.ensureAnthropic();\n const options = getSdkClientOptions(anthropic);\n const provider = createAnthropic({\n baseURL: anthropic.baseURL,\n apiKey: anthropic.apiKey,\n headers: options.defaultHeaders,\n fetch: options.fetch,\n });\n return provider(this.model);\n }\n\n private ensureAnthropic(): SdkClientLike {\n if (!this._anthropic) {\n // eslint-disable-next-line @typescript-eslint/no-var-requires\n const Anthropic = require(\"@anthropic-ai/sdk\").default;\n this._anthropic = new Anthropic({});\n }\n return this._anthropic;\n }\n\n /**\n * Adds cache control to system prompt\n */\n private addSystemPromptCaching(\n system: string,\n debug: boolean = false,\n ):\n | string\n | Array<{\n type: \"text\";\n text: string;\n cache_control?: { type: \"ephemeral\" };\n }> {\n if (!this.promptCaching.enabled || !system) {\n return system;\n }\n\n const originalTextLength = system.length;\n\n if (debug) {\n console.log(\n `[ANTHROPIC CACHE DEBUG] Added cache control to system prompt (${originalTextLength} chars).`,\n );\n }\n\n return [\n {\n type: \"text\",\n text: system,\n cache_control: { type: \"ephemeral\" },\n },\n ];\n }\n\n /**\n * Adds cache control to the final message\n */\n private addIncrementalMessageCaching(\n messages: Anthropic.Messages.MessageParam[],\n debug: boolean = false,\n ): any[] {\n if (!this.promptCaching.enabled || messages.length === 0) {\n return messages;\n }\n\n const finalMessage = messages[messages.length - 1];\n const messageNumber = messages.length;\n\n if (\n Array.isArray(finalMessage.content) &&\n finalMessage.content.length > 0\n ) {\n const finalBlock = finalMessage.content[finalMessage.content.length - 1];\n\n const updatedMessages = [\n ...messages.slice(0, -1),\n {\n ...finalMessage,\n content: [\n ...finalMessage.content.slice(0, -1),\n { ...finalBlock, cache_control: { type: \"ephemeral\" } } as any,\n ],\n },\n ];\n\n if (debug) {\n console.log(\n `[ANTHROPIC CACHE DEBUG] Added cache control to final message (message ${messageNumber}).`,\n );\n }\n\n return updatedMessages;\n }\n\n return messages;\n }\n\n private shouldGenerateFallbackResponse(\n messages: Anthropic.Messages.MessageParam[],\n ): boolean {\n if (messages.length === 0) return false;\n\n const lastMessage = messages[messages.length - 1];\n\n // Check if the last message is a tool result\n const endsWithToolResult =\n lastMessage.role === \"user\" &&\n Array.isArray(lastMessage.content) &&\n lastMessage.content.some(\n (content: any) => content.type === \"tool_result\",\n );\n\n // Also check if we have a recent pattern of user message -> assistant tool use -> user tool result\n // This indicates a completed action that might not need a response\n if (messages.length >= 3 && endsWithToolResult) {\n const lastThree = messages.slice(-3);\n const hasRecentToolPattern =\n lastThree[0]?.role === \"user\" && // Initial user message\n lastThree[1]?.role === \"assistant\" && // Assistant tool use\n Array.isArray(lastThree[1].content) &&\n lastThree[1].content.some(\n (content: any) => content.type === \"tool_use\",\n ) &&\n lastThree[2]?.role === \"user\" && // Tool result\n Array.isArray(lastThree[2].content) &&\n lastThree[2].content.some(\n (content: any) => content.type === \"tool_result\",\n );\n\n return hasRecentToolPattern;\n }\n\n return endsWithToolResult;\n }\n\n async process(\n request: CopilotRuntimeChatCompletionRequest,\n ): Promise<CopilotRuntimeChatCompletionResponse> {\n const {\n threadId,\n model = this.model,\n messages: rawMessages,\n actions,\n eventSource,\n forwardedParameters,\n } = request;\n const tools = actions.map(convertActionInputToAnthropicTool);\n const knownActionNames = new Set(actions.map((a) => a.name));\n\n const messages = [...rawMessages];\n\n // get the instruction message\n const instructionsMessage = messages.shift();\n const instructions = instructionsMessage.isTextMessage()\n ? instructionsMessage.content\n : \"\";\n\n // ALLOWLIST APPROACH:\n // 1. First, identify all valid tool_use calls (from assistant)\n // 2. Then, only keep tool_result blocks that correspond to these valid tool_use IDs\n // 3. Discard any other tool_result blocks\n\n // Step 1: Extract valid tool_use IDs\n const validToolUseIds = new Set<string>();\n\n for (const message of messages) {\n if (message.isActionExecutionMessage()) {\n validToolUseIds.add(message.id);\n }\n }\n\n // Step 2: Map each message to an Anthropic message, eliminating invalid tool_results\n const processedToolResultIds = new Set<string>();\n const anthropicMessages = messages\n .map((message) => {\n // For tool results, only include if they match a valid tool_use ID AND haven't been processed\n if (message.isResultMessage()) {\n // Skip if there's no corresponding tool_use\n if (!validToolUseIds.has(message.actionExecutionId)) {\n return null; // Will be filtered out later\n }\n\n // Skip if we've already processed a result for this tool_use ID\n if (processedToolResultIds.has(message.actionExecutionId)) {\n return null; // Will be filtered out later\n }\n\n // Mark this tool result as processed\n processedToolResultIds.add(message.actionExecutionId);\n\n return {\n role: \"user\",\n content: [\n {\n type: \"tool_result\",\n content: message.result || \"Action completed successfully\",\n tool_use_id: message.actionExecutionId,\n },\n ],\n };\n }\n\n // For non-tool-result messages, convert normally\n return convertMessageToAnthropicMessage(message);\n })\n .filter(Boolean) // Remove nulls\n .filter((msg) => {\n // Filter out assistant messages with empty text content\n if (msg.role === \"assistant\" && Array.isArray(msg.content)) {\n const hasEmptyTextOnly =\n msg.content.length === 1 &&\n msg.content[0].type === \"text\" &&\n (!(msg.content[0] as any).text ||\n (msg.content[0] as any).text.trim() === \"\");\n\n // Keep messages that have tool_use or non-empty text\n return !hasEmptyTextOnly;\n }\n return true;\n }) as Anthropic.Messages.MessageParam[];\n\n // Coalesce consecutive same-role messages so the Anthropic API receives alternating turns.\n // A mixed text+tool_use response is stored as separate TextMessage / ActionExecutionMessage\n // objects; they must be merged into one assistant message before dispatch.\n const coalescedMessages =\n coalesceConsecutiveSameRoleMessages(anthropicMessages);\n\n // Apply token limits\n const limitedMessages = limitMessagesToTokenCount(\n coalescedMessages,\n tools,\n model,\n this.maxInputTokens,\n );\n\n // Apply prompt caching if enabled\n const cachedSystemPrompt = this.addSystemPromptCaching(\n instructions,\n this.promptCaching.debug,\n );\n const cachedMessages = this.addIncrementalMessageCaching(\n limitedMessages,\n this.promptCaching.debug,\n );\n\n // We'll check if we need a fallback response after seeing what Anthropic returns\n // Role alternation is now enforced by coalesceConsecutiveSameRoleMessages above.\n\n let toolChoice: any = forwardedParameters?.toolChoice;\n if (forwardedParameters?.toolChoice === \"function\") {\n toolChoice = {\n type: \"tool\",\n name: forwardedParameters.toolChoiceFunctionName,\n };\n }\n\n try {\n const createParams = {\n system: cachedSystemPrompt,\n model,\n messages: cachedMessages,\n max_tokens: forwardedParameters?.maxTokens || 4096,\n ...(forwardedParameters?.temperature\n ? { temperature: forwardedParameters.temperature }\n : {}),\n ...(tools.length > 0 && { tools }),\n ...(toolChoice && { tool_choice: toolChoice }),\n stream: true,\n };\n\n // The public surface is structural so consumers who skip the optional\n // peer can still compile (see SdkClientLike); the SDK type is only\n // reachable here, inside a function body, where it is never emitted.\n const anthropic = this.ensureAnthropic() as Anthropic;\n const stream = await anthropic.messages.create(createParams);\n\n eventSource.stream(async (eventStream$) => {\n let mode: \"function\" | \"message\" | null = null;\n let didOutputText = false;\n let currentMessageId = randomId();\n let currentToolCallId = randomId();\n let filterThinkingTextBuffer = new FilterThinkingTextBuffer();\n let hasReceivedContent = false;\n\n const emitText = (text: string) => {\n if (text.length === 0) {\n return;\n }\n if (!didOutputText) {\n eventStream$.sendTextMessageStart({\n messageId: currentMessageId,\n });\n didOutputText = true;\n }\n eventStream$.sendTextMessageContent({\n messageId: currentMessageId,\n content: text,\n });\n };\n\n try {\n for await (const chunk of stream as AsyncIterable<any>) {\n if (chunk.type === \"message_start\") {\n currentMessageId = chunk.message.id;\n } else if (chunk.type === \"content_block_start\") {\n if (chunk.content_block.type === \"text\") {\n hasReceivedContent = true;\n didOutputText = false;\n filterThinkingTextBuffer.reset();\n mode = \"message\";\n } else if (chunk.content_block.type === \"tool_use\") {\n if (!knownActionNames.has(chunk.content_block.name)) {\n // Unknown tool - skip execution to prevent crashes\n mode = null;\n continue;\n }\n hasReceivedContent = true;\n currentToolCallId = chunk.content_block.id;\n eventStream$.sendActionExecutionStart({\n actionExecutionId: currentToolCallId,\n actionName: chunk.content_block.name,\n parentMessageId: currentMessageId,\n });\n mode = \"function\";\n }\n } else if (chunk.type === \"content_block_delta\") {\n if (mode === null) {\n // Skip deltas for unknown/skipped content blocks\n continue;\n }\n if (chunk.delta.type === \"text_delta\") {\n emitText(\n filterThinkingTextBuffer.onTextChunk(chunk.delta.text),\n );\n } else if (chunk.delta.type === \"input_json_delta\") {\n eventStream$.sendActionExecutionArgs({\n actionExecutionId: currentToolCallId,\n args: chunk.delta.partial_json,\n });\n }\n } else if (chunk.type === \"content_block_stop\") {\n if (mode === \"message\") {\n // Text held back as a possible `<thinking>` prefix is real\n // output once the block ends without resolving it.\n emitText(filterThinkingTextBuffer.flush());\n if (didOutputText) {\n eventStream$.sendTextMessageEnd({\n messageId: currentMessageId,\n });\n }\n } else if (mode === \"function\") {\n eventStream$.sendActionExecutionEnd({\n actionExecutionId: currentToolCallId,\n });\n }\n }\n }\n } catch (error) {\n throw convertServiceAdapterError(error, \"Anthropic\");\n }\n\n // Generate fallback response only if Anthropic produced no content\n if (\n !hasReceivedContent &&\n this.shouldGenerateFallbackResponse(cachedMessages)\n ) {\n // Extract the tool result content for a more contextual response\n let fallbackContent = \"Task completed successfully.\";\n const lastMessage = cachedMessages[cachedMessages.length - 1];\n if (\n lastMessage?.role === \"user\" &&\n Array.isArray(lastMessage.content)\n ) {\n const toolResult = lastMessage.content.find(\n (c: any) => c.type === \"tool_result\",\n );\n if (\n toolResult?.content &&\n toolResult.content !== \"Action completed successfully\"\n ) {\n fallbackContent = toolResult.content;\n }\n }\n\n currentMessageId = randomId();\n eventStream$.sendTextMessageStart({ messageId: currentMessageId });\n eventStream$.sendTextMessageContent({\n messageId: currentMessageId,\n content: fallbackContent,\n });\n eventStream$.sendTextMessageEnd({ messageId: currentMessageId });\n }\n\n eventStream$.complete();\n });\n } catch (error) {\n throw convertServiceAdapterError(error, \"Anthropic\");\n }\n\n return {\n threadId: threadId || randomUUID(),\n };\n }\n}\n\nconst THINKING_TAG = \"<thinking>\";\nconst THINKING_TAG_END = \"</thinking>\";\n\n/**\n * Strips a `<thinking>…</thinking>` block that opens a streamed text content\n * block, passing everything else through unchanged.\n *\n * Only the START of a content block can be a thinking tag. Text is held back\n * while the block's opening characters could still turn into `<thinking>`;\n * the moment they diverge, everything held back is emitted in one piece and\n * the detector disarms for the rest of the block, so a literal `<thinking>`\n * mentioned later in the answer is never treated as a tag. `flush()` releases\n * whatever is still held back when the content block ends (a block that is\n * just `<`, for example).\n */\nexport class FilterThinkingTextBuffer {\n private buffer: string = \"\";\n /**\n * True once the block's start is known to be, or not to be, a thinking tag;\n * from then on chunks pass straight through.\n */\n private resolved: boolean = false;\n\n onTextChunk(text: string): string {\n if (this.resolved) {\n return text;\n }\n this.buffer += text;\n\n if (this.buffer.length < THINKING_TAG.length) {\n if (THINKING_TAG.startsWith(this.buffer)) {\n // Still a possible tag prefix — keep holding it back.\n return \"\";\n }\n return this.release();\n }\n\n if (!this.buffer.startsWith(THINKING_TAG)) {\n // Diverged from `<thinking>`: emit the whole held-back text, not just\n // this chunk, and stop looking.\n return this.release();\n }\n\n const end = this.buffer.indexOf(THINKING_TAG_END);\n if (end === -1) {\n // Inside the thinking block — swallow until it closes.\n return \"\";\n }\n const rest = this.buffer.slice(end + THINKING_TAG_END.length);\n this.buffer = \"\";\n this.resolved = true;\n return rest;\n }\n\n /**\n * Text still held back when the content block ends. A partial tag prefix\n * (`<`, `<thin`) is real output and comes back; an unterminated thinking\n * block stays hidden.\n */\n flush(): string {\n if (this.resolved) {\n return \"\";\n }\n const pending =\n this.buffer.length < THINKING_TAG.length &&\n THINKING_TAG.startsWith(this.buffer)\n ? this.buffer\n : \"\";\n this.buffer = \"\";\n this.resolved = true;\n return pending;\n }\n\n reset() {\n this.buffer = \"\";\n this.resolved = false;\n }\n\n private release(): string {\n const pending = this.buffer;\n this.buffer = \"\";\n this.resolved = true;\n return pending;\n }\n}\n"],"mappings":";;;;;;;;;AA4EA,MAAM,gBAAgB;AAuCtB,IAAa,mBAAb,MAA+D;CAO7D,IAAW,YAA2B;AACpC,SAAO,KAAK;;CAEd,IAAW,OAAO;AAChB,SAAO;;CAGT,YAAY,QAAiC;eAbtB;kBACL;AAahB,MAAI,QAAQ,UACV,MAAK,aAAa,OAAO;AAG3B,MAAI,QAAQ,MACV,MAAK,QAAQ,OAAO;AAEtB,OAAK,gBAAgB,QAAQ,iBAAiB,EAAE,SAAS,OAAO;AAChE,OAAK,iBAAiB,QAAQ;;CAGhC,mBAAkC;EAChC,MAAM,YAAY,KAAK,iBAA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allbackResponse;
58
58
  process(request: CopilotRuntimeChatCompletionRequest): Promise<CopilotRuntimeChatCompletionResponse>;
59
59
  }
60
+ /**
61
+ * Strips a `<thinking>…</thinking>` block that opens a streamed text content
62
+ * block, passing everything else through unchanged.
63
+ *
64
+ * Only the START of a content block can be a thinking tag. Text is held back
65
+ * while the block's opening characters could still turn into `<thinking>`;
66
+ * the moment they diverge, everything held back is emitted in one piece and
67
+ * the detector disarms for the rest of the block, so a literal `<thinking>`
68
+ * mentioned later in the answer is never treated as a tag. `flush()` releases
69
+ * whatever is still held back when the content block ends (a block that is
70
+ * just `<`, for example).
71
+ */
72
+ declare class FilterThinkingTextBuffer {
73
+ private buffer;
74
+ /**
75
+ * True once the block's start is known to be, or not to be, a thinking tag;
76
+ * from then on chunks pass straight through.
77
+ */
78
+ private resolved;
79
+ onTextChunk(text: string): string;
80
+ /**
81
+ * Text still held back when the content block ends. A partial tag prefix
82
+ * (`<`, `<thin`) is real output and comes back; an unterminated thinking
83
+ * block stays hidden.
84
+ */
85
+ flush(): string;
86
+ reset(): void;
87
+ private release;
88
+ }
60
89
  //#endregion
61
- export { AnthropicAdapter, AnthropicAdapterParams, AnthropicPromptCachingConfig };
90
+ export { AnthropicAdapter, AnthropicAdapterParams, AnthropicPromptCachingConfig, FilterThinkingTextBuffer };
62
91
  //# sourceMappingURL=anthropic-adapter.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"anthropic-adapter.d.cts","names":[],"sources":["../../../../src/v1-deprecated/service-adapters/anthropic/anthropic-adapter.ts"],"mappings":";;;;;;UA8EiB,4BAAA;EAwMJ;;;EApMX,OAAA;EAiC4D;;;EA5B5D,KAAA;AAAA;AAAA,UAGe,sBAAA;EA6BP;;;;EAxBR,SAAA,GAAY,aAAA;;;;EAKZ,KAAA;EAyCoB;;;;EAnCpB,aAAA,GAAgB,4BAAA;EA2KV;;;;EArKN,cAAA;AAAA;AAAA,cAGW,gBAAA,YAA4B,qBAAA;EAChC,KAAA;EACA,QAAA;EAAA,QACC,aAAA;EAAA,QACA,cAAA;EAAA,QAEA,UAAA;EAAA,IACG,SAAA,CAAA,GAAa,aAAA;EAAA,IAGb,IAAA,CAAA;cAIC,MAAA,GAAS,sBAAA;EAYrB,gBAAA,CAAA,GAAoB,aAAA;EAAA,QAYZ,eAAA;;;;UAYA,sBAAA;;;;UAkCA,4BAAA;EAAA,QAwCA,8BAAA;EAsCF,OAAA,CACJ,OAAA,EAAS,mCAAA,GACR,OAAA,CAAQ,oCAAA;AAAA"}
1
+ {"version":3,"file":"anthropic-adapter.d.cts","names":[],"sources":["../../../../src/v1-deprecated/service-adapters/anthropic/anthropic-adapter.ts"],"mappings":";;;;;;UA8EiB,4BAAA;EAwMJ;;;EApMX,OAAA;EAiC4D;;;EA5B5D,KAAA;AAAA;AAAA,UAGe,sBAAA;EA6BP;;;;EAxBR,SAAA,GAAY,aAAA;;;;EAKZ,KAAA;EAyCoB;;;;EAnCpB,aAAA,GAAgB,4BAAA;EA2KV;;;;EArKN,cAAA;AAAA;AAAA,cAGW,gBAAA,YAA4B,qBAAA;EAChC,KAAA;EACA,QAAA;EAAA,QACC,aAAA;EAAA,QACA,cAAA;EAAA,QAEA,UAAA;EAAA,IACG,SAAA,CAAA,GAAa,aAAA;EAAA,IAGb,IAAA,CAAA;cAIC,MAAA,GAAS,sBAAA;EAYrB,gBAAA,CAAA,GAAoB,aAAA;EAAA,QAYZ,eAAA;EAwdA;;;EAAA,QA5cA,sBAAA;;;;UAkCA,4BAAA;EAAA,QAwCA,8BAAA;EAsCF,OAAA,CACJ,OAAA,EAAS,mCAAA,GACR,OAAA,CAAQ,oCAAA;AAAA;;;;;;;;;;;;;cA2RA,wBAAA;EAAA,QACH,MAAA;;;;;UAKA,QAAA;EAER,WAAA,CAAY,IAAA;;;;;;EAoCZ,KAAA,CAAA;EAcA,KAAA,CAAA;EAAA,QAKQ,OAAA;AAAA"}
@@ -58,6 +58,35 @@ declare class AnthropicAdapter implements CopilotServiceAdapter {
58
58
  private shouldGenerateFallbackResponse;
59
59
  process(request: CopilotRuntimeChatCompletionRequest): Promise<CopilotRuntimeChatCompletionResponse>;
60
60
  }
61
+ /**
62
+ * Strips a `<thinking>…</thinking>` block that opens a streamed text content
63
+ * block, passing everything else through unchanged.
64
+ *
65
+ * Only the START of a content block can be a thinking tag. Text is held back
66
+ * while the block's opening characters could still turn into `<thinking>`;
67
+ * the moment they diverge, everything held back is emitted in one piece and
68
+ * the detector disarms for the rest of the block, so a literal `<thinking>`
69
+ * mentioned later in the answer is never treated as a tag. `flush()` releases
70
+ * whatever is still held back when the content block ends (a block that is
71
+ * just `<`, for example).
72
+ */
73
+ declare class FilterThinkingTextBuffer {
74
+ private buffer;
75
+ /**
76
+ * True once the block's start is known to be, or not to be, a thinking tag;
77
+ * from then on chunks pass straight through.
78
+ */
79
+ private resolved;
80
+ onTextChunk(text: string): string;
81
+ /**
82
+ * Text still held back when the content block ends. A partial tag prefix
83
+ * (`<`, `<thin`) is real output and comes back; an unterminated thinking
84
+ * block stays hidden.
85
+ */
86
+ flush(): string;
87
+ reset(): void;
88
+ private release;
89
+ }
61
90
  //#endregion
62
- export { AnthropicAdapter, AnthropicAdapterParams, AnthropicPromptCachingConfig };
91
+ export { AnthropicAdapter, AnthropicAdapterParams, AnthropicPromptCachingConfig, FilterThinkingTextBuffer };
63
92
  //# sourceMappingURL=anthropic-adapter.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"anthropic-adapter.d.mts","names":[],"sources":["../../../../src/v1-deprecated/service-adapters/anthropic/anthropic-adapter.ts"],"mappings":";;;;;;;UA8EiB,4BAAA;EA+DK;;;EA3DpB,OAAA;EAiCuC;;;EA5BvC,KAAA;AAAA;AAAA,UAGe,sBAAA;EA4BP;;;;EAvBR,SAAA,GAAY,aAAA;EA8BD;;;EAzBX,KAAA;EAyCA;;;;EAnCA,aAAA,GAAgB,4BAAA;EAqIR;;;;EA/HR,cAAA;AAAA;AAAA,cAGW,gBAAA,YAA4B,qBAAA;EAChC,KAAA;EACA,QAAA;EAAA,QACC,aAAA;EAAA,QACA,cAAA;EAAA,QAEA,UAAA;EAAA,IACG,SAAA,CAAA,GAAa,aAAA;EAAA,IAGb,IAAA,CAAA;cAIC,MAAA,GAAS,sBAAA;EAYrB,gBAAA,CAAA,GAAoB,aAAA;EAAA,QAYZ,eAAA;;;;UAYA,sBAAA;;;;UAkCA,4BAAA;EAAA,QAwCA,8BAAA;EAsCF,OAAA,CACJ,OAAA,EAAS,mCAAA,GACR,OAAA,CAAQ,oCAAA;AAAA"}
1
+ {"version":3,"file":"anthropic-adapter.d.mts","names":[],"sources":["../../../../src/v1-deprecated/service-adapters/anthropic/anthropic-adapter.ts"],"mappings":";;;;;;;UA8EiB,4BAAA;EA+DK;;;EA3DpB,OAAA;EAiCuC;;;EA5BvC,KAAA;AAAA;AAAA,UAGe,sBAAA;EA4BP;;;;EAvBR,SAAA,GAAY,aAAA;EA8BD;;;EAzBX,KAAA;EAyCA;;;;EAnCA,aAAA,GAAgB,4BAAA;EAqIR;;;;EA/HR,cAAA;AAAA;AAAA,cAGW,gBAAA,YAA4B,qBAAA;EAChC,KAAA;EACA,QAAA;EAAA,QACC,aAAA;EAAA,QACA,cAAA;EAAA,QAEA,UAAA;EAAA,IACG,SAAA,CAAA,GAAa,aAAA;EAAA,IAGb,IAAA,CAAA;cAIC,MAAA,GAAS,sBAAA;EAYrB,gBAAA,CAAA,GAAoB,aAAA;EAAA,QAYZ,eAAA;EAmdR;;;EAAA,QAvcQ,sBAAA;;;;UAkCA,4BAAA;EAAA,QAwCA,8BAAA;EAsCF,OAAA,CACJ,OAAA,EAAS,mCAAA,GACR,OAAA,CAAQ,oCAAA;AAAA;;;;;;;;;;;;;cA2RA,wBAAA;EAAA,QACH,MAAA;;;;;UAKA,QAAA;EAER,WAAA,CAAY,IAAA;;;;;;EAoCZ,KAAA,CAAA;EAcA,KAAA,CAAA;EAAA,QAKQ,OAAA;AAAA"}
@@ -139,6 +139,17 @@ var AnthropicAdapter = class {
139
139
  let currentToolCallId = randomId();
140
140
  let filterThinkingTextBuffer = new FilterThinkingTextBuffer();
141
141
  let hasReceivedContent = false;
142
+ const emitText = (text) => {
143
+ if (text.length === 0) return;
144
+ if (!didOutputText) {
145
+ eventStream$.sendTextMessageStart({ messageId: currentMessageId });
146
+ didOutputText = true;
147
+ }
148
+ eventStream$.sendTextMessageContent({
149
+ messageId: currentMessageId,
150
+ content: text
151
+ });
152
+ };
142
153
  try {
143
154
  for await (const chunk of stream) if (chunk.type === "message_start") currentMessageId = chunk.message.id;
144
155
  else if (chunk.type === "content_block_start") {
@@ -163,24 +174,14 @@ var AnthropicAdapter = class {
163
174
  }
164
175
  } else if (chunk.type === "content_block_delta") {
165
176
  if (mode === null) continue;
166
- if (chunk.delta.type === "text_delta") {
167
- const text = filterThinkingTextBuffer.onTextChunk(chunk.delta.text);
168
- if (text.length > 0) {
169
- if (!didOutputText) {
170
- eventStream$.sendTextMessageStart({ messageId: currentMessageId });
171
- didOutputText = true;
172
- }
173
- eventStream$.sendTextMessageContent({
174
- messageId: currentMessageId,
175
- content: text
176
- });
177
- }
178
- } else if (chunk.delta.type === "input_json_delta") eventStream$.sendActionExecutionArgs({
177
+ if (chunk.delta.type === "text_delta") emitText(filterThinkingTextBuffer.onTextChunk(chunk.delta.text));
178
+ else if (chunk.delta.type === "input_json_delta") eventStream$.sendActionExecutionArgs({
179
179
  actionExecutionId: currentToolCallId,
180
180
  args: chunk.delta.partial_json
181
181
  });
182
182
  } else if (chunk.type === "content_block_stop") {
183
183
  if (mode === "message") {
184
+ emitText(filterThinkingTextBuffer.flush());
184
185
  if (didOutputText) eventStream$.sendTextMessageEnd({ messageId: currentMessageId });
185
186
  } else if (mode === "function") eventStream$.sendActionExecutionEnd({ actionExecutionId: currentToolCallId });
186
187
  }
@@ -212,30 +213,62 @@ var AnthropicAdapter = class {
212
213
  };
213
214
  const THINKING_TAG = "<thinking>";
214
215
  const THINKING_TAG_END = "</thinking>";
216
+ /**
217
+ * Strips a `<thinking>…</thinking>` block that opens a streamed text content
218
+ * block, passing everything else through unchanged.
219
+ *
220
+ * Only the START of a content block can be a thinking tag. Text is held back
221
+ * while the block's opening characters could still turn into `<thinking>`;
222
+ * the moment they diverge, everything held back is emitted in one piece and
223
+ * the detector disarms for the rest of the block, so a literal `<thinking>`
224
+ * mentioned later in the answer is never treated as a tag. `flush()` releases
225
+ * whatever is still held back when the content block ends (a block that is
226
+ * just `<`, for example).
227
+ */
215
228
  var FilterThinkingTextBuffer = class {
216
229
  constructor() {
217
- this.didFilterThinkingTag = false;
218
230
  this.buffer = "";
231
+ this.resolved = false;
219
232
  }
220
233
  onTextChunk(text) {
234
+ if (this.resolved) return text;
221
235
  this.buffer += text;
222
- if (this.didFilterThinkingTag) return text;
223
- const potentialTag = this.buffer.slice(0, 10);
224
- if (THINKING_TAG.startsWith(potentialTag)) if (this.buffer.includes(THINKING_TAG_END)) {
225
- const end = this.buffer.indexOf(THINKING_TAG_END);
226
- const filteredText = this.buffer.slice(end + 11);
227
- this.buffer = filteredText;
228
- this.didFilterThinkingTag = true;
229
- return filteredText;
230
- } else return "";
231
- return text;
236
+ if (this.buffer.length < 10) {
237
+ if (THINKING_TAG.startsWith(this.buffer)) return "";
238
+ return this.release();
239
+ }
240
+ if (!this.buffer.startsWith(THINKING_TAG)) return this.release();
241
+ const end = this.buffer.indexOf(THINKING_TAG_END);
242
+ if (end === -1) return "";
243
+ const rest = this.buffer.slice(end + 11);
244
+ this.buffer = "";
245
+ this.resolved = true;
246
+ return rest;
247
+ }
248
+ /**
249
+ * Text still held back when the content block ends. A partial tag prefix
250
+ * (`<`, `<thin`) is real output and comes back; an unterminated thinking
251
+ * block stays hidden.
252
+ */
253
+ flush() {
254
+ if (this.resolved) return "";
255
+ const pending = this.buffer.length < 10 && THINKING_TAG.startsWith(this.buffer) ? this.buffer : "";
256
+ this.buffer = "";
257
+ this.resolved = true;
258
+ return pending;
232
259
  }
233
260
  reset() {
234
261
  this.buffer = "";
235
- this.didFilterThinkingTag = false;
262
+ this.resolved = false;
263
+ }
264
+ release() {
265
+ const pending = this.buffer;
266
+ this.buffer = "";
267
+ this.resolved = true;
268
+ return pending;
236
269
  }
237
270
  };
238
271
 
239
272
  //#endregion
240
- export { AnthropicAdapter };
273
+ export { AnthropicAdapter, FilterThinkingTextBuffer };
241
274
  //# sourceMappingURL=anthropic-adapter.mjs.map