@copilotkit/runtime 1.5.12-next.5 → 1.5.12-next.7
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/CHANGELOG.md +17 -0
- package/__snapshots__/schema/schema.graphql +33 -0
- package/dist/{chunk-MFDRA3BJ.mjs → chunk-34276UUU.mjs} +652 -285
- package/dist/chunk-34276UUU.mjs.map +1 -0
- package/dist/{chunk-XRW7ZSWJ.mjs → chunk-DLESGNLO.mjs} +2 -2
- package/dist/{chunk-TPTCSIAR.mjs → chunk-S3KKBII4.mjs} +42 -30
- package/dist/chunk-S3KKBII4.mjs.map +1 -0
- package/dist/{chunk-QDMAQO2C.mjs → chunk-UBYSQI43.mjs} +2 -2
- package/dist/{chunk-NORCONUM.mjs → chunk-WTUPF3W3.mjs} +2 -2
- package/dist/{copilot-runtime-1a224a0f.d.ts → copilot-runtime-8c442d65.d.ts} +16 -3
- package/dist/graphql/types/converted/index.d.ts +1 -1
- package/dist/{groq-adapter-c35c5374.d.ts → groq-adapter-7a82cd22.d.ts} +21 -1
- package/dist/{index-24315d90.d.ts → index-a7f37670.d.ts} +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +727 -348
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -5
- package/dist/{langserve-a16ef8f4.d.ts → langserve-e308c437.d.ts} +32 -3
- package/dist/lib/index.d.ts +4 -4
- package/dist/lib/index.js +725 -346
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +5 -5
- package/dist/lib/integrations/index.d.ts +4 -4
- package/dist/lib/integrations/index.js +504 -244
- package/dist/lib/integrations/index.js.map +1 -1
- package/dist/lib/integrations/index.mjs +5 -5
- package/dist/lib/integrations/nest/index.d.ts +3 -3
- package/dist/lib/integrations/nest/index.js +504 -244
- package/dist/lib/integrations/nest/index.js.map +1 -1
- package/dist/lib/integrations/nest/index.mjs +3 -3
- package/dist/lib/integrations/node-express/index.d.ts +3 -3
- package/dist/lib/integrations/node-express/index.js +504 -244
- package/dist/lib/integrations/node-express/index.js.map +1 -1
- package/dist/lib/integrations/node-express/index.mjs +3 -3
- package/dist/lib/integrations/node-http/index.d.ts +3 -3
- package/dist/lib/integrations/node-http/index.js +504 -244
- package/dist/lib/integrations/node-http/index.js.map +1 -1
- package/dist/lib/integrations/node-http/index.mjs +2 -2
- package/dist/service-adapters/index.d.ts +4 -4
- package/dist/service-adapters/index.js +23 -11
- package/dist/service-adapters/index.js.map +1 -1
- package/dist/service-adapters/index.mjs +1 -1
- package/package.json +2 -2
- package/src/graphql/inputs/extensions.input.ts +21 -0
- package/src/graphql/inputs/generate-copilot-response.input.ts +4 -0
- package/src/graphql/inputs/load-agent-state.input.ts +10 -0
- package/src/graphql/resolvers/copilot.resolver.ts +8 -3
- package/src/graphql/resolvers/state.resolver.ts +23 -0
- package/src/graphql/types/agents-response.type.ts +1 -4
- package/src/graphql/types/copilot-response.type.ts +5 -1
- package/src/graphql/types/extensions-response.type.ts +23 -0
- package/src/graphql/types/load-agent-state-response.type.ts +17 -0
- package/src/lib/integrations/shared.ts +2 -1
- package/src/lib/runtime/copilot-runtime.ts +155 -31
- package/src/lib/runtime/remote-action-constructors.ts +42 -33
- package/src/lib/runtime/remote-actions.ts +12 -7
- package/src/lib/runtime/remote-lg-action.ts +24 -12
- package/src/service-adapters/anthropic/anthropic-adapter.ts +2 -3
- package/src/service-adapters/empty/empty-adapter.ts +2 -2
- package/src/service-adapters/events.ts +5 -0
- package/src/service-adapters/experimental/ollama/ollama-adapter.ts +2 -2
- package/src/service-adapters/groq/groq-adapter.ts +2 -2
- package/src/service-adapters/langchain/langchain-adapter.ts +10 -3
- package/src/service-adapters/openai/openai-adapter.ts +4 -3
- package/src/service-adapters/openai/openai-assistant-adapter.ts +15 -4
- package/src/service-adapters/service-adapter.ts +4 -0
- package/src/service-adapters/unify/unify-adapter.ts +2 -3
- package/dist/chunk-MFDRA3BJ.mjs.map +0 -1
- package/dist/chunk-TPTCSIAR.mjs.map +0 -1
- /package/dist/{chunk-XRW7ZSWJ.mjs.map → chunk-DLESGNLO.mjs.map} +0 -0
- /package/dist/{chunk-QDMAQO2C.mjs.map → chunk-UBYSQI43.mjs.map} +0 -0
- /package/dist/{chunk-NORCONUM.mjs.map → chunk-WTUPF3W3.mjs.map} +0 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Client } from "@langchain/langgraph-sdk";
|
|
2
|
-
import { createHash
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { randomUUID, isValidUUID } from "@copilotkit/shared";
|
|
3
4
|
import { parse as parsePartialJson } from "partial-json";
|
|
4
5
|
import { Logger } from "pino";
|
|
5
6
|
import { ActionInput } from "../../graphql/inputs/action.input";
|
|
@@ -76,7 +77,7 @@ async function streamEvents(controller: ReadableStreamDefaultController, args: E
|
|
|
76
77
|
const {
|
|
77
78
|
deploymentUrl,
|
|
78
79
|
langsmithApiKey,
|
|
79
|
-
threadId:
|
|
80
|
+
threadId: argsInitialThreadId,
|
|
80
81
|
agent,
|
|
81
82
|
nodeName: initialNodeName,
|
|
82
83
|
state: initialState,
|
|
@@ -90,26 +91,37 @@ async function streamEvents(controller: ReadableStreamDefaultController, args: E
|
|
|
90
91
|
const { name, assistantId: initialAssistantId } = agent;
|
|
91
92
|
|
|
92
93
|
const client = new Client({ apiUrl: deploymentUrl, apiKey: langsmithApiKey });
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
if (
|
|
96
|
-
|
|
94
|
+
|
|
95
|
+
let threadId = argsInitialThreadId ?? randomUUID();
|
|
96
|
+
if (argsInitialThreadId && argsInitialThreadId.startsWith("ck-")) {
|
|
97
|
+
threadId = argsInitialThreadId.substring(3);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (!isValidUUID(threadId)) {
|
|
101
|
+
console.warn(
|
|
102
|
+
`Cannot use the threadId ${threadId} with LangGraph Platform. Must be a valid UUID.`,
|
|
103
|
+
);
|
|
97
104
|
}
|
|
98
105
|
|
|
99
|
-
|
|
100
|
-
|
|
106
|
+
let wasInitiatedWithExistingThread = true;
|
|
107
|
+
try {
|
|
101
108
|
await client.threads.get(threadId);
|
|
102
|
-
}
|
|
103
|
-
|
|
109
|
+
} catch (error) {
|
|
110
|
+
wasInitiatedWithExistingThread = false;
|
|
111
|
+
await client.threads.create({ threadId });
|
|
104
112
|
}
|
|
105
113
|
|
|
106
114
|
let agentState = { values: {} };
|
|
107
115
|
if (wasInitiatedWithExistingThread) {
|
|
108
116
|
agentState = await client.threads.getState(threadId);
|
|
109
117
|
}
|
|
118
|
+
|
|
110
119
|
const agentStateValues = agentState.values as State;
|
|
111
120
|
state.messages = agentStateValues.messages;
|
|
112
|
-
const mode =
|
|
121
|
+
const mode =
|
|
122
|
+
threadId && nodeName != "__end__" && nodeName != undefined && nodeName != null
|
|
123
|
+
? "continue"
|
|
124
|
+
: "start";
|
|
113
125
|
let formattedMessages = [];
|
|
114
126
|
try {
|
|
115
127
|
formattedMessages = copilotkitMessagesToLangChain(messages);
|
|
@@ -483,7 +495,7 @@ function langGraphDefaultMergeState(
|
|
|
483
495
|
};
|
|
484
496
|
}
|
|
485
497
|
|
|
486
|
-
function langchainMessagesToCopilotKit(messages: any[]): any[] {
|
|
498
|
+
export function langchainMessagesToCopilotKit(messages: any[]): any[] {
|
|
487
499
|
const result: any[] = [];
|
|
488
500
|
const tool_call_names: Record<string, string> = {};
|
|
489
501
|
|
|
@@ -29,8 +29,7 @@ import {
|
|
|
29
29
|
limitMessagesToTokenCount,
|
|
30
30
|
} from "./utils";
|
|
31
31
|
|
|
32
|
-
import { randomId } from "@copilotkit/shared";
|
|
33
|
-
import { TextMessage } from "../../graphql/types/converted";
|
|
32
|
+
import { randomId, randomUUID } from "@copilotkit/shared";
|
|
34
33
|
|
|
35
34
|
const DEFAULT_MODEL = "claude-3-sonnet-20240229";
|
|
36
35
|
|
|
@@ -162,7 +161,7 @@ export class AnthropicAdapter implements CopilotServiceAdapter {
|
|
|
162
161
|
});
|
|
163
162
|
|
|
164
163
|
return {
|
|
165
|
-
threadId: threadId ||
|
|
164
|
+
threadId: threadId || randomUUID(),
|
|
166
165
|
};
|
|
167
166
|
}
|
|
168
167
|
}
|
|
@@ -20,14 +20,14 @@ import {
|
|
|
20
20
|
CopilotRuntimeChatCompletionRequest,
|
|
21
21
|
CopilotRuntimeChatCompletionResponse,
|
|
22
22
|
} from "../service-adapter";
|
|
23
|
-
import {
|
|
23
|
+
import { randomUUID } from "@copilotkit/shared";
|
|
24
24
|
|
|
25
25
|
export class EmptyAdapter implements CopilotServiceAdapter {
|
|
26
26
|
async process(
|
|
27
27
|
request: CopilotRuntimeChatCompletionRequest,
|
|
28
28
|
): Promise<CopilotRuntimeChatCompletionResponse> {
|
|
29
29
|
return {
|
|
30
|
-
threadId: request.threadId ||
|
|
30
|
+
threadId: request.threadId || randomUUID(),
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
33
|
}
|
|
@@ -226,10 +226,12 @@ export class RuntimeEventSource {
|
|
|
226
226
|
serverSideActions,
|
|
227
227
|
guardrailsResult$,
|
|
228
228
|
actionInputsWithoutAgents,
|
|
229
|
+
threadId,
|
|
229
230
|
}: {
|
|
230
231
|
serverSideActions: Action<any>[];
|
|
231
232
|
guardrailsResult$?: Subject<GuardrailsResult>;
|
|
232
233
|
actionInputsWithoutAgents: ActionInput[];
|
|
234
|
+
threadId: string;
|
|
233
235
|
}) {
|
|
234
236
|
this.callback(this.eventStream$).catch((error) => {
|
|
235
237
|
console.error("Error in event source callback", error);
|
|
@@ -285,6 +287,7 @@ export class RuntimeEventSource {
|
|
|
285
287
|
eventWithState.actionExecutionParentMessageId,
|
|
286
288
|
eventWithState.actionExecutionId,
|
|
287
289
|
actionInputsWithoutAgents,
|
|
290
|
+
threadId,
|
|
288
291
|
).catch((error) => {
|
|
289
292
|
console.error(error);
|
|
290
293
|
});
|
|
@@ -313,6 +316,7 @@ async function executeAction(
|
|
|
313
316
|
actionExecutionParentMessageId: string | null,
|
|
314
317
|
actionExecutionId: string,
|
|
315
318
|
actionInputsWithoutAgents: ActionInput[],
|
|
319
|
+
threadId: string,
|
|
316
320
|
) {
|
|
317
321
|
if (guardrailsResult$) {
|
|
318
322
|
const { status } = await firstValueFrom(guardrailsResult$);
|
|
@@ -370,6 +374,7 @@ async function executeAction(
|
|
|
370
374
|
|
|
371
375
|
const stream = await action.langGraphAgentHandler({
|
|
372
376
|
name: action.name,
|
|
377
|
+
threadId,
|
|
373
378
|
actionInputsWithoutAgents,
|
|
374
379
|
additionalMessages: [agentExecution, agentExecutionResult],
|
|
375
380
|
});
|
|
@@ -24,7 +24,7 @@ import {
|
|
|
24
24
|
CopilotRuntimeChatCompletionResponse,
|
|
25
25
|
} from "../../service-adapter";
|
|
26
26
|
import { Ollama } from "@langchain/community/llms/ollama";
|
|
27
|
-
import { randomId } from "@copilotkit/shared";
|
|
27
|
+
import { randomId, randomUUID } from "@copilotkit/shared";
|
|
28
28
|
|
|
29
29
|
const DEFAULT_MODEL = "llama3:latest";
|
|
30
30
|
|
|
@@ -73,7 +73,7 @@ export class ExperimentalOllamaAdapter implements CopilotServiceAdapter {
|
|
|
73
73
|
eventStream$.complete();
|
|
74
74
|
});
|
|
75
75
|
return {
|
|
76
|
-
threadId: request.threadId ||
|
|
76
|
+
threadId: request.threadId || randomUUID(),
|
|
77
77
|
};
|
|
78
78
|
}
|
|
79
79
|
}
|
|
@@ -25,7 +25,7 @@ import {
|
|
|
25
25
|
convertMessageToOpenAIMessage,
|
|
26
26
|
limitMessagesToTokenCount,
|
|
27
27
|
} from "../openai/utils";
|
|
28
|
-
import {
|
|
28
|
+
import { randomUUID } from "@copilotkit/shared";
|
|
29
29
|
|
|
30
30
|
const DEFAULT_MODEL = "llama3-groq-70b-8192-tool-use-preview";
|
|
31
31
|
|
|
@@ -167,7 +167,7 @@ export class GroqAdapter implements CopilotServiceAdapter {
|
|
|
167
167
|
});
|
|
168
168
|
|
|
169
169
|
return {
|
|
170
|
-
threadId: threadId ||
|
|
170
|
+
threadId: request.threadId || randomUUID(),
|
|
171
171
|
};
|
|
172
172
|
}
|
|
173
173
|
}
|
|
@@ -44,7 +44,7 @@ import {
|
|
|
44
44
|
} from "./utils";
|
|
45
45
|
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
46
46
|
import { LangChainReturnType } from "./types";
|
|
47
|
-
import {
|
|
47
|
+
import { randomUUID } from "@copilotkit/shared";
|
|
48
48
|
import { awaitAllCallbacks } from "@langchain/core/callbacks/promises";
|
|
49
49
|
|
|
50
50
|
interface ChainFnParameters {
|
|
@@ -72,8 +72,15 @@ export class LangChainAdapter implements CopilotServiceAdapter {
|
|
|
72
72
|
request: CopilotRuntimeChatCompletionRequest,
|
|
73
73
|
): Promise<CopilotRuntimeChatCompletionResponse> {
|
|
74
74
|
try {
|
|
75
|
-
const {
|
|
76
|
-
|
|
75
|
+
const {
|
|
76
|
+
eventSource,
|
|
77
|
+
model,
|
|
78
|
+
actions,
|
|
79
|
+
messages,
|
|
80
|
+
runId,
|
|
81
|
+
threadId: threadIdFromRequest,
|
|
82
|
+
} = request;
|
|
83
|
+
const threadId = threadIdFromRequest ?? randomUUID();
|
|
77
84
|
const result = await this.options.chainFn({
|
|
78
85
|
messages: messages.map(convertMessageToLangChainMessage),
|
|
79
86
|
tools: actions.map(convertActionInputToLangChainTool),
|
|
@@ -59,7 +59,7 @@ import {
|
|
|
59
59
|
convertMessageToOpenAIMessage,
|
|
60
60
|
limitMessagesToTokenCount,
|
|
61
61
|
} from "./utils";
|
|
62
|
-
import {
|
|
62
|
+
import { randomUUID } from "@copilotkit/shared";
|
|
63
63
|
|
|
64
64
|
const DEFAULT_MODEL = "gpt-4o";
|
|
65
65
|
|
|
@@ -107,7 +107,7 @@ export class OpenAIAdapter implements CopilotServiceAdapter {
|
|
|
107
107
|
request: CopilotRuntimeChatCompletionRequest,
|
|
108
108
|
): Promise<CopilotRuntimeChatCompletionResponse> {
|
|
109
109
|
const {
|
|
110
|
-
threadId,
|
|
110
|
+
threadId: threadIdFromRequest,
|
|
111
111
|
model = this.model,
|
|
112
112
|
messages,
|
|
113
113
|
actions,
|
|
@@ -115,6 +115,7 @@ export class OpenAIAdapter implements CopilotServiceAdapter {
|
|
|
115
115
|
forwardedParameters,
|
|
116
116
|
} = request;
|
|
117
117
|
const tools = actions.map(convertActionInputToOpenAITool);
|
|
118
|
+
const threadId = threadIdFromRequest ?? randomUUID();
|
|
118
119
|
|
|
119
120
|
let openaiMessages = messages.map(convertMessageToOpenAIMessage);
|
|
120
121
|
openaiMessages = limitMessagesToTokenCount(openaiMessages, tools, model);
|
|
@@ -204,7 +205,7 @@ export class OpenAIAdapter implements CopilotServiceAdapter {
|
|
|
204
205
|
});
|
|
205
206
|
|
|
206
207
|
return {
|
|
207
|
-
threadId
|
|
208
|
+
threadId,
|
|
208
209
|
};
|
|
209
210
|
}
|
|
210
211
|
}
|
|
@@ -94,8 +94,13 @@ export class OpenAIAssistantAdapter implements CopilotServiceAdapter {
|
|
|
94
94
|
request: CopilotRuntimeChatCompletionRequest,
|
|
95
95
|
): Promise<CopilotRuntimeChatCompletionResponse> {
|
|
96
96
|
const { messages, actions, eventSource, runId, forwardedParameters } = request;
|
|
97
|
+
|
|
97
98
|
// if we don't have a threadId, create a new thread
|
|
98
|
-
let threadId = request.threadId
|
|
99
|
+
let threadId = request.extensions?.openaiAssistantAPI?.threadId;
|
|
100
|
+
|
|
101
|
+
if (!threadId) {
|
|
102
|
+
threadId = (await this.openai.beta.threads.create()).id;
|
|
103
|
+
}
|
|
99
104
|
|
|
100
105
|
const lastMessage = messages.at(-1);
|
|
101
106
|
|
|
@@ -121,8 +126,15 @@ export class OpenAIAssistantAdapter implements CopilotServiceAdapter {
|
|
|
121
126
|
}
|
|
122
127
|
|
|
123
128
|
return {
|
|
124
|
-
threadId,
|
|
125
129
|
runId: nextRunId,
|
|
130
|
+
threadId,
|
|
131
|
+
extensions: {
|
|
132
|
+
...request.extensions,
|
|
133
|
+
openaiAssistantAPI: {
|
|
134
|
+
threadId: threadId,
|
|
135
|
+
runId: nextRunId,
|
|
136
|
+
},
|
|
137
|
+
},
|
|
126
138
|
};
|
|
127
139
|
}
|
|
128
140
|
|
|
@@ -133,6 +145,7 @@ export class OpenAIAssistantAdapter implements CopilotServiceAdapter {
|
|
|
133
145
|
eventSource: RuntimeEventSource,
|
|
134
146
|
) {
|
|
135
147
|
let run = await this.openai.beta.threads.runs.retrieve(threadId, runId);
|
|
148
|
+
|
|
136
149
|
if (!run.required_action) {
|
|
137
150
|
throw new Error("No tool outputs required");
|
|
138
151
|
}
|
|
@@ -193,7 +206,6 @@ export class OpenAIAssistantAdapter implements CopilotServiceAdapter {
|
|
|
193
206
|
throw new Error("No user message found");
|
|
194
207
|
}
|
|
195
208
|
|
|
196
|
-
// create a new message on the thread
|
|
197
209
|
await this.openai.beta.threads.messages.create(threadId, {
|
|
198
210
|
role: "user",
|
|
199
211
|
content: userMessage.content,
|
|
@@ -207,7 +219,6 @@ export class OpenAIAssistantAdapter implements CopilotServiceAdapter {
|
|
|
207
219
|
...(this.fileSearchEnabled ? [{ type: "file_search" } as AssistantTool] : []),
|
|
208
220
|
];
|
|
209
221
|
|
|
210
|
-
// run the thread
|
|
211
222
|
let stream = this.openai.beta.threads.runs.stream(threadId, {
|
|
212
223
|
assistant_id: this.assistantId,
|
|
213
224
|
instructions,
|
|
@@ -2,6 +2,8 @@ import { Message } from "../graphql/types/converted";
|
|
|
2
2
|
import { RuntimeEventSource } from "./events";
|
|
3
3
|
import { ActionInput } from "../graphql/inputs/action.input";
|
|
4
4
|
import { ForwardedParametersInput } from "../graphql/inputs/forwarded-parameters.input";
|
|
5
|
+
import { ExtensionsInput } from "../graphql/inputs/extensions.input";
|
|
6
|
+
import { ExtensionsResponse } from "../graphql/types/extensions-response.type";
|
|
5
7
|
|
|
6
8
|
export interface CopilotKitResponse {
|
|
7
9
|
stream: ReadableStream;
|
|
@@ -16,11 +18,13 @@ export interface CopilotRuntimeChatCompletionRequest {
|
|
|
16
18
|
threadId?: string;
|
|
17
19
|
runId?: string;
|
|
18
20
|
forwardedParameters?: ForwardedParametersInput;
|
|
21
|
+
extensions?: ExtensionsInput;
|
|
19
22
|
}
|
|
20
23
|
|
|
21
24
|
export interface CopilotRuntimeChatCompletionResponse {
|
|
22
25
|
threadId: string;
|
|
23
26
|
runId?: string;
|
|
27
|
+
extensions?: ExtensionsResponse;
|
|
24
28
|
}
|
|
25
29
|
|
|
26
30
|
export interface CopilotServiceAdapter {
|
|
@@ -17,14 +17,13 @@
|
|
|
17
17
|
* );
|
|
18
18
|
* ```
|
|
19
19
|
*/
|
|
20
|
-
import { TextMessage } from "../../graphql/types/converted";
|
|
21
20
|
import {
|
|
22
21
|
CopilotRuntimeChatCompletionRequest,
|
|
23
22
|
CopilotRuntimeChatCompletionResponse,
|
|
24
23
|
CopilotServiceAdapter,
|
|
25
24
|
} from "../service-adapter";
|
|
26
25
|
import OpenAI from "openai";
|
|
27
|
-
import { randomId } from "@copilotkit/shared";
|
|
26
|
+
import { randomId, randomUUID } from "@copilotkit/shared";
|
|
28
27
|
import { convertActionInputToOpenAITool, convertMessageToOpenAIMessage } from "../openai/utils";
|
|
29
28
|
|
|
30
29
|
export interface UnifyAdapterParams {
|
|
@@ -139,7 +138,7 @@ export class UnifyAdapter implements CopilotServiceAdapter {
|
|
|
139
138
|
});
|
|
140
139
|
|
|
141
140
|
return {
|
|
142
|
-
threadId: request.threadId ||
|
|
141
|
+
threadId: request.threadId || randomUUID(),
|
|
143
142
|
};
|
|
144
143
|
}
|
|
145
144
|
}
|