UNPKG

@autobe/agent

Version:

AI backend server code generator

357 lines 17.1 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.orchestrateCommonCorrectCasting = void 0; const __typia_transform__validateReport = __importStar(require("typia/lib/internal/_validateReport")); const __typia_transform__llmApplicationFinalize = __importStar(require("typia/lib/internal/_llmApplicationFinalize")); const typia_1 = __importDefault(require("typia")); const transformCommonCorrectCastingHistory_1 = require("./histories/transformCommonCorrectCastingHistory"); const orchestrateCommonCorrectCasting = (ctx, factory, script) => __awaiter(void 0, void 0, void 0, function* () { const event = yield compileWithFiltering({ compile: factory.compile, location: factory.location, script, }); return yield predicate(ctx, factory, [], script, event, 4 /* AutoBeConfigConstant.COMPILER_RETRY */); }); exports.orchestrateCommonCorrectCasting = orchestrateCommonCorrectCasting; const predicate = (ctx, factory, failures, script, event, life) => __awaiter(void 0, void 0, void 0, function* () { if (event.result.type === "failure") { ctx.dispatch(event); try { return yield correct(ctx, factory, failures, script, event, life - 1); } catch (error) { console.log("correctCasting", error); return yield correct(ctx, factory, failures, script, event, life - 2); } } return event; }); const correct = (ctx, factory, failures, script, event, life) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b; if (event.result.type !== "failure") return event; else if (life < 0) return event; const pointer = { value: null, }; const { metric, tokenUsage } = yield ctx.conversate(Object.assign({ source: factory.source, controller: createController({ factory: factory, then: (next) => { pointer.value = next; }, reject: () => { pointer.value = false; }, }), enforceFunctionCall: true }, (0, transformCommonCorrectCastingHistory_1.transformCommonCorrectCastingHistory)([...failures, event].map((e) => ({ diagnostics: e.result .diagnostics, script: factory.script(e), }))))); if (pointer.value === null) throw new Error("Failed to correct test code."); else if (pointer.value === false) return event; ctx.dispatch(yield factory.correct({ failure: event.result, think: pointer.value.think, draft: pointer.value.draft, review: pointer.value.revise.review, final: (_a = pointer.value.revise.final) !== null && _a !== void 0 ? _a : undefined, metric, tokenUsage, })); return yield predicate(ctx, factory, [...failures, event], script, yield compileWithFiltering({ compile: factory.compile, location: factory.location, script: (_b = pointer.value.revise.final) !== null && _b !== void 0 ? _b : pointer.value.draft, }), life - 1); }); const compileWithFiltering = (props) => __awaiter(void 0, void 0, void 0, function* () { const event = yield props.compile(props.script); if (event.result.type === "failure") { event.result.diagnostics = event.result.diagnostics.filter((d) => d.file === props.location); if (event.result.diagnostics.length === 0) event.result = { type: "success" }; } return event; }); const createController = (props) => { const validate = (input) => { const result = (() => { const _io0 = input => "string" === typeof input.think && "string" === typeof input.draft && ("object" === typeof input.revise && null !== input.revise && _io1(input.revise)); const _io1 = input => "string" === typeof input.review && (null === input.final || "string" === typeof input.final); const _vo0 = (input, _path, _exceptionable = true) => ["string" === typeof input.think || _report(_exceptionable, { path: _path + ".think", expected: "string", value: input.think }), "string" === typeof input.draft || _report(_exceptionable, { path: _path + ".draft", expected: "string", value: input.draft }), ("object" === typeof input.revise && null !== input.revise || _report(_exceptionable, { path: _path + ".revise", expected: "IAutoBeCommonCorrectCastingApplication.IReviseProps", value: input.revise })) && _vo1(input.revise, _path + ".revise", true && _exceptionable) || _report(_exceptionable, { path: _path + ".revise", expected: "IAutoBeCommonCorrectCastingApplication.IReviseProps", value: input.revise })].every(flag => flag); const _vo1 = (input, _path, _exceptionable = true) => ["string" === typeof input.review || _report(_exceptionable, { path: _path + ".review", expected: "string", value: input.review }), null === input.final || "string" === typeof input.final || _report(_exceptionable, { path: _path + ".final", expected: "(null | string)", value: input.final })].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: "IAutoBeCommonCorrectCastingApplication.IProps", value: input })) && _vo0(input, _path + "", true) || _report(true, { path: _path + "", expected: "IAutoBeCommonCorrectCastingApplication.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 }; }; })()(input); if (result.success === false) return result; const errors = props.factory.validateEmptyCode({ draft: result.data.draft, revise: result.data.revise, path: "$input", }); return errors.length ? { success: false, errors, data: result.data, } : result; }; const application = __typia_transform__llmApplicationFinalize._llmApplicationFinalize({ functions: [ { name: "rewrite", parameters: { description: "Current Type: {@link IAutoBeCommonCorrectCastingApplication.IProps}", type: "object", properties: { think: { description: "Analysis of the error pattern and chosen fix strategy.", type: "string" }, draft: { description: "Draft code with initial syntax/type fixes applied.", type: "string" }, revise: { description: "Self-review pass that checks corrections and produces final code.", $ref: "#/$defs/IAutoBeCommonCorrectCastingApplication.IReviseProps" } }, required: [ "think", "draft", "revise" ], additionalProperties: false, $defs: { "IAutoBeCommonCorrectCastingApplication.IReviseProps": { type: "object", properties: { review: { description: "Review of correction patterns applied and confirmation all errors\nresolved.", type: "string" }, final: { description: "Final corrected code, or `null` when draft already resolves all issues.", anyOf: [ { type: "null" }, { type: "string" } ] } }, required: [ "review", "final" ] } } }, description: "Rewrite code to fix severe syntax structure or type system errors.", validate: (() => { const _io0 = input => "string" === typeof input.think && "string" === typeof input.draft && ("object" === typeof input.revise && null !== input.revise && _io1(input.revise)); const _io1 = input => "string" === typeof input.review && (null === input.final || "string" === typeof input.final); const _vo0 = (input, _path, _exceptionable = true) => ["string" === typeof input.think || _report(_exceptionable, { path: _path + ".think", expected: "string", value: input.think }), "string" === typeof input.draft || _report(_exceptionable, { path: _path + ".draft", expected: "string", value: input.draft }), ("object" === typeof input.revise && null !== input.revise || _report(_exceptionable, { path: _path + ".revise", expected: "IAutoBeCommonCorrectCastingApplication.IReviseProps", value: input.revise })) && _vo1(input.revise, _path + ".revise", true && _exceptionable) || _report(_exceptionable, { path: _path + ".revise", expected: "IAutoBeCommonCorrectCastingApplication.IReviseProps", value: input.revise })].every(flag => flag); const _vo1 = (input, _path, _exceptionable = true) => ["string" === typeof input.review || _report(_exceptionable, { path: _path + ".review", expected: "string", value: input.review }), null === input.final || "string" === typeof input.final || _report(_exceptionable, { path: _path + ".final", expected: "(null | string)", value: input.final })].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: "IAutoBeCommonCorrectCastingApplication.IProps", value: input })) && _vo0(input, _path + "", true) || _report(true, { path: _path + "", expected: "IAutoBeCommonCorrectCastingApplication.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 }; }; })() }, { name: "reject", parameters: { type: "object", properties: {}, additionalProperties: false, required: [], $defs: {} }, description: "Reject when error is outside scope (handled by subsequent agents).", validate: (() => { const __is = input => true; let errors; let _report; return input => { if (false === __is(input)) { errors = []; _report = __typia_transform__validateReport._validateReport(errors); ((input, _path, _exceptionable = true) => true)(input, "$input", true); const success = 0 === errors.length; return success ? { success, data: input } : { success, errors, data: input }; } return { success: true, data: input }; }; })() } ] }, { validate: { rewrite: validate, reject: () => ({ success: true, data: undefined, }), }, }); return { protocol: "class", name: "correctInvalidRequest", application, execute: { rewrite: (next) => { props.then(next); }, reject: () => { props.reject(); }, }, }; }; //# sourceMappingURL=orchestrateCommonCorrectCasting.js.map