@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;" />
146 lines (144 loc) • 5.25 kB
JavaScript
import "reflect-metadata";
import { LangGraphEventTypes } from "../../../../agents/langgraph/events.mjs";
import { CustomEventNames } from "./consts.mjs";
import { map } from "rxjs";
import { EventType } from "@ag-ui/client";
import { LangGraphAgent, LangGraphHttpAgent } from "@ag-ui/langgraph";
//#region src/lib/runtime/agent-integrations/langgraph/agent.ts
var LangGraphAgent$1 = class extends LangGraphAgent {
constructor(config) {
super(config);
}
dispatchEvent(event) {
if (event.type === EventType.CUSTOM) {
const customEvent = event;
if (customEvent.name === CustomEventNames.CopilotKitManuallyEmitMessage) {
this.subscriber.next({
type: EventType.TEXT_MESSAGE_START,
role: "assistant",
messageId: customEvent.value.message_id,
rawEvent: event
});
this.subscriber.next({
type: EventType.TEXT_MESSAGE_CONTENT,
messageId: customEvent.value.message_id,
delta: customEvent.value.message,
rawEvent: event
});
this.subscriber.next({
type: EventType.TEXT_MESSAGE_END,
messageId: customEvent.value.message_id,
rawEvent: event
});
return true;
}
if (customEvent.name === CustomEventNames.CopilotKitManuallyEmitToolCall) {
this.subscriber.next({
type: EventType.TOOL_CALL_START,
toolCallId: customEvent.value.id,
toolCallName: customEvent.value.name,
parentMessageId: customEvent.value.id,
rawEvent: event
});
this.subscriber.next({
type: EventType.TOOL_CALL_ARGS,
toolCallId: customEvent.value.id,
delta: customEvent.value.args,
rawEvent: event
});
this.subscriber.next({
type: EventType.TOOL_CALL_END,
toolCallId: customEvent.value.id,
rawEvent: event
});
return true;
}
if (customEvent.name === CustomEventNames.CopilotKitManuallyEmitIntermediateState) {
this.activeRun.manuallyEmittedState = customEvent.value;
this.dispatchEvent({
type: EventType.STATE_SNAPSHOT,
snapshot: this.getStateSnapshot({ values: this.activeRun.manuallyEmittedState }),
rawEvent: event
});
return true;
}
if (customEvent.name === CustomEventNames.CopilotKitExit) {
this.subscriber.next({
type: EventType.CUSTOM,
name: "Exit",
value: true
});
return true;
}
}
const rawEvent = event.rawEvent;
if (!rawEvent) {
this.subscriber.next(event);
return true;
}
const isMessageEvent = event.type === EventType.TEXT_MESSAGE_START || event.type === EventType.TEXT_MESSAGE_CONTENT || event.type === EventType.TEXT_MESSAGE_END;
const isToolEvent = event.type === EventType.TOOL_CALL_START || event.type === EventType.TOOL_CALL_ARGS || event.type === 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) {
if (this.activeRun?.id) this.messagesInProcess[this.activeRun.id] = null;
return false;
}
}
this.subscriber.next(event);
return true;
}
run(input) {
const messages = (input.messages ?? []).filter((m) => m?.role !== "reasoning");
const enrichedInput = {
...input,
messages,
forwardedProps: {
...input.forwardedProps,
streamSubgraphs: input.forwardedProps?.streamSubgraphs ?? true
}
};
return super.run(enrichedInput).pipe(map((processedEvent) => {
if (processedEvent.type === EventType.RAW) {
const event = processedEvent.event ?? processedEvent.rawEvent;
const eventType = event.event;
const toolCallData = event.data?.chunk?.tool_call_chunks?.[0];
const toolCallUsedToPredictState = event.metadata?.["copilotkit:emit-intermediate-state"]?.some((predictStateTool) => predictStateTool.tool === toolCallData?.name);
if (eventType === LangGraphEventTypes.OnChatModelStream && toolCallUsedToPredictState) return {
type: 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?.tools ?? []];
const combinedTools = Array.from(new Map(rawCombinedTools.map((t) => [t?.id ?? t?.name ?? t?.key ?? JSON.stringify(t), t])).values());
return {
...aguiMergedState,
copilotkit: {
actions: combinedTools,
context: 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
};
}
};
//#endregion
export { LangGraphAgent$1 as LangGraphAgent, LangGraphHttpAgent };
//# sourceMappingURL=agent.mjs.map