@agent-labs/ag-ui-server
Version:
AG-UI Node.js server with OpenAI integration
111 lines (109 loc) • 2.76 kB
JavaScript
import { __toESM, require_dist } from "./dist-B3mwKzb8.mjs";
//#region src/agent/handlers/tool-call.handler.ts
var import_dist = __toESM(require_dist());
var ToolCallHandler = class {
constructor(encoder) {
this.encoder = encoder;
}
async *handle(chunk, context) {
const toolCall = chunk.choices[0].delta.tool_calls?.[0];
if (!toolCall) return;
if (!context.isToolCallStarted) {
context.toolCallId = toolCall.id ?? "";
context.toolCallName = toolCall.function?.name ?? "";
context.isToolCallStarted = true;
const event = {
type: import_dist.EventType.TOOL_CALL_START,
toolCallId: context.toolCallId,
toolCallName: context.toolCallName,
toolCallArgs: "",
toolCalls: [{
id: context.toolCallId,
type: "function",
function: {
name: context.toolCallName,
arguments: ""
}
}],
messages: [{
id: context.messageId,
role: "assistant",
toolCalls: [{
id: context.toolCallId,
type: "function",
function: {
name: context.toolCallName,
arguments: ""
}
}]
}]
};
yield this.encoder.encode(event);
}
if (toolCall.function?.arguments) {
context.toolCallArgs += toolCall.function.arguments;
const event = {
type: import_dist.EventType.TOOL_CALL_ARGS,
toolCallId: context.toolCallId,
toolCallName: context.toolCallName,
toolCallArgs: context.toolCallArgs,
delta: toolCall.function?.arguments,
toolCalls: [{
id: context.toolCallId,
type: "function",
function: {
name: context.toolCallName,
arguments: context.toolCallArgs
}
}],
messages: [{
id: context.messageId,
role: "assistant",
toolCalls: [{
id: context.toolCallId,
type: "function",
function: {
name: context.toolCallName,
arguments: context.toolCallArgs
}
}]
}]
};
yield this.encoder.encode(event);
}
}
async *finalize(context) {
if (context.isToolCallStarted) {
const event = {
type: import_dist.EventType.TOOL_CALL_END,
toolCallId: context.toolCallId,
toolCallName: context.toolCallName,
toolCallArgs: context.toolCallArgs,
toolCalls: [{
id: context.toolCallId,
type: "function",
function: {
name: context.toolCallName,
arguments: context.toolCallArgs
}
}],
messages: [{
id: context.messageId,
role: "assistant",
toolCalls: [{
id: context.toolCallId,
type: "function",
function: {
name: context.toolCallName,
arguments: context.toolCallArgs
}
}]
}]
};
yield this.encoder.encode(event);
}
}
};
//#endregion
export { ToolCallHandler };
//# sourceMappingURL=tool-call.handler-CVx-QXEI.mjs.map