@axplusb/kepler 0.0.1 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. package/README.md +82 -0
  2. package/package.json +36 -4
  3. package/pulse/app/activity/page.tsx +190 -0
  4. package/pulse/app/api/activity/route.ts +138 -0
  5. package/pulse/app/api/costs/route.ts +88 -0
  6. package/pulse/app/api/export/route.ts +77 -0
  7. package/pulse/app/api/history/route.ts +11 -0
  8. package/pulse/app/api/import/route.ts +31 -0
  9. package/pulse/app/api/memory/route.ts +52 -0
  10. package/pulse/app/api/plans/route.ts +9 -0
  11. package/pulse/app/api/projects/[slug]/route.ts +96 -0
  12. package/pulse/app/api/projects/route.ts +121 -0
  13. package/pulse/app/api/sessions/[id]/replay/route.ts +20 -0
  14. package/pulse/app/api/sessions/[id]/route.ts +31 -0
  15. package/pulse/app/api/sessions/route.ts +112 -0
  16. package/pulse/app/api/settings/route.ts +14 -0
  17. package/pulse/app/api/stats/route.ts +143 -0
  18. package/pulse/app/api/todos/route.ts +9 -0
  19. package/pulse/app/api/tools/route.ts +160 -0
  20. package/pulse/app/costs/page.tsx +179 -0
  21. package/pulse/app/export/page.tsx +465 -0
  22. package/pulse/app/favicon.ico +0 -0
  23. package/pulse/app/globals.css +263 -0
  24. package/pulse/app/help/page.tsx +142 -0
  25. package/pulse/app/history/page.tsx +157 -0
  26. package/pulse/app/layout.tsx +46 -0
  27. package/pulse/app/memory/page.tsx +365 -0
  28. package/pulse/app/overview-client.tsx +393 -0
  29. package/pulse/app/page.tsx +14 -0
  30. package/pulse/app/plans/page.tsx +308 -0
  31. package/pulse/app/projects/[slug]/page.tsx +390 -0
  32. package/pulse/app/projects/page.tsx +110 -0
  33. package/pulse/app/sessions/[id]/page.tsx +243 -0
  34. package/pulse/app/sessions/page.tsx +39 -0
  35. package/pulse/app/settings/page.tsx +188 -0
  36. package/pulse/app/todos/page.tsx +211 -0
  37. package/pulse/app/tools/page.tsx +249 -0
  38. package/pulse/cli.js +159 -0
  39. package/pulse/components/activity/day-of-week-chart.tsx +35 -0
  40. package/pulse/components/activity/streak-card.tsx +36 -0
  41. package/pulse/components/costs/cache-efficiency-panel.tsx +76 -0
  42. package/pulse/components/costs/cost-by-project-chart.tsx +48 -0
  43. package/pulse/components/costs/cost-over-time-chart.tsx +95 -0
  44. package/pulse/components/costs/model-token-table.tsx +60 -0
  45. package/pulse/components/global-search.tsx +193 -0
  46. package/pulse/components/keyboard-nav-provider.tsx +23 -0
  47. package/pulse/components/layout/bottom-nav.tsx +52 -0
  48. package/pulse/components/layout/client-layout.tsx +31 -0
  49. package/pulse/components/layout/sidebar-context.tsx +50 -0
  50. package/pulse/components/layout/sidebar.tsx +182 -0
  51. package/pulse/components/layout/top-bar.tsx +121 -0
  52. package/pulse/components/overview/activity-heatmap.tsx +107 -0
  53. package/pulse/components/overview/conversation-table.tsx +148 -0
  54. package/pulse/components/overview/model-breakdown-donut.tsx +95 -0
  55. package/pulse/components/overview/peak-hours-chart.tsx +87 -0
  56. package/pulse/components/overview/project-activity-donut.tsx +96 -0
  57. package/pulse/components/overview/stat-card.tsx +102 -0
  58. package/pulse/components/overview/usage-over-time-chart.tsx +166 -0
  59. package/pulse/components/projects/project-card.tsx +175 -0
  60. package/pulse/components/sessions/replay/assistant-markdown.tsx +94 -0
  61. package/pulse/components/sessions/replay/compaction-card.tsx +25 -0
  62. package/pulse/components/sessions/replay/session-sidebar.tsx +231 -0
  63. package/pulse/components/sessions/replay/token-accumulation-chart.tsx +98 -0
  64. package/pulse/components/sessions/replay/tool-call-badge.tsx +127 -0
  65. package/pulse/components/sessions/replay/turn-cards.tsx +220 -0
  66. package/pulse/components/sessions/replay/user-tool-result.tsx +158 -0
  67. package/pulse/components/sessions/session-badges.tsx +49 -0
  68. package/pulse/components/sessions/session-table.tsx +299 -0
  69. package/pulse/components/theme-provider.tsx +44 -0
  70. package/pulse/components/tools/feature-adoption-table.tsx +58 -0
  71. package/pulse/components/tools/mcp-server-panel.tsx +45 -0
  72. package/pulse/components/tools/tool-ranking-chart.tsx +57 -0
  73. package/pulse/components/tools/version-history-table.tsx +32 -0
  74. package/pulse/components/ui/alert.tsx +66 -0
  75. package/pulse/components/ui/badge.tsx +48 -0
  76. package/pulse/components/ui/breadcrumb.tsx +109 -0
  77. package/pulse/components/ui/button.tsx +64 -0
  78. package/pulse/components/ui/calendar.tsx +220 -0
  79. package/pulse/components/ui/card.tsx +92 -0
  80. package/pulse/components/ui/command.tsx +158 -0
  81. package/pulse/components/ui/dialog.tsx +158 -0
  82. package/pulse/components/ui/input.tsx +21 -0
  83. package/pulse/components/ui/popover.tsx +89 -0
  84. package/pulse/components/ui/progress.tsx +31 -0
  85. package/pulse/components/ui/select.tsx +190 -0
  86. package/pulse/components/ui/separator.tsx +28 -0
  87. package/pulse/components/ui/sheet.tsx +143 -0
  88. package/pulse/components/ui/skeleton.tsx +13 -0
  89. package/pulse/components/ui/table.tsx +116 -0
  90. package/pulse/components/ui/tabs.tsx +91 -0
  91. package/pulse/components/ui/tooltip.tsx +57 -0
  92. package/pulse/components/use-global-keyboard-nav.ts +79 -0
  93. package/pulse/components.json +23 -0
  94. package/pulse/eslint.config.mjs +18 -0
  95. package/pulse/lib/claude-reader.ts +594 -0
  96. package/pulse/lib/decode.ts +129 -0
  97. package/pulse/lib/pricing.ts +102 -0
  98. package/pulse/lib/replay-parser.ts +165 -0
  99. package/pulse/lib/tool-categories.ts +127 -0
  100. package/pulse/lib/utils.ts +6 -0
  101. package/pulse/next-env.d.ts +6 -0
  102. package/pulse/next.config.ts +16 -0
  103. package/pulse/package.json +45 -0
  104. package/pulse/postcss.config.mjs +7 -0
  105. package/pulse/public/activity.png +0 -0
  106. package/pulse/public/cc-lens.png +0 -0
  107. package/pulse/public/command-k.png +0 -0
  108. package/pulse/public/costs.png +0 -0
  109. package/pulse/public/dashboard-dark.png +0 -0
  110. package/pulse/public/dashboard-white.png +0 -0
  111. package/pulse/public/export.png +0 -0
  112. package/pulse/public/file.svg +1 -0
  113. package/pulse/public/globe.svg +1 -0
  114. package/pulse/public/next.svg +1 -0
  115. package/pulse/public/projects.png +0 -0
  116. package/pulse/public/session-chat.png +0 -0
  117. package/pulse/public/todos.png +0 -0
  118. package/pulse/public/tools.png +0 -0
  119. package/pulse/public/vercel.svg +1 -0
  120. package/pulse/public/window.svg +1 -0
  121. package/pulse/tsconfig.json +34 -0
  122. package/pulse/types/claude.ts +294 -0
  123. package/src/agents/loader.mjs +89 -0
  124. package/src/agents/parser.mjs +98 -0
  125. package/src/agents/teams.mjs +123 -0
  126. package/src/auth/oauth.mjs +220 -0
  127. package/src/auth/tarang-auth.mjs +277 -0
  128. package/src/config/cli-args.mjs +173 -0
  129. package/src/config/env.mjs +263 -0
  130. package/src/config/settings.mjs +132 -0
  131. package/src/context/ast-parser.mjs +298 -0
  132. package/src/context/bm25.mjs +85 -0
  133. package/src/context/retriever.mjs +270 -0
  134. package/src/context/skeleton.mjs +134 -0
  135. package/src/core/agent-loop.mjs +480 -0
  136. package/src/core/approval.mjs +273 -0
  137. package/src/core/backend-url.mjs +57 -0
  138. package/src/core/cache.mjs +105 -0
  139. package/src/core/callback-client.mjs +149 -0
  140. package/src/core/checkpoints.mjs +142 -0
  141. package/src/core/context-manager.mjs +198 -0
  142. package/src/core/headless.mjs +168 -0
  143. package/src/core/hooks-manager.mjs +87 -0
  144. package/src/core/jsonl-writer.mjs +351 -0
  145. package/src/core/local-agent.mjs +429 -0
  146. package/src/core/local-store.mjs +325 -0
  147. package/src/core/mode-selector.mjs +51 -0
  148. package/src/core/output-filter.mjs +177 -0
  149. package/src/core/paths.mjs +101 -0
  150. package/src/core/pricing.mjs +314 -0
  151. package/src/core/providers.mjs +219 -0
  152. package/src/core/rate-limiter.mjs +119 -0
  153. package/src/core/safety.mjs +200 -0
  154. package/src/core/scheduler.mjs +173 -0
  155. package/src/core/session-manager.mjs +317 -0
  156. package/src/core/session.mjs +143 -0
  157. package/src/core/settings-sync.mjs +85 -0
  158. package/src/core/stagnation.mjs +57 -0
  159. package/src/core/stream-client.mjs +367 -0
  160. package/src/core/streaming.mjs +182 -0
  161. package/src/core/system-prompt.mjs +135 -0
  162. package/src/core/tool-executor.mjs +725 -0
  163. package/src/hooks/engine.mjs +162 -0
  164. package/src/index.mjs +370 -0
  165. package/src/mcp/client.mjs +253 -0
  166. package/src/mcp/transport-shttp.mjs +130 -0
  167. package/src/mcp/transport-sse.mjs +131 -0
  168. package/src/mcp/transport-ws.mjs +134 -0
  169. package/src/permissions/checker.mjs +57 -0
  170. package/src/permissions/command-classifier.mjs +573 -0
  171. package/src/permissions/injection-check.mjs +60 -0
  172. package/src/permissions/path-check.mjs +102 -0
  173. package/src/permissions/prompt.mjs +73 -0
  174. package/src/permissions/sandbox.mjs +112 -0
  175. package/src/plugins/loader.mjs +138 -0
  176. package/src/skills/loader.mjs +147 -0
  177. package/src/skills/runner.mjs +55 -0
  178. package/src/telemetry/index.mjs +96 -0
  179. package/src/terminal/agents.mjs +177 -0
  180. package/src/terminal/analytics.mjs +292 -0
  181. package/src/terminal/ansi.mjs +421 -0
  182. package/src/terminal/main.mjs +150 -0
  183. package/src/terminal/repl.mjs +1484 -0
  184. package/src/terminal/tool-display.mjs +58 -0
  185. package/src/tools/agent.mjs +137 -0
  186. package/src/tools/ask-user.mjs +61 -0
  187. package/src/tools/bash.mjs +148 -0
  188. package/src/tools/cron-create.mjs +120 -0
  189. package/src/tools/cron-delete.mjs +49 -0
  190. package/src/tools/cron-list.mjs +37 -0
  191. package/src/tools/edit.mjs +82 -0
  192. package/src/tools/enter-worktree.mjs +69 -0
  193. package/src/tools/exit-worktree.mjs +57 -0
  194. package/src/tools/glob.mjs +117 -0
  195. package/src/tools/grep.mjs +129 -0
  196. package/src/tools/lint.mjs +71 -0
  197. package/src/tools/ls.mjs +58 -0
  198. package/src/tools/lsp.mjs +115 -0
  199. package/src/tools/multi-edit.mjs +94 -0
  200. package/src/tools/notebook-edit.mjs +96 -0
  201. package/src/tools/read-mcp-resource.mjs +57 -0
  202. package/src/tools/read.mjs +138 -0
  203. package/src/tools/registry.mjs +132 -0
  204. package/src/tools/remote-trigger.mjs +84 -0
  205. package/src/tools/send-message.mjs +64 -0
  206. package/src/tools/skill.mjs +52 -0
  207. package/src/tools/test-runner.mjs +49 -0
  208. package/src/tools/todo-write.mjs +68 -0
  209. package/src/tools/tool-search.mjs +77 -0
  210. package/src/tools/web-fetch.mjs +65 -0
  211. package/src/tools/web-search.mjs +89 -0
  212. package/src/tools/write.mjs +55 -0
  213. package/src/ui/banner.mjs +237 -0
  214. package/src/ui/commands.mjs +499 -0
  215. package/src/ui/formatter.mjs +379 -0
  216. package/src/ui/markdown.mjs +278 -0
  217. package/src/ui/slash-commands.mjs +258 -0
  218. package/index.js +0 -1
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Project Skeleton — lightweight codebase overview for LLM context.
3
+ *
4
+ * Generates a compact representation of the project:
5
+ * - File tree (directories + file names)
6
+ * - Function/class signatures extracted via regex (not full AST)
7
+ *
8
+ * Designed to be ~500-1000 tokens — gives the model a "map" so it knows
9
+ * where to look without reading every file.
10
+ */
11
+
12
+ import * as fs from 'node:fs';
13
+ import * as path from 'node:path';
14
+
15
+ const IGNORED_DIRS = new Set(['.git', 'node_modules', '.orca', '__pycache__', '.venv', 'venv', 'dist', 'build', '.next', '.cache', 'coverage', '.tox']);
16
+ const CODE_EXTS = new Set(['.js', '.mjs', '.ts', '.tsx', '.py', '.go', '.rs', '.java', '.rb', '.c', '.cpp', '.h']);
17
+ const MAX_FILE_SIZE = 200_000;
18
+
19
+ // Regex patterns for function/class signatures (multi-language)
20
+ const SIGNATURE_PATTERNS = [
21
+ // Python: def/class/async def
22
+ /^(?:async\s+)?(?:def|class)\s+(\w+)\s*[\(:].*$/gm,
23
+ // JS/TS: function, class, export function, const fn =
24
+ /^(?:export\s+)?(?:async\s+)?(?:function\s+(\w+)|class\s+(\w+))/gm,
25
+ // Go: func
26
+ /^func\s+(?:\(.*?\)\s+)?(\w+)\s*\(/gm,
27
+ // Rust: fn, struct, impl
28
+ /^(?:pub\s+)?(?:fn|struct|impl)\s+(\w+)/gm,
29
+ ];
30
+
31
+ /**
32
+ * Build a project skeleton — file tree + key signatures.
33
+ * @param {string} projectDir
34
+ * @param {Object} [options]
35
+ * @param {number} [options.maxFiles=200] — max files to include
36
+ * @param {number} [options.maxChars=4000] — max total chars (~1000 tokens)
37
+ * @returns {string} — skeleton text for LLM context
38
+ */
39
+ export function buildProjectSkeleton(projectDir, { maxFiles = 200, maxChars = 4000 } = {}) {
40
+ const files = scanFiles(projectDir, 0, maxFiles);
41
+ if (files.length === 0) return '';
42
+
43
+ const parts = [];
44
+ parts.push(`Project: ${path.basename(projectDir)} (${files.length} source files)`);
45
+ parts.push('');
46
+
47
+ // Group by directory
48
+ const dirs = new Map();
49
+ for (const f of files) {
50
+ const rel = path.relative(projectDir, f);
51
+ const dir = path.dirname(rel);
52
+ if (!dirs.has(dir)) dirs.set(dir, []);
53
+ dirs.get(dir).push(rel);
54
+ }
55
+
56
+ // File tree
57
+ parts.push('## File Tree');
58
+ for (const [dir, dirFiles] of dirs) {
59
+ parts.push(`${dir}/`);
60
+ for (const f of dirFiles) {
61
+ parts.push(` ${path.basename(f)}`);
62
+ }
63
+ }
64
+
65
+ // Key signatures (top-level functions/classes)
66
+ parts.push('');
67
+ parts.push('## Key Signatures');
68
+
69
+ let sigCount = 0;
70
+ for (const f of files) {
71
+ if (sigCount > 50) break; // Cap signatures
72
+ try {
73
+ const content = fs.readFileSync(f, 'utf-8');
74
+ const rel = path.relative(projectDir, f);
75
+ const sigs = extractSignatures(content);
76
+ if (sigs.length > 0) {
77
+ parts.push(`${rel}: ${sigs.join(', ')}`);
78
+ sigCount += sigs.length;
79
+ }
80
+ } catch { /* skip */ }
81
+ }
82
+
83
+ let skeleton = parts.join('\n');
84
+ if (skeleton.length > maxChars) {
85
+ skeleton = skeleton.slice(0, maxChars) + '\n... (truncated)';
86
+ }
87
+ return skeleton;
88
+ }
89
+
90
+ /**
91
+ * Extract function/class names from source code via regex.
92
+ */
93
+ function extractSignatures(content) {
94
+ const names = new Set();
95
+ for (const pattern of SIGNATURE_PATTERNS) {
96
+ pattern.lastIndex = 0;
97
+ let match;
98
+ while ((match = pattern.exec(content)) !== null) {
99
+ // Take the first non-null capture group
100
+ const name = match[1] || match[2] || match[3];
101
+ if (name && !name.startsWith('_')) names.add(name);
102
+ }
103
+ }
104
+ return Array.from(names).slice(0, 10);
105
+ }
106
+
107
+ /**
108
+ * Scan project for source files.
109
+ */
110
+ function scanFiles(dir, depth = 0, maxFiles = 200) {
111
+ if (depth > 10) return [];
112
+ const results = [];
113
+ let entries;
114
+ try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return []; }
115
+
116
+ for (const entry of entries) {
117
+ if (results.length >= maxFiles) break;
118
+ if (entry.name.startsWith('.') || IGNORED_DIRS.has(entry.name)) continue;
119
+
120
+ const fullPath = path.join(dir, entry.name);
121
+ if (entry.isDirectory()) {
122
+ results.push(...scanFiles(fullPath, depth + 1, maxFiles - results.length));
123
+ } else if (entry.isFile()) {
124
+ const ext = path.extname(entry.name);
125
+ if (!CODE_EXTS.has(ext)) continue;
126
+ try {
127
+ const stat = fs.statSync(fullPath);
128
+ if (stat.size > MAX_FILE_SIZE) continue;
129
+ } catch { continue; }
130
+ results.push(fullPath);
131
+ }
132
+ }
133
+ return results;
134
+ }
@@ -0,0 +1,480 @@
1
+ /**
2
+ * Agent Loop — async generator yielding 13 event types.
3
+ * Handles streaming, tool calls, thinking, auto-compaction, hooks, multi-provider.
4
+ */
5
+ import { streamResponse, accumulateStream } from './streaming.mjs';
6
+ import { ContextManager } from './context-manager.mjs';
7
+ import { buildSystemPrompt } from './system-prompt.mjs';
8
+ import { createStagnationTracker, stagnationMessage } from './stagnation.mjs';
9
+ import fs from 'fs';
10
+ import path from 'path';
11
+ export function createAgentLoop({ model, tools, permissions, settings, hooks }) {
12
+ const contextManager = new ContextManager(settings.maxContextTokens || 180000);
13
+
14
+ // Build system prompt using the new builder
15
+ const promptResult = buildSystemPrompt({
16
+ cwd: process.cwd(),
17
+ tools: tools.list?.() || [],
18
+ override: settings.systemPromptOverride,
19
+ addDirs: settings.addDirs,
20
+ });
21
+
22
+ const state = {
23
+ messages: [],
24
+ systemPrompt: promptResult.full,
25
+ turnCount: 0,
26
+ tokenUsage: { input: 0, output: 0 },
27
+ model,
28
+ tools,
29
+ _contextManager: contextManager,
30
+ };
31
+ const stagnation = createStagnationTracker({
32
+ enabled: settings.stagnationDetection === true,
33
+ threshold: settings.stagnationThreshold,
34
+ });
35
+
36
+ async function* run(userMessage, options = {}) {
37
+ // Add user message (skip for continuation turns)
38
+ if (userMessage && !options.continuation) {
39
+ state.messages = contextManager.addMessage(state.messages, {
40
+ role: 'user',
41
+ content: userMessage,
42
+ });
43
+ state.turnCount++;
44
+ }
45
+
46
+ // Check max turns
47
+ if (settings.maxTurns && state.turnCount > settings.maxTurns) {
48
+ yield { type: 'error', message: `Max turns (${settings.maxTurns}) reached.` };
49
+ yield { type: 'stop', reason: 'max_turns' };
50
+ return;
51
+ }
52
+
53
+ // Auto-compact if needed
54
+ if (contextManager.shouldCompact(state.messages)) {
55
+ yield { type: 'compaction', count: contextManager.compactionCount + 1 };
56
+ state.messages = contextManager.compact(state.messages);
57
+ }
58
+
59
+ yield { type: 'stream_request_start', turn: state.turnCount };
60
+
61
+ // Detect provider and call API
62
+ const provider = detectProvider(model);
63
+ let response;
64
+
65
+ try {
66
+ if (settings.stream !== false) {
67
+ // Streaming mode
68
+ response = await callApiStreaming(provider, model, state, tools.list(), settings);
69
+ const collectedContent = [];
70
+ let currentText = '';
71
+ let currentThinking = '';
72
+
73
+ for await (const event of response.events) {
74
+ if (event.type === 'content_block_start') {
75
+ if (event.content_block?.type === 'thinking') {
76
+ currentThinking = '';
77
+ }
78
+ } else if (event.type === 'content_block_delta') {
79
+ if (event.delta?.type === 'text_delta') {
80
+ currentText += event.delta.text;
81
+ yield { type: 'stream_event', text: event.delta.text };
82
+ } else if (event.delta?.type === 'thinking_delta') {
83
+ currentThinking += event.delta.thinking;
84
+ yield { type: 'thinking', text: event.delta.thinking };
85
+ }
86
+ } else if (event.type === 'ping') {
87
+ // Keepalive, ignore
88
+ }
89
+ }
90
+
91
+ // Use the accumulated message
92
+ response = response.accumulated;
93
+ } else {
94
+ // Non-streaming mode
95
+ response = await callApi(provider, model, state, tools.list(), settings);
96
+ }
97
+ } catch (err) {
98
+ yield { type: 'error', message: err.message };
99
+ return;
100
+ }
101
+
102
+ // Track token usage
103
+ if (response.usage) {
104
+ state.tokenUsage.input += response.usage.input_tokens || 0;
105
+ state.tokenUsage.output += response.usage.output_tokens || 0;
106
+ }
107
+
108
+ // Build assistant message for history
109
+ const assistantMessage = { role: 'assistant', content: response.content };
110
+ state.messages.push(assistantMessage);
111
+
112
+ // Process content blocks
113
+ const toolUseBlocks = [];
114
+
115
+ for (const block of response.content || []) {
116
+ if (block.type === 'text') {
117
+ yield { type: 'assistant', content: block.text };
118
+ }
119
+
120
+ if (block.type === 'thinking') {
121
+ yield { type: 'thinking_complete', thinking: block.thinking };
122
+ }
123
+
124
+ if (block.type === 'tool_use') {
125
+ toolUseBlocks.push(block);
126
+ }
127
+ }
128
+
129
+ // Process tool calls
130
+ if (toolUseBlocks.length > 0) {
131
+ const toolResults = [];
132
+
133
+ for (const block of toolUseBlocks) {
134
+ // Only consecutive identical calls indicate a loop. The same read or
135
+ // validation later in a task can be legitimate progress verification.
136
+ const stagnationResult = stagnation.record(block.name, block.input);
137
+ if (stagnationResult.detected) {
138
+ yield { type: 'stagnation', tool: block.name, count: stagnationResult.count };
139
+ toolResults.push({
140
+ type: 'tool_result',
141
+ tool_use_id: block.id,
142
+ content: stagnationMessage(block.name, stagnationResult.count),
143
+ });
144
+ continue;
145
+ }
146
+
147
+ // Run pre-tool hooks
148
+ if (hooks) {
149
+ const hookResult = await hooks.runPreToolUse(block.name, block.input);
150
+ if (!hookResult.allow) {
151
+ yield { type: 'hookPermissionResult', tool: block.name, allowed: false, message: hookResult.message };
152
+ toolResults.push({
153
+ type: 'tool_result',
154
+ tool_use_id: block.id,
155
+ content: `Blocked by hook: ${hookResult.message}`,
156
+ });
157
+ continue;
158
+ }
159
+ }
160
+
161
+ // Check permission
162
+ const allowed = await permissions.check(block.name, block.input);
163
+ if (!allowed) {
164
+ yield { type: 'hookPermissionResult', tool: block.name, allowed: false };
165
+ toolResults.push({
166
+ type: 'tool_result',
167
+ tool_use_id: block.id,
168
+ content: 'Permission denied',
169
+ });
170
+ continue;
171
+ }
172
+
173
+ // Execute tool
174
+ yield { type: 'tool_progress', tool: block.name, status: 'running' };
175
+
176
+ let result;
177
+ try {
178
+ result = await tools.call(block.name, block.input);
179
+ } catch (err) {
180
+ result = `Tool error: ${err.message}`;
181
+ }
182
+
183
+ // Run post-tool hooks
184
+ if (hooks) {
185
+ result = await hooks.runPostToolUse(block.name, result);
186
+ }
187
+
188
+ yield { type: 'result', tool: block.name, result };
189
+
190
+ toolResults.push({
191
+ type: 'tool_result',
192
+ tool_use_id: block.id,
193
+ content: typeof result === 'string' ? result : JSON.stringify(result),
194
+ });
195
+ }
196
+
197
+ // Add tool results as a single user message
198
+ state.messages.push({ role: 'user', content: toolResults });
199
+
200
+ // Recursive: continue the loop after tool execution
201
+ yield* run(null, { continuation: true });
202
+ return;
203
+ }
204
+
205
+ // No tool calls — check stop hooks
206
+ if (hooks) {
207
+ const allowStop = await hooks.runStop();
208
+ if (!allowStop) {
209
+ // Hook prevented stopping — continue with a nudge
210
+ state.messages = contextManager.addMessage(state.messages, {
211
+ role: 'user',
212
+ content: '[System: A hook prevented stopping. Please continue with the task.]',
213
+ });
214
+ yield* run(null, { continuation: true });
215
+ return;
216
+ }
217
+ }
218
+
219
+ yield { type: 'stop', reason: response.stop_reason || 'end_turn' };
220
+ }
221
+
222
+ return { run, state };
223
+ }
224
+
225
+ function detectProvider(model) {
226
+ if (model.startsWith('gpt-') || model.startsWith('o1') || model.startsWith('o3')) return 'openai';
227
+ if (model.startsWith('gemini')) return 'google';
228
+ return 'anthropic';
229
+ }
230
+
231
+ async function callApi(provider, model, state, toolDefs, settings) {
232
+ const callers = { anthropic: callAnthropic, openai: callOpenAI, google: callGoogle };
233
+ const caller = callers[provider] || callers.anthropic;
234
+ return caller(model, state, toolDefs, settings, false);
235
+ }
236
+
237
+ async function callApiStreaming(provider, model, state, toolDefs, settings) {
238
+ const callers = { anthropic: callAnthropic, openai: callOpenAI, google: callGoogle };
239
+ const caller = callers[provider] || callers.anthropic;
240
+ return caller(model, state, toolDefs, settings, true);
241
+ }
242
+
243
+ async function callAnthropic(model, state, toolDefs, settings, stream) {
244
+ const apiKey = process.env.ANTHROPIC_API_KEY;
245
+ if (!apiKey) throw new Error('ANTHROPIC_API_KEY not set');
246
+
247
+ const body = {
248
+ model,
249
+ max_tokens: settings.maxTokens || 16384,
250
+ messages: state.messages,
251
+ ...(state.systemPrompt && { system: state.systemPrompt }),
252
+ ...(toolDefs.length > 0 && { tools: toolDefs }),
253
+ ...(stream && { stream: true }),
254
+ };
255
+
256
+ // Enable extended thinking if model supports it
257
+ if (model.includes('opus') || settings.thinking) {
258
+ body.thinking = { type: 'enabled', budget_tokens: settings.thinkingBudget || 10000 };
259
+ }
260
+
261
+ const res = await fetch('https://api.anthropic.com/v1/messages', {
262
+ method: 'POST',
263
+ headers: {
264
+ 'Content-Type': 'application/json',
265
+ 'x-api-key': apiKey,
266
+ 'anthropic-version': '2023-06-01',
267
+ },
268
+ body: JSON.stringify(body),
269
+ });
270
+
271
+ if (!res.ok) {
272
+ const err = await res.text();
273
+ throw new Error(`Anthropic API error ${res.status}: ${err}`);
274
+ }
275
+
276
+ if (stream) {
277
+ const collected = [];
278
+ const eventGenerator = async function* () {
279
+ for await (const event of streamResponse(res)) {
280
+ collected.push(event);
281
+ yield event;
282
+ }
283
+ };
284
+ return {
285
+ events: eventGenerator(),
286
+ get accumulated() {
287
+ return accumulateFromCollected(collected);
288
+ },
289
+ };
290
+ }
291
+
292
+ return res.json();
293
+ }
294
+
295
+ async function callOpenAI(model, state, toolDefs, settings, stream) {
296
+ const apiKey = process.env.OPENAI_API_KEY;
297
+ if (!apiKey) throw new Error('OPENAI_API_KEY not set');
298
+
299
+ const messages = [];
300
+ if (state.systemPrompt) {
301
+ messages.push({ role: 'system', content: state.systemPrompt });
302
+ }
303
+ for (const msg of state.messages) {
304
+ if (typeof msg.content === 'string') {
305
+ messages.push({ role: msg.role, content: msg.content });
306
+ } else if (Array.isArray(msg.content)) {
307
+ for (const block of msg.content) {
308
+ if (block.type === 'tool_result') {
309
+ messages.push({
310
+ role: 'tool',
311
+ tool_call_id: block.tool_use_id,
312
+ content: block.content,
313
+ });
314
+ }
315
+ }
316
+ }
317
+ }
318
+
319
+ const tools = toolDefs.map(t => ({
320
+ type: 'function',
321
+ function: { name: t.name, description: t.description, parameters: t.input_schema },
322
+ }));
323
+
324
+ const body = {
325
+ model,
326
+ messages,
327
+ ...(tools.length > 0 && { tools }),
328
+ };
329
+
330
+ const baseUrl = process.env.OPENAI_BASE_URL || 'https://api.openai.com/v1';
331
+ const res = await fetch(`${baseUrl}/chat/completions`, {
332
+ method: 'POST',
333
+ headers: {
334
+ 'Content-Type': 'application/json',
335
+ 'Authorization': `Bearer ${apiKey}`,
336
+ },
337
+ body: JSON.stringify(body),
338
+ });
339
+
340
+ if (!res.ok) {
341
+ const err = await res.text();
342
+ throw new Error(`OpenAI API error ${res.status}: ${err}`);
343
+ }
344
+
345
+ const data = await res.json();
346
+ return convertOpenAIResponse(data);
347
+ }
348
+
349
+ async function callGoogle(model, state, toolDefs, settings, stream) {
350
+ const apiKey = process.env.GOOGLE_API_KEY || process.env.GEMINI_API_KEY;
351
+ if (!apiKey) throw new Error('GOOGLE_API_KEY or GEMINI_API_KEY not set');
352
+
353
+ const contents = [];
354
+ for (const msg of state.messages) {
355
+ const role = msg.role === 'assistant' ? 'model' : 'user';
356
+ if (typeof msg.content === 'string') {
357
+ contents.push({ role, parts: [{ text: msg.content }] });
358
+ }
359
+ }
360
+
361
+ const body = {
362
+ contents,
363
+ ...(state.systemPrompt && {
364
+ systemInstruction: { parts: [{ text: state.systemPrompt }] },
365
+ }),
366
+ };
367
+
368
+ const res = await fetch(
369
+ `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${apiKey}`,
370
+ {
371
+ method: 'POST',
372
+ headers: { 'Content-Type': 'application/json' },
373
+ body: JSON.stringify(body),
374
+ }
375
+ );
376
+
377
+ if (!res.ok) {
378
+ const err = await res.text();
379
+ throw new Error(`Google API error ${res.status}: ${err}`);
380
+ }
381
+
382
+ const data = await res.json();
383
+ return convertGoogleResponse(data);
384
+ }
385
+
386
+ function convertOpenAIResponse(data) {
387
+ const choice = data.choices?.[0];
388
+ if (!choice) throw new Error('No choices in OpenAI response');
389
+
390
+ const content = [];
391
+ if (choice.message?.content) {
392
+ content.push({ type: 'text', text: choice.message.content });
393
+ }
394
+
395
+ if (choice.message?.tool_calls) {
396
+ for (const tc of choice.message.tool_calls) {
397
+ content.push({
398
+ type: 'tool_use',
399
+ id: tc.id,
400
+ name: tc.function.name,
401
+ input: JSON.parse(tc.function.arguments || '{}'),
402
+ });
403
+ }
404
+ }
405
+
406
+ return {
407
+ content,
408
+ stop_reason: choice.finish_reason === 'stop' ? 'end_turn' : choice.finish_reason,
409
+ usage: {
410
+ input_tokens: data.usage?.prompt_tokens || 0,
411
+ output_tokens: data.usage?.completion_tokens || 0,
412
+ },
413
+ };
414
+ }
415
+
416
+ function convertGoogleResponse(data) {
417
+ const candidate = data.candidates?.[0];
418
+ if (!candidate) throw new Error('No candidates in Google response');
419
+
420
+ const content = [];
421
+ for (const part of candidate.content?.parts || []) {
422
+ if (part.text) content.push({ type: 'text', text: part.text });
423
+ }
424
+
425
+ return {
426
+ content,
427
+ stop_reason: 'end_turn',
428
+ usage: {
429
+ input_tokens: data.usageMetadata?.promptTokenCount || 0,
430
+ output_tokens: data.usageMetadata?.candidatesTokenCount || 0,
431
+ },
432
+ };
433
+ }
434
+
435
+ function accumulateFromCollected(events) {
436
+ const message = {
437
+ content: [],
438
+ stop_reason: null,
439
+ usage: { input_tokens: 0, output_tokens: 0 },
440
+ };
441
+
442
+ let currentBlock = null;
443
+
444
+ for (const event of events) {
445
+ switch (event.type) {
446
+ case 'message_start':
447
+ if (event.message?.usage) {
448
+ message.usage.input_tokens = event.message.usage.input_tokens || 0;
449
+ }
450
+ break;
451
+ case 'content_block_start':
452
+ currentBlock = { ...event.content_block };
453
+ if (currentBlock.type === 'text') currentBlock.text = '';
454
+ if (currentBlock.type === 'thinking') currentBlock.thinking = '';
455
+ if (currentBlock.type === 'tool_use') currentBlock.input = '';
456
+ message.content.push(currentBlock);
457
+ break;
458
+ case 'content_block_delta':
459
+ if (!currentBlock) break;
460
+ if (event.delta?.type === 'text_delta') currentBlock.text += event.delta.text;
461
+ else if (event.delta?.type === 'thinking_delta') currentBlock.thinking += event.delta.thinking;
462
+ else if (event.delta?.type === 'input_json_delta') currentBlock.input += event.delta.partial_json;
463
+ break;
464
+ case 'content_block_stop':
465
+ if (currentBlock?.type === 'tool_use' && typeof currentBlock.input === 'string') {
466
+ try { currentBlock.input = JSON.parse(currentBlock.input || '{}'); } catch { currentBlock.input = {}; }
467
+ }
468
+ currentBlock = null;
469
+ break;
470
+ case 'message_delta':
471
+ if (event.delta?.stop_reason) message.stop_reason = event.delta.stop_reason;
472
+ if (event.usage) message.usage.output_tokens = event.usage.output_tokens || 0;
473
+ break;
474
+ case 'ping':
475
+ break;
476
+ }
477
+ }
478
+
479
+ return message;
480
+ }