aios-core 3.6.0 → 3.8.0
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.
- package/.aios-core/core/session/context-detector.js +3 -0
- package/.aios-core/core/session/context-loader.js +154 -0
- package/.aios-core/data/learned-patterns.yaml +3 -0
- package/.aios-core/data/workflow-patterns.yaml +347 -3
- package/.aios-core/development/agents/dev.md +13 -0
- package/.aios-core/development/agents/squad-creator.md +30 -0
- package/.aios-core/development/scripts/squad/squad-analyzer.js +638 -0
- package/.aios-core/development/scripts/squad/squad-extender.js +871 -0
- package/.aios-core/development/scripts/squad/squad-generator.js +107 -19
- package/.aios-core/development/scripts/squad/squad-migrator.js +3 -5
- package/.aios-core/development/scripts/squad/squad-validator.js +98 -0
- package/.aios-core/development/tasks/create-service.md +391 -0
- package/.aios-core/development/tasks/next.md +294 -0
- package/.aios-core/development/tasks/patterns.md +334 -0
- package/.aios-core/development/tasks/squad-creator-analyze.md +315 -0
- package/.aios-core/development/tasks/squad-creator-create.md +26 -3
- package/.aios-core/development/tasks/squad-creator-extend.md +411 -0
- package/.aios-core/development/tasks/squad-creator-validate.md +9 -1
- package/.aios-core/development/tasks/waves.md +205 -0
- package/.aios-core/development/templates/service-template/README.md.hbs +158 -0
- package/.aios-core/development/templates/service-template/__tests__/index.test.ts.hbs +237 -0
- package/.aios-core/development/templates/service-template/client.ts.hbs +403 -0
- package/.aios-core/development/templates/service-template/errors.ts.hbs +182 -0
- package/.aios-core/development/templates/service-template/index.ts.hbs +120 -0
- package/.aios-core/development/templates/service-template/jest.config.js +89 -0
- package/.aios-core/development/templates/service-template/package.json.hbs +87 -0
- package/.aios-core/development/templates/service-template/tsconfig.json +45 -0
- package/.aios-core/development/templates/service-template/types.ts.hbs +145 -0
- package/.aios-core/development/templates/squad/agent-template.md +69 -0
- package/.aios-core/development/templates/squad/checklist-template.md +82 -0
- package/.aios-core/development/templates/squad/data-template.yaml +105 -0
- package/.aios-core/development/templates/squad/script-template.js +179 -0
- package/.aios-core/development/templates/squad/task-template.md +125 -0
- package/.aios-core/development/templates/squad/template-template.md +97 -0
- package/.aios-core/development/templates/squad/tool-template.js +103 -0
- package/.aios-core/development/templates/squad/workflow-template.yaml +108 -0
- package/.aios-core/infrastructure/scripts/ide-sync/agent-parser.js +45 -1
- package/.aios-core/infrastructure/scripts/ide-sync/transformers/antigravity.js +6 -6
- package/.aios-core/infrastructure/scripts/ide-sync/transformers/cursor.js +5 -4
- package/.aios-core/infrastructure/scripts/ide-sync/transformers/trae.js +3 -3
- package/.aios-core/infrastructure/scripts/ide-sync/transformers/windsurf.js +3 -3
- package/.aios-core/install-manifest.yaml +139 -35
- package/.aios-core/quality/metrics-collector.js +27 -0
- package/.aios-core/scripts/session-context-loader.js +13 -254
- package/.aios-core/utils/aios-validator.js +25 -0
- package/.aios-core/workflow-intelligence/__tests__/confidence-scorer.test.js +334 -0
- package/.aios-core/workflow-intelligence/__tests__/integration.test.js +337 -0
- package/.aios-core/workflow-intelligence/__tests__/suggestion-engine.test.js +431 -0
- package/.aios-core/workflow-intelligence/__tests__/wave-analyzer.test.js +458 -0
- package/.aios-core/workflow-intelligence/__tests__/workflow-registry.test.js +302 -0
- package/.aios-core/workflow-intelligence/engine/confidence-scorer.js +305 -0
- package/.aios-core/workflow-intelligence/engine/output-formatter.js +285 -0
- package/.aios-core/workflow-intelligence/engine/suggestion-engine.js +603 -0
- package/.aios-core/workflow-intelligence/engine/wave-analyzer.js +676 -0
- package/.aios-core/workflow-intelligence/index.js +327 -0
- package/.aios-core/workflow-intelligence/learning/capture-hook.js +147 -0
- package/.aios-core/workflow-intelligence/learning/index.js +230 -0
- package/.aios-core/workflow-intelligence/learning/pattern-capture.js +340 -0
- package/.aios-core/workflow-intelligence/learning/pattern-store.js +498 -0
- package/.aios-core/workflow-intelligence/learning/pattern-validator.js +309 -0
- package/.aios-core/workflow-intelligence/registry/workflow-registry.js +358 -0
- package/package.json +1 -1
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP client for {{pascalCase serviceName}} API integration.
|
|
3
|
+
* Includes rate limiting, retry logic with exponential backoff, and request logging.
|
|
4
|
+
* @module @aios/{{kebabCase serviceName}}/client
|
|
5
|
+
* @story {{storyId}}
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
{{#if isApiIntegration}}
|
|
9
|
+
import type {
|
|
10
|
+
{{pascalCase serviceName}}Config,
|
|
11
|
+
{{pascalCase serviceName}}ApiResponse,
|
|
12
|
+
{{pascalCase serviceName}}RequestOptions,
|
|
13
|
+
{{pascalCase serviceName}}RateLimit,
|
|
14
|
+
} from './types';
|
|
15
|
+
import {
|
|
16
|
+
{{pascalCase serviceName}}Error,
|
|
17
|
+
{{pascalCase serviceName}}ErrorCode,
|
|
18
|
+
{{pascalCase serviceName}}Errors,
|
|
19
|
+
} from './errors';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Default configuration values.
|
|
23
|
+
*/
|
|
24
|
+
const DEFAULTS = {
|
|
25
|
+
baseUrl: '{{apiBaseUrl}}',
|
|
26
|
+
timeout: 30000,
|
|
27
|
+
maxRetries: 3,
|
|
28
|
+
retryBaseDelay: 1000,
|
|
29
|
+
retryMaxDelay: 30000,
|
|
30
|
+
debug: false,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* HTTP client for {{pascalCase serviceName}} API.
|
|
35
|
+
*/
|
|
36
|
+
export class {{pascalCase serviceName}}Client {
|
|
37
|
+
private readonly config: Required<Pick<{{pascalCase serviceName}}Config, 'baseUrl' | 'timeout' | 'maxRetries' | 'debug'>> & {{pascalCase serviceName}}Config;
|
|
38
|
+
private rateLimit: {{pascalCase serviceName}}RateLimit | null = null;
|
|
39
|
+
|
|
40
|
+
constructor(config: {{pascalCase serviceName}}Config) {
|
|
41
|
+
this.config = {
|
|
42
|
+
...config,
|
|
43
|
+
baseUrl: config.baseUrl ?? DEFAULTS.baseUrl,
|
|
44
|
+
timeout: config.timeout ?? DEFAULTS.timeout,
|
|
45
|
+
maxRetries: config.maxRetries ?? DEFAULTS.maxRetries,
|
|
46
|
+
debug: config.debug ?? DEFAULTS.debug,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Make an API request with automatic retry and rate limiting.
|
|
52
|
+
*/
|
|
53
|
+
async request<T>(
|
|
54
|
+
endpoint: string,
|
|
55
|
+
options: {{pascalCase serviceName}}RequestOptions = {}
|
|
56
|
+
): Promise<{{pascalCase serviceName}}ApiResponse<T>> {
|
|
57
|
+
const {
|
|
58
|
+
method = 'GET',
|
|
59
|
+
headers = {},
|
|
60
|
+
body,
|
|
61
|
+
params,
|
|
62
|
+
timeout = this.config.timeout,
|
|
63
|
+
noRetry = false,
|
|
64
|
+
} = options;
|
|
65
|
+
|
|
66
|
+
// Build URL with query parameters
|
|
67
|
+
const url = this.buildUrl(endpoint, params);
|
|
68
|
+
|
|
69
|
+
// Wait for rate limit if necessary
|
|
70
|
+
await this.waitForRateLimit();
|
|
71
|
+
|
|
72
|
+
const requestHeaders: Record<string, string> = {
|
|
73
|
+
'Content-Type': 'application/json',
|
|
74
|
+
'Accept': 'application/json',
|
|
75
|
+
...this.getAuthHeaders(),
|
|
76
|
+
...headers,
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const requestInit: RequestInit = {
|
|
80
|
+
method,
|
|
81
|
+
headers: requestHeaders,
|
|
82
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// Retry logic
|
|
86
|
+
const maxAttempts = noRetry ? 1 : this.config.maxRetries + 1;
|
|
87
|
+
let lastError: Error | null = null;
|
|
88
|
+
|
|
89
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
90
|
+
try {
|
|
91
|
+
this.debug(`Request attempt ${attempt}/${maxAttempts}: ${method} ${url}`);
|
|
92
|
+
|
|
93
|
+
const response = await this.fetchWithTimeout(url, requestInit, timeout);
|
|
94
|
+
|
|
95
|
+
// Update rate limit info from headers
|
|
96
|
+
this.updateRateLimit(response.headers);
|
|
97
|
+
|
|
98
|
+
// Handle rate limiting (429)
|
|
99
|
+
if (response.status === 429) {
|
|
100
|
+
const retryAfter = this.getRetryAfter(response.headers);
|
|
101
|
+
if (attempt < maxAttempts) {
|
|
102
|
+
this.debug(`Rate limited, waiting ${retryAfter}ms before retry`);
|
|
103
|
+
await this.sleep(retryAfter);
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
throw {{pascalCase serviceName}}Errors.rateLimitError(
|
|
107
|
+
Math.ceil(retryAfter / 1000),
|
|
108
|
+
this.rateLimit ?? { remaining: 0, reset: Date.now() + retryAfter }
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Parse response
|
|
113
|
+
const data = await this.parseResponse<T>(response);
|
|
114
|
+
|
|
115
|
+
// Handle error responses
|
|
116
|
+
if (!response.ok) {
|
|
117
|
+
throw this.handleErrorResponse(response.status, data);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
success: true,
|
|
122
|
+
data: data as T,
|
|
123
|
+
meta: {
|
|
124
|
+
requestId: response.headers.get('x-request-id') ?? undefined,
|
|
125
|
+
rateLimit: this.rateLimit ?? undefined,
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
} catch (error) {
|
|
129
|
+
lastError = error as Error;
|
|
130
|
+
|
|
131
|
+
// Don't retry on certain errors
|
|
132
|
+
if (
|
|
133
|
+
error instanceof {{pascalCase serviceName}}Error &&
|
|
134
|
+
[
|
|
135
|
+
{{pascalCase serviceName}}ErrorCode.AUTHENTICATION_ERROR,
|
|
136
|
+
{{pascalCase serviceName}}ErrorCode.AUTHORIZATION_ERROR,
|
|
137
|
+
{{pascalCase serviceName}}ErrorCode.CONFIGURATION_ERROR,
|
|
138
|
+
].includes(error.code)
|
|
139
|
+
) {
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Retry with exponential backoff
|
|
144
|
+
if (attempt < maxAttempts) {
|
|
145
|
+
const delay = this.calculateBackoff(attempt);
|
|
146
|
+
this.debug(`Request failed, retrying in ${delay}ms: ${(error as Error).message}`);
|
|
147
|
+
await this.sleep(delay);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// All retries exhausted
|
|
153
|
+
throw lastError instanceof {{pascalCase serviceName}}Error
|
|
154
|
+
? lastError
|
|
155
|
+
: {{pascalCase serviceName}}Errors.networkError(
|
|
156
|
+
`Request failed after ${maxAttempts} attempts`,
|
|
157
|
+
lastError ?? undefined
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Convenience method for GET requests.
|
|
163
|
+
*/
|
|
164
|
+
async get<T>(endpoint: string, params?: Record<string, string | number | boolean>): Promise<T> {
|
|
165
|
+
const response = await this.request<T>(endpoint, { method: 'GET', params });
|
|
166
|
+
return response.data as T;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Convenience method for POST requests.
|
|
171
|
+
*/
|
|
172
|
+
async post<T>(endpoint: string, body?: unknown): Promise<T> {
|
|
173
|
+
const response = await this.request<T>(endpoint, { method: 'POST', body });
|
|
174
|
+
return response.data as T;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Convenience method for PUT requests.
|
|
179
|
+
*/
|
|
180
|
+
async put<T>(endpoint: string, body?: unknown): Promise<T> {
|
|
181
|
+
const response = await this.request<T>(endpoint, { method: 'PUT', body });
|
|
182
|
+
return response.data as T;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Convenience method for DELETE requests.
|
|
187
|
+
*/
|
|
188
|
+
async delete<T>(endpoint: string): Promise<T> {
|
|
189
|
+
const response = await this.request<T>(endpoint, { method: 'DELETE' });
|
|
190
|
+
return response.data as T;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Health check / ping endpoint.
|
|
195
|
+
*/
|
|
196
|
+
async ping(): Promise<boolean> {
|
|
197
|
+
try {
|
|
198
|
+
await this.get('/ping');
|
|
199
|
+
return true;
|
|
200
|
+
} catch {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Get current rate limit status.
|
|
207
|
+
*/
|
|
208
|
+
getRateLimit(): {{pascalCase serviceName}}RateLimit | null {
|
|
209
|
+
return this.rateLimit;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
213
|
+
// Private Methods
|
|
214
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Build full URL with query parameters.
|
|
218
|
+
*/
|
|
219
|
+
private buildUrl(
|
|
220
|
+
endpoint: string,
|
|
221
|
+
params?: Record<string, string | number | boolean>
|
|
222
|
+
): string {
|
|
223
|
+
const baseUrl = this.config.baseUrl.replace(/\/$/, '');
|
|
224
|
+
const path = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
|
|
225
|
+
const url = new URL(`${baseUrl}${path}`);
|
|
226
|
+
|
|
227
|
+
if (params) {
|
|
228
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
229
|
+
if (value !== undefined && value !== null) {
|
|
230
|
+
url.searchParams.append(key, String(value));
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return url.toString();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Get authentication headers.
|
|
240
|
+
*/
|
|
241
|
+
private getAuthHeaders(): Record<string, string> {
|
|
242
|
+
const headers: Record<string, string> = {};
|
|
243
|
+
|
|
244
|
+
{{#each envVars}}
|
|
245
|
+
{{#if this.isAuthHeader}}
|
|
246
|
+
if (this.config.{{camelCase this.name}}) {
|
|
247
|
+
headers['{{this.headerName}}'] = {{#if this.headerPrefix}}`{{this.headerPrefix}} ${this.config.{{camelCase this.name}}}`{{else}}this.config.{{camelCase this.name}}{{/if}};
|
|
248
|
+
}
|
|
249
|
+
{{/if}}
|
|
250
|
+
{{/each}}
|
|
251
|
+
|
|
252
|
+
return headers;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Fetch with timeout support.
|
|
257
|
+
*/
|
|
258
|
+
private async fetchWithTimeout(
|
|
259
|
+
url: string,
|
|
260
|
+
init: RequestInit,
|
|
261
|
+
timeout: number
|
|
262
|
+
): Promise<Response> {
|
|
263
|
+
const controller = new AbortController();
|
|
264
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
265
|
+
|
|
266
|
+
try {
|
|
267
|
+
const response = await fetch(url, {
|
|
268
|
+
...init,
|
|
269
|
+
signal: controller.signal,
|
|
270
|
+
});
|
|
271
|
+
return response;
|
|
272
|
+
} catch (error) {
|
|
273
|
+
if ((error as Error).name === 'AbortError') {
|
|
274
|
+
throw {{pascalCase serviceName}}Errors.timeoutError(`Request timed out after ${timeout}ms`);
|
|
275
|
+
}
|
|
276
|
+
throw error;
|
|
277
|
+
} finally {
|
|
278
|
+
clearTimeout(timeoutId);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Parse response body.
|
|
284
|
+
*/
|
|
285
|
+
private async parseResponse<T>(response: Response): Promise<T | null> {
|
|
286
|
+
const contentType = response.headers.get('content-type');
|
|
287
|
+
|
|
288
|
+
if (contentType?.includes('application/json')) {
|
|
289
|
+
try {
|
|
290
|
+
return await response.json();
|
|
291
|
+
} catch {
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Handle error response.
|
|
301
|
+
*/
|
|
302
|
+
private handleErrorResponse(status: number, data: unknown): {{pascalCase serviceName}}Error {
|
|
303
|
+
const errorData = data as { error?: { message?: string; code?: string } } | null;
|
|
304
|
+
const message = errorData?.error?.message ?? `Request failed with status ${status}`;
|
|
305
|
+
|
|
306
|
+
switch (status) {
|
|
307
|
+
case 401:
|
|
308
|
+
return {{pascalCase serviceName}}Errors.authenticationError(message);
|
|
309
|
+
case 403:
|
|
310
|
+
return {{pascalCase serviceName}}Errors.authorizationError(message);
|
|
311
|
+
default:
|
|
312
|
+
return {{pascalCase serviceName}}Errors.apiError(status, message, errorData ?? undefined);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Update rate limit from response headers.
|
|
318
|
+
*/
|
|
319
|
+
private updateRateLimit(headers: Headers): void {
|
|
320
|
+
const limit = headers.get('x-ratelimit-limit');
|
|
321
|
+
const remaining = headers.get('x-ratelimit-remaining');
|
|
322
|
+
const reset = headers.get('x-ratelimit-reset');
|
|
323
|
+
|
|
324
|
+
if (limit && remaining && reset) {
|
|
325
|
+
const parsedLimit = parseInt(limit, 10);
|
|
326
|
+
const parsedRemaining = parseInt(remaining, 10);
|
|
327
|
+
const parsedReset = parseInt(reset, 10);
|
|
328
|
+
|
|
329
|
+
if (isNaN(parsedLimit) || isNaN(parsedRemaining) || isNaN(parsedReset)) {
|
|
330
|
+
this.debug('Invalid rate limit headers received');
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
this.rateLimit = {
|
|
335
|
+
limit: parsedLimit,
|
|
336
|
+
remaining: parsedRemaining,
|
|
337
|
+
reset: parsedReset * 1000, // Convert to milliseconds
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Wait if rate limited.
|
|
344
|
+
*/
|
|
345
|
+
private async waitForRateLimit(): Promise<void> {
|
|
346
|
+
if (this.rateLimit && this.rateLimit.remaining <= 0) {
|
|
347
|
+
const waitTime = Math.max(0, this.rateLimit.reset - Date.now());
|
|
348
|
+
if (waitTime > 0) {
|
|
349
|
+
this.debug(`Rate limit reached, waiting ${waitTime}ms`);
|
|
350
|
+
await this.sleep(waitTime);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Get retry-after delay from headers.
|
|
357
|
+
*/
|
|
358
|
+
private getRetryAfter(headers: Headers): number {
|
|
359
|
+
const retryAfter = headers.get('retry-after');
|
|
360
|
+
if (retryAfter) {
|
|
361
|
+
// Could be seconds or HTTP date
|
|
362
|
+
const seconds = parseInt(retryAfter, 10);
|
|
363
|
+
if (!isNaN(seconds)) {
|
|
364
|
+
return seconds * 1000;
|
|
365
|
+
}
|
|
366
|
+
const date = new Date(retryAfter);
|
|
367
|
+
if (!isNaN(date.getTime())) {
|
|
368
|
+
return Math.max(0, date.getTime() - Date.now());
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return DEFAULTS.retryBaseDelay;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Calculate exponential backoff delay.
|
|
376
|
+
*/
|
|
377
|
+
private calculateBackoff(attempt: number): number {
|
|
378
|
+
const delay = DEFAULTS.retryBaseDelay * Math.pow(2, attempt - 1);
|
|
379
|
+
// Add jitter (±25%)
|
|
380
|
+
const jitter = delay * 0.25 * (Math.random() * 2 - 1);
|
|
381
|
+
return Math.min(delay + jitter, DEFAULTS.retryMaxDelay);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Sleep for specified milliseconds.
|
|
386
|
+
*/
|
|
387
|
+
private sleep(ms: number): Promise<void> {
|
|
388
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Debug logging.
|
|
393
|
+
*/
|
|
394
|
+
private debug(message: string): void {
|
|
395
|
+
if (this.config.debug) {
|
|
396
|
+
console.debug(`[{{pascalCase serviceName}}Client] ${message}`);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
{{else}}
|
|
401
|
+
// This file is only generated for API integrations (isApiIntegration: true)
|
|
402
|
+
export {};
|
|
403
|
+
{{/if}}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error definitions for {{pascalCase serviceName}} service.
|
|
3
|
+
* @module @aios/{{kebabCase serviceName}}/errors
|
|
4
|
+
* @story {{storyId}}
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Error codes for {{pascalCase serviceName}} service.
|
|
9
|
+
*/
|
|
10
|
+
export enum {{pascalCase serviceName}}ErrorCode {
|
|
11
|
+
/** Configuration is missing or invalid */
|
|
12
|
+
CONFIGURATION_ERROR = 'CONFIGURATION_ERROR',
|
|
13
|
+
|
|
14
|
+
/** Network or connectivity issue */
|
|
15
|
+
NETWORK_ERROR = 'NETWORK_ERROR',
|
|
16
|
+
|
|
17
|
+
/** Operation timed out */
|
|
18
|
+
TIMEOUT_ERROR = 'TIMEOUT_ERROR',
|
|
19
|
+
|
|
20
|
+
/** Feature not yet implemented */
|
|
21
|
+
NOT_IMPLEMENTED = 'NOT_IMPLEMENTED',
|
|
22
|
+
|
|
23
|
+
/** Unknown or unexpected error */
|
|
24
|
+
UNKNOWN_ERROR = 'UNKNOWN_ERROR',
|
|
25
|
+
|
|
26
|
+
{{#if isApiIntegration}}
|
|
27
|
+
/** API rate limit exceeded */
|
|
28
|
+
RATE_LIMIT_EXCEEDED = 'RATE_LIMIT_EXCEEDED',
|
|
29
|
+
|
|
30
|
+
/** API authentication failed */
|
|
31
|
+
AUTHENTICATION_ERROR = 'AUTHENTICATION_ERROR',
|
|
32
|
+
|
|
33
|
+
/** API authorization failed */
|
|
34
|
+
AUTHORIZATION_ERROR = 'AUTHORIZATION_ERROR',
|
|
35
|
+
|
|
36
|
+
/** API returned an error response */
|
|
37
|
+
API_ERROR = 'API_ERROR',
|
|
38
|
+
|
|
39
|
+
/** Invalid API response format */
|
|
40
|
+
INVALID_RESPONSE = 'INVALID_RESPONSE',
|
|
41
|
+
{{/if}}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Custom error class for {{pascalCase serviceName}} service.
|
|
46
|
+
*/
|
|
47
|
+
export class {{pascalCase serviceName}}Error extends Error {
|
|
48
|
+
/**
|
|
49
|
+
* Error code for programmatic handling.
|
|
50
|
+
*/
|
|
51
|
+
public readonly code: {{pascalCase serviceName}}ErrorCode;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Additional error details.
|
|
55
|
+
*/
|
|
56
|
+
public readonly details?: Record<string, unknown>;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Original error that caused this error.
|
|
60
|
+
*/
|
|
61
|
+
public readonly cause?: Error;
|
|
62
|
+
|
|
63
|
+
{{#if isApiIntegration}}
|
|
64
|
+
/**
|
|
65
|
+
* HTTP status code (for API errors).
|
|
66
|
+
*/
|
|
67
|
+
public readonly statusCode?: number;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Rate limit information (for rate limit errors).
|
|
71
|
+
*/
|
|
72
|
+
public readonly rateLimit?: {
|
|
73
|
+
remaining: number;
|
|
74
|
+
reset: number;
|
|
75
|
+
};
|
|
76
|
+
{{/if}}
|
|
77
|
+
|
|
78
|
+
constructor(
|
|
79
|
+
message: string,
|
|
80
|
+
code: {{pascalCase serviceName}}ErrorCode = {{pascalCase serviceName}}ErrorCode.UNKNOWN_ERROR,
|
|
81
|
+
options?: {
|
|
82
|
+
details?: Record<string, unknown>;
|
|
83
|
+
cause?: Error;
|
|
84
|
+
{{#if isApiIntegration}}
|
|
85
|
+
statusCode?: number;
|
|
86
|
+
rateLimit?: { remaining: number; reset: number };
|
|
87
|
+
{{/if}}
|
|
88
|
+
}
|
|
89
|
+
) {
|
|
90
|
+
super(message);
|
|
91
|
+
this.name = '{{pascalCase serviceName}}Error';
|
|
92
|
+
this.code = code;
|
|
93
|
+
this.details = options?.details;
|
|
94
|
+
this.cause = options?.cause;
|
|
95
|
+
{{#if isApiIntegration}}
|
|
96
|
+
this.statusCode = options?.statusCode;
|
|
97
|
+
this.rateLimit = options?.rateLimit;
|
|
98
|
+
{{/if}}
|
|
99
|
+
|
|
100
|
+
// Maintains proper stack trace for where error was thrown
|
|
101
|
+
if (Error.captureStackTrace) {
|
|
102
|
+
Error.captureStackTrace(this, {{pascalCase serviceName}}Error);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Returns a JSON representation of the error.
|
|
108
|
+
*/
|
|
109
|
+
toJSON(): Record<string, unknown> {
|
|
110
|
+
return {
|
|
111
|
+
name: this.name,
|
|
112
|
+
code: this.code,
|
|
113
|
+
message: this.message,
|
|
114
|
+
details: this.details,
|
|
115
|
+
cause: this.cause ? { name: this.cause.name, message: this.cause.message } : undefined,
|
|
116
|
+
{{#if isApiIntegration}}
|
|
117
|
+
statusCode: this.statusCode,
|
|
118
|
+
rateLimit: this.rateLimit,
|
|
119
|
+
{{/if}}
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Factory functions for creating typed errors.
|
|
126
|
+
*/
|
|
127
|
+
export const {{pascalCase serviceName}}Errors = {
|
|
128
|
+
/**
|
|
129
|
+
* Create a configuration error.
|
|
130
|
+
*/
|
|
131
|
+
configurationError(message: string, details?: Record<string, unknown>): {{pascalCase serviceName}}Error {
|
|
132
|
+
return new {{pascalCase serviceName}}Error(message, {{pascalCase serviceName}}ErrorCode.CONFIGURATION_ERROR, { details });
|
|
133
|
+
},
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Create a network error.
|
|
137
|
+
*/
|
|
138
|
+
networkError(message: string, cause?: Error): {{pascalCase serviceName}}Error {
|
|
139
|
+
return new {{pascalCase serviceName}}Error(message, {{pascalCase serviceName}}ErrorCode.NETWORK_ERROR, { cause });
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Create a timeout error.
|
|
144
|
+
*/
|
|
145
|
+
timeoutError(message: string, details?: Record<string, unknown>): {{pascalCase serviceName}}Error {
|
|
146
|
+
return new {{pascalCase serviceName}}Error(message, {{pascalCase serviceName}}ErrorCode.TIMEOUT_ERROR, { details });
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
{{#if isApiIntegration}}
|
|
150
|
+
/**
|
|
151
|
+
* Create a rate limit error.
|
|
152
|
+
*/
|
|
153
|
+
rateLimitError(retryAfter: number, rateLimit: { remaining: number; reset: number }): {{pascalCase serviceName}}Error {
|
|
154
|
+
return new {{pascalCase serviceName}}Error(
|
|
155
|
+
`Rate limit exceeded. Retry after ${retryAfter} seconds.`,
|
|
156
|
+
{{pascalCase serviceName}}ErrorCode.RATE_LIMIT_EXCEEDED,
|
|
157
|
+
{ statusCode: 429, rateLimit }
|
|
158
|
+
);
|
|
159
|
+
},
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Create an authentication error.
|
|
163
|
+
*/
|
|
164
|
+
authenticationError(message: string = 'Authentication failed'): {{pascalCase serviceName}}Error {
|
|
165
|
+
return new {{pascalCase serviceName}}Error(message, {{pascalCase serviceName}}ErrorCode.AUTHENTICATION_ERROR, { statusCode: 401 });
|
|
166
|
+
},
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Create an authorization error.
|
|
170
|
+
*/
|
|
171
|
+
authorizationError(message: string = 'Access denied'): {{pascalCase serviceName}}Error {
|
|
172
|
+
return new {{pascalCase serviceName}}Error(message, {{pascalCase serviceName}}ErrorCode.AUTHORIZATION_ERROR, { statusCode: 403 });
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Create an API error from response.
|
|
177
|
+
*/
|
|
178
|
+
apiError(statusCode: number, message: string, details?: Record<string, unknown>): {{pascalCase serviceName}}Error {
|
|
179
|
+
return new {{pascalCase serviceName}}Error(message, {{pascalCase serviceName}}ErrorCode.API_ERROR, { statusCode, details });
|
|
180
|
+
},
|
|
181
|
+
{{/if}}
|
|
182
|
+
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module @aios/{{kebabCase serviceName}}
|
|
3
|
+
* @description {{description}}
|
|
4
|
+
* @story {{storyId}}
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Re-export types
|
|
8
|
+
export * from './types';
|
|
9
|
+
|
|
10
|
+
// Re-export errors
|
|
11
|
+
export * from './errors';
|
|
12
|
+
|
|
13
|
+
{{#if isApiIntegration}}
|
|
14
|
+
// Re-export client (API integrations only)
|
|
15
|
+
export { {{pascalCase serviceName}}Client } from './client';
|
|
16
|
+
{{/if}}
|
|
17
|
+
|
|
18
|
+
import type { {{pascalCase serviceName}}Config, {{pascalCase serviceName}}Service } from './types';
|
|
19
|
+
import { {{pascalCase serviceName}}Error, {{pascalCase serviceName}}ErrorCode } from './errors';
|
|
20
|
+
{{#if isApiIntegration}}
|
|
21
|
+
import { {{pascalCase serviceName}}Client } from './client';
|
|
22
|
+
{{/if}}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new {{pascalCase serviceName}} service instance.
|
|
26
|
+
*
|
|
27
|
+
* @param config - Service configuration
|
|
28
|
+
* @returns Configured service instance
|
|
29
|
+
* @throws {{{pascalCase serviceName}}Error} If configuration is invalid
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const service = create{{pascalCase serviceName}}Service({
|
|
34
|
+
* // configuration options
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* const result = await service.execute();
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function create{{pascalCase serviceName}}Service(
|
|
41
|
+
config: {{pascalCase serviceName}}Config
|
|
42
|
+
): {{pascalCase serviceName}}Service {
|
|
43
|
+
// Validate configuration
|
|
44
|
+
validateConfig(config);
|
|
45
|
+
|
|
46
|
+
{{#if isApiIntegration}}
|
|
47
|
+
// Create API client
|
|
48
|
+
const client = new {{pascalCase serviceName}}Client(config);
|
|
49
|
+
|
|
50
|
+
{{/if}}
|
|
51
|
+
return {
|
|
52
|
+
/**
|
|
53
|
+
* Execute the primary service operation.
|
|
54
|
+
*/
|
|
55
|
+
async execute(): Promise<void> {
|
|
56
|
+
// TODO: Implement primary operation
|
|
57
|
+
throw new {{pascalCase serviceName}}Error(
|
|
58
|
+
'Not implemented',
|
|
59
|
+
{{pascalCase serviceName}}ErrorCode.NOT_IMPLEMENTED
|
|
60
|
+
);
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Get the current configuration (without sensitive values).
|
|
65
|
+
*/
|
|
66
|
+
getConfig(): Partial<{{pascalCase serviceName}}Config> {
|
|
67
|
+
return {
|
|
68
|
+
// Return non-sensitive config values
|
|
69
|
+
{{#each envVars}}
|
|
70
|
+
{{#unless this.sensitive}}
|
|
71
|
+
{{camelCase this.name}}: config.{{camelCase this.name}},
|
|
72
|
+
{{/unless}}
|
|
73
|
+
{{/each}}
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Check if the service is properly configured and operational.
|
|
79
|
+
*/
|
|
80
|
+
async healthCheck(): Promise<boolean> {
|
|
81
|
+
try {
|
|
82
|
+
{{#if isApiIntegration}}
|
|
83
|
+
// Verify API connectivity
|
|
84
|
+
await client.ping();
|
|
85
|
+
{{/if}}
|
|
86
|
+
return true;
|
|
87
|
+
} catch {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Validates the service configuration.
|
|
96
|
+
* @throws {{{pascalCase serviceName}}Error} If configuration is invalid
|
|
97
|
+
*/
|
|
98
|
+
function validateConfig(config: {{pascalCase serviceName}}Config): void {
|
|
99
|
+
if (!config) {
|
|
100
|
+
throw new {{pascalCase serviceName}}Error(
|
|
101
|
+
'Configuration is required',
|
|
102
|
+
{{pascalCase serviceName}}ErrorCode.CONFIGURATION_ERROR
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
{{#each envVars}}
|
|
107
|
+
{{#if this.required}}
|
|
108
|
+
if (config.{{camelCase this.name}} === undefined || config.{{camelCase this.name}} === null) {
|
|
109
|
+
throw new {{pascalCase serviceName}}Error(
|
|
110
|
+
'{{this.name}} is required',
|
|
111
|
+
{{pascalCase serviceName}}ErrorCode.CONFIGURATION_ERROR
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
{{/if}}
|
|
116
|
+
{{/each}}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Default export
|
|
120
|
+
export default create{{pascalCase serviceName}}Service;
|