@agent-labs/ag-ui-server
Version:
AG-UI Node.js server with OpenAI integration
154 lines (148 loc) • 4.85 kB
JavaScript
import { __toESM, require_dist } from "./dist-B3mwKzb8.mjs";
import { TextMessageHandler } from "./text-message.handler-AFvULw9J.mjs";
import { randomFillSync, randomUUID } from "crypto";
//#region node_modules/uuid/dist/esm/stringify.js
const byteToHex = [];
for (let i = 0; i < 256; ++i) byteToHex.push((i + 256).toString(16).slice(1));
function unsafeStringify(arr, offset = 0) {
return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
}
//#endregion
//#region node_modules/uuid/dist/esm/rng.js
const rnds8Pool = new Uint8Array(256);
let poolPtr = rnds8Pool.length;
function rng() {
if (poolPtr > rnds8Pool.length - 16) {
randomFillSync(rnds8Pool);
poolPtr = 0;
}
return rnds8Pool.slice(poolPtr, poolPtr += 16);
}
//#endregion
//#region node_modules/uuid/dist/esm/native.js
var native_default = { randomUUID };
//#endregion
//#region node_modules/uuid/dist/esm/v4.js
function v4(options, buf, offset) {
if (native_default.randomUUID && !buf && !options) return native_default.randomUUID();
options = options || {};
const rnds = options.random ?? options.rng?.() ?? rng();
if (rnds.length < 16) throw new Error("Random bytes length must be >= 16");
rnds[6] = rnds[6] & 15 | 64;
rnds[8] = rnds[8] & 63 | 128;
if (buf) {
offset = offset || 0;
if (offset < 0 || offset + 16 > buf.length) throw new RangeError(`UUID byte range ${offset}:${offset + 15} is out of buffer bounds`);
for (let i = 0; i < 16; ++i) buf[offset + i] = rnds[i];
return buf;
}
return unsafeStringify(rnds);
}
var v4_default = v4;
//#endregion
//#region src/agent/stream-processor.ts
var import_dist = __toESM(require_dist());
var StreamProcessor = class {
handlers = /* @__PURE__ */ new Map();
context;
constructor(encoder, inputData) {
this.encoder = encoder;
this.inputData = inputData;
this.context = {
messageId: v4_default(),
toolCallId: "",
isMessageStarted: false,
isToolCallStarted: false,
fullResponse: "",
toolCallArgs: "",
toolCallName: "",
getSnapshot: () => ({
last_response: this.context.fullResponse,
last_tool_call: this.context.isToolCallStarted ? {
name: this.context.toolCallName,
arguments: this.context.toolCallArgs
} : null,
usage: {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0
}
})
};
}
addHandler(type, handler) {
this.handlers.set(type, handler);
}
async *process(stream) {
try {
for await (const chunk of stream) {
const type = this.getChunkType(chunk);
const handler = this.handlers.get(type);
if (handler) yield* handler.handle(chunk, this.context);
}
for (const handler of this.handlers.values()) yield* handler.finalize(this.context);
const event = {
type: import_dist.EventType.STATE_SNAPSHOT,
snapshot: this.context.getSnapshot(),
content: this.context.fullResponse,
toolCalls: this.context.isToolCallStarted ? [{
id: this.context.toolCallId,
type: "function",
function: {
name: this.context.toolCallName,
arguments: this.context.toolCallArgs
}
}] : void 0,
messages: [{
id: this.context.messageId,
role: "assistant",
content: this.context.fullResponse,
toolCalls: this.context.isToolCallStarted ? [{
id: this.context.toolCallId,
type: "function",
function: {
name: this.context.toolCallName,
arguments: this.context.toolCallArgs
}
}] : void 0
}]
};
yield this.encoder.encode(event);
} catch (error) {
yield* this.handleError(error);
}
}
getChunkType(chunk) {
if (chunk.choices[0].delta.tool_calls) return "tool";
if (chunk.choices[0].delta.content) return "text";
return "unknown";
}
async *handleError(error) {
const errorHandler = new TextMessageHandler(this.encoder);
yield* errorHandler.handle({
id: "",
created: 0,
model: "",
object: "chat.completion.chunk",
choices: [{
delta: { content: `Error: ${error.message}` },
finish_reason: "stop",
index: 0
}],
usage: {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0
}
}, this.context);
yield* errorHandler.finalize(this.context);
const event = {
type: import_dist.EventType.RUN_ERROR,
error: { message: error.message }
};
yield this.encoder.encode(event);
}
};
//#endregion
export { StreamProcessor };
//# sourceMappingURL=stream-processor-DCsfEpfk.mjs.map