mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-07 22:10:21 +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({
|
chunks.push({
|
||||||
type: 'tool-result',
|
type: 'tool-result',
|
||||||
toolCallId: block.tool_use_id,
|
toolCallId: block.tool_use_id,
|
||||||
toolName: '',
|
toolName: contentBlockState[block.tool_use_id].toolName,
|
||||||
input: '',
|
input: '',
|
||||||
output: block.content
|
output: block.content
|
||||||
})
|
})
|
||||||
|
|||||||
@ -46,6 +46,7 @@ const buildSessionForm = (existing?: AgentSessionEntity, agent?: AgentEntity): B
|
|||||||
interface BaseProps {
|
interface BaseProps {
|
||||||
agentId: string
|
agentId: string
|
||||||
session?: AgentSessionEntity
|
session?: AgentSessionEntity
|
||||||
|
onSessionCreated?: (session: AgentSessionEntity) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TriggerProps extends BaseProps {
|
interface TriggerProps extends BaseProps {
|
||||||
@ -73,7 +74,14 @@ type Props = TriggerProps | StateProps
|
|||||||
* @param onClose - Optional callback when modal closes. From useDisclosure.
|
* @param onClose - Optional callback when modal closes. From useDisclosure.
|
||||||
* @returns Modal component for agent creation/editing
|
* @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 { isOpen, onClose, onOpen } = useDisclosure({ isOpen: _isOpen, onClose: _onClose })
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const loadingRef = useRef(false)
|
const loadingRef = useRef(false)
|
||||||
@ -161,38 +169,43 @@ export const SessionModal: React.FC<Props> = ({ agentId, session, trigger, isOpe
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isEditing(session)) {
|
try {
|
||||||
if (!session) {
|
if (isEditing(session)) {
|
||||||
throw new Error('Agent is required for editing mode')
|
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 = {
|
// setTimeoutTimer('onCreateAgent', () => EventEmitter.emit(EVENT_NAMES.SHOW_ASSISTANTS), 0)
|
||||||
id: session.id,
|
onClose()
|
||||||
name: form.name,
|
} finally {
|
||||||
description: form.description,
|
loadingRef.current = false
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
loadingRef.current = false
|
|
||||||
|
|
||||||
// setTimeoutTimer('onCreateAgent', () => EventEmitter.emit(EVENT_NAMES.SHOW_ASSISTANTS), 0)
|
|
||||||
onClose()
|
|
||||||
},
|
},
|
||||||
[
|
[
|
||||||
form.model,
|
form.model,
|
||||||
@ -202,6 +215,7 @@ export const SessionModal: React.FC<Props> = ({ agentId, session, trigger, isOpe
|
|||||||
form.accessible_paths,
|
form.accessible_paths,
|
||||||
session,
|
session,
|
||||||
onClose,
|
onClose,
|
||||||
|
onSessionCreated,
|
||||||
t,
|
t,
|
||||||
updateSession,
|
updateSession,
|
||||||
createSession
|
createSession
|
||||||
|
|||||||
@ -21,9 +21,11 @@ export const useSessions = (agentId: string) => {
|
|||||||
async (form: CreateSessionForm) => {
|
async (form: CreateSessionForm) => {
|
||||||
try {
|
try {
|
||||||
const result = await client.createSession(agentId, form)
|
const result = await client.createSession(agentId, form)
|
||||||
mutate((prev) => [...(prev ?? []), result])
|
await mutate((prev) => [...(prev ?? []), result], { revalidate: false })
|
||||||
|
return result
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
window.toast.error(formatErrorMessageWithPrefix(error, t('agent.session.create.error.failed')))
|
window.toast.error(formatErrorMessageWithPrefix(error, t('agent.session.create.error.failed')))
|
||||||
|
return undefined
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[agentId, client, mutate, t]
|
[agentId, client, mutate, t]
|
||||||
|
|||||||
@ -66,6 +66,7 @@ const Sessions: React.FC<SessionsProps> = ({ agentId }) => {
|
|||||||
transition={{ duration: 0.2, delay: 0.1 }}>
|
transition={{ duration: 0.2, delay: 0.1 }}>
|
||||||
<SessionModal
|
<SessionModal
|
||||||
agentId={agentId}
|
agentId={agentId}
|
||||||
|
onSessionCreated={(created) => setActiveSessionId(agentId, created.id)}
|
||||||
trigger={{
|
trigger={{
|
||||||
content: (
|
content: (
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user