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.
This commit is contained in:
icarus 2025-09-18 22:58:49 +08:00
parent 369cc37071
commit 809736dd33
5 changed files with 48 additions and 7 deletions

View File

@ -1,6 +1,6 @@
import { loggerService } from '@logger' import { loggerService } from '@logger'
import { sessionMessageService, sessionService } from '@main/services/agents' import { sessionMessageService, sessionService } from '@main/services/agents'
import { CreateSessionResponse, ListAgentSessionsResponse } from '@types' import { CreateSessionResponse, ListAgentSessionsResponse, UpdateSessionResponse } from '@types'
import { Request, Response } from 'express' import { Request, Response } from 'express'
const logger = loggerService.withContext('ApiServerSessionsHandlers') const logger = loggerService.withContext('ApiServerSessionsHandlers')
@ -147,7 +147,7 @@ export const updateSession = async (req: Request, res: Response): Promise<Respon
} }
logger.info(`Session updated successfully: ${sessionId}`) logger.info(`Session updated successfully: ${sessionId}`)
return res.json(session) return res.json(session satisfies UpdateSessionResponse)
} catch (error: any) { } catch (error: any) {
logger.error('Error updating session:', error) logger.error('Error updating session:', error)
return res.status(500).json({ return res.status(500).json({

View File

@ -5,7 +5,8 @@ import type {
CreateSessionResponse, CreateSessionResponse,
GetAgentSessionResponse, GetAgentSessionResponse,
ListOptions, ListOptions,
UpdateSessionRequest UpdateSessionRequest,
UpdateSessionResponse
} from '@types' } from '@types'
import { and, count, eq, type SQL } from 'drizzle-orm' import { and, count, eq, type SQL } from 'drizzle-orm'
@ -139,7 +140,7 @@ export class SessionService extends BaseService {
return { sessions, total } return { sessions, total }
} }
async updateSession(id: string, updates: UpdateSessionRequest): Promise<GetAgentSessionResponse | null> { async updateSession(id: string, updates: UpdateSessionRequest): Promise<UpdateSessionResponse | null> {
this.ensureInitialized() this.ensureInitialized()
// Check if session exists // Check if session exists

View File

@ -21,7 +21,11 @@ import {
UpdateAgentForm, UpdateAgentForm,
UpdateAgentRequest, UpdateAgentRequest,
UpdateAgentResponse, UpdateAgentResponse,
UpdateAgentResponseSchema UpdateAgentResponseSchema,
UpdateSessionForm,
UpdateSessionRequest,
UpdateSessionResponse,
UpdateSessionResponseSchema
} from '@types' } from '@types'
import axios, { Axios, AxiosRequestConfig, isAxiosError } from 'axios' import axios, { Axios, AxiosRequestConfig, isAxiosError } from 'axios'
import { ZodError } from 'zod' import { ZodError } from 'zod'
@ -185,4 +189,23 @@ export class AgentApiClient {
throw processError(error, 'Failed to delete session.') throw processError(error, 'Failed to delete session.')
} }
} }
public async updateSession(
agentId: string,
sessionId: string,
session: UpdateSessionForm
): Promise<UpdateSessionResponse> {
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.')
}
}
} }

View File

@ -1,4 +1,4 @@
import { AgentEntity, CreateSessionForm } from '@renderer/types' import { AgentEntity, CreateSessionForm, UpdateSessionForm } from '@renderer/types'
import { useCallback } from 'react' import { useCallback } from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import useSWR from 'swr' import useSWR from 'swr'
@ -49,12 +49,25 @@ export const useSessions = (agent: AgentEntity) => {
[agent.id, client, mutate, t] [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 { return {
sessions: data ?? [], sessions: data ?? [],
error, error,
isLoading, isLoading,
createSession, createSession,
getSession, getSession,
deleteSession deleteSession,
updateSession
} }
} }

View File

@ -211,6 +211,10 @@ export const CreateSessionResponseSchema = AgentSessionEntitySchema
export type CreateSessionResponse = AgentSessionEntity export type CreateSessionResponse = AgentSessionEntity
export const UpdateSessionResponseSchema = GetAgentSessionResponseSchema
export type UpdateSessionResponse = GetAgentSessionResponse
export const AgentServerErrorSchema = z.object({ export const AgentServerErrorSchema = z.object({
error: z.object({ error: z.object({
message: z.string(), message: z.string(),