@copilotkit/runtime 1.1.3-feat-runtime-remote-actions.2 → 1.2.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 (75) hide show
  1. package/CHANGELOG.md +17 -13
  2. package/__snapshots__/schema/schema.graphql +0 -41
  3. package/dist/{chunk-BJ2LVHWA.mjs → chunk-736EEICU.mjs} +3 -3
  4. package/dist/{chunk-W7RRKPE2.mjs → chunk-DMO6FA25.mjs} +2 -2
  5. package/dist/{chunk-JYLUCJP7.mjs → chunk-DYF5MUAH.mjs} +2 -2
  6. package/dist/{chunk-TBZGOJJX.mjs → chunk-GEIBJJQ4.mjs} +2 -14
  7. package/dist/chunk-GEIBJJQ4.mjs.map +1 -0
  8. package/dist/chunk-PB24CCIJ.mjs +158 -0
  9. package/dist/chunk-PB24CCIJ.mjs.map +1 -0
  10. package/dist/{chunk-7FSNQXPE.mjs → chunk-Q5ZTE7WH.mjs} +158 -786
  11. package/dist/chunk-Q5ZTE7WH.mjs.map +1 -0
  12. package/dist/{chunk-73NMP3DI.mjs → chunk-URMISMK2.mjs} +2 -2
  13. package/dist/{chunk-WRPRFRSL.mjs → chunk-YMUS43FR.mjs} +2 -2
  14. package/dist/graphql/types/converted/index.d.ts +1 -1
  15. package/dist/graphql/types/converted/index.js +0 -13
  16. package/dist/graphql/types/converted/index.js.map +1 -1
  17. package/dist/graphql/types/converted/index.mjs +1 -3
  18. package/dist/{index-079752b9.d.ts → index-13aa818e.d.ts} +1 -1
  19. package/dist/{index-0476e4f7.d.ts → index-aa091e3c.d.ts} +2 -24
  20. package/dist/index.d.ts +7 -7
  21. package/dist/index.js +245 -982
  22. package/dist/index.js.map +1 -1
  23. package/dist/index.mjs +8 -8
  24. package/dist/{langserve-d6073a3b.d.ts → langserve-a54438c6.d.ts} +11 -24
  25. package/dist/lib/index.d.ts +7 -7
  26. package/dist/lib/index.js +245 -982
  27. package/dist/lib/index.js.map +1 -1
  28. package/dist/lib/index.mjs +8 -8
  29. package/dist/lib/integrations/index.d.ts +6 -6
  30. package/dist/lib/integrations/index.js +182 -437
  31. package/dist/lib/integrations/index.js.map +1 -1
  32. package/dist/lib/integrations/index.mjs +6 -6
  33. package/dist/lib/integrations/nest/index.d.ts +5 -5
  34. package/dist/lib/integrations/nest/index.js +182 -437
  35. package/dist/lib/integrations/nest/index.js.map +1 -1
  36. package/dist/lib/integrations/nest/index.mjs +4 -4
  37. package/dist/lib/integrations/node-express/index.d.ts +5 -5
  38. package/dist/lib/integrations/node-express/index.js +182 -437
  39. package/dist/lib/integrations/node-express/index.js.map +1 -1
  40. package/dist/lib/integrations/node-express/index.mjs +4 -4
  41. package/dist/lib/integrations/node-http/index.d.ts +5 -5
  42. package/dist/lib/integrations/node-http/index.js +182 -437
  43. package/dist/lib/integrations/node-http/index.js.map +1 -1
  44. package/dist/lib/integrations/node-http/index.mjs +3 -3
  45. package/dist/service-adapters/index.d.ts +3 -3
  46. package/dist/service-adapters/index.js.map +1 -1
  47. package/dist/service-adapters/index.mjs +3 -3
  48. package/dist/{copilot-runtime-d427e991.d.ts → shared-c5362338.d.ts} +38 -65
  49. package/package.json +5 -7
  50. package/src/graphql/inputs/generate-copilot-response.input.ts +0 -11
  51. package/src/graphql/inputs/message.input.ts +0 -30
  52. package/src/graphql/resolvers/copilot.resolver.ts +12 -56
  53. package/src/graphql/types/converted/index.ts +0 -15
  54. package/src/graphql/types/copilot-response.type.ts +0 -29
  55. package/src/graphql/types/enums.ts +0 -1
  56. package/src/lib/copilot-runtime.ts +231 -0
  57. package/src/lib/index.ts +1 -1
  58. package/src/lib/integrations/shared.ts +1 -1
  59. package/src/service-adapters/conversion.ts +0 -16
  60. package/src/service-adapters/events.ts +19 -101
  61. package/dist/chunk-5TYP4A3G.mjs +0 -260
  62. package/dist/chunk-5TYP4A3G.mjs.map +0 -1
  63. package/dist/chunk-7FSNQXPE.mjs.map +0 -1
  64. package/dist/chunk-TBZGOJJX.mjs.map +0 -1
  65. package/src/agents/langgraph/event-source.ts +0 -222
  66. package/src/agents/langgraph/events.ts +0 -309
  67. package/src/graphql/inputs/agent-session.input.ts +0 -13
  68. package/src/graphql/inputs/agent-state.input.ts +0 -10
  69. package/src/lib/runtime/copilot-runtime.ts +0 -360
  70. package/src/lib/runtime/remote-actions.ts +0 -241
  71. /package/dist/{chunk-BJ2LVHWA.mjs.map → chunk-736EEICU.mjs.map} +0 -0
  72. /package/dist/{chunk-W7RRKPE2.mjs.map → chunk-DMO6FA25.mjs.map} +0 -0
  73. /package/dist/{chunk-JYLUCJP7.mjs.map → chunk-DYF5MUAH.mjs.map} +0 -0
  74. /package/dist/{chunk-73NMP3DI.mjs.map → chunk-URMISMK2.mjs.map} +0 -0
  75. /package/dist/{chunk-WRPRFRSL.mjs.map → chunk-YMUS43FR.mjs.map} +0 -0
@@ -1,241 +0,0 @@
1
- import { Action } from "@copilotkit/shared";
2
- import { GraphQLContext } from "../integrations/shared";
3
- import { Logger } from "pino";
4
- import telemetry from "../../lib/telemetry-client";
5
- import { Message } from "../../graphql/types/converted";
6
- import { RuntimeEvent, RuntimeEventSubject } from "../../service-adapters/events";
7
- import { RemoteLangGraphEventSource } from "../../agents/langgraph/event-source";
8
- import { Observable } from "rxjs";
9
- import { ActionInput } from "../../graphql/inputs/action.input";
10
- import { AgentStateInput } from "../../graphql/inputs/agent-state.input";
11
-
12
- export type RemoteActionDefinition = {
13
- url: string;
14
- onBeforeRequest?: ({ ctx }: { ctx: GraphQLContext }) => {
15
- headers?: Record<string, string> | undefined;
16
- };
17
- };
18
-
19
- export type LangGraphAgentHandlerParams = {
20
- name: string;
21
- actionInputsWithoutAgents: ActionInput[];
22
- threadId?: string;
23
- nodeName?: string;
24
- };
25
-
26
- export type LangGraphAgentAction = Action<any> & {
27
- langGraphAgentHandler: (params: LangGraphAgentHandlerParams) => Promise<Observable<RuntimeEvent>>;
28
- };
29
-
30
- export function isLangGraphAgentAction(action: Action<any>): action is LangGraphAgentAction {
31
- if (!action) {
32
- return false;
33
- }
34
- return typeof (action as LangGraphAgentAction).langGraphAgentHandler === "function";
35
- }
36
-
37
- function createHeaders(
38
- onBeforeRequest: RemoteActionDefinition["onBeforeRequest"],
39
- graphqlContext: GraphQLContext,
40
- ) {
41
- const headers = {
42
- "Content-Type": "application/json",
43
- };
44
-
45
- if (onBeforeRequest) {
46
- const { headers: additionalHeaders } = onBeforeRequest({ ctx: graphqlContext });
47
- if (additionalHeaders) {
48
- Object.assign(headers, additionalHeaders);
49
- }
50
- }
51
-
52
- return headers;
53
- }
54
-
55
- async function fetchRemoteInfo({
56
- url,
57
- onBeforeRequest,
58
- graphqlContext,
59
- logger,
60
- }: {
61
- url: string;
62
- onBeforeRequest?: RemoteActionDefinition["onBeforeRequest"];
63
- graphqlContext: GraphQLContext;
64
- logger: Logger;
65
- }): Promise<any[]> {
66
- logger.debug({ url }, "Fetching actions from url");
67
- const headers = createHeaders(onBeforeRequest, graphqlContext);
68
-
69
- const response = await fetch(`${url}/info`, {
70
- method: "POST",
71
- headers,
72
- body: JSON.stringify({ properties: graphqlContext.properties }),
73
- });
74
-
75
- if (!response.ok) {
76
- logger.error(
77
- { url, status: response.status, body: await response.text() },
78
- "Failed to fetch actions from url",
79
- );
80
- return [];
81
- }
82
-
83
- const json = await response.json();
84
- logger.debug({ json }, "Fetched actions from url");
85
- return json;
86
- }
87
-
88
- function constructRemoteActions({
89
- json,
90
- url,
91
- onBeforeRequest,
92
- graphqlContext,
93
- logger,
94
- messages,
95
- agentStates,
96
- }: {
97
- json: any[];
98
- url: string;
99
- onBeforeRequest?: RemoteActionDefinition["onBeforeRequest"];
100
- graphqlContext: GraphQLContext;
101
- logger: Logger;
102
- messages: Message[];
103
- agentStates?: AgentStateInput[];
104
- }): Action<any>[] {
105
- const actions = json["actions"].map((action) => ({
106
- name: action.name,
107
- description: action.description,
108
- parameters: action.parameters,
109
- handler: async (args: any) => {
110
- logger.debug({ actionName: action.name, args }, "Executing remote action");
111
-
112
- const headers = createHeaders(onBeforeRequest, graphqlContext);
113
- telemetry.capture("oss.runtime.remote_action_executed", {});
114
-
115
- const response = await fetch(`${url}/actions/execute`, {
116
- method: "POST",
117
- headers,
118
- body: JSON.stringify({
119
- name: action.name,
120
- arguments: args,
121
- properties: graphqlContext.properties,
122
- }),
123
- });
124
-
125
- if (!response.ok) {
126
- logger.error(
127
- { url, status: response.status, body: await response.text() },
128
- "Failed to execute remote action",
129
- );
130
- return "Failed to execute remote action";
131
- }
132
-
133
- const requestResult = await response.json();
134
-
135
- const result = requestResult["result"];
136
- logger.debug({ actionName: action.name, result }, "Executed remote action");
137
- return result;
138
- },
139
- }));
140
-
141
- const agents = json["agents"].map((agent) => ({
142
- name: agent.name,
143
- description: agent.description,
144
- parameters: [],
145
- handler: async (_args: any) => {},
146
-
147
- langGraphAgentHandler: async ({
148
- name,
149
- actionInputsWithoutAgents,
150
- threadId,
151
- nodeName,
152
- }: LangGraphAgentHandlerParams): Promise<Observable<RuntimeEvent>> => {
153
- logger.debug({ actionName: agent.name }, "Executing remote agent");
154
-
155
- const headers = createHeaders(onBeforeRequest, graphqlContext);
156
- telemetry.capture("oss.runtime.remote_action_executed", {});
157
-
158
- let state = {};
159
- if (agentStates) {
160
- const jsonState = agentStates.find((state) => state.agentName === name)?.state;
161
- if (jsonState) {
162
- state = JSON.parse(jsonState);
163
- }
164
- }
165
-
166
- const response = await fetch(`${url}/agents/execute`, {
167
- method: "POST",
168
- headers,
169
- body: JSON.stringify({
170
- name,
171
- threadId,
172
- nodeName,
173
- messages,
174
- state,
175
- properties: graphqlContext.properties,
176
- actions: actionInputsWithoutAgents.map((action) => ({
177
- name: action.name,
178
- description: action.description,
179
- parameters: JSON.parse(action.jsonSchema),
180
- })),
181
- }),
182
- });
183
-
184
- if (!response.ok) {
185
- logger.error(
186
- { url, status: response.status, body: await response.text() },
187
- "Failed to execute remote agent",
188
- );
189
- throw new Error("Failed to execute remote agent");
190
- }
191
-
192
- const eventSource = new RemoteLangGraphEventSource();
193
- eventSource.streamResponse(response);
194
- return eventSource.processLangGraphEvents();
195
- },
196
- }));
197
-
198
- return [...actions, ...agents];
199
- }
200
-
201
- export async function setupRemoteActions({
202
- remoteActionDefinitions,
203
- graphqlContext,
204
- messages,
205
- agentStates,
206
- }: {
207
- remoteActionDefinitions: RemoteActionDefinition[];
208
- graphqlContext: GraphQLContext;
209
- messages: Message[];
210
- agentStates?: AgentStateInput[];
211
- }): Promise<Action[]> {
212
- const logger = graphqlContext.logger.child({ component: "remote-actions.fetchRemoteActions" });
213
- logger.debug({ remoteActionDefinitions }, "Fetching remote actions");
214
-
215
- // Remove duplicates of remoteActionDefinitions.url
216
- const filtered = remoteActionDefinitions.filter(
217
- (value, index, self) => index === self.findIndex((t) => t.url === value.url),
218
- );
219
-
220
- const result = await Promise.all(
221
- filtered.map(async (actionDefinition) => {
222
- const json = await fetchRemoteInfo({
223
- url: actionDefinition.url,
224
- onBeforeRequest: actionDefinition.onBeforeRequest,
225
- graphqlContext,
226
- logger: logger.child({ component: "remote-actions.fetchActionsFromUrl", actionDefinition }),
227
- });
228
- return constructRemoteActions({
229
- json,
230
- messages,
231
- url: actionDefinition.url,
232
- onBeforeRequest: actionDefinition.onBeforeRequest,
233
- graphqlContext,
234
- logger: logger.child({ component: "remote-actions.constructActions", actionDefinition }),
235
- agentStates,
236
- });
237
- }),
238
- );
239
-
240
- return result.flat();
241
- }