@autobe/agent 0.16.0 → 0.17.1

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 (135) hide show
  1. package/LICENSE +661 -21
  2. package/lib/AutoBeAgent.js +9 -9
  3. package/lib/AutoBeAgent.js.map +1 -1
  4. package/lib/constants/AutoBeSystemPromptConstant.d.ts +2 -2
  5. package/lib/context/AutoBeContext.d.ts +16 -4
  6. package/lib/context/AutoBeTokenUsage.d.ts +168 -11
  7. package/lib/context/AutoBeTokenUsage.js +152 -32
  8. package/lib/context/AutoBeTokenUsage.js.map +1 -1
  9. package/lib/context/AutoBeTokenUsageComponent.d.ts +116 -0
  10. package/lib/context/AutoBeTokenUsageComponent.js +120 -0
  11. package/lib/context/AutoBeTokenUsageComponent.js.map +1 -0
  12. package/lib/factory/createAutoBeApplication.js +1 -1
  13. package/lib/factory/createAutoBeApplication.js.map +1 -1
  14. package/lib/factory/createAutoBeContext.d.ts +19 -0
  15. package/lib/factory/createAutoBeContext.js +141 -0
  16. package/lib/factory/createAutoBeContext.js.map +1 -0
  17. package/lib/factory/getAutoBeGenerated.d.ts +1 -1
  18. package/lib/factory/getAutoBeGenerated.js +14 -11
  19. package/lib/factory/getAutoBeGenerated.js.map +1 -1
  20. package/lib/factory/getAutoBeRealizeGenerated.d.ts +7 -0
  21. package/lib/factory/getAutoBeRealizeGenerated.js +27 -0
  22. package/lib/factory/getAutoBeRealizeGenerated.js.map +1 -0
  23. package/lib/index.d.ts +1 -0
  24. package/lib/index.js +1 -0
  25. package/lib/index.js.map +1 -1
  26. package/lib/index.mjs +1090 -927
  27. package/lib/index.mjs.map +1 -1
  28. package/lib/orchestrate/analyze/orchestrateAnalyze.js +9 -36
  29. package/lib/orchestrate/analyze/orchestrateAnalyze.js.map +1 -1
  30. package/lib/orchestrate/analyze/orchestrateAnalyzeReviewer.js +9 -9
  31. package/lib/orchestrate/analyze/orchestrateAnalyzeReviewer.js.map +1 -1
  32. package/lib/orchestrate/analyze/writeDocumentUntilReviewPassed.js +1 -1
  33. package/lib/orchestrate/analyze/writeDocumentUntilReviewPassed.js.map +1 -1
  34. package/lib/orchestrate/interface/orchestrateInterface.js +6 -23
  35. package/lib/orchestrate/interface/orchestrateInterface.js.map +1 -1
  36. package/lib/orchestrate/interface/orchestrateInterfaceComplement.js +1 -1
  37. package/lib/orchestrate/interface/orchestrateInterfaceComplement.js.map +1 -1
  38. package/lib/orchestrate/interface/orchestrateInterfaceEndpoints.js +1 -1
  39. package/lib/orchestrate/interface/orchestrateInterfaceEndpoints.js.map +1 -1
  40. package/lib/orchestrate/interface/orchestrateInterfaceGroups.js +1 -1
  41. package/lib/orchestrate/interface/orchestrateInterfaceGroups.js.map +1 -1
  42. package/lib/orchestrate/interface/orchestrateInterfaceOperations.js +1 -1
  43. package/lib/orchestrate/interface/orchestrateInterfaceOperations.js.map +1 -1
  44. package/lib/orchestrate/interface/orchestrateInterfaceSchemas.js +1 -1
  45. package/lib/orchestrate/interface/orchestrateInterfaceSchemas.js.map +1 -1
  46. package/lib/orchestrate/prisma/{transformPrismaComponentsHistories.d.ts → histories/transformPrismaComponentsHistories.d.ts} +1 -1
  47. package/lib/orchestrate/prisma/{transformPrismaComponentsHistories.js → histories/transformPrismaComponentsHistories.js} +1 -1
  48. package/lib/orchestrate/prisma/histories/transformPrismaComponentsHistories.js.map +1 -0
  49. package/lib/orchestrate/prisma/histories/transformPrismaCorrectHistories.js.map +1 -0
  50. package/lib/orchestrate/prisma/{transformPrismaHistories.d.ts → histories/transformPrismaHistories.d.ts} +1 -1
  51. package/lib/orchestrate/prisma/histories/transformPrismaHistories.js.map +1 -0
  52. package/lib/orchestrate/prisma/histories/transformPrismaSchemaHistories.js +48 -0
  53. package/lib/orchestrate/prisma/histories/transformPrismaSchemaHistories.js.map +1 -0
  54. package/lib/orchestrate/prisma/orchestratePrisma.d.ts +1 -1
  55. package/lib/orchestrate/prisma/orchestratePrisma.js +9 -29
  56. package/lib/orchestrate/prisma/orchestratePrisma.js.map +1 -1
  57. package/lib/orchestrate/prisma/orchestratePrismaComponent.js +126 -19
  58. package/lib/orchestrate/prisma/orchestratePrismaComponent.js.map +1 -1
  59. package/lib/orchestrate/prisma/orchestratePrismaCorrect.js +10 -10
  60. package/lib/orchestrate/prisma/orchestratePrismaCorrect.js.map +1 -1
  61. package/lib/orchestrate/prisma/orchestratePrismaSchemas.js +128 -150
  62. package/lib/orchestrate/prisma/orchestratePrismaSchemas.js.map +1 -1
  63. package/lib/orchestrate/prisma/structures/IAutoBePrismaComponentApplication.d.ts +120 -0
  64. package/lib/orchestrate/prisma/structures/IAutoBePrismaComponentApplication.js +3 -0
  65. package/lib/orchestrate/prisma/structures/IAutoBePrismaComponentApplication.js.map +1 -0
  66. package/lib/orchestrate/prisma/structures/IAutoBePrismaCorrectApplication.d.ts +142 -0
  67. package/lib/orchestrate/prisma/structures/IAutoBePrismaCorrectApplication.js +3 -0
  68. package/lib/orchestrate/prisma/structures/IAutoBePrismaCorrectApplication.js.map +1 -0
  69. package/lib/orchestrate/prisma/structures/IAutoBePrismaSchemaApplication.d.ts +209 -0
  70. package/lib/orchestrate/prisma/structures/IAutoBePrismaSchemaApplication.js +3 -0
  71. package/lib/orchestrate/prisma/structures/IAutoBePrismaSchemaApplication.js.map +1 -0
  72. package/lib/orchestrate/realize/orchestrateRealize.js +13 -24
  73. package/lib/orchestrate/realize/orchestrateRealize.js.map +1 -1
  74. package/lib/orchestrate/realize/orchestrateRealizeAuthorization.js +1 -1
  75. package/lib/orchestrate/realize/orchestrateRealizeAuthorization.js.map +1 -1
  76. package/lib/orchestrate/realize/orchestrateRealizeAuthorizationCorrect.js +1 -1
  77. package/lib/orchestrate/realize/orchestrateRealizeAuthorizationCorrect.js.map +1 -1
  78. package/lib/orchestrate/realize/orchestrateRealizeCoder.js +23 -23
  79. package/lib/orchestrate/realize/orchestrateRealizeCoder.js.map +1 -1
  80. package/lib/orchestrate/test/orchestrateTest.js +8 -26
  81. package/lib/orchestrate/test/orchestrateTest.js.map +1 -1
  82. package/lib/orchestrate/test/orchestrateTestCorrect.js +1 -1
  83. package/lib/orchestrate/test/orchestrateTestCorrect.js.map +1 -1
  84. package/lib/orchestrate/test/orchestrateTestScenario.js +1 -1
  85. package/lib/orchestrate/test/orchestrateTestScenario.js.map +1 -1
  86. package/lib/orchestrate/test/orchestrateTestWrite.js +1 -1
  87. package/lib/orchestrate/test/orchestrateTestWrite.js.map +1 -1
  88. package/package.json +8 -8
  89. package/src/AutoBeAgent.ts +10 -9
  90. package/src/constants/AutoBeSystemPromptConstant.ts +2 -2
  91. package/src/context/AutoBeContext.ts +30 -3
  92. package/src/context/AutoBeTokenUsage.ts +252 -54
  93. package/src/context/AutoBeTokenUsageComponent.ts +180 -0
  94. package/src/factory/createAutoBeApplication.ts +1 -1
  95. package/src/factory/createAutoBeContext.ts +193 -0
  96. package/src/factory/getAutoBeGenerated.ts +14 -21
  97. package/src/factory/getAutoBeRealizeGenerated.ts +30 -0
  98. package/src/index.ts +1 -0
  99. package/src/orchestrate/analyze/orchestrateAnalyze.ts +9 -38
  100. package/src/orchestrate/analyze/orchestrateAnalyzeReviewer.ts +1 -1
  101. package/src/orchestrate/analyze/writeDocumentUntilReviewPassed.ts +1 -1
  102. package/src/orchestrate/interface/orchestrateInterface.ts +4 -21
  103. package/src/orchestrate/interface/orchestrateInterfaceComplement.ts +1 -1
  104. package/src/orchestrate/interface/orchestrateInterfaceEndpoints.ts +1 -1
  105. package/src/orchestrate/interface/orchestrateInterfaceGroups.ts +1 -1
  106. package/src/orchestrate/interface/orchestrateInterfaceOperations.ts +1 -1
  107. package/src/orchestrate/interface/orchestrateInterfaceSchemas.ts +1 -1
  108. package/src/orchestrate/prisma/{transformPrismaComponentsHistories.ts → histories/transformPrismaComponentsHistories.ts} +2 -2
  109. package/src/orchestrate/prisma/{transformPrismaCorrectHistories.ts → histories/transformPrismaCorrectHistories.ts} +1 -1
  110. package/src/orchestrate/prisma/{transformPrismaHistories.ts → histories/transformPrismaHistories.ts} +2 -2
  111. package/src/orchestrate/prisma/{transformPrismaSchemaHistories.ts → histories/transformPrismaSchemaHistories.ts} +1 -1
  112. package/src/orchestrate/prisma/orchestratePrisma.ts +45 -68
  113. package/src/orchestrate/prisma/orchestratePrismaComponent.ts +13 -81
  114. package/src/orchestrate/prisma/orchestratePrismaCorrect.ts +8 -148
  115. package/src/orchestrate/prisma/orchestratePrismaSchemas.ts +15 -82
  116. package/src/orchestrate/prisma/structures/IAutoBePrismaComponentApplication.ts +124 -0
  117. package/src/orchestrate/prisma/structures/IAutoBePrismaCorrectApplication.ts +144 -0
  118. package/src/orchestrate/prisma/structures/IAutoBePrismaSchemaApplication.ts +214 -0
  119. package/src/orchestrate/realize/orchestrateRealize.ts +19 -32
  120. package/src/orchestrate/realize/orchestrateRealizeAuthorization.ts +1 -1
  121. package/src/orchestrate/realize/orchestrateRealizeAuthorizationCorrect.ts +1 -1
  122. package/src/orchestrate/realize/orchestrateRealizeCoder.ts +1 -1
  123. package/src/orchestrate/test/orchestrateTest.ts +6 -26
  124. package/src/orchestrate/test/orchestrateTestCorrect.ts +1 -1
  125. package/src/orchestrate/test/orchestrateTestScenario.ts +1 -1
  126. package/src/orchestrate/test/orchestrateTestWrite.ts +1 -1
  127. package/lib/orchestrate/prisma/transformPrismaComponentsHistories.js.map +0 -1
  128. package/lib/orchestrate/prisma/transformPrismaCorrectHistories.js.map +0 -1
  129. package/lib/orchestrate/prisma/transformPrismaHistories.js.map +0 -1
  130. package/lib/orchestrate/prisma/transformPrismaSchemaHistories.js +0 -48
  131. package/lib/orchestrate/prisma/transformPrismaSchemaHistories.js.map +0 -1
  132. /package/lib/orchestrate/prisma/{transformPrismaCorrectHistories.d.ts → histories/transformPrismaCorrectHistories.d.ts} +0 -0
  133. /package/lib/orchestrate/prisma/{transformPrismaCorrectHistories.js → histories/transformPrismaCorrectHistories.js} +0 -0
  134. /package/lib/orchestrate/prisma/{transformPrismaHistories.js → histories/transformPrismaHistories.js} +0 -0
  135. /package/lib/orchestrate/prisma/{transformPrismaSchemaHistories.d.ts → histories/transformPrismaSchemaHistories.d.ts} +0 -0
@@ -1,45 +1,232 @@
1
- import { AgenticaTokenUsage } from "@agentica/core";
2
1
  import { IAutoBeTokenUsageJson } from "@autobe/interface";
3
2
 
3
+ import { AutoBeTokenUsageComponent } from "./AutoBeTokenUsageComponent";
4
4
  import { IAutoBeApplication } from "./IAutoBeApplication";
5
5
 
6
- export class AutoBeTokenUsage {
7
- public readonly facade: AgenticaTokenUsage;
8
- public readonly analyze: AgenticaTokenUsage;
9
- public readonly prisma: AgenticaTokenUsage;
10
- public readonly interface: AgenticaTokenUsage;
11
- public readonly test: AgenticaTokenUsage;
12
- public readonly realize: AgenticaTokenUsage;
6
+ /**
7
+ * Comprehensive token usage tracker for the AutoBe vibe coding system.
8
+ *
9
+ * This class provides centralized tracking and management of token consumption
10
+ * across all AI agents in the automated development pipeline. It captures
11
+ * detailed token usage statistics for each processing phase - from initial
12
+ * requirements analysis through final implementation - enabling cost
13
+ * monitoring, performance optimization, and resource utilization analysis.
14
+ *
15
+ * The token usage data includes both input tokens (with cache efficiency) and
16
+ * output tokens (with generation type breakdowns), providing insights into AI
17
+ * processing efficiency and helping optimize the balance between computational
18
+ * costs and output quality.
19
+ *
20
+ * @author SunRabbit123
21
+ */
22
+ export class AutoBeTokenUsage implements IAutoBeTokenUsageJson {
23
+ /**
24
+ * Token usage for the facade agent orchestrating the entire pipeline.
25
+ *
26
+ * Tracks tokens consumed by the initial agent that coordinates and manages
27
+ * the overall vibe coding process, including request parsing and response
28
+ * orchestration.
29
+ */
30
+ public readonly facade: AutoBeTokenUsageComponent;
13
31
 
14
- public constructor(props?: IAutoBeTokenUsageJson) {
32
+ /**
33
+ * Token usage for the requirements analysis agent.
34
+ *
35
+ * Captures tokens used during the analysis phase where user requirements are
36
+ * processed, understood, and transformed into structured specifications for
37
+ * subsequent development phases.
38
+ */
39
+ public readonly analyze: AutoBeTokenUsageComponent;
40
+
41
+ /**
42
+ * Token usage for the Prisma database schema generation agent.
43
+ *
44
+ * Records tokens consumed while designing and generating database schemas,
45
+ * including entity relationships, field definitions, and database-specific
46
+ * optimizations.
47
+ */
48
+ public readonly prisma: AutoBeTokenUsageComponent;
49
+
50
+ /**
51
+ * Token usage for the API interface specification agent.
52
+ *
53
+ * Tracks tokens used in creating OpenAPI/Swagger specifications, defining
54
+ * endpoints, request/response structures, and API documentation.
55
+ */
56
+ public readonly interface: AutoBeTokenUsageComponent;
57
+
58
+ /**
59
+ * Token usage for the test code generation agent.
60
+ *
61
+ * Monitors tokens consumed during automated test creation, including scenario
62
+ * planning, test case generation, and end-to-end test implementation.
63
+ */
64
+ public readonly test: AutoBeTokenUsageComponent;
65
+
66
+ /**
67
+ * Token usage for the implementation realization agent.
68
+ *
69
+ * Captures tokens used in the final implementation phase where actual
70
+ * business logic, controllers, services, and integration code are generated.
71
+ */
72
+ public readonly realize: AutoBeTokenUsageComponent;
73
+
74
+ /**
75
+ * Aggregated token usage statistics across all agents.
76
+ *
77
+ * Provides a unified view of token consumption by combining data from all
78
+ * processing phases in the vibe coding pipeline. This computed property
79
+ * dynamically calculates the sum of all agent components (facade, analyze,
80
+ * prisma, interface, test, realize) whenever accessed, ensuring the aggregate
81
+ * always reflects the current state of token usage.
82
+ *
83
+ * The aggregation performs element-wise addition across all token metrics,
84
+ * including total counts, input breakdowns with cache statistics, and output
85
+ * categorizations by generation type. This comprehensive view enables overall
86
+ * cost assessment and resource utilization analysis for the entire automated
87
+ * development session.
88
+ */
89
+ public get aggregate(): IAutoBeTokenUsageJson.IComponent {
90
+ return AutoBeTokenUsage.keys()
91
+ .reduce(
92
+ (acc, cur) => AutoBeTokenUsageComponent.plus(acc, this[cur]),
93
+ new AutoBeTokenUsageComponent(),
94
+ )
95
+ .toJSON();
96
+ }
97
+
98
+ /* -----------------------------------------------------------
99
+ CONSTRUCTORS
100
+ ----------------------------------------------------------- */
101
+ /**
102
+ * Default Constructor.
103
+ *
104
+ * Creates a new token usage tracker with all agent phases initialized to
105
+ * empty components. Each component starts with zero values for all token
106
+ * counters, providing a clean slate for tracking token consumption from the
107
+ * beginning of a vibe coding session.
108
+ */
109
+ public constructor();
110
+
111
+ /**
112
+ * Binding Constructor.
113
+ *
114
+ * Creates a new instance that shares component references with the provided
115
+ * AutoBeTokenUsage instance. This establishes a bound relationship where both
116
+ * instances point to the same component objects. Any modifications to token
117
+ * counts through either instance will be reflected in both, enabling shared
118
+ * tracking across different contexts.
119
+ *
120
+ * @param bind Existing AutoBeTokenUsage instance to bind to
121
+ */
122
+ public constructor(bind: AutoBeTokenUsage);
123
+
124
+ /**
125
+ * Initializer Constructor.
126
+ *
127
+ * Reconstructs a token usage tracker from serialized JSON data. Creates new
128
+ * AutoBeTokenUsageComponent instances for each agent phase, initializing them
129
+ * with the corresponding values from the JSON structure. This enables
130
+ * restoration of token usage state from persisted data or transmission
131
+ * between different parts of the system.
132
+ *
133
+ * @param props Token usage data in JSON format
134
+ */
135
+ public constructor(props?: Omit<IAutoBeTokenUsageJson, "aggregate">);
136
+
137
+ public constructor(
138
+ props?: AutoBeTokenUsage | IAutoBeTokenUsageJson | undefined,
139
+ ) {
15
140
  if (props === undefined) {
16
- this.facade = new AgenticaTokenUsage();
17
- this.analyze = new AgenticaTokenUsage();
18
- this.prisma = new AgenticaTokenUsage();
19
- this.interface = new AgenticaTokenUsage();
20
- this.test = new AgenticaTokenUsage();
21
- this.realize = new AgenticaTokenUsage();
141
+ this.facade = new AutoBeTokenUsageComponent();
142
+ this.analyze = new AutoBeTokenUsageComponent();
143
+ this.prisma = new AutoBeTokenUsageComponent();
144
+ this.interface = new AutoBeTokenUsageComponent();
145
+ this.test = new AutoBeTokenUsageComponent();
146
+ this.realize = new AutoBeTokenUsageComponent();
22
147
  return;
148
+ } else if (props instanceof AutoBeTokenUsage) {
149
+ this.facade = props.facade;
150
+ this.analyze = props.analyze;
151
+ this.prisma = props.prisma;
152
+ this.interface = props.interface;
153
+ this.test = props.test;
154
+ this.realize = props.realize;
155
+ } else {
156
+ this.facade = new AutoBeTokenUsageComponent(props.facade);
157
+ this.analyze = new AutoBeTokenUsageComponent(props.analyze);
158
+ this.prisma = new AutoBeTokenUsageComponent(props.prisma);
159
+ this.interface = new AutoBeTokenUsageComponent(props.interface);
160
+ this.test = new AutoBeTokenUsageComponent(props.test);
161
+ this.realize = new AutoBeTokenUsageComponent(props.realize);
23
162
  }
163
+ }
24
164
 
25
- this.facade = new AgenticaTokenUsage(props.facade);
26
- this.analyze = new AgenticaTokenUsage(props.analyze);
27
- this.prisma = new AgenticaTokenUsage(props.prisma);
28
- this.interface = new AgenticaTokenUsage(props.interface);
29
- this.test = new AgenticaTokenUsage(props.test);
30
- this.realize = new AgenticaTokenUsage(props.realize);
165
+ /**
166
+ * Serialize token usage data to JSON format.
167
+ *
168
+ * Converts the internal token usage representation to the standardized
169
+ * IAutoBeTokenUsageJson format, suitable for persistence, transmission, or
170
+ * external analysis. The serialized data maintains the complete structure
171
+ * including all agent phases and detailed token breakdowns.
172
+ *
173
+ * @returns JSON representation of token usage statistics
174
+ */
175
+ public toJSON(): IAutoBeTokenUsageJson {
176
+ return {
177
+ aggregate: this.aggregate,
178
+ facade: this.facade.toJSON(),
179
+ analyze: this.analyze.toJSON(),
180
+ prisma: this.prisma.toJSON(),
181
+ interface: this.interface.toJSON(),
182
+ test: this.test.toJSON(),
183
+ realize: this.realize.toJSON(),
184
+ };
31
185
  }
32
186
 
187
+ /* -----------------------------------------------------------
188
+ OPERATORS
189
+ ----------------------------------------------------------- */
190
+ /**
191
+ * Record token usage for specific processing stages.
192
+ *
193
+ * Updates token consumption statistics for one or more agent phases based on
194
+ * the provided usage data. This method allows selective recording of token
195
+ * usage for specific stages, enabling fine-grained tracking during
196
+ * multi-phase processing or when certain agents are invoked multiple times.
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * tokenUsage.record(
201
+ * { total: 150, input: { total: 100, cached: 20 }, output: { total: 50, ... } },
202
+ * ['analyze', 'prisma']
203
+ * );
204
+ * ```;
205
+ *
206
+ * @param usage - Token usage component data to record
207
+ * @param additionalStages - Array of stage names to update with the usage
208
+ * data
209
+ */
33
210
  public record(
34
- usage: AgenticaTokenUsage,
211
+ usage: IAutoBeTokenUsageJson.IComponent,
35
212
  additionalStages: (keyof IAutoBeApplication)[] = [],
36
213
  ) {
37
- this.facade.increment(usage);
38
214
  additionalStages.forEach((stage) => {
39
215
  this[stage].increment(usage);
40
216
  });
41
217
  }
42
218
 
219
+ /**
220
+ * Increment current token usage with data from another instance.
221
+ *
222
+ * Adds token usage statistics from another AutoBeTokenUsage instance to this
223
+ * one, updating all agent phases simultaneously. This method is useful for
224
+ * combining token usage from parallel processing, multiple runs, or when
225
+ * aggregating statistics from distributed agent executions.
226
+ *
227
+ * @param usage - AutoBeTokenUsage instance to add to current statistics
228
+ * @returns This instance for method chaining
229
+ */
43
230
  public increment(usage: AutoBeTokenUsage) {
44
231
  AutoBeTokenUsage.keys().forEach((key) => {
45
232
  this[key].increment(usage[key]);
@@ -47,41 +234,52 @@ export class AutoBeTokenUsage {
47
234
  return this;
48
235
  }
49
236
 
237
+ /**
238
+ * Create a new instance combining token usage from two sources.
239
+ *
240
+ * Performs element-wise addition of token usage statistics from two
241
+ * AutoBeTokenUsage instances, creating a new instance with the combined
242
+ * totals. This static method is useful for aggregating token usage across
243
+ * multiple vibe coding sessions or when merging statistics from different
244
+ * execution contexts.
245
+ *
246
+ * @param usageA - First token usage instance
247
+ * @param usageB - Second token usage instance
248
+ * @returns New instance with combined token usage statistics
249
+ */
50
250
  public static plus(usageA: AutoBeTokenUsage, usageB: AutoBeTokenUsage) {
51
251
  return new AutoBeTokenUsage({
52
- facade: AgenticaTokenUsage.plus(usageA.facade, usageB.facade),
53
- analyze: AgenticaTokenUsage.plus(usageA.analyze, usageB.analyze),
54
- prisma: AgenticaTokenUsage.plus(usageA.prisma, usageB.prisma),
55
- interface: AgenticaTokenUsage.plus(usageA.interface, usageB.interface),
56
- test: AgenticaTokenUsage.plus(usageA.test, usageB.test),
57
- realize: AgenticaTokenUsage.plus(usageA.realize, usageB.realize),
252
+ facade: AutoBeTokenUsageComponent.plus(usageA.facade, usageB.facade),
253
+ analyze: AutoBeTokenUsageComponent.plus(usageA.analyze, usageB.analyze),
254
+ prisma: AutoBeTokenUsageComponent.plus(usageA.prisma, usageB.prisma),
255
+ interface: AutoBeTokenUsageComponent.plus(
256
+ usageA.interface,
257
+ usageB.interface,
258
+ ),
259
+ test: AutoBeTokenUsageComponent.plus(usageA.test, usageB.test),
260
+ realize: AutoBeTokenUsageComponent.plus(usageA.realize, usageB.realize),
58
261
  });
59
262
  }
60
263
 
61
- public toJSON(): IAutoBeTokenUsageJson {
62
- return {
63
- facade: this.facade.toJSON(),
64
- analyze: this.analyze.toJSON(),
65
- prisma: this.prisma.toJSON(),
66
- interface: this.interface.toJSON(),
67
- test: this.test.toJSON(),
68
- realize: this.realize.toJSON(),
69
- };
70
- }
71
-
72
- /** @internal */
73
- private static keys(): ("facade" | keyof IAutoBeApplication)[] {
74
- return ["facade", "analyze", "prisma", "interface", "test", "realize"];
264
+ /**
265
+ * Get all agent phase keys for iteration.
266
+ *
267
+ * Returns a readonly array of all agent phase names used in the vibe coding
268
+ * system. This internal utility method ensures consistent iteration over all
269
+ * token usage components when performing operations like aggregation or
270
+ * serialization.
271
+ *
272
+ * @returns Readonly array of agent phase keys
273
+ * @internal
274
+ */
275
+ private static keys() {
276
+ return [
277
+ "facade",
278
+ "analyze",
279
+ "prisma",
280
+ "interface",
281
+ "test",
282
+ "realize",
283
+ ] as const;
75
284
  }
76
285
  }
77
-
78
- // /** Type check statements */
79
- // 1 as unknown as AutoBeTokenUsage satisfies {
80
- // [key in "facade" | keyof IAutoBeApplication]: AgenticaTokenUsage;
81
- // };
82
-
83
- // 1 as unknown as IAutoBeTokenUsageJson satisfies {
84
- // [key in "facade" | keyof IAutoBeApplication]: IAutoBeInternalTokenUsageJson;
85
- // };
86
-
87
- // 1 as unknown as IAutoBeInternalTokenUsageJson satisfies IAgenticaTokenUsageJson;
@@ -0,0 +1,180 @@
1
+ import { IAutoBeTokenUsageJson } from "@autobe/interface";
2
+
3
+ /**
4
+ * Token usage component for individual AI agents in the vibe coding pipeline.
5
+ *
6
+ * Represents detailed token consumption statistics for a specific processing
7
+ * phase (facade, analyze, prisma, interface, test, or realize). This class
8
+ * tracks both input and output token usage with granular breakdowns, enabling
9
+ * precise cost analysis and performance optimization for each agent.
10
+ *
11
+ * The component structure includes:
12
+ *
13
+ * - Total token count for quick cost calculations
14
+ * - Input token breakdown with cache efficiency metrics
15
+ * - Output token categorization by generation type
16
+ *
17
+ * This granular tracking helps identify optimization opportunities and
18
+ * understand the computational characteristics of each agent phase.
19
+ *
20
+ * @author SunRabbit123
21
+ */
22
+ export class AutoBeTokenUsageComponent
23
+ implements IAutoBeTokenUsageJson.IComponent
24
+ {
25
+ /**
26
+ * Detailed breakdown of input token consumption.
27
+ *
28
+ * Tracks how many tokens were processed as input to the AI agent, including:
29
+ *
30
+ * - Total input tokens processed
31
+ * - Cached tokens that were reused from previous operations
32
+ *
33
+ * The cache efficiency (cached/total ratio) indicates how well the system is
34
+ * reusing context across multiple invocations.
35
+ */
36
+ public readonly input: IAutoBeTokenUsageJson.IInput;
37
+
38
+ /**
39
+ * Detailed breakdown of output token generation.
40
+ *
41
+ * Categorizes generated tokens by their purpose and acceptance status:
42
+ *
43
+ * - Total output tokens generated
44
+ * - Reasoning tokens (internal processing)
45
+ * - Accepted prediction tokens (efficient generation)
46
+ * - Rejected prediction tokens (quality control overhead)
47
+ *
48
+ * These metrics help understand the AI's generation efficiency and the
49
+ * effectiveness of its predictive mechanisms.
50
+ */
51
+ public readonly output: IAutoBeTokenUsageJson.IOutput;
52
+
53
+ /**
54
+ * Total token count combining all input and output tokens.
55
+ *
56
+ * Represents the complete token consumption for this component, providing a
57
+ * single metric for overall resource utilization. This value is critical for
58
+ * cost calculations and comparing efficiency across different agents or
59
+ * processing phases.
60
+ */
61
+ public get total(): number {
62
+ return this.input.total + this.output.total;
63
+ }
64
+
65
+ /* -----------------------------------------------------------
66
+ CONSTRUCTORS
67
+ ----------------------------------------------------------- */
68
+ /**
69
+ * Default Constructor.
70
+ *
71
+ * Creates a new token usage component with all counters initialized to zero.
72
+ * Constructs fresh input and output objects with default values, providing a
73
+ * clean starting point for tracking token consumption in an agent phase.
74
+ */
75
+ public constructor();
76
+
77
+ /**
78
+ * Initializer Constructor.
79
+ *
80
+ * Creates a new component populated with existing token usage data. Directly
81
+ * assigns the provided values to instance properties, preserving the exact
82
+ * token counts and structure from the source data for accurate tracking
83
+ * continuation.
84
+ *
85
+ * @param props - Token usage data to initialize the component
86
+ */
87
+ public constructor(props: Omit<IAutoBeTokenUsageJson.IComponent, "total">);
88
+
89
+ public constructor(props?: Omit<IAutoBeTokenUsageJson.IComponent, "total">) {
90
+ if (props === undefined) {
91
+ this.input = { total: 0, cached: 0 };
92
+ this.output = {
93
+ total: 0,
94
+ reasoning: 0,
95
+ accepted_prediction: 0,
96
+ rejected_prediction: 0,
97
+ };
98
+ return;
99
+ }
100
+ this.input = props.input;
101
+ this.output = props.output;
102
+ }
103
+
104
+ /**
105
+ * Export token usage data as JSON.
106
+ *
107
+ * Converts the component's token usage statistics to the standardized
108
+ * IAutoBeTokenUsageJson.IComponent format. This serialization maintains the
109
+ * complete structure including total counts and detailed breakdowns for both
110
+ * input and output tokens.
111
+ *
112
+ * @returns JSON representation of the token usage component
113
+ */
114
+ public toJSON(): IAutoBeTokenUsageJson.IComponent {
115
+ return {
116
+ total: this.total,
117
+ input: this.input,
118
+ output: this.output,
119
+ };
120
+ }
121
+
122
+ /* -----------------------------------------------------------
123
+ OPERATORS
124
+ ----------------------------------------------------------- */
125
+ /**
126
+ * Add token usage data to current statistics.
127
+ *
128
+ * Increments all token counters in this component by the corresponding values
129
+ * from the provided component data. This method performs in-place updates,
130
+ * modifying the current instance rather than creating a new one.
131
+ *
132
+ * Updates include:
133
+ *
134
+ * - Total token count
135
+ * - Input tokens (both total and cached)
136
+ * - Output tokens (reasoning, accepted/rejected predictions)
137
+ *
138
+ * @param props - Token usage component data to add to current values
139
+ */
140
+ public increment(props: IAutoBeTokenUsageJson.IComponent) {
141
+ this.input.total += props.input.total;
142
+ this.input.cached += props.input.cached;
143
+ this.output.total += props.output.total;
144
+ this.output.reasoning += props.output.reasoning;
145
+ this.output.accepted_prediction += props.output.accepted_prediction;
146
+ this.output.rejected_prediction += props.output.rejected_prediction;
147
+ }
148
+
149
+ /**
150
+ * Create new component combining two token usage statistics.
151
+ *
152
+ * Performs element-wise addition of all token counters from two components,
153
+ * creating a new AutoBeTokenUsageComponent instance with the combined totals.
154
+ * This static method is useful for aggregating token usage across multiple
155
+ * agent invocations or combining statistics from parallel processing.
156
+ *
157
+ * @param a - First token usage component
158
+ * @param b - Second token usage component
159
+ * @returns New component with combined token statistics
160
+ */
161
+ public static plus(
162
+ a: AutoBeTokenUsageComponent,
163
+ b: AutoBeTokenUsageComponent,
164
+ ) {
165
+ return new AutoBeTokenUsageComponent({
166
+ input: {
167
+ total: a.input.total + b.input.total,
168
+ cached: a.input.cached + b.input.cached,
169
+ },
170
+ output: {
171
+ total: a.output.total + b.output.total,
172
+ reasoning: a.output.reasoning + b.output.reasoning,
173
+ accepted_prediction:
174
+ a.output.accepted_prediction + b.output.accepted_prediction,
175
+ rejected_prediction:
176
+ a.output.rejected_prediction + b.output.rejected_prediction,
177
+ },
178
+ });
179
+ }
180
+ }
@@ -43,7 +43,7 @@ export const createAutoBeController = <Model extends ILlmSchema.Model>(props: {
43
43
  };
44
44
  },
45
45
  prisma: async (next) => {
46
- const r = await orchestratePrisma(props.context)(next);
46
+ const r = await orchestratePrisma(props.context, next);
47
47
  if (r.type === "prisma")
48
48
  return {
49
49
  type: r.compiled.type,