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(web-app): harden the Cowork transcript converter

Akarshan Biswasd34369f5fdc0
diff --git a/web-app/src/lib/__tests__/coworkThreadMessages.test.ts b/web-app/src/lib/__tests__/coworkThreadMessages.test.ts
index 994942f83..5ee552396 100644
--- a/web-app/src/lib/__tests__/coworkThreadMessages.test.ts
+++ b/web-app/src/lib/__tests__/coworkThreadMessages.test.ts
@@ -234,4 +234,48 @@ describe('coworkSessionToThreadMessages', () => {
)
expect(viaFile).toEqual(once)
})
+
+ it('treats a session without `updated` as 0, never NaN', () => {
+ const s = session([msg('u', 'user', [{ type: 'text', text: 'hi' }])])
+ delete (s as Partial<CoworkSession>).updated
+ const out = coworkSessionToThreadMessages(s)
+ expect(out[0].created_at).toBe(0)
+ expect(JSON.stringify(out)).not.toContain('null')
+ })
+
+ it('lets the converter own toolErrors: stale source errors do not survive', () => {
+ const s = session([
+ {
+ id: 'a',
+ role: 'assistant',
+ metadata: { toolErrors: { stale: 'old failure' }, keep: 1 },
+ parts: [
+ toolPart('read', 'ok', { state: 'output-available', output: 'x' }),
+ ],
+ },
+ {
+ id: 'b',
+ role: 'assistant',
+ metadata: { toolErrors: { stale: 'old failure' } },
+ parts: [toolPart('read', 'bad', { state: 'output-error', errorText: 'E' })],
+ },
+ ])
+ const [first, second] = coworkSessionToThreadMessages(s)
+ expect(first.metadata).toEqual({ keep: 1 })
+ expect(second.metadata).toEqual({ toolErrors: { bad: 'E' } })
+ })
+
+ it('reads reasoning as `reasoning ?? text`, like extractContentPartsFromUIMessage', () => {
+ const s = session([
+ msg('a', 'assistant', [
+ { type: 'reasoning', reasoning: 'from reasoning', text: 'from text' },
+ { type: 'reasoning', text: 'only text' },
+ ]),
+ ])
+ const [m] = coworkSessionToThreadMessages(s)
+ expect(m.content.map((c) => c.text?.value)).toEqual([
+ 'from reasoning',
+ 'only text',
+ ])
+ })
})
diff --git a/web-app/src/lib/coworkThreadMessages.ts b/web-app/src/lib/coworkThreadMessages.ts
index 0150b2d12..561e68cac 100644
--- a/web-app/src/lib/coworkThreadMessages.ts
+++ b/web-app/src/lib/coworkThreadMessages.ts
@@ -64,7 +64,11 @@ function stable<T>(value: T): T {
}
/** Cowork stores `updated` in ms, older data in seconds; threads want ms here. */
-const updatedMs = (updated: number) => (updated > 1e12 ? updated : updated * 1000)
+const updatedMs = (updated: number | undefined) => {
+ // A session with no usable `updated` is dated 0: NaN would be stored as null.
+ if (typeof updated !== 'number' || !Number.isFinite(updated)) return 0
+ return updated > 1e12 ? updated : updated * 1000
+}
const textItem = (type: ContentType, value: string): ThreadContent => ({
type,
@@ -136,7 +140,8 @@ function convertMessage(
return
}
if (part.type === 'reasoning') {
- const text = part.text ?? part.reasoning
+ // Same order as extractContentPartsFromUIMessage.
+ const text = part.reasoning ?? part.text
if (typeof text === 'string' && text) {
content.push(textItem(ContentType.Reasoning, text))
}
@@ -209,8 +214,14 @@ export function coworkSessionToThreadMessages(
)
if (content.length === 0) return
+ // toolErrors is derived from the parts alone: a stale one in the source
+ // metadata would mark a call that has a result as failed.
+ const sourceMetadata: Record<string, unknown> = isRecord(message.metadata)
+ ? { ...message.metadata }
+ : {}
+ delete sourceMetadata.toolErrors
const metadata = {
- ...(isRecord(message.metadata) ? message.metadata : {}),
+ ...sourceMetadata,
...(Object.keys(toolErrors).length > 0 && { toolErrors }),
}
const createdAt = base + index