@shareai-lab/kode 1.0.69 → 1.0.71
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 +205 -72
- package/README.zh-CN.md +246 -0
- package/cli.js +62 -0
- package/package.json +45 -25
- package/scripts/postinstall.js +56 -0
- package/src/ProjectOnboarding.tsx +180 -0
- package/src/Tool.ts +53 -0
- package/src/commands/approvedTools.ts +53 -0
- package/src/commands/bug.tsx +20 -0
- package/src/commands/clear.ts +43 -0
- package/src/commands/compact.ts +120 -0
- package/src/commands/config.tsx +19 -0
- package/src/commands/cost.ts +18 -0
- package/src/commands/ctx_viz.ts +209 -0
- package/src/commands/doctor.ts +24 -0
- package/src/commands/help.tsx +19 -0
- package/src/commands/init.ts +37 -0
- package/src/commands/listen.ts +42 -0
- package/src/commands/login.tsx +51 -0
- package/src/commands/logout.tsx +40 -0
- package/src/commands/mcp.ts +41 -0
- package/src/commands/model.tsx +40 -0
- package/src/commands/modelstatus.tsx +20 -0
- package/src/commands/onboarding.tsx +34 -0
- package/src/commands/pr_comments.ts +59 -0
- package/src/commands/refreshCommands.ts +54 -0
- package/src/commands/release-notes.ts +34 -0
- package/src/commands/resume.tsx +30 -0
- package/src/commands/review.ts +49 -0
- package/src/commands/terminalSetup.ts +221 -0
- package/src/commands.ts +136 -0
- package/src/components/ApproveApiKey.tsx +93 -0
- package/src/components/AsciiLogo.tsx +13 -0
- package/src/components/AutoUpdater.tsx +148 -0
- package/src/components/Bug.tsx +367 -0
- package/src/components/Config.tsx +289 -0
- package/src/components/ConsoleOAuthFlow.tsx +326 -0
- package/src/components/Cost.tsx +23 -0
- package/src/components/CostThresholdDialog.tsx +46 -0
- package/src/components/CustomSelect/option-map.ts +42 -0
- package/src/components/CustomSelect/select-option.tsx +52 -0
- package/src/components/CustomSelect/select.tsx +143 -0
- package/src/components/CustomSelect/use-select-state.ts +414 -0
- package/src/components/CustomSelect/use-select.ts +35 -0
- package/src/components/FallbackToolUseRejectedMessage.tsx +15 -0
- package/src/components/FileEditToolUpdatedMessage.tsx +66 -0
- package/src/components/Help.tsx +215 -0
- package/src/components/HighlightedCode.tsx +33 -0
- package/src/components/InvalidConfigDialog.tsx +113 -0
- package/src/components/Link.tsx +32 -0
- package/src/components/LogSelector.tsx +86 -0
- package/src/components/Logo.tsx +145 -0
- package/src/components/MCPServerApprovalDialog.tsx +100 -0
- package/src/components/MCPServerDialogCopy.tsx +25 -0
- package/src/components/MCPServerMultiselectDialog.tsx +109 -0
- package/src/components/Message.tsx +219 -0
- package/src/components/MessageResponse.tsx +15 -0
- package/src/components/MessageSelector.tsx +211 -0
- package/src/components/ModeIndicator.tsx +88 -0
- package/src/components/ModelConfig.tsx +301 -0
- package/src/components/ModelListManager.tsx +223 -0
- package/src/components/ModelSelector.tsx +3208 -0
- package/src/components/ModelStatusDisplay.tsx +228 -0
- package/src/components/Onboarding.tsx +274 -0
- package/src/components/PressEnterToContinue.tsx +11 -0
- package/src/components/PromptInput.tsx +710 -0
- package/src/components/SentryErrorBoundary.ts +33 -0
- package/src/components/Spinner.tsx +129 -0
- package/src/components/StructuredDiff.tsx +184 -0
- package/src/components/TextInput.tsx +246 -0
- package/src/components/TokenWarning.tsx +31 -0
- package/src/components/ToolUseLoader.tsx +40 -0
- package/src/components/TrustDialog.tsx +106 -0
- package/src/components/binary-feedback/BinaryFeedback.tsx +63 -0
- package/src/components/binary-feedback/BinaryFeedbackOption.tsx +111 -0
- package/src/components/binary-feedback/BinaryFeedbackView.tsx +172 -0
- package/src/components/binary-feedback/utils.ts +220 -0
- package/src/components/messages/AssistantBashOutputMessage.tsx +22 -0
- package/src/components/messages/AssistantLocalCommandOutputMessage.tsx +45 -0
- package/src/components/messages/AssistantRedactedThinkingMessage.tsx +19 -0
- package/src/components/messages/AssistantTextMessage.tsx +144 -0
- package/src/components/messages/AssistantThinkingMessage.tsx +40 -0
- package/src/components/messages/AssistantToolUseMessage.tsx +123 -0
- package/src/components/messages/UserBashInputMessage.tsx +28 -0
- package/src/components/messages/UserCommandMessage.tsx +30 -0
- package/src/components/messages/UserKodingInputMessage.tsx +28 -0
- package/src/components/messages/UserPromptMessage.tsx +35 -0
- package/src/components/messages/UserTextMessage.tsx +39 -0
- package/src/components/messages/UserToolResultMessage/UserToolCanceledMessage.tsx +12 -0
- package/src/components/messages/UserToolResultMessage/UserToolErrorMessage.tsx +36 -0
- package/src/components/messages/UserToolResultMessage/UserToolRejectMessage.tsx +31 -0
- package/src/components/messages/UserToolResultMessage/UserToolResultMessage.tsx +57 -0
- package/src/components/messages/UserToolResultMessage/UserToolSuccessMessage.tsx +35 -0
- package/src/components/messages/UserToolResultMessage/utils.tsx +56 -0
- package/src/components/permissions/BashPermissionRequest/BashPermissionRequest.tsx +121 -0
- package/src/components/permissions/FallbackPermissionRequest.tsx +155 -0
- package/src/components/permissions/FileEditPermissionRequest/FileEditPermissionRequest.tsx +182 -0
- package/src/components/permissions/FileEditPermissionRequest/FileEditToolDiff.tsx +75 -0
- package/src/components/permissions/FileWritePermissionRequest/FileWritePermissionRequest.tsx +164 -0
- package/src/components/permissions/FileWritePermissionRequest/FileWriteToolDiff.tsx +81 -0
- package/src/components/permissions/FilesystemPermissionRequest/FilesystemPermissionRequest.tsx +242 -0
- package/src/components/permissions/PermissionRequest.tsx +103 -0
- package/src/components/permissions/PermissionRequestTitle.tsx +69 -0
- package/src/components/permissions/hooks.ts +44 -0
- package/src/components/permissions/toolUseOptions.ts +59 -0
- package/src/components/permissions/utils.ts +23 -0
- package/src/constants/betas.ts +5 -0
- package/src/constants/claude-asterisk-ascii-art.tsx +238 -0
- package/src/constants/figures.ts +4 -0
- package/src/constants/keys.ts +3 -0
- package/src/constants/macros.ts +6 -0
- package/src/constants/models.ts +935 -0
- package/src/constants/oauth.ts +18 -0
- package/src/constants/product.ts +17 -0
- package/src/constants/prompts.ts +177 -0
- package/src/constants/releaseNotes.ts +7 -0
- package/src/context/PermissionContext.tsx +149 -0
- package/src/context.ts +278 -0
- package/src/cost-tracker.ts +84 -0
- package/src/entrypoints/cli.tsx +1498 -0
- package/src/entrypoints/mcp.ts +176 -0
- package/src/history.ts +25 -0
- package/src/hooks/useApiKeyVerification.ts +59 -0
- package/src/hooks/useArrowKeyHistory.ts +55 -0
- package/src/hooks/useCanUseTool.ts +138 -0
- package/src/hooks/useCancelRequest.ts +39 -0
- package/src/hooks/useDoublePress.ts +42 -0
- package/src/hooks/useExitOnCtrlCD.ts +31 -0
- package/src/hooks/useInterval.ts +25 -0
- package/src/hooks/useLogMessages.ts +16 -0
- package/src/hooks/useLogStartupTime.ts +12 -0
- package/src/hooks/useNotifyAfterTimeout.ts +65 -0
- package/src/hooks/usePermissionRequestLogging.ts +44 -0
- package/src/hooks/useSlashCommandTypeahead.ts +137 -0
- package/src/hooks/useTerminalSize.ts +49 -0
- package/src/hooks/useTextInput.ts +315 -0
- package/src/messages.ts +37 -0
- package/src/permissions.ts +268 -0
- package/src/query.ts +704 -0
- package/src/screens/ConfigureNpmPrefix.tsx +197 -0
- package/src/screens/Doctor.tsx +219 -0
- package/src/screens/LogList.tsx +68 -0
- package/src/screens/REPL.tsx +792 -0
- package/src/screens/ResumeConversation.tsx +68 -0
- package/src/services/browserMocks.ts +66 -0
- package/src/services/claude.ts +1947 -0
- package/src/services/customCommands.ts +683 -0
- package/src/services/fileFreshness.ts +377 -0
- package/src/services/mcpClient.ts +564 -0
- package/src/services/mcpServerApproval.tsx +50 -0
- package/src/services/notifier.ts +40 -0
- package/src/services/oauth.ts +357 -0
- package/src/services/openai.ts +796 -0
- package/src/services/sentry.ts +3 -0
- package/src/services/statsig.ts +171 -0
- package/src/services/statsigStorage.ts +86 -0
- package/src/services/systemReminder.ts +406 -0
- package/src/services/vcr.ts +161 -0
- package/src/tools/ArchitectTool/ArchitectTool.tsx +122 -0
- package/src/tools/ArchitectTool/prompt.ts +15 -0
- package/src/tools/AskExpertModelTool/AskExpertModelTool.tsx +505 -0
- package/src/tools/BashTool/BashTool.tsx +270 -0
- package/src/tools/BashTool/BashToolResultMessage.tsx +38 -0
- package/src/tools/BashTool/OutputLine.tsx +48 -0
- package/src/tools/BashTool/prompt.ts +174 -0
- package/src/tools/BashTool/utils.ts +56 -0
- package/src/tools/FileEditTool/FileEditTool.tsx +316 -0
- package/src/tools/FileEditTool/prompt.ts +51 -0
- package/src/tools/FileEditTool/utils.ts +58 -0
- package/src/tools/FileReadTool/FileReadTool.tsx +371 -0
- package/src/tools/FileReadTool/prompt.ts +7 -0
- package/src/tools/FileWriteTool/FileWriteTool.tsx +297 -0
- package/src/tools/FileWriteTool/prompt.ts +10 -0
- package/src/tools/GlobTool/GlobTool.tsx +119 -0
- package/src/tools/GlobTool/prompt.ts +8 -0
- package/src/tools/GrepTool/GrepTool.tsx +147 -0
- package/src/tools/GrepTool/prompt.ts +11 -0
- package/src/tools/MCPTool/MCPTool.tsx +106 -0
- package/src/tools/MCPTool/prompt.ts +3 -0
- package/src/tools/MemoryReadTool/MemoryReadTool.tsx +127 -0
- package/src/tools/MemoryReadTool/prompt.ts +3 -0
- package/src/tools/MemoryWriteTool/MemoryWriteTool.tsx +89 -0
- package/src/tools/MemoryWriteTool/prompt.ts +3 -0
- package/src/tools/MultiEditTool/MultiEditTool.tsx +366 -0
- package/src/tools/MultiEditTool/prompt.ts +45 -0
- package/src/tools/NotebookEditTool/NotebookEditTool.tsx +298 -0
- package/src/tools/NotebookEditTool/prompt.ts +3 -0
- package/src/tools/NotebookReadTool/NotebookReadTool.tsx +266 -0
- package/src/tools/NotebookReadTool/prompt.ts +3 -0
- package/src/tools/StickerRequestTool/StickerRequestTool.tsx +93 -0
- package/src/tools/StickerRequestTool/prompt.ts +19 -0
- package/src/tools/TaskTool/TaskTool.tsx +382 -0
- package/src/tools/TaskTool/constants.ts +1 -0
- package/src/tools/TaskTool/prompt.ts +56 -0
- package/src/tools/ThinkTool/ThinkTool.tsx +56 -0
- package/src/tools/ThinkTool/prompt.ts +12 -0
- package/src/tools/TodoWriteTool/TodoWriteTool.tsx +289 -0
- package/src/tools/TodoWriteTool/prompt.ts +63 -0
- package/src/tools/lsTool/lsTool.tsx +269 -0
- package/src/tools/lsTool/prompt.ts +2 -0
- package/src/tools.ts +63 -0
- package/src/types/PermissionMode.ts +120 -0
- package/src/types/RequestContext.ts +72 -0
- package/src/utils/Cursor.ts +436 -0
- package/src/utils/PersistentShell.ts +373 -0
- package/src/utils/agentStorage.ts +97 -0
- package/src/utils/array.ts +3 -0
- package/src/utils/ask.tsx +98 -0
- package/src/utils/auth.ts +13 -0
- package/src/utils/autoCompactCore.ts +223 -0
- package/src/utils/autoUpdater.ts +318 -0
- package/src/utils/betas.ts +20 -0
- package/src/utils/browser.ts +14 -0
- package/src/utils/cleanup.ts +72 -0
- package/src/utils/commands.ts +261 -0
- package/src/utils/config.ts +771 -0
- package/src/utils/conversationRecovery.ts +54 -0
- package/src/utils/debugLogger.ts +1123 -0
- package/src/utils/diff.ts +42 -0
- package/src/utils/env.ts +57 -0
- package/src/utils/errors.ts +21 -0
- package/src/utils/exampleCommands.ts +108 -0
- package/src/utils/execFileNoThrow.ts +51 -0
- package/src/utils/expertChatStorage.ts +136 -0
- package/src/utils/file.ts +402 -0
- package/src/utils/fileRecoveryCore.ts +71 -0
- package/src/utils/format.tsx +44 -0
- package/src/utils/generators.ts +62 -0
- package/src/utils/git.ts +92 -0
- package/src/utils/globalLogger.ts +77 -0
- package/src/utils/http.ts +10 -0
- package/src/utils/imagePaste.ts +38 -0
- package/src/utils/json.ts +13 -0
- package/src/utils/log.ts +382 -0
- package/src/utils/markdown.ts +213 -0
- package/src/utils/messageContextManager.ts +289 -0
- package/src/utils/messages.tsx +938 -0
- package/src/utils/model.ts +836 -0
- package/src/utils/permissions/filesystem.ts +118 -0
- package/src/utils/ripgrep.ts +167 -0
- package/src/utils/sessionState.ts +49 -0
- package/src/utils/state.ts +25 -0
- package/src/utils/style.ts +29 -0
- package/src/utils/terminal.ts +49 -0
- package/src/utils/theme.ts +122 -0
- package/src/utils/thinking.ts +144 -0
- package/src/utils/todoStorage.ts +431 -0
- package/src/utils/tokens.ts +43 -0
- package/src/utils/toolExecutionController.ts +163 -0
- package/src/utils/unaryLogging.ts +26 -0
- package/src/utils/user.ts +37 -0
- package/src/utils/validate.ts +165 -0
- package/cli.mjs +0 -1803
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import { statSync, existsSync, watchFile, unwatchFile } from 'fs'
|
|
2
|
+
import {
|
|
3
|
+
emitReminderEvent,
|
|
4
|
+
systemReminderService,
|
|
5
|
+
} from '../services/systemReminder'
|
|
6
|
+
import { getAgentFilePath } from '../utils/agentStorage'
|
|
7
|
+
import { logEvent } from '../services/statsig'
|
|
8
|
+
|
|
9
|
+
interface FileTimestamp {
|
|
10
|
+
path: string
|
|
11
|
+
lastRead: number
|
|
12
|
+
lastModified: number
|
|
13
|
+
size: number
|
|
14
|
+
lastAgentEdit?: number // Track when Agent last edited this file
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface FileFreshnessState {
|
|
18
|
+
readTimestamps: Map<string, FileTimestamp>
|
|
19
|
+
editConflicts: Set<string>
|
|
20
|
+
sessionFiles: Set<string>
|
|
21
|
+
watchedTodoFiles: Map<string, string> // agentId -> filePath
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
class FileFreshnessService {
|
|
25
|
+
private state: FileFreshnessState = {
|
|
26
|
+
readTimestamps: new Map(),
|
|
27
|
+
editConflicts: new Set(),
|
|
28
|
+
sessionFiles: new Set(),
|
|
29
|
+
watchedTodoFiles: new Map(),
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
constructor() {
|
|
33
|
+
this.setupEventListeners()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Setup event listeners for session management
|
|
38
|
+
*/
|
|
39
|
+
private setupEventListeners(): void {
|
|
40
|
+
// Listen for session startup events through the SystemReminderService
|
|
41
|
+
systemReminderService.addEventListener(
|
|
42
|
+
'session:startup',
|
|
43
|
+
(context: any) => {
|
|
44
|
+
// Reset session state on startup
|
|
45
|
+
this.resetSession()
|
|
46
|
+
|
|
47
|
+
// Log session startup
|
|
48
|
+
logEvent('file_freshness_session_startup', {
|
|
49
|
+
agentId: context.agentId || 'default',
|
|
50
|
+
timestamp: context.timestamp,
|
|
51
|
+
})
|
|
52
|
+
},
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Record file read operation with timestamp tracking
|
|
58
|
+
*/
|
|
59
|
+
public recordFileRead(filePath: string): void {
|
|
60
|
+
try {
|
|
61
|
+
if (!existsSync(filePath)) {
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const stats = statSync(filePath)
|
|
66
|
+
const timestamp: FileTimestamp = {
|
|
67
|
+
path: filePath,
|
|
68
|
+
lastRead: Date.now(),
|
|
69
|
+
lastModified: stats.mtimeMs,
|
|
70
|
+
size: stats.size,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
this.state.readTimestamps.set(filePath, timestamp)
|
|
74
|
+
this.state.sessionFiles.add(filePath)
|
|
75
|
+
|
|
76
|
+
// Emit file read event for system reminders
|
|
77
|
+
emitReminderEvent('file:read', {
|
|
78
|
+
filePath,
|
|
79
|
+
timestamp: timestamp.lastRead,
|
|
80
|
+
size: timestamp.size,
|
|
81
|
+
modified: timestamp.lastModified,
|
|
82
|
+
})
|
|
83
|
+
} catch (error) {
|
|
84
|
+
console.error(`Error recording file read for ${filePath}:`, error)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Check if file has been modified since last read
|
|
90
|
+
*/
|
|
91
|
+
public checkFileFreshness(filePath: string): {
|
|
92
|
+
isFresh: boolean
|
|
93
|
+
lastRead?: number
|
|
94
|
+
currentModified?: number
|
|
95
|
+
conflict: boolean
|
|
96
|
+
} {
|
|
97
|
+
const recorded = this.state.readTimestamps.get(filePath)
|
|
98
|
+
|
|
99
|
+
if (!recorded) {
|
|
100
|
+
return { isFresh: true, conflict: false }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
if (!existsSync(filePath)) {
|
|
105
|
+
return { isFresh: false, conflict: true }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const currentStats = statSync(filePath)
|
|
109
|
+
const isFresh = currentStats.mtimeMs <= recorded.lastModified
|
|
110
|
+
const conflict = !isFresh
|
|
111
|
+
|
|
112
|
+
if (conflict) {
|
|
113
|
+
this.state.editConflicts.add(filePath)
|
|
114
|
+
|
|
115
|
+
// Emit file conflict event
|
|
116
|
+
emitReminderEvent('file:conflict', {
|
|
117
|
+
filePath,
|
|
118
|
+
lastRead: recorded.lastRead,
|
|
119
|
+
lastModified: recorded.lastModified,
|
|
120
|
+
currentModified: currentStats.mtimeMs,
|
|
121
|
+
sizeDiff: currentStats.size - recorded.size,
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {
|
|
126
|
+
isFresh,
|
|
127
|
+
lastRead: recorded.lastRead,
|
|
128
|
+
currentModified: currentStats.mtimeMs,
|
|
129
|
+
conflict,
|
|
130
|
+
}
|
|
131
|
+
} catch (error) {
|
|
132
|
+
console.error(`Error checking freshness for ${filePath}:`, error)
|
|
133
|
+
return { isFresh: false, conflict: true }
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Record file edit operation by Agent
|
|
139
|
+
*/
|
|
140
|
+
public recordFileEdit(filePath: string, content?: string): void {
|
|
141
|
+
try {
|
|
142
|
+
const now = Date.now()
|
|
143
|
+
|
|
144
|
+
// Update recorded timestamp after edit
|
|
145
|
+
if (existsSync(filePath)) {
|
|
146
|
+
const stats = statSync(filePath)
|
|
147
|
+
const existing = this.state.readTimestamps.get(filePath)
|
|
148
|
+
|
|
149
|
+
if (existing) {
|
|
150
|
+
existing.lastModified = stats.mtimeMs
|
|
151
|
+
existing.size = stats.size
|
|
152
|
+
existing.lastAgentEdit = now // Mark this as Agent-initiated edit
|
|
153
|
+
this.state.readTimestamps.set(filePath, existing)
|
|
154
|
+
} else {
|
|
155
|
+
// Create new record for Agent-edited file
|
|
156
|
+
const timestamp: FileTimestamp = {
|
|
157
|
+
path: filePath,
|
|
158
|
+
lastRead: now,
|
|
159
|
+
lastModified: stats.mtimeMs,
|
|
160
|
+
size: stats.size,
|
|
161
|
+
lastAgentEdit: now,
|
|
162
|
+
}
|
|
163
|
+
this.state.readTimestamps.set(filePath, timestamp)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Remove from conflicts since we just edited it
|
|
168
|
+
this.state.editConflicts.delete(filePath)
|
|
169
|
+
|
|
170
|
+
// Emit file edit event
|
|
171
|
+
emitReminderEvent('file:edited', {
|
|
172
|
+
filePath,
|
|
173
|
+
timestamp: now,
|
|
174
|
+
contentLength: content?.length || 0,
|
|
175
|
+
source: 'agent',
|
|
176
|
+
})
|
|
177
|
+
} catch (error) {
|
|
178
|
+
console.error(`Error recording file edit for ${filePath}:`, error)
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
public generateFileModificationReminder(filePath: string): string | null {
|
|
183
|
+
const recorded = this.state.readTimestamps.get(filePath)
|
|
184
|
+
|
|
185
|
+
if (!recorded) {
|
|
186
|
+
return null
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
if (!existsSync(filePath)) {
|
|
191
|
+
return `Note: ${filePath} was deleted since last read.`
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const currentStats = statSync(filePath)
|
|
195
|
+
const isModified = currentStats.mtimeMs > recorded.lastModified
|
|
196
|
+
|
|
197
|
+
if (!isModified) {
|
|
198
|
+
return null
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Check if this was an Agent-initiated change
|
|
202
|
+
// Use small time tolerance to handle filesystem timestamp precision issues
|
|
203
|
+
const TIME_TOLERANCE_MS = 100
|
|
204
|
+
if (
|
|
205
|
+
recorded.lastAgentEdit &&
|
|
206
|
+
recorded.lastAgentEdit >= recorded.lastModified - TIME_TOLERANCE_MS
|
|
207
|
+
) {
|
|
208
|
+
// Agent modified this file recently, no reminder needed
|
|
209
|
+
// (context already contains before/after content)
|
|
210
|
+
return null
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// External modification detected - generate reminder
|
|
214
|
+
return `Note: ${filePath} was modified externally since last read. The file may have changed outside of this session.`
|
|
215
|
+
} catch (error) {
|
|
216
|
+
console.error(`Error checking modification for ${filePath}:`, error)
|
|
217
|
+
return null
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
public getConflictedFiles(): string[] {
|
|
222
|
+
return Array.from(this.state.editConflicts)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
public getSessionFiles(): string[] {
|
|
226
|
+
return Array.from(this.state.sessionFiles)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
public resetSession(): void {
|
|
230
|
+
// Clean up existing todo file watchers
|
|
231
|
+
this.state.watchedTodoFiles.forEach(filePath => {
|
|
232
|
+
try {
|
|
233
|
+
unwatchFile(filePath)
|
|
234
|
+
} catch (error) {
|
|
235
|
+
console.error(`Error unwatching file ${filePath}:`, error)
|
|
236
|
+
}
|
|
237
|
+
})
|
|
238
|
+
|
|
239
|
+
this.state = {
|
|
240
|
+
readTimestamps: new Map(),
|
|
241
|
+
editConflicts: new Set(),
|
|
242
|
+
sessionFiles: new Set(),
|
|
243
|
+
watchedTodoFiles: new Map(),
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Start watching todo file for an agent
|
|
249
|
+
*/
|
|
250
|
+
public startWatchingTodoFile(agentId: string): void {
|
|
251
|
+
try {
|
|
252
|
+
const filePath = getAgentFilePath(agentId)
|
|
253
|
+
|
|
254
|
+
// Don't watch if already watching
|
|
255
|
+
if (this.state.watchedTodoFiles.has(agentId)) {
|
|
256
|
+
return
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
this.state.watchedTodoFiles.set(agentId, filePath)
|
|
260
|
+
|
|
261
|
+
// Record initial state if file exists
|
|
262
|
+
if (existsSync(filePath)) {
|
|
263
|
+
this.recordFileRead(filePath)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Start watching for changes
|
|
267
|
+
watchFile(filePath, { interval: 1000 }, (curr, prev) => {
|
|
268
|
+
// Check if this was an external modification
|
|
269
|
+
const reminder = this.generateFileModificationReminder(filePath)
|
|
270
|
+
if (reminder) {
|
|
271
|
+
// File was modified externally, emit todo change reminder
|
|
272
|
+
emitReminderEvent('todo:file_changed', {
|
|
273
|
+
agentId,
|
|
274
|
+
filePath,
|
|
275
|
+
reminder,
|
|
276
|
+
timestamp: Date.now(),
|
|
277
|
+
currentStats: { mtime: curr.mtime, size: curr.size },
|
|
278
|
+
previousStats: { mtime: prev.mtime, size: prev.size },
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
})
|
|
282
|
+
} catch (error) {
|
|
283
|
+
console.error(
|
|
284
|
+
`Error starting todo file watch for agent ${agentId}:`,
|
|
285
|
+
error,
|
|
286
|
+
)
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Stop watching todo file for an agent
|
|
292
|
+
*/
|
|
293
|
+
public stopWatchingTodoFile(agentId: string): void {
|
|
294
|
+
try {
|
|
295
|
+
const filePath = this.state.watchedTodoFiles.get(agentId)
|
|
296
|
+
if (filePath) {
|
|
297
|
+
unwatchFile(filePath)
|
|
298
|
+
this.state.watchedTodoFiles.delete(agentId)
|
|
299
|
+
}
|
|
300
|
+
} catch (error) {
|
|
301
|
+
console.error(
|
|
302
|
+
`Error stopping todo file watch for agent ${agentId}:`,
|
|
303
|
+
error,
|
|
304
|
+
)
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
public getFileInfo(filePath: string): FileTimestamp | null {
|
|
309
|
+
return this.state.readTimestamps.get(filePath) || null
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
public isFileTracked(filePath: string): boolean {
|
|
313
|
+
return this.state.readTimestamps.has(filePath)
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Retrieves files prioritized for recovery during conversation compression
|
|
318
|
+
*
|
|
319
|
+
* Selects recently accessed files based on:
|
|
320
|
+
* - File access recency (most recent first)
|
|
321
|
+
* - File type relevance (excludes dependencies, build artifacts)
|
|
322
|
+
* - Development workflow importance
|
|
323
|
+
*
|
|
324
|
+
* Used to maintain coding context when conversation history is compressed
|
|
325
|
+
*/
|
|
326
|
+
public getImportantFiles(maxFiles: number = 5): Array<{
|
|
327
|
+
path: string
|
|
328
|
+
timestamp: number
|
|
329
|
+
size: number
|
|
330
|
+
}> {
|
|
331
|
+
return Array.from(this.state.readTimestamps.entries())
|
|
332
|
+
.map(([path, info]) => ({
|
|
333
|
+
path,
|
|
334
|
+
timestamp: info.lastRead,
|
|
335
|
+
size: info.size,
|
|
336
|
+
}))
|
|
337
|
+
.filter(file => this.isValidForRecovery(file.path))
|
|
338
|
+
.sort((a, b) => b.timestamp - a.timestamp) // Newest first
|
|
339
|
+
.slice(0, maxFiles)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Determines which files are suitable for automatic recovery
|
|
344
|
+
*
|
|
345
|
+
* Excludes files that are typically not relevant for development context:
|
|
346
|
+
* - Build artifacts and generated files
|
|
347
|
+
* - Dependencies and cached files
|
|
348
|
+
* - Temporary files and system directories
|
|
349
|
+
*/
|
|
350
|
+
private isValidForRecovery(filePath: string): boolean {
|
|
351
|
+
return (
|
|
352
|
+
!filePath.includes('node_modules') &&
|
|
353
|
+
!filePath.includes('.git') &&
|
|
354
|
+
!filePath.startsWith('/tmp') &&
|
|
355
|
+
!filePath.includes('.cache') &&
|
|
356
|
+
!filePath.includes('dist/') &&
|
|
357
|
+
!filePath.includes('build/')
|
|
358
|
+
)
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
export const fileFreshnessService = new FileFreshnessService()
|
|
363
|
+
|
|
364
|
+
export const recordFileRead = (filePath: string) =>
|
|
365
|
+
fileFreshnessService.recordFileRead(filePath)
|
|
366
|
+
export const recordFileEdit = (filePath: string, content?: string) =>
|
|
367
|
+
fileFreshnessService.recordFileEdit(filePath, content)
|
|
368
|
+
export const checkFileFreshness = (filePath: string) =>
|
|
369
|
+
fileFreshnessService.checkFileFreshness(filePath)
|
|
370
|
+
export const generateFileModificationReminder = (filePath: string) =>
|
|
371
|
+
fileFreshnessService.generateFileModificationReminder(filePath)
|
|
372
|
+
export const resetFileFreshnessSession = () =>
|
|
373
|
+
fileFreshnessService.resetSession()
|
|
374
|
+
export const startWatchingTodoFile = (agentId: string) =>
|
|
375
|
+
fileFreshnessService.startWatchingTodoFile(agentId)
|
|
376
|
+
export const stopWatchingTodoFile = (agentId: string) =>
|
|
377
|
+
fileFreshnessService.stopWatchingTodoFile(agentId)
|