mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-03 14:34: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,165 @@
|
||||
import type { ImageContent, Message, ToolResultMessage } from '@earendil-works/pi-ai'
|
||||
import type { Cursor, EntryRecord, HookApi, StorageWrite } from '@earendil-works/pi-durable'
|
||||
import { serializeContextToXml, shumaiMessageContextSchema } from '@shumai/dtos'
|
||||
import { s3Service } from '../s3/s3'
|
||||
import { agentService } from './agent'
|
||||
|
||||
export const S3_PLACEHOLDER = '__S3_DATA__'
|
||||
export const SKILL_PLACEHOLDER = '__SKILL_CONTENT__'
|
||||
|
||||
function contentReferences(value: unknown): { sourceKeys: string[]; skillId?: string } {
|
||||
if (!value || typeof value !== 'object') return { sourceKeys: [] }
|
||||
const record = value as Record<string, unknown>
|
||||
return {
|
||||
sourceKeys: Array.isArray(record.sourceKeys)
|
||||
? record.sourceKeys.filter((key): key is string => typeof key === 'string')
|
||||
: [],
|
||||
skillId: typeof record.skillId === 'string' ? record.skillId : undefined,
|
||||
}
|
||||
}
|
||||
|
||||
function compactMessage(message: Message, entryData: unknown): Message {
|
||||
if (message.role !== 'toolResult' && message.role !== 'user') return message
|
||||
const refs = contentReferences(message.role === 'toolResult' ? message.details : entryData)
|
||||
if (typeof message.content === 'string') return message
|
||||
let imageIndex = 0
|
||||
return {
|
||||
...message,
|
||||
content: message.content.map((block) => {
|
||||
if (block.type === 'image') {
|
||||
const key = refs.sourceKeys[imageIndex++]
|
||||
return key ? { ...block, data: S3_PLACEHOLDER } : block
|
||||
}
|
||||
if (
|
||||
message.role === 'toolResult' &&
|
||||
message.toolName === 'read_skill' &&
|
||||
!message.isError &&
|
||||
refs.skillId &&
|
||||
block.type === 'text'
|
||||
)
|
||||
return { ...block, text: SKILL_PLACEHOLDER }
|
||||
return block
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/** Only immutable entries are rewritten. Durable's document deltas keep their original bases. */
|
||||
export function compactSessionWrites(writes: readonly StorageWrite[]): StorageWrite[] {
|
||||
return writes.map((write) =>
|
||||
write.type === 'entry'
|
||||
? {
|
||||
...write,
|
||||
value: {
|
||||
...write.value,
|
||||
model: write.value.model?.map((m) => compactMessage(m, write.value.data)),
|
||||
},
|
||||
}
|
||||
: write,
|
||||
)
|
||||
}
|
||||
|
||||
export async function hydrateMessages(
|
||||
messages: readonly Message[],
|
||||
entryData?: unknown,
|
||||
): Promise<Message[]> {
|
||||
return Promise.all(
|
||||
messages.map(async (message): Promise<Message> => {
|
||||
if (message.role !== 'toolResult' && message.role !== 'user') return message
|
||||
if (typeof message.content === 'string') return message
|
||||
const refs = contentReferences(message.role === 'toolResult' ? message.details : entryData)
|
||||
let imageIndex = 0
|
||||
const skillText =
|
||||
message.role === 'toolResult' &&
|
||||
message.toolName === 'read_skill' &&
|
||||
!message.isError &&
|
||||
refs.skillId
|
||||
? await agentService.getSkillContent(refs.skillId)
|
||||
: undefined
|
||||
const content = await Promise.all(
|
||||
message.content.map(async (block) => {
|
||||
if (block.type === 'image') {
|
||||
const key = refs.sourceKeys[imageIndex++]
|
||||
if (block.data === S3_PLACEHOLDER && key) {
|
||||
const { buffer } = await s3Service.getObject(process.env.S3_BUCKET || 'shumai', key)
|
||||
return { ...block, data: buffer.toString('base64') } satisfies ImageContent
|
||||
}
|
||||
}
|
||||
if (block.type === 'text' && skillText !== undefined) return { ...block, text: skillText }
|
||||
return block
|
||||
}),
|
||||
)
|
||||
return { ...message, content } as ToolResultMessage | Extract<Message, { role: 'user' }>
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
export async function hydrateEntry(entry: EntryRecord): Promise<EntryRecord> {
|
||||
if (!entry.model) return entry
|
||||
let model = await hydrateMessages(entry.model, entry.data)
|
||||
if (
|
||||
entry.kind === 'shumai.message' &&
|
||||
entry.data &&
|
||||
typeof entry.data === 'object' &&
|
||||
!Array.isArray(entry.data)
|
||||
) {
|
||||
const parsed = shumaiMessageContextSchema.safeParse(entry.data.context)
|
||||
if (parsed.success) {
|
||||
const xml = serializeContextToXml(parsed.data)
|
||||
model = model.map((message) => {
|
||||
if (message.role !== 'user') return message
|
||||
const content =
|
||||
typeof message.content === 'string'
|
||||
? message.content
|
||||
: message.content
|
||||
.filter((b) => b.type === 'text')
|
||||
.map((b) => b.text)
|
||||
.join('\n')
|
||||
if (!xml || content.includes('<context>')) return message
|
||||
return {
|
||||
...message,
|
||||
content:
|
||||
typeof message.content === 'string'
|
||||
? `${message.content}\n\n${xml}`
|
||||
: [...message.content, { type: 'text' as const, text: xml }],
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
return { ...entry, model }
|
||||
}
|
||||
|
||||
/** An admitted input may still be in Durable's warm context before its first storage read. */
|
||||
export async function hydrateRequest(
|
||||
messages: readonly Message[],
|
||||
api: HookApi,
|
||||
context: import('@earendil-works/chord/context').Context,
|
||||
) {
|
||||
const waiting = new Set(
|
||||
messages
|
||||
.filter(
|
||||
(m) =>
|
||||
m.role === 'user' &&
|
||||
Array.isArray(m.content) &&
|
||||
m.content.some((b) => b.type === 'image' && b.data === S3_PLACEHOLDER),
|
||||
)
|
||||
.map((m) => m.timestamp),
|
||||
)
|
||||
const references = new Map<number, unknown>()
|
||||
let cursor: Cursor | undefined
|
||||
while (waiting.size) {
|
||||
const page = await api.entries({ order: 'descending' }, 100, cursor, context)
|
||||
for (const entry of page.items) {
|
||||
for (const message of entry.model ?? []) {
|
||||
if (message.role === 'user' && waiting.delete(message.timestamp))
|
||||
references.set(message.timestamp, entry.data)
|
||||
}
|
||||
}
|
||||
if (!page.next) break
|
||||
cursor = page.next
|
||||
}
|
||||
return (
|
||||
await Promise.all(
|
||||
messages.map((message) => hydrateMessages([message], references.get(message.timestamp))),
|
||||
)
|
||||
).flat()
|
||||
}
|
||||
Reference in New Issue
Block a user