@copilotkit/runtime 1.1.2 → 1.1.3-feat-runtime-remote-actions.0

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 (85) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +32 -37
  3. package/__snapshots__/schema/schema.graphql +42 -0
  4. package/dist/chunk-4WFNRUBE.mjs +260 -0
  5. package/dist/chunk-4WFNRUBE.mjs.map +1 -0
  6. package/dist/{chunk-MKV3LEJ6.mjs → chunk-6X5MPWIC.mjs} +795 -159
  7. package/dist/chunk-6X5MPWIC.mjs.map +1 -0
  8. package/dist/{chunk-URMISMK2.mjs → chunk-73NMP3DI.mjs} +2 -2
  9. package/dist/{chunk-CUVWSISN.mjs → chunk-BJ2LVHWA.mjs} +22 -4
  10. package/dist/chunk-BJ2LVHWA.mjs.map +1 -0
  11. package/dist/{chunk-74B76SMO.mjs → chunk-HYNSUFUM.mjs} +2 -2
  12. package/dist/{chunk-6PAC74F2.mjs → chunk-JV3CSVW6.mjs} +2 -2
  13. package/dist/{chunk-NPCP4YZB.mjs → chunk-OYUVLDJF.mjs} +2 -2
  14. package/dist/{chunk-GEIBJJQ4.mjs → chunk-TBZGOJJX.mjs} +14 -2
  15. package/dist/chunk-TBZGOJJX.mjs.map +1 -0
  16. package/dist/{shared-ec6c7db5.d.ts → copilot-runtime-d427e991.d.ts} +68 -37
  17. package/dist/graphql/types/converted/index.d.ts +1 -1
  18. package/dist/graphql/types/converted/index.js +13 -0
  19. package/dist/graphql/types/converted/index.js.map +1 -1
  20. package/dist/graphql/types/converted/index.mjs +3 -1
  21. package/dist/{index-aa091e3c.d.ts → index-0476e4f7.d.ts} +24 -2
  22. package/dist/{groq-adapter-675b30c6.d.ts → index-079752b9.d.ts} +31 -1
  23. package/dist/index.d.ts +9 -9
  24. package/dist/index.js +1012 -246
  25. package/dist/index.js.map +1 -1
  26. package/dist/index.mjs +8 -8
  27. package/dist/{langserve-a54438c6.d.ts → langserve-d6073a3b.d.ts} +24 -11
  28. package/dist/lib/index.d.ts +8 -8
  29. package/dist/lib/index.js +1012 -246
  30. package/dist/lib/index.js.map +1 -1
  31. package/dist/lib/index.mjs +8 -8
  32. package/dist/lib/integrations/index.d.ts +26 -7
  33. package/dist/lib/integrations/index.js +446 -183
  34. package/dist/lib/integrations/index.js.map +1 -1
  35. package/dist/lib/integrations/index.mjs +6 -6
  36. package/dist/lib/integrations/nest/index.d.ts +5 -5
  37. package/dist/lib/integrations/nest/index.js +446 -183
  38. package/dist/lib/integrations/nest/index.js.map +1 -1
  39. package/dist/lib/integrations/nest/index.mjs +4 -4
  40. package/dist/lib/integrations/node-express/index.d.ts +5 -5
  41. package/dist/lib/integrations/node-express/index.js +446 -183
  42. package/dist/lib/integrations/node-express/index.js.map +1 -1
  43. package/dist/lib/integrations/node-express/index.mjs +4 -4
  44. package/dist/lib/integrations/node-http/index.d.ts +5 -5
  45. package/dist/lib/integrations/node-http/index.js +446 -183
  46. package/dist/lib/integrations/node-http/index.js.map +1 -1
  47. package/dist/lib/integrations/node-http/index.mjs +3 -3
  48. package/dist/service-adapters/index.d.ts +3 -3
  49. package/dist/service-adapters/index.js +19 -1
  50. package/dist/service-adapters/index.js.map +1 -1
  51. package/dist/service-adapters/index.mjs +3 -3
  52. package/dist/utils/index.d.ts +49 -1
  53. package/package.json +7 -5
  54. package/src/agents/langgraph/event-source.ts +222 -0
  55. package/src/agents/langgraph/events.ts +309 -0
  56. package/src/graphql/inputs/agent-session.input.ts +13 -0
  57. package/src/graphql/inputs/agent-state.input.ts +10 -0
  58. package/src/graphql/inputs/frontend.input.ts +3 -0
  59. package/src/graphql/inputs/generate-copilot-response.input.ts +11 -0
  60. package/src/graphql/inputs/message.input.ts +30 -0
  61. package/src/graphql/resolvers/copilot.resolver.ts +57 -12
  62. package/src/graphql/types/converted/index.ts +15 -0
  63. package/src/graphql/types/copilot-response.type.ts +29 -0
  64. package/src/graphql/types/enums.ts +1 -0
  65. package/src/lib/index.ts +1 -1
  66. package/src/lib/integrations/shared.ts +1 -1
  67. package/src/lib/runtime/copilot-runtime.ts +360 -0
  68. package/src/lib/runtime/remote-actions.ts +241 -0
  69. package/src/service-adapters/conversion.ts +16 -0
  70. package/src/service-adapters/events.ts +101 -19
  71. package/src/service-adapters/groq/groq-adapter.ts +13 -0
  72. package/src/service-adapters/openai/openai-adapter.ts +13 -0
  73. package/src/service-adapters/openai/openai-assistant-adapter.ts +14 -0
  74. package/dist/chunk-CUVWSISN.mjs.map +0 -1
  75. package/dist/chunk-GEIBJJQ4.mjs.map +0 -1
  76. package/dist/chunk-MKV3LEJ6.mjs.map +0 -1
  77. package/dist/chunk-RDEOIOQR.mjs +0 -155
  78. package/dist/chunk-RDEOIOQR.mjs.map +0 -1
  79. package/dist/failed-response-status-reasons-0ab19e06.d.ts +0 -49
  80. package/dist/pages-router-df82c666.d.ts +0 -21
  81. package/src/lib/copilot-runtime.ts +0 -225
  82. /package/dist/{chunk-URMISMK2.mjs.map → chunk-73NMP3DI.mjs.map} +0 -0
  83. /package/dist/{chunk-74B76SMO.mjs.map → chunk-HYNSUFUM.mjs.map} +0 -0
  84. /package/dist/{chunk-6PAC74F2.mjs.map → chunk-JV3CSVW6.mjs.map} +0 -0
  85. /package/dist/{chunk-NPCP4YZB.mjs.map → chunk-OYUVLDJF.mjs.map} +0 -0
@@ -1,225 +0,0 @@
1
- /**
2
- * <Callout type="info">
3
- * This is the reference for the `CopilotRuntime` class. For more information and example code snippets, please see [Concept: Copilot Runtime](/concepts/copilot-runtime).
4
- * </Callout>
5
- *
6
- * ## Usage
7
- *
8
- * ```tsx
9
- * import { CopilotRuntime } from "@copilotkit/runtime";
10
- *
11
- * const copilotKit = new CopilotRuntime();
12
- * ```
13
- */
14
-
15
- import { Action, actionParametersToJsonSchema, Parameter } from "@copilotkit/shared";
16
- import { RemoteChain, RemoteChainParameters, CopilotServiceAdapter } from "../service-adapters";
17
- import { MessageInput } from "../graphql/inputs/message.input";
18
- import { ActionInput } from "../graphql/inputs/action.input";
19
- import { RuntimeEventSource } from "../service-adapters/events";
20
- import { convertGqlInputToMessages } from "../service-adapters/conversion";
21
- import { Message } from "../graphql/types/converted";
22
- import { ForwardedParametersInput } from "../graphql/inputs/forwarded-parameters.input";
23
-
24
- interface CopilotRuntimeRequest {
25
- serviceAdapter: CopilotServiceAdapter;
26
- messages: MessageInput[];
27
- actions: ActionInput[];
28
- outputMessagesPromise: Promise<Message[]>;
29
- properties: any;
30
- threadId?: string;
31
- runId?: string;
32
- publicApiKey?: string;
33
- forwardedParameters?: ForwardedParametersInput;
34
- }
35
-
36
- interface CopilotRuntimeResponse {
37
- threadId: string;
38
- runId?: string;
39
- eventSource: RuntimeEventSource;
40
- actions: Action<any>[];
41
- }
42
-
43
- type ActionsConfiguration<T extends Parameter[] | [] = []> =
44
- | Action<T>[]
45
- | ((ctx: { properties: any }) => Action<T>[]);
46
-
47
- interface OnBeforeRequestOptions {
48
- threadId?: string;
49
- runId?: string;
50
- inputMessages: Message[];
51
- properties: any;
52
- }
53
-
54
- type OnBeforeRequestHandler = (options: OnBeforeRequestOptions) => void | Promise<void>;
55
-
56
- interface OnAfterRequestOptions {
57
- threadId: string;
58
- runId?: string;
59
- inputMessages: Message[];
60
- outputMessages: Message[];
61
- properties: any;
62
- }
63
-
64
- type OnAfterRequestHandler = (options: OnAfterRequestOptions) => void | Promise<void>;
65
-
66
- interface Middleware {
67
- /**
68
- * A function that is called before the request is processed.
69
- */
70
- onBeforeRequest?: OnBeforeRequestHandler;
71
-
72
- /**
73
- * A function that is called after the request is processed.
74
- */
75
- onAfterRequest?: OnAfterRequestHandler;
76
- }
77
-
78
- export interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []> {
79
- /**
80
- * Middleware to be used by the runtime.
81
- *
82
- * ```ts
83
- * onBeforeRequest: (options: {
84
- * threadId?: string;
85
- * runId?: string;
86
- * inputMessages: Message[];
87
- * properties: any;
88
- * }) => void | Promise<void>;
89
- * ```
90
- *
91
- * ```ts
92
- * onAfterRequest: (options: {
93
- * threadId?: string;
94
- * runId?: string;
95
- * inputMessages: Message[];
96
- * outputMessages: Message[];
97
- * properties: any;
98
- * }) => void | Promise<void>;
99
- * ```
100
- */
101
- middleware?: Middleware;
102
-
103
- /*
104
- * A list of server side actions that can be executed.
105
- */
106
- actions?: ActionsConfiguration<T>;
107
-
108
- /*
109
- * An array of LangServer URLs.
110
- */
111
- langserve?: RemoteChainParameters[];
112
- }
113
-
114
- export class CopilotRuntime<const T extends Parameter[] | [] = []> {
115
- public actions: ActionsConfiguration<T>;
116
- private langserve: Promise<Action<any>>[] = [];
117
- private onBeforeRequest?: OnBeforeRequestHandler;
118
- private onAfterRequest?: OnAfterRequestHandler;
119
-
120
- constructor(params?: CopilotRuntimeConstructorParams<T>) {
121
- this.actions = params?.actions || [];
122
-
123
- for (const chain of params?.langserve || []) {
124
- const remoteChain = new RemoteChain(chain);
125
- this.langserve.push(remoteChain.toAction());
126
- }
127
-
128
- this.onBeforeRequest = params?.middleware?.onBeforeRequest;
129
- this.onAfterRequest = params?.middleware?.onAfterRequest;
130
- }
131
-
132
- async process(request: CopilotRuntimeRequest): Promise<CopilotRuntimeResponse> {
133
- const {
134
- serviceAdapter,
135
- messages,
136
- actions: clientSideActionsInput,
137
- threadId,
138
- runId,
139
- properties,
140
- outputMessagesPromise,
141
- forwardedParameters,
142
- } = request;
143
- const langserveFunctions: Action<any>[] = [];
144
-
145
- for (const chainPromise of this.langserve) {
146
- try {
147
- const chain = await chainPromise;
148
- langserveFunctions.push(chain);
149
- } catch (error) {
150
- console.error("Error loading langserve chain:", error);
151
- }
152
- }
153
-
154
- const configuredActions =
155
- typeof this.actions === "function" ? this.actions({ properties }) : this.actions;
156
-
157
- const actions = [...configuredActions, ...langserveFunctions];
158
-
159
- const serverSideActionsInput: ActionInput[] = actions.map((action) => ({
160
- name: action.name,
161
- description: action.description,
162
- jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters)),
163
- }));
164
-
165
- const actionInputs = flattenToolCallsNoDuplicates([
166
- ...serverSideActionsInput,
167
- ...clientSideActionsInput,
168
- ]);
169
- const inputMessages = convertGqlInputToMessages(messages);
170
-
171
- await this.onBeforeRequest?.({
172
- threadId,
173
- runId,
174
- inputMessages,
175
- properties,
176
- });
177
-
178
- try {
179
- const eventSource = new RuntimeEventSource();
180
-
181
- const result = await serviceAdapter.process({
182
- messages: inputMessages,
183
- actions: actionInputs,
184
- threadId,
185
- runId,
186
- eventSource,
187
- forwardedParameters,
188
- });
189
-
190
- outputMessagesPromise
191
- .then((outputMessages) => {
192
- this.onAfterRequest?.({
193
- threadId: result.threadId,
194
- runId: result.runId,
195
- inputMessages,
196
- outputMessages,
197
- properties,
198
- });
199
- })
200
- .catch((_error) => {});
201
-
202
- return {
203
- threadId: result.threadId,
204
- runId: result.runId,
205
- eventSource,
206
- actions: actions,
207
- };
208
- } catch (error) {
209
- console.error("Error getting response:", error);
210
- throw error;
211
- }
212
- }
213
- }
214
-
215
- export function flattenToolCallsNoDuplicates(toolsByPriority: ActionInput[]): ActionInput[] {
216
- let allTools: ActionInput[] = [];
217
- const allToolNames: string[] = [];
218
- for (const tool of toolsByPriority) {
219
- if (!allToolNames.includes(tool.name)) {
220
- allTools.push(tool);
221
- allToolNames.push(tool.name);
222
- }
223
- }
224
- return allTools;
225
- }