feat: establish typed runtime protocol model

This commit is contained in:
2026-08-03 12:58:01 +08:00
parent 73b5194cee
commit d2646f534e
5 changed files with 325 additions and 45 deletions
+24 -3
View File
@@ -1,7 +1,13 @@
import DOMPurify from "dompurify";
import { marked } from "marked";
import "./style.css";
import { decodeConversationItem, type ConversationItem, type Envelope } from "./protocol";
import {
decodeConversationItem,
type Actor,
type ConversationItem,
type Envelope,
type JsonObject,
} from "./protocol";
type Session = {
api: string;
@@ -232,6 +238,21 @@ $<HTMLFormElement>("#message-form").addEventListener("submit", async event => {
$<HTMLButtonElement>("#logout").addEventListener("click", () => { session.active = false; messages.replaceChildren(); chatView.hidden = true; loginView.hidden = false; });
export function makeProtocolEvent(type: string, payload: Record<string, unknown>, conversationID: string): Envelope {
return { v: 1, id: `msg_${crypto.randomUUID()}`, type, conversation_id: conversationID, payload };
export function makeProtocolEvent(
type: string,
payload: JsonObject,
conversationID: string,
sender: Actor,
target?: Actor,
): Envelope {
return {
v: 1,
id: `msg_${crypto.randomUUID()}`,
type,
conversation_id: conversationID,
sender,
...(target ? { target } : {}),
timestamp: new Date().toISOString(),
payload,
};
}