@autobe/agent 0.30.3 → 0.30.4-dev.20260324

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,237 +1,237 @@
1
- import {
2
- AgenticaSystemPrompt,
3
- IAgenticaController,
4
- MicroAgentica,
5
- } from "@agentica/core";
6
- import {
7
- AutoBeTest,
8
- AutoBeTestValidateEvent,
9
- IAutoBeTypeScriptCompileResult,
10
- } from "@autobe/interface";
11
- import {
12
- ILlmApplication,
13
- ILlmSchema,
14
- OpenApi,
15
- OpenApiTypeChecker,
16
- } from "@samchon/openapi";
17
- import { IPointer } from "tstl";
18
- import typia, { IJsonSchemaUnit } from "typia";
19
-
20
- import { AutoBeSystemPromptConstant } from "../../constants/AutoBeSystemPromptConstant";
21
- import { AutoBeContext } from "../../context/AutoBeContext";
22
- import { assertSchemaModel } from "../../context/assertSchemaModel";
23
- import { enforceToolCall } from "../../utils/enforceToolCall";
24
- import { IAutoBeTestWriteResult } from "./structures/IAutoBeTestWriteResult";
25
- import { transformTestCorrectHistories } from "./transformTestCorrectHistories";
26
-
27
- export function orchestrateTestCorrect<Model extends ILlmSchema.Model>(
28
- ctx: AutoBeContext<Model>,
29
- results: IAutoBeTestWriteResult[],
30
- life: number = 4,
31
- ): Promise<AutoBeTestValidateEvent[]> {
32
- return Promise.all(
33
- results.map(async (written) => {
34
- const event: AutoBeTestValidateEvent = await compile(ctx, written);
35
- return predicate(ctx, written, event, life);
36
- }),
37
- );
38
- }
39
-
40
- async function predicate<Model extends ILlmSchema.Model>(
41
- ctx: AutoBeContext<Model>,
42
- written: IAutoBeTestWriteResult,
43
- event: AutoBeTestValidateEvent,
44
- life: number,
45
- ): Promise<AutoBeTestValidateEvent> {
46
- ctx.dispatch(event);
47
- return event.result.type === "failure"
48
- ? correct(ctx, written, event, life - 1)
49
- : event;
50
- }
51
-
52
- async function correct<Model extends ILlmSchema.Model>(
53
- ctx: AutoBeContext<Model>,
54
- written: IAutoBeTestWriteResult,
55
- event: AutoBeTestValidateEvent,
56
- life: number,
57
- ): Promise<AutoBeTestValidateEvent> {
58
- if (event.result.type !== "failure") return event;
59
- else if (--life <= 0) return event;
60
-
61
- const pointer: IPointer<ICorrectTestFunctionProps | null> = {
62
- value: null,
63
- };
64
- const agentica = new MicroAgentica({
65
- model: ctx.model,
66
- vendor: { ...ctx.vendor },
67
- config: {
68
- ...(ctx.config ?? {}),
69
- executor: {
70
- describe: null,
71
- },
72
- systemPrompt: {
73
- validate: (events) =>
74
- [
75
- AgenticaSystemPrompt.VALIDATE_REPEATED.replace(
76
- "${{HISTORICAL_ERRORS}}",
77
- JSON.stringify(events.map((e) => e.result.errors)),
78
- ),
79
- AutoBeSystemPromptConstant.TEST_VALIDATE.replace(
80
- "${{AutoBeTest.IStatement}}",
81
- getUnionTypeName(typia.json.schema<AutoBeTest.IStatement>()),
82
- ).replace(
83
- "${{AutoBeTest.IExpression}}",
84
- getUnionTypeName(typia.json.schema<AutoBeTest.IExpression>()),
85
- ),
86
- ].join("\n\n"),
87
- },
88
- retry: 4,
89
- throw: true,
90
- },
91
- histories: transformTestCorrectHistories(written, event.result),
92
- controllers: [
93
- createController({
94
- model: ctx.model,
95
- build: (next) => {
96
- pointer.value = next;
97
- },
98
- }),
99
- ],
100
- });
101
- enforceToolCall(agentica);
102
-
103
- await agentica
104
- .conversate(
105
- "Fix the `AutoBeTest.IFunction` data to resolve the compilation error.",
106
- )
107
- .finally(() => {
108
- const tokenUsage = agentica.getTokenUsage();
109
- ctx.usage().record(tokenUsage, ["test"]);
110
- });
111
- if (pointer.value === null) throw new Error("Failed to modify test code.");
112
- event = await compile(ctx, {
113
- ...written,
114
- file: {
115
- ...written.file,
116
- function: pointer.value.function,
117
- },
118
- });
119
- return predicate(ctx, written, event, life);
120
- }
121
-
122
- async function compile<Model extends ILlmSchema.Model>(
123
- ctx: AutoBeContext<Model>,
124
- written: IAutoBeTestWriteResult,
125
- ): Promise<AutoBeTestValidateEvent> {
126
- const compiled: IAutoBeTypeScriptCompileResult =
127
- await ctx.compiler.test.compile({
128
- files: {
129
- ...written.artifacts.dto,
130
- ...written.artifacts.sdk,
131
- [written.file.location]: written.file.content,
132
- },
133
- });
134
- return {
135
- type: "testValidate",
136
- file: written.file,
137
- result: compiled,
138
- created_at: new Date().toISOString(),
139
- step: ctx.state().analyze?.step ?? 0,
140
- };
141
- }
142
-
143
- function getUnionTypeName(unit: IJsonSchemaUnit): string {
144
- if (OpenApiTypeChecker.isReference(unit.schema) === false) return "unknown";
145
-
146
- const typeName: string = unit.schema.$ref.split("/").pop() ?? "";
147
- const schema: OpenApi.IJsonSchema | undefined =
148
- unit.components.schemas?.[typeName];
149
- if (schema === undefined || OpenApiTypeChecker.isOneOf(schema) === false)
150
- return "unknown";
151
- return schema.oneOf
152
- .filter(OpenApiTypeChecker.isReference)
153
- .map((r) => r.$ref.split("/").pop() ?? "")
154
- .join(" | ");
155
- }
156
-
157
- function createController<Model extends ILlmSchema.Model>(props: {
158
- model: Model;
159
- build: (next: ICorrectTestFunctionProps) => void;
160
- }): IAgenticaController.IClass<Model> {
161
- assertSchemaModel(props.model);
162
-
163
- const application: ILlmApplication<Model> = collection[
164
- props.model
165
- ] satisfies ILlmApplication<any> as unknown as ILlmApplication<Model>;
166
- return {
167
- protocol: "class",
168
- name: "Modify Test Code",
169
- application,
170
- execute: {
171
- correctTestCode: (next) => {
172
- props.build(next);
173
- },
174
- } satisfies IApplication,
175
- };
176
- }
177
-
178
- const claude = typia.llm.application<
179
- IApplication,
180
- "claude"
181
- >();
182
- const collection = {
183
- chatgpt: typia.llm.application<
184
- IApplication,
185
- "chatgpt"
186
- >(),
187
- claude,
188
- llama: claude,
189
- deepseek: claude,
190
- "3.1": claude,
191
- };
192
-
193
- interface IApplication {
194
- correctTestCode(props: ICorrectTestFunctionProps): void;
195
- }
196
-
197
- interface ICorrectTestFunctionProps {
198
- /**
199
- * Step 1: Initial self-reflection on the source code without compiler error
200
- * context.
201
- *
202
- * The AI agent analyzes the previously generated test code to identify
203
- * potential issues, relying solely on its understanding of TypeScript syntax,
204
- * testing patterns, and best practices.
205
- *
206
- * This encourages the agent to develop independent debugging skills before
207
- * being influenced by external error messages.
208
- */
209
- think_without_compile_error: string;
210
-
211
- /**
212
- * Step 2: Re-evaluation of the code with compiler error messages as
213
- * additional context.
214
- *
215
- * After the initial analysis, the AI agent reviews the same code again, this
216
- * time incorporating the specific TypeScript compiler error messages.
217
- *
218
- * This allows the agent to correlate its initial observations with concrete
219
- * compilation failures and refine its understanding of what went wrong.
220
- */
221
- think_again_with_compile_error: string;
222
-
223
- /**
224
- * Step 3: Concrete action plan for fixing the identified issues.
225
- *
226
- * Based on the analysis from steps 1 and 2, the AI agent formulates a
227
- * specific, step-by-step solution strategy.
228
- *
229
- * This should include what changes need to be made, why those changes are
230
- * necessary, and how they will resolve the compilation errors while
231
- * maintaining the test's intended functionality.
232
- */
233
- solution: string;
234
-
235
- /** Re-written AST data to fix the compilation error. */
236
- function: AutoBeTest.IFunction;
237
- }
1
+ import {
2
+ AgenticaSystemPrompt,
3
+ IAgenticaController,
4
+ MicroAgentica,
5
+ } from "@agentica/core";
6
+ import {
7
+ AutoBeTest,
8
+ AutoBeTestValidateEvent,
9
+ IAutoBeTypeScriptCompileResult,
10
+ } from "@autobe/interface";
11
+ import {
12
+ ILlmApplication,
13
+ ILlmSchema,
14
+ OpenApi,
15
+ OpenApiTypeChecker,
16
+ } from "@samchon/openapi";
17
+ import { IPointer } from "tstl";
18
+ import typia, { IJsonSchemaUnit } from "typia";
19
+
20
+ import { AutoBeSystemPromptConstant } from "../../constants/AutoBeSystemPromptConstant";
21
+ import { AutoBeContext } from "../../context/AutoBeContext";
22
+ import { assertSchemaModel } from "../../context/assertSchemaModel";
23
+ import { enforceToolCall } from "../../utils/enforceToolCall";
24
+ import { IAutoBeTestWriteResult } from "./structures/IAutoBeTestWriteResult";
25
+ import { transformTestCorrectHistories } from "./transformTestCorrectHistories";
26
+
27
+ export function orchestrateTestCorrect<Model extends ILlmSchema.Model>(
28
+ ctx: AutoBeContext<Model>,
29
+ results: IAutoBeTestWriteResult[],
30
+ life: number = 4,
31
+ ): Promise<AutoBeTestValidateEvent[]> {
32
+ return Promise.all(
33
+ results.map(async (written) => {
34
+ const event: AutoBeTestValidateEvent = await compile(ctx, written);
35
+ return predicate(ctx, written, event, life);
36
+ }),
37
+ );
38
+ }
39
+
40
+ async function predicate<Model extends ILlmSchema.Model>(
41
+ ctx: AutoBeContext<Model>,
42
+ written: IAutoBeTestWriteResult,
43
+ event: AutoBeTestValidateEvent,
44
+ life: number,
45
+ ): Promise<AutoBeTestValidateEvent> {
46
+ ctx.dispatch(event);
47
+ return event.result.type === "failure"
48
+ ? correct(ctx, written, event, life - 1)
49
+ : event;
50
+ }
51
+
52
+ async function correct<Model extends ILlmSchema.Model>(
53
+ ctx: AutoBeContext<Model>,
54
+ written: IAutoBeTestWriteResult,
55
+ event: AutoBeTestValidateEvent,
56
+ life: number,
57
+ ): Promise<AutoBeTestValidateEvent> {
58
+ if (event.result.type !== "failure") return event;
59
+ else if (--life <= 0) return event;
60
+
61
+ const pointer: IPointer<ICorrectTestFunctionProps | null> = {
62
+ value: null,
63
+ };
64
+ const agentica = new MicroAgentica({
65
+ model: ctx.model,
66
+ vendor: { ...ctx.vendor },
67
+ config: {
68
+ ...(ctx.config ?? {}),
69
+ executor: {
70
+ describe: null,
71
+ },
72
+ systemPrompt: {
73
+ validate: (events) =>
74
+ [
75
+ AgenticaSystemPrompt.VALIDATE_REPEATED.replace(
76
+ "${{HISTORICAL_ERRORS}}",
77
+ JSON.stringify(events.map((e) => e.result.errors)),
78
+ ),
79
+ AutoBeSystemPromptConstant.TEST_VALIDATE.replace(
80
+ "${{AutoBeTest.IStatement}}",
81
+ getUnionTypeName(typia.json.schema<AutoBeTest.IStatement>()),
82
+ ).replace(
83
+ "${{AutoBeTest.IExpression}}",
84
+ getUnionTypeName(typia.json.schema<AutoBeTest.IExpression>()),
85
+ ),
86
+ ].join("\n\n"),
87
+ },
88
+ retry: 4,
89
+ throw: true,
90
+ },
91
+ histories: transformTestCorrectHistories(written, event.result),
92
+ controllers: [
93
+ createController({
94
+ model: ctx.model,
95
+ build: (next) => {
96
+ pointer.value = next;
97
+ },
98
+ }),
99
+ ],
100
+ });
101
+ enforceToolCall(agentica);
102
+
103
+ await agentica
104
+ .conversate(
105
+ "Fix the `AutoBeTest.IFunction` data to resolve the compilation error.",
106
+ )
107
+ .finally(() => {
108
+ const tokenUsage = agentica.getTokenUsage();
109
+ ctx.usage().record(tokenUsage, ["test"]);
110
+ });
111
+ if (pointer.value === null) throw new Error("Failed to modify test code.");
112
+ event = await compile(ctx, {
113
+ ...written,
114
+ file: {
115
+ ...written.file,
116
+ function: pointer.value.function,
117
+ },
118
+ });
119
+ return predicate(ctx, written, event, life);
120
+ }
121
+
122
+ async function compile<Model extends ILlmSchema.Model>(
123
+ ctx: AutoBeContext<Model>,
124
+ written: IAutoBeTestWriteResult,
125
+ ): Promise<AutoBeTestValidateEvent> {
126
+ const compiled: IAutoBeTypeScriptCompileResult =
127
+ await ctx.compiler.test.compile({
128
+ files: {
129
+ ...written.artifacts.dto,
130
+ ...written.artifacts.sdk,
131
+ [written.file.location]: written.file.content,
132
+ },
133
+ });
134
+ return {
135
+ type: "testValidate",
136
+ file: written.file,
137
+ result: compiled,
138
+ created_at: new Date().toISOString(),
139
+ step: ctx.state().analyze?.step ?? 0,
140
+ };
141
+ }
142
+
143
+ function getUnionTypeName(unit: IJsonSchemaUnit): string {
144
+ if (OpenApiTypeChecker.isReference(unit.schema) === false) return "unknown";
145
+
146
+ const typeName: string = unit.schema.$ref.split("/").pop() ?? "";
147
+ const schema: OpenApi.IJsonSchema | undefined =
148
+ unit.components.schemas?.[typeName];
149
+ if (schema === undefined || OpenApiTypeChecker.isOneOf(schema) === false)
150
+ return "unknown";
151
+ return schema.oneOf
152
+ .filter(OpenApiTypeChecker.isReference)
153
+ .map((r) => r.$ref.split("/").pop() ?? "")
154
+ .join(" | ");
155
+ }
156
+
157
+ function createController<Model extends ILlmSchema.Model>(props: {
158
+ model: Model;
159
+ build: (next: ICorrectTestFunctionProps) => void;
160
+ }): IAgenticaController.IClass<Model> {
161
+ assertSchemaModel(props.model);
162
+
163
+ const application: ILlmApplication<Model> = collection[
164
+ props.model
165
+ ] satisfies ILlmApplication<any> as unknown as ILlmApplication<Model>;
166
+ return {
167
+ protocol: "class",
168
+ name: "Modify Test Code",
169
+ application,
170
+ execute: {
171
+ correctTestCode: (next) => {
172
+ props.build(next);
173
+ },
174
+ } satisfies IApplication,
175
+ };
176
+ }
177
+
178
+ const claude = typia.llm.application<
179
+ IApplication,
180
+ "claude"
181
+ >();
182
+ const collection = {
183
+ chatgpt: typia.llm.application<
184
+ IApplication,
185
+ "chatgpt"
186
+ >(),
187
+ claude,
188
+ llama: claude,
189
+ deepseek: claude,
190
+ "3.1": claude,
191
+ };
192
+
193
+ interface IApplication {
194
+ correctTestCode(props: ICorrectTestFunctionProps): void;
195
+ }
196
+
197
+ interface ICorrectTestFunctionProps {
198
+ /**
199
+ * Step 1: Initial self-reflection on the source code without compiler error
200
+ * context.
201
+ *
202
+ * The AI agent analyzes the previously generated test code to identify
203
+ * potential issues, relying solely on its understanding of TypeScript syntax,
204
+ * testing patterns, and best practices.
205
+ *
206
+ * This encourages the agent to develop independent debugging skills before
207
+ * being influenced by external error messages.
208
+ */
209
+ think_without_compile_error: string;
210
+
211
+ /**
212
+ * Step 2: Re-evaluation of the code with compiler error messages as
213
+ * additional context.
214
+ *
215
+ * After the initial analysis, the AI agent reviews the same code again, this
216
+ * time incorporating the specific TypeScript compiler error messages.
217
+ *
218
+ * This allows the agent to correlate its initial observations with concrete
219
+ * compilation failures and refine its understanding of what went wrong.
220
+ */
221
+ think_again_with_compile_error: string;
222
+
223
+ /**
224
+ * Step 3: Concrete action plan for fixing the identified issues.
225
+ *
226
+ * Based on the analysis from steps 1 and 2, the AI agent formulates a
227
+ * specific, step-by-step solution strategy.
228
+ *
229
+ * This should include what changes need to be made, why those changes are
230
+ * necessary, and how they will resolve the compilation errors while
231
+ * maintaining the test's intended functionality.
232
+ */
233
+ solution: string;
234
+
235
+ /** Re-written AST data to fix the compilation error. */
236
+ function: AutoBeTest.IFunction;
237
+ }