mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-12 00:49:14 +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(
|
public async updateSession(agentId: string, session: UpdateSessionForm): Promise<UpdateSessionResponse> {
|
||||||
agentId: string,
|
const url = this.getSessionPaths(agentId).withId(session.id)
|
||||||
sessionId: string,
|
|
||||||
session: UpdateSessionForm
|
|
||||||
): Promise<UpdateSessionResponse> {
|
|
||||||
const url = this.getSessionPaths(agentId).withId(sessionId)
|
|
||||||
try {
|
try {
|
||||||
const payload = session satisfies UpdateSessionRequest
|
const payload = session satisfies UpdateSessionRequest
|
||||||
const response = await this.axios.patch(url, payload)
|
const response = await this.axios.patch(url, payload)
|
||||||
const data = UpdateSessionResponseSchema.parse(response.data)
|
const data = UpdateSessionResponseSchema.parse(response.data)
|
||||||
if (sessionId !== data.id) {
|
if (session.id !== data.id) {
|
||||||
throw new Error('Session ID mismatch in response')
|
throw new Error('Session ID mismatch in response')
|
||||||
}
|
}
|
||||||
return data
|
return data
|
||||||
|
|||||||
@ -57,11 +57,11 @@ export const useSessions = (agentId: string) => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
const updateSession = useCallback(
|
const updateSession = useCallback(
|
||||||
async (id: string, form: UpdateSessionForm) => {
|
async (form: UpdateSessionForm) => {
|
||||||
if (!agentId) return
|
if (!agentId) return
|
||||||
try {
|
try {
|
||||||
const result = await client.updateSession(agentId, id, form)
|
const result = await client.updateSession(agentId, form)
|
||||||
mutate((prev) => prev?.map((session) => (session.id === id ? result : session)))
|
mutate((prev) => prev?.map((session) => (session.id === form.id ? result : session)))
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
window.toast.error(t('agent.session.update.error.failed'))
|
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 UpdateSessionForm = Partial<BaseSessionForm> & { id: string }
|
||||||
|
|
||||||
|
export type SessionForm = CreateSessionForm | UpdateSessionForm
|
||||||
|
|
||||||
// ------------------ API data transfer objects ------------------
|
// ------------------ API data transfer objects ------------------
|
||||||
export interface CreateAgentRequest extends AgentBase {
|
export interface CreateAgentRequest extends AgentBase {
|
||||||
type: AgentType
|
type: AgentType
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user