|
| 1 | +import type { McpServerConfig, PermissionScope, PermissionSettings } from "./settings"; |
| 2 | +import type { AskPermissionRequest, MessageToolPermission, UserToolPermission } from "./common/permissions"; |
| 3 | +import type { CreateOpenAIClient } from "./tools/executor"; |
| 4 | + |
| 5 | +export type SessionStatus = |
| 6 | + | "failed" |
| 7 | + | "pending" |
| 8 | + | "processing" |
| 9 | + | "waiting_for_user" |
| 10 | + | "completed" |
| 11 | + | "interrupted" |
| 12 | + | "ask_permission" |
| 13 | + | "permission_denied"; |
| 14 | + |
| 15 | +export type ModelUsage = { |
| 16 | + prompt_tokens: number; |
| 17 | + completion_tokens: number; |
| 18 | + total_tokens: number; |
| 19 | + completion_tokens_details?: Record<string, unknown>; |
| 20 | + prompt_tokens_details?: Record<string, unknown>; |
| 21 | + prompt_cache_hit_tokens?: number; |
| 22 | + prompt_cache_miss_tokens?: number; |
| 23 | + total_reqs?: number; |
| 24 | +}; |
| 25 | + |
| 26 | +export type SessionProcessEntry = { |
| 27 | + startTime: string; |
| 28 | + command: string; |
| 29 | + timeoutMs?: number; |
| 30 | + deadlineAt?: string; |
| 31 | + timedOut?: boolean; |
| 32 | +}; |
| 33 | + |
| 34 | +export type BashTimeoutAdjustment = { |
| 35 | + processId: string; |
| 36 | + timeoutMs: number; |
| 37 | + deadlineAt: string; |
| 38 | + timedOut: boolean; |
| 39 | +}; |
| 40 | + |
| 41 | +export type SessionEntry = { |
| 42 | + id: string; |
| 43 | + summary: string | null; |
| 44 | + assistantReply: string | null; |
| 45 | + assistantThinking: string | null; |
| 46 | + assistantRefusal: string | null; |
| 47 | + toolCalls: unknown[] | null; |
| 48 | + status: SessionStatus; |
| 49 | + failReason: string | null; |
| 50 | + usage: ModelUsage | null; |
| 51 | + usagePerModel: Record<string, ModelUsage> | null; |
| 52 | + activeTokens: number; |
| 53 | + createTime: string; |
| 54 | + updateTime: string; |
| 55 | + processes: Map<string, SessionProcessEntry> | null; |
| 56 | + askPermissions?: AskPermissionRequest[]; |
| 57 | +}; |
| 58 | + |
| 59 | +export type SessionsIndex = { |
| 60 | + version: 1; |
| 61 | + entries: SessionEntry[]; |
| 62 | + originalPath: string; |
| 63 | +}; |
| 64 | + |
| 65 | +export type SessionMessageRole = "system" | "user" | "assistant" | "tool"; |
| 66 | + |
| 67 | +export type MessageMeta = { |
| 68 | + function?: unknown; |
| 69 | + paramsMd?: string; |
| 70 | + resultMd?: string; |
| 71 | + asThinking?: boolean; |
| 72 | + isSummary?: boolean; |
| 73 | + isModelChange?: boolean; |
| 74 | + skill?: SkillInfo; |
| 75 | + permissions?: MessageToolPermission[]; |
| 76 | + userPrompt?: UserPromptContent; |
| 77 | +}; |
| 78 | + |
| 79 | +export type SessionMessage = { |
| 80 | + id: string; |
| 81 | + sessionId: string; |
| 82 | + role: SessionMessageRole; |
| 83 | + content: string | null; |
| 84 | + contentParams: unknown | null; |
| 85 | + messageParams: unknown | null; |
| 86 | + compacted: boolean; |
| 87 | + visible: boolean; |
| 88 | + createTime: string; |
| 89 | + updateTime: string; |
| 90 | + meta?: MessageMeta; |
| 91 | + html?: string; |
| 92 | + checkpointHash?: string; |
| 93 | +}; |
| 94 | + |
| 95 | +export type UndoTarget = { |
| 96 | + message: SessionMessage; |
| 97 | + index: number; |
| 98 | + canRestoreCode: boolean; |
| 99 | +}; |
| 100 | + |
| 101 | +export type UserPromptContent = { |
| 102 | + text?: string; |
| 103 | + imageUrls?: string[]; |
| 104 | + skills?: SkillInfo[]; |
| 105 | + permissions?: UserToolPermission[]; |
| 106 | + alwaysAllows?: PermissionScope[]; |
| 107 | +}; |
| 108 | + |
| 109 | +export type SkillInfo = { |
| 110 | + name: string; |
| 111 | + path: string; |
| 112 | + description: string; |
| 113 | + isLoaded?: boolean; |
| 114 | +}; |
| 115 | + |
| 116 | +export type SessionManagerOptions = { |
| 117 | + projectRoot: string; |
| 118 | + createOpenAIClient: CreateOpenAIClient; |
| 119 | + getResolvedSettings: () => { |
| 120 | + model: string; |
| 121 | + webSearchTool?: string; |
| 122 | + mcpServers?: Record<string, McpServerConfig>; |
| 123 | + permissions?: Required<PermissionSettings>; |
| 124 | + }; |
| 125 | + renderMarkdown: (text: string) => string; |
| 126 | + onAssistantMessage: (message: SessionMessage, shouldConnect: boolean) => void; |
| 127 | + onSessionEntryUpdated?: (entry: SessionEntry) => void; |
| 128 | + onLlmStreamProgress?: (progress: LlmStreamProgress) => void; |
| 129 | + onMcpStatusChanged?: () => void; |
| 130 | + onProcessStdout?: (pid: number, chunk: string) => void; |
| 131 | +}; |
| 132 | + |
| 133 | +export type LlmStreamProgress = { |
| 134 | + requestId: string; |
| 135 | + sessionId?: string; |
| 136 | + startedAt: string; |
| 137 | + estimatedTokens: number; |
| 138 | + formattedTokens: string; |
| 139 | + phase: "start" | "update" | "end"; |
| 140 | +}; |
| 141 | +import { DEEPSEEK_V4_MODELS } from "./common/model-capabilities"; |
| 142 | + |
| 143 | +const DEFAULT_COMPACT_PROMPT_TOKEN_THRESHOLD = 128 * 1024; |
| 144 | +const DEEPSEEK_V4_COMPACT_PROMPT_TOKEN_THRESHOLD = 512 * 1024; |
| 145 | + |
| 146 | +export function getCompactPromptTokenThreshold(model: string): number { |
| 147 | + return DEEPSEEK_V4_MODELS.has(model) |
| 148 | + ? DEEPSEEK_V4_COMPACT_PROMPT_TOKEN_THRESHOLD |
| 149 | + : DEFAULT_COMPACT_PROMPT_TOKEN_THRESHOLD; |
| 150 | +} |
| 151 | + |
| 152 | +export function isUsageRecord(value: unknown): value is Record<string, unknown> { |
| 153 | + return value !== null && typeof value === "object" && !Array.isArray(value); |
| 154 | +} |
| 155 | + |
| 156 | +export function getTotalTokens(usage: ModelUsage | null | undefined): number { |
| 157 | + if (!isUsageRecord(usage)) { |
| 158 | + return 0; |
| 159 | + } |
| 160 | + const totalTokens = (usage as Record<string, unknown>).total_tokens; |
| 161 | + return typeof totalTokens === "number" ? totalTokens : 0; |
| 162 | +} |
| 163 | + |
| 164 | +export function summarizeCompletionOptions(options?: Record<string, unknown>): Record<string, unknown> | undefined { |
| 165 | + if (!options) { |
| 166 | + return undefined; |
| 167 | + } |
| 168 | + return { |
| 169 | + ...options, |
| 170 | + signal: options.signal instanceof AbortSignal ? { aborted: options.signal.aborted } : options.signal, |
| 171 | + }; |
| 172 | +} |
| 173 | + |
| 174 | +export function addUsageValue(current: unknown, next: unknown): unknown { |
| 175 | + if (typeof next === "number") { |
| 176 | + return (typeof current === "number" ? current : 0) + next; |
| 177 | + } |
| 178 | + |
| 179 | + if (isUsageRecord(next)) { |
| 180 | + const currentRecord = isUsageRecord(current) ? current : {}; |
| 181 | + const result: Record<string, unknown> = { ...currentRecord }; |
| 182 | + for (const [key, value] of Object.entries(next)) { |
| 183 | + result[key] = addUsageValue(currentRecord[key], value); |
| 184 | + } |
| 185 | + return result; |
| 186 | + } |
| 187 | + |
| 188 | + return next; |
| 189 | +} |
| 190 | + |
| 191 | +export function accumulateUsage(current: ModelUsage | null, next: unknown | null | undefined): ModelUsage | null { |
| 192 | + if (next == null) { |
| 193 | + return current ?? null; |
| 194 | + } |
| 195 | + return addUsageValue(current, next) as ModelUsage; |
| 196 | +} |
| 197 | + |
| 198 | +export function usageWithRequestCount(usage: ModelUsage): ModelUsage { |
| 199 | + const totalReqs = typeof usage.total_reqs === "number" ? usage.total_reqs + 1 : 1; |
| 200 | + return { |
| 201 | + ...usage, |
| 202 | + total_reqs: totalReqs, |
| 203 | + }; |
| 204 | +} |
| 205 | + |
| 206 | +export function accumulateUsagePerModel( |
| 207 | + current: Record<string, ModelUsage> | null | undefined, |
| 208 | + model: string, |
| 209 | + next: ModelUsage | null | undefined |
| 210 | +): Record<string, ModelUsage> | null { |
| 211 | + if (next == null) { |
| 212 | + return current ?? null; |
| 213 | + } |
| 214 | + |
| 215 | + const usagePerModel = { ...(current ?? {}) }; |
| 216 | + const modelName = model.trim() || "unknown"; |
| 217 | + usagePerModel[modelName] = accumulateUsage(usagePerModel[modelName] ?? null, usageWithRequestCount(next))!; |
| 218 | + return usagePerModel; |
| 219 | +} |
| 220 | + |
| 221 | +export { getExtensionRoot } from "./prompt"; |
1 | 222 | import * as fs from "fs"; |
2 | 223 | import * as path from "path"; |
3 | 224 | import * as os from "os"; |
4 | 225 | import * as crypto from "crypto"; |
5 | 226 | import matter from "gray-matter"; |
6 | 227 | import ejs from "ejs"; |
7 | 228 | import type { ChatCompletionContentPart, ChatCompletionMessageParam } from "openai/resources/chat/completions"; |
8 | | -import { launchNotifyScript } from "../common/notify"; |
9 | | -import { buildThinkingRequestOptions } from "../common/openai-thinking"; |
10 | | -import { supportsMultimodal } from "../common/model-capabilities"; |
| 229 | +import { launchNotifyScript } from "./common/notify"; |
| 230 | +import { buildThinkingRequestOptions } from "./common/openai-thinking"; |
| 231 | +import { supportsMultimodal } from "./common/model-capabilities"; |
11 | 232 | import { |
12 | 233 | getCompactPrompt, |
13 | 234 | getDefaultSkillPrompt, |
| 235 | + getExtensionRoot, |
14 | 236 | getRuntimeContext, |
15 | 237 | getSystemPrompt, |
16 | 238 | getTools, |
17 | 239 | type ToolDefinition, |
18 | | -} from "../prompt"; |
| 240 | +} from "./prompt"; |
19 | 241 | import { |
20 | | - type CreateOpenAIClient, |
21 | 242 | type ProcessTimeoutControl, |
22 | 243 | type ProcessTimeoutInfo, |
23 | 244 | type ToolCallExecution, |
24 | 245 | type ToolExecutionHooks, |
25 | 246 | ToolExecutor, |
26 | | -} from "../tools/executor"; |
27 | | -import { McpManager } from "../mcp/mcp-manager"; |
28 | | -import type { McpServerConfig, PermissionSettings } from "../settings"; |
29 | | -import { logApiError } from "../common/logging/error-logger"; |
30 | | -import { logOpenAIChatCompletionDebug, normalizeDebugError } from "../common/logging/debug-logger"; |
31 | | -import { killProcessTree } from "../common/system/process-tree"; |
32 | | -import { GitFileHistory } from "../common/runtime/file-history"; |
33 | | -import { getSnippet } from "../common/runtime/state"; |
| 247 | +} from "./tools/executor"; |
| 248 | +import { McpManager } from "./mcp/mcp-manager"; |
| 249 | + |
| 250 | +import { logApiError } from "./common/logging/error-logger"; |
| 251 | +import { logOpenAIChatCompletionDebug, normalizeDebugError } from "./common/logging/debug-logger"; |
| 252 | +import { killProcessTree } from "./common/system/process-tree"; |
| 253 | +import { GitFileHistory } from "./common/runtime/file-history"; |
| 254 | +import { getSnippet } from "./common/runtime/state"; |
34 | 255 | import { |
35 | 256 | appendProjectPermissionAllows, |
36 | 257 | buildPermissionToolExecution, |
37 | 258 | computeToolCallPermissions, |
38 | 259 | hasUserPermissionReplies, |
39 | | - type MessageToolPermission, |
40 | 260 | normalizeAskPermissions, |
41 | 261 | parseToolCallForPermissions, |
42 | 262 | type PermissionToolCall, |
43 | | - type UserToolPermission, |
44 | | -} from "../common/permissions"; |
45 | | - |
46 | | -import { |
47 | | - accumulateUsage, |
48 | | - accumulateUsagePerModel, |
49 | | - getCompactPromptTokenThreshold, |
50 | | - getExtensionRoot, |
51 | | - getTotalTokens, |
52 | | - isUsageRecord, |
53 | | - summarizeCompletionOptions, |
54 | | -} from "./utils"; |
55 | | -import { |
56 | | - type BashTimeoutAdjustment, |
57 | | - type LlmStreamProgress, |
58 | | - type MessageMeta, |
59 | | - type ModelUsage, |
60 | | - type SessionEntry, |
61 | | - type SessionManagerOptions, |
62 | | - type SessionMessage, |
63 | | - type SessionProcessEntry, |
64 | | - type SessionsIndex, |
65 | | - type SessionStatus, |
66 | | - type SkillInfo, |
67 | | - type UndoTarget, |
68 | | - type UserPromptContent, |
69 | | -} from "./types"; |
| 263 | +} from "./common/permissions"; |
70 | 264 |
|
71 | 265 | const MAX_SESSION_ENTRIES = 50; |
72 | 266 | const DEFAULT_NEW_PROMPT_API_URL = "https://deepcode.vegamo.cn/api/plugin/new"; |
|
0 commit comments