@copilotkit/runtime
Version:
<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />
211 lines (206 loc) • 8.56 kB
JavaScript
;
var rxjs = require('rxjs');
var langgraph = require('@ag-ui/langgraph');
var client = require('@ag-ui/client');
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/lib/runtime/agent-integrations/langgraph/consts.ts
exports.CustomEventNames = void 0;
(function(CustomEventNames3) {
CustomEventNames3["CopilotKitManuallyEmitMessage"] = "copilotkit_manually_emit_message";
CustomEventNames3["CopilotKitManuallyEmitToolCall"] = "copilotkit_manually_emit_tool_call";
CustomEventNames3["CopilotKitManuallyEmitIntermediateState"] = "copilotkit_manually_emit_intermediate_state";
CustomEventNames3["CopilotKitExit"] = "copilotkit_exit";
})(exports.CustomEventNames || (exports.CustomEventNames = {}));
// src/agents/langgraph/events.ts
var LangGraphEventTypes;
(function(LangGraphEventTypes2) {
LangGraphEventTypes2["OnChainStart"] = "on_chain_start";
LangGraphEventTypes2["OnChainStream"] = "on_chain_stream";
LangGraphEventTypes2["OnChainEnd"] = "on_chain_end";
LangGraphEventTypes2["OnChatModelStart"] = "on_chat_model_start";
LangGraphEventTypes2["OnChatModelStream"] = "on_chat_model_stream";
LangGraphEventTypes2["OnChatModelEnd"] = "on_chat_model_end";
LangGraphEventTypes2["OnToolStart"] = "on_tool_start";
LangGraphEventTypes2["OnToolEnd"] = "on_tool_end";
LangGraphEventTypes2["OnCopilotKitStateSync"] = "on_copilotkit_state_sync";
LangGraphEventTypes2["OnCopilotKitEmitMessage"] = "on_copilotkit_emit_message";
LangGraphEventTypes2["OnCopilotKitEmitToolCall"] = "on_copilotkit_emit_tool_call";
LangGraphEventTypes2["OnCustomEvent"] = "on_custom_event";
LangGraphEventTypes2["OnInterrupt"] = "on_interrupt";
LangGraphEventTypes2["OnCopilotKitInterrupt"] = "on_copilotkit_interrupt";
LangGraphEventTypes2["OnCopilotKitError"] = "on_copilotkit_error";
})(LangGraphEventTypes || (LangGraphEventTypes = {}));
var MetaEventNames;
(function(MetaEventNames2) {
MetaEventNames2["LangGraphInterruptEvent"] = "LangGraphInterruptEvent";
MetaEventNames2["CopilotKitLangGraphInterruptEvent"] = "CopilotKitLangGraphInterruptEvent";
})(MetaEventNames || (MetaEventNames = {}));
var CustomEventNames2;
(function(CustomEventNames3) {
CustomEventNames3["CopilotKitManuallyEmitMessage"] = "copilotkit_manually_emit_message";
CustomEventNames3["CopilotKitManuallyEmitToolCall"] = "copilotkit_manually_emit_tool_call";
CustomEventNames3["CopilotKitManuallyEmitIntermediateState"] = "copilotkit_manually_emit_intermediate_state";
CustomEventNames3["CopilotKitExit"] = "copilotkit_exit";
})(CustomEventNames2 || (CustomEventNames2 = {}));
var LangGraphAgent = class extends langgraph.LangGraphAgent {
constructor(config) {
super(config);
}
// @ts-ignore
clone() {
return new LangGraphAgent(this.config);
}
dispatchEvent(event) {
if (event.type === client.EventType.CUSTOM) {
const customEvent = event;
if (customEvent.name === exports.CustomEventNames.CopilotKitManuallyEmitMessage) {
this.subscriber.next({
type: client.EventType.TEXT_MESSAGE_START,
role: "assistant",
messageId: customEvent.value.message_id,
rawEvent: event
});
this.subscriber.next({
type: client.EventType.TEXT_MESSAGE_CONTENT,
messageId: customEvent.value.message_id,
delta: customEvent.value.message,
rawEvent: event
});
this.subscriber.next({
type: client.EventType.TEXT_MESSAGE_END,
messageId: customEvent.value.message_id,
rawEvent: event
});
return true;
}
if (customEvent.name === exports.CustomEventNames.CopilotKitManuallyEmitToolCall) {
this.subscriber.next({
type: client.EventType.TOOL_CALL_START,
toolCallId: customEvent.value.id,
toolCallName: customEvent.value.name,
parentMessageId: customEvent.value.id,
rawEvent: event
});
this.subscriber.next({
type: client.EventType.TOOL_CALL_ARGS,
toolCallId: customEvent.value.id,
delta: customEvent.value.args,
rawEvent: event
});
this.subscriber.next({
type: client.EventType.TOOL_CALL_END,
toolCallId: customEvent.value.id,
rawEvent: event
});
return true;
}
if (customEvent.name === exports.CustomEventNames.CopilotKitManuallyEmitIntermediateState) {
this.activeRun.manuallyEmittedState = customEvent.value;
this.dispatchEvent({
type: client.EventType.STATE_SNAPSHOT,
snapshot: this.getStateSnapshot({
values: this.activeRun.manuallyEmittedState
}),
rawEvent: event
});
return true;
}
if (customEvent.name === exports.CustomEventNames.CopilotKitExit) {
this.subscriber.next({
type: client.EventType.CUSTOM,
name: "Exit",
value: true
});
return true;
}
}
const rawEvent = event.rawEvent;
if (!rawEvent) {
this.subscriber.next(event);
return true;
}
const isMessageEvent = event.type === client.EventType.TEXT_MESSAGE_START || event.type === client.EventType.TEXT_MESSAGE_CONTENT || event.type === client.EventType.TEXT_MESSAGE_END;
const isToolEvent = event.type === client.EventType.TOOL_CALL_START || event.type === client.EventType.TOOL_CALL_ARGS || event.type === client.EventType.TOOL_CALL_END;
if ("copilotkit:emit-tool-calls" in (rawEvent.metadata || {})) {
if (rawEvent.metadata["copilotkit:emit-tool-calls"] === false && isToolEvent) {
return false;
}
}
if ("copilotkit:emit-messages" in (rawEvent.metadata || {})) {
if (rawEvent.metadata["copilotkit:emit-messages"] === false && isMessageEvent) {
return false;
}
}
this.subscriber.next(event);
return true;
}
// @ts-ignore
run(input) {
return super.run(input).pipe(rxjs.map((processedEvent) => {
var _a, _b, _c, _d, _e;
if (processedEvent.type === client.EventType.RAW) {
const event = processedEvent.event ?? processedEvent.rawEvent;
const eventType = event.event;
const toolCallData = (_c = (_b = (_a = event.data) == null ? void 0 : _a.chunk) == null ? void 0 : _b.tool_call_chunks) == null ? void 0 : _c[0];
const toolCallUsedToPredictState = (_e = (_d = event.metadata) == null ? void 0 : _d["copilotkit:emit-intermediate-state"]) == null ? void 0 : _e.some((predictStateTool) => predictStateTool.tool === (toolCallData == null ? void 0 : toolCallData.name));
if (eventType === LangGraphEventTypes.OnChatModelStream && toolCallUsedToPredictState) {
return {
type: client.EventType.CUSTOM,
name: "PredictState",
value: event.metadata["copilotkit:emit-intermediate-state"]
};
}
}
return processedEvent;
}));
}
langGraphDefaultMergeState(state, messages, input) {
const aguiMergedState = super.langGraphDefaultMergeState(state, messages, input);
const { tools: returnedTools, "ag-ui": agui } = aguiMergedState;
const rawCombinedTools = [
...returnedTools ?? [],
...(agui == null ? void 0 : agui.tools) ?? []
];
const combinedTools = Array.from(new Map(rawCombinedTools.map((t) => [
(t == null ? void 0 : t.id) ?? (t == null ? void 0 : t.name) ?? (t == null ? void 0 : t.key) ?? JSON.stringify(t),
t
])).values());
return {
...aguiMergedState,
copilotkit: {
actions: combinedTools,
context: (agui == null ? void 0 : agui.context) ?? []
}
};
}
async getSchemaKeys() {
const CONSTANT_KEYS = [
"copilotkit"
];
const schemaKeys = await super.getSchemaKeys();
return {
config: schemaKeys.config,
input: schemaKeys.input ? [
...schemaKeys.input,
...CONSTANT_KEYS
] : null,
output: schemaKeys.output ? [
...schemaKeys.output,
...CONSTANT_KEYS
] : null,
context: schemaKeys.context ? [
...schemaKeys.context,
...CONSTANT_KEYS
] : null
};
}
};
__name(LangGraphAgent, "LangGraphAgent");
Object.defineProperty(exports, 'LangGraphHttpAgent', {
enumerable: true,
get: function () { return langgraph.LangGraphHttpAgent; }
});
exports.LangGraphAgent = LangGraphAgent;
//# sourceMappingURL=out.js.map
//# sourceMappingURL=langgraph.js.map