mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 04:46:54 +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
394 lines
12 KiB
TypeScript
394 lines
12 KiB
TypeScript
import { client } from '@/ui/api/client'
|
|
import type { Annotation } from '@/ui/types'
|
|
import type { EntryId, EntryRecord, JsonObject } from '@earendil-works/pi-durable'
|
|
import { ROOT_CONVERSATION_ID } from '@earendil-works/pi-durable'
|
|
import type { ChatSessionInfo } from '@shumai/dtos'
|
|
import { toast } from 'sonner'
|
|
import { create } from 'zustand'
|
|
|
|
export const AGENT_PREFERENCE_STORAGE_KEY = 'shumai_selected_agent_id'
|
|
|
|
const getInitialAgentId = (): string | null => {
|
|
if (typeof window !== 'undefined' && window.localStorage) {
|
|
return localStorage.getItem(AGENT_PREFERENCE_STORAGE_KEY)
|
|
}
|
|
return null
|
|
}
|
|
|
|
export interface ChatAsset {
|
|
id: string
|
|
name: string
|
|
type: 'file' | 'folder'
|
|
}
|
|
|
|
export interface AttachedFileMeta {
|
|
id: string
|
|
name: string
|
|
type: string
|
|
url?: string
|
|
mediaType?: string
|
|
mimeType?: string
|
|
}
|
|
|
|
interface ChatbotState {
|
|
isChatbotOpen: boolean
|
|
setIsChatbotOpen: (open: boolean) => void
|
|
isHistoryMode: boolean
|
|
setIsHistoryMode: (history: boolean) => void
|
|
currentSessionId: string | null
|
|
setCurrentSessionId: (id: string | null) => void
|
|
chatAssets: ChatAsset[]
|
|
addAssets: (assets: ChatAsset[]) => void
|
|
removeAsset: (id: string) => void
|
|
clearAssets: () => void
|
|
messages: EntryRecord[]
|
|
setMessages: (messages: EntryRecord[]) => void
|
|
historySessions: ChatSessionInfo[]
|
|
setHistorySessions: (sessions: ChatSessionInfo[]) => void
|
|
isStreaming: boolean
|
|
setIsStreaming: (streaming: boolean) => void
|
|
selectedAgentId: string | null
|
|
setSelectedAgentId: (id: string | null) => void
|
|
inputText: string
|
|
setInputText: (text: string) => void
|
|
|
|
scrollTop: number
|
|
isAtBottom: boolean
|
|
setScrollState: (scrollTop: number, isAtBottom: boolean) => void
|
|
|
|
fetchHistorySessions: (teamId: string) => Promise<void>
|
|
loadSession: (teamId: string, sessionId: string) => Promise<void>
|
|
deleteSession: (teamId: string, sessionId: string) => Promise<void>
|
|
startNewSession: () => void
|
|
sendMessage: (
|
|
teamId: string,
|
|
text: string,
|
|
projectId: string,
|
|
contextAssetId?: string,
|
|
onAssetMutation?: () => void,
|
|
attachmentIds?: string[],
|
|
annotations?: Annotation[],
|
|
second?: number | null,
|
|
attachedFilesMeta?: AttachedFileMeta[],
|
|
) => Promise<void>
|
|
abortActiveSession: (teamId: string) => Promise<void>
|
|
}
|
|
|
|
export const useChatbotStore = create<ChatbotState>((set) => ({
|
|
isChatbotOpen: false,
|
|
setIsChatbotOpen: (open) => set({ isChatbotOpen: open }),
|
|
isHistoryMode: false,
|
|
setIsHistoryMode: (history) => set({ isHistoryMode: history }),
|
|
currentSessionId: null,
|
|
setCurrentSessionId: (id) => set({ currentSessionId: id }),
|
|
chatAssets: [],
|
|
addAssets: (assets) =>
|
|
set((state) => {
|
|
const existingIds = new Set(state.chatAssets.map((a) => a.id))
|
|
const uniqueNewAssets = assets.filter((a) => !existingIds.has(a.id))
|
|
return { chatAssets: [...state.chatAssets, ...uniqueNewAssets] }
|
|
}),
|
|
removeAsset: (id) =>
|
|
set((state) => ({
|
|
chatAssets: state.chatAssets.filter((a) => a.id !== id),
|
|
})),
|
|
clearAssets: () => set({ chatAssets: [] }),
|
|
messages: [],
|
|
setMessages: (messages) => set({ messages }),
|
|
historySessions: [],
|
|
setHistorySessions: (sessions) => set({ historySessions: sessions }),
|
|
isStreaming: false,
|
|
setIsStreaming: (streaming) => set({ isStreaming: streaming }),
|
|
selectedAgentId: getInitialAgentId(),
|
|
setSelectedAgentId: (id) => {
|
|
if (typeof window !== 'undefined' && window.localStorage) {
|
|
if (id) {
|
|
localStorage.setItem(AGENT_PREFERENCE_STORAGE_KEY, id)
|
|
} else {
|
|
localStorage.removeItem(AGENT_PREFERENCE_STORAGE_KEY)
|
|
}
|
|
}
|
|
set({ selectedAgentId: id })
|
|
},
|
|
inputText: '',
|
|
setInputText: (text) => set({ inputText: text }),
|
|
scrollTop: 0,
|
|
isAtBottom: true,
|
|
setScrollState: (scrollTop, isAtBottom) => set({ scrollTop, isAtBottom }),
|
|
|
|
fetchHistorySessions: async (teamId) => {
|
|
try {
|
|
const res = await client.api.teams[':teamId'].chat.sessions.$get({
|
|
param: { teamId },
|
|
query: { first: '50' },
|
|
})
|
|
if (res.ok) {
|
|
const data = await res.json()
|
|
set({ historySessions: data.data || [] })
|
|
}
|
|
} catch (err) {
|
|
console.error('Failed to fetch history sessions:', err)
|
|
}
|
|
},
|
|
|
|
loadSession: async (teamId, sessionId) => {
|
|
try {
|
|
const res = await client.api.teams[':teamId'].chat.sessions[':sessionId'].messages.$get({
|
|
param: { teamId, sessionId },
|
|
})
|
|
if (res.ok) {
|
|
const messages = await res.json()
|
|
const sessionInfo = useChatbotStore
|
|
.getState()
|
|
.historySessions.find((s) => s.id === sessionId)
|
|
const agentId = sessionInfo?.agentId || null
|
|
set({
|
|
currentSessionId: sessionId,
|
|
messages: messages as EntryRecord[],
|
|
isHistoryMode: false,
|
|
scrollTop: 0,
|
|
isAtBottom: true,
|
|
...(agentId ? { selectedAgentId: agentId } : {}),
|
|
})
|
|
} else {
|
|
throw new Error('Failed to load session messages')
|
|
}
|
|
} catch (err) {
|
|
const errMsg = err instanceof Error ? err.message : 'Failed to load session'
|
|
toast.error(errMsg)
|
|
}
|
|
},
|
|
|
|
deleteSession: async (teamId, sessionId) => {
|
|
try {
|
|
const res = await client.api.teams[':teamId'].chat.sessions[':sessionId'].$delete({
|
|
param: { teamId, sessionId },
|
|
})
|
|
if (res.ok) {
|
|
toast.success('Session deleted successfully')
|
|
set((s) => ({
|
|
historySessions: s.historySessions.filter((sess) => sess.id !== sessionId),
|
|
currentSessionId: s.currentSessionId === sessionId ? null : s.currentSessionId,
|
|
messages: s.currentSessionId === sessionId ? [] : s.messages,
|
|
}))
|
|
} else {
|
|
throw new Error('Failed to delete session')
|
|
}
|
|
} catch (err) {
|
|
const errMsg = err instanceof Error ? err.message : 'Failed to delete session'
|
|
toast.error(errMsg)
|
|
}
|
|
},
|
|
|
|
startNewSession: () => {
|
|
const savedAgentId = getInitialAgentId()
|
|
set({
|
|
currentSessionId: null,
|
|
messages: [],
|
|
chatAssets: [],
|
|
inputText: '',
|
|
scrollTop: 0,
|
|
isAtBottom: true,
|
|
...(savedAgentId ? { selectedAgentId: savedAgentId } : {}),
|
|
})
|
|
},
|
|
|
|
sendMessage: async (
|
|
teamId,
|
|
text,
|
|
projectId,
|
|
contextAssetId,
|
|
onAssetMutation,
|
|
attachmentIds,
|
|
annotations,
|
|
second,
|
|
attachedFilesMeta,
|
|
) => {
|
|
const state = useChatbotStore.getState()
|
|
if (state.isStreaming) return
|
|
|
|
const tempId = -Date.now() as EntryId
|
|
const optimisticMsg: EntryRecord = {
|
|
id: tempId,
|
|
conversationId: ROOT_CONVERSATION_ID,
|
|
kind: 'shumai.optimistic',
|
|
data: JSON.parse(
|
|
JSON.stringify({
|
|
displayText: text,
|
|
timestamp: Date.now(),
|
|
context: {
|
|
currentAsset: contextAssetId
|
|
? { id: contextAssetId, name: '', type: 'file' }
|
|
: undefined,
|
|
position:
|
|
second !== undefined && second !== null
|
|
? { type: 'time', seconds: second }
|
|
: undefined,
|
|
annotation: !!(annotations && annotations.length > 0),
|
|
annotations: annotations || undefined,
|
|
attachedFiles: attachedFilesMeta || undefined,
|
|
referencedAssets:
|
|
state.chatAssets.length > 0
|
|
? state.chatAssets.map((a) => ({ id: a.id, name: a.name, type: a.type }))
|
|
: undefined,
|
|
},
|
|
}),
|
|
) as JsonObject,
|
|
}
|
|
|
|
const newMessages = [...state.messages, optimisticMsg]
|
|
|
|
set({
|
|
messages: newMessages,
|
|
isStreaming: true,
|
|
chatAssets: [],
|
|
isAtBottom: true,
|
|
})
|
|
|
|
const assetIds = state.chatAssets.map((a) => a.id)
|
|
|
|
const agentId = state.selectedAgentId
|
|
if (!agentId) {
|
|
toast.error('No agent selected')
|
|
return
|
|
}
|
|
|
|
try {
|
|
const res = await client.api.teams[':teamId'].chat.$post({
|
|
param: { teamId },
|
|
json: {
|
|
agentId,
|
|
textPrompt: text || undefined,
|
|
attachedFiles: attachmentIds && attachmentIds.length > 0 ? attachmentIds : undefined,
|
|
assetIds: assetIds && assetIds.length > 0 ? assetIds : undefined,
|
|
sessionId: state.currentSessionId || undefined,
|
|
contextAssetId,
|
|
projectId,
|
|
second: second ?? undefined,
|
|
annotations:
|
|
annotations && annotations.length > 0
|
|
? (annotations as unknown as Record<string, unknown>[])
|
|
: undefined,
|
|
},
|
|
})
|
|
|
|
if (!res.ok) {
|
|
throw new Error('Failed to start chat')
|
|
}
|
|
|
|
// Try reading custom header first
|
|
const headerSessionId = res.headers.get('x-session-id')
|
|
if (headerSessionId && !state.currentSessionId) {
|
|
set({ currentSessionId: headerSessionId })
|
|
}
|
|
|
|
const reader = res.body?.getReader()
|
|
if (!reader) {
|
|
throw new Error('No stream body')
|
|
}
|
|
|
|
const decoder = new TextDecoder()
|
|
let buffer = ''
|
|
|
|
while (true) {
|
|
const { done, value } = await reader.read()
|
|
if (done) break
|
|
|
|
buffer += decoder.decode(value, { stream: true })
|
|
const lines = buffer.split('\n')
|
|
buffer = lines.pop() || ''
|
|
|
|
for (const line of lines) {
|
|
const trimmed = line.trim()
|
|
if (!trimmed) continue
|
|
|
|
if (trimmed.startsWith('data: ')) {
|
|
const dataStr = trimmed.slice(6)
|
|
try {
|
|
const data = JSON.parse(dataStr)
|
|
if (data.type === 'session') {
|
|
if (!useChatbotStore.getState().currentSessionId) {
|
|
set({ currentSessionId: data.sessionId })
|
|
}
|
|
} else if (data.type === 'entry') {
|
|
const entry = data.entry as EntryRecord
|
|
const entryObj = entry.model?.[0]
|
|
if (
|
|
entryObj?.role === 'toolResult' &&
|
|
typeof entryObj.toolName === 'string' &&
|
|
[
|
|
'create_file',
|
|
'create_folder',
|
|
'create_version',
|
|
'rename_asset',
|
|
'move_assets',
|
|
'delete_asset',
|
|
].includes(entryObj.toolName) &&
|
|
!entryObj.isError
|
|
) {
|
|
onAssetMutation?.()
|
|
}
|
|
|
|
set((s) => {
|
|
const isExisting = s.messages.some((m) => m.id === entry.id)
|
|
const isUserMessage =
|
|
['pi.user', 'shumai.message'].includes(entry.kind) && !isExisting
|
|
const filtered = s.messages.filter((m) => !isUserMessage || m.id >= 0)
|
|
|
|
// Prevent duplicates by ID
|
|
const existingIdx = filtered.findIndex((m) => m.id === entry.id)
|
|
if (existingIdx !== -1) {
|
|
const copy = [...filtered]
|
|
copy[existingIdx] = entry
|
|
return { messages: copy }
|
|
} else {
|
|
return { messages: [...filtered, entry] }
|
|
}
|
|
})
|
|
} else if (data.type === 'done') {
|
|
if (data.status === 'failed') {
|
|
toast.error(data.error || 'Workflow failed')
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.error('Error parsing SSE line:', err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// If we finished successfully, trigger a history list refresh
|
|
const finalState = useChatbotStore.getState()
|
|
finalState.fetchHistorySessions(teamId)
|
|
} catch (err) {
|
|
const errMsg = err instanceof Error ? err.message : 'Failed to send message'
|
|
toast.error(errMsg)
|
|
// Remove optimistic message on failure
|
|
set((s) => ({
|
|
messages: s.messages.filter((m) => m.id !== tempId),
|
|
}))
|
|
} finally {
|
|
set({ isStreaming: false })
|
|
}
|
|
},
|
|
|
|
abortActiveSession: async (teamId) => {
|
|
const { currentSessionId, isStreaming } = useChatbotStore.getState()
|
|
if (!currentSessionId || !isStreaming) return
|
|
|
|
try {
|
|
const res = await client.api.teams[':teamId'].chat.sessions[':sessionId'].abort.$post({
|
|
param: { teamId, sessionId: currentSessionId },
|
|
})
|
|
if (res.ok) {
|
|
toast.success('Agent execution stopped')
|
|
} else {
|
|
throw new Error('Failed to abort agent execution')
|
|
}
|
|
} catch (err) {
|
|
const errMsg = err instanceof Error ? err.message : 'Failed to abort execution'
|
|
toast.error(errMsg)
|
|
}
|
|
},
|
|
}))
|