mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-06 05:09:09 +08:00
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:
parent
d3378dcf78
commit
432d84cda5
@ -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
|
||||
|
||||
@ -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'))
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user