Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions packages/genui/a2ui-playground/src/pages/AIChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import type { StaticDemo } from '../demos.js';
import { useConversation } from '../hooks/useConversation.js';
import type { ModelChatMessage } from '../hooks/useConversation.js';
import { useResizablePanels } from '../hooks/useResizablePanels.js';
import { loadConversation } from '../storage/conversationRepo.js';
import {
loadConversation,
saveConversationSharePayload,
} from '../storage/conversationRepo.js';
import {
isSharedConversationDoc,
serializeConversation,
Expand Down Expand Up @@ -2154,8 +2157,25 @@ export function AIChatPage(
showCopyToast(false);
return;
}
const doc = serializeConversation(record, protocol.name);
const conversationUrl = await publishConversation(doc);
// Reuse the link already published for this conversation while it is
// unchanged, so repeated shares — including after a page reload — copy
// the same link instead of uploading a fresh copy (and minting a new
// URL) each time. `meta.updatedAt` bumps on every turn or rename, and a
// new turn rewrites the snapshot and drops the cached payload.
const cached = record.snapshot?.sharePayload;
let conversationUrl: string | undefined;
if (cached && cached.updatedAt === record.meta.updatedAt) {
conversationUrl = cached.url;
}
if (!conversationUrl) {
const doc = serializeConversation(record, protocol.name);
conversationUrl = await publishConversation(doc);
await saveConversationSharePayload(
id,
conversationUrl,
record.meta.updatedAt,
);
}
const link = buildConversationShareUrl(
conversationUrl,
baseUrl,
Expand Down
21 changes: 21 additions & 0 deletions packages/genui/a2ui-playground/src/storage/conversationRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,27 @@ export async function saveConversationMessages(
await tx.done;
}

/**
* Persist the durable URL of the published share document on the conversation
* snapshot, paired with the `meta.updatedAt` it was generated for. Touches only
* the snapshot's share field (not `meta.updatedAt`), so it does not invalidate
* itself; a later turn rewrites the snapshot and drops it.
*/
export async function saveConversationSharePayload(
conversationId: string,
url: string,
updatedAt: number,
): Promise<void> {
const db = await getDB();
const tx = db.transaction('snapshots', 'readwrite');
const store = tx.objectStore('snapshots');
const snapshot = await store.get(conversationId);
if (snapshot) {
await store.put({ ...snapshot, sharePayload: { url, updatedAt } });
}
await tx.done;
}

export function previewTextFromSharedMessages(
messages: SharedConversationDoc['messages'],
): string {
Expand Down
7 changes: 7 additions & 0 deletions packages/genui/a2ui-playground/src/storage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export interface DataModelSnapshot {
previewMessages?: unknown[];
previewPayloadUrls?: PreviewPayloadUrls;
updatedAt: number;
/**
* Durable URL of the most recently published "share whole conversation"
* document, paired with the `meta.updatedAt` it was generated for. Lets
* repeated shares (including after a page reload) reuse the same link until
* the conversation changes.
*/
sharePayload?: { url: string; updatedAt: number };
}

export interface MetaRecord {
Expand Down
Loading