UNPKG

@autobe/agent

Version:

AI backend server code generator

100 lines 4.01 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimedConversation = void 0; const tstl_1 = require("tstl"); const AutoBeTimeoutError_1 = require("./AutoBeTimeoutError"); /** * Wraps LLM conversation with timeout enforcement and discriminated result * types. * * Prevents hanging indefinitely when LLM APIs become unresponsive or requests * take excessively long. Uses AbortController to cancel in-flight requests when * timeout expires. Returns discriminated union enabling callers to handle * success/timeout/error cases separately. * * @author Samchon */ var TimedConversation; (function (TimedConversation) { /** * Executes conversation with optional timeout enforcement. * * If timeout is null, executes without time limit. Otherwise, sets up timeout * handler that aborts the request and returns timeout result. Uses condition * variable for synchronization between conversation and timeout threads. * * @param props Agent, message, and timeout configuration * @returns Discriminated result indicating success, timeout, or error */ TimedConversation.process = (props) => __awaiter(this, void 0, void 0, function* () { if (props.timeout === null) try { const histories = yield props.agent.conversate(props.message); return { type: "success", histories, }; } catch (error) { return { type: "error", error: error, }; } // PREPARE TIMEOUT HANDLERS const result = { value: null, }; const holder = new tstl_1.ConditionVariable(); const abort = new AbortController(); const timeout = new tstl_1.Singleton(() => setTimeout(() => { if (result.value !== null) return; result.value = { type: "timeout", error: new AutoBeTimeoutError_1.AutoBeTimeoutError(`Timeout, over ${props.timeout} ms.`), }; abort.abort(`Timeout, over ${props.timeout} ms`); void holder.notify_all().catch(() => { }); }, props.timeout)); // DO CONVERSATE props.agent.on("request", () => { timeout.get(); }); props.agent .conversate(props.message, { abortSignal: abort.signal, }) .then((v) => { var _a; return ((_a = result.value) !== null && _a !== void 0 ? _a : (result.value = { type: "success", histories: v, })); }) .catch((e) => { var _a; return ((_a = result.value) !== null && _a !== void 0 ? _a : (result.value = { type: "error", error: e, })); }) .finally(() => { void holder.notify_all().catch(() => { }); clearTimeout(timeout.get()); }); yield holder.wait(); yield (0, tstl_1.sleep_for)(0); return result.value; }); })(TimedConversation || (exports.TimedConversation = TimedConversation = {})); //# sourceMappingURL=TimedConversation.js.map