@copilotkit/runtime 1.1.3-feat-runtime-remote-actions.2 → 1.2.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 +17 -13
- package/__snapshots__/schema/schema.graphql +0 -41
- package/dist/{chunk-BJ2LVHWA.mjs → chunk-736EEICU.mjs} +3 -3
- package/dist/{chunk-W7RRKPE2.mjs → chunk-DMO6FA25.mjs} +2 -2
- package/dist/{chunk-JYLUCJP7.mjs → chunk-DYF5MUAH.mjs} +2 -2
- package/dist/{chunk-TBZGOJJX.mjs → chunk-GEIBJJQ4.mjs} +2 -14
- package/dist/chunk-GEIBJJQ4.mjs.map +1 -0
- package/dist/chunk-PB24CCIJ.mjs +158 -0
- package/dist/chunk-PB24CCIJ.mjs.map +1 -0
- package/dist/{chunk-7FSNQXPE.mjs → chunk-Q5ZTE7WH.mjs} +158 -786
- package/dist/chunk-Q5ZTE7WH.mjs.map +1 -0
- package/dist/{chunk-73NMP3DI.mjs → chunk-URMISMK2.mjs} +2 -2
- package/dist/{chunk-WRPRFRSL.mjs → chunk-YMUS43FR.mjs} +2 -2
- package/dist/graphql/types/converted/index.d.ts +1 -1
- package/dist/graphql/types/converted/index.js +0 -13
- package/dist/graphql/types/converted/index.js.map +1 -1
- package/dist/graphql/types/converted/index.mjs +1 -3
- package/dist/{index-079752b9.d.ts → index-13aa818e.d.ts} +1 -1
- package/dist/{index-0476e4f7.d.ts → index-aa091e3c.d.ts} +2 -24
- package/dist/index.d.ts +7 -7
- package/dist/index.js +245 -982
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +8 -8
- package/dist/{langserve-d6073a3b.d.ts → langserve-a54438c6.d.ts} +11 -24
- package/dist/lib/index.d.ts +7 -7
- package/dist/lib/index.js +245 -982
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +8 -8
- package/dist/lib/integrations/index.d.ts +6 -6
- package/dist/lib/integrations/index.js +182 -437
- package/dist/lib/integrations/index.js.map +1 -1
- package/dist/lib/integrations/index.mjs +6 -6
- package/dist/lib/integrations/nest/index.d.ts +5 -5
- package/dist/lib/integrations/nest/index.js +182 -437
- package/dist/lib/integrations/nest/index.js.map +1 -1
- package/dist/lib/integrations/nest/index.mjs +4 -4
- package/dist/lib/integrations/node-express/index.d.ts +5 -5
- package/dist/lib/integrations/node-express/index.js +182 -437
- package/dist/lib/integrations/node-express/index.js.map +1 -1
- package/dist/lib/integrations/node-express/index.mjs +4 -4
- package/dist/lib/integrations/node-http/index.d.ts +5 -5
- package/dist/lib/integrations/node-http/index.js +182 -437
- package/dist/lib/integrations/node-http/index.js.map +1 -1
- package/dist/lib/integrations/node-http/index.mjs +3 -3
- package/dist/service-adapters/index.d.ts +3 -3
- package/dist/service-adapters/index.js.map +1 -1
- package/dist/service-adapters/index.mjs +3 -3
- package/dist/{copilot-runtime-d427e991.d.ts → shared-c5362338.d.ts} +38 -65
- package/package.json +5 -7
- package/src/graphql/inputs/generate-copilot-response.input.ts +0 -11
- package/src/graphql/inputs/message.input.ts +0 -30
- package/src/graphql/resolvers/copilot.resolver.ts +12 -56
- package/src/graphql/types/converted/index.ts +0 -15
- package/src/graphql/types/copilot-response.type.ts +0 -29
- package/src/graphql/types/enums.ts +0 -1
- package/src/lib/copilot-runtime.ts +231 -0
- package/src/lib/index.ts +1 -1
- package/src/lib/integrations/shared.ts +1 -1
- package/src/service-adapters/conversion.ts +0 -16
- package/src/service-adapters/events.ts +19 -101
- package/dist/chunk-5TYP4A3G.mjs +0 -260
- package/dist/chunk-5TYP4A3G.mjs.map +0 -1
- package/dist/chunk-7FSNQXPE.mjs.map +0 -1
- package/dist/chunk-TBZGOJJX.mjs.map +0 -1
- package/src/agents/langgraph/event-source.ts +0 -222
- package/src/agents/langgraph/events.ts +0 -309
- package/src/graphql/inputs/agent-session.input.ts +0 -13
- package/src/graphql/inputs/agent-state.input.ts +0 -10
- package/src/lib/runtime/copilot-runtime.ts +0 -360
- package/src/lib/runtime/remote-actions.ts +0 -241
- /package/dist/{chunk-BJ2LVHWA.mjs.map → chunk-736EEICU.mjs.map} +0 -0
- /package/dist/{chunk-W7RRKPE2.mjs.map → chunk-DMO6FA25.mjs.map} +0 -0
- /package/dist/{chunk-JYLUCJP7.mjs.map → chunk-DYF5MUAH.mjs.map} +0 -0
- /package/dist/{chunk-73NMP3DI.mjs.map → chunk-URMISMK2.mjs.map} +0 -0
- /package/dist/{chunk-WRPRFRSL.mjs.map → chunk-YMUS43FR.mjs.map} +0 -0
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"publishConfig": {
|
|
10
10
|
"access": "public"
|
|
11
11
|
},
|
|
12
|
-
"version": "1.1
|
|
12
|
+
"version": "1.2.1",
|
|
13
13
|
"sideEffects": false,
|
|
14
14
|
"main": "./dist/index.js",
|
|
15
15
|
"module": "./dist/index.mjs",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"ts-node": "^10.9.2",
|
|
32
32
|
"tsup": "^6.7.0",
|
|
33
33
|
"typescript": "^5.2.3",
|
|
34
|
-
"eslint-config-custom": "1.
|
|
35
|
-
"tsconfig": "1.
|
|
34
|
+
"eslint-config-custom": "1.2.1",
|
|
35
|
+
"tsconfig": "1.2.1"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@google/generative-ai": "^0.11.2",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"rxjs": "^7.8.1",
|
|
55
55
|
"type-graphql": "2.0.0-rc.1",
|
|
56
56
|
"zod": "^3.23.3",
|
|
57
|
-
"@copilotkit/shared": "1.1
|
|
57
|
+
"@copilotkit/shared": "1.2.1"
|
|
58
58
|
},
|
|
59
59
|
"keywords": [
|
|
60
60
|
"copilotkit",
|
|
@@ -74,8 +74,6 @@
|
|
|
74
74
|
"test": "jest --passWithNoTests",
|
|
75
75
|
"check-types": "tsc --noEmit",
|
|
76
76
|
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist && rm -rf .next && rm -rf __snapshots__",
|
|
77
|
-
"generate-graphql-schema": "rm -rf __snapshots__ && ts-node ./scripts/generate-gql-schema.ts"
|
|
78
|
-
"link:global": "pnpm link --global",
|
|
79
|
-
"unlink:global": "pnpm unlink --global"
|
|
77
|
+
"generate-graphql-schema": "rm -rf __snapshots__ && ts-node ./scripts/generate-gql-schema.ts"
|
|
80
78
|
}
|
|
81
79
|
}
|
|
@@ -4,8 +4,6 @@ import { FrontendInput } from "./frontend.input";
|
|
|
4
4
|
import { CloudInput } from "./cloud.input";
|
|
5
5
|
import { CopilotRequestType } from "../types/enums";
|
|
6
6
|
import { ForwardedParametersInput } from "./forwarded-parameters.input";
|
|
7
|
-
import { AgentSessionInput } from "./agent-session.input";
|
|
8
|
-
import { AgentStateInput } from "./agent-state.input";
|
|
9
7
|
|
|
10
8
|
@InputType()
|
|
11
9
|
export class GenerateCopilotResponseMetadataInput {
|
|
@@ -35,13 +33,4 @@ export class GenerateCopilotResponseInput {
|
|
|
35
33
|
|
|
36
34
|
@Field(() => ForwardedParametersInput, { nullable: true })
|
|
37
35
|
forwardedParameters?: ForwardedParametersInput;
|
|
38
|
-
|
|
39
|
-
@Field(() => AgentSessionInput, { nullable: true })
|
|
40
|
-
agentSession?: AgentSessionInput;
|
|
41
|
-
|
|
42
|
-
@Field(() => AgentStateInput, { nullable: true })
|
|
43
|
-
agentState?: AgentStateInput;
|
|
44
|
-
|
|
45
|
-
@Field(() => [AgentStateInput], { nullable: true })
|
|
46
|
-
agentStates?: AgentStateInput[];
|
|
47
36
|
}
|
|
@@ -14,9 +14,6 @@ export class MessageInput extends BaseMessage {
|
|
|
14
14
|
|
|
15
15
|
@Field(() => ResultMessageInput, { nullable: true })
|
|
16
16
|
resultMessage?: ResultMessageInput;
|
|
17
|
-
|
|
18
|
-
@Field(() => AgentStateMessageInput, { nullable: true })
|
|
19
|
-
agentStateMessage?: AgentStateMessageInput;
|
|
20
17
|
}
|
|
21
18
|
|
|
22
19
|
@InputType()
|
|
@@ -51,30 +48,3 @@ export class ResultMessageInput {
|
|
|
51
48
|
@Field(() => String)
|
|
52
49
|
result: string;
|
|
53
50
|
}
|
|
54
|
-
|
|
55
|
-
@InputType()
|
|
56
|
-
export class AgentStateMessageInput {
|
|
57
|
-
@Field(() => String)
|
|
58
|
-
threadId: string;
|
|
59
|
-
|
|
60
|
-
@Field(() => String)
|
|
61
|
-
agentName: string;
|
|
62
|
-
|
|
63
|
-
@Field(() => MessageRole)
|
|
64
|
-
role: MessageRole;
|
|
65
|
-
|
|
66
|
-
@Field(() => String)
|
|
67
|
-
state: string;
|
|
68
|
-
|
|
69
|
-
@Field(() => Boolean)
|
|
70
|
-
running: boolean;
|
|
71
|
-
|
|
72
|
-
@Field(() => String)
|
|
73
|
-
nodeName: string;
|
|
74
|
-
|
|
75
|
-
@Field(() => String)
|
|
76
|
-
runId: string;
|
|
77
|
-
|
|
78
|
-
@Field(() => Boolean)
|
|
79
|
-
active: boolean;
|
|
80
|
-
}
|
|
@@ -32,13 +32,7 @@ import {
|
|
|
32
32
|
MessageStreamInterruptedResponse,
|
|
33
33
|
UnknownErrorResponse,
|
|
34
34
|
} from "../../utils";
|
|
35
|
-
import {
|
|
36
|
-
ActionExecutionMessage,
|
|
37
|
-
AgentStateMessage,
|
|
38
|
-
Message,
|
|
39
|
-
ResultMessage,
|
|
40
|
-
TextMessage,
|
|
41
|
-
} from "../types/converted";
|
|
35
|
+
import { ActionExecutionMessage, Message, ResultMessage, TextMessage } from "../types/converted";
|
|
42
36
|
import telemetry from "../../lib/telemetry-client";
|
|
43
37
|
import { randomId } from "@copilotkit/shared";
|
|
44
38
|
|
|
@@ -55,6 +49,8 @@ const invokeGuardrails = async ({
|
|
|
55
49
|
onResult: (result: GuardrailsResult) => void;
|
|
56
50
|
onError: (err: Error) => void;
|
|
57
51
|
}) => {
|
|
52
|
+
console.log("invokeGuardrails.baseUrl", baseUrl);
|
|
53
|
+
|
|
58
54
|
if (
|
|
59
55
|
data.messages.length &&
|
|
60
56
|
data.messages[data.messages.length - 1].textMessage?.role === MessageRole.user
|
|
@@ -120,14 +116,14 @@ export class CopilotResolver {
|
|
|
120
116
|
let logger = ctx.logger.child({ component: "CopilotResolver.generateCopilotResponse" });
|
|
121
117
|
logger.debug({ data }, "Generating Copilot response");
|
|
122
118
|
|
|
119
|
+
const copilotRuntime = ctx._copilotkit.runtime;
|
|
120
|
+
const serviceAdapter = ctx._copilotkit.serviceAdapter;
|
|
121
|
+
|
|
123
122
|
if (properties) {
|
|
124
123
|
logger.debug("Properties provided, merging with context properties");
|
|
125
124
|
ctx.properties = { ...ctx.properties, ...properties };
|
|
126
125
|
}
|
|
127
126
|
|
|
128
|
-
const copilotRuntime = ctx._copilotkit.runtime;
|
|
129
|
-
const serviceAdapter = ctx._copilotkit.serviceAdapter;
|
|
130
|
-
|
|
131
127
|
let copilotCloudPublicApiKey: string | null = null;
|
|
132
128
|
let copilotCloudBaseUrl: string;
|
|
133
129
|
|
|
@@ -173,20 +169,17 @@ export class CopilotResolver {
|
|
|
173
169
|
eventSource,
|
|
174
170
|
threadId = randomId(),
|
|
175
171
|
runId,
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
} = await copilotRuntime.processRuntimeRequest({
|
|
172
|
+
actions,
|
|
173
|
+
} = await copilotRuntime.process({
|
|
179
174
|
serviceAdapter,
|
|
180
175
|
messages: data.messages,
|
|
181
176
|
actions: data.frontend.actions,
|
|
182
177
|
threadId: data.threadId,
|
|
183
178
|
runId: data.runId,
|
|
184
179
|
publicApiKey: undefined,
|
|
180
|
+
properties: ctx.properties || {},
|
|
185
181
|
outputMessagesPromise,
|
|
186
|
-
graphqlContext: ctx,
|
|
187
182
|
forwardedParameters: data.forwardedParameters,
|
|
188
|
-
agentSession: data.agentSession,
|
|
189
|
-
agentStates: data.agentStates,
|
|
190
183
|
url: data.frontend.url,
|
|
191
184
|
});
|
|
192
185
|
|
|
@@ -254,14 +247,9 @@ export class CopilotResolver {
|
|
|
254
247
|
|
|
255
248
|
// run and process the event stream
|
|
256
249
|
const eventStream = eventSource
|
|
257
|
-
.
|
|
258
|
-
|
|
250
|
+
.process({
|
|
251
|
+
serversideActions: actions,
|
|
259
252
|
guardrailsResult$: data.cloud?.guardrails ? guardrailsResult$ : null,
|
|
260
|
-
actionInputsWithoutAgents: actionInputsWithoutAgents.filter(
|
|
261
|
-
// TODO-AGENTS: do not exclude ALL server side actions
|
|
262
|
-
(action) =>
|
|
263
|
-
!serverSideActions.find((serverSideAction) => serverSideAction.name == action.name),
|
|
264
|
-
),
|
|
265
253
|
})
|
|
266
254
|
.pipe(
|
|
267
255
|
// shareReplay() ensures that later subscribers will see the whole stream instead of
|
|
@@ -293,6 +281,7 @@ export class CopilotResolver {
|
|
|
293
281
|
const streamingTextStatus = new Subject<typeof MessageStatusUnion>();
|
|
294
282
|
|
|
295
283
|
const messageId = randomId();
|
|
284
|
+
|
|
296
285
|
// push the new message
|
|
297
286
|
pushMessage({
|
|
298
287
|
id: messageId,
|
|
@@ -444,39 +433,6 @@ export class CopilotResolver {
|
|
|
444
433
|
}),
|
|
445
434
|
);
|
|
446
435
|
break;
|
|
447
|
-
////////////////////////////////
|
|
448
|
-
// AgentStateMessage
|
|
449
|
-
////////////////////////////////
|
|
450
|
-
case RuntimeEventTypes.AgentStateMessage:
|
|
451
|
-
logger.debug({ event }, "Agent message event received");
|
|
452
|
-
pushMessage({
|
|
453
|
-
id: randomId(),
|
|
454
|
-
status: new SuccessMessageStatus(),
|
|
455
|
-
threadId: event.threadId,
|
|
456
|
-
agentName: event.agentName,
|
|
457
|
-
nodeName: event.nodeName,
|
|
458
|
-
runId: event.runId,
|
|
459
|
-
active: event.active,
|
|
460
|
-
state: event.state,
|
|
461
|
-
running: event.running,
|
|
462
|
-
role: MessageRole.assistant,
|
|
463
|
-
createdAt: new Date(),
|
|
464
|
-
});
|
|
465
|
-
outputMessages.push(
|
|
466
|
-
plainToInstance(AgentStateMessage, {
|
|
467
|
-
id: randomId(),
|
|
468
|
-
threadId: event.threadId,
|
|
469
|
-
agentName: event.agentName,
|
|
470
|
-
nodeName: event.nodeName,
|
|
471
|
-
runId: event.runId,
|
|
472
|
-
active: event.active,
|
|
473
|
-
state: event.state,
|
|
474
|
-
running: event.running,
|
|
475
|
-
role: MessageRole.assistant,
|
|
476
|
-
createdAt: new Date(),
|
|
477
|
-
}),
|
|
478
|
-
);
|
|
479
|
-
break;
|
|
480
436
|
}
|
|
481
437
|
},
|
|
482
438
|
error: (err) => {
|
|
@@ -2,7 +2,6 @@ import {
|
|
|
2
2
|
ActionExecutionMessageInput,
|
|
3
3
|
ResultMessageInput,
|
|
4
4
|
TextMessageInput,
|
|
5
|
-
AgentStateMessageInput,
|
|
6
5
|
} from "../../inputs/message.input";
|
|
7
6
|
import { BaseMessage } from "../base";
|
|
8
7
|
import { ActionExecutionScope, MessageRole } from "../enums";
|
|
@@ -28,17 +27,3 @@ export class ResultMessage extends BaseMessage implements ResultMessageInput {
|
|
|
28
27
|
actionName: string;
|
|
29
28
|
result: string;
|
|
30
29
|
}
|
|
31
|
-
|
|
32
|
-
export class AgentStateMessage
|
|
33
|
-
extends BaseMessage
|
|
34
|
-
implements Omit<AgentStateMessageInput, "state">
|
|
35
|
-
{
|
|
36
|
-
threadId: string;
|
|
37
|
-
agentName: string;
|
|
38
|
-
nodeName: string;
|
|
39
|
-
runId: string;
|
|
40
|
-
active: boolean;
|
|
41
|
-
role: MessageRole;
|
|
42
|
-
state: any;
|
|
43
|
-
running: boolean;
|
|
44
|
-
}
|
|
@@ -11,8 +11,6 @@ import { ResponseStatusUnion } from "./response-status.type";
|
|
|
11
11
|
return ActionExecutionMessageOutput;
|
|
12
12
|
} else if (value.hasOwnProperty("result")) {
|
|
13
13
|
return ResultMessageOutput;
|
|
14
|
-
} else if (value.hasOwnProperty("state")) {
|
|
15
|
-
return AgentStateMessageOutput;
|
|
16
14
|
}
|
|
17
15
|
return undefined;
|
|
18
16
|
},
|
|
@@ -61,33 +59,6 @@ export class ResultMessageOutput {
|
|
|
61
59
|
result: string;
|
|
62
60
|
}
|
|
63
61
|
|
|
64
|
-
@ObjectType({ implements: BaseMessageOutput })
|
|
65
|
-
export class AgentStateMessageOutput {
|
|
66
|
-
@Field(() => String)
|
|
67
|
-
threadId: string;
|
|
68
|
-
|
|
69
|
-
@Field(() => String)
|
|
70
|
-
agentName: string;
|
|
71
|
-
|
|
72
|
-
@Field(() => String)
|
|
73
|
-
nodeName: string;
|
|
74
|
-
|
|
75
|
-
@Field(() => String)
|
|
76
|
-
runId: string;
|
|
77
|
-
|
|
78
|
-
@Field(() => Boolean)
|
|
79
|
-
active: boolean;
|
|
80
|
-
|
|
81
|
-
@Field(() => MessageRole)
|
|
82
|
-
role: MessageRole;
|
|
83
|
-
|
|
84
|
-
@Field(() => String)
|
|
85
|
-
state: string;
|
|
86
|
-
|
|
87
|
-
@Field(() => Boolean)
|
|
88
|
-
running: boolean;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
62
|
@ObjectType()
|
|
92
63
|
export class CopilotResponse {
|
|
93
64
|
@Field(() => String)
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <Callout type="info">
|
|
3
|
+
* This is the reference for the `CopilotRuntime` class. For more information and example code snippets, please see [Concept: Copilot Runtime](/concepts/copilot-runtime).
|
|
4
|
+
* </Callout>
|
|
5
|
+
*
|
|
6
|
+
* ## Usage
|
|
7
|
+
*
|
|
8
|
+
* ```tsx
|
|
9
|
+
* import { CopilotRuntime } from "@copilotkit/runtime";
|
|
10
|
+
*
|
|
11
|
+
* const copilotKit = new CopilotRuntime();
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { Action, actionParametersToJsonSchema, Parameter } from "@copilotkit/shared";
|
|
16
|
+
import { RemoteChain, RemoteChainParameters, CopilotServiceAdapter } from "../service-adapters";
|
|
17
|
+
import { MessageInput } from "../graphql/inputs/message.input";
|
|
18
|
+
import { ActionInput } from "../graphql/inputs/action.input";
|
|
19
|
+
import { RuntimeEventSource } from "../service-adapters/events";
|
|
20
|
+
import { convertGqlInputToMessages } from "../service-adapters/conversion";
|
|
21
|
+
import { Message } from "../graphql/types/converted";
|
|
22
|
+
import { ForwardedParametersInput } from "../graphql/inputs/forwarded-parameters.input";
|
|
23
|
+
|
|
24
|
+
interface CopilotRuntimeRequest {
|
|
25
|
+
serviceAdapter: CopilotServiceAdapter;
|
|
26
|
+
messages: MessageInput[];
|
|
27
|
+
actions: ActionInput[];
|
|
28
|
+
outputMessagesPromise: Promise<Message[]>;
|
|
29
|
+
properties: any;
|
|
30
|
+
threadId?: string;
|
|
31
|
+
runId?: string;
|
|
32
|
+
publicApiKey?: string;
|
|
33
|
+
url?: string;
|
|
34
|
+
forwardedParameters?: ForwardedParametersInput;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface CopilotRuntimeResponse {
|
|
38
|
+
threadId: string;
|
|
39
|
+
runId?: string;
|
|
40
|
+
eventSource: RuntimeEventSource;
|
|
41
|
+
actions: Action<any>[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type ActionsConfiguration<T extends Parameter[] | [] = []> =
|
|
45
|
+
| Action<T>[]
|
|
46
|
+
| ((ctx: { properties: any; url?: string }) => Action<T>[]);
|
|
47
|
+
|
|
48
|
+
interface OnBeforeRequestOptions {
|
|
49
|
+
threadId?: string;
|
|
50
|
+
runId?: string;
|
|
51
|
+
inputMessages: Message[];
|
|
52
|
+
properties: any;
|
|
53
|
+
url?: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
type OnBeforeRequestHandler = (options: OnBeforeRequestOptions) => void | Promise<void>;
|
|
57
|
+
|
|
58
|
+
interface OnAfterRequestOptions {
|
|
59
|
+
threadId: string;
|
|
60
|
+
runId?: string;
|
|
61
|
+
inputMessages: Message[];
|
|
62
|
+
outputMessages: Message[];
|
|
63
|
+
properties: any;
|
|
64
|
+
url?: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
type OnAfterRequestHandler = (options: OnAfterRequestOptions) => void | Promise<void>;
|
|
68
|
+
|
|
69
|
+
interface Middleware {
|
|
70
|
+
/**
|
|
71
|
+
* A function that is called before the request is processed.
|
|
72
|
+
*/
|
|
73
|
+
onBeforeRequest?: OnBeforeRequestHandler;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* A function that is called after the request is processed.
|
|
77
|
+
*/
|
|
78
|
+
onAfterRequest?: OnAfterRequestHandler;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface CopilotRuntimeConstructorParams<T extends Parameter[] | [] = []> {
|
|
82
|
+
/**
|
|
83
|
+
* Middleware to be used by the runtime.
|
|
84
|
+
*
|
|
85
|
+
* ```ts
|
|
86
|
+
* onBeforeRequest: (options: {
|
|
87
|
+
* threadId?: string;
|
|
88
|
+
* runId?: string;
|
|
89
|
+
* inputMessages: Message[];
|
|
90
|
+
* properties: any;
|
|
91
|
+
* }) => void | Promise<void>;
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* onAfterRequest: (options: {
|
|
96
|
+
* threadId?: string;
|
|
97
|
+
* runId?: string;
|
|
98
|
+
* inputMessages: Message[];
|
|
99
|
+
* outputMessages: Message[];
|
|
100
|
+
* properties: any;
|
|
101
|
+
* }) => void | Promise<void>;
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
middleware?: Middleware;
|
|
105
|
+
|
|
106
|
+
/*
|
|
107
|
+
* A list of server side actions that can be executed.
|
|
108
|
+
*/
|
|
109
|
+
actions?: ActionsConfiguration<T>;
|
|
110
|
+
|
|
111
|
+
/*
|
|
112
|
+
* An array of LangServer URLs.
|
|
113
|
+
*/
|
|
114
|
+
langserve?: RemoteChainParameters[];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export class CopilotRuntime<const T extends Parameter[] | [] = []> {
|
|
118
|
+
public actions: ActionsConfiguration<T>;
|
|
119
|
+
private langserve: Promise<Action<any>>[] = [];
|
|
120
|
+
private onBeforeRequest?: OnBeforeRequestHandler;
|
|
121
|
+
private onAfterRequest?: OnAfterRequestHandler;
|
|
122
|
+
|
|
123
|
+
constructor(params?: CopilotRuntimeConstructorParams<T>) {
|
|
124
|
+
this.actions = params?.actions || [];
|
|
125
|
+
|
|
126
|
+
for (const chain of params?.langserve || []) {
|
|
127
|
+
const remoteChain = new RemoteChain(chain);
|
|
128
|
+
this.langserve.push(remoteChain.toAction());
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
this.onBeforeRequest = params?.middleware?.onBeforeRequest;
|
|
132
|
+
this.onAfterRequest = params?.middleware?.onAfterRequest;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async process(request: CopilotRuntimeRequest): Promise<CopilotRuntimeResponse> {
|
|
136
|
+
const {
|
|
137
|
+
serviceAdapter,
|
|
138
|
+
messages,
|
|
139
|
+
actions: clientSideActionsInput,
|
|
140
|
+
threadId,
|
|
141
|
+
runId,
|
|
142
|
+
properties,
|
|
143
|
+
outputMessagesPromise,
|
|
144
|
+
forwardedParameters,
|
|
145
|
+
url,
|
|
146
|
+
} = request;
|
|
147
|
+
const langserveFunctions: Action<any>[] = [];
|
|
148
|
+
|
|
149
|
+
for (const chainPromise of this.langserve) {
|
|
150
|
+
try {
|
|
151
|
+
const chain = await chainPromise;
|
|
152
|
+
langserveFunctions.push(chain);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
console.error("Error loading langserve chain:", error);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const configuredActions =
|
|
159
|
+
typeof this.actions === "function" ? this.actions({ properties, url }) : this.actions;
|
|
160
|
+
|
|
161
|
+
const actions = [...configuredActions, ...langserveFunctions];
|
|
162
|
+
|
|
163
|
+
const serverSideActionsInput: ActionInput[] = actions.map((action) => ({
|
|
164
|
+
name: action.name,
|
|
165
|
+
description: action.description,
|
|
166
|
+
jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters)),
|
|
167
|
+
}));
|
|
168
|
+
|
|
169
|
+
const actionInputs = flattenToolCallsNoDuplicates([
|
|
170
|
+
...serverSideActionsInput,
|
|
171
|
+
...clientSideActionsInput,
|
|
172
|
+
]);
|
|
173
|
+
const inputMessages = convertGqlInputToMessages(messages);
|
|
174
|
+
|
|
175
|
+
await this.onBeforeRequest?.({
|
|
176
|
+
threadId,
|
|
177
|
+
runId,
|
|
178
|
+
inputMessages,
|
|
179
|
+
properties,
|
|
180
|
+
url,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
const eventSource = new RuntimeEventSource();
|
|
185
|
+
|
|
186
|
+
const result = await serviceAdapter.process({
|
|
187
|
+
messages: inputMessages,
|
|
188
|
+
actions: actionInputs,
|
|
189
|
+
threadId,
|
|
190
|
+
runId,
|
|
191
|
+
eventSource,
|
|
192
|
+
forwardedParameters,
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
outputMessagesPromise
|
|
196
|
+
.then((outputMessages) => {
|
|
197
|
+
this.onAfterRequest?.({
|
|
198
|
+
threadId: result.threadId,
|
|
199
|
+
runId: result.runId,
|
|
200
|
+
inputMessages,
|
|
201
|
+
outputMessages,
|
|
202
|
+
properties,
|
|
203
|
+
url,
|
|
204
|
+
});
|
|
205
|
+
})
|
|
206
|
+
.catch((_error) => {});
|
|
207
|
+
|
|
208
|
+
return {
|
|
209
|
+
threadId: result.threadId,
|
|
210
|
+
runId: result.runId,
|
|
211
|
+
eventSource,
|
|
212
|
+
actions: actions,
|
|
213
|
+
};
|
|
214
|
+
} catch (error) {
|
|
215
|
+
console.error("Error getting response:", error);
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function flattenToolCallsNoDuplicates(toolsByPriority: ActionInput[]): ActionInput[] {
|
|
222
|
+
let allTools: ActionInput[] = [];
|
|
223
|
+
const allToolNames: string[] = [];
|
|
224
|
+
for (const tool of toolsByPriority) {
|
|
225
|
+
if (!allToolNames.includes(tool.name)) {
|
|
226
|
+
allTools.push(tool);
|
|
227
|
+
allToolNames.push(tool.name);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return allTools;
|
|
231
|
+
}
|
package/src/lib/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from "./
|
|
1
|
+
export * from "./copilot-runtime";
|
|
2
2
|
export * from "../service-adapters/openai/openai-adapter";
|
|
3
3
|
export * from "../service-adapters/langchain/langchain-adapter";
|
|
4
4
|
export * from "../service-adapters/google/google-genai-adapter";
|
|
@@ -2,7 +2,7 @@ import { YogaInitialContext } from "graphql-yoga";
|
|
|
2
2
|
import { buildSchemaSync } from "type-graphql";
|
|
3
3
|
import { CopilotResolver } from "../../graphql/resolvers/copilot.resolver";
|
|
4
4
|
import { useDeferStream } from "@graphql-yoga/plugin-defer-stream";
|
|
5
|
-
import { CopilotRuntime } from "../
|
|
5
|
+
import { CopilotRuntime } from "../copilot-runtime";
|
|
6
6
|
import { CopilotServiceAdapter } from "../../service-adapters";
|
|
7
7
|
import { CopilotCloudOptions } from "../cloud";
|
|
8
8
|
import { LogLevel, createLogger } from "../../lib/logger";
|
|
@@ -3,7 +3,6 @@ import {
|
|
|
3
3
|
Message,
|
|
4
4
|
ResultMessage,
|
|
5
5
|
TextMessage,
|
|
6
|
-
AgentStateMessage,
|
|
7
6
|
} from "../graphql/types/converted";
|
|
8
7
|
import { MessageInput } from "../graphql/inputs/message.input";
|
|
9
8
|
import { plainToInstance } from "class-transformer";
|
|
@@ -41,21 +40,6 @@ export function convertGqlInputToMessages(inputMessages: MessageInput[]): Messag
|
|
|
41
40
|
result: message.resultMessage.result,
|
|
42
41
|
}),
|
|
43
42
|
);
|
|
44
|
-
} else if (message.agentStateMessage) {
|
|
45
|
-
messages.push(
|
|
46
|
-
plainToInstance(AgentStateMessage, {
|
|
47
|
-
id: message.id,
|
|
48
|
-
threadId: message.agentStateMessage.threadId,
|
|
49
|
-
createdAt: message.createdAt,
|
|
50
|
-
agentName: message.agentStateMessage.agentName,
|
|
51
|
-
nodeName: message.agentStateMessage.nodeName,
|
|
52
|
-
runId: message.agentStateMessage.runId,
|
|
53
|
-
active: message.agentStateMessage.active,
|
|
54
|
-
role: message.agentStateMessage.role,
|
|
55
|
-
state: JSON.parse(message.agentStateMessage.state),
|
|
56
|
-
running: message.agentStateMessage.running,
|
|
57
|
-
}),
|
|
58
|
-
);
|
|
59
43
|
}
|
|
60
44
|
}
|
|
61
45
|
|