dartmind 17.0.0 → 17.3.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/dist/cli/InteractiveModeV12.d.ts +4 -0
- package/dist/cli/InteractiveModeV12.d.ts.map +1 -1
- package/dist/cli/InteractiveModeV12.js +57 -1
- package/dist/cli/InteractiveModeV12.js.map +1 -1
- package/dist/cli/commands/SlashCommands.d.ts.map +1 -1
- package/dist/cli/commands/SlashCommands.js +21 -5
- package/dist/cli/commands/SlashCommands.js.map +1 -1
- package/dist/core/cost/__tests__/day1.test.d.ts +9 -0
- package/dist/core/cost/__tests__/day1.test.d.ts.map +1 -0
- package/dist/core/cost/__tests__/day1.test.js +284 -0
- package/dist/core/cost/__tests__/day1.test.js.map +1 -0
- package/dist/core/cost/__tests__/day2.test.d.ts +9 -0
- package/dist/core/cost/__tests__/day2.test.d.ts.map +1 -0
- package/dist/core/cost/__tests__/day2.test.js +414 -0
- package/dist/core/cost/__tests__/day2.test.js.map +1 -0
- package/dist/core/cost/__tests__/day3.test.d.ts +9 -0
- package/dist/core/cost/__tests__/day3.test.d.ts.map +1 -0
- package/dist/core/cost/__tests__/day3.test.js +415 -0
- package/dist/core/cost/__tests__/day3.test.js.map +1 -0
- package/dist/core/cost/__tests__/day4.test.d.ts +9 -0
- package/dist/core/cost/__tests__/day4.test.d.ts.map +1 -0
- package/dist/core/cost/__tests__/day4.test.js +354 -0
- package/dist/core/cost/__tests__/day4.test.js.map +1 -0
- package/dist/core/cost/context-scaler.d.ts +129 -0
- package/dist/core/cost/context-scaler.d.ts.map +1 -0
- package/dist/core/cost/context-scaler.js +336 -0
- package/dist/core/cost/context-scaler.js.map +1 -0
- package/dist/core/cost/index.d.ts +31 -0
- package/dist/core/cost/index.d.ts.map +1 -0
- package/dist/core/cost/index.js +103 -0
- package/dist/core/cost/index.js.map +1 -0
- package/dist/core/cost/integration.d.ts +144 -0
- package/dist/core/cost/integration.d.ts.map +1 -0
- package/dist/core/cost/integration.js +349 -0
- package/dist/core/cost/integration.js.map +1 -0
- package/dist/core/cost/model-router.d.ts +70 -0
- package/dist/core/cost/model-router.d.ts.map +1 -0
- package/dist/core/cost/model-router.js +319 -0
- package/dist/core/cost/model-router.js.map +1 -0
- package/dist/core/cost/prompt-cache.d.ts +98 -0
- package/dist/core/cost/prompt-cache.d.ts.map +1 -0
- package/dist/core/cost/prompt-cache.js +219 -0
- package/dist/core/cost/prompt-cache.js.map +1 -0
- package/dist/core/cost/reflex-layer.d.ts +48 -0
- package/dist/core/cost/reflex-layer.d.ts.map +1 -0
- package/dist/core/cost/reflex-layer.js +295 -0
- package/dist/core/cost/reflex-layer.js.map +1 -0
- package/dist/core/cost/response-cache.d.ts +139 -0
- package/dist/core/cost/response-cache.d.ts.map +1 -0
- package/dist/core/cost/response-cache.js +290 -0
- package/dist/core/cost/response-cache.js.map +1 -0
- package/dist/core/cost/tool-loader.d.ts +94 -0
- package/dist/core/cost/tool-loader.d.ts.map +1 -0
- package/dist/core/cost/tool-loader.js +233 -0
- package/dist/core/cost/tool-loader.js.map +1 -0
- package/dist/core/cost/types.d.ts +93 -0
- package/dist/core/cost/types.d.ts.map +1 -0
- package/dist/core/cost/types.js +29 -0
- package/dist/core/cost/types.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DartMind V17.3 - Response Caching
|
|
3
|
+
*
|
|
4
|
+
* PURPOSE: Cache repeated queries locally to avoid redundant API calls.
|
|
5
|
+
* COST IMPACT: 5-10% savings on repeated queries within session
|
|
6
|
+
* QUALITY IMPACT: NONE (identical responses for identical queries)
|
|
7
|
+
*
|
|
8
|
+
* CACHING RULES:
|
|
9
|
+
* - Only cache for specific intents (explain, search, list, simple_qa)
|
|
10
|
+
* - TTL: 5 minutes (matches Anthropic's prompt cache)
|
|
11
|
+
* - Max entries: 100 (LRU eviction)
|
|
12
|
+
* - Only exact query matches (no fuzzy matching)
|
|
13
|
+
*
|
|
14
|
+
* SAFETY:
|
|
15
|
+
* - Code generation NEVER cached (context-dependent)
|
|
16
|
+
* - Bug fixes NEVER cached (need latest context)
|
|
17
|
+
* - Refactoring NEVER cached (code may have changed)
|
|
18
|
+
* - Cache cleared on file modifications
|
|
19
|
+
*
|
|
20
|
+
* @module cost/response-cache
|
|
21
|
+
* @version 17.3.0
|
|
22
|
+
*/
|
|
23
|
+
export const DEFAULT_RESPONSE_CACHE_CONFIG = {
|
|
24
|
+
enabled: true,
|
|
25
|
+
maxEntries: 100,
|
|
26
|
+
ttlMs: 5 * 60 * 1000, // 5 minutes
|
|
27
|
+
cacheableIntents: [
|
|
28
|
+
'explain',
|
|
29
|
+
'read',
|
|
30
|
+
'search',
|
|
31
|
+
'list',
|
|
32
|
+
'simple_qa',
|
|
33
|
+
],
|
|
34
|
+
};
|
|
35
|
+
// Intents that should NEVER be cached
|
|
36
|
+
export const UNCACHEABLE_INTENTS = [
|
|
37
|
+
'generate',
|
|
38
|
+
'refactor',
|
|
39
|
+
'fix_bug',
|
|
40
|
+
'write_tests',
|
|
41
|
+
'multi_file',
|
|
42
|
+
'architecture',
|
|
43
|
+
'security',
|
|
44
|
+
'complex',
|
|
45
|
+
'unknown',
|
|
46
|
+
];
|
|
47
|
+
// ═══════════════════════════════════════════════════════════════
|
|
48
|
+
// RESPONSE CACHE CLASS
|
|
49
|
+
// ═══════════════════════════════════════════════════════════════
|
|
50
|
+
export class ResponseCache {
|
|
51
|
+
cache = new Map();
|
|
52
|
+
config;
|
|
53
|
+
stats = {
|
|
54
|
+
hits: 0,
|
|
55
|
+
misses: 0,
|
|
56
|
+
evictions: 0,
|
|
57
|
+
totalSaved: 0, // Estimated tokens saved
|
|
58
|
+
};
|
|
59
|
+
constructor(config = {}) {
|
|
60
|
+
this.config = { ...DEFAULT_RESPONSE_CACHE_CONFIG, ...config };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get cached response for a query.
|
|
64
|
+
*
|
|
65
|
+
* @param query - User query
|
|
66
|
+
* @param intent - Task intent (for validation)
|
|
67
|
+
* @returns Cached response or undefined
|
|
68
|
+
*/
|
|
69
|
+
get(query, intent) {
|
|
70
|
+
if (!this.config.enabled) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
// Check if intent is cacheable
|
|
74
|
+
if (!this.isCacheableIntent(intent)) {
|
|
75
|
+
this.stats.misses++;
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
const key = this.getKey(query);
|
|
79
|
+
const entry = this.cache.get(key);
|
|
80
|
+
if (!entry) {
|
|
81
|
+
this.stats.misses++;
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
// Check TTL
|
|
85
|
+
if (Date.now() - entry.timestamp > this.config.ttlMs) {
|
|
86
|
+
this.cache.delete(key);
|
|
87
|
+
this.stats.misses++;
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
// Cache hit
|
|
91
|
+
entry.hitCount++;
|
|
92
|
+
this.stats.hits++;
|
|
93
|
+
this.stats.totalSaved += this.estimateTokens(entry.response);
|
|
94
|
+
return entry.response;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Store response in cache.
|
|
98
|
+
*
|
|
99
|
+
* @param query - User query
|
|
100
|
+
* @param response - API response
|
|
101
|
+
* @param intent - Task intent
|
|
102
|
+
*/
|
|
103
|
+
set(query, response, intent) {
|
|
104
|
+
if (!this.config.enabled) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
// Don't cache uncacheable intents
|
|
108
|
+
if (!this.isCacheableIntent(intent)) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const key = this.getKey(query);
|
|
112
|
+
// Evict if at capacity
|
|
113
|
+
if (this.cache.size >= this.config.maxEntries) {
|
|
114
|
+
this.evictLRU();
|
|
115
|
+
}
|
|
116
|
+
this.cache.set(key, {
|
|
117
|
+
query,
|
|
118
|
+
response,
|
|
119
|
+
intent,
|
|
120
|
+
timestamp: Date.now(),
|
|
121
|
+
hitCount: 0,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Check if an intent is cacheable.
|
|
126
|
+
*/
|
|
127
|
+
isCacheableIntent(intent) {
|
|
128
|
+
return this.config.cacheableIntents.includes(intent) &&
|
|
129
|
+
!UNCACHEABLE_INTENTS.includes(intent);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Clear entire cache.
|
|
133
|
+
*/
|
|
134
|
+
clear() {
|
|
135
|
+
this.cache.clear();
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Clear cache entries related to file modifications.
|
|
139
|
+
* Call this when files are modified to prevent stale cache hits.
|
|
140
|
+
*
|
|
141
|
+
* @param filePath - Path of modified file
|
|
142
|
+
*/
|
|
143
|
+
invalidateForFile(filePath) {
|
|
144
|
+
const keysToDelete = [];
|
|
145
|
+
for (const [key, entry] of this.cache) {
|
|
146
|
+
// Invalidate if query mentions the file
|
|
147
|
+
if (entry.query.includes(filePath) ||
|
|
148
|
+
entry.response.includes(filePath)) {
|
|
149
|
+
keysToDelete.push(key);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
keysToDelete.forEach(key => {
|
|
153
|
+
this.cache.delete(key);
|
|
154
|
+
this.stats.evictions++;
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Get cache statistics.
|
|
159
|
+
*/
|
|
160
|
+
getStats() {
|
|
161
|
+
const total = this.stats.hits + this.stats.misses;
|
|
162
|
+
return {
|
|
163
|
+
hits: this.stats.hits,
|
|
164
|
+
misses: this.stats.misses,
|
|
165
|
+
hitRate: total > 0 ? (this.stats.hits / total) * 100 : 0,
|
|
166
|
+
entries: this.cache.size,
|
|
167
|
+
evictions: this.stats.evictions,
|
|
168
|
+
totalSaved: this.stats.totalSaved,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Format cache stats for logging.
|
|
173
|
+
*/
|
|
174
|
+
formatStats() {
|
|
175
|
+
const stats = this.getStats();
|
|
176
|
+
return [
|
|
177
|
+
`Cache: ${stats.hits} hits, ${stats.misses} misses`,
|
|
178
|
+
`${stats.hitRate.toFixed(1)}% hit rate`,
|
|
179
|
+
`${stats.entries}/${this.config.maxEntries} entries`,
|
|
180
|
+
`~${stats.totalSaved.toLocaleString()} tokens saved`,
|
|
181
|
+
].join(' | ');
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Reset statistics.
|
|
185
|
+
*/
|
|
186
|
+
resetStats() {
|
|
187
|
+
this.stats = { hits: 0, misses: 0, evictions: 0, totalSaved: 0 };
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Update configuration.
|
|
191
|
+
*/
|
|
192
|
+
updateConfig(config) {
|
|
193
|
+
this.config = { ...this.config, ...config };
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Get current configuration.
|
|
197
|
+
*/
|
|
198
|
+
getConfig() {
|
|
199
|
+
return { ...this.config };
|
|
200
|
+
}
|
|
201
|
+
// ─────────────────────────────────────────────────────────────
|
|
202
|
+
// PRIVATE METHODS
|
|
203
|
+
// ─────────────────────────────────────────────────────────────
|
|
204
|
+
getKey(query) {
|
|
205
|
+
// Normalize query for key generation
|
|
206
|
+
return query.trim().toLowerCase();
|
|
207
|
+
}
|
|
208
|
+
evictLRU() {
|
|
209
|
+
// Find entry with oldest timestamp and lowest hit count
|
|
210
|
+
let oldest = null;
|
|
211
|
+
let oldestTime = Infinity;
|
|
212
|
+
let lowestHits = Infinity;
|
|
213
|
+
for (const [key, entry] of this.cache) {
|
|
214
|
+
// Prioritize removing entries with fewer hits
|
|
215
|
+
if (entry.hitCount < lowestHits ||
|
|
216
|
+
(entry.hitCount === lowestHits && entry.timestamp < oldestTime)) {
|
|
217
|
+
oldest = key;
|
|
218
|
+
oldestTime = entry.timestamp;
|
|
219
|
+
lowestHits = entry.hitCount;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if (oldest) {
|
|
223
|
+
this.cache.delete(oldest);
|
|
224
|
+
this.stats.evictions++;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
estimateTokens(text) {
|
|
228
|
+
return Math.ceil(text.length / 4);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
// ═══════════════════════════════════════════════════════════════
|
|
232
|
+
// SINGLETON INSTANCE
|
|
233
|
+
// ═══════════════════════════════════════════════════════════════
|
|
234
|
+
/**
|
|
235
|
+
* Default response cache instance.
|
|
236
|
+
*/
|
|
237
|
+
export const responseCache = new ResponseCache();
|
|
238
|
+
// ═══════════════════════════════════════════════════════════════
|
|
239
|
+
// CONVENIENCE FUNCTIONS
|
|
240
|
+
// ═══════════════════════════════════════════════════════════════
|
|
241
|
+
/**
|
|
242
|
+
* Quick check if a response is cached.
|
|
243
|
+
*/
|
|
244
|
+
export function getCachedResponse(query, intent) {
|
|
245
|
+
return responseCache.get(query, intent);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Quick cache a response.
|
|
249
|
+
*/
|
|
250
|
+
export function cacheResponse(query, response, intent) {
|
|
251
|
+
responseCache.set(query, response, intent);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Check if an intent allows caching.
|
|
255
|
+
*/
|
|
256
|
+
export function isIntentCacheable(intent) {
|
|
257
|
+
return responseCache.isCacheableIntent(intent);
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Invalidate cache when a file is modified.
|
|
261
|
+
*/
|
|
262
|
+
export function invalidateCacheForFile(filePath) {
|
|
263
|
+
responseCache.invalidateForFile(filePath);
|
|
264
|
+
}
|
|
265
|
+
// ═══════════════════════════════════════════════════════════════
|
|
266
|
+
// CACHE OVERRIDE DETECTION
|
|
267
|
+
// ═══════════════════════════════════════════════════════════════
|
|
268
|
+
/**
|
|
269
|
+
* Check if user requested to bypass cache.
|
|
270
|
+
* Supports: /no-cache, /fresh
|
|
271
|
+
*/
|
|
272
|
+
export function checkNoCacheOverride(input) {
|
|
273
|
+
const trimmed = input.trim();
|
|
274
|
+
const patterns = [/^\/no-cache\s+(.+)$/i, /^\/fresh\s+(.+)$/i];
|
|
275
|
+
for (const pattern of patterns) {
|
|
276
|
+
const match = trimmed.match(pattern);
|
|
277
|
+
if (match) {
|
|
278
|
+
return { bypass: true, query: match[1].trim() };
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Check if user requested to clear cache.
|
|
285
|
+
* Supports: /clear-cache
|
|
286
|
+
*/
|
|
287
|
+
export function checkClearCacheCommand(input) {
|
|
288
|
+
return /^\/clear-cache\s*$/i.test(input.trim());
|
|
289
|
+
}
|
|
290
|
+
//# sourceMappingURL=response-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response-cache.js","sourceRoot":"","sources":["../../../src/core/cost/response-cache.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAuBH,MAAM,CAAC,MAAM,6BAA6B,GAAwB;IAChE,OAAO,EAAE,IAAI;IACb,UAAU,EAAE,GAAG;IACf,KAAK,EAAE,CAAC,GAAG,EAAE,GAAG,IAAI,EAAG,YAAY;IACnC,gBAAgB,EAAE;QAChB,SAAS;QACT,MAAM;QACN,QAAQ;QACR,MAAM;QACN,WAAW;KACZ;CACF,CAAC;AAEF,sCAAsC;AACtC,MAAM,CAAC,MAAM,mBAAmB,GAAiB;IAC/C,UAAU;IACV,UAAU;IACV,SAAS;IACT,aAAa;IACb,YAAY;IACZ,cAAc;IACd,UAAU;IACV,SAAS;IACT,SAAS;CACV,CAAC;AAEF,kEAAkE;AAClE,uBAAuB;AACvB,kEAAkE;AAElE,MAAM,OAAO,aAAa;IAChB,KAAK,GAAgC,IAAI,GAAG,EAAE,CAAC;IAC/C,MAAM,CAA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C,OAAO,CAAC,CAAC;QACrC,IAAI,KAAK,EAAE,CAAC;YACV,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;QAClD,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAAa;IAClD,OAAO,qBAAqB,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;AAClD,CAAC"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DartMind V17.3 - Tool Loading Optimization
|
|
3
|
+
*
|
|
4
|
+
* PURPOSE: Load only tools needed for the current task.
|
|
5
|
+
* COST IMPACT: 10-15% savings by reducing token overhead
|
|
6
|
+
* QUALITY IMPACT: NONE (all needed tools available)
|
|
7
|
+
*
|
|
8
|
+
* TOOL GROUPS:
|
|
9
|
+
* - Core: Always loaded (Read, Write, Edit, Bash)
|
|
10
|
+
* - Analysis: For code review/explain (Grep, Glob)
|
|
11
|
+
* - Generation: For code creation (Write, Edit, TodoWrite)
|
|
12
|
+
* - Testing: For test-related tasks (Bash, Glob)
|
|
13
|
+
* - MCP: For DartMind tools (verify_package, analyze_dart, etc.)
|
|
14
|
+
*
|
|
15
|
+
* SAFETY:
|
|
16
|
+
* - Core tools always available
|
|
17
|
+
* - Unknown intents get all tools
|
|
18
|
+
* - User can force all tools with /all-tools
|
|
19
|
+
*
|
|
20
|
+
* @module cost/tool-loader
|
|
21
|
+
* @version 17.3.0
|
|
22
|
+
*/
|
|
23
|
+
import { TaskIntent } from './types.js';
|
|
24
|
+
export type ToolName = 'Read' | 'Write' | 'Edit' | 'Bash' | 'Glob' | 'Grep' | 'TodoWrite' | 'WebFetch' | 'WebSearch' | 'mcp__verify_package' | 'mcp__analyze_dart' | 'mcp__security_scan' | 'mcp__check_policy';
|
|
25
|
+
export type ToolGroup = 'core' | 'analysis' | 'generation' | 'testing' | 'mcp' | 'web';
|
|
26
|
+
export declare const TOOL_GROUPS: Record<ToolGroup, ToolName[]>;
|
|
27
|
+
export declare const ALL_TOOLS: ToolName[];
|
|
28
|
+
/**
|
|
29
|
+
* Maps task intents to required tool groups.
|
|
30
|
+
* Design principle: Include all potentially needed tools, exclude clearly unnecessary ones.
|
|
31
|
+
*/
|
|
32
|
+
export declare const INTENT_TOOLS_MAP: Record<TaskIntent, ToolGroup[]>;
|
|
33
|
+
export interface ToolLoaderConfig {
|
|
34
|
+
alwaysIncludeCore: boolean;
|
|
35
|
+
enableMcpTools: boolean;
|
|
36
|
+
enableWebTools: boolean;
|
|
37
|
+
maxTools: number;
|
|
38
|
+
}
|
|
39
|
+
export declare const DEFAULT_TOOL_CONFIG: ToolLoaderConfig;
|
|
40
|
+
/**
|
|
41
|
+
* Get tools needed for a specific intent.
|
|
42
|
+
*
|
|
43
|
+
* @param intent - Task intent
|
|
44
|
+
* @param config - Tool loader configuration
|
|
45
|
+
* @returns Array of tool names to load
|
|
46
|
+
*/
|
|
47
|
+
export declare function getToolsForIntent(intent: TaskIntent, config?: ToolLoaderConfig): ToolName[];
|
|
48
|
+
/**
|
|
49
|
+
* Get all available tools (for /all-tools override).
|
|
50
|
+
*/
|
|
51
|
+
export declare function getAllTools(): ToolName[];
|
|
52
|
+
/**
|
|
53
|
+
* Get tools for multiple intents (union).
|
|
54
|
+
*/
|
|
55
|
+
export declare function getToolsForIntents(intents: TaskIntent[], config?: ToolLoaderConfig): ToolName[];
|
|
56
|
+
export interface ToolMetrics {
|
|
57
|
+
toolsLoaded: number;
|
|
58
|
+
toolsAvailable: number;
|
|
59
|
+
tokensEstimate: number;
|
|
60
|
+
savingsPercent: number;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Calculate metrics for loaded tools.
|
|
64
|
+
*/
|
|
65
|
+
export declare function calculateToolMetrics(loadedTools: ToolName[]): ToolMetrics;
|
|
66
|
+
/**
|
|
67
|
+
* Format tool metrics for logging.
|
|
68
|
+
*/
|
|
69
|
+
export declare function formatToolMetrics(metrics: ToolMetrics): string;
|
|
70
|
+
/**
|
|
71
|
+
* Check if user requested all tools.
|
|
72
|
+
* Supports: /all-tools, /full-tools
|
|
73
|
+
*/
|
|
74
|
+
export declare function checkAllToolsOverride(input: string): {
|
|
75
|
+
override: boolean;
|
|
76
|
+
query: string;
|
|
77
|
+
} | null;
|
|
78
|
+
/**
|
|
79
|
+
* Check if user requested specific tool groups.
|
|
80
|
+
* Supports: /tools:core, /tools:analysis,generation
|
|
81
|
+
*/
|
|
82
|
+
export declare function checkToolGroupOverride(input: string): {
|
|
83
|
+
groups: ToolGroup[];
|
|
84
|
+
query: string;
|
|
85
|
+
} | null;
|
|
86
|
+
/**
|
|
87
|
+
* Get tools from specific groups.
|
|
88
|
+
*/
|
|
89
|
+
export declare function getToolsFromGroups(groups: ToolGroup[], config?: ToolLoaderConfig): ToolName[];
|
|
90
|
+
/**
|
|
91
|
+
* Get a human-readable description of which tools will be loaded.
|
|
92
|
+
*/
|
|
93
|
+
export declare function describeTools(tools: ToolName[]): string;
|
|
94
|
+
//# sourceMappingURL=tool-loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-loader.d.ts","sourceRoot":"","sources":["../../../src/core/cost/tool-loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAMxC,MAAM,MAAM,QAAQ,GAChB,MAAM,GACN,OAAO,GACP,MAAM,GACN,MAAM,GACN,MAAM,GACN,MAAM,GACN,WAAW,GACX,UAAU,GACV,WAAW,GAEX,qBAAqB,GACrB,mBAAmB,GACnB,oBAAoB,GACpB,mBAAmB,CAAC;AAExB,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,UAAU,GAAG,YAAY,GAAG,SAAS,GAAG,KAAK,GAAG,K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sBAAsB,CAAC,KAAK,EAAE,MAAM,GAAG;IAAE,MAAM,EAAE,SAAS,EAAE,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAoBnG;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,SAAS,EAAE,EACnB,MAAM,GAAE,gBAAsC,GAC7C,QAAQ,EAAE,CAgBZ;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,MAAM,CAUvD"}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DartMind V17.3 - Tool Loading Optimization
|
|
3
|
+
*
|
|
4
|
+
* PURPOSE: Load only tools needed for the current task.
|
|
5
|
+
* COST IMPACT: 10-15% savings by reducing token overhead
|
|
6
|
+
* QUALITY IMPACT: NONE (all needed tools available)
|
|
7
|
+
*
|
|
8
|
+
* TOOL GROUPS:
|
|
9
|
+
* - Core: Always loaded (Read, Write, Edit, Bash)
|
|
10
|
+
* - Analysis: For code review/explain (Grep, Glob)
|
|
11
|
+
* - Generation: For code creation (Write, Edit, TodoWrite)
|
|
12
|
+
* - Testing: For test-related tasks (Bash, Glob)
|
|
13
|
+
* - MCP: For DartMind tools (verify_package, analyze_dart, etc.)
|
|
14
|
+
*
|
|
15
|
+
* SAFETY:
|
|
16
|
+
* - Core tools always available
|
|
17
|
+
* - Unknown intents get all tools
|
|
18
|
+
* - User can force all tools with /all-tools
|
|
19
|
+
*
|
|
20
|
+
* @module cost/tool-loader
|
|
21
|
+
* @version 17.3.0
|
|
22
|
+
*/
|
|
23
|
+
export const TOOL_GROUPS = {
|
|
24
|
+
core: ['Read', 'Bash'],
|
|
25
|
+
analysis: ['Grep', 'Glob'],
|
|
26
|
+
generation: ['Write', 'Edit', 'TodoWrite'],
|
|
27
|
+
testing: ['Bash', 'Glob', 'Grep'],
|
|
28
|
+
mcp: ['mcp__verify_package', 'mcp__analyze_dart', 'mcp__security_scan', 'mcp__check_policy'],
|
|
29
|
+
web: ['WebFetch', 'WebSearch'],
|
|
30
|
+
};
|
|
31
|
+
export const ALL_TOOLS = [
|
|
32
|
+
'Read', 'Write', 'Edit', 'Bash', 'Glob', 'Grep',
|
|
33
|
+
'TodoWrite', 'WebFetch', 'WebSearch',
|
|
34
|
+
'mcp__verify_package', 'mcp__analyze_dart', 'mcp__security_scan', 'mcp__check_policy'
|
|
35
|
+
];
|
|
36
|
+
// ═══════════════════════════════════════════════════════════════
|
|
37
|
+
// INTENT TO TOOLS MAPPING
|
|
38
|
+
// ═══════════════════════════════════════════════════════════════
|
|
39
|
+
/**
|
|
40
|
+
* Maps task intents to required tool groups.
|
|
41
|
+
* Design principle: Include all potentially needed tools, exclude clearly unnecessary ones.
|
|
42
|
+
*/
|
|
43
|
+
export const INTENT_TOOLS_MAP = {
|
|
44
|
+
// Reflex - No tools needed
|
|
45
|
+
greeting: [],
|
|
46
|
+
farewell: [],
|
|
47
|
+
gibberish: [],
|
|
48
|
+
affirmation: [],
|
|
49
|
+
command: [],
|
|
50
|
+
// Simple tasks - Core only
|
|
51
|
+
simple_qa: ['core'],
|
|
52
|
+
search: ['core', 'analysis'],
|
|
53
|
+
// Read/explain - Core + Analysis
|
|
54
|
+
explain: ['core', 'analysis'],
|
|
55
|
+
read: ['core', 'analysis'],
|
|
56
|
+
list: ['core', 'analysis'],
|
|
57
|
+
// Code generation - Core + Generation + MCP
|
|
58
|
+
generate: ['core', 'generation', 'mcp'],
|
|
59
|
+
refactor: ['core', 'analysis', 'generation', 'mcp'],
|
|
60
|
+
fix_bug: ['core', 'analysis', 'generation', 'mcp'],
|
|
61
|
+
write_tests: ['core', 'analysis', 'generation', 'testing'],
|
|
62
|
+
// Complex tasks - All tools
|
|
63
|
+
multi_file: ['core', 'analysis', 'generation', 'mcp'],
|
|
64
|
+
architecture: ['core', 'analysis', 'generation', 'mcp'],
|
|
65
|
+
security: ['core', 'analysis', 'generation', 'mcp'],
|
|
66
|
+
complex: ['core', 'analysis', 'generation', 'mcp', 'web'],
|
|
67
|
+
// Unknown - All tools (safe default)
|
|
68
|
+
unknown: ['core', 'analysis', 'generation', 'mcp', 'web'],
|
|
69
|
+
};
|
|
70
|
+
export const DEFAULT_TOOL_CONFIG = {
|
|
71
|
+
alwaysIncludeCore: true,
|
|
72
|
+
enableMcpTools: true,
|
|
73
|
+
enableWebTools: true,
|
|
74
|
+
maxTools: 20,
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Get tools needed for a specific intent.
|
|
78
|
+
*
|
|
79
|
+
* @param intent - Task intent
|
|
80
|
+
* @param config - Tool loader configuration
|
|
81
|
+
* @returns Array of tool names to load
|
|
82
|
+
*/
|
|
83
|
+
export function getToolsForIntent(intent, config = DEFAULT_TOOL_CONFIG) {
|
|
84
|
+
const toolSet = new Set();
|
|
85
|
+
// Always include core tools
|
|
86
|
+
if (config.alwaysIncludeCore) {
|
|
87
|
+
TOOL_GROUPS.core.forEach(tool => toolSet.add(tool));
|
|
88
|
+
}
|
|
89
|
+
// Get tool groups for intent
|
|
90
|
+
const groups = INTENT_TOOLS_MAP[intent] || INTENT_TOOLS_MAP.unknown;
|
|
91
|
+
// Add tools from each group
|
|
92
|
+
for (const group of groups) {
|
|
93
|
+
// Skip MCP/web tools if disabled
|
|
94
|
+
if (group === 'mcp' && !config.enableMcpTools)
|
|
95
|
+
continue;
|
|
96
|
+
if (group === 'web' && !config.enableWebTools)
|
|
97
|
+
continue;
|
|
98
|
+
const tools = TOOL_GROUPS[group];
|
|
99
|
+
tools.forEach(tool => toolSet.add(tool));
|
|
100
|
+
}
|
|
101
|
+
// Convert to array and limit
|
|
102
|
+
const result = Array.from(toolSet).slice(0, config.maxTools);
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get all available tools (for /all-tools override).
|
|
107
|
+
*/
|
|
108
|
+
export function getAllTools() {
|
|
109
|
+
return [...ALL_TOOLS];
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Get tools for multiple intents (union).
|
|
113
|
+
*/
|
|
114
|
+
export function getToolsForIntents(intents, config = DEFAULT_TOOL_CONFIG) {
|
|
115
|
+
const toolSet = new Set();
|
|
116
|
+
for (const intent of intents) {
|
|
117
|
+
const tools = getToolsForIntent(intent, config);
|
|
118
|
+
tools.forEach(tool => toolSet.add(tool));
|
|
119
|
+
}
|
|
120
|
+
return Array.from(toolSet).slice(0, config.maxTools);
|
|
121
|
+
}
|
|
122
|
+
// Rough token estimates per tool definition
|
|
123
|
+
const TOOL_TOKEN_ESTIMATES = {
|
|
124
|
+
Read: 100,
|
|
125
|
+
Write: 120,
|
|
126
|
+
Edit: 150,
|
|
127
|
+
Bash: 200,
|
|
128
|
+
Glob: 80,
|
|
129
|
+
Grep: 100,
|
|
130
|
+
TodoWrite: 150,
|
|
131
|
+
WebFetch: 100,
|
|
132
|
+
WebSearch: 100,
|
|
133
|
+
mcp__verify_package: 80,
|
|
134
|
+
mcp__analyze_dart: 80,
|
|
135
|
+
mcp__security_scan: 80,
|
|
136
|
+
mcp__check_policy: 80,
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Calculate metrics for loaded tools.
|
|
140
|
+
*/
|
|
141
|
+
export function calculateToolMetrics(loadedTools) {
|
|
142
|
+
const tokensLoaded = loadedTools.reduce((sum, tool) => sum + (TOOL_TOKEN_ESTIMATES[tool] || 100), 0);
|
|
143
|
+
const allToolsTokens = ALL_TOOLS.reduce((sum, tool) => sum + (TOOL_TOKEN_ESTIMATES[tool] || 100), 0);
|
|
144
|
+
const savings = allToolsTokens > 0
|
|
145
|
+
? ((allToolsTokens - tokensLoaded) / allToolsTokens) * 100
|
|
146
|
+
: 0;
|
|
147
|
+
return {
|
|
148
|
+
toolsLoaded: loadedTools.length,
|
|
149
|
+
toolsAvailable: ALL_TOOLS.length,
|
|
150
|
+
tokensEstimate: tokensLoaded,
|
|
151
|
+
savingsPercent: savings,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Format tool metrics for logging.
|
|
156
|
+
*/
|
|
157
|
+
export function formatToolMetrics(metrics) {
|
|
158
|
+
return [
|
|
159
|
+
`Tools: ${metrics.toolsLoaded}/${metrics.toolsAvailable}`,
|
|
160
|
+
`~${metrics.tokensEstimate} tokens`,
|
|
161
|
+
`${metrics.savingsPercent.toFixed(1)}% saved`,
|
|
162
|
+
].join(' | ');
|
|
163
|
+
}
|
|
164
|
+
// ═══════════════════════════════════════════════════════════════
|
|
165
|
+
// TOOL OVERRIDE DETECTION
|
|
166
|
+
// ═══════════════════════════════════════════════════════════════
|
|
167
|
+
/**
|
|
168
|
+
* Check if user requested all tools.
|
|
169
|
+
* Supports: /all-tools, /full-tools
|
|
170
|
+
*/
|
|
171
|
+
export function checkAllToolsOverride(input) {
|
|
172
|
+
const trimmed = input.trim();
|
|
173
|
+
const patterns = [/^\/all-tools\s+(.+)$/i, /^\/full-tools\s+(.+)$/i];
|
|
174
|
+
for (const pattern of patterns) {
|
|
175
|
+
const match = trimmed.match(pattern);
|
|
176
|
+
if (match) {
|
|
177
|
+
return { override: true, query: match[1].trim() };
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Check if user requested specific tool groups.
|
|
184
|
+
* Supports: /tools:core, /tools:analysis,generation
|
|
185
|
+
*/
|
|
186
|
+
export function checkToolGroupOverride(input) {
|
|
187
|
+
const trimmed = input.trim();
|
|
188
|
+
const match = trimmed.match(/^\/tools:([a-z,]+)\s+(.+)$/i);
|
|
189
|
+
if (match) {
|
|
190
|
+
const groupNames = match[1].split(',').map(g => g.trim().toLowerCase());
|
|
191
|
+
const validGroups = [];
|
|
192
|
+
for (const name of groupNames) {
|
|
193
|
+
if (name in TOOL_GROUPS) {
|
|
194
|
+
validGroups.push(name);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (validGroups.length > 0) {
|
|
198
|
+
return { groups: validGroups, query: match[2].trim() };
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Get tools from specific groups.
|
|
205
|
+
*/
|
|
206
|
+
export function getToolsFromGroups(groups, config = DEFAULT_TOOL_CONFIG) {
|
|
207
|
+
const toolSet = new Set();
|
|
208
|
+
// Always include core
|
|
209
|
+
if (config.alwaysIncludeCore) {
|
|
210
|
+
TOOL_GROUPS.core.forEach(tool => toolSet.add(tool));
|
|
211
|
+
}
|
|
212
|
+
for (const group of groups) {
|
|
213
|
+
if (group === 'mcp' && !config.enableMcpTools)
|
|
214
|
+
continue;
|
|
215
|
+
if (group === 'web' && !config.enableWebTools)
|
|
216
|
+
continue;
|
|
217
|
+
TOOL_GROUPS[group].forEach(tool => toolSet.add(tool));
|
|
218
|
+
}
|
|
219
|
+
return Array.from(toolSet).slice(0, config.maxTools);
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Get a human-readable description of which tools will be loaded.
|
|
223
|
+
*/
|
|
224
|
+
export function describeTools(tools) {
|
|
225
|
+
const groups = [];
|
|
226
|
+
for (const [group, groupTools] of Object.entries(TOOL_GROUPS)) {
|
|
227
|
+
if (groupTools.some(t => tools.includes(t))) {
|
|
228
|
+
groups.push(group);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return `${tools.length} tools (${groups.join(', ')})`;
|
|
232
|
+
}
|
|
233
|
+
//# sourceMappingURL=tool-loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-loader.js","sourceRoot":"","sources":["../../../src/core/cost/tool-loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AA0BH,MAAM,CAAC,MAAM,WAAW,GAAkC;IACxD,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,CAAC,MAAM,EAAE,MAA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