UNPKG

@copilotkit/runtime-client-gql

Version:

<div align="center"> <a href="https://copilotkit.ai" target="_blank"> <img src="https://github.com/copilotkit/copilotkit/raw/main/assets/banner.png" alt="CopilotKit Logo"> </a>

676 lines (665 loc) 37 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { ActionExecutionMessage: () => ActionExecutionMessage, ActionInputAvailability: () => ActionInputAvailability, AgentStateMessage: () => AgentStateMessage, AvailableAgentsDocument: () => AvailableAgentsDocument, CopilotRequestType: () => CopilotRequestType, CopilotRuntimeClient: () => CopilotRuntimeClient, FailedResponseStatusReason: () => FailedResponseStatusReason, GenerateCopilotResponseDocument: () => GenerateCopilotResponseDocument, LoadAgentStateDocument: () => LoadAgentStateDocument, Message: () => Message, MessageRole: () => MessageRole, MessageStatusCode: () => MessageStatusCode, MetaEventName: () => MetaEventName, ResponseStatusCode: () => ResponseStatusCode, ResultMessage: () => ResultMessage, Role: () => Role, TextMessage: () => TextMessage, convertGqlOutputToMessages: () => convertGqlOutputToMessages, convertMessagesToGqlInput: () => convertMessagesToGqlInput, filterAdjacentAgentStateMessages: () => filterAdjacentAgentStateMessages, filterAgentStateMessages: () => filterAgentStateMessages, langGraphInterruptEvent: () => langGraphInterruptEvent, loadMessagesFromJsonRepresentation: () => loadMessagesFromJsonRepresentation }); module.exports = __toCommonJS(src_exports); // src/client/CopilotRuntimeClient.ts var import_core = require("@urql/core"); // package.json var version = "1.8.4"; // src/graphql/@generated/graphql.ts var ActionInputAvailability = /* @__PURE__ */ ((ActionInputAvailability2) => { ActionInputAvailability2["Disabled"] = "disabled"; ActionInputAvailability2["Enabled"] = "enabled"; ActionInputAvailability2["Remote"] = "remote"; return ActionInputAvailability2; })(ActionInputAvailability || {}); var CopilotRequestType = /* @__PURE__ */ ((CopilotRequestType2) => { CopilotRequestType2["Chat"] = "Chat"; CopilotRequestType2["Suggestion"] = "Suggestion"; CopilotRequestType2["Task"] = "Task"; CopilotRequestType2["TextareaCompletion"] = "TextareaCompletion"; CopilotRequestType2["TextareaPopover"] = "TextareaPopover"; return CopilotRequestType2; })(CopilotRequestType || {}); var FailedResponseStatusReason = /* @__PURE__ */ ((FailedResponseStatusReason2) => { FailedResponseStatusReason2["GuardrailsValidationFailed"] = "GUARDRAILS_VALIDATION_FAILED"; FailedResponseStatusReason2["MessageStreamInterrupted"] = "MESSAGE_STREAM_INTERRUPTED"; FailedResponseStatusReason2["UnknownError"] = "UNKNOWN_ERROR"; return FailedResponseStatusReason2; })(FailedResponseStatusReason || {}); var MessageRole = /* @__PURE__ */ ((MessageRole2) => { MessageRole2["Assistant"] = "assistant"; MessageRole2["Developer"] = "developer"; MessageRole2["System"] = "system"; MessageRole2["Tool"] = "tool"; MessageRole2["User"] = "user"; return MessageRole2; })(MessageRole || {}); var MessageStatusCode = /* @__PURE__ */ ((MessageStatusCode2) => { MessageStatusCode2["Failed"] = "Failed"; MessageStatusCode2["Pending"] = "Pending"; MessageStatusCode2["Success"] = "Success"; return MessageStatusCode2; })(MessageStatusCode || {}); var MetaEventName = /* @__PURE__ */ ((MetaEventName2) => { MetaEventName2["CopilotKitLangGraphInterruptEvent"] = "CopilotKitLangGraphInterruptEvent"; MetaEventName2["LangGraphInterruptEvent"] = "LangGraphInterruptEvent"; return MetaEventName2; })(MetaEventName || {}); var ResponseStatusCode = /* @__PURE__ */ ((ResponseStatusCode2) => { ResponseStatusCode2["Failed"] = "Failed"; ResponseStatusCode2["Pending"] = "Pending"; ResponseStatusCode2["Success"] = "Success"; return ResponseStatusCode2; })(ResponseStatusCode || {}); 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" } }] } }] } }, { "kind": "Field", "name": { "kind": "Name", "value": "metaEvents" }, "directives": [{ "kind": "Directive", "name": { "kind": "Name", "value": "stream" } }], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "InlineFragment", "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "LangGraphInterruptEvent" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "type" } }, { "kind": "Field", "name": { "kind": "Name", "value": "name" } }, { "kind": "Field", "name": { "kind": "Name", "value": "value" } }] } }, { "kind": "InlineFragment", "typeCondition": { "kind": "NamedType", "name": { "kind": "Name", "value": "CopilotKitLangGraphInterruptEvent" } }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "type" } }, { "kind": "Field", "name": { "kind": "Name", "value": "name" } }, { "kind": "Field", "name": { "kind": "Name", "value": "data" }, "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "messages" }, "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" } }, { "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" } }, { "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": "Field", "name": { "kind": "Name", "value": "value" } }] } }] } }] } }] } }] } }] }; 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" } }] } }] } }] } }] }; 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" } }] } }] } }] }; // src/graphql/@generated/gql.ts var documents = { "\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 metaEvents @stream {\n ... on LangGraphInterruptEvent {\n type\n name\n value\n }\n\n ... on CopilotKitLangGraphInterruptEvent {\n type\n name\n data {\n messages {\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\n role\n parentMessageId\n }\n ... on ActionExecutionMessageOutput {\n name\n arguments\n parentMessageId\n }\n ... on ResultMessageOutput {\n result\n actionExecutionId\n actionName\n }\n }\n value\n }\n }\n }\n }\n }\n": GenerateCopilotResponseDocument, "\n query availableAgents {\n availableAgents {\n agents {\n name\n id\n description\n }\n }\n }\n": AvailableAgentsDocument, "\n query loadAgentState($data: LoadAgentStateInput!) {\n loadAgentState(data: $data) {\n threadId\n threadExists\n state\n messages\n }\n }\n": LoadAgentStateDocument }; function graphql(source) { return documents[source] ?? {}; } // src/graphql/definitions/mutations.ts var generateCopilotResponseMutation = graphql( /** GraphQL **/ ` mutation generateCopilotResponse($data: GenerateCopilotResponseInput!, $properties: JSONObject) { generateCopilotResponse(data: $data, properties: $properties) { threadId runId extensions { openaiAssistantAPI { runId threadId } } ... on CopilotResponse @defer { status { ... on BaseResponseStatus { code } ... on FailedResponseStatus { reason details } } } messages @stream { __typename ... on BaseMessageOutput { id createdAt } ... on BaseMessageOutput @defer { status { ... on SuccessMessageStatus { code } ... on FailedMessageStatus { code reason } ... on PendingMessageStatus { code } } } ... on TextMessageOutput { content @stream role parentMessageId } ... on ActionExecutionMessageOutput { name arguments @stream parentMessageId } ... on ResultMessageOutput { result actionExecutionId actionName } ... on AgentStateMessageOutput { threadId state running agentName nodeName runId active role } } metaEvents @stream { ... on LangGraphInterruptEvent { type name value } ... on CopilotKitLangGraphInterruptEvent { type name data { messages { __typename ... on BaseMessageOutput { id createdAt } ... on BaseMessageOutput @defer { status { ... on SuccessMessageStatus { code } ... on FailedMessageStatus { code reason } ... on PendingMessageStatus { code } } } ... on TextMessageOutput { content role parentMessageId } ... on ActionExecutionMessageOutput { name arguments parentMessageId } ... on ResultMessageOutput { result actionExecutionId actionName } } value } } } } } ` ); // src/graphql/definitions/queries.ts var getAvailableAgentsQuery = graphql( /** GraphQL **/ ` query availableAgents { availableAgents { agents { name id description } } } ` ); var loadAgentStateQuery = graphql( /** GraphQL **/ ` query loadAgentState($data: LoadAgentStateInput!) { loadAgentState(data: $data) { threadId threadExists state messages } } ` ); // src/client/CopilotRuntimeClient.ts var import_shared = require("@copilotkit/shared"); var createFetchFn = (signal, handleGQLWarning) => async (...args) => { var _a, _b; const publicApiKey = (_b = (_a = args[1]) == null ? void 0 : _a.headers) == null ? void 0 : _b["x-copilotcloud-public-api-key"]; try { const result = await fetch(args[0], { ...args[1] ?? {}, signal }); const mismatch = publicApiKey ? null : await (0, import_shared.getPossibleVersionMismatch)({ runtimeVersion: result.headers.get("X-CopilotKit-Runtime-Version"), runtimeClientGqlVersion: version }); if (result.status !== 200) { if (result.status >= 400 && result.status <= 500) { if (mismatch) { throw new import_shared.CopilotKitVersionMismatchError(mismatch); } throw new import_shared.ResolvedCopilotKitError({ status: result.status }); } } if (mismatch && handleGQLWarning) { handleGQLWarning(mismatch.message); } return result; } catch (error) { if (error.message.includes("BodyStreamBuffer was aborted") || error.message.includes("signal is aborted without reason")) { throw error; } if (error instanceof import_shared.CopilotKitError) { throw error; } throw new import_shared.CopilotKitLowLevelError({ error, url: args[0] }); } }; var CopilotRuntimeClient = class { constructor(options) { const headers = {}; this.handleGQLErrors = options.handleGQLErrors; this.handleGQLWarning = options.handleGQLWarning; if (options.headers) { Object.assign(headers, options.headers); } if (options.publicApiKey) { headers["x-copilotcloud-public-api-key"] = options.publicApiKey; } this.client = new import_core.Client({ url: options.url, exchanges: [import_core.cacheExchange, import_core.fetchExchange], fetchOptions: { headers: { ...headers, "X-CopilotKit-Runtime-Client-GQL-Version": version }, ...options.credentials ? { credentials: options.credentials } : {} } }); } generateCopilotResponse({ data, properties, signal }) { const fetchFn = createFetchFn(signal, this.handleGQLWarning); const result = this.client.mutation(generateCopilotResponseMutation, { data, properties }, { fetch: fetchFn }); return result; } asStream(source) { const handleGQLErrors = this.handleGQLErrors; return new ReadableStream({ start(controller) { source.subscribe(({ data, hasNext, error }) => { if (error) { if (error.message.includes("BodyStreamBuffer was aborted") || error.message.includes("signal is aborted without reason")) { if (!hasNext) controller.close(); console.warn("Abort error suppressed"); return; } controller.error(error); if (handleGQLErrors) { handleGQLErrors(error); } } else { controller.enqueue(data); if (!hasNext) { controller.close(); } } }); } }); } availableAgents() { const fetchFn = createFetchFn(); return this.client.query(getAvailableAgentsQuery, {}, { fetch: fetchFn }); } loadAgentState(data) { const fetchFn = createFetchFn(); return this.client.query( loadAgentStateQuery, { data }, { fetch: fetchFn } ); } static removeGraphQLTypename(data) { if (Array.isArray(data)) { data.forEach((item) => CopilotRuntimeClient.removeGraphQLTypename(item)); } else if (typeof data === "object" && data !== null) { delete data.__typename; Object.keys(data).forEach((key) => { if (typeof data[key] === "object" && data[key] !== null) { CopilotRuntimeClient.removeGraphQLTypename(data[key]); } }); } return data; } }; // src/client/types.ts var import_shared2 = require("@copilotkit/shared"); var import_shared3 = require("@copilotkit/shared"); var Message = class { constructor(props) { props.id ?? (props.id = (0, import_shared2.randomId)()); props.status ?? (props.status = { code: "Success" /* Success */ }); props.createdAt ?? (props.createdAt = /* @__PURE__ */ new Date()); Object.assign(this, props); } isTextMessage() { return this.type === "TextMessage"; } isActionExecutionMessage() { return this.type === "ActionExecutionMessage"; } isResultMessage() { return this.type === "ResultMessage"; } isAgentStateMessage() { return this.type === "AgentStateMessage"; } }; var Role = MessageRole; var TextMessage = class extends Message { constructor(props) { super(props); this.type = "TextMessage"; } }; var ActionExecutionMessage = class extends Message { constructor(props) { super(props); this.type = "ActionExecutionMessage"; } }; var ResultMessage = class extends Message { constructor(props) { super(props); this.type = "ResultMessage"; } static decodeResult(result) { return (0, import_shared3.parseJson)(result, result); } static encodeResult(result) { if (result === void 0) { return ""; } else if (typeof result === "string") { return result; } else { return JSON.stringify(result); } } }; var AgentStateMessage = class extends Message { constructor(props) { super(props); this.type = "AgentStateMessage"; } }; function langGraphInterruptEvent(eventProps) { return { ...eventProps, name: "LangGraphInterruptEvent" /* LangGraphInterruptEvent */, type: "MetaEvent" }; } // src/client/conversion.ts var import_untruncate_json = __toESM(require("untruncate-json")); var import_shared4 = require("@copilotkit/shared"); function filterAgentStateMessages(messages) { return messages.filter((message) => !message.isAgentStateMessage()); } function convertMessagesToGqlInput(messages) { return messages.map((message) => { if (message.isTextMessage()) { return { id: message.id, createdAt: message.createdAt, textMessage: { content: message.content, role: message.role, parentMessageId: message.parentMessageId } }; } else if (message.isActionExecutionMessage()) { return { id: message.id, createdAt: message.createdAt, actionExecutionMessage: { name: message.name, arguments: JSON.stringify(message.arguments), parentMessageId: message.parentMessageId } }; } else if (message.isResultMessage()) { return { id: message.id, createdAt: message.createdAt, resultMessage: { result: message.result, actionExecutionId: message.actionExecutionId, actionName: message.actionName } }; } else if (message.isAgentStateMessage()) { return { id: message.id, createdAt: message.createdAt, agentStateMessage: { threadId: message.threadId, role: message.role, agentName: message.agentName, nodeName: message.nodeName, runId: message.runId, active: message.active, running: message.running, state: JSON.stringify(message.state) } }; } else { throw new Error("Unknown message type"); } }); } function filterAdjacentAgentStateMessages(messages) { const filteredMessages = []; messages.forEach((message, i) => { if (message.__typename !== "AgentStateMessageOutput") { filteredMessages.push(message); } else { const prevAgentStateMessageIndex = filteredMessages.findIndex( // TODO: also check runId (m) => m.__typename === "AgentStateMessageOutput" && m.agentName === message.agentName ); if (prevAgentStateMessageIndex === -1) { filteredMessages.push(message); } else { filteredMessages[prevAgentStateMessageIndex] = message; } } }); return filteredMessages; } function convertGqlOutputToMessages(messages) { return messages.map((message) => { if (message.__typename === "TextMessageOutput") { return new TextMessage({ id: message.id, role: message.role, content: message.content.join(""), parentMessageId: message.parentMessageId, createdAt: /* @__PURE__ */ new Date(), status: message.status || { code: "Pending" /* Pending */ } }); } else if (message.__typename === "ActionExecutionMessageOutput") { return new ActionExecutionMessage({ id: message.id, name: message.name, arguments: getPartialArguments(message.arguments), parentMessageId: message.parentMessageId, createdAt: /* @__PURE__ */ new Date(), status: message.status || { code: "Pending" /* Pending */ } }); } else if (message.__typename === "ResultMessageOutput") { return new ResultMessage({ id: message.id, result: message.result, actionExecutionId: message.actionExecutionId, actionName: message.actionName, createdAt: /* @__PURE__ */ new Date(), status: message.status || { code: "Pending" /* Pending */ } }); } else if (message.__typename === "AgentStateMessageOutput") { return new AgentStateMessage({ id: message.id, threadId: message.threadId, role: message.role, agentName: message.agentName, nodeName: message.nodeName, runId: message.runId, active: message.active, running: message.running, state: (0, import_shared4.parseJson)(message.state, {}), createdAt: /* @__PURE__ */ new Date() }); } throw new Error("Unknown message type"); }); } function loadMessagesFromJsonRepresentation(json) { const result = []; for (const item of json) { if ("content" in item) { result.push( new TextMessage({ id: item.id, role: item.role, content: item.content, parentMessageId: item.parentMessageId, createdAt: item.createdAt || /* @__PURE__ */ new Date(), status: item.status || { code: "Success" /* Success */ } }) ); } else if ("arguments" in item) { result.push( new ActionExecutionMessage({ id: item.id, name: item.name, arguments: item.arguments, parentMessageId: item.parentMessageId, createdAt: item.createdAt || /* @__PURE__ */ new Date(), status: item.status || { code: "Success" /* Success */ } }) ); } else if ("result" in item) { result.push( new ResultMessage({ id: item.id, result: item.result, actionExecutionId: item.actionExecutionId, actionName: item.actionName, createdAt: item.createdAt || /* @__PURE__ */ new Date(), status: item.status || { code: "Success" /* Success */ } }) ); } else if ("state" in item) { result.push( new AgentStateMessage({ id: item.id, threadId: item.threadId, role: item.role, agentName: item.agentName, nodeName: item.nodeName, runId: item.runId, active: item.active, running: item.running, state: item.state, createdAt: item.createdAt || /* @__PURE__ */ new Date() }) ); } } return result; } function getPartialArguments(args) { try { if (!args.length) return {}; return JSON.parse((0, import_untruncate_json.default)(args.join(""))); } catch (e) { return {}; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ActionExecutionMessage, ActionInputAvailability, AgentStateMessage, AvailableAgentsDocument, CopilotRequestType, CopilotRuntimeClient, FailedResponseStatusReason, GenerateCopilotResponseDocument, LoadAgentStateDocument, Message, MessageRole, MessageStatusCode, MetaEventName, ResponseStatusCode, ResultMessage, Role, TextMessage, convertGqlOutputToMessages, convertMessagesToGqlInput, filterAdjacentAgentStateMessages, filterAgentStateMessages, langGraphInterruptEvent, loadMessagesFromJsonRepresentation }); //# sourceMappingURL=index.js.map