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,363 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live prose for a PTY turn: what the model has written so far, for the
|
|
3
|
+
* streaming message to show while the turn is still running.
|
|
4
|
+
*
|
|
5
|
+
* The shape is the final reply's: the turn's assistant text records joined
|
|
6
|
+
* the way readLastAssistantTurnText joins them, minus what an ask_user flush
|
|
7
|
+
* already sent (turn-prose.ts). The live message then grows into the final
|
|
8
|
+
* one instead of being replaced by something else at the end.
|
|
9
|
+
*
|
|
10
|
+
* Two sources, because neither is enough alone.
|
|
11
|
+
*
|
|
12
|
+
* The session log is the trustworthy one. Claude Code writes an assistant
|
|
13
|
+
* record per content block, as the block completes, in the Markdown the model
|
|
14
|
+
* wrote; the end-of-turn read uses the very same records. But a block lands
|
|
15
|
+
* only once it is whole, so a turn that is one long answer would show nothing
|
|
16
|
+
* until it was over.
|
|
17
|
+
*
|
|
18
|
+
* The screen fills that gap. Measured on claude 2.1.311–2.1.322 at 120x40: a
|
|
19
|
+
* text block is drawn as it streams, a finished line at a time (` 12.` sits
|
|
20
|
+
* alone until its line is done), and its record is written when the last line
|
|
21
|
+
* is. While text streams the live spinner is gone — it comes back for thinking
|
|
22
|
+
* and for every tool call — and the block is the last thing above the input
|
|
23
|
+
* box. The TUI runs in the alternate screen, so there is no scrollback: a block
|
|
24
|
+
* taller than the screen scrolls its `●` head off the top.
|
|
25
|
+
*
|
|
26
|
+
* The screen is also where everything that must never be streamed lives: tool
|
|
27
|
+
* calls, their output, the spinner, hook output, notices. Some of it is drawn
|
|
28
|
+
* in shapes prose has too — a call's description row (`● Printing hello to
|
|
29
|
+
* stdout`), and with a running call's `●` blinking off, a row indented like a
|
|
30
|
+
* text block's — so the screen is only read under every one of these:
|
|
31
|
+
*
|
|
32
|
+
* - the input box is drawn (a dialog replaces it), and nothing between it
|
|
33
|
+
* and the block's `●` head sits at column 0 but blank rows — a spinner, the
|
|
34
|
+
* turn's `✻` summary, a prompt echo, any notice claude draws as its own row;
|
|
35
|
+
* - nothing in the block is a tool's: no `⎿` row, no collapsed-call hint, no
|
|
36
|
+
* head shaped like a call or an agent's notice;
|
|
37
|
+
* - the block has been seen to grow, its rows extending the ones before.
|
|
38
|
+
* Streaming text grows; a notice is drawn whole and stays as it is;
|
|
39
|
+
* - its head first showed after the turn's prompt reached the log, and no
|
|
40
|
+
* record of the turn has landed with its text. Once one has, the block is
|
|
41
|
+
* read from the log, and anything the screen then draws under it — a
|
|
42
|
+
* collapsed call's row, which carries no `●` — is not read at all.
|
|
43
|
+
*
|
|
44
|
+
* What comes off the screen is drawn text, not Markdown: emphasis markers and
|
|
45
|
+
* `#`s are gone and a table is box drawing. That is the price of seeing a block
|
|
46
|
+
* before it lands, and it lasts only until the block's record replaces it.
|
|
47
|
+
* Once a block's head scrolls off, the screen can no longer say which rows are
|
|
48
|
+
* the block's, so it stops growing on the live message until its record lands.
|
|
49
|
+
*/
|
|
50
|
+
import * as fs from 'fs';
|
|
51
|
+
import { turnRecordIn } from './session-jsonl.js';
|
|
52
|
+
import { stripDeliveredPrefix } from './turn-prose.js';
|
|
53
|
+
/** How readLastAssistantTurnText joins a turn's text records. */
|
|
54
|
+
const RECORD_SEPARATOR = '\n\n';
|
|
55
|
+
/**
|
|
56
|
+
* A turn's text records, read from the session log as they land.
|
|
57
|
+
*
|
|
58
|
+
* Reads only what was appended since the last poll, starting where the log
|
|
59
|
+
* ended at submit, and only whole lines: claude appends a record in more than
|
|
60
|
+
* one write, and half a line doesn't parse. Records are classified by
|
|
61
|
+
* turnRecordIn, the end-of-turn read's own rule, so the two cannot disagree on
|
|
62
|
+
* what the turn's prose is.
|
|
63
|
+
*/
|
|
64
|
+
export class TurnProseReader {
|
|
65
|
+
filePath;
|
|
66
|
+
commandMode;
|
|
67
|
+
cursor;
|
|
68
|
+
pending = Buffer.alloc(0);
|
|
69
|
+
texts = [];
|
|
70
|
+
promptSeen = false;
|
|
71
|
+
/**
|
|
72
|
+
* `commandMode` is for a slash command, which is answered by every text
|
|
73
|
+
* record it writes, with no prompt boundary (see commandReplyIn).
|
|
74
|
+
*/
|
|
75
|
+
constructor(filePath, fromByte, commandMode = false) {
|
|
76
|
+
this.filePath = filePath;
|
|
77
|
+
this.commandMode = commandMode;
|
|
78
|
+
this.cursor = fromByte;
|
|
79
|
+
}
|
|
80
|
+
/** The turn's text records so far, oldest first. */
|
|
81
|
+
get records() { return this.texts; }
|
|
82
|
+
/** Whether the turn's prompt is in the log, which is when its prose can start. */
|
|
83
|
+
get started() { return this.promptSeen; }
|
|
84
|
+
/** Read whatever has been appended since the last poll. */
|
|
85
|
+
poll() {
|
|
86
|
+
let size;
|
|
87
|
+
try {
|
|
88
|
+
size = fs.statSync(this.filePath).size;
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
// A log shorter than our cursor was rewritten underneath us; nothing in it
|
|
94
|
+
// is this turn's any more. Keep what we have, the end-of-turn read decides.
|
|
95
|
+
if (size <= this.cursor)
|
|
96
|
+
return;
|
|
97
|
+
const chunk = Buffer.alloc(size - this.cursor);
|
|
98
|
+
try {
|
|
99
|
+
const fd = fs.openSync(this.filePath, 'r');
|
|
100
|
+
try {
|
|
101
|
+
fs.readSync(fd, chunk, 0, chunk.length, this.cursor);
|
|
102
|
+
}
|
|
103
|
+
finally {
|
|
104
|
+
fs.closeSync(fd);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
this.cursor = size;
|
|
111
|
+
this.consume(chunk);
|
|
112
|
+
}
|
|
113
|
+
/** Feed raw log bytes. Split out from poll for tests. */
|
|
114
|
+
consume(chunk) {
|
|
115
|
+
const buf = this.pending.length ? Buffer.concat([this.pending, chunk]) : chunk;
|
|
116
|
+
const lastNewline = buf.lastIndexOf(0x0a);
|
|
117
|
+
if (lastNewline === -1) {
|
|
118
|
+
this.pending = buf;
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
this.pending = Buffer.from(buf.subarray(lastNewline + 1));
|
|
122
|
+
for (const line of buf.subarray(0, lastNewline).toString('utf-8').split('\n')) {
|
|
123
|
+
const rec = turnRecordIn(line);
|
|
124
|
+
if (rec?.kind === 'prompt') {
|
|
125
|
+
this.promptSeen = true;
|
|
126
|
+
// Another real prompt is another turn. Not ours to stream, but the
|
|
127
|
+
// end-of-turn read slices from the last one, so do as it does.
|
|
128
|
+
if (!this.commandMode)
|
|
129
|
+
this.texts = [];
|
|
130
|
+
}
|
|
131
|
+
else if (rec?.kind === 'text') {
|
|
132
|
+
if (this.commandMode)
|
|
133
|
+
this.promptSeen = true;
|
|
134
|
+
if (this.promptSeen)
|
|
135
|
+
this.texts.push(rec.text);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// ---- Reading the in-flight block off the screen -----------------------------
|
|
141
|
+
/** The input box's borders: a full-width rule. Same rule as tui-state.ts. */
|
|
142
|
+
const RULE = /^\s*─{10,}\s*$/u;
|
|
143
|
+
/** The input box's own row. */
|
|
144
|
+
const INPUT_ROW = /^\s*(?:│\s*)?❯/u;
|
|
145
|
+
const SIDE_BORDERED_INPUT = /│\s*❯/u;
|
|
146
|
+
/** A transcript block's head row: claude's output bullet at column 0. */
|
|
147
|
+
const HEAD_ROW = /^●(?:\s|$)/u;
|
|
148
|
+
/**
|
|
149
|
+
* The rows right above the input box that hold chrome drawn flush right:
|
|
150
|
+
* `◐ medium · /effort`, and notices like `✔ Update installed · /restart to
|
|
151
|
+
* apply` in the same spot. No row of a text block starts this far in.
|
|
152
|
+
*/
|
|
153
|
+
const RIGHT_ALIGNED_INDENT = 40;
|
|
154
|
+
/** Hangs under a tool call: its output, hook output, `Interrupted`. */
|
|
155
|
+
const TOOL_RESULT_ROW = /^\s*⎿/u;
|
|
156
|
+
/** A collapsed run of calls: `Read 1 file (ctrl+o to expand)`. */
|
|
157
|
+
const COLLAPSED_CALL_ROW = /\(ctrl\+o to expand\)\s*$/u;
|
|
158
|
+
/** A call's own head: `Bash(npm test)`, `mcp__x__y(…)`, cut off or not. */
|
|
159
|
+
const TOOL_CALL_HEAD = /^[A-Za-z][\w:-]*\(/u;
|
|
160
|
+
/** A background agent reporting in: `Agent "Run the checks" finished · 4s`. */
|
|
161
|
+
const AGENT_NOTICE_HEAD = /^Agent\s+".*"\s+\w+\s+·/u;
|
|
162
|
+
/** A call still running reads `Running 1 shell command…`, `Reading 1 file…`. */
|
|
163
|
+
const RUNNING_HEAD = /…\s*$/u;
|
|
164
|
+
/**
|
|
165
|
+
* The rows of the text block claude is streaming, as drawn: the `●` head's text
|
|
166
|
+
* first, each row's two-column indent removed, trailing blank rows dropped.
|
|
167
|
+
* Undefined when the screen shows no such block, or shows anything that makes
|
|
168
|
+
* it unsafe to read one — see the module comment for the rules.
|
|
169
|
+
*/
|
|
170
|
+
export function inflightBlockRows(screenText) {
|
|
171
|
+
const lines = screenText.split('\n');
|
|
172
|
+
let boxTop = -1;
|
|
173
|
+
for (let i = lines.length - 1; i > 0; i--) {
|
|
174
|
+
if (SIDE_BORDERED_INPUT.test(lines[i])) {
|
|
175
|
+
boxTop = i;
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
if (INPUT_ROW.test(lines[i]) && RULE.test(lines[i - 1])) {
|
|
179
|
+
boxTop = i - 1;
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (boxTop === -1)
|
|
184
|
+
return undefined;
|
|
185
|
+
let end = boxTop;
|
|
186
|
+
while (end > 0 && isBlankOrRightAligned(lines[end - 1]))
|
|
187
|
+
end--;
|
|
188
|
+
if (end === 0)
|
|
189
|
+
return undefined;
|
|
190
|
+
let head = -1;
|
|
191
|
+
for (let i = end - 1; i >= 0; i--) {
|
|
192
|
+
const line = lines[i];
|
|
193
|
+
if (HEAD_ROW.test(line)) {
|
|
194
|
+
head = i;
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
if (!line.trim())
|
|
198
|
+
continue;
|
|
199
|
+
// Everything of a text block below its head is indented. A row that isn't
|
|
200
|
+
// is something else's — the spinner, the turn summary, a prompt, a notice.
|
|
201
|
+
if (!line.startsWith(' '))
|
|
202
|
+
return undefined;
|
|
203
|
+
if (TOOL_RESULT_ROW.test(line) || COLLAPSED_CALL_ROW.test(line))
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
// No head on screen: either it scrolled off or there is no block. The rows
|
|
207
|
+
// left can't be told apart, so neither is read.
|
|
208
|
+
if (head === -1)
|
|
209
|
+
return undefined;
|
|
210
|
+
const headText = lines[head].replace(/^●\s*/u, '').trimEnd();
|
|
211
|
+
if (!headText.trim())
|
|
212
|
+
return undefined;
|
|
213
|
+
if (TOOL_CALL_HEAD.test(headText) || AGENT_NOTICE_HEAD.test(headText)
|
|
214
|
+
|| RUNNING_HEAD.test(headText) || COLLAPSED_CALL_ROW.test(headText)) {
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
const body = lines.slice(head + 1, end).map((line) => line.slice(2).trimEnd());
|
|
218
|
+
while (body.length && !body[body.length - 1].trim())
|
|
219
|
+
body.pop();
|
|
220
|
+
return [headText, ...body];
|
|
221
|
+
}
|
|
222
|
+
function isBlankOrRightAligned(line) {
|
|
223
|
+
if (!line.trim())
|
|
224
|
+
return true;
|
|
225
|
+
return line.length - line.trimStart().length >= RIGHT_ALIGNED_INDENT;
|
|
226
|
+
}
|
|
227
|
+
/** A list item, table row or other row that starts a line of its own. */
|
|
228
|
+
const LINE_START = /^\s*(?:\d+[.)]\s|[-*+•]\s|[│┌├└╭╰])/u;
|
|
229
|
+
/** Rows of a table drawn in box drawing, which fill the width without wrapping. */
|
|
230
|
+
const BOX_DRAWING = /[│┌┐└┘├┤┬┴┼─]/u;
|
|
231
|
+
/**
|
|
232
|
+
* Join rows the TUI wrapped back into the lines the model wrote.
|
|
233
|
+
*
|
|
234
|
+
* Claude wraps at word boundaries one column short of the terminal's width,
|
|
235
|
+
* so a row ends a wrapped line when the next row's first word would not have
|
|
236
|
+
* fit after it. A row that ends a real line early is left alone, and so is a
|
|
237
|
+
* row that opens a list item or belongs to a table. Getting one wrong costs a
|
|
238
|
+
* line break in the live message, until the record replaces it.
|
|
239
|
+
*/
|
|
240
|
+
export function unwrapRows(rows, cols) {
|
|
241
|
+
const out = [];
|
|
242
|
+
for (let i = 0; i < rows.length; i++) {
|
|
243
|
+
const row = rows[i];
|
|
244
|
+
const prev = out.length ? out[out.length - 1] : undefined;
|
|
245
|
+
const prevRow = i > 0 ? rows[i - 1] : undefined;
|
|
246
|
+
if (prev !== undefined && prevRow !== undefined && wraps(prevRow, row, cols)) {
|
|
247
|
+
out[out.length - 1] = `${prev} ${row.trim()}`;
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
out.push(row);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return out.join('\n');
|
|
254
|
+
}
|
|
255
|
+
function wraps(row, next, cols) {
|
|
256
|
+
if (!row.trim() || !next.trim())
|
|
257
|
+
return false;
|
|
258
|
+
if (LINE_START.test(next) || BOX_DRAWING.test(row) || BOX_DRAWING.test(next))
|
|
259
|
+
return false;
|
|
260
|
+
const firstWord = next.trim().split(/\s/u)[0];
|
|
261
|
+
// Rows here have lost the two-column indent the screen drew them with.
|
|
262
|
+
return row.length + 2 + 1 + firstWord.length > cols - 1;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Text reduced to its letters and digits, for asking whether a drawn block and
|
|
266
|
+
* a Markdown record hold the same words. The drawing drops `**`, `#`, fences
|
|
267
|
+
* and link brackets and turns a table into box drawing; none of that is a
|
|
268
|
+
* letter or a digit.
|
|
269
|
+
*/
|
|
270
|
+
export function comparable(text) {
|
|
271
|
+
return text.replace(/[^\p{L}\p{N}]+/gu, '').toLowerCase();
|
|
272
|
+
}
|
|
273
|
+
/** Does `next` continue `prev`: the same rows, the last one perhaps finished since, and more? */
|
|
274
|
+
function grewFrom(prev, next) {
|
|
275
|
+
if (next.length < prev.length)
|
|
276
|
+
return false;
|
|
277
|
+
const last = prev.length - 1;
|
|
278
|
+
for (let i = 0; i < last; i++)
|
|
279
|
+
if (prev[i] !== next[i])
|
|
280
|
+
return false;
|
|
281
|
+
if (!next[last].startsWith(prev[last]))
|
|
282
|
+
return false;
|
|
283
|
+
return next.length > prev.length || next[last].length > prev[last].length;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* One PTY turn's live prose. The provider calls tick() on a timer while the
|
|
287
|
+
* turn runs; it calls `emit` with the turn's prose whenever that changed.
|
|
288
|
+
*/
|
|
289
|
+
export class PtyTurnStream {
|
|
290
|
+
reader;
|
|
291
|
+
emit;
|
|
292
|
+
delivered;
|
|
293
|
+
block;
|
|
294
|
+
lastEmitted = '';
|
|
295
|
+
constructor(reader, emit,
|
|
296
|
+
/** What an ask_user flush has already sent this turn (turn-prose.ts). */
|
|
297
|
+
delivered) {
|
|
298
|
+
this.reader = reader;
|
|
299
|
+
this.emit = emit;
|
|
300
|
+
this.delivered = delivered;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Bring the live prose up to date. `screenText` is the screen as it stands,
|
|
304
|
+
* or undefined when the caller knows it can't hold streaming text (a dialog
|
|
305
|
+
* is being answered, the turn has stopped).
|
|
306
|
+
*/
|
|
307
|
+
tick(screenText, cols) {
|
|
308
|
+
this.reader.poll();
|
|
309
|
+
if (screenText !== undefined && this.reader.started)
|
|
310
|
+
this.observe(inflightBlockRows(screenText));
|
|
311
|
+
const text = this.compose(cols);
|
|
312
|
+
// Nothing yet is not news. Once something went out, every change is,
|
|
313
|
+
// including to empty: an ask_user flush just moved it all above the question.
|
|
314
|
+
if (text === this.lastEmitted)
|
|
315
|
+
return;
|
|
316
|
+
this.lastEmitted = text;
|
|
317
|
+
this.emit(text);
|
|
318
|
+
}
|
|
319
|
+
observe(rows) {
|
|
320
|
+
// Nothing safe to read this time: keep what was read before. The block is
|
|
321
|
+
// either still there behind a frame we refused, or has scrolled its head
|
|
322
|
+
// away, and either way the log will bring the rest.
|
|
323
|
+
if (!rows)
|
|
324
|
+
return;
|
|
325
|
+
const block = this.block;
|
|
326
|
+
if (block && rows[0] === block.rows[0]) {
|
|
327
|
+
if (grewFrom(block.rows, rows))
|
|
328
|
+
block.grown = true;
|
|
329
|
+
block.rows = rows;
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
this.block = { rows, recordsAtSighting: this.reader.records.length, grown: false };
|
|
333
|
+
}
|
|
334
|
+
/** The turn's prose: its records, then the block still streaming, if it may be shown. */
|
|
335
|
+
compose(cols) {
|
|
336
|
+
const records = this.reader.records;
|
|
337
|
+
const parts = [...records];
|
|
338
|
+
const inflight = this.inflightText(cols);
|
|
339
|
+
if (inflight)
|
|
340
|
+
parts.push(inflight);
|
|
341
|
+
return stripDeliveredPrefix(parts.join(RECORD_SEPARATOR), this.delivered());
|
|
342
|
+
}
|
|
343
|
+
inflightText(cols) {
|
|
344
|
+
const block = this.block;
|
|
345
|
+
if (!block?.grown)
|
|
346
|
+
return undefined;
|
|
347
|
+
const text = unwrapRows(block.rows, cols).trim();
|
|
348
|
+
const drawn = comparable(text);
|
|
349
|
+
if (!drawn)
|
|
350
|
+
return undefined;
|
|
351
|
+
// The block's own record, once it lands, holds the same words. Only records
|
|
352
|
+
// from around its sighting are compared: one landed just before it showed
|
|
353
|
+
// (it streamed and finished between two ticks), and any since.
|
|
354
|
+
const landed = this.reader.records
|
|
355
|
+
.slice(Math.max(0, block.recordsAtSighting - 1))
|
|
356
|
+
.some((record) => {
|
|
357
|
+
const written = comparable(record);
|
|
358
|
+
return !!written && (written.startsWith(drawn) || drawn.startsWith(written));
|
|
359
|
+
});
|
|
360
|
+
return landed ? undefined : text;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
//# sourceMappingURL=pty-stream.js.map
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lets `telecoder watch` look at a running claude's screen.
|
|
3
|
+
*
|
|
4
|
+
* Each chat's claude draws its screen into a terminal that exists
|
|
5
|
+
* only in this process. This module serves those screens, read-only, over a
|
|
6
|
+
* Unix socket: a watcher gets the screen as it is now and then everything
|
|
7
|
+
* claude writes after it. Nothing a watcher sends reaches claude.
|
|
8
|
+
*
|
|
9
|
+
* The socket is `~/.telecoder/watch/<bot username>.sock`, one per bot (several
|
|
10
|
+
* bots can share a process, as worker threads), in a directory only the owner
|
|
11
|
+
* can enter. So only the user the bot runs as can watch, which is the user who
|
|
12
|
+
* can already read every transcript.
|
|
13
|
+
*
|
|
14
|
+
* Protocol: one JSON object per line.
|
|
15
|
+
* → {"op":"list"} ← {"type":"sessions","bot":…,"sessions":[…]}
|
|
16
|
+
* → {"op":"watch","key":…} ← {"type":"screen","key","cols","rows","data"}
|
|
17
|
+
* ← {"type":"data","data"} …
|
|
18
|
+
* ← {"type":"end"} (claude exited)
|
|
19
|
+
* ← {"type":"error","message"}
|
|
20
|
+
*/
|
|
21
|
+
import * as fs from 'fs';
|
|
22
|
+
import * as net from 'net';
|
|
23
|
+
import * as path from 'path';
|
|
24
|
+
import { getStateDir } from '../utils/json-store.js';
|
|
25
|
+
import { screenSnapshot } from './screen-render.js';
|
|
26
|
+
export const WATCH_DIR_NAME = 'watch';
|
|
27
|
+
/** A watcher this far behind is dropped rather than buffered for without end. */
|
|
28
|
+
const MAX_BACKLOG_BYTES = 8 * 1024 * 1024;
|
|
29
|
+
const watched = new Map();
|
|
30
|
+
const byTerm = new WeakMap();
|
|
31
|
+
let server = null;
|
|
32
|
+
let socketPath = null;
|
|
33
|
+
let botName = '';
|
|
34
|
+
export function getWatchDir() {
|
|
35
|
+
return path.join(getStateDir(), WATCH_DIR_NAME);
|
|
36
|
+
}
|
|
37
|
+
function send(socket, message) {
|
|
38
|
+
if (socket.destroyed)
|
|
39
|
+
return;
|
|
40
|
+
if (socket.writableLength > MAX_BACKLOG_BYTES) {
|
|
41
|
+
socket.destroy();
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
socket.write(`${JSON.stringify(message)}\n`);
|
|
45
|
+
}
|
|
46
|
+
/** A claude has started: its screen can be watched from now on. */
|
|
47
|
+
export function watchRegister(key, cwd, xterm) {
|
|
48
|
+
watchUnregister(key);
|
|
49
|
+
const now = Date.now();
|
|
50
|
+
const entry = {
|
|
51
|
+
info: { key, cwd, cols: xterm.cols, rows: xterm.rows, startedAt: now, lastActiveAt: now },
|
|
52
|
+
xterm,
|
|
53
|
+
watchers: new Set(),
|
|
54
|
+
};
|
|
55
|
+
watched.set(key, entry);
|
|
56
|
+
byTerm.set(xterm, entry);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Output from the claude that draws into `xterm`. Call it right after the same
|
|
60
|
+
* chunk went into that xterm, so a snapshot and the chunks after it line up.
|
|
61
|
+
*/
|
|
62
|
+
export function watchData(xterm, chunk) {
|
|
63
|
+
// Not found for a claude replaced at its key (/clear) that still writes as it exits.
|
|
64
|
+
const entry = byTerm.get(xterm);
|
|
65
|
+
if (!entry)
|
|
66
|
+
return;
|
|
67
|
+
entry.info.lastActiveAt = Date.now();
|
|
68
|
+
for (const watcher of entry.watchers) {
|
|
69
|
+
if (watcher.pending)
|
|
70
|
+
watcher.pending.push(chunk);
|
|
71
|
+
else
|
|
72
|
+
send(watcher.socket, { type: 'data', data: chunk });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** `key`'s claude is gone. With `xterm`, only if it is still that one. */
|
|
76
|
+
export function watchUnregister(key, xterm) {
|
|
77
|
+
const entry = watched.get(key);
|
|
78
|
+
if (!entry || (xterm && entry.xterm !== xterm))
|
|
79
|
+
return;
|
|
80
|
+
watched.delete(key);
|
|
81
|
+
byTerm.delete(entry.xterm);
|
|
82
|
+
for (const watcher of entry.watchers)
|
|
83
|
+
send(watcher.socket, { type: 'end' });
|
|
84
|
+
entry.watchers.clear();
|
|
85
|
+
}
|
|
86
|
+
function subscribe(socket, key) {
|
|
87
|
+
const entry = watched.get(key);
|
|
88
|
+
if (!entry) {
|
|
89
|
+
send(socket, { type: 'error', message: 'No such session.' });
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
const watcher = { socket, pending: [] };
|
|
93
|
+
entry.watchers.add(watcher);
|
|
94
|
+
// xterm parses what it is given a little later. An empty write's callback
|
|
95
|
+
// runs once everything given before it is on the screen, so the snapshot
|
|
96
|
+
// holds exactly the output up to here, and `pending` everything after.
|
|
97
|
+
entry.xterm.write('', () => {
|
|
98
|
+
if (!entry.watchers.has(watcher))
|
|
99
|
+
return;
|
|
100
|
+
send(socket, {
|
|
101
|
+
type: 'screen',
|
|
102
|
+
key,
|
|
103
|
+
cols: entry.xterm.cols,
|
|
104
|
+
rows: entry.xterm.rows,
|
|
105
|
+
data: screenSnapshot(entry.xterm),
|
|
106
|
+
});
|
|
107
|
+
for (const chunk of watcher.pending ?? [])
|
|
108
|
+
send(socket, { type: 'data', data: chunk });
|
|
109
|
+
watcher.pending = null;
|
|
110
|
+
});
|
|
111
|
+
return watcher;
|
|
112
|
+
}
|
|
113
|
+
function unsubscribe(watcher) {
|
|
114
|
+
if (!watcher)
|
|
115
|
+
return;
|
|
116
|
+
for (const entry of watched.values())
|
|
117
|
+
entry.watchers.delete(watcher);
|
|
118
|
+
}
|
|
119
|
+
function onConnection(socket) {
|
|
120
|
+
let buffered = '';
|
|
121
|
+
let watcher = null;
|
|
122
|
+
socket.setEncoding('utf8');
|
|
123
|
+
socket.on('error', () => socket.destroy());
|
|
124
|
+
socket.on('close', () => unsubscribe(watcher));
|
|
125
|
+
socket.on('data', (data) => {
|
|
126
|
+
buffered += data;
|
|
127
|
+
// Requests are a few dozen bytes. Anything long is not a watcher.
|
|
128
|
+
if (buffered.length > 4096) {
|
|
129
|
+
socket.destroy();
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
let newline;
|
|
133
|
+
while ((newline = buffered.indexOf('\n')) !== -1) {
|
|
134
|
+
const line = buffered.slice(0, newline);
|
|
135
|
+
buffered = buffered.slice(newline + 1);
|
|
136
|
+
let request;
|
|
137
|
+
try {
|
|
138
|
+
request = JSON.parse(line);
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
socket.destroy();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (request?.op === 'list') {
|
|
145
|
+
send(socket, { type: 'sessions', bot: botName, sessions: [...watched.values()].map((e) => e.info) });
|
|
146
|
+
}
|
|
147
|
+
else if (request?.op === 'watch' && typeof request.key === 'string') {
|
|
148
|
+
unsubscribe(watcher);
|
|
149
|
+
watcher = subscribe(socket, request.key);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
send(socket, { type: 'error', message: 'Unknown request.' });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Start serving this process's claude screens. `name` is the bot's username,
|
|
159
|
+
* shown to a watcher choosing between several bots.
|
|
160
|
+
*/
|
|
161
|
+
export async function startWatchServer(name, dir = getWatchDir()) {
|
|
162
|
+
if (server)
|
|
163
|
+
return socketPath;
|
|
164
|
+
botName = name;
|
|
165
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
166
|
+
fs.chmodSync(dir, 0o700);
|
|
167
|
+
const file = path.join(dir, `${name.replace(/[^A-Za-z0-9_-]/g, '_') || 'bot'}.sock`);
|
|
168
|
+
// One left by an earlier run of this bot: nothing listens on it any more.
|
|
169
|
+
try {
|
|
170
|
+
fs.unlinkSync(file);
|
|
171
|
+
}
|
|
172
|
+
catch { /* not there */ }
|
|
173
|
+
const created = net.createServer(onConnection);
|
|
174
|
+
await new Promise((resolve, reject) => {
|
|
175
|
+
created.once('error', reject);
|
|
176
|
+
created.listen(file, () => {
|
|
177
|
+
created.off('error', reject);
|
|
178
|
+
resolve();
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
fs.chmodSync(file, 0o600);
|
|
182
|
+
// The socket must not keep a stopping bot alive.
|
|
183
|
+
created.unref();
|
|
184
|
+
server = created;
|
|
185
|
+
socketPath = file;
|
|
186
|
+
return file;
|
|
187
|
+
}
|
|
188
|
+
export function stopWatchServer() {
|
|
189
|
+
for (const key of [...watched.keys()])
|
|
190
|
+
watchUnregister(key);
|
|
191
|
+
server?.close();
|
|
192
|
+
server = null;
|
|
193
|
+
if (socketPath) {
|
|
194
|
+
try {
|
|
195
|
+
fs.unlinkSync(socketPath);
|
|
196
|
+
}
|
|
197
|
+
catch { /* already gone */ }
|
|
198
|
+
}
|
|
199
|
+
socketPath = null;
|
|
200
|
+
}
|
|
201
|
+
//# sourceMappingURL=pty-watch.js.map
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { markInFlight, clearInFlight } from './in-flight-tracker.js';
|
|
2
|
+
const activeAbortControllers = new Map();
|
|
3
|
+
const pendingQueues = new Map();
|
|
4
|
+
const processingFlags = new Map();
|
|
5
|
+
// Chats where a cancel was initiated — checked by the pty provider to tell a
|
|
6
|
+
// /cancel from a turn that failed on its own.
|
|
7
|
+
const cancelledChats = new Set();
|
|
8
|
+
export function getAbortController(sessionKey) {
|
|
9
|
+
return activeAbortControllers.get(sessionKey);
|
|
10
|
+
}
|
|
11
|
+
export function setAbortController(sessionKey, controller) {
|
|
12
|
+
activeAbortControllers.set(sessionKey, controller);
|
|
13
|
+
}
|
|
14
|
+
export function clearAbortController(sessionKey) {
|
|
15
|
+
activeAbortControllers.delete(sessionKey);
|
|
16
|
+
}
|
|
17
|
+
export function isCancelled(sessionKey) {
|
|
18
|
+
return cancelledChats.has(sessionKey);
|
|
19
|
+
}
|
|
20
|
+
export function clearCancelled(sessionKey) {
|
|
21
|
+
cancelledChats.delete(sessionKey);
|
|
22
|
+
}
|
|
23
|
+
export function isProcessing(sessionKey) {
|
|
24
|
+
return processingFlags.get(sessionKey) === true;
|
|
25
|
+
}
|
|
26
|
+
/** Sessions with a turn running or waiting in line for one. */
|
|
27
|
+
export function busySessionCount() {
|
|
28
|
+
let busy = 0;
|
|
29
|
+
for (const key of new Set([...processingFlags.keys(), ...pendingQueues.keys()])) {
|
|
30
|
+
if (processingFlags.get(key) || (pendingQueues.get(key)?.length ?? 0) > 0)
|
|
31
|
+
busy++;
|
|
32
|
+
}
|
|
33
|
+
return busy;
|
|
34
|
+
}
|
|
35
|
+
export async function queueRequest(sessionKey, message, handler) {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
const request = {
|
|
38
|
+
message,
|
|
39
|
+
handler,
|
|
40
|
+
resolve: resolve,
|
|
41
|
+
reject,
|
|
42
|
+
};
|
|
43
|
+
let queue = pendingQueues.get(sessionKey);
|
|
44
|
+
if (!queue) {
|
|
45
|
+
queue = [];
|
|
46
|
+
pendingQueues.set(sessionKey, queue);
|
|
47
|
+
}
|
|
48
|
+
queue.push(request);
|
|
49
|
+
processQueue(sessionKey);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
async function processQueue(sessionKey) {
|
|
53
|
+
if (processingFlags.get(sessionKey)) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const queue = pendingQueues.get(sessionKey);
|
|
57
|
+
if (!queue || queue.length === 0) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
processingFlags.set(sessionKey, true);
|
|
61
|
+
const request = queue.shift();
|
|
62
|
+
// Persist a marker so an unexpected exit during this turn can be surfaced
|
|
63
|
+
// to the user on next startup.
|
|
64
|
+
markInFlight(sessionKey, request.message);
|
|
65
|
+
try {
|
|
66
|
+
const result = await request.handler();
|
|
67
|
+
request.resolve(result);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
request.reject(error instanceof Error ? error : new Error(String(error)));
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
processingFlags.set(sessionKey, false);
|
|
74
|
+
clearAbortController(sessionKey);
|
|
75
|
+
clearCancelled(sessionKey);
|
|
76
|
+
clearInFlight(sessionKey);
|
|
77
|
+
if (queue.length > 0) {
|
|
78
|
+
processQueue(sessionKey);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Soft cancel: interrupt the running turn but keep the session alive. False
|
|
84
|
+
* when no turn is running.
|
|
85
|
+
*/
|
|
86
|
+
export async function cancelRequest(sessionKey) {
|
|
87
|
+
const controller = activeAbortControllers.get(sessionKey);
|
|
88
|
+
if (controller) {
|
|
89
|
+
cancelledChats.add(sessionKey);
|
|
90
|
+
controller.abort();
|
|
91
|
+
clearAbortController(sessionKey);
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
export function clearQueue(sessionKey) {
|
|
97
|
+
const queue = pendingQueues.get(sessionKey);
|
|
98
|
+
if (!queue)
|
|
99
|
+
return 0;
|
|
100
|
+
const count = queue.length;
|
|
101
|
+
for (const request of queue) {
|
|
102
|
+
request.reject(new Error('Queue cleared'));
|
|
103
|
+
}
|
|
104
|
+
queue.length = 0;
|
|
105
|
+
return count;
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=request-queue.js.map
|