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.

feat(web-app): show migrated Cowork pings as system notes, keep them in history

Akarshan Biswas29a42466fe1b
diff --git a/web-app/src/containers/MessageItem.test.tsx b/web-app/src/containers/MessageItem.test.tsx
index 99b3e7bd2..dbf919a2e 100644
--- a/web-app/src/containers/MessageItem.test.tsx
+++ b/web-app/src/containers/MessageItem.test.tsx
@@ -72,6 +72,73 @@ describe('what a turn shows besides its messages', () => {
})
})
+describe('a migrated Cowork ping', () => {
+ const PING = '<SYSTEM>\nSubagent researcher (c1) finished.\n</SYSTEM>'
+ // Stored as a user message (the model saw a user turn) and flagged, so the
+ // history replays unchanged while the row shows as a system note.
+ const ping = {
+ id: 'p1',
+ role: 'user',
+ parts: [{ type: 'text', text: PING }],
+ metadata: { systemNotice: true },
+ } as UIMessage
+
+ it('renders through the system-note template, not as a raw <SYSTEM> user bubble', () => {
+ render(
+ <MessageItem
+ message={ping}
+ isFirstMessage={false}
+ isLastMessage={false}
+ status="ready"
+ />
+ )
+ expect(screen.getByText(/Subagent researcher \(c1\) finished\./)).toBeInTheDocument()
+ expect(screen.queryByText(/<SYSTEM>/)).not.toBeInTheDocument()
+ expect(screen.queryByTestId('message-item')).not.toBeInTheDocument()
+ expect(screen.queryByRole('button')).not.toBeInTheDocument()
+ })
+
+ it('looks the same as a native system note', () => {
+ const native = render(
+ <MessageItem
+ message={{ ...ping, id: 'n1', role: 'system', metadata: undefined } as UIMessage}
+ isFirstMessage={false}
+ isLastMessage={false}
+ status="ready"
+ />
+ )
+ const nativeHtml = native.container.innerHTML
+ native.unmount()
+ const flagged = render(
+ <MessageItem
+ message={ping}
+ isFirstMessage={false}
+ isLastMessage={false}
+ status="ready"
+ />
+ )
+ expect(flagged.container.innerHTML).toBe(nativeHtml)
+ })
+
+ it('an ordinary user message that mentions the tag is still a user bubble', () => {
+ render(
+ <MessageItem
+ message={
+ {
+ id: 'u1',
+ role: 'user',
+ parts: [{ type: 'text', text: 'what does <SYSTEM> mean?' }],
+ } as UIMessage
+ }
+ isFirstMessage={false}
+ isLastMessage={false}
+ status="ready"
+ />
+ )
+ expect(screen.getByTestId('message-item')).toBeInTheDocument()
+ })
+})
+
describe('extension message actions', () => {
const reply = {
id: 'm2',
diff --git a/web-app/src/containers/MessageItem.tsx b/web-app/src/containers/MessageItem.tsx
index 7ffece93a..22fbd811f 100644
--- a/web-app/src/containers/MessageItem.tsx
+++ b/web-app/src/containers/MessageItem.tsx
@@ -39,7 +39,7 @@ import {
IconChevronRight,
IconBellRinging,
} from '@tabler/icons-react'
-import { PING_OPEN, PING_CLOSE } from '@/lib/coworkPing'
+import { PING_OPEN, PING_CLOSE, isSystemNotice } from '@/lib/coworkPing'
import { EditMessageDialog } from '@/containers/dialogs/EditMessageDialog'
import { DeleteMessageDialog } from '@/containers/dialogs/DeleteMessageDialog'
import TokenSpeedIndicator from '@/containers/TokenSpeedIndicator'
@@ -645,7 +645,9 @@ export const MessageItem = memo(
// said: no actions, no trace, no timestamp. It is on screen because it is
// what the next turn is replying to -- a turn that reacts to a subagent
// finishing reads as a non sequitur without it.
- if (message.role === 'system') {
+ // A migrated Cowork ping is stored as the user turn the model saw and
+ // flagged (see SYSTEM_NOTICE_KEY), so it takes the same template.
+ if (message.role === 'system' || isSystemNotice(message)) {
const note = getFullTextContent()
.split(PING_OPEN)
.join('')
diff --git a/web-app/src/lib/__tests__/coworkThreadMessages.test.ts b/web-app/src/lib/__tests__/coworkThreadMessages.test.ts
index 5ee552396..5f3f3b22d 100644
--- a/web-app/src/lib/__tests__/coworkThreadMessages.test.ts
+++ b/web-app/src/lib/__tests__/coworkThreadMessages.test.ts
@@ -5,6 +5,7 @@ import type { CoworkSession } from '@/hooks/useCoworkSessions'
import { convertThreadMessageToUIMessage } from '@/lib/messages'
import { STOPPED_TOOL_ERROR } from '@/lib/stopped-tool-calls'
import { coworkSessionToThreadMessages } from '../coworkThreadMessages'
+import { SYSTEM_NOTICE_KEY, isSystemNotice } from '../coworkPing'
const UPDATED_MS = 1_700_000_000_000
@@ -278,4 +279,61 @@ describe('coworkSessionToThreadMessages', () => {
'only text',
])
})
+
+ describe('<SYSTEM> pings', () => {
+ const PING = '<SYSTEM>\nSubagent researcher (c1) finished.\n</SYSTEM>'
+ const withPing = () =>
+ session([
+ msg('u-1', 'user', [{ type: 'text', text: 'research it' }]),
+ msg('a-1', 'assistant', [{ type: 'text', text: 'Started.' }]),
+ msg('ping-2', 'user', [{ type: 'text', text: PING }]),
+ msg('a-2', 'assistant', [{ type: 'text', text: 'It finished.' }]),
+ ])
+
+ it('keeps the ping in the history, as a user message with its text byte-identical', () => {
+ const out = coworkSessionToThreadMessages(withPing())
+ expect(out).toHaveLength(4)
+ const ping = out[2]
+ // Still what the model saw: a user turn carrying the exact same text, so
+ // the replayed prompt prefix is unchanged.
+ expect(ping.role).toBe('user')
+ expect(ping.content).toEqual([
+ { type: ContentType.Text, text: { value: PING, annotations: [] } },
+ ])
+ })
+
+ it('flags the ping as a system notice and nothing else', () => {
+ const out = coworkSessionToThreadMessages(withPing())
+ expect(out[2].metadata).toEqual({ [SYSTEM_NOTICE_KEY]: true })
+ for (const i of [0, 1, 3]) {
+ expect(out[i].metadata?.[SYSTEM_NOTICE_KEY]).toBeUndefined()
+ }
+ })
+
+ it('is deterministic: the same session gives the same bytes', () => {
+ const a = JSON.stringify(coworkSessionToThreadMessages(withPing()))
+ const b = JSON.stringify(coworkSessionToThreadMessages(withPing()))
+ expect(a).toBe(b)
+ })
+
+ it('does not flag a message a user typed, even one that mentions the tag later', () => {
+ const s = session([
+ msg('u-1', 'user', [{ type: 'text', text: 'what does <SYSTEM> mean?' }]),
+ msg('u-2', 'user', [
+ { type: 'text', text: 'hi' },
+ { type: 'text', text: PING },
+ ]),
+ ])
+ const out = coworkSessionToThreadMessages(s)
+ expect(out.map((m) => isSystemNotice(m))).toEqual([false, false])
+ })
+
+ it('survives the reload main chat does: the UI message still carries the flag', () => {
+ const [, , ping] = coworkSessionToThreadMessages(withPing())
+ const ui = convertThreadMessageToUIMessage(ping)
+ expect(ui.role).toBe('user')
+ expect(isSystemNotice(ui)).toBe(true)
+ expect(ui.parts).toEqual([{ type: 'text', text: PING }])
+ })
+ })
})
diff --git a/web-app/src/lib/coworkPing.ts b/web-app/src/lib/coworkPing.ts
index b6ed5c441..4fa45149a 100644
--- a/web-app/src/lib/coworkPing.ts
+++ b/web-app/src/lib/coworkPing.ts
@@ -22,6 +22,26 @@ import type { UIMessage } from 'ai'
export const PING_OPEN = '<SYSTEM>'
export const PING_CLOSE = '</SYSTEM>'
+/**
+ * Metadata flag on a stored message that is a ping and should be shown as a
+ * system note. The Cowork-to-threads migration sets it: the message stays a
+ * `user` message with its text untouched, because that is what the model was
+ * sent. A real `role: 'system'` row in the middle of a replayed history is
+ * turned into a system turn by `convertToModelMessages`, which some providers
+ * reject, and it would change the prompt prefix.
+ */
+export const SYSTEM_NOTICE_KEY = 'systemNotice'
+
+/** True when a (stored or UI) message is flagged to render as a system note. */
+export function isSystemNotice(message: { metadata?: unknown }): boolean {
+ const meta = message.metadata
+ return (
+ typeof meta === 'object' &&
+ meta !== null &&
+ (meta as Record<string, unknown>)[SYSTEM_NOTICE_KEY] === true
+ )
+}
+
/** True when a message carries pings and nothing the user typed. */
export function isPingOnly(message: { role: string; parts?: unknown }): boolean {
if (message.role !== 'user') return false
diff --git a/web-app/src/lib/coworkThreadMessages.ts b/web-app/src/lib/coworkThreadMessages.ts
index 561e68cac..f4587b024 100644
--- a/web-app/src/lib/coworkThreadMessages.ts
+++ b/web-app/src/lib/coworkThreadMessages.ts
@@ -3,6 +3,7 @@ import type { ThreadContent, ThreadMessage } from '@janhq/core'
import type { CoworkSession } from '@/hooks/useCoworkSessions'
import { coworkTurnsToUIMessages } from '@/lib/coworkTurns'
import { STOPPED_TOOL_ERROR } from '@/lib/stopped-tool-calls'
+import { SYSTEM_NOTICE_KEY, isPingOnly } from '@/lib/coworkPing'
/**
* Converts a Cowork session's transcript into the records main chat persists
@@ -220,9 +221,17 @@ export function coworkSessionToThreadMessages(
? { ...message.metadata }
: {}
delete sourceMetadata.toolErrors
+ delete sourceMetadata[SYSTEM_NOTICE_KEY]
const metadata = {
...sourceMetadata,
...(Object.keys(toolErrors).length > 0 && { toolErrors }),
+ // A ping (`<SYSTEM>...</SYSTEM>`, a subagent finishing) is in the model's
+ // history as a user turn, so it is kept as one with its text unchanged
+ // and only flagged; main chat then shows it as a system note instead of
+ // a raw user bubble. Dropping it would change what the model is replayed.
+ ...(isPingOnly({ role: String(message.role), parts: message.parts }) && {
+ [SYSTEM_NOTICE_KEY]: true,
+ }),
}
const createdAt = base + index
out.push(