test: cover TUI optimistic prompt sync
This commit is contained in:
@@ -0,0 +1,50 @@
|
|||||||
|
import type { AgentPartInput, FilePartInput, Message, Part, SubtaskPartInput, TextPartInput } from "@opencode-ai/sdk/v2"
|
||||||
|
import { Binary } from "@opencode-ai/core/util/binary"
|
||||||
|
|
||||||
|
export type OptimisticPromptPart = (TextPartInput | FilePartInput | AgentPartInput | SubtaskPartInput) & { id: string }
|
||||||
|
|
||||||
|
export function optimisticParts(input: { sessionID: string; messageID: string; parts: OptimisticPromptPart[] }) {
|
||||||
|
return input.parts.map((part): Part => {
|
||||||
|
const withIDs = {
|
||||||
|
...part,
|
||||||
|
sessionID: input.sessionID,
|
||||||
|
messageID: input.messageID,
|
||||||
|
}
|
||||||
|
if (withIDs.type === "file") return { ...withIDs, url: "" }
|
||||||
|
return withIDs
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export function mergeFetchedMessages(input: {
|
||||||
|
currentMessages: Message[]
|
||||||
|
currentParts: Record<string, Part[] | undefined>
|
||||||
|
fetched: { info: Message; parts: Part[] }[]
|
||||||
|
optimisticMessages: ReadonlySet<string>
|
||||||
|
}) {
|
||||||
|
const fetchedIDs = new Set(input.fetched.map((message) => message.info.id))
|
||||||
|
const messages = input.fetched.map((message) => message.info)
|
||||||
|
const parts = new Map<string, Part[]>()
|
||||||
|
const resolved = new Set<string>()
|
||||||
|
|
||||||
|
for (const message of input.currentMessages) {
|
||||||
|
if (input.optimisticMessages.has(message.id) && !fetchedIDs.has(message.id)) {
|
||||||
|
Binary.insert(messages, message, (item) => item.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const message of input.fetched) {
|
||||||
|
if (message.parts.length > 0) {
|
||||||
|
resolved.add(message.info.id)
|
||||||
|
parts.set(message.info.id, message.parts)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (input.optimisticMessages.has(message.info.id)) {
|
||||||
|
const current = input.currentParts[message.info.id]
|
||||||
|
if (current) parts.set(message.info.id, current)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
parts.set(message.info.id, message.parts)
|
||||||
|
}
|
||||||
|
|
||||||
|
return { messages, parts, resolved }
|
||||||
|
}
|
||||||
@@ -4,10 +4,6 @@ import type {
|
|||||||
Provider,
|
Provider,
|
||||||
Session,
|
Session,
|
||||||
Part,
|
Part,
|
||||||
TextPartInput,
|
|
||||||
FilePartInput,
|
|
||||||
AgentPartInput,
|
|
||||||
SubtaskPartInput,
|
|
||||||
Config,
|
Config,
|
||||||
Todo,
|
Todo,
|
||||||
Command,
|
Command,
|
||||||
@@ -37,8 +33,7 @@ import { emptyConsoleState, type ConsoleState } from "@/config/console-state"
|
|||||||
import path from "path"
|
import path from "path"
|
||||||
import { useKV } from "./kv"
|
import { useKV } from "./kv"
|
||||||
import { aggregateFailures } from "./aggregate-failures"
|
import { aggregateFailures } from "./aggregate-failures"
|
||||||
|
import { mergeFetchedMessages, optimisticParts, type OptimisticPromptPart } from "./sync-optimistic"
|
||||||
type OptimisticPromptPart = (TextPartInput | FilePartInput | AgentPartInput | SubtaskPartInput) & { id: string }
|
|
||||||
|
|
||||||
export const { use: useSync, provider: SyncProvider } = createSimpleContext({
|
export const { use: useSync, provider: SyncProvider } = createSimpleContext({
|
||||||
name: "Sync",
|
name: "Sync",
|
||||||
@@ -226,6 +221,7 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
|
|||||||
break
|
break
|
||||||
|
|
||||||
case "session.deleted": {
|
case "session.deleted": {
|
||||||
|
for (const message of store.message[event.properties.info.id] ?? []) optimisticMessages.delete(message.id)
|
||||||
const result = Binary.search(store.session, event.properties.info.id, (s) => s.id)
|
const result = Binary.search(store.session, event.properties.info.id, (s) => s.id)
|
||||||
if (result.found) {
|
if (result.found) {
|
||||||
setStore(
|
setStore(
|
||||||
@@ -258,7 +254,6 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "message.updated": {
|
case "message.updated": {
|
||||||
optimisticMessages.delete(event.properties.info.id)
|
|
||||||
const messages = store.message[event.properties.info.sessionID]
|
const messages = store.message[event.properties.info.sessionID]
|
||||||
if (!messages) {
|
if (!messages) {
|
||||||
setStore("message", event.properties.info.sessionID, [event.properties.info])
|
setStore("message", event.properties.info.sessionID, [event.properties.info])
|
||||||
@@ -313,6 +308,7 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
case "message.part.updated": {
|
case "message.part.updated": {
|
||||||
|
optimisticMessages.delete(event.properties.part.messageID)
|
||||||
const parts = store.part[event.properties.part.messageID]
|
const parts = store.part[event.properties.part.messageID]
|
||||||
if (!parts) {
|
if (!parts) {
|
||||||
setStore("part", event.properties.part.messageID, [event.properties.part])
|
setStore("part", event.properties.part.messageID, [event.properties.part])
|
||||||
@@ -550,21 +546,6 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
|
|||||||
...(input.variant ? { variant: input.variant } : {}),
|
...(input.variant ? { variant: input.variant } : {}),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
const parts = input.parts.map((part): Part => {
|
|
||||||
const withIDs = {
|
|
||||||
...part,
|
|
||||||
sessionID: input.sessionID,
|
|
||||||
messageID: input.messageID,
|
|
||||||
}
|
|
||||||
if (withIDs.type === "file") {
|
|
||||||
return {
|
|
||||||
...withIDs,
|
|
||||||
url: "",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return withIDs
|
|
||||||
})
|
|
||||||
|
|
||||||
batch(() => {
|
batch(() => {
|
||||||
if (!messages) {
|
if (!messages) {
|
||||||
setStore("message", input.sessionID, [info])
|
setStore("message", input.sessionID, [info])
|
||||||
@@ -577,7 +558,7 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
setStore("part", input.messageID, reconcile(parts))
|
setStore("part", input.messageID, reconcile(optimisticParts(input)))
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
removeOptimisticPrompt(sessionID: string, messageID: string) {
|
removeOptimisticPrompt(sessionID: string, messageID: string) {
|
||||||
@@ -613,23 +594,22 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
|
|||||||
setStore(
|
setStore(
|
||||||
produce((draft) => {
|
produce((draft) => {
|
||||||
const match = Binary.search(draft.session, sessionID, (s) => s.id)
|
const match = Binary.search(draft.session, sessionID, (s) => s.id)
|
||||||
const fetched = messages.data!
|
const merged = mergeFetchedMessages({
|
||||||
const fetchedIDs = new Set(fetched.map((message) => message.info.id))
|
currentMessages: draft.message[sessionID] ?? [],
|
||||||
const optimistic = (draft.message[sessionID] ?? []).filter(
|
currentParts: draft.part,
|
||||||
(message) => optimisticMessages.has(message.id) && !fetchedIDs.has(message.id),
|
fetched: messages.data ?? [],
|
||||||
)
|
optimisticMessages,
|
||||||
|
})
|
||||||
if (match.found) draft.session[match.index] = session.data!
|
if (match.found) draft.session[match.index] = session.data!
|
||||||
if (!match.found) draft.session.splice(match.index, 0, session.data!)
|
if (!match.found) draft.session.splice(match.index, 0, session.data!)
|
||||||
draft.todo[sessionID] = todo.data ?? []
|
draft.todo[sessionID] = todo.data ?? []
|
||||||
const infos: (typeof draft.message)[string] = fetched.map((message) => message.info)
|
draft.message[sessionID] = merged.messages
|
||||||
for (const message of optimistic) {
|
for (const messageID of merged.resolved) {
|
||||||
Binary.insert(infos, message, (item) => item.id)
|
optimisticMessages.delete(messageID)
|
||||||
}
|
}
|
||||||
for (const message of fetched) {
|
for (const [messageID, parts] of merged.parts) {
|
||||||
optimisticMessages.delete(message.info.id)
|
draft.part[messageID] = parts
|
||||||
draft.part[message.info.id] = message.parts
|
|
||||||
}
|
}
|
||||||
draft.message[sessionID] = infos
|
|
||||||
draft.session_diff[sessionID] = diff.data ?? []
|
draft.session_diff[sessionID] = diff.data ?? []
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
import { describe, expect, test } from "bun:test"
|
||||||
|
import type { Message, Part } from "@opencode-ai/sdk/v2"
|
||||||
|
import { mergeFetchedMessages, optimisticParts } from "@/cli/cmd/tui/context/sync-optimistic"
|
||||||
|
|
||||||
|
function user(id: string): Message {
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
sessionID: "ses_test",
|
||||||
|
role: "user",
|
||||||
|
time: { created: 1 },
|
||||||
|
agent: "build",
|
||||||
|
model: { providerID: "test", modelID: "model" },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function text(messageID: string, text: string): Part {
|
||||||
|
return {
|
||||||
|
id: `part_${messageID}`,
|
||||||
|
sessionID: "ses_test",
|
||||||
|
messageID,
|
||||||
|
type: "text",
|
||||||
|
text,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("TUI optimistic prompt sync", () => {
|
||||||
|
test("keeps an optimistic message while session sync has not fetched it yet", () => {
|
||||||
|
const merged = mergeFetchedMessages({
|
||||||
|
currentMessages: [user("msg_2")],
|
||||||
|
currentParts: { msg_2: [text("msg_2", "optimistic")] },
|
||||||
|
fetched: [{ info: user("msg_1"), parts: [text("msg_1", "persisted")] }],
|
||||||
|
optimisticMessages: new Set(["msg_2"]),
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(merged.messages.map((message) => message.id)).toEqual(["msg_1", "msg_2"])
|
||||||
|
expect(merged.parts.get("msg_1")?.map((part) => (part.type === "text" ? part.text : ""))).toEqual(["persisted"])
|
||||||
|
expect(merged.resolved.has("msg_2")).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("preserves optimistic parts when sync fetches the message before its parts", () => {
|
||||||
|
const merged = mergeFetchedMessages({
|
||||||
|
currentMessages: [user("msg_1")],
|
||||||
|
currentParts: { msg_1: [text("msg_1", "optimistic")] },
|
||||||
|
fetched: [{ info: user("msg_1"), parts: [] }],
|
||||||
|
optimisticMessages: new Set(["msg_1"]),
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(merged.messages.map((message) => message.id)).toEqual(["msg_1"])
|
||||||
|
expect(merged.parts.get("msg_1")?.map((part) => (part.type === "text" ? part.text : ""))).toEqual(["optimistic"])
|
||||||
|
expect(merged.resolved.has("msg_1")).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("replaces optimistic parts once real fetched parts arrive", () => {
|
||||||
|
const merged = mergeFetchedMessages({
|
||||||
|
currentMessages: [user("msg_1")],
|
||||||
|
currentParts: { msg_1: [text("msg_1", "optimistic")] },
|
||||||
|
fetched: [{ info: user("msg_1"), parts: [text("msg_1", "persisted")] }],
|
||||||
|
optimisticMessages: new Set(["msg_1"]),
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(merged.parts.get("msg_1")?.map((part) => (part.type === "text" ? part.text : ""))).toEqual(["persisted"])
|
||||||
|
expect(merged.resolved.has("msg_1")).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("strips file URLs from optimistic render parts", () => {
|
||||||
|
const parts = optimisticParts({
|
||||||
|
sessionID: "ses_test",
|
||||||
|
messageID: "msg_1",
|
||||||
|
parts: [
|
||||||
|
{
|
||||||
|
id: "part_file",
|
||||||
|
type: "file",
|
||||||
|
mime: "image/png",
|
||||||
|
filename: "image.png",
|
||||||
|
url: "data:image/png;base64,large",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(parts).toEqual([
|
||||||
|
{
|
||||||
|
id: "part_file",
|
||||||
|
sessionID: "ses_test",
|
||||||
|
messageID: "msg_1",
|
||||||
|
type: "file",
|
||||||
|
mime: "image/png",
|
||||||
|
filename: "image.png",
|
||||||
|
url: "",
|
||||||
|
},
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user