codemini-cli 0.3.0 → 0.3.2
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 +2 -1
- package/souls/anime.md +13 -2
- package/souls/caveman.md +12 -2
- package/souls/ceo.md +14 -3
- package/souls/default.md +10 -2
- package/souls/pirate.md +13 -3
- package/souls/playful.md +13 -3
- package/souls/professional.md +13 -3
- package/src/cli.js +2 -1
- package/src/commands/chat.js +3 -0
- package/src/commands/run.js +6 -2
- package/src/core/agent-loop.js +13 -9
- package/src/core/ast.js +2 -55
- package/src/core/bounded-cache.js +121 -0
- package/src/core/chat-runtime.js +552 -143
- package/src/core/config-store.js +30 -0
- package/src/core/constants.js +171 -0
- package/src/core/crypto-utils.js +18 -0
- package/src/core/memory-policy.js +27 -0
- package/src/core/memory-prompt.js +45 -0
- package/src/core/memory-store.js +181 -0
- package/src/core/paths.js +8 -0
- package/src/core/project-index.js +6 -34
- package/src/core/provider/anthropic.js +388 -0
- package/src/core/provider/index.js +37 -0
- package/src/core/soul.js +3 -2
- package/src/core/tools.js +175 -71
- package/src/tui/chat-app.js +291 -38
- package/src/tui/tool-activity/presenters/command.js +14 -1
- package/src/tui/tool-activity/presenters/files.js +23 -1
- package/src/tui/tool-activity/presenters/system.js +1 -1
- package/src/tui/tool-narration/presenters/glob.js +2 -2
- package/src/tui/tool-narration/presenters/grep.js +2 -2
- package/src/tui/tool-narration/presenters/list.js +2 -2
- package/src/tui/tool-narration/presenters/run.js +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codemini-cli",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Coding CLI optimized for small-model workflows and Windows PowerShell",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -47,6 +47,7 @@
|
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@cursorless/tree-sitter-wasms": "^0.5.0",
|
|
50
|
+
"@anthropic-ai/sdk": "^0.82.0",
|
|
50
51
|
"ink": "^6.3.1",
|
|
51
52
|
"openai": "^6.33.0",
|
|
52
53
|
"react": "^19.0.0",
|
package/souls/anime.md
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
1
|
Respond with a light anime-inspired tone.
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
|
|
3
|
+
Style guidelines:
|
|
4
|
+
- Be cheerful and encouraging, like a helpful companion on an adventure.
|
|
5
|
+
- Use occasional playful expressions (e.g. "好嘞,开干!", "搞定啦~", "Let's go!") but keep them natural and brief.
|
|
6
|
+
- Add a touch of enthusiasm to progress updates and completions.
|
|
7
|
+
- When something goes wrong, stay upbeat and frame it as a solvable challenge.
|
|
8
|
+
- Use em dashes, tildes, or exclamation marks sparingly for personality.
|
|
9
|
+
|
|
10
|
+
Boundaries:
|
|
11
|
+
- Never sacrifice clarity, accuracy, or usefulness for style.
|
|
12
|
+
- Do not overdo catchphrases, memes, or anime references.
|
|
13
|
+
- Do not use this style as an excuse to be verbose — stay concise.
|
|
14
|
+
- Technical terms, code, file paths, and command output must remain precise and unchanged.
|
package/souls/caveman.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
1
|
Respond with a simple caveman-inspired tone.
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
|
|
3
|
+
Style guidelines:
|
|
4
|
+
- Keep sentences short, punchy, and concrete.
|
|
5
|
+
- Speak in direct, action-oriented phrases — "We fix bug. Code good now." style.
|
|
6
|
+
- Use simple metaphors from the physical world (hunting, building, fire) when explaining concepts.
|
|
7
|
+
- Celebrate successes with primal enthusiasm — "Bug crushed! Tribe safe."
|
|
8
|
+
|
|
9
|
+
Boundaries:
|
|
10
|
+
- Keep explanations readable and technically accurate.
|
|
11
|
+
- Do not make wording so primitive that instructions or code suggestions become unclear.
|
|
12
|
+
- Technical terms, code, file paths, and command output must remain precise and unchanged.
|
|
13
|
+
- Never sacrifice correctness for the caveman gimmick.
|
package/souls/ceo.md
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
-
Respond with a bold CEO-style tone.
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
Respond with a bold, decisive CEO-style tone.
|
|
2
|
+
|
|
3
|
+
Style guidelines:
|
|
4
|
+
- Speak with confidence and urgency — focus on what matters, cut the noise.
|
|
5
|
+
- Frame every decision around impact, tradeoffs, and execution velocity.
|
|
6
|
+
- Use executive phrasing: "Here's the play...", "The right call is...", "Let's ship this."
|
|
7
|
+
- Acknowledge risks briefly, then commit to a clear direction.
|
|
8
|
+
- Celebrate wins like closing a deal — "Clean execution. Moving on."
|
|
9
|
+
|
|
10
|
+
Boundaries:
|
|
11
|
+
- Do not imitate any real person or use cringe corporate buzzwords ("synergy", "paradigm shift").
|
|
12
|
+
- Do not let the style override careful technical judgment — precision still wins.
|
|
13
|
+
- Technical terms, code, file paths, and command output must remain precise and unchanged.
|
|
14
|
+
- Stay concise — CEOs do not ramble.
|
package/souls/default.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
1
|
Respond in a clear, calm, helpful tone.
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
|
|
3
|
+
Style guidelines:
|
|
4
|
+
- Be concise, friendly, and practical in every response.
|
|
5
|
+
- Prioritize clarity and directness over embellishment.
|
|
6
|
+
- Use simple, natural language — no forced personality or quirks.
|
|
7
|
+
|
|
8
|
+
Boundaries:
|
|
9
|
+
- Avoid roleplay, slang overload, or exaggerated personality.
|
|
10
|
+
- Never sacrifice accuracy or usefulness for style.
|
|
11
|
+
- Technical terms, code, file paths, and command output must remain precise and unchanged.
|
package/souls/pirate.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
Respond with a playful pirate-inspired tone.
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
Respond with a playful pirate-inspired tone, matey.
|
|
2
|
+
|
|
3
|
+
Style guidelines:
|
|
4
|
+
- Use light nautical flavor and pirate expressions — "Aye", "Shiver me timbers", "Arrr", "Set sail".
|
|
5
|
+
- Frame tasks as voyages and adventures — "Let's chart a course for that bug."
|
|
6
|
+
- Celebrate successes like plundering treasure — "Shipshape! Bug walkin' the plank."
|
|
7
|
+
- Keep the tone adventurous but grounded.
|
|
8
|
+
|
|
9
|
+
Boundaries:
|
|
10
|
+
- Keep the answer clear, useful, and technically accurate first, pirate flavor second.
|
|
11
|
+
- Do not overdo slang — every sentence should still be understandable on first read.
|
|
12
|
+
- Technical terms, code, file paths, and command output must remain precise and unchanged.
|
|
13
|
+
- Never let roleplay reduce precision or hide important warnings.
|
package/souls/playful.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
Respond with a witty and
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
Respond with a witty, lively, and slightly cheeky tone.
|
|
2
|
+
|
|
3
|
+
Style guidelines:
|
|
4
|
+
- Add personality and humor naturally — a well-placed quip or clever analogy goes a long way.
|
|
5
|
+
- Use casual, conversational phrasing — "So here's the fun part...", "Plot twist:", "Easy fix incoming."
|
|
6
|
+
- React to bugs and errors with good-natured humor — "Well, that's a creative way to break things."
|
|
7
|
+
- Celebrate wins with flair — "Nailed it. Next?"
|
|
8
|
+
|
|
9
|
+
Boundaries:
|
|
10
|
+
- Keep the answer readable and practical first — humor is the seasoning, not the main dish.
|
|
11
|
+
- Do not let jokes obscure instructions, warnings, or technical accuracy.
|
|
12
|
+
- Technical terms, code, file paths, and command output must remain precise and unchanged.
|
|
13
|
+
- Avoid sarcasm that could feel dismissive of the user's question.
|
package/souls/professional.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
-
Respond in a polished, professional tone.
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
Respond in a polished, professional, and authoritative tone.
|
|
2
|
+
|
|
3
|
+
Style guidelines:
|
|
4
|
+
- Keep phrasing precise, confident, and concise — like a senior engineer briefing a team.
|
|
5
|
+
- Prefer structured explanations: numbered steps, clear headings, and logical flow.
|
|
6
|
+
- State conclusions first, then back them up — lead with the answer, follow with reasoning.
|
|
7
|
+
- Use measured, deliberate language — "The recommended approach is...", "This ensures..."
|
|
8
|
+
|
|
9
|
+
Boundaries:
|
|
10
|
+
- Prefer structured explanations over playful expression.
|
|
11
|
+
- Avoid colloquialisms, emojis, or overly casual phrasing.
|
|
12
|
+
- Technical terms, code, file paths, and command output must remain precise and unchanged.
|
|
13
|
+
- Never pad responses with filler — every sentence should earn its place.
|
package/src/cli.js
CHANGED
|
@@ -3,8 +3,9 @@ import { handleRun } from './commands/run.js';
|
|
|
3
3
|
import { handleConfig } from './commands/config.js';
|
|
4
4
|
import { handleDoctor } from './commands/doctor.js';
|
|
5
5
|
import { handleSkill } from './commands/skill.js';
|
|
6
|
+
import pkg from '../package.json' with { type: 'json' };
|
|
6
7
|
|
|
7
|
-
const VERSION =
|
|
8
|
+
const VERSION = pkg.version;
|
|
8
9
|
|
|
9
10
|
function printHelp() {
|
|
10
11
|
console.log(`codemini ${VERSION}
|
package/src/commands/chat.js
CHANGED
|
@@ -92,16 +92,19 @@ export async function handleChat(args) {
|
|
|
92
92
|
const React = (await import('react')).default;
|
|
93
93
|
const { render } = await import('ink');
|
|
94
94
|
const { ChatApp } = await import('../tui/chat-app.js');
|
|
95
|
+
|
|
95
96
|
const instance = render(
|
|
96
97
|
React.createElement(ChatApp, {
|
|
97
98
|
runtime,
|
|
98
99
|
sessionId: session.id,
|
|
99
100
|
model: parsed.model || config.model.name,
|
|
101
|
+
sdkProvider: config.sdk?.provider || 'openai-compatible',
|
|
100
102
|
language: config.ui?.language || 'zh',
|
|
101
103
|
shellName: config.shell?.default || 'powershell',
|
|
102
104
|
safeMode: config.policy?.safe_mode !== false,
|
|
103
105
|
version: pkg.version
|
|
104
106
|
})
|
|
105
107
|
);
|
|
108
|
+
|
|
106
109
|
await instance.waitUntilExit();
|
|
107
110
|
}
|
package/src/commands/run.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { loadConfig } from '../core/config-store.js';
|
|
2
2
|
import { buildDefaultSystemPrompt } from '../core/default-system-prompt.js';
|
|
3
3
|
import { runAgentLoop } from '../core/agent-loop.js';
|
|
4
|
-
import { createChatCompletion } from '../core/provider/
|
|
4
|
+
import { createChatCompletion } from '../core/provider/index.js';
|
|
5
5
|
import { buildSystemPromptWithSoul } from '../core/soul.js';
|
|
6
6
|
import { getBuiltinTools } from '../core/tools.js';
|
|
7
|
+
import { buildMemorySnapshot } from '../core/memory-prompt.js';
|
|
7
8
|
|
|
8
9
|
function parseRunArgs(args) {
|
|
9
10
|
const parsed = {
|
|
@@ -39,7 +40,9 @@ export async function handleRun(args) {
|
|
|
39
40
|
workspaceRoot: process.cwd(),
|
|
40
41
|
config
|
|
41
42
|
});
|
|
42
|
-
const
|
|
43
|
+
const soulPrompt = await buildSystemPromptWithSoul(buildDefaultSystemPrompt(config), config);
|
|
44
|
+
const memorySnapshot = await buildMemorySnapshot({ config, workspaceRoot: process.cwd() }).catch(() => '');
|
|
45
|
+
const systemPrompt = [soulPrompt, memorySnapshot].filter(Boolean).join('\n\n');
|
|
43
46
|
|
|
44
47
|
const result = await runAgentLoop({
|
|
45
48
|
systemPrompt,
|
|
@@ -52,6 +55,7 @@ export async function handleRun(args) {
|
|
|
52
55
|
maxSteps: parsed.maxSteps,
|
|
53
56
|
requestCompletion: async ({ messages, tools, model }) =>
|
|
54
57
|
createChatCompletion({
|
|
58
|
+
sdkProvider: config.sdk?.provider,
|
|
55
59
|
baseUrl: config.gateway.base_url,
|
|
56
60
|
apiKey: config.gateway.api_key,
|
|
57
61
|
model,
|
package/src/core/agent-loop.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import os from 'node:os';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import fs from 'node:fs/promises';
|
|
4
|
+
import { BoundedCache } from './bounded-cache.js';
|
|
4
5
|
|
|
5
6
|
function safeJsonParse(raw) {
|
|
6
7
|
if (!raw || typeof raw !== 'string') return {};
|
|
@@ -206,8 +207,16 @@ const TOOL_RESULTS_SUBDIR = 'tool-results';
|
|
|
206
207
|
|
|
207
208
|
let currentResultDir = null;
|
|
208
209
|
let resultDirReady = false;
|
|
209
|
-
const storedResults = new
|
|
210
|
-
|
|
210
|
+
const storedResults = new BoundedCache({
|
|
211
|
+
maxSize: 64,
|
|
212
|
+
ttlMs: 30 * 60 * 1000,
|
|
213
|
+
onEvict(key, value) {
|
|
214
|
+
if (value?.filePath) {
|
|
215
|
+
fs.unlink(value.filePath).catch(() => {});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}); // callId -> { filePath, summary }
|
|
219
|
+
const readCache = new BoundedCache({ maxSize: 128, ttlMs: 10 * 60 * 1000 }); // "path:startLine:endLine:mtimeMs" -> true
|
|
211
220
|
|
|
212
221
|
function generatePreview(content) {
|
|
213
222
|
if (content.length <= PREVIEW_SIZE_BYTES) {
|
|
@@ -272,7 +281,7 @@ Summary: ${summary}
|
|
|
272
281
|
|
|
273
282
|
export function clearResultStore() {
|
|
274
283
|
const files = [];
|
|
275
|
-
for (const [, val] of storedResults) {
|
|
284
|
+
for (const [, val] of storedResults.entries()) {
|
|
276
285
|
files.push(val.filePath);
|
|
277
286
|
}
|
|
278
287
|
storedResults.clear();
|
|
@@ -288,11 +297,6 @@ export function checkReadDedup(filePath, startLine, endLine, mtimeMs) {
|
|
|
288
297
|
return true;
|
|
289
298
|
}
|
|
290
299
|
readCache.set(key, true);
|
|
291
|
-
// Keep cache bounded
|
|
292
|
-
if (readCache.size > 100) {
|
|
293
|
-
const firstKey = readCache.keys().next().value;
|
|
294
|
-
readCache.delete(firstKey);
|
|
295
|
-
}
|
|
296
300
|
return false;
|
|
297
301
|
}
|
|
298
302
|
|
|
@@ -687,7 +691,7 @@ export async function runAgentLoop({
|
|
|
687
691
|
const assistantText = completion.text || '';
|
|
688
692
|
lastAssistantText = assistantText || lastAssistantText;
|
|
689
693
|
|
|
690
|
-
const assistantMessage = { role: 'assistant', content: assistantText };
|
|
694
|
+
const assistantMessage = { role: 'assistant', content: completion?.content ?? assistantText };
|
|
691
695
|
if (toolCalls.length > 0) {
|
|
692
696
|
assistantMessage.tool_calls = toolCalls.map((tc) => ({
|
|
693
697
|
id: tc.id,
|
package/src/core/ast.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import fs from 'node:fs/promises';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import crypto from 'node:crypto';
|
|
4
3
|
import { createRequire } from 'node:module';
|
|
5
4
|
import { Parser, Language, Query } from 'web-tree-sitter';
|
|
5
|
+
import { LANGUAGE_ALIASES, EXTENSION_LANGUAGE_MAP } from './constants.js';
|
|
6
|
+
import { sha256Prefixed as sha256 } from './crypto-utils.js';
|
|
6
7
|
|
|
7
8
|
const require = createRequire(import.meta.url);
|
|
8
9
|
|
|
@@ -20,56 +21,6 @@ const WRAPPER_NODE_TYPES = new Set([
|
|
|
20
21
|
'template_function',
|
|
21
22
|
'template_type'
|
|
22
23
|
]);
|
|
23
|
-
const LANGUAGE_ALIASES = {
|
|
24
|
-
javascript: 'js',
|
|
25
|
-
js: 'js',
|
|
26
|
-
jsx: 'js',
|
|
27
|
-
typescript: 'ts',
|
|
28
|
-
ts: 'ts',
|
|
29
|
-
tsx: 'tsx',
|
|
30
|
-
python: 'python',
|
|
31
|
-
py: 'python',
|
|
32
|
-
go: 'go',
|
|
33
|
-
c: 'c',
|
|
34
|
-
cpp: 'cpp',
|
|
35
|
-
'c++': 'cpp',
|
|
36
|
-
bash: 'bash',
|
|
37
|
-
sh: 'bash',
|
|
38
|
-
shell: 'bash',
|
|
39
|
-
java: 'java',
|
|
40
|
-
rust: 'rust',
|
|
41
|
-
rs: 'rust',
|
|
42
|
-
csharp: 'csharp',
|
|
43
|
-
'c#': 'csharp',
|
|
44
|
-
cs: 'csharp',
|
|
45
|
-
php: 'php',
|
|
46
|
-
ruby: 'ruby',
|
|
47
|
-
rb: 'ruby'
|
|
48
|
-
};
|
|
49
|
-
const EXTENSION_LANGUAGE_MAP = {
|
|
50
|
-
'.js': 'js',
|
|
51
|
-
'.jsx': 'js',
|
|
52
|
-
'.mjs': 'js',
|
|
53
|
-
'.cjs': 'js',
|
|
54
|
-
'.ts': 'ts',
|
|
55
|
-
'.tsx': 'tsx',
|
|
56
|
-
'.py': 'python',
|
|
57
|
-
'.go': 'go',
|
|
58
|
-
'.c': 'c',
|
|
59
|
-
'.h': 'c',
|
|
60
|
-
'.cpp': 'cpp',
|
|
61
|
-
'.cc': 'cpp',
|
|
62
|
-
'.cxx': 'cpp',
|
|
63
|
-
'.hpp': 'cpp',
|
|
64
|
-
'.hh': 'cpp',
|
|
65
|
-
'.java': 'java',
|
|
66
|
-
'.rs': 'rust',
|
|
67
|
-
'.cs': 'csharp',
|
|
68
|
-
'.php': 'php',
|
|
69
|
-
'.rb': 'ruby',
|
|
70
|
-
'.sh': 'bash',
|
|
71
|
-
'.bash': 'bash'
|
|
72
|
-
};
|
|
73
24
|
const LANGUAGE_WASM_PATHS = {
|
|
74
25
|
js: require.resolve('@cursorless/tree-sitter-wasms/out/tree-sitter-javascript.wasm'),
|
|
75
26
|
ts: require.resolve('@cursorless/tree-sitter-wasms/out/tree-sitter-typescript.wasm'),
|
|
@@ -94,10 +45,6 @@ const parserInitPromise = Parser.init({
|
|
|
94
45
|
});
|
|
95
46
|
const languageCache = new Map();
|
|
96
47
|
|
|
97
|
-
function sha256(input) {
|
|
98
|
-
return `sha256:${crypto.createHash('sha256').update(String(input || '')).digest('hex')}`;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
48
|
function clipText(text, maxLen = 220) {
|
|
102
49
|
const normalized = String(text || '').replace(/\s+/g, ' ').trim();
|
|
103
50
|
if (normalized.length <= maxLen) return normalized;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 有限大小 + TTL 的 Map 缓存,带主动清理和可选的 onEvict 钩子。
|
|
3
|
+
* 用于替代无界 Map 以防止长时间运行时的内存泄漏。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const DEFAULT_MAX_SIZE = 128;
|
|
7
|
+
const DEFAULT_TTL_MS = 10 * 60 * 1000; // 10 分钟
|
|
8
|
+
|
|
9
|
+
export class BoundedCache {
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
this._maxSize = options.maxSize ?? DEFAULT_MAX_SIZE;
|
|
12
|
+
this._ttlMs = options.ttlMs ?? DEFAULT_TTL_MS;
|
|
13
|
+
this._onEvict = options.onEvict ?? null;
|
|
14
|
+
this._map = new Map();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get(key) {
|
|
18
|
+
const entry = this._map.get(key);
|
|
19
|
+
if (!entry) return undefined;
|
|
20
|
+
if (this._expired(entry)) {
|
|
21
|
+
this._removeEntry(key, entry);
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
return entry.value;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
has(key) {
|
|
28
|
+
return this.get(key) !== undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
set(key, value) {
|
|
32
|
+
if (this._map.has(key)) {
|
|
33
|
+
const old = this._map.get(key);
|
|
34
|
+
if (this._onEvict && old !== undefined) {
|
|
35
|
+
try { this._onEvict(key, old.value); } catch {}
|
|
36
|
+
}
|
|
37
|
+
this._map.delete(key);
|
|
38
|
+
}
|
|
39
|
+
this._map.set(key, { value, ts: Date.now() });
|
|
40
|
+
this._evict();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
delete(key) {
|
|
44
|
+
const entry = this._map.get(key);
|
|
45
|
+
if (!entry) return false;
|
|
46
|
+
this._removeEntry(key, entry);
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
clear() {
|
|
51
|
+
if (this._onEvict) {
|
|
52
|
+
for (const [key, entry] of this._map) {
|
|
53
|
+
try { this._onEvict(key, entry.value); } catch {}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
this._map.clear();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
get size() {
|
|
60
|
+
this._prune();
|
|
61
|
+
return this._map.size;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
keys() {
|
|
65
|
+
this._prune();
|
|
66
|
+
return this._map.keys();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
values() {
|
|
70
|
+
this._prune();
|
|
71
|
+
const out = [];
|
|
72
|
+
for (const entry of this._map.values()) {
|
|
73
|
+
out.push(entry.value);
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
entries() {
|
|
79
|
+
this._prune();
|
|
80
|
+
const out = [];
|
|
81
|
+
for (const [key, entry] of this._map.entries()) {
|
|
82
|
+
out.push([key, entry.value]);
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ─── 内部方法 ──────────────────────────────────────────────────────
|
|
88
|
+
|
|
89
|
+
_expired(entry) {
|
|
90
|
+
return Date.now() - entry.ts > this._ttlMs;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
_removeEntry(key, entry) {
|
|
94
|
+
this._map.delete(key);
|
|
95
|
+
if (this._onEvict) {
|
|
96
|
+
try { this._onEvict(key, entry.value); } catch {}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 清理所有过期条目 */
|
|
101
|
+
_prune() {
|
|
102
|
+
for (const [key, entry] of this._map) {
|
|
103
|
+
if (this._expired(entry)) {
|
|
104
|
+
this._removeEntry(key, entry);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 按数量裁剪最旧的条目 */
|
|
110
|
+
_evict() {
|
|
111
|
+
this._prune();
|
|
112
|
+
if (this._map.size <= this._maxSize) return;
|
|
113
|
+
const iter = this._map.keys();
|
|
114
|
+
while (this._map.size > this._maxSize) {
|
|
115
|
+
const oldest = iter.next().value;
|
|
116
|
+
if (oldest === undefined) break;
|
|
117
|
+
const entry = this._map.get(oldest);
|
|
118
|
+
this._removeEntry(oldest, entry);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|