forked from tetherto/qvac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcancel.ts
More file actions
89 lines (85 loc) · 3.09 KB
/
Copy pathcancel.ts
File metadata and controls
89 lines (85 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { send } from "@/client/rpc/rpc-client";
import {
type CancelClientInput,
type CancelParams,
type CancelRequest,
} from "@/schemas";
import { InvalidResponseError, CancelFailedError } from "@/utils/errors-client";
/**
* Cancels an ongoing operation.
*
* Two cancel paths are supported:
*
* - **By `requestId`** (introduced in 0.11.0, primary path) — pass the
* `requestId` exposed on the result of a long-running call (e.g.
* `(await completion({ ... })).requestId`) to cancel exactly that
* request. Either pass `{ requestId }` directly or the explicit
* `{ operation: "request", requestId }` form; both are equivalent.
* The cancel takes effect once the server has begun the request; a
* cancel that races the originating call to the worker may arrive
* before the request is registered and is logged as a no-match.
* - **By `modelId`** (broad-cancel escape hatch, kept indefinitely) —
* `{ operation: "inference" | "embeddings", modelId }` cancels every
* in-flight request running on that model. Useful for model unload,
* app shutdown, or "cancel everything" admin paths where the caller
* doesn't have a `requestId` to hand.
*
* The download and RAG cancel paths are unchanged in 0.11.0; they still
* route through their own existing handlers.
*
* @param params - The parameters for the cancellation
* @throws {QvacErrorBase} When the response type is invalid or when the cancellation fails
*
* @example
* // Cancel a specific completion by requestId (new in 0.11.0)
* const run = completion({ ... });
* await cancel({ requestId: run.requestId });
*
* @example
* // Broad-cancel every inference running on a model (escape hatch)
* await cancel({ operation: "inference", modelId: "model-123" });
*
* @example
* // Pause download (preserves partial file for automatic resume)
* await cancel({ operation: "downloadAsset", downloadKey: "download-key" });
*
* @example
* // Cancel download completely (deletes partial file)
* await cancel({ operation: "downloadAsset", downloadKey: "download-key", clearCache: true });
*
* @example
* // Cancel delegated remote download
* await cancel({
* operation: "downloadAsset",
* downloadKey: "download-key",
* delegate: { providerPublicKey: "peerHex" },
* });
*
* @example
* // Cancel RAG operation on default workspace
* await cancel({ operation: "rag" });
*
* @example
* // Cancel RAG operation on specific workspace
* await cancel({ operation: "rag", workspace: "my-workspace" });
*/
export async function cancel(params: CancelClientInput) {
const wireParams = normalizeCancelParams(params);
const request: CancelRequest = {
type: "cancel",
...wireParams,
};
const response = await send(request);
if (response.type !== "cancel") {
throw new InvalidResponseError("cancel");
}
if (!response.success) {
throw new CancelFailedError(response.error);
}
}
function normalizeCancelParams(params: CancelClientInput): CancelParams {
if (!("operation" in params) && "requestId" in params) {
return { operation: "request", requestId: params.requestId };
}
return params;
}