|
1 | 1 | import type { HandleMessageStreamEvent } from "#protocol/message.js"; |
2 | 2 | import { EVE_SESSION_ID_HEADER, isCurrentTurnBoundaryEvent } from "#protocol/message.js"; |
| 3 | +import { CancelTurnResponseSchema } from "#protocol/cancel-turn.js"; |
3 | 4 | import { |
4 | 5 | EVE_CREATE_SESSION_ROUTE_PATH, |
5 | 6 | createEveCancelTurnRoutePath, |
@@ -91,7 +92,7 @@ export class ClientSession { |
91 | 92 | /** |
92 | 93 | * Requests cooperative cancellation of this session's active turn. |
93 | 94 | * |
94 | | - * Both `cancelling` and `no_active_turn` are successful outcomes. The latter |
| 95 | + * Both `accepted` and `no_active_turn` are successful outcomes. The latter |
95 | 96 | * means the active turn settled before the request arrived or the session is |
96 | 97 | * already parked. Credentials are resolved immediately before the request. |
97 | 98 | * |
@@ -123,12 +124,12 @@ export class ClientSession { |
123 | 124 | throw new Error(`Cancel route returned invalid JSON (${response.status}).`); |
124 | 125 | } |
125 | 126 |
|
126 | | - const status = readCancelStatus(payload); |
127 | | - if (status === undefined) { |
| 127 | + const result = CancelTurnResponseSchema.safeParse(payload); |
| 128 | + if (!result.success || result.data.sessionId !== sessionId) { |
128 | 129 | throw new Error(`Cancel route returned an invalid response (${response.status}).`); |
129 | 130 | } |
130 | 131 |
|
131 | | - return { sessionId, status }; |
| 132 | + return { sessionId: result.data.sessionId, status: result.data.status }; |
132 | 133 | } |
133 | 134 |
|
134 | 135 | /** |
@@ -427,12 +428,6 @@ function createHandleMessageBody(input: { |
427 | 428 | return body; |
428 | 429 | } |
429 | 430 |
|
430 | | -function readCancelStatus(value: unknown): CancelSessionResult["status"] | undefined { |
431 | | - if (typeof value !== "object" || value === null) return undefined; |
432 | | - const status = (value as { readonly status?: unknown }).status; |
433 | | - return status === "cancelling" || status === "no_active_turn" ? status : undefined; |
434 | | -} |
435 | | - |
436 | 431 | function withRedirectPolicy(init: RequestInit, redirect?: ClientRedirectPolicy): RequestInit { |
437 | 432 | return redirect === undefined ? init : { ...init, redirect }; |
438 | 433 | } |
0 commit comments