File size: 5,119 Bytes
a8aec61 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
import { useCallback } from 'react'
import {
getPlaygroundSessionAPI,
getAllPlaygroundSessionsAPI
} from '@/api/playground'
import { usePlaygroundStore } from '../store'
import { toast } from 'sonner'
import {
PlaygroundChatMessage,
ToolCall,
ReasoningMessage,
ChatEntry
} from '@/types/playground'
import { getJsonMarkdown } from '@/lib/utils'
interface SessionResponse {
session_id: string
agent_id: string
user_id: string | null
runs?: ChatEntry[]
memory: {
runs?: ChatEntry[]
chats?: ChatEntry[]
}
agent_data: Record<string, unknown>
}
const useSessionLoader = () => {
const setMessages = usePlaygroundStore((state) => state.setMessages)
const selectedEndpoint = usePlaygroundStore((state) => state.selectedEndpoint)
const setIsSessionsLoading = usePlaygroundStore(
(state) => state.setIsSessionsLoading
)
const setSessionsData = usePlaygroundStore((state) => state.setSessionsData)
const getSessions = useCallback(
async (agentId: string) => {
if (!agentId || !selectedEndpoint) return
try {
setIsSessionsLoading(true)
const sessions = await getAllPlaygroundSessionsAPI(
selectedEndpoint,
agentId
)
setSessionsData(sessions)
} catch {
toast.error('Error loading sessions')
} finally {
setIsSessionsLoading(false)
}
},
[selectedEndpoint, setSessionsData, setIsSessionsLoading]
)
const getSession = useCallback(
async (sessionId: string, agentId: string) => {
if (!sessionId || !agentId || !selectedEndpoint) {
return null
}
try {
const response = (await getPlaygroundSessionAPI(
selectedEndpoint,
agentId,
sessionId
)) as SessionResponse
if (response && response.memory) {
const sessionHistory = response.runs
? response.runs
: response.memory.runs
if (sessionHistory && Array.isArray(sessionHistory)) {
const messagesForPlayground = sessionHistory.flatMap((run) => {
const filteredMessages: PlaygroundChatMessage[] = []
if (run.message) {
filteredMessages.push({
role: 'user',
content: run.message.content ?? '',
created_at: run.message.created_at
})
}
if (run.response) {
const toolCalls = [
...(run.response.tools ?? []),
...(run.response.extra_data?.reasoning_messages ?? []).reduce(
(acc: ToolCall[], msg: ReasoningMessage) => {
if (msg.role === 'tool') {
acc.push({
role: msg.role,
content: msg.content,
tool_call_id: msg.tool_call_id ?? '',
tool_name: msg.tool_name ?? '',
tool_args: msg.tool_args ?? {},
tool_call_error: msg.tool_call_error ?? false,
metrics: msg.metrics ?? { time: 0 },
created_at:
msg.created_at ?? Math.floor(Date.now() / 1000)
})
}
return acc
},
[]
)
]
filteredMessages.push({
role: 'agent',
content: (run.response.content as string) ?? '',
tool_calls: toolCalls.length > 0 ? toolCalls : undefined,
extra_data: run.response.extra_data,
images: run.response.images,
videos: run.response.videos,
audio: run.response.audio,
response_audio: run.response.response_audio,
created_at: run.response.created_at
})
}
return filteredMessages
})
const processedMessages = messagesForPlayground.map(
(message: PlaygroundChatMessage) => {
if (Array.isArray(message.content)) {
const textContent = message.content
.filter((item: { type: string }) => item.type === 'text')
.map((item) => item.text)
.join(' ')
return {
...message,
content: textContent
}
}
if (typeof message.content !== 'string') {
return {
...message,
content: getJsonMarkdown(message.content)
}
}
return message
}
)
setMessages(processedMessages)
return processedMessages
}
}
} catch {
return null
}
},
[selectedEndpoint, setMessages]
)
return { getSession, getSessions }
}
export default useSessionLoader
|