UNPKG

@autobe/agent

Version:

AI backend server code generator

358 lines 16.8 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.orchestrateImageDescribeDrafts = void 0; const __typia_transform__validateReport = __importStar(require("typia/lib/internal/_validateReport")); const __typia_transform__llmApplicationFinalize = __importStar(require("typia/lib/internal/_llmApplicationFinalize")); const core_1 = require("@agentica/core"); const typia_1 = __importDefault(require("typia")); const uuid_1 = require("uuid"); const createAutoBeMessageContent_1 = require("../../factory/createAutoBeMessageContent"); const mergeSystemMessages_1 = require("../../factory/mergeSystemMessages"); // import { supportFunctionCallFallback } from "../../factory/supportFunctionCallFallback"; const supportMistral_1 = require("../../factory/supportMistral"); const executeCachedBatch_1 = require("../../utils/executeCachedBatch"); const transformImageDescribeDraftHistories_1 = require("./histories/transformImageDescribeDraftHistories"); const orchestrateImageDescribeDrafts = (ctx, props) => __awaiter(void 0, void 0, void 0, function* () { const [imageContents, otherContents] = props.content.reduce((acc, cur) => { if (cur.type === "image") { acc[0].push(cur); } else { acc[1].push(cur); } return acc; }, [ [], [], ]); const progress = { total: imageContents.length, completed: 0, }; // Process each image individually const drafts = yield (0, executeCachedBatch_1.executeCachedBatch)(ctx, imageContents.map((imageContent) => (promptCacheKey) => __awaiter(void 0, void 0, void 0, function* () { try { const event = yield process(ctx, { imageContents: [imageContent], // Single image userContents: otherContents, progress, promptCacheKey, }); ctx.dispatch(event); return Object.assign(Object.assign({}, event), { image: imageContent, description: event.draft }); } catch (_a) { return null; } }))); return drafts.filter((d) => d !== null); }); exports.orchestrateImageDescribeDrafts = orchestrateImageDescribeDrafts; function process(ctx, props) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const pointer = { value: null, }; const content = [ ...props.imageContents, ...props.userContents, ]; const agent = new core_1.MicroAgentica({ vendor: ctx.vendor, config: { executor: { describe: false, }, retry: ctx.retry, // stream: false, }, histories: (0, transformImageDescribeDraftHistories_1.transformImageDescribeDraftHistories)(), controllers: [ createController({ build: (next) => { pointer.value = next; }, }), ], }); (0, supportMistral_1.supportMistral)(agent, { api: ctx.vendor.api, model: ctx.vendor.model, options: ctx.vendor.options, semaphore: typeof ctx.vendor.semaphore === "number" ? ctx.vendor.semaphore : (_a = ctx.vendor.semaphore) === null || _a === void 0 ? void 0 : _a.max(), }); // supportFunctionCallFallback(agent, { // api: ctx.vendor.api, // model: ctx.vendor.model, // options: ctx.vendor.options, // semaphore: // typeof ctx.vendor.semaphore === "number" // ? ctx.vendor.semaphore // : ctx.vendor.semaphore?.max(), // }); (0, mergeSystemMessages_1.mergeSystemMessages)(agent, { api: ctx.vendor.api, model: ctx.vendor.model, options: ctx.vendor.options, semaphore: typeof ctx.vendor.semaphore === "number" ? ctx.vendor.semaphore : (_b = ctx.vendor.semaphore) === null || _b === void 0 ? void 0 : _b.max(), }); agent.on("request", (e) => { var _a; if (!!((_a = e.body.tools) === null || _a === void 0 ? void 0 : _a.length)) { e.body.tool_choice = "required"; } }); yield agent.conversate(content.map((c) => (0, createAutoBeMessageContent_1.createAutoBeUserMessageContent)({ content: c }))); const tokenUsage = agent .getTokenUsage() .toJSON().aggregate; ctx.usage().record(tokenUsage, ["facade"]); props.progress.completed += props.imageContents.length; if (pointer.value === null) throw new Error("Failed to analyze image."); const event = { type: "imageDescribeDraft", id: (0, uuid_1.v7)(), observation: pointer.value.observation, analysis: pointer.value.analysis, topics: pointer.value.topics, summary: pointer.value.summary, draft: pointer.value.description, completed: props.progress.completed, tokenUsage, total: props.progress.total, created_at: new Date().toISOString(), }; return event; }); } function createController(props) { const validate = (next) => { const result = (() => { const _io0 = input => "string" === typeof input.observation && "string" === typeof input.analysis && (Array.isArray(input.topics) && input.topics.every(elem => "string" === typeof elem)) && "string" === typeof input.summary && "string" === typeof input.description; const _vo0 = (input, _path, _exceptionable = true) => ["string" === typeof input.observation || _report(_exceptionable, { path: _path + ".observation", expected: "string", value: input.observation }), "string" === typeof input.analysis || _report(_exceptionable, { path: _path + ".analysis", expected: "string", value: input.analysis }), (Array.isArray(input.topics) || _report(_exceptionable, { path: _path + ".topics", expected: "Array<string>", value: input.topics })) && input.topics.map((elem, _index2) => "string" === typeof elem || _report(_exceptionable, { path: _path + ".topics[" + _index2 + "]", expected: "string", value: elem })).every(flag => flag) || _report(_exceptionable, { path: _path + ".topics", expected: "Array<string>", value: input.topics }), "string" === typeof input.summary || _report(_exceptionable, { path: _path + ".summary", expected: "string", value: input.summary }), "string" === typeof input.description || _report(_exceptionable, { path: _path + ".description", expected: "string", value: input.description })].every(flag => flag); const __is = input => "object" === typeof input && null !== input && _io0(input); let errors; let _report; return input => { if (false === __is(input)) { errors = []; _report = __typia_transform__validateReport._validateReport(errors); ((input, _path, _exceptionable = true) => ("object" === typeof input && null !== input || _report(true, { path: _path + "", expected: "IAutoBeImageDescribeDraftApplication.IProps", value: input })) && _vo0(input, _path + "", true) || _report(true, { path: _path + "", expected: "IAutoBeImageDescribeDraftApplication.IProps", value: input }))(input, "$input", true); const success = 0 === errors.length; return success ? { success, data: input } : { success, errors, data: input }; } return { success: true, data: input }; }; })()(next); if (result.success === false) return result; return result; }; const application = __typia_transform__llmApplicationFinalize._llmApplicationFinalize({ functions: [ { name: "analyzeImage", parameters: { description: "Current Type: {@link IAutoBeImageDescribeDraftApplication.IProps}", type: "object", properties: { observation: { description: "Step 1: Raw observation of all visible elements (objects, text, UI,\nlayout) without interpretation.", type: "string" }, analysis: { description: "Step 2: Interpret observed elements \u2014 purpose, relationships, workflows,\ndomain context.", type: "string" }, topics: { description: "Step 3: 3-5 main topics/themes in kebab-case (e.g., \"user-dashboard\",\n\"payment-flow\").", type: "array", items: { type: "string" } }, summary: { description: "Step 4: Concise 2-3 sentence summary capturing what the image shows and\nits purpose.", type: "string" }, description: { description: "Step 5: Comprehensive markdown description enabling understanding without\nseeing the image.", type: "string" } }, required: [ "observation", "analysis", "topics", "summary", "description" ], additionalProperties: false, $defs: {} }, description: "Analyze an image through observation, interpretation, and documentation\nphases.", validate: (() => { const _io0 = input => "string" === typeof input.observation && "string" === typeof input.analysis && (Array.isArray(input.topics) && input.topics.every(elem => "string" === typeof elem)) && "string" === typeof input.summary && "string" === typeof input.description; const _vo0 = (input, _path, _exceptionable = true) => ["string" === typeof input.observation || _report(_exceptionable, { path: _path + ".observation", expected: "string", value: input.observation }), "string" === typeof input.analysis || _report(_exceptionable, { path: _path + ".analysis", expected: "string", value: input.analysis }), (Array.isArray(input.topics) || _report(_exceptionable, { path: _path + ".topics", expected: "Array<string>", value: input.topics })) && input.topics.map((elem, _index2) => "string" === typeof elem || _report(_exceptionable, { path: _path + ".topics[" + _index2 + "]", expected: "string", value: elem })).every(flag => flag) || _report(_exceptionable, { path: _path + ".topics", expected: "Array<string>", value: input.topics }), "string" === typeof input.summary || _report(_exceptionable, { path: _path + ".summary", expected: "string", value: input.summary }), "string" === typeof input.description || _report(_exceptionable, { path: _path + ".description", expected: "string", value: input.description })].every(flag => flag); const __is = input => "object" === typeof input && null !== input && _io0(input); let errors; let _report; return input => { if (false === __is(input)) { errors = []; _report = __typia_transform__validateReport._validateReport(errors); ((input, _path, _exceptionable = true) => ("object" === typeof input && null !== input || _report(true, { path: _path + "", expected: "IAutoBeImageDescribeDraftApplication.IProps", value: input })) && _vo0(input, _path + "", true) || _report(true, { path: _path + "", expected: "IAutoBeImageDescribeDraftApplication.IProps", value: input }))(input, "$input", true); const success = 0 === errors.length; return success ? { success, data: input } : { success, errors, data: input }; } return { success: true, data: input }; }; })() } ] }, { validate: { analyzeImage: validate, }, }); return { protocol: "class", name: "image", application, execute: { analyzeImage: (next) => { props.build(next); }, }, }; } //# sourceMappingURL=orchestrateImageDescribeDraft.js.map