@autobe/agent
Version:
AI backend server code generator
94 lines (89 loc) • 2.98 kB
text/typescript
import {
AgenticaAssistantMessageHistory,
IAgenticaController,
} from "@agentica/core";
import {
AutoBeAnalyzeScenarioEvent,
AutoBeAssistantMessageHistory,
} from "@autobe/interface";
import { ILlmApplication, ILlmSchema } from "@samchon/openapi";
import { IPointer } from "tstl";
import typia from "typia";
import { v7 } from "uuid";
import { AutoBeContext } from "../../context/AutoBeContext";
import { assertSchemaModel } from "../../context/assertSchemaModel";
import { transformAnalyzeSceHistories } from "./histories/transformAnalyzeScenarioHistories";
import { IAutoBeAnalyzeScenarioApplication } from "./structures/IAutoBeAnalyzeScenarioApplication";
export const orchestrateAnalyzeScenario = async <
Model extends ILlmSchema.Model,
>(
ctx: AutoBeContext<Model>,
): Promise<AutoBeAnalyzeScenarioEvent | AutoBeAssistantMessageHistory> => {
const start: Date = new Date();
const pointer: IPointer<IAutoBeAnalyzeScenarioApplication.IProps | null> = {
value: null,
};
const { histories, tokenUsage, metric } = await ctx.conversate({
source: "analyzeScenario",
controller: createController<Model>({
model: ctx.model,
build: (value) => (pointer.value = value),
}),
enforceFunctionCall: false,
...transformAnalyzeSceHistories(ctx),
});
if (histories.at(-1)?.type === "assistantMessage")
return {
...(histories.at(-1)! as AgenticaAssistantMessageHistory),
created_at: start.toISOString(),
completed_at: new Date().toISOString(),
id: v7(),
} satisfies AutoBeAssistantMessageHistory;
else if (pointer.value === null) {
// unreachable
throw new Error("Failed to extract files and tables.");
}
return {
type: "analyzeScenario",
id: v7(),
prefix: pointer.value.prefix,
language: pointer.value.language,
actors: pointer.value.actors,
files: pointer.value.files,
metric,
tokenUsage,
step: (ctx.state().analyze?.step ?? -1) + 1,
created_at: start.toISOString(),
};
};
function createController<Model extends ILlmSchema.Model>(props: {
model: Model;
build: (value: IAutoBeAnalyzeScenarioApplication.IProps) => void;
}): IAgenticaController.IClass<Model> {
assertSchemaModel(props.model);
const application: ILlmApplication<Model> = collection[
props.model === "chatgpt"
? "chatgpt"
: props.model === "gemini"
? "gemini"
: "claude"
] satisfies ILlmApplication<any> as unknown as ILlmApplication<Model>;
return {
protocol: "class",
name: "Compose",
application,
execute: {
compose: (input) => {
props.build(input);
},
} satisfies IAutoBeAnalyzeScenarioApplication,
};
}
const collection = {
chatgpt: typia.llm.application<
IAutoBeAnalyzeScenarioApplication,
"chatgpt"
>(),
claude: typia.llm.application<IAutoBeAnalyzeScenarioApplication, "claude">(),
gemini: typia.llm.application<IAutoBeAnalyzeScenarioApplication, "gemini">(),
};