myshell-tools 1.0.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.
Files changed (45) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/LICENSE +21 -0
  3. package/README.md +318 -0
  4. package/data/orchestrator.json +113 -0
  5. package/package.json +49 -0
  6. package/src/auth/recovery.mjs +328 -0
  7. package/src/auth/refresh.mjs +373 -0
  8. package/src/chef.mjs +348 -0
  9. package/src/cli/doctor.mjs +568 -0
  10. package/src/cli/reset.mjs +447 -0
  11. package/src/cli/status.mjs +379 -0
  12. package/src/cli.mjs +429 -0
  13. package/src/commands/doctor.mjs +375 -0
  14. package/src/commands/help.mjs +324 -0
  15. package/src/commands/status.mjs +331 -0
  16. package/src/monitor/health.mjs +486 -0
  17. package/src/monitor/performance.mjs +442 -0
  18. package/src/monitor/report.mjs +535 -0
  19. package/src/orchestrator/classify.mjs +391 -0
  20. package/src/orchestrator/confidence.mjs +151 -0
  21. package/src/orchestrator/handoffs.mjs +231 -0
  22. package/src/orchestrator/review.mjs +222 -0
  23. package/src/providers/balance.mjs +201 -0
  24. package/src/providers/claude.mjs +236 -0
  25. package/src/providers/codex.mjs +255 -0
  26. package/src/providers/detect.mjs +185 -0
  27. package/src/providers/errors.mjs +373 -0
  28. package/src/providers/select.mjs +162 -0
  29. package/src/repl-enhanced.mjs +417 -0
  30. package/src/repl.mjs +321 -0
  31. package/src/state/archive.mjs +366 -0
  32. package/src/state/atomic.mjs +116 -0
  33. package/src/state/cleanup.mjs +440 -0
  34. package/src/state/recovery.mjs +461 -0
  35. package/src/state/session.mjs +147 -0
  36. package/src/ui/errors.mjs +456 -0
  37. package/src/ui/formatter.mjs +327 -0
  38. package/src/ui/icons.mjs +318 -0
  39. package/src/ui/progress.mjs +468 -0
  40. package/templates/prompts/confidence-format.txt +14 -0
  41. package/templates/prompts/ic-with-feedback.txt +41 -0
  42. package/templates/prompts/ic.txt +13 -0
  43. package/templates/prompts/manager-review.txt +40 -0
  44. package/templates/prompts/manager.txt +14 -0
  45. package/templates/prompts/worker.txt +12 -0
package/src/chef.mjs ADDED
@@ -0,0 +1,348 @@
1
+ /**
2
+ * chef.mjs — The three-tier hierarchical orchestration engine
3
+ */
4
+
5
+ import { executeClaude, buildClaudePrompt } from './providers/claude.mjs';
6
+ import { executeCodex, buildCodexPrompt } from './providers/codex.mjs';
7
+ import { classifyTask, shouldEscalate, selectModel } from './orchestrator/classify.mjs';
8
+ import { addMessage, addHandoff } from './state/session.mjs';
9
+ import { parseConfidence, shouldEscalateOnConfidence } from './orchestrator/confidence.mjs';
10
+ import { logHandoff, logEscalation, logBounce, checkFailureLoop } from './orchestrator/handoffs.mjs';
11
+ import { runManagerReview, shouldTriggerManagerReview } from './orchestrator/review.mjs';
12
+ import { selectProvider, checkProviderHealth } from './providers/select.mjs';
13
+ import { balanceProviderLoad } from './providers/balance.mjs';
14
+ import { trackHandoff } from './monitor/performance.mjs';
15
+
16
+ /**
17
+ * Execute a task at a specific tier with intelligent provider selection
18
+ */
19
+ async function runTier(tier, task, context = {}) {
20
+ const { availableModels, sessionId } = context;
21
+
22
+ // Use intelligent provider selection with load balancing
23
+ const model = selectProvider ?
24
+ selectProvider(tier, { availableModels, sessionId }) :
25
+ selectModel(tier, availableModels);
26
+
27
+ if (!model) {
28
+ return {
29
+ success: false,
30
+ error: `No available model for tier: ${tier}`,
31
+ tier,
32
+ model: null
33
+ };
34
+ }
35
+
36
+ // Check provider health before executing
37
+ if (checkProviderHealth) {
38
+ const health = checkProviderHealth(model.provider);
39
+ if (!health.available) {
40
+ console.log(` âš ī¸ ${model.provider} is degraded (${health.failures} recent failures)`);
41
+ // Try to find alternative provider
42
+ const tierModels = availableModels[tier] || [];
43
+ const alternative = tierModels.find(m => m.provider !== model.provider);
44
+ if (alternative) {
45
+ console.log(` 🔄 Switching to ${alternative.provider}/${alternative.model}`);
46
+ return runTier(tier, task, { ...context, availableModels: { [tier]: [alternative] } });
47
+ }
48
+ }
49
+ }
50
+
51
+ console.log(` ${tier.toUpperCase()} (${model.provider}/${model.model}): Working...`);
52
+
53
+ const startTime = Date.now();
54
+ let result;
55
+ let prompt;
56
+
57
+ try {
58
+ // Build appropriate prompt for the provider and tier
59
+ if (model.provider === 'claude') {
60
+ prompt = buildClaudePrompt(tier, task, context);
61
+ result = await executeClaude(model.bin, model.model, prompt, context.options);
62
+ } else if (model.provider === 'codex') {
63
+ prompt = buildCodexPrompt(tier, task, context);
64
+ result = await executeCodex(model.bin, model.model, prompt, context.options);
65
+ } else {
66
+ return {
67
+ success: false,
68
+ error: `Unknown provider: ${model.provider}`,
69
+ tier,
70
+ model
71
+ };
72
+ }
73
+
74
+ // Enhanced confidence parsing
75
+ if (result.success && result.output) {
76
+ const confidenceData = parseConfidence(result.output);
77
+ result.confidence = confidenceData.confidence;
78
+ result.escalate = confidenceData.escalate;
79
+ result.reasoning = confidenceData.reason;
80
+ result.needsReview = confidenceData.needsReview;
81
+ result.structured = confidenceData.structured;
82
+ }
83
+
84
+ // Add metadata
85
+ result.tier = tier;
86
+ result.selectedModel = model;
87
+ result.durationMs = Date.now() - startTime;
88
+
89
+ // Log the handoff for audit trail
90
+ if (logHandoff) {
91
+ logHandoff('execute', 'user', tier, {
92
+ providerfrom: 'user',
93
+ providerTo: model.provider,
94
+ confidenceOut: result.confidence,
95
+ durationMs: result.durationMs,
96
+ sessionId: context.sessionId,
97
+ success: result.success
98
+ });
99
+ }
100
+
101
+ // Track performance metrics
102
+ trackHandoff({
103
+ operation: 'execute',
104
+ fromTier: 'user',
105
+ toTier: tier,
106
+ provider: model.provider,
107
+ model: model.model,
108
+ confidence: result.confidence,
109
+ success: result.success,
110
+ durationMs: result.durationMs,
111
+ tokensUsed: result.usage?.total_tokens || null,
112
+ prompt: task,
113
+ output: result.output
114
+ });
115
+
116
+ return result;
117
+
118
+ } catch (error) {
119
+ const errorResult = {
120
+ success: false,
121
+ error: error.message,
122
+ tier,
123
+ model,
124
+ output: '',
125
+ durationMs: Date.now() - startTime
126
+ };
127
+
128
+ // Log failure for future routing decisions
129
+ if (logHandoff) {
130
+ logHandoff('execute_failed', 'user', tier, {
131
+ providerFrom: 'user',
132
+ providerTo: model.provider,
133
+ durationMs: errorResult.durationMs,
134
+ sessionId: context.sessionId,
135
+ reason: error.message,
136
+ success: false
137
+ });
138
+ }
139
+
140
+ return errorResult;
141
+ }
142
+ }
143
+
144
+ /**
145
+ * The main orchestration function with Phase 2 smart routing enhancements
146
+ */
147
+ export async function chef(userMessage, context = {}) {
148
+ console.log(`\nCortex AI Org Chart Processing: "${userMessage}"`);
149
+
150
+ // Generate session ID for tracking
151
+ const sessionId = context.sessionId || `session_${Date.now()}_${Math.random().toString(36).substr(2, 6)}`;
152
+ context.sessionId = sessionId;
153
+
154
+ // Step 1: Enhanced task classification
155
+ const classification = classifyTask(userMessage, context.fileContext);
156
+ console.log(`Task Classification: ${classification.tier} tier, ${classification.risk} risk`);
157
+ console.log(`Reason: ${classification.reason}`);
158
+ console.log(`Confidence Estimate: ${(classification.confidence * 100).toFixed(0)}%`);
159
+
160
+ // Check for failure loops before starting
161
+ const taskHash = createTaskHash(userMessage);
162
+ const failureCheck = checkFailureLoop ? checkFailureLoop(taskHash) : { isLoop: false };
163
+
164
+ if (failureCheck.isLoop) {
165
+ console.log(` âš ī¸ Failure loop detected (${failureCheck.bounceCount} bounces, score: ${failureCheck.weightedScore.toFixed(1)})`);
166
+ if (failureCheck.suggestion === 'escalate_to_manager') {
167
+ classification.tier = 'manager';
168
+ console.log(` â†—ī¸ Auto-escalating to MANAGER due to failure loop`);
169
+ }
170
+ }
171
+
172
+ // Step 2: Start with the classified tier (or IC as default)
173
+ let currentTier = classification.tier === 'manager' ? 'manager' : 'ic';
174
+ let attempts = 0;
175
+ const maxAttempts = 3;
176
+ let lastResult = null;
177
+
178
+ while (attempts < maxAttempts) {
179
+ attempts++;
180
+ console.log(`\nAttempt ${attempts}: Starting at ${currentTier.toUpperCase()} tier`);
181
+
182
+ // Execute at current tier
183
+ const result = await runTier(currentTier, userMessage, {
184
+ ...context,
185
+ attempt: attempts,
186
+ taskHash,
187
+ managerNotes: context.managerNotes
188
+ });
189
+
190
+ if (!result.success) {
191
+ console.log(` ❌ ${currentTier.toUpperCase()} failed: ${result.error}`);
192
+
193
+ // Log escalation due to failure
194
+ if (logEscalation && currentTier !== 'manager') {
195
+ logEscalation(currentTier, 'manager', 'execution failure', {
196
+ sessionId,
197
+ attempt: attempts,
198
+ taskHash,
199
+ error: result.error
200
+ });
201
+ }
202
+
203
+ if (currentTier !== 'manager') {
204
+ console.log(` â†—ī¸ ESCALATE → MANAGER (execution failure)`);
205
+ addHandoff('escalate', currentTier, 'manager', 'execution failure');
206
+ currentTier = 'manager';
207
+ context.previous = result;
208
+ continue;
209
+ } else {
210
+ return {
211
+ success: false,
212
+ output: result.error,
213
+ tier: 'manager',
214
+ totalAttempts: attempts,
215
+ finalResult: result,
216
+ sessionId
217
+ };
218
+ }
219
+ }
220
+
221
+ lastResult = result;
222
+
223
+ // Log successful completion
224
+ console.log(` ✓ ${currentTier.toUpperCase()} completed`);
225
+ if (result.confidence !== null) {
226
+ console.log(` Confidence: ${(result.confidence * 100).toFixed(0)}%`);
227
+ }
228
+
229
+ // Phase 2 Enhancement: Manager Review Pattern for high-stakes work
230
+ if (currentTier === 'ic' && shouldTriggerManagerReview) {
231
+ const reviewCheck = shouldTriggerManagerReview(userMessage, classification, result);
232
+
233
+ if (reviewCheck.required) {
234
+ console.log(` 🔍 Triggering manager review: ${reviewCheck.reason}`);
235
+
236
+ const review = await runManagerReview(userMessage, result, context, runTier);
237
+
238
+ if (review.verdict === 'bounce' && attempts < maxAttempts) {
239
+ console.log(` â†Šī¸ BOUNCE DOWN: Manager wants fixes (attempt ${attempts + 1})`);
240
+
241
+ // Log bounce for audit trail
242
+ if (logBounce) {
243
+ logBounce('manager', 'ic', review.notes, attempts + 1, {
244
+ sessionId,
245
+ taskHash,
246
+ originalConfidence: result.confidence,
247
+ reviewConfidence: review.managerResult.confidence
248
+ });
249
+ }
250
+
251
+ // Set up context for retry with manager feedback
252
+ context.managerNotes = review.notes;
253
+ context.attempt = attempts + 1;
254
+
255
+ // Stay at IC tier but incorporate manager feedback
256
+ continue; // This will increment attempts and retry
257
+ } else if (review.verdict === 'escalate') {
258
+ console.log(` â†—ī¸ ESCALATE: Manager taking over directly`);
259
+ currentTier = 'manager';
260
+ context.previous = result;
261
+ continue;
262
+ } else if (review.verdict === 'approve') {
263
+ console.log(` ✅ Manager approved IC work`);
264
+ // Continue with approval
265
+ }
266
+ }
267
+ }
268
+
269
+ // Check if we should escalate based on confidence/risk
270
+ const needsEscalation = shouldEscalate(result, classification) ||
271
+ shouldEscalateOnConfidence(result.confidence, classification.risk, currentTier);
272
+
273
+ if (needsEscalation && currentTier !== 'manager') {
274
+ console.log(` â†—ī¸ ESCALATE → MANAGER (${result.reasoning || 'low confidence'})`);
275
+
276
+ if (logEscalation) {
277
+ logEscalation(currentTier, 'manager', result.reasoning || 'low confidence', {
278
+ sessionId,
279
+ attempt: attempts,
280
+ taskHash,
281
+ confidence: result.confidence,
282
+ riskLevel: classification.risk
283
+ });
284
+ }
285
+
286
+ addHandoff('escalate', currentTier, 'manager', result.reasoning || 'low confidence', {
287
+ confidence: result.confidence
288
+ });
289
+
290
+ currentTier = 'manager';
291
+ context.previous = result;
292
+ continue;
293
+ }
294
+
295
+ // Task completed successfully
296
+ console.log(` đŸŽ¯ Task completed at ${currentTier.toUpperCase()} tier`);
297
+
298
+ return {
299
+ success: true,
300
+ output: result.output,
301
+ tier: currentTier,
302
+ confidence: result.confidence,
303
+ model: result.selectedModel,
304
+ classification,
305
+ totalAttempts: attempts,
306
+ finalResult: result,
307
+ sessionId,
308
+ review: currentTier === 'ic' ? 'manager_approved' : 'direct_completion'
309
+ };
310
+ }
311
+
312
+ return {
313
+ success: false,
314
+ output: 'Maximum escalation attempts reached',
315
+ tier: currentTier,
316
+ totalAttempts: attempts,
317
+ error: 'Max attempts exceeded',
318
+ sessionId,
319
+ lastResult
320
+ };
321
+ }
322
+
323
+ /**
324
+ * Create a hash for task tracking across attempts
325
+ */
326
+ function createTaskHash(task) {
327
+ // Simple hash for tracking related attempts
328
+ return `task_${Buffer.from(task.slice(0, 100)).toString('base64').slice(0, 8)}`;
329
+ }
330
+
331
+ /**
332
+ * Handle delegation to worker tier for simple tasks
333
+ */
334
+ export async function delegateToWorker(task, context = {}) {
335
+ console.log(` â†˜ī¸ DELEGATE → WORKER`);
336
+ addHandoff('delegate', 'ic', 'worker', 'simple task delegation');
337
+
338
+ const result = await runTier('worker', task, context);
339
+
340
+ if (!result.success || shouldEscalate(result, classifyTask(task))) {
341
+ console.log(` â†—ī¸ WORKER → IC (delegation failed or escalation needed)`);
342
+ addHandoff('escalate', 'worker', 'ic', result.reasoning || 'worker task failed');
343
+ return null; // Signal to retry at IC level
344
+ }
345
+
346
+ console.log(` ✓ WORKER completed delegation`);
347
+ return result;
348
+ }