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(chat): inline an attachment once per request, not once per turn (#9022) (#9358)

James Dam21e342a531b1
diff --git a/web-app/src/lib/__tests__/custom-chat-transport-class.test.ts b/web-app/src/lib/__tests__/custom-chat-transport-class.test.ts
index 0a07949de..5f9965018 100644
--- a/web-app/src/lib/__tests__/custom-chat-transport-class.test.ts
+++ b/web-app/src/lib/__tests__/custom-chat-transport-class.test.ts
@@ -1,4 +1,5 @@
import { describe, expect, it, vi, beforeEach } from 'vitest'
+import type { UIMessage } from '@ai-sdk/react'
import { CustomChatTransport, normalizeToolInputSchema } from '../custom-chat-transport'
// Mock all the heavy dependencies
@@ -120,6 +121,29 @@ describe('CustomChatTransport', () => {
expect(result[0].parts[0].text).toContain('hello world')
})
+ it('does not mutate reused messages when mapping inline file content', () => {
+ const messages = [
+ {
+ id: 'user-1',
+ role: 'user',
+ parts: [{ type: 'text', text: 'Check this' }],
+ metadata: {
+ inline_file_contents: [{ name: 'file.txt', content: 'hello world' }],
+ },
+ },
+ ] as UIMessage[]
+
+ // The chat passes the same message objects on every turn (#9022), so each
+ // mapping must inline the file exactly once.
+ for (let turn = 0; turn < 3; turn++) {
+ const mapped = transport.mapUserInlineAttachments(messages)
+ expect(mapped[0].parts).toEqual([
+ { type: 'text', text: 'Check this\n\nFile: file.txt\nhello world' },
+ ])
+ }
+ expect(messages[0].parts).toEqual([{ type: 'text', text: 'Check this' }])
+ })
+
it('mapUserInlineAttachments ignores entries without content', () => {
const messages = [
{
diff --git a/web-app/src/lib/custom-chat-transport.ts b/web-app/src/lib/custom-chat-transport.ts
index cbdd2800c..9097ea9e1 100644
--- a/web-app/src/lib/custom-chat-transport.ts
+++ b/web-app/src/lib/custom-chat-transport.ts
@@ -1935,7 +1935,7 @@ export class CustomChatTransport implements ChatTransport<UIMessage> {
}
return part
})
- message.parts = parts
+ return { ...message, parts }
}
}
}