@copilotkit/runtime-client-gql 0.0.0-mme-load-agent-state-20250117154700

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 (92) hide show
  1. package/CHANGELOG.md +937 -0
  2. package/README.md +46 -0
  3. package/codegen.ts +22 -0
  4. package/dist/chunk-7ECCT6PK.mjs +1 -0
  5. package/dist/chunk-7ECCT6PK.mjs.map +1 -0
  6. package/dist/chunk-BBNGLVX5.mjs +38 -0
  7. package/dist/chunk-BBNGLVX5.mjs.map +1 -0
  8. package/dist/chunk-GQ2BQR46.mjs +119 -0
  9. package/dist/chunk-GQ2BQR46.mjs.map +1 -0
  10. package/dist/chunk-JGGSE6H3.mjs +20 -0
  11. package/dist/chunk-JGGSE6H3.mjs.map +1 -0
  12. package/dist/chunk-LBECNDL5.mjs +56 -0
  13. package/dist/chunk-LBECNDL5.mjs.map +1 -0
  14. package/dist/chunk-MLWD2EGY.mjs +200 -0
  15. package/dist/chunk-MLWD2EGY.mjs.map +1 -0
  16. package/dist/chunk-RHAQH56F.mjs +84 -0
  17. package/dist/chunk-RHAQH56F.mjs.map +1 -0
  18. package/dist/chunk-RIPDMDI6.mjs +77 -0
  19. package/dist/chunk-RIPDMDI6.mjs.map +1 -0
  20. package/dist/chunk-XZPL6QVK.mjs +24 -0
  21. package/dist/chunk-XZPL6QVK.mjs.map +1 -0
  22. package/dist/client/CopilotRuntimeClient.d.ts +37 -0
  23. package/dist/client/CopilotRuntimeClient.js +251 -0
  24. package/dist/client/CopilotRuntimeClient.js.map +1 -0
  25. package/dist/client/CopilotRuntimeClient.mjs +11 -0
  26. package/dist/client/CopilotRuntimeClient.mjs.map +1 -0
  27. package/dist/client/conversion.d.ts +11 -0
  28. package/dist/client/conversion.js +296 -0
  29. package/dist/client/conversion.js.map +1 -0
  30. package/dist/client/conversion.mjs +17 -0
  31. package/dist/client/conversion.mjs.map +1 -0
  32. package/dist/client/index.d.ts +8 -0
  33. package/dist/client/index.js +541 -0
  34. package/dist/client/index.js.map +1 -0
  35. package/dist/client/index.mjs +38 -0
  36. package/dist/client/index.mjs.map +1 -0
  37. package/dist/client/types.d.ts +55 -0
  38. package/dist/client/types.js +113 -0
  39. package/dist/client/types.js.map +1 -0
  40. package/dist/client/types.mjs +18 -0
  41. package/dist/client/types.mjs.map +1 -0
  42. package/dist/graphql/@generated/fragment-masking.d.ts +22 -0
  43. package/dist/graphql/@generated/fragment-masking.js +50 -0
  44. package/dist/graphql/@generated/fragment-masking.js.map +1 -0
  45. package/dist/graphql/@generated/fragment-masking.mjs +11 -0
  46. package/dist/graphql/@generated/fragment-masking.mjs.map +1 -0
  47. package/dist/graphql/@generated/gql.d.ts +53 -0
  48. package/dist/graphql/@generated/gql.js +45 -0
  49. package/dist/graphql/@generated/gql.js.map +1 -0
  50. package/dist/graphql/@generated/gql.mjs +8 -0
  51. package/dist/graphql/@generated/gql.mjs.map +1 -0
  52. package/dist/graphql/@generated/graphql.d.ts +477 -0
  53. package/dist/graphql/@generated/graphql.js +88 -0
  54. package/dist/graphql/@generated/graphql.js.map +1 -0
  55. package/dist/graphql/@generated/graphql.mjs +23 -0
  56. package/dist/graphql/@generated/graphql.mjs.map +1 -0
  57. package/dist/graphql/@generated/index.d.ts +4 -0
  58. package/dist/graphql/@generated/index.js +69 -0
  59. package/dist/graphql/@generated/index.js.map +1 -0
  60. package/dist/graphql/@generated/index.mjs +16 -0
  61. package/dist/graphql/@generated/index.mjs.map +1 -0
  62. package/dist/graphql/definitions/mutations.d.ts +9 -0
  63. package/dist/graphql/definitions/mutations.js +121 -0
  64. package/dist/graphql/definitions/mutations.js.map +1 -0
  65. package/dist/graphql/definitions/mutations.mjs +9 -0
  66. package/dist/graphql/definitions/mutations.mjs.map +1 -0
  67. package/dist/graphql/definitions/queries.d.ts +11 -0
  68. package/dist/graphql/definitions/queries.js +76 -0
  69. package/dist/graphql/definitions/queries.js.map +1 -0
  70. package/dist/graphql/definitions/queries.mjs +11 -0
  71. package/dist/graphql/definitions/queries.mjs.map +1 -0
  72. package/dist/index.d.ts +8 -0
  73. package/dist/index.js +591 -0
  74. package/dist/index.js.map +1 -0
  75. package/dist/index.mjs +57 -0
  76. package/dist/index.mjs.map +1 -0
  77. package/jest.config.js +5 -0
  78. package/package.json +77 -0
  79. package/src/client/CopilotRuntimeClient.ts +150 -0
  80. package/src/client/conversion.ts +211 -0
  81. package/src/client/index.ts +10 -0
  82. package/src/client/types.ts +127 -0
  83. package/src/graphql/@generated/fragment-masking.ts +87 -0
  84. package/src/graphql/@generated/gql.ts +52 -0
  85. package/src/graphql/@generated/graphql.ts +366 -0
  86. package/src/graphql/@generated/index.ts +2 -0
  87. package/src/graphql/definitions/mutations.ts +73 -0
  88. package/src/graphql/definitions/queries.ts +24 -0
  89. package/src/index.ts +2 -0
  90. package/tsconfig.json +16 -0
  91. package/tsup.config.ts +33 -0
  92. package/typedoc.json +4 -0
@@ -0,0 +1,541 @@
1
+ "use strict";
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 __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/client/index.ts
31
+ var client_exports = {};
32
+ __export(client_exports, {
33
+ ActionExecutionMessage: () => ActionExecutionMessage,
34
+ AgentStateMessage: () => AgentStateMessage,
35
+ CopilotRuntimeClient: () => CopilotRuntimeClient,
36
+ Message: () => Message,
37
+ ResultMessage: () => ResultMessage,
38
+ Role: () => Role,
39
+ TextMessage: () => TextMessage,
40
+ convertGqlOutputToMessages: () => convertGqlOutputToMessages,
41
+ convertMessagesToGqlInput: () => convertMessagesToGqlInput,
42
+ filterAdjacentAgentStateMessages: () => filterAdjacentAgentStateMessages,
43
+ filterAgentStateMessages: () => filterAgentStateMessages,
44
+ loadMessagesFromJsonRepresentation: () => loadMessagesFromJsonRepresentation
45
+ });
46
+ module.exports = __toCommonJS(client_exports);
47
+
48
+ // src/client/CopilotRuntimeClient.ts
49
+ var import_core = require("@urql/core");
50
+
51
+ // package.json
52
+ var version = "1.5.12-next.4";
53
+
54
+ // src/graphql/@generated/graphql.ts
55
+ var MessageRole = /* @__PURE__ */ ((MessageRole2) => {
56
+ MessageRole2["Assistant"] = "assistant";
57
+ MessageRole2["System"] = "system";
58
+ MessageRole2["Tool"] = "tool";
59
+ MessageRole2["User"] = "user";
60
+ return MessageRole2;
61
+ })(MessageRole || {});
62
+ var GenerateCopilotResponseDocument = { "kind": "Document", "definitions": [{ "kind": "OperationDefinition", "operation": "mutation", "name": { "kind": "Name", "value": "generateCopilotResponse" }, "variableDefinitions": [{ "kind": "VariableDefinition", "variable": { "kind": "Variable", "name": { "kind": "Name", "value": "data" } }, "type": { "kind": "NonNullType", "type": { "kind": "NamedType", "name": { "kind": "Name", "value": "GenerateCopilotResponseInput" } } } }, { "kind": "VariableDefinition", "variable": { "kind": "Variable", "name": { "kind": "Name", "value": "properties" } }, "type": { "kind": "NamedType", "name": { "kind": "Name", "value": "JSONObject" } } }], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "generateCopilotResponse" }, "arguments": [{ "kind": "Argument", "name": { "kind": "Name", "value": "data" }, "value": { "kind": "Variable", "name": { "kind": "Name", "value": "data" } } }, { "kind": "Argument", "name": { "kind": "Name", "value": "properties" }, "value": { "kind": "Variable", "name": { "kind": "Name", "value": "properties" } } }], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "threadId" } }, { "kind": "Field", "name": { "kind": "Name", "value": "runId" } }, { "kind": "Field", "name": { "kind": "Name", "value": "extensions" }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "openaiAssistantAPI" }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "runId" } }, { "kind": "Field", "name": { "kind": "Name", "value": "threadId" } }] } }] } }, { "kind": "InlineFragment", "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "CopilotResponse" } }, "directives": [{ "kind": "Directive", "name": { "kind": "Name", "value": "defer" } }], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "status" }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "InlineFragment", "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "BaseResponseStatus" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "code" } }] } }, { "kind": "InlineFragment", "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "FailedResponseStatus" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "reason" } }, { "kind": "Field", "name": { "kind": "Name", "value": "details" } }] } }] } }] } }, { "kind": "Field", "name": { "kind": "Name", "value": "messages" }, "directives": [{ "kind": "Directive", "name": { "kind": "Name", "value": "stream" } }], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "__typename" } }, { "kind": "InlineFragment", "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "BaseMessageOutput" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "id" } }, { "kind": "Field", "name": { "kind": "Name", "value": "createdAt" } }] } }, { "kind": "InlineFragment", "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "BaseMessageOutput" } }, "directives": [{ "kind": "Directive", "name": { "kind": "Name", "value": "defer" } }], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "status" }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "InlineFragment", "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "SuccessMessageStatus" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "code" } }] } }, { "kind": "InlineFragment", "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "FailedMessageStatus" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "code" } }, { "kind": "Field", "name": { "kind": "Name", "value": "reason" } }] } }, { "kind": "InlineFragment", "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "PendingMessageStatus" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "code" } }] } }] } }] } }, { "kind": "InlineFragment", "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "TextMessageOutput" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "content" }, "directives": [{ "kind": "Directive", "name": { "kind": "Name", "value": "stream" } }] }, { "kind": "Field", "name": { "kind": "Name", "value": "role" } }, { "kind": "Field", "name": { "kind": "Name", "value": "parentMessageId" } }] } }, { "kind": "InlineFragment", "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "ActionExecutionMessageOutput" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "name" } }, { "kind": "Field", "name": { "kind": "Name", "value": "arguments" }, "directives": [{ "kind": "Directive", "name": { "kind": "Name", "value": "stream" } }] }, { "kind": "Field", "name": { "kind": "Name", "value": "parentMessageId" } }] } }, { "kind": "InlineFragment", "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "ResultMessageOutput" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "result" } }, { "kind": "Field", "name": { "kind": "Name", "value": "actionExecutionId" } }, { "kind": "Field", "name": { "kind": "Name", "value": "actionName" } }] } }, { "kind": "InlineFragment", "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "AgentStateMessageOutput" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "threadId" } }, { "kind": "Field", "name": { "kind": "Name", "value": "state" } }, { "kind": "Field", "name": { "kind": "Name", "value": "running" } }, { "kind": "Field", "name": { "kind": "Name", "value": "agentName" } }, { "kind": "Field", "name": { "kind": "Name", "value": "nodeName" } }, { "kind": "Field", "name": { "kind": "Name", "value": "runId" } }, { "kind": "Field", "name": { "kind": "Name", "value": "active" } }, { "kind": "Field", "name": { "kind": "Name", "value": "role" } }] } }] } }] } }] } }] };
63
+ var AvailableAgentsDocument = { "kind": "Document", "definitions": [{ "kind": "OperationDefinition", "operation": "query", "name": { "kind": "Name", "value": "availableAgents" }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "availableAgents" }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "agents" }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "name" } }, { "kind": "Field", "name": { "kind": "Name", "value": "id" } }, { "kind": "Field", "name": { "kind": "Name", "value": "description" } }] } }] } }] } }] };
64
+ var LoadAgentStateDocument = { "kind": "Document", "definitions": [{ "kind": "OperationDefinition", "operation": "query", "name": { "kind": "Name", "value": "loadAgentState" }, "variableDefinitions": [{ "kind": "VariableDefinition", "variable": { "kind": "Variable", "name": { "kind": "Name", "value": "data" } }, "type": { "kind": "NonNullType", "type": { "kind": "NamedType", "name": { "kind": "Name", "value": "LoadAgentStateInput" } } } }], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "loadAgentState" }, "arguments": [{ "kind": "Argument", "name": { "kind": "Name", "value": "data" }, "value": { "kind": "Variable", "name": { "kind": "Name", "value": "data" } } }], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "threadId" } }, { "kind": "Field", "name": { "kind": "Name", "value": "threadExists" } }, { "kind": "Field", "name": { "kind": "Name", "value": "state" } }, { "kind": "Field", "name": { "kind": "Name", "value": "messages" } }] } }] } }] };
65
+
66
+ // src/graphql/@generated/gql.ts
67
+ var documents = {
68
+ "\n mutation generateCopilotResponse($data: GenerateCopilotResponseInput!, $properties: JSONObject) {\n generateCopilotResponse(data: $data, properties: $properties) {\n threadId\n runId\n extensions {\n openaiAssistantAPI {\n runId\n threadId\n }\n }\n ... on CopilotResponse @defer {\n status {\n ... on BaseResponseStatus {\n code\n }\n ... on FailedResponseStatus {\n reason\n details\n }\n }\n }\n messages @stream {\n __typename\n ... on BaseMessageOutput {\n id\n createdAt\n }\n ... on BaseMessageOutput @defer {\n status {\n ... on SuccessMessageStatus {\n code\n }\n ... on FailedMessageStatus {\n code\n reason\n }\n ... on PendingMessageStatus {\n code\n }\n }\n }\n ... on TextMessageOutput {\n content @stream\n role\n parentMessageId\n }\n ... on ActionExecutionMessageOutput {\n name\n arguments @stream\n parentMessageId\n }\n ... on ResultMessageOutput {\n result\n actionExecutionId\n actionName\n }\n ... on AgentStateMessageOutput {\n threadId\n state\n running\n agentName\n nodeName\n runId\n active\n role\n }\n }\n }\n }\n": GenerateCopilotResponseDocument,
69
+ "\n query availableAgents {\n availableAgents {\n agents {\n name\n id\n description\n }\n }\n }\n": AvailableAgentsDocument,
70
+ "\n query loadAgentState($data: LoadAgentStateInput!) {\n loadAgentState(data: $data) {\n threadId\n threadExists\n state\n messages\n }\n }\n": LoadAgentStateDocument
71
+ };
72
+ function graphql(source) {
73
+ return documents[source] ?? {};
74
+ }
75
+
76
+ // src/graphql/definitions/mutations.ts
77
+ var generateCopilotResponseMutation = graphql(
78
+ /** GraphQL **/
79
+ `
80
+ mutation generateCopilotResponse($data: GenerateCopilotResponseInput!, $properties: JSONObject) {
81
+ generateCopilotResponse(data: $data, properties: $properties) {
82
+ threadId
83
+ runId
84
+ extensions {
85
+ openaiAssistantAPI {
86
+ runId
87
+ threadId
88
+ }
89
+ }
90
+ ... on CopilotResponse @defer {
91
+ status {
92
+ ... on BaseResponseStatus {
93
+ code
94
+ }
95
+ ... on FailedResponseStatus {
96
+ reason
97
+ details
98
+ }
99
+ }
100
+ }
101
+ messages @stream {
102
+ __typename
103
+ ... on BaseMessageOutput {
104
+ id
105
+ createdAt
106
+ }
107
+ ... on BaseMessageOutput @defer {
108
+ status {
109
+ ... on SuccessMessageStatus {
110
+ code
111
+ }
112
+ ... on FailedMessageStatus {
113
+ code
114
+ reason
115
+ }
116
+ ... on PendingMessageStatus {
117
+ code
118
+ }
119
+ }
120
+ }
121
+ ... on TextMessageOutput {
122
+ content @stream
123
+ role
124
+ parentMessageId
125
+ }
126
+ ... on ActionExecutionMessageOutput {
127
+ name
128
+ arguments @stream
129
+ parentMessageId
130
+ }
131
+ ... on ResultMessageOutput {
132
+ result
133
+ actionExecutionId
134
+ actionName
135
+ }
136
+ ... on AgentStateMessageOutput {
137
+ threadId
138
+ state
139
+ running
140
+ agentName
141
+ nodeName
142
+ runId
143
+ active
144
+ role
145
+ }
146
+ }
147
+ }
148
+ }
149
+ `
150
+ );
151
+
152
+ // src/graphql/definitions/queries.ts
153
+ var getAvailableAgentsQuery = graphql(
154
+ /** GraphQL **/
155
+ `
156
+ query availableAgents {
157
+ availableAgents {
158
+ agents {
159
+ name
160
+ id
161
+ description
162
+ }
163
+ }
164
+ }
165
+ `
166
+ );
167
+ var loadAgentStateQuery = graphql(
168
+ /** GraphQL **/
169
+ `
170
+ query loadAgentState($data: LoadAgentStateInput!) {
171
+ loadAgentState(data: $data) {
172
+ threadId
173
+ threadExists
174
+ state
175
+ messages
176
+ }
177
+ }
178
+ `
179
+ );
180
+
181
+ // src/client/CopilotRuntimeClient.ts
182
+ var createFetchFn = (signal) => async (...args) => {
183
+ const result = await fetch(args[0], { ...args[1] ?? {}, signal });
184
+ if (result.status !== 200) {
185
+ switch (result.status) {
186
+ case 404:
187
+ throw new Error(
188
+ "Runtime URL seems to be invalid - got 404 response. Please check the runtimeUrl passed to CopilotKit"
189
+ );
190
+ default:
191
+ throw new Error("Could not fetch copilot response");
192
+ }
193
+ }
194
+ return result;
195
+ };
196
+ var CopilotRuntimeClient = class {
197
+ constructor(options) {
198
+ const headers = {};
199
+ this.handleGQLErrors = options.handleGQLErrors;
200
+ if (options.headers) {
201
+ Object.assign(headers, options.headers);
202
+ }
203
+ if (options.publicApiKey) {
204
+ headers["x-copilotcloud-public-api-key"] = options.publicApiKey;
205
+ }
206
+ this.client = new import_core.Client({
207
+ url: options.url,
208
+ exchanges: [import_core.cacheExchange, import_core.fetchExchange],
209
+ fetchOptions: {
210
+ headers: {
211
+ ...headers,
212
+ "X-CopilotKit-Runtime-Client-GQL-Version": version
213
+ },
214
+ ...options.credentials ? { credentials: options.credentials } : {}
215
+ }
216
+ });
217
+ }
218
+ generateCopilotResponse({
219
+ data,
220
+ properties,
221
+ signal
222
+ }) {
223
+ const fetchFn = createFetchFn(signal);
224
+ const result = this.client.mutation(generateCopilotResponseMutation, { data, properties }, { fetch: fetchFn });
225
+ return result;
226
+ }
227
+ asStream(source) {
228
+ const handleGQLErrors = this.handleGQLErrors;
229
+ return new ReadableStream({
230
+ start(controller) {
231
+ source.subscribe(({ data, hasNext, error }) => {
232
+ if (error) {
233
+ if (error.message.includes("BodyStreamBuffer was aborted") || error.message.includes("signal is aborted without reason")) {
234
+ console.warn("Abort error suppressed");
235
+ return;
236
+ }
237
+ controller.error(error);
238
+ if (handleGQLErrors) {
239
+ handleGQLErrors(error);
240
+ }
241
+ } else {
242
+ controller.enqueue(data);
243
+ if (!hasNext) {
244
+ controller.close();
245
+ }
246
+ }
247
+ });
248
+ }
249
+ });
250
+ }
251
+ availableAgents() {
252
+ const fetchFn = createFetchFn();
253
+ return this.client.query(getAvailableAgentsQuery, {}, { fetch: fetchFn });
254
+ }
255
+ loadAgentState(data) {
256
+ const fetchFn = createFetchFn();
257
+ return this.client.query(
258
+ loadAgentStateQuery,
259
+ { data },
260
+ { fetch: fetchFn }
261
+ );
262
+ }
263
+ static removeGraphQLTypename(data) {
264
+ if (Array.isArray(data)) {
265
+ data.forEach((item) => CopilotRuntimeClient.removeGraphQLTypename(item));
266
+ } else if (typeof data === "object" && data !== null) {
267
+ delete data.__typename;
268
+ Object.keys(data).forEach((key) => {
269
+ if (typeof data[key] === "object" && data[key] !== null) {
270
+ CopilotRuntimeClient.removeGraphQLTypename(data[key]);
271
+ }
272
+ });
273
+ }
274
+ return data;
275
+ }
276
+ };
277
+
278
+ // src/client/types.ts
279
+ var import_shared = require("@copilotkit/shared");
280
+ var Message = class {
281
+ constructor(props) {
282
+ props.id ?? (props.id = (0, import_shared.randomId)());
283
+ props.status ?? (props.status = { code: "Success" /* Success */ });
284
+ props.createdAt ?? (props.createdAt = /* @__PURE__ */ new Date());
285
+ Object.assign(this, props);
286
+ }
287
+ isTextMessage() {
288
+ return this.type === "TextMessage";
289
+ }
290
+ isActionExecutionMessage() {
291
+ return this.type === "ActionExecutionMessage";
292
+ }
293
+ isResultMessage() {
294
+ return this.type === "ResultMessage";
295
+ }
296
+ isAgentStateMessage() {
297
+ return this.type === "AgentStateMessage";
298
+ }
299
+ };
300
+ var Role = MessageRole;
301
+ var TextMessage = class extends Message {
302
+ constructor(props) {
303
+ super(props);
304
+ this.type = "TextMessage";
305
+ }
306
+ };
307
+ var ActionExecutionMessage = class extends Message {
308
+ constructor(props) {
309
+ super(props);
310
+ this.type = "ActionExecutionMessage";
311
+ }
312
+ };
313
+ var ResultMessage = class extends Message {
314
+ constructor(props) {
315
+ super(props);
316
+ this.type = "ResultMessage";
317
+ }
318
+ static decodeResult(result) {
319
+ try {
320
+ return JSON.parse(result);
321
+ } catch (e) {
322
+ return result;
323
+ }
324
+ }
325
+ static encodeResult(result) {
326
+ if (result === void 0) {
327
+ return "";
328
+ } else if (typeof result === "string") {
329
+ return result;
330
+ } else {
331
+ return JSON.stringify(result);
332
+ }
333
+ }
334
+ };
335
+ var AgentStateMessage = class extends Message {
336
+ constructor(props) {
337
+ super(props);
338
+ this.type = "AgentStateMessage";
339
+ }
340
+ };
341
+
342
+ // src/client/conversion.ts
343
+ var import_untruncate_json = __toESM(require("untruncate-json"));
344
+ function filterAgentStateMessages(messages) {
345
+ return messages.filter((message) => !message.isAgentStateMessage());
346
+ }
347
+ function convertMessagesToGqlInput(messages) {
348
+ return messages.map((message) => {
349
+ if (message.isTextMessage()) {
350
+ return {
351
+ id: message.id,
352
+ createdAt: message.createdAt,
353
+ textMessage: {
354
+ content: message.content,
355
+ role: message.role,
356
+ parentMessageId: message.parentMessageId
357
+ }
358
+ };
359
+ } else if (message.isActionExecutionMessage()) {
360
+ return {
361
+ id: message.id,
362
+ createdAt: message.createdAt,
363
+ actionExecutionMessage: {
364
+ name: message.name,
365
+ arguments: JSON.stringify(message.arguments),
366
+ parentMessageId: message.parentMessageId
367
+ }
368
+ };
369
+ } else if (message.isResultMessage()) {
370
+ return {
371
+ id: message.id,
372
+ createdAt: message.createdAt,
373
+ resultMessage: {
374
+ result: message.result,
375
+ actionExecutionId: message.actionExecutionId,
376
+ actionName: message.actionName
377
+ }
378
+ };
379
+ } else if (message.isAgentStateMessage()) {
380
+ return {
381
+ id: message.id,
382
+ createdAt: message.createdAt,
383
+ agentStateMessage: {
384
+ threadId: message.threadId,
385
+ role: message.role,
386
+ agentName: message.agentName,
387
+ nodeName: message.nodeName,
388
+ runId: message.runId,
389
+ active: message.active,
390
+ running: message.running,
391
+ state: JSON.stringify(message.state)
392
+ }
393
+ };
394
+ } else {
395
+ throw new Error("Unknown message type");
396
+ }
397
+ });
398
+ }
399
+ function filterAdjacentAgentStateMessages(messages) {
400
+ const filteredMessages = [];
401
+ messages.forEach((message, i) => {
402
+ if (message.__typename !== "AgentStateMessageOutput") {
403
+ filteredMessages.push(message);
404
+ } else {
405
+ const prevAgentStateMessageIndex = filteredMessages.findIndex(
406
+ // TODO: also check runId
407
+ (m) => m.__typename === "AgentStateMessageOutput" && m.agentName === message.agentName
408
+ );
409
+ if (prevAgentStateMessageIndex === -1) {
410
+ filteredMessages.push(message);
411
+ } else {
412
+ filteredMessages[prevAgentStateMessageIndex] = message;
413
+ }
414
+ }
415
+ });
416
+ return filteredMessages;
417
+ }
418
+ function convertGqlOutputToMessages(messages) {
419
+ return messages.map((message) => {
420
+ if (message.__typename === "TextMessageOutput") {
421
+ return new TextMessage({
422
+ id: message.id,
423
+ role: message.role,
424
+ content: message.content.join(""),
425
+ parentMessageId: message.parentMessageId,
426
+ createdAt: /* @__PURE__ */ new Date(),
427
+ status: message.status || { code: "Pending" /* Pending */ }
428
+ });
429
+ } else if (message.__typename === "ActionExecutionMessageOutput") {
430
+ return new ActionExecutionMessage({
431
+ id: message.id,
432
+ name: message.name,
433
+ arguments: getPartialArguments(message.arguments),
434
+ parentMessageId: message.parentMessageId,
435
+ createdAt: /* @__PURE__ */ new Date(),
436
+ status: message.status || { code: "Pending" /* Pending */ }
437
+ });
438
+ } else if (message.__typename === "ResultMessageOutput") {
439
+ return new ResultMessage({
440
+ id: message.id,
441
+ result: message.result,
442
+ actionExecutionId: message.actionExecutionId,
443
+ actionName: message.actionName,
444
+ createdAt: /* @__PURE__ */ new Date(),
445
+ status: message.status || { code: "Pending" /* Pending */ }
446
+ });
447
+ } else if (message.__typename === "AgentStateMessageOutput") {
448
+ return new AgentStateMessage({
449
+ id: message.id,
450
+ threadId: message.threadId,
451
+ role: message.role,
452
+ agentName: message.agentName,
453
+ nodeName: message.nodeName,
454
+ runId: message.runId,
455
+ active: message.active,
456
+ running: message.running,
457
+ state: JSON.parse(message.state),
458
+ createdAt: /* @__PURE__ */ new Date()
459
+ });
460
+ }
461
+ throw new Error("Unknown message type");
462
+ });
463
+ }
464
+ function loadMessagesFromJsonRepresentation(json) {
465
+ const result = [];
466
+ for (const item of json) {
467
+ if ("content" in item) {
468
+ result.push(
469
+ new TextMessage({
470
+ id: item.id,
471
+ role: item.role,
472
+ content: item.content,
473
+ parentMessageId: item.parentMessageId,
474
+ createdAt: item.createdAt || /* @__PURE__ */ new Date(),
475
+ status: item.status || { code: "Success" /* Success */ }
476
+ })
477
+ );
478
+ } else if ("arguments" in item) {
479
+ result.push(
480
+ new ActionExecutionMessage({
481
+ id: item.id,
482
+ name: item.name,
483
+ arguments: item.arguments,
484
+ parentMessageId: item.parentMessageId,
485
+ createdAt: item.createdAt || /* @__PURE__ */ new Date(),
486
+ status: item.status || { code: "Success" /* Success */ }
487
+ })
488
+ );
489
+ } else if ("result" in item) {
490
+ result.push(
491
+ new ResultMessage({
492
+ id: item.id,
493
+ result: item.result,
494
+ actionExecutionId: item.actionExecutionId,
495
+ actionName: item.actionName,
496
+ createdAt: item.createdAt || /* @__PURE__ */ new Date(),
497
+ status: item.status || { code: "Success" /* Success */ }
498
+ })
499
+ );
500
+ } else if ("state" in item) {
501
+ result.push(
502
+ new AgentStateMessage({
503
+ id: item.id,
504
+ threadId: item.threadId,
505
+ role: item.role,
506
+ agentName: item.agentName,
507
+ nodeName: item.nodeName,
508
+ runId: item.runId,
509
+ active: item.active,
510
+ running: item.running,
511
+ state: item.state,
512
+ createdAt: item.createdAt || /* @__PURE__ */ new Date()
513
+ })
514
+ );
515
+ }
516
+ }
517
+ return result;
518
+ }
519
+ function getPartialArguments(args) {
520
+ try {
521
+ return JSON.parse((0, import_untruncate_json.default)(args.join("")));
522
+ } catch (e) {
523
+ return {};
524
+ }
525
+ }
526
+ // Annotate the CommonJS export names for ESM import in node:
527
+ 0 && (module.exports = {
528
+ ActionExecutionMessage,
529
+ AgentStateMessage,
530
+ CopilotRuntimeClient,
531
+ Message,
532
+ ResultMessage,
533
+ Role,
534
+ TextMessage,
535
+ convertGqlOutputToMessages,
536
+ convertMessagesToGqlInput,
537
+ filterAdjacentAgentStateMessages,
538
+ filterAgentStateMessages,
539
+ loadMessagesFromJsonRepresentation
540
+ });
541
+ //# sourceMappingURL=index.js.map