@n8n/n8n-nodes-langchain
Version:

82 lines • 2.89 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var processEventStream_exports = {};
__export(processEventStream_exports, {
processEventStream: () => processEventStream
});
module.exports = __toCommonJS(processEventStream_exports);
async function processEventStream(ctx, eventStream, itemIndex) {
const agentResult = {
output: ""
};
const toolCalls = [];
ctx.sendChunk("begin", itemIndex);
for await (const event of eventStream) {
switch (event.event) {
case "on_chat_model_stream":
const chunk = event.data?.chunk;
if (chunk?.content) {
const chunkContent = chunk.content;
let chunkText = "";
if (Array.isArray(chunkContent)) {
for (const message of chunkContent) {
if (message?.type === "text") {
chunkText += message?.text;
}
}
} else if (typeof chunkContent === "string") {
chunkText = chunkContent;
}
ctx.sendChunk("item", itemIndex, chunkText);
agentResult.output += chunkText;
}
break;
case "on_chat_model_end":
if (event.data) {
const chatModelData = event.data;
const output = chatModelData.output;
if (output?.tool_calls && output.tool_calls.length > 0) {
for (const toolCall of output.tool_calls) {
toolCalls.push({
tool: toolCall.name,
toolInput: toolCall.args,
toolCallId: toolCall.id || "unknown",
type: toolCall.type || "tool_call",
log: output.content || `Calling ${toolCall.name} with input: ${JSON.stringify(toolCall.args)}`,
messageLog: [output]
});
}
}
}
break;
default:
break;
}
}
ctx.sendChunk("end", itemIndex);
if (toolCalls.length > 0) {
agentResult.toolCalls = toolCalls;
}
return agentResult;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
processEventStream
});
//# sourceMappingURL=processEventStream.js.map