@soleri/forge 5.14.0 → 5.14.2

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 (105) hide show
  1. package/dist/index.js +0 -0
  2. package/dist/lib.d.ts +2 -0
  3. package/dist/lib.js +2 -0
  4. package/dist/lib.js.map +1 -1
  5. package/dist/skills/brain-debrief.md +47 -19
  6. package/dist/skills/brainstorming.md +19 -9
  7. package/dist/skills/code-patrol.md +21 -19
  8. package/dist/skills/context-resume.md +14 -11
  9. package/dist/skills/executing-plans.md +30 -15
  10. package/dist/skills/fix-and-learn.md +17 -14
  11. package/dist/skills/health-check.md +29 -23
  12. package/dist/skills/knowledge-harvest.md +27 -20
  13. package/dist/skills/onboard-me.md +16 -15
  14. package/dist/skills/retrospective.md +34 -18
  15. package/dist/skills/second-opinion.md +16 -9
  16. package/dist/skills/systematic-debugging.md +40 -29
  17. package/dist/skills/test-driven-development.md +45 -30
  18. package/dist/skills/vault-capture.md +31 -15
  19. package/dist/skills/vault-navigator.md +24 -13
  20. package/dist/skills/verification-before-completion.md +38 -26
  21. package/dist/skills/writing-plans.md +21 -13
  22. package/dist/templates/claude-md-template.d.ts +9 -8
  23. package/dist/templates/claude-md-template.js +29 -11
  24. package/dist/templates/claude-md-template.js.map +1 -1
  25. package/dist/templates/inject-claude-md.js +65 -25
  26. package/dist/templates/inject-claude-md.js.map +1 -1
  27. package/dist/templates/shared-rules.d.ts +10 -6
  28. package/dist/templates/shared-rules.js +242 -199
  29. package/dist/templates/shared-rules.js.map +1 -1
  30. package/dist/templates/test-facades.js +6 -4
  31. package/dist/templates/test-facades.js.map +1 -1
  32. package/package.json +1 -1
  33. package/src/lib.ts +2 -0
  34. package/src/templates/claude-md-template.ts +30 -12
  35. package/src/templates/inject-claude-md.ts +65 -25
  36. package/src/templates/shared-rules.ts +259 -210
  37. package/src/templates/test-facades.ts +6 -4
  38. package/dist/skills/skills/brain-debrief.md +0 -214
  39. package/dist/skills/skills/brainstorming.md +0 -180
  40. package/dist/skills/skills/code-patrol.md +0 -178
  41. package/dist/skills/skills/context-resume.md +0 -146
  42. package/dist/skills/skills/executing-plans.md +0 -216
  43. package/dist/skills/skills/fix-and-learn.md +0 -167
  44. package/dist/skills/skills/health-check.md +0 -231
  45. package/dist/skills/skills/knowledge-harvest.md +0 -185
  46. package/dist/skills/skills/onboard-me.md +0 -198
  47. package/dist/skills/skills/retrospective.md +0 -205
  48. package/dist/skills/skills/second-opinion.md +0 -149
  49. package/dist/skills/skills/systematic-debugging.md +0 -241
  50. package/dist/skills/skills/test-driven-development.md +0 -281
  51. package/dist/skills/skills/vault-capture.md +0 -170
  52. package/dist/skills/skills/vault-navigator.md +0 -140
  53. package/dist/skills/skills/verification-before-completion.md +0 -182
  54. package/dist/skills/skills/writing-plans.md +0 -215
  55. package/dist/templates/brain.d.ts +0 -6
  56. package/dist/templates/brain.js +0 -478
  57. package/dist/templates/brain.js.map +0 -1
  58. package/dist/templates/core-facade.d.ts +0 -6
  59. package/dist/templates/core-facade.js +0 -564
  60. package/dist/templates/core-facade.js.map +0 -1
  61. package/dist/templates/facade-factory.d.ts +0 -1
  62. package/dist/templates/facade-factory.js +0 -63
  63. package/dist/templates/facade-factory.js.map +0 -1
  64. package/dist/templates/facade-types.d.ts +0 -1
  65. package/dist/templates/facade-types.js +0 -46
  66. package/dist/templates/facade-types.js.map +0 -1
  67. package/dist/templates/intelligence-loader.d.ts +0 -1
  68. package/dist/templates/intelligence-loader.js +0 -43
  69. package/dist/templates/intelligence-loader.js.map +0 -1
  70. package/dist/templates/intelligence-types.d.ts +0 -1
  71. package/dist/templates/intelligence-types.js +0 -24
  72. package/dist/templates/intelligence-types.js.map +0 -1
  73. package/dist/templates/llm-client.d.ts +0 -7
  74. package/dist/templates/llm-client.js +0 -300
  75. package/dist/templates/llm-client.js.map +0 -1
  76. package/dist/templates/llm-key-pool.d.ts +0 -7
  77. package/dist/templates/llm-key-pool.js +0 -211
  78. package/dist/templates/llm-key-pool.js.map +0 -1
  79. package/dist/templates/llm-types.d.ts +0 -5
  80. package/dist/templates/llm-types.js +0 -161
  81. package/dist/templates/llm-types.js.map +0 -1
  82. package/dist/templates/llm-utils.d.ts +0 -5
  83. package/dist/templates/llm-utils.js +0 -260
  84. package/dist/templates/llm-utils.js.map +0 -1
  85. package/dist/templates/planner.d.ts +0 -5
  86. package/dist/templates/planner.js +0 -150
  87. package/dist/templates/planner.js.map +0 -1
  88. package/dist/templates/test-brain.d.ts +0 -6
  89. package/dist/templates/test-brain.js +0 -474
  90. package/dist/templates/test-brain.js.map +0 -1
  91. package/dist/templates/test-llm.d.ts +0 -7
  92. package/dist/templates/test-llm.js +0 -574
  93. package/dist/templates/test-llm.js.map +0 -1
  94. package/dist/templates/test-loader.d.ts +0 -5
  95. package/dist/templates/test-loader.js +0 -146
  96. package/dist/templates/test-loader.js.map +0 -1
  97. package/dist/templates/test-planner.d.ts +0 -5
  98. package/dist/templates/test-planner.js +0 -271
  99. package/dist/templates/test-planner.js.map +0 -1
  100. package/dist/templates/test-vault.d.ts +0 -5
  101. package/dist/templates/test-vault.js +0 -380
  102. package/dist/templates/test-vault.js.map +0 -1
  103. package/dist/templates/vault.d.ts +0 -5
  104. package/dist/templates/vault.js +0 -263
  105. package/dist/templates/vault.js.map +0 -1
@@ -1 +0,0 @@
1
- {"version":3,"file":"facade-types.js","sourceRoot":"","sources":["../../src/templates/facade-types.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,mBAAmB;IACjC,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0CR,CAAC;AACF,CAAC"}
@@ -1 +0,0 @@
1
- export declare function generateIntelligenceLoader(): string;
@@ -1,43 +0,0 @@
1
- export function generateIntelligenceLoader() {
2
- return `import { readFileSync, readdirSync } from 'node:fs';
3
- import { join } from 'node:path';
4
- import type { IntelligenceBundle, IntelligenceEntry } from './types.js';
5
-
6
- export function loadIntelligenceData(dataDir: string): IntelligenceEntry[] {
7
- const entries: IntelligenceEntry[] = [];
8
- let files: string[];
9
- try {
10
- files = readdirSync(dataDir).filter((f) => f.endsWith('.json'));
11
- } catch {
12
- console.warn('Intelligence data directory not found: ' + dataDir);
13
- return entries;
14
- }
15
-
16
- for (const file of files) {
17
- try {
18
- const raw = readFileSync(join(dataDir, file), 'utf-8');
19
- const bundle = JSON.parse(raw) as IntelligenceBundle;
20
- if (!bundle.entries || !Array.isArray(bundle.entries)) continue;
21
- for (const entry of bundle.entries) {
22
- if (validateEntry(entry)) entries.push(entry);
23
- }
24
- } catch (err) {
25
- console.warn('Failed to load ' + file + ': ' + (err instanceof Error ? err.message : err));
26
- }
27
- }
28
- return entries;
29
- }
30
-
31
- function validateEntry(entry: IntelligenceEntry): boolean {
32
- return (
33
- typeof entry.id === 'string' && entry.id.length > 0 &&
34
- ['pattern', 'anti-pattern', 'rule'].includes(entry.type) &&
35
- typeof entry.title === 'string' && entry.title.length > 0 &&
36
- typeof entry.description === 'string' && entry.description.length > 0 &&
37
- ['critical', 'warning', 'suggestion'].includes(entry.severity) &&
38
- Array.isArray(entry.tags)
39
- );
40
- }
41
- `;
42
- }
43
- //# sourceMappingURL=intelligence-loader.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"intelligence-loader.js","sourceRoot":"","sources":["../../src/templates/intelligence-loader.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,0BAA0B;IACxC,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAuCR,CAAC;AACF,CAAC"}
@@ -1 +0,0 @@
1
- export declare function generateIntelligenceTypes(): string;
@@ -1,24 +0,0 @@
1
- export function generateIntelligenceTypes() {
2
- return `export interface IntelligenceEntry {
3
- id: string;
4
- type: 'pattern' | 'anti-pattern' | 'rule';
5
- domain: string;
6
- title: string;
7
- severity: 'critical' | 'warning' | 'suggestion';
8
- description: string;
9
- context?: string;
10
- example?: string;
11
- counterExample?: string;
12
- why?: string;
13
- tags: string[];
14
- appliesTo?: string[];
15
- }
16
-
17
- export interface IntelligenceBundle {
18
- domain: string;
19
- version: string;
20
- entries: IntelligenceEntry[];
21
- }
22
- `;
23
- }
24
- //# sourceMappingURL=intelligence-types.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"intelligence-types.js","sourceRoot":"","sources":["../../src/templates/intelligence-types.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,yBAAyB;IACvC,OAAO;;;;;;;;;;;;;;;;;;;;CAoBR,CAAC;AACF,CAAC"}
@@ -1,7 +0,0 @@
1
- import type { AgentConfig } from '../types.js';
2
- /**
3
- * Generate the LLM client file for a new agent.
4
- * Contains LLMClient (OpenAI fetch + Anthropic SDK) and ModelRouter (inlined).
5
- * Uses config.id to resolve ~/.{agentId}/model-routing.json.
6
- */
7
- export declare function generateLLMClient(config: AgentConfig): string;
@@ -1,300 +0,0 @@
1
- /**
2
- * Generate the LLM client file for a new agent.
3
- * Contains LLMClient (OpenAI fetch + Anthropic SDK) and ModelRouter (inlined).
4
- * Uses config.id to resolve ~/.{agentId}/model-routing.json.
5
- */
6
- export function generateLLMClient(config) {
7
- return `/**
8
- * LLM Client — Unified OpenAI/Anthropic caller with key pool rotation
9
- * Generated by Soleri — do not edit manually.
10
- */
11
-
12
- import Anthropic from '@anthropic-ai/sdk';
13
- import { SecretString, LLMError, CircuitBreaker, retry, parseRateLimitHeaders } from '@soleri/core';
14
- import type {
15
- LLMCallOptions,
16
- LLMCallResult,
17
- RouteEntry,
18
- RoutingConfig,
19
- KeyPool,
20
- } from '@soleri/core';
21
- import * as fs from 'node:fs';
22
- import * as path from 'node:path';
23
- import { homedir } from 'node:os';
24
-
25
- // =============================================================================
26
- // CONSTANTS
27
- // =============================================================================
28
-
29
- const OPENAI_API_URL = 'https://api.openai.com/v1/chat/completions';
30
-
31
- // =============================================================================
32
- // MODEL ROUTER (inlined)
33
- // =============================================================================
34
-
35
- function loadRoutingConfig(): RoutingConfig {
36
- const defaultConfig: RoutingConfig = {
37
- routes: [],
38
- defaultOpenAIModel: 'gpt-4o-mini',
39
- defaultAnthropicModel: 'claude-sonnet-4-20250514',
40
- };
41
-
42
- const configPath = path.join(
43
- homedir(),
44
- '.${config.id}',
45
- 'model-routing.json',
46
- );
47
-
48
- try {
49
- if (fs.existsSync(configPath)) {
50
- const data = JSON.parse(fs.readFileSync(configPath, 'utf-8')) as Partial<RoutingConfig>;
51
- if (data.routes && Array.isArray(data.routes)) {
52
- defaultConfig.routes = data.routes;
53
- }
54
- if (data.defaultOpenAIModel) {
55
- defaultConfig.defaultOpenAIModel = data.defaultOpenAIModel;
56
- }
57
- if (data.defaultAnthropicModel) {
58
- defaultConfig.defaultAnthropicModel = data.defaultAnthropicModel;
59
- }
60
- }
61
- } catch {
62
- // Config not available — use defaults
63
- }
64
-
65
- return defaultConfig;
66
- }
67
-
68
- function inferProvider(model: string): 'openai' | 'anthropic' {
69
- if (model.startsWith('claude-') || model.startsWith('anthropic/')) {
70
- return 'anthropic';
71
- }
72
- return 'openai';
73
- }
74
-
75
- class ModelRouter {
76
- private config: RoutingConfig;
77
-
78
- constructor(config?: RoutingConfig) {
79
- this.config = config || loadRoutingConfig();
80
- }
81
-
82
- resolve(
83
- caller: string,
84
- task?: string,
85
- originalModel?: string,
86
- ): { model: string; provider: 'openai' | 'anthropic' } {
87
- if (task) {
88
- const exactMatch = this.config.routes.find(
89
- (r) => r.caller === caller && r.task === task,
90
- );
91
- if (exactMatch) {
92
- return { model: exactMatch.model, provider: exactMatch.provider };
93
- }
94
- }
95
-
96
- const callerMatch = this.config.routes.find(
97
- (r) => r.caller === caller && !r.task,
98
- );
99
- if (callerMatch) {
100
- return { model: callerMatch.model, provider: callerMatch.provider };
101
- }
102
-
103
- if (originalModel) {
104
- const provider = inferProvider(originalModel);
105
- return { model: originalModel, provider };
106
- }
107
-
108
- return { model: this.config.defaultOpenAIModel, provider: 'openai' };
109
- }
110
-
111
- getRoutes(): RouteEntry[] {
112
- return [...this.config.routes];
113
- }
114
- }
115
-
116
- // =============================================================================
117
- // LLM CLIENT
118
- // =============================================================================
119
-
120
- export class LLMClient {
121
- private openaiKeyPool: KeyPool;
122
- private anthropicKeyPool: KeyPool;
123
- private anthropicClient: Anthropic | null = null;
124
- private anthropicBreaker: CircuitBreaker;
125
- private anthropicKeyFingerprint: string = '';
126
- private router: ModelRouter;
127
-
128
- constructor(openaiKeyPool: KeyPool, anthropicKeyPool: KeyPool) {
129
- this.openaiKeyPool = openaiKeyPool;
130
- this.anthropicKeyPool = anthropicKeyPool;
131
- this.anthropicBreaker = new CircuitBreaker({
132
- name: 'llm-anthropic',
133
- failureThreshold: 5,
134
- resetTimeoutMs: 60_000,
135
- });
136
- this.router = new ModelRouter();
137
- }
138
-
139
- async complete(options: LLMCallOptions): Promise<LLMCallResult> {
140
- const routed = this.router.resolve(
141
- options.caller,
142
- options.task,
143
- options.model,
144
- );
145
- const resolvedOptions = { ...options, model: routed.model, provider: routed.provider };
146
-
147
- return resolvedOptions.provider === 'anthropic'
148
- ? this.callAnthropic(resolvedOptions)
149
- : this.callOpenAI(resolvedOptions);
150
- }
151
-
152
- isAvailable(): { openai: boolean; anthropic: boolean } {
153
- return {
154
- openai: this.openaiKeyPool.hasKeys,
155
- anthropic: this.anthropicKeyPool.hasKeys,
156
- };
157
- }
158
-
159
- getRoutes(): RouteEntry[] {
160
- return this.router.getRoutes();
161
- }
162
-
163
- // ===========================================================================
164
- // OPENAI
165
- // ===========================================================================
166
-
167
- private async callOpenAI(options: LLMCallOptions): Promise<LLMCallResult> {
168
- const keyPool = this.openaiKeyPool.hasKeys ? this.openaiKeyPool : null;
169
-
170
- if (!keyPool) {
171
- throw new LLMError('OpenAI API key not configured', { retryable: false });
172
- }
173
-
174
- const start = Date.now();
175
-
176
- const doRequest = async (): Promise<LLMCallResult> => {
177
- const apiKey = keyPool.getActiveKey().expose();
178
- const keyIndex = keyPool.activeKeyIndex;
179
-
180
- const response = await fetch(OPENAI_API_URL, {
181
- method: 'POST',
182
- headers: {
183
- 'Content-Type': 'application/json',
184
- Authorization: \`Bearer \${apiKey}\`,
185
- },
186
- body: JSON.stringify({
187
- model: options.model,
188
- messages: [
189
- { role: 'system', content: options.systemPrompt },
190
- { role: 'user', content: options.userPrompt },
191
- ],
192
- temperature: options.temperature ?? 0.3,
193
- max_completion_tokens: options.maxTokens ?? 500,
194
- }),
195
- });
196
-
197
- if (response.headers) {
198
- const rateLimits = parseRateLimitHeaders(response.headers);
199
- if (rateLimits.remaining !== null) {
200
- keyPool.updateQuota(keyIndex, rateLimits.remaining);
201
- keyPool.rotatePreemptive();
202
- }
203
- }
204
-
205
- if (!response.ok) {
206
- if (response.status === 429 && keyPool.poolSize > 1) {
207
- keyPool.rotateOnError();
208
- }
209
-
210
- const errorBody = await response.text();
211
- throw new LLMError(
212
- \`OpenAI API error: \${response.status} - \${errorBody}\`,
213
- { retryable: response.status === 429 || response.status >= 500, statusCode: response.status },
214
- );
215
- }
216
-
217
- const data = (await response.json()) as {
218
- choices: Array<{ message: { content: string } }>;
219
- usage?: { prompt_tokens?: number; completion_tokens?: number };
220
- };
221
-
222
- return {
223
- text: data.choices[0]?.message?.content || '',
224
- model: options.model,
225
- provider: 'openai' as const,
226
- inputTokens: data.usage?.prompt_tokens,
227
- outputTokens: data.usage?.completion_tokens,
228
- durationMs: Date.now() - start,
229
- };
230
- };
231
-
232
- return retry(doRequest, { maxAttempts: 3 });
233
- }
234
-
235
- // ===========================================================================
236
- // ANTHROPIC
237
- // ===========================================================================
238
-
239
- private async callAnthropic(options: LLMCallOptions): Promise<LLMCallResult> {
240
- const client = this.getAnthropicClient();
241
- if (!client) {
242
- throw new LLMError('Anthropic API key not configured', { retryable: false });
243
- }
244
-
245
- const start = Date.now();
246
-
247
- return this.anthropicBreaker.call(() =>
248
- retry(
249
- async () => {
250
- const response = await client.messages.create(
251
- {
252
- model: options.model,
253
- max_tokens: options.maxTokens ?? 1024,
254
- system: options.systemPrompt,
255
- messages: [{ role: 'user', content: options.userPrompt }],
256
- },
257
- { timeout: 60_000 },
258
- );
259
-
260
- const text = response.content
261
- .filter(
262
- (block): block is Anthropic.TextBlock => block.type === 'text',
263
- )
264
- .map((block) => block.text)
265
- .join('\\n');
266
-
267
- return {
268
- text,
269
- model: options.model,
270
- provider: 'anthropic' as const,
271
- inputTokens: response.usage?.input_tokens,
272
- outputTokens: response.usage?.output_tokens,
273
- durationMs: Date.now() - start,
274
- };
275
- },
276
- { maxAttempts: 2 },
277
- ),
278
- );
279
- }
280
-
281
- private getAnthropicClient(): Anthropic | null {
282
- if (!this.anthropicKeyPool.hasKeys) return null;
283
-
284
- const currentKey = this.anthropicKeyPool.getActiveKey().expose();
285
- const currentFingerprint = currentKey.slice(-8);
286
-
287
- if (currentFingerprint !== this.anthropicKeyFingerprint) {
288
- this.anthropicClient = null;
289
- this.anthropicKeyFingerprint = currentFingerprint;
290
- }
291
-
292
- if (this.anthropicClient) return this.anthropicClient;
293
-
294
- this.anthropicClient = new Anthropic({ apiKey: currentKey });
295
- return this.anthropicClient;
296
- }
297
- }
298
- `;
299
- }
300
- //# sourceMappingURL=llm-client.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"llm-client.js","sourceRoot":"","sources":["../../src/templates/llm-client.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAmB;IACnD,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;QAqCD,MAAM,CAAC,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA8PhB,CAAC;AACF,CAAC"}
@@ -1,7 +0,0 @@
1
- import type { AgentConfig } from '../types.js';
2
- /**
3
- * Generate the LLM key pool file for a new agent.
4
- * KeyPool manages multiple API keys with per-key circuit breakers.
5
- * Uses config.id to resolve ~/.{agentId}/keys.json.
6
- */
7
- export declare function generateLLMKeyPool(config: AgentConfig): string;
@@ -1,211 +0,0 @@
1
- /**
2
- * Generate the LLM key pool file for a new agent.
3
- * KeyPool manages multiple API keys with per-key circuit breakers.
4
- * Uses config.id to resolve ~/.{agentId}/keys.json.
5
- */
6
- export function generateLLMKeyPool(config) {
7
- return `/**
8
- * Key Pool — Multi-key management with auto-rotation
9
- * Generated by Soleri — do not edit manually.
10
- *
11
- * Key loading priority:
12
- * 1. ~/.${config.id}/keys.json → { "openai": ["sk-..."], "anthropic": ["sk-ant-..."] }
13
- * 2. Fallback: OPENAI_API_KEY / ANTHROPIC_API_KEY env vars
14
- * 3. Empty pool → graceful degradation
15
- */
16
-
17
- import * as fs from 'node:fs';
18
- import * as path from 'node:path';
19
- import { homedir } from 'node:os';
20
- import { SecretString } from './types.js';
21
- import type { KeyPoolConfig, KeyStatus } from './types.js';
22
- import { CircuitBreaker } from './utils.js';
23
-
24
- // =============================================================================
25
- // CONSTANTS
26
- // =============================================================================
27
-
28
- const DEFAULT_PREEMPTIVE_THRESHOLD = 50;
29
-
30
- // =============================================================================
31
- // KEY POOL
32
- // =============================================================================
33
-
34
- class RateLimitRotationError extends Error {
35
- retryable = true;
36
- constructor() {
37
- super('Rate limit rotation');
38
- this.name = 'RateLimitRotationError';
39
- }
40
- }
41
-
42
- export class KeyPool {
43
- private keys: SecretString[];
44
- private activeIndex: number = 0;
45
- private keyBreakers: Map<number, CircuitBreaker> = new Map();
46
- private remainingQuota: Map<number, number> = new Map();
47
- private readonly preemptiveThreshold: number;
48
-
49
- constructor(config: KeyPoolConfig) {
50
- this.keys = config.keys
51
- .filter((k) => k.length > 0)
52
- .map((k) => new SecretString(k));
53
- this.preemptiveThreshold =
54
- config.preemptiveThreshold ?? DEFAULT_PREEMPTIVE_THRESHOLD;
55
-
56
- for (let i = 0; i < this.keys.length; i++) {
57
- this.keyBreakers.set(
58
- i,
59
- new CircuitBreaker({
60
- name: \`key-pool-\${i}\`,
61
- failureThreshold: 3,
62
- resetTimeoutMs: 30_000,
63
- }),
64
- );
65
- }
66
-
67
- if (this.keys.length > 1) {
68
- console.error(\`[LLM] KeyPool initialized with \${this.keys.length} keys\`);
69
- }
70
- }
71
-
72
- get hasKeys(): boolean {
73
- return this.keys.length > 0;
74
- }
75
-
76
- getActiveKey(): SecretString {
77
- return this.keys[this.activeIndex];
78
- }
79
-
80
- get activeKeyIndex(): number {
81
- return this.activeIndex;
82
- }
83
-
84
- get poolSize(): number {
85
- return this.keys.length;
86
- }
87
-
88
- get exhausted(): boolean {
89
- if (this.keys.length === 0) return true;
90
- for (let i = 0; i < this.keys.length; i++) {
91
- const breaker = this.keyBreakers.get(i)!;
92
- if (!breaker.isOpen()) return false;
93
- }
94
- return true;
95
- }
96
-
97
- rotateOnError(): SecretString | null {
98
- const breaker = this.keyBreakers.get(this.activeIndex);
99
- if (breaker) {
100
- breaker.recordFailure();
101
- }
102
- return this.findNextHealthyKey();
103
- }
104
-
105
- rotatePreemptive(): boolean {
106
- const previousIndex = this.activeIndex;
107
- const remaining = this.remainingQuota.get(previousIndex);
108
- if (remaining !== undefined && remaining < this.preemptiveThreshold) {
109
- const next = this.findNextHealthyKey();
110
- if (next) {
111
- console.error(
112
- \`[LLM] KeyPool preemptive rotation: key \${previousIndex} remaining=\${remaining} < threshold=\${this.preemptiveThreshold}\`,
113
- );
114
- return true;
115
- }
116
- }
117
- return false;
118
- }
119
-
120
- updateQuota(keyIndex: number, remaining: number): void {
121
- this.remainingQuota.set(keyIndex, remaining);
122
- }
123
-
124
- getStatus(): {
125
- poolSize: number;
126
- activeKeyIndex: number;
127
- exhausted: boolean;
128
- perKeyStatus: KeyStatus[];
129
- } {
130
- const perKeyStatus: KeyStatus[] = [];
131
- for (let i = 0; i < this.keys.length; i++) {
132
- perKeyStatus.push({
133
- index: i,
134
- circuitState: this.keyBreakers.get(i)!.getState(),
135
- remainingQuota: this.remainingQuota.get(i) ?? null,
136
- });
137
- }
138
- return {
139
- poolSize: this.poolSize,
140
- activeKeyIndex: this.activeIndex,
141
- exhausted: this.exhausted,
142
- perKeyStatus,
143
- };
144
- }
145
-
146
- private findNextHealthyKey(): SecretString | null {
147
- const startIndex = this.activeIndex;
148
- for (let offset = 1; offset <= this.keys.length; offset++) {
149
- const candidateIndex = (startIndex + offset) % this.keys.length;
150
- const breaker = this.keyBreakers.get(candidateIndex)!;
151
- if (!breaker.isOpen()) {
152
- this.activeIndex = candidateIndex;
153
- return this.keys[candidateIndex];
154
- }
155
- }
156
- console.error('[LLM] KeyPool: all keys exhausted — no healthy key available');
157
- return null;
158
- }
159
- }
160
-
161
- // =============================================================================
162
- // KEY LOADING
163
- // =============================================================================
164
-
165
- export interface KeyPoolFiles {
166
- openai: KeyPoolConfig;
167
- anthropic: KeyPoolConfig;
168
- }
169
-
170
- export function loadKeyPoolConfig(): KeyPoolFiles {
171
- const keysFilePath = path.join(
172
- homedir(),
173
- '.${config.id}',
174
- 'keys.json',
175
- );
176
-
177
- let openaiKeys: string[] = [];
178
- let anthropicKeys: string[] = [];
179
-
180
- try {
181
- if (fs.existsSync(keysFilePath)) {
182
- const data = JSON.parse(fs.readFileSync(keysFilePath, 'utf-8'));
183
- if (data?.openai && Array.isArray(data.openai)) {
184
- openaiKeys = data.openai;
185
- }
186
- if (data?.anthropic && Array.isArray(data.anthropic)) {
187
- anthropicKeys = data.anthropic;
188
- }
189
- }
190
- } catch {
191
- console.error('[LLM] Could not read keys.json, falling back to env vars');
192
- }
193
-
194
- // Fallback: environment variables
195
- if (openaiKeys.length === 0) {
196
- const envKey = process.env.OPENAI_API_KEY || '';
197
- if (envKey) openaiKeys = [envKey];
198
- }
199
- if (anthropicKeys.length === 0) {
200
- const envKey = process.env.ANTHROPIC_API_KEY || '';
201
- if (envKey) anthropicKeys = [envKey];
202
- }
203
-
204
- return {
205
- openai: { keys: openaiKeys },
206
- anthropic: { keys: anthropicKeys },
207
- };
208
- }
209
- `;
210
- }
211
- //# sourceMappingURL=llm-key-pool.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"llm-key-pool.js","sourceRoot":"","sources":["../../src/templates/llm-key-pool.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAmB;IACpD,OAAO;;;;;WAKE,MAAM,CAAC,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;QAiKZ,MAAM,CAAC,EAAE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoChB,CAAC;AACF,CAAC"}
@@ -1,5 +0,0 @@
1
- /**
2
- * Generate the LLM types file for a new agent.
3
- * Contains all types, LLMError class, and SecretString class.
4
- */
5
- export declare function generateLLMTypes(): string;