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,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `telecoder start` — run the bot in this process.
|
|
3
|
+
*
|
|
4
|
+
* One bot is what `npm start` runs (index.ts); several are what
|
|
5
|
+
* `npm run start:multi` runs (launcher.ts). In a checkout you pick by script;
|
|
6
|
+
* here the config decides: an instances.json in the config home means the
|
|
7
|
+
* launcher, otherwise a single bot from .env. --single / --multi override that.
|
|
8
|
+
*
|
|
9
|
+
* Either way the bot's own module takes over the process, so this resolves
|
|
10
|
+
* with nothing and leaves the exit to it.
|
|
11
|
+
*/
|
|
12
|
+
import * as fs from 'fs';
|
|
13
|
+
import * as path from 'path';
|
|
14
|
+
import { configFile, getConfigHome } from '../../utils/config-home.js';
|
|
15
|
+
import { legacyEnv } from '../../utils/legacy-env.js';
|
|
16
|
+
import { PACKAGE_ROOT } from '../../utils/install-mode.js';
|
|
17
|
+
/** Which entry point to run. Pure, for tests. */
|
|
18
|
+
export function chooseStartTarget({ args, instancesExists }) {
|
|
19
|
+
if (args.includes('--single'))
|
|
20
|
+
return 'single';
|
|
21
|
+
if (args.includes('--multi') || args.includes('--config'))
|
|
22
|
+
return 'multi';
|
|
23
|
+
return instancesExists ? 'multi' : 'single';
|
|
24
|
+
}
|
|
25
|
+
export function usage() {
|
|
26
|
+
return [
|
|
27
|
+
'Usage: telecoder start [--single|--multi] [--config <file>]',
|
|
28
|
+
'',
|
|
29
|
+
'Runs in the foreground until Ctrl+C. With an instances.json it runs every bot in it.',
|
|
30
|
+
' --single just the bot in .env, even when instances.json exists',
|
|
31
|
+
' --multi the multi-bot launcher',
|
|
32
|
+
' --config <file> the launcher, with another instances file',
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
/** The lines to print when there is no config to start from. */
|
|
36
|
+
export function missingConfigMessage(envPath) {
|
|
37
|
+
return [
|
|
38
|
+
`No TeleCoder config found: ${envPath} doesn't exist.`,
|
|
39
|
+
'',
|
|
40
|
+
'Run "telecoder init" to set one up. (Or copy',
|
|
41
|
+
`${path.join(PACKAGE_ROOT, '.env.example')} to ${envPath} and fill in`,
|
|
42
|
+
'TELEGRAM_BOT_TOKEN and ALLOWED_USER_IDS.)',
|
|
43
|
+
'',
|
|
44
|
+
`Config is read from ${getConfigHome()}; set TELECODER_HOME to use another directory.`,
|
|
45
|
+
];
|
|
46
|
+
}
|
|
47
|
+
export async function run(args, io) {
|
|
48
|
+
const target = chooseStartTarget({ args, instancesExists: fs.existsSync(configFile('instances.json')) });
|
|
49
|
+
if (target === 'single') {
|
|
50
|
+
const envPath = legacyEnv('ENV_PATH') || configFile('.env');
|
|
51
|
+
// A token in the environment is a complete config on its own (a service
|
|
52
|
+
// unit with Environment= lines, say), so only a missing both is fatal.
|
|
53
|
+
if (!fs.existsSync(envPath) && !process.env.TELEGRAM_BOT_TOKEN) {
|
|
54
|
+
for (const line of missingConfigMessage(envPath))
|
|
55
|
+
io.err(line);
|
|
56
|
+
return 1;
|
|
57
|
+
}
|
|
58
|
+
await import('../../index.js');
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
await import('../../launcher.js');
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=start.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { getInstalledVersion } from '../../utils/install-mode.js';
|
|
2
|
+
export async function run(_args, io) {
|
|
3
|
+
const version = getInstalledVersion();
|
|
4
|
+
if (!version) {
|
|
5
|
+
io.err('Could not read the version from package.json.');
|
|
6
|
+
return 1;
|
|
7
|
+
}
|
|
8
|
+
io.out(version);
|
|
9
|
+
return 0;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=version.js.map
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `telecoder watch` — the live Claude Code screen behind a chat, read-only.
|
|
3
|
+
*
|
|
4
|
+
* The bot drives a real `claude` per chat and serves its screen on
|
|
5
|
+
* a socket (claude/pty-watch.ts). This command connects to it and shows that
|
|
6
|
+
* screen in this terminal as it changes: what you'd see had you typed the
|
|
7
|
+
* chat's messages into Claude Code yourself.
|
|
8
|
+
*
|
|
9
|
+
* It keeps its own copy of the screen and paints that row by row, so it works
|
|
10
|
+
* in a terminal of any size: a larger one shows the screen in its corner, a
|
|
11
|
+
* smaller one a part of it.
|
|
12
|
+
*/
|
|
13
|
+
import * as fs from 'fs';
|
|
14
|
+
import * as net from 'net';
|
|
15
|
+
import * as path from 'path';
|
|
16
|
+
import headless from '@xterm/headless';
|
|
17
|
+
import { getWatchDir } from '../../claude/pty-watch.js';
|
|
18
|
+
import { renderRows } from '../../claude/screen-render.js';
|
|
19
|
+
const { Terminal } = headless;
|
|
20
|
+
const POLL_MS = 500;
|
|
21
|
+
const FRAME_MS = 33;
|
|
22
|
+
const CONNECT_TIMEOUT_MS = 1500;
|
|
23
|
+
export function usage() {
|
|
24
|
+
return [
|
|
25
|
+
'Usage: telecoder watch [<project>] [--list]',
|
|
26
|
+
'',
|
|
27
|
+
'Shows the live Claude Code screen of a chat, read-only: what Claude Code looks like',
|
|
28
|
+
'while TeleCoder drives it. With several chats running it shows the most recently',
|
|
29
|
+
'active one; with none it waits for the next message. Ctrl+C leaves.',
|
|
30
|
+
'',
|
|
31
|
+
' <project> only a chat in this project (its folder name), or of this bot (@name)',
|
|
32
|
+
' --list list the running Claude Code sessions and exit',
|
|
33
|
+
'',
|
|
34
|
+
'PTY_WATCH=false in the .env turns it off.',
|
|
35
|
+
];
|
|
36
|
+
}
|
|
37
|
+
/** The project a session works in: its folder's name. */
|
|
38
|
+
export function projectOf(session) {
|
|
39
|
+
return path.basename(session.cwd) || session.cwd;
|
|
40
|
+
}
|
|
41
|
+
/** The session to show: the most recently active one that matches `filter`. */
|
|
42
|
+
export function pickSession(sessions, filter) {
|
|
43
|
+
const wanted = filter?.replace(/^@/, '').toLowerCase();
|
|
44
|
+
return sessions
|
|
45
|
+
.filter((s) => !wanted || projectOf(s).toLowerCase() === wanted || s.bot.toLowerCase() === wanted || s.key === wanted)
|
|
46
|
+
.sort((a, b) => b.lastActiveAt - a.lastActiveAt)[0];
|
|
47
|
+
}
|
|
48
|
+
function ago(ms) {
|
|
49
|
+
const s = Math.max(0, Math.round(ms / 1000));
|
|
50
|
+
if (s < 60)
|
|
51
|
+
return `${s}s`;
|
|
52
|
+
if (s < 3600)
|
|
53
|
+
return `${Math.round(s / 60)}m`;
|
|
54
|
+
return `${Math.round(s / 3600)}h`;
|
|
55
|
+
}
|
|
56
|
+
export function formatList(sessions, now = Date.now()) {
|
|
57
|
+
if (sessions.length === 0)
|
|
58
|
+
return ['No Claude Code session is running. One starts when a chat picks its project or gets its next message.'];
|
|
59
|
+
const rows = [...sessions]
|
|
60
|
+
.sort((a, b) => b.lastActiveAt - a.lastActiveAt)
|
|
61
|
+
.map((s) => [`@${s.bot}`, projectOf(s), `chat ${s.key}`, `active ${ago(now - s.lastActiveAt)} ago`]);
|
|
62
|
+
const widths = rows[0].map((_, i) => Math.max(...rows.map((r) => r[i].length)));
|
|
63
|
+
return rows.map((r) => r.map((cell, i) => cell.padEnd(widths[i])).join(' ').trimEnd());
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Which screen row goes at the top when only `visible` of `rows` fit. Claude
|
|
67
|
+
* Code fills its screen from the top, so the part to keep is the one that ends
|
|
68
|
+
* at the last row with something on it.
|
|
69
|
+
*/
|
|
70
|
+
export function cropStart(rows, visible, lastUsedRow) {
|
|
71
|
+
if (visible >= rows)
|
|
72
|
+
return 0;
|
|
73
|
+
return Math.max(0, Math.min(lastUsedRow + 1 - visible, rows - visible));
|
|
74
|
+
}
|
|
75
|
+
/** Ask one bot's socket for its sessions. Resolves with none when nothing answers. */
|
|
76
|
+
function listSocket(socketFile) {
|
|
77
|
+
return new Promise((resolve) => {
|
|
78
|
+
const socket = net.createConnection(socketFile);
|
|
79
|
+
let buffered = '';
|
|
80
|
+
const done = (sessions) => {
|
|
81
|
+
socket.destroy();
|
|
82
|
+
resolve(sessions);
|
|
83
|
+
};
|
|
84
|
+
socket.setEncoding('utf8');
|
|
85
|
+
socket.setTimeout(CONNECT_TIMEOUT_MS, () => done([]));
|
|
86
|
+
socket.on('error', () => done([]));
|
|
87
|
+
socket.on('connect', () => socket.write('{"op":"list"}\n'));
|
|
88
|
+
socket.on('data', (data) => {
|
|
89
|
+
buffered += data;
|
|
90
|
+
const newline = buffered.indexOf('\n');
|
|
91
|
+
if (newline === -1)
|
|
92
|
+
return;
|
|
93
|
+
try {
|
|
94
|
+
const reply = JSON.parse(buffered.slice(0, newline));
|
|
95
|
+
done((reply.sessions ?? []).map((s) => ({ ...s, bot: reply.bot ?? '', socket: socketFile })));
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
done([]);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
export async function discover(dir = getWatchDir()) {
|
|
104
|
+
let names;
|
|
105
|
+
try {
|
|
106
|
+
names = fs.readdirSync(dir).filter((name) => name.endsWith('.sock'));
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return { bots: 0, sessions: [] };
|
|
110
|
+
}
|
|
111
|
+
let bots = 0;
|
|
112
|
+
const all = await Promise.all(names.map(async (name) => {
|
|
113
|
+
const file = path.join(dir, name);
|
|
114
|
+
const alive = await new Promise((resolve) => {
|
|
115
|
+
const probe = net.createConnection(file);
|
|
116
|
+
probe.on('connect', () => { probe.destroy(); resolve(true); });
|
|
117
|
+
probe.on('error', () => resolve(false));
|
|
118
|
+
});
|
|
119
|
+
if (!alive)
|
|
120
|
+
return [];
|
|
121
|
+
bots++;
|
|
122
|
+
return listSocket(file);
|
|
123
|
+
}));
|
|
124
|
+
return { bots, sessions: all.flat() };
|
|
125
|
+
}
|
|
126
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
127
|
+
/** Paints one session's screen into the terminal until it ends or `signal` aborts. */
|
|
128
|
+
function show(session, out, signal) {
|
|
129
|
+
return new Promise((resolve) => {
|
|
130
|
+
const socket = net.createConnection(session.socket);
|
|
131
|
+
let mirror = null;
|
|
132
|
+
let painted = [];
|
|
133
|
+
let paintedFooter = '';
|
|
134
|
+
let dirty = false;
|
|
135
|
+
let buffered = '';
|
|
136
|
+
let settled = false;
|
|
137
|
+
const paint = () => {
|
|
138
|
+
if (!mirror || !dirty)
|
|
139
|
+
return;
|
|
140
|
+
dirty = false;
|
|
141
|
+
const width = out.columns || mirror.cols;
|
|
142
|
+
const height = out.rows || mirror.rows + 1;
|
|
143
|
+
const visible = Math.max(1, Math.min(mirror.rows, height - 1));
|
|
144
|
+
const cols = Math.min(mirror.cols, width);
|
|
145
|
+
const buffer = mirror.buffer.active;
|
|
146
|
+
let lastUsed = 0;
|
|
147
|
+
// Trimmed by hand: the snapshot fills every row with spaces.
|
|
148
|
+
for (let y = mirror.rows - 1; y >= 0; y--) {
|
|
149
|
+
if (buffer.getLine(buffer.baseY + y)?.translateToString(true).trim()) {
|
|
150
|
+
lastUsed = y;
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const start = cropStart(mirror.rows, visible, lastUsed);
|
|
155
|
+
const rows = renderRows(mirror, { maxCols: cols }).slice(start, start + visible);
|
|
156
|
+
const clear = cols < width ? '\x1b[K' : '';
|
|
157
|
+
let frame = '';
|
|
158
|
+
rows.forEach((row, i) => {
|
|
159
|
+
if (painted[i] === row)
|
|
160
|
+
return;
|
|
161
|
+
painted[i] = row;
|
|
162
|
+
frame += `\x1b[${i + 1};1H${row}${clear}`;
|
|
163
|
+
});
|
|
164
|
+
const cropped = cols < mirror.cols || visible < mirror.rows;
|
|
165
|
+
const footer = [
|
|
166
|
+
' telecoder watch',
|
|
167
|
+
`@${session.bot}`,
|
|
168
|
+
projectOf(session),
|
|
169
|
+
'read-only',
|
|
170
|
+
'Ctrl+C leaves',
|
|
171
|
+
cropped ? `part of ${mirror.cols}x${mirror.rows} shown, enlarge the terminal for all` : '',
|
|
172
|
+
].filter(Boolean).join(' · ').concat(' ').slice(0, width);
|
|
173
|
+
if (footer !== paintedFooter && height > visible) {
|
|
174
|
+
paintedFooter = footer;
|
|
175
|
+
frame += `\x1b[${visible + 1};1H\x1b[0;7m${footer}\x1b[0m\x1b[K`;
|
|
176
|
+
}
|
|
177
|
+
if (frame)
|
|
178
|
+
out.write(frame);
|
|
179
|
+
};
|
|
180
|
+
const timer = setInterval(paint, FRAME_MS);
|
|
181
|
+
const repaintAll = () => {
|
|
182
|
+
painted = [];
|
|
183
|
+
paintedFooter = '';
|
|
184
|
+
dirty = true;
|
|
185
|
+
out.write('\x1b[2J');
|
|
186
|
+
};
|
|
187
|
+
out.on('resize', repaintAll);
|
|
188
|
+
const finish = (how) => {
|
|
189
|
+
if (settled)
|
|
190
|
+
return;
|
|
191
|
+
settled = true;
|
|
192
|
+
paint();
|
|
193
|
+
clearInterval(timer);
|
|
194
|
+
out.off('resize', repaintAll);
|
|
195
|
+
signal.removeEventListener('abort', onAbort);
|
|
196
|
+
socket.destroy();
|
|
197
|
+
resolve(how);
|
|
198
|
+
};
|
|
199
|
+
const onAbort = () => finish('aborted');
|
|
200
|
+
signal.addEventListener('abort', onAbort);
|
|
201
|
+
socket.setEncoding('utf8');
|
|
202
|
+
socket.on('connect', () => socket.write(`${JSON.stringify({ op: 'watch', key: session.key })}\n`));
|
|
203
|
+
socket.on('error', () => finish('ended'));
|
|
204
|
+
socket.on('close', () => finish('ended'));
|
|
205
|
+
socket.on('data', (data) => {
|
|
206
|
+
buffered += data;
|
|
207
|
+
let newline;
|
|
208
|
+
while ((newline = buffered.indexOf('\n')) !== -1) {
|
|
209
|
+
const line = buffered.slice(0, newline);
|
|
210
|
+
buffered = buffered.slice(newline + 1);
|
|
211
|
+
let message;
|
|
212
|
+
try {
|
|
213
|
+
message = JSON.parse(line);
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (message.type === 'screen') {
|
|
219
|
+
mirror = new Terminal({ cols: message.cols ?? 120, rows: message.rows ?? 40, scrollback: 0, allowProposedApi: true });
|
|
220
|
+
repaintAll();
|
|
221
|
+
}
|
|
222
|
+
if ((message.type === 'screen' || message.type === 'data') && mirror && message.data) {
|
|
223
|
+
mirror.write(message.data, () => { dirty = true; });
|
|
224
|
+
}
|
|
225
|
+
else if (message.type === 'end' || message.type === 'error') {
|
|
226
|
+
// Let what was written reach the screen before leaving.
|
|
227
|
+
if (mirror)
|
|
228
|
+
mirror.write('', () => { dirty = true; finish('ended'); });
|
|
229
|
+
else
|
|
230
|
+
finish('ended');
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
export async function run(args, io) {
|
|
237
|
+
const filter = args.find((arg) => !arg.startsWith('--'));
|
|
238
|
+
const unknown = args.find((arg) => arg.startsWith('--') && arg !== '--list');
|
|
239
|
+
if (unknown) {
|
|
240
|
+
io.err(`Unknown option: ${unknown}`);
|
|
241
|
+
for (const line of usage())
|
|
242
|
+
io.err(line);
|
|
243
|
+
return 2;
|
|
244
|
+
}
|
|
245
|
+
if (args.includes('--list')) {
|
|
246
|
+
const found = await discover();
|
|
247
|
+
if (found.bots === 0) {
|
|
248
|
+
io.err('No running bot to watch. Start one with "telecoder start" or "telecoder service install".');
|
|
249
|
+
return 1;
|
|
250
|
+
}
|
|
251
|
+
for (const line of formatList(found.sessions))
|
|
252
|
+
io.out(line);
|
|
253
|
+
return 0;
|
|
254
|
+
}
|
|
255
|
+
const out = process.stdout;
|
|
256
|
+
const input = process.stdin;
|
|
257
|
+
if (!out.isTTY) {
|
|
258
|
+
io.err('telecoder watch draws a live screen, so it needs a terminal. "telecoder watch --list" works anywhere.');
|
|
259
|
+
return 1;
|
|
260
|
+
}
|
|
261
|
+
if ((await discover()).bots === 0) {
|
|
262
|
+
io.err('No running bot to watch. Start one with "telecoder start" or "telecoder service install".');
|
|
263
|
+
io.err('(A bot with PTY_WATCH=false, or one that runs as another user, can\'t be watched.)');
|
|
264
|
+
return 1;
|
|
265
|
+
}
|
|
266
|
+
const abort = new AbortController();
|
|
267
|
+
const leave = () => abort.abort();
|
|
268
|
+
const onKey = (key) => {
|
|
269
|
+
// Ctrl+C, Ctrl+D, q
|
|
270
|
+
if (key[0] === 3 || key[0] === 4 || key.toString() === 'q')
|
|
271
|
+
leave();
|
|
272
|
+
};
|
|
273
|
+
if (input.isTTY) {
|
|
274
|
+
input.setRawMode(true);
|
|
275
|
+
input.resume();
|
|
276
|
+
input.on('data', onKey);
|
|
277
|
+
}
|
|
278
|
+
process.on('SIGINT', leave);
|
|
279
|
+
process.on('SIGTERM', leave);
|
|
280
|
+
// The alternate screen, so leaving gives the terminal back as it was; no
|
|
281
|
+
// cursor and no wrapping, since every row is placed where it belongs.
|
|
282
|
+
out.write('\x1b[?1049h\x1b[?25l\x1b[?7l\x1b[2J\x1b[H');
|
|
283
|
+
const notice = (text) => {
|
|
284
|
+
const row = out.rows || 1;
|
|
285
|
+
out.write(`\x1b[${row};1H\x1b[0;2m${text.slice(0, out.columns || 80)}\x1b[0m\x1b[K`);
|
|
286
|
+
};
|
|
287
|
+
try {
|
|
288
|
+
let waitingSaid = false;
|
|
289
|
+
while (!abort.signal.aborted) {
|
|
290
|
+
const session = pickSession((await discover()).sessions, filter);
|
|
291
|
+
if (!session) {
|
|
292
|
+
if (!waitingSaid) {
|
|
293
|
+
notice(filter
|
|
294
|
+
? ` Waiting for a Claude Code session in "${filter}". Send that chat a message. Ctrl+C leaves.`
|
|
295
|
+
: ' Waiting for a Claude Code session. Send your bot a message. Ctrl+C leaves.');
|
|
296
|
+
waitingSaid = true;
|
|
297
|
+
}
|
|
298
|
+
await Promise.race([sleep(POLL_MS), new Promise((resolve) => abort.signal.addEventListener('abort', () => resolve(), { once: true }))]);
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
waitingSaid = false;
|
|
302
|
+
const how = await show(session, out, abort.signal);
|
|
303
|
+
if (how === 'ended') {
|
|
304
|
+
notice(' That Claude Code session ended. Waiting for the next one. Ctrl+C leaves.');
|
|
305
|
+
waitingSaid = true;
|
|
306
|
+
await sleep(POLL_MS);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
finally {
|
|
311
|
+
out.write('\x1b[0m\x1b[?7h\x1b[?25h\x1b[?1049l');
|
|
312
|
+
if (input.isTTY) {
|
|
313
|
+
input.off('data', onKey);
|
|
314
|
+
input.setRawMode(false);
|
|
315
|
+
input.pause();
|
|
316
|
+
}
|
|
317
|
+
process.off('SIGINT', leave);
|
|
318
|
+
process.off('SIGTERM', leave);
|
|
319
|
+
}
|
|
320
|
+
return 0;
|
|
321
|
+
}
|
|
322
|
+
//# sourceMappingURL=watch.js.map
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `telecoder <command>` — argument parsing and the command table.
|
|
3
|
+
*
|
|
4
|
+
* Each command lives in its own module under ./commands/ and is imported only
|
|
5
|
+
* when it runs, so `telecoder --version` never loads the bot (whose config
|
|
6
|
+
* module reads and validates the .env as it is imported).
|
|
7
|
+
*
|
|
8
|
+
* A command resolves with an exit code, or with nothing when it has handed the
|
|
9
|
+
* process over to something long-running (`start`), which then owns the exit.
|
|
10
|
+
*
|
|
11
|
+
* Adding a command: write ./commands/<name>.ts exporting `run(args, io)`, and
|
|
12
|
+
* add an entry below.
|
|
13
|
+
*/
|
|
14
|
+
export const defaultIo = {
|
|
15
|
+
out: (line) => console.log(line),
|
|
16
|
+
err: (line) => console.error(line),
|
|
17
|
+
};
|
|
18
|
+
export const COMMANDS = {
|
|
19
|
+
start: {
|
|
20
|
+
summary: 'Run the bot; all of instances.json when it exists',
|
|
21
|
+
usage: '[--single|--multi]',
|
|
22
|
+
load: () => import('./commands/start.js'),
|
|
23
|
+
},
|
|
24
|
+
doctor: {
|
|
25
|
+
summary: 'Check that everything TeleCoder needs is in place',
|
|
26
|
+
usage: '[--live]',
|
|
27
|
+
load: () => import('./commands/doctor.js'),
|
|
28
|
+
},
|
|
29
|
+
init: {
|
|
30
|
+
summary: 'Set up a bot step by step: token, owner, workspace; writes the .env',
|
|
31
|
+
usage: '[options]',
|
|
32
|
+
load: () => import('./commands/init.js'),
|
|
33
|
+
},
|
|
34
|
+
service: {
|
|
35
|
+
summary: 'Run it in the background: systemd (Linux) or launchd (macOS)',
|
|
36
|
+
usage: 'install|status|logs|restart|uninstall',
|
|
37
|
+
load: () => import('./commands/service.js'),
|
|
38
|
+
},
|
|
39
|
+
watch: {
|
|
40
|
+
summary: 'Show the live Claude Code screen of a chat, read-only',
|
|
41
|
+
usage: '[<project>] [--list]',
|
|
42
|
+
load: () => import('./commands/watch.js'),
|
|
43
|
+
},
|
|
44
|
+
help: {
|
|
45
|
+
summary: 'Show this help',
|
|
46
|
+
load: () => import('./commands/help.js'),
|
|
47
|
+
},
|
|
48
|
+
version: {
|
|
49
|
+
summary: 'Print the installed version',
|
|
50
|
+
load: () => import('./commands/version.js'),
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
/** Flags that stand in for a command when given first. */
|
|
54
|
+
const ALIASES = {
|
|
55
|
+
'--version': 'version',
|
|
56
|
+
'-v': 'version',
|
|
57
|
+
'-V': 'version',
|
|
58
|
+
'--help': 'help',
|
|
59
|
+
'-h': 'help',
|
|
60
|
+
};
|
|
61
|
+
export function parseArgs(argv, commands = COMMANDS) {
|
|
62
|
+
const [first, ...rest] = argv;
|
|
63
|
+
if (first === undefined)
|
|
64
|
+
return { kind: 'command', name: 'help', args: [] };
|
|
65
|
+
const name = ALIASES[first] ?? first;
|
|
66
|
+
if (!Object.hasOwn(commands, name))
|
|
67
|
+
return { kind: 'unknown', name: first };
|
|
68
|
+
// `telecoder start --help` is help about start, not a start.
|
|
69
|
+
if (name !== 'help' && (rest.includes('--help') || rest.includes('-h'))) {
|
|
70
|
+
return { kind: 'command', name: 'help', args: [name] };
|
|
71
|
+
}
|
|
72
|
+
return { kind: 'command', name, args: rest };
|
|
73
|
+
}
|
|
74
|
+
export async function dispatch(argv, { commands = COMMANDS, io = defaultIo } = {}) {
|
|
75
|
+
const parsed = parseArgs(argv, commands);
|
|
76
|
+
if (parsed.kind === 'unknown') {
|
|
77
|
+
io.err(`Unknown command: ${parsed.name}`);
|
|
78
|
+
io.err('Run "telecoder help" to see the commands.');
|
|
79
|
+
return 2;
|
|
80
|
+
}
|
|
81
|
+
const mod = await commands[parsed.name].load();
|
|
82
|
+
return mod.run(parsed.args, io);
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=dispatch.js.map
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading, writing and backing up the .env `telecoder init` produces.
|
|
3
|
+
*
|
|
4
|
+
* A fresh file holds only what the wizard asked about, under a header pointing
|
|
5
|
+
* at .env.example for everything else. Editing an existing file changes the
|
|
6
|
+
* lines for the keys that changed and leaves every other line — comments,
|
|
7
|
+
* settings the wizard never asks about — exactly as it was.
|
|
8
|
+
*
|
|
9
|
+
* The file is written 0600 in a 0700 directory: it holds the bot token.
|
|
10
|
+
*/
|
|
11
|
+
import * as fs from 'fs';
|
|
12
|
+
import * as path from 'path';
|
|
13
|
+
export const ENV_HEADER = [
|
|
14
|
+
'# TeleCoder configuration, written by `telecoder init`.',
|
|
15
|
+
'#',
|
|
16
|
+
'# Only the settings the wizard asked about are here; everything else runs on',
|
|
17
|
+
'# its default. Every setting, with what it does, is listed in .env.example',
|
|
18
|
+
'# (in the package, or https://github.com/telecoderbot/telecoder/blob/main/.env.example)',
|
|
19
|
+
'# and in the Configuration Reference: https://telecoder.dev',
|
|
20
|
+
'# Run `telecoder init` again to change these, or edit this file by hand.',
|
|
21
|
+
];
|
|
22
|
+
/** Quote a value only when dotenv would otherwise misread it. */
|
|
23
|
+
export function formatValue(value) {
|
|
24
|
+
if (value === '' || /^[A-Za-z0-9_@%+=:,./~-]+$/.test(value))
|
|
25
|
+
return value;
|
|
26
|
+
return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
|
|
27
|
+
}
|
|
28
|
+
export function renderEnv(values) {
|
|
29
|
+
const lines = [...ENV_HEADER, ''];
|
|
30
|
+
for (const [key, value] of Object.entries(values))
|
|
31
|
+
lines.push(`${key}=${formatValue(value)}`);
|
|
32
|
+
return `${lines.join('\n')}\n`;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Apply `updates` to the text of an existing .env: replace the first active
|
|
36
|
+
* `KEY=` line of each key (dropping any later duplicates, which would win),
|
|
37
|
+
* remove the lines of keys set to undefined, and append keys not there yet.
|
|
38
|
+
*/
|
|
39
|
+
export function updateEnvText(text, updates) {
|
|
40
|
+
const lines = text.split('\n');
|
|
41
|
+
if (lines.at(-1) === '')
|
|
42
|
+
lines.pop();
|
|
43
|
+
const done = new Set();
|
|
44
|
+
const out = [];
|
|
45
|
+
for (const line of lines) {
|
|
46
|
+
const key = line.match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/)?.[1];
|
|
47
|
+
if (key === undefined || !Object.hasOwn(updates, key)) {
|
|
48
|
+
out.push(line);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (done.has(key))
|
|
52
|
+
continue;
|
|
53
|
+
done.add(key);
|
|
54
|
+
const value = updates[key];
|
|
55
|
+
if (value !== undefined)
|
|
56
|
+
out.push(`${key}=${formatValue(value)}`);
|
|
57
|
+
}
|
|
58
|
+
const added = Object.entries(updates).filter(([key, value]) => !done.has(key) && value !== undefined);
|
|
59
|
+
if (added.length) {
|
|
60
|
+
if (out.length && out.at(-1)?.trim() !== '')
|
|
61
|
+
out.push('');
|
|
62
|
+
for (const [key, value] of added)
|
|
63
|
+
out.push(`${key}=${formatValue(value)}`);
|
|
64
|
+
}
|
|
65
|
+
return `${out.join('\n')}\n`;
|
|
66
|
+
}
|
|
67
|
+
/** `.env.bak-20261113-142501`: sorts by time, and says what it was. */
|
|
68
|
+
export function backupName(file, now = new Date()) {
|
|
69
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
70
|
+
const stamp = `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-` +
|
|
71
|
+
`${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`;
|
|
72
|
+
return `${file}.bak-${stamp}`;
|
|
73
|
+
}
|
|
74
|
+
/** Copy `file` aside, keeping it private. Returns the backup's path. */
|
|
75
|
+
export function backupFile(file, now) {
|
|
76
|
+
const backup = backupName(file, now);
|
|
77
|
+
fs.copyFileSync(file, backup);
|
|
78
|
+
fs.chmodSync(backup, 0o600);
|
|
79
|
+
return backup;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Write the .env privately. The directory is created 0700; an existing one is
|
|
83
|
+
* tightened to 0700 only when it is the config home TeleCoder made (not a
|
|
84
|
+
* source checkout, whose directory other things live in).
|
|
85
|
+
*/
|
|
86
|
+
export function writeEnvFile(file, content, { ownDir }) {
|
|
87
|
+
const dir = path.dirname(file);
|
|
88
|
+
if (!fs.existsSync(dir))
|
|
89
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
90
|
+
if (ownDir)
|
|
91
|
+
fs.chmodSync(dir, 0o700);
|
|
92
|
+
// Write beside it and rename, so a crash never leaves half a config — and
|
|
93
|
+
// create it 0600 so the token is never readable, not even for a moment.
|
|
94
|
+
const tmp = path.join(dir, `.env.init-${process.pid}.tmp`);
|
|
95
|
+
fs.writeFileSync(tmp, content, { mode: 0o600 });
|
|
96
|
+
fs.chmodSync(tmp, 0o600);
|
|
97
|
+
fs.renameSync(tmp, file);
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=env-file.js.map
|