From 432d84cda5a43c4d3e668ecb9510b730f4ca03ec Mon Sep 17 00:00:00 2001 From: icarus Date: Fri, 19 Sep 2025 15:08:53 +0800 Subject: [PATCH] refactor(sessions): simplify session update API by using form object Remove redundant sessionId parameter from updateSession methods since it's already included in the UpdateSessionForm. This makes the API more consistent and reduces potential for mismatched IDs. --- src/renderer/src/api/agent.ts | 10 +++------- src/renderer/src/hooks/agents/useSessions.ts | 6 +++--- src/renderer/src/types/agent.ts | 2 ++ 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/renderer/src/api/agent.ts b/src/renderer/src/api/agent.ts index b84ccf1fa0..b648ee4a41 100644 --- a/src/renderer/src/api/agent.ts +++ b/src/renderer/src/api/agent.ts @@ -191,17 +191,13 @@ export class AgentApiClient { } } - public async updateSession( - agentId: string, - sessionId: string, - session: UpdateSessionForm - ): Promise { - const url = this.getSessionPaths(agentId).withId(sessionId) + public async updateSession(agentId: string, session: UpdateSessionForm): Promise { + const url = this.getSessionPaths(agentId).withId(session.id) try { const payload = session satisfies UpdateSessionRequest const response = await this.axios.patch(url, payload) const data = UpdateSessionResponseSchema.parse(response.data) - if (sessionId !== data.id) { + if (session.id !== data.id) { throw new Error('Session ID mismatch in response') } return data diff --git a/src/renderer/src/hooks/agents/useSessions.ts b/src/renderer/src/hooks/agents/useSessions.ts index 8abe08c086..7471edd5d8 100644 --- a/src/renderer/src/hooks/agents/useSessions.ts +++ b/src/renderer/src/hooks/agents/useSessions.ts @@ -57,11 +57,11 @@ export const useSessions = (agentId: string) => { ) const updateSession = useCallback( - async (id: string, form: UpdateSessionForm) => { + async (form: UpdateSessionForm) => { if (!agentId) return try { - const result = await client.updateSession(agentId, id, form) - mutate((prev) => prev?.map((session) => (session.id === id ? result : session))) + const result = await client.updateSession(agentId, form) + mutate((prev) => prev?.map((session) => (session.id === form.id ? result : session))) } catch (error) { window.toast.error(t('agent.session.update.error.failed')) } diff --git a/src/renderer/src/types/agent.ts b/src/renderer/src/types/agent.ts index e78964e524..2db40b4582 100644 --- a/src/renderer/src/types/agent.ts +++ b/src/renderer/src/types/agent.ts @@ -154,6 +154,8 @@ export type CreateSessionForm = BaseSessionForm & { id?: never } export type UpdateSessionForm = Partial & { id: string } +export type SessionForm = CreateSessionForm | UpdateSessionForm + // ------------------ API data transfer objects ------------------ export interface CreateAgentRequest extends AgentBase { type: AgentType