aegiscode 3.1.10 → 3.2.0
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/bin/aegis-cli-linux-x64 +0 -0
- package/bin/cli.js +68 -0
- package/package.json +11 -48
- package/scripts/download-binary.mjs +155 -0
- package/dist/main.js +0 -2171
- package/gui/run-gui.cjs +0 -13
- package/src/agent/Agent.ts +0 -903
- package/src/agent/SimpleAgent.ts +0 -48
- package/src/agent/index.ts +0 -54
- package/src/agent/orchestrator/AppBuilder.ts +0 -443
- package/src/agent/orchestrator/CouncilAgent.ts +0 -310
- package/src/agent/orchestrator/DiscussionRoom.ts +0 -462
- package/src/agent/orchestrator/OrchestratorAgent.ts +0 -637
- package/src/agent/orchestrator/index.ts +0 -38
- package/src/agent/orchestrator/utils.ts +0 -397
- package/src/agent/pricing.ts +0 -115
- package/src/agent/router.ts +0 -74
- package/src/agent/routerStats.ts +0 -121
- package/src/agent/types.ts +0 -318
- package/src/auth/login.ts +0 -383
- package/src/cli/config.ts +0 -189
- package/src/cli/index.ts +0 -17
- package/src/cli/middleware.ts +0 -119
- package/src/cli/types.ts +0 -75
- package/src/config/ConfigManager.ts +0 -587
- package/src/config/index.ts +0 -7
- package/src/config/types.ts +0 -584
- package/src/context/CompactionService.ts +0 -300
- package/src/context/ContextManager.ts +0 -450
- package/src/context/FileAnalyzer.ts +0 -267
- package/src/context/TokenCounter.ts +0 -265
- package/src/context/index.ts +0 -27
- package/src/context/storage/CacheStore.ts +0 -176
- package/src/context/storage/JSONLStore.ts +0 -201
- package/src/context/storage/MemoryStore.ts +0 -205
- package/src/context/storage/PersistentStore.ts +0 -327
- package/src/context/storage/index.ts +0 -9
- package/src/context/storage/pathUtils.ts +0 -114
- package/src/context/test.ts +0 -309
- package/src/context/types.ts +0 -268
- package/src/hooks/HookExecutor.ts +0 -434
- package/src/hooks/HookManager.ts +0 -596
- package/src/hooks/HookService.ts +0 -269
- package/src/hooks/Matcher.ts +0 -157
- package/src/hooks/index.ts +0 -63
- package/src/hooks/types.ts +0 -424
- package/src/main.tsx +0 -596
- package/src/mcp/HealthMonitor.ts +0 -150
- package/src/mcp/McpClient.ts +0 -491
- package/src/mcp/McpRegistry.ts +0 -321
- package/src/mcp/createMcpTool.ts +0 -251
- package/src/mcp/index.ts +0 -15
- package/src/mcp/server.ts +0 -334
- package/src/mcp/test-server.ts +0 -88
- package/src/mcp/test.ts +0 -372
- package/src/mcp/types.ts +0 -247
- package/src/memory/AgentMemoryBus.ts +0 -432
- package/src/memory/CloudSync.ts +0 -99
- package/src/memory/DriveSync.ts +0 -106
- package/src/memory/SharedMemory.ts +0 -951
- package/src/memory/index.ts +0 -14
- package/src/memory/machineFingerprint.ts +0 -40
- package/src/orchestrator/SubAgentMetadata.ts +0 -136
- package/src/prompts/builder.ts +0 -213
- package/src/prompts/default.ts +0 -144
- package/src/prompts/index.ts +0 -16
- package/src/prompts/plan.ts +0 -64
- package/src/prompts/test.ts +0 -78
- package/src/services/AnthropicChatService.ts +0 -341
- package/src/services/ChatService.ts +0 -347
- package/src/services/ClaudeCliChatService.ts +0 -256
- package/src/services/CloudSync.ts +0 -168
- package/src/services/CostLedger.ts +0 -211
- package/src/services/Heartbeat.ts +0 -135
- package/src/services/LearningCollector.ts +0 -291
- package/src/services/OllamaInstaller.ts +0 -342
- package/src/services/VersionChecker.ts +0 -445
- package/src/services/index.ts +0 -57
- package/src/services/streaming/OpenAIEventAdapter.ts +0 -126
- package/src/services/streaming/RenderingProfile.ts +0 -90
- package/src/services/streaming/StreamEventParser.ts +0 -181
- package/src/services/streaming/ThrottledRenderer.ts +0 -139
- package/src/services/streaming/TranscriptBuffer.ts +0 -574
- package/src/services/streaming/eventStatusMap.ts +0 -52
- package/src/services/streaming/index.ts +0 -46
- package/src/services/streaming/renderFormatting.ts +0 -79
- package/src/services/streaming/types.ts +0 -234
- package/src/skills/SkillLoader.ts +0 -126
- package/src/skills/SkillRegistry.ts +0 -366
- package/src/skills/index.ts +0 -48
- package/src/skills/types.ts +0 -146
- package/src/slash-commands/billing.ts +0 -70
- package/src/slash-commands/build.ts +0 -413
- package/src/slash-commands/builtinCommands.ts +0 -2733
- package/src/slash-commands/clone.ts +0 -242
- package/src/slash-commands/council.ts +0 -125
- package/src/slash-commands/custom/CustomCommandExecutor.ts +0 -143
- package/src/slash-commands/custom/CustomCommandLoader.ts +0 -251
- package/src/slash-commands/custom/CustomCommandRegistry.ts +0 -144
- package/src/slash-commands/custom/index.ts +0 -7
- package/src/slash-commands/debate.ts +0 -254
- package/src/slash-commands/gmail.ts +0 -105
- package/src/slash-commands/index.ts +0 -388
- package/src/slash-commands/mcpCommand.ts +0 -205
- package/src/slash-commands/types.ts +0 -201
- package/src/store/index.ts +0 -76
- package/src/store/selectors.ts +0 -246
- package/src/store/slices/appSlice.ts +0 -205
- package/src/store/slices/commandSlice.ts +0 -115
- package/src/store/slices/configSlice.ts +0 -46
- package/src/store/slices/focusSlice.ts +0 -64
- package/src/store/slices/index.ts +0 -9
- package/src/store/slices/sessionSlice.ts +0 -424
- package/src/store/streaming-buffer.ts +0 -425
- package/src/store/test.ts +0 -296
- package/src/store/types.ts +0 -274
- package/src/store/vanilla.ts +0 -186
- package/src/tools/builtin/bash.ts +0 -236
- package/src/tools/builtin/council.ts +0 -105
- package/src/tools/builtin/edit.ts +0 -213
- package/src/tools/builtin/glob.ts +0 -136
- package/src/tools/builtin/grep.ts +0 -263
- package/src/tools/builtin/index.ts +0 -61
- package/src/tools/builtin/memory.ts +0 -66
- package/src/tools/builtin/read.ts +0 -168
- package/src/tools/builtin/skill.ts +0 -97
- package/src/tools/builtin/snapshot.ts +0 -40
- package/src/tools/builtin/task.ts +0 -106
- package/src/tools/builtin/write.ts +0 -134
- package/src/tools/createTool.ts +0 -221
- package/src/tools/execution/ExecutionPipeline.ts +0 -263
- package/src/tools/execution/index.ts +0 -40
- package/src/tools/execution/stages/CacheStage.ts +0 -131
- package/src/tools/execution/stages/ConfirmationStage.ts +0 -203
- package/src/tools/execution/stages/DiscoveryStage.ts +0 -46
- package/src/tools/execution/stages/ExecutionStage.ts +0 -48
- package/src/tools/execution/stages/FormattingStage.ts +0 -44
- package/src/tools/execution/stages/HookStage.ts +0 -72
- package/src/tools/execution/stages/PermissionStage.ts +0 -287
- package/src/tools/execution/stages/PostHookStage.ts +0 -71
- package/src/tools/execution/stages/index.ts +0 -12
- package/src/tools/execution/test.ts +0 -266
- package/src/tools/execution/types.ts +0 -273
- package/src/tools/index.ts +0 -81
- package/src/tools/registry.ts +0 -304
- package/src/tools/schemas.ts +0 -109
- package/src/tools/test.ts +0 -220
- package/src/tools/types.ts +0 -175
- package/src/tools/validation/PermissionChecker.ts +0 -242
- package/src/tools/validation/SensitiveFileDetector.ts +0 -210
- package/src/tools/validation/index.ts +0 -11
- package/src/ui/App.tsx +0 -166
- package/src/ui/components/AegisInterface.tsx +0 -484
- package/src/ui/components/common/ChatSearch.tsx +0 -150
- package/src/ui/components/common/ErrorBoundary.tsx +0 -82
- package/src/ui/components/common/ExitMessage.tsx +0 -120
- package/src/ui/components/common/LoadingIndicator.tsx +0 -49
- package/src/ui/components/common/index.ts +0 -6
- package/src/ui/components/dialog/ConfirmationPrompt.tsx +0 -208
- package/src/ui/components/dialog/InteractiveSelector.tsx +0 -149
- package/src/ui/components/dialog/SetupWizard.tsx +0 -297
- package/src/ui/components/dialog/UpdatePrompt.tsx +0 -155
- package/src/ui/components/dialog/index.ts +0 -8
- package/src/ui/components/index.ts +0 -28
- package/src/ui/components/input/CommandSuggestions.tsx +0 -139
- package/src/ui/components/input/CustomTextInput.tsx +0 -220
- package/src/ui/components/input/InputArea.tsx +0 -361
- package/src/ui/components/input/PromptSuggestions.tsx +0 -66
- package/src/ui/components/input/index.ts +0 -6
- package/src/ui/components/layout/ChatStatusBar.tsx +0 -90
- package/src/ui/components/layout/ContextBar.tsx +0 -79
- package/src/ui/components/layout/MessageArea.tsx +0 -96
- package/src/ui/components/layout/MessageList.tsx +0 -647
- package/src/ui/components/layout/MessageSeparator.tsx +0 -26
- package/src/ui/components/layout/WelcomeMessage.tsx +0 -93
- package/src/ui/components/layout/index.ts +0 -7
- package/src/ui/components/markdown/CodeHighlighter.tsx +0 -292
- package/src/ui/components/markdown/MessageRenderer.tsx +0 -1211
- package/src/ui/components/markdown/index.ts +0 -8
- package/src/ui/components/markdown/parser.ts +0 -336
- package/src/ui/components/markdown/types.ts +0 -66
- package/src/ui/focus/FocusManager.ts +0 -137
- package/src/ui/focus/index.ts +0 -13
- package/src/ui/focus/types.ts +0 -54
- package/src/ui/focus/useFocus.ts +0 -75
- package/src/ui/hooks/index.ts +0 -11
- package/src/ui/hooks/useAgent.ts +0 -284
- package/src/ui/hooks/useCommandHistory.ts +0 -87
- package/src/ui/hooks/useCommandProcessor.ts +0 -443
- package/src/ui/hooks/useConfirmation.ts +0 -99
- package/src/ui/hooks/useCtrlCHandler.ts +0 -100
- package/src/ui/hooks/useInputBuffer.ts +0 -122
- package/src/ui/hooks/useTerminalSize.ts +0 -68
- package/src/ui/hooks/useTerminalWidth.ts +0 -5
- package/src/ui/hooks/useWindowedList.ts +0 -118
- package/src/ui/render-debugger.ts +0 -621
- package/src/ui/test.ts +0 -189
- package/src/ui/themes/ThemeManager.ts +0 -332
- package/src/ui/themes/aegisTheme.ts +0 -87
- package/src/ui/themes/darkTheme.ts +0 -87
- package/src/ui/themes/defaultTheme.ts +0 -85
- package/src/ui/themes/index.ts +0 -10
- package/src/ui/themes/lightTheme.ts +0 -89
- package/src/ui/themes/popularThemes.ts +0 -187
- package/src/ui/themes/types.ts +0 -130
- package/src/utils/clipboard.ts +0 -48
- package/src/utils/debug.ts +0 -43
- package/src/utils/environment.ts +0 -68
- package/src/utils/index.ts +0 -10
|
@@ -1,265 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Token
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { encodingForModel, type Tiktoken } from 'js-tiktoken';
|
|
8
|
-
import type { Message } from '../agent/types.js';
|
|
9
|
-
|
|
10
|
-
export class TokenCounter {
|
|
11
|
-
private static encodingCache = new Map<string, Tiktoken>();
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
*
|
|
15
|
-
*/
|
|
16
|
-
static countTokens(messages: Message[], modelName: string): number {
|
|
17
|
-
const encoding = this.getEncoding(modelName);
|
|
18
|
-
let totalTokens = 0;
|
|
19
|
-
|
|
20
|
-
for (const msg of messages) {
|
|
21
|
-
//
|
|
22
|
-
totalTokens += 4;
|
|
23
|
-
|
|
24
|
-
// Role
|
|
25
|
-
if (msg.role) {
|
|
26
|
-
totalTokens += encoding.encode(msg.role).length;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// Content
|
|
30
|
-
if (msg.content) {
|
|
31
|
-
if (typeof msg.content === 'string') {
|
|
32
|
-
totalTokens += encoding.encode(msg.content).length;
|
|
33
|
-
} else {
|
|
34
|
-
totalTokens += encoding.encode(JSON.stringify(msg.content)).length;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
//
|
|
39
|
-
if (msg.tool_calls && Array.isArray(msg.tool_calls)) {
|
|
40
|
-
totalTokens += this.countToolCallTokens(msg.tool_calls, encoding);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return totalTokens;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
*
|
|
49
|
-
*/
|
|
50
|
-
static countMessageTokens(message: Message, modelName: string): number {
|
|
51
|
-
return this.countTokens([message], modelName);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
*
|
|
56
|
-
*/
|
|
57
|
-
static countTextTokens(text: string, modelName: string): number {
|
|
58
|
-
const encoding = this.getEncoding(modelName);
|
|
59
|
-
return encoding.encode(text).length;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
*
|
|
64
|
-
*/
|
|
65
|
-
private static countToolCallTokens(
|
|
66
|
-
toolCalls: Array<{ id?: string; function?: { name?: string; arguments?: string } }>,
|
|
67
|
-
encoding: Tiktoken
|
|
68
|
-
): number {
|
|
69
|
-
let tokens = 0;
|
|
70
|
-
|
|
71
|
-
for (const call of toolCalls) {
|
|
72
|
-
//
|
|
73
|
-
tokens += 4;
|
|
74
|
-
|
|
75
|
-
if (call.id) {
|
|
76
|
-
tokens += encoding.encode(call.id).length;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (call.function) {
|
|
80
|
-
if (call.function.name) {
|
|
81
|
-
tokens += encoding.encode(call.function.name).length;
|
|
82
|
-
}
|
|
83
|
-
if (call.function.arguments) {
|
|
84
|
-
tokens += encoding.encode(call.function.arguments).length;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return tokens;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
*
|
|
94
|
-
*/
|
|
95
|
-
static shouldCompact(
|
|
96
|
-
messages: Message[],
|
|
97
|
-
modelName: string,
|
|
98
|
-
maxTokens: number,
|
|
99
|
-
thresholdPercent: number = 0.8
|
|
100
|
-
): boolean {
|
|
101
|
-
const currentTokens = this.countTokens(messages, modelName);
|
|
102
|
-
const threshold = Math.floor(maxTokens * thresholdPercent);
|
|
103
|
-
return currentTokens >= threshold;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
*
|
|
108
|
-
*/
|
|
109
|
-
private static getEncoding(modelName: string): Tiktoken {
|
|
110
|
-
//
|
|
111
|
-
const normalizedName = this.normalizeModelName(modelName);
|
|
112
|
-
|
|
113
|
-
if (!this.encodingCache.has(normalizedName)) {
|
|
114
|
-
try {
|
|
115
|
-
const encoding = encodingForModel(normalizedName as Parameters<typeof encodingForModel>[0]);
|
|
116
|
-
this.encodingCache.set(normalizedName, encoding);
|
|
117
|
-
} catch {
|
|
118
|
-
// Fallback to gpt-4o tokenizer
|
|
119
|
-
try {
|
|
120
|
-
const encoding = encodingForModel('gpt-4o');
|
|
121
|
-
this.encodingCache.set(normalizedName, encoding);
|
|
122
|
-
} catch {
|
|
123
|
-
//
|
|
124
|
-
console.warn(` ${modelName} encoding`);
|
|
125
|
-
const fallbackEncoding = {
|
|
126
|
-
encode: (text: string) => new Array(Math.ceil(text.length / 4)),
|
|
127
|
-
decode: () => '',
|
|
128
|
-
free: () => {},
|
|
129
|
-
} as unknown as Tiktoken;
|
|
130
|
-
this.encodingCache.set(normalizedName, fallbackEncoding);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
return this.encodingCache.get(normalizedName)!;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
*
|
|
139
|
-
*/
|
|
140
|
-
private static normalizeModelName(modelName: string): string {
|
|
141
|
-
// Map model names to valid js-tiktoken encodings
|
|
142
|
-
const lower = modelName.toLowerCase();
|
|
143
|
-
|
|
144
|
-
// Anthropic Claude models
|
|
145
|
-
if (lower.includes('claude-sonnet-4') || lower.includes('claude-3.5') || lower.includes('claude-3')) {
|
|
146
|
-
return 'gpt-4o';
|
|
147
|
-
}
|
|
148
|
-
if (lower.includes('claude')) {
|
|
149
|
-
return 'gpt-4o';
|
|
150
|
-
}
|
|
151
|
-
// GPT-4o variants
|
|
152
|
-
if (lower.includes('gpt-4o') || lower.includes('gpt4o')) {
|
|
153
|
-
return 'gpt-4o';
|
|
154
|
-
}
|
|
155
|
-
// GPT-4 variants
|
|
156
|
-
if (lower.includes('gpt-4') || lower.includes('gpt4')) {
|
|
157
|
-
return 'gpt-4';
|
|
158
|
-
}
|
|
159
|
-
// GPT-3.5 variants -> gpt-4o-mini (current replacement)
|
|
160
|
-
if (lower.includes('gpt-3.5') || lower.includes('gpt35') || lower.includes('gpt-3')) {
|
|
161
|
-
return 'gpt-4o-mini';
|
|
162
|
-
}
|
|
163
|
-
// Chinese GLM models
|
|
164
|
-
if (lower.includes('glm')) {
|
|
165
|
-
return 'gpt-4o';
|
|
166
|
-
}
|
|
167
|
-
// DeepSeek models
|
|
168
|
-
if (lower.includes('deepseek')) {
|
|
169
|
-
return 'gpt-4o';
|
|
170
|
-
}
|
|
171
|
-
// Llama/Mixtral/Groq models
|
|
172
|
-
if (lower.includes('llama') || lower.includes('mixtral') || lower.includes('groq')) {
|
|
173
|
-
return 'gpt-4o-mini';
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return modelName;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*/
|
|
184
|
-
static estimateTokens(text: string): number {
|
|
185
|
-
// 1 token ≈ 1.5
|
|
186
|
-
// 1 token ≈ 4
|
|
187
|
-
const chineseChars = (text.match(/[\u4e00-\u9fa5]/g) || []).length;
|
|
188
|
-
const otherChars = text.length - chineseChars;
|
|
189
|
-
return Math.ceil(chineseChars / 1.5 + otherChars / 4);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
*
|
|
194
|
-
*/
|
|
195
|
-
static estimateMessagesTokens(messages: Message[]): number {
|
|
196
|
-
let total = 0;
|
|
197
|
-
|
|
198
|
-
for (const msg of messages) {
|
|
199
|
-
//
|
|
200
|
-
total += 4;
|
|
201
|
-
|
|
202
|
-
if (msg.content) {
|
|
203
|
-
const content = typeof msg.content === 'string'
|
|
204
|
-
? msg.content
|
|
205
|
-
: JSON.stringify(msg.content);
|
|
206
|
-
total += this.estimateTokens(content);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
if (msg.tool_calls) {
|
|
210
|
-
total += this.estimateTokens(JSON.stringify(msg.tool_calls));
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return total;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
*
|
|
219
|
-
*/
|
|
220
|
-
static getRemainingTokens(
|
|
221
|
-
messages: Message[],
|
|
222
|
-
modelName: string,
|
|
223
|
-
maxContextTokens: number,
|
|
224
|
-
maxOutputTokens: number = 4096
|
|
225
|
-
): number {
|
|
226
|
-
const currentTokens = this.countTokens(messages, modelName);
|
|
227
|
-
const availableForInput = maxContextTokens - maxOutputTokens;
|
|
228
|
-
return Math.max(0, availableForInput - currentTokens);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
*
|
|
233
|
-
*/
|
|
234
|
-
static truncateMessages(
|
|
235
|
-
messages: Message[],
|
|
236
|
-
modelName: string,
|
|
237
|
-
maxTokens: number
|
|
238
|
-
): Message[] {
|
|
239
|
-
const result: Message[] = [];
|
|
240
|
-
let currentTokens = 0;
|
|
241
|
-
|
|
242
|
-
//
|
|
243
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
244
|
-
const msg = messages[i];
|
|
245
|
-
const msgTokens = this.countMessageTokens(msg, modelName);
|
|
246
|
-
|
|
247
|
-
if (currentTokens + msgTokens > maxTokens) {
|
|
248
|
-
break;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
result.unshift(msg);
|
|
252
|
-
currentTokens += msgTokens;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
return result;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
*
|
|
260
|
-
*/
|
|
261
|
-
static clearCache(): void {
|
|
262
|
-
// js-tiktoken Tiktoken
|
|
263
|
-
this.encodingCache.clear();
|
|
264
|
-
}
|
|
265
|
-
}
|
package/src/context/index.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
// 类型导
|
|
6
|
-
export * from './types.js';
|
|
7
|
-
|
|
8
|
-
// 核心类导
|
|
9
|
-
export { ContextManager } from './ContextManager.js';
|
|
10
|
-
export { TokenCounter } from './TokenCounter.js';
|
|
11
|
-
export { CompactionService } from './CompactionService.js';
|
|
12
|
-
export { FileAnalyzer } from './FileAnalyzer.js';
|
|
13
|
-
|
|
14
|
-
// 存储层导
|
|
15
|
-
export {
|
|
16
|
-
MemoryStore,
|
|
17
|
-
PersistentStore,
|
|
18
|
-
CacheStore,
|
|
19
|
-
JSONLStore,
|
|
20
|
-
getStorageRoot,
|
|
21
|
-
getProjectStoragePath,
|
|
22
|
-
getSessionFilePath,
|
|
23
|
-
escapeProjectPath,
|
|
24
|
-
detectGitBranch,
|
|
25
|
-
detectGitRemote,
|
|
26
|
-
getLatestSessionFile,
|
|
27
|
-
} from './storage/index.js';
|
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
interface CacheEntry<T> {
|
|
8
|
-
value: T;
|
|
9
|
-
createdAt: number;
|
|
10
|
-
lastAccessedAt: number;
|
|
11
|
-
ttl: number;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export class CacheStore {
|
|
15
|
-
private readonly cache: Map<string, CacheEntry<unknown>> = new Map();
|
|
16
|
-
private readonly maxSize: number;
|
|
17
|
-
private readonly defaultTtl: number;
|
|
18
|
-
|
|
19
|
-
constructor(maxSize: number = 100, defaultTtl: number = 5 * 60 * 1000) {
|
|
20
|
-
this.maxSize = maxSize;
|
|
21
|
-
this.defaultTtl = defaultTtl;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
*
|
|
26
|
-
*/
|
|
27
|
-
set<T>(key: string, value: T, ttl?: number): void {
|
|
28
|
-
// 检查是否需要淘
|
|
29
|
-
if (this.cache.size >= this.maxSize && !this.cache.has(key)) {
|
|
30
|
-
this.evictLRU();
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const now = Date.now();
|
|
34
|
-
this.cache.set(key, {
|
|
35
|
-
value,
|
|
36
|
-
createdAt: now,
|
|
37
|
-
lastAccessedAt: now,
|
|
38
|
-
ttl: ttl ?? this.defaultTtl,
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
*
|
|
44
|
-
*/
|
|
45
|
-
get<T>(key: string): T | undefined {
|
|
46
|
-
const entry = this.cache.get(key);
|
|
47
|
-
if (!entry) {
|
|
48
|
-
return undefined;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// 检查是否过
|
|
52
|
-
const now = Date.now();
|
|
53
|
-
if (now - entry.createdAt > entry.ttl) {
|
|
54
|
-
this.cache.delete(key);
|
|
55
|
-
return undefined;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// 更新访问时
|
|
59
|
-
entry.lastAccessedAt = now;
|
|
60
|
-
return entry.value as T;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
*
|
|
65
|
-
*/
|
|
66
|
-
has(key: string): boolean {
|
|
67
|
-
const entry = this.cache.get(key);
|
|
68
|
-
if (!entry) {
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const now = Date.now();
|
|
73
|
-
if (now - entry.createdAt > entry.ttl) {
|
|
74
|
-
this.cache.delete(key);
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return true;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
*
|
|
83
|
-
*/
|
|
84
|
-
delete(key: string): boolean {
|
|
85
|
-
return this.cache.delete(key);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
*
|
|
90
|
-
*/
|
|
91
|
-
async getOrSet<T>(
|
|
92
|
-
key: string,
|
|
93
|
-
factory: () => T | Promise<T>,
|
|
94
|
-
ttl?: number
|
|
95
|
-
): Promise<T> {
|
|
96
|
-
const existing = this.get<T>(key);
|
|
97
|
-
if (existing !== undefined) {
|
|
98
|
-
return existing;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const value = await factory();
|
|
102
|
-
this.set(key, value, ttl);
|
|
103
|
-
return value;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
*
|
|
108
|
-
*/
|
|
109
|
-
private evictLRU(): void {
|
|
110
|
-
let oldestKey: string | null = null;
|
|
111
|
-
let oldestTime = Infinity;
|
|
112
|
-
|
|
113
|
-
for (const [key, entry] of this.cache) {
|
|
114
|
-
if (entry.lastAccessedAt < oldestTime) {
|
|
115
|
-
oldestTime = entry.lastAccessedAt;
|
|
116
|
-
oldestKey = key;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (oldestKey) {
|
|
121
|
-
this.cache.delete(oldestKey);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
*
|
|
127
|
-
*/
|
|
128
|
-
cleanup(): number {
|
|
129
|
-
const now = Date.now();
|
|
130
|
-
let cleaned = 0;
|
|
131
|
-
|
|
132
|
-
for (const [key, entry] of this.cache) {
|
|
133
|
-
if (now - entry.createdAt > entry.ttl) {
|
|
134
|
-
this.cache.delete(key);
|
|
135
|
-
cleaned++;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return cleaned;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
*
|
|
144
|
-
*/
|
|
145
|
-
clear(): void {
|
|
146
|
-
this.cache.clear();
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
*
|
|
151
|
-
*/
|
|
152
|
-
get size(): number {
|
|
153
|
-
return this.cache.size;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
*
|
|
158
|
-
*/
|
|
159
|
-
keys(): string[] {
|
|
160
|
-
return Array.from(this.cache.keys());
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
*
|
|
165
|
-
*/
|
|
166
|
-
getStats(): {
|
|
167
|
-
size: number;
|
|
168
|
-
maxSize: number;
|
|
169
|
-
hitRate?: number;
|
|
170
|
-
} {
|
|
171
|
-
return {
|
|
172
|
-
size: this.cache.size,
|
|
173
|
-
maxSize: this.maxSize,
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
}
|
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* JSONL 文件存储
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import * as fs from 'node:fs/promises';
|
|
8
|
-
import { existsSync, createReadStream } from 'node:fs';
|
|
9
|
-
import { createInterface } from 'node:readline';
|
|
10
|
-
import * as path from 'node:path';
|
|
11
|
-
import type { JSONLEntry } from '../types.js';
|
|
12
|
-
|
|
13
|
-
export class JSONLStore {
|
|
14
|
-
private readonly filePath: string;
|
|
15
|
-
|
|
16
|
-
constructor(filePath: string) {
|
|
17
|
-
this.filePath = filePath;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
*
|
|
22
|
-
*/
|
|
23
|
-
getFilePath(): string {
|
|
24
|
-
return this.filePath;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
*
|
|
29
|
-
*/
|
|
30
|
-
exists(): boolean {
|
|
31
|
-
return existsSync(this.filePath);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
*
|
|
36
|
-
*/
|
|
37
|
-
async append(entry: JSONLEntry): Promise<void> {
|
|
38
|
-
// 确保父目录存
|
|
39
|
-
await fs.mkdir(path.dirname(this.filePath), { recursive: true });
|
|
40
|
-
|
|
41
|
-
// 序列化并追
|
|
42
|
-
const line = JSON.stringify(entry) + '\n';
|
|
43
|
-
await fs.appendFile(this.filePath, line, 'utf-8');
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
*
|
|
48
|
-
*/
|
|
49
|
-
async appendBatch(entries: JSONLEntry[]): Promise<void> {
|
|
50
|
-
if (entries.length === 0) return;
|
|
51
|
-
|
|
52
|
-
await fs.mkdir(path.dirname(this.filePath), { recursive: true });
|
|
53
|
-
|
|
54
|
-
const lines = entries.map(entry => JSON.stringify(entry)).join('\n') + '\n';
|
|
55
|
-
await fs.appendFile(this.filePath, lines, 'utf-8');
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
*
|
|
60
|
-
*/
|
|
61
|
-
async readAll(): Promise<JSONLEntry[]> {
|
|
62
|
-
if (!this.exists()) {
|
|
63
|
-
return [];
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const content = await fs.readFile(this.filePath, 'utf-8');
|
|
67
|
-
const lines = content.split('\n').filter(line => line.trim().length > 0);
|
|
68
|
-
|
|
69
|
-
const entries: JSONLEntry[] = [];
|
|
70
|
-
for (const line of lines) {
|
|
71
|
-
try {
|
|
72
|
-
entries.push(JSON.parse(line) as JSONLEntry);
|
|
73
|
-
} catch (parseError) {
|
|
74
|
-
console.warn(`[JSONLStore] 解析 JSON 行失败: ${line.substring(0, 100)}...`);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return entries;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
*
|
|
82
|
-
*/
|
|
83
|
-
async readStream(callback: (entry: JSONLEntry) => void | Promise<void>): Promise<void> {
|
|
84
|
-
return new Promise((resolve, reject) => {
|
|
85
|
-
if (!this.exists()) {
|
|
86
|
-
resolve();
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const fileStream = createReadStream(this.filePath, { encoding: 'utf-8' });
|
|
91
|
-
const rl = createInterface({
|
|
92
|
-
input: fileStream,
|
|
93
|
-
crlfDelay: Number.POSITIVE_INFINITY,
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
const processLine = async (line: string) => {
|
|
97
|
-
const trimmed = line.trim();
|
|
98
|
-
if (trimmed.length === 0) return;
|
|
99
|
-
|
|
100
|
-
try {
|
|
101
|
-
const entry = JSON.parse(trimmed) as JSONLEntry;
|
|
102
|
-
await callback(entry);
|
|
103
|
-
} catch (error) {
|
|
104
|
-
console.warn(`[JSONLStore] 解析失败: ${trimmed.substring(0, 50)}...`);
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
rl.on('line', (line) => {
|
|
109
|
-
processLine(line).catch(reject);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
rl.on('close', () => resolve());
|
|
113
|
-
rl.on('error', reject);
|
|
114
|
-
fileStream.on('error', reject);
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
*
|
|
120
|
-
*/
|
|
121
|
-
async readLast(count: number): Promise<JSONLEntry[]> {
|
|
122
|
-
const all = await this.readAll();
|
|
123
|
-
return all.slice(-count);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
*
|
|
128
|
-
*/
|
|
129
|
-
async filter(predicate: (entry: JSONLEntry) => boolean): Promise<JSONLEntry[]> {
|
|
130
|
-
const all = await this.readAll();
|
|
131
|
-
return all.filter(predicate);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
*
|
|
136
|
-
*/
|
|
137
|
-
async count(): Promise<number> {
|
|
138
|
-
if (!this.exists()) {
|
|
139
|
-
return 0;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
let count = 0;
|
|
143
|
-
await this.readStream(() => {
|
|
144
|
-
count++;
|
|
145
|
-
});
|
|
146
|
-
return count;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
*
|
|
151
|
-
*/
|
|
152
|
-
async readAfterCompaction(): Promise<JSONLEntry[]> {
|
|
153
|
-
const all = await this.readAll();
|
|
154
|
-
|
|
155
|
-
// 找到最后一个压缩边
|
|
156
|
-
let boundaryIndex = -1;
|
|
157
|
-
for (let i = all.length - 1; i >= 0; i--) {
|
|
158
|
-
if (all[i].subtype === 'compact_boundary') {
|
|
159
|
-
boundaryIndex = i;
|
|
160
|
-
break;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// 如果没有压缩边界,返回所有记
|
|
165
|
-
if (boundaryIndex === -1) {
|
|
166
|
-
return all;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// 返回压缩边界之后的记录(包括压缩总
|
|
170
|
-
return all.slice(boundaryIndex);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
*
|
|
175
|
-
*/
|
|
176
|
-
async clear(): Promise<void> {
|
|
177
|
-
if (this.exists()) {
|
|
178
|
-
await fs.writeFile(this.filePath, '', 'utf-8');
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
*
|
|
184
|
-
*/
|
|
185
|
-
async delete(): Promise<void> {
|
|
186
|
-
if (this.exists()) {
|
|
187
|
-
await fs.unlink(this.filePath);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
*
|
|
193
|
-
*/
|
|
194
|
-
async getFileSize(): Promise<number> {
|
|
195
|
-
if (!this.exists()) {
|
|
196
|
-
return 0;
|
|
197
|
-
}
|
|
198
|
-
const stats = await fs.stat(this.filePath);
|
|
199
|
-
return stats.size;
|
|
200
|
-
}
|
|
201
|
-
}
|