mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-26 03:31:24 +08:00
Fix tool result handling and session creation flow
- Populate toolName in tool-result chunks from contentBlockState - Add onSessionCreated callback to SessionModal for post-creation actions - Return created session from useSessions hook and update SWR cache optimistically
This commit is contained in:
parent
efa54f3435
commit
e09cd6b6d7
@ -118,7 +118,7 @@ function handleUserOrAssistantMessage(message: Extract<SDKMessage, { type: 'assi
|
||||
chunks.push({
|
||||
type: 'tool-result',
|
||||
toolCallId: block.tool_use_id,
|
||||
toolName: '',
|
||||
toolName: contentBlockState[block.tool_use_id].toolName,
|
||||
input: '',
|
||||
output: block.content
|
||||
})
|
||||
|
||||
@ -46,6 +46,7 @@ const buildSessionForm = (existing?: AgentSessionEntity, agent?: AgentEntity): B
|
||||
interface BaseProps {
|
||||
agentId: string
|
||||
session?: AgentSessionEntity
|
||||
onSessionCreated?: (session: AgentSessionEntity) => void
|
||||
}
|
||||
|
||||
interface TriggerProps extends BaseProps {
|
||||
@ -73,7 +74,14 @@ type Props = TriggerProps | StateProps
|
||||
* @param onClose - Optional callback when modal closes. From useDisclosure.
|
||||
* @returns Modal component for agent creation/editing
|
||||
*/
|
||||
export const SessionModal: React.FC<Props> = ({ agentId, session, trigger, isOpen: _isOpen, onClose: _onClose }) => {
|
||||
export const SessionModal: React.FC<Props> = ({
|
||||
agentId,
|
||||
session,
|
||||
trigger,
|
||||
isOpen: _isOpen,
|
||||
onClose: _onClose,
|
||||
onSessionCreated
|
||||
}) => {
|
||||
const { isOpen, onClose, onOpen } = useDisclosure({ isOpen: _isOpen, onClose: _onClose })
|
||||
const { t } = useTranslation()
|
||||
const loadingRef = useRef(false)
|
||||
@ -161,38 +169,43 @@ export const SessionModal: React.FC<Props> = ({ agentId, session, trigger, isOpe
|
||||
return
|
||||
}
|
||||
|
||||
if (isEditing(session)) {
|
||||
if (!session) {
|
||||
throw new Error('Agent is required for editing mode')
|
||||
try {
|
||||
if (isEditing(session)) {
|
||||
if (!session) {
|
||||
throw new Error('Agent is required for editing mode')
|
||||
}
|
||||
|
||||
const updatePayload = {
|
||||
id: session.id,
|
||||
name: form.name,
|
||||
description: form.description,
|
||||
instructions: form.instructions,
|
||||
model: form.model,
|
||||
accessible_paths: [...form.accessible_paths]
|
||||
} satisfies UpdateSessionForm
|
||||
|
||||
updateSession(updatePayload)
|
||||
logger.debug('Updated agent', updatePayload)
|
||||
} else {
|
||||
const newSession = {
|
||||
name: form.name,
|
||||
description: form.description,
|
||||
instructions: form.instructions,
|
||||
model: form.model,
|
||||
accessible_paths: [...form.accessible_paths]
|
||||
} satisfies CreateSessionForm
|
||||
const createdSession = await createSession(newSession)
|
||||
if (createdSession) {
|
||||
onSessionCreated?.(createdSession)
|
||||
}
|
||||
logger.debug('Added agent', newSession)
|
||||
}
|
||||
|
||||
const updatePayload = {
|
||||
id: session.id,
|
||||
name: form.name,
|
||||
description: form.description,
|
||||
instructions: form.instructions,
|
||||
model: form.model,
|
||||
accessible_paths: [...form.accessible_paths]
|
||||
} satisfies UpdateSessionForm
|
||||
|
||||
updateSession(updatePayload)
|
||||
logger.debug('Updated agent', updatePayload)
|
||||
} else {
|
||||
const newSession = {
|
||||
name: form.name,
|
||||
description: form.description,
|
||||
instructions: form.instructions,
|
||||
model: form.model,
|
||||
accessible_paths: [...form.accessible_paths]
|
||||
} satisfies CreateSessionForm
|
||||
createSession(newSession)
|
||||
logger.debug('Added agent', newSession)
|
||||
// setTimeoutTimer('onCreateAgent', () => EventEmitter.emit(EVENT_NAMES.SHOW_ASSISTANTS), 0)
|
||||
onClose()
|
||||
} finally {
|
||||
loadingRef.current = false
|
||||
}
|
||||
|
||||
loadingRef.current = false
|
||||
|
||||
// setTimeoutTimer('onCreateAgent', () => EventEmitter.emit(EVENT_NAMES.SHOW_ASSISTANTS), 0)
|
||||
onClose()
|
||||
},
|
||||
[
|
||||
form.model,
|
||||
@ -202,6 +215,7 @@ export const SessionModal: React.FC<Props> = ({ agentId, session, trigger, isOpe
|
||||
form.accessible_paths,
|
||||
session,
|
||||
onClose,
|
||||
onSessionCreated,
|
||||
t,
|
||||
updateSession,
|
||||
createSession
|
||||
|
||||
@ -21,9 +21,11 @@ export const useSessions = (agentId: string) => {
|
||||
async (form: CreateSessionForm) => {
|
||||
try {
|
||||
const result = await client.createSession(agentId, form)
|
||||
mutate((prev) => [...(prev ?? []), result])
|
||||
await mutate((prev) => [...(prev ?? []), result], { revalidate: false })
|
||||
return result
|
||||
} catch (error) {
|
||||
window.toast.error(formatErrorMessageWithPrefix(error, t('agent.session.create.error.failed')))
|
||||
return undefined
|
||||
}
|
||||
},
|
||||
[agentId, client, mutate, t]
|
||||
|
||||
@ -66,6 +66,7 @@ const Sessions: React.FC<SessionsProps> = ({ agentId }) => {
|
||||
transition={{ duration: 0.2, delay: 0.1 }}>
|
||||
<SessionModal
|
||||
agentId={agentId}
|
||||
onSessionCreated={(created) => setActiveSessionId(agentId, created.id)}
|
||||
trigger={{
|
||||
content: (
|
||||
<Button
|
||||
|
||||
Loading…
Reference in New Issue
Block a user