mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 00:27:40 +00:00
refactor(agent)!: migrate to Pi Durable and embed the agent queue (#525)
* 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
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
import type { EntryRecord } from '@earendil-works/pi-durable'
|
||||
import type { TypedResponse } from 'hono'
|
||||
|
||||
/** Pi records are already JSON; retain their public types without recursively remapping SDK schemas. */
|
||||
export function agentEntriesResponse(
|
||||
entries: EntryRecord[],
|
||||
): Response & TypedResponse<EntryRecord[], 200, 'json'> {
|
||||
return new Response(JSON.stringify(entries), {
|
||||
headers: { 'Content-Type': 'application/json; charset=UTF-8' },
|
||||
}) as Response & TypedResponse<EntryRecord[], 200, 'json'>
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { ROOT_CONVERSATION_ID, type EntryId, type EntryRecord } from '@earendil-works/pi-durable'
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
@@ -352,15 +353,15 @@ describe('Agent API', () => {
|
||||
|
||||
describe('GET /agent-sessions/:sessionId/entries', () => {
|
||||
it('returns entries of agent session if user is admin', async () => {
|
||||
const mockEntries = [
|
||||
const mockEntries: EntryRecord[] = [
|
||||
{
|
||||
id: 'entry1',
|
||||
sessionId: 'session1',
|
||||
entry: { step: 1, message: 'Step 1' },
|
||||
id: 7 as EntryId,
|
||||
conversationId: ROOT_CONVERSATION_ID,
|
||||
kind: 'pi.user',
|
||||
model: [{ role: 'user', content: 'Step 1', timestamp: 1 }],
|
||||
},
|
||||
]
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
vi.mocked(agentService.getSessionEntries).mockResolvedValue(mockEntries as any)
|
||||
vi.mocked(agentService.getSessionEntries).mockResolvedValue(mockEntries)
|
||||
|
||||
const res = await app.request('/agent-sessions/session1/entries', {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -369,8 +370,9 @@ describe('Agent API', () => {
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data).toHaveLength(1)
|
||||
expect(data[0].id).toBe('entry1')
|
||||
expect(data[0].entry.step).toBe(1)
|
||||
expect(data[0].id).toBe(7)
|
||||
expect(data[0].kind).toBe('pi.user')
|
||||
expect(data[0].model[0].content).toBe('Step 1')
|
||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||
user: undefined,
|
||||
permission: 'Admin',
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
import { Hono } from 'hono'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { agentService } from '@shumai/core/src/agent/agent'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import {
|
||||
createAgentRequestSchema,
|
||||
updateAgentRequestSchema,
|
||||
updateAgentPermissionRequestSchema,
|
||||
paginationParamsSchema,
|
||||
AgentInfo,
|
||||
AuditAction,
|
||||
createAgentRequestSchema,
|
||||
paginationParamsSchema,
|
||||
updateAgentPermissionRequestSchema,
|
||||
updateAgentRequestSchema,
|
||||
} from '@shumai/dtos'
|
||||
import { Hono } from 'hono'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import { agentEntriesResponse } from './agent-entries'
|
||||
|
||||
import type { Prisma } from '@shumai/db'
|
||||
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
|
||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||
|
||||
@@ -219,13 +220,7 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
})
|
||||
|
||||
const entries = await agentService.getSessionEntries({ sessionId })
|
||||
const infos = entries.map((entry) => ({
|
||||
id: entry.id,
|
||||
sessionId: entry.sessionId,
|
||||
entry: entry.entry,
|
||||
}))
|
||||
|
||||
return c.json(infos, 200)
|
||||
return agentEntriesResponse(entries)
|
||||
})
|
||||
.get('/teams/:teamId/agent-sessions', zValidator('query', paginationParamsSchema), async (c) => {
|
||||
const teamId = c.req.param('teamId')
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { describe, expect, it, vi, beforeEach } from 'vitest'
|
||||
import { Hono } from 'hono'
|
||||
import chatRoute from './chat'
|
||||
import { chatService } from '@shumai/core/src/chat/chat'
|
||||
import { authzService } from '@shumai/core/src/authz/authz'
|
||||
import type { ChatMessage } from '@shumai/dtos'
|
||||
import { chatService } from '@shumai/core/src/chat/chat'
|
||||
import { ROOT_CONVERSATION_ID, type EntryId, type EntryRecord } from '@earendil-works/pi-durable'
|
||||
import { Hono } from 'hono'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import chatRoute from './chat'
|
||||
|
||||
vi.mock('@shumai/core/src/chat/chat', async (importOriginal) => {
|
||||
const original = await importOriginal<typeof import('@shumai/core/src/chat/chat')>()
|
||||
@@ -65,23 +65,22 @@ describe('Chat API', () => {
|
||||
|
||||
describe('GET /teams/:teamId/chat/sessions/:sessionId/messages', () => {
|
||||
it('returns list of messages', async () => {
|
||||
const mockMessages = [
|
||||
const mockMessages: EntryRecord[] = [
|
||||
{
|
||||
id: 'msg1',
|
||||
role: 'user',
|
||||
content: 'hello',
|
||||
timestamp: '2026-07-07T10:00:00Z',
|
||||
entry: {},
|
||||
id: 7 as EntryId,
|
||||
conversationId: ROOT_CONVERSATION_ID,
|
||||
kind: 'pi.user',
|
||||
model: [{ role: 'user', content: 'hello', timestamp: 1 }],
|
||||
},
|
||||
]
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
vi.mocked(chatService.listMessages).mockResolvedValue(mockMessages as any)
|
||||
vi.mocked(chatService.listMessages).mockResolvedValue(mockMessages)
|
||||
|
||||
const res = await app.request('/teams/team1/chat/sessions/session1/messages')
|
||||
expect(res.status).toBe(200)
|
||||
const data = await res.json()
|
||||
expect(data).toHaveLength(1)
|
||||
expect(data[0].content).toBe('hello')
|
||||
expect(data[0].model[0].content).toBe('hello')
|
||||
expect(data[0].kind).toBe('pi.user')
|
||||
expect(chatService.listMessages).toHaveBeenCalledWith('user1', 'team1', 'session1')
|
||||
})
|
||||
})
|
||||
@@ -109,16 +108,17 @@ describe('Chat API', () => {
|
||||
.mockResolvedValueOnce({
|
||||
messages: [
|
||||
{
|
||||
id: 'entry1',
|
||||
role: 'user',
|
||||
content: 'hi',
|
||||
} as unknown as ChatMessage,
|
||||
id: 7 as EntryId,
|
||||
conversationId: ROOT_CONVERSATION_ID,
|
||||
kind: 'pi.user',
|
||||
model: [{ role: 'user', content: 'hi', timestamp: 1 }],
|
||||
},
|
||||
],
|
||||
lastEntryId: 'entry1',
|
||||
lastEntryId: '7',
|
||||
})
|
||||
.mockResolvedValue({
|
||||
messages: [],
|
||||
lastEntryId: 'entry1',
|
||||
lastEntryId: '7',
|
||||
})
|
||||
|
||||
vi.mocked(chatService.getChatWorkflowStatus).mockResolvedValue({
|
||||
@@ -140,7 +140,8 @@ describe('Chat API', () => {
|
||||
|
||||
const text = await res.text()
|
||||
expect(text).toContain('data:')
|
||||
expect(text).toContain('entry1')
|
||||
expect(text).toContain('"id":7')
|
||||
expect(text).toContain('"kind":"pi.user"')
|
||||
expect(chatService.startOrContinueChat).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ id: 'user1' }),
|
||||
'team1',
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { Hono } from 'hono'
|
||||
import { zValidator } from '@hono/zod-validator'
|
||||
import { streamSSE } from 'hono/streaming'
|
||||
import { chatService } from '@shumai/core/src/chat/chat'
|
||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||
import { chatRequestSchema, paginationParamsSchema } from '@shumai/dtos'
|
||||
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>>
|
||||
|
||||
@@ -37,7 +38,7 @@ const route = new Hono<{ Variables: { user: User } }>()
|
||||
})
|
||||
|
||||
const res = await chatService.listMessages(user.id, teamId, sessionId)
|
||||
return c.json(res, 200)
|
||||
return agentEntriesResponse(res)
|
||||
})
|
||||
.delete('/teams/:teamId/chat/sessions/:sessionId', async (c) => {
|
||||
const user = c.get('user')
|
||||
|
||||
Reference in New Issue
Block a user