aegiscode 3.1.8 → 3.1.10
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/README.md +23 -15
- package/dist/main.js +549 -552
- package/package.json +4 -2
- package/src/agent/Agent.ts +903 -0
- package/src/agent/SimpleAgent.ts +48 -0
- package/src/agent/index.ts +54 -0
- package/src/agent/orchestrator/AppBuilder.ts +443 -0
- package/src/agent/orchestrator/CouncilAgent.ts +310 -0
- package/src/agent/orchestrator/DiscussionRoom.ts +462 -0
- package/src/agent/orchestrator/OrchestratorAgent.ts +637 -0
- package/src/agent/orchestrator/index.ts +38 -0
- package/src/agent/orchestrator/utils.ts +397 -0
- package/src/agent/pricing.ts +115 -0
- package/src/agent/router.ts +74 -0
- package/src/agent/routerStats.ts +121 -0
- package/src/agent/types.ts +318 -0
- package/src/auth/login.ts +383 -0
- package/src/cli/config.ts +189 -0
- package/src/cli/index.ts +17 -0
- package/src/cli/middleware.ts +119 -0
- package/src/cli/types.ts +75 -0
- package/src/config/ConfigManager.ts +587 -0
- package/src/config/index.ts +7 -0
- package/src/config/types.ts +584 -0
- package/src/context/CompactionService.ts +300 -0
- package/src/context/ContextManager.ts +450 -0
- package/src/context/FileAnalyzer.ts +267 -0
- package/src/context/TokenCounter.ts +265 -0
- package/src/context/index.ts +27 -0
- package/src/context/storage/CacheStore.ts +176 -0
- package/src/context/storage/JSONLStore.ts +201 -0
- package/src/context/storage/MemoryStore.ts +205 -0
- package/src/context/storage/PersistentStore.ts +327 -0
- package/src/context/storage/index.ts +9 -0
- package/src/context/storage/pathUtils.ts +114 -0
- package/src/context/test.ts +309 -0
- package/src/context/types.ts +268 -0
- package/src/hooks/HookExecutor.ts +434 -0
- package/src/hooks/HookManager.ts +596 -0
- package/src/hooks/HookService.ts +269 -0
- package/src/hooks/Matcher.ts +157 -0
- package/src/hooks/index.ts +63 -0
- package/src/hooks/types.ts +424 -0
- package/src/main.tsx +596 -0
- package/src/mcp/HealthMonitor.ts +150 -0
- package/src/mcp/McpClient.ts +491 -0
- package/src/mcp/McpRegistry.ts +321 -0
- package/src/mcp/createMcpTool.ts +251 -0
- package/src/mcp/index.ts +15 -0
- package/src/mcp/server.ts +334 -0
- package/src/mcp/test-server.ts +88 -0
- package/src/mcp/test.ts +372 -0
- package/src/mcp/types.ts +247 -0
- package/src/memory/AgentMemoryBus.ts +432 -0
- package/src/memory/CloudSync.ts +99 -0
- package/src/memory/DriveSync.ts +106 -0
- package/src/memory/SharedMemory.ts +951 -0
- package/src/memory/index.ts +14 -0
- package/src/memory/machineFingerprint.ts +40 -0
- package/src/orchestrator/SubAgentMetadata.ts +136 -0
- package/src/prompts/builder.ts +213 -0
- package/src/prompts/default.ts +144 -0
- package/src/prompts/index.ts +16 -0
- package/src/prompts/plan.ts +64 -0
- package/src/prompts/test.ts +78 -0
- package/src/services/AnthropicChatService.ts +341 -0
- package/src/services/ChatService.ts +347 -0
- package/src/services/ClaudeCliChatService.ts +256 -0
- package/src/services/CloudSync.ts +168 -0
- package/src/services/CostLedger.ts +211 -0
- package/src/services/Heartbeat.ts +135 -0
- package/src/services/LearningCollector.ts +291 -0
- package/src/services/OllamaInstaller.ts +342 -0
- package/src/services/VersionChecker.ts +445 -0
- package/src/services/index.ts +57 -0
- package/src/services/streaming/OpenAIEventAdapter.ts +126 -0
- package/src/services/streaming/RenderingProfile.ts +90 -0
- package/src/services/streaming/StreamEventParser.ts +181 -0
- package/src/services/streaming/ThrottledRenderer.ts +139 -0
- package/src/services/streaming/TranscriptBuffer.ts +574 -0
- package/src/services/streaming/eventStatusMap.ts +52 -0
- package/src/services/streaming/index.ts +46 -0
- package/src/services/streaming/renderFormatting.ts +79 -0
- package/src/services/streaming/types.ts +234 -0
- package/src/skills/SkillLoader.ts +126 -0
- package/src/skills/SkillRegistry.ts +366 -0
- package/src/skills/index.ts +48 -0
- package/src/skills/types.ts +146 -0
- package/src/slash-commands/billing.ts +70 -0
- package/src/slash-commands/build.ts +413 -0
- package/src/slash-commands/builtinCommands.ts +2733 -0
- package/src/slash-commands/clone.ts +242 -0
- package/src/slash-commands/council.ts +125 -0
- package/src/slash-commands/custom/CustomCommandExecutor.ts +143 -0
- package/src/slash-commands/custom/CustomCommandLoader.ts +251 -0
- package/src/slash-commands/custom/CustomCommandRegistry.ts +144 -0
- package/src/slash-commands/custom/index.ts +7 -0
- package/src/slash-commands/debate.ts +254 -0
- package/src/slash-commands/gmail.ts +105 -0
- package/src/slash-commands/index.ts +388 -0
- package/src/slash-commands/mcpCommand.ts +205 -0
- package/src/slash-commands/types.ts +201 -0
- package/src/store/index.ts +76 -0
- package/src/store/selectors.ts +246 -0
- package/src/store/slices/appSlice.ts +205 -0
- package/src/store/slices/commandSlice.ts +115 -0
- package/src/store/slices/configSlice.ts +46 -0
- package/src/store/slices/focusSlice.ts +64 -0
- package/src/store/slices/index.ts +9 -0
- package/src/store/slices/sessionSlice.ts +424 -0
- package/src/store/streaming-buffer.ts +425 -0
- package/src/store/test.ts +296 -0
- package/src/store/types.ts +274 -0
- package/src/store/vanilla.ts +186 -0
- package/src/tools/builtin/bash.ts +236 -0
- package/src/tools/builtin/council.ts +105 -0
- package/src/tools/builtin/edit.ts +213 -0
- package/src/tools/builtin/glob.ts +136 -0
- package/src/tools/builtin/grep.ts +263 -0
- package/src/tools/builtin/index.ts +61 -0
- package/src/tools/builtin/memory.ts +66 -0
- package/src/tools/builtin/read.ts +168 -0
- package/src/tools/builtin/skill.ts +97 -0
- package/src/tools/builtin/snapshot.ts +40 -0
- package/src/tools/builtin/task.ts +106 -0
- package/src/tools/builtin/write.ts +134 -0
- package/src/tools/createTool.ts +221 -0
- package/src/tools/execution/ExecutionPipeline.ts +263 -0
- package/src/tools/execution/index.ts +40 -0
- package/src/tools/execution/stages/CacheStage.ts +131 -0
- package/src/tools/execution/stages/ConfirmationStage.ts +203 -0
- package/src/tools/execution/stages/DiscoveryStage.ts +46 -0
- package/src/tools/execution/stages/ExecutionStage.ts +48 -0
- package/src/tools/execution/stages/FormattingStage.ts +44 -0
- package/src/tools/execution/stages/HookStage.ts +72 -0
- package/src/tools/execution/stages/PermissionStage.ts +287 -0
- package/src/tools/execution/stages/PostHookStage.ts +71 -0
- package/src/tools/execution/stages/index.ts +12 -0
- package/src/tools/execution/test.ts +266 -0
- package/src/tools/execution/types.ts +273 -0
- package/src/tools/index.ts +81 -0
- package/src/tools/registry.ts +304 -0
- package/src/tools/schemas.ts +109 -0
- package/src/tools/test.ts +220 -0
- package/src/tools/types.ts +175 -0
- package/src/tools/validation/PermissionChecker.ts +242 -0
- package/src/tools/validation/SensitiveFileDetector.ts +210 -0
- package/src/tools/validation/index.ts +11 -0
- package/src/ui/App.tsx +166 -0
- package/src/ui/components/AegisInterface.tsx +484 -0
- package/src/ui/components/common/ChatSearch.tsx +150 -0
- package/src/ui/components/common/ErrorBoundary.tsx +82 -0
- package/src/ui/components/common/ExitMessage.tsx +120 -0
- package/src/ui/components/common/LoadingIndicator.tsx +49 -0
- package/src/ui/components/common/index.ts +6 -0
- package/src/ui/components/dialog/ConfirmationPrompt.tsx +208 -0
- package/src/ui/components/dialog/InteractiveSelector.tsx +149 -0
- package/src/ui/components/dialog/SetupWizard.tsx +297 -0
- package/src/ui/components/dialog/UpdatePrompt.tsx +155 -0
- package/src/ui/components/dialog/index.ts +8 -0
- package/src/ui/components/index.ts +28 -0
- package/src/ui/components/input/CommandSuggestions.tsx +139 -0
- package/src/ui/components/input/CustomTextInput.tsx +220 -0
- package/src/ui/components/input/InputArea.tsx +361 -0
- package/src/ui/components/input/PromptSuggestions.tsx +66 -0
- package/src/ui/components/input/index.ts +6 -0
- package/src/ui/components/layout/ChatStatusBar.tsx +90 -0
- package/src/ui/components/layout/ContextBar.tsx +79 -0
- package/src/ui/components/layout/MessageArea.tsx +96 -0
- package/src/ui/components/layout/MessageList.tsx +647 -0
- package/src/ui/components/layout/MessageSeparator.tsx +26 -0
- package/src/ui/components/layout/WelcomeMessage.tsx +93 -0
- package/src/ui/components/layout/index.ts +7 -0
- package/src/ui/components/markdown/CodeHighlighter.tsx +292 -0
- package/src/ui/components/markdown/MessageRenderer.tsx +1211 -0
- package/src/ui/components/markdown/index.ts +8 -0
- package/src/ui/components/markdown/parser.ts +336 -0
- package/src/ui/components/markdown/types.ts +66 -0
- package/src/ui/focus/FocusManager.ts +137 -0
- package/src/ui/focus/index.ts +13 -0
- package/src/ui/focus/types.ts +54 -0
- package/src/ui/focus/useFocus.ts +75 -0
- package/src/ui/hooks/index.ts +11 -0
- package/src/ui/hooks/useAgent.ts +284 -0
- package/src/ui/hooks/useCommandHistory.ts +87 -0
- package/src/ui/hooks/useCommandProcessor.ts +443 -0
- package/src/ui/hooks/useConfirmation.ts +99 -0
- package/src/ui/hooks/useCtrlCHandler.ts +100 -0
- package/src/ui/hooks/useInputBuffer.ts +122 -0
- package/src/ui/hooks/useTerminalSize.ts +68 -0
- package/src/ui/hooks/useTerminalWidth.ts +5 -0
- package/src/ui/hooks/useWindowedList.ts +118 -0
- package/src/ui/render-debugger.ts +621 -0
- package/src/ui/test.ts +189 -0
- package/src/ui/themes/ThemeManager.ts +332 -0
- package/src/ui/themes/aegisTheme.ts +87 -0
- package/src/ui/themes/darkTheme.ts +87 -0
- package/src/ui/themes/defaultTheme.ts +85 -0
- package/src/ui/themes/index.ts +10 -0
- package/src/ui/themes/lightTheme.ts +89 -0
- package/src/ui/themes/popularThemes.ts +187 -0
- package/src/ui/themes/types.ts +130 -0
- package/src/utils/clipboard.ts +48 -0
- package/src/utils/debug.ts +43 -0
- package/src/utils/environment.ts +68 -0
- package/src/utils/index.ts +10 -0
|
@@ -0,0 +1,265 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
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';
|
|
@@ -0,0 +1,176 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
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
|
+
}
|