Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Deep Code 使用 `settings.json` 设置文件进行持久化配置,支持两
| -------------------- | --------- | ------------------------------------------------------------------- |
| `env` | object | 环境变量分组(见下方子字段表) |
| `contextWindow` | number/string | 上下文窗口上限,可使用精确 token 数或 `128K`、`1M` 等格式 |
| `autoCompactWindow` | number/string | 自动压缩阈值,默认取最终上下文窗口的 50% |
| `autoCompactWindow` | number/string | 自动压缩阈值,默认取上下文窗口的 80% |
| `model` | string | 模型名称。优先级高于 `env.MODEL` |
| `thinkingEnabled` | boolean | 是否启用思考模式(DeepSeek V4 系列默认启用) |
| `reasoningEffort` | string | 推理强度,可选 `"low"`、`"high"` 或 `"max"`(默认 `"max"`) |
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The following are all the top-level fields supported in `settings.json`, along w
| ------------------ | ------- | --------------------------------------------------------------------------- |
| `env` | object | Group of environment variables (see sub-field table below) |
| `contextWindow` | number/string | Context-window limit as an exact token count or `128K`/`1M` value |
| `autoCompactWindow` | number/string | Auto-compaction threshold; defaults to 50% of the final context window |
| `autoCompactWindow` | number/string | Auto-compaction threshold; defaults to 80% of the context window |
| `model` | string | Model name. Takes precedence over `env.MODEL` |
| `thinkingEnabled` | boolean | Whether to enable thinking mode (enabled by default for DeepSeek V4 series)|
| `reasoningEffort` | string | Reasoning intensity: `"low"`, `"high"`, or `"max"` (default `"max"`) |
Expand Down
145 changes: 145 additions & 0 deletions packages/core/src/common/compaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import type { ChatCompletionMessageParam } from "openai/resources/chat/completions";
import type { SessionMessage } from "../session";

export type CompactBudget = {
output: number;
input: number;
threshold: number;
retain: number;
};

export function compactBudget(window: number, configuredThreshold = Math.floor(window * 0.8)): CompactBudget {
const output = Math.max(1, Math.min(65536, Math.floor(window / 8)));
const input = window - output;
const threshold = Math.min(configuredThreshold, input - output);
if (threshold <= 0) throw new Error("The context window leaves no room for conversation input.");
return { output, input, threshold, retain: Math.min(Math.floor(input * 0.16), Math.floor(threshold * 0.8)) };
}

/** An estimate, not a tokenizer. Never charge binary image encodings as text. */
export function estimateRequestTokens(messages: readonly ChatCompletionMessageParam[], tools: unknown = []): number {
return messages.reduce((sum, message) => sum + 4 + estimateValue(message), estimateValue(tools));
}

function estimateValue(value: unknown): number {
if (typeof value === "string") {
let tokens = 0;
for (const char of value) tokens += /[\u3400-\u9fff\uf900-\ufaff]/u.test(char) ? 0.6 : 0.3;
return Math.ceil(tokens);
}
if (Array.isArray(value)) return value.reduce((sum, item) => sum + estimateValue(item), 0);
if (value && typeof value === "object") {
const record = value as Record<string, unknown>;
if (record.type === "image_url" || record.type === "file") {
// Unknown model/image geometry: allow a conservative multi-tile visual budget.
// Files here are uploaded images; their short file IDs do not reflect their cost.
return (record.image_url as { detail?: string } | undefined)?.detail === "low" ? 85 : 4096;
}
return Object.entries(record).reduce((sum, [key, item]) => sum + estimateValue(key) + estimateValue(item), 0);
}
return value == null ? 0 : 1;
}

export function isFixedContext(message: SessionMessage): boolean {
return Boolean(message.meta?.skillCatalog) || (message.role === "system" && !message.meta?.isSummary);
}

/** Eligible prefixes, longest first, with tool calls/results kept on the same side. */
export function compactPrefixes(
messages: SessionMessage[],
prices: number[],
retain: number
): {
history: SessionMessage[];
ends: number[];
} {
const history = messages.filter((message) => !isFixedContext(message));
if (history.length < 2) return { history, ends: [] };
const priceById = new Map(messages.map((message, index) => [message.id, prices[index] ?? 0]));
let keepFrom = history.length - 1;
let tail = priceById.get(history[keepFrom].id) ?? 0;
while (keepFrom > 0 && tail < retain) tail += priceById.get(history[--keepFrom].id) ?? 0;

const calls = new Map<string, number[]>();
const edges: Array<[number, number]> = [];
const pending = new Set<number>();
const orphanResults = new Set<number>();
history.forEach((message, index) => {
// Once the conversation resumes, missing earlier results are replayed as interrupted.
if (message.role === "user" || message.role === "assistant") pending.clear();
const params = message.messageParams as { tool_calls?: Array<{ id?: string }>; tool_call_id?: string } | null;
if (message.role === "assistant") {
for (const call of params?.tool_calls ?? []) {
if (!call.id) continue;
calls.set(call.id, [...(calls.get(call.id) ?? []), index]);
pending.add(index);
}
}
if (message.role === "tool") {
const candidates = calls.get(params?.tool_call_id ?? "");
const from = candidates?.at(-1);
if (from === undefined) orphanResults.add(index);
else edges.push([from, index]);
}
});
// Without a later conversation message, the trailing batch needs all of its results.
for (const index of pending) {
const params = history[index].messageParams as { tool_calls: Array<{ id?: string }> };
if (
params.tool_calls.every((call) =>
history
.slice(index + 1)
.some(
(message) =>
message.role === "tool" &&
(message.messageParams as { tool_call_id?: string } | null)?.tool_call_id === call.id
)
)
)
pending.delete(index);
}
const lastSummary = history.reduce((last, message, index) => (message.meta?.isSummary ? index : last), -1);
const ends: number[] = [];
for (let end = keepFrom; end > 0; end--) {
if (end <= lastSummary || edges.some(([from, to]) => from < end && to >= end)) continue;
if ([...pending, ...orphanResults].some((index) => index < end)) continue;
ends.push(end);
}
return { history, ends };
}

function errorRecord(error: unknown): Record<string, unknown> {
return error && typeof error === "object" ? (error as Record<string, unknown>) : {};
}

export function isContextOverflow(error: unknown): boolean {
const outer = errorRecord(error);
const inner = errorRecord(outer.error);
const code = String(inner.code ?? outer.code ?? "");
const message = String(inner.message ?? outer.message ?? "");
if (outer.status != null && outer.status !== 400 && outer.status !== 413 && outer.status !== 422) return false;
return (
/^(context_length_exceeded|context_window_exceeded|max_context_length|CONTEXT_OVERFLOW)$/i.test(code) ||
/(?:maximum context length|context (?:window|length).{0,40}(?:exceed|too (?:long|large))|(?:exceed|too many).{0,40}(?:context|input tokens))/i.test(
message
)
);
}

/** Only explicit image capacity failures authorize lossy image projection. */
export function imageOffloadCount(error: unknown): number {
const outer = errorRecord(error);
const inner = errorRecord(outer.error);
if (outer.status != null && outer.status !== 400 && outer.status !== 413 && outer.status !== 422) return 0;
const code = String(inner.code ?? outer.code ?? "");
const message = String(inner.message ?? outer.message ?? "");
if (
!/^(IMAGE_OFFLOAD_REQUIRED|too_many_images|image_limit_exceeded|image_token_limit_exceeded)$/i.test(code) &&
!/(?:too many images|images?.{0,80}(?:exceed.{0,40}(?:limit|budget)|(?:token|count|number).{0,30}(?:limit|exceed)))/i.test(
message
)
)
return 0;
const count = inner.offloadImages ?? outer.offloadImages;
return typeof count === "number" && Number.isInteger(count) && count > 0 ? count : 1;
}
152 changes: 56 additions & 96 deletions packages/core/src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,91 +5,59 @@ import * as path from "path";
import ejs from "ejs";
import matter from "gray-matter";
import { fileURLToPath } from "url";
import type { SessionMessage } from "./session";
import { findGitBashPath, resolveShellPath } from "./common/shell-utils";
import { supportsMultimodal, type MultimodalMode } from "./common/model-capabilities";

const COMPACT_PROMPT_BASE = `Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions.
This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context.

Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:

1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
- The user's explicit requests and intents
- Your approach to addressing the user's requests
- Key decisions, technical concepts and code patterns
- Specific details like:
- file names
- full code snippets
- function signatures
- file edits
- Errors that you ran into and how you fixed them
- Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.

Your summary should include the following sections:

1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.
3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Pay special attention to the most recent messages and include full code snippets where applicable and include a summary of why this file read or edit is important.
4. Errors and fixes: List all errors that you ran into, and how you fixed them. Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
6. All user messages: List ALL user messages that are not tool results. These are critical for understanding the users' feedback and changing intent.
6. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
7. Current Work: Describe in detail precisely what was being worked on immediately before this summary request, paying special attention to the most recent messages from both user and assistant. Include file names and code snippets where applicable.
8. Optional Next Step: List the next step that you will take that is related to the most recent work you were doing. IMPORTANT: ensure that this step is DIRECTLY in line with the user's most recent explicit requests, and the task you were working on immediately before this summary request. If your last task was concluded, then only list next steps if they are explicitly in line with the users request. Do not start on tangential requests or really old requests that were already completed without confirming with the user first.
If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation.

Here's an example of how your output should be structured:

<example>
<analysis>
[Your thought process, ensuring all points are covered thoroughly and accurately]
</analysis>

<summary>
1. Primary Request and Intent:
[Detailed description]

2. Key Technical Concepts:
- [Concept 1]
- [Concept 2]
- [...]

3. Files and Code Sections:
- [File Name 1]
- [Summary of why this file is important]
- [Summary of the changes made to this file, if any]
- [Important Code Snippet]
- [File Name 2]
- [Important Code Snippet]
- [...]

4. Errors and fixes:
- [Detailed description of error 1]:
- [How you fixed the error]
- [User feedback on the error if any]
- [...]

5. Problem Solving:
[Description of solved problems and ongoing troubleshooting]

6. All user messages:
- [Detailed non tool use user message]
- [...]

7. Pending Tasks:
- [Task 1]
- [Task 2]
- [...]

8. Current Work:
[Precise description of current work]

9. Optional Next Step:
[Optional Next step to take]

</summary>`;
const SUMMARY_OPEN_TAG = "<compacted-summary>";
const SUMMARY_CLOSE_TAG = "</compacted-summary>";

/**
* The summarization directive, delivered as the FINAL user message after the
* replayed conversation rather than as a distinct summarizer system prompt.
* Keeping the conversation's own system prompt, tools, and message prefix in
* front of it makes the auxiliary call a genuine prefix of the last routed
* request, so the provider's KV cache is reused instead of invalidated.
*/
export const COMPACTION_INSTRUCTION = [
"You are now acting as a compaction engine for this AI coding assistant. Condense the conversation ABOVE into a structured checkpoint that lets another model resume the work with no loss of essential context.",
"",
'Output EXACTLY the Markdown structure below: keep every section, in order. Use terse bullets, not prose paragraphs. Write "(none)" for an empty section — never drop a section.',
"",
"## Primary Request and Intent",
"- [the user's original and evolving goals; quote verbatim where the exact wording matters]",
"",
"## Key Technical Concepts",
"- [technologies, frameworks, patterns, and conventions in play]",
"",
"## Files and Code",
"- [exact path: why it matters, key changes or snippets]",
"",
"## Errors and Fixes",
"- [error: how it was resolved, plus any related user feedback]",
"",
"## Pending Jobs",
"- [explicitly requested work not yet completed]",
"",
"## Current Work",
"- [precisely what was in progress at this checkpoint]",
"",
"## Next Step",
'- [the single next action, directly in line with the most recent request, or "(none)"]',
"",
"## Critical Context",
"- [decisions and their rationale, constraints, user preferences, open questions, data needed to continue]",
"",
"Rules:",
"- Write concise English engineering prose. Preserve exact file paths, commands, error strings, identifiers, numeric values, function signatures, and syntax fragments.",
"- Capture user feedback and explicit instructions faithfully, especially corrections.",
"- Do NOT mention this summarization request or that the context was compacted.",
"- Output only the checkpoint text: do not call any tool or take any other action.",
`- If the conversation already contains a ${SUMMARY_OPEN_TAG} block, it is a PRIOR checkpoint. Do not copy it forward verbatim: preserve still-true facts, drop stale ones, and merge newer information into a single consolidated summary under the same structure.`,
].join("\n");

/** Framing that makes the replacement user message established context. */
export const CHECKPOINT_PREAMBLE =
"This is an automatically generated checkpoint condensing an earlier span of the conversation to free up context. Treat the captured context as established background and build on it without restating it. Continue the task directly from the messages that follow, without acknowledging this checkpoint.";

const SYSTEM_PROMPT_BASE = `You are a helpful software engineer assistant.`;

Expand Down Expand Up @@ -293,20 +261,12 @@ export function getSystemPrompt(_projectRoot: string, options: PromptToolOptions
return toolDocs ? `${SYSTEM_PROMPT_BASE}\n\n# Available Tools\n\n${toolDocs}` : SYSTEM_PROMPT_BASE;
}

export function getCompactPrompt(sessionMessages: SessionMessage[]): string {
const jsonl = sessionMessages
.map((message) =>
JSON.stringify({
id: message.id,
role: message.role,
content: message.content,
contentParams: message.contentParams,
messageParams: message.messageParams,
createTime: message.createTime,
})
)
.join("\n");
return `${COMPACT_PROMPT_BASE}\n\nconversation below:\n\n\`\`\`jsonl\n${jsonl}\n\`\`\``;
export function getCompactPrompt(): string {
return COMPACTION_INSTRUCTION;
}

export function frameCompactSummary(summary: string): string {
return `${CHECKPOINT_PREAMBLE}\n\n${SUMMARY_OPEN_TAG}\n${summary}\n${SUMMARY_CLOSE_TAG}`;
}

export function getRuntimeContext(projectRoot: string, model?: string, addWorkingDirs: string[] = []): string {
Expand Down
Loading
Loading