feat: establish typed runtime protocol model
This commit is contained in:
+24
-3
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user