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): carry Cowork <SYSTEM> pings past a dropped user turn; treat 127.0.0.0/8 and 0.0.0.0 as loopback

thinhlpga19c7316043e
diff --git a/web-app/src/lib/__tests__/custom-chat-transport.test.ts b/web-app/src/lib/__tests__/custom-chat-transport.test.ts
index 1bf723fe4..b479667f5 100644
--- a/web-app/src/lib/__tests__/custom-chat-transport.test.ts
+++ b/web-app/src/lib/__tests__/custom-chat-transport.test.ts
@@ -516,6 +516,25 @@ describe('coalesceMessagesForAlternation', () => {
])
})
+ it('carries a Cowork ping turn forward into the next user message', () => {
+ const ping = {
+ type: 'text',
+ text: '<SYSTEM>\nsubagent a1 finished\n</SYSTEM>',
+ }
+ const input: UIMessage[] = [
+ userMsg('u1', 'start'),
+ assistantMsg('a1', [{ type: 'text', text: 'ok' }] as UIMessage['parts']),
+ { id: 'ping-2', role: 'user', parts: [ping] } as UIMessage,
+ userMsg('u2', 'what did it find?'),
+ ]
+ const out = coalesceMessagesForAlternation(input)
+ expect(out.map((m) => m.id)).toEqual(['u1', 'a1', 'u2'])
+ expect(out[2].parts).toEqual([
+ ping,
+ { type: 'text', text: 'what did it find?' },
+ ])
+ })
+
it('keeps assistant messages with real content', () => {
const input = [
userMsg('u1', 'q'),
@@ -856,6 +875,8 @@ describe('isLocalChatServer', () => {
['mlx', undefined],
['custom', 'http://localhost:11434/v1'],
['custom', 'http://127.0.0.1:8080/v1'],
+ ['custom', 'http://127.0.0.2:8080/v1'],
+ ['custom', 'http://0.0.0.0:1234/v1'],
['custom', 'http://[::1]:1234/v1'],
])('treats %s at %s as local (no retries)', (provider, baseUrl) => {
expect(isLocalChatServer(provider, baseUrl)).toBe(true)
@@ -865,6 +886,7 @@ describe('isLocalChatServer', () => {
['openai', 'https://api.openai.com/v1'],
['custom', 'http://192.168.1.5:1234/v1'],
['custom', 'https://localhost.example.com/v1'],
+ ['custom', 'http://127.example.com/v1'],
['custom', undefined],
['custom', 'not a url'],
])('treats %s at %s as hosted (SDK retries)', (provider, baseUrl) => {
diff --git a/web-app/src/lib/custom-chat-transport.ts b/web-app/src/lib/custom-chat-transport.ts
index 065848d4d..8d72a84b9 100644
--- a/web-app/src/lib/custom-chat-transport.ts
+++ b/web-app/src/lib/custom-chat-transport.ts
@@ -72,6 +72,7 @@ import {
parseVideoDataUrl,
} from '@/lib/video-sentinel'
import { isPredefinedRemoteProvider } from '@/lib/providerCaps'
+import { PING_OPEN } from '@/lib/coworkPing'
import { paramsSettings } from '@/lib/predefinedParams'
import { CHAT_SLOT_ID } from '@/constants/models'
import { createStepMetadata } from '@/lib/stepMetadata'
@@ -387,7 +388,8 @@ function isAssistantMessageEmpty(message: UIMessage): boolean {
* in the earlier bubble, so a follow-up like "what is in it?" must still reach
* the model with them. By this point images are `file` parts, audio and video
* are sentinel-only text parts, and documents are an [ATTACHED_FILES] block
- * (plus any inlined contents) appended after the question text.
+ * (plus any inlined contents) appended after the question text. Cowork pings
+ * are carried too.
*/
function carryAttachmentsForward(
dropped: UIMessage['parts'],
@@ -398,7 +400,13 @@ function carryAttachmentsForward(
if (part.type === 'file') {
attachments.push(part)
} else if (part.type === 'text' && typeof part.text === 'string') {
- if (hasAudioSentinel(part.text) || hasVideoSentinel(part.text)) {
+ // Cowork's <SYSTEM> pings (finished subagents) ride as their own user
+ // turn; they are context the model has not seen yet, not a question.
+ if (
+ hasAudioSentinel(part.text) ||
+ hasVideoSentinel(part.text) ||
+ part.text.trimStart().startsWith(PING_OPEN)
+ ) {
attachments.push(part)
continue
}
@@ -679,7 +687,8 @@ const LOCAL_ENGINE_PROVIDERS: Record<string, true> = {
const LOOPBACK_HOSTS: Record<string, true> = {
localhost: true,
- '127.0.0.1': true,
+ // What some local servers print as their listen address.
+ '0.0.0.0': true,
// URL.hostname keeps the brackets on IPv6 literals.
'[::1]': true,
}
@@ -696,7 +705,9 @@ export function isLocalChatServer(
if (LOCAL_ENGINE_PROVIDERS[providerId]) return true
if (!baseUrl) return false
try {
- return !!LOOPBACK_HOSTS[new URL(baseUrl).hostname]
+ const host = new URL(baseUrl).hostname
+ // URL has already normalised IPv4, so 127.0.0.0/8 is a prefix match.
+ return !!LOOPBACK_HOSTS[host] || /^127\.\d+\.\d+\.\d+$/.test(host)
} catch {
return false
}