@copilotkit/runtime 1.8.5-next.3 → 1.8.5-next.5

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 (41) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/dist/{chunk-TDPCQIMB.mjs → chunk-AIQJ5N22.mjs} +2 -2
  3. package/dist/{chunk-UOCZJTXN.mjs → chunk-FP7EJSMU.mjs} +2 -2
  4. package/dist/{chunk-2PVUPC44.mjs → chunk-GQEDXH53.mjs} +2 -2
  5. package/dist/{chunk-UTUGZIQI.mjs → chunk-Q4KJZACA.mjs} +146 -9
  6. package/dist/chunk-Q4KJZACA.mjs.map +1 -0
  7. package/dist/index.d.ts +2 -1
  8. package/dist/index.js +183 -46
  9. package/dist/index.js.map +1 -1
  10. package/dist/index.mjs +4 -4
  11. package/dist/lib/index.d.ts +2 -1
  12. package/dist/lib/index.js +176 -39
  13. package/dist/lib/index.js.map +1 -1
  14. package/dist/lib/index.mjs +4 -4
  15. package/dist/lib/integrations/index.d.ts +3 -2
  16. package/dist/lib/integrations/index.js +15 -10
  17. package/dist/lib/integrations/index.js.map +1 -1
  18. package/dist/lib/integrations/index.mjs +4 -4
  19. package/dist/lib/integrations/nest/index.d.ts +2 -1
  20. package/dist/lib/integrations/nest/index.js +15 -10
  21. package/dist/lib/integrations/nest/index.js.map +1 -1
  22. package/dist/lib/integrations/nest/index.mjs +2 -2
  23. package/dist/lib/integrations/node-express/index.d.ts +2 -1
  24. package/dist/lib/integrations/node-express/index.js +15 -10
  25. package/dist/lib/integrations/node-express/index.js.map +1 -1
  26. package/dist/lib/integrations/node-express/index.mjs +2 -2
  27. package/dist/lib/integrations/node-http/index.d.ts +2 -1
  28. package/dist/lib/integrations/node-http/index.js +15 -10
  29. package/dist/lib/integrations/node-http/index.js.map +1 -1
  30. package/dist/lib/integrations/node-http/index.mjs +1 -1
  31. package/dist/{shared-86ec42e7.d.ts → shared-93687488.d.ts} +3 -0
  32. package/package.json +6 -2
  33. package/src/lib/runtime/agentwire-action.ts +123 -0
  34. package/src/lib/runtime/copilot-runtime.ts +9 -1
  35. package/src/lib/runtime/remote-action-constructors.ts +2 -7
  36. package/src/lib/runtime/remote-actions.ts +25 -1
  37. package/src/lib/runtime/remote-lg-action.ts +24 -2
  38. package/dist/chunk-UTUGZIQI.mjs.map +0 -1
  39. /package/dist/{chunk-TDPCQIMB.mjs.map → chunk-AIQJ5N22.mjs.map} +0 -0
  40. /package/dist/{chunk-UOCZJTXN.mjs.map → chunk-FP7EJSMU.mjs.map} +0 -0
  41. /package/dist/{chunk-2PVUPC44.mjs.map → chunk-GQEDXH53.mjs.map} +0 -0
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  copilotRuntimeNodeHttpEndpoint
3
- } from "../../../chunk-UTUGZIQI.mjs";
3
+ } from "../../../chunk-Q4KJZACA.mjs";
4
4
  import "../../../chunk-DNI7KA7Y.mjs";
5
5
  import "../../../chunk-5BIEM2UU.mjs";
6
6
  import "../../../chunk-RTFJTJMA.mjs";
@@ -5,6 +5,7 @@ import { Parameter, Action } from '@copilotkit/shared';
5
5
  import { b as CopilotServiceAdapter, A as ActionInput, d as AgentSessionInput, e as AgentStateInput, F as ForwardedParametersInput, E as ExtensionsInput, R as RemoteChainParameters, f as RuntimeEventSource, g as ExtensionsResponse } from './langserve-6f7af8d3.js';
6
6
  import { M as MessageInput, a as Message } from './index-5bec5424.js';
7
7
  import { CopilotCloudOptions } from './lib/cloud/index.js';
8
+ import { AbstractAgent } from '@agentwire/client';
8
9
 
9
10
  declare enum MetaEventName {
10
11
  LangGraphInterruptEvent = "LangGraphInterruptEvent",
@@ -274,6 +275,7 @@ interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []> {
274
275
  remoteActions?: CopilotKitEndpoint[];
275
276
  remoteEndpoints?: EndpointDefinition[];
276
277
  langserve?: RemoteChainParameters[];
278
+ agents?: Record<string, AbstractAgent>;
277
279
  delegateAgentProcessingToServiceAdapter?: boolean;
278
280
  /**
279
281
  * Configuration for LLM request/response logging.
@@ -333,6 +335,7 @@ interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []> {
333
335
  }
334
336
  declare class CopilotRuntime<const T extends Parameter[] | [] = []> {
335
337
  actions: ActionsConfiguration<T>;
338
+ agents: Record<string, AbstractAgent>;
336
339
  remoteEndpointDefinitions: EndpointDefinition[];
337
340
  private langserve;
338
341
  private onBeforeRequest?;
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "publishConfig": {
10
10
  "access": "public"
11
11
  },
12
- "version": "1.8.5-next.3",
12
+ "version": "1.8.5-next.5",
13
13
  "sideEffects": false,
14
14
  "main": "./dist/index.js",
15
15
  "module": "./dist/index.mjs",
@@ -36,6 +36,10 @@
36
36
  "tsconfig": "1.4.6"
37
37
  },
38
38
  "dependencies": {
39
+ "@agentwire/client": "0.0.26",
40
+ "@agentwire/proto": "0.0.26",
41
+ "@agentwire/core": "0.0.26",
42
+ "@agentwire/encoder": "0.0.26",
39
43
  "@anthropic-ai/sdk": "^0.27.3",
40
44
  "@graphql-yoga/plugin-defer-stream": "^3.3.1",
41
45
  "@langchain/community": "^0.3.29",
@@ -59,7 +63,7 @@
59
63
  "rxjs": "^7.8.1",
60
64
  "type-graphql": "2.0.0-rc.1",
61
65
  "zod": "^3.23.3",
62
- "@copilotkit/shared": "1.8.5-next.3"
66
+ "@copilotkit/shared": "1.8.5-next.5"
63
67
  },
64
68
  "keywords": [
65
69
  "copilotkit",
@@ -0,0 +1,123 @@
1
+ import { createHash } from "node:crypto";
2
+ import { CopilotKitEndpoint, RemoteAgentHandlerParams } from "./remote-actions";
3
+ import { GraphQLContext } from "../integrations";
4
+ import { Logger } from "pino";
5
+ import { Message } from "../../graphql/types/converted";
6
+ import { AgentStateInput } from "../../graphql/inputs/agent-state.input";
7
+ import { Observable } from "rxjs";
8
+ import { RuntimeEvent } from "../../service-adapters/events";
9
+ import telemetry from "../telemetry-client";
10
+
11
+ import { ToolCall } from "@agentwire/client";
12
+ import {
13
+ Message as AgentWireMessage,
14
+ AssistantMessage as AgentWireAssistantMessage,
15
+ } from "@agentwire/client";
16
+
17
+ import { parseJson } from "@copilotkit/shared";
18
+ import { AbstractAgent } from "@agentwire/client";
19
+
20
+ export function constructAgentWireRemoteAction({
21
+ logger,
22
+ messages,
23
+ agentStates,
24
+ agent,
25
+ }: {
26
+ logger: Logger;
27
+ messages: Message[];
28
+ agentStates?: AgentStateInput[];
29
+ agent: AbstractAgent;
30
+ }) {
31
+ const action = {
32
+ name: agent.agentId,
33
+ description: agent.description,
34
+ parameters: [],
35
+ handler: async (_args: any) => {},
36
+ remoteAgentHandler: async ({
37
+ actionInputsWithoutAgents,
38
+ threadId,
39
+ }: RemoteAgentHandlerParams): Promise<Observable<RuntimeEvent>> => {
40
+ logger.debug({ actionName: agent.agentId }, "Executing remote agent");
41
+
42
+ const agentWireMessages = convertMessagesToAgentWire(messages);
43
+ agent.messages = agentWireMessages;
44
+ agent.threadId = threadId;
45
+
46
+ telemetry.capture("oss.runtime.remote_action_executed", {
47
+ agentExecution: true,
48
+ type: "self-hosted",
49
+ agentsAmount: 1,
50
+ });
51
+
52
+ let state = {};
53
+ if (agentStates) {
54
+ const jsonState = agentStates.find((state) => state.agentName === agent.agentId);
55
+ if (jsonState) {
56
+ state = parseJson(jsonState.state, {});
57
+ }
58
+ }
59
+ agent.state = state;
60
+
61
+ const tools = actionInputsWithoutAgents.map((input) => {
62
+ return {
63
+ name: input.name,
64
+ description: input.description,
65
+ parameters: JSON.parse(input.jsonSchema),
66
+ };
67
+ });
68
+
69
+ return agent.legacy_to_be_removed_runAgentBridged({
70
+ tools,
71
+ }) as Observable<RuntimeEvent>;
72
+ },
73
+ };
74
+ return [action];
75
+ }
76
+
77
+ export function convertMessagesToAgentWire(messages: Message[]): AgentWireMessage[] {
78
+ const result: AgentWireMessage[] = [];
79
+
80
+ for (const message of messages) {
81
+ if (message.isTextMessage()) {
82
+ result.push({
83
+ id: message.id,
84
+ role: message.role as any,
85
+ content: message.content,
86
+ });
87
+ } else if (message.isActionExecutionMessage()) {
88
+ const toolCall: ToolCall = {
89
+ id: message.id,
90
+ type: "function",
91
+ function: {
92
+ name: message.name,
93
+ arguments: JSON.stringify(message.arguments),
94
+ },
95
+ };
96
+
97
+ if (message.parentMessageId && result.some((m) => m.id === message.parentMessageId)) {
98
+ const parentMessage: AgentWireAssistantMessage | undefined = result.find(
99
+ (m) => m.id === message.parentMessageId,
100
+ ) as AgentWireAssistantMessage;
101
+ if (parentMessage.toolCalls === undefined) {
102
+ parentMessage.toolCalls = [];
103
+ }
104
+ parentMessage.toolCalls.push(toolCall);
105
+ } else {
106
+ result.push({
107
+ id: message.parentMessageId ?? message.id,
108
+ role: "assistant",
109
+ toolCalls: [toolCall],
110
+ });
111
+ }
112
+ } else if (message.isResultMessage()) {
113
+ result.push({
114
+ id: message.id,
115
+ role: "tool",
116
+ content: message.result,
117
+ toolCallId: message.actionExecutionId,
118
+ });
119
+ }
120
+ }
121
+
122
+ return result;
123
+ }
@@ -67,6 +67,7 @@ import {
67
67
  LLMResponseData,
68
68
  LLMErrorData,
69
69
  } from "../observability";
70
+ import { AbstractAgent } from "@agentwire/client";
70
71
  import { MessageRole } from "../../graphql/types/enums";
71
72
 
72
73
  // +++ MCP Imports +++
@@ -185,6 +186,11 @@ export interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []
185
186
  */
186
187
  langserve?: RemoteChainParameters[];
187
188
 
189
+ /*
190
+ * A map of agent names to AgentWire agents.
191
+ */
192
+ agents?: Record<string, AbstractAgent>;
193
+
188
194
  /*
189
195
  * Delegates agent state processing to the service adapter.
190
196
  *
@@ -254,6 +260,7 @@ export interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []
254
260
 
255
261
  export class CopilotRuntime<const T extends Parameter[] | [] = []> {
256
262
  public actions: ActionsConfiguration<T>;
263
+ public agents: Record<string, AbstractAgent>;
257
264
  public remoteEndpointDefinitions: EndpointDefinition[];
258
265
  private langserve: Promise<Action<any>>[] = [];
259
266
  private onBeforeRequest?: OnBeforeRequestHandler;
@@ -294,7 +301,7 @@ export class CopilotRuntime<const T extends Parameter[] | [] = []> {
294
301
  this.delegateAgentProcessingToServiceAdapter =
295
302
  params?.delegateAgentProcessingToServiceAdapter || false;
296
303
  this.observability = params?.observability_c;
297
-
304
+ this.agents = params?.agents ?? {};
298
305
  // +++ MCP Initialization +++
299
306
  this.mcpEndpointsConfig = params?.mcpEndpoints;
300
307
  this.createMCPClientImpl = params?.createMCPClient;
@@ -1090,6 +1097,7 @@ please use an LLM adapter instead.`,
1090
1097
  messages: inputMessages,
1091
1098
  agentStates,
1092
1099
  frontendUrl: url,
1100
+ agents: this.agents,
1093
1101
  });
1094
1102
 
1095
1103
  const configuredActions =
@@ -9,16 +9,11 @@ import { GraphQLContext } from "../integrations";
9
9
  import { Logger } from "pino";
10
10
  import { Message } from "../../graphql/types/converted";
11
11
  import { AgentStateInput } from "../../graphql/inputs/agent-state.input";
12
- import { Observable, ReplaySubject } from "rxjs";
13
- import {
14
- RuntimeEvent,
15
- RuntimeEventSource,
16
- RuntimeEventSubject,
17
- } from "../../service-adapters/events";
12
+ import { Observable } from "rxjs";
13
+ import { RuntimeEvent, RuntimeEventSubject } from "../../service-adapters/events";
18
14
  import telemetry from "../telemetry-client";
19
15
  import { RemoteLangGraphEventSource } from "../../agents/langgraph/event-source";
20
16
  import { Action } from "@copilotkit/shared";
21
- import { LangGraphEvent } from "../../agents/langgraph/events";
22
17
  import { execute } from "./remote-lg-action";
23
18
  import { CopilotKitError, CopilotKitLowLevelError } from "@copilotkit/shared";
24
19
  import { writeJsonLineResponseToEventStream } from "../streaming";
@@ -1,4 +1,4 @@
1
- import { Action } from "@copilotkit/shared";
1
+ import { Action, CopilotKitErrorCode } from "@copilotkit/shared";
2
2
  import { GraphQLContext } from "../integrations/shared";
3
3
  import { Logger } from "pino";
4
4
  import { Message } from "../../graphql/types/converted";
@@ -17,6 +17,8 @@ import {
17
17
  CopilotKitError,
18
18
  } from "@copilotkit/shared";
19
19
  import { MetaEventInput } from "../../graphql/inputs/meta-event.input";
20
+ import { AbstractAgent } from "@agentwire/client";
21
+ import { constructAgentWireRemoteAction } from "./agentwire-action";
20
22
 
21
23
  export type EndpointDefinition = CopilotKitEndpoint | LangGraphPlatformEndpoint;
22
24
 
@@ -127,12 +129,14 @@ export async function setupRemoteActions({
127
129
  messages,
128
130
  agentStates,
129
131
  frontendUrl,
132
+ agents,
130
133
  }: {
131
134
  remoteEndpointDefinitions: EndpointDefinition[];
132
135
  graphqlContext: GraphQLContext;
133
136
  messages: Message[];
134
137
  agentStates?: AgentStateInput[];
135
138
  frontendUrl?: string;
139
+ agents: Record<string, AbstractAgent>;
136
140
  }): Promise<Action[]> {
137
141
  const logger = graphqlContext.logger.child({ component: "remote-actions.fetchRemoteActions" });
138
142
  logger.debug({ remoteEndpointDefinitions }, "Fetching from remote endpoints");
@@ -181,5 +185,25 @@ export async function setupRemoteActions({
181
185
  }),
182
186
  );
183
187
 
188
+ for (const [key, agent] of Object.entries(agents)) {
189
+ if (agent.agentId !== undefined && agent.agentId !== key) {
190
+ throw new CopilotKitError({
191
+ message: `Agent ${key} has agentId ${agent.agentId} which does not match the key ${key}`,
192
+ code: CopilotKitErrorCode.UNKNOWN,
193
+ });
194
+ } else if (agent.agentId === undefined) {
195
+ agent.agentId = key;
196
+ }
197
+
198
+ result.push(
199
+ constructAgentWireRemoteAction({
200
+ logger,
201
+ messages,
202
+ agentStates,
203
+ agent: agent,
204
+ }),
205
+ );
206
+ }
207
+
184
208
  return result.flat();
185
209
  }
@@ -13,7 +13,7 @@ import telemetry from "../telemetry-client";
13
13
  import { MetaEventInput } from "../../graphql/inputs/meta-event.input";
14
14
  import { MetaEventName } from "../../graphql/types/meta-events.type";
15
15
  import { RunsStreamPayload } from "@langchain/langgraph-sdk/dist/types";
16
- import { parseJson } from "@copilotkit/shared";
16
+ import { parseJson, CopilotKitMisuseError } from "@copilotkit/shared";
17
17
  import { RemoveMessage } from "@langchain/core/messages";
18
18
 
19
19
  type State = Record<string, any>;
@@ -83,7 +83,29 @@ export async function execute(args: ExecutionArgs): Promise<ReadableStream<Uint8
83
83
  try {
84
84
  await streamEvents(controller, args);
85
85
  controller.close();
86
- } catch (err) {}
86
+ } catch (err) {
87
+ // Unwrap the possible cause
88
+ const cause = err?.cause;
89
+
90
+ // Check code directly if it exists
91
+ const errorCode = cause?.code || err?.code;
92
+
93
+ if (errorCode === "ECONNREFUSED") {
94
+ throw new CopilotKitMisuseError({
95
+ message: `
96
+ The LangGraph client could not connect to the graph. Please further check previous logs, which includes further details.
97
+
98
+ See more: https://docs.copilotkit.ai/troubleshooting/common-issues`,
99
+ });
100
+ } else {
101
+ throw new CopilotKitMisuseError({
102
+ message: `
103
+ The LangGraph client threw unhandled error ${err}.
104
+
105
+ See more: https://docs.copilotkit.ai/troubleshooting/common-issues`,
106
+ });
107
+ }
108
+ }
87
109
  },
88
110
  });
89
111
  }