@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.
- package/README.md +82 -0
- package/package.json +36 -4
- package/pulse/app/activity/page.tsx +190 -0
- package/pulse/app/api/activity/route.ts +138 -0
- package/pulse/app/api/costs/route.ts +88 -0
- package/pulse/app/api/export/route.ts +77 -0
- package/pulse/app/api/history/route.ts +11 -0
- package/pulse/app/api/import/route.ts +31 -0
- package/pulse/app/api/memory/route.ts +52 -0
- package/pulse/app/api/plans/route.ts +9 -0
- package/pulse/app/api/projects/[slug]/route.ts +96 -0
- package/pulse/app/api/projects/route.ts +121 -0
- package/pulse/app/api/sessions/[id]/replay/route.ts +20 -0
- package/pulse/app/api/sessions/[id]/route.ts +31 -0
- package/pulse/app/api/sessions/route.ts +112 -0
- package/pulse/app/api/settings/route.ts +14 -0
- package/pulse/app/api/stats/route.ts +143 -0
- package/pulse/app/api/todos/route.ts +9 -0
- package/pulse/app/api/tools/route.ts +160 -0
- package/pulse/app/costs/page.tsx +179 -0
- package/pulse/app/export/page.tsx +465 -0
- package/pulse/app/favicon.ico +0 -0
- package/pulse/app/globals.css +263 -0
- package/pulse/app/help/page.tsx +142 -0
- package/pulse/app/history/page.tsx +157 -0
- package/pulse/app/layout.tsx +46 -0
- package/pulse/app/memory/page.tsx +365 -0
- package/pulse/app/overview-client.tsx +393 -0
- package/pulse/app/page.tsx +14 -0
- package/pulse/app/plans/page.tsx +308 -0
- package/pulse/app/projects/[slug]/page.tsx +390 -0
- package/pulse/app/projects/page.tsx +110 -0
- package/pulse/app/sessions/[id]/page.tsx +243 -0
- package/pulse/app/sessions/page.tsx +39 -0
- package/pulse/app/settings/page.tsx +188 -0
- package/pulse/app/todos/page.tsx +211 -0
- package/pulse/app/tools/page.tsx +249 -0
- package/pulse/cli.js +159 -0
- package/pulse/components/activity/day-of-week-chart.tsx +35 -0
- package/pulse/components/activity/streak-card.tsx +36 -0
- package/pulse/components/costs/cache-efficiency-panel.tsx +76 -0
- package/pulse/components/costs/cost-by-project-chart.tsx +48 -0
- package/pulse/components/costs/cost-over-time-chart.tsx +95 -0
- package/pulse/components/costs/model-token-table.tsx +60 -0
- package/pulse/components/global-search.tsx +193 -0
- package/pulse/components/keyboard-nav-provider.tsx +23 -0
- package/pulse/components/layout/bottom-nav.tsx +52 -0
- package/pulse/components/layout/client-layout.tsx +31 -0
- package/pulse/components/layout/sidebar-context.tsx +50 -0
- package/pulse/components/layout/sidebar.tsx +182 -0
- package/pulse/components/layout/top-bar.tsx +121 -0
- package/pulse/components/overview/activity-heatmap.tsx +107 -0
- package/pulse/components/overview/conversation-table.tsx +148 -0
- package/pulse/components/overview/model-breakdown-donut.tsx +95 -0
- package/pulse/components/overview/peak-hours-chart.tsx +87 -0
- package/pulse/components/overview/project-activity-donut.tsx +96 -0
- package/pulse/components/overview/stat-card.tsx +102 -0
- package/pulse/components/overview/usage-over-time-chart.tsx +166 -0
- package/pulse/components/projects/project-card.tsx +175 -0
- package/pulse/components/sessions/replay/assistant-markdown.tsx +94 -0
- package/pulse/components/sessions/replay/compaction-card.tsx +25 -0
- package/pulse/components/sessions/replay/session-sidebar.tsx +231 -0
- package/pulse/components/sessions/replay/token-accumulation-chart.tsx +98 -0
- package/pulse/components/sessions/replay/tool-call-badge.tsx +127 -0
- package/pulse/components/sessions/replay/turn-cards.tsx +220 -0
- package/pulse/components/sessions/replay/user-tool-result.tsx +158 -0
- package/pulse/components/sessions/session-badges.tsx +49 -0
- package/pulse/components/sessions/session-table.tsx +299 -0
- package/pulse/components/theme-provider.tsx +44 -0
- package/pulse/components/tools/feature-adoption-table.tsx +58 -0
- package/pulse/components/tools/mcp-server-panel.tsx +45 -0
- package/pulse/components/tools/tool-ranking-chart.tsx +57 -0
- package/pulse/components/tools/version-history-table.tsx +32 -0
- package/pulse/components/ui/alert.tsx +66 -0
- package/pulse/components/ui/badge.tsx +48 -0
- package/pulse/components/ui/breadcrumb.tsx +109 -0
- package/pulse/components/ui/button.tsx +64 -0
- package/pulse/components/ui/calendar.tsx +220 -0
- package/pulse/components/ui/card.tsx +92 -0
- package/pulse/components/ui/command.tsx +158 -0
- package/pulse/components/ui/dialog.tsx +158 -0
- package/pulse/components/ui/input.tsx +21 -0
- package/pulse/components/ui/popover.tsx +89 -0
- package/pulse/components/ui/progress.tsx +31 -0
- package/pulse/components/ui/select.tsx +190 -0
- package/pulse/components/ui/separator.tsx +28 -0
- package/pulse/components/ui/sheet.tsx +143 -0
- package/pulse/components/ui/skeleton.tsx +13 -0
- package/pulse/components/ui/table.tsx +116 -0
- package/pulse/components/ui/tabs.tsx +91 -0
- package/pulse/components/ui/tooltip.tsx +57 -0
- package/pulse/components/use-global-keyboard-nav.ts +79 -0
- package/pulse/components.json +23 -0
- package/pulse/eslint.config.mjs +18 -0
- package/pulse/lib/claude-reader.ts +594 -0
- package/pulse/lib/decode.ts +129 -0
- package/pulse/lib/pricing.ts +102 -0
- package/pulse/lib/replay-parser.ts +165 -0
- package/pulse/lib/tool-categories.ts +127 -0
- package/pulse/lib/utils.ts +6 -0
- package/pulse/next-env.d.ts +6 -0
- package/pulse/next.config.ts +16 -0
- package/pulse/package.json +45 -0
- package/pulse/postcss.config.mjs +7 -0
- package/pulse/public/activity.png +0 -0
- package/pulse/public/cc-lens.png +0 -0
- package/pulse/public/command-k.png +0 -0
- package/pulse/public/costs.png +0 -0
- package/pulse/public/dashboard-dark.png +0 -0
- package/pulse/public/dashboard-white.png +0 -0
- package/pulse/public/export.png +0 -0
- package/pulse/public/file.svg +1 -0
- package/pulse/public/globe.svg +1 -0
- package/pulse/public/next.svg +1 -0
- package/pulse/public/projects.png +0 -0
- package/pulse/public/session-chat.png +0 -0
- package/pulse/public/todos.png +0 -0
- package/pulse/public/tools.png +0 -0
- package/pulse/public/vercel.svg +1 -0
- package/pulse/public/window.svg +1 -0
- package/pulse/tsconfig.json +34 -0
- package/pulse/types/claude.ts +294 -0
- package/src/agents/loader.mjs +89 -0
- package/src/agents/parser.mjs +98 -0
- package/src/agents/teams.mjs +123 -0
- package/src/auth/oauth.mjs +220 -0
- package/src/auth/tarang-auth.mjs +277 -0
- package/src/config/cli-args.mjs +173 -0
- package/src/config/env.mjs +263 -0
- package/src/config/settings.mjs +132 -0
- package/src/context/ast-parser.mjs +298 -0
- package/src/context/bm25.mjs +85 -0
- package/src/context/retriever.mjs +270 -0
- package/src/context/skeleton.mjs +134 -0
- package/src/core/agent-loop.mjs +480 -0
- package/src/core/approval.mjs +273 -0
- package/src/core/backend-url.mjs +57 -0
- package/src/core/cache.mjs +105 -0
- package/src/core/callback-client.mjs +149 -0
- package/src/core/checkpoints.mjs +142 -0
- package/src/core/context-manager.mjs +198 -0
- package/src/core/headless.mjs +168 -0
- package/src/core/hooks-manager.mjs +87 -0
- package/src/core/jsonl-writer.mjs +351 -0
- package/src/core/local-agent.mjs +429 -0
- package/src/core/local-store.mjs +325 -0
- package/src/core/mode-selector.mjs +51 -0
- package/src/core/output-filter.mjs +177 -0
- package/src/core/paths.mjs +101 -0
- package/src/core/pricing.mjs +314 -0
- package/src/core/providers.mjs +219 -0
- package/src/core/rate-limiter.mjs +119 -0
- package/src/core/safety.mjs +200 -0
- package/src/core/scheduler.mjs +173 -0
- package/src/core/session-manager.mjs +317 -0
- package/src/core/session.mjs +143 -0
- package/src/core/settings-sync.mjs +85 -0
- package/src/core/stagnation.mjs +57 -0
- package/src/core/stream-client.mjs +367 -0
- package/src/core/streaming.mjs +182 -0
- package/src/core/system-prompt.mjs +135 -0
- package/src/core/tool-executor.mjs +725 -0
- package/src/hooks/engine.mjs +162 -0
- package/src/index.mjs +370 -0
- package/src/mcp/client.mjs +253 -0
- package/src/mcp/transport-shttp.mjs +130 -0
- package/src/mcp/transport-sse.mjs +131 -0
- package/src/mcp/transport-ws.mjs +134 -0
- package/src/permissions/checker.mjs +57 -0
- package/src/permissions/command-classifier.mjs +573 -0
- package/src/permissions/injection-check.mjs +60 -0
- package/src/permissions/path-check.mjs +102 -0
- package/src/permissions/prompt.mjs +73 -0
- package/src/permissions/sandbox.mjs +112 -0
- package/src/plugins/loader.mjs +138 -0
- package/src/skills/loader.mjs +147 -0
- package/src/skills/runner.mjs +55 -0
- package/src/telemetry/index.mjs +96 -0
- package/src/terminal/agents.mjs +177 -0
- package/src/terminal/analytics.mjs +292 -0
- package/src/terminal/ansi.mjs +421 -0
- package/src/terminal/main.mjs +150 -0
- package/src/terminal/repl.mjs +1484 -0
- package/src/terminal/tool-display.mjs +58 -0
- package/src/tools/agent.mjs +137 -0
- package/src/tools/ask-user.mjs +61 -0
- package/src/tools/bash.mjs +148 -0
- package/src/tools/cron-create.mjs +120 -0
- package/src/tools/cron-delete.mjs +49 -0
- package/src/tools/cron-list.mjs +37 -0
- package/src/tools/edit.mjs +82 -0
- package/src/tools/enter-worktree.mjs +69 -0
- package/src/tools/exit-worktree.mjs +57 -0
- package/src/tools/glob.mjs +117 -0
- package/src/tools/grep.mjs +129 -0
- package/src/tools/lint.mjs +71 -0
- package/src/tools/ls.mjs +58 -0
- package/src/tools/lsp.mjs +115 -0
- package/src/tools/multi-edit.mjs +94 -0
- package/src/tools/notebook-edit.mjs +96 -0
- package/src/tools/read-mcp-resource.mjs +57 -0
- package/src/tools/read.mjs +138 -0
- package/src/tools/registry.mjs +132 -0
- package/src/tools/remote-trigger.mjs +84 -0
- package/src/tools/send-message.mjs +64 -0
- package/src/tools/skill.mjs +52 -0
- package/src/tools/test-runner.mjs +49 -0
- package/src/tools/todo-write.mjs +68 -0
- package/src/tools/tool-search.mjs +77 -0
- package/src/tools/web-fetch.mjs +65 -0
- package/src/tools/web-search.mjs +89 -0
- package/src/tools/write.mjs +55 -0
- package/src/ui/banner.mjs +237 -0
- package/src/ui/commands.mjs +499 -0
- package/src/ui/formatter.mjs +379 -0
- package/src/ui/markdown.mjs +278 -0
- package/src/ui/slash-commands.mjs +258 -0
- package/index.js +0 -1
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming Handler — processes Anthropic SSE events from the Messages API.
|
|
3
|
+
*
|
|
4
|
+
* Handles ALL SSE event types:
|
|
5
|
+
* - message_start, message_delta, message_stop
|
|
6
|
+
* - content_block_start, content_block_delta, content_block_stop
|
|
7
|
+
* - ping
|
|
8
|
+
* - error
|
|
9
|
+
*
|
|
10
|
+
* Parses:
|
|
11
|
+
* - thinking blocks (type: "thinking")
|
|
12
|
+
* - tool_use input streaming (type: "input_json_delta")
|
|
13
|
+
* - Usage tracking from message_delta.usage
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Parse an SSE stream from an Anthropic streaming response.
|
|
18
|
+
* @param {Response} response - fetch Response with streaming body
|
|
19
|
+
* @yields {object} Parsed SSE event data
|
|
20
|
+
*/
|
|
21
|
+
export async function* streamResponse(response) {
|
|
22
|
+
const reader = response.body.getReader();
|
|
23
|
+
const decoder = new TextDecoder();
|
|
24
|
+
let buffer = '';
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
while (true) {
|
|
28
|
+
const { done, value } = await reader.read();
|
|
29
|
+
if (done) break;
|
|
30
|
+
|
|
31
|
+
buffer += decoder.decode(value, { stream: true });
|
|
32
|
+
|
|
33
|
+
// SSE events are separated by double newlines
|
|
34
|
+
while (buffer.includes('\n\n')) {
|
|
35
|
+
const idx = buffer.indexOf('\n\n');
|
|
36
|
+
const chunk = buffer.slice(0, idx);
|
|
37
|
+
buffer = buffer.slice(idx + 2);
|
|
38
|
+
|
|
39
|
+
const event = parseSSEChunk(chunk);
|
|
40
|
+
if (event) yield event;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Handle remaining buffer
|
|
45
|
+
if (buffer.trim()) {
|
|
46
|
+
const event = parseSSEChunk(buffer.trim());
|
|
47
|
+
if (event) yield event;
|
|
48
|
+
}
|
|
49
|
+
} finally {
|
|
50
|
+
reader.releaseLock();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Parse a single SSE chunk into an event object.
|
|
56
|
+
* @param {string} chunk - raw SSE text (may contain event: and data: lines)
|
|
57
|
+
* @returns {object|null} Parsed event or null
|
|
58
|
+
*/
|
|
59
|
+
function parseSSEChunk(chunk) {
|
|
60
|
+
let eventType = null;
|
|
61
|
+
let dataLines = [];
|
|
62
|
+
|
|
63
|
+
for (const line of chunk.split('\n')) {
|
|
64
|
+
if (line.startsWith('event: ')) {
|
|
65
|
+
eventType = line.slice(7).trim();
|
|
66
|
+
} else if (line.startsWith('data: ')) {
|
|
67
|
+
dataLines.push(line.slice(6));
|
|
68
|
+
} else if (line.startsWith(':')) {
|
|
69
|
+
// SSE comment, ignore
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Handle ping events (no data)
|
|
75
|
+
if (eventType === 'ping') {
|
|
76
|
+
return { type: 'ping' };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (dataLines.length === 0) return null;
|
|
80
|
+
|
|
81
|
+
const raw = dataLines.join('\n');
|
|
82
|
+
if (raw === '[DONE]') return { type: 'done' };
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
const data = JSON.parse(raw);
|
|
86
|
+
return { type: eventType || data.type || 'unknown', ...data };
|
|
87
|
+
} catch {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Accumulate streaming events into a complete message response.
|
|
94
|
+
* Collects content blocks, thinking blocks, and usage stats.
|
|
95
|
+
*
|
|
96
|
+
* @param {AsyncIterable} events - stream of SSE events
|
|
97
|
+
* @returns {object} Complete message in the same shape as non-streaming API
|
|
98
|
+
*/
|
|
99
|
+
export async function accumulateStream(events) {
|
|
100
|
+
const message = {
|
|
101
|
+
id: null,
|
|
102
|
+
role: 'assistant',
|
|
103
|
+
content: [],
|
|
104
|
+
model: null,
|
|
105
|
+
stop_reason: null,
|
|
106
|
+
usage: { input_tokens: 0, output_tokens: 0, cache_creation_input_tokens: 0, cache_read_input_tokens: 0 },
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
let currentBlock = null;
|
|
110
|
+
let blockIndex = -1;
|
|
111
|
+
|
|
112
|
+
for await (const event of events) {
|
|
113
|
+
switch (event.type) {
|
|
114
|
+
case 'message_start':
|
|
115
|
+
if (event.message) {
|
|
116
|
+
message.id = event.message.id;
|
|
117
|
+
message.model = event.message.model;
|
|
118
|
+
if (event.message.usage) {
|
|
119
|
+
message.usage.input_tokens = event.message.usage.input_tokens || 0;
|
|
120
|
+
message.usage.cache_creation_input_tokens = event.message.usage.cache_creation_input_tokens || 0;
|
|
121
|
+
message.usage.cache_read_input_tokens = event.message.usage.cache_read_input_tokens || 0;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
break;
|
|
125
|
+
|
|
126
|
+
case 'content_block_start':
|
|
127
|
+
blockIndex = event.index ?? message.content.length;
|
|
128
|
+
currentBlock = { ...event.content_block };
|
|
129
|
+
if (currentBlock.type === 'text') currentBlock.text = '';
|
|
130
|
+
if (currentBlock.type === 'thinking') currentBlock.thinking = '';
|
|
131
|
+
if (currentBlock.type === 'tool_use') {
|
|
132
|
+
currentBlock.input = '';
|
|
133
|
+
}
|
|
134
|
+
message.content[blockIndex] = currentBlock;
|
|
135
|
+
break;
|
|
136
|
+
|
|
137
|
+
case 'content_block_delta':
|
|
138
|
+
if (!currentBlock) break;
|
|
139
|
+
if (event.delta?.type === 'text_delta') {
|
|
140
|
+
currentBlock.text += event.delta.text;
|
|
141
|
+
} else if (event.delta?.type === 'thinking_delta') {
|
|
142
|
+
currentBlock.thinking += event.delta.thinking;
|
|
143
|
+
} else if (event.delta?.type === 'input_json_delta') {
|
|
144
|
+
currentBlock.input += event.delta.partial_json;
|
|
145
|
+
}
|
|
146
|
+
break;
|
|
147
|
+
|
|
148
|
+
case 'content_block_stop':
|
|
149
|
+
// Parse tool_use input from accumulated JSON string
|
|
150
|
+
if (currentBlock?.type === 'tool_use' && typeof currentBlock.input === 'string') {
|
|
151
|
+
try {
|
|
152
|
+
currentBlock.input = JSON.parse(currentBlock.input || '{}');
|
|
153
|
+
} catch {
|
|
154
|
+
currentBlock.input = {};
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
currentBlock = null;
|
|
158
|
+
break;
|
|
159
|
+
|
|
160
|
+
case 'message_delta':
|
|
161
|
+
if (event.delta?.stop_reason) {
|
|
162
|
+
message.stop_reason = event.delta.stop_reason;
|
|
163
|
+
}
|
|
164
|
+
if (event.usage) {
|
|
165
|
+
message.usage.output_tokens = event.usage.output_tokens || 0;
|
|
166
|
+
}
|
|
167
|
+
break;
|
|
168
|
+
|
|
169
|
+
case 'message_stop':
|
|
170
|
+
break;
|
|
171
|
+
|
|
172
|
+
case 'ping':
|
|
173
|
+
// Keepalive, ignore
|
|
174
|
+
break;
|
|
175
|
+
|
|
176
|
+
case 'error':
|
|
177
|
+
throw new Error(`Stream error: ${event.error?.message || JSON.stringify(event)}`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return message;
|
|
182
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* System Prompt Builder — loads and merges CLAUDE.md files.
|
|
3
|
+
*
|
|
4
|
+
* Features:
|
|
5
|
+
* - Loads CLAUDE.md from: ~/.claude/CLAUDE.md, project root, parent dirs
|
|
6
|
+
* - Merges in order (global -> project -> local)
|
|
7
|
+
* - Splits at cache boundary (static prefix cached, dynamic suffix not)
|
|
8
|
+
* - Includes tool schemas in the system prompt
|
|
9
|
+
*/
|
|
10
|
+
import fs from 'fs';
|
|
11
|
+
import path from 'path';
|
|
12
|
+
import os from 'os';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Load all CLAUDE.md files and merge them in order.
|
|
16
|
+
* @param {string} [cwd] - current working directory
|
|
17
|
+
* @returns {string[]} Array of CLAUDE.md contents in merge order
|
|
18
|
+
*/
|
|
19
|
+
export function loadClaudeMdFiles(cwd = process.cwd()) {
|
|
20
|
+
const files = [];
|
|
21
|
+
|
|
22
|
+
// 1. Global: ~/.claude/CLAUDE.md
|
|
23
|
+
const globalPath = path.join(os.homedir(), '.claude', 'CLAUDE.md');
|
|
24
|
+
if (fs.existsSync(globalPath)) {
|
|
25
|
+
try {
|
|
26
|
+
files.push({ source: 'global', content: fs.readFileSync(globalPath, 'utf-8') });
|
|
27
|
+
} catch { /* skip */ }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 2. Walk from cwd up to root, collecting CLAUDE.md files
|
|
31
|
+
const projectFiles = [];
|
|
32
|
+
let dir = path.resolve(cwd);
|
|
33
|
+
const root = path.parse(dir).root;
|
|
34
|
+
while (dir !== root) {
|
|
35
|
+
const candidates = [
|
|
36
|
+
path.join(dir, 'CLAUDE.md'),
|
|
37
|
+
path.join(dir, '.claude', 'CLAUDE.md'),
|
|
38
|
+
];
|
|
39
|
+
for (const f of candidates) {
|
|
40
|
+
if (fs.existsSync(f)) {
|
|
41
|
+
try {
|
|
42
|
+
projectFiles.push({ source: dir, content: fs.readFileSync(f, 'utf-8'), path: f });
|
|
43
|
+
} catch { /* skip */ }
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
dir = path.dirname(dir);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Reverse so parent dirs come first (global -> project -> local)
|
|
50
|
+
projectFiles.reverse();
|
|
51
|
+
files.push(...projectFiles);
|
|
52
|
+
|
|
53
|
+
return files;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Build the full system prompt from CLAUDE.md files and tool schemas.
|
|
58
|
+
* @param {object} options
|
|
59
|
+
* @param {string} [options.cwd] - current working directory
|
|
60
|
+
* @param {Array} [options.tools] - tool definitions for schema inclusion
|
|
61
|
+
* @param {string} [options.override] - override system prompt entirely
|
|
62
|
+
* @param {string[]} [options.addDirs] - additional directories to search for CLAUDE.md
|
|
63
|
+
* @returns {{ staticPrefix: string, dynamicSuffix: string, full: string }}
|
|
64
|
+
*/
|
|
65
|
+
export function buildSystemPrompt({ cwd, tools, override, addDirs } = {}) {
|
|
66
|
+
if (override) {
|
|
67
|
+
return { staticPrefix: override, dynamicSuffix: '', full: override };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const parts = ['You are an AI coding assistant.'];
|
|
71
|
+
|
|
72
|
+
// Load CLAUDE.md files
|
|
73
|
+
const mdFiles = loadClaudeMdFiles(cwd);
|
|
74
|
+
|
|
75
|
+
// Add additional directories
|
|
76
|
+
if (addDirs) {
|
|
77
|
+
for (const dir of addDirs) {
|
|
78
|
+
const p = path.join(dir, 'CLAUDE.md');
|
|
79
|
+
if (fs.existsSync(p)) {
|
|
80
|
+
try {
|
|
81
|
+
mdFiles.push({ source: dir, content: fs.readFileSync(p, 'utf-8') });
|
|
82
|
+
} catch { /* skip */ }
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
for (const f of mdFiles) {
|
|
88
|
+
parts.push(f.content);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// The static prefix is the base prompt + CLAUDE.md content (cacheable)
|
|
92
|
+
const staticPrefix = parts.join('\n\n');
|
|
93
|
+
|
|
94
|
+
// Dynamic suffix includes tool schemas (changes per-request)
|
|
95
|
+
let dynamicSuffix = '';
|
|
96
|
+
if (tools && tools.length > 0) {
|
|
97
|
+
const toolSummary = tools.map(t =>
|
|
98
|
+
`- ${t.name}: ${(t.description || '').slice(0, 100)}`
|
|
99
|
+
).join('\n');
|
|
100
|
+
dynamicSuffix = `\n\nAvailable tools:\n${toolSummary}`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
staticPrefix,
|
|
105
|
+
dynamicSuffix,
|
|
106
|
+
full: staticPrefix + dynamicSuffix,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Convert system prompt to Anthropic cache-control format.
|
|
112
|
+
* @param {string} staticPrefix
|
|
113
|
+
* @param {string} dynamicSuffix
|
|
114
|
+
* @returns {Array} system blocks with cache_control
|
|
115
|
+
*/
|
|
116
|
+
export function toCacheBlocks(staticPrefix, dynamicSuffix) {
|
|
117
|
+
const blocks = [];
|
|
118
|
+
|
|
119
|
+
if (staticPrefix) {
|
|
120
|
+
blocks.push({
|
|
121
|
+
type: 'text',
|
|
122
|
+
text: staticPrefix,
|
|
123
|
+
cache_control: { type: 'ephemeral' },
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (dynamicSuffix) {
|
|
128
|
+
blocks.push({
|
|
129
|
+
type: 'text',
|
|
130
|
+
text: dynamicSuffix,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return blocks;
|
|
135
|
+
}
|