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.
This commit is contained in:
icarus 2025-09-19 15:08:53 +08:00
parent d3378dcf78
commit 432d84cda5
3 changed files with 8 additions and 10 deletions

View File

@ -191,17 +191,13 @@ export class AgentApiClient {
}
}
public async updateSession(
agentId: string,
sessionId: string,
session: UpdateSessionForm
): Promise<UpdateSessionResponse> {
const url = this.getSessionPaths(agentId).withId(sessionId)
public async updateSession(agentId: string, session: UpdateSessionForm): Promise<UpdateSessionResponse> {
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

View File

@ -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'))
}

View File

@ -154,6 +154,8 @@ export type CreateSessionForm = BaseSessionForm & { id?: never }
export type UpdateSessionForm = Partial<BaseSessionForm> & { id: string }
export type SessionForm = CreateSessionForm | UpdateSessionForm
// ------------------ API data transfer objects ------------------
export interface CreateAgentRequest extends AgentBase {
type: AgentType