feat(chat): send SSE keep-alive ping heartbeats during polling

This commit is contained in:
yiling
2026-07-13 12:17:00 +08:00
parent 02f2fde8ac
commit d560032013
2 changed files with 43 additions and 0 deletions
+37
View File
@@ -158,5 +158,42 @@ describe('Chat API', () => {
expect.any(Object),
)
})
it('sends ping keep-alive SSE events when there are no new messages', async () => {
vi.mocked(chatService.startOrContinueChat).mockResolvedValue({
sessionId: 'session1',
taskId: 'task1',
})
// No new entries initially
// Mock returns a simple array instead of full PrismaPromise, so we bypass type safety.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(prisma.agentSessionEntry.findMany).mockResolvedValue([] as any)
const mockTask = {
id: 'task1',
status: 'completed',
output: {},
}
// Mock returns a simple task object instead of PrismaPromise, so we bypass type safety.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(prisma.workflowTask.findUnique).mockResolvedValue(mockTask as any)
const res = await app.request('/teams/team1/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
agentId: 'agent1',
textPrompt: 'hi',
}),
})
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('text/event-stream')
const text = await res.text()
expect(text).toContain('data:')
expect(text).toContain('{"type":"ping"}')
})
})
})
+6
View File
@@ -109,6 +109,12 @@ const route = new Hono<{ Variables: { user: User } }>()
})
}
lastEntryId = newEntries[newEntries.length - 1].id
} else {
await stream.writeSSE({
data: JSON.stringify({
type: 'ping',
}),
})
}
// Check if workflow has finished