@copilotkit/runtime 1.6.0 → 1.7.0-next.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/CHANGELOG.md +21 -0
- package/dist/{chunk-MPI4JZZR.mjs → chunk-2BN7NZNC.mjs} +2 -2
- package/dist/{chunk-DUW72ZZB.mjs → chunk-34Y5DNNJ.mjs} +88 -67
- package/dist/chunk-34Y5DNNJ.mjs.map +1 -0
- package/dist/{chunk-WUMAYJP3.mjs → chunk-PH24IU7T.mjs} +2 -2
- package/dist/{chunk-2RP2NR4F.mjs → chunk-ZYFN76KV.mjs} +2 -2
- package/dist/index.js +87 -66
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -4
- package/dist/lib/index.js +87 -66
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +4 -4
- 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.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.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.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/runtime/__tests__/remote-action-constructors.test.ts +45 -35
- package/src/lib/runtime/copilot-runtime.ts +14 -14
- 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-DUW72ZZB.mjs.map +0 -1
- /package/dist/{chunk-MPI4JZZR.mjs.map → chunk-2BN7NZNC.mjs.map} +0 -0
- /package/dist/{chunk-WUMAYJP3.mjs.map → chunk-PH24IU7T.mjs.map} +0 -0
- /package/dist/{chunk-2RP2NR4F.mjs.map → chunk-ZYFN76KV.mjs.map} +0 -0
package/dist/index.mjs
CHANGED
|
@@ -3,13 +3,13 @@ import {
|
|
|
3
3
|
config,
|
|
4
4
|
copilotRuntimeNextJSAppRouterEndpoint,
|
|
5
5
|
copilotRuntimeNextJSPagesRouterEndpoint
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-PH24IU7T.mjs";
|
|
7
7
|
import {
|
|
8
8
|
copilotRuntimeNestEndpoint
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-2BN7NZNC.mjs";
|
|
10
10
|
import {
|
|
11
11
|
copilotRuntimeNodeExpressEndpoint
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-ZYFN76KV.mjs";
|
|
13
13
|
import {
|
|
14
14
|
CopilotRuntime,
|
|
15
15
|
addCustomHeaderPlugin,
|
|
@@ -21,7 +21,7 @@ import {
|
|
|
21
21
|
getCommonConfig,
|
|
22
22
|
langGraphPlatformEndpoint,
|
|
23
23
|
resolveEndpointType
|
|
24
|
-
} from "./chunk-
|
|
24
|
+
} from "./chunk-34Y5DNNJ.mjs";
|
|
25
25
|
import {
|
|
26
26
|
AnthropicAdapter,
|
|
27
27
|
EmptyAdapter,
|
package/dist/lib/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.0",
|
|
48
48
|
sideEffects: false,
|
|
49
49
|
main: "./dist/index.js",
|
|
50
50
|
module: "./dist/index.mjs",
|
|
@@ -2301,7 +2301,7 @@ async function streamEvents(controller, args) {
|
|
|
2301
2301
|
let emitIntermediateStateUntilEnd = null;
|
|
2302
2302
|
let shouldExit = false;
|
|
2303
2303
|
let externalRunId = null;
|
|
2304
|
-
const
|
|
2304
|
+
const streamResponse = client.runs.stream(threadId, assistantId, payload);
|
|
2305
2305
|
const emit = /* @__PURE__ */ __name((message) => controller.enqueue(new TextEncoder().encode(message)), "emit");
|
|
2306
2306
|
let latestStateValues = {};
|
|
2307
2307
|
let updatedState = state;
|
|
@@ -2311,7 +2311,7 @@ async function streamEvents(controller, args) {
|
|
|
2311
2311
|
telemetry_client_default.capture("oss.runtime.agent_execution_stream_started", {
|
|
2312
2312
|
hashedLgcKey: streamInfo.hashedLgcKey
|
|
2313
2313
|
});
|
|
2314
|
-
for await (const chunk of
|
|
2314
|
+
for await (const chunk of streamResponse) {
|
|
2315
2315
|
if (![
|
|
2316
2316
|
"events",
|
|
2317
2317
|
"values",
|
|
@@ -2726,6 +2726,54 @@ __name(getSchemaKeys, "getSchemaKeys");
|
|
|
2726
2726
|
|
|
2727
2727
|
// src/lib/runtime/remote-action-constructors.ts
|
|
2728
2728
|
var import_shared11 = require("@copilotkit/shared");
|
|
2729
|
+
|
|
2730
|
+
// src/lib/streaming.ts
|
|
2731
|
+
async function writeJsonLineResponseToEventStream(response, eventStream$) {
|
|
2732
|
+
const reader = response.getReader();
|
|
2733
|
+
const decoder = new TextDecoder();
|
|
2734
|
+
let buffer = [];
|
|
2735
|
+
function flushBuffer() {
|
|
2736
|
+
const currentBuffer = buffer.join("");
|
|
2737
|
+
if (currentBuffer.trim().length === 0) {
|
|
2738
|
+
return;
|
|
2739
|
+
}
|
|
2740
|
+
const parts = currentBuffer.split("\n");
|
|
2741
|
+
if (parts.length === 0) {
|
|
2742
|
+
return;
|
|
2743
|
+
}
|
|
2744
|
+
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
2745
|
+
buffer = [];
|
|
2746
|
+
if (!lastPartIsComplete) {
|
|
2747
|
+
buffer.push(parts.pop());
|
|
2748
|
+
}
|
|
2749
|
+
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
2750
|
+
eventStream$.next(JSON.parse(part));
|
|
2751
|
+
});
|
|
2752
|
+
}
|
|
2753
|
+
__name(flushBuffer, "flushBuffer");
|
|
2754
|
+
try {
|
|
2755
|
+
while (true) {
|
|
2756
|
+
const { done, value } = await reader.read();
|
|
2757
|
+
if (!done) {
|
|
2758
|
+
buffer.push(decoder.decode(value, {
|
|
2759
|
+
stream: true
|
|
2760
|
+
}));
|
|
2761
|
+
}
|
|
2762
|
+
flushBuffer();
|
|
2763
|
+
if (done) {
|
|
2764
|
+
break;
|
|
2765
|
+
}
|
|
2766
|
+
}
|
|
2767
|
+
} catch (error) {
|
|
2768
|
+
console.error("Error in stream", error);
|
|
2769
|
+
eventStream$.error(error);
|
|
2770
|
+
return;
|
|
2771
|
+
}
|
|
2772
|
+
eventStream$.complete();
|
|
2773
|
+
}
|
|
2774
|
+
__name(writeJsonLineResponseToEventStream, "writeJsonLineResponseToEventStream");
|
|
2775
|
+
|
|
2776
|
+
// src/lib/runtime/remote-action-constructors.ts
|
|
2729
2777
|
var import_shared12 = require("@copilotkit/shared");
|
|
2730
2778
|
var import_shared13 = require("@copilotkit/shared");
|
|
2731
2779
|
function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
@@ -2735,7 +2783,7 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
|
|
|
2735
2783
|
parameters: [],
|
|
2736
2784
|
handler: async (_args) => {
|
|
2737
2785
|
},
|
|
2738
|
-
|
|
2786
|
+
remoteAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [], metaEvents }) => {
|
|
2739
2787
|
logger2.debug({
|
|
2740
2788
|
actionName: agent.name
|
|
2741
2789
|
}, "Executing LangGraph Platform agent");
|
|
@@ -2779,7 +2827,7 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
|
|
|
2779
2827
|
metaEvents
|
|
2780
2828
|
});
|
|
2781
2829
|
const eventSource = new RemoteLangGraphEventSource();
|
|
2782
|
-
|
|
2830
|
+
writeJsonLineResponseToEventStream(response, eventSource.eventStream$);
|
|
2783
2831
|
return eventSource.processLangGraphEvents();
|
|
2784
2832
|
} catch (error) {
|
|
2785
2833
|
logger2.error({
|
|
@@ -2796,6 +2844,11 @@ function constructLGCRemoteAction({ endpoint, graphqlContext, logger: logger2, m
|
|
|
2796
2844
|
];
|
|
2797
2845
|
}
|
|
2798
2846
|
__name(constructLGCRemoteAction, "constructLGCRemoteAction");
|
|
2847
|
+
var RemoteAgentType;
|
|
2848
|
+
(function(RemoteAgentType2) {
|
|
2849
|
+
RemoteAgentType2["LangGraph"] = "langgraph";
|
|
2850
|
+
RemoteAgentType2["CrewAI"] = "crewai";
|
|
2851
|
+
})(RemoteAgentType || (RemoteAgentType = {}));
|
|
2799
2852
|
function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, logger: logger2, messages, agentStates }) {
|
|
2800
2853
|
const totalAgents = Array.isArray(json["agents"]) ? json["agents"].length : 0;
|
|
2801
2854
|
const actions = json["actions"].map((action) => ({
|
|
@@ -2865,7 +2918,7 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
2865
2918
|
parameters: [],
|
|
2866
2919
|
handler: async (_args) => {
|
|
2867
2920
|
},
|
|
2868
|
-
|
|
2921
|
+
remoteAgentHandler: async ({ name, actionInputsWithoutAgents, threadId, nodeName, additionalMessages = [], metaEvents }) => {
|
|
2869
2922
|
logger2.debug({
|
|
2870
2923
|
actionName: agent.name
|
|
2871
2924
|
}, "Executing remote agent");
|
|
@@ -2925,9 +2978,17 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
2925
2978
|
isRemoteEndpoint: true
|
|
2926
2979
|
});
|
|
2927
2980
|
}
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2981
|
+
if (agent.type === "langgraph") {
|
|
2982
|
+
const eventSource = new RemoteLangGraphEventSource();
|
|
2983
|
+
writeJsonLineResponseToEventStream(response.body, eventSource.eventStream$);
|
|
2984
|
+
return eventSource.processLangGraphEvents();
|
|
2985
|
+
} else if (agent.type === "crewai") {
|
|
2986
|
+
const eventStream$ = new RuntimeEventSubject();
|
|
2987
|
+
writeJsonLineResponseToEventStream(response.body, eventStream$);
|
|
2988
|
+
return eventStream$;
|
|
2989
|
+
} else {
|
|
2990
|
+
throw new Error("Unsupported agent type");
|
|
2991
|
+
}
|
|
2931
2992
|
} catch (error) {
|
|
2932
2993
|
if (error instanceof import_shared11.CopilotKitError) {
|
|
2933
2994
|
throw error;
|
|
@@ -2945,50 +3006,6 @@ function constructRemoteActions({ json, url, onBeforeRequest, graphqlContext, lo
|
|
|
2945
3006
|
];
|
|
2946
3007
|
}
|
|
2947
3008
|
__name(constructRemoteActions, "constructRemoteActions");
|
|
2948
|
-
async function streamResponse(response, eventStream$) {
|
|
2949
|
-
const reader = response.getReader();
|
|
2950
|
-
const decoder = new TextDecoder();
|
|
2951
|
-
let buffer = [];
|
|
2952
|
-
function flushBuffer() {
|
|
2953
|
-
const currentBuffer = buffer.join("");
|
|
2954
|
-
if (currentBuffer.trim().length === 0) {
|
|
2955
|
-
return;
|
|
2956
|
-
}
|
|
2957
|
-
const parts = currentBuffer.split("\n");
|
|
2958
|
-
if (parts.length === 0) {
|
|
2959
|
-
return;
|
|
2960
|
-
}
|
|
2961
|
-
const lastPartIsComplete = currentBuffer.endsWith("\n");
|
|
2962
|
-
buffer = [];
|
|
2963
|
-
if (!lastPartIsComplete) {
|
|
2964
|
-
buffer.push(parts.pop());
|
|
2965
|
-
}
|
|
2966
|
-
parts.map((part) => part.trim()).filter((part) => part != "").forEach((part) => {
|
|
2967
|
-
eventStream$.next(JSON.parse(part));
|
|
2968
|
-
});
|
|
2969
|
-
}
|
|
2970
|
-
__name(flushBuffer, "flushBuffer");
|
|
2971
|
-
try {
|
|
2972
|
-
while (true) {
|
|
2973
|
-
const { done, value } = await reader.read();
|
|
2974
|
-
if (!done) {
|
|
2975
|
-
buffer.push(decoder.decode(value, {
|
|
2976
|
-
stream: true
|
|
2977
|
-
}));
|
|
2978
|
-
}
|
|
2979
|
-
flushBuffer();
|
|
2980
|
-
if (done) {
|
|
2981
|
-
break;
|
|
2982
|
-
}
|
|
2983
|
-
}
|
|
2984
|
-
} catch (error) {
|
|
2985
|
-
console.error("Error in stream", error);
|
|
2986
|
-
eventStream$.error(error);
|
|
2987
|
-
return;
|
|
2988
|
-
}
|
|
2989
|
-
eventStream$.complete();
|
|
2990
|
-
}
|
|
2991
|
-
__name(streamResponse, "streamResponse");
|
|
2992
3009
|
function createHeaders(onBeforeRequest, graphqlContext) {
|
|
2993
3010
|
const headers = {
|
|
2994
3011
|
"Content-Type": "application/json"
|
|
@@ -3012,13 +3029,13 @@ var EndpointType;
|
|
|
3012
3029
|
EndpointType2["CopilotKit"] = "copilotKit";
|
|
3013
3030
|
EndpointType2["LangGraphPlatform"] = "langgraph-platform";
|
|
3014
3031
|
})(EndpointType || (EndpointType = {}));
|
|
3015
|
-
function
|
|
3032
|
+
function isRemoteAgentAction(action) {
|
|
3016
3033
|
if (!action) {
|
|
3017
3034
|
return false;
|
|
3018
3035
|
}
|
|
3019
|
-
return typeof action.
|
|
3036
|
+
return typeof action.remoteAgentHandler === "function";
|
|
3020
3037
|
}
|
|
3021
|
-
__name(
|
|
3038
|
+
__name(isRemoteAgentAction, "isRemoteAgentAction");
|
|
3022
3039
|
async function fetchRemoteInfo({ url, onBeforeRequest, graphqlContext, logger: logger2, frontendUrl }) {
|
|
3023
3040
|
logger2.debug({
|
|
3024
3041
|
url
|
|
@@ -3522,7 +3539,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
|
|
|
3522
3539
|
return;
|
|
3523
3540
|
}
|
|
3524
3541
|
}
|
|
3525
|
-
if (
|
|
3542
|
+
if (isRemoteAgentAction(action)) {
|
|
3526
3543
|
const result = `${action.name} agent started`;
|
|
3527
3544
|
const agentExecution = (0, import_class_transformer.plainToInstance)(ActionExecutionMessage, {
|
|
3528
3545
|
id: actionExecutionId,
|
|
@@ -3543,7 +3560,7 @@ async function executeAction(eventStream$, guardrailsResult$, action, actionArgu
|
|
|
3543
3560
|
actionName: action.name,
|
|
3544
3561
|
result
|
|
3545
3562
|
});
|
|
3546
|
-
const stream = await action.
|
|
3563
|
+
const stream = await action.remoteAgentHandler({
|
|
3547
3564
|
name: action.name,
|
|
3548
3565
|
threadId,
|
|
3549
3566
|
actionInputsWithoutAgents,
|
|
@@ -3754,7 +3771,6 @@ please use an LLM adapter instead.`
|
|
|
3754
3771
|
}
|
|
3755
3772
|
}
|
|
3756
3773
|
async discoverAgentsFromEndpoints(graphqlContext) {
|
|
3757
|
-
const headers = createHeaders(null, graphqlContext);
|
|
3758
3774
|
const agents = this.remoteEndpointDefinitions.reduce(async (acc, endpoint) => {
|
|
3759
3775
|
const agents2 = await acc;
|
|
3760
3776
|
if (endpoint.type === EndpointType.LangGraphPlatform) {
|
|
@@ -3780,11 +3796,12 @@ please use an LLM adapter instead.`
|
|
|
3780
3796
|
...endpointAgents
|
|
3781
3797
|
];
|
|
3782
3798
|
}
|
|
3799
|
+
const cpkEndpoint = endpoint;
|
|
3783
3800
|
const fetchUrl = `${endpoint.url}/info`;
|
|
3784
3801
|
try {
|
|
3785
3802
|
const response = await fetch(fetchUrl, {
|
|
3786
3803
|
method: "POST",
|
|
3787
|
-
headers,
|
|
3804
|
+
headers: createHeaders(cpkEndpoint.onBeforeRequest, graphqlContext),
|
|
3788
3805
|
body: JSON.stringify({
|
|
3789
3806
|
properties: graphqlContext.properties
|
|
3790
3807
|
})
|
|
@@ -3865,11 +3882,12 @@ please use an LLM adapter instead.`
|
|
|
3865
3882
|
};
|
|
3866
3883
|
}
|
|
3867
3884
|
} else if (agentWithEndpoint.endpoint.type === EndpointType.CopilotKit || !("type" in agentWithEndpoint.endpoint)) {
|
|
3868
|
-
const
|
|
3885
|
+
const cpkEndpoint = agentWithEndpoint.endpoint;
|
|
3886
|
+
const fetchUrl = `${cpkEndpoint.url}/agents/state`;
|
|
3869
3887
|
try {
|
|
3870
3888
|
const response = await fetch(fetchUrl, {
|
|
3871
3889
|
method: "POST",
|
|
3872
|
-
headers,
|
|
3890
|
+
headers: createHeaders(cpkEndpoint.onBeforeRequest, graphqlContext),
|
|
3873
3891
|
body: JSON.stringify({
|
|
3874
3892
|
properties: graphqlContext.properties,
|
|
3875
3893
|
threadId,
|
|
@@ -3914,7 +3932,7 @@ please use an LLM adapter instead.`
|
|
|
3914
3932
|
const threadId = threadIdFromRequest ?? agentSession.threadId;
|
|
3915
3933
|
const serverSideActions = await this.getServerSideActions(request);
|
|
3916
3934
|
const messages = convertGqlInputToMessages(rawMessages);
|
|
3917
|
-
const currentAgent = serverSideActions.find((action) => action.name === agentName &&
|
|
3935
|
+
const currentAgent = serverSideActions.find((action) => action.name === agentName && isRemoteAgentAction(action));
|
|
3918
3936
|
if (!currentAgent) {
|
|
3919
3937
|
throw new import_shared18.CopilotKitAgentDiscoveryError({
|
|
3920
3938
|
agentName
|
|
@@ -3922,8 +3940,8 @@ please use an LLM adapter instead.`
|
|
|
3922
3940
|
}
|
|
3923
3941
|
const availableActionsForCurrentAgent = serverSideActions.filter((action) => (
|
|
3924
3942
|
// Case 1: Keep all regular (non-agent) actions
|
|
3925
|
-
!
|
|
3926
|
-
|
|
3943
|
+
!isRemoteAgentAction(action) || // Case 2: For agent actions, keep all except self (prevent infinite loops)
|
|
3944
|
+
isRemoteAgentAction(action) && action.name !== agentName
|
|
3927
3945
|
)).map((action) => ({
|
|
3928
3946
|
name: action.name,
|
|
3929
3947
|
description: action.description,
|
|
@@ -3941,7 +3959,7 @@ please use an LLM adapter instead.`
|
|
|
3941
3959
|
}));
|
|
3942
3960
|
try {
|
|
3943
3961
|
const eventSource = new RuntimeEventSource();
|
|
3944
|
-
const stream = await currentAgent.
|
|
3962
|
+
const stream = await currentAgent.remoteAgentHandler({
|
|
3945
3963
|
name: agentName,
|
|
3946
3964
|
threadId,
|
|
3947
3965
|
nodeName,
|
|
@@ -5034,6 +5052,9 @@ var CopilotResolver = class {
|
|
|
5034
5052
|
resolveOutputMessagesPromise = resolve;
|
|
5035
5053
|
rejectOutputMessagesPromise = reject;
|
|
5036
5054
|
});
|
|
5055
|
+
if (copilotCloudPublicApiKey) {
|
|
5056
|
+
ctx.properties["copilotCloudPublicApiKey"] = copilotCloudPublicApiKey;
|
|
5057
|
+
}
|
|
5037
5058
|
logger2.debug("Processing");
|
|
5038
5059
|
const { eventSource, threadId = (0, import_shared19.randomId)(), runId, serverSideActions, actionInputsWithoutAgents, extensions } = await copilotRuntime.processRuntimeRequest({
|
|
5039
5060
|
serviceAdapter,
|