@copilotkit/runtime 1.6.0 → 1.7.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -0
- package/README.md +75 -0
- package/dist/{chunk-WUMAYJP3.mjs → chunk-D6YNY2XB.mjs} +2 -2
- package/dist/chunk-PTC5JN3P.mjs +1 -0
- package/dist/{chunk-DUW72ZZB.mjs → chunk-QTRO3GPV.mjs} +206 -70
- package/dist/chunk-QTRO3GPV.mjs.map +1 -0
- package/dist/{chunk-MPI4JZZR.mjs → chunk-QZ6X33MR.mjs} +2 -2
- package/dist/{chunk-2RP2NR4F.mjs → chunk-RQS3BGAT.mjs} +2 -2
- package/dist/{copilot-runtime-15bfc4f4.d.ts → copilot-runtime-5103c7e7.d.ts} +66 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +206 -69
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +7 -5
- package/dist/index.mjs.map +1 -1
- package/dist/lib/index.d.ts +1 -1
- package/dist/lib/index.js +206 -69
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +7 -5
- package/dist/lib/integrations/index.d.ts +2 -2
- package/dist/lib/integrations/index.js +4 -1
- package/dist/lib/integrations/index.js.map +1 -1
- package/dist/lib/integrations/index.mjs +4 -4
- package/dist/lib/integrations/nest/index.d.ts +1 -1
- package/dist/lib/integrations/nest/index.js +4 -1
- package/dist/lib/integrations/nest/index.js.map +1 -1
- package/dist/lib/integrations/nest/index.mjs +2 -2
- package/dist/lib/integrations/node-express/index.d.ts +1 -1
- package/dist/lib/integrations/node-express/index.js +4 -1
- package/dist/lib/integrations/node-express/index.js.map +1 -1
- package/dist/lib/integrations/node-express/index.mjs +2 -2
- package/dist/lib/integrations/node-http/index.d.ts +1 -1
- package/dist/lib/integrations/node-http/index.js +4 -1
- package/dist/lib/integrations/node-http/index.js.map +1 -1
- package/dist/lib/integrations/node-http/index.mjs +1 -1
- package/package.json +2 -2
- package/src/graphql/resolvers/copilot.resolver.ts +4 -0
- package/src/lib/index.ts +1 -0
- package/src/lib/logger.ts +48 -0
- package/src/lib/runtime/__tests__/remote-action-constructors.test.ts +45 -35
- package/src/lib/runtime/copilot-runtime.ts +176 -16
- package/src/lib/runtime/remote-action-constructors.ts +28 -68
- package/src/lib/runtime/remote-actions.ts +5 -5
- package/src/lib/streaming.ts +59 -0
- package/src/service-adapters/events.ts +3 -3
- package/dist/chunk-DFOKBSIS.mjs +0 -1
- package/dist/chunk-DUW72ZZB.mjs.map +0 -1
- /package/dist/{chunk-WUMAYJP3.mjs.map → chunk-D6YNY2XB.mjs.map} +0 -0
- /package/dist/{chunk-DFOKBSIS.mjs.map → chunk-PTC5JN3P.mjs.map} +0 -0
- /package/dist/{chunk-MPI4JZZR.mjs.map → chunk-QZ6X33MR.mjs.map} +0 -0
- /package/dist/{chunk-2RP2NR4F.mjs.map → chunk-RQS3BGAT.mjs.map} +0 -0
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
copilotRuntimeNodeHttpEndpoint,
|
|
3
3
|
getRuntimeInstanceTelemetryInfo,
|
|
4
4
|
telemetry_client_default
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-QTRO3GPV.mjs";
|
|
6
6
|
import {
|
|
7
7
|
__name
|
|
8
8
|
} from "./chunk-FHD4JECV.mjs";
|
|
@@ -22,4 +22,4 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
|
|
|
22
22
|
export {
|
|
23
23
|
copilotRuntimeNestEndpoint
|
|
24
24
|
};
|
|
25
|
-
//# sourceMappingURL=chunk-
|
|
25
|
+
//# sourceMappingURL=chunk-QZ6X33MR.mjs.map
|
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
copilotRuntimeNodeHttpEndpoint,
|
|
3
3
|
getRuntimeInstanceTelemetryInfo,
|
|
4
4
|
telemetry_client_default
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-QTRO3GPV.mjs";
|
|
6
6
|
import {
|
|
7
7
|
__name
|
|
8
8
|
} from "./chunk-FHD4JECV.mjs";
|
|
@@ -22,4 +22,4 @@ __name(copilotRuntimeNodeExpressEndpoint, "copilotRuntimeNodeExpressEndpoint");
|
|
|
22
22
|
export {
|
|
23
23
|
copilotRuntimeNodeExpressEndpoint
|
|
24
24
|
};
|
|
25
|
-
//# sourceMappingURL=chunk-
|
|
25
|
+
//# sourceMappingURL=chunk-RQS3BGAT.mjs.map
|
|
@@ -7,6 +7,53 @@ import { YogaInitialContext, createYoga } from 'graphql-yoga';
|
|
|
7
7
|
import { CopilotCloudOptions } from './lib/cloud/index.js';
|
|
8
8
|
|
|
9
9
|
type LogLevel = "debug" | "info" | "warn" | "error";
|
|
10
|
+
type CopilotRuntimeLogger = ReturnType<typeof createLogger>;
|
|
11
|
+
declare function createLogger(options?: {
|
|
12
|
+
level?: LogLevel;
|
|
13
|
+
component?: string;
|
|
14
|
+
}): pino.Logger<never>;
|
|
15
|
+
interface LogLLMRequestData {
|
|
16
|
+
threadId?: string;
|
|
17
|
+
runId?: string;
|
|
18
|
+
model?: string;
|
|
19
|
+
messages: any[];
|
|
20
|
+
actions?: any[];
|
|
21
|
+
forwardedParameters?: any;
|
|
22
|
+
timestamp: number;
|
|
23
|
+
provider?: string;
|
|
24
|
+
[key: string]: any;
|
|
25
|
+
}
|
|
26
|
+
interface LogLLMResponseData {
|
|
27
|
+
threadId: string;
|
|
28
|
+
runId?: string;
|
|
29
|
+
model?: string;
|
|
30
|
+
output: any;
|
|
31
|
+
latency: number;
|
|
32
|
+
timestamp: number;
|
|
33
|
+
provider?: string;
|
|
34
|
+
isProgressiveChunk?: boolean;
|
|
35
|
+
isFinalResponse?: boolean;
|
|
36
|
+
[key: string]: any;
|
|
37
|
+
}
|
|
38
|
+
interface LogLLMErrorData {
|
|
39
|
+
threadId?: string;
|
|
40
|
+
runId?: string;
|
|
41
|
+
model?: string;
|
|
42
|
+
error: Error | string;
|
|
43
|
+
timestamp: number;
|
|
44
|
+
provider?: string;
|
|
45
|
+
[key: string]: any;
|
|
46
|
+
}
|
|
47
|
+
interface CopilotLoggerHooks {
|
|
48
|
+
logRequest: (data: LogLLMRequestData) => void | Promise<void>;
|
|
49
|
+
logResponse: (data: LogLLMResponseData) => void | Promise<void>;
|
|
50
|
+
logError: (data: LogLLMErrorData) => void | Promise<void>;
|
|
51
|
+
}
|
|
52
|
+
interface CopilotLoggingConfig {
|
|
53
|
+
enabled: boolean;
|
|
54
|
+
progressive: boolean;
|
|
55
|
+
logger: CopilotLoggerHooks;
|
|
56
|
+
}
|
|
10
57
|
|
|
11
58
|
declare const logger: pino.Logger<never>;
|
|
12
59
|
declare const addCustomHeaderPlugin: {
|
|
@@ -195,6 +242,22 @@ interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []> {
|
|
|
195
242
|
remoteEndpoints?: EndpointDefinition[];
|
|
196
243
|
langserve?: RemoteChainParameters[];
|
|
197
244
|
delegateAgentProcessingToServiceAdapter?: boolean;
|
|
245
|
+
/**
|
|
246
|
+
* Configuration for LLM request/response logging
|
|
247
|
+
*
|
|
248
|
+
* ```ts
|
|
249
|
+
* logging: {
|
|
250
|
+
* enabled: true, // Enable or disable logging
|
|
251
|
+
* progressive: true, // Set to false for buffered logging
|
|
252
|
+
* logger: {
|
|
253
|
+
* logRequest: (data) => langfuse.trace({ name: "LLM Request", input: data }),
|
|
254
|
+
* logResponse: (data) => langfuse.trace({ name: "LLM Response", output: data }),
|
|
255
|
+
* logError: (errorData) => langfuse.trace({ name: "LLM Error", metadata: errorData }),
|
|
256
|
+
* },
|
|
257
|
+
* }
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
logging?: CopilotLoggingConfig;
|
|
198
261
|
}
|
|
199
262
|
declare class CopilotRuntime<const T extends Parameter[] | [] = []> {
|
|
200
263
|
actions: ActionsConfiguration<T>;
|
|
@@ -203,16 +266,18 @@ declare class CopilotRuntime<const T extends Parameter[] | [] = []> {
|
|
|
203
266
|
private onBeforeRequest?;
|
|
204
267
|
private onAfterRequest?;
|
|
205
268
|
private delegateAgentProcessingToServiceAdapter;
|
|
269
|
+
private logging?;
|
|
206
270
|
constructor(params?: CopilotRuntimeConstructorParams<T>);
|
|
207
271
|
processRuntimeRequest(request: CopilotRuntimeRequest): Promise<CopilotRuntimeResponse>;
|
|
208
272
|
discoverAgentsFromEndpoints(graphqlContext: GraphQLContext): Promise<AgentWithEndpoint[]>;
|
|
209
273
|
loadAgentState(graphqlContext: GraphQLContext, threadId: string, agentName: string): Promise<LoadAgentStateResponse>;
|
|
210
274
|
private processAgentRequest;
|
|
211
275
|
private getServerSideActions;
|
|
276
|
+
private detectProvider;
|
|
212
277
|
}
|
|
213
278
|
declare function flattenToolCallsNoDuplicates(toolsByPriority: ActionInput[]): ActionInput[];
|
|
214
279
|
declare function copilotKitEndpoint(config: Omit<CopilotKitEndpoint, "type">): CopilotKitEndpoint;
|
|
215
280
|
declare function langGraphPlatformEndpoint(config: Omit<LangGraphPlatformEndpoint, "type">): LangGraphPlatformEndpoint;
|
|
216
281
|
declare function resolveEndpointType(endpoint: EndpointDefinition): EndpointType;
|
|
217
282
|
|
|
218
|
-
export { CopilotRuntimeConstructorParams as C, GraphQLContext as G, CopilotRuntime as a, addCustomHeaderPlugin as b, copilotKitEndpoint as c, CopilotRequestContextProperties as d, CreateCopilotRuntimeServerOptions as e, flattenToolCallsNoDuplicates as f, createContext as g, buildSchema as h, CommonConfig as i, getCommonConfig as j, langGraphPlatformEndpoint as l, resolveEndpointType as r };
|
|
283
|
+
export { CopilotRuntimeConstructorParams as C, GraphQLContext as G, LogLevel as L, CopilotRuntime as a, addCustomHeaderPlugin as b, copilotKitEndpoint as c, CopilotRequestContextProperties as d, CreateCopilotRuntimeServerOptions as e, flattenToolCallsNoDuplicates as f, createContext as g, buildSchema as h, CommonConfig as i, getCommonConfig as j, CopilotRuntimeLogger as k, langGraphPlatformEndpoint as l, createLogger as m, LogLLMRequestData as n, LogLLMResponseData as o, LogLLMErrorData as p, CopilotLoggerHooks as q, resolveEndpointType as r, CopilotLoggingConfig as s };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { i as CommonConfig, d as CopilotRequestContextProperties, a as CopilotRuntime, C as CopilotRuntimeConstructorParams, e as CreateCopilotRuntimeServerOptions, G as GraphQLContext, b as addCustomHeaderPlugin, h as buildSchema, c as copilotKitEndpoint, g as createContext, f as flattenToolCallsNoDuplicates, j as getCommonConfig, l as langGraphPlatformEndpoint, r as resolveEndpointType } from './copilot-runtime-
|
|
1
|
+
export { i as CommonConfig, q as CopilotLoggerHooks, s as CopilotLoggingConfig, d as CopilotRequestContextProperties, a as CopilotRuntime, C as CopilotRuntimeConstructorParams, k as CopilotRuntimeLogger, e as CreateCopilotRuntimeServerOptions, G as GraphQLContext, p as LogLLMErrorData, n as LogLLMRequestData, o as LogLLMResponseData, L as LogLevel, b as addCustomHeaderPlugin, h as buildSchema, c as copilotKitEndpoint, g as createContext, m as createLogger, f as flattenToolCallsNoDuplicates, j as getCommonConfig, l as langGraphPlatformEndpoint, r as resolveEndpointType } from './copilot-runtime-5103c7e7.js';
|
|
2
2
|
export { G as GoogleGenerativeAIAdapter, f as GroqAdapter, e as GroqAdapterParams, L as LangChainAdapter, a as OpenAIAdapter, O as OpenAIAdapterParams, c as OpenAIAssistantAdapter, b as OpenAIAssistantAdapterParams, d as UnifyAdapter, U as UnifyAdapterParams } from './groq-adapter-fb9aa3ab.js';
|
|
3
3
|
export { CopilotRuntimeServerInstance, config, copilotRuntimeNextJSAppRouterEndpoint, copilotRuntimeNextJSPagesRouterEndpoint } from './lib/integrations/index.js';
|
|
4
4
|
export { copilotRuntimeNodeHttpEndpoint } from './lib/integrations/node-http/index.js';
|
package/dist/index.js
CHANGED
|
@@ -44,7 +44,7 @@ var require_package = __commonJS({
|
|
|
44
44
|
publishConfig: {
|
|
45
45
|
access: "public"
|
|
46
46
|
},
|
|
47
|
-
version: "1.
|
|
47
|
+
version: "1.7.0-next.1",
|
|
48
48
|
sideEffects: false,
|
|
49
49
|
main: "./dist/index.js",
|
|
50
50
|
module: "./dist/index.mjs",
|
|
@@ -150,6 +150,7 @@ __export(src_exports, {
|
|
|
150
150
|
copilotRuntimeNodeExpressEndpoint: () => copilotRuntimeNodeExpressEndpoint,
|
|
151
151
|
copilotRuntimeNodeHttpEndpoint: () => copilotRuntimeNodeHttpEndpoint,
|
|
152
152
|
createContext: () => createContext,
|
|
153
|
+
createLogger: () => createLogger,
|
|
153
154
|
flattenToolCallsNoDuplicates: () => flattenToolCallsNoDuplicates,
|
|
154
155
|
getCommonConfig: () => getCommonConfig,
|
|
155
156
|
langGraphPlatformEndpoint: () => langGraphPlatformEndpoint,
|
|
@@ -2637,7 +2638,7 @@ async function streamEvents(controller, args) {
|
|
|
2637
2638
|
let emitIntermediateStateUntilEnd = null;
|
|
2638
2639
|
let shouldExit = false;
|
|
2639
2640
|
let externalRunId = null;
|
|
2640
|
-
const
|
|
2641
|
+
const streamResponse = client.runs.stream(threadId, assistantId, payload);
|
|
2641
2642
|
const emit = /* @__PURE__ */ __name((message) => controller.enqueue(new TextEncoder().encode(message)), "emit");
|
|
2642
2643
|
let latestStateValues = {};
|
|
2643
2644
|
let updatedState = state;
|
|
@@ -2647,7 +2648,7 @@ async function streamEvents(controller, args) {
|
|
|
2647
2648
|
telemetry_client_default.capture("oss.runtime.agent_execution_stream_started", {
|
|
2648
2649
|
hashedLgcKey: streamInfo.hashedLgcKey
|
|
2649
2650
|
});
|
|
2650
|
-
for await (const chunk of
|
|
2651
|
+
for await (const chunk of streamResponse) {
|
|
2651
2652
|
if (![
|
|
2652
2653
|
"events",
|
|
2653
2654
|
"values",
|
|
@@ -3062,6 +3063,54 @@ __name(getSchemaKeys, "getSchemaKeys");
|
|
|
3062
3063
|
|
|
3063
3064
|
// src/lib/runtime/remote-action-constructors.ts
|
|
3064
3065
|
var import_shared13 = require("@copilotkit/shared");
|
|
3066
|
+
|
|
3067
|
+
// src/lib/streaming.ts
|
|
3068
|
+
async function writeJsonLineResponseToEventStream(response, eventStream$) {
|
|
3069
|
+
const reader = response.getReader();
|
|
3070
|
+
const decoder = new TextDecoder();
|
|
3071
|
+
let buffer = [];
|
|
3072
|
+
function flushBuffer() {
|
|
3073
|
+
const currentBuffer = buffer.join("");
|
|
3074
|
+
if (currentBuffer.trim().length === 0) {
|
|
3075
|
+
return;
|
|
3076
|
+
}
|
|
3077
|
+
const parts = currentBuffer.split("\n");
|
|
3078
|
+
if (parts.length === 0) {
|
|
3079
|
+
return;
|
|
3080
|
+
}
|
|
3081
|
+
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
3082
|
+
buffer = [];
|
|
3083
|
+
if (!lastPartIsComplete) {
|
|
3084
|
+
buffer.push(parts.pop());
|
|
3085
|
+
}
|
|
3086
|
+
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
3087
|
+
eventStream$.next(JSON.parse(part));
|
|
3088
|
+
});
|
|
3089
|
+
}
|
|
3090
|
+
__name(flushBuffer, "flushBuffer");
|
|
3091
|
+
try {
|
|
3092
|
+
while (true) {
|
|
3093
|
+
const { done, value } = await reader.read();
|
|
3094
|
+
if (!done) {
|
|
3095
|
+
buffer.push(decoder.decode(value, {
|
|
3096
|
+
stream: true
|
|
3097
|
+
}));
|
|
3098
|
+
}
|
|
3099
|
+
flushBuffer();
|
|
3100
|
+
if (done) {
|
|
3101
|
+
break;
|
|
3102
|
+
}
|
|
3103
|
+
}
|
|
3104
|
+
} catch (error) {
|
|
3105
|
+
console.error("Error in stream", error);
|
|
3106
|
+
eventStream$.error(error);
|
|
3107
|
+
return;
|
|
3108
|
+
}
|
|
3109
|
+
eventStream$.complete();
|
|
3110
|
+
}
|
|
3111
|
+
__name(writeJsonLineResponseToEventStream, "writeJsonLineResponseToEventStream");
|
|
3112
|
+
|
|
3113
|
+
// src/lib/runtime/remote-action-constructors.ts
|
|
3065
3114
|
var import_shared14 = require("@copilotkit/shared");
|
|
3066
3115
|
var import_shared15 = require("@copilotkit/shared");
|
|
3067
3116
|
function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
@@ -3071,7 +3120,7 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
|
|
|
3071
3120
|
parameters: [],
|
|
3072
3121
|
handler: async (_args) => {
|
|
3073
3122
|
},
|
|
3074
|
-
|
|
3123
|
+
remoteAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [], metaEvents }) => {
|
|
3075
3124
|
logger2.debug({
|
|
3076
3125
|
actionName: agent.name
|
|
3077
3126
|
}, "Executing LangGraph Platform agent");
|
|
@@ -3115,7 +3164,7 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
|
|
|
3115
3164
|
metaEvents
|
|
3116
3165
|
});
|
|
3117
3166
|
const eventSource = new RemoteLangGraphEventSource();
|
|
3118
|
-
|
|
3167
|
+
writeJsonLineResponseToEventStream(response, eventSource.eventStream$);
|
|
3119
3168
|
return eventSource.processLangGraphEvents();
|
|
3120
3169
|
} catch (error) {
|
|
3121
3170
|
logger2.error({
|
|
@@ -3132,6 +3181,11 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
|
|
|
3132
3181
|
];
|
|
3133
3182
|
}
|
|
3134
3183
|
__name(constructLGCRemoteAction, "constructLGCRemoteAction");
|
|
3184
|
+
var RemoteAgentType;
|
|
3185
|
+
(function(RemoteAgentType2) {
|
|
3186
|
+
RemoteAgentType2["LangGraph"] = "langgraph";
|
|
3187
|
+
RemoteAgentType2["CrewAI"] = "crewai";
|
|
3188
|
+
})(RemoteAgentType || (RemoteAgentType = {}));
|
|
3135
3189
|
function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
3136
3190
|
const totalAgents = Array.isArray(json["agents"]) ? json["agents"].length : 0;
|
|
3137
3191
|
const actions = json["actions"].map((action) => ({
|
|
@@ -3201,7 +3255,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
3201
3255
|
parameters: [],
|
|
3202
3256
|
handler: async (_args) => {
|
|
3203
3257
|
},
|
|
3204
|
-
|
|
3258
|
+
remoteAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [], metaEvents }) => {
|
|
3205
3259
|
logger2.debug({
|
|
3206
3260
|
actionName: agent.name
|
|
3207
3261
|
}, "Executing remote agent");
|
|
@@ -3261,9 +3315,17 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
3261
3315
|
isRemoteEndpoint: true
|
|
3262
3316
|
});
|
|
3263
3317
|
}
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3318
|
+
if (agent.type === "langgraph") {
|
|
3319
|
+
const eventSource = new RemoteLangGraphEventSource();
|
|
3320
|
+
writeJsonLineResponseToEventStream(response.body, eventSource.eventStream$);
|
|
3321
|
+
return eventSource.processLangGraphEvents();
|
|
3322
|
+
} else if (agent.type === "crewai") {
|
|
3323
|
+
const eventStream$ = new RuntimeEventSubject();
|
|
3324
|
+
writeJsonLineResponseToEventStream(response.body, eventStream$);
|
|
3325
|
+
return eventStream$;
|
|
3326
|
+
} else {
|
|
3327
|
+
throw new Error("Unsupported agent type");
|
|
3328
|
+
}
|
|
3267
3329
|
} catch (error) {
|
|
3268
3330
|
if (error instanceof import_shared13.CopilotKitError) {
|
|
3269
3331
|
throw error;
|
|
@@ -3281,50 +3343,6 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
3281
3343
|
];
|
|
3282
3344
|
}
|
|
3283
3345
|
__name(constructRemoteActions, "constructRemoteActions");
|
|
3284
|
-
async function streamResponse(response, eventStream$) {
|
|
3285
|
-
const reader = response.getReader();
|
|
3286
|
-
const decoder = new TextDecoder();
|
|
3287
|
-
let buffer = [];
|
|
3288
|
-
function flushBuffer() {
|
|
3289
|
-
const currentBuffer = buffer.join("");
|
|
3290
|
-
if (currentBuffer.trim().length === 0) {
|
|
3291
|
-
return;
|
|
3292
|
-
}
|
|
3293
|
-
const parts = currentBuffer.split("\n");
|
|
3294
|
-
if (parts.length === 0) {
|
|
3295
|
-
return;
|
|
3296
|
-
}
|
|
3297
|
-
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
3298
|
-
buffer = [];
|
|
3299
|
-
if (!lastPartIsComplete) {
|
|
3300
|
-
buffer.push(parts.pop());
|
|
3301
|
-
}
|
|
3302
|
-
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
3303
|
-
eventStream$.next(JSON.parse(part));
|
|
3304
|
-
});
|
|
3305
|
-
}
|
|
3306
|
-
__name(flushBuffer, "flushBuffer");
|
|
3307
|
-
try {
|
|
3308
|
-
while (true) {
|
|
3309
|
-
const { done, value } = await reader.read();
|
|
3310
|
-
if (!done) {
|
|
3311
|
-
buffer.push(decoder.decode(value, {
|
|
3312
|
-
stream: true
|
|
3313
|
-
}));
|
|
3314
|
-
}
|
|
3315
|
-
flushBuffer();
|
|
3316
|
-
if (done) {
|
|
3317
|
-
break;
|
|
3318
|
-
}
|
|
3319
|
-
}
|
|
3320
|
-
} catch (error) {
|
|
3321
|
-
console.error("Error in stream", error);
|
|
3322
|
-
eventStream$.error(error);
|
|
3323
|
-
return;
|
|
3324
|
-
}
|
|
3325
|
-
eventStream$.complete();
|
|
3326
|
-
}
|
|
3327
|
-
__name(streamResponse, "streamResponse");
|
|
3328
3346
|
function createHeaders(onBeforeRequest, graphqlContext) {
|
|
3329
3347
|
const headers = {
|
|
3330
3348
|
"Content-Type": "application/json"
|
|
@@ -3348,13 +3366,13 @@ var EndpointType;
|
|
|
3348
3366
|
EndpointType2["CopilotKit"] = "copilotKit";
|
|
3349
3367
|
EndpointType2["LangGraphPlatform"] = "langgraph-platform";
|
|
3350
3368
|
})(EndpointType || (EndpointType = {}));
|
|
3351
|
-
function
|
|
3369
|
+
function isRemoteAgentAction(action) {
|
|
3352
3370
|
if (!action) {
|
|
3353
3371
|
return false;
|
|
3354
3372
|
}
|
|
3355
|
-
return typeof action.
|
|
3373
|
+
return typeof action.remoteAgentHandler === "function";
|
|
3356
3374
|
}
|
|
3357
|
-
__name(
|
|
3375
|
+
__name(isRemoteAgentAction, "isRemoteAgentAction");
|
|
3358
3376
|
async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
|
|
3359
3377
|
logger2.debug({
|
|
3360
3378
|
url
|
|
@@ -3858,7 +3876,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
|
|
|
3858
3876
|
return;
|
|
3859
3877
|
}
|
|
3860
3878
|
}
|
|
3861
|
-
if (
|
|
3879
|
+
if (isRemoteAgentAction(action)) {
|
|
3862
3880
|
const result = `${action.name} agent started`;
|
|
3863
3881
|
const agentExecution = (0, import_class_transformer.plainToInstance)(ActionExecutionMessage, {
|
|
3864
3882
|
id: actionExecutionId,
|
|
@@ -3879,7 +3897,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
|
|
|
3879
3897
|
actionName: action.name,
|
|
3880
3898
|
result
|
|
3881
3899
|
});
|
|
3882
|
-
const stream = await action.
|
|
3900
|
+
const stream = await action.remoteAgentHandler({
|
|
3883
3901
|
name: action.name,
|
|
3884
3902
|
threadId,
|
|
3885
3903
|
actionInputsWithoutAgents,
|
|
@@ -3991,6 +4009,7 @@ var CopilotRuntime = class {
|
|
|
3991
4009
|
onBeforeRequest;
|
|
3992
4010
|
onAfterRequest;
|
|
3993
4011
|
delegateAgentProcessingToServiceAdapter;
|
|
4012
|
+
logging;
|
|
3994
4013
|
constructor(params) {
|
|
3995
4014
|
var _a, _b;
|
|
3996
4015
|
if ((params == null ? void 0 : params.actions) && (params == null ? void 0 : params.remoteEndpoints)) {
|
|
@@ -4007,11 +4026,14 @@ var CopilotRuntime = class {
|
|
|
4007
4026
|
this.onBeforeRequest = (_a = params == null ? void 0 : params.middleware) == null ? void 0 : _a.onBeforeRequest;
|
|
4008
4027
|
this.onAfterRequest = (_b = params == null ? void 0 : params.middleware) == null ? void 0 : _b.onAfterRequest;
|
|
4009
4028
|
this.delegateAgentProcessingToServiceAdapter = (params == null ? void 0 : params.delegateAgentProcessingToServiceAdapter) || false;
|
|
4029
|
+
this.logging = params == null ? void 0 : params.logging;
|
|
4010
4030
|
}
|
|
4011
4031
|
async processRuntimeRequest(request) {
|
|
4012
|
-
var _a;
|
|
4032
|
+
var _a, _b, _c, _d, _e;
|
|
4013
4033
|
const { serviceAdapter, messages: rawMessages, actions: clientSideActionsInput, threadId, runId, outputMessagesPromise, graphqlContext, forwardedParameters, url, extensions, agentSession, agentStates } = request;
|
|
4014
4034
|
const eventSource = new RuntimeEventSource();
|
|
4035
|
+
const requestStartTime = Date.now();
|
|
4036
|
+
const streamedChunks = [];
|
|
4015
4037
|
try {
|
|
4016
4038
|
if (agentSession && !this.delegateAgentProcessingToServiceAdapter) {
|
|
4017
4039
|
return await this.processAgentRequest(request);
|
|
@@ -4026,6 +4048,23 @@ please use an LLM adapter instead.`
|
|
|
4026
4048
|
const messages = rawMessages.filter((message) => !message.agentStateMessage);
|
|
4027
4049
|
const inputMessages = convertGqlInputToMessages(messages);
|
|
4028
4050
|
const serverSideActions = await this.getServerSideActions(request);
|
|
4051
|
+
if ((_a = this.logging) == null ? void 0 : _a.enabled) {
|
|
4052
|
+
try {
|
|
4053
|
+
const requestData = {
|
|
4054
|
+
threadId,
|
|
4055
|
+
runId,
|
|
4056
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
4057
|
+
messages: inputMessages,
|
|
4058
|
+
actions: clientSideActionsInput,
|
|
4059
|
+
forwardedParameters,
|
|
4060
|
+
timestamp: requestStartTime,
|
|
4061
|
+
provider: this.detectProvider(serviceAdapter)
|
|
4062
|
+
};
|
|
4063
|
+
await this.logging.logger.logRequest(requestData);
|
|
4064
|
+
} catch (error) {
|
|
4065
|
+
console.error("Error logging LLM request:", error);
|
|
4066
|
+
}
|
|
4067
|
+
}
|
|
4029
4068
|
const serverSideActionsInput = serverSideActions.map((action) => ({
|
|
4030
4069
|
name: action.name,
|
|
4031
4070
|
description: action.description,
|
|
@@ -4038,7 +4077,7 @@ please use an LLM adapter instead.`
|
|
|
4038
4077
|
(action) => action.available !== ActionInputAvailability.remote
|
|
4039
4078
|
)
|
|
4040
4079
|
]);
|
|
4041
|
-
await ((
|
|
4080
|
+
await ((_b = this.onBeforeRequest) == null ? void 0 : _b.call(this, {
|
|
4042
4081
|
threadId,
|
|
4043
4082
|
runId,
|
|
4044
4083
|
inputMessages,
|
|
@@ -4069,6 +4108,69 @@ please use an LLM adapter instead.`
|
|
|
4069
4108
|
});
|
|
4070
4109
|
}).catch((_error) => {
|
|
4071
4110
|
});
|
|
4111
|
+
if ((_c = this.logging) == null ? void 0 : _c.enabled) {
|
|
4112
|
+
try {
|
|
4113
|
+
outputMessagesPromise.then((outputMessages) => {
|
|
4114
|
+
var _a2;
|
|
4115
|
+
const responseData = {
|
|
4116
|
+
threadId: result.threadId,
|
|
4117
|
+
runId: result.runId,
|
|
4118
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
4119
|
+
// Use collected chunks for progressive mode or outputMessages for regular mode
|
|
4120
|
+
output: this.logging.progressive ? streamedChunks : outputMessages,
|
|
4121
|
+
latency: Date.now() - requestStartTime,
|
|
4122
|
+
timestamp: Date.now(),
|
|
4123
|
+
provider: this.detectProvider(serviceAdapter),
|
|
4124
|
+
// Indicate this is the final response
|
|
4125
|
+
isFinalResponse: true
|
|
4126
|
+
};
|
|
4127
|
+
try {
|
|
4128
|
+
(_a2 = this.logging) == null ? void 0 : _a2.logger.logResponse(responseData);
|
|
4129
|
+
} catch (logError) {
|
|
4130
|
+
console.error("Error logging LLM response:", logError);
|
|
4131
|
+
}
|
|
4132
|
+
}).catch((error) => {
|
|
4133
|
+
console.error("Failed to get output messages for logging:", error);
|
|
4134
|
+
});
|
|
4135
|
+
} catch (error) {
|
|
4136
|
+
console.error("Error setting up logging for LLM response:", error);
|
|
4137
|
+
}
|
|
4138
|
+
}
|
|
4139
|
+
if (((_d = this.logging) == null ? void 0 : _d.enabled) && this.logging.progressive) {
|
|
4140
|
+
const originalStream = eventSource.stream.bind(eventSource);
|
|
4141
|
+
eventSource.stream = async (callback) => {
|
|
4142
|
+
await originalStream(async (eventStream$) => {
|
|
4143
|
+
eventStream$.subscribe({
|
|
4144
|
+
next: (event) => {
|
|
4145
|
+
if (event.type === RuntimeEventTypes.TextMessageContent) {
|
|
4146
|
+
streamedChunks.push(event.content);
|
|
4147
|
+
try {
|
|
4148
|
+
const progressiveData = {
|
|
4149
|
+
threadId: threadId || "",
|
|
4150
|
+
runId,
|
|
4151
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
4152
|
+
output: event.content,
|
|
4153
|
+
latency: Date.now() - requestStartTime,
|
|
4154
|
+
timestamp: Date.now(),
|
|
4155
|
+
provider: this.detectProvider(serviceAdapter),
|
|
4156
|
+
isProgressiveChunk: true
|
|
4157
|
+
};
|
|
4158
|
+
Promise.resolve().then(() => {
|
|
4159
|
+
var _a2;
|
|
4160
|
+
(_a2 = this.logging) == null ? void 0 : _a2.logger.logResponse(progressiveData);
|
|
4161
|
+
}).catch((error) => {
|
|
4162
|
+
console.error("Error in progressive logging:", error);
|
|
4163
|
+
});
|
|
4164
|
+
} catch (error) {
|
|
4165
|
+
console.error("Error preparing progressive log data:", error);
|
|
4166
|
+
}
|
|
4167
|
+
}
|
|
4168
|
+
}
|
|
4169
|
+
});
|
|
4170
|
+
await callback(eventStream$);
|
|
4171
|
+
});
|
|
4172
|
+
};
|
|
4173
|
+
}
|
|
4072
4174
|
return {
|
|
4073
4175
|
threadId: nonEmptyThreadId,
|
|
4074
4176
|
runId: result.runId,
|
|
@@ -4081,6 +4183,22 @@ please use an LLM adapter instead.`
|
|
|
4081
4183
|
extensions: result.extensions
|
|
4082
4184
|
};
|
|
4083
4185
|
} catch (error) {
|
|
4186
|
+
if ((_e = this.logging) == null ? void 0 : _e.enabled) {
|
|
4187
|
+
try {
|
|
4188
|
+
const errorData = {
|
|
4189
|
+
threadId,
|
|
4190
|
+
runId,
|
|
4191
|
+
model: forwardedParameters == null ? void 0 : forwardedParameters.model,
|
|
4192
|
+
error: error instanceof Error ? error : String(error),
|
|
4193
|
+
timestamp: Date.now(),
|
|
4194
|
+
latency: Date.now() - requestStartTime,
|
|
4195
|
+
provider: this.detectProvider(serviceAdapter)
|
|
4196
|
+
};
|
|
4197
|
+
await this.logging.logger.logError(errorData);
|
|
4198
|
+
} catch (logError) {
|
|
4199
|
+
console.error("Error logging LLM error:", logError);
|
|
4200
|
+
}
|
|
4201
|
+
}
|
|
4084
4202
|
if (error instanceof import_shared20.CopilotKitError) {
|
|
4085
4203
|
throw error;
|
|
4086
4204
|
}
|
|
@@ -4090,7 +4208,6 @@ please use an LLM adapter instead.`
|
|
|
4090
4208
|
}
|
|
4091
4209
|
}
|
|
4092
4210
|
async discoverAgentsFromEndpoints(graphqlContext) {
|
|
4093
|
-
const headers = createHeaders(null, graphqlContext);
|
|
4094
4211
|
const agents = this.remoteEndpointDefinitions.reduce(async (acc, endpoint) => {
|
|
4095
4212
|
const agents2 = await acc;
|
|
4096
4213
|
if (endpoint.type === EndpointType.LangGraphPlatform) {
|
|
@@ -4116,11 +4233,12 @@ please use an LLM adapter instead.`
|
|
|
4116
4233
|
...endpointAgents
|
|
4117
4234
|
];
|
|
4118
4235
|
}
|
|
4236
|
+
const cpkEndpoint = endpoint;
|
|
4119
4237
|
const fetchUrl = `${endpoint.url}/info`;
|
|
4120
4238
|
try {
|
|
4121
4239
|
const response = await fetch(fetchUrl, {
|
|
4122
4240
|
method: "POST",
|
|
4123
|
-
headers,
|
|
4241
|
+
headers: createHeaders(cpkEndpoint.onBeforeRequest, graphqlContext),
|
|
4124
4242
|
body: JSON.stringify({
|
|
4125
4243
|
properties: graphqlContext.properties
|
|
4126
4244
|
})
|
|
@@ -4166,7 +4284,6 @@ please use an LLM adapter instead.`
|
|
|
4166
4284
|
if (!agentWithEndpoint) {
|
|
4167
4285
|
throw new Error("Agent not found");
|
|
4168
4286
|
}
|
|
4169
|
-
const headers = createHeaders(null, graphqlContext);
|
|
4170
4287
|
if (agentWithEndpoint.endpoint.type === EndpointType.LangGraphPlatform) {
|
|
4171
4288
|
const propertyHeaders = graphqlContext.properties.authorization ? {
|
|
4172
4289
|
authorization: `Bearer ${graphqlContext.properties.authorization}`
|
|
@@ -4201,11 +4318,12 @@ please use an LLM adapter instead.`
|
|
|
4201
4318
|
};
|
|
4202
4319
|
}
|
|
4203
4320
|
} else if (agentWithEndpoint.endpoint.type === EndpointType.CopilotKit || !("type" in agentWithEndpoint.endpoint)) {
|
|
4204
|
-
const
|
|
4321
|
+
const cpkEndpoint = agentWithEndpoint.endpoint;
|
|
4322
|
+
const fetchUrl = `${cpkEndpoint.url}/agents/state`;
|
|
4205
4323
|
try {
|
|
4206
4324
|
const response = await fetch(fetchUrl, {
|
|
4207
4325
|
method: "POST",
|
|
4208
|
-
headers,
|
|
4326
|
+
headers: createHeaders(cpkEndpoint.onBeforeRequest, graphqlContext),
|
|
4209
4327
|
body: JSON.stringify({
|
|
4210
4328
|
properties: graphqlContext.properties,
|
|
4211
4329
|
threadId,
|
|
@@ -4250,7 +4368,7 @@ please use an LLM adapter instead.`
|
|
|
4250
4368
|
const threadId = threadIdFromRequest ?? agentSession.threadId;
|
|
4251
4369
|
const serverSideActions = await this.getServerSideActions(request);
|
|
4252
4370
|
const messages = convertGqlInputToMessages(rawMessages);
|
|
4253
|
-
const currentAgent = serverSideActions.find((action) => action.name === agentName &&
|
|
4371
|
+
const currentAgent = serverSideActions.find((action) => action.name === agentName && isRemoteAgentAction(action));
|
|
4254
4372
|
if (!currentAgent) {
|
|
4255
4373
|
throw new import_shared20.CopilotKitAgentDiscoveryError({
|
|
4256
4374
|
agentName
|
|
@@ -4258,8 +4376,8 @@ please use an LLM adapter instead.`
|
|
|
4258
4376
|
}
|
|
4259
4377
|
const availableActionsForCurrentAgent = serverSideActions.filter((action) => (
|
|
4260
4378
|
// Case 1: Keep all regular (non-agent) actions
|
|
4261
|
-
!
|
|
4262
|
-
|
|
4379
|
+
!isRemoteAgentAction(action) || // Case 2: For agent actions, keep all except self (prevent infinite loops)
|
|
4380
|
+
isRemoteAgentAction(action) && action.name !== agentName
|
|
4263
4381
|
)).map((action) => ({
|
|
4264
4382
|
name: action.name,
|
|
4265
4383
|
description: action.description,
|
|
@@ -4277,7 +4395,7 @@ please use an LLM adapter instead.`
|
|
|
4277
4395
|
}));
|
|
4278
4396
|
try {
|
|
4279
4397
|
const eventSource = new RuntimeEventSource();
|
|
4280
|
-
const stream = await currentAgent.
|
|
4398
|
+
const stream = await currentAgent.remoteAgentHandler({
|
|
4281
4399
|
name: agentName,
|
|
4282
4400
|
threadId,
|
|
4283
4401
|
nodeName,
|
|
@@ -4351,6 +4469,21 @@ please use an LLM adapter instead.`
|
|
|
4351
4469
|
...remoteActions
|
|
4352
4470
|
];
|
|
4353
4471
|
}
|
|
4472
|
+
// Add helper method to detect provider
|
|
4473
|
+
detectProvider(serviceAdapter) {
|
|
4474
|
+
const adapterName = serviceAdapter.constructor.name;
|
|
4475
|
+
if (adapterName.includes("OpenAI"))
|
|
4476
|
+
return "openai";
|
|
4477
|
+
if (adapterName.includes("Anthropic"))
|
|
4478
|
+
return "anthropic";
|
|
4479
|
+
if (adapterName.includes("Google"))
|
|
4480
|
+
return "google";
|
|
4481
|
+
if (adapterName.includes("Groq"))
|
|
4482
|
+
return "groq";
|
|
4483
|
+
if (adapterName.includes("LangChain"))
|
|
4484
|
+
return "langchain";
|
|
4485
|
+
return void 0;
|
|
4486
|
+
}
|
|
4354
4487
|
};
|
|
4355
4488
|
__name(CopilotRuntime, "CopilotRuntime");
|
|
4356
4489
|
function flattenToolCallsNoDuplicates(toolsByPriority) {
|
|
@@ -5370,6 +5503,9 @@ var CopilotResolver = class {
|
|
|
5370
5503
|
resolveOutputMessagesPromise = resolve;
|
|
5371
5504
|
rejectOutputMessagesPromise = reject;
|
|
5372
5505
|
});
|
|
5506
|
+
if (copilotCloudPublicApiKey) {
|
|
5507
|
+
ctx.properties["copilotCloudPublicApiKey"] = copilotCloudPublicApiKey;
|
|
5508
|
+
}
|
|
5373
5509
|
logger2.debug("Processing");
|
|
5374
5510
|
const { eventSource, threadId = (0, import_shared21.randomId)(), runId, serverSideActions, actionInputsWithoutAgents, extensions } = await copilotRuntime.processRuntimeRequest({
|
|
5375
5511
|
serviceAdapter,
|
|
@@ -6166,6 +6302,7 @@ __name(copilotRuntimeNestEndpoint, "copilotRuntimeNestEndpoint");
|
|
|
6166
6302
|
copilotRuntimeNodeExpressEndpoint,
|
|
6167
6303
|
copilotRuntimeNodeHttpEndpoint,
|
|
6168
6304
|
createContext,
|
|
6305
|
+
createLogger,
|
|
6169
6306
|
flattenToolCallsNoDuplicates,
|
|
6170
6307
|
getCommonConfig,
|
|
6171
6308
|
langGraphPlatformEndpoint,
|