@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
package/dist/index.js
CHANGED
|
@@ -34,10 +34,13 @@ __export(src_exports, {
|
|
|
34
34
|
CopilotCloud: () => CopilotCloud,
|
|
35
35
|
CopilotRuntime: () => CopilotRuntime,
|
|
36
36
|
GoogleGenerativeAIAdapter: () => GoogleGenerativeAIAdapter,
|
|
37
|
+
GuardrailsValidationFailureResponse: () => GuardrailsValidationFailureResponse,
|
|
37
38
|
LangChainAdapter: () => LangChainAdapter,
|
|
39
|
+
MessageStreamInterruptedResponse: () => MessageStreamInterruptedResponse,
|
|
38
40
|
OpenAIAdapter: () => OpenAIAdapter,
|
|
39
41
|
OpenAIAssistantAdapter: () => OpenAIAssistantAdapter,
|
|
40
42
|
RemoteCopilotCloud: () => RemoteCopilotCloud,
|
|
43
|
+
UnknownErrorResponse: () => UnknownErrorResponse,
|
|
41
44
|
buildSchema: () => buildSchema,
|
|
42
45
|
config: () => config,
|
|
43
46
|
copilotRuntimeNextJSAppRouterEndpoint: () => copilotRuntimeNextJSAppRouterEndpoint,
|
|
@@ -506,7 +509,7 @@ function convertMessageToGoogleGenAIMessage(message) {
|
|
|
506
509
|
};
|
|
507
510
|
} else if (message instanceof ResultMessage) {
|
|
508
511
|
return {
|
|
509
|
-
role: "
|
|
512
|
+
role: "function",
|
|
510
513
|
parts: [
|
|
511
514
|
{
|
|
512
515
|
functionResponse: {
|
|
@@ -623,6 +626,9 @@ var GoogleGenerativeAIAdapter = class {
|
|
|
623
626
|
let isTextMessage = false;
|
|
624
627
|
for await (const chunk of result.stream) {
|
|
625
628
|
const chunkText = chunk.text();
|
|
629
|
+
if (chunkText === "") {
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
626
632
|
if (!isTextMessage) {
|
|
627
633
|
isTextMessage = true;
|
|
628
634
|
eventStream$.sendTextMessageStart((0, import_nanoid.nanoid)());
|
|
@@ -730,6 +736,31 @@ function convertActionInputToLangChainTool(actionInput) {
|
|
|
730
736
|
});
|
|
731
737
|
}
|
|
732
738
|
__name(convertActionInputToLangChainTool, "convertActionInputToLangChainTool");
|
|
739
|
+
function getConstructorName(object) {
|
|
740
|
+
if (object && typeof object === "object" && object.constructor && object.constructor.name) {
|
|
741
|
+
return object.constructor.name;
|
|
742
|
+
}
|
|
743
|
+
return "";
|
|
744
|
+
}
|
|
745
|
+
__name(getConstructorName, "getConstructorName");
|
|
746
|
+
function isAIMessage(message) {
|
|
747
|
+
return getConstructorName(message) === "AIMessage";
|
|
748
|
+
}
|
|
749
|
+
__name(isAIMessage, "isAIMessage");
|
|
750
|
+
function isAIMessageChunk(message) {
|
|
751
|
+
return getConstructorName(message) === "AIMessageChunk";
|
|
752
|
+
}
|
|
753
|
+
__name(isAIMessageChunk, "isAIMessageChunk");
|
|
754
|
+
function isBaseMessageChunk(message) {
|
|
755
|
+
return getConstructorName(message) === "BaseMessageChunk";
|
|
756
|
+
}
|
|
757
|
+
__name(isBaseMessageChunk, "isBaseMessageChunk");
|
|
758
|
+
function maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution) {
|
|
759
|
+
if (actionExecution) {
|
|
760
|
+
eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, "Sending a message");
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
__name(maybeSendActionExecutionResultIsMessage, "maybeSendActionExecutionResultIsMessage");
|
|
733
764
|
async function streamLangChainResponse({ result, eventStream$, actionExecution }) {
|
|
734
765
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
735
766
|
if (typeof result === "string") {
|
|
@@ -738,14 +769,16 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
|
|
|
738
769
|
} else {
|
|
739
770
|
eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, result);
|
|
740
771
|
}
|
|
741
|
-
} else if (result
|
|
772
|
+
} else if (isAIMessage(result)) {
|
|
773
|
+
maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
|
|
742
774
|
if (result.content) {
|
|
743
775
|
eventStream$.sendTextMessage((0, import_nanoid2.nanoid)(), result.content);
|
|
744
776
|
}
|
|
745
777
|
for (const toolCall of result.tool_calls) {
|
|
746
778
|
eventStream$.sendActionExecution(toolCall.id || (0, import_nanoid2.nanoid)(), toolCall.name, JSON.stringify(toolCall.args));
|
|
747
779
|
}
|
|
748
|
-
} else if (result
|
|
780
|
+
} else if (isBaseMessageChunk(result)) {
|
|
781
|
+
maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
|
|
749
782
|
if ((_a = result.lc_kwargs) == null ? void 0 : _a.content) {
|
|
750
783
|
eventStream$.sendTextMessage((0, import_nanoid2.nanoid)(), result.content);
|
|
751
784
|
}
|
|
@@ -754,7 +787,8 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
|
|
|
754
787
|
eventStream$.sendActionExecution(toolCall.id || (0, import_nanoid2.nanoid)(), toolCall.name, JSON.stringify(toolCall.args));
|
|
755
788
|
}
|
|
756
789
|
}
|
|
757
|
-
} else if ("getReader" in result) {
|
|
790
|
+
} else if (result && "getReader" in result) {
|
|
791
|
+
maybeSendActionExecutionResultIsMessage(eventStream$, actionExecution);
|
|
758
792
|
let reader = result.getReader();
|
|
759
793
|
let mode = null;
|
|
760
794
|
while (true) {
|
|
@@ -764,14 +798,14 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
|
|
|
764
798
|
let toolCallId = void 0;
|
|
765
799
|
let toolCallArgs = void 0;
|
|
766
800
|
let hasToolCall = false;
|
|
767
|
-
let content = value.content;
|
|
768
|
-
if (value
|
|
801
|
+
let content = value == null ? void 0 : value.content;
|
|
802
|
+
if (isAIMessageChunk(value)) {
|
|
769
803
|
let chunk = (_d = value.tool_call_chunks) == null ? void 0 : _d[0];
|
|
770
804
|
toolCallName = chunk == null ? void 0 : chunk.name;
|
|
771
805
|
toolCallId = chunk == null ? void 0 : chunk.id;
|
|
772
806
|
toolCallArgs = chunk == null ? void 0 : chunk.args;
|
|
773
807
|
hasToolCall = chunk != void 0;
|
|
774
|
-
} else if (value
|
|
808
|
+
} else if (isBaseMessageChunk(value)) {
|
|
775
809
|
let chunk = (_f = (_e = value.additional_kwargs) == null ? void 0 : _e.tool_calls) == null ? void 0 : _f[0];
|
|
776
810
|
toolCallName = (_g = chunk == null ? void 0 : chunk.function) == null ? void 0 : _g.name;
|
|
777
811
|
toolCallId = chunk == null ? void 0 : chunk.id;
|
|
@@ -808,13 +842,23 @@ async function streamLangChainResponse({ result, eventStream$, actionExecution }
|
|
|
808
842
|
}
|
|
809
843
|
}
|
|
810
844
|
} else if (actionExecution) {
|
|
811
|
-
eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name,
|
|
845
|
+
eventStream$.sendActionExecutionResult(actionExecution.id, actionExecution.name, encodeResult(result));
|
|
812
846
|
} else {
|
|
813
847
|
throw new Error("Invalid return type from LangChain function.");
|
|
814
848
|
}
|
|
815
849
|
eventStream$.complete();
|
|
816
850
|
}
|
|
817
851
|
__name(streamLangChainResponse, "streamLangChainResponse");
|
|
852
|
+
function encodeResult(result) {
|
|
853
|
+
if (result === void 0) {
|
|
854
|
+
return "";
|
|
855
|
+
} else if (typeof result === "string") {
|
|
856
|
+
return result;
|
|
857
|
+
} else {
|
|
858
|
+
return JSON.stringify(result);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
__name(encodeResult, "encodeResult");
|
|
818
862
|
|
|
819
863
|
// src/service-adapters/langchain/langchain-adapter.ts
|
|
820
864
|
var LangChainAdapter = class {
|
|
@@ -847,6 +891,11 @@ __name(LangChainAdapter, "LangChainAdapter");
|
|
|
847
891
|
// src/service-adapters/langchain/langserve.ts
|
|
848
892
|
var import_remote = require("langchain/runnables/remote");
|
|
849
893
|
var RemoteChain = class {
|
|
894
|
+
name;
|
|
895
|
+
description;
|
|
896
|
+
chainUrl;
|
|
897
|
+
parameters;
|
|
898
|
+
parameterType;
|
|
850
899
|
constructor(options) {
|
|
851
900
|
this.name = options.name;
|
|
852
901
|
this.description = options.description;
|
|
@@ -1038,7 +1087,7 @@ var RuntimeEventSource = class {
|
|
|
1038
1087
|
async stream(callback) {
|
|
1039
1088
|
this.callback = callback;
|
|
1040
1089
|
}
|
|
1041
|
-
process(serversideActions) {
|
|
1090
|
+
process({ serversideActions, guardrailsResult$ }) {
|
|
1042
1091
|
this.callback(this.eventStream$).catch((error) => {
|
|
1043
1092
|
console.error("Error in event source callback", error);
|
|
1044
1093
|
});
|
|
@@ -1074,7 +1123,7 @@ var RuntimeEventSource = class {
|
|
|
1074
1123
|
(0, import_rxjs.concatMap)((eventWithState) => {
|
|
1075
1124
|
if (eventWithState.event.type === "ActionExecutionEnd" && eventWithState.callActionServerSide) {
|
|
1076
1125
|
const toolCallEventStream$ = new RuntimeEventSubject();
|
|
1077
|
-
executeAction(toolCallEventStream$, eventWithState.action, eventWithState.args, eventWithState.actionExecutionId).catch((error) => {
|
|
1126
|
+
executeAction(toolCallEventStream$, guardrailsResult$ ? guardrailsResult$ : null, eventWithState.action, eventWithState.args, eventWithState.actionExecutionId).catch((error) => {
|
|
1078
1127
|
console.error(error);
|
|
1079
1128
|
});
|
|
1080
1129
|
return (0, import_rxjs.concat)((0, import_rxjs.of)(eventWithState.event), toolCallEventStream$);
|
|
@@ -1086,7 +1135,14 @@ var RuntimeEventSource = class {
|
|
|
1086
1135
|
}
|
|
1087
1136
|
};
|
|
1088
1137
|
__name(RuntimeEventSource, "RuntimeEventSource");
|
|
1089
|
-
async function executeAction(eventStream$, action, actionArguments, actionExecutionId) {
|
|
1138
|
+
async function executeAction(eventStream$, guardrailsResult$, action, actionArguments, actionExecutionId) {
|
|
1139
|
+
if (guardrailsResult$) {
|
|
1140
|
+
const { status } = await (0, import_rxjs.firstValueFrom)(guardrailsResult$);
|
|
1141
|
+
if (status === "denied") {
|
|
1142
|
+
eventStream$.complete();
|
|
1143
|
+
return;
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1090
1146
|
let args = [];
|
|
1091
1147
|
if (actionArguments) {
|
|
1092
1148
|
args = JSON.parse(actionArguments);
|
|
@@ -1169,15 +1225,16 @@ var CopilotRuntime = class {
|
|
|
1169
1225
|
console.error("Error loading langserve chain:", error);
|
|
1170
1226
|
}
|
|
1171
1227
|
}
|
|
1172
|
-
const
|
|
1228
|
+
const actions = [
|
|
1173
1229
|
...this.actions,
|
|
1174
1230
|
...langserveFunctions
|
|
1175
|
-
]
|
|
1231
|
+
];
|
|
1232
|
+
const serverSideActionsInput = actions.map((action) => ({
|
|
1176
1233
|
name: action.name,
|
|
1177
1234
|
description: action.description,
|
|
1178
1235
|
jsonSchema: JSON.stringify((0, import_shared2.actionParametersToJsonSchema)(action.parameters))
|
|
1179
1236
|
}));
|
|
1180
|
-
const
|
|
1237
|
+
const actionInputs = flattenToolCallsNoDuplicates([
|
|
1181
1238
|
...serverSideActionsInput,
|
|
1182
1239
|
...clientSideActionsInput
|
|
1183
1240
|
]);
|
|
@@ -1185,7 +1242,7 @@ var CopilotRuntime = class {
|
|
|
1185
1242
|
const eventSource = new RuntimeEventSource();
|
|
1186
1243
|
const result = await serviceAdapter.process({
|
|
1187
1244
|
messages: convertGqlInputToMessages(messages),
|
|
1188
|
-
actions,
|
|
1245
|
+
actions: actionInputs,
|
|
1189
1246
|
threadId,
|
|
1190
1247
|
runId,
|
|
1191
1248
|
eventSource
|
|
@@ -1193,7 +1250,8 @@ var CopilotRuntime = class {
|
|
|
1193
1250
|
return {
|
|
1194
1251
|
threadId: result.threadId,
|
|
1195
1252
|
runId: result.runId,
|
|
1196
|
-
eventSource
|
|
1253
|
+
eventSource,
|
|
1254
|
+
actions
|
|
1197
1255
|
};
|
|
1198
1256
|
} catch (error) {
|
|
1199
1257
|
console.error("Error getting response:", error);
|
|
@@ -1219,10 +1277,10 @@ var CopilotBackend = class extends CopilotRuntime {
|
|
|
1219
1277
|
__name(CopilotBackend, "CopilotBackend");
|
|
1220
1278
|
|
|
1221
1279
|
// src/lib/integrations/shared.ts
|
|
1222
|
-
var
|
|
1280
|
+
var import_type_graphql14 = require("type-graphql");
|
|
1223
1281
|
|
|
1224
1282
|
// src/graphql/resolvers/copilot.resolver.ts
|
|
1225
|
-
var
|
|
1283
|
+
var import_type_graphql13 = require("type-graphql");
|
|
1226
1284
|
var import_rxjs2 = require("rxjs");
|
|
1227
1285
|
|
|
1228
1286
|
// src/graphql/inputs/generate-copilot-response.input.ts
|
|
@@ -1459,15 +1517,10 @@ function _ts_metadata5(k, v) {
|
|
|
1459
1517
|
}
|
|
1460
1518
|
__name(_ts_metadata5, "_ts_metadata");
|
|
1461
1519
|
var GuardrailsRuleInput = class {
|
|
1462
|
-
id;
|
|
1463
1520
|
allowList = [];
|
|
1464
1521
|
denyList = [];
|
|
1465
1522
|
};
|
|
1466
1523
|
__name(GuardrailsRuleInput, "GuardrailsRuleInput");
|
|
1467
|
-
_ts_decorate5([
|
|
1468
|
-
(0, import_type_graphql6.Field)(() => String),
|
|
1469
|
-
_ts_metadata5("design:type", String)
|
|
1470
|
-
], GuardrailsRuleInput.prototype, "id", void 0);
|
|
1471
1524
|
_ts_decorate5([
|
|
1472
1525
|
(0, import_type_graphql6.Field)(() => [
|
|
1473
1526
|
String
|
|
@@ -1488,16 +1541,17 @@ GuardrailsRuleInput = _ts_decorate5([
|
|
|
1488
1541
|
(0, import_type_graphql6.InputType)()
|
|
1489
1542
|
], GuardrailsRuleInput);
|
|
1490
1543
|
var GuardrailsInput = class {
|
|
1491
|
-
inputValidationRules =
|
|
1544
|
+
inputValidationRules = {
|
|
1545
|
+
allowList: [],
|
|
1546
|
+
denyList: []
|
|
1547
|
+
};
|
|
1492
1548
|
};
|
|
1493
1549
|
__name(GuardrailsInput, "GuardrailsInput");
|
|
1494
1550
|
_ts_decorate5([
|
|
1495
|
-
(0, import_type_graphql6.Field)(() =>
|
|
1496
|
-
GuardrailsRuleInput
|
|
1497
|
-
], {
|
|
1551
|
+
(0, import_type_graphql6.Field)(() => GuardrailsRuleInput, {
|
|
1498
1552
|
nullable: true
|
|
1499
1553
|
}),
|
|
1500
|
-
_ts_metadata5("design:type",
|
|
1554
|
+
_ts_metadata5("design:type", typeof GuardrailsRuleInput === "undefined" ? Object : GuardrailsRuleInput)
|
|
1501
1555
|
], GuardrailsInput.prototype, "inputValidationRules", void 0);
|
|
1502
1556
|
GuardrailsInput = _ts_decorate5([
|
|
1503
1557
|
(0, import_type_graphql6.InputType)()
|
|
@@ -1525,7 +1579,9 @@ var CloudInput = class {
|
|
|
1525
1579
|
};
|
|
1526
1580
|
__name(CloudInput, "CloudInput");
|
|
1527
1581
|
_ts_decorate6([
|
|
1528
|
-
(0, import_type_graphql7.Field)(() => GuardrailsInput
|
|
1582
|
+
(0, import_type_graphql7.Field)(() => GuardrailsInput, {
|
|
1583
|
+
nullable: true
|
|
1584
|
+
}),
|
|
1529
1585
|
_ts_metadata6("design:type", typeof GuardrailsInput === "undefined" ? Object : GuardrailsInput)
|
|
1530
1586
|
], CloudInput.prototype, "guardrails", void 0);
|
|
1531
1587
|
CloudInput = _ts_decorate6([
|
|
@@ -1665,6 +1721,7 @@ var MessageStatusUnion = (0, import_type_graphql9.createUnionType)({
|
|
|
1665
1721
|
});
|
|
1666
1722
|
|
|
1667
1723
|
// src/graphql/types/response-status.type.ts
|
|
1724
|
+
var import_graphql_scalars = require("graphql-scalars");
|
|
1668
1725
|
var import_type_graphql10 = require("type-graphql");
|
|
1669
1726
|
function _ts_decorate9(decorators, target, key, desc) {
|
|
1670
1727
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
@@ -1731,15 +1788,31 @@ SuccessResponseStatus = _ts_decorate9([
|
|
|
1731
1788
|
implements: BaseResponseStatus
|
|
1732
1789
|
})
|
|
1733
1790
|
], SuccessResponseStatus);
|
|
1791
|
+
var FailedResponseStatusReason;
|
|
1792
|
+
(function(FailedResponseStatusReason2) {
|
|
1793
|
+
FailedResponseStatusReason2["GUARDRAILS_VALIDATION_FAILED"] = "GUARDRAILS_VALIDATION_FAILED";
|
|
1794
|
+
FailedResponseStatusReason2["MESSAGE_STREAM_INTERRUPTED"] = "MESSAGE_STREAM_INTERRUPTED";
|
|
1795
|
+
FailedResponseStatusReason2["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
|
|
1796
|
+
})(FailedResponseStatusReason || (FailedResponseStatusReason = {}));
|
|
1797
|
+
(0, import_type_graphql10.registerEnumType)(FailedResponseStatusReason, {
|
|
1798
|
+
name: "FailedResponseStatusReason"
|
|
1799
|
+
});
|
|
1734
1800
|
var FailedResponseStatus = class extends BaseResponseStatus {
|
|
1735
1801
|
code = "failed";
|
|
1736
1802
|
reason;
|
|
1803
|
+
details = null;
|
|
1737
1804
|
};
|
|
1738
1805
|
__name(FailedResponseStatus, "FailedResponseStatus");
|
|
1739
1806
|
_ts_decorate9([
|
|
1740
|
-
(0, import_type_graphql10.Field)(() =>
|
|
1807
|
+
(0, import_type_graphql10.Field)(() => FailedResponseStatusReason),
|
|
1741
1808
|
_ts_metadata9("design:type", String)
|
|
1742
1809
|
], FailedResponseStatus.prototype, "reason", void 0);
|
|
1810
|
+
_ts_decorate9([
|
|
1811
|
+
(0, import_type_graphql10.Field)(() => import_graphql_scalars.GraphQLJSON, {
|
|
1812
|
+
nullable: true
|
|
1813
|
+
}),
|
|
1814
|
+
_ts_metadata9("design:type", typeof Record === "undefined" ? Object : Record)
|
|
1815
|
+
], FailedResponseStatus.prototype, "details", void 0);
|
|
1743
1816
|
FailedResponseStatus = _ts_decorate9([
|
|
1744
1817
|
(0, import_type_graphql10.ObjectType)({
|
|
1745
1818
|
implements: BaseResponseStatus
|
|
@@ -1904,7 +1977,11 @@ CopilotResponse = _ts_decorate10([
|
|
|
1904
1977
|
// src/graphql/resolvers/copilot.resolver.ts
|
|
1905
1978
|
var import_graphql_yoga = require("graphql-yoga");
|
|
1906
1979
|
var import_nanoid3 = require("nanoid");
|
|
1907
|
-
var
|
|
1980
|
+
var import_graphql_scalars2 = require("graphql-scalars");
|
|
1981
|
+
var import_class_transformer2 = require("class-transformer");
|
|
1982
|
+
|
|
1983
|
+
// src/graphql/types/guardrails-result.type.ts
|
|
1984
|
+
var import_type_graphql12 = require("type-graphql");
|
|
1908
1985
|
function _ts_decorate11(decorators, target, key, desc) {
|
|
1909
1986
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1910
1987
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -1921,27 +1998,182 @@ function _ts_metadata11(k, v) {
|
|
|
1921
1998
|
return Reflect.metadata(k, v);
|
|
1922
1999
|
}
|
|
1923
2000
|
__name(_ts_metadata11, "_ts_metadata");
|
|
2001
|
+
var GuardrailsResultStatus;
|
|
2002
|
+
(function(GuardrailsResultStatus2) {
|
|
2003
|
+
GuardrailsResultStatus2["ALLOWED"] = "allowed";
|
|
2004
|
+
GuardrailsResultStatus2["DENIED"] = "denied";
|
|
2005
|
+
})(GuardrailsResultStatus || (GuardrailsResultStatus = {}));
|
|
2006
|
+
(0, import_type_graphql12.registerEnumType)(GuardrailsResultStatus, {
|
|
2007
|
+
name: "GuardrailsResultStatus",
|
|
2008
|
+
description: "The status of the guardrails check"
|
|
2009
|
+
});
|
|
2010
|
+
var GuardrailsResult = class {
|
|
2011
|
+
status;
|
|
2012
|
+
reason;
|
|
2013
|
+
};
|
|
2014
|
+
__name(GuardrailsResult, "GuardrailsResult");
|
|
2015
|
+
_ts_decorate11([
|
|
2016
|
+
(0, import_type_graphql12.Field)(() => GuardrailsResultStatus),
|
|
2017
|
+
_ts_metadata11("design:type", String)
|
|
2018
|
+
], GuardrailsResult.prototype, "status", void 0);
|
|
2019
|
+
_ts_decorate11([
|
|
2020
|
+
(0, import_type_graphql12.Field)(() => String, {
|
|
2021
|
+
nullable: true
|
|
2022
|
+
}),
|
|
2023
|
+
_ts_metadata11("design:type", String)
|
|
2024
|
+
], GuardrailsResult.prototype, "reason", void 0);
|
|
2025
|
+
GuardrailsResult = _ts_decorate11([
|
|
2026
|
+
(0, import_type_graphql12.ObjectType)()
|
|
2027
|
+
], GuardrailsResult);
|
|
2028
|
+
|
|
2029
|
+
// src/graphql/resolvers/copilot.resolver.ts
|
|
2030
|
+
var import_graphql = require("graphql");
|
|
2031
|
+
|
|
2032
|
+
// src/utils/failed-response-status-reasons.ts
|
|
2033
|
+
var GuardrailsValidationFailureResponse = class extends FailedResponseStatus {
|
|
2034
|
+
reason = FailedResponseStatusReason.GUARDRAILS_VALIDATION_FAILED;
|
|
2035
|
+
constructor({ guardrailsReason }) {
|
|
2036
|
+
super();
|
|
2037
|
+
this.details = {
|
|
2038
|
+
guardrailsReason
|
|
2039
|
+
};
|
|
2040
|
+
}
|
|
2041
|
+
};
|
|
2042
|
+
__name(GuardrailsValidationFailureResponse, "GuardrailsValidationFailureResponse");
|
|
2043
|
+
var MessageStreamInterruptedResponse = class extends FailedResponseStatus {
|
|
2044
|
+
reason = FailedResponseStatusReason.MESSAGE_STREAM_INTERRUPTED;
|
|
2045
|
+
constructor({ messageId }) {
|
|
2046
|
+
super();
|
|
2047
|
+
this.details = {
|
|
2048
|
+
messageId,
|
|
2049
|
+
description: "Check the message for mode details"
|
|
2050
|
+
};
|
|
2051
|
+
}
|
|
2052
|
+
};
|
|
2053
|
+
__name(MessageStreamInterruptedResponse, "MessageStreamInterruptedResponse");
|
|
2054
|
+
var UnknownErrorResponse = class extends FailedResponseStatus {
|
|
2055
|
+
reason = FailedResponseStatusReason.UNKNOWN_ERROR;
|
|
2056
|
+
constructor({ description }) {
|
|
2057
|
+
super();
|
|
2058
|
+
this.details = {
|
|
2059
|
+
description
|
|
2060
|
+
};
|
|
2061
|
+
}
|
|
2062
|
+
};
|
|
2063
|
+
__name(UnknownErrorResponse, "UnknownErrorResponse");
|
|
2064
|
+
|
|
2065
|
+
// src/graphql/resolvers/copilot.resolver.ts
|
|
2066
|
+
function _ts_decorate12(decorators, target, key, desc) {
|
|
2067
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
2068
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
2069
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
2070
|
+
else
|
|
2071
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
2072
|
+
if (d = decorators[i])
|
|
2073
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
2074
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
2075
|
+
}
|
|
2076
|
+
__name(_ts_decorate12, "_ts_decorate");
|
|
2077
|
+
function _ts_metadata12(k, v) {
|
|
2078
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
|
|
2079
|
+
return Reflect.metadata(k, v);
|
|
2080
|
+
}
|
|
2081
|
+
__name(_ts_metadata12, "_ts_metadata");
|
|
1924
2082
|
function _ts_param(paramIndex, decorator) {
|
|
1925
2083
|
return function(target, key) {
|
|
1926
2084
|
decorator(target, key, paramIndex);
|
|
1927
2085
|
};
|
|
1928
2086
|
}
|
|
1929
2087
|
__name(_ts_param, "_ts_param");
|
|
2088
|
+
var invokeGuardrails = /* @__PURE__ */ __name(async ({ baseUrl, copilotCloudPublicApiKey, data, logger: logger2, onResult }) => {
|
|
2089
|
+
var _a;
|
|
2090
|
+
if (data.messages.length && ((_a = data.messages[data.messages.length - 1].textMessage) == null ? void 0 : _a.role) === MessageRole.user) {
|
|
2091
|
+
const messages = data.messages.filter((m) => m.textMessage !== void 0 && (m.textMessage.role === MessageRole.user || m.textMessage.role === MessageRole.assistant)).map((m) => ({
|
|
2092
|
+
role: m.textMessage.role,
|
|
2093
|
+
content: m.textMessage.content
|
|
2094
|
+
}));
|
|
2095
|
+
const lastMessage = messages[messages.length - 1];
|
|
2096
|
+
const restOfMessages = messages.slice(0, -1);
|
|
2097
|
+
const body = {
|
|
2098
|
+
input: lastMessage.content,
|
|
2099
|
+
validTopics: data.cloud.guardrails.inputValidationRules.allowList,
|
|
2100
|
+
invalidTopics: data.cloud.guardrails.inputValidationRules.denyList,
|
|
2101
|
+
messages: restOfMessages
|
|
2102
|
+
};
|
|
2103
|
+
const guardrailsResult = await fetch(`${baseUrl}/guardrails/validate`, {
|
|
2104
|
+
method: "POST",
|
|
2105
|
+
headers: {
|
|
2106
|
+
"Content-Type": "application/json",
|
|
2107
|
+
"X-CopilotCloud-Public-API-Key": copilotCloudPublicApiKey
|
|
2108
|
+
},
|
|
2109
|
+
body: JSON.stringify(body)
|
|
2110
|
+
});
|
|
2111
|
+
const responseBody = await guardrailsResult.json();
|
|
2112
|
+
if (!guardrailsResult.ok) {
|
|
2113
|
+
logger2.error({
|
|
2114
|
+
error: responseBody
|
|
2115
|
+
}, "Failed to invoke guardrails");
|
|
2116
|
+
throw new import_graphql.GraphQLError("Failed to invoke guardrails");
|
|
2117
|
+
}
|
|
2118
|
+
onResult(responseBody);
|
|
2119
|
+
} else {
|
|
2120
|
+
onResult({
|
|
2121
|
+
status: GuardrailsResultStatus.ALLOWED
|
|
2122
|
+
});
|
|
2123
|
+
}
|
|
2124
|
+
}, "invokeGuardrails");
|
|
1930
2125
|
var CopilotResolver = class {
|
|
1931
2126
|
async hello() {
|
|
1932
2127
|
return "Hello World";
|
|
1933
2128
|
}
|
|
1934
2129
|
async generateCopilotResponse(ctx, data, properties) {
|
|
2130
|
+
let logger2 = ctx.logger.child({
|
|
2131
|
+
component: "CopilotResolver.generateCopilotResponse"
|
|
2132
|
+
});
|
|
2133
|
+
logger2.debug({
|
|
2134
|
+
data
|
|
2135
|
+
}, "Generating Copilot response");
|
|
2136
|
+
const copilotRuntime = ctx._copilotkit.runtime;
|
|
2137
|
+
const serviceAdapter = ctx._copilotkit.serviceAdapter;
|
|
1935
2138
|
if (properties) {
|
|
1936
|
-
|
|
1937
|
-
|
|
2139
|
+
logger2.debug("Properties provided, merging with context properties");
|
|
2140
|
+
ctx.properties = {
|
|
2141
|
+
...ctx.properties,
|
|
1938
2142
|
...properties
|
|
1939
2143
|
};
|
|
1940
2144
|
}
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
2145
|
+
let copilotCloudPublicApiKey = null;
|
|
2146
|
+
let copilotCloudBaseUrl;
|
|
2147
|
+
if (data.cloud) {
|
|
2148
|
+
logger2 = logger2.child({
|
|
2149
|
+
cloud: true
|
|
2150
|
+
});
|
|
2151
|
+
logger2.debug("Cloud configuration provided, checking for public API key in headers");
|
|
2152
|
+
const key = ctx.request.headers.get("x-copilotcloud-public-api-key");
|
|
2153
|
+
if (key) {
|
|
2154
|
+
logger2.debug("Public API key found in headers");
|
|
2155
|
+
copilotCloudPublicApiKey = key;
|
|
2156
|
+
} else {
|
|
2157
|
+
logger2.error("Public API key not found in headers");
|
|
2158
|
+
throw new import_graphql.GraphQLError("X-CopilotCloud-Public-API-Key header is required");
|
|
2159
|
+
}
|
|
2160
|
+
if (process.env.COPILOT_CLOUD_BASE_URL) {
|
|
2161
|
+
copilotCloudBaseUrl = process.env.COPILOT_CLOUD_BASE_URL;
|
|
2162
|
+
} else if (ctx._copilotkit.baseUrl) {
|
|
2163
|
+
copilotCloudBaseUrl = ctx._copilotkit.baseUrl;
|
|
2164
|
+
} else {
|
|
2165
|
+
copilotCloudBaseUrl = "https://api.cloud.copilotkit.ai";
|
|
2166
|
+
}
|
|
2167
|
+
logger2 = logger2.child({
|
|
2168
|
+
copilotCloudBaseUrl
|
|
2169
|
+
});
|
|
2170
|
+
}
|
|
2171
|
+
logger2.debug("Setting up subjects");
|
|
2172
|
+
const responseStatus$ = new import_rxjs2.ReplaySubject();
|
|
2173
|
+
const interruptStreaming$ = new import_rxjs2.ReplaySubject();
|
|
2174
|
+
const guardrailsResult$ = new import_rxjs2.ReplaySubject();
|
|
2175
|
+
logger2.debug("Processing");
|
|
2176
|
+
const { eventSource, threadId = (0, import_nanoid3.nanoid)(), runId, actions } = await copilotRuntime.process({
|
|
1945
2177
|
serviceAdapter,
|
|
1946
2178
|
messages: data.messages,
|
|
1947
2179
|
actions: data.frontend.actions,
|
|
@@ -1949,17 +2181,55 @@ var CopilotResolver = class {
|
|
|
1949
2181
|
runId: data.runId,
|
|
1950
2182
|
publicApiKey: void 0
|
|
1951
2183
|
});
|
|
2184
|
+
logger2.debug("Event source created, creating response");
|
|
1952
2185
|
const response = {
|
|
1953
2186
|
threadId,
|
|
1954
2187
|
runId,
|
|
1955
|
-
status: (0, import_rxjs2.firstValueFrom)(responseStatus),
|
|
2188
|
+
status: (0, import_rxjs2.firstValueFrom)(responseStatus$),
|
|
1956
2189
|
messages: new import_graphql_yoga.Repeater(async (pushMessage, stopStreamingMessages) => {
|
|
1957
|
-
|
|
2190
|
+
var _a, _b;
|
|
2191
|
+
logger2.debug("Messages repeater created");
|
|
2192
|
+
if ((_a = data.cloud) == null ? void 0 : _a.guardrails) {
|
|
2193
|
+
logger2 = logger2.child({
|
|
2194
|
+
guardrails: true
|
|
2195
|
+
});
|
|
2196
|
+
logger2.debug("Guardrails is enabled, validating input");
|
|
2197
|
+
invokeGuardrails({
|
|
2198
|
+
baseUrl: copilotCloudBaseUrl,
|
|
2199
|
+
copilotCloudPublicApiKey,
|
|
2200
|
+
data,
|
|
2201
|
+
logger: logger2,
|
|
2202
|
+
onResult: (result) => {
|
|
2203
|
+
logger2.debug({
|
|
2204
|
+
status: result.status
|
|
2205
|
+
}, "Guardrails validation done");
|
|
2206
|
+
guardrailsResult$.next(result);
|
|
2207
|
+
if (result.status === "denied") {
|
|
2208
|
+
responseStatus$.next(new GuardrailsValidationFailureResponse({
|
|
2209
|
+
guardrailsReason: result.reason
|
|
2210
|
+
}));
|
|
2211
|
+
interruptStreaming$.next({
|
|
2212
|
+
reason: `Interrupted due to Guardrails validation failure. Reason: ${result.reason}`
|
|
2213
|
+
});
|
|
2214
|
+
}
|
|
2215
|
+
}
|
|
2216
|
+
});
|
|
2217
|
+
}
|
|
2218
|
+
let eventStreamSubscription;
|
|
2219
|
+
const eventStream = eventSource.process({
|
|
2220
|
+
serversideActions: copilotRuntime.actions,
|
|
2221
|
+
guardrailsResult$: ((_b = data.cloud) == null ? void 0 : _b.guardrails) ? guardrailsResult$ : null
|
|
2222
|
+
}).pipe(
|
|
1958
2223
|
// shareReplay() ensures that later subscribers will see the whole stream instead of
|
|
1959
2224
|
// just the events that were emitted after the subscriber was added.
|
|
1960
|
-
(0, import_rxjs2.shareReplay)()
|
|
2225
|
+
(0, import_rxjs2.shareReplay)(),
|
|
2226
|
+
(0, import_rxjs2.finalize)(() => {
|
|
2227
|
+
logger2.debug("Event stream finalized, stopping streaming messages");
|
|
2228
|
+
stopStreamingMessages();
|
|
2229
|
+
})
|
|
1961
2230
|
);
|
|
1962
|
-
|
|
2231
|
+
logger2.debug("Event stream created, subscribing to event stream");
|
|
2232
|
+
eventStreamSubscription = eventStream.subscribe({
|
|
1963
2233
|
next: async (event) => {
|
|
1964
2234
|
switch (event.type) {
|
|
1965
2235
|
case RuntimeEventTypes.TextMessageStart:
|
|
@@ -1970,23 +2240,59 @@ var CopilotResolver = class {
|
|
|
1970
2240
|
(0, import_rxjs2.takeWhile)((e) => e.type != RuntimeEventTypes.TextMessageEnd)
|
|
1971
2241
|
);
|
|
1972
2242
|
const streamingTextStatus = new import_rxjs2.Subject();
|
|
2243
|
+
const messageId = (0, import_nanoid3.nanoid)();
|
|
1973
2244
|
pushMessage({
|
|
1974
|
-
id:
|
|
2245
|
+
id: messageId,
|
|
1975
2246
|
status: (0, import_rxjs2.firstValueFrom)(streamingTextStatus),
|
|
1976
2247
|
createdAt: /* @__PURE__ */ new Date(),
|
|
1977
2248
|
role: MessageRole.assistant,
|
|
1978
2249
|
content: new import_graphql_yoga.Repeater(async (pushTextChunk, stopStreamingText) => {
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
2250
|
+
logger2.debug("Text message content repeater created");
|
|
2251
|
+
let textSubscription;
|
|
2252
|
+
interruptStreaming$.pipe((0, import_rxjs2.shareReplay)(), (0, import_rxjs2.take)(1), (0, import_rxjs2.tap)(({ reason, messageId: messageId2 }) => {
|
|
2253
|
+
logger2.debug({
|
|
2254
|
+
reason,
|
|
2255
|
+
messageId: messageId2
|
|
2256
|
+
}, "Text streaming interrupted");
|
|
2257
|
+
streamingTextStatus.next((0, import_class_transformer2.plainToInstance)(FailedMessageStatus, {
|
|
2258
|
+
reason
|
|
2259
|
+
}));
|
|
2260
|
+
responseStatus$.next(new MessageStreamInterruptedResponse({
|
|
2261
|
+
messageId: messageId2
|
|
2262
|
+
}));
|
|
2263
|
+
stopStreamingText();
|
|
2264
|
+
textSubscription.unsubscribe();
|
|
2265
|
+
})).subscribe();
|
|
2266
|
+
logger2.debug("Subscribing to text message content stream");
|
|
2267
|
+
textSubscription = textMessageContentStream.subscribe({
|
|
2268
|
+
next: async (e) => {
|
|
2269
|
+
if (e.type == RuntimeEventTypes.TextMessageContent) {
|
|
2270
|
+
await pushTextChunk(e.content);
|
|
2271
|
+
}
|
|
2272
|
+
},
|
|
2273
|
+
error: (err) => {
|
|
2274
|
+
logger2.error({
|
|
2275
|
+
err
|
|
2276
|
+
}, "Error in text message content stream");
|
|
2277
|
+
interruptStreaming$.next({
|
|
2278
|
+
reason: "Error streaming message content",
|
|
2279
|
+
messageId
|
|
2280
|
+
});
|
|
2281
|
+
stopStreamingText();
|
|
2282
|
+
textSubscription.unsubscribe();
|
|
2283
|
+
},
|
|
2284
|
+
complete: () => {
|
|
2285
|
+
logger2.debug("Text message content stream completed");
|
|
2286
|
+
streamingTextStatus.next(new SuccessMessageStatus());
|
|
2287
|
+
stopStreamingText();
|
|
2288
|
+
textSubscription.unsubscribe();
|
|
1982
2289
|
}
|
|
1983
2290
|
});
|
|
1984
|
-
stopStreamingText();
|
|
1985
|
-
streamingTextStatus.next(new SuccessMessageStatus());
|
|
1986
2291
|
})
|
|
1987
2292
|
});
|
|
1988
2293
|
break;
|
|
1989
2294
|
case RuntimeEventTypes.ActionExecutionStart:
|
|
2295
|
+
logger2.debug("Action execution start event received");
|
|
1990
2296
|
const actionExecutionArgumentStream = eventStream.pipe((0, import_rxjs2.skipWhile)((e) => e !== event), (0, import_rxjs2.takeWhile)((e) => e.type != RuntimeEventTypes.ActionExecutionEnd));
|
|
1991
2297
|
const streamingArgumentsStatus = new import_rxjs2.Subject();
|
|
1992
2298
|
pushMessage({
|
|
@@ -1996,17 +2302,36 @@ var CopilotResolver = class {
|
|
|
1996
2302
|
name: event.actionName,
|
|
1997
2303
|
scope: event.scope,
|
|
1998
2304
|
arguments: new import_graphql_yoga.Repeater(async (pushArgumentsChunk, stopStreamingArguments) => {
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2305
|
+
logger2.debug("Action execution argument stream created");
|
|
2306
|
+
let actionExecutionArgumentSubscription;
|
|
2307
|
+
actionExecutionArgumentSubscription = actionExecutionArgumentStream.subscribe({
|
|
2308
|
+
next: async (e) => {
|
|
2309
|
+
if (e.type == RuntimeEventTypes.ActionExecutionArgs) {
|
|
2310
|
+
await pushArgumentsChunk(e.args);
|
|
2311
|
+
}
|
|
2312
|
+
},
|
|
2313
|
+
error: (err) => {
|
|
2314
|
+
logger2.error({
|
|
2315
|
+
err
|
|
2316
|
+
}, "Error in action execution argument stream");
|
|
2317
|
+
streamingArgumentsStatus.next((0, import_class_transformer2.plainToInstance)(FailedMessageStatus, {
|
|
2318
|
+
reason: "An unknown error has occurred in the action execution argument stream"
|
|
2319
|
+
}));
|
|
2320
|
+
stopStreamingArguments();
|
|
2321
|
+
actionExecutionArgumentSubscription.unsubscribe();
|
|
2322
|
+
},
|
|
2323
|
+
complete: () => {
|
|
2324
|
+
logger2.debug("Action execution argument stream completed");
|
|
2325
|
+
streamingArgumentsStatus.next(new SuccessMessageStatus());
|
|
2326
|
+
stopStreamingArguments();
|
|
2327
|
+
actionExecutionArgumentSubscription.unsubscribe();
|
|
2002
2328
|
}
|
|
2003
2329
|
});
|
|
2004
|
-
stopStreamingArguments();
|
|
2005
|
-
streamingArgumentsStatus.next(new SuccessMessageStatus());
|
|
2006
2330
|
})
|
|
2007
2331
|
});
|
|
2008
2332
|
break;
|
|
2009
2333
|
case RuntimeEventTypes.ActionExecutionResult:
|
|
2334
|
+
logger2.debug("Action execution result event received");
|
|
2010
2335
|
pushMessage({
|
|
2011
2336
|
id: (0, import_nanoid3.nanoid)(),
|
|
2012
2337
|
status: new SuccessMessageStatus(),
|
|
@@ -2018,9 +2343,25 @@ var CopilotResolver = class {
|
|
|
2018
2343
|
break;
|
|
2019
2344
|
}
|
|
2020
2345
|
},
|
|
2021
|
-
error: (err) =>
|
|
2022
|
-
|
|
2023
|
-
|
|
2346
|
+
error: (err) => {
|
|
2347
|
+
logger2.error({
|
|
2348
|
+
err
|
|
2349
|
+
}, "Error in event stream");
|
|
2350
|
+
responseStatus$.next(new UnknownErrorResponse({
|
|
2351
|
+
description: `An unknown error has occurred in the event stream`
|
|
2352
|
+
}));
|
|
2353
|
+
eventStreamSubscription.unsubscribe();
|
|
2354
|
+
stopStreamingMessages();
|
|
2355
|
+
},
|
|
2356
|
+
complete: async () => {
|
|
2357
|
+
var _a2;
|
|
2358
|
+
logger2.debug("Event stream completed");
|
|
2359
|
+
if ((_a2 = data.cloud) == null ? void 0 : _a2.guardrails) {
|
|
2360
|
+
logger2.debug("Guardrails is enabled, waiting for guardrails result");
|
|
2361
|
+
await (0, import_rxjs2.firstValueFrom)(guardrailsResult$);
|
|
2362
|
+
}
|
|
2363
|
+
responseStatus$.next(new SuccessResponseStatus());
|
|
2364
|
+
eventStreamSubscription.unsubscribe();
|
|
2024
2365
|
stopStreamingMessages();
|
|
2025
2366
|
}
|
|
2026
2367
|
});
|
|
@@ -2030,78 +2371,126 @@ var CopilotResolver = class {
|
|
|
2030
2371
|
}
|
|
2031
2372
|
};
|
|
2032
2373
|
__name(CopilotResolver, "CopilotResolver");
|
|
2033
|
-
|
|
2034
|
-
(0,
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2374
|
+
_ts_decorate12([
|
|
2375
|
+
(0, import_type_graphql13.Query)(() => String),
|
|
2376
|
+
_ts_metadata12("design:type", Function),
|
|
2377
|
+
_ts_metadata12("design:paramtypes", []),
|
|
2378
|
+
_ts_metadata12("design:returntype", Promise)
|
|
2038
2379
|
], CopilotResolver.prototype, "hello", null);
|
|
2039
|
-
|
|
2040
|
-
(0,
|
|
2041
|
-
_ts_param(0, (0,
|
|
2042
|
-
_ts_param(1, (0,
|
|
2043
|
-
_ts_param(2, (0,
|
|
2380
|
+
_ts_decorate12([
|
|
2381
|
+
(0, import_type_graphql13.Mutation)(() => CopilotResponse),
|
|
2382
|
+
_ts_param(0, (0, import_type_graphql13.Ctx)()),
|
|
2383
|
+
_ts_param(1, (0, import_type_graphql13.Arg)("data")),
|
|
2384
|
+
_ts_param(2, (0, import_type_graphql13.Arg)("properties", () => import_graphql_scalars2.GraphQLJSONObject, {
|
|
2044
2385
|
nullable: true
|
|
2045
2386
|
})),
|
|
2046
|
-
|
|
2047
|
-
|
|
2387
|
+
_ts_metadata12("design:type", Function),
|
|
2388
|
+
_ts_metadata12("design:paramtypes", [
|
|
2048
2389
|
typeof GraphQLContext === "undefined" ? Object : GraphQLContext,
|
|
2049
2390
|
typeof GenerateCopilotResponseInput === "undefined" ? Object : GenerateCopilotResponseInput,
|
|
2050
2391
|
typeof CopilotRequestContextProperties === "undefined" ? Object : CopilotRequestContextProperties
|
|
2051
2392
|
]),
|
|
2052
|
-
|
|
2393
|
+
_ts_metadata12("design:returntype", Promise)
|
|
2053
2394
|
], CopilotResolver.prototype, "generateCopilotResponse", null);
|
|
2054
|
-
CopilotResolver =
|
|
2055
|
-
(0,
|
|
2395
|
+
CopilotResolver = _ts_decorate12([
|
|
2396
|
+
(0, import_type_graphql13.Resolver)(() => CopilotResponse)
|
|
2056
2397
|
], CopilotResolver);
|
|
2057
2398
|
|
|
2058
2399
|
// src/lib/integrations/shared.ts
|
|
2059
2400
|
var import_plugin_defer_stream = require("@graphql-yoga/plugin-defer-stream");
|
|
2060
|
-
|
|
2401
|
+
|
|
2402
|
+
// src/lib/logger.ts
|
|
2403
|
+
var import_pino = __toESM(require("pino"));
|
|
2404
|
+
var import_pino_pretty = __toESM(require("pino-pretty"));
|
|
2405
|
+
function createLogger(options) {
|
|
2406
|
+
const { level, component } = options || {};
|
|
2407
|
+
const stream = (0, import_pino_pretty.default)({
|
|
2408
|
+
colorize: true
|
|
2409
|
+
});
|
|
2410
|
+
const logger2 = (0, import_pino.default)({
|
|
2411
|
+
level: process.env.LOG_LEVEL || level || "error",
|
|
2412
|
+
redact: {
|
|
2413
|
+
paths: [
|
|
2414
|
+
"pid",
|
|
2415
|
+
"hostname"
|
|
2416
|
+
],
|
|
2417
|
+
remove: true
|
|
2418
|
+
}
|
|
2419
|
+
}, stream);
|
|
2420
|
+
if (component) {
|
|
2421
|
+
return logger2.child({
|
|
2422
|
+
component
|
|
2423
|
+
});
|
|
2424
|
+
} else {
|
|
2425
|
+
return logger2;
|
|
2426
|
+
}
|
|
2427
|
+
}
|
|
2428
|
+
__name(createLogger, "createLogger");
|
|
2429
|
+
|
|
2430
|
+
// src/lib/integrations/shared.ts
|
|
2431
|
+
var logger = createLogger();
|
|
2432
|
+
async function createContext(initialContext, copilotKitContext, contextLogger, properties = {}) {
|
|
2433
|
+
logger.debug({
|
|
2434
|
+
copilotKitContext
|
|
2435
|
+
}, "Creating GraphQL context");
|
|
2061
2436
|
const ctx = {
|
|
2062
2437
|
...initialContext,
|
|
2063
2438
|
_copilotkit: {
|
|
2064
2439
|
...copilotKitContext
|
|
2065
|
-
}
|
|
2440
|
+
},
|
|
2441
|
+
properties: {
|
|
2442
|
+
...properties
|
|
2443
|
+
},
|
|
2444
|
+
logger: contextLogger
|
|
2066
2445
|
};
|
|
2067
2446
|
return ctx;
|
|
2068
2447
|
}
|
|
2069
2448
|
__name(createContext, "createContext");
|
|
2070
2449
|
function buildSchema(options = {}) {
|
|
2071
|
-
|
|
2450
|
+
logger.debug("Building GraphQL schema...");
|
|
2451
|
+
const schema = (0, import_type_graphql14.buildSchemaSync)({
|
|
2072
2452
|
resolvers: [
|
|
2073
2453
|
CopilotResolver
|
|
2074
2454
|
],
|
|
2075
2455
|
emitSchemaFile: options.emitSchemaFile
|
|
2076
2456
|
});
|
|
2457
|
+
logger.debug("GraphQL schema built successfully");
|
|
2077
2458
|
return schema;
|
|
2078
2459
|
}
|
|
2079
2460
|
__name(buildSchema, "buildSchema");
|
|
2080
2461
|
function getCommonConfig(options) {
|
|
2462
|
+
const logLevel = process.env.LOG_LEVEL || options.logLevel || "error";
|
|
2463
|
+
const logger2 = createLogger({
|
|
2464
|
+
level: logLevel,
|
|
2465
|
+
component: "getCommonConfig"
|
|
2466
|
+
});
|
|
2467
|
+
logger2.debug("Getting common config");
|
|
2468
|
+
const contextLogger = createLogger({
|
|
2469
|
+
level: logLevel
|
|
2470
|
+
});
|
|
2081
2471
|
return {
|
|
2472
|
+
logging: createLogger({
|
|
2473
|
+
component: "Yoga GraphQL",
|
|
2474
|
+
level: logLevel
|
|
2475
|
+
}),
|
|
2082
2476
|
schema: buildSchema(),
|
|
2083
2477
|
plugins: [
|
|
2084
2478
|
(0, import_plugin_defer_stream.useDeferStream)()
|
|
2085
2479
|
],
|
|
2086
|
-
context: (ctx) => createContext(ctx,
|
|
2087
|
-
runtime: options.runtime,
|
|
2088
|
-
serviceAdapter: options.serviceAdapter,
|
|
2089
|
-
properties: {}
|
|
2090
|
-
})
|
|
2480
|
+
context: (ctx) => createContext(ctx, options, contextLogger, options.properties)
|
|
2091
2481
|
};
|
|
2092
2482
|
}
|
|
2093
2483
|
__name(getCommonConfig, "getCommonConfig");
|
|
2094
2484
|
|
|
2095
2485
|
// src/lib/integrations/nextjs/app-router.ts
|
|
2096
2486
|
var import_graphql_yoga2 = require("graphql-yoga");
|
|
2097
|
-
function copilotRuntimeNextJSAppRouterEndpoint(
|
|
2098
|
-
const commonConfig = getCommonConfig(
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
});
|
|
2487
|
+
function copilotRuntimeNextJSAppRouterEndpoint(options) {
|
|
2488
|
+
const commonConfig = getCommonConfig(options);
|
|
2489
|
+
const logger2 = commonConfig.logging;
|
|
2490
|
+
logger2.debug("Creating NextJS App Router endpoint");
|
|
2102
2491
|
const yoga = (0, import_graphql_yoga2.createYoga)({
|
|
2103
2492
|
...commonConfig,
|
|
2104
|
-
graphqlEndpoint: endpoint,
|
|
2493
|
+
graphqlEndpoint: options.endpoint,
|
|
2105
2494
|
fetchAPI: {
|
|
2106
2495
|
Response: globalThis.Response
|
|
2107
2496
|
}
|
|
@@ -2122,14 +2511,13 @@ var config = {
|
|
|
2122
2511
|
bodyParser: false
|
|
2123
2512
|
}
|
|
2124
2513
|
};
|
|
2125
|
-
function copilotRuntimeNextJSPagesRouterEndpoint(
|
|
2126
|
-
const commonConfig = getCommonConfig(
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
});
|
|
2514
|
+
function copilotRuntimeNextJSPagesRouterEndpoint(options) {
|
|
2515
|
+
const commonConfig = getCommonConfig(options);
|
|
2516
|
+
const logger2 = commonConfig.logging;
|
|
2517
|
+
logger2.debug("Creating NextJS Pages Router endpoint");
|
|
2130
2518
|
const yoga = (0, import_graphql_yoga3.createYoga)({
|
|
2131
2519
|
...commonConfig,
|
|
2132
|
-
graphqlEndpoint: endpoint
|
|
2520
|
+
graphqlEndpoint: options.endpoint
|
|
2133
2521
|
});
|
|
2134
2522
|
return yoga;
|
|
2135
2523
|
}
|
|
@@ -2137,14 +2525,13 @@ __name(copilotRuntimeNextJSPagesRouterEndpoint, "copilotRuntimeNextJSPagesRouter
|
|
|
2137
2525
|
|
|
2138
2526
|
// src/lib/integrations/node-http/index.ts
|
|
2139
2527
|
var import_graphql_yoga4 = require("graphql-yoga");
|
|
2140
|
-
function copilotRuntimeNodeHttpEndpoint(
|
|
2141
|
-
const commonConfig = getCommonConfig(
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
});
|
|
2528
|
+
function copilotRuntimeNodeHttpEndpoint(options) {
|
|
2529
|
+
const commonConfig = getCommonConfig(options);
|
|
2530
|
+
const logger2 = commonConfig.logging;
|
|
2531
|
+
logger2.debug("Creating Node HTTP endpoint");
|
|
2145
2532
|
const yoga = (0, import_graphql_yoga4.createYoga)({
|
|
2146
2533
|
...commonConfig,
|
|
2147
|
-
graphqlEndpoint: endpoint
|
|
2534
|
+
graphqlEndpoint: options.endpoint
|
|
2148
2535
|
});
|
|
2149
2536
|
return yoga;
|
|
2150
2537
|
}
|
|
@@ -2155,10 +2542,13 @@ __name(copilotRuntimeNodeHttpEndpoint, "copilotRuntimeNodeHttpEndpoint");
|
|
|
2155
2542
|
CopilotCloud,
|
|
2156
2543
|
CopilotRuntime,
|
|
2157
2544
|
GoogleGenerativeAIAdapter,
|
|
2545
|
+
GuardrailsValidationFailureResponse,
|
|
2158
2546
|
LangChainAdapter,
|
|
2547
|
+
MessageStreamInterruptedResponse,
|
|
2159
2548
|
OpenAIAdapter,
|
|
2160
2549
|
OpenAIAssistantAdapter,
|
|
2161
2550
|
RemoteCopilotCloud,
|
|
2551
|
+
UnknownErrorResponse,
|
|
2162
2552
|
buildSchema,
|
|
2163
2553
|
config,
|
|
2164
2554
|
copilotRuntimeNextJSAppRouterEndpoint,
|