mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 11:07:18 +00:00
* refactor(agent)!: migrate sessions to Pi Durable Replace patched AgentHarness storage with per-session SQLite and PostgreSQL commit mirroring. Migrate legacy history automatically and run agent queues with the main server. BREAKING CHANGE: Remove standalone agent workers and return native Pi entry records from chat history APIs. * fix(agent): constrain session log dialog scrolling Keep the header visible and wrap long output while log entries and expanded records scroll within the dialog. Cover desktop and narrow viewports through the dashboard Sessions flow. * fix(agent): hide entry IDs from session log card headers Retain internal entry IDs in expanded raw records for debugging. Verify that card headers omit the IDs while raw JSON preserves them. * test(e2e): handle browser wheel limits in session log checks Verify that a normal wheel event moves the viewport before explicitly scrolling to the final record. Keep the dialog bounds, fixed header, wrapping, and expanded-record assertions. * fix(agent): select model providers without inheriting unrelated auth * fix(webui): prevent resize feedback when toggling agent panel * test(agent): reproduce concurrent comment session races * fix(agent): serialize comment sessions and deduplicate history * docs(changelog): consolidate Pi Durable changes into one entry
195 lines
5.6 KiB
TypeScript
195 lines
5.6 KiB
TypeScript
import { zValidator } from '@hono/zod-validator'
|
|
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
|
import { chatService } from '@shumai/core/src/chat/chat'
|
|
import type { Prisma } from '@shumai/db'
|
|
import { chatRequestSchema, paginationParamsSchema } from '@shumai/dtos'
|
|
import { Hono } from 'hono'
|
|
import { streamSSE } from 'hono/streaming'
|
|
import { agentEntriesResponse } from './agent-entries'
|
|
|
|
type User = Prisma.UserGetPayload<Record<string, never>>
|
|
|
|
const route = new Hono<{ Variables: { user: User } }>()
|
|
.get('/teams/:teamId/chat/sessions', zValidator('query', paginationParamsSchema), async (c) => {
|
|
const user = c.get('user')
|
|
const teamId = c.req.param('teamId')
|
|
const query = c.req.valid('query')
|
|
|
|
await authzService.hasPermission({
|
|
user,
|
|
permission: Permission.Read,
|
|
type: ResourceType.Team,
|
|
id: teamId,
|
|
})
|
|
|
|
const res = await chatService.listSessions(user.id, teamId, query)
|
|
return c.json(res, 200)
|
|
})
|
|
.get('/teams/:teamId/chat/sessions/:sessionId/messages', async (c) => {
|
|
const user = c.get('user')
|
|
const teamId = c.req.param('teamId')
|
|
const sessionId = c.req.param('sessionId')
|
|
|
|
await authzService.hasPermission({
|
|
user,
|
|
permission: Permission.Read,
|
|
type: ResourceType.Team,
|
|
id: teamId,
|
|
})
|
|
|
|
const res = await chatService.listMessages(user.id, teamId, sessionId)
|
|
return agentEntriesResponse(res)
|
|
})
|
|
.delete('/teams/:teamId/chat/sessions/:sessionId', async (c) => {
|
|
const user = c.get('user')
|
|
const teamId = c.req.param('teamId')
|
|
const sessionId = c.req.param('sessionId')
|
|
|
|
await authzService.hasPermission({
|
|
user,
|
|
permission: Permission.Read,
|
|
type: ResourceType.Team,
|
|
id: teamId,
|
|
})
|
|
|
|
await chatService.deleteSession(user.id, teamId, sessionId)
|
|
return new Response(null, { status: 204 })
|
|
})
|
|
.post('/teams/:teamId/chat', zValidator('json', chatRequestSchema), async (c) => {
|
|
const user = c.get('user')
|
|
const teamId = c.req.param('teamId')
|
|
const req = c.req.valid('json')
|
|
|
|
await authzService.hasPermission({
|
|
user,
|
|
permission: Permission.Read,
|
|
type: ResourceType.Team,
|
|
id: teamId,
|
|
})
|
|
|
|
const { sessionId, taskId } = await chatService.startOrContinueChat(user, teamId, req)
|
|
|
|
c.header('x-session-id', sessionId)
|
|
|
|
return streamSSE(c, async (stream) => {
|
|
// Send session ID as the first event
|
|
await stream.writeSSE({
|
|
data: JSON.stringify({
|
|
type: 'session',
|
|
sessionId,
|
|
}),
|
|
})
|
|
|
|
let lastEntryId = ''
|
|
const timeoutMs = 5 * 60 * 1000 // 5 minutes timeout
|
|
const startTime = Date.now()
|
|
|
|
while (Date.now() - startTime < timeoutMs) {
|
|
if (stream.aborted) {
|
|
break
|
|
}
|
|
// Query new entries from the DB via chatService
|
|
const { messages, lastEntryId: newLastId } = await chatService.getNewSessionMessages(
|
|
sessionId,
|
|
lastEntryId || undefined,
|
|
)
|
|
|
|
if (messages.length > 0) {
|
|
for (const message of messages) {
|
|
await stream.writeSSE({
|
|
data: JSON.stringify({
|
|
type: 'entry',
|
|
entry: message,
|
|
}),
|
|
})
|
|
}
|
|
} else {
|
|
await stream.writeSSE({
|
|
data: JSON.stringify({
|
|
type: 'ping',
|
|
}),
|
|
})
|
|
}
|
|
|
|
if (newLastId) {
|
|
lastEntryId = newLastId
|
|
}
|
|
|
|
// Check if workflow has finished via chatService
|
|
const task = await chatService.getChatWorkflowStatus(taskId)
|
|
|
|
if (task && (task.status === 'completed' || task.status === 'failed')) {
|
|
// Fetch any remaining entries one last time
|
|
const { messages: finalMessages } = await chatService.getNewSessionMessages(
|
|
sessionId,
|
|
lastEntryId || undefined,
|
|
)
|
|
for (const message of finalMessages) {
|
|
await stream.writeSSE({
|
|
data: JSON.stringify({
|
|
type: 'entry',
|
|
entry: message,
|
|
}),
|
|
})
|
|
}
|
|
|
|
// Send done status
|
|
const output = task.output
|
|
await stream.writeSSE({
|
|
data: JSON.stringify({
|
|
type: 'done',
|
|
status: task.status,
|
|
error:
|
|
task.status === 'failed'
|
|
? (output?.error as string) || 'Workflow failed'
|
|
: undefined,
|
|
}),
|
|
})
|
|
break
|
|
}
|
|
|
|
// Wait before polling again
|
|
await stream.sleep(1000)
|
|
}
|
|
|
|
if (Date.now() - startTime >= timeoutMs) {
|
|
await stream.writeSSE({
|
|
data: JSON.stringify({
|
|
type: 'done',
|
|
status: 'failed',
|
|
error: 'Request timed out',
|
|
}),
|
|
})
|
|
}
|
|
})
|
|
})
|
|
.post('/teams/:teamId/chat/sessions/:sessionId/abort', async (c) => {
|
|
const user = c.get('user')
|
|
const teamId = c.req.param('teamId')
|
|
const sessionId = c.req.param('sessionId')
|
|
|
|
await authzService.hasPermission({
|
|
user,
|
|
permission: Permission.Read,
|
|
type: ResourceType.Team,
|
|
id: teamId,
|
|
})
|
|
|
|
try {
|
|
await chatService.abortSession(user.id, sessionId)
|
|
return c.json({ success: true }, 200)
|
|
} catch (error: unknown) {
|
|
const err = error as Error
|
|
if (
|
|
err.message === 'Session not found' ||
|
|
err.message === 'Unauthorized session access' ||
|
|
err.message === 'No active execution found for this session'
|
|
) {
|
|
return c.json({ error: err.message }, 404)
|
|
}
|
|
throw error
|
|
}
|
|
})
|
|
|
|
export default route
|