telecoder 0.0.0-stage → 1.3.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 +375 -0
- package/CHANGELOG.md +1867 -0
- package/LICENSE +22 -0
- package/README.md +130 -2
- package/dist/audio/transcribe.js +41 -0
- package/dist/bin/mcp-server.js +363 -0
- package/dist/bin/tool-result.js +22 -0
- package/dist/bot/access/group-access.js +147 -0
- package/dist/bot/bot.js +573 -0
- package/dist/bot/command-catalog.js +263 -0
- package/dist/bot/deleted-threads.js +103 -0
- package/dist/bot/file-access.js +150 -0
- package/dist/bot/handlers/album.handler.js +128 -0
- package/dist/bot/handlers/branch.handler.js +1096 -0
- package/dist/bot/handlers/command/access.js +407 -0
- package/dist/bot/handlers/command/admin.js +803 -0
- package/dist/bot/handlers/command/all-view.js +33 -0
- package/dist/bot/handlers/command/auto-approve.js +138 -0
- package/dist/bot/handlers/command/background.js +37 -0
- package/dist/bot/handlers/command/botname.js +76 -0
- package/dist/bot/handlers/command/btw.js +175 -0
- package/dist/bot/handlers/command/cleanup.js +329 -0
- package/dist/bot/handlers/command/model.js +226 -0
- package/dist/bot/handlers/command/prefs-scope.js +362 -0
- package/dist/bot/handlers/command/project.js +458 -0
- package/dist/bot/handlers/command/resume.js +325 -0
- package/dist/bot/handlers/command/session.js +299 -0
- package/dist/bot/handlers/command/settings.js +657 -0
- package/dist/bot/handlers/command/shared.js +207 -0
- package/dist/bot/handlers/command/tasks.js +681 -0
- package/dist/bot/handlers/command/thread.js +128 -0
- package/dist/bot/handlers/command/topic-store.js +74 -0
- package/dist/bot/handlers/command/topic.js +27 -0
- package/dist/bot/handlers/command/usage.js +382 -0
- package/dist/bot/handlers/command-fallback.js +179 -0
- package/dist/bot/handlers/command.handler.js +446 -0
- package/dist/bot/handlers/loop.handler.js +389 -0
- package/dist/bot/handlers/media-fallback.js +126 -0
- package/dist/bot/handlers/message/replies.js +56 -0
- package/dist/bot/handlers/message/shared.js +95 -0
- package/dist/bot/handlers/message/turn-notify.js +135 -0
- package/dist/bot/handlers/message/turn-runner.js +56 -0
- package/dist/bot/handlers/message.handler.js +114 -0
- package/dist/bot/handlers/newbot.handler.js +230 -0
- package/dist/bot/handlers/photo.handler.js +286 -0
- package/dist/bot/handlers/prompts.handler.js +343 -0
- package/dist/bot/handlers/running-note.js +44 -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 +147 -0
- package/dist/bot/kept-threads.js +51 -0
- package/dist/bot/middleware/admin-only.js +49 -0
- package/dist/bot/middleware/album-collector.js +69 -0
- package/dist/bot/middleware/auth.middleware.js +122 -0
- package/dist/bot/middleware/group-mention.middleware.js +145 -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 +1082 -0
- package/dist/bot/middleware/stale-filter.js +9 -0
- package/dist/build-info.json +8 -0
- package/dist/claude/ask-user-form.js +517 -0
- package/dist/claude/ask-user.js +363 -0
- package/dist/claude/auto-approve.js +225 -0
- package/dist/claude/auto-topic-haiku.js +143 -0
- package/dist/claude/available-commands.js +156 -0
- package/dist/claude/background-hint.js +91 -0
- package/dist/claude/branch-tree.js +393 -0
- package/dist/claude/carry-over.js +25 -0
- package/dist/claude/ccr-shim.js +48 -0
- package/dist/claude/charter-judge.js +214 -0
- package/dist/claude/claude-auth.js +117 -0
- package/dist/claude/command-parser.js +206 -0
- package/dist/claude/cost-overlay-scraper.js +134 -0
- package/dist/claude/effort-level.js +5 -0
- package/dist/claude/enabled-plugins.js +52 -0
- package/dist/claude/in-flight-tracker.js +82 -0
- package/dist/claude/ipc-server.js +192 -0
- package/dist/claude/mcp-bridge.js +481 -0
- package/dist/claude/memory-guard.js +400 -0
- package/dist/claude/message-offsets.js +88 -0
- package/dist/claude/modal-relay.js +817 -0
- package/dist/claude/model-catalog.js +102 -0
- package/dist/claude/monitor-relay.js +712 -0
- package/dist/claude/native-commands.js +40 -0
- package/dist/claude/pending-branches.js +93 -0
- package/dist/claude/pending-suggestions.js +50 -0
- package/dist/claude/permission-gate.js +338 -0
- package/dist/claude/permission-mode.js +152 -0
- package/dist/claude/poll-user.js +51 -0
- package/dist/claude/prewarm.js +70 -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-idle-reaper.js +96 -0
- package/dist/claude/pty-provider.js +2499 -0
- package/dist/claude/pty-spawn-config.js +343 -0
- package/dist/claude/pty-stream.js +363 -0
- package/dist/claude/pty-watch.js +201 -0
- package/dist/claude/request-queue.js +107 -0
- package/dist/claude/rewind.js +111 -0
- package/dist/claude/scheduled-runner.js +89 -0
- package/dist/claude/scheduler.js +241 -0
- package/dist/claude/scope-guard.js +279 -0
- package/dist/claude/screen-marks.js +83 -0
- package/dist/claude/screen-render.js +96 -0
- package/dist/claude/secret-request.js +576 -0
- package/dist/claude/session-history.js +288 -0
- package/dist/claude/session-jsonl.js +1067 -0
- package/dist/claude/session-manager.js +216 -0
- package/dist/claude/session-move.js +70 -0
- package/dist/claude/session-scan.js +270 -0
- package/dist/claude/setup-token.js +285 -0
- package/dist/claude/shell-mode-gate.js +60 -0
- package/dist/claude/side-call-options.js +45 -0
- package/dist/claude/side-question.js +252 -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/tasks-overlay-scraper.js +217 -0
- package/dist/claude/thread-judge-haiku.js +88 -0
- package/dist/claude/tip-scraper.js +77 -0
- package/dist/claude/tui-modal.js +746 -0
- package/dist/claude/tui-state.js +456 -0
- package/dist/claude/turn-end.js +105 -0
- package/dist/claude/turn-prose.js +64 -0
- package/dist/claude/typed-answer.js +104 -0
- package/dist/claude/update-banner-relay.js +70 -0
- package/dist/cli/commands/doctor.js +338 -0
- package/dist/cli/commands/help.js +34 -0
- package/dist/cli/commands/init.js +126 -0
- package/dist/cli/commands/service.js +710 -0
- package/dist/cli/commands/start.js +63 -0
- package/dist/cli/commands/version.js +11 -0
- package/dist/cli/commands/watch.js +322 -0
- package/dist/cli/dispatch.js +84 -0
- package/dist/cli/init/env-file.js +99 -0
- package/dist/cli/init/flow.js +705 -0
- package/dist/cli/init/prompter.js +124 -0
- package/dist/cli/init/telegram.js +120 -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 +525 -0
- package/dist/index.js +443 -0
- package/dist/launcher.js +960 -0
- package/dist/node-version-check.js +27 -0
- package/dist/providers/ccr-health.js +119 -0
- package/dist/providers/ccr-introspect.js +142 -0
- package/dist/providers/ccr-provider.js +59 -0
- package/dist/providers/claude-provider.js +10 -0
- package/dist/providers/prefs-sync.js +121 -0
- package/dist/providers/project-favorites.js +74 -0
- package/dist/providers/provider-router.js +128 -0
- package/dist/providers/provider-switch.js +91 -0
- package/dist/providers/spawn-settings.js +45 -0
- package/dist/providers/types.js +8 -0
- package/dist/providers/user-preferences.js +322 -0
- package/dist/restart-when-idle.js +173 -0
- package/dist/startup/auto-continue.js +67 -0
- package/dist/startup/auto-resume.js +161 -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 +522 -0
- package/dist/telegram/admin-mention.js +107 -0
- package/dist/telegram/background-activity-card.js +502 -0
- package/dist/telegram/botname-settings.js +282 -0
- package/dist/telegram/button-text.js +26 -0
- package/dist/telegram/client-options.js +26 -0
- package/dist/telegram/deduplication.js +33 -0
- package/dist/telegram/entities.js +295 -0
- package/dist/telegram/force-reply-tracker.js +96 -0
- package/dist/telegram/lettered-options.js +87 -0
- package/dist/telegram/markdown.js +362 -0
- package/dist/telegram/message-sender.js +1019 -0
- package/dist/telegram/prompt-label.js +148 -0
- package/dist/telegram/prompt-suggestion.js +54 -0
- package/dist/telegram/send.js +123 -0
- package/dist/telegram/streaming-turn.js +103 -0
- package/dist/telegram/suggestions-settings.js +30 -0
- package/dist/telegram/table-lists.js +56 -0
- package/dist/telegram/task-list.js +367 -0
- package/dist/telegram/task-tracker.js +128 -0
- package/dist/telegram/terminal-renderer.js +667 -0
- package/dist/telegram/thread-titles.js +262 -0
- package/dist/telegram/threads-setup.js +117 -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-icon.js +248 -0
- package/dist/utils/bot-token.js +77 -0
- package/dist/utils/bounded-map.js +55 -0
- package/dist/utils/build-info.js +57 -0
- package/dist/utils/caffeinate.js +53 -0
- package/dist/utils/claude-env.js +53 -0
- package/dist/utils/claude-version.js +48 -0
- package/dist/utils/config-home.js +62 -0
- package/dist/utils/download.js +137 -0
- package/dist/utils/exit-codes.js +59 -0
- package/dist/utils/file-type.js +149 -0
- package/dist/utils/format.js +78 -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/memory-pressure.js +60 -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/resolve-bin.js +86 -0
- package/dist/utils/resolve-claude-bin.js +52 -0
- package/dist/utils/respawn-backoff.js +28 -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/telegram-proxy.js +72 -0
- package/dist/utils/uploads-dir.js +27 -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 +75 -4
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Naming the threads of a private chat after what they're about.
|
|
3
|
+
*
|
|
4
|
+
* A message sent from a bot's "All" view starts a new thread, and Telegram
|
|
5
|
+
* names it "New chat" and flags the name as implicit: one the bot is expected
|
|
6
|
+
* to replace. The session topic (Haiku's summary, or what the agent set with
|
|
7
|
+
* telecoder_set_topic) already says what the thread is about, so every topic
|
|
8
|
+
* change is passed on to the thread with editForumTopic. Only the topic's
|
|
9
|
+
* first half goes in: the broad area, which stays put while the second half,
|
|
10
|
+
* the task of the moment, changes with every message. The project's folder
|
|
11
|
+
* name goes in front, since one bot's threads can each be in another project.
|
|
12
|
+
*
|
|
13
|
+
* A name the user gave the thread is theirs, and is never overwritten: one
|
|
14
|
+
* typed when creating the thread, or a rename seen afterwards. Group forum
|
|
15
|
+
* topics are left alone entirely; people name those for each other.
|
|
16
|
+
*
|
|
17
|
+
* Who owns each thread's name, and what we last set it to, is kept per bot
|
|
18
|
+
* so a restart neither forgets a user's rename nor re-sends the same name.
|
|
19
|
+
*/
|
|
20
|
+
import * as path from 'path';
|
|
21
|
+
import { z } from 'zod';
|
|
22
|
+
import { BOT_ID } from '../config.js';
|
|
23
|
+
import { ensureStateDir, getStateDir, readJsonFile, writeJsonFile } from '../utils/json-store.js';
|
|
24
|
+
import { buildSessionKey, parseSessionKey } from '../utils/session-key.js';
|
|
25
|
+
import { TOPIC_PART_SEPARATOR } from '../claude/auto-topic-haiku.js';
|
|
26
|
+
/** Telegram's limit on a topic name. */
|
|
27
|
+
const MAX_NAME_LENGTH = 128;
|
|
28
|
+
/**
|
|
29
|
+
* At most one rename per thread per this long. The topic can change several
|
|
30
|
+
* times in a few seconds (Haiku's guess, then the agent's own), and each
|
|
31
|
+
* rename leaves a service message in the thread; only the last one matters.
|
|
32
|
+
*/
|
|
33
|
+
const RENAME_COOLDOWN_MS = 60_000;
|
|
34
|
+
const titleSchema = z.object({
|
|
35
|
+
/** 'user' names are never touched; 'implicit' and 'bot' ones are ours to replace. */
|
|
36
|
+
owner: z.enum(['implicit', 'bot', 'user']),
|
|
37
|
+
name: z.string().optional(),
|
|
38
|
+
/** Goes in front of every name we give the thread, e.g. 🌱 for a branch's. */
|
|
39
|
+
prefix: z.string().optional(),
|
|
40
|
+
});
|
|
41
|
+
const fileSchema = z.object({ threads: z.record(z.string(), titleSchema) });
|
|
42
|
+
const STATE_DIR = getStateDir();
|
|
43
|
+
const STATE_FILE = path.join(STATE_DIR, `thread-titles-${BOT_ID}.json`);
|
|
44
|
+
let threads;
|
|
45
|
+
let api;
|
|
46
|
+
const lastRenameAt = new Map();
|
|
47
|
+
const pending = new Map();
|
|
48
|
+
/** editForumTopic calls not yet answered. */
|
|
49
|
+
let renamesInFlight = 0;
|
|
50
|
+
function state() {
|
|
51
|
+
if (!threads) {
|
|
52
|
+
ensureStateDir(STATE_DIR, 'ThreadTitles');
|
|
53
|
+
threads = { ...(readJsonFile(STATE_FILE, fileSchema, 'ThreadTitles')?.threads ?? {}) };
|
|
54
|
+
}
|
|
55
|
+
return threads;
|
|
56
|
+
}
|
|
57
|
+
function record(sessionKey, title) {
|
|
58
|
+
state()[sessionKey] = title;
|
|
59
|
+
writeJsonFile(STATE_FILE, { threads: state() }, 'ThreadTitles');
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The name a topic gives a thread: its first half, the broad area, which
|
|
63
|
+
* holds still while the second half follows each message. A topic set by
|
|
64
|
+
* hand or by the agent has no halves and goes in whole.
|
|
65
|
+
*
|
|
66
|
+
* The project leads, so it survives the thread list cutting names short. It
|
|
67
|
+
* is left out when the topic already names it, as Haiku's often does, and is
|
|
68
|
+
* the whole name while there is no topic yet, as just after a project switch.
|
|
69
|
+
*/
|
|
70
|
+
export function threadName(topic, prefix = '', project = '') {
|
|
71
|
+
const general = topic.split(TOPIC_PART_SEPARATOR)[0].trim();
|
|
72
|
+
const showProject = project && !general.toLowerCase().includes(project.toLowerCase());
|
|
73
|
+
const name = [showProject ? project : '', general].filter(Boolean).join(TOPIC_PART_SEPARATOR);
|
|
74
|
+
if (!name)
|
|
75
|
+
return '';
|
|
76
|
+
return `${prefix}${name}`.slice(0, MAX_NAME_LENGTH);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Take on a thread the bot created itself, under the name it was created
|
|
80
|
+
* with, so the topic it inherits isn't sent straight back as a rename.
|
|
81
|
+
*/
|
|
82
|
+
export function adoptThread(sessionKey, name, prefix) {
|
|
83
|
+
record(sessionKey, { owner: 'bot', name, ...(prefix ? { prefix } : {}) });
|
|
84
|
+
}
|
|
85
|
+
/** A thread of a private chat, which is the only kind we name. */
|
|
86
|
+
function isPrivateThread(sessionKey) {
|
|
87
|
+
const { chatId, threadId } = parseSessionKey(sessionKey);
|
|
88
|
+
return threadId !== undefined && chatId > 0;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Whether a topic for this session would end up as a thread name — for the
|
|
92
|
+
* auto-topic side call, which otherwise only runs when something displays it.
|
|
93
|
+
*/
|
|
94
|
+
export function namesThread(sessionKey) {
|
|
95
|
+
return api !== undefined && isPrivateThread(sessionKey) && state()[sessionKey]?.owner !== 'user';
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Learn who named each thread from the messages that pass by: the creation
|
|
99
|
+
* notice every thread message replies to says whether its name was implicit,
|
|
100
|
+
* and a rename notice is the user's (the bot gets no updates for its own).
|
|
101
|
+
*/
|
|
102
|
+
function observe(ctx) {
|
|
103
|
+
const msg = ctx.message;
|
|
104
|
+
if (!msg?.is_topic_message || msg.message_thread_id === undefined)
|
|
105
|
+
return;
|
|
106
|
+
const sessionKey = buildSessionKey(msg.chat.id, msg.message_thread_id);
|
|
107
|
+
if (!isPrivateThread(sessionKey))
|
|
108
|
+
return;
|
|
109
|
+
const edited = msg.forum_topic_edited;
|
|
110
|
+
if (edited?.name !== undefined && msg.from?.id !== ctx.me.id) {
|
|
111
|
+
// Logged in full while it's new: whether Telegram's own names for threads
|
|
112
|
+
// arrive this way, as the user's, is not documented.
|
|
113
|
+
console.log(`[ThreadTitles] ${sessionKey} renamed to "${edited.name}" by ${msg.from?.is_bot ? 'bot' : 'user'} ${msg.from?.id}`);
|
|
114
|
+
if (edited.name === state()[sessionKey]?.name)
|
|
115
|
+
return;
|
|
116
|
+
record(sessionKey, { owner: 'user', name: edited.name });
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (state()[sessionKey])
|
|
120
|
+
return;
|
|
121
|
+
const created = msg.forum_topic_created ?? msg.reply_to_message?.forum_topic_created;
|
|
122
|
+
if (!created)
|
|
123
|
+
return;
|
|
124
|
+
record(sessionKey, { owner: created.is_name_implicit ? 'implicit' : 'user', name: created.name });
|
|
125
|
+
}
|
|
126
|
+
async function rename(sessionKey, name) {
|
|
127
|
+
const current = state()[sessionKey];
|
|
128
|
+
if (!api || current?.owner === 'user')
|
|
129
|
+
return;
|
|
130
|
+
if (current?.name === name)
|
|
131
|
+
return;
|
|
132
|
+
const { chatId, threadId } = parseSessionKey(sessionKey);
|
|
133
|
+
lastRenameAt.set(sessionKey, Date.now());
|
|
134
|
+
renamesInFlight++;
|
|
135
|
+
try {
|
|
136
|
+
await api.editForumTopic(chatId, threadId, { name });
|
|
137
|
+
// Read again: the prefix may have changed while the call was out.
|
|
138
|
+
const prefix = state()[sessionKey]?.prefix;
|
|
139
|
+
record(sessionKey, { owner: 'bot', name, ...(prefix ? { prefix } : {}) });
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
console.debug(`[ThreadTitles] Failed to rename ${sessionKey}:`, err instanceof Error ? err.message : err);
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
renamesInFlight--;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Renames still to make: held for the cooldown, or on their way to Telegram.
|
|
150
|
+
* For /botrebuild's "when idle", which would otherwise restart over them.
|
|
151
|
+
*/
|
|
152
|
+
export function renamesOutstanding() {
|
|
153
|
+
return pending.size + renamesInFlight;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Pass a new session topic on to the thread's name. A no-op outside private
|
|
157
|
+
* threads, for an empty topic in no known project, and for a name the user
|
|
158
|
+
* chose. Renames inside the cooldown are held and only the latest one is sent
|
|
159
|
+
* when it runs out.
|
|
160
|
+
*/
|
|
161
|
+
export function syncThreadTitle(sessionKey, topic, project) {
|
|
162
|
+
if (!namesThread(sessionKey))
|
|
163
|
+
return;
|
|
164
|
+
const name = threadName(topic, state()[sessionKey]?.prefix, project);
|
|
165
|
+
if (!name)
|
|
166
|
+
return;
|
|
167
|
+
const held = pending.get(sessionKey);
|
|
168
|
+
if (held)
|
|
169
|
+
clearTimeout(held.timer);
|
|
170
|
+
pending.delete(sessionKey);
|
|
171
|
+
const wait = (lastRenameAt.get(sessionKey) ?? 0) + RENAME_COOLDOWN_MS - Date.now();
|
|
172
|
+
if (wait <= 0) {
|
|
173
|
+
void rename(sessionKey, name);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const timer = setTimeout(() => {
|
|
177
|
+
pending.delete(sessionKey);
|
|
178
|
+
void rename(sessionKey, name);
|
|
179
|
+
}, wait);
|
|
180
|
+
timer.unref?.();
|
|
181
|
+
pending.set(sessionKey, { name, timer });
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Put `mark` in front of the thread's name, or take it off, and rename the
|
|
185
|
+
* thread now rather than after the cooldown: it's what the user just asked
|
|
186
|
+
* for. The mark joins the prefix, so every later rename keeps it, next to a
|
|
187
|
+
* branch's 🌱. A name the user chose is left as it is.
|
|
188
|
+
*
|
|
189
|
+
* 'renamed' when the thread now has the name, 'user-named' for a name that's
|
|
190
|
+
* the user's, 'unnamed' for a thread the bot doesn't name (a group's topic,
|
|
191
|
+
* one it has never seen a name for) or a rename Telegram refused.
|
|
192
|
+
*/
|
|
193
|
+
export async function markThreadName(sessionKey, mark, on) {
|
|
194
|
+
const current = state()[sessionKey];
|
|
195
|
+
if (!api || !isPrivateThread(sessionKey) || !current?.name)
|
|
196
|
+
return 'unnamed';
|
|
197
|
+
if (current.owner === 'user')
|
|
198
|
+
return 'user-named';
|
|
199
|
+
const oldPrefix = current.prefix ?? '';
|
|
200
|
+
const rest = oldPrefix.startsWith(mark) ? oldPrefix.slice(mark.length) : oldPrefix;
|
|
201
|
+
const prefix = on ? mark + rest : rest;
|
|
202
|
+
// A rename held for the cooldown carries the newer topic: that one goes now.
|
|
203
|
+
const held = pending.get(sessionKey);
|
|
204
|
+
if (held)
|
|
205
|
+
clearTimeout(held.timer);
|
|
206
|
+
pending.delete(sessionKey);
|
|
207
|
+
const latest = held?.name ?? current.name;
|
|
208
|
+
const base = latest.startsWith(oldPrefix) ? latest.slice(oldPrefix.length) : latest;
|
|
209
|
+
const name = `${prefix}${base}`.slice(0, MAX_NAME_LENGTH);
|
|
210
|
+
const next = { ...current };
|
|
211
|
+
if (prefix)
|
|
212
|
+
next.prefix = prefix;
|
|
213
|
+
else
|
|
214
|
+
delete next.prefix;
|
|
215
|
+
record(sessionKey, next);
|
|
216
|
+
await rename(sessionKey, name);
|
|
217
|
+
return state()[sessionKey]?.name === name ? 'renamed' : 'unnamed';
|
|
218
|
+
}
|
|
219
|
+
/** The thread's name as last seen or set, for naming it in a message. */
|
|
220
|
+
export function threadDisplayName(sessionKey) {
|
|
221
|
+
return state()[sessionKey]?.name;
|
|
222
|
+
}
|
|
223
|
+
/** Whether the thread still has the name Telegram gave it on creation from the "All" view. */
|
|
224
|
+
export function hasImplicitName(sessionKey) {
|
|
225
|
+
return state()[sessionKey]?.owner === 'implicit';
|
|
226
|
+
}
|
|
227
|
+
/** Drop what's kept about a thread that no longer exists, and any rename still waiting for it. */
|
|
228
|
+
export function forgetThreadTitle(sessionKey) {
|
|
229
|
+
const held = pending.get(sessionKey);
|
|
230
|
+
if (held)
|
|
231
|
+
clearTimeout(held.timer);
|
|
232
|
+
pending.delete(sessionKey);
|
|
233
|
+
lastRenameAt.delete(sessionKey);
|
|
234
|
+
if (!state()[sessionKey])
|
|
235
|
+
return;
|
|
236
|
+
delete state()[sessionKey];
|
|
237
|
+
writeJsonFile(STATE_FILE, { threads: state() }, 'ThreadTitles');
|
|
238
|
+
}
|
|
239
|
+
/** Start naming threads on `bot`. Call once, after auth, before the handlers. */
|
|
240
|
+
export function initThreadTitles(bot) {
|
|
241
|
+
api = bot.api;
|
|
242
|
+
bot.use((ctx, next) => {
|
|
243
|
+
try {
|
|
244
|
+
observe(ctx);
|
|
245
|
+
}
|
|
246
|
+
catch (err) {
|
|
247
|
+
console.debug('[ThreadTitles] Failed to read a thread notice:', err instanceof Error ? err.message : err);
|
|
248
|
+
}
|
|
249
|
+
return next();
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
/** Test hook: forget the module state between cases. */
|
|
253
|
+
export function resetThreadTitlesForTest(testApi) {
|
|
254
|
+
threads = undefined;
|
|
255
|
+
api = testApi;
|
|
256
|
+
lastRenameAt.clear();
|
|
257
|
+
for (const { timer } of pending.values())
|
|
258
|
+
clearTimeout(timer);
|
|
259
|
+
pending.clear();
|
|
260
|
+
renamesInFlight = 0;
|
|
261
|
+
}
|
|
262
|
+
//# sourceMappingURL=thread-titles.js.map
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Walking the owner of a project bot through turning its threads on.
|
|
3
|
+
*
|
|
4
|
+
* Telegram has no API for it: a bot can read has_topics_enabled but not set
|
|
5
|
+
* it, for itself or for a bot it manages, so every bot /newbot creates starts
|
|
6
|
+
* without threads until its owner flips Threaded Mode in BotFather's Mini App.
|
|
7
|
+
* A bot can't message someone who hasn't written to it, so the steps go out
|
|
8
|
+
* with the first message an admin sends it. From then on it checks getMe once
|
|
9
|
+
* a minute, says so when threads are on, and takes the new setting into
|
|
10
|
+
* bot.botInfo (what ctx.me reads), so nothing waits for a restart.
|
|
11
|
+
*
|
|
12
|
+
* Only bots with a HOME_PROJECT, the ones /newbot makes: the numbered bots
|
|
13
|
+
* are deliberately one conversation each, and shouldn't be told otherwise.
|
|
14
|
+
*/
|
|
15
|
+
import * as path from 'path';
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
import { BOT_ID, config } from '../config.js';
|
|
18
|
+
import { isAdmin } from '../utils/admins.js';
|
|
19
|
+
import { ensureStateDir, getStateDir, readJsonFile, writeJsonFile } from '../utils/json-store.js';
|
|
20
|
+
const CHECK_EVERY_MS = 60_000;
|
|
21
|
+
/** How long after the steps went out it keeps checking; a restart checks once more anyway. */
|
|
22
|
+
const CHECK_FOR_MS = 24 * 60 * 60_000;
|
|
23
|
+
const stateSchema = z.object({
|
|
24
|
+
/** When the steps were sent, and where to. */
|
|
25
|
+
notifiedAt: z.number().optional(),
|
|
26
|
+
chatId: z.number().optional(),
|
|
27
|
+
/** Threads are on; nothing more to say. */
|
|
28
|
+
done: z.boolean().optional(),
|
|
29
|
+
});
|
|
30
|
+
const STATE_DIR = getStateDir();
|
|
31
|
+
const stateFile = () => path.join(STATE_DIR, `threads-setup-${BOT_ID}.json`);
|
|
32
|
+
let state = {};
|
|
33
|
+
let timer;
|
|
34
|
+
function save(next) {
|
|
35
|
+
state = next;
|
|
36
|
+
ensureStateDir(STATE_DIR, 'ThreadsSetup');
|
|
37
|
+
writeJsonFile(stateFile(), state, 'ThreadsSetup');
|
|
38
|
+
}
|
|
39
|
+
/** The steps, for the bot's own chat. */
|
|
40
|
+
export function threadsSetupSteps(username) {
|
|
41
|
+
return '🧵 Threads are off for this bot, and Telegram only lets you turn them on yourself:\n\n' +
|
|
42
|
+
`1. Open @BotFather and tap Open to start its Mini App\n2. Pick @${username}\n3. Turn on Threaded Mode\n\n` +
|
|
43
|
+
'I\'ll say so here once they\'re on. Until then this chat is one conversation.';
|
|
44
|
+
}
|
|
45
|
+
async function check(bot) {
|
|
46
|
+
const me = await bot.api.getMe();
|
|
47
|
+
if (!me.has_topics_enabled) {
|
|
48
|
+
if (state.notifiedAt !== undefined && Date.now() - state.notifiedAt > CHECK_FOR_MS)
|
|
49
|
+
stop();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
bot.botInfo = me;
|
|
53
|
+
stop();
|
|
54
|
+
save({ ...state, done: true });
|
|
55
|
+
console.log('[ThreadsSetup] Threads are on');
|
|
56
|
+
if (state.chatId !== undefined) {
|
|
57
|
+
await bot.api.sendMessage(state.chatId, '✅ Threads are on. Start one from the bot\'s "All" view, or with 🌱 Branch; each is its own conversation, named after what it\'s about.');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function watch(bot) {
|
|
61
|
+
if (timer)
|
|
62
|
+
return;
|
|
63
|
+
timer = setInterval(() => {
|
|
64
|
+
check(bot).catch((err) => console.debug('[ThreadsSetup] Check failed:', err instanceof Error ? err.message : err));
|
|
65
|
+
}, CHECK_EVERY_MS);
|
|
66
|
+
timer.unref?.();
|
|
67
|
+
}
|
|
68
|
+
function stop() {
|
|
69
|
+
if (timer)
|
|
70
|
+
clearInterval(timer);
|
|
71
|
+
timer = undefined;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Send the steps with an admin's first private message. Call once in
|
|
75
|
+
* createBot, before the handlers; it reads the setting from ctx.me.
|
|
76
|
+
*/
|
|
77
|
+
export function initThreadsSetup(bot) {
|
|
78
|
+
if (!config.HOME_PROJECT)
|
|
79
|
+
return;
|
|
80
|
+
state = readJsonFile(stateFile(), stateSchema, 'ThreadsSetup') ?? {};
|
|
81
|
+
bot.use(async (ctx, next) => {
|
|
82
|
+
if (!state.done && state.notifiedAt === undefined && !ctx.me.has_topics_enabled
|
|
83
|
+
&& ctx.chat?.type === 'private' && isAdmin(ctx.from?.id)) {
|
|
84
|
+
save({ notifiedAt: Date.now(), chatId: ctx.chat.id });
|
|
85
|
+
try {
|
|
86
|
+
await ctx.reply(threadsSetupSteps(ctx.me.username), {
|
|
87
|
+
reply_markup: { inline_keyboard: [[{ text: 'Open @BotFather', url: 'https://t.me/BotFather' }]] },
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
console.debug('[ThreadsSetup] Failed to send the steps:', err instanceof Error ? err.message : err);
|
|
92
|
+
}
|
|
93
|
+
watch(bot);
|
|
94
|
+
}
|
|
95
|
+
return next();
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* After bot.init(): note threads that are on already, or pick the checking
|
|
100
|
+
* back up after a restart if the steps went out and they still aren't.
|
|
101
|
+
*/
|
|
102
|
+
export function resumeThreadsSetup(bot) {
|
|
103
|
+
if (!config.HOME_PROJECT || state.done)
|
|
104
|
+
return;
|
|
105
|
+
if (bot.botInfo.has_topics_enabled) {
|
|
106
|
+
save({ ...state, done: true });
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (state.notifiedAt !== undefined && Date.now() - state.notifiedAt < CHECK_FOR_MS)
|
|
110
|
+
watch(bot);
|
|
111
|
+
}
|
|
112
|
+
/** Test hook. */
|
|
113
|
+
export function resetThreadsSetupForTest() {
|
|
114
|
+
stop();
|
|
115
|
+
state = {};
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=threads-setup.js.map
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who counts as an admin of this bot instance.
|
|
3
|
+
*
|
|
4
|
+
* TeleCoder's original access model is one flat list: every id in
|
|
5
|
+
* `ALLOWED_USER_IDS` can do everything. That is the right model for a bot with
|
|
6
|
+
* one user, and it stops being right the moment the bot is shared. An approval
|
|
7
|
+
* prompt the person who triggered it can tap themselves is not an approval, and
|
|
8
|
+
* a guest who can `/botrestart` or flip the transport can step around every
|
|
9
|
+
* guardrail the owner configured.
|
|
10
|
+
*
|
|
11
|
+
* `ADMIN_USER_IDS` names the subset that keeps those unrestricted rights. Left
|
|
12
|
+
* unset it resolves to *every* allowed user, so a single-user install behaves
|
|
13
|
+
* exactly as it did before this module existed and nobody has to migrate.
|
|
14
|
+
*
|
|
15
|
+
* Admin-ness is deliberately not inferred from Telegram's own group-admin
|
|
16
|
+
* status: who may promote someone in a Telegram group is a different question
|
|
17
|
+
* from who may approve a `sudo` on the machine the bot runs on.
|
|
18
|
+
*/
|
|
19
|
+
import { config } from '../config.js';
|
|
20
|
+
import { allAllowedUserIds } from './user-roster.js';
|
|
21
|
+
/**
|
|
22
|
+
* Telegram's GroupAnonymousBot id — the sender id on messages posted
|
|
23
|
+
* anonymously by a group admin. Shared with the auth middleware, which lets it
|
|
24
|
+
* through in allow-listed groups.
|
|
25
|
+
*
|
|
26
|
+
* It is never an admin here. Any Telegram group admin can post anonymously, so
|
|
27
|
+
* honouring it would hand bot-admin rights to whoever holds group-admin rights
|
|
28
|
+
* — a promotion the bot owner may not have intended to be the same thing.
|
|
29
|
+
*/
|
|
30
|
+
export const GROUP_ANONYMOUS_BOT_ID = 1087968824;
|
|
31
|
+
/**
|
|
32
|
+
* The effective admin roster. Falls back to the full allow-list when
|
|
33
|
+
* `ADMIN_USER_IDS` is unset, which is what keeps existing installs unchanged.
|
|
34
|
+
*
|
|
35
|
+
* An id here that `ALLOWED_USER_IDS` does not also carry can never act — the
|
|
36
|
+
* auth middleware turns it away before any admin check runs — so config.ts
|
|
37
|
+
* refuses to start on one rather than leaving it to surface later, as an
|
|
38
|
+
* approval prompt that silently went to the wrong person.
|
|
39
|
+
*/
|
|
40
|
+
export function getAdminIds() {
|
|
41
|
+
if (config.ADMIN_USER_IDS.length === 0)
|
|
42
|
+
return [...config.ALLOWED_USER_IDS];
|
|
43
|
+
return [...config.ADMIN_USER_IDS];
|
|
44
|
+
}
|
|
45
|
+
/** True when this user may approve prompts and run bot-lifecycle commands. */
|
|
46
|
+
export function isAdmin(userId) {
|
|
47
|
+
if (userId === undefined)
|
|
48
|
+
return false;
|
|
49
|
+
if (userId === GROUP_ANONYMOUS_BOT_ID)
|
|
50
|
+
return false;
|
|
51
|
+
return getAdminIds().includes(userId);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The people who may answer an approval prompt but do not own the bot.
|
|
55
|
+
*
|
|
56
|
+
* This exists because "can approve" and "can restart the bot" were one flag,
|
|
57
|
+
* and on a bot shared with several people that forces a choice between one
|
|
58
|
+
* owner being the bottleneck for every prompt and handing guests the controls
|
|
59
|
+
* — including `/method` and `/permissionmode`, either of which switches the
|
|
60
|
+
* gate off. An approver is a guest in every other respect: the charter judge
|
|
61
|
+
* still reads their messages, the gate still stops their tool calls, and the
|
|
62
|
+
* lifecycle commands still refuse them.
|
|
63
|
+
*
|
|
64
|
+
* Empty when `ADMIN_USER_IDS` is unset, because then every allowed user is
|
|
65
|
+
* already an admin and there is nothing to widen. Ids the admin roster already
|
|
66
|
+
* carries are dropped rather than counted twice.
|
|
67
|
+
*/
|
|
68
|
+
export function getApproverIds() {
|
|
69
|
+
// config.ts refuses to start on approvers-without-admins, so this is a
|
|
70
|
+
// belt-and-braces restatement of the invariant rather than a reachable path.
|
|
71
|
+
if (config.ADMIN_USER_IDS.length === 0)
|
|
72
|
+
return [];
|
|
73
|
+
const admins = getAdminIds();
|
|
74
|
+
return config.APPROVER_USER_IDS.filter((id) => id !== GROUP_ANONYMOUS_BOT_ID && !admins.includes(id));
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Approver ids that no allow-list carries — neither `ALLOWED_USER_IDS` nor the
|
|
78
|
+
* roster `/allow` writes to. Those can never act, so they are always a typo.
|
|
79
|
+
*
|
|
80
|
+
* This lives here rather than beside the matching `ADMIN_USER_IDS` check in
|
|
81
|
+
* config.ts because it has to read the runtime roster, and user-roster.ts
|
|
82
|
+
* imports config.ts. The env-only version of this check was wrong in exactly
|
|
83
|
+
* the case the tier exists for: a friend admitted from chat with `/allow` is
|
|
84
|
+
* never in `ALLOWED_USER_IDS`, so promoting them would have refused to start
|
|
85
|
+
* the bot — and the message would have told them to widen a list that had
|
|
86
|
+
* nothing to do with it.
|
|
87
|
+
*
|
|
88
|
+
* Reported at startup rather than enforced here: the roster moves while the
|
|
89
|
+
* bot runs, and an approver who is later `/deny`'d should stop being able to
|
|
90
|
+
* act, not take the process down with them.
|
|
91
|
+
*/
|
|
92
|
+
export function findOrphanedApprovers() {
|
|
93
|
+
if (config.APPROVER_USER_IDS.length === 0)
|
|
94
|
+
return [];
|
|
95
|
+
const reachable = new Set(allAllowedUserIds());
|
|
96
|
+
return config.APPROVER_USER_IDS.filter((id) => !reachable.has(id));
|
|
97
|
+
}
|
|
98
|
+
/** True when this user may answer someone else's approval prompt. */
|
|
99
|
+
export function isApprover(userId) {
|
|
100
|
+
if (userId === undefined)
|
|
101
|
+
return false;
|
|
102
|
+
return getApproverIds().includes(userId);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Who may answer a prompt raised by `requesterId`.
|
|
106
|
+
*
|
|
107
|
+
* Approvers are filtered against the requester: tapping "allow" on your own
|
|
108
|
+
* request confirms nothing, and a tier that let you do it would be a speed
|
|
109
|
+
* bump rather than supervision. Admins are not filtered, because an admin
|
|
110
|
+
* approving their own `sudo` is the owner of the machine agreeing with
|
|
111
|
+
* themselves — which is what a solo bot has always done.
|
|
112
|
+
*
|
|
113
|
+
* Never empty: `getAdminIds()` falls back to the whole allow-list.
|
|
114
|
+
*/
|
|
115
|
+
export function getPromptResponderIds(requesterId) {
|
|
116
|
+
const admins = getAdminIds();
|
|
117
|
+
const approvers = getApproverIds().filter((id) => id !== requesterId);
|
|
118
|
+
return [...admins, ...approvers];
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* True when at least one allowed user is not an admin — i.e. the bot is shared
|
|
122
|
+
* with someone who is meant to be supervised.
|
|
123
|
+
*
|
|
124
|
+
* This is the predicate the rest of the codebase should branch on rather than
|
|
125
|
+
* "is ADMIN_USER_IDS set": a roster that happens to list everybody restricts
|
|
126
|
+
* nobody, and should not switch on supervision UI or change any default.
|
|
127
|
+
*
|
|
128
|
+
* It reads the *effective* allow-list, `.env` plus everyone `/allow` has
|
|
129
|
+
* admitted since — this is what turns the permission gate, the scope guard and
|
|
130
|
+
* the charter judge on, and admitting a guest from chat has to switch on the
|
|
131
|
+
* supervision meant for guests. Reading only `.env` here would leave a bot that
|
|
132
|
+
* had just gained its first guest running with every guardrail off.
|
|
133
|
+
*/
|
|
134
|
+
export function hasGuestUsers() {
|
|
135
|
+
const admins = getAdminIds();
|
|
136
|
+
return allAllowedUserIds().some((id) => !admins.includes(id));
|
|
137
|
+
}
|
|
138
|
+
/** The allowed users who are not admins. Used by `/gate` to report the split. */
|
|
139
|
+
export function getGuestIds() {
|
|
140
|
+
const admins = getAdminIds();
|
|
141
|
+
return allAllowedUserIds().filter((id) => !admins.includes(id));
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=admins.js.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent timer utility for tracking elapsed time during agent queries.
|
|
3
|
+
* Provides human-readable duration formatting and timing state management.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Create a new agent timer initialized to the current time.
|
|
7
|
+
*/
|
|
8
|
+
export function createAgentTimer() {
|
|
9
|
+
const now = Date.now();
|
|
10
|
+
return {
|
|
11
|
+
startTime: now,
|
|
12
|
+
lastMessageTime: now,
|
|
13
|
+
messageCount: 0,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Record that a message was received, updating the last message time.
|
|
18
|
+
*/
|
|
19
|
+
export function recordMessage(timer) {
|
|
20
|
+
timer.lastMessageTime = Date.now();
|
|
21
|
+
timer.messageCount++;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Get elapsed milliseconds since timer start.
|
|
25
|
+
*/
|
|
26
|
+
export function getElapsedMs(timer) {
|
|
27
|
+
return Date.now() - timer.startTime;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get milliseconds since last message was recorded.
|
|
31
|
+
*/
|
|
32
|
+
export function getSinceLastMessageMs(timer) {
|
|
33
|
+
return Date.now() - timer.lastMessageTime;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Format a duration in milliseconds to human-readable string.
|
|
37
|
+
* Examples: "0s", "45s", "1m 30s", "2m 0s"
|
|
38
|
+
*/
|
|
39
|
+
export function formatDuration(ms) {
|
|
40
|
+
const totalSeconds = Math.floor(ms / 1000);
|
|
41
|
+
const minutes = Math.floor(totalSeconds / 60);
|
|
42
|
+
const seconds = totalSeconds % 60;
|
|
43
|
+
if (minutes === 0) {
|
|
44
|
+
return `${seconds}s`;
|
|
45
|
+
}
|
|
46
|
+
return `${minutes}m ${seconds}s`;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get a timing report string for logging.
|
|
50
|
+
*/
|
|
51
|
+
export function getTimingReport(timer) {
|
|
52
|
+
const elapsed = formatDuration(getElapsedMs(timer));
|
|
53
|
+
return `${elapsed} elapsed, ${timer.messageCount} messages`;
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=agent-timer.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
/**
|
|
3
|
+
* Write a file atomically using write-to-temp + rename.
|
|
4
|
+
* On POSIX, rename is atomic. On Windows, it's best-effort but still
|
|
5
|
+
* safer than a direct writeFileSync which can corrupt on crash.
|
|
6
|
+
*/
|
|
7
|
+
export function atomicWriteFileSync(filePath, data, options) {
|
|
8
|
+
const tmpPath = filePath + '.tmp';
|
|
9
|
+
try {
|
|
10
|
+
fs.writeFileSync(tmpPath, data, { mode: options?.mode });
|
|
11
|
+
fs.renameSync(tmpPath, filePath);
|
|
12
|
+
}
|
|
13
|
+
catch (err) {
|
|
14
|
+
// Clean up temp file on failure
|
|
15
|
+
try {
|
|
16
|
+
fs.unlinkSync(tmpPath);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
// Ignore cleanup errors
|
|
20
|
+
}
|
|
21
|
+
throw err;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=atomic-write.js.map
|