-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(core,webapp): version 2 transcript snapshot and a dual-version dashboard reader #4892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ericallam
wants to merge
1
commit into
feat/chat-bound-streamtext
from
feat/transcript-snapshot-v2-tri-13667
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
apps/webapp/app/components/runs/v3/agent/transcriptSnapshotSeed.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { seedFromTranscriptSnapshot } from "./transcriptSnapshotSeed"; | ||
|
|
||
| const user = { id: "u-1", role: "user", parts: [{ type: "text", text: "hello" }] }; | ||
| const assistant = { id: "a-1", role: "assistant", parts: [{ type: "text", text: "world" }] }; | ||
|
|
||
| describe("seedFromTranscriptSnapshot", () => { | ||
| it("seeds from a version 1 snapshot in array order", () => { | ||
| const seed = seedFromTranscriptSnapshot({ | ||
| version: 1, | ||
| savedAt: 1_000, | ||
| messages: [user, assistant], | ||
| lastOutEventId: "42", | ||
| lastInEventId: "7", | ||
| }); | ||
|
|
||
| expect(seed).toBeDefined(); | ||
| expect(seed!.lastOutEventId).toBe("42"); | ||
| expect(seed!.messages.map((m) => m.id)).toEqual(["u-1", "a-1"]); | ||
| expect(seed!.messages.map((m) => m.timestamp)).toEqual([998, 999]); | ||
| expect(seed!.messages[1]!.message).toEqual(assistant); | ||
| }); | ||
|
|
||
| it("seeds from a version 2 snapshot, unwrapping the message envelope", () => { | ||
| const seed = seedFromTranscriptSnapshot({ | ||
| version: 2, | ||
| savedAt: 1_000, | ||
| messages: [ | ||
| { id: "u-1", final: true, message: user }, | ||
| { id: "a-1", final: false, message: assistant }, | ||
| ], | ||
| state: { summary: "irrelevant to rendering" }, | ||
| lastOutEventId: "42", | ||
| lastInEventId: "7", | ||
| }); | ||
|
|
||
| expect(seed).toBeDefined(); | ||
| expect(seed!.lastOutEventId).toBe("42"); | ||
| expect(seed!.messages.map((m) => m.id)).toEqual(["u-1", "a-1"]); | ||
| expect(seed!.messages.map((m) => m.timestamp)).toEqual([998, 999]); | ||
| expect(seed!.messages[1]!.message).toEqual(assistant); | ||
| }); | ||
|
|
||
| it("skips version 1 entries without an id but keeps the others' positions", () => { | ||
| const seed = seedFromTranscriptSnapshot({ | ||
| version: 1, | ||
| savedAt: 1_000, | ||
| messages: [{ role: "user", parts: [] }, assistant], | ||
| }); | ||
|
|
||
| expect(seed!.messages.map((m) => m.id)).toEqual(["a-1"]); | ||
| expect(seed!.messages[0]!.timestamp).toBe(999); | ||
| expect(seed!.lastOutEventId).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("returns undefined for an unknown version or a non-snapshot body", () => { | ||
| expect(seedFromTranscriptSnapshot({ version: 3, savedAt: 1, messages: [] })).toBeUndefined(); | ||
| expect(seedFromTranscriptSnapshot({ error: "not found" })).toBeUndefined(); | ||
| expect(seedFromTranscriptSnapshot(null)).toBeUndefined(); | ||
| expect(seedFromTranscriptSnapshot("[]")).toBeUndefined(); | ||
| }); | ||
| }); |
32 changes: 32 additions & 0 deletions
32
apps/webapp/app/components/runs/v3/agent/transcriptSnapshotSeed.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import type { UIMessage } from "@ai-sdk/react"; | ||
| import { parseTranscriptSnapshot } from "@trigger.dev/core/v3"; | ||
|
|
||
| export type TranscriptSnapshotSeed = { | ||
| messages: Array<{ id: string; message: UIMessage; timestamp: number }>; | ||
| lastOutEventId: string | undefined; | ||
| }; | ||
|
|
||
| /** | ||
| * Turn a fetched chat-snapshot blob into the messages the AgentView seeds | ||
| * before it opens the `.out` subscription. | ||
| * | ||
| * Each message gets a unique, monotonically increasing timestamp from | ||
| * `(savedAt - count + index)`. Live chunk timestamps are S2 arrival | ||
| * milliseconds in the present, so anything below `savedAt` sorts before | ||
| * live chunks while preserving the snapshot's own order. | ||
| * | ||
| * Reads both snapshot versions through `parseTranscriptSnapshot`. Returns | ||
| * `undefined` for anything that is not a snapshot this reader understands; | ||
| * the caller then falls back to the seq=0 SSE. | ||
| */ | ||
| export function seedFromTranscriptSnapshot(json: unknown): TranscriptSnapshotSeed | undefined { | ||
| const snapshot = parseTranscriptSnapshot<UIMessage>(json); | ||
| if (!snapshot) return undefined; | ||
| const count = snapshot.messages.length; | ||
| const messages = snapshot.messages.map((entry, i) => ({ | ||
| id: entry.id, | ||
| message: entry.message, | ||
| timestamp: snapshot.savedAt - count + i, | ||
| })); | ||
| return { messages, lastOutEventId: snapshot.lastOutEventId }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| parseTranscriptSnapshot, | ||
| type ChatSnapshotV1, | ||
| type TranscriptSnapshotV2, | ||
| } from "../src/v3/sessionStreams/chatSnapshot.js"; | ||
|
|
||
| const user = { id: "u-1", role: "user" as const, parts: [{ type: "text" as const, text: "hi" }] }; | ||
| const assistant = { | ||
| id: "a-1", | ||
| role: "assistant" as const, | ||
| parts: [{ type: "text" as const, text: "hello" }], | ||
| }; | ||
|
|
||
| describe("parseTranscriptSnapshot", () => { | ||
| it("returns a version 2 blob unchanged", () => { | ||
| const blob: TranscriptSnapshotV2 = { | ||
| version: 2, | ||
| savedAt: 10, | ||
| messages: [ | ||
| { id: "u-1", final: true, message: user }, | ||
| { id: "a-1", final: false, message: assistant }, | ||
| ], | ||
| state: { summary: "s", through: "u-1" }, | ||
| lastOutEventId: "9", | ||
| lastInEventId: "3", | ||
| }; | ||
|
|
||
| expect(parseTranscriptSnapshot(blob)).toEqual(blob); | ||
| }); | ||
|
|
||
| it("normalises a version 2 blob with no state to state: null", () => { | ||
| const parsed = parseTranscriptSnapshot({ | ||
| version: 2, | ||
| savedAt: 10, | ||
| messages: [], | ||
| state: undefined, | ||
| }); | ||
|
|
||
| expect(parsed?.state).toBeNull(); | ||
| }); | ||
|
|
||
| it("upgrades a version 1 blob: every message final, state null, cursors kept", () => { | ||
| const blob: ChatSnapshotV1 = { | ||
| version: 1, | ||
| savedAt: 10, | ||
| messages: [user, assistant], | ||
| lastOutEventId: "9", | ||
| lastInEventId: "3", | ||
| }; | ||
|
|
||
| expect(parseTranscriptSnapshot(blob)).toEqual({ | ||
| version: 2, | ||
| savedAt: 10, | ||
| messages: [ | ||
| { id: "u-1", final: true, message: user }, | ||
| { id: "a-1", final: true, message: assistant }, | ||
| ], | ||
| state: null, | ||
| lastOutEventId: "9", | ||
| lastInEventId: "3", | ||
| }); | ||
| }); | ||
|
|
||
| it("drops version 1 messages that have no string id", () => { | ||
| const parsed = parseTranscriptSnapshot({ | ||
| version: 1, | ||
| savedAt: 10, | ||
| messages: [{ role: "user", parts: [] }, { id: 7, role: "user", parts: [] }, assistant, null], | ||
| }); | ||
|
|
||
| expect(parsed?.messages.map((m) => m.id)).toEqual(["a-1"]); | ||
| }); | ||
|
|
||
| it("returns undefined for unknown versions and non-snapshot bodies", () => { | ||
| expect(parseTranscriptSnapshot({ version: 3, savedAt: 1, messages: [] })).toBeUndefined(); | ||
| expect( | ||
| parseTranscriptSnapshot({ version: 2, savedAt: 1, messages: [{ id: "x" }] }) | ||
| ).toBeUndefined(); | ||
| expect(parseTranscriptSnapshot({ version: 1, savedAt: "1", messages: [] })).toBeUndefined(); | ||
| expect(parseTranscriptSnapshot({ message: "Not Found" })).toBeUndefined(); | ||
| expect(parseTranscriptSnapshot(undefined)).toBeUndefined(); | ||
| expect(parseTranscriptSnapshot([])).toBeUndefined(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.