@copilotkit/runtime 0.0.0-feat-dynamic-copilotcloud-qa-20250117190454
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.
- package/.eslintrc.js +7 -0
- package/CHANGELOG.md +913 -0
- package/README.md +46 -0
- package/__snapshots__/schema/schema.graphql +273 -0
- package/dist/chunk-44O2JGUY.mjs +12 -0
- package/dist/chunk-44O2JGUY.mjs.map +1 -0
- package/dist/chunk-BETLEV37.mjs +25 -0
- package/dist/chunk-BETLEV37.mjs.map +1 -0
- package/dist/chunk-CLGKEUOA.mjs +1408 -0
- package/dist/chunk-CLGKEUOA.mjs.map +1 -0
- package/dist/chunk-D2WLFQS6.mjs +43 -0
- package/dist/chunk-D2WLFQS6.mjs.map +1 -0
- package/dist/chunk-DFOKBSIS.mjs +1 -0
- package/dist/chunk-DFOKBSIS.mjs.map +1 -0
- package/dist/chunk-FA5DJ2TZ.mjs +3437 -0
- package/dist/chunk-FA5DJ2TZ.mjs.map +1 -0
- package/dist/chunk-HNUNXFTW.mjs +129 -0
- package/dist/chunk-HNUNXFTW.mjs.map +1 -0
- package/dist/chunk-SFLMY3ES.mjs +80 -0
- package/dist/chunk-SFLMY3ES.mjs.map +1 -0
- package/dist/chunk-U3V2BCGI.mjs +152 -0
- package/dist/chunk-U3V2BCGI.mjs.map +1 -0
- package/dist/chunk-ZCU6UPCY.mjs +25 -0
- package/dist/chunk-ZCU6UPCY.mjs.map +1 -0
- package/dist/copilot-runtime-1a224a0f.d.ts +196 -0
- package/dist/graphql/types/base/index.d.ts +6 -0
- package/dist/graphql/types/base/index.js +63 -0
- package/dist/graphql/types/base/index.js.map +1 -0
- package/dist/graphql/types/base/index.mjs +8 -0
- package/dist/graphql/types/base/index.mjs.map +1 -0
- package/dist/graphql/types/converted/index.d.ts +2 -0
- package/dist/graphql/types/converted/index.js +187 -0
- package/dist/graphql/types/converted/index.js.map +1 -0
- package/dist/graphql/types/converted/index.mjs +17 -0
- package/dist/graphql/types/converted/index.mjs.map +1 -0
- package/dist/groq-adapter-c35c5374.d.ts +281 -0
- package/dist/index-24315d90.d.ts +103 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +5258 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +76 -0
- package/dist/index.mjs.map +1 -0
- package/dist/langserve-a16ef8f4.d.ts +180 -0
- package/dist/lib/cloud/index.d.ts +6 -0
- package/dist/lib/cloud/index.js +18 -0
- package/dist/lib/cloud/index.js.map +1 -0
- package/dist/lib/cloud/index.mjs +1 -0
- package/dist/lib/cloud/index.mjs.map +1 -0
- package/dist/lib/index.d.ts +20 -0
- package/dist/lib/index.js +4906 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/index.mjs +58 -0
- package/dist/lib/index.mjs.map +1 -0
- package/dist/lib/integrations/index.d.ts +33 -0
- package/dist/lib/integrations/index.js +2229 -0
- package/dist/lib/integrations/index.js.map +1 -0
- package/dist/lib/integrations/index.mjs +34 -0
- package/dist/lib/integrations/index.mjs.map +1 -0
- package/dist/lib/integrations/nest/index.d.ts +14 -0
- package/dist/lib/integrations/nest/index.js +2138 -0
- package/dist/lib/integrations/nest/index.js.map +1 -0
- package/dist/lib/integrations/nest/index.mjs +13 -0
- package/dist/lib/integrations/nest/index.mjs.map +1 -0
- package/dist/lib/integrations/node-express/index.d.ts +14 -0
- package/dist/lib/integrations/node-express/index.js +2138 -0
- package/dist/lib/integrations/node-express/index.js.map +1 -0
- package/dist/lib/integrations/node-express/index.mjs +13 -0
- package/dist/lib/integrations/node-express/index.mjs.map +1 -0
- package/dist/lib/integrations/node-http/index.d.ts +14 -0
- package/dist/lib/integrations/node-http/index.js +2124 -0
- package/dist/lib/integrations/node-http/index.js.map +1 -0
- package/dist/lib/integrations/node-http/index.mjs +12 -0
- package/dist/lib/integrations/node-http/index.mjs.map +1 -0
- package/dist/service-adapters/index.d.ts +84 -0
- package/dist/service-adapters/index.js +1448 -0
- package/dist/service-adapters/index.js.map +1 -0
- package/dist/service-adapters/index.mjs +26 -0
- package/dist/service-adapters/index.mjs.map +1 -0
- package/dist/utils/index.d.ts +49 -0
- package/dist/utils/index.js +174 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/index.mjs +12 -0
- package/dist/utils/index.mjs.map +1 -0
- package/jest.config.js +5 -0
- package/package.json +85 -0
- package/scripts/generate-gql-schema.ts +13 -0
- package/src/agents/langgraph/event-source.ts +287 -0
- package/src/agents/langgraph/events.ts +338 -0
- package/src/graphql/inputs/action.input.ts +16 -0
- package/src/graphql/inputs/agent-session.input.ts +13 -0
- package/src/graphql/inputs/agent-state.input.ts +10 -0
- package/src/graphql/inputs/cloud-guardrails.input.ts +16 -0
- package/src/graphql/inputs/cloud.input.ts +8 -0
- package/src/graphql/inputs/context-property.input.ts +10 -0
- package/src/graphql/inputs/custom-property.input.ts +15 -0
- package/src/graphql/inputs/forwarded-parameters.input.ts +22 -0
- package/src/graphql/inputs/frontend.input.ts +14 -0
- package/src/graphql/inputs/generate-copilot-response.input.ts +47 -0
- package/src/graphql/inputs/message.input.ts +92 -0
- package/src/graphql/resolvers/copilot.resolver.ts +556 -0
- package/src/graphql/types/agents-response.type.ts +22 -0
- package/src/graphql/types/base/index.ts +10 -0
- package/src/graphql/types/converted/index.ts +136 -0
- package/src/graphql/types/copilot-response.type.ts +113 -0
- package/src/graphql/types/enums.ts +37 -0
- package/src/graphql/types/guardrails-result.type.ts +20 -0
- package/src/graphql/types/message-status.type.ts +40 -0
- package/src/graphql/types/response-status.type.ts +66 -0
- package/src/index.ts +4 -0
- package/src/lib/cloud/index.ts +4 -0
- package/src/lib/index.ts +8 -0
- package/src/lib/integrations/index.ts +6 -0
- package/src/lib/integrations/nest/index.ts +17 -0
- package/src/lib/integrations/nextjs/app-router.ts +40 -0
- package/src/lib/integrations/nextjs/pages-router.ts +49 -0
- package/src/lib/integrations/node-express/index.ts +17 -0
- package/src/lib/integrations/node-http/index.ts +34 -0
- package/src/lib/integrations/shared.ts +109 -0
- package/src/lib/logger.ts +28 -0
- package/src/lib/runtime/copilot-runtime.ts +466 -0
- package/src/lib/runtime/remote-action-constructors.ts +304 -0
- package/src/lib/runtime/remote-actions.ts +174 -0
- package/src/lib/runtime/remote-lg-action.ts +657 -0
- package/src/lib/telemetry-client.ts +52 -0
- package/src/service-adapters/anthropic/anthropic-adapter.ts +205 -0
- package/src/service-adapters/anthropic/utils.ts +144 -0
- package/src/service-adapters/conversion.ts +64 -0
- package/src/service-adapters/events.ts +419 -0
- package/src/service-adapters/experimental/empty/empty-adapter.ts +33 -0
- package/src/service-adapters/experimental/ollama/ollama-adapter.ts +79 -0
- package/src/service-adapters/google/google-genai-adapter.ts +39 -0
- package/src/service-adapters/groq/groq-adapter.ts +173 -0
- package/src/service-adapters/index.ts +16 -0
- package/src/service-adapters/langchain/langchain-adapter.ts +99 -0
- package/src/service-adapters/langchain/langserve.ts +87 -0
- package/src/service-adapters/langchain/types.ts +14 -0
- package/src/service-adapters/langchain/utils.ts +306 -0
- package/src/service-adapters/openai/openai-adapter.ts +210 -0
- package/src/service-adapters/openai/openai-assistant-adapter.ts +304 -0
- package/src/service-adapters/openai/utils.ts +161 -0
- package/src/service-adapters/service-adapter.ts +30 -0
- package/src/service-adapters/unify/unify-adapter.ts +145 -0
- package/src/utils/failed-response-status-reasons.ts +48 -0
- package/src/utils/index.ts +1 -0
- package/tsconfig.json +11 -0
- package/tsup.config.ts +16 -0
- package/typedoc.json +4 -0
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
import { Action, randomId } from "@copilotkit/shared";
|
|
2
|
+
import {
|
|
3
|
+
of,
|
|
4
|
+
concat,
|
|
5
|
+
scan,
|
|
6
|
+
concatMap,
|
|
7
|
+
ReplaySubject,
|
|
8
|
+
Subject,
|
|
9
|
+
firstValueFrom,
|
|
10
|
+
from,
|
|
11
|
+
catchError,
|
|
12
|
+
EMPTY,
|
|
13
|
+
} from "rxjs";
|
|
14
|
+
import { streamLangChainResponse } from "./langchain/utils";
|
|
15
|
+
import { GuardrailsResult } from "../graphql/types/guardrails-result.type";
|
|
16
|
+
import telemetry from "../lib/telemetry-client";
|
|
17
|
+
import { isLangGraphAgentAction } from "../lib/runtime/remote-actions";
|
|
18
|
+
import { ActionInput } from "../graphql/inputs/action.input";
|
|
19
|
+
import { ActionExecutionMessage, ResultMessage } from "../graphql/types/converted";
|
|
20
|
+
import { plainToInstance } from "class-transformer";
|
|
21
|
+
|
|
22
|
+
export enum RuntimeEventTypes {
|
|
23
|
+
TextMessageStart = "TextMessageStart",
|
|
24
|
+
TextMessageContent = "TextMessageContent",
|
|
25
|
+
TextMessageEnd = "TextMessageEnd",
|
|
26
|
+
ActionExecutionStart = "ActionExecutionStart",
|
|
27
|
+
ActionExecutionArgs = "ActionExecutionArgs",
|
|
28
|
+
ActionExecutionEnd = "ActionExecutionEnd",
|
|
29
|
+
ActionExecutionResult = "ActionExecutionResult",
|
|
30
|
+
AgentStateMessage = "AgentStateMessage",
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type RuntimeEvent =
|
|
34
|
+
| { type: RuntimeEventTypes.TextMessageStart; messageId: string; parentMessageId?: string }
|
|
35
|
+
| {
|
|
36
|
+
type: RuntimeEventTypes.TextMessageContent;
|
|
37
|
+
messageId: string;
|
|
38
|
+
content: string;
|
|
39
|
+
}
|
|
40
|
+
| { type: RuntimeEventTypes.TextMessageEnd; messageId: string }
|
|
41
|
+
| {
|
|
42
|
+
type: RuntimeEventTypes.ActionExecutionStart;
|
|
43
|
+
actionExecutionId: string;
|
|
44
|
+
actionName: string;
|
|
45
|
+
parentMessageId?: string;
|
|
46
|
+
}
|
|
47
|
+
| { type: RuntimeEventTypes.ActionExecutionArgs; actionExecutionId: string; args: string }
|
|
48
|
+
| { type: RuntimeEventTypes.ActionExecutionEnd; actionExecutionId: string }
|
|
49
|
+
| {
|
|
50
|
+
type: RuntimeEventTypes.ActionExecutionResult;
|
|
51
|
+
actionName: string;
|
|
52
|
+
actionExecutionId: string;
|
|
53
|
+
result: string;
|
|
54
|
+
}
|
|
55
|
+
| {
|
|
56
|
+
type: RuntimeEventTypes.AgentStateMessage;
|
|
57
|
+
threadId: string;
|
|
58
|
+
agentName: string;
|
|
59
|
+
nodeName: string;
|
|
60
|
+
runId: string;
|
|
61
|
+
active: boolean;
|
|
62
|
+
role: string;
|
|
63
|
+
state: string;
|
|
64
|
+
running: boolean;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
interface RuntimeEventWithState {
|
|
68
|
+
event: RuntimeEvent | null;
|
|
69
|
+
callActionServerSide: boolean;
|
|
70
|
+
action: Action<any> | null;
|
|
71
|
+
actionExecutionId: string | null;
|
|
72
|
+
args: string;
|
|
73
|
+
actionExecutionParentMessageId: string | null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
type EventSourceCallback = (eventStream$: RuntimeEventSubject) => Promise<void>;
|
|
77
|
+
|
|
78
|
+
export class RuntimeEventSubject extends ReplaySubject<RuntimeEvent> {
|
|
79
|
+
constructor() {
|
|
80
|
+
super();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
sendTextMessageStart({
|
|
84
|
+
messageId,
|
|
85
|
+
parentMessageId,
|
|
86
|
+
}: {
|
|
87
|
+
messageId: string;
|
|
88
|
+
parentMessageId?: string;
|
|
89
|
+
}) {
|
|
90
|
+
this.next({ type: RuntimeEventTypes.TextMessageStart, messageId, parentMessageId });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
sendTextMessageContent({ messageId, content }: { messageId: string; content: string }) {
|
|
94
|
+
this.next({ type: RuntimeEventTypes.TextMessageContent, content, messageId });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
sendTextMessageEnd({ messageId }: { messageId: string }) {
|
|
98
|
+
this.next({ type: RuntimeEventTypes.TextMessageEnd, messageId });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
sendTextMessage(messageId: string, content: string) {
|
|
102
|
+
this.sendTextMessageStart({ messageId });
|
|
103
|
+
this.sendTextMessageContent({ messageId, content });
|
|
104
|
+
this.sendTextMessageEnd({ messageId });
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
sendActionExecutionStart({
|
|
108
|
+
actionExecutionId,
|
|
109
|
+
actionName,
|
|
110
|
+
parentMessageId,
|
|
111
|
+
}: {
|
|
112
|
+
actionExecutionId: string;
|
|
113
|
+
actionName: string;
|
|
114
|
+
parentMessageId?: string;
|
|
115
|
+
}) {
|
|
116
|
+
this.next({
|
|
117
|
+
type: RuntimeEventTypes.ActionExecutionStart,
|
|
118
|
+
actionExecutionId,
|
|
119
|
+
actionName,
|
|
120
|
+
parentMessageId,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
sendActionExecutionArgs({
|
|
125
|
+
actionExecutionId,
|
|
126
|
+
args,
|
|
127
|
+
}: {
|
|
128
|
+
actionExecutionId: string;
|
|
129
|
+
args: string;
|
|
130
|
+
}) {
|
|
131
|
+
this.next({ type: RuntimeEventTypes.ActionExecutionArgs, args, actionExecutionId });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
sendActionExecutionEnd({ actionExecutionId }: { actionExecutionId: string }) {
|
|
135
|
+
this.next({ type: RuntimeEventTypes.ActionExecutionEnd, actionExecutionId });
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
sendActionExecution({
|
|
139
|
+
actionExecutionId,
|
|
140
|
+
actionName,
|
|
141
|
+
args,
|
|
142
|
+
parentMessageId,
|
|
143
|
+
}: {
|
|
144
|
+
actionExecutionId: string;
|
|
145
|
+
actionName: string;
|
|
146
|
+
args: string;
|
|
147
|
+
parentMessageId?: string;
|
|
148
|
+
}) {
|
|
149
|
+
this.sendActionExecutionStart({ actionExecutionId, actionName, parentMessageId });
|
|
150
|
+
this.sendActionExecutionArgs({ actionExecutionId, args });
|
|
151
|
+
this.sendActionExecutionEnd({ actionExecutionId });
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
sendActionExecutionResult({
|
|
155
|
+
actionExecutionId,
|
|
156
|
+
actionName,
|
|
157
|
+
result,
|
|
158
|
+
error,
|
|
159
|
+
}: {
|
|
160
|
+
actionExecutionId: string;
|
|
161
|
+
actionName: string;
|
|
162
|
+
result?: string;
|
|
163
|
+
error?: { code: string; message: string };
|
|
164
|
+
}) {
|
|
165
|
+
this.next({
|
|
166
|
+
type: RuntimeEventTypes.ActionExecutionResult,
|
|
167
|
+
actionName,
|
|
168
|
+
actionExecutionId,
|
|
169
|
+
result: ResultMessage.encodeResult(result, error),
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
sendAgentStateMessage({
|
|
174
|
+
threadId,
|
|
175
|
+
agentName,
|
|
176
|
+
nodeName,
|
|
177
|
+
runId,
|
|
178
|
+
active,
|
|
179
|
+
role,
|
|
180
|
+
state,
|
|
181
|
+
running,
|
|
182
|
+
}: {
|
|
183
|
+
threadId: string;
|
|
184
|
+
agentName: string;
|
|
185
|
+
nodeName: string;
|
|
186
|
+
runId: string;
|
|
187
|
+
active: boolean;
|
|
188
|
+
role: string;
|
|
189
|
+
state: string;
|
|
190
|
+
running: boolean;
|
|
191
|
+
}) {
|
|
192
|
+
this.next({
|
|
193
|
+
type: RuntimeEventTypes.AgentStateMessage,
|
|
194
|
+
threadId,
|
|
195
|
+
agentName,
|
|
196
|
+
nodeName,
|
|
197
|
+
runId,
|
|
198
|
+
active,
|
|
199
|
+
role,
|
|
200
|
+
state,
|
|
201
|
+
running,
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export class RuntimeEventSource {
|
|
207
|
+
private eventStream$ = new RuntimeEventSubject();
|
|
208
|
+
private callback!: EventSourceCallback;
|
|
209
|
+
|
|
210
|
+
async stream(callback: EventSourceCallback): Promise<void> {
|
|
211
|
+
this.callback = callback;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
sendErrorMessageToChat(message = "An error occurred. Please try again.") {
|
|
215
|
+
const errorMessage = `❌ ${message}`;
|
|
216
|
+
if (!this.callback) {
|
|
217
|
+
this.stream(async (eventStream$) => {
|
|
218
|
+
eventStream$.sendTextMessage(randomId(), errorMessage);
|
|
219
|
+
});
|
|
220
|
+
} else {
|
|
221
|
+
this.eventStream$.sendTextMessage(randomId(), errorMessage);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
processRuntimeEvents({
|
|
226
|
+
serverSideActions,
|
|
227
|
+
guardrailsResult$,
|
|
228
|
+
actionInputsWithoutAgents,
|
|
229
|
+
}: {
|
|
230
|
+
serverSideActions: Action<any>[];
|
|
231
|
+
guardrailsResult$?: Subject<GuardrailsResult>;
|
|
232
|
+
actionInputsWithoutAgents: ActionInput[];
|
|
233
|
+
}) {
|
|
234
|
+
this.callback(this.eventStream$).catch((error) => {
|
|
235
|
+
console.error("Error in event source callback", error);
|
|
236
|
+
this.sendErrorMessageToChat();
|
|
237
|
+
this.eventStream$.complete();
|
|
238
|
+
});
|
|
239
|
+
return this.eventStream$.pipe(
|
|
240
|
+
// track state
|
|
241
|
+
scan(
|
|
242
|
+
(acc, event) => {
|
|
243
|
+
// It seems like this is needed so that rxjs recognizes the object has changed
|
|
244
|
+
// This fixes an issue where action were executed multiple times
|
|
245
|
+
// Not investigating further for now (Markus)
|
|
246
|
+
acc = { ...acc };
|
|
247
|
+
|
|
248
|
+
if (event.type === RuntimeEventTypes.ActionExecutionStart) {
|
|
249
|
+
acc.callActionServerSide =
|
|
250
|
+
serverSideActions.find((action) => action.name === event.actionName) !== undefined;
|
|
251
|
+
acc.args = "";
|
|
252
|
+
acc.actionExecutionId = event.actionExecutionId;
|
|
253
|
+
if (acc.callActionServerSide) {
|
|
254
|
+
acc.action = serverSideActions.find((action) => action.name === event.actionName);
|
|
255
|
+
}
|
|
256
|
+
acc.actionExecutionParentMessageId = event.parentMessageId;
|
|
257
|
+
} else if (event.type === RuntimeEventTypes.ActionExecutionArgs) {
|
|
258
|
+
acc.args += event.args;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
acc.event = event;
|
|
262
|
+
|
|
263
|
+
return acc;
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
event: null,
|
|
267
|
+
callActionServerSide: false,
|
|
268
|
+
args: "",
|
|
269
|
+
actionExecutionId: null,
|
|
270
|
+
action: null,
|
|
271
|
+
actionExecutionParentMessageId: null,
|
|
272
|
+
} as RuntimeEventWithState,
|
|
273
|
+
),
|
|
274
|
+
concatMap((eventWithState) => {
|
|
275
|
+
if (
|
|
276
|
+
eventWithState.event!.type === RuntimeEventTypes.ActionExecutionEnd &&
|
|
277
|
+
eventWithState.callActionServerSide
|
|
278
|
+
) {
|
|
279
|
+
const toolCallEventStream$ = new RuntimeEventSubject();
|
|
280
|
+
executeAction(
|
|
281
|
+
toolCallEventStream$,
|
|
282
|
+
guardrailsResult$ ? guardrailsResult$ : null,
|
|
283
|
+
eventWithState.action!,
|
|
284
|
+
eventWithState.args,
|
|
285
|
+
eventWithState.actionExecutionParentMessageId,
|
|
286
|
+
eventWithState.actionExecutionId,
|
|
287
|
+
actionInputsWithoutAgents,
|
|
288
|
+
).catch((error) => {
|
|
289
|
+
console.error(error);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
telemetry.capture("oss.runtime.server_action_executed", {});
|
|
293
|
+
return concat(of(eventWithState.event!), toolCallEventStream$).pipe(
|
|
294
|
+
catchError((error) => {
|
|
295
|
+
console.error("Error in tool call stream", error);
|
|
296
|
+
this.sendErrorMessageToChat();
|
|
297
|
+
return EMPTY;
|
|
298
|
+
}),
|
|
299
|
+
);
|
|
300
|
+
} else {
|
|
301
|
+
return of(eventWithState.event!);
|
|
302
|
+
}
|
|
303
|
+
}),
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
async function executeAction(
|
|
309
|
+
eventStream$: RuntimeEventSubject,
|
|
310
|
+
guardrailsResult$: Subject<GuardrailsResult> | null,
|
|
311
|
+
action: Action<any>,
|
|
312
|
+
actionArguments: string,
|
|
313
|
+
actionExecutionParentMessageId: string | null,
|
|
314
|
+
actionExecutionId: string,
|
|
315
|
+
actionInputsWithoutAgents: ActionInput[],
|
|
316
|
+
) {
|
|
317
|
+
if (guardrailsResult$) {
|
|
318
|
+
const { status } = await firstValueFrom(guardrailsResult$);
|
|
319
|
+
|
|
320
|
+
if (status === "denied") {
|
|
321
|
+
eventStream$.complete();
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Prepare arguments for function calling
|
|
327
|
+
let args: Record<string, any>[] = [];
|
|
328
|
+
if (actionArguments) {
|
|
329
|
+
try {
|
|
330
|
+
args = JSON.parse(actionArguments);
|
|
331
|
+
} catch (e) {
|
|
332
|
+
console.error("Action argument unparsable", { actionArguments });
|
|
333
|
+
eventStream$.sendActionExecutionResult({
|
|
334
|
+
actionExecutionId,
|
|
335
|
+
actionName: action.name,
|
|
336
|
+
error: {
|
|
337
|
+
code: "INVALID_ARGUMENTS",
|
|
338
|
+
message: "Failed to parse action arguments",
|
|
339
|
+
},
|
|
340
|
+
});
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// handle LangGraph agents
|
|
346
|
+
if (isLangGraphAgentAction(action)) {
|
|
347
|
+
const result = `${action.name} agent started`;
|
|
348
|
+
|
|
349
|
+
const agentExecution = plainToInstance(ActionExecutionMessage, {
|
|
350
|
+
id: actionExecutionId,
|
|
351
|
+
createdAt: new Date(),
|
|
352
|
+
name: action.name,
|
|
353
|
+
arguments: JSON.parse(actionArguments),
|
|
354
|
+
parentMessageId: actionExecutionParentMessageId ?? actionExecutionId,
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
const agentExecutionResult = plainToInstance(ResultMessage, {
|
|
358
|
+
id: "result-" + actionExecutionId,
|
|
359
|
+
createdAt: new Date(),
|
|
360
|
+
actionExecutionId,
|
|
361
|
+
actionName: action.name,
|
|
362
|
+
result,
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
eventStream$.sendActionExecutionResult({
|
|
366
|
+
actionExecutionId,
|
|
367
|
+
actionName: action.name,
|
|
368
|
+
result,
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
const stream = await action.langGraphAgentHandler({
|
|
372
|
+
name: action.name,
|
|
373
|
+
actionInputsWithoutAgents,
|
|
374
|
+
additionalMessages: [agentExecution, agentExecutionResult],
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
// forward to eventStream$
|
|
378
|
+
from(stream).subscribe({
|
|
379
|
+
next: (event) => eventStream$.next(event),
|
|
380
|
+
error: (err) => {
|
|
381
|
+
console.error("Error in stream", err);
|
|
382
|
+
eventStream$.sendActionExecutionResult({
|
|
383
|
+
actionExecutionId,
|
|
384
|
+
actionName: action.name,
|
|
385
|
+
error: {
|
|
386
|
+
code: "STREAM_ERROR",
|
|
387
|
+
message: err.message,
|
|
388
|
+
},
|
|
389
|
+
});
|
|
390
|
+
eventStream$.complete();
|
|
391
|
+
},
|
|
392
|
+
complete: () => eventStream$.complete(),
|
|
393
|
+
});
|
|
394
|
+
} else {
|
|
395
|
+
// call the function
|
|
396
|
+
try {
|
|
397
|
+
const result = await action.handler?.(args);
|
|
398
|
+
await streamLangChainResponse({
|
|
399
|
+
result,
|
|
400
|
+
eventStream$,
|
|
401
|
+
actionExecution: {
|
|
402
|
+
name: action.name,
|
|
403
|
+
id: actionExecutionId,
|
|
404
|
+
},
|
|
405
|
+
});
|
|
406
|
+
} catch (e) {
|
|
407
|
+
console.error("Error in action handler", e);
|
|
408
|
+
eventStream$.sendActionExecutionResult({
|
|
409
|
+
actionExecutionId,
|
|
410
|
+
actionName: action.name,
|
|
411
|
+
error: {
|
|
412
|
+
code: "HANDLER_ERROR",
|
|
413
|
+
message: e.message,
|
|
414
|
+
},
|
|
415
|
+
});
|
|
416
|
+
eventStream$.complete();
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CopilotKit Empty Adapter
|
|
3
|
+
*
|
|
4
|
+
* This adapter is meant to preserve adherence to runtime requirements, while doing nothing
|
|
5
|
+
* Ideal if you don't want to connect an LLM the to the runtime, and only use your LangGraph agent.
|
|
6
|
+
* Be aware that Copilot Suggestions will not work if you use this adapter
|
|
7
|
+
*
|
|
8
|
+
* ## Example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { CopilotRuntime, ExperimentalEmptyAdapter } from "@copilotkit/runtime";
|
|
12
|
+
*
|
|
13
|
+
* const copilotKit = new CopilotRuntime();
|
|
14
|
+
*
|
|
15
|
+
* return new ExperimentalEmptyAdapter();
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
import {
|
|
19
|
+
CopilotServiceAdapter,
|
|
20
|
+
CopilotRuntimeChatCompletionRequest,
|
|
21
|
+
CopilotRuntimeChatCompletionResponse,
|
|
22
|
+
} from "../../service-adapter";
|
|
23
|
+
import { randomId } from "@copilotkit/shared";
|
|
24
|
+
|
|
25
|
+
export class ExperimentalEmptyAdapter implements CopilotServiceAdapter {
|
|
26
|
+
async process(
|
|
27
|
+
request: CopilotRuntimeChatCompletionRequest,
|
|
28
|
+
): Promise<CopilotRuntimeChatCompletionResponse> {
|
|
29
|
+
return {
|
|
30
|
+
threadId: request.threadId || randomId(),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CopilotKit Adapter for Ollama
|
|
3
|
+
*
|
|
4
|
+
* <RequestExample>
|
|
5
|
+
* ```jsx CopilotRuntime Example
|
|
6
|
+
* const copilotKit = new CopilotRuntime();
|
|
7
|
+
* return copilotKit.response(req, new OllamaAdapter());
|
|
8
|
+
* ```
|
|
9
|
+
* </RequestExample>
|
|
10
|
+
*
|
|
11
|
+
* You can easily set the model to use by passing it to the constructor.
|
|
12
|
+
* ```jsx
|
|
13
|
+
* const copilotKit = new CopilotRuntime();
|
|
14
|
+
* return copilotKit.response(
|
|
15
|
+
* req,
|
|
16
|
+
* new OllamaAdapter({ model: "llama3-70b-8192" }),
|
|
17
|
+
* );
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
import { TextMessage } from "../../../graphql/types/converted";
|
|
21
|
+
import {
|
|
22
|
+
CopilotServiceAdapter,
|
|
23
|
+
CopilotRuntimeChatCompletionRequest,
|
|
24
|
+
CopilotRuntimeChatCompletionResponse,
|
|
25
|
+
} from "../../service-adapter";
|
|
26
|
+
import { Ollama } from "@langchain/community/llms/ollama";
|
|
27
|
+
import { randomId } from "@copilotkit/shared";
|
|
28
|
+
|
|
29
|
+
const DEFAULT_MODEL = "llama3:latest";
|
|
30
|
+
|
|
31
|
+
interface OllamaAdapterOptions {
|
|
32
|
+
model?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class ExperimentalOllamaAdapter implements CopilotServiceAdapter {
|
|
36
|
+
private model: string;
|
|
37
|
+
|
|
38
|
+
constructor(options?: OllamaAdapterOptions) {
|
|
39
|
+
if (options?.model) {
|
|
40
|
+
this.model = options.model;
|
|
41
|
+
} else {
|
|
42
|
+
this.model = DEFAULT_MODEL;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async process(
|
|
47
|
+
request: CopilotRuntimeChatCompletionRequest,
|
|
48
|
+
): Promise<CopilotRuntimeChatCompletionResponse> {
|
|
49
|
+
const { messages, actions, eventSource } = request;
|
|
50
|
+
// const messages = this.transformMessages(forwardedProps.messages);
|
|
51
|
+
|
|
52
|
+
const ollama = new Ollama({
|
|
53
|
+
model: this.model,
|
|
54
|
+
});
|
|
55
|
+
const contents = (messages.filter((m) => m.isTextMessage()) as TextMessage[]).map(
|
|
56
|
+
(m) => m.content,
|
|
57
|
+
);
|
|
58
|
+
const _stream = await ollama.stream(contents); // [TODO] role info is dropped...
|
|
59
|
+
|
|
60
|
+
eventSource.stream(async (eventStream$) => {
|
|
61
|
+
const currentMessageId = randomId();
|
|
62
|
+
eventStream$.sendTextMessageStart({ messageId: currentMessageId });
|
|
63
|
+
for await (const chunkText of _stream) {
|
|
64
|
+
eventStream$.sendTextMessageContent({
|
|
65
|
+
messageId: currentMessageId,
|
|
66
|
+
content: chunkText,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
eventStream$.sendTextMessageEnd({ messageId: currentMessageId });
|
|
70
|
+
// we may need to add this later.. [nc]
|
|
71
|
+
// let calls = (await result.response).functionCalls();
|
|
72
|
+
|
|
73
|
+
eventStream$.complete();
|
|
74
|
+
});
|
|
75
|
+
return {
|
|
76
|
+
threadId: request.threadId || randomId(),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copilot Runtime adapter for Google Generative AI (e.g. Gemini).
|
|
3
|
+
*
|
|
4
|
+
* ## Example
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { CopilotRuntime, GoogleGenerativeAIAdapter } from "@copilotkit/runtime";
|
|
8
|
+
* const { GoogleGenerativeAI } = require("@google/generative-ai");
|
|
9
|
+
*
|
|
10
|
+
* const genAI = new GoogleGenerativeAI(process.env["GOOGLE_API_KEY"]);
|
|
11
|
+
*
|
|
12
|
+
* const copilotKit = new CopilotRuntime();
|
|
13
|
+
*
|
|
14
|
+
* return new GoogleGenerativeAIAdapter({ model: "gemini-1.5-pro" });
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
import { ChatGoogle } from "@langchain/google-gauth";
|
|
18
|
+
import { LangChainAdapter } from "../langchain/langchain-adapter";
|
|
19
|
+
|
|
20
|
+
interface GoogleGenerativeAIAdapterOptions {
|
|
21
|
+
/**
|
|
22
|
+
* A custom Google Generative AI model to use.
|
|
23
|
+
*/
|
|
24
|
+
model?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class GoogleGenerativeAIAdapter extends LangChainAdapter {
|
|
28
|
+
constructor(options?: GoogleGenerativeAIAdapterOptions) {
|
|
29
|
+
super({
|
|
30
|
+
chainFn: async ({ messages, tools, threadId }) => {
|
|
31
|
+
const model = new ChatGoogle({
|
|
32
|
+
modelName: options?.model ?? "gemini-1.5-pro",
|
|
33
|
+
apiVersion: "v1beta",
|
|
34
|
+
}).bindTools(tools);
|
|
35
|
+
return model.stream(messages, { metadata: { conversation_id: threadId } });
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|