mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-12-05 01:29:32 +01:00
A local or Docker Claude session can show its conversation as rendered text, the way the Response Viewer draws it, in place of the terminal. The terminal stays attached and sized underneath, so switching views never resizes the pane, and the user still types in the terminal. Discussion #858 has the background. - A Chat / Terminal switch and Alt+Shift+C change the view, and each session remembers its choice on this device until the session is gone. Two per-device settings pick the default view and hide the switch. Phones, remote and adopted sessions keep the terminal. - The server broadcasts `transcript:update`, at most one per 750 ms per session, and the view re-reads the conversation on it, so an answer fills in mid-turn. - The server keeps each transcript's parse and the byte offset it reached, so a read parses only the bytes appended since the last one, in chunks with an await between them. Overlapping reads of one file share one pass. The Response Viewer and the Discord bridge read through it too. - A full conversation read starts a transcript watcher for a session that has none, such as one picked up after a server restart. - A Docker session's hook reports the container's transcript path, and the server watches the host's copy of that file. - After `/clear`, Claude Code runs the statusline command with the new conversation's id and transcript path. The statusline exporter posts them, and the server follows the new conversation the way it follows a hook's report. - Keep-warm turns are hidden, long prompts fold, a "Working" line shows the turn's running time, and a pending dialog shows a notice that opens the terminal. The design note, docs/chat-view-plan.md, covers this and the four steps that follow it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
89 lines
3.3 KiB
TypeScript
89 lines
3.3 KiB
TypeScript
/**
|
|
* @fileoverview The server path from a transcript watcher's `transcript:update` event to the
|
|
* throttled `transcript:update` SSE broadcast, which the chat view's live updates depend on.
|
|
*
|
|
* Pinned: the first update broadcasts at once and a second one inside the interval broadcasts
|
|
* once at its end (750 ms), stopTranscriptWatcher drops a pending trailing broadcast, and no
|
|
* watcher starts for a session that cleanup is removing.
|
|
*
|
|
* Strategy: construct WebServer on port 0 in test mode (nothing listens until start()) and call
|
|
* its private watcher methods directly.
|
|
*
|
|
* Port: N/A (no server start)
|
|
*/
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { mkdtempSync, writeFileSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { tmpdir } from 'node:os';
|
|
import type { EventEmitter } from 'node:events';
|
|
import { WebServer } from '../src/web/server.js';
|
|
import { SseEvent } from '../src/web/sse-events.js';
|
|
|
|
interface ServerInternals {
|
|
broadcast: (event: string, data: unknown) => void;
|
|
startTranscriptWatcher: (sessionId: string, transcriptPath: string) => void;
|
|
stopTranscriptWatcher: (sessionId: string) => void;
|
|
transcriptWatchers: Map<string, EventEmitter>;
|
|
cleaningUp: Set<string>;
|
|
}
|
|
|
|
describe('transcript:update broadcast', () => {
|
|
const SID = 'transcript-update-session';
|
|
let server: ServerInternals;
|
|
let broadcast: ReturnType<typeof vi.spyOn>;
|
|
let transcriptPath: string;
|
|
|
|
const updates = () => broadcast.mock.calls.filter(([event]) => event === SseEvent.TranscriptUpdate);
|
|
|
|
beforeEach(() => {
|
|
server = new WebServer(0, false, true) as unknown as ServerInternals;
|
|
broadcast = vi.spyOn(server, 'broadcast').mockImplementation(() => {});
|
|
transcriptPath = join(mkdtempSync(join(tmpdir(), 'codeman-transcript-update-')), 'conv.jsonl');
|
|
writeFileSync(transcriptPath, '');
|
|
});
|
|
|
|
afterEach(() => {
|
|
server.stopTranscriptWatcher(SID);
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('broadcasts the first update at once and a second one at the end of the interval', () => {
|
|
server.startTranscriptWatcher(SID, transcriptPath);
|
|
const watcher = server.transcriptWatchers.get(SID);
|
|
expect(watcher).toBeDefined();
|
|
vi.useFakeTimers();
|
|
|
|
watcher!.emit('transcript:update');
|
|
watcher!.emit('transcript:update');
|
|
expect(updates()).toHaveLength(1);
|
|
expect(updates()[0][1]).toMatchObject({ sessionId: SID });
|
|
|
|
vi.advanceTimersByTime(749);
|
|
expect(updates()).toHaveLength(1);
|
|
vi.advanceTimersByTime(1);
|
|
expect(updates()).toHaveLength(2);
|
|
expect(updates()[1][1]).toMatchObject({ sessionId: SID });
|
|
});
|
|
|
|
it('drops a pending trailing broadcast when the watcher stops', () => {
|
|
server.startTranscriptWatcher(SID, transcriptPath);
|
|
const watcher = server.transcriptWatchers.get(SID)!;
|
|
vi.useFakeTimers();
|
|
|
|
watcher.emit('transcript:update');
|
|
watcher.emit('transcript:update');
|
|
server.stopTranscriptWatcher(SID);
|
|
vi.advanceTimersByTime(1000);
|
|
expect(updates()).toHaveLength(1);
|
|
expect(server.transcriptWatchers.has(SID)).toBe(false);
|
|
});
|
|
|
|
it('starts no watcher for a session that cleanup is removing', () => {
|
|
server.cleaningUp.add(SID);
|
|
server.startTranscriptWatcher(SID, transcriptPath);
|
|
expect(server.transcriptWatchers.has(SID)).toBe(false);
|
|
server.cleaningUp.delete(SID);
|
|
});
|
|
});
|