From 809736dd33138475442e8e7eae15f9760d567e32 Mon Sep 17 00:00:00 2001 From: icarus Date: Thu, 18 Sep 2025 22:58:49 +0800 Subject: [PATCH] feat(sessions): add update session functionality Introduce UpdateSessionResponse type and schema to support session updates. Implement update session methods across client, service, and handler layers to enable session modifications. --- .../routes/agents/handlers/sessions.ts | 4 +-- .../agents/services/SessionService.ts | 5 ++-- src/renderer/src/api/agent.ts | 25 ++++++++++++++++++- src/renderer/src/hooks/agents/useSessions.ts | 17 +++++++++++-- src/renderer/src/types/agent.ts | 4 +++ 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/main/apiServer/routes/agents/handlers/sessions.ts b/src/main/apiServer/routes/agents/handlers/sessions.ts index d9f45833c1..fc4c517623 100644 --- a/src/main/apiServer/routes/agents/handlers/sessions.ts +++ b/src/main/apiServer/routes/agents/handlers/sessions.ts @@ -1,6 +1,6 @@ import { loggerService } from '@logger' import { sessionMessageService, sessionService } from '@main/services/agents' -import { CreateSessionResponse, ListAgentSessionsResponse } from '@types' +import { CreateSessionResponse, ListAgentSessionsResponse, UpdateSessionResponse } from '@types' import { Request, Response } from 'express' const logger = loggerService.withContext('ApiServerSessionsHandlers') @@ -147,7 +147,7 @@ export const updateSession = async (req: Request, res: Response): Promise { + async updateSession(id: string, updates: UpdateSessionRequest): Promise { this.ensureInitialized() // Check if session exists diff --git a/src/renderer/src/api/agent.ts b/src/renderer/src/api/agent.ts index 0ca16b984a..e54ab88506 100644 --- a/src/renderer/src/api/agent.ts +++ b/src/renderer/src/api/agent.ts @@ -21,7 +21,11 @@ import { UpdateAgentForm, UpdateAgentRequest, UpdateAgentResponse, - UpdateAgentResponseSchema + UpdateAgentResponseSchema, + UpdateSessionForm, + UpdateSessionRequest, + UpdateSessionResponse, + UpdateSessionResponseSchema } from '@types' import axios, { Axios, AxiosRequestConfig, isAxiosError } from 'axios' import { ZodError } from 'zod' @@ -185,4 +189,23 @@ export class AgentApiClient { throw processError(error, 'Failed to delete session.') } } + + public async updateSession( + agentId: string, + sessionId: string, + session: UpdateSessionForm + ): Promise { + const url = this.getSessionPaths(agentId).withId(sessionId) + try { + const payload = session satisfies UpdateSessionRequest + const response = await this.axios.patch(url, payload) + const data = UpdateSessionResponseSchema.parse(response.data) + if (sessionId !== data.id) { + throw new Error('Session ID mismatch in response') + } + return data + } catch (error) { + throw processError(error, 'Failed to update session.') + } + } } diff --git a/src/renderer/src/hooks/agents/useSessions.ts b/src/renderer/src/hooks/agents/useSessions.ts index 6aa17a216d..1921cd4db0 100644 --- a/src/renderer/src/hooks/agents/useSessions.ts +++ b/src/renderer/src/hooks/agents/useSessions.ts @@ -1,4 +1,4 @@ -import { AgentEntity, CreateSessionForm } from '@renderer/types' +import { AgentEntity, CreateSessionForm, UpdateSessionForm } from '@renderer/types' import { useCallback } from 'react' import { useTranslation } from 'react-i18next' import useSWR from 'swr' @@ -49,12 +49,25 @@ export const useSessions = (agent: AgentEntity) => { [agent.id, client, mutate, t] ) + const updateSession = useCallback( + async (id: string, form: UpdateSessionForm) => { + try { + const result = await client.updateSession(agent.id, id, form) + mutate((prev) => prev?.map((session) => (session.id === id ? result : session))) + } catch (error) { + window.toast.error(t('agent.session.update.error.failed')) + } + }, + [agent.id, client, mutate, t] + ) + return { sessions: data ?? [], error, isLoading, createSession, getSession, - deleteSession + deleteSession, + updateSession } } diff --git a/src/renderer/src/types/agent.ts b/src/renderer/src/types/agent.ts index c8e9a3112b..8bbbd264ee 100644 --- a/src/renderer/src/types/agent.ts +++ b/src/renderer/src/types/agent.ts @@ -211,6 +211,10 @@ export const CreateSessionResponseSchema = AgentSessionEntitySchema export type CreateSessionResponse = AgentSessionEntity +export const UpdateSessionResponseSchema = GetAgentSessionResponseSchema + +export type UpdateSessionResponse = GetAgentSessionResponse + export const AgentServerErrorSchema = z.object({ error: z.object({ message: z.string(),