@copilotkit/runtime-client-gql 1.51.4 → 1.51.5-next.1

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 (176) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/_virtual/_rolldown/runtime.cjs +29 -0
  3. package/dist/client/CopilotRuntimeClient.cjs +119 -0
  4. package/dist/client/CopilotRuntimeClient.cjs.map +1 -0
  5. package/dist/client/CopilotRuntimeClient.d.cts +44 -0
  6. package/dist/client/CopilotRuntimeClient.d.cts.map +1 -0
  7. package/dist/client/CopilotRuntimeClient.d.mts +44 -0
  8. package/dist/client/CopilotRuntimeClient.d.mts.map +1 -0
  9. package/dist/client/CopilotRuntimeClient.mjs +116 -10
  10. package/dist/client/CopilotRuntimeClient.mjs.map +1 -1
  11. package/dist/client/conversion.cjs +194 -0
  12. package/dist/client/conversion.cjs.map +1 -0
  13. package/dist/client/{conversion.d.ts → conversion.d.cts} +5 -4
  14. package/dist/client/conversion.d.cts.map +1 -0
  15. package/dist/client/conversion.d.mts +12 -0
  16. package/dist/client/conversion.d.mts.map +1 -0
  17. package/dist/client/conversion.mjs +187 -17
  18. package/dist/client/conversion.mjs.map +1 -1
  19. package/dist/client/index.d.cts +5 -0
  20. package/dist/client/index.d.mts +5 -0
  21. package/dist/client/types.cjs +86 -0
  22. package/dist/client/types.cjs.map +1 -0
  23. package/dist/client/types.d.cts +76 -0
  24. package/dist/client/types.d.cts.map +1 -0
  25. package/dist/client/types.d.mts +76 -0
  26. package/dist/client/types.d.mts.map +1 -0
  27. package/dist/client/types.mjs +76 -21
  28. package/dist/client/types.mjs.map +1 -1
  29. package/dist/graphql/@generated/gql.cjs +15 -0
  30. package/dist/graphql/@generated/gql.cjs.map +1 -0
  31. package/dist/graphql/@generated/gql.mjs +13 -7
  32. package/dist/graphql/@generated/gql.mjs.map +1 -1
  33. package/dist/graphql/@generated/graphql.cjs +1154 -0
  34. package/dist/graphql/@generated/graphql.cjs.map +1 -0
  35. package/dist/graphql/@generated/graphql.d.cts +642 -0
  36. package/dist/graphql/@generated/graphql.d.cts.map +1 -0
  37. package/dist/graphql/@generated/graphql.d.mts +642 -0
  38. package/dist/graphql/@generated/graphql.d.mts.map +1 -0
  39. package/dist/graphql/@generated/graphql.mjs +1142 -24
  40. package/dist/graphql/@generated/graphql.mjs.map +1 -1
  41. package/dist/{chunk-E5FOLXLN.mjs → graphql/definitions/mutations.cjs} +8 -11
  42. package/dist/graphql/definitions/mutations.cjs.map +1 -0
  43. package/dist/graphql/definitions/mutations.mjs +140 -9
  44. package/dist/graphql/definitions/mutations.mjs.map +1 -1
  45. package/dist/graphql/definitions/queries.cjs +35 -0
  46. package/dist/graphql/definitions/queries.cjs.map +1 -0
  47. package/dist/graphql/definitions/queries.mjs +33 -11
  48. package/dist/graphql/definitions/queries.mjs.map +1 -1
  49. package/dist/index.cjs +43 -0
  50. package/dist/index.d.cts +9 -0
  51. package/dist/index.d.mts +9 -0
  52. package/dist/index.mjs +8 -91
  53. package/dist/index.umd.js +2080 -2
  54. package/dist/index.umd.js.map +1 -1
  55. package/dist/message-conversion/agui-to-gql.cjs +162 -0
  56. package/dist/message-conversion/agui-to-gql.cjs.map +1 -0
  57. package/dist/message-conversion/{agui-to-gql.d.ts → agui-to-gql.d.cts} +6 -5
  58. package/dist/message-conversion/agui-to-gql.d.cts.map +1 -0
  59. package/dist/message-conversion/agui-to-gql.d.mts +14 -0
  60. package/dist/message-conversion/agui-to-gql.d.mts.map +1 -0
  61. package/dist/message-conversion/agui-to-gql.mjs +156 -25
  62. package/dist/message-conversion/agui-to-gql.mjs.map +1 -1
  63. package/dist/message-conversion/gql-to-agui.cjs +169 -0
  64. package/dist/message-conversion/gql-to-agui.cjs.map +1 -0
  65. package/dist/message-conversion/{gql-to-agui.d.ts → gql-to-agui.d.cts} +6 -5
  66. package/dist/message-conversion/gql-to-agui.d.cts.map +1 -0
  67. package/dist/message-conversion/gql-to-agui.d.mts +13 -0
  68. package/dist/message-conversion/gql-to-agui.d.mts.map +1 -0
  69. package/dist/message-conversion/gql-to-agui.mjs +164 -23
  70. package/dist/message-conversion/gql-to-agui.mjs.map +1 -1
  71. package/dist/message-conversion/index.d.cts +2 -0
  72. package/dist/message-conversion/index.d.mts +2 -0
  73. package/dist/package.cjs +12 -0
  74. package/dist/package.cjs.map +1 -0
  75. package/dist/package.mjs +6 -0
  76. package/dist/package.mjs.map +1 -0
  77. package/package.json +15 -15
  78. package/src/message-conversion/agui-to-gql.test.ts +70 -0
  79. package/src/message-conversion/agui-to-gql.ts +4 -0
  80. package/tsdown.config.ts +49 -0
  81. package/vitest.config.mjs +11 -0
  82. package/dist/chunk-2XWNDVTL.mjs +0 -176
  83. package/dist/chunk-2XWNDVTL.mjs.map +0 -1
  84. package/dist/chunk-7ECCT6PK.mjs +0 -1
  85. package/dist/chunk-7ECCT6PK.mjs.map +0 -1
  86. package/dist/chunk-A4INSSNE.mjs +0 -17722
  87. package/dist/chunk-A4INSSNE.mjs.map +0 -1
  88. package/dist/chunk-CA4VMP2C.mjs +0 -1
  89. package/dist/chunk-CA4VMP2C.mjs.map +0 -1
  90. package/dist/chunk-DELDZXUX.mjs +0 -31
  91. package/dist/chunk-DELDZXUX.mjs.map +0 -1
  92. package/dist/chunk-E5FOLXLN.mjs.map +0 -1
  93. package/dist/chunk-LTVE64IE.mjs +0 -241
  94. package/dist/chunk-LTVE64IE.mjs.map +0 -1
  95. package/dist/chunk-M4CK5HDR.mjs +0 -237
  96. package/dist/chunk-M4CK5HDR.mjs.map +0 -1
  97. package/dist/chunk-OBDQ5BWD.mjs +0 -63
  98. package/dist/chunk-OBDQ5BWD.mjs.map +0 -1
  99. package/dist/chunk-QUH7QWUA.mjs +0 -38
  100. package/dist/chunk-QUH7QWUA.mjs.map +0 -1
  101. package/dist/chunk-SDQPJXLL.mjs +0 -20
  102. package/dist/chunk-SDQPJXLL.mjs.map +0 -1
  103. package/dist/chunk-UOAPP5IN.mjs +0 -93
  104. package/dist/chunk-UOAPP5IN.mjs.map +0 -1
  105. package/dist/chunk-VIHKTLUA.mjs +0 -221
  106. package/dist/chunk-VIHKTLUA.mjs.map +0 -1
  107. package/dist/chunk-XZPL6QVK.mjs +0 -24
  108. package/dist/chunk-XZPL6QVK.mjs.map +0 -1
  109. package/dist/client/CopilotRuntimeClient.d.ts +0 -39
  110. package/dist/client/CopilotRuntimeClient.js +0 -366
  111. package/dist/client/CopilotRuntimeClient.js.map +0 -1
  112. package/dist/client/conversion.js +0 -339
  113. package/dist/client/conversion.js.map +0 -1
  114. package/dist/client/index.d.ts +0 -8
  115. package/dist/client/index.js +0 -711
  116. package/dist/client/index.js.map +0 -1
  117. package/dist/client/index.mjs +0 -43
  118. package/dist/client/index.mjs.map +0 -1
  119. package/dist/client/types.d.ts +0 -75
  120. package/dist/client/types.js +0 -132
  121. package/dist/client/types.js.map +0 -1
  122. package/dist/graphql/@generated/fragment-masking.d.ts +0 -22
  123. package/dist/graphql/@generated/fragment-masking.js +0 -50
  124. package/dist/graphql/@generated/fragment-masking.js.map +0 -1
  125. package/dist/graphql/@generated/fragment-masking.mjs +0 -12
  126. package/dist/graphql/@generated/fragment-masking.mjs.map +0 -1
  127. package/dist/graphql/@generated/gql.d.ts +0 -48
  128. package/dist/graphql/@generated/gql.js +0 -45
  129. package/dist/graphql/@generated/gql.js.map +0 -1
  130. package/dist/graphql/@generated/graphql.d.ts +0 -655
  131. package/dist/graphql/@generated/graphql.js +0 -96
  132. package/dist/graphql/@generated/graphql.js.map +0 -1
  133. package/dist/graphql/@generated/index.d.ts +0 -4
  134. package/dist/graphql/@generated/index.js +0 -69
  135. package/dist/graphql/@generated/index.js.map +0 -1
  136. package/dist/graphql/@generated/index.mjs +0 -17
  137. package/dist/graphql/@generated/index.mjs.map +0 -1
  138. package/dist/graphql/definitions/mutations.d.ts +0 -9
  139. package/dist/graphql/definitions/mutations.js +0 -181
  140. package/dist/graphql/definitions/mutations.js.map +0 -1
  141. package/dist/graphql/definitions/queries.d.ts +0 -11
  142. package/dist/graphql/definitions/queries.js +0 -76
  143. package/dist/graphql/definitions/queries.js.map +0 -1
  144. package/dist/index.d.ts +0 -11
  145. package/dist/index.js +0 -1222
  146. package/dist/index.js.map +0 -1
  147. package/dist/index.mjs.map +0 -1
  148. package/dist/magic-string.es-XBKSJO3K.mjs +0 -1389
  149. package/dist/magic-string.es-XBKSJO3K.mjs.map +0 -1
  150. package/dist/message-conversion/agui-to-gql.js +0 -344
  151. package/dist/message-conversion/agui-to-gql.js.map +0 -1
  152. package/dist/message-conversion/agui-to-gql.test.d.ts +0 -2
  153. package/dist/message-conversion/agui-to-gql.test.js +0 -20553
  154. package/dist/message-conversion/agui-to-gql.test.js.map +0 -1
  155. package/dist/message-conversion/agui-to-gql.test.mjs +0 -1137
  156. package/dist/message-conversion/agui-to-gql.test.mjs.map +0 -1
  157. package/dist/message-conversion/gql-to-agui.js +0 -262
  158. package/dist/message-conversion/gql-to-agui.js.map +0 -1
  159. package/dist/message-conversion/gql-to-agui.test.d.ts +0 -2
  160. package/dist/message-conversion/gql-to-agui.test.js +0 -20850
  161. package/dist/message-conversion/gql-to-agui.test.js.map +0 -1
  162. package/dist/message-conversion/gql-to-agui.test.mjs +0 -1447
  163. package/dist/message-conversion/gql-to-agui.test.mjs.map +0 -1
  164. package/dist/message-conversion/index.d.ts +0 -6
  165. package/dist/message-conversion/index.js +0 -563
  166. package/dist/message-conversion/index.js.map +0 -1
  167. package/dist/message-conversion/index.mjs +0 -39
  168. package/dist/message-conversion/index.mjs.map +0 -1
  169. package/dist/message-conversion/roundtrip-conversion.test.d.ts +0 -2
  170. package/dist/message-conversion/roundtrip-conversion.test.js +0 -20095
  171. package/dist/message-conversion/roundtrip-conversion.test.js.map +0 -1
  172. package/dist/message-conversion/roundtrip-conversion.test.mjs +0 -489
  173. package/dist/message-conversion/roundtrip-conversion.test.mjs.map +0 -1
  174. package/jest.config.js +0 -5
  175. package/rollup.config.mjs +0 -62
  176. package/tsup.config.ts +0 -12
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @copilotkit/runtime-client-gql
2
2
 
3
+ ## 1.51.5-next.1
4
+
5
+ ### Patch Changes
6
+
7
+ - @copilotkit/runtime@1.51.5-next.1
8
+ - @copilotkit/shared@1.51.5-next.1
9
+
10
+ ## 1.51.5-next.0
11
+
12
+ ### Patch Changes
13
+
14
+ - Updated dependencies [ef0f539]
15
+ - @copilotkit/runtime@1.51.5-next.0
16
+ - @copilotkit/shared@1.51.5-next.0
17
+
3
18
  ## 1.51.4
4
19
 
5
20
  ### Patch Changes
@@ -0,0 +1,29 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) {
13
+ __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ }
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
+ value: mod,
24
+ enumerable: true
25
+ }) : target, mod));
26
+
27
+ //#endregion
28
+
29
+ exports.__toESM = __toESM;
@@ -0,0 +1,119 @@
1
+ const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
2
+ const require_package = require('../package.cjs');
3
+ const require_mutations = require('../graphql/definitions/mutations.cjs');
4
+ const require_queries = require('../graphql/definitions/queries.cjs');
5
+ let _urql_core = require("@urql/core");
6
+ let _copilotkit_shared = require("@copilotkit/shared");
7
+
8
+ //#region src/client/CopilotRuntimeClient.ts
9
+ const createFetchFn = (signal, handleGQLWarning) => async (...args) => {
10
+ const publicApiKey = args[1]?.headers?.["x-copilotcloud-public-api-key"];
11
+ try {
12
+ const result = await fetch(args[0], {
13
+ ...args[1] ?? {},
14
+ signal
15
+ });
16
+ const mismatch = publicApiKey ? null : await (0, _copilotkit_shared.getPossibleVersionMismatch)({
17
+ runtimeVersion: result.headers.get("X-CopilotKit-Runtime-Version"),
18
+ runtimeClientGqlVersion: require_package.version
19
+ });
20
+ if (result.status !== 200) {
21
+ if (result.status >= 400 && result.status <= 500) {
22
+ if (mismatch) throw new _copilotkit_shared.CopilotKitVersionMismatchError(mismatch);
23
+ throw new _copilotkit_shared.ResolvedCopilotKitError({ status: result.status });
24
+ }
25
+ }
26
+ if (mismatch && handleGQLWarning) handleGQLWarning(mismatch.message);
27
+ return result;
28
+ } catch (error) {
29
+ if (error.message.includes("BodyStreamBuffer was aborted") || error.message.includes("signal is aborted without reason")) throw error;
30
+ if (error instanceof _copilotkit_shared.CopilotKitError) throw error;
31
+ throw new _copilotkit_shared.CopilotKitLowLevelError({
32
+ error,
33
+ url: args[0]
34
+ });
35
+ }
36
+ };
37
+ var CopilotRuntimeClient = class CopilotRuntimeClient {
38
+ constructor(options) {
39
+ const headers = {};
40
+ this.handleGQLErrors = options.handleGQLErrors;
41
+ this.handleGQLWarning = options.handleGQLWarning;
42
+ if (options.headers) Object.assign(headers, options.headers);
43
+ if (options.publicApiKey) headers["x-copilotcloud-public-api-key"] = options.publicApiKey;
44
+ this.client = new _urql_core.Client({
45
+ url: options.url,
46
+ exchanges: [_urql_core.cacheExchange, _urql_core.fetchExchange],
47
+ fetchOptions: {
48
+ headers: {
49
+ ...headers,
50
+ "X-CopilotKit-Runtime-Client-GQL-Version": require_package.version
51
+ },
52
+ ...options.credentials ? { credentials: options.credentials } : {}
53
+ }
54
+ });
55
+ }
56
+ generateCopilotResponse({ data, properties, signal }) {
57
+ const fetchFn = createFetchFn(signal, this.handleGQLWarning);
58
+ return this.client.mutation(require_mutations.generateCopilotResponseMutation, {
59
+ data,
60
+ properties
61
+ }, { fetch: fetchFn });
62
+ }
63
+ asStream(source) {
64
+ const handleGQLErrors = this.handleGQLErrors;
65
+ return new ReadableStream({ start(controller) {
66
+ source.subscribe(({ data, hasNext, error }) => {
67
+ if (error) {
68
+ if (error.message.includes("BodyStreamBuffer was aborted") || error.message.includes("signal is aborted without reason")) {
69
+ if (!hasNext) controller.close();
70
+ console.warn("Abort error suppressed");
71
+ return;
72
+ }
73
+ if (error.extensions?.visibility) {
74
+ const syntheticError = {
75
+ ...error,
76
+ graphQLErrors: [{
77
+ message: error.message,
78
+ extensions: error.extensions
79
+ }]
80
+ };
81
+ if (handleGQLErrors) handleGQLErrors(syntheticError);
82
+ return;
83
+ }
84
+ controller.error(error);
85
+ if (handleGQLErrors) handleGQLErrors(error);
86
+ } else {
87
+ controller.enqueue(data);
88
+ if (!hasNext) controller.close();
89
+ }
90
+ });
91
+ } });
92
+ }
93
+ availableAgents() {
94
+ const fetchFn = createFetchFn();
95
+ return this.client.query(require_queries.getAvailableAgentsQuery, {}, { fetch: fetchFn });
96
+ }
97
+ loadAgentState(data) {
98
+ const fetchFn = createFetchFn();
99
+ const result = this.client.query(require_queries.loadAgentStateQuery, { data }, { fetch: fetchFn });
100
+ result.toPromise().then(({ error }) => {
101
+ if (error && this.handleGQLErrors) this.handleGQLErrors(error);
102
+ }).catch(() => {});
103
+ return result;
104
+ }
105
+ static removeGraphQLTypename(data) {
106
+ if (Array.isArray(data)) data.forEach((item) => CopilotRuntimeClient.removeGraphQLTypename(item));
107
+ else if (typeof data === "object" && data !== null) {
108
+ delete data.__typename;
109
+ Object.keys(data).forEach((key) => {
110
+ if (typeof data[key] === "object" && data[key] !== null) CopilotRuntimeClient.removeGraphQLTypename(data[key]);
111
+ });
112
+ }
113
+ return data;
114
+ }
115
+ };
116
+
117
+ //#endregion
118
+ exports.CopilotRuntimeClient = CopilotRuntimeClient;
119
+ //# sourceMappingURL=CopilotRuntimeClient.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CopilotRuntimeClient.cjs","names":["CopilotKitVersionMismatchError","ResolvedCopilotKitError","CopilotKitError","CopilotKitLowLevelError","Client","cacheExchange","fetchExchange","generateCopilotResponseMutation","getAvailableAgentsQuery","loadAgentStateQuery"],"sources":["../../src/client/CopilotRuntimeClient.ts"],"sourcesContent":["import { Client, cacheExchange, fetchExchange } from \"@urql/core\";\nimport * as packageJson from \"../../package.json\";\nimport {\n AvailableAgentsQuery,\n GenerateCopilotResponseMutation,\n GenerateCopilotResponseMutationVariables,\n LoadAgentStateQuery,\n} from \"../graphql/@generated/graphql\";\nimport { generateCopilotResponseMutation } from \"../graphql/definitions/mutations\";\nimport {\n getAvailableAgentsQuery,\n loadAgentStateQuery,\n} from \"../graphql/definitions/queries\";\nimport { OperationResultSource, OperationResult } from \"urql\";\nimport {\n ResolvedCopilotKitError,\n CopilotKitLowLevelError,\n CopilotKitError,\n CopilotKitVersionMismatchError,\n getPossibleVersionMismatch,\n} from \"@copilotkit/shared\";\n\nconst createFetchFn =\n (signal?: AbortSignal, handleGQLWarning?: (warning: string) => void) =>\n async (...args: Parameters<typeof fetch>) => {\n // @ts-expect-error -- since this is our own header, TS will not recognize\n const publicApiKey = args[1]?.headers?.[\"x-copilotcloud-public-api-key\"];\n try {\n const result = await fetch(args[0], { ...(args[1] ?? {}), signal });\n\n // No mismatch checking if cloud is being used\n const mismatch = publicApiKey\n ? null\n : await getPossibleVersionMismatch({\n runtimeVersion: result.headers.get(\"X-CopilotKit-Runtime-Version\")!,\n runtimeClientGqlVersion: packageJson.version,\n });\n if (result.status !== 200) {\n if (result.status >= 400 && result.status <= 500) {\n if (mismatch) {\n throw new CopilotKitVersionMismatchError(mismatch);\n }\n\n throw new ResolvedCopilotKitError({ status: result.status });\n }\n }\n\n if (mismatch && handleGQLWarning) {\n handleGQLWarning(mismatch.message);\n }\n\n return result;\n } catch (error) {\n // Let abort error pass through. It will be suppressed later\n if (\n (error as Error).message.includes(\"BodyStreamBuffer was aborted\") ||\n (error as Error).message.includes(\"signal is aborted without reason\")\n ) {\n throw error;\n }\n if (error instanceof CopilotKitError) {\n throw error;\n }\n throw new CopilotKitLowLevelError({\n error: error as Error,\n url: args[0] as string,\n });\n }\n };\n\nexport interface CopilotRuntimeClientOptions {\n url: string;\n publicApiKey?: string;\n headers?: Record<string, string>;\n credentials?: RequestCredentials;\n handleGQLErrors?: (error: Error) => void;\n handleGQLWarning?: (warning: string) => void;\n}\n\nexport class CopilotRuntimeClient {\n client: Client;\n public handleGQLErrors?: (error: Error) => void;\n public handleGQLWarning?: (warning: string) => void;\n\n constructor(options: CopilotRuntimeClientOptions) {\n const headers: Record<string, string> = {};\n\n this.handleGQLErrors = options.handleGQLErrors;\n this.handleGQLWarning = options.handleGQLWarning;\n\n if (options.headers) {\n Object.assign(headers, options.headers);\n }\n\n if (options.publicApiKey) {\n headers[\"x-copilotcloud-public-api-key\"] = options.publicApiKey;\n }\n\n this.client = new Client({\n url: options.url,\n exchanges: [cacheExchange, fetchExchange],\n fetchOptions: {\n headers: {\n ...headers,\n \"X-CopilotKit-Runtime-Client-GQL-Version\": packageJson.version,\n },\n ...(options.credentials ? { credentials: options.credentials } : {}),\n },\n });\n }\n\n generateCopilotResponse({\n data,\n properties,\n signal,\n }: {\n data: GenerateCopilotResponseMutationVariables[\"data\"];\n properties?: GenerateCopilotResponseMutationVariables[\"properties\"];\n signal?: AbortSignal;\n }) {\n const fetchFn = createFetchFn(signal, this.handleGQLWarning);\n const result = this.client.mutation<\n GenerateCopilotResponseMutation,\n GenerateCopilotResponseMutationVariables\n >(\n generateCopilotResponseMutation,\n { data, properties },\n { fetch: fetchFn },\n );\n\n return result;\n }\n\n public asStream<S, T>(\n source: OperationResultSource<OperationResult<S, { data: T }>>,\n ) {\n const handleGQLErrors = this.handleGQLErrors;\n return new ReadableStream<S>({\n start(controller) {\n source.subscribe(({ data, hasNext, error }) => {\n if (error) {\n if (\n error.message.includes(\"BodyStreamBuffer was aborted\") ||\n error.message.includes(\"signal is aborted without reason\")\n ) {\n // close the stream if there is no next item\n if (!hasNext) controller.close();\n\n //suppress this specific error\n console.warn(\"Abort error suppressed\");\n return;\n }\n\n // Handle structured errors specially - check if it's a CopilotKitError with visibility\n if ((error as any).extensions?.visibility) {\n // Create a synthetic GraphQL error with the structured error info\n const syntheticError = {\n ...error,\n graphQLErrors: [\n {\n message: error.message,\n extensions: (error as any).extensions,\n },\n ],\n };\n\n if (handleGQLErrors) {\n handleGQLErrors(syntheticError);\n }\n return; // Don't close the stream for structured errors, let the error handler decide\n }\n\n controller.error(error);\n if (handleGQLErrors) {\n handleGQLErrors(error);\n }\n } else {\n controller.enqueue(data);\n if (!hasNext) {\n controller.close();\n }\n }\n });\n },\n });\n }\n\n availableAgents() {\n const fetchFn = createFetchFn();\n return this.client.query<AvailableAgentsQuery>(\n getAvailableAgentsQuery,\n {},\n { fetch: fetchFn },\n );\n }\n\n loadAgentState(data: { threadId: string; agentName: string }) {\n const fetchFn = createFetchFn();\n const result = this.client.query<LoadAgentStateQuery>(\n loadAgentStateQuery,\n { data },\n { fetch: fetchFn },\n );\n\n // Add error handling for GraphQL errors - similar to generateCopilotResponse\n result\n .toPromise()\n .then(({ error }) => {\n if (error && this.handleGQLErrors) {\n this.handleGQLErrors(error);\n }\n })\n .catch(() => {}); // Suppress promise rejection warnings\n\n return result;\n }\n\n static removeGraphQLTypename(data: any) {\n if (Array.isArray(data)) {\n data.forEach((item) => CopilotRuntimeClient.removeGraphQLTypename(item));\n } else if (typeof data === \"object\" && data !== null) {\n delete data.__typename;\n Object.keys(data).forEach((key) => {\n if (typeof data[key] === \"object\" && data[key] !== null) {\n CopilotRuntimeClient.removeGraphQLTypename(data[key]);\n }\n });\n }\n return data;\n }\n}\n"],"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WAC/D,OAAO,SAAS,YAAY,SAAS,MAAM;AACpD,UAAO,KAAK;AACZ,UAAO,KAAK,KAAK,CAAC,SAAS,QAAQ;AACjC,QAAI,OAAO,KAAK,SAAS,YAAY,KAAK,SAAS,KACjD,sBAAqB,sBAAsB,KAAK,KAAK;KAEvD;;AAEJ,SAAO"}
@@ -0,0 +1,44 @@
1
+ import { AvailableAgentsQuery, Exact, GenerateCopilotResponseInput, GenerateCopilotResponseMutation, GenerateCopilotResponseMutationVariables, InputMaybe, LoadAgentStateQuery, Scalars } from "../graphql/@generated/graphql.cjs";
2
+ import * as urql from "urql";
3
+ import { OperationResult, OperationResultSource } from "urql";
4
+ import { Client } from "@urql/core";
5
+
6
+ //#region src/client/CopilotRuntimeClient.d.ts
7
+ interface CopilotRuntimeClientOptions {
8
+ url: string;
9
+ publicApiKey?: string;
10
+ headers?: Record<string, string>;
11
+ credentials?: RequestCredentials;
12
+ handleGQLErrors?: (error: Error) => void;
13
+ handleGQLWarning?: (warning: string) => void;
14
+ }
15
+ declare class CopilotRuntimeClient {
16
+ client: Client;
17
+ handleGQLErrors?: (error: Error) => void;
18
+ handleGQLWarning?: (warning: string) => void;
19
+ constructor(options: CopilotRuntimeClientOptions);
20
+ generateCopilotResponse({
21
+ data,
22
+ properties,
23
+ signal
24
+ }: {
25
+ data: GenerateCopilotResponseMutationVariables["data"];
26
+ properties?: GenerateCopilotResponseMutationVariables["properties"];
27
+ signal?: AbortSignal;
28
+ }): OperationResultSource<OperationResult<GenerateCopilotResponseMutation, Exact<{
29
+ data: GenerateCopilotResponseInput;
30
+ properties?: InputMaybe<Scalars["JSONObject"]["input"]>;
31
+ }>>>;
32
+ asStream<S, T>(source: OperationResultSource<OperationResult<S, {
33
+ data: T;
34
+ }>>): ReadableStream<S>;
35
+ availableAgents(): OperationResultSource<OperationResult<AvailableAgentsQuery, urql.AnyVariables>>;
36
+ loadAgentState(data: {
37
+ threadId: string;
38
+ agentName: string;
39
+ }): OperationResultSource<OperationResult<LoadAgentStateQuery, urql.AnyVariables>>;
40
+ static removeGraphQLTypename(data: any): any;
41
+ }
42
+ //#endregion
43
+ export { CopilotRuntimeClient, CopilotRuntimeClientOptions };
44
+ //# sourceMappingURL=CopilotRuntimeClient.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CopilotRuntimeClient.d.cts","names":[],"sources":["../../src/client/CopilotRuntimeClient.ts"],"mappings":";;;;;;UAsEiB,2BAAA;EACf,GAAA;EACA,YAAA;EACA,OAAA,GAAU,MAAA;EACV,WAAA,GAAc,kBAAA;EACd,eAAA,IAAmB,KAAA,EAAO,KAAA;EAC1B,gBAAA,IAAoB,OAAA;AAAA;AAAA,cAGT,oBAAA;EACX,MAAA,EAAQ,MAAA;EACD,eAAA,IAAmB,KAAA,EAAO,KAAA;EAC1B,gBAAA,IAAoB,OAAA;cAEf,OAAA,EAAS,2BAAA;EA2BrB,uBAAA,CAAA;IACE,IAAA;IACA,UAAA;IACA;EAAA;IAEA,IAAA,EAAM,wCAAA;IACN,UAAA,GAAa,wCAAA;IACb,MAAA,GAAS,WAAA;EAAA,IACV,qBAAA,CAAA,eAAA,CAAA,+BAAA;UAAA,4BAAA;iBAgHujM,UAAA;;EAlGjjM,QAAA,MAAA,CACL,MAAA,EAAQ,qBAAA,CAAsB,eAAA,CAAgB,CAAA;IAAK,IAAA,EAAM,CAAA;EAAA,MAAK,cAAA,CAAA,CAAA;EAqDhE,eAAA,CAAA,GAAe,qBAAA,CAAA,eAAA,CAAA,oBAAA,EAAA,IAAA,CAAA,YAAA;EASf,cAAA,CAAe,IAAA;IAAQ,QAAA;IAAkB,SAAA;EAAA,IAAmB,qBAAA,CAAA,eAAA,CAAA,mBAAA,EAAA,IAAA,CAAA,YAAA;EAAA,OAqBrD,qBAAA,CAAsB,IAAA;AAAA"}
@@ -0,0 +1,44 @@
1
+ import { AvailableAgentsQuery, Exact, GenerateCopilotResponseInput, GenerateCopilotResponseMutation, GenerateCopilotResponseMutationVariables, InputMaybe, LoadAgentStateQuery, Scalars } from "../graphql/@generated/graphql.mjs";
2
+ import { Client } from "@urql/core";
3
+ import * as urql from "urql";
4
+ import { OperationResult, OperationResultSource } from "urql";
5
+
6
+ //#region src/client/CopilotRuntimeClient.d.ts
7
+ interface CopilotRuntimeClientOptions {
8
+ url: string;
9
+ publicApiKey?: string;
10
+ headers?: Record<string, string>;
11
+ credentials?: RequestCredentials;
12
+ handleGQLErrors?: (error: Error) => void;
13
+ handleGQLWarning?: (warning: string) => void;
14
+ }
15
+ declare class CopilotRuntimeClient {
16
+ client: Client;
17
+ handleGQLErrors?: (error: Error) => void;
18
+ handleGQLWarning?: (warning: string) => void;
19
+ constructor(options: CopilotRuntimeClientOptions);
20
+ generateCopilotResponse({
21
+ data,
22
+ properties,
23
+ signal
24
+ }: {
25
+ data: GenerateCopilotResponseMutationVariables["data"];
26
+ properties?: GenerateCopilotResponseMutationVariables["properties"];
27
+ signal?: AbortSignal;
28
+ }): OperationResultSource<OperationResult<GenerateCopilotResponseMutation, Exact<{
29
+ data: GenerateCopilotResponseInput;
30
+ properties?: InputMaybe<Scalars["JSONObject"]["input"]>;
31
+ }>>>;
32
+ asStream<S, T>(source: OperationResultSource<OperationResult<S, {
33
+ data: T;
34
+ }>>): ReadableStream<S>;
35
+ availableAgents(): OperationResultSource<OperationResult<AvailableAgentsQuery, urql.AnyVariables>>;
36
+ loadAgentState(data: {
37
+ threadId: string;
38
+ agentName: string;
39
+ }): OperationResultSource<OperationResult<LoadAgentStateQuery, urql.AnyVariables>>;
40
+ static removeGraphQLTypename(data: any): any;
41
+ }
42
+ //#endregion
43
+ export { CopilotRuntimeClient, CopilotRuntimeClientOptions };
44
+ //# sourceMappingURL=CopilotRuntimeClient.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CopilotRuntimeClient.d.mts","names":[],"sources":["../../src/client/CopilotRuntimeClient.ts"],"mappings":";;;;;;UAsEiB,2BAAA;EACf,GAAA;EACA,YAAA;EACA,OAAA,GAAU,MAAA;EACV,WAAA,GAAc,kBAAA;EACd,eAAA,IAAmB,KAAA,EAAO,KAAA;EAC1B,gBAAA,IAAoB,OAAA;AAAA;AAAA,cAGT,oBAAA;EACX,MAAA,EAAQ,MAAA;EACD,eAAA,IAAmB,KAAA,EAAO,KAAA;EAC1B,gBAAA,IAAoB,OAAA;cAEf,OAAA,EAAS,2BAAA;EA2BrB,uBAAA,CAAA;IACE,IAAA;IACA,UAAA;IACA;EAAA;IAEA,IAAA,EAAM,wCAAA;IACN,UAAA,GAAa,wCAAA;IACb,MAAA,GAAS,WAAA;EAAA,IACV,qBAAA,CAAA,eAAA,CAAA,+BAAA;UAAA,4BAAA;iBAgHujM,UAAA;;EAlGjjM,QAAA,MAAA,CACL,MAAA,EAAQ,qBAAA,CAAsB,eAAA,CAAgB,CAAA;IAAK,IAAA,EAAM,CAAA;EAAA,MAAK,cAAA,CAAA,CAAA;EAqDhE,eAAA,CAAA,GAAe,qBAAA,CAAA,eAAA,CAAA,oBAAA,EAAA,IAAA,CAAA,YAAA;EASf,cAAA,CAAe,IAAA;IAAQ,QAAA;IAAkB,SAAA;EAAA,IAAmB,qBAAA,CAAA,eAAA,CAAA,mBAAA,EAAA,IAAA,CAAA,YAAA;EAAA,OAqBrD,qBAAA,CAAsB,IAAA;AAAA"}
@@ -1,12 +1,118 @@
1
- import {
2
- CopilotRuntimeClient
3
- } from "../chunk-2XWNDVTL.mjs";
4
- import "../chunk-QUH7QWUA.mjs";
5
- import "../chunk-E5FOLXLN.mjs";
6
- import "../chunk-SDQPJXLL.mjs";
7
- import "../chunk-OBDQ5BWD.mjs";
8
- import "../chunk-DELDZXUX.mjs";
9
- export {
10
- CopilotRuntimeClient
1
+ import { version } from "../package.mjs";
2
+ import { generateCopilotResponseMutation } from "../graphql/definitions/mutations.mjs";
3
+ import { getAvailableAgentsQuery, loadAgentStateQuery } from "../graphql/definitions/queries.mjs";
4
+ import { Client, cacheExchange, fetchExchange } from "@urql/core";
5
+ import { CopilotKitError, CopilotKitLowLevelError, CopilotKitVersionMismatchError, ResolvedCopilotKitError, getPossibleVersionMismatch } from "@copilotkit/shared";
6
+
7
+ //#region src/client/CopilotRuntimeClient.ts
8
+ const createFetchFn = (signal, handleGQLWarning) => async (...args) => {
9
+ const publicApiKey = args[1]?.headers?.["x-copilotcloud-public-api-key"];
10
+ try {
11
+ const result = await fetch(args[0], {
12
+ ...args[1] ?? {},
13
+ signal
14
+ });
15
+ const mismatch = publicApiKey ? null : await getPossibleVersionMismatch({
16
+ runtimeVersion: result.headers.get("X-CopilotKit-Runtime-Version"),
17
+ runtimeClientGqlVersion: version
18
+ });
19
+ if (result.status !== 200) {
20
+ if (result.status >= 400 && result.status <= 500) {
21
+ if (mismatch) throw new CopilotKitVersionMismatchError(mismatch);
22
+ throw new ResolvedCopilotKitError({ status: result.status });
23
+ }
24
+ }
25
+ if (mismatch && handleGQLWarning) handleGQLWarning(mismatch.message);
26
+ return result;
27
+ } catch (error) {
28
+ if (error.message.includes("BodyStreamBuffer was aborted") || error.message.includes("signal is aborted without reason")) throw error;
29
+ if (error instanceof CopilotKitError) throw error;
30
+ throw new CopilotKitLowLevelError({
31
+ error,
32
+ url: args[0]
33
+ });
34
+ }
11
35
  };
36
+ var CopilotRuntimeClient = class CopilotRuntimeClient {
37
+ constructor(options) {
38
+ const headers = {};
39
+ this.handleGQLErrors = options.handleGQLErrors;
40
+ this.handleGQLWarning = options.handleGQLWarning;
41
+ if (options.headers) Object.assign(headers, options.headers);
42
+ if (options.publicApiKey) headers["x-copilotcloud-public-api-key"] = options.publicApiKey;
43
+ this.client = new Client({
44
+ url: options.url,
45
+ exchanges: [cacheExchange, fetchExchange],
46
+ fetchOptions: {
47
+ headers: {
48
+ ...headers,
49
+ "X-CopilotKit-Runtime-Client-GQL-Version": version
50
+ },
51
+ ...options.credentials ? { credentials: options.credentials } : {}
52
+ }
53
+ });
54
+ }
55
+ generateCopilotResponse({ data, properties, signal }) {
56
+ const fetchFn = createFetchFn(signal, this.handleGQLWarning);
57
+ return this.client.mutation(generateCopilotResponseMutation, {
58
+ data,
59
+ properties
60
+ }, { fetch: fetchFn });
61
+ }
62
+ asStream(source) {
63
+ const handleGQLErrors = this.handleGQLErrors;
64
+ return new ReadableStream({ start(controller) {
65
+ source.subscribe(({ data, hasNext, error }) => {
66
+ if (error) {
67
+ if (error.message.includes("BodyStreamBuffer was aborted") || error.message.includes("signal is aborted without reason")) {
68
+ if (!hasNext) controller.close();
69
+ console.warn("Abort error suppressed");
70
+ return;
71
+ }
72
+ if (error.extensions?.visibility) {
73
+ const syntheticError = {
74
+ ...error,
75
+ graphQLErrors: [{
76
+ message: error.message,
77
+ extensions: error.extensions
78
+ }]
79
+ };
80
+ if (handleGQLErrors) handleGQLErrors(syntheticError);
81
+ return;
82
+ }
83
+ controller.error(error);
84
+ if (handleGQLErrors) handleGQLErrors(error);
85
+ } else {
86
+ controller.enqueue(data);
87
+ if (!hasNext) controller.close();
88
+ }
89
+ });
90
+ } });
91
+ }
92
+ availableAgents() {
93
+ const fetchFn = createFetchFn();
94
+ return this.client.query(getAvailableAgentsQuery, {}, { fetch: fetchFn });
95
+ }
96
+ loadAgentState(data) {
97
+ const fetchFn = createFetchFn();
98
+ const result = this.client.query(loadAgentStateQuery, { data }, { fetch: fetchFn });
99
+ result.toPromise().then(({ error }) => {
100
+ if (error && this.handleGQLErrors) this.handleGQLErrors(error);
101
+ }).catch(() => {});
102
+ return result;
103
+ }
104
+ static removeGraphQLTypename(data) {
105
+ if (Array.isArray(data)) data.forEach((item) => CopilotRuntimeClient.removeGraphQLTypename(item));
106
+ else if (typeof data === "object" && data !== null) {
107
+ delete data.__typename;
108
+ Object.keys(data).forEach((key) => {
109
+ if (typeof data[key] === "object" && data[key] !== null) CopilotRuntimeClient.removeGraphQLTypename(data[key]);
110
+ });
111
+ }
112
+ return data;
113
+ }
114
+ };
115
+
116
+ //#endregion
117
+ export { CopilotRuntimeClient };
12
118
  //# sourceMappingURL=CopilotRuntimeClient.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
1
+ {"version":3,"file":"CopilotRuntimeClient.mjs","names":["packageJson.version"],"sources":["../../src/client/CopilotRuntimeClient.ts"],"sourcesContent":["import { Client, cacheExchange, fetchExchange } from \"@urql/core\";\nimport * as packageJson from \"../../package.json\";\nimport {\n AvailableAgentsQuery,\n GenerateCopilotResponseMutation,\n GenerateCopilotResponseMutationVariables,\n LoadAgentStateQuery,\n} from \"../graphql/@generated/graphql\";\nimport { generateCopilotResponseMutation } from \"../graphql/definitions/mutations\";\nimport {\n getAvailableAgentsQuery,\n loadAgentStateQuery,\n} from \"../graphql/definitions/queries\";\nimport { OperationResultSource, OperationResult } from \"urql\";\nimport {\n ResolvedCopilotKitError,\n CopilotKitLowLevelError,\n CopilotKitError,\n CopilotKitVersionMismatchError,\n getPossibleVersionMismatch,\n} from \"@copilotkit/shared\";\n\nconst createFetchFn =\n (signal?: AbortSignal, handleGQLWarning?: (warning: string) => void) =>\n async (...args: Parameters<typeof fetch>) => {\n // @ts-expect-error -- since this is our own header, TS will not recognize\n const publicApiKey = args[1]?.headers?.[\"x-copilotcloud-public-api-key\"];\n try {\n const result = await fetch(args[0], { ...(args[1] ?? {}), signal });\n\n // No mismatch checking if cloud is being used\n const mismatch = publicApiKey\n ? null\n : await getPossibleVersionMismatch({\n runtimeVersion: result.headers.get(\"X-CopilotKit-Runtime-Version\")!,\n runtimeClientGqlVersion: packageJson.version,\n });\n if (result.status !== 200) {\n if (result.status >= 400 && result.status <= 500) {\n if (mismatch) {\n throw new CopilotKitVersionMismatchError(mismatch);\n }\n\n throw new ResolvedCopilotKitError({ status: result.status });\n }\n }\n\n if (mismatch && handleGQLWarning) {\n handleGQLWarning(mismatch.message);\n }\n\n return result;\n } catch (error) {\n // Let abort error pass through. It will be suppressed later\n if (\n (error as Error).message.includes(\"BodyStreamBuffer was aborted\") ||\n (error as Error).message.includes(\"signal is aborted without reason\")\n ) {\n throw error;\n }\n if (error instanceof CopilotKitError) {\n throw error;\n }\n throw new CopilotKitLowLevelError({\n error: error as Error,\n url: args[0] as string,\n });\n }\n };\n\nexport interface CopilotRuntimeClientOptions {\n url: string;\n publicApiKey?: string;\n headers?: Record<string, string>;\n credentials?: RequestCredentials;\n handleGQLErrors?: (error: Error) => void;\n handleGQLWarning?: (warning: string) => void;\n}\n\nexport class CopilotRuntimeClient {\n client: Client;\n public handleGQLErrors?: (error: Error) => void;\n public handleGQLWarning?: (warning: string) => void;\n\n constructor(options: CopilotRuntimeClientOptions) {\n const headers: Record<string, string> = {};\n\n this.handleGQLErrors = options.handleGQLErrors;\n this.handleGQLWarning = options.handleGQLWarning;\n\n if (options.headers) {\n Object.assign(headers, options.headers);\n }\n\n if (options.publicApiKey) {\n headers[\"x-copilotcloud-public-api-key\"] = options.publicApiKey;\n }\n\n this.client = new Client({\n url: options.url,\n exchanges: [cacheExchange, fetchExchange],\n fetchOptions: {\n headers: {\n ...headers,\n \"X-CopilotKit-Runtime-Client-GQL-Version\": packageJson.version,\n },\n ...(options.credentials ? { credentials: options.credentials } : {}),\n },\n });\n }\n\n generateCopilotResponse({\n data,\n properties,\n signal,\n }: {\n data: GenerateCopilotResponseMutationVariables[\"data\"];\n properties?: GenerateCopilotResponseMutationVariables[\"properties\"];\n signal?: AbortSignal;\n }) {\n const fetchFn = createFetchFn(signal, this.handleGQLWarning);\n const result = this.client.mutation<\n GenerateCopilotResponseMutation,\n GenerateCopilotResponseMutationVariables\n >(\n generateCopilotResponseMutation,\n { data, properties },\n { fetch: fetchFn },\n );\n\n return result;\n }\n\n public asStream<S, T>(\n source: OperationResultSource<OperationResult<S, { data: T }>>,\n ) {\n const handleGQLErrors = this.handleGQLErrors;\n return new ReadableStream<S>({\n start(controller) {\n source.subscribe(({ data, hasNext, error }) => {\n if (error) {\n if (\n error.message.includes(\"BodyStreamBuffer was aborted\") ||\n error.message.includes(\"signal is aborted without reason\")\n ) {\n // close the stream if there is no next item\n if (!hasNext) controller.close();\n\n //suppress this specific error\n console.warn(\"Abort error suppressed\");\n return;\n }\n\n // Handle structured errors specially - check if it's a CopilotKitError with visibility\n if ((error as any).extensions?.visibility) {\n // Create a synthetic GraphQL error with the structured error info\n const syntheticError = {\n ...error,\n graphQLErrors: [\n {\n message: error.message,\n extensions: (error as any).extensions,\n },\n ],\n };\n\n if (handleGQLErrors) {\n handleGQLErrors(syntheticError);\n }\n return; // Don't close the stream for structured errors, let the error handler decide\n }\n\n controller.error(error);\n if (handleGQLErrors) {\n handleGQLErrors(error);\n }\n } else {\n controller.enqueue(data);\n if (!hasNext) {\n controller.close();\n }\n }\n });\n },\n });\n }\n\n availableAgents() {\n const fetchFn = createFetchFn();\n return this.client.query<AvailableAgentsQuery>(\n getAvailableAgentsQuery,\n {},\n { fetch: fetchFn },\n );\n }\n\n loadAgentState(data: { threadId: string; agentName: string }) {\n const fetchFn = createFetchFn();\n const result = this.client.query<LoadAgentStateQuery>(\n loadAgentStateQuery,\n { data },\n { fetch: fetchFn },\n );\n\n // Add error handling for GraphQL errors - similar to generateCopilotResponse\n result\n .toPromise()\n .then(({ error }) => {\n if (error && this.handleGQLErrors) {\n this.handleGQLErrors(error);\n }\n })\n .catch(() => {}); // Suppress promise rejection warnings\n\n return result;\n }\n\n static removeGraphQLTypename(data: any) {\n if (Array.isArray(data)) {\n data.forEach((item) => CopilotRuntimeClient.removeGraphQLTypename(item));\n } else if (typeof data === \"object\" && data !== null) {\n delete data.__typename;\n Object.keys(data).forEach((key) => {\n if (typeof data[key] === \"object\" && data[key] !== null) {\n CopilotRuntimeClient.removeGraphQLTypename(data[key]);\n }\n });\n }\n return data;\n }\n}\n"],"mappings":";;;;;;;AAsBA,MAAM,iBACH,QAAsB,qBACvB,OAAO,GAAG,SAAmC;CAE3C,MAAM,eAAe,KAAK,IAAI,UAAU;AACxC,KAAI;EACF,MAAM,SAAS,MAAM,MAAM,KAAK,IAAI;GAAE,GAAI,KAAK,MAAM,EAAE;GAAG;GAAQ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EvD;;AAEJ,SAAO"}
@@ -0,0 +1,194 @@
1
+ const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
2
+ const require_graphql = require('../graphql/@generated/graphql.cjs');
3
+ const require_types = require('./types.cjs');
4
+ let _copilotkit_shared = require("@copilotkit/shared");
5
+ let untruncate_json = require("untruncate-json");
6
+ untruncate_json = require_runtime.__toESM(untruncate_json);
7
+
8
+ //#region src/client/conversion.ts
9
+ function filterAgentStateMessages(messages) {
10
+ return messages.filter((message) => !message.isAgentStateMessage());
11
+ }
12
+ function convertMessagesToGqlInput(messages) {
13
+ return messages.map((message) => {
14
+ if (message.isTextMessage()) return {
15
+ id: message.id,
16
+ createdAt: message.createdAt,
17
+ textMessage: {
18
+ content: message.content,
19
+ role: message.role,
20
+ parentMessageId: message.parentMessageId
21
+ }
22
+ };
23
+ else if (message.isActionExecutionMessage()) return {
24
+ id: message.id,
25
+ createdAt: message.createdAt,
26
+ actionExecutionMessage: {
27
+ name: message.name,
28
+ arguments: JSON.stringify(message.arguments),
29
+ parentMessageId: message.parentMessageId
30
+ }
31
+ };
32
+ else if (message.isResultMessage()) return {
33
+ id: message.id,
34
+ createdAt: message.createdAt,
35
+ resultMessage: {
36
+ result: message.result,
37
+ actionExecutionId: message.actionExecutionId,
38
+ actionName: message.actionName
39
+ }
40
+ };
41
+ else if (message.isAgentStateMessage()) return {
42
+ id: message.id,
43
+ createdAt: message.createdAt,
44
+ agentStateMessage: {
45
+ threadId: message.threadId,
46
+ role: message.role,
47
+ agentName: message.agentName,
48
+ nodeName: message.nodeName,
49
+ runId: message.runId,
50
+ active: message.active,
51
+ running: message.running,
52
+ state: JSON.stringify(message.state)
53
+ }
54
+ };
55
+ else if (message.isImageMessage()) return {
56
+ id: message.id,
57
+ createdAt: message.createdAt,
58
+ imageMessage: {
59
+ format: message.format,
60
+ bytes: message.bytes,
61
+ role: message.role,
62
+ parentMessageId: message.parentMessageId
63
+ }
64
+ };
65
+ else throw new Error("Unknown message type");
66
+ });
67
+ }
68
+ function filterAdjacentAgentStateMessages(messages) {
69
+ const filteredMessages = [];
70
+ messages.forEach((message, i) => {
71
+ if (message.__typename !== "AgentStateMessageOutput") filteredMessages.push(message);
72
+ else {
73
+ const prevAgentStateMessageIndex = filteredMessages.findIndex((m) => m.__typename === "AgentStateMessageOutput" && m.agentName === message.agentName);
74
+ if (prevAgentStateMessageIndex === -1) filteredMessages.push(message);
75
+ else filteredMessages[prevAgentStateMessageIndex] = message;
76
+ }
77
+ });
78
+ return filteredMessages;
79
+ }
80
+ function convertGqlOutputToMessages(messages) {
81
+ return messages.map((message) => {
82
+ if (message.__typename === "TextMessageOutput") return new require_types.TextMessage({
83
+ id: message.id,
84
+ role: message.role,
85
+ content: message.content.join(""),
86
+ parentMessageId: message.parentMessageId,
87
+ createdAt: /* @__PURE__ */ new Date(),
88
+ status: message.status || { code: require_graphql.MessageStatusCode.Pending }
89
+ });
90
+ else if (message.__typename === "ActionExecutionMessageOutput") return new require_types.ActionExecutionMessage({
91
+ id: message.id,
92
+ name: message.name,
93
+ arguments: getPartialArguments(message.arguments),
94
+ parentMessageId: message.parentMessageId,
95
+ createdAt: /* @__PURE__ */ new Date(),
96
+ status: message.status || { code: require_graphql.MessageStatusCode.Pending }
97
+ });
98
+ else if (message.__typename === "ResultMessageOutput") return new require_types.ResultMessage({
99
+ id: message.id,
100
+ result: message.result,
101
+ actionExecutionId: message.actionExecutionId,
102
+ actionName: message.actionName,
103
+ createdAt: /* @__PURE__ */ new Date(),
104
+ status: message.status || { code: require_graphql.MessageStatusCode.Pending }
105
+ });
106
+ else if (message.__typename === "AgentStateMessageOutput") return new require_types.AgentStateMessage({
107
+ id: message.id,
108
+ threadId: message.threadId,
109
+ role: message.role,
110
+ agentName: message.agentName,
111
+ nodeName: message.nodeName,
112
+ runId: message.runId,
113
+ active: message.active,
114
+ running: message.running,
115
+ state: (0, _copilotkit_shared.parseJson)(message.state, {}),
116
+ createdAt: /* @__PURE__ */ new Date()
117
+ });
118
+ else if (message.__typename === "ImageMessageOutput") return new require_types.ImageMessage({
119
+ id: message.id,
120
+ format: message.format,
121
+ bytes: message.bytes,
122
+ role: message.role,
123
+ parentMessageId: message.parentMessageId,
124
+ createdAt: /* @__PURE__ */ new Date(),
125
+ status: message.status || { code: require_graphql.MessageStatusCode.Pending }
126
+ });
127
+ throw new Error("Unknown message type");
128
+ });
129
+ }
130
+ function loadMessagesFromJsonRepresentation(json) {
131
+ const result = [];
132
+ for (const item of json) if ("content" in item) result.push(new require_types.TextMessage({
133
+ id: item.id,
134
+ role: item.role,
135
+ content: item.content,
136
+ parentMessageId: item.parentMessageId,
137
+ createdAt: item.createdAt || /* @__PURE__ */ new Date(),
138
+ status: item.status || { code: require_graphql.MessageStatusCode.Success }
139
+ }));
140
+ else if ("arguments" in item) result.push(new require_types.ActionExecutionMessage({
141
+ id: item.id,
142
+ name: item.name,
143
+ arguments: item.arguments,
144
+ parentMessageId: item.parentMessageId,
145
+ createdAt: item.createdAt || /* @__PURE__ */ new Date(),
146
+ status: item.status || { code: require_graphql.MessageStatusCode.Success }
147
+ }));
148
+ else if ("result" in item) result.push(new require_types.ResultMessage({
149
+ id: item.id,
150
+ result: item.result,
151
+ actionExecutionId: item.actionExecutionId,
152
+ actionName: item.actionName,
153
+ createdAt: item.createdAt || /* @__PURE__ */ new Date(),
154
+ status: item.status || { code: require_graphql.MessageStatusCode.Success }
155
+ }));
156
+ else if ("state" in item) result.push(new require_types.AgentStateMessage({
157
+ id: item.id,
158
+ threadId: item.threadId,
159
+ role: item.role,
160
+ agentName: item.agentName,
161
+ nodeName: item.nodeName,
162
+ runId: item.runId,
163
+ active: item.active,
164
+ running: item.running,
165
+ state: item.state,
166
+ createdAt: item.createdAt || /* @__PURE__ */ new Date()
167
+ }));
168
+ else if ("format" in item && "bytes" in item) result.push(new require_types.ImageMessage({
169
+ id: item.id,
170
+ format: item.format,
171
+ bytes: item.bytes,
172
+ role: item.role,
173
+ parentMessageId: item.parentMessageId,
174
+ createdAt: item.createdAt || /* @__PURE__ */ new Date(),
175
+ status: item.status || { code: require_graphql.MessageStatusCode.Success }
176
+ }));
177
+ return result;
178
+ }
179
+ function getPartialArguments(args) {
180
+ try {
181
+ if (!args.length) return {};
182
+ return JSON.parse((0, untruncate_json.default)(args.join("")));
183
+ } catch (e) {
184
+ return {};
185
+ }
186
+ }
187
+
188
+ //#endregion
189
+ exports.convertGqlOutputToMessages = convertGqlOutputToMessages;
190
+ exports.convertMessagesToGqlInput = convertMessagesToGqlInput;
191
+ exports.filterAdjacentAgentStateMessages = filterAdjacentAgentStateMessages;
192
+ exports.filterAgentStateMessages = filterAgentStateMessages;
193
+ exports.loadMessagesFromJsonRepresentation = loadMessagesFromJsonRepresentation;
194
+ //# sourceMappingURL=conversion.cjs.map