@aidenhq/sdk 0.4.3 → 0.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/paperclip.js +37 -1
- package/package.json +1 -1
package/dist/src/paperclip.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const RESTART_WAIT_MS = 20_000;
|
|
1
2
|
export class PaperclipError extends Error {
|
|
2
3
|
status;
|
|
3
4
|
body;
|
|
@@ -22,12 +23,47 @@ function errorMessage(text) {
|
|
|
22
23
|
export function paperclipClient({ url, token, lane, headers, timeoutMs = 20_000, fetcher = fetch }) {
|
|
23
24
|
const prefix = (method) => (lane === "owner" ? (method === "GET" ? "/v1/cp/read" : "/v1/cp/owner") : lane === "agent" ? "/v1/cp/agent" : "");
|
|
24
25
|
/** The raw response, for a body that isn't JSON (an upload's form, a file's bytes). */
|
|
25
|
-
const
|
|
26
|
+
const once = (method, path, init) => fetcher(new URL(`${prefix(method)}${path}`, url), {
|
|
26
27
|
method,
|
|
27
28
|
headers: { authorization: `Bearer ${token}`, accept: "application/json", ...(init.type ? { "content-type": init.type } : {}), ...headers?.(method) },
|
|
28
29
|
body: init.body,
|
|
29
30
|
signal: AbortSignal.timeout(init.timeoutMs ?? timeoutMs),
|
|
30
31
|
});
|
|
32
|
+
// A read rides out a Paperclip restart (an apply): a 503 or a refused connection is retried for up to RESTART_WAIT_MS,
|
|
33
|
+
// honouring retry-after. A write is sent once. The caller's own timeout is the first attempt's; each retry's attempt is bounded
|
|
34
|
+
// by what is left of the deadline, so a stalled request can't outlast it.
|
|
35
|
+
const send = async (method, path, init = {}) => {
|
|
36
|
+
const until = Date.now() + RESTART_WAIT_MS;
|
|
37
|
+
for (let attempt = init.timeoutMs;;) {
|
|
38
|
+
let res, down;
|
|
39
|
+
try {
|
|
40
|
+
res = await once(method, path, attempt === undefined ? init : { ...init, timeoutMs: attempt });
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
if (method !== "GET" || !(e instanceof TypeError))
|
|
44
|
+
throw e;
|
|
45
|
+
down = e;
|
|
46
|
+
}
|
|
47
|
+
if (res && (res.status !== 503 || method !== "GET"))
|
|
48
|
+
return res;
|
|
49
|
+
const ra = Number(res?.headers.get("retry-after") ?? "");
|
|
50
|
+
const wait = Math.max(200, (res?.headers.get("retry-after") && ra >= 0 ? ra : 1) * 1000);
|
|
51
|
+
if (Date.now() + wait >= until) {
|
|
52
|
+
if (res)
|
|
53
|
+
return res;
|
|
54
|
+
throw down;
|
|
55
|
+
}
|
|
56
|
+
await res?.body?.cancel();
|
|
57
|
+
await new Promise((r) => setTimeout(r, wait));
|
|
58
|
+
const left = until - Date.now();
|
|
59
|
+
if (left <= 0) {
|
|
60
|
+
if (res)
|
|
61
|
+
return res;
|
|
62
|
+
throw down;
|
|
63
|
+
}
|
|
64
|
+
attempt = Math.min(init.timeoutMs ?? timeoutMs, left);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
31
67
|
const fail = async (res, method, path) => {
|
|
32
68
|
const text = await res.text();
|
|
33
69
|
let body;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aidenhq/sdk",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
4
4
|
"description": "The shared code of AidenHQ modules: contract types, engine IO, the caller check and fence, write executors, the reviewer side, the judges' models and call, the source writer and the vendor MCP pass-through.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|