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.

test(transport): cover assistant completion (continuation prefill) prompts

Akarshan Biswas456f4054d06d
diff --git a/web-app/src/lib/__tests__/custom-chat-transport-prefix-stability.test.ts b/web-app/src/lib/__tests__/custom-chat-transport-prefix-stability.test.ts
index a1fa680ae..c0b4bd3a2 100644
--- a/web-app/src/lib/__tests__/custom-chat-transport-prefix-stability.test.ts
+++ b/web-app/src/lib/__tests__/custom-chat-transport-prefix-stability.test.ts
@@ -207,3 +207,77 @@ describe('CustomChatTransport prompt-prefix stability across turns', () => {
).toBe(JSON.stringify(secondMessages))
})
})
+
+describe('CustomChatTransport assistant completion (continuation prefill)', () => {
+ beforeEach(() => {
+ streamTextCalls.length = 0
+ h.disabledTools = []
+ h.servers = ['srv']
+ h.getRelevantTools.mockReset()
+ h.getRelevantTools.mockResolvedValue([
+ { name: 'tool_a', description: 'a', inputSchema: {}, server: 'srv' },
+ ])
+ })
+
+ const history: UIMessage[] = [
+ user('u1', 'write a poem'),
+ assistant('a1', 'here goes'),
+ user('u2', 'continue'),
+ ]
+
+ it('appends the partial content as a trailing assistant prefill message', async () => {
+ const transport = new CustomChatTransport('you are jan', 'thread-1')
+ transport.setContinueFromContent('Roses are red,')
+ await drain(await send(transport, history))
+
+ expect(streamTextCalls).toHaveLength(1)
+ const messages = streamTextCalls[0].messages as Array<{
+ role: string
+ content: unknown
+ }>
+ const last = messages[messages.length - 1]
+ expect(last.role).toBe('assistant')
+ expect(last.content).toBe('Roses are red,')
+ })
+
+ it('leaves the cached prefix byte-identical (prefill only appends)', async () => {
+ const transport = new CustomChatTransport('you are jan', 'thread-1')
+
+ // Normal send seeds the cache prefix.
+ await drain(await send(transport, history))
+ // Continuation send of the same history appends the prefill.
+ transport.setContinueFromContent('Roses are red,')
+ await drain(await send(transport, history))
+
+ expect(streamTextCalls).toHaveLength(2)
+ const [normal, cont] = streamTextCalls
+ const normalMessages = normal.messages as unknown[]
+ const contMessages = cont.messages as unknown[]
+
+ // The prefill adds exactly one trailing message; everything before it is
+ // re-serialized byte-for-byte, so llama.cpp reuses the KV cache.
+ expect(contMessages).toHaveLength(normalMessages.length + 1)
+ expect(JSON.stringify(contMessages.slice(0, normalMessages.length))).toBe(
+ JSON.stringify(normalMessages)
+ )
+ })
+
+ it('consumes the prefill once (next send appends nothing)', async () => {
+ const transport = new CustomChatTransport('you are jan', 'thread-1')
+
+ transport.setContinueFromContent('Roses are red,')
+ await drain(await send(transport, history))
+ // A second send without re-setting must not re-append the prefill.
+ await drain(await send(transport, history))
+
+ expect(streamTextCalls).toHaveLength(2)
+ const [withPrefill, without] = streamTextCalls
+ const withMessages = withPrefill.messages as unknown[]
+ const withoutMessages = without.messages as unknown[]
+ expect(withMessages).toHaveLength(withoutMessages.length + 1)
+ const last = (withoutMessages[withoutMessages.length - 1] as {
+ role: string
+ }).role
+ expect(last).toBe('user')
+ })
+})