Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(cowork): hold artifact cards until the run ends

Akarshan Biswas0e753c58ca0c
diff --git a/web-app/src/lib/__tests__/coworkTurns.test.ts b/web-app/src/lib/__tests__/coworkTurns.test.ts
index 6c1b4d8a6..5690e20f9 100644
--- a/web-app/src/lib/__tests__/coworkTurns.test.ts
+++ b/web-app/src/lib/__tests__/coworkTurns.test.ts
@@ -2,6 +2,7 @@ import { describe, it, expect } from 'vitest'
import {
appendLiveMessages,
coworkTurnsToUIMessages,
+ liveTailStart,
userTurn,
} from '@/lib/coworkTurns'
import type { CoworkTurn } from '@/hooks/useCoworkSessions'
@@ -295,3 +296,57 @@ describe('appendLiveMessages', () => {
expect(appendLiveMessages(base, [])).toBe(base)
})
})
+
+describe('liveTailStart', () => {
+ const committed: CoworkTurn[] = [
+ { role: 'user', content: 'q' },
+ { role: 'assistant', content: 'first' },
+ ]
+ const base = coworkTurnsToUIMessages(committed, 's')
+
+ it('is the whole list when nothing is live', () => {
+ expect(liveTailStart(base, [])).toBe(base.length)
+ })
+
+ it('marks the appended live messages when the run opens a new message', () => {
+ // First live row is a user turn, so it is appended, not joined: the tail
+ // starts right after the committed messages.
+ const live = coworkTurnsToUIMessages(
+ [{ role: 'user', content: 'again' }],
+ 's',
+ committed.length
+ )
+ expect(liveTailStart(base, live)).toBe(base.length)
+ })
+
+ it('steps back onto the joined assistant message', () => {
+ // First live row continues the committed assistant, so they merge; that
+ // joined message is mid-run and must be inside the tail.
+ const live = coworkTurnsToUIMessages(
+ [{ role: 'assistant', content: 'continued' }],
+ 's',
+ committed.length
+ )
+ expect(liveTailStart(base, live)).toBe(base.length - 1)
+ // The index it returns is the joined message in the appended list.
+ const out = appendLiveMessages(base, live)
+ expect(out[liveTailStart(base, live)].role).toBe('assistant')
+ })
+
+ it('agrees with the appended length so every live message is covered', () => {
+ const live = coworkTurnsToUIMessages(
+ [
+ { role: 'assistant', content: 'mid' },
+ { role: 'system', content: 'subagent finished' },
+ { role: 'assistant', content: 'after' },
+ ],
+ 's',
+ committed.length
+ )
+ const start = liveTailStart(base, live)
+ const out = appendLiveMessages(base, live)
+ // Everything from `start` on is live; everything before is committed.
+ expect(out.slice(0, start)).toEqual(base.slice(0, start))
+ expect(start).toBeLessThan(out.length)
+ })
+})
diff --git a/web-app/src/lib/coworkTurns.ts b/web-app/src/lib/coworkTurns.ts
index 356e703b1..597f2dd3f 100644
--- a/web-app/src/lib/coworkTurns.ts
+++ b/web-app/src/lib/coworkTurns.ts
@@ -189,3 +189,20 @@ export function appendLiveMessages(
const joined = { ...last, parts: [...last.parts, ...live[0].parts] }
return [...committed.slice(0, -1), joined, ...live.slice(1)]
}
+
+/**
+ * The index into `appendLiveMessages(committed, live)` where the live tail
+ * begins -- the first message that is still mid-run. When the first live
+ * message is joined onto the last committed assistant (the same rule
+ * `appendLiveMessages` uses), that joined message is mid-run too, so the
+ * boundary steps back onto it.
+ */
+export function liveTailStart(
+ committed: UIMessage[],
+ live: UIMessage[]
+): number {
+ if (live.length === 0) return committed.length
+ const last = committed.at(-1)
+ const joined = last?.role === 'assistant' && live[0]?.role === 'assistant'
+ return committed.length - (joined ? 1 : 0)
+}
diff --git a/web-app/src/routes/cowork.tsx b/web-app/src/routes/cowork.tsx
index 945705a45..980f33ef8 100644
--- a/web-app/src/routes/cowork.tsx
+++ b/web-app/src/routes/cowork.tsx
@@ -48,6 +48,7 @@ import SkillSelector from '@/containers/SkillSelector'
import {
appendLiveMessages,
coworkTurnsToUIMessages,
+ liveTailStart,
userTurn,
} from '@/lib/coworkTurns'
import { extractFilesFromPrompt } from '@/lib/fileMetadata'
@@ -378,29 +379,36 @@ function CoworkPage() {
coworkTurnsToUIMessages(session?.turns ?? [], session?.id ?? 'cowork'),
[session?.turns, session?.id]
)
- const uiMessages = useMemo(
+ const liveMessages = useMemo(
() =>
viewingRun
- ? appendLiveMessages(
- committedMessages,
- coworkTurnsToUIMessages(
- liveTurns,
- session?.id ?? 'cowork',
- session?.turns?.length ?? 0
- )
+ ? coworkTurnsToUIMessages(
+ liveTurns,
+ session?.id ?? 'cowork',
+ session?.turns?.length ?? 0
)
+ : [],
+ [viewingRun, liveTurns, session?.id, session?.turns]
+ )
+ const uiMessages = useMemo(
+ () =>
+ viewingRun
+ ? appendLiveMessages(committedMessages, liveMessages)
: committedMessages,
- [viewingRun, committedMessages, liveTurns, session?.id, session?.turns]
+ [viewingRun, committedMessages, liveMessages]
+ )
+ // Where the live tail begins. Every message from here on is mid-run: its
+ // write parts may yet be rewritten, and a <SYSTEM> note (a subagent finishing,
+ // a monitor matching) can split the turn so an earlier fragment is not the
+ // final answer either. Artifact cards wait until the run ends and control is
+ // back with the user; committed turns from before the run keep theirs.
+ const liveStartIdx = useMemo(
+ () =>
+ viewingRun
+ ? liveTailStart(committedMessages, liveMessages)
+ : uiMessages.length,
+ [viewingRun, committedMessages, liveMessages, uiMessages.length]
)
- // While a run streams, the last assistant message is the only one that can
- // still gain parts; its artifact cards wait until the turn finishes.
- const streamingMessageIdx = useMemo(() => {
- if (!viewingRun) return -1
- for (let i = uiMessages.length - 1; i >= 0; i--) {
- if (uiMessages[i].role === 'assistant') return i
- }
- return -1
- }, [viewingRun, uiMessages])
const usage = liveUsage ?? session?.lastUsage ?? null
const tokenSource = useMemo(
@@ -1438,10 +1446,10 @@ function CoworkPage() {
/>
{/* Derived from the message's own write parts, so nothing
shared with the chat surface needs to know artifacts
- exist. Held back on the message still streaming: a card
- mid-run announces a deliverable the agent may yet
- rewrite, and it pushes the live tail around. */}
- {i !== streamingMessageIdx &&
+ exist. Held back for the whole live tail: a card mid-run
+ announces a deliverable the agent may yet rewrite, and it
+ pushes the live tail around. */}
+ {i < liveStartIdx &&
artifactsFromParts(message.parts).map((artifact) => (
<CoworkArtifactCard
key={artifact.path}