UNPKG

@autobe/agent

Version:

AI backend server code generator

422 lines 24.5 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.orchestrateInterfaceSchemaDecouple = void 0; const __typia_transform__validateReport = __importStar(require("typia/lib/internal/_validateReport")); const __typia_transform__llmApplicationFinalize = __importStar(require("typia/lib/internal/_llmApplicationFinalize")); const tstl_1 = require("tstl"); const typia_1 = __importDefault(require("typia")); const uuid_1 = require("uuid"); const executeCachedBatch_1 = require("../../utils/executeCachedBatch"); const transformInterfaceSchemaDecoupleHistory_1 = require("./histories/transformInterfaceSchemaDecoupleHistory"); const AutoBeInterfaceSchemaDecoupleProgrammer_1 = require("./programmers/AutoBeInterfaceSchemaDecoupleProgrammer"); const fulfillJsonSchemaErrorMessages_1 = require("./utils/fulfillJsonSchemaErrorMessages"); /** * Detect and resolve cross-type circular references in schemas. * * Uses a while loop with parallel processing per round: * * - Each round: detect remaining cycles → process ALL in parallel (one LLM call * per cycle) → re-detect → repeat until no cycles remain. * * WHY parallel within a round: Tarjan's SCCs are mathematically disjoint. Two * detected SCCs share no nodes, so fixing SCC-A never creates or destroys edges * in SCC-B. Parallel execution is safe. * * WHY while loop across rounds: a large SCC (3+ nodes with multiple independent * internal cycles) may not be fully resolved in one pass — validate only * requires "at least one edge removed" per SCC. After that partial fix, the SCC * may split into smaller cycles that re-detect in the next round. Convergence * is guaranteed: each round removes ≥1 edge, so the total cycle-edge count * decreases monotonically to zero. * * Mutates `props.schemas` in-place. Dispatches one event per cycle. */ const orchestrateInterfaceSchemaDecouple = (ctx, props) => __awaiter(void 0, void 0, void 0, function* () { const progress = { total: 0, completed: 0, }; while (true) { const cycles = AutoBeInterfaceSchemaDecoupleProgrammer_1.AutoBeInterfaceSchemaDecoupleProgrammer.detectCycles(props.schemas); if (cycles.length === 0) break; progress.total += cycles.length; yield (0, executeCachedBatch_1.executeCachedBatch)(ctx, cycles.map((c) => (promptCacheKey) => __awaiter(void 0, void 0, void 0, function* () { const counter = new tstl_1.Singleton(() => ++progress.completed); try { return yield process(ctx, { schemas: props.schemas, operations: props.operations, cycle: c, progress, counter, promptCacheKey, }); } catch (error) { counter.get(); throw error; } }))); } }); exports.orchestrateInterfaceSchemaDecouple = orchestrateInterfaceSchemaDecouple; function process(ctx, props) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; const pointer = { value: null }; const result = yield ctx.conversate(Object.assign({ source: SOURCE, controller: createController({ schemas: props.schemas, cycle: props.cycle, pointer, }), enforceFunctionCall: true, promptCacheKey: props.promptCacheKey }, (0, transformInterfaceSchemaDecoupleHistory_1.transformInterfaceSchemaDecoupleHistory)({ schemas: props.schemas, operations: props.operations, cycle: props.cycle, }))); if (pointer.value === null) throw new Error("interfaceSchemaDecouple: agent failed to produce a result"); // Apply removal decision to the schema const removal = (_a = pointer.value.final) !== null && _a !== void 0 ? _a : pointer.value.draft; AutoBeInterfaceSchemaDecoupleProgrammer_1.AutoBeInterfaceSchemaDecoupleProgrammer.execute({ schemas: props.schemas, removal, }); // Emit per-cycle event ctx.dispatch({ type: SOURCE, id: (0, uuid_1.v7)(), cycle: props.cycle, removal, analysis: pointer.value.review, metric: result.metric, tokenUsage: result.tokenUsage, step: (_c = (_b = ctx.state().analyze) === null || _b === void 0 ? void 0 : _b.step) !== null && _c !== void 0 ? _c : 0, total: props.progress.total, completed: props.counter.get(), created_at: new Date().toISOString(), }); }); } function createController(props) { const validate = (next) => { var _a; const result = (() => { const _io0 = input => "string" === typeof input.thinking && ("object" === typeof input.draft && null !== input.draft && _io1(input.draft)) && "string" === typeof input.review && (null === input.final || "object" === typeof input.final && null !== input.final && _io1(input.final)); const _io1 = input => "string" === typeof input.reason && "string" === typeof input.typeName && "string" === typeof input.propertyName && (null === input.description || "string" === typeof input.description) && (null === input.specification || "string" === typeof input.specification); const _vo0 = (input, _path, _exceptionable = true) => ["string" === typeof input.thinking || _report(_exceptionable, { path: _path + ".thinking", expected: "string", value: input.thinking }), ("object" === typeof input.draft && null !== input.draft || _report(_exceptionable, { path: _path + ".draft", expected: "AutoBeInterfaceSchemaDecoupleRemoval", value: input.draft })) && _vo1(input.draft, _path + ".draft", true && _exceptionable) || _report(_exceptionable, { path: _path + ".draft", expected: "AutoBeInterfaceSchemaDecoupleRemoval", value: input.draft }), "string" === typeof input.review || _report(_exceptionable, { path: _path + ".review", expected: "string", value: input.review }), null === input.final || ("object" === typeof input.final && null !== input.final || _report(_exceptionable, { path: _path + ".final", expected: "(AutoBeInterfaceSchemaDecoupleRemoval | null)", value: input.final })) && _vo1(input.final, _path + ".final", true && _exceptionable) || _report(_exceptionable, { path: _path + ".final", expected: "(AutoBeInterfaceSchemaDecoupleRemoval | null)", value: input.final })].every(flag => flag); const _vo1 = (input, _path, _exceptionable = true) => ["string" === typeof input.reason || _report(_exceptionable, { path: _path + ".reason", expected: "string", value: input.reason }), "string" === typeof input.typeName || _report(_exceptionable, { path: _path + ".typeName", expected: "string", value: input.typeName }), "string" === typeof input.propertyName || _report(_exceptionable, { path: _path + ".propertyName", expected: "string", value: input.propertyName }), null === input.description || "string" === typeof input.description || _report(_exceptionable, { path: _path + ".description", expected: "(null | string)", value: input.description }), null === input.specification || "string" === typeof input.specification || _report(_exceptionable, { path: _path + ".specification", expected: "(null | string)", value: input.specification })].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: "IAutoBeInterfaceSchemaDecoupleApplication.IProps", value: input })) && _vo0(input, _path + "", true) || _report(true, { path: _path + "", expected: "IAutoBeInterfaceSchemaDecoupleApplication.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) { (0, fulfillJsonSchemaErrorMessages_1.fulfillJsonSchemaErrorMessages)(result.errors); return result; } const errors = []; AutoBeInterfaceSchemaDecoupleProgrammer_1.AutoBeInterfaceSchemaDecoupleProgrammer.validate({ schemas: props.schemas, cycle: props.cycle, removal: (_a = result.data.final) !== null && _a !== void 0 ? _a : result.data.draft, errors, path: "$input", }); return errors.length ? { success: false, errors, data: result.data } : result; }; const application = __typia_transform__llmApplicationFinalize._llmApplicationFinalize({ functions: [ { name: "process", parameters: { description: "LLM response for one circular reference cycle.\n\nFields are ordered for chain-of-thought generation:\n\n1. `thinking` \u2014 initial reasoning about the cycle\n2. `draft` \u2014 first candidate removal based on that reasoning\n3. `review` \u2014 critical reflection on the draft\n4. `final` \u2014 refined removal, or `null` if the draft was already correct\n\nThe effective removal used is `final ?? draft`.\n\n------------------------------\n\nDescription of the current {@link IAutoBeInterfaceSchemaDecoupleApplication.IProps} type:\n\n> LLM response for one circular reference cycle.\n> \n> Fields are ordered for chain-of-thought generation:\n> \n> 1. `thinking` \u2014 initial reasoning about the cycle\n> 2. `draft` \u2014 first candidate removal based on that reasoning\n> 3. `review` \u2014 critical reflection on the draft\n> 4. `final` \u2014 refined removal, or `null` if the draft was already correct\n> \n> The effective removal used is `final ?? draft`.", type: "object", properties: { thinking: { description: "Initial reasoning about this cycle and the resolution strategy.\n\nThink through which edge is semantically least important, which direction\nthe reference should flow, and whether any documentation will need\nupdating after the removal.", type: "string" }, draft: { description: "First candidate removal based on the initial reasoning.\n\nMust correspond to an edge in the detected cycle (typeName.propertyName).\nInclude `description`/`specification` if the schema docs reference the\nremoved property.", $ref: "#/$defs/AutoBeInterfaceSchemaDecoupleRemoval" }, review: { description: "Critical review of the draft.\n\nRe-examine the draft: Is the chosen edge truly the least semantically\nimportant? Does removing it actually break the cycle? Are the doc updates\ncorrect? Use this field to catch mistakes before committing.", type: "string" }, final: { description: "Final removal decision after reviewing the draft, or `null` if the draft\nwas already correct and needs no revision.\n\nThe effective removal applied is `final ?? draft`. Return `null` here to\navoid redundant repetition when the draft required no change.", anyOf: [ { type: "null" }, { $ref: "#/$defs/AutoBeInterfaceSchemaDecoupleRemoval" } ] } }, required: [ "thinking", "draft", "review", "final" ], additionalProperties: false, $defs: { AutoBeInterfaceSchemaDecoupleRemoval: { description: "A property removal that breaks a circular reference cycle.\n\nProduced by the LLM Decouple agent after analyzing cross-type circular\nreferences and choosing which edge to cut based on semantic importance,\nreference direction, and DTO purpose.\n\nFields are ordered for chain-of-thought generation:\n\n1. `reason` \u2014 commit to WHY first\n2. `typeName` / `propertyName` \u2014 then commit to WHAT\n3. `description` / `specification` \u2014 finally update docs if needed", type: "object", properties: { reason: { description: "Reason for removing this specific edge.\n\nWritten first so the LLM commits to the rationale before deciding which\nexact property to remove.", type: "string" }, typeName: { description: "Schema type that owns the property to remove.", type: "string" }, propertyName: { description: "Property name to delete from the schema.", type: "string" }, description: { description: "Updated `description` for the schema identified by `typeName`, or `null` if\nthe existing description does not reference the removed property and needs\nno change.", anyOf: [ { type: "null" }, { type: "string" } ] }, specification: { description: "Updated `x-autobe-specification` for the schema identified by `typeName`,\nor `null` if the existing specification does not reference the removed\nproperty and needs no change.", anyOf: [ { type: "null" }, { type: "string" } ] } }, required: [ "reason", "typeName", "propertyName", "description", "specification" ] } } }, description: "Resolve cross-type circular references by selecting the property to remove.", validate: (() => { const _io0 = input => "string" === typeof input.thinking && ("object" === typeof input.draft && null !== input.draft && _io1(input.draft)) && "string" === typeof input.review && (null === input.final || "object" === typeof input.final && null !== input.final && _io1(input.final)); const _io1 = input => "string" === typeof input.reason && "string" === typeof input.typeName && "string" === typeof input.propertyName && (null === input.description || "string" === typeof input.description) && (null === input.specification || "string" === typeof input.specification); const _vo0 = (input, _path, _exceptionable = true) => ["string" === typeof input.thinking || _report(_exceptionable, { path: _path + ".thinking", expected: "string", value: input.thinking }), ("object" === typeof input.draft && null !== input.draft || _report(_exceptionable, { path: _path + ".draft", expected: "AutoBeInterfaceSchemaDecoupleRemoval", value: input.draft })) && _vo1(input.draft, _path + ".draft", true && _exceptionable) || _report(_exceptionable, { path: _path + ".draft", expected: "AutoBeInterfaceSchemaDecoupleRemoval", value: input.draft }), "string" === typeof input.review || _report(_exceptionable, { path: _path + ".review", expected: "string", value: input.review }), null === input.final || ("object" === typeof input.final && null !== input.final || _report(_exceptionable, { path: _path + ".final", expected: "(AutoBeInterfaceSchemaDecoupleRemoval | null)", value: input.final })) && _vo1(input.final, _path + ".final", true && _exceptionable) || _report(_exceptionable, { path: _path + ".final", expected: "(AutoBeInterfaceSchemaDecoupleRemoval | null)", value: input.final })].every(flag => flag); const _vo1 = (input, _path, _exceptionable = true) => ["string" === typeof input.reason || _report(_exceptionable, { path: _path + ".reason", expected: "string", value: input.reason }), "string" === typeof input.typeName || _report(_exceptionable, { path: _path + ".typeName", expected: "string", value: input.typeName }), "string" === typeof input.propertyName || _report(_exceptionable, { path: _path + ".propertyName", expected: "string", value: input.propertyName }), null === input.description || "string" === typeof input.description || _report(_exceptionable, { path: _path + ".description", expected: "(null | string)", value: input.description }), null === input.specification || "string" === typeof input.specification || _report(_exceptionable, { path: _path + ".specification", expected: "(null | string)", value: input.specification })].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: "IAutoBeInterfaceSchemaDecoupleApplication.IProps", value: input })) && _vo0(input, _path + "", true) || _report(true, { path: _path + "", expected: "IAutoBeInterfaceSchemaDecoupleApplication.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: { process: validate, }, }); AutoBeInterfaceSchemaDecoupleProgrammer_1.AutoBeInterfaceSchemaDecoupleProgrammer.fixApplication({ application, cycle: props.cycle, }); return { protocol: "class", name: SOURCE, application, execute: { process: (input) => { props.pointer.value = input; }, }, }; } const SOURCE = "interfaceSchemaDecouple"; //# sourceMappingURL=orchestrateInterfaceSchemaDecouple.js.map