@kkelly-offical/kkcode 0.1.6 → 0.1.7
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/package.json +1 -1
- package/src/agent/agent.mjs +220 -211
- package/src/agent/prompt/bug-hunter.txt +90 -0
- package/src/repl.mjs +58 -6
- package/src/session/compaction.mjs +298 -276
- package/src/session/engine.mjs +5 -0
- package/src/session/longagent-hybrid.mjs +21 -5
- package/src/session/longagent.mjs +21 -5
- package/src/session/loop.mjs +65 -40
- package/src/session/prompt/agent.txt +25 -0
- package/src/session/prompt/plan.txt +31 -9
- package/src/session/rollback.mjs +196 -0
- package/src/session/store.mjs +12 -3
- package/src/session/system-prompt.mjs +273 -260
- package/src/tool/question-prompt.mjs +93 -86
|
@@ -1,260 +1,273 @@
|
|
|
1
|
-
import { readFile, access } from "node:fs/promises"
|
|
2
|
-
import { execSync } from "node:child_process"
|
|
3
|
-
import path from "node:path"
|
|
4
|
-
import { fileURLToPath } from "node:url"
|
|
5
|
-
import { createHash } from "node:crypto"
|
|
6
|
-
import { loadSessionPrompt } from "./prompt-loader.mjs"
|
|
7
|
-
import { getAgentPrompt, listAgents } from "../agent/agent.mjs"
|
|
8
|
-
import { loadAutoMemory } from "./memory-loader.mjs"
|
|
9
|
-
|
|
10
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
11
|
-
const TOOL_PROMPT_DIR = path.join(__dirname, "..", "tool", "prompt")
|
|
12
|
-
|
|
13
|
-
const toolPromptCache = new Map()
|
|
14
|
-
|
|
15
|
-
// Session-level block cache: avoids rebuilding identical blocks across turns
|
|
16
|
-
// Key = hash of inputs, Value = { blocks, text, timestamp }
|
|
17
|
-
let blockCache = { key: null, result: null }
|
|
18
|
-
|
|
19
|
-
function hashInputs(obj) {
|
|
20
|
-
return createHash("md5").update(JSON.stringify(obj)).digest("hex")
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async function loadToolPrompt(name) {
|
|
24
|
-
if (!toolPromptCache.has(name)) {
|
|
25
|
-
try {
|
|
26
|
-
const file = path.join(TOOL_PROMPT_DIR, `${name}.txt`)
|
|
27
|
-
const text = (await readFile(file, "utf8")).trim()
|
|
28
|
-
toolPromptCache.set(name, text)
|
|
29
|
-
} catch {
|
|
30
|
-
toolPromptCache.set(name, "")
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
return toolPromptCache.get(name)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// Detect if cwd is a git repo
|
|
37
|
-
function detectGitRepo(cwd) {
|
|
38
|
-
try {
|
|
39
|
-
execSync("git rev-parse --is-inside-work-tree", { cwd, stdio: "pipe", timeout: 3000 })
|
|
40
|
-
return true
|
|
41
|
-
} catch {
|
|
42
|
-
return false
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Detect the user's default shell
|
|
47
|
-
function detectShell() {
|
|
48
|
-
if (process.platform === "win32") {
|
|
49
|
-
// On Windows, kkcode uses bash (git bash / WSL) internally
|
|
50
|
-
return "bash (use Unix shell syntax, not Windows — e.g., /dev/null not NUL, forward slashes in paths)"
|
|
51
|
-
}
|
|
52
|
-
const shell = process.env.SHELL || "/bin/bash"
|
|
53
|
-
return path.basename(shell)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Layer 1: Environment information (dynamic per turn — changes with cwd/date)
|
|
57
|
-
export function environmentPrompt({ model, cwd }) {
|
|
58
|
-
const isGit = detectGitRepo(cwd)
|
|
59
|
-
const shell = detectShell()
|
|
60
|
-
const today = new Date().toISOString().slice(0, 10)
|
|
61
|
-
const lines = [
|
|
62
|
-
`<env>`,
|
|
63
|
-
` model: ${model}`,
|
|
64
|
-
` cwd: ${cwd}`,
|
|
65
|
-
` platform: ${process.platform}`,
|
|
66
|
-
` shell: ${shell}`,
|
|
67
|
-
` node: ${process.version}`,
|
|
68
|
-
` date: ${today}`,
|
|
69
|
-
` git_repo: ${isGit}`,
|
|
70
|
-
`</env>`,
|
|
71
|
-
``,
|
|
72
|
-
`Knowledge cutoff: early 2025. Current date: ${today}.`,
|
|
73
|
-
`When searching for recent information, use the current year (${today.slice(0, 4)}) in queries.`
|
|
74
|
-
]
|
|
75
|
-
return lines.join("\n")
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Layer 2: System prompt (model-specific — stable across session)
|
|
79
|
-
export async function providerPromptByModel(model) {
|
|
80
|
-
const m = String(model).toLowerCase()
|
|
81
|
-
if (m.includes("claude")) return loadSessionPrompt("anthropic.txt")
|
|
82
|
-
if (m.includes("gpt-5") || m.includes("codex")) return loadSessionPrompt("beast.txt")
|
|
83
|
-
if (m.includes("gpt") || m.includes("o1") || m.includes("o3")) return loadSessionPrompt("beast.txt")
|
|
84
|
-
if (m.includes("gemini")) return loadSessionPrompt("qwen.txt")
|
|
85
|
-
if (m.includes("deepseek")) return loadSessionPrompt("qwen.txt")
|
|
86
|
-
if (m.includes("qwen")) return loadSessionPrompt("qwen.txt")
|
|
87
|
-
return loadSessionPrompt("qwen.txt")
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Layer 3: Agent-specific prompt (stable across session)
|
|
91
|
-
export async function agentPrompt(agent) {
|
|
92
|
-
if (!agent) return ""
|
|
93
|
-
return getAgentPrompt(agent.name)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// Layer 4: Mode reminder (stable within mode)
|
|
97
|
-
export async function modeReminder(mode) {
|
|
98
|
-
if (mode === "plan") return loadSessionPrompt("plan.txt")
|
|
99
|
-
return ""
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
//
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
* -
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
* - Blocks marked cacheable=
|
|
129
|
-
* -
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
1
|
+
import { readFile, access } from "node:fs/promises"
|
|
2
|
+
import { execSync } from "node:child_process"
|
|
3
|
+
import path from "node:path"
|
|
4
|
+
import { fileURLToPath } from "node:url"
|
|
5
|
+
import { createHash } from "node:crypto"
|
|
6
|
+
import { loadSessionPrompt } from "./prompt-loader.mjs"
|
|
7
|
+
import { getAgentPrompt, listAgents } from "../agent/agent.mjs"
|
|
8
|
+
import { loadAutoMemory } from "./memory-loader.mjs"
|
|
9
|
+
|
|
10
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
11
|
+
const TOOL_PROMPT_DIR = path.join(__dirname, "..", "tool", "prompt")
|
|
12
|
+
|
|
13
|
+
const toolPromptCache = new Map()
|
|
14
|
+
|
|
15
|
+
// Session-level block cache: avoids rebuilding identical blocks across turns
|
|
16
|
+
// Key = hash of inputs, Value = { blocks, text, timestamp }
|
|
17
|
+
let blockCache = { key: null, result: null }
|
|
18
|
+
|
|
19
|
+
function hashInputs(obj) {
|
|
20
|
+
return createHash("md5").update(JSON.stringify(obj)).digest("hex")
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function loadToolPrompt(name) {
|
|
24
|
+
if (!toolPromptCache.has(name)) {
|
|
25
|
+
try {
|
|
26
|
+
const file = path.join(TOOL_PROMPT_DIR, `${name}.txt`)
|
|
27
|
+
const text = (await readFile(file, "utf8")).trim()
|
|
28
|
+
toolPromptCache.set(name, text)
|
|
29
|
+
} catch {
|
|
30
|
+
toolPromptCache.set(name, "")
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return toolPromptCache.get(name)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Detect if cwd is a git repo
|
|
37
|
+
function detectGitRepo(cwd) {
|
|
38
|
+
try {
|
|
39
|
+
execSync("git rev-parse --is-inside-work-tree", { cwd, stdio: "pipe", timeout: 3000 })
|
|
40
|
+
return true
|
|
41
|
+
} catch {
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Detect the user's default shell
|
|
47
|
+
function detectShell() {
|
|
48
|
+
if (process.platform === "win32") {
|
|
49
|
+
// On Windows, kkcode uses bash (git bash / WSL) internally
|
|
50
|
+
return "bash (use Unix shell syntax, not Windows — e.g., /dev/null not NUL, forward slashes in paths)"
|
|
51
|
+
}
|
|
52
|
+
const shell = process.env.SHELL || "/bin/bash"
|
|
53
|
+
return path.basename(shell)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Layer 1: Environment information (dynamic per turn — changes with cwd/date)
|
|
57
|
+
export function environmentPrompt({ model, cwd }) {
|
|
58
|
+
const isGit = detectGitRepo(cwd)
|
|
59
|
+
const shell = detectShell()
|
|
60
|
+
const today = new Date().toISOString().slice(0, 10)
|
|
61
|
+
const lines = [
|
|
62
|
+
`<env>`,
|
|
63
|
+
` model: ${model}`,
|
|
64
|
+
` cwd: ${cwd}`,
|
|
65
|
+
` platform: ${process.platform}`,
|
|
66
|
+
` shell: ${shell}`,
|
|
67
|
+
` node: ${process.version}`,
|
|
68
|
+
` date: ${today}`,
|
|
69
|
+
` git_repo: ${isGit}`,
|
|
70
|
+
`</env>`,
|
|
71
|
+
``,
|
|
72
|
+
`Knowledge cutoff: early 2025. Current date: ${today}.`,
|
|
73
|
+
`When searching for recent information, use the current year (${today.slice(0, 4)}) in queries.`
|
|
74
|
+
]
|
|
75
|
+
return lines.join("\n")
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Layer 2: System prompt (model-specific — stable across session)
|
|
79
|
+
export async function providerPromptByModel(model) {
|
|
80
|
+
const m = String(model).toLowerCase()
|
|
81
|
+
if (m.includes("claude")) return loadSessionPrompt("anthropic.txt")
|
|
82
|
+
if (m.includes("gpt-5") || m.includes("codex")) return loadSessionPrompt("beast.txt")
|
|
83
|
+
if (m.includes("gpt") || m.includes("o1") || m.includes("o3")) return loadSessionPrompt("beast.txt")
|
|
84
|
+
if (m.includes("gemini")) return loadSessionPrompt("qwen.txt")
|
|
85
|
+
if (m.includes("deepseek")) return loadSessionPrompt("qwen.txt")
|
|
86
|
+
if (m.includes("qwen")) return loadSessionPrompt("qwen.txt")
|
|
87
|
+
return loadSessionPrompt("qwen.txt")
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Layer 3: Agent-specific prompt (stable across session)
|
|
91
|
+
export async function agentPrompt(agent) {
|
|
92
|
+
if (!agent) return ""
|
|
93
|
+
return getAgentPrompt(agent.name)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Layer 4: Mode reminder (stable within mode)
|
|
97
|
+
export async function modeReminder(mode) {
|
|
98
|
+
if (mode === "plan") return loadSessionPrompt("plan.txt")
|
|
99
|
+
if (mode === "agent") return loadSessionPrompt("agent.txt")
|
|
100
|
+
return ""
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Layer 5: Tool descriptions (stable across session — ideal cache target)
|
|
104
|
+
export async function toolDescriptions(tools) {
|
|
105
|
+
if (!tools || !tools.length) return ""
|
|
106
|
+
const descriptions = []
|
|
107
|
+
for (const tool of tools) {
|
|
108
|
+
const prompt = await loadToolPrompt(tool.name)
|
|
109
|
+
if (prompt) {
|
|
110
|
+
descriptions.push(`## ${tool.name}\n${prompt}`)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (!descriptions.length) return ""
|
|
114
|
+
return `# Available Tools\n\n${descriptions.join("\n\n")}`
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Layer 6: User custom instructions (loaded externally via instruction-loader.mjs and rules)
|
|
118
|
+
// Assembled in loop.mjs from loadInstructions() and renderRulesPrompt()
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Build system prompt as structured blocks for provider-level cache optimization.
|
|
122
|
+
*
|
|
123
|
+
* Returns { text, blocks } where:
|
|
124
|
+
* - text: single concatenated string (for providers that don't support block-level caching)
|
|
125
|
+
* - blocks: array of { label, text, cacheable } objects
|
|
126
|
+
*
|
|
127
|
+
* Cache strategy:
|
|
128
|
+
* - Blocks marked cacheable=true are stable across turns (provider/agent/tools/skills)
|
|
129
|
+
* - Blocks marked cacheable=false are dynamic per turn (env/user instructions)
|
|
130
|
+
* - Providers use this to place cache_control breakpoints optimally
|
|
131
|
+
*
|
|
132
|
+
* Anthropic: up to 4 cache breakpoints — place on stable blocks
|
|
133
|
+
* OpenAI: automatic prefix caching — stable blocks should come first
|
|
134
|
+
*/
|
|
135
|
+
export async function buildSystemPromptBlocks({ mode, model, cwd, agent = null, tools = [], skills = [], userInstructions = "", projectContext = "", language = "en" }) {
|
|
136
|
+
// Cache key: hash of all inputs that affect block content
|
|
137
|
+
const cacheKey = hashInputs({
|
|
138
|
+
mode, model, cwd, language,
|
|
139
|
+
agent: agent?.name || null,
|
|
140
|
+
tools: tools.map(t => t.name).sort(),
|
|
141
|
+
skills: skills.map(s => s.name).sort(),
|
|
142
|
+
userInstructions: userInstructions.slice(0, 200) // first 200 chars as fingerprint
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
if (blockCache.key === cacheKey && blockCache.result) {
|
|
146
|
+
// Only env block changes per turn — rebuild just that
|
|
147
|
+
const cached = blockCache.result
|
|
148
|
+
const envIdx = cached.blocks.findIndex(b => b.label === "env")
|
|
149
|
+
if (envIdx >= 0) {
|
|
150
|
+
const freshEnv = environmentPrompt({ model, cwd })
|
|
151
|
+
if (cached.blocks[envIdx].text === freshEnv) {
|
|
152
|
+
return cached // fully identical
|
|
153
|
+
}
|
|
154
|
+
// Clone and update only the env block
|
|
155
|
+
const updatedBlocks = cached.blocks.map((b, i) =>
|
|
156
|
+
i === envIdx ? { ...b, text: freshEnv } : b
|
|
157
|
+
)
|
|
158
|
+
const text = updatedBlocks.map(b => b.text).join("\n\n")
|
|
159
|
+
const result = { text, blocks: updatedBlocks }
|
|
160
|
+
blockCache = { key: cacheKey, result }
|
|
161
|
+
return result
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const blocks = []
|
|
166
|
+
|
|
167
|
+
// Block 0: Provider prompt (stable — loaded once per model)
|
|
168
|
+
const providerText = await providerPromptByModel(model)
|
|
169
|
+
if (providerText) {
|
|
170
|
+
blocks.push({ label: "provider", text: providerText, cacheable: true })
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Block 1: Agent prompt (stable — loaded once per agent)
|
|
174
|
+
const agentText = agent ? await getAgentPrompt(agent.name) : ""
|
|
175
|
+
if (agentText) {
|
|
176
|
+
blocks.push({ label: "agent", text: agentText, cacheable: true })
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Block 2: Mode reminder (stable within mode)
|
|
180
|
+
const modeText = await modeReminder(mode)
|
|
181
|
+
if (modeText) {
|
|
182
|
+
blocks.push({ label: "mode", text: modeText, cacheable: true })
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Block 3: Tool descriptions (stable — changes only when tools change)
|
|
186
|
+
const toolText = await toolDescriptions(tools)
|
|
187
|
+
if (toolText) {
|
|
188
|
+
blocks.push({ label: "tools", text: toolText, cacheable: true })
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Block 3.5: Large output strategy (stable — always included)
|
|
192
|
+
const outputStrategyLines = [
|
|
193
|
+
"# Large Output Strategy",
|
|
194
|
+
"",
|
|
195
|
+
"When generating large amounts of content:",
|
|
196
|
+
"- For large file creation, write no more than 200 lines per tool call; use append mode for subsequent chunks",
|
|
197
|
+
"- For partial file edits, use patch with line ranges instead of rewriting the whole file",
|
|
198
|
+
"- If a task requires more than 300 lines of code, proactively split into multiple sequential tool calls",
|
|
199
|
+
"- Never attempt to write an entire large file in a single tool call"
|
|
200
|
+
]
|
|
201
|
+
blocks.push({ label: "output_strategy", text: outputStrategyLines.join("\n"), cacheable: true })
|
|
202
|
+
|
|
203
|
+
// Block 4: Skills descriptions (stable — changes only when skills change)
|
|
204
|
+
if (skills.length) {
|
|
205
|
+
const skillLines = skills.map((s) => `- /${s.name}: ${s.description || s.name}`).join("\n")
|
|
206
|
+
const skillText = `# Available Skills\n\nInvoke with /<skill-name> [arguments].\n\n${skillLines}`
|
|
207
|
+
blocks.push({ label: "skills", text: skillText, cacheable: true })
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Block 4.5: Available sub-agents (stable — changes only when custom agents change)
|
|
211
|
+
const allAgents = listAgents({ includeHidden: false })
|
|
212
|
+
const customSubagents = allAgents.filter((a) => a.mode === "subagent" && a._customAgent)
|
|
213
|
+
if (customSubagents.length) {
|
|
214
|
+
const agentLines = customSubagents.map((a) => {
|
|
215
|
+
const perms = a.permission === "readonly" ? " (read-only)" : a.permission === "full" ? " (full access)" : ""
|
|
216
|
+
return `- ${a.name}: ${a.description}${perms}`
|
|
217
|
+
})
|
|
218
|
+
const subagentText = [
|
|
219
|
+
"# Available Sub-agents",
|
|
220
|
+
"",
|
|
221
|
+
"Delegate specialized work to these sub-agents using the `task` tool with `subagent_type` parameter.",
|
|
222
|
+
"Use sub-agents when a task is self-contained and would benefit from a specialist, or to save context window space.",
|
|
223
|
+
"",
|
|
224
|
+
...agentLines
|
|
225
|
+
].join("\n")
|
|
226
|
+
blocks.push({ label: "subagents", text: subagentText, cacheable: true })
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Block 4.7: Project context (semi-stable — changes when cwd changes)
|
|
230
|
+
if (projectContext) {
|
|
231
|
+
blocks.push({ label: "project", text: projectContext, cacheable: false })
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Block 4.9: Language constraint (stable — changes only when config changes)
|
|
235
|
+
if (language && language !== "en") {
|
|
236
|
+
const langMap = {
|
|
237
|
+
zh: "Always respond in Chinese (中文). Use Chinese for all explanations, comments, and communications. Technical terms, code identifiers, and code content should remain in their original form (typically English)."
|
|
238
|
+
}
|
|
239
|
+
const langText = langMap[language]
|
|
240
|
+
if (langText) {
|
|
241
|
+
blocks.push({ label: "language", text: `# Language\n\n${langText}`, cacheable: true })
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Block 4.95: Auto Memory (semi-stable — changes when user updates memory files)
|
|
246
|
+
const memoryText = await loadAutoMemory(cwd)
|
|
247
|
+
if (memoryText) {
|
|
248
|
+
blocks.push({ label: "memory", text: memoryText, cacheable: false })
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Block 5: Environment (dynamic per turn)
|
|
252
|
+
const envText = environmentPrompt({ model, cwd })
|
|
253
|
+
blocks.push({ label: "env", text: envText, cacheable: false })
|
|
254
|
+
|
|
255
|
+
// Block 6: User instructions + rules (semi-stable — cacheable if unchanged between turns)
|
|
256
|
+
if (userInstructions) {
|
|
257
|
+
blocks.push({ label: "user", text: userInstructions, cacheable: false })
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const text = blocks.map((b) => b.text).join("\n\n")
|
|
261
|
+
const result = { text, blocks }
|
|
262
|
+
blockCache = { key: cacheKey, result }
|
|
263
|
+
return result
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Legacy flat assembly (kept for backward compatibility)
|
|
267
|
+
export async function buildSystemPromptLayers({ mode, model, cwd, agent = null }) {
|
|
268
|
+
const layer1 = environmentPrompt({ model, cwd })
|
|
269
|
+
const layer2 = await providerPromptByModel(model)
|
|
270
|
+
const layer3 = await agentPrompt(agent)
|
|
271
|
+
const layer4 = await modeReminder(mode)
|
|
272
|
+
return { layer1, layer2, layer3, layer4 }
|
|
273
|
+
}
|