@autobe/agent 0.30.2 → 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.
Files changed (89) hide show
  1. package/LICENSE +661 -661
  2. package/README.md +261 -0
  3. package/lib/AutoBeMockAgent.d.ts +2 -1
  4. package/lib/AutoBeMockAgent.js +37 -18
  5. package/lib/AutoBeMockAgent.js.map +1 -1
  6. package/lib/constants/AutoBeConfigConstant.d.ts +1 -1
  7. package/lib/constants/AutoBeSystemPromptConstant.d.ts +12 -11
  8. package/lib/constants/AutoBeSystemPromptConstant.js.map +1 -1
  9. package/lib/index.mjs +497 -71
  10. package/lib/index.mjs.map +1 -1
  11. package/lib/orchestrate/analyze/histories/transformAnalyzeExtractDecisionsHistory.d.ts +18 -0
  12. package/lib/orchestrate/analyze/histories/transformAnalyzeExtractDecisionsHistory.js +51 -0
  13. package/lib/orchestrate/analyze/histories/transformAnalyzeExtractDecisionsHistory.js.map +1 -0
  14. package/lib/orchestrate/analyze/histories/transformAnalyzeScenarioHistory.js +1 -1
  15. package/lib/orchestrate/analyze/histories/transformAnalyzeScenarioHistory.js.map +1 -1
  16. package/lib/orchestrate/analyze/histories/transformAnalyzeScenarioReviewHistory.js +1 -1
  17. package/lib/orchestrate/analyze/histories/transformAnalyzeScenarioReviewHistory.js.map +1 -1
  18. package/lib/orchestrate/analyze/histories/transformAnalyzeSectionCrossFileReviewHistory.d.ts +1 -0
  19. package/lib/orchestrate/analyze/histories/transformAnalyzeSectionCrossFileReviewHistory.js +15 -1
  20. package/lib/orchestrate/analyze/histories/transformAnalyzeSectionCrossFileReviewHistory.js.map +1 -1
  21. package/lib/orchestrate/analyze/histories/transformAnalyzeSectionReviewHistory.js +1 -1
  22. package/lib/orchestrate/analyze/histories/transformAnalyzeSectionReviewHistory.js.map +1 -1
  23. package/lib/orchestrate/analyze/orchestrateAnalyze.js +48 -13
  24. package/lib/orchestrate/analyze/orchestrateAnalyze.js.map +1 -1
  25. package/lib/orchestrate/analyze/orchestrateAnalyzeExtractDecisions.d.ts +17 -0
  26. package/lib/orchestrate/analyze/orchestrateAnalyzeExtractDecisions.js +345 -0
  27. package/lib/orchestrate/analyze/orchestrateAnalyzeExtractDecisions.js.map +1 -0
  28. package/lib/orchestrate/analyze/orchestrateAnalyzeScenario.js +2 -2
  29. package/lib/orchestrate/analyze/orchestrateAnalyzeSectionCrossFileReview.d.ts +1 -0
  30. package/lib/orchestrate/analyze/orchestrateAnalyzeSectionCrossFileReview.js +1 -0
  31. package/lib/orchestrate/analyze/orchestrateAnalyzeSectionCrossFileReview.js.map +1 -1
  32. package/lib/orchestrate/analyze/structures/IAutoBeAnalyzeExtractDecisionsApplication.d.ts +91 -0
  33. package/lib/orchestrate/analyze/structures/IAutoBeAnalyzeExtractDecisionsApplication.js +3 -0
  34. package/lib/orchestrate/analyze/structures/IAutoBeAnalyzeExtractDecisionsApplication.js.map +1 -0
  35. package/lib/orchestrate/analyze/structures/IAutoBeAnalyzeScenarioApplication.d.ts +15 -5
  36. package/lib/orchestrate/analyze/utils/buildErrorCodeRegistry.d.ts +0 -9
  37. package/lib/orchestrate/analyze/utils/buildErrorCodeRegistry.js +1 -13
  38. package/lib/orchestrate/analyze/utils/buildErrorCodeRegistry.js.map +1 -1
  39. package/lib/orchestrate/analyze/utils/detectDecisionConflicts.d.ts +63 -0
  40. package/lib/orchestrate/analyze/utils/detectDecisionConflicts.js +105 -0
  41. package/lib/orchestrate/analyze/utils/detectDecisionConflicts.js.map +1 -0
  42. package/lib/orchestrate/common/histories/transformPreliminaryHistory.js +1 -1
  43. package/lib/orchestrate/common/histories/transformPreliminaryHistory.js.map +1 -1
  44. package/lib/orchestrate/interface/histories/transformInterfaceActionEndpointReviewHistory.js +1 -1
  45. package/lib/orchestrate/interface/histories/transformInterfaceActionEndpointReviewHistory.js.map +1 -1
  46. package/lib/orchestrate/interface/histories/transformInterfaceBaseEndpointReviewHistory.js +1 -1
  47. package/lib/orchestrate/interface/histories/transformInterfaceBaseEndpointReviewHistory.js.map +1 -1
  48. package/lib/orchestrate/interface/histories/transformInterfaceSchemaRefineHistory.js +1 -1
  49. package/lib/orchestrate/interface/histories/transformInterfaceSchemaRefineHistory.js.map +1 -1
  50. package/lib/orchestrate/interface/orchestrateInterfaceSchemaRefine.js +1 -2
  51. package/lib/orchestrate/interface/orchestrateInterfaceSchemaRefine.js.map +1 -1
  52. package/lib/orchestrate/interface/orchestrateInterfaceSchemaReview.js +1 -2
  53. package/lib/orchestrate/interface/orchestrateInterfaceSchemaReview.js.map +1 -1
  54. package/lib/orchestrate/interface/utils/AutoBeJsonSchemaValidator.js +316 -283
  55. package/lib/orchestrate/interface/utils/AutoBeJsonSchemaValidator.js.map +1 -1
  56. package/lib/orchestrate/prisma/histories/transformPrismaComponentReviewHistory.js +2 -2
  57. package/lib/orchestrate/prisma/histories/transformPrismaComponentReviewHistory.js.map +1 -1
  58. package/lib/orchestrate/prisma/histories/transformPrismaComponentsHistory.js +1 -1
  59. package/lib/orchestrate/prisma/histories/transformPrismaComponentsHistory.js.map +1 -1
  60. package/lib/orchestrate/prisma/histories/transformPrismaGroupHistory.js +1 -1
  61. package/lib/orchestrate/prisma/histories/transformPrismaGroupHistory.js.map +1 -1
  62. package/lib/orchestrate/prisma/histories/transformPrismaGroupReviewHistory.js +1 -1
  63. package/lib/orchestrate/prisma/histories/transformPrismaGroupReviewHistory.js.map +1 -1
  64. package/lib/orchestrate/prisma/histories/transformPrismaSchemaHistory.js +1 -1
  65. package/lib/orchestrate/prisma/histories/transformPrismaSchemaHistory.js.map +1 -1
  66. package/lib/orchestrate/prisma/histories/transformPrismaSchemaReviewHistory.js +1 -1
  67. package/lib/orchestrate/prisma/histories/transformPrismaSchemaReviewHistory.js.map +1 -1
  68. package/lib/orchestrate/prisma/orchestratePrismaCorrect.js +1 -1
  69. package/lib/structures/IAutoBeVendor.d.ts +13 -0
  70. package/package.json +5 -5
  71. package/src/AutoBeMockAgent.ts +283 -254
  72. package/src/constants/AutoBeConfigConstant.ts +1 -1
  73. package/src/constants/AutoBeSystemPromptConstant.ts +12 -11
  74. package/src/orchestrate/analyze/histories/transformAnalyzeExtractDecisionsHistory.ts +69 -0
  75. package/src/orchestrate/analyze/histories/transformAnalyzeSectionCrossFileReviewHistory.ts +20 -0
  76. package/src/orchestrate/analyze/orchestrateAnalyze.ts +58 -1
  77. package/src/orchestrate/analyze/orchestrateAnalyzeExtractDecisions.ts +97 -0
  78. package/src/orchestrate/analyze/orchestrateAnalyzeSectionCrossFileReview.ts +2 -0
  79. package/src/orchestrate/analyze/structures/IAutoBeAnalyzeExtractDecisionsApplication.ts +99 -0
  80. package/src/orchestrate/analyze/structures/IAutoBeAnalyzeScenarioApplication.ts +15 -5
  81. package/src/orchestrate/analyze/utils/buildErrorCodeRegistry.ts +0 -20
  82. package/src/orchestrate/analyze/utils/detectDecisionConflicts.ts +172 -0
  83. package/src/orchestrate/interface/orchestrateInterfaceSchemaRefine.ts +291 -292
  84. package/src/orchestrate/interface/orchestrateInterfaceSchemaReview.ts +309 -310
  85. package/src/orchestrate/interface/utils/AutoBeJsonSchemaValidator.ts +763 -725
  86. package/src/orchestrate/test/experimental/orchestrateTestCorrect.ast +237 -237
  87. package/src/orchestrate/test/experimental/orchestrateTestWrite.ast +322 -322
  88. package/src/orchestrate/test/experimental/transformTestCorrectHistories.ast +52 -52
  89. package/src/structures/IAutoBeVendor.ts +127 -113
@@ -1,310 +1,309 @@
1
- import { IAgenticaController } from "@agentica/core";
2
- import {
3
- AutoBeDatabase,
4
- AutoBeEventSource,
5
- AutoBeInterfaceSchemaReviewEvent,
6
- // AutoBeInterfaceSchemaReviewEvent,
7
- AutoBeOpenApi,
8
- AutoBeProgressEventBase,
9
- } from "@autobe/interface";
10
- import { AutoBeOpenApiTypeChecker } from "@autobe/utils";
11
- import { IPointer } from "tstl";
12
- import typia, { ILlmApplication, IValidation } from "typia";
13
- import { v7 } from "uuid";
14
-
15
- import { AutoBeContext } from "../../context/AutoBeContext";
16
- import { buildAnalysisContextSections } from "../../utils/RAGRetrieval";
17
- import { executeCachedBatch } from "../../utils/executeCachedBatch";
18
- import { getEmbedder } from "../../utils/getEmbedder";
19
- import { AutoBePreliminaryController } from "../common/AutoBePreliminaryController";
20
- import { convertToSectionEntries } from "../common/internal/convertToSectionEntries";
21
- import { IAnalysisSectionEntry } from "../common/structures/IAnalysisSectionEntry";
22
- import { AutoBeDatabaseModelProgrammer } from "../prisma/programmers/AutoBeDatabaseModelProgrammer";
23
- import { transformInterfaceSchemaReviewHistory } from "./histories/transformInterfaceSchemaReviewHistory";
24
- import { AutoBeInterfaceSchemaProgrammer } from "./programmers/AutoBeInterfaceSchemaProgrammer";
25
- import { AutoBeInterfaceSchemaReviewProgrammer } from "./programmers/AutoBeInterfaceSchemaReviewProgrammer";
26
- import { IAutoBeInterfaceSchemaReviewApplication } from "./structures/IAutoBeInterfaceSchemaReviewApplication";
27
- import { AutoBeJsonSchemaFactory } from "./utils/AutoBeJsonSchemaFactory";
28
- import { AutoBeJsonSchemaValidator } from "./utils/AutoBeJsonSchemaValidator";
29
- import { fulfillJsonSchemaErrorMessages } from "./utils/fulfillJsonSchemaErrorMessages";
30
-
31
- export async function orchestrateInterfaceSchemaReview(
32
- ctx: AutoBeContext,
33
- props: {
34
- document: AutoBeOpenApi.IDocument;
35
- schemas: Record<string, AutoBeOpenApi.IJsonSchemaDescriptive>;
36
- instruction: string;
37
- progress: AutoBeProgressEventBase;
38
- },
39
- ): Promise<Record<string, AutoBeOpenApi.IJsonSchemaDescriptive>> {
40
- // Filter to only process object-type schemas (non-preset and object type)
41
- const typeNames: string[] = Object.entries(props.schemas)
42
- .filter(
43
- ([k, v]) =>
44
- AutoBeJsonSchemaValidator.isPreset(k) === false &&
45
- AutoBeOpenApiTypeChecker.isObject(v) &&
46
- Object.keys(v.properties).length !== 0,
47
- )
48
- .map(([k]) => k);
49
- const x: Record<string, AutoBeOpenApi.IJsonSchemaDescriptive> = {};
50
- await executeCachedBatch(
51
- ctx,
52
- typeNames.map((it) => async (promptCacheKey) => {
53
- const predicate = (key: string) =>
54
- key === it ||
55
- (AutoBeJsonSchemaValidator.isPage(key) &&
56
- AutoBeJsonSchemaFactory.getPageName(key) === it);
57
- const reviewOperations: AutoBeOpenApi.IOperation[] =
58
- props.document.operations.filter(
59
- (op) =>
60
- (op.requestBody && predicate(op.requestBody.typeName)) ||
61
- (op.responseBody && predicate(op.responseBody.typeName)),
62
- );
63
- try {
64
- const value: AutoBeOpenApi.IJsonSchemaDescriptive = props.schemas[it];
65
- if (AutoBeOpenApiTypeChecker.isObject(value) === false) {
66
- ++props.progress.completed;
67
- return;
68
- }
69
- const reviewed: AutoBeOpenApi.IJsonSchemaDescriptive.IObject =
70
- await process(ctx, {
71
- instruction: props.instruction,
72
- document: props.document,
73
- typeName: it,
74
- reviewOperations,
75
- reviewSchema: value,
76
- progress: props.progress,
77
- promptCacheKey,
78
- });
79
- x[it] = reviewed;
80
- } catch (error) {
81
- console.log("interfaceSchemaReview failure", it, error);
82
- --props.progress.total;
83
- }
84
- }),
85
- );
86
- return x;
87
- }
88
-
89
- async function process(
90
- ctx: AutoBeContext,
91
- props: {
92
- instruction: string;
93
- document: AutoBeOpenApi.IDocument;
94
- typeName: string;
95
- reviewOperations: AutoBeOpenApi.IOperation[];
96
- reviewSchema: AutoBeOpenApi.IJsonSchemaDescriptive.IObject;
97
- progress: AutoBeProgressEventBase;
98
- promptCacheKey: string;
99
- },
100
- ): Promise<AutoBeOpenApi.IJsonSchemaDescriptive.IObject> {
101
- const allSections: IAnalysisSectionEntry[] = convertToSectionEntries(
102
- ctx.state().analyze?.files ?? [],
103
- );
104
-
105
- const schemaNames = [props.typeName];
106
- const opSummaries = props.reviewOperations
107
- .map((op) => `${op.method} ${op.path}: ${op.name}`)
108
- .join("\n");
109
- const queryText: string = `${schemaNames.join(", ")}\n${opSummaries}\n${props.instruction}`;
110
-
111
- const ragSections: IAnalysisSectionEntry[] =
112
- await buildAnalysisContextSections(
113
- getEmbedder(),
114
- allSections,
115
- queryText,
116
- "TOPK",
117
- { log: false, logPrefix: "interfaceSchemaReview" },
118
- );
119
-
120
- const preliminary: AutoBePreliminaryController<
121
- | "analysisSections"
122
- | "databaseSchemas"
123
- | "interfaceOperations"
124
- | "interfaceSchemas"
125
- | "previousAnalysisSections"
126
- | "previousDatabaseSchemas"
127
- | "previousInterfaceOperations"
128
- | "previousInterfaceSchemas"
129
- > = new AutoBePreliminaryController({
130
- application:
131
- typia.json.application<IAutoBeInterfaceSchemaReviewApplication>(),
132
- source: SOURCE,
133
- kinds: [
134
- "analysisSections",
135
- "previousAnalysisSections",
136
- "databaseSchemas",
137
- "previousDatabaseSchemas",
138
- "interfaceOperations",
139
- "previousInterfaceOperations",
140
- "interfaceSchemas",
141
- "previousInterfaceSchemas",
142
- ],
143
- config: {
144
- database: "text",
145
- databaseProperty: true,
146
- },
147
- state: ctx.state(),
148
- all: {
149
- interfaceOperations: props.document.operations,
150
- interfaceSchemas: props.document.components.schemas,
151
- },
152
- local: {
153
- analysisSections: ragSections,
154
- interfaceOperations: props.reviewOperations,
155
- interfaceSchemas: { [props.typeName]: props.reviewSchema },
156
- databaseSchemas: (() => {
157
- const expected: string =
158
- props.reviewSchema["x-autobe-database-schema"] ??
159
- AutoBeInterfaceSchemaProgrammer.getDatabaseSchemaName(props.typeName);
160
- const model: AutoBeDatabase.IModel | undefined = ctx
161
- .state()
162
- .database?.result.data.files.flatMap((f) => f.models)
163
- .find((m) => m.name === expected);
164
- if (model === undefined) return [];
165
- return AutoBeDatabaseModelProgrammer.getNeighbors({
166
- application: ctx.state().database!.result.data,
167
- model,
168
- });
169
- })(),
170
- },
171
- });
172
- return await preliminary.orchestrate(ctx, async (out) => {
173
- const pointer: IPointer<IAutoBeInterfaceSchemaReviewApplication.IComplete | null> =
174
- {
175
- value: null,
176
- };
177
- const result: AutoBeContext.IResult = await ctx.conversate({
178
- source: SOURCE,
179
- controller: createController(ctx, {
180
- typeName: props.typeName,
181
- operations: props.document.operations,
182
- schema: props.reviewSchema,
183
- preliminary,
184
- pointer,
185
- }),
186
- enforceFunctionCall: true,
187
- promptCacheKey: props.promptCacheKey,
188
- ...transformInterfaceSchemaReviewHistory({
189
- state: ctx.state(),
190
- instruction: props.instruction,
191
- typeName: props.typeName,
192
- reviewOperations: props.reviewOperations,
193
- reviewSchema: props.reviewSchema,
194
- preliminary,
195
- }),
196
- });
197
- if (pointer.value === null) return out(result)(null);
198
-
199
- // Apply revises to generate the modified schema content
200
- const content: AutoBeOpenApi.IJsonSchemaDescriptive.IObject =
201
- AutoBeInterfaceSchemaReviewProgrammer.execute({
202
- schema: props.reviewSchema,
203
- revises: pointer.value.revises,
204
- });
205
- ctx.dispatch({
206
- type: SOURCE,
207
- id: v7(),
208
- typeName: props.typeName,
209
- schema: props.reviewSchema,
210
- review: pointer.value.review,
211
- excludes: pointer.value.excludes,
212
- revises: pointer.value.revises,
213
- acquisition: preliminary.getAcquisition(),
214
- metric: result.metric,
215
- tokenUsage: result.tokenUsage,
216
- step: ctx.state().analyze?.step ?? 0,
217
- total: props.progress.total,
218
- completed: ++props.progress.completed,
219
- created_at: new Date().toISOString(),
220
- } satisfies AutoBeInterfaceSchemaReviewEvent);
221
- return out(result)(content);
222
- });
223
- }
224
-
225
- function createController(
226
- ctx: AutoBeContext,
227
- props: {
228
- typeName: string;
229
- schema: AutoBeOpenApi.IJsonSchemaDescriptive.IObject;
230
- operations: AutoBeOpenApi.IOperation[];
231
- pointer: IPointer<
232
- IAutoBeInterfaceSchemaReviewApplication.IComplete | null | false
233
- >;
234
- preliminary: AutoBePreliminaryController<
235
- | "analysisSections"
236
- | "databaseSchemas"
237
- | "interfaceOperations"
238
- | "interfaceSchemas"
239
- | "previousAnalysisSections"
240
- | "previousDatabaseSchemas"
241
- | "previousInterfaceOperations"
242
- | "previousInterfaceSchemas"
243
- >;
244
- },
245
- ): IAgenticaController.IClass {
246
- const validate: Validator = (next) => {
247
- const result: IValidation<IAutoBeInterfaceSchemaReviewApplication.IProps> =
248
- typia.validate<IAutoBeInterfaceSchemaReviewApplication.IProps>(next);
249
- if (result.success === false) {
250
- fulfillJsonSchemaErrorMessages(result.errors);
251
- return result;
252
- } else if (result.data.request.type !== "complete")
253
- return props.preliminary.validate({
254
- thinking: result.data.thinking,
255
- request: result.data.request,
256
- });
257
-
258
- const errors: IValidation.IError[] = [];
259
- AutoBeInterfaceSchemaReviewProgrammer.validate({
260
- typeName: props.typeName,
261
- schema: props.schema,
262
- excludes: result.data.request.excludes,
263
- revises: result.data.request.revises,
264
- errors,
265
- path: `$input.request`,
266
- everyModels:
267
- ctx.state().database?.result.data.files.flatMap((f) => f.models) ?? [],
268
- });
269
- return errors.length
270
- ? {
271
- success: false,
272
- errors,
273
- data: result.data,
274
- }
275
- : result;
276
- };
277
-
278
- const application: ILlmApplication = props.preliminary.fixApplication(
279
- typia.llm.application<IAutoBeInterfaceSchemaReviewApplication>({
280
- validate: {
281
- process: validate,
282
- },
283
- }),
284
- );
285
- AutoBeInterfaceSchemaReviewProgrammer.fixApplication({
286
- everyModels:
287
- ctx.state().database?.result.data.files.flatMap((f) => f.models) ?? [],
288
- application,
289
- typeName: props.typeName,
290
- schema: props.schema,
291
- });
292
-
293
- return {
294
- protocol: "class",
295
- name: SOURCE,
296
- application,
297
- execute: {
298
- process: (input) => {
299
- if (input.request.type === "complete")
300
- props.pointer.value = input.request;
301
- },
302
- } satisfies IAutoBeInterfaceSchemaReviewApplication,
303
- };
304
- }
305
-
306
- type Validator = (
307
- input: unknown,
308
- ) => IValidation<IAutoBeInterfaceSchemaReviewApplication.IProps>;
309
-
310
- const SOURCE = "interfaceSchemaReview" satisfies AutoBeEventSource;
1
+ import { IAgenticaController } from "@agentica/core";
2
+ import {
3
+ AutoBeDatabase,
4
+ AutoBeEventSource,
5
+ AutoBeInterfaceSchemaReviewEvent,
6
+ // AutoBeInterfaceSchemaReviewEvent,
7
+ AutoBeOpenApi,
8
+ AutoBeProgressEventBase,
9
+ } from "@autobe/interface";
10
+ import { AutoBeOpenApiTypeChecker } from "@autobe/utils";
11
+ import { IPointer } from "tstl";
12
+ import typia, { ILlmApplication, IValidation } from "typia";
13
+ import { v7 } from "uuid";
14
+
15
+ import { AutoBeContext } from "../../context/AutoBeContext";
16
+ import { buildAnalysisContextSections } from "../../utils/RAGRetrieval";
17
+ import { executeCachedBatch } from "../../utils/executeCachedBatch";
18
+ import { getEmbedder } from "../../utils/getEmbedder";
19
+ import { AutoBePreliminaryController } from "../common/AutoBePreliminaryController";
20
+ import { convertToSectionEntries } from "../common/internal/convertToSectionEntries";
21
+ import { IAnalysisSectionEntry } from "../common/structures/IAnalysisSectionEntry";
22
+ import { AutoBeDatabaseModelProgrammer } from "../prisma/programmers/AutoBeDatabaseModelProgrammer";
23
+ import { transformInterfaceSchemaReviewHistory } from "./histories/transformInterfaceSchemaReviewHistory";
24
+ import { AutoBeInterfaceSchemaProgrammer } from "./programmers/AutoBeInterfaceSchemaProgrammer";
25
+ import { AutoBeInterfaceSchemaReviewProgrammer } from "./programmers/AutoBeInterfaceSchemaReviewProgrammer";
26
+ import { IAutoBeInterfaceSchemaReviewApplication } from "./structures/IAutoBeInterfaceSchemaReviewApplication";
27
+ import { AutoBeJsonSchemaFactory } from "./utils/AutoBeJsonSchemaFactory";
28
+ import { AutoBeJsonSchemaValidator } from "./utils/AutoBeJsonSchemaValidator";
29
+ import { fulfillJsonSchemaErrorMessages } from "./utils/fulfillJsonSchemaErrorMessages";
30
+
31
+ export async function orchestrateInterfaceSchemaReview(
32
+ ctx: AutoBeContext,
33
+ props: {
34
+ document: AutoBeOpenApi.IDocument;
35
+ schemas: Record<string, AutoBeOpenApi.IJsonSchemaDescriptive>;
36
+ instruction: string;
37
+ progress: AutoBeProgressEventBase;
38
+ },
39
+ ): Promise<Record<string, AutoBeOpenApi.IJsonSchemaDescriptive>> {
40
+ // Filter to only process object-type schemas (non-preset and object type)
41
+ const typeNames: string[] = Object.entries(props.schemas)
42
+ .filter(
43
+ ([k, v]) =>
44
+ AutoBeJsonSchemaValidator.isPreset(k) === false &&
45
+ AutoBeOpenApiTypeChecker.isObject(v),
46
+ )
47
+ .map(([k]) => k);
48
+ const x: Record<string, AutoBeOpenApi.IJsonSchemaDescriptive> = {};
49
+ await executeCachedBatch(
50
+ ctx,
51
+ typeNames.map((it) => async (promptCacheKey) => {
52
+ const predicate = (key: string) =>
53
+ key === it ||
54
+ (AutoBeJsonSchemaValidator.isPage(key) &&
55
+ AutoBeJsonSchemaFactory.getPageName(key) === it);
56
+ const reviewOperations: AutoBeOpenApi.IOperation[] =
57
+ props.document.operations.filter(
58
+ (op) =>
59
+ (op.requestBody && predicate(op.requestBody.typeName)) ||
60
+ (op.responseBody && predicate(op.responseBody.typeName)),
61
+ );
62
+ try {
63
+ const value: AutoBeOpenApi.IJsonSchemaDescriptive = props.schemas[it];
64
+ if (AutoBeOpenApiTypeChecker.isObject(value) === false) {
65
+ ++props.progress.completed;
66
+ return;
67
+ }
68
+ const reviewed: AutoBeOpenApi.IJsonSchemaDescriptive.IObject =
69
+ await process(ctx, {
70
+ instruction: props.instruction,
71
+ document: props.document,
72
+ typeName: it,
73
+ reviewOperations,
74
+ reviewSchema: value,
75
+ progress: props.progress,
76
+ promptCacheKey,
77
+ });
78
+ x[it] = reviewed;
79
+ } catch (error) {
80
+ console.log("interfaceSchemaReview failure", it, error);
81
+ --props.progress.total;
82
+ }
83
+ }),
84
+ );
85
+ return x;
86
+ }
87
+
88
+ async function process(
89
+ ctx: AutoBeContext,
90
+ props: {
91
+ instruction: string;
92
+ document: AutoBeOpenApi.IDocument;
93
+ typeName: string;
94
+ reviewOperations: AutoBeOpenApi.IOperation[];
95
+ reviewSchema: AutoBeOpenApi.IJsonSchemaDescriptive.IObject;
96
+ progress: AutoBeProgressEventBase;
97
+ promptCacheKey: string;
98
+ },
99
+ ): Promise<AutoBeOpenApi.IJsonSchemaDescriptive.IObject> {
100
+ const allSections: IAnalysisSectionEntry[] = convertToSectionEntries(
101
+ ctx.state().analyze?.files ?? [],
102
+ );
103
+
104
+ const schemaNames = [props.typeName];
105
+ const opSummaries = props.reviewOperations
106
+ .map((op) => `${op.method} ${op.path}: ${op.name}`)
107
+ .join("\n");
108
+ const queryText: string = `${schemaNames.join(", ")}\n${opSummaries}\n${props.instruction}`;
109
+
110
+ const ragSections: IAnalysisSectionEntry[] =
111
+ await buildAnalysisContextSections(
112
+ getEmbedder(),
113
+ allSections,
114
+ queryText,
115
+ "TOPK",
116
+ { log: false, logPrefix: "interfaceSchemaReview" },
117
+ );
118
+
119
+ const preliminary: AutoBePreliminaryController<
120
+ | "analysisSections"
121
+ | "databaseSchemas"
122
+ | "interfaceOperations"
123
+ | "interfaceSchemas"
124
+ | "previousAnalysisSections"
125
+ | "previousDatabaseSchemas"
126
+ | "previousInterfaceOperations"
127
+ | "previousInterfaceSchemas"
128
+ > = new AutoBePreliminaryController({
129
+ application:
130
+ typia.json.application<IAutoBeInterfaceSchemaReviewApplication>(),
131
+ source: SOURCE,
132
+ kinds: [
133
+ "analysisSections",
134
+ "previousAnalysisSections",
135
+ "databaseSchemas",
136
+ "previousDatabaseSchemas",
137
+ "interfaceOperations",
138
+ "previousInterfaceOperations",
139
+ "interfaceSchemas",
140
+ "previousInterfaceSchemas",
141
+ ],
142
+ config: {
143
+ database: "text",
144
+ databaseProperty: true,
145
+ },
146
+ state: ctx.state(),
147
+ all: {
148
+ interfaceOperations: props.document.operations,
149
+ interfaceSchemas: props.document.components.schemas,
150
+ },
151
+ local: {
152
+ analysisSections: ragSections,
153
+ interfaceOperations: props.reviewOperations,
154
+ interfaceSchemas: { [props.typeName]: props.reviewSchema },
155
+ databaseSchemas: (() => {
156
+ const expected: string =
157
+ props.reviewSchema["x-autobe-database-schema"] ??
158
+ AutoBeInterfaceSchemaProgrammer.getDatabaseSchemaName(props.typeName);
159
+ const model: AutoBeDatabase.IModel | undefined = ctx
160
+ .state()
161
+ .database?.result.data.files.flatMap((f) => f.models)
162
+ .find((m) => m.name === expected);
163
+ if (model === undefined) return [];
164
+ return AutoBeDatabaseModelProgrammer.getNeighbors({
165
+ application: ctx.state().database!.result.data,
166
+ model,
167
+ });
168
+ })(),
169
+ },
170
+ });
171
+ return await preliminary.orchestrate(ctx, async (out) => {
172
+ const pointer: IPointer<IAutoBeInterfaceSchemaReviewApplication.IComplete | null> =
173
+ {
174
+ value: null,
175
+ };
176
+ const result: AutoBeContext.IResult = await ctx.conversate({
177
+ source: SOURCE,
178
+ controller: createController(ctx, {
179
+ typeName: props.typeName,
180
+ operations: props.document.operations,
181
+ schema: props.reviewSchema,
182
+ preliminary,
183
+ pointer,
184
+ }),
185
+ enforceFunctionCall: true,
186
+ promptCacheKey: props.promptCacheKey,
187
+ ...transformInterfaceSchemaReviewHistory({
188
+ state: ctx.state(),
189
+ instruction: props.instruction,
190
+ typeName: props.typeName,
191
+ reviewOperations: props.reviewOperations,
192
+ reviewSchema: props.reviewSchema,
193
+ preliminary,
194
+ }),
195
+ });
196
+ if (pointer.value === null) return out(result)(null);
197
+
198
+ // Apply revises to generate the modified schema content
199
+ const content: AutoBeOpenApi.IJsonSchemaDescriptive.IObject =
200
+ AutoBeInterfaceSchemaReviewProgrammer.execute({
201
+ schema: props.reviewSchema,
202
+ revises: pointer.value.revises,
203
+ });
204
+ ctx.dispatch({
205
+ type: SOURCE,
206
+ id: v7(),
207
+ typeName: props.typeName,
208
+ schema: props.reviewSchema,
209
+ review: pointer.value.review,
210
+ excludes: pointer.value.excludes,
211
+ revises: pointer.value.revises,
212
+ acquisition: preliminary.getAcquisition(),
213
+ metric: result.metric,
214
+ tokenUsage: result.tokenUsage,
215
+ step: ctx.state().analyze?.step ?? 0,
216
+ total: props.progress.total,
217
+ completed: ++props.progress.completed,
218
+ created_at: new Date().toISOString(),
219
+ } satisfies AutoBeInterfaceSchemaReviewEvent);
220
+ return out(result)(content);
221
+ });
222
+ }
223
+
224
+ function createController(
225
+ ctx: AutoBeContext,
226
+ props: {
227
+ typeName: string;
228
+ schema: AutoBeOpenApi.IJsonSchemaDescriptive.IObject;
229
+ operations: AutoBeOpenApi.IOperation[];
230
+ pointer: IPointer<
231
+ IAutoBeInterfaceSchemaReviewApplication.IComplete | null | false
232
+ >;
233
+ preliminary: AutoBePreliminaryController<
234
+ | "analysisSections"
235
+ | "databaseSchemas"
236
+ | "interfaceOperations"
237
+ | "interfaceSchemas"
238
+ | "previousAnalysisSections"
239
+ | "previousDatabaseSchemas"
240
+ | "previousInterfaceOperations"
241
+ | "previousInterfaceSchemas"
242
+ >;
243
+ },
244
+ ): IAgenticaController.IClass {
245
+ const validate: Validator = (next) => {
246
+ const result: IValidation<IAutoBeInterfaceSchemaReviewApplication.IProps> =
247
+ typia.validate<IAutoBeInterfaceSchemaReviewApplication.IProps>(next);
248
+ if (result.success === false) {
249
+ fulfillJsonSchemaErrorMessages(result.errors);
250
+ return result;
251
+ } else if (result.data.request.type !== "complete")
252
+ return props.preliminary.validate({
253
+ thinking: result.data.thinking,
254
+ request: result.data.request,
255
+ });
256
+
257
+ const errors: IValidation.IError[] = [];
258
+ AutoBeInterfaceSchemaReviewProgrammer.validate({
259
+ typeName: props.typeName,
260
+ schema: props.schema,
261
+ excludes: result.data.request.excludes,
262
+ revises: result.data.request.revises,
263
+ errors,
264
+ path: `$input.request`,
265
+ everyModels:
266
+ ctx.state().database?.result.data.files.flatMap((f) => f.models) ?? [],
267
+ });
268
+ return errors.length
269
+ ? {
270
+ success: false,
271
+ errors,
272
+ data: result.data,
273
+ }
274
+ : result;
275
+ };
276
+
277
+ const application: ILlmApplication = props.preliminary.fixApplication(
278
+ typia.llm.application<IAutoBeInterfaceSchemaReviewApplication>({
279
+ validate: {
280
+ process: validate,
281
+ },
282
+ }),
283
+ );
284
+ AutoBeInterfaceSchemaReviewProgrammer.fixApplication({
285
+ everyModels:
286
+ ctx.state().database?.result.data.files.flatMap((f) => f.models) ?? [],
287
+ application,
288
+ typeName: props.typeName,
289
+ schema: props.schema,
290
+ });
291
+
292
+ return {
293
+ protocol: "class",
294
+ name: SOURCE,
295
+ application,
296
+ execute: {
297
+ process: (input) => {
298
+ if (input.request.type === "complete")
299
+ props.pointer.value = input.request;
300
+ },
301
+ } satisfies IAutoBeInterfaceSchemaReviewApplication,
302
+ };
303
+ }
304
+
305
+ type Validator = (
306
+ input: unknown,
307
+ ) => IValidation<IAutoBeInterfaceSchemaReviewApplication.IProps>;
308
+
309
+ const SOURCE = "interfaceSchemaReview" satisfies AutoBeEventSource;