web: chat — session list, open/close, history paging, markdown, scroll (C1/C3/C4/C7/C14)
Browser Chat destination for parity with the Android app: - session list grouped by directory (closed grouped under "Closed"), kind chip, activity badge, work-label meta line, "Search chats" filter (C1) - open/close/reopen with the Android copy verbatim (C3) - conversation history paging via the opaque next_cursor (C4) - markdown subset renderer + code blocks (C7) - scroll follow / "Jump to latest" / per-session saved position (C14) Request builders in src/repository.js; pure merge/format/markdown/paging logic in src/chat.js and src/markdown.js; view in app.js. Every server string set with textContent, markdown built as DOM nodes.
This commit is contained in:
+67
-1
@@ -1,9 +1,12 @@
|
||||
// Typed access to the existing WebSocket protocol. Request builders and id
|
||||
// matching live here so the app never constructs frames by hand or invents
|
||||
// divergent field names. Mirrors android/.../data/PipelinesRepository.kt.
|
||||
import { Protocol } from "./protocol.js";
|
||||
import { ChatKeys, Protocol } from "./protocol.js";
|
||||
import { ProtocolError } from "./client.js";
|
||||
|
||||
/** The server default history page the app asks for (CHAT_HISTORY_LIMIT, 200). */
|
||||
export const CHAT_HISTORY_LIMIT = 200;
|
||||
|
||||
export class PipelinesRepository {
|
||||
#client;
|
||||
|
||||
@@ -153,4 +156,67 @@ export class PipelinesRepository {
|
||||
}
|
||||
return response.run_detail;
|
||||
}
|
||||
|
||||
// ---- Brokered OpenCode chat (C1/C3/C4) -----------------------------------
|
||||
// The browser never speaks OpenCode directly and never holds its credentials;
|
||||
// it talks only to the Pipelines server. Mirrors PipelinesRepository.chat*.
|
||||
|
||||
/**
|
||||
* Lists the sessions this server owns. The server already returns operator
|
||||
* sessions only, so the app never filters the list by ownership.
|
||||
*/
|
||||
async chatSessions() {
|
||||
return (await this.#client.call({ type: Protocol.CHAT_SESSIONS }))[ChatKeys.SESSIONS] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a bounded chronological history page. `cursor` is the opaque
|
||||
* continuation the server returned as `next_cursor`; it is passed back
|
||||
* verbatim and never interpreted. The reply is `chat_history`
|
||||
* (`{messages,next_cursor,has_more}`); an older server that only knows
|
||||
* `chat_messages` exposes no continuation, so the page is that list with
|
||||
* `hasMore:false` and an empty cursor — exactly like
|
||||
* PipelinesRepository.chatMessages.
|
||||
*/
|
||||
async chatHistory(sessionId, { limit = CHAT_HISTORY_LIMIT, cursor = "" } = {}) {
|
||||
if (!String(sessionId ?? "").trim()) {
|
||||
throw new ProtocolError("Select a chat session first.");
|
||||
}
|
||||
const response = await this.#client.call({
|
||||
type: Protocol.CHAT_MESSAGES,
|
||||
[ChatKeys.QUERY]: { session_id: sessionId, limit, cursor },
|
||||
});
|
||||
const page = response[ChatKeys.HISTORY];
|
||||
if (page) {
|
||||
return {
|
||||
messages: page.messages ?? [],
|
||||
nextCursor: page.next_cursor ?? "",
|
||||
hasMore: Boolean(page.has_more),
|
||||
};
|
||||
}
|
||||
return { messages: response[ChatKeys.MESSAGES] ?? [], nextCursor: "", hasMore: false };
|
||||
}
|
||||
|
||||
/**
|
||||
* Finishes a session and records it closed on the server (nothing is
|
||||
* deleted). The reply echoes the updated session (`chat_session`) and the
|
||||
* close outcome (`chat_close`: the id, the close time and whether an
|
||||
* in-flight turn was interrupted).
|
||||
*/
|
||||
async closeChat(sessionId) {
|
||||
const response = await this.#client.call({
|
||||
type: Protocol.CHAT_CLOSE,
|
||||
[ChatKeys.QUERY]: { session_id: sessionId },
|
||||
});
|
||||
return { session: response[ChatKeys.SESSION] ?? null, close: response[ChatKeys.CLOSE] ?? null };
|
||||
}
|
||||
|
||||
/** Clears the closed state again, so a finished chat can be resumed. */
|
||||
async reopenChat(sessionId) {
|
||||
const response = await this.#client.call({
|
||||
type: Protocol.CHAT_REOPEN,
|
||||
[ChatKeys.QUERY]: { session_id: sessionId },
|
||||
});
|
||||
return { session: response[ChatKeys.SESSION] ?? null, close: response[ChatKeys.CLOSE] ?? null };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user