@copilotkit/runtime 0.37.0 → 0.38.0-mme-alpha.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.
- package/.turbo/turbo-build.log +69 -54
- package/CHANGELOG.md +11 -0
- package/__snapshots__/schema/schema.graphql +15 -4
- package/dist/{chunk-NFCPM5AM.mjs → chunk-6NZ4UMOD.mjs} +4 -4
- package/dist/chunk-6NZ4UMOD.mjs.map +1 -0
- package/dist/{chunk-2CCVVJDU.mjs → chunk-6OJ47NCG.mjs} +13 -15
- package/dist/chunk-6OJ47NCG.mjs.map +1 -0
- package/dist/chunk-BYB2LNMK.mjs +152 -0
- package/dist/chunk-BYB2LNMK.mjs.map +1 -0
- package/dist/{chunk-XPAUPJMW.mjs → chunk-EWS5TMDA.mjs} +411 -206
- package/dist/chunk-EWS5TMDA.mjs.map +1 -0
- package/dist/{chunk-7IFP53C6.mjs → chunk-FRK6BXXV.mjs} +49 -11
- package/dist/chunk-FRK6BXXV.mjs.map +1 -0
- package/dist/{chunk-BLTAUVRP.mjs → chunk-OZMCHYYR.mjs} +5 -3
- package/dist/{chunk-BLTAUVRP.mjs.map → chunk-OZMCHYYR.mjs.map} +1 -1
- package/dist/chunk-RHQLCJGG.mjs +7 -0
- package/dist/chunk-RHQLCJGG.mjs.map +1 -0
- package/dist/{chunk-5HGYI6EG.mjs → chunk-XI3HBDMA.mjs} +13 -5
- package/dist/chunk-XI3HBDMA.mjs.map +1 -0
- package/dist/{chunk-4UA4RB4C.mjs → chunk-XXYCNRFT.mjs} +12 -10
- package/dist/chunk-XXYCNRFT.mjs.map +1 -0
- package/dist/failed-response-status-reasons-0ab19e06.d.ts +49 -0
- package/dist/graphql/types/base/index.mjs +2 -1
- package/dist/graphql/types/converted/index.mjs +3 -2
- package/dist/{index-f0875df3.d.ts → index-d5ba24be.d.ts} +24 -23
- package/dist/index.d.ts +7 -4
- package/dist/index.js +486 -96
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +16 -7
- package/dist/index.mjs.map +1 -1
- package/dist/{langchain-adapter-9ce103f3.d.ts → langchain-adapter-54784d29.d.ts} +1 -1
- package/dist/{langserve-fd5066ee.d.ts → langserve-63794237.d.ts} +24 -7
- 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 +6 -4
- package/dist/lib/index.js +480 -96
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +9 -7
- package/dist/lib/integrations/index.d.ts +5 -3
- package/dist/lib/integrations/index.js +419 -80
- package/dist/lib/integrations/index.js.map +1 -1
- package/dist/lib/integrations/index.mjs +7 -5
- package/dist/lib/integrations/node-http/index.d.ts +4 -2
- package/dist/lib/integrations/node-http/index.js +409 -68
- package/dist/lib/integrations/node-http/index.js.map +1 -1
- package/dist/lib/integrations/node-http/index.mjs +6 -4
- package/dist/pages-router-d94f01da.d.ts +21 -0
- package/dist/service-adapters/index.d.ts +2 -2
- package/dist/service-adapters/index.js +54 -8
- package/dist/service-adapters/index.js.map +1 -1
- package/dist/service-adapters/index.mjs +5 -4
- package/dist/utils/index.d.ts +1 -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/package.json +6 -4
- package/src/graphql/inputs/cloud-guardrails.input.ts +2 -5
- package/src/graphql/inputs/cloud.input.ts +2 -2
- package/src/graphql/resolvers/copilot.resolver.ts +269 -30
- package/src/graphql/types/response-status.type.ts +16 -2
- package/src/index.ts +1 -0
- package/src/lib/cloud/index.ts +4 -0
- package/src/lib/copilot-runtime.ts +13 -37
- package/src/lib/integrations/nextjs/app-router.ts +9 -17
- package/src/lib/integrations/nextjs/pages-router.ts +9 -15
- package/src/lib/integrations/node-http/index.ts +6 -14
- package/src/lib/integrations/shared.ts +37 -17
- package/src/lib/logger.ts +28 -0
- package/src/service-adapters/events.ts +20 -2
- package/src/service-adapters/google/google-genai-adapter.ts +3 -0
- package/src/service-adapters/google/utils.ts +1 -1
- package/src/service-adapters/index.ts +1 -1
- package/src/service-adapters/langchain/langserve.ts +10 -4
- package/src/service-adapters/langchain/utils.ts +58 -9
- package/src/utils/failed-response-status-reasons.ts +48 -0
- package/src/utils/index.ts +1 -0
- package/dist/chunk-2CCVVJDU.mjs.map +0 -1
- package/dist/chunk-4UA4RB4C.mjs.map +0 -1
- package/dist/chunk-5HGYI6EG.mjs.map +0 -1
- package/dist/chunk-7IFP53C6.mjs.map +0 -1
- package/dist/chunk-NFCPM5AM.mjs.map +0 -1
- package/dist/chunk-XPAUPJMW.mjs.map +0 -1
- package/dist/pages-router-b6bc6c60.d.ts +0 -30
- package/src/lib/guardrails.ts +0 -3
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { YogaInitialContext } from "graphql-yoga";
|
|
2
|
-
import { GuardrailsOptions } from "../guardrails";
|
|
3
2
|
import { buildSchemaSync } from "type-graphql";
|
|
4
3
|
import { CopilotResolver } from "../../graphql/resolvers/copilot.resolver";
|
|
5
4
|
import { useDeferStream } from "@graphql-yoga/plugin-defer-stream";
|
|
6
5
|
import { CopilotRuntime } from "../copilot-runtime";
|
|
7
6
|
import { CopilotServiceAdapter } from "../../service-adapters";
|
|
7
|
+
import { CopilotCloudOptions } from "../cloud";
|
|
8
|
+
import { LogLevel, createLogger } from "../../lib/logger";
|
|
9
|
+
import { createYoga } from "graphql-yoga";
|
|
10
|
+
|
|
11
|
+
const logger = createLogger();
|
|
8
12
|
|
|
9
13
|
type AnyPrimitive = string | boolean | number | null;
|
|
10
14
|
export type CopilotRequestContextProperties = Record<
|
|
@@ -12,33 +16,37 @@ export type CopilotRequestContextProperties = Record<
|
|
|
12
16
|
AnyPrimitive | Record<string, AnyPrimitive>
|
|
13
17
|
>;
|
|
14
18
|
|
|
15
|
-
type CopilotKitContext = {
|
|
16
|
-
runtime: CopilotRuntime;
|
|
17
|
-
serviceAdapter: CopilotServiceAdapter;
|
|
18
|
-
properties: CopilotRequestContextProperties;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
19
|
export type GraphQLContext = YogaInitialContext & {
|
|
22
|
-
_copilotkit:
|
|
20
|
+
_copilotkit: CreateCopilotRuntimeServerOptions;
|
|
21
|
+
properties: CopilotRequestContextProperties;
|
|
22
|
+
logger: typeof logger;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
export interface CreateCopilotRuntimeServerOptions {
|
|
26
26
|
runtime: CopilotRuntime;
|
|
27
27
|
serviceAdapter: CopilotServiceAdapter;
|
|
28
|
-
|
|
28
|
+
endpoint: string;
|
|
29
|
+
baseUrl?: string;
|
|
30
|
+
cloud?: CopilotCloudOptions;
|
|
31
|
+
properties?: CopilotRequestContextProperties;
|
|
32
|
+
logLevel?: LogLevel;
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
export async function createContext(
|
|
32
36
|
initialContext: YogaInitialContext,
|
|
33
|
-
copilotKitContext:
|
|
37
|
+
copilotKitContext: CreateCopilotRuntimeServerOptions,
|
|
38
|
+
contextLogger: typeof logger,
|
|
39
|
+
properties: CopilotRequestContextProperties = {},
|
|
34
40
|
): Promise<Partial<GraphQLContext>> {
|
|
41
|
+
logger.debug({ copilotKitContext }, "Creating GraphQL context");
|
|
35
42
|
const ctx: GraphQLContext = {
|
|
36
43
|
...initialContext,
|
|
37
44
|
_copilotkit: {
|
|
38
45
|
...copilotKitContext,
|
|
39
46
|
},
|
|
47
|
+
properties: { ...properties },
|
|
48
|
+
logger: contextLogger,
|
|
40
49
|
};
|
|
41
|
-
|
|
42
50
|
return ctx;
|
|
43
51
|
}
|
|
44
52
|
|
|
@@ -47,22 +55,34 @@ export function buildSchema(
|
|
|
47
55
|
emitSchemaFile?: string;
|
|
48
56
|
} = {},
|
|
49
57
|
) {
|
|
58
|
+
logger.debug("Building GraphQL schema...");
|
|
50
59
|
const schema = buildSchemaSync({
|
|
51
60
|
resolvers: [CopilotResolver],
|
|
52
61
|
emitSchemaFile: options.emitSchemaFile,
|
|
53
62
|
});
|
|
63
|
+
logger.debug("GraphQL schema built successfully");
|
|
54
64
|
return schema;
|
|
55
65
|
}
|
|
56
66
|
|
|
57
|
-
export
|
|
67
|
+
export type CommonConfig = {
|
|
68
|
+
logging: typeof logger;
|
|
69
|
+
schema: ReturnType<typeof buildSchema>;
|
|
70
|
+
plugins: Parameters<typeof createYoga>[0]["plugins"];
|
|
71
|
+
context: (ctx: YogaInitialContext) => Promise<Partial<GraphQLContext>>;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export function getCommonConfig(options: CreateCopilotRuntimeServerOptions): CommonConfig {
|
|
75
|
+
const logLevel = (process.env.LOG_LEVEL as LogLevel) || (options.logLevel as LogLevel) || "error";
|
|
76
|
+
const logger = createLogger({ level: logLevel, component: "getCommonConfig" });
|
|
77
|
+
logger.debug("Getting common config");
|
|
78
|
+
|
|
79
|
+
const contextLogger = createLogger({ level: logLevel });
|
|
80
|
+
|
|
58
81
|
return {
|
|
82
|
+
logging: createLogger({ component: "Yoga GraphQL", level: logLevel }),
|
|
59
83
|
schema: buildSchema(),
|
|
60
84
|
plugins: [useDeferStream()],
|
|
61
85
|
context: (ctx: YogaInitialContext): Promise<Partial<GraphQLContext>> =>
|
|
62
|
-
createContext(ctx,
|
|
63
|
-
runtime: options.runtime,
|
|
64
|
-
serviceAdapter: options.serviceAdapter,
|
|
65
|
-
properties: {},
|
|
66
|
-
}),
|
|
86
|
+
createContext(ctx, options, contextLogger, options.properties),
|
|
67
87
|
};
|
|
68
88
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import createPinoLogger from "pino";
|
|
2
|
+
import pretty from "pino-pretty";
|
|
3
|
+
|
|
4
|
+
export type LogLevel = "debug" | "info" | "warn" | "error";
|
|
5
|
+
|
|
6
|
+
export type CopilotRuntimeLogger = ReturnType<typeof createLogger>;
|
|
7
|
+
|
|
8
|
+
export function createLogger(options?: { level?: LogLevel; component?: string }) {
|
|
9
|
+
const { level, component } = options || {};
|
|
10
|
+
const stream = pretty({ colorize: true });
|
|
11
|
+
|
|
12
|
+
const logger = createPinoLogger(
|
|
13
|
+
{
|
|
14
|
+
level: process.env.LOG_LEVEL || level || "error",
|
|
15
|
+
redact: {
|
|
16
|
+
paths: ["pid", "hostname"],
|
|
17
|
+
remove: true,
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
stream,
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
if (component) {
|
|
24
|
+
return logger.child({ component });
|
|
25
|
+
} else {
|
|
26
|
+
return logger;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Action } from "@copilotkit/shared";
|
|
2
|
-
import { of, concat, map, scan, concatMap, ReplaySubject } from "rxjs";
|
|
2
|
+
import { of, concat, map, scan, concatMap, ReplaySubject, Subject, firstValueFrom } from "rxjs";
|
|
3
3
|
import { streamLangChainResponse } from "./langchain/utils";
|
|
4
|
+
import { GuardrailsResult } from "../graphql/types/guardrails-result.type";
|
|
4
5
|
|
|
5
6
|
export enum RuntimeEventTypes {
|
|
6
7
|
TextMessageStart = "TextMessageStart",
|
|
@@ -109,7 +110,13 @@ export class RuntimeEventSource {
|
|
|
109
110
|
this.callback = callback;
|
|
110
111
|
}
|
|
111
112
|
|
|
112
|
-
process(
|
|
113
|
+
process({
|
|
114
|
+
serversideActions,
|
|
115
|
+
guardrailsResult$,
|
|
116
|
+
}: {
|
|
117
|
+
serversideActions: Action<any>[];
|
|
118
|
+
guardrailsResult$?: Subject<GuardrailsResult>;
|
|
119
|
+
}) {
|
|
113
120
|
this.callback(this.eventStream$).catch((error) => {
|
|
114
121
|
console.error("Error in event source callback", error);
|
|
115
122
|
});
|
|
@@ -156,6 +163,7 @@ export class RuntimeEventSource {
|
|
|
156
163
|
const toolCallEventStream$ = new RuntimeEventSubject();
|
|
157
164
|
executeAction(
|
|
158
165
|
toolCallEventStream$,
|
|
166
|
+
guardrailsResult$ ? guardrailsResult$ : null,
|
|
159
167
|
eventWithState.action!,
|
|
160
168
|
eventWithState.args,
|
|
161
169
|
eventWithState.actionExecutionId,
|
|
@@ -173,10 +181,20 @@ export class RuntimeEventSource {
|
|
|
173
181
|
|
|
174
182
|
async function executeAction(
|
|
175
183
|
eventStream$: RuntimeEventSubject,
|
|
184
|
+
guardrailsResult$: Subject<GuardrailsResult> | null,
|
|
176
185
|
action: Action<any>,
|
|
177
186
|
actionArguments: string,
|
|
178
187
|
actionExecutionId: string,
|
|
179
188
|
) {
|
|
189
|
+
if (guardrailsResult$) {
|
|
190
|
+
const { status } = await firstValueFrom(guardrailsResult$);
|
|
191
|
+
|
|
192
|
+
if (status === "denied") {
|
|
193
|
+
eventStream$.complete();
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
180
198
|
// Prepare arguments for function calling
|
|
181
199
|
let args: Record<string, any>[] = [];
|
|
182
200
|
if (actionArguments) {
|
|
@@ -104,6 +104,9 @@ export class GoogleGenerativeAIAdapter implements CopilotServiceAdapter {
|
|
|
104
104
|
let isTextMessage = false;
|
|
105
105
|
for await (const chunk of result.stream) {
|
|
106
106
|
const chunkText = chunk.text();
|
|
107
|
+
if (chunkText === "") {
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
107
110
|
if (!isTextMessage) {
|
|
108
111
|
isTextMessage = true;
|
|
109
112
|
eventStream$.sendTextMessageStart(nanoid());
|
|
@@ -3,4 +3,4 @@ export { OpenAIAdapter } from "./openai/openai-adapter";
|
|
|
3
3
|
export { OpenAIAssistantAdapter } from "./openai/openai-assistant-adapter";
|
|
4
4
|
export { GoogleGenerativeAIAdapter } from "./google/google-genai-adapter";
|
|
5
5
|
export { LangChainAdapter } from "./langchain/langchain-adapter";
|
|
6
|
-
export { RemoteChain } from "./langchain/langserve";
|
|
6
|
+
export { RemoteChain, type RemoteChainParameters } from "./langchain/langserve";
|
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
import { Parameter, Action } from "@copilotkit/shared";
|
|
2
2
|
import { RemoteRunnable } from "langchain/runnables/remote";
|
|
3
3
|
|
|
4
|
-
export interface
|
|
4
|
+
export interface RemoteChainParameters {
|
|
5
5
|
name: string;
|
|
6
6
|
description: string;
|
|
7
7
|
chainUrl: string;
|
|
8
8
|
parameters?: Parameter[];
|
|
9
|
-
parameterType
|
|
9
|
+
parameterType?: "single" | "multi";
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
export class RemoteChain
|
|
13
|
-
|
|
12
|
+
export class RemoteChain {
|
|
13
|
+
name: string;
|
|
14
|
+
description: string;
|
|
15
|
+
chainUrl: string;
|
|
16
|
+
parameters?: Parameter[];
|
|
17
|
+
parameterType: "single" | "multi";
|
|
18
|
+
|
|
19
|
+
constructor(options: RemoteChainParameters) {
|
|
14
20
|
this.name = options.name;
|
|
15
21
|
this.description = options.description;
|
|
16
22
|
this.chainUrl = options.chainUrl;
|
|
@@ -75,7 +75,7 @@ export function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean)
|
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
export function convertActionInputToLangChainTool(actionInput: ActionInput):
|
|
78
|
+
export function convertActionInputToLangChainTool(actionInput: ActionInput): any {
|
|
79
79
|
return new DynamicStructuredTool({
|
|
80
80
|
name: actionInput.name,
|
|
81
81
|
description: actionInput.description,
|
|
@@ -98,6 +98,40 @@ interface StreamLangChainResponseParams {
|
|
|
98
98
|
};
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
+
function getConstructorName(object: any): string {
|
|
102
|
+
if (object && typeof object === "object" && object.constructor && object.constructor.name) {
|
|
103
|
+
return object.constructor.name;
|
|
104
|
+
}
|
|
105
|
+
return "";
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function isAIMessage(message: any): message is AIMessage {
|
|
109
|
+
return getConstructorName(message) === "AIMessage";
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function isAIMessageChunk(message: any): message is AIMessageChunk {
|
|
113
|
+
return getConstructorName(message) === "AIMessageChunk";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function isBaseMessageChunk(message: any): message is BaseMessageChunk {
|
|
117
|
+
return getConstructorName(message) === "BaseMessageChunk";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function maybeSendActionExecutionResultIsMessage(
|
|
121
|
+
eventStream$: RuntimeEventSubject,
|
|
122
|
+
actionExecution?: { id: string; name: string },
|
|
123
|
+
) {
|
|
124
|
+
// language models need a result after the function call
|
|
125
|
+
// we simply let them know that we are sending a message
|
|
126
|
+
if (actionExecution) {
|
|
127
|
+
eventStream$.sendActionExecutionResult(
|
|
128
|
+
actionExecution.id,
|
|
129
|
+
actionExecution.name,
|
|
130
|
+
"Sending a message",
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
101
135
|
export async function streamLangChainResponse({
|
|
102
136
|
result,
|
|
103
137
|
eventStream$,
|
|
@@ -119,8 +153,9 @@ export async function streamLangChainResponse({
|
|
|
119
153
|
|
|
120
154
|
// 2. AIMessage
|
|
121
155
|
// Send the content and function call of the AIMessage as the content of the chunk.
|
|
122
|
-
|
|
123
|
-
|
|
156
|
+
else if (isAIMessage(result)) {
|
|
157
|
+
maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
|
|
158
|
+
|
|
124
159
|
if (result.content) {
|
|
125
160
|
eventStream$.sendTextMessage(nanoid(), result.content as string);
|
|
126
161
|
}
|
|
@@ -135,7 +170,9 @@ export async function streamLangChainResponse({
|
|
|
135
170
|
|
|
136
171
|
// 3. BaseMessageChunk
|
|
137
172
|
// Send the content and function call of the AIMessage as the content of the chunk.
|
|
138
|
-
else if (result
|
|
173
|
+
else if (isBaseMessageChunk(result)) {
|
|
174
|
+
maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
|
|
175
|
+
|
|
139
176
|
if (result.lc_kwargs?.content) {
|
|
140
177
|
eventStream$.sendTextMessage(nanoid(), result.content as string);
|
|
141
178
|
}
|
|
@@ -152,7 +189,9 @@ export async function streamLangChainResponse({
|
|
|
152
189
|
|
|
153
190
|
// 4. IterableReadableStream
|
|
154
191
|
// Stream the result of the LangChain function.
|
|
155
|
-
else if ("getReader" in result) {
|
|
192
|
+
else if (result && "getReader" in result) {
|
|
193
|
+
maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
|
|
194
|
+
|
|
156
195
|
let reader = result.getReader();
|
|
157
196
|
|
|
158
197
|
let mode: "function" | "message" | null = null;
|
|
@@ -165,15 +204,15 @@ export async function streamLangChainResponse({
|
|
|
165
204
|
let toolCallId: string | undefined = undefined;
|
|
166
205
|
let toolCallArgs: string | undefined = undefined;
|
|
167
206
|
let hasToolCall: boolean = false;
|
|
168
|
-
let content = value
|
|
207
|
+
let content = value?.content as string;
|
|
169
208
|
|
|
170
|
-
if (value
|
|
209
|
+
if (isAIMessageChunk(value)) {
|
|
171
210
|
let chunk = value.tool_call_chunks?.[0];
|
|
172
211
|
toolCallName = chunk?.name;
|
|
173
212
|
toolCallId = chunk?.id;
|
|
174
213
|
toolCallArgs = chunk?.args;
|
|
175
214
|
hasToolCall = chunk != undefined;
|
|
176
|
-
} else if (value
|
|
215
|
+
} else if (isBaseMessageChunk(value)) {
|
|
177
216
|
let chunk = value.additional_kwargs?.tool_calls?.[0];
|
|
178
217
|
toolCallName = chunk?.function?.name;
|
|
179
218
|
toolCallId = chunk?.id;
|
|
@@ -222,7 +261,7 @@ export async function streamLangChainResponse({
|
|
|
222
261
|
eventStream$.sendActionExecutionResult(
|
|
223
262
|
actionExecution.id,
|
|
224
263
|
actionExecution.name,
|
|
225
|
-
|
|
264
|
+
encodeResult(result),
|
|
226
265
|
);
|
|
227
266
|
}
|
|
228
267
|
|
|
@@ -233,3 +272,13 @@ export async function streamLangChainResponse({
|
|
|
233
272
|
|
|
234
273
|
eventStream$.complete();
|
|
235
274
|
}
|
|
275
|
+
|
|
276
|
+
function encodeResult(result: any): string {
|
|
277
|
+
if (result === undefined) {
|
|
278
|
+
return "";
|
|
279
|
+
} else if (typeof result === "string") {
|
|
280
|
+
return result;
|
|
281
|
+
} else {
|
|
282
|
+
return JSON.stringify(result);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FailedResponseStatus,
|
|
3
|
+
FailedResponseStatusReason,
|
|
4
|
+
} from "../graphql/types/response-status.type";
|
|
5
|
+
|
|
6
|
+
export class GuardrailsValidationFailureResponse extends FailedResponseStatus {
|
|
7
|
+
reason = FailedResponseStatusReason.GUARDRAILS_VALIDATION_FAILED;
|
|
8
|
+
declare details: {
|
|
9
|
+
guardrailsReason: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
constructor({ guardrailsReason }) {
|
|
13
|
+
super();
|
|
14
|
+
this.details = {
|
|
15
|
+
guardrailsReason,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class MessageStreamInterruptedResponse extends FailedResponseStatus {
|
|
21
|
+
reason = FailedResponseStatusReason.MESSAGE_STREAM_INTERRUPTED;
|
|
22
|
+
declare details: {
|
|
23
|
+
messageId: string;
|
|
24
|
+
description: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
constructor({ messageId }: { messageId: string }) {
|
|
28
|
+
super();
|
|
29
|
+
this.details = {
|
|
30
|
+
messageId,
|
|
31
|
+
description: "Check the message for mode details",
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export class UnknownErrorResponse extends FailedResponseStatus {
|
|
37
|
+
reason = FailedResponseStatusReason.UNKNOWN_ERROR;
|
|
38
|
+
declare details: {
|
|
39
|
+
description?: string;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
constructor({ description }: { description?: string }) {
|
|
43
|
+
super();
|
|
44
|
+
this.details = {
|
|
45
|
+
description,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./failed-response-status-reasons";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib/integrations/nextjs/app-router.ts","../src/lib/integrations/nextjs/pages-router.ts"],"sourcesContent":["import { createYoga } from \"graphql-yoga\";\nimport { getCommonConfig } from \"../shared\";\nimport { CopilotRuntime } from \"../../copilot-runtime\";\nimport { CopilotServiceAdapter } from \"../../../service-adapters\";\n\nexport function copilotRuntimeNextJSAppRouterEndpoint({\n runtime,\n endpoint,\n serviceAdapter,\n}: {\n runtime: CopilotRuntime;\n serviceAdapter: CopilotServiceAdapter;\n endpoint: string;\n}) {\n const commonConfig = getCommonConfig({ runtime, serviceAdapter });\n\n const yoga = createYoga({\n ...commonConfig,\n graphqlEndpoint: endpoint,\n fetchAPI: { Response: globalThis.Response },\n });\n\n return {\n handleRequest: yoga,\n GET: yoga,\n POST: yoga,\n OPTIONS: yoga,\n };\n}\n","import { YogaServerInstance, createYoga } from \"graphql-yoga\";\nimport { GraphQLContext, getCommonConfig } from \"../shared\";\nimport { CopilotRuntime } from \"../../copilot-runtime\";\nimport { CopilotServiceAdapter } from \"../../../service-adapters\";\n\nexport const config = {\n api: {\n bodyParser: false,\n },\n};\n\nexport type CopilotRuntimeServerInstance<T> = YogaServerInstance<T, Partial<GraphQLContext>>;\n\n// Theis import is needed to fix the type error\n// Fix is currently in TypeScript 5.5 beta, waiting for stable version\n// https://github.com/microsoft/TypeScript/issues/42873#issuecomment-2066874644\nexport type {} from \"@whatwg-node/server\";\n\nexport function copilotRuntimeNextJSPagesRouterEndpoint({\n runtime,\n endpoint,\n serviceAdapter,\n}: {\n runtime: CopilotRuntime;\n serviceAdapter: CopilotServiceAdapter;\n endpoint: string;\n}): CopilotRuntimeServerInstance<GraphQLContext> {\n const commonConfig = getCommonConfig({ runtime, serviceAdapter });\n\n const yoga = createYoga({\n ...commonConfig,\n graphqlEndpoint: endpoint,\n });\n\n return yoga;\n}\n"],"mappings":";;;;;;;;AAAA,SAASA,kBAAkB;AAKpB,SAASC,sCAAsC,EACpDC,SACAC,UACAC,eAAc,GAKf;AACC,QAAMC,eAAeC,gBAAgB;IAAEJ;IAASE;EAAe,CAAA;AAE/D,QAAMG,OAAOC,WAAW;IACtB,GAAGH;IACHI,iBAAiBN;IACjBO,UAAU;MAAEC,UAAUC,WAAWD;IAAS;EAC5C,CAAA;AAEA,SAAO;IACLE,eAAeN;IACfO,KAAKP;IACLQ,MAAMR;IACNS,SAAST;EACX;AACF;AAvBgBN;;;ACLhB,SAA6BgB,cAAAA,mBAAkB;AAKxC,IAAMC,SAAS;EACpBC,KAAK;IACHC,YAAY;EACd;AACF;AASO,SAASC,wCAAwC,EACtDC,SACAC,UACAC,eAAc,GAKf;AACC,QAAMC,eAAeC,gBAAgB;IAAEJ;IAASE;EAAe,CAAA;AAE/D,QAAMG,OAAOC,YAAW;IACtB,GAAGH;IACHI,iBAAiBN;EACnB,CAAA;AAEA,SAAOI;AACT;AAjBgBN;","names":["createYoga","copilotRuntimeNextJSAppRouterEndpoint","runtime","endpoint","serviceAdapter","commonConfig","getCommonConfig","yoga","createYoga","graphqlEndpoint","fetchAPI","Response","globalThis","handleRequest","GET","POST","OPTIONS","createYoga","config","api","bodyParser","copilotRuntimeNextJSPagesRouterEndpoint","runtime","endpoint","serviceAdapter","commonConfig","getCommonConfig","yoga","createYoga","graphqlEndpoint"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib/copilot-runtime.ts","../src/lib/copilot-cloud.ts","../src/service-adapters/conversion.ts"],"sourcesContent":["/**\n * Handles requests from frontend, provides function calling and various LLM backends.\n *\n * <img\n * referrerPolicy=\"no-referrer-when-downgrade\"\n * src=\"https://static.scarf.sh/a.png?x-pxid=a9b290bb-38f9-4518-ac3b-8f54fdbf43be\"\n * />\n *\n * <RequestExample>\n * ```jsx CopilotRuntime Example\n * import {\n * CopilotRuntime,\n * OpenAIAdapter\n * } from \"@copilotkit/runtime\";\n *\n * export async function POST(req: Request) {\n * const copilotKit = new CopilotRuntime();\n * return copilotKit.response(req, new OpenAIAdapter());\n * }\n *\n * ```\n * </RequestExample>\n *\n * This class is the main entry point for the runtime. It handles requests from the frontend, provides function calling and various LLM backends.\n *\n * For example, to use OpenAI as a backend (check the [OpenAI Adapter](./OpenAIAdapter) docs for more info):\n * ```typescript\n * const copilotKit = new CopilotRuntime();\n * return copilotKit.response(req, new OpenAIAdapter());\n * ```\n *\n * Currently we support:\n *\n * - [OpenAI](./OpenAIAdapter)\n * - [LangChain](./LangChainAdapter)\n * - [OpenAI Assistant API](./OpenAIAssistantAdapter)\n * - [Google Gemini](./GoogleGenerativeAIAdapter)\n *\n * ## Server Side Actions\n *\n * CopilotKit supports actions that can be executed on the server side. You can define server side actions by passing the `actions` parameter:\n *\n * ```typescript\n * const copilotKit = new CopilotRuntime({\n * actions: [\n * {\n * name: \"sayHello\",\n * description: \"Says hello to someone.\",\n * argumentAnnotations: [\n * {\n * name: \"arg\",\n * type: \"string\",\n * description: \"The name of the person to say hello to.\",\n * required: true,\n * },\n * ],\n * implementation: async (arg) => {\n * console.log(\"Hello from the server\", arg, \"!\");\n * },\n * },\n * ],\n * });\n * ```\n *\n * Server side actions can also return a result which becomes part of the message history.\n *\n * This is useful because it gives the LLM context about what happened on the server side. In addition,\n * it can be used to look up information from a vector or relational database and other sources.\n *\n * In addition to that, server side actions can also come from LangChain, including support for streaming responses.\n *\n * Returned results can be of the following type:\n *\n * - anything serializable to JSON\n * - `string`\n * - LangChain types:\n * - `IterableReadableStream`\n * - `BaseMessageChunk`\n * - `AIMessage`\n *\n * ## LangServe\n *\n * The backend also supports LangServe, enabling you to connect to existing chains, for example python based chains.\n * Use the `langserve` parameter to specify URLs for LangServe.\n *\n * ```typescript\n * const copilotKit = new CopilotRuntime({\n * langserve: [\n * {\n * chainUrl: \"http://my-langserve.chain\",\n * name: \"performResearch\",\n * description: \"Performs research on a given topic.\",\n * },\n * ],\n * });\n * ```\n *\n * When left out, arguments are automatically inferred from the schema provided by LangServe.\n */\n\nimport { Action, actionParametersToJsonSchema, Parameter } from \"@copilotkit/shared\";\nimport { RemoteChain, CopilotServiceAdapter } from \"../service-adapters\";\nimport { CopilotCloud, RemoteCopilotCloud } from \"./copilot-cloud\";\nimport { MessageInput } from \"../graphql/inputs/message.input\";\nimport { ActionInput } from \"../graphql/inputs/action.input\";\nimport { RuntimeEventSource } from \"../service-adapters/events\";\nimport { convertGqlInputToMessages } from \"../service-adapters/conversion\";\n\ninterface CopilotRuntimeRequest {\n serviceAdapter: CopilotServiceAdapter;\n messages: MessageInput[];\n actions: ActionInput[];\n threadId?: string;\n runId?: string;\n publicApiKey?: string;\n}\n\ninterface CopilotRuntimeResponse {\n threadId?: string;\n runId?: string;\n eventSource: RuntimeEventSource;\n}\n\nexport interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []> {\n /*\n * A list of server side actions that can be executed.\n */\n actions?: Action<T>[];\n\n /*\n * An array of LangServer URLs.\n */\n langserve?: RemoteChain[];\n\n debug?: boolean;\n copilotCloud?: CopilotCloud;\n}\n\nexport class CopilotRuntime<const T extends Parameter[] | [] = []> {\n public actions: Action<any>[] = [];\n private langserve: Promise<Action<any>>[] = [];\n private debug: boolean = false;\n private copilotCloud: CopilotCloud;\n\n constructor(params?: CopilotRuntimeConstructorParams<T>) {\n this.actions = params?.actions || [];\n\n for (const chain of params?.langserve || []) {\n const remoteChain = new RemoteChain(chain);\n this.langserve.push(remoteChain.toAction());\n }\n this.debug = params?.debug || false;\n this.copilotCloud = params?.copilotCloud || new RemoteCopilotCloud();\n }\n\n addAction<const T extends Parameter[] | [] = []>(action: Action<T>): void {\n this.removeAction(action.name);\n this.actions.push(action);\n }\n\n removeAction(actionName: string): void {\n this.actions = this.actions.filter((f) => f.name !== actionName);\n }\n\n async process({\n serviceAdapter,\n messages,\n actions: clientSideActionsInput,\n threadId,\n runId,\n publicApiKey,\n }: CopilotRuntimeRequest): Promise<CopilotRuntimeResponse> {\n // TODO-PROTOCOL: cloud configuration\n // const cloud: CopilotCloudConfig = forwardedProps.cloud;\n\n const langserveFunctions: Action<any>[] = [];\n\n for (const chainPromise of this.langserve) {\n try {\n const chain = await chainPromise;\n langserveFunctions.push(chain);\n } catch (error) {\n console.error(\"Error loading langserve chain:\", error);\n }\n }\n\n const serverSideActionsInput: ActionInput[] = [...this.actions, ...langserveFunctions].map(\n (action) => ({\n name: action.name,\n description: action.description,\n jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters)),\n }),\n );\n\n const actions = flattenToolCallsNoDuplicates([\n ...serverSideActionsInput,\n ...clientSideActionsInput,\n ]);\n\n try {\n const eventSource = new RuntimeEventSource();\n // TODO-PROTOCOL: type this and support function calls\n const result = await serviceAdapter.process({\n messages: convertGqlInputToMessages(messages),\n actions,\n threadId,\n runId,\n eventSource,\n });\n\n // TODO-PROTOCOL add guardrails\n //\n // if (publicApiKey !== undefined) {\n // // wait for the cloud log chat to finish before streaming back the response\n // try {\n // const checkGuardrailsInputResult = await this.copilotCloud.checkGuardrailsInput({\n // cloud,\n // publicApiKey,\n // messages: forwardedProps.messages || [],\n // });\n\n // if (checkGuardrailsInputResult.status === \"denied\") {\n // // the chat was denied. instead of streaming back the response,\n // // we let the client know...\n // return {\n // stream: new SingleChunkReadableStream(checkGuardrailsInputResult.reason),\n // // headers: result.headers,\n // };\n // }\n // } catch (error) {\n // console.error(\"Error checking guardrails:\", error);\n // }\n // }\n return {\n threadId: result.threadId,\n runId: result.runId,\n eventSource,\n };\n } catch (error) {\n console.error(\"Error getting response:\", error);\n throw error;\n }\n }\n}\n\nexport function flattenToolCallsNoDuplicates(toolsByPriority: ActionInput[]): ActionInput[] {\n let allTools: ActionInput[] = [];\n const allToolNames: string[] = [];\n for (const tool of toolsByPriority) {\n if (!allToolNames.includes(tool.name)) {\n allTools.push(tool);\n allToolNames.push(tool.name);\n }\n }\n return allTools;\n}\n\n/**\n * @deprecated use CopilotRuntime instead\n */\nexport class CopilotBackend extends CopilotRuntime {}\n","import {\n COPILOT_CLOUD_API_URL,\n COPILOT_CLOUD_VERSION,\n COPILOT_CLOUD_PUBLIC_API_KEY_HEADER,\n CopilotCloudConfig,\n} from \"@copilotkit/shared\";\n\nexport interface CloudCheckGuardrailsInputParams {\n cloud: CopilotCloudConfig;\n publicApiKey: string;\n // TODO-PROTOCOL: switch to graphql\n messages: any[];\n}\n\nexport interface CloudCheckGuardrailsInputResponse {\n status: \"allowed\" | \"denied\";\n reason: string;\n}\n\nexport abstract class CopilotCloud {\n abstract checkGuardrailsInput(\n params: CloudCheckGuardrailsInputParams,\n ): Promise<CloudCheckGuardrailsInputResponse>;\n}\n\nexport class RemoteCopilotCloud extends CopilotCloud {\n constructor(private readonly cloudApiUrl: string = COPILOT_CLOUD_API_URL) {\n super();\n }\n\n async checkGuardrailsInput({\n cloud,\n publicApiKey,\n messages,\n }: CloudCheckGuardrailsInputParams): Promise<CloudCheckGuardrailsInputResponse> {\n const url = `${this.cloudApiUrl}/${COPILOT_CLOUD_VERSION}/copilotkit/guardrails/input`;\n\n const response = await fetch(url, {\n method: \"PUT\",\n headers: {\n \"Content-Type\": \"application/json\",\n [COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey,\n },\n body: JSON.stringify({ messages, cloud }),\n });\n\n if (!response.ok) {\n let message = response.statusText;\n\n // try to parse the response body for a more detailed error message\n try {\n const json = await response.json();\n if (json.message) {\n message = json.message;\n }\n } catch (error) {}\n\n throw new Error(\"Failed to check input guardrails: \" + message);\n }\n const json = await response.json();\n return { status: json.status, reason: json.reason };\n }\n}\n","import {\n ActionExecutionMessage,\n Message,\n ResultMessage,\n TextMessage,\n} from \"../graphql/types/converted\";\nimport { MessageInput } from \"../graphql/inputs/message.input\";\nimport { plainToInstance } from \"class-transformer\";\n\nexport function convertGqlInputToMessages(inputMessages: MessageInput[]): Message[] {\n const messages: Message[] = [];\n\n for (const message of inputMessages) {\n if (message.textMessage) {\n messages.push(\n plainToInstance(TextMessage, {\n id: message.id,\n createdAt: message.createdAt,\n role: message.textMessage.role,\n content: message.textMessage.content,\n }),\n );\n } else if (message.actionExecutionMessage) {\n messages.push(\n plainToInstance(ActionExecutionMessage, {\n id: message.id,\n createdAt: message.createdAt,\n name: message.actionExecutionMessage.name,\n arguments: JSON.parse(message.actionExecutionMessage.arguments),\n scope: message.actionExecutionMessage.scope,\n }),\n );\n } else if (message.resultMessage) {\n messages.push(\n plainToInstance(ResultMessage, {\n id: message.id,\n createdAt: message.createdAt,\n actionExecutionId: message.resultMessage.actionExecutionId,\n actionName: message.resultMessage.actionName,\n result: message.resultMessage.result,\n }),\n );\n }\n }\n\n return messages;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAoGA,SAAiBA,oCAA+C;;;ACpGhE,SACEC,uBACAC,uBACAC,2CAEK;AAcA,IAAeC,eAAf,MAAeA;AAItB;AAJsBA;AAMf,IAAMC,qBAAN,cAAiCD,aAAAA;;EACtCE,YAA6BC,cAAsBC,uBAAuB;AACxE,UAAK;SADsBD,cAAAA;EAE7B;EAEA,MAAME,qBAAqB,EACzBC,OACAC,cACAC,SAAQ,GACsE;AAC9E,UAAMC,MAAM,GAAG,KAAKN,eAAeO;AAEnC,UAAMC,WAAW,MAAMC,MAAMH,KAAK;MAChCI,QAAQ;MACRC,SAAS;QACP,gBAAgB;QAChB,CAACC,mCAAAA,GAAsCR;MACzC;MACAS,MAAMC,KAAKC,UAAU;QAAEV;QAAUF;MAAM,CAAA;IACzC,CAAA;AAEA,QAAI,CAACK,SAASQ,IAAI;AAChB,UAAIC,UAAUT,SAASU;AAGvB,UAAI;AACF,cAAMC,QAAO,MAAMX,SAASW,KAAI;AAChC,YAAIA,MAAKF,SAAS;AAChBA,oBAAUE,MAAKF;QACjB;MACF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ACzE,MAAIC,WAA0B,CAAA;AAC9B,QAAMC,eAAyB,CAAA;AAC/B,aAAWC,QAAQH,iBAAiB;AAClC,QAAI,CAACE,aAAaE,SAASD,KAAK/B,IAAI,GAAG;AACrC6B,eAASnC,KAAKqC,IAAAA;AACdD,mBAAapC,KAAKqC,KAAK/B,IAAI;IAC7B;EACF;AACA,SAAO6B;AACT;AAVgBN;AAeT,IAAMU,iBAAN,cAA6BjD,eAAAA;AAAgB;AAAvCiD;","names":["actionParametersToJsonSchema","COPILOT_CLOUD_API_URL","COPILOT_CLOUD_VERSION","COPILOT_CLOUD_PUBLIC_API_KEY_HEADER","CopilotCloud","RemoteCopilotCloud","constructor","cloudApiUrl","COPILOT_CLOUD_API_URL","checkGuardrailsInput","cloud","publicApiKey","messages","url","COPILOT_CLOUD_VERSION","response","fetch","method","headers","COPILOT_CLOUD_PUBLIC_API_KEY_HEADER","body","JSON","stringify","ok","message","statusText","json","error","Error","status","reason","plainToInstance","convertGqlInputToMessages","inputMessages","messages","message","textMessage","push","plainToInstance","TextMessage","id","createdAt","role","content","actionExecutionMessage","ActionExecutionMessage","name","arguments","JSON","parse","scope","resultMessage","ResultMessage","actionExecutionId","actionName","result","CopilotRuntime","actions","langserve","debug","copilotCloud","constructor","params","chain","remoteChain","RemoteChain","push","toAction","RemoteCopilotCloud","addAction","action","removeAction","name","actionName","filter","f","process","serviceAdapter","messages","clientSideActionsInput","threadId","runId","publicApiKey","langserveFunctions","chainPromise","error","console","serverSideActionsInput","map","description","jsonSchema","JSON","stringify","actionParametersToJsonSchema","parameters","flattenToolCallsNoDuplicates","eventSource","RuntimeEventSource","result","convertGqlInputToMessages","toolsByPriority","allTools","allToolNames","tool","includes","CopilotBackend"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/service-adapters/openai/openai-adapter.ts","../src/service-adapters/openai/utils.ts","../src/service-adapters/openai/openai-assistant-adapter.ts","../src/service-adapters/google/google-genai-adapter.ts","../src/service-adapters/google/utils.ts","../src/service-adapters/langchain/langchain-adapter.ts","../src/service-adapters/langchain/langserve.ts"],"sourcesContent":["/**\n * CopilotRuntime Adapter for OpenAI.\n *\n * <RequestExample>\n * ```jsx CopilotRuntime Example\n * const copilotKit = new CopilotRuntime();\n * return copilotKit.response(req, new OpenAIAdapter());\n * ```\n * </RequestExample>\n *\n * You can easily set the model to use by passing it to the constructor.\n * ```jsx\n * const copilotKit = new CopilotRuntime();\n * return copilotKit.response(\n * req,\n * new OpenAIAdapter({ model: \"gpt-4o\" }),\n * );\n * ```\n *\n * To use your custom OpenAI instance, pass the `openai` property.\n * ```jsx\n * const openai = new OpenAI({\n * organization: \"your-organization-id\",\n * apiKey: \"your-api-key\"\n * });\n *\n * const copilotKit = new CopilotRuntime();\n * return copilotKit.response(\n * req,\n * new OpenAIAdapter({ openai }),\n * );\n * ```\n *\n */\nimport OpenAI from \"openai\";\nimport {\n CopilotServiceAdapter,\n CopilotRuntimeChatCompletionRequest,\n CopilotRuntimeChatCompletionResponse,\n} from \"../service-adapter\";\nimport {\n convertActionInputToOpenAITool,\n convertMessageToOpenAIMessage,\n limitMessagesToTokenCount,\n} from \"./utils\";\n\nconst DEFAULT_MODEL = \"gpt-4o\";\n\nexport interface OpenAIAdapterParams {\n /**\n * An optional OpenAI instance to use.\n */\n openai?: OpenAI;\n\n /**\n * The model to use.\n */\n model?: string;\n}\n\nexport class OpenAIAdapter implements CopilotServiceAdapter {\n private model: string = DEFAULT_MODEL;\n\n private _openai: OpenAI;\n public get openai(): OpenAI {\n return this._openai;\n }\n\n constructor(params?: OpenAIAdapterParams) {\n this._openai = params?.openai || new OpenAI({});\n if (params?.model) {\n this.model = params.model;\n }\n }\n\n async process({\n model = this.model,\n messages,\n actions,\n eventSource,\n }: CopilotRuntimeChatCompletionRequest): Promise<CopilotRuntimeChatCompletionResponse> {\n const tools = actions.map(convertActionInputToOpenAITool);\n\n let openaiMessages = messages.map(convertMessageToOpenAIMessage);\n openaiMessages = limitMessagesToTokenCount(openaiMessages, tools, model);\n\n const stream = this.openai.beta.chat.completions.stream({\n model: model,\n stream: true,\n messages: openaiMessages,\n ...(tools.length > 0 && { tools }),\n });\n\n eventSource.stream(async (eventStream$) => {\n let mode: \"function\" | \"message\" | null = null;\n for await (const chunk of stream) {\n const toolCall = chunk.choices[0].delta.tool_calls?.[0];\n const content = chunk.choices[0].delta.content;\n\n // When switching from message to function or vice versa,\n // send the respective end event.\n // If toolCall?.id is defined, it means a new tool call starts.\n if (mode === \"message\" && toolCall?.id) {\n mode = null;\n eventStream$.sendTextMessageEnd();\n } else if (mode === \"function\" && (toolCall === undefined || toolCall?.id)) {\n mode = null;\n eventStream$.sendActionExecutionEnd();\n }\n\n // If we send a new message type, send the appropriate start event.\n if (mode === null) {\n if (toolCall?.id) {\n mode = \"function\";\n eventStream$.sendActionExecutionStart(toolCall!.id, toolCall!.function!.name);\n } else if (content) {\n mode = \"message\";\n eventStream$.sendTextMessageStart(chunk.id);\n }\n }\n\n // send the content events\n if (mode === \"message\" && content) {\n eventStream$.sendTextMessageContent(content);\n } else if (mode === \"function\" && toolCall?.function?.arguments) {\n eventStream$.sendActionExecutionArgs(toolCall.function.arguments);\n }\n }\n\n // send the end events\n if (mode === \"message\") {\n eventStream$.sendTextMessageEnd();\n } else if (mode === \"function\") {\n eventStream$.sendActionExecutionEnd();\n }\n\n eventStream$.complete();\n });\n\n return {};\n }\n}\n","import {\n ActionExecutionMessage,\n Message,\n ResultMessage,\n TextMessage,\n} from \"../../graphql/types/converted\";\nimport { Tiktoken, TiktokenModel, encodingForModel } from \"js-tiktoken\";\nimport { ActionInput } from \"../../graphql/inputs/action.input\";\nimport { ChatCompletionMessageParam, ChatCompletionTool } from \"openai/resources\";\n\nexport function limitMessagesToTokenCount(\n messages: any[],\n tools: any[],\n model: string,\n maxTokens?: number,\n): any[] {\n maxTokens ||= maxTokensForOpenAIModel(model);\n\n const result: any[] = [];\n const toolsNumTokens = countToolsTokens(model, tools);\n if (toolsNumTokens > maxTokens) {\n throw new Error(`Too many tokens in function definitions: ${toolsNumTokens} > ${maxTokens}`);\n }\n maxTokens -= toolsNumTokens;\n\n for (const message of messages) {\n if (message.role === \"system\") {\n const numTokens = countMessageTokens(model, message);\n maxTokens -= numTokens;\n\n if (maxTokens < 0) {\n throw new Error(\"Not enough tokens for system message.\");\n }\n }\n }\n\n let cutoff: boolean = false;\n\n const reversedMessages = [...messages].reverse();\n for (const message of reversedMessages) {\n if (message.role === \"system\") {\n result.unshift(message);\n continue;\n } else if (cutoff) {\n continue;\n }\n let numTokens = countMessageTokens(model, message);\n if (maxTokens < numTokens) {\n cutoff = true;\n continue;\n }\n result.unshift(message);\n maxTokens -= numTokens;\n }\n\n return result;\n}\n\nexport function maxTokensForOpenAIModel(model: string): number {\n return maxTokensByModel[model] || DEFAULT_MAX_TOKENS;\n}\n\nconst DEFAULT_MAX_TOKENS = 128000;\n\nconst maxTokensByModel: { [key: string]: number } = {\n // GPT-4\n \"gpt-4o\": 128000,\n \"gpt-4o-2024-05-13\": 128000,\n \"gpt-4-turbo\": 128000,\n \"gpt-4-turbo-2024-04-09\": 128000,\n \"gpt-4-0125-preview\": 128000,\n \"gpt-4-turbo-preview\": 128000,\n \"gpt-4-1106-preview\": 128000,\n \"gpt-4-vision-preview\": 128000,\n \"gpt-4-1106-vision-preview\": 128000,\n \"gpt-4-32k\": 32768,\n \"gpt-4-32k-0613\": 32768,\n \"gpt-4-32k-0314\": 32768,\n \"gpt-4\": 8192,\n \"gpt-4-0613\": 8192,\n \"gpt-4-0314\": 8192,\n\n // GPT-3.5\n \"gpt-3.5-turbo-0125\": 16385,\n \"gpt-3.5-turbo\": 16385,\n \"gpt-3.5-turbo-1106\": 16385,\n \"gpt-3.5-turbo-instruct\": 4096,\n \"gpt-3.5-turbo-16k\": 16385,\n \"gpt-3.5-turbo-0613\": 4096,\n \"gpt-3.5-turbo-16k-0613\": 16385,\n \"gpt-3.5-turbo-0301\": 4097,\n};\n\nfunction countToolsTokens(model: string, tools: any[]): number {\n if (tools.length === 0) {\n return 0;\n }\n const json = JSON.stringify(tools);\n return countTokens(model, json);\n}\n\nfunction countMessageTokens(model: string, message: any): number {\n return countTokens(model, message.content || \"\");\n}\n\nfunction countTokens(model: string, text: string): number {\n let enc: Tiktoken;\n try {\n enc = encodingForModel(model as TiktokenModel);\n } catch (e) {\n enc = encodingForModel(\"gpt-4\");\n }\n return enc.encode(text).length;\n}\n\nexport function convertActionInputToOpenAITool(action: ActionInput): ChatCompletionTool {\n return {\n type: \"function\",\n function: {\n name: action.name,\n description: action.description,\n parameters: JSON.parse(action.jsonSchema),\n },\n };\n}\n\nexport function convertMessageToOpenAIMessage(message: Message): ChatCompletionMessageParam {\n if (message instanceof TextMessage) {\n return {\n role: message.role,\n content: message.content,\n };\n } else if (message instanceof ActionExecutionMessage) {\n return {\n role: \"assistant\",\n tool_calls: [\n {\n id: message.id,\n type: \"function\",\n function: {\n name: message.name,\n arguments: JSON.stringify(message.arguments),\n },\n },\n ],\n };\n } else if (message instanceof ResultMessage) {\n return {\n role: \"tool\",\n content: message.result,\n tool_call_id: message.actionExecutionId,\n };\n }\n}\n\nexport function convertSystemMessageToAssistantAPI(message: ChatCompletionMessageParam) {\n return {\n ...message,\n ...(message.role === \"system\" && {\n role: \"assistant\",\n content: \"THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: \" + message.content,\n }),\n };\n}\n","/**\n * CopilotKit Adapter for the OpenAI Assistant API.\n *\n * Use this adapter to get responses from the OpenAI Assistant API.\n *\n * <RequestExample>\n * ```typescript\n * const copilotKit = new CopilotRuntime();\n * return copilotKit.response(\n * req,\n * new OpenAIAssistantAdapter({\n * assistantId: \"your-assistant-id\"\n * })\n * );\n * ```\n * </RequestExample>\n */\nimport OpenAI from \"openai\";\nimport {\n CopilotServiceAdapter,\n CopilotRuntimeChatCompletionRequest,\n CopilotRuntimeChatCompletionResponse,\n} from \"../service-adapter\";\nimport { Message, ResultMessage, TextMessage } from \"../../graphql/types/converted\";\nimport {\n convertActionInputToOpenAITool,\n convertMessageToOpenAIMessage,\n convertSystemMessageToAssistantAPI,\n} from \"./utils\";\nimport { RunSubmitToolOutputsStreamParams } from \"openai/resources/beta/threads/runs/runs\";\nimport { AssistantStream } from \"openai/lib/AssistantStream\";\nimport { RuntimeEventSource } from \"../events\";\nimport { ActionInput } from \"../../graphql/inputs/action.input\";\nimport { AssistantStreamEvent, AssistantTool } from \"openai/resources/beta/assistants\";\n\nexport interface OpenAIAssistantAdapterParams {\n /**\n * The ID of the assistant to use.\n */\n assistantId: string;\n\n /**\n * An instance of `OpenAI` to use for the request. If not provided, a new instance will be created.\n */\n openai?: OpenAI;\n\n /**\n * Whether to enable the code interpreter. Defaults to `true`.\n */\n codeInterpreterEnabled?: boolean;\n\n /**\n * Whether to enable retrieval. Defaults to `true`.\n */\n fileSearchEnabled?: boolean;\n}\n\nexport class OpenAIAssistantAdapter implements CopilotServiceAdapter {\n private openai: OpenAI;\n private codeInterpreterEnabled: boolean;\n private assistantId: string;\n private fileSearchEnabled: boolean;\n\n constructor(params: OpenAIAssistantAdapterParams) {\n this.openai = params.openai || new OpenAI({});\n this.codeInterpreterEnabled = params.codeInterpreterEnabled === false || true;\n this.fileSearchEnabled = params.fileSearchEnabled === false || true;\n this.assistantId = params.assistantId;\n }\n\n async process({\n messages,\n actions,\n eventSource,\n threadId,\n runId,\n }: CopilotRuntimeChatCompletionRequest): Promise<CopilotRuntimeChatCompletionResponse> {\n // if we don't have a threadId, create a new thread\n threadId ||= (await this.openai.beta.threads.create()).id;\n const lastMessage = messages.at(-1);\n\n let nextRunId: string | undefined = undefined;\n\n // submit function outputs\n if (lastMessage instanceof ResultMessage && runId) {\n nextRunId = await this.submitToolOutputs(threadId, runId, messages, eventSource);\n }\n // submit user message\n else if (lastMessage instanceof TextMessage) {\n nextRunId = await this.submitUserMessage(threadId, messages, actions, eventSource);\n }\n // unsupported message\n else {\n throw new Error(\"No actionable message found in the messages\");\n }\n\n return {\n threadId,\n runId: nextRunId,\n };\n }\n\n private async submitToolOutputs(\n threadId: string,\n runId: string,\n messages: Message[],\n eventSource: RuntimeEventSource,\n ) {\n let run = await this.openai.beta.threads.runs.retrieve(threadId, runId);\n if (!run.required_action) {\n throw new Error(\"No tool outputs required\");\n }\n\n // get the required tool call ids\n const toolCallsIds = run.required_action.submit_tool_outputs.tool_calls.map(\n (toolCall) => toolCall.id,\n );\n\n // search for these tool calls\n const resultMessages = messages.filter(\n (message) =>\n message instanceof ResultMessage && toolCallsIds.includes(message.actionExecutionId),\n ) as ResultMessage[];\n\n if (toolCallsIds.length != resultMessages.length) {\n throw new Error(\"Number of function results does not match the number of tool calls\");\n }\n\n // submit the tool outputs\n const toolOutputs: RunSubmitToolOutputsStreamParams.ToolOutput[] = resultMessages.map(\n (message) => {\n return {\n tool_call_id: message.actionExecutionId,\n output: message.result,\n };\n },\n );\n\n const stream = this.openai.beta.threads.runs.submitToolOutputsStream(threadId, runId, {\n tool_outputs: toolOutputs,\n });\n\n await this.streamResponse(stream, eventSource);\n return runId;\n }\n\n private async submitUserMessage(\n threadId: string,\n messages: Message[],\n actions: ActionInput[],\n eventSource: RuntimeEventSource,\n ) {\n messages = [...messages];\n\n // get the instruction message\n const instructionsMessage = messages.shift();\n const instructions =\n instructionsMessage instanceof TextMessage ? instructionsMessage.content : \"\";\n\n // get the latest user message\n const userMessage = messages\n .map(convertMessageToOpenAIMessage)\n .map(convertSystemMessageToAssistantAPI)\n .at(-1);\n\n if (userMessage.role !== \"user\") {\n throw new Error(\"No user message found\");\n }\n\n // create a new message on the thread\n await this.openai.beta.threads.messages.create(threadId, {\n role: \"user\",\n content: userMessage.content,\n });\n\n const openaiTools = actions.map(convertActionInputToOpenAITool);\n\n const tools = [\n ...openaiTools,\n ...(this.codeInterpreterEnabled ? [{ type: \"code_interpreter\" } as AssistantTool] : []),\n ...(this.fileSearchEnabled ? [{ type: \"file_search\" } as AssistantTool] : []),\n ];\n\n // run the thread\n let stream = this.openai.beta.threads.runs.stream(threadId, {\n assistant_id: this.assistantId,\n instructions,\n tools: tools,\n });\n\n await this.streamResponse(stream, eventSource);\n\n return getRunIdFromStream(stream);\n }\n\n private async streamResponse(stream: AssistantStream, eventSource: RuntimeEventSource) {\n eventSource.stream(async (eventStream$) => {\n let inFunctionCall = false;\n\n for await (const chunk of stream) {\n switch (chunk.event) {\n case \"thread.message.created\":\n if (inFunctionCall) {\n eventStream$.sendActionExecutionEnd();\n }\n eventStream$.sendTextMessageStart(chunk.data.id);\n break;\n case \"thread.message.delta\":\n if (chunk.data.delta.content?.[0].type === \"text\") {\n eventStream$.sendTextMessageContent(chunk.data.delta.content?.[0].text.value);\n }\n break;\n case \"thread.message.completed\":\n eventStream$.sendTextMessageEnd();\n break;\n case \"thread.run.step.delta\":\n let toolCallId: string | undefined;\n let toolCallName: string | undefined;\n let toolCallArgs: string | undefined;\n if (\n chunk.data.delta.step_details.type === \"tool_calls\" &&\n chunk.data.delta.step_details.tool_calls?.[0].type === \"function\"\n ) {\n toolCallId = chunk.data.delta.step_details.tool_calls?.[0].id;\n toolCallName = chunk.data.delta.step_details.tool_calls?.[0].function.name;\n toolCallArgs = chunk.data.delta.step_details.tool_calls?.[0].function.arguments;\n }\n\n if (toolCallName && toolCallId) {\n if (inFunctionCall) {\n eventStream$.sendActionExecutionEnd();\n }\n inFunctionCall = true;\n eventStream$.sendActionExecutionStart(toolCallId, toolCallName);\n } else if (toolCallArgs) {\n eventStream$.sendActionExecutionArgs(toolCallArgs);\n }\n break;\n }\n }\n if (inFunctionCall) {\n eventStream$.sendActionExecutionEnd();\n }\n eventStream$.complete();\n });\n }\n}\n\nfunction getRunIdFromStream(stream: AssistantStream): Promise<string> {\n return new Promise<string>((resolve, reject) => {\n let runIdGetter = (event: AssistantStreamEvent) => {\n if (event.event === \"thread.run.created\") {\n const runId = event.data.id;\n stream.off(\"event\", runIdGetter);\n resolve(runId);\n }\n };\n stream.on(\"event\", runIdGetter);\n });\n}\n","/**\n * CopilotKit Adapter for Google Gemini\n *\n * Use this adapter for a Google Gemini backend.\n *\n * <RequestExample>\n * ```typescript\n * const copilotKit = new CopilotRuntime();\n * return copilotKit.response(\n * req,\n * new GoogleGenerativeAIAdapter()\n * );\n * ```\n * </RequestExample>\n *\n * To set up a different model, pass the model prop:\n *\n * ```typescript\n * const copilotKit = new CopilotRuntime();\n * const genAI = new GoogleGenerativeAI(\n * process.env[\"GOOGLE_API_KEY\"]!\n * );\n * const model = genAI.getGenerativeModel(\n * { model: \"gemini-pro\" }\n * );\n * return copilotKit.response(\n * req,\n * new GoogleGenerativeAIAdapter()\n * );\n * ```\n */\nimport { CopilotServiceAdapter } from \"../service-adapter\";\nimport {\n CopilotRuntimeChatCompletionRequest,\n CopilotRuntimeChatCompletionResponse,\n} from \"../service-adapter\";\nimport { GenerativeModel, GoogleGenerativeAI } from \"@google/generative-ai\";\nimport { TextMessage } from \"../../graphql/types/converted\";\nimport { convertMessageToGoogleGenAIMessage, transformActionToGoogleGenAITool } from \"./utils\";\nimport { nanoid } from \"nanoid\";\n\ninterface GoogleGenerativeAIAdapterOptions {\n /**\n * A custom `GenerativeModel` to use for the request.\n */\n model?: GenerativeModel;\n}\n\nexport class GoogleGenerativeAIAdapter implements CopilotServiceAdapter {\n private model: GenerativeModel;\n\n constructor(options?: GoogleGenerativeAIAdapterOptions) {\n if (options?.model) {\n this.model = options.model;\n } else {\n const genAI = new GoogleGenerativeAI(process.env[\"GOOGLE_API_KEY\"]!);\n this.model = genAI.getGenerativeModel({ model: \"gemini-pro\" });\n }\n }\n\n async process(\n request: CopilotRuntimeChatCompletionRequest,\n ): Promise<CopilotRuntimeChatCompletionResponse> {\n const { messages, actions, eventSource } = request;\n\n // get the history (everything except the first and last message)\n const history = messages.slice(1, -1).map(convertMessageToGoogleGenAIMessage);\n\n // get the current message (the last message)\n const currentMessage = convertMessageToGoogleGenAIMessage(messages.at(-1));\n if (!currentMessage) {\n throw new Error(\"No current message\");\n }\n\n let systemMessage: string;\n const firstMessage = messages.at(0);\n if (firstMessage instanceof TextMessage && firstMessage.role === \"system\") {\n systemMessage = firstMessage.content.trim();\n } else {\n throw new Error(\"First message is not a system message\");\n }\n\n const tools = actions.map(transformActionToGoogleGenAITool);\n\n const isFirstGenGeminiPro =\n this.model.model === \"gemini-pro\" || this.model.model === \"models/gemini-pro\";\n\n const chat = this.model.startChat({\n history: [\n ...history,\n // gemini-pro does not support system instructions, so we need to add them to the history\n ...(isFirstGenGeminiPro ? [{ role: \"user\", parts: [{ text: systemMessage }] }] : []),\n ],\n // only gemini-1.5-pro-latest and later supports setting system instructions\n ...(isFirstGenGeminiPro\n ? {}\n : { systemInstruction: { role: \"user\", parts: [{ text: systemMessage }] } }),\n tools,\n });\n\n const result = await chat.sendMessageStream(currentMessage.parts);\n\n eventSource.stream(async (eventStream$) => {\n let isTextMessage = false;\n for await (const chunk of result.stream) {\n const chunkText = chunk.text();\n if (!isTextMessage) {\n isTextMessage = true;\n eventStream$.sendTextMessageStart(nanoid());\n }\n eventStream$.sendTextMessageContent(chunkText);\n }\n if (isTextMessage) {\n eventStream$.sendTextMessageEnd();\n }\n\n let calls = (await result.response).functionCalls();\n if (calls) {\n for (let call of calls) {\n eventStream$.sendActionExecution(\n nanoid(),\n call.name,\n JSON.stringify(replaceNewlinesInObject(call.args)),\n );\n }\n }\n eventStream$.complete();\n });\n\n return {};\n }\n}\n\nfunction replaceNewlinesInObject(obj: any): any {\n if (typeof obj === \"string\") {\n return obj.replace(/\\\\\\\\n/g, \"\\n\");\n } else if (Array.isArray(obj)) {\n return obj.map(replaceNewlinesInObject);\n } else if (typeof obj === \"object\" && obj !== null) {\n const newObj: any = {};\n for (const key in obj) {\n if (obj.hasOwnProperty(key)) {\n newObj[key] = replaceNewlinesInObject(obj[key]);\n }\n }\n return newObj;\n }\n return obj;\n}\n","import {\n ActionExecutionMessage,\n Message,\n ResultMessage,\n TextMessage,\n} from \"../../graphql/types/converted\";\nimport { Tool } from \"@google/generative-ai\";\nimport { ActionInput } from \"../../graphql/inputs/action.input\";\n\nexport function convertMessageToGoogleGenAIMessage(message: Message) {\n if (message instanceof TextMessage) {\n const role = {\n user: \"user\",\n assistant: \"model\",\n system: \"user\",\n }[message.role];\n\n const text =\n message.role === \"system\"\n ? \"THE FOLLOWING MESSAGE IS A SYSTEM MESSAGE: \" + message.content\n : message.content;\n\n return {\n role,\n parts: [{ text }],\n };\n } else if (message instanceof ActionExecutionMessage) {\n return {\n role: \"model\",\n parts: [\n {\n functionCall: {\n name: message.name,\n args: message.arguments,\n },\n },\n ],\n };\n } else if (message instanceof ResultMessage) {\n return {\n role: \"model\",\n parts: [\n {\n functionResponse: {\n name: message.actionName,\n response: {\n name: message.actionName,\n content: tryParseJson(message.result),\n },\n },\n },\n ],\n };\n }\n}\n\nexport function transformActionToGoogleGenAITool(action: ActionInput): Tool {\n const name = action.name;\n const description = action.description;\n const parameters = JSON.parse(action.jsonSchema);\n\n const transformProperties = (props: any) => {\n for (const key in props) {\n if (props[key].type) {\n props[key].type = props[key].type.toUpperCase();\n }\n if (props[key].properties) {\n transformProperties(props[key].properties);\n }\n }\n };\n transformProperties(parameters);\n\n return {\n functionDeclarations: [\n {\n name,\n description,\n parameters,\n },\n ],\n };\n}\n\nfunction tryParseJson(str?: string) {\n if (!str) {\n return \"\";\n }\n try {\n return JSON.parse(str);\n } catch (e) {\n return str;\n }\n}\n","/**\n * CopilotKit Adapter for LangChain\n *\n * Use this adapter to use LangChain as a backend.\n *\n * ```typescript\n * return copilotKit.response(\n * req,\n * new LangChainAdapter(async (forwardedProps) => {\n * const model = new ChatOpenAI({ modelName: \"gpt-4o\" });\n * return model.stream(forwardedProps.messages, {\n * tools: forwardedProps.tools,\n * });\n * })\n * );\n * ```\n * The async handler function can return:\n *\n * - a simple `string` response\n * - a LangChain stream `IterableReadableStream`\n * - a LangChain `BaseMessageChunk` object\n * - a LangChain `AIMessage` object\n */\n\nimport { BaseMessage } from \"@langchain/core/messages\";\nimport { CopilotServiceAdapter } from \"../service-adapter\";\nimport {\n CopilotRuntimeChatCompletionRequest,\n CopilotRuntimeChatCompletionResponse,\n} from \"../service-adapter\";\nimport {\n convertActionInputToLangChainTool,\n convertMessageToLangChainMessage,\n streamLangChainResponse,\n} from \"./utils\";\nimport { DynamicStructuredTool } from \"@langchain/core/tools\";\nimport { LangChainReturnType } from \"./types\";\n\ninterface ChainFnParameters {\n model: string;\n messages: BaseMessage[];\n tools: DynamicStructuredTool[];\n threadId?: string;\n runId?: string;\n}\n\ninterface LangChainAdapterOptions {\n chainFn: (parameters: ChainFnParameters) => Promise<LangChainReturnType>;\n}\n\nexport class LangChainAdapter implements CopilotServiceAdapter {\n /**\n * To use LangChain as a backend, provide a handler function to the adapter with your custom LangChain logic.\n */\n constructor(private options: LangChainAdapterOptions) {}\n\n async process({\n eventSource,\n model,\n actions,\n messages,\n threadId,\n runId,\n }: CopilotRuntimeChatCompletionRequest): Promise<CopilotRuntimeChatCompletionResponse> {\n const result = await this.options.chainFn({\n messages: messages.map(convertMessageToLangChainMessage),\n tools: actions.map(convertActionInputToLangChainTool),\n model,\n threadId,\n runId,\n });\n\n eventSource.stream(async (eventStream$) => {\n await streamLangChainResponse({\n result,\n eventStream$,\n });\n });\n\n return {};\n }\n}\n","import { Parameter, Action } from \"@copilotkit/shared\";\nimport { RemoteRunnable } from \"langchain/runnables/remote\";\n\nexport interface RemoteChain {\n name: string;\n description: string;\n chainUrl: string;\n parameters?: Parameter[];\n parameterType: \"single\" | \"multi\";\n}\n\nexport class RemoteChain implements RemoteChain {\n constructor(options: RemoteChain) {\n this.name = options.name;\n this.description = options.description;\n this.chainUrl = options.chainUrl;\n this.parameters = options.parameters;\n this.parameterType = options.parameterType || \"multi\";\n }\n\n async toAction(): Promise<Action<any>> {\n if (!this.parameters) {\n await this.inferLangServeParameters();\n }\n\n return {\n name: this.name,\n description: this.description,\n parameters: this.parameters!,\n handler: async (args: any) => {\n const runnable = new RemoteRunnable({ url: this.chainUrl });\n let input: any;\n if (this.parameterType === \"single\") {\n input = args[Object.keys(args)[0]];\n } else {\n input = args;\n }\n return await runnable.invoke(input);\n },\n };\n }\n\n async inferLangServeParameters() {\n const supportedTypes = [\"string\", \"number\", \"boolean\"];\n\n let schemaUrl = this.chainUrl.replace(/\\/+$/, \"\") + \"/input_schema\";\n let schema = await fetch(schemaUrl)\n .then((res) => res.json())\n .catch(() => {\n throw new Error(\"Failed to fetch langserve schema at \" + schemaUrl);\n });\n // for now, don't use json schema, just do a simple conversion\n\n if (supportedTypes.includes(schema.type)) {\n this.parameterType = \"single\";\n this.parameters = [\n {\n name: \"input\",\n type: schema.type,\n description: \"The input to the chain\",\n },\n ];\n } else if (schema.type === \"object\") {\n this.parameterType = \"multi\";\n this.parameters = Object.keys(schema.properties).map((key) => {\n let property = schema.properties[key];\n if (!supportedTypes.includes(property.type)) {\n throw new Error(\"Unsupported schema type\");\n }\n return {\n name: key,\n type: property.type,\n description: property.description || \"\",\n required: schema.required?.includes(key) || false,\n };\n });\n } else {\n throw new Error(\"Unsupported schema type\");\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAkCA,OAAOA,YAAY;;;AC5BnB,SAAkCC,wBAAwB;AAInD,SAASC,0BACdC,UACAC,OACAC,OACAC,WAAkB;AAElBA,4BAAcC,wBAAwBF,KAAAA;AAEtC,QAAMG,SAAgB,CAAA;AACtB,QAAMC,iBAAiBC,iBAAiBL,OAAOD,KAAAA;AAC/C,MAAIK,iBAAiBH,WAAW;AAC9B,UAAM,IAAIK,MAAM,4CAA4CF,oBAAoBH,WAAW;EAC7F;AACAA,eAAaG;AAEb,aAAWG,WAAWT,UAAU;AAC9B,QAAIS,QAAQC,SAAS,UAAU;AAC7B,YAAMC,YAAYC,mBAAmBV,OAAOO,OAAAA;AAC5CN,mBAAaQ;AAEb,UAAIR,YAAY,GAAG;AACjB,cAAM,IAAIK,MAAM,uCAAA;MAClB;IACF;EACF;AAEA,MAAIK,SAAkB;AAEtB,QAAMC,mBAAmB;OAAId;IAAUe,QAAO;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MAAU;MAAU;;AAE5C,QAAIC,YAAY,KAAKf,SAASgB,QAAQ,QAAQ,EAAA,IAAM;AACpD,QAAIC,SAAS,MAAMC,MAAMH,SAAAA,EACtBI,KAAK,CAACC,QAAQA,IAAIC,KAAI,CAAA,EACtBC,MAAM,MAAA;AACL,YAAM,IAAIC,MAAM,yCAAyCR,SAAAA;IAC3D,CAAA;AAGF,QAAID,eAAeU,SAASP,OAAOQ,IAAI,GAAG;AACxC,WAAKvB,gBAAgB;AACrB,WAAKD,aAAa;QAChB;UACEH,MAAM;UACN2B,MAAMR,OAAOQ;UACb1B,aAAa;QACf;;IAEJ,WAAWkB,OAAOQ,SAAS,UAAU;AACnC,WAAKvB,gBAAgB;AACrB,WAAKD,aAAaU,OAAOC,KAAKK,OAAOS,UAAU,EAAEC,IAAI,CAACC,QAAAA;AA/D5D;AAgEQ,YAAIC,WAAWZ,OAAOS,WAAWE,GAAAA;AACjC,YAAI,CAACd,eAAeU,SAASK,SAASJ,IAAI,GAAG;AAC3C,gBAAM,IAAIF,MAAM,yBAAA;QAClB;AACA,eAAO;UACLzB,MAAM8B;UACNH,MAAMI,SAASJ;UACf1B,aAAa8B,SAAS9B,eAAe;UACrC+B,YAAUb,YAAOa,aAAPb,mBAAiBO,SAASI,SAAQ;QAC9C;MACF,CAAA;IACF,OAAO;AACL,YAAM,IAAIL,MAAM,yBAAA;IAClB;EACF;AACF;AArEa5B;","names":["OpenAI","encodingForModel","limitMessagesToTokenCount","messages","tools","model","maxTokens","maxTokensForOpenAIModel","result","toolsNumTokens","countToolsTokens","Error","message","role","numTokens","countMessageTokens","cutoff","reversedMessages","reverse","unshift","maxTokensByModel","DEFAULT_MAX_TOKENS","length","json","JSON","stringify","countTokens","content","text","enc","encodingForModel","e","encode","convertActionInputToOpenAITool","action","type","function","name","description","parameters","parse","jsonSchema","convertMessageToOpenAIMessage","TextMessage","ActionExecutionMessage","tool_calls","id","arguments","ResultMessage","tool_call_id","actionExecutionId","convertSystemMessageToAssistantAPI","DEFAULT_MODEL","OpenAIAdapter","model","_openai","openai","constructor","params","OpenAI","process","messages","actions","eventSource","tools","map","convertActionInputToOpenAITool","openaiMessages","convertMessageToOpenAIMessage","limitMessagesToTokenCount","stream","beta","chat","completions","length","eventStream$","mode","chunk","toolCall","choices","delta","tool_calls","content","id","sendTextMessageEnd","undefined","sendActionExecutionEnd","sendActionExecutionStart","function","name","sendTextMessageStart","sendTextMessageContent","arguments","sendActionExecutionArgs","complete","OpenAI","OpenAIAssistantAdapter","openai","codeInterpreterEnabled","assistantId","fileSearchEnabled","constructor","params","OpenAI","process","messages","actions","eventSource","threadId","runId","beta","threads","create","id","lastMessage","at","nextRunId","undefined","ResultMessage","submitToolOutputs","TextMessage","submitUserMessage","Error","run","runs","retrieve","required_action","toolCallsIds","submit_tool_outputs","tool_calls","map","toolCall","resultMessages","filter","message","includes","actionExecutionId","length","toolOutputs","tool_call_id","output","result","stream","submitToolOutputsStream","tool_outputs","streamResponse","instructionsMessage","shift","instructions","content","userMessage","convertMessageToOpenAIMessage","convertSystemMessageToAssistantAPI","role","openaiTools","convertActionInputToOpenAITool","tools","type","assistant_id","getRunIdFromStream","eventStream$","inFunctionCall","chunk","event","sendActionExecutionEnd","sendTextMessageStart","data","delta","sendTextMessageContent","text","value","sendTextMessageEnd","toolCallId","toolCallName","toolCallArgs","step_details","function","name","arguments","sendActionExecutionStart","sendActionExecutionArgs","complete","Promise","resolve","reject","runIdGetter","off","on","GoogleGenerativeAI","convertMessageToGoogleGenAIMessage","message","TextMessage","role","user","assistant","system","text","content","parts","ActionExecutionMessage","functionCall","name","args","arguments","ResultMessage","functionResponse","actionName","response","tryParseJson","result","transformActionToGoogleGenAITool","action","description","parameters","JSON","parse","jsonSchema","transformProperties","props","key","type","toUpperCase","properties","functionDeclarations","str","e","nanoid","GoogleGenerativeAIAdapter","model","constructor","options","genAI","GoogleGenerativeAI","process","env","getGenerativeModel","request","messages","actions","eventSource","history","slice","map","convertMessageToGoogleGenAIMessage","currentMessage","at","Error","systemMessage","firstMessage","TextMessage","role","content","trim","tools","transformActionToGoogleGenAITool","isFirstGenGeminiPro","chat","startChat","parts","text","systemInstruction","result","sendMessageStream","stream","eventStream$","isTextMessage","chunk","chunkText","sendTextMessageStart","nanoid","sendTextMessageContent","sendTextMessageEnd","calls","response","functionCalls","call","sendActionExecution","name","JSON","stringify","replaceNewlinesInObject","args","complete","obj","replace","Array","isArray","newObj","key","hasOwnProperty","LangChainAdapter","constructor","options","process","eventSource","model","actions","messages","threadId","runId","result","chainFn","map","convertMessageToLangChainMessage","tools","convertActionInputToLangChainTool","stream","eventStream$","streamLangChainResponse","RemoteRunnable","RemoteChain","constructor","options","name","description","chainUrl","parameters","parameterType","toAction","inferLangServeParameters","handler","args","runnable","RemoteRunnable","url","input","Object","keys","invoke","supportedTypes","schemaUrl","replace","schema","fetch","then","res","json","catch","Error","includes","type","properties","map","key","property","required"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/service-adapters/langchain/utils.ts"],"sourcesContent":["import {\n ActionExecutionMessage,\n Message,\n ResultMessage,\n TextMessage,\n} from \"../../graphql/types/converted\";\nimport {\n AIMessage,\n AIMessageChunk,\n BaseMessage,\n BaseMessageChunk,\n HumanMessage,\n SystemMessage,\n ToolMessage,\n} from \"@langchain/core/messages\";\nimport { DynamicStructuredTool } from \"@langchain/core/tools\";\nimport { z } from \"zod\";\nimport { ActionInput } from \"../../graphql/inputs/action.input\";\nimport { LangChainReturnType } from \"./types\";\nimport { RuntimeEventSubject } from \"../events\";\nimport { nanoid } from \"nanoid\";\n\nexport function convertMessageToLangChainMessage(message: Message): BaseMessage {\n if (message instanceof TextMessage) {\n if (message.role == \"user\") {\n return new HumanMessage(message.content);\n } else if (message.role == \"assistant\") {\n return new AIMessage(message.content);\n } else if (message.role === \"system\") {\n return new SystemMessage(message.content);\n }\n } else if (message instanceof ActionExecutionMessage) {\n return new AIMessage({\n content: \"\",\n tool_calls: [\n {\n id: message.id,\n args: message.arguments,\n name: message.name,\n },\n ],\n });\n } else if (message instanceof ResultMessage) {\n return new ToolMessage({\n content: message.result,\n tool_call_id: message.actionExecutionId,\n });\n }\n}\n\nexport function convertJsonSchemaToZodSchema(jsonSchema: any, required: boolean): z.ZodSchema {\n if (jsonSchema.type === \"object\") {\n const spec: { [key: string]: z.ZodSchema } = {};\n for (const [key, value] of Object.entries(jsonSchema.properties)) {\n spec[key] = convertJsonSchemaToZodSchema(\n value,\n jsonSchema.required ? jsonSchema.required.includes(key) : false,\n );\n }\n let schema = z.object(spec);\n return !required ? schema.optional() : schema;\n } else if (jsonSchema.type === \"string\") {\n let schema = z.string().describe(jsonSchema.description);\n return !required ? schema.optional() : schema;\n } else if (jsonSchema.type === \"number\") {\n let schema = z.number().describe(jsonSchema.description);\n return !required ? schema.optional() : schema;\n } else if (jsonSchema.type === \"boolean\") {\n let schema = z.boolean().describe(jsonSchema.description);\n return !required ? schema.optional() : schema;\n } else if (jsonSchema.type === \"array\") {\n let itemSchema = convertJsonSchemaToZodSchema(jsonSchema.items, false);\n let schema = z.array(itemSchema);\n return !required ? schema.optional() : schema;\n }\n}\n\nexport function convertActionInputToLangChainTool(actionInput: ActionInput): DynamicStructuredTool {\n return new DynamicStructuredTool({\n name: actionInput.name,\n description: actionInput.description,\n schema: convertJsonSchemaToZodSchema(\n JSON.parse(actionInput.jsonSchema),\n true,\n ) as z.ZodObject<any>,\n func: async () => {\n return \"\";\n },\n });\n}\n\ninterface StreamLangChainResponseParams {\n result: LangChainReturnType;\n eventStream$: RuntimeEventSubject;\n actionExecution?: {\n id: string;\n name: string;\n };\n}\n\nexport async function streamLangChainResponse({\n result,\n eventStream$,\n actionExecution,\n}: StreamLangChainResponseParams) {\n // We support several types of return values from LangChain functions:\n\n // 1. string\n\n if (typeof result === \"string\") {\n if (!actionExecution) {\n // Just send one chunk with the string as the content.\n eventStream$.sendTextMessage(nanoid(), result);\n } else {\n // Send as a result\n eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, result);\n }\n }\n\n // 2. AIMessage\n // Send the content and function call of the AIMessage as the content of the chunk.\n // else if (\"content\" in result && typeof result.content === \"string\") {\n else if (result instanceof AIMessage) {\n if (result.content) {\n eventStream$.sendTextMessage(nanoid(), result.content as string);\n }\n for (const toolCall of result.tool_calls) {\n eventStream$.sendActionExecution(\n toolCall.id || nanoid(),\n toolCall.name,\n JSON.stringify(toolCall.args),\n );\n }\n }\n\n // 3. BaseMessageChunk\n // Send the content and function call of the AIMessage as the content of the chunk.\n else if (result instanceof BaseMessageChunk) {\n if (result.lc_kwargs?.content) {\n eventStream$.sendTextMessage(nanoid(), result.content as string);\n }\n if (result.lc_kwargs?.tool_calls) {\n for (const toolCall of result.lc_kwargs?.tool_calls) {\n eventStream$.sendActionExecution(\n toolCall.id || nanoid(),\n toolCall.name,\n JSON.stringify(toolCall.args),\n );\n }\n }\n }\n\n // 4. IterableReadableStream\n // Stream the result of the LangChain function.\n else if (\"getReader\" in result) {\n let reader = result.getReader();\n\n let mode: \"function\" | \"message\" | null = null;\n\n while (true) {\n try {\n const { done, value } = await reader.read();\n\n let toolCallName: string | undefined = undefined;\n let toolCallId: string | undefined = undefined;\n let toolCallArgs: string | undefined = undefined;\n let hasToolCall: boolean = false;\n let content = value.content as string;\n\n if (value instanceof AIMessageChunk) {\n let chunk = value.tool_call_chunks?.[0];\n toolCallName = chunk?.name;\n toolCallId = chunk?.id;\n toolCallArgs = chunk?.args;\n hasToolCall = chunk != undefined;\n } else if (value instanceof BaseMessageChunk) {\n let chunk = value.additional_kwargs?.tool_calls?.[0];\n toolCallName = chunk?.function?.name;\n toolCallId = chunk?.id;\n toolCallArgs = chunk?.function?.arguments;\n hasToolCall = chunk?.function != undefined;\n }\n\n // When switching from message to function or vice versa,\n // send the respective end event.\n // If toolCallName is defined, it means a new tool call starts.\n if (mode === \"message\" && (toolCallId || done)) {\n mode = null;\n eventStream$.sendTextMessageEnd();\n } else if (mode === \"function\" && (!hasToolCall || done)) {\n mode = null;\n eventStream$.sendActionExecutionEnd();\n }\n\n if (done) {\n break;\n }\n\n // If we send a new message type, send the appropriate start event.\n if (mode === null) {\n if (hasToolCall) {\n mode = \"function\";\n eventStream$.sendActionExecutionStart(toolCallId, toolCallName);\n } else if (content) {\n mode = \"message\";\n eventStream$.sendTextMessageStart(nanoid());\n }\n }\n\n // send the content events\n if (mode === \"message\" && content) {\n eventStream$.sendTextMessageContent(content);\n } else if (mode === \"function\" && toolCallArgs) {\n eventStream$.sendActionExecutionArgs(toolCallArgs);\n }\n } catch (error) {\n console.error(\"Error reading from stream\", error);\n break;\n }\n }\n } else if (actionExecution) {\n eventStream$.sendActionExecutionResult(\n actionExecution.id,\n actionExecution.name,\n JSON.stringify(result),\n );\n }\n\n // unsupported type\n else {\n throw new Error(\"Invalid return type from LangChain function.\");\n }\n\n eventStream$.complete();\n}\n"],"mappings":";;;;;;;;;;AAMA,SACEA,WACAC,gBAEAC,kBACAC,cACAC,eACAC,mBACK;AACP,SAASC,6BAA6B;AACtC,SAASC,SAAS;AAIlB,SAASC,cAAc;AAEhB,SAASC,iCAAiCC,SAAgB;AAC/D,MAAIA,mBAAmBC,aAAa;AAClC,QAAID,QAAQE,QAAQ,QAAQ;AAC1B,aAAO,IAAIC,aAAaH,QAAQI,OAAO;IACzC,WAAWJ,QAAQE,QAAQ,aAAa;AACtC,aAAO,IAAIG,UAAUL,QAAQI,OAAO;IACtC,WAAWJ,QAAQE,SAAS,UAAU;AACpC,aAAO,IAAII,cAAcN,QAAQI,OAAO;IAC1C;EACF,WAAWJ,mBAAmBO,wBAAwB;AACpD,WAAO,IAAIF,UAAU;MACnBD,SAAS;MACTI,YAAY;QACV;UACEC,IAAIT,QAAQS;UACZC,MAAMV,QAAQW;UACdC,MAAMZ,QAAQY;QAChB;;IAEJ,CAAA;EACF,WAAWZ,mBAAmBa,eAAe;AAC3C,WAAO,IAAIC,YAAY;MACrBV,SAASJ,QAAQe;MACjBC,cAAchB,QAAQiB;IACxB,CA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MessageChunk","HumanMessage","SystemMessage","ToolMessage","DynamicStructuredTool","z","nanoid","convertMessageToLangChainMessage","message","TextMessage","role","HumanMessage","content","AIMessage","SystemMessage","ActionExecutionMessage","tool_calls","id","args","arguments","name","ResultMessage","ToolMessage","result","tool_call_id","actionExecutionId","convertJsonSchemaToZodSchema","jsonSchema","required","type","spec","key","value","Object","entries","properties","includes","schema","z","object","optional","string","describe","description","number","boolean","itemSchema","items","array","convertActionInputToLangChainTool","actionInput","DynamicStructuredTool","JSON","parse","func","streamLangChainResponse","eventStream$","actionExecution","sendTextMessage","nanoid","sendActionExecutionResult","toolCall","sendActionExecution","stringify","BaseMessageChunk","lc_kwargs","reader","getReader","mode","done","read","toolCallName","undefined","toolCallId","toolCallArgs","hasToolCall","AIMessageChunk","chunk","tool_call_chunks","additional_kwargs","function","sendTextMessageEnd","sendActionExecutionEnd","sendActionExecutionStart","sendTextMessageStart","sendTextMessageContent","sendActionExecutionArgs","error","console","Error","complete"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/graphql/types/base/index.ts"],"sourcesContent":["import { Field, InputType } from \"type-graphql\";\n\n@InputType()\nexport class BaseMessage {\n @Field(() => String)\n id: string;\n\n @Field(() => Date)\n createdAt: Date;\n}\n"],"mappings":";;;;AAAA,SAASA,OAAOC,iBAAiB;;;;;;;;;;;;;;;;;AAG1B,IAAMC,cAAN,MAAMA;EAEXC;EAGAC;AACF;AANaF;;EACVG,MAAM,MAAMC,MAAAA;;GADFJ,YAAAA,WAAAA,MAAAA,MAAAA;;EAIVG,MAAM,MAAME,IAAAA;qCACF,SAAA,cAAA,SAAA,IAAA;GALAL,YAAAA,WAAAA,aAAAA,MAAAA;AAAAA,cAAAA,aAAAA;EADZM,UAAAA;GACYN,WAAAA;","names":["Field","InputType","BaseMessage","id","createdAt","Field","String","Date","InputType"]}
|