@autobe/agent
Version:
AI backend server code generator
272 lines (254 loc) • 8.71 kB
text/typescript
import {
AutoBeEventSource,
AutoBeInterfaceHistory,
AutoBeOpenApi,
AutoBeProgressEventBase,
AutoBeRealizePlanEvent,
AutoBeRealizeTransformerPlan,
} from "@autobe/interface";
import { StringUtil } from "@autobe/utils";
import { IPointer, Singleton } from "tstl";
import typia, { ILlmApplication, ILlmController, IValidation } from "typia";
import { v4 } from "uuid";
import { AutoBeContext } from "../../context/AutoBeContext";
import { buildAnalysisContextSections } from "../../utils/RAGRetrieval";
import { executeCachedBatch } from "../../utils/executeCachedBatch";
import { forceRetry } from "../../utils/forceRetry";
import { getEmbedder } from "../../utils/getEmbedder";
import { AutoBePreliminaryController } from "../common/AutoBePreliminaryController";
import { convertToSectionEntries } from "../common/internal/convertToSectionEntries";
import { IAnalysisSectionEntry } from "../common/structures/IAnalysisSectionEntry";
import { transformRealizeTransformerPlanHistory } from "./histories/transformRealizeTransformerPlanHistory";
import { AutoBeRealizeTransformerProgrammer } from "./programmers/AutoBeRealizeTransformerProgrammer";
import { IAutoBeRealizeTransformerPlanApplication } from "./structures/IAutoBeRealizeTransformerPlanApplication";
export async function orchestrateRealizeTransformerPlan(
ctx: AutoBeContext,
props: {
progress: AutoBeProgressEventBase;
},
): Promise<AutoBeRealizeTransformerPlan[]> {
const history: AutoBeInterfaceHistory | null = ctx.state().interface;
if (history === null)
throw new Error("Cannot realize transformer write without interface.");
const document: AutoBeOpenApi.IDocument = history.document;
const dtoTypeNames: string[] = Object.keys(
document.components.schemas,
).filter((key) =>
AutoBeRealizeTransformerProgrammer.filter({
schemas: document.components.schemas,
key,
}),
);
const prismaSchemaNames: Set<string> = new Set(
ctx
.state()
.database!.result.data.files.map((f) => f.models)
.flat()
.map((m) => m.name),
);
const result: AutoBeRealizeTransformerPlan[][] = await executeCachedBatch(
ctx,
Array.from(dtoTypeNames).map((it) => async (promptCacheKey) => {
const counter = new Singleton(() => ++props.progress.completed);
try {
return await forceRetry(() =>
process(ctx, {
document,
dtoTypeName: it,
prismaSchemaNames,
promptCacheKey,
progress: props.progress,
counter,
}),
);
} catch (error) {
counter.get();
throw error;
}
}),
);
return result.flat();
}
async function process(
ctx: AutoBeContext,
props: {
document: AutoBeOpenApi.IDocument;
dtoTypeName: string;
prismaSchemaNames: Set<string>;
promptCacheKey: string;
progress: AutoBeProgressEventBase;
counter: Singleton<number>;
},
): Promise<AutoBeRealizeTransformerPlan[]> {
const allSections: IAnalysisSectionEntry[] = convertToSectionEntries(
ctx.state().analyze?.files ?? [],
);
const queryText: string = [
"transformer",
"plan",
"dto",
"prisma",
props.dtoTypeName,
].join(" ");
const ragSections: IAnalysisSectionEntry[] =
await buildAnalysisContextSections(
getEmbedder(),
allSections,
queryText,
"TOPK",
{ log: false, logPrefix: "realizeTransformerPlan" },
);
const preliminary: AutoBePreliminaryController<
"analysisSections" | "databaseSchemas" | "interfaceSchemas" | "complete"
> = new AutoBePreliminaryController({
dispatch: (e) => ctx.dispatch(e),
state: ctx.state(),
source: SOURCE,
application:
typia.json.application<IAutoBeRealizeTransformerPlanApplication>(),
kinds: [
"analysisSections",
"databaseSchemas",
"interfaceSchemas",
"complete",
],
local: {
analysisSections: ragSections,
interfaceSchemas: Object.fromEntries(
Object.entries(props.document.components.schemas).filter(
([key]) => key === props.dtoTypeName,
),
),
},
});
const event: AutoBeRealizePlanEvent = await preliminary.orchestrate(
ctx,
async (out) => {
const pointer: IPointer<IAutoBeRealizeTransformerPlanApplication.IWrite | null> =
{
value: null,
};
const result: AutoBeContext.IResult = await ctx.conversate({
source: "realizePlan",
controller: createController({
prismaSchemaNames: props.prismaSchemaNames,
dtoTypeName: props.dtoTypeName,
build: (next) => {
pointer.value = next;
},
preliminary,
}),
enforceFunctionCall: true,
promptCacheKey: props.promptCacheKey,
...transformRealizeTransformerPlanHistory({
state: ctx.state(),
preliminary,
dtoTypeName: props.dtoTypeName,
}),
});
if (pointer.value === null) return out(result)(null);
const plans: AutoBeRealizeTransformerPlan[] = pointer.value.plans
.filter((p) => p.databaseSchemaName !== null)
.map((p) => ({
type: "transformer",
dtoTypeName: p.dtoTypeName,
thinking: p.thinking,
databaseSchemaName: p.databaseSchemaName!,
}));
const event: AutoBeRealizePlanEvent = {
type: "realizePlan",
id: v4(),
plans,
acquisition: preliminary.getAcquisition(),
metric: result.metric,
tokenUsage: result.tokenUsage,
completed: props.counter.get(),
total: props.progress.total,
step: ctx.state().analyze?.step ?? 0,
created_at: new Date().toISOString(),
};
return out(result)(event);
},
);
ctx.dispatch(event);
return event.plans as AutoBeRealizeTransformerPlan[];
}
function createController(props: {
prismaSchemaNames: Set<string>;
dtoTypeName: string;
build: (next: IAutoBeRealizeTransformerPlanApplication.IWrite) => void;
preliminary: AutoBePreliminaryController<
"analysisSections" | "databaseSchemas" | "interfaceSchemas" | "complete"
>;
}): ILlmController {
const validate: Validator = (input) => {
const result: IValidation<IAutoBeRealizeTransformerPlanApplication.IProps> =
typia.validate<IAutoBeRealizeTransformerPlanApplication.IProps>(input);
if (result.success === false) return result;
else if (result.data.request.type !== "write")
return props.preliminary.validate({
thinking: result.data.thinking,
request: result.data.request,
});
const errors: IValidation.IError[] = [];
result.data.request.plans.map((plan, i) => {
if (plan.dtoTypeName !== props.dtoTypeName)
errors.push({
path: `$input.request.plans[${i}].dtoTypeName`,
value: plan.dtoTypeName,
expected: JSON.stringify(props.dtoTypeName),
description: StringUtil.trim`
The DTO type name must be ${JSON.stringify(props.dtoTypeName)}.
If you have planned other DTO type's transformer,
please entirely remake the plan with ONLY the DTO type
${JSON.stringify(props.dtoTypeName)}.
`,
});
if (
plan.databaseSchemaName !== null &&
props.prismaSchemaNames.has(plan.databaseSchemaName) === false
)
errors.push({
path: `$input.request.plans[${i}].databaseSchemaName`,
value: plan.databaseSchemaName,
expected: Array.from(props.prismaSchemaNames)
.map((s) => JSON.stringify(s))
.join(" | "),
description: StringUtil.trim`
The database schema name must be one of the available database schemas.
${Array.from(props.prismaSchemaNames)
.map((s) => `- ${s}`)
.join("\n")}
`,
});
});
return errors.length
? {
success: false,
errors,
data: result.data,
}
: result;
};
const application: ILlmApplication = props.preliminary.fixApplication(
typia.llm.application<IAutoBeRealizeTransformerPlanApplication>({
validate: {
process: validate,
},
}),
);
return {
protocol: "class",
name: SOURCE,
application,
execute: {
process: (next) => {
if (next.request.type === "write") props.build(next.request);
},
} satisfies IAutoBeRealizeTransformerPlanApplication,
};
}
type Validator = (
input: unknown,
) => IValidation<IAutoBeRealizeTransformerPlanApplication.IProps>;
const SOURCE = "realizePlan" satisfies AutoBeEventSource;