telecoder 0.0.0-stage → 1.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/.env.example +363 -0
- package/CHANGELOG.md +1186 -0
- package/LICENSE +22 -0
- package/README.md +126 -2
- package/dist/audio/transcribe.js +50 -0
- package/dist/bin/mcp-server.js +378 -0
- package/dist/bin/tool-result.js +22 -0
- package/dist/bot/access/group-access.js +147 -0
- package/dist/bot/bot.js +592 -0
- package/dist/bot/file-access.js +141 -0
- package/dist/bot/handlers/command/access.js +407 -0
- package/dist/bot/handlers/command/admin.js +827 -0
- package/dist/bot/handlers/command/auto-approve.js +138 -0
- package/dist/bot/handlers/command/btw.js +224 -0
- package/dist/bot/handlers/command/cost.js +409 -0
- package/dist/bot/handlers/command/media.js +197 -0
- package/dist/bot/handlers/command/model.js +305 -0
- package/dist/bot/handlers/command/prefs-scope.js +197 -0
- package/dist/bot/handlers/command/project.js +536 -0
- package/dist/bot/handlers/command/resume.js +321 -0
- package/dist/bot/handlers/command/session.js +438 -0
- package/dist/bot/handlers/command/settings.js +1012 -0
- package/dist/bot/handlers/command/shared.js +210 -0
- package/dist/bot/handlers/command/streaming-mode.js +16 -0
- package/dist/bot/handlers/command/tasks.js +513 -0
- package/dist/bot/handlers/command/topic-store.js +113 -0
- package/dist/bot/handlers/command/topic.js +80 -0
- package/dist/bot/handlers/command.handler.js +772 -0
- package/dist/bot/handlers/fork.handler.js +1109 -0
- package/dist/bot/handlers/media-fallback.js +121 -0
- package/dist/bot/handlers/message/replies.js +133 -0
- package/dist/bot/handlers/message/shared.js +117 -0
- package/dist/bot/handlers/message/throttle.js +49 -0
- package/dist/bot/handlers/message/turn-notify.js +169 -0
- package/dist/bot/handlers/message/turn-runner.js +141 -0
- package/dist/bot/handlers/message.handler.js +107 -0
- package/dist/bot/handlers/newbot.handler.js +221 -0
- package/dist/bot/handlers/photo.handler.js +327 -0
- package/dist/bot/handlers/prompts.handler.js +343 -0
- package/dist/bot/handlers/running-note.js +44 -0
- package/dist/bot/handlers/schedule.handler.js +303 -0
- package/dist/bot/handlers/session-guard.js +78 -0
- package/dist/bot/handlers/suggestion.handler.js +84 -0
- package/dist/bot/handlers/tree.handler.js +480 -0
- package/dist/bot/handlers/view.js +41 -0
- package/dist/bot/handlers/voice.handler.js +209 -0
- package/dist/bot/middleware/admin-only.js +49 -0
- package/dist/bot/middleware/auth.middleware.js +122 -0
- package/dist/bot/middleware/group-mention.middleware.js +139 -0
- package/dist/bot/middleware/group-role.middleware.js +75 -0
- package/dist/bot/middleware/message-batcher.js +87 -0
- package/dist/bot/middleware/queued-messages.js +971 -0
- package/dist/bot/middleware/stale-filter.js +31 -0
- package/dist/build-info.json +8 -0
- package/dist/claude/agent-watchdog.js +162 -0
- package/dist/claude/agent.js +1458 -0
- package/dist/claude/ask-user.js +295 -0
- package/dist/claude/auto-approve.js +225 -0
- package/dist/claude/auto-topic-haiku.js +126 -0
- package/dist/claude/available-commands.js +153 -0
- package/dist/claude/carry-over.js +27 -0
- package/dist/claude/ccr-shim.js +46 -0
- package/dist/claude/charter-judge.js +214 -0
- package/dist/claude/claude-auth.js +121 -0
- package/dist/claude/command-parser.js +200 -0
- package/dist/claude/cost-overlay-scraper.js +129 -0
- package/dist/claude/enabled-plugins.js +56 -0
- package/dist/claude/fork-tree.js +393 -0
- package/dist/claude/held-query.js +190 -0
- package/dist/claude/in-flight-tracker.js +61 -0
- package/dist/claude/ipc-server.js +192 -0
- package/dist/claude/mcp-bridge.js +453 -0
- package/dist/claude/mcp-tools.js +354 -0
- package/dist/claude/message-offsets.js +88 -0
- package/dist/claude/modal-relay.js +450 -0
- package/dist/claude/model-catalog.js +85 -0
- package/dist/claude/monitor-relay.js +597 -0
- package/dist/claude/pending-forks.js +93 -0
- package/dist/claude/pending-suggestions.js +50 -0
- package/dist/claude/permission-gate.js +337 -0
- package/dist/claude/permission-mode.js +178 -0
- package/dist/claude/poll-user.js +51 -0
- package/dist/claude/project-commands.js +69 -0
- package/dist/claude/prompt-hold.js +124 -0
- package/dist/claude/prompt-suggestion-scraper.js +89 -0
- package/dist/claude/pty-provider.js +2214 -0
- package/dist/claude/pty-spawn-config.js +296 -0
- package/dist/claude/pty-stream.js +363 -0
- package/dist/claude/request-queue.js +135 -0
- package/dist/claude/scheduled-runner.js +91 -0
- package/dist/claude/scheduler.js +237 -0
- package/dist/claude/scope-guard.js +274 -0
- package/dist/claude/sdk-permission-gate.js +77 -0
- package/dist/claude/secret-request.js +576 -0
- package/dist/claude/session-cost.js +41 -0
- package/dist/claude/session-history.js +286 -0
- package/dist/claude/session-jsonl.js +920 -0
- package/dist/claude/session-manager.js +195 -0
- package/dist/claude/session-scan.js +270 -0
- package/dist/claude/setup-token.js +285 -0
- package/dist/claude/side-call-options.js +45 -0
- package/dist/claude/side-question.js +249 -0
- package/dist/claude/stage-images.js +62 -0
- package/dist/claude/subagent-tools.js +12 -0
- package/dist/claude/synthetic-ctx.js +51 -0
- package/dist/claude/system-prompt.js +162 -0
- package/dist/claude/tasks-overlay-scraper.js +94 -0
- package/dist/claude/tip-scraper.js +78 -0
- package/dist/claude/tui-modal.js +501 -0
- package/dist/claude/tui-state.js +384 -0
- package/dist/claude/turn-end.js +91 -0
- package/dist/claude/turn-prose.js +64 -0
- package/dist/claude/update-banner-relay.js +70 -0
- package/dist/cli/commands/doctor.js +305 -0
- package/dist/cli/commands/help.js +25 -0
- package/dist/cli/commands/init.js +123 -0
- package/dist/cli/commands/service.js +678 -0
- package/dist/cli/commands/start.js +53 -0
- package/dist/cli/commands/version.js +11 -0
- package/dist/cli/dispatch.js +79 -0
- package/dist/cli/init/env-file.js +99 -0
- package/dist/cli/init/flow.js +638 -0
- package/dist/cli/init/prompter.js +116 -0
- package/dist/cli/init/telegram.js +94 -0
- package/dist/cli/service/render.js +212 -0
- package/dist/cli.js +18 -0
- package/dist/config-report.js +107 -0
- package/dist/config.js +500 -0
- package/dist/index.js +367 -0
- package/dist/launcher.js +933 -0
- package/dist/node-version-check.js +27 -0
- package/dist/providers/ccr-health.js +119 -0
- package/dist/providers/ccr-introspect.js +149 -0
- package/dist/providers/ccr-provider.js +60 -0
- package/dist/providers/claude-provider.js +63 -0
- package/dist/providers/prefs-sync.js +164 -0
- package/dist/providers/project-favorites.js +74 -0
- package/dist/providers/provider-router.js +127 -0
- package/dist/providers/provider-switch.js +91 -0
- package/dist/providers/sdk-provider.js +37 -0
- package/dist/providers/types.js +8 -0
- package/dist/providers/user-preferences.js +214 -0
- package/dist/restart-when-idle.js +153 -0
- package/dist/startup/auto-continue.js +67 -0
- package/dist/startup/auto-resume.js +173 -0
- package/dist/startup/notices.js +120 -0
- package/dist/startup/shared.js +44 -0
- package/dist/startup/verify-token.js +31 -0
- package/dist/telegram/access-request.js +153 -0
- package/dist/telegram/action-logger.js +684 -0
- package/dist/telegram/admin-mention.js +107 -0
- package/dist/telegram/background-activity-card.js +502 -0
- package/dist/telegram/botname-settings.js +336 -0
- package/dist/telegram/button-text.js +26 -0
- package/dist/telegram/client-options.js +27 -0
- package/dist/telegram/deduplication.js +33 -0
- package/dist/telegram/entities.js +295 -0
- package/dist/telegram/force-reply-tracker.js +57 -0
- package/dist/telegram/lettered-options.js +87 -0
- package/dist/telegram/markdown.js +362 -0
- package/dist/telegram/message-sender.js +1287 -0
- package/dist/telegram/prompt-label.js +142 -0
- package/dist/telegram/prompt-suggestion.js +54 -0
- package/dist/telegram/send.js +123 -0
- package/dist/telegram/streaming-turn.js +105 -0
- package/dist/telegram/suggestions-settings.js +30 -0
- package/dist/telegram/task-list.js +367 -0
- package/dist/telegram/task-tracker.js +209 -0
- package/dist/telegram/telegraph-settings.js +31 -0
- package/dist/telegram/telegraph.js +453 -0
- package/dist/telegram/terminal-renderer.js +671 -0
- package/dist/telegram/terminal-settings.js +25 -0
- package/dist/telegram/thread-titles.js +190 -0
- package/dist/telegram/threads-setup.js +117 -0
- package/dist/tts/tts-settings.js +62 -0
- package/dist/tts/tts.js +245 -0
- package/dist/tts/voice-reply.js +78 -0
- package/dist/utils/admins.js +143 -0
- package/dist/utils/agent-timer.js +55 -0
- package/dist/utils/atomic-write.js +24 -0
- package/dist/utils/bot-token.js +74 -0
- package/dist/utils/bounded-map.js +55 -0
- package/dist/utils/build-info.js +57 -0
- package/dist/utils/caffeinate.js +50 -0
- package/dist/utils/claude-env.js +54 -0
- package/dist/utils/claude-version.js +48 -0
- package/dist/utils/config-home.js +62 -0
- package/dist/utils/debug-agent.js +117 -0
- package/dist/utils/debug-watchdog.js +121 -0
- package/dist/utils/download.js +111 -0
- package/dist/utils/exit-codes.js +48 -0
- package/dist/utils/file-type.js +149 -0
- package/dist/utils/format.js +81 -0
- package/dist/utils/home-project.js +30 -0
- package/dist/utils/host-stall.js +51 -0
- package/dist/utils/id-list.js +44 -0
- package/dist/utils/idle-restart.js +92 -0
- package/dist/utils/install-mode.js +63 -0
- package/dist/utils/instance-config.js +148 -0
- package/dist/utils/instances.js +95 -0
- package/dist/utils/json-store.js +140 -0
- package/dist/utils/keyed-settings.js +62 -0
- package/dist/utils/legacy-env.js +35 -0
- package/dist/utils/npm-version.js +134 -0
- package/dist/utils/prefs-relay.js +107 -0
- package/dist/utils/proc-children.js +102 -0
- package/dist/utils/project-data-dir.js +33 -0
- package/dist/utils/reasoning-summary.js +12 -0
- package/dist/utils/resolve-bin.js +86 -0
- package/dist/utils/resolve-claude-bin.js +63 -0
- package/dist/utils/respawn-backoff.js +19 -0
- package/dist/utils/sanitize.js +91 -0
- package/dist/utils/session-key.js +37 -0
- package/dist/utils/stale-launcher.js +109 -0
- package/dist/utils/state-dir-migration.js +92 -0
- package/dist/utils/systemd.js +126 -0
- package/dist/utils/user-roster.js +275 -0
- package/dist/utils/verbosity.js +118 -0
- package/dist/utils/version-check.js +166 -0
- package/dist/utils/workspace-guard.js +44 -0
- package/dist/worker-restart.js +141 -0
- package/instances.json.example +41 -0
- package/package.json +76 -4
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { ensureStateDir, getStateDir, readJsonFile, writeJsonFile } from '../utils/json-store.js';
|
|
5
|
+
// Zod schema for session history entry
|
|
6
|
+
const sessionHistoryEntrySchema = z.object({
|
|
7
|
+
conversationId: z.string(),
|
|
8
|
+
claudeSessionId: z.string().optional(),
|
|
9
|
+
// Which provider backend (claude / ccr) created this Claude
|
|
10
|
+
// session. Used to refuse cross-backend resumes — a session whose thinking
|
|
11
|
+
// blocks were minted by DeepSeek-via-CCR can't be replayed against the real
|
|
12
|
+
// Anthropic API. Optional for backward compatibility with pre-existing files.
|
|
13
|
+
ownerProvider: z.string().optional(),
|
|
14
|
+
projectPath: z.string(),
|
|
15
|
+
projectName: z.string(),
|
|
16
|
+
lastMessagePreview: z.string(),
|
|
17
|
+
lastAssistantPreview: z.string().optional(),
|
|
18
|
+
topic: z.string().optional(),
|
|
19
|
+
createdAt: z.string(),
|
|
20
|
+
lastActivity: z.string(),
|
|
21
|
+
});
|
|
22
|
+
// Zod schema for the full session history file
|
|
23
|
+
const sessionHistoryDataSchema = z.object({
|
|
24
|
+
sessions: z.record(z.string(), z.array(sessionHistoryEntrySchema)),
|
|
25
|
+
});
|
|
26
|
+
const HISTORY_DIR = getStateDir();
|
|
27
|
+
const DEFAULT_HISTORY_FILE = path.join(HISTORY_DIR, 'sessions.json');
|
|
28
|
+
const MAX_HISTORY_PER_CHAT = 20;
|
|
29
|
+
// Cap stored previews at 50KB so multi-chunk content survives a reload intact
|
|
30
|
+
// (Telegram allows 4096 chars/message — the restore flow chunks). Applies to
|
|
31
|
+
// both the user's last prompt and the assistant's last response.
|
|
32
|
+
const MAX_PREVIEW_CHARS = 50_000;
|
|
33
|
+
class SessionHistory {
|
|
34
|
+
data = { sessions: {} };
|
|
35
|
+
historyFile = DEFAULT_HISTORY_FILE;
|
|
36
|
+
constructor() {
|
|
37
|
+
this.ensureDirectory();
|
|
38
|
+
this.load();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Scope session history to a specific bot instance. Call early in startup
|
|
42
|
+
* before any sessions are created. Migrates from the shared sessions.json
|
|
43
|
+
* if a per-bot file doesn't exist yet.
|
|
44
|
+
*/
|
|
45
|
+
initForBot(botId) {
|
|
46
|
+
const perBotFile = path.join(HISTORY_DIR, `sessions-${botId}.json`);
|
|
47
|
+
if (perBotFile === this.historyFile)
|
|
48
|
+
return; // already initialized
|
|
49
|
+
this.historyFile = perBotFile;
|
|
50
|
+
if (fs.existsSync(perBotFile)) {
|
|
51
|
+
// Per-bot file exists, load it
|
|
52
|
+
this.load();
|
|
53
|
+
}
|
|
54
|
+
else if (fs.existsSync(DEFAULT_HISTORY_FILE)) {
|
|
55
|
+
// First run with per-bot scoping: copy shared file as starting point
|
|
56
|
+
try {
|
|
57
|
+
fs.copyFileSync(DEFAULT_HISTORY_FILE, perBotFile);
|
|
58
|
+
console.log(`[SessionHistory] Migrated shared sessions.json → sessions-${botId}.json`);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// If copy fails, start fresh
|
|
62
|
+
}
|
|
63
|
+
this.load();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
ensureDirectory() {
|
|
67
|
+
ensureStateDir(HISTORY_DIR, 'SessionHistory');
|
|
68
|
+
}
|
|
69
|
+
load() {
|
|
70
|
+
const loaded = readJsonFile(this.historyFile, sessionHistoryDataSchema, 'SessionHistory');
|
|
71
|
+
// Keep string keys as-is (supports both "12345" and "12345:42" formats)
|
|
72
|
+
this.data = { sessions: { ...(loaded?.sessions ?? {}) } };
|
|
73
|
+
// Files written before history was kept most-recently-used first are in
|
|
74
|
+
// creation order, where a conversation you went back to sits below newer
|
|
75
|
+
// ones. `lastActivity` is only ever moved by using a conversation, so
|
|
76
|
+
// sorting on it puts one of those files back in the order it would have
|
|
77
|
+
// had. For a file already in that order this is a no-op.
|
|
78
|
+
for (const history of Object.values(this.data.sessions)) {
|
|
79
|
+
history.sort((a, b) => b.lastActivity.localeCompare(a.lastActivity));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
save() {
|
|
83
|
+
writeJsonFile(this.historyFile, this.data, 'SessionHistory');
|
|
84
|
+
}
|
|
85
|
+
saveSession(sessionKey, conversationId, projectPath, lastMessagePreview = '', claudeSessionId, ownerProvider) {
|
|
86
|
+
if (!this.data.sessions[sessionKey]) {
|
|
87
|
+
this.data.sessions[sessionKey] = [];
|
|
88
|
+
}
|
|
89
|
+
const history = this.data.sessions[sessionKey];
|
|
90
|
+
const projectName = path.basename(projectPath);
|
|
91
|
+
// Check if this conversation already exists
|
|
92
|
+
const existingIndex = history.findIndex((entry) => entry.conversationId === conversationId);
|
|
93
|
+
const existingEntry = existingIndex >= 0 ? history[existingIndex] : undefined;
|
|
94
|
+
const entry = {
|
|
95
|
+
conversationId,
|
|
96
|
+
claudeSessionId: claudeSessionId ?? existingEntry?.claudeSessionId,
|
|
97
|
+
ownerProvider: ownerProvider ?? existingEntry?.ownerProvider,
|
|
98
|
+
projectPath,
|
|
99
|
+
projectName,
|
|
100
|
+
lastMessagePreview: lastMessagePreview.substring(0, MAX_PREVIEW_CHARS),
|
|
101
|
+
lastAssistantPreview: existingEntry?.lastAssistantPreview,
|
|
102
|
+
topic: existingEntry?.topic,
|
|
103
|
+
createdAt: existingIndex >= 0
|
|
104
|
+
? history[existingIndex].createdAt
|
|
105
|
+
: new Date().toISOString(),
|
|
106
|
+
lastActivity: new Date().toISOString(),
|
|
107
|
+
};
|
|
108
|
+
// The entry saved is always the conversation the chat is now in — a new
|
|
109
|
+
// one, or one it went back to — so it goes to the front. Everything that
|
|
110
|
+
// wants "the current conversation" reads index 0: the topic and the
|
|
111
|
+
// restore after a restart. Left where it was, a
|
|
112
|
+
// conversation you went back to wrote its topic onto the newest one and
|
|
113
|
+
// lost the restore to it. The cap below then drops whichever was used
|
|
114
|
+
// longest ago, rather than whichever was started first.
|
|
115
|
+
if (existingIndex >= 0)
|
|
116
|
+
history.splice(existingIndex, 1);
|
|
117
|
+
history.unshift(entry);
|
|
118
|
+
// Keep only recent history
|
|
119
|
+
if (history.length > MAX_HISTORY_PER_CHAT) {
|
|
120
|
+
this.data.sessions[sessionKey] = history.slice(0, MAX_HISTORY_PER_CHAT);
|
|
121
|
+
}
|
|
122
|
+
this.save();
|
|
123
|
+
}
|
|
124
|
+
getHistory(sessionKey, limit = 5) {
|
|
125
|
+
const history = this.data.sessions[sessionKey] || [];
|
|
126
|
+
return history.slice(0, limit);
|
|
127
|
+
}
|
|
128
|
+
getLastSession(sessionKey) {
|
|
129
|
+
const history = this.data.sessions[sessionKey];
|
|
130
|
+
return history?.[0];
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Most recent entry that actually carries a claudeSessionId — i.e. one that
|
|
134
|
+
* can be resumed against Claude. History is newest-first, so `find` returns
|
|
135
|
+
* the latest resumable entry. Skips past "stub" entries left by conversations
|
|
136
|
+
* that never finished init (a query interrupted by a rebuild, an aborted
|
|
137
|
+
* /clear), which would otherwise mask a healthy session sitting one slot back.
|
|
138
|
+
*/
|
|
139
|
+
getLastResumableSession(sessionKey) {
|
|
140
|
+
const history = this.data.sessions[sessionKey];
|
|
141
|
+
return history?.find((entry) => !!entry.claudeSessionId);
|
|
142
|
+
}
|
|
143
|
+
getSessionByConversationId(sessionKey, conversationId) {
|
|
144
|
+
const history = this.data.sessions[sessionKey] || [];
|
|
145
|
+
return history.find((entry) => entry.conversationId === conversationId);
|
|
146
|
+
}
|
|
147
|
+
getAllActiveSessions() {
|
|
148
|
+
const active = new Map();
|
|
149
|
+
for (const [key, history] of Object.entries(this.data.sessions)) {
|
|
150
|
+
if (history.length > 0) {
|
|
151
|
+
active.set(key, history[0]);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return active;
|
|
155
|
+
}
|
|
156
|
+
updateLastMessage(sessionKey, conversationId, preview) {
|
|
157
|
+
const history = this.data.sessions[sessionKey];
|
|
158
|
+
if (!history)
|
|
159
|
+
return;
|
|
160
|
+
const entry = history.find((e) => e.conversationId === conversationId);
|
|
161
|
+
if (entry) {
|
|
162
|
+
entry.lastMessagePreview = preview.substring(0, MAX_PREVIEW_CHARS);
|
|
163
|
+
entry.lastActivity = new Date().toISOString();
|
|
164
|
+
this.save();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
updateLastAssistantMessage(sessionKey, conversationId, preview) {
|
|
168
|
+
const history = this.data.sessions[sessionKey];
|
|
169
|
+
if (!history)
|
|
170
|
+
return;
|
|
171
|
+
const entry = history.find((e) => e.conversationId === conversationId);
|
|
172
|
+
if (entry) {
|
|
173
|
+
entry.lastAssistantPreview = preview.substring(0, MAX_PREVIEW_CHARS);
|
|
174
|
+
entry.lastActivity = new Date().toISOString();
|
|
175
|
+
this.save();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
clearLastAssistantPreview(sessionKey, conversationId) {
|
|
179
|
+
const history = this.data.sessions[sessionKey];
|
|
180
|
+
if (!history)
|
|
181
|
+
return;
|
|
182
|
+
const entry = history.find((e) => e.conversationId === conversationId);
|
|
183
|
+
if (entry) {
|
|
184
|
+
entry.lastAssistantPreview = undefined;
|
|
185
|
+
this.save();
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
updateTopic(sessionKey, topic) {
|
|
189
|
+
const history = this.data.sessions[sessionKey];
|
|
190
|
+
if (!history || history.length === 0)
|
|
191
|
+
return;
|
|
192
|
+
// Update the most recent entry (index 0)
|
|
193
|
+
history[0].topic = topic || undefined;
|
|
194
|
+
this.save();
|
|
195
|
+
}
|
|
196
|
+
updateClaudeSessionId(sessionKey, conversationId, claudeSessionId, ownerProvider) {
|
|
197
|
+
const history = this.data.sessions[sessionKey];
|
|
198
|
+
if (!history)
|
|
199
|
+
return;
|
|
200
|
+
const entry = history.find((e) => e.conversationId === conversationId);
|
|
201
|
+
if (entry) {
|
|
202
|
+
entry.claudeSessionId = claudeSessionId;
|
|
203
|
+
if (ownerProvider)
|
|
204
|
+
entry.ownerProvider = ownerProvider;
|
|
205
|
+
entry.lastActivity = new Date().toISOString();
|
|
206
|
+
this.save();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
clearHistory(sessionKey) {
|
|
210
|
+
delete this.data.sessions[sessionKey];
|
|
211
|
+
this.save();
|
|
212
|
+
}
|
|
213
|
+
/** Every chat of this bot whose history holds the Claude session `claudeSessionId`. */
|
|
214
|
+
/** Every entry of every chat this bot keeps, with the chat it belongs to. */
|
|
215
|
+
allEntries() {
|
|
216
|
+
return Object.entries(this.data.sessions).flatMap(([sessionKey, history]) => history.map((entry) => ({ sessionKey, entry })));
|
|
217
|
+
}
|
|
218
|
+
findByClaudeSessionId(claudeSessionId) {
|
|
219
|
+
const found = [];
|
|
220
|
+
for (const [sessionKey, history] of Object.entries(this.data.sessions)) {
|
|
221
|
+
for (const entry of history) {
|
|
222
|
+
if (entry.claudeSessionId === claudeSessionId)
|
|
223
|
+
found.push({ sessionKey, entry });
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return found;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
export const sessionHistory = new SessionHistory();
|
|
230
|
+
/**
|
|
231
|
+
* Every chat history on this machine holding one of `claudeSessionIds`, keyed
|
|
232
|
+
* by session id: this bot's, from memory, and each sibling's, from the file it
|
|
233
|
+
* keeps next to ours. A sibling's file is only as fresh as its last save,
|
|
234
|
+
* which is every change it makes.
|
|
235
|
+
*
|
|
236
|
+
* The shared sessions.json that predates per-bot files is left out: every bot
|
|
237
|
+
* copied it as its starting point, so what it lists is either in a bot's own
|
|
238
|
+
* file too, or long gone from all of them.
|
|
239
|
+
*/
|
|
240
|
+
export function findHistoryOwners(claudeSessionIds, selfBotId) {
|
|
241
|
+
const wanted = new Set(claudeSessionIds);
|
|
242
|
+
const own = [...wanted].flatMap((id) => sessionHistory.findByClaudeSessionId(id));
|
|
243
|
+
return collectOwners(selfBotId, own, (id) => wanted.has(id));
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Every conversation any TeleCoder bot on this machine has a chat history
|
|
247
|
+
* entry for, keyed by Claude session id — the same sources as
|
|
248
|
+
* findHistoryOwners, without picking ids first.
|
|
249
|
+
*/
|
|
250
|
+
export function allHistoryOwners(selfBotId) {
|
|
251
|
+
return collectOwners(selfBotId, sessionHistory.allEntries(), () => true);
|
|
252
|
+
}
|
|
253
|
+
function collectOwners(selfBotId, own, keep) {
|
|
254
|
+
const owners = new Map();
|
|
255
|
+
const add = (owner) => {
|
|
256
|
+
const id = owner.entry.claudeSessionId;
|
|
257
|
+
if (!id || !keep(id))
|
|
258
|
+
return;
|
|
259
|
+
const list = owners.get(id);
|
|
260
|
+
if (list)
|
|
261
|
+
list.push(owner);
|
|
262
|
+
else
|
|
263
|
+
owners.set(id, [owner]);
|
|
264
|
+
};
|
|
265
|
+
for (const { sessionKey, entry } of own)
|
|
266
|
+
add({ botId: selfBotId, sessionKey, entry });
|
|
267
|
+
let names = [];
|
|
268
|
+
try {
|
|
269
|
+
names = fs.readdirSync(HISTORY_DIR);
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
// No state dir means no sibling has saved anything yet.
|
|
273
|
+
}
|
|
274
|
+
for (const name of names) {
|
|
275
|
+
const botId = /^sessions-(.+)\.json$/.exec(name)?.[1];
|
|
276
|
+
if (!botId || botId === selfBotId)
|
|
277
|
+
continue;
|
|
278
|
+
const data = readJsonFile(path.join(HISTORY_DIR, name), sessionHistoryDataSchema, 'SessionHistory');
|
|
279
|
+
for (const [sessionKey, history] of Object.entries(data?.sessions ?? {})) {
|
|
280
|
+
for (const entry of history)
|
|
281
|
+
add({ botId, sessionKey, entry });
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return owners;
|
|
285
|
+
}
|
|
286
|
+
//# sourceMappingURL=session-history.js.map
|