@vibedev-si/dsh-vibedev 0.3.4 → 0.3.5
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/README.md +11 -0
- package/THIRD-PARTY-NOTICES.txt +1 -1
- package/client/client.js +720 -73
- package/lib/account/index.d.ts +21 -6
- package/lib/account/latency.d.ts +81 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +214 -15
- package/package.json +1 -1
package/lib/account/index.d.ts
CHANGED
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
*
|
|
6
6
|
* - `GET /api/dsh-vibedev/account` — where the credential comes from (the
|
|
7
7
|
* app's account, the plugin's own sign-in, a development key, or none), who
|
|
8
|
-
* is signed in, the balance
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
8
|
+
* is signed in, the balance (what can be spent, and what running tasks hold),
|
|
9
|
+
* when it was read, a sign-in waiting for the browser, and how many VibeDev
|
|
10
|
+
* models the pickers list; `?fresh=1` reads the balance again (a read that
|
|
11
|
+
* fails keeps the last balance, says when it was read, and says the read
|
|
12
|
+
* failed); `?since=<revision>` holds the request until the person or the
|
|
13
|
+
* balance changes (a charge is read again right after it ends), so the pages
|
|
14
|
+
* show a new balance within seconds instead of on their next minute poll;
|
|
13
15
|
* - `POST /api/dsh-vibedev/account/sign-in` — `{ open?: boolean }`: start the
|
|
14
16
|
* plugin's own sign-in; answers the authorization link (`open: false` leaves
|
|
15
17
|
* opening it to the page);
|
|
@@ -36,11 +38,20 @@ export interface AccountView {
|
|
|
36
38
|
readonly email?: string;
|
|
37
39
|
readonly nickname?: string;
|
|
38
40
|
};
|
|
39
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* The balance, when it could be read: `amount` is what can be spent (the gateway's `balance`), `frozen` what
|
|
43
|
+
* running tasks hold (`frozen_balance`: a video or batch-image task moves a hold there when it is submitted and
|
|
44
|
+
* settles it when it ends; absent when the gateway does not say), both in `currency`.
|
|
45
|
+
*/
|
|
40
46
|
readonly balance?: {
|
|
41
47
|
readonly amount: string;
|
|
42
48
|
readonly currency: string;
|
|
49
|
+
readonly frozen?: string;
|
|
43
50
|
};
|
|
51
|
+
/** When the balance shown was read from the gateway (epoch ms): the last read that succeeded. */
|
|
52
|
+
readonly readAt?: number;
|
|
53
|
+
/** The latest read of the balance failed (epoch ms), after `readAt`: the balance shown, if any, is the older one. */
|
|
54
|
+
readonly readFailedAt?: number;
|
|
44
55
|
/** A sign-in waiting for the browser. */
|
|
45
56
|
readonly pending?: {
|
|
46
57
|
readonly url: string;
|
|
@@ -102,6 +113,10 @@ export declare class AccountService {
|
|
|
102
113
|
private epoch;
|
|
103
114
|
/** When the last read after a charge started, to space them out. */
|
|
104
115
|
private lastSpendReadAt;
|
|
116
|
+
/** The last read that succeeded, kept for the pages while a later read fails. */
|
|
117
|
+
private lastGood;
|
|
118
|
+
/** The latest read failed, after the last one that succeeded. */
|
|
119
|
+
private failure;
|
|
105
120
|
/**
|
|
106
121
|
* @param options - origin, credential chain, the plugin's sign-in and the route's model state.
|
|
107
122
|
*/
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The network latency from this Host to the VibeDev gateway, for the balance
|
|
3
|
+
* card (`POST /api/dsh-vibedev/latency`; `?fresh=1` measures again within the
|
|
4
|
+
* minute a measurement is kept).
|
|
5
|
+
*
|
|
6
|
+
* It times `GET {origin}/health` — no credential, no database on the gateway,
|
|
7
|
+
* and left out of its access log — from the Host process, over the same fetch
|
|
8
|
+
* (and so the same network path and proxy settings) the plugin's gateway and
|
|
9
|
+
* model requests use: one request first, untimed, to set up the connection
|
|
10
|
+
* (DNS, TCP, TLS), then four in a row on that connection, each given 5 s. The
|
|
11
|
+
* answer is their median, each of them, the first request's time and when it
|
|
12
|
+
* ended; or why it stopped (no answer in time, no connection, an HTTP status).
|
|
13
|
+
* This is how far away the gateway is on the network, not how fast a model is.
|
|
14
|
+
*
|
|
15
|
+
* One measurement runs at a time, and a result is reused for a minute.
|
|
16
|
+
* @module dsh-vibedev/account/latency
|
|
17
|
+
*/
|
|
18
|
+
import type { ConnectionFetchRoute } from '@deepseek-ai/dsh-client-connection';
|
|
19
|
+
/** Path of the latency route. */
|
|
20
|
+
export declare const LATENCY_ROUTE = "/api/dsh-vibedev/latency";
|
|
21
|
+
/** Why a measurement stopped. */
|
|
22
|
+
export type LatencyErrorKind = 'timeout' | 'network' | 'http';
|
|
23
|
+
/** One measurement. Times are whole milliseconds. */
|
|
24
|
+
export interface LatencyResult {
|
|
25
|
+
/** Median of the timed requests, when every request answered. */
|
|
26
|
+
readonly medianMs?: number;
|
|
27
|
+
/** Each timed request, in order (those that answered before one failed, on a failure). */
|
|
28
|
+
readonly samplesMs: readonly number[];
|
|
29
|
+
/** The first, untimed request, connection setup included; absent when it failed. */
|
|
30
|
+
readonly warmupMs?: number;
|
|
31
|
+
/** When the measurement ended (epoch ms). */
|
|
32
|
+
readonly measuredAt: number;
|
|
33
|
+
/** Why it stopped: no answer within 5 s, no connection (or a redirect), or an HTTP status other than 2xx. */
|
|
34
|
+
readonly error?: {
|
|
35
|
+
readonly kind: LatencyErrorKind;
|
|
36
|
+
readonly status?: number;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** What the probe needs. */
|
|
40
|
+
export interface LatencyProbeOptions {
|
|
41
|
+
readonly origin: string;
|
|
42
|
+
readonly userAgent: string;
|
|
43
|
+
/** The fetch the plugin's gateway requests use (the global one unless a test passes its own). */
|
|
44
|
+
readonly fetch?: typeof globalThis.fetch;
|
|
45
|
+
/** Wall clock (epoch ms), for `measuredAt` and the minute a result is kept. */
|
|
46
|
+
readonly now?: () => number;
|
|
47
|
+
/** Monotonic clock (ms) one request is timed with. */
|
|
48
|
+
readonly clock?: () => number;
|
|
49
|
+
/** The per-request deadline (a test seam for `AbortSignal.timeout`). */
|
|
50
|
+
readonly timeout?: (ms: number) => AbortSignal;
|
|
51
|
+
/** The untimed pause before each timed request (a test seam). */
|
|
52
|
+
readonly pause?: () => Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
/** Measures the network latency to the gateway. */
|
|
55
|
+
export declare class LatencyProbe {
|
|
56
|
+
private readonly options;
|
|
57
|
+
private last;
|
|
58
|
+
private running;
|
|
59
|
+
private readonly now;
|
|
60
|
+
private readonly clock;
|
|
61
|
+
/**
|
|
62
|
+
* @param options - the gateway origin, the plugin's user agent, and the fetch and clock seams.
|
|
63
|
+
*/
|
|
64
|
+
constructor(options: LatencyProbeOptions);
|
|
65
|
+
/**
|
|
66
|
+
* The latency, measured now or within the last minute. One measurement runs at a time: a call while one runs
|
|
67
|
+
* shares it.
|
|
68
|
+
* @param fresh - measure again even when the last result is younger than a minute.
|
|
69
|
+
* @returns the result (a failure is a result too, with its error).
|
|
70
|
+
*/
|
|
71
|
+
measure(fresh?: boolean): Promise<LatencyResult>;
|
|
72
|
+
private run;
|
|
73
|
+
/** One `GET /health`, timed until its body has been read (which also lets the connection be reused). */
|
|
74
|
+
private once;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The latency route.
|
|
78
|
+
* @param probe - the probe.
|
|
79
|
+
* @returns routes for the client connection.
|
|
80
|
+
*/
|
|
81
|
+
export declare function latencyRoutes(probe: LatencyProbe): ConnectionFetchRoute[];
|
package/lib/index.d.ts
CHANGED
|
@@ -33,6 +33,8 @@ export { parseMediaCatalog } from './gateway/catalog.js';
|
|
|
33
33
|
export type { AudioCapabilities, AudioGenerationKind, MediaModel, PriceUnit, VideoCapabilities, VideoMode, VideoPricingIssue, VideoTokenPricing, VideoTokenRule, } from './gateway/catalog.js';
|
|
34
34
|
export { ACCOUNT_ROUTE_PREFIX, AccountService } from './account/index.js';
|
|
35
35
|
export type { AccountSource, AccountView } from './account/index.js';
|
|
36
|
+
export { LATENCY_ROUTE } from './account/latency.js';
|
|
37
|
+
export type { LatencyErrorKind, LatencyResult } from './account/latency.js';
|
|
36
38
|
export { GATEWAY_SEARCH_PROVIDER_ID as SEARCH_PROVIDER_ID } from './search/provider.js';
|
|
37
39
|
export { INSUFFICIENT_BALANCE_CODE, SIGN_IN_REQUIRED_CODE, VIBEDEV_ROUTE } from './llm/index.js';
|
|
38
40
|
/** The package version, sent in the `User-Agent` the gateway attributes plugin traffic by. */
|
package/lib/index.js
CHANGED
|
@@ -42,6 +42,25 @@ const SETTLE_READS_MS = [1500, 8e3];
|
|
|
42
42
|
const MIN_READ_SPACING_MS = 5e3;
|
|
43
43
|
const record$8 = (value) => typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
|
|
44
44
|
const text$8 = (value) => typeof value === "string" && value.trim() !== "" ? value.trim() : void 0;
|
|
45
|
+
/**
|
|
46
|
+
* A gateway amount as plain decimal text: a number as JavaScript writes it (`99976091.91426945`; the few numbers it
|
|
47
|
+
* writes with an exponent are spelled out), a string as sent; anything else, or text that is not a plain decimal, is
|
|
48
|
+
* no amount at all.
|
|
49
|
+
*/
|
|
50
|
+
function amountOf(value) {
|
|
51
|
+
let raw;
|
|
52
|
+
if (typeof value === "number") {
|
|
53
|
+
if (!Number.isFinite(value)) return void 0;
|
|
54
|
+
raw = String(value);
|
|
55
|
+
if (raw.includes("e")) raw = Math.abs(value) >= 1 ? BigInt(value).toString() : value.toFixed(20).replace(/0+$/, "").replace(/\.$/, "");
|
|
56
|
+
} else raw = text$8(value);
|
|
57
|
+
return raw !== void 0 && /^-?\d+(\.\d+)?$/.test(raw) ? raw : void 0;
|
|
58
|
+
}
|
|
59
|
+
/** What a read showed, for telling whether the next one changed anything (when it was read is not a change). */
|
|
60
|
+
const seenOf = (read) => JSON.stringify({
|
|
61
|
+
balance: read.balance,
|
|
62
|
+
user: read.user
|
|
63
|
+
});
|
|
45
64
|
/** The account state behind the pages. */
|
|
46
65
|
var AccountService = class {
|
|
47
66
|
options;
|
|
@@ -58,6 +77,10 @@ var AccountService = class {
|
|
|
58
77
|
epoch = 0;
|
|
59
78
|
/** When the last read after a charge started, to space them out. */
|
|
60
79
|
lastSpendReadAt = Number.NEGATIVE_INFINITY;
|
|
80
|
+
/** The last read that succeeded, kept for the pages while a later read fails. */
|
|
81
|
+
lastGood;
|
|
82
|
+
/** The latest read failed, after the last one that succeeded. */
|
|
83
|
+
failure;
|
|
61
84
|
/**
|
|
62
85
|
* @param options - origin, credential chain, the plugin's sign-in and the route's model state.
|
|
63
86
|
*/
|
|
@@ -90,6 +113,8 @@ var AccountService = class {
|
|
|
90
113
|
this.balanceCache = void 0;
|
|
91
114
|
this.revalidating = void 0;
|
|
92
115
|
this.lastSeen = void 0;
|
|
116
|
+
this.lastGood = void 0;
|
|
117
|
+
this.failure = void 0;
|
|
93
118
|
this.changed();
|
|
94
119
|
}
|
|
95
120
|
/**
|
|
@@ -163,12 +188,17 @@ var AccountService = class {
|
|
|
163
188
|
...base,
|
|
164
189
|
revision: this.revision
|
|
165
190
|
};
|
|
166
|
-
const [grantUser,
|
|
191
|
+
const [grantUser, read] = await Promise.all([credential.kind === "plugin" ? this.options.login.user().catch(() => void 0) : Promise.resolve(void 0), this.me(credential.token, fresh)]);
|
|
192
|
+
const good = this.lastGood?.token === credential.token ? this.lastGood.read : void 0;
|
|
193
|
+
const me = read.balance === void 0 && good !== void 0 ? good : read;
|
|
167
194
|
const user = me.user ?? userOf(grantUser);
|
|
195
|
+
const failedAt = this.failure?.token === credential.token ? this.failure.at : void 0;
|
|
168
196
|
return {
|
|
169
197
|
...base,
|
|
170
198
|
...user === void 0 ? {} : { user },
|
|
171
199
|
...me.balance === void 0 ? {} : { balance: me.balance },
|
|
200
|
+
...me.readAt === void 0 ? {} : { readAt: me.readAt },
|
|
201
|
+
...failedAt === void 0 ? {} : { readFailedAt: failedAt },
|
|
172
202
|
revision: this.revision
|
|
173
203
|
};
|
|
174
204
|
}
|
|
@@ -206,8 +236,9 @@ var AccountService = class {
|
|
|
206
236
|
if (!response.ok) return {};
|
|
207
237
|
const body = record$8(await response.json().catch(() => void 0));
|
|
208
238
|
const data = record$8(body?.data) ?? body;
|
|
209
|
-
const balance =
|
|
239
|
+
const balance = amountOf(data?.balance);
|
|
210
240
|
const currency = text$8(data?.currency) ?? "CNY";
|
|
241
|
+
const frozen = amountOf(data?.frozen_balance);
|
|
211
242
|
const id = typeof data?.id === "number" ? String(data.id) : text$8(data?.id);
|
|
212
243
|
const nickname = text$8(data?.nickname) ?? text$8(data?.username);
|
|
213
244
|
const email = text$8(data?.email);
|
|
@@ -217,10 +248,14 @@ var AccountService = class {
|
|
|
217
248
|
...nickname === void 0 ? {} : { nickname }
|
|
218
249
|
};
|
|
219
250
|
return {
|
|
220
|
-
...balance === void 0
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
251
|
+
...balance === void 0 ? {} : {
|
|
252
|
+
balance: {
|
|
253
|
+
amount: balance,
|
|
254
|
+
currency,
|
|
255
|
+
...frozen === void 0 ? {} : { frozen }
|
|
256
|
+
},
|
|
257
|
+
readAt: this.now()
|
|
258
|
+
},
|
|
224
259
|
...user === void 0 ? {} : { user }
|
|
225
260
|
};
|
|
226
261
|
} catch {
|
|
@@ -237,6 +272,10 @@ var AccountService = class {
|
|
|
237
272
|
if (epoch !== this.epoch) return;
|
|
238
273
|
if (read.balance === void 0) {
|
|
239
274
|
if (this.balanceCache?.value === value) this.balanceCache = void 0;
|
|
275
|
+
this.failure = {
|
|
276
|
+
token,
|
|
277
|
+
at: this.now()
|
|
278
|
+
};
|
|
240
279
|
return;
|
|
241
280
|
}
|
|
242
281
|
this.balanceCache = {
|
|
@@ -244,7 +283,12 @@ var AccountService = class {
|
|
|
244
283
|
at: this.now(),
|
|
245
284
|
value
|
|
246
285
|
};
|
|
247
|
-
|
|
286
|
+
this.lastGood = {
|
|
287
|
+
token,
|
|
288
|
+
read
|
|
289
|
+
};
|
|
290
|
+
this.failure = void 0;
|
|
291
|
+
const seen = seenOf(read);
|
|
248
292
|
if (this.lastSeen !== void 0 && this.lastSeen !== seen) {
|
|
249
293
|
this.lastSeen = seen;
|
|
250
294
|
this.changed();
|
|
@@ -298,7 +342,7 @@ function userOf(user) {
|
|
|
298
342
|
...nickname === void 0 ? {} : { nickname }
|
|
299
343
|
};
|
|
300
344
|
}
|
|
301
|
-
function json(status, body) {
|
|
345
|
+
function json$1(status, body) {
|
|
302
346
|
return new Response(JSON.stringify(body), {
|
|
303
347
|
status,
|
|
304
348
|
headers: {
|
|
@@ -310,7 +354,7 @@ function json(status, body) {
|
|
|
310
354
|
function failure(error) {
|
|
311
355
|
const candidate = record$8(error)?.code;
|
|
312
356
|
const code = candidate === "SIGN_IN_REFUSED" || candidate === "SIGN_IN_TIMEOUT" || candidate === "SIGN_IN_CANCELLED" ? candidate : "INTERNAL";
|
|
313
|
-
return json(500, { error: {
|
|
357
|
+
return json$1(500, { error: {
|
|
314
358
|
code,
|
|
315
359
|
message: code === "SIGN_IN_CANCELLED" ? "Sign-in was cancelled. 登录已取消。" : code === "SIGN_IN_TIMEOUT" ? "Sign-in expired. 登录已超时,请重新登录。" : "The account request could not be completed. 账号请求未完成,请查看登录诊断后重试。"
|
|
316
360
|
} });
|
|
@@ -331,7 +375,7 @@ function accountRoutes(account) {
|
|
|
331
375
|
const query = new URL(request.url, "http://host").searchParams;
|
|
332
376
|
const since = query.get("since");
|
|
333
377
|
if (since !== null && /^\d{1,15}$/.test(since)) await account.wait(Number(since), request.signal);
|
|
334
|
-
return json(200, await account.view(query.get("fresh") === "1"));
|
|
378
|
+
return json$1(200, await account.view(query.get("fresh") === "1"));
|
|
335
379
|
} catch (error) {
|
|
336
380
|
return failure(error);
|
|
337
381
|
}
|
|
@@ -344,7 +388,7 @@ function accountRoutes(account) {
|
|
|
344
388
|
fetch: async (request) => {
|
|
345
389
|
try {
|
|
346
390
|
const body = record$8(await request.json().catch(() => void 0));
|
|
347
|
-
return json(200, await account.signIn(body?.open !== false));
|
|
391
|
+
return json$1(200, await account.signIn(body?.open !== false));
|
|
348
392
|
} catch (error) {
|
|
349
393
|
return failure(error);
|
|
350
394
|
}
|
|
@@ -356,7 +400,7 @@ function accountRoutes(account) {
|
|
|
356
400
|
requestBody: "buffered",
|
|
357
401
|
fetch: () => {
|
|
358
402
|
account.cancel();
|
|
359
|
-
return Promise.resolve(json(200, { ok: true }));
|
|
403
|
+
return Promise.resolve(json$1(200, { ok: true }));
|
|
360
404
|
}
|
|
361
405
|
},
|
|
362
406
|
{
|
|
@@ -365,7 +409,7 @@ function accountRoutes(account) {
|
|
|
365
409
|
requestBody: "buffered",
|
|
366
410
|
fetch: async () => {
|
|
367
411
|
try {
|
|
368
|
-
return await account.signOut() ? json(200, { ok: true }) : json(409, { error: {
|
|
412
|
+
return await account.signOut() ? json$1(200, { ok: true }) : json$1(409, { error: {
|
|
369
413
|
code: "SIGNED_IN_BY_APP",
|
|
370
414
|
message: "The VibeDev account is the app's sign-in; sign out in the app."
|
|
371
415
|
} });
|
|
@@ -377,6 +421,157 @@ function accountRoutes(account) {
|
|
|
377
421
|
];
|
|
378
422
|
}
|
|
379
423
|
//#endregion
|
|
424
|
+
//#region src/account/latency.ts
|
|
425
|
+
/** Path of the latency route. */
|
|
426
|
+
const LATENCY_ROUTE = "/api/dsh-vibedev/latency";
|
|
427
|
+
/** Timed requests after the first one. */
|
|
428
|
+
const TIMED_REQUESTS = 4;
|
|
429
|
+
/** Each request gets this long, its body included. */
|
|
430
|
+
const REQUEST_TIMEOUT_MS = 5e3;
|
|
431
|
+
/** A result is answered again for this long; `fresh` measures anyway. */
|
|
432
|
+
const RESULT_TTL_MS = 6e4;
|
|
433
|
+
/** One turn of the event loop, after which the previous request's connection is back in the pool. */
|
|
434
|
+
const pauseBriefly = () => new Promise((resolve) => {
|
|
435
|
+
setTimeout(resolve, 0);
|
|
436
|
+
});
|
|
437
|
+
var StatusError = class extends Error {
|
|
438
|
+
status;
|
|
439
|
+
constructor(status) {
|
|
440
|
+
super(`HTTP ${status}`);
|
|
441
|
+
this.status = status;
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
/**
|
|
445
|
+
* What stopped a request.
|
|
446
|
+
* @param error - what the request threw.
|
|
447
|
+
* @returns the error kind (and the status, for an HTTP answer).
|
|
448
|
+
*/
|
|
449
|
+
function errorOf$1(error) {
|
|
450
|
+
if (error instanceof StatusError) return {
|
|
451
|
+
kind: "http",
|
|
452
|
+
status: error.status
|
|
453
|
+
};
|
|
454
|
+
const name = error?.name;
|
|
455
|
+
const code = error?.cause?.code;
|
|
456
|
+
if (name === "TimeoutError" || name === "AbortError" || code === "UND_ERR_CONNECT_TIMEOUT" || code === "UND_ERR_HEADERS_TIMEOUT" || code === "UND_ERR_BODY_TIMEOUT" || code === "ETIMEDOUT") return { kind: "timeout" };
|
|
457
|
+
return { kind: "network" };
|
|
458
|
+
}
|
|
459
|
+
/** The middle value; for an even count, the mean of the two middle ones. */
|
|
460
|
+
function median(values) {
|
|
461
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
462
|
+
const upper = sorted[Math.floor(sorted.length / 2)] ?? 0;
|
|
463
|
+
return sorted.length % 2 === 1 ? upper : ((sorted[sorted.length / 2 - 1] ?? upper) + upper) / 2;
|
|
464
|
+
}
|
|
465
|
+
/** Measures the network latency to the gateway. */
|
|
466
|
+
var LatencyProbe = class {
|
|
467
|
+
options;
|
|
468
|
+
last;
|
|
469
|
+
running;
|
|
470
|
+
now;
|
|
471
|
+
clock;
|
|
472
|
+
/**
|
|
473
|
+
* @param options - the gateway origin, the plugin's user agent, and the fetch and clock seams.
|
|
474
|
+
*/
|
|
475
|
+
constructor(options) {
|
|
476
|
+
this.options = options;
|
|
477
|
+
this.now = options.now ?? Date.now;
|
|
478
|
+
this.clock = options.clock ?? (() => performance.now());
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* The latency, measured now or within the last minute. One measurement runs at a time: a call while one runs
|
|
482
|
+
* shares it.
|
|
483
|
+
* @param fresh - measure again even when the last result is younger than a minute.
|
|
484
|
+
* @returns the result (a failure is a result too, with its error).
|
|
485
|
+
*/
|
|
486
|
+
measure(fresh = false) {
|
|
487
|
+
if (this.running !== void 0) return this.running;
|
|
488
|
+
const last = this.last;
|
|
489
|
+
if (!fresh && last !== void 0 && this.now() - last.measuredAt < RESULT_TTL_MS) return Promise.resolve(last);
|
|
490
|
+
const running = this.run().then((result) => {
|
|
491
|
+
this.last = result;
|
|
492
|
+
return result;
|
|
493
|
+
}).finally(() => {
|
|
494
|
+
this.running = void 0;
|
|
495
|
+
});
|
|
496
|
+
this.running = running;
|
|
497
|
+
return running;
|
|
498
|
+
}
|
|
499
|
+
async run() {
|
|
500
|
+
const samples = [];
|
|
501
|
+
let warmup;
|
|
502
|
+
try {
|
|
503
|
+
warmup = await this.once();
|
|
504
|
+
for (let index = 0; index < TIMED_REQUESTS; index++) {
|
|
505
|
+
await (this.options.pause ?? pauseBriefly)();
|
|
506
|
+
samples.push(await this.once());
|
|
507
|
+
}
|
|
508
|
+
} catch (error) {
|
|
509
|
+
return {
|
|
510
|
+
samplesMs: samples,
|
|
511
|
+
...warmup === void 0 ? {} : { warmupMs: warmup },
|
|
512
|
+
measuredAt: this.now(),
|
|
513
|
+
error: errorOf$1(error)
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
return {
|
|
517
|
+
medianMs: Math.round(median(samples)),
|
|
518
|
+
samplesMs: samples,
|
|
519
|
+
warmupMs: warmup,
|
|
520
|
+
measuredAt: this.now()
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
/** One `GET /health`, timed until its body has been read (which also lets the connection be reused). */
|
|
524
|
+
async once() {
|
|
525
|
+
const init = {
|
|
526
|
+
method: "GET",
|
|
527
|
+
headers: {
|
|
528
|
+
accept: "application/json",
|
|
529
|
+
"user-agent": this.options.userAgent
|
|
530
|
+
},
|
|
531
|
+
cache: "no-store",
|
|
532
|
+
redirect: "error",
|
|
533
|
+
signal: (this.options.timeout ?? ((ms) => AbortSignal.timeout(ms)))(REQUEST_TIMEOUT_MS)
|
|
534
|
+
};
|
|
535
|
+
const started = this.clock();
|
|
536
|
+
const response = await (this.options.fetch ?? globalThis.fetch)(`${this.options.origin}/health`, init);
|
|
537
|
+
await response.arrayBuffer();
|
|
538
|
+
const elapsed = Math.round(this.clock() - started);
|
|
539
|
+
if (!response.ok) throw new StatusError(response.status);
|
|
540
|
+
return elapsed;
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
function json(status, body) {
|
|
544
|
+
return new Response(JSON.stringify(body), {
|
|
545
|
+
status,
|
|
546
|
+
headers: {
|
|
547
|
+
"content-type": "application/json; charset=utf-8",
|
|
548
|
+
"cache-control": "no-store"
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* The latency route.
|
|
554
|
+
* @param probe - the probe.
|
|
555
|
+
* @returns routes for the client connection.
|
|
556
|
+
*/
|
|
557
|
+
function latencyRoutes(probe) {
|
|
558
|
+
return [{
|
|
559
|
+
path: LATENCY_ROUTE,
|
|
560
|
+
methods: ["POST"],
|
|
561
|
+
requestBody: "buffered",
|
|
562
|
+
fetch: async (request) => {
|
|
563
|
+
try {
|
|
564
|
+
return json(200, await probe.measure(new URL(request.url, "http://host").searchParams.get("fresh") === "1"));
|
|
565
|
+
} catch {
|
|
566
|
+
return json(500, { error: {
|
|
567
|
+
code: "INTERNAL",
|
|
568
|
+
message: "The latency could not be measured. 网络延迟没有测成,请稍后重试。"
|
|
569
|
+
} });
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
}];
|
|
573
|
+
}
|
|
574
|
+
//#endregion
|
|
380
575
|
//#region src/gateway/errors.ts
|
|
381
576
|
/**
|
|
382
577
|
* Failures the plugin reports to the model: a gateway refusal, or a request the
|
|
@@ -9397,8 +9592,12 @@ function apply(ctx, config) {
|
|
|
9397
9592
|
})
|
|
9398
9593
|
});
|
|
9399
9594
|
account = accountService;
|
|
9595
|
+
const latency = new LatencyProbe({
|
|
9596
|
+
origin,
|
|
9597
|
+
userAgent
|
|
9598
|
+
});
|
|
9400
9599
|
ctx.inject(["connection"], (scoped) => {
|
|
9401
|
-
for (const accountRoute of accountRoutes(accountService)) scoped.effect(() => scoped.connection.fetch.register(accountRoute), `dsh-vibedev: ${accountRoute.path}`);
|
|
9600
|
+
for (const accountRoute of [...accountRoutes(accountService), ...latencyRoutes(latency)]) scoped.effect(() => scoped.connection.fetch.register(accountRoute), `dsh-vibedev: ${accountRoute.path}`);
|
|
9402
9601
|
});
|
|
9403
9602
|
ctx.effect(() => {
|
|
9404
9603
|
tracker.resume().catch((error) => {
|
|
@@ -9433,6 +9632,6 @@ function apply(ctx, config) {
|
|
|
9433
9632
|
});
|
|
9434
9633
|
}
|
|
9435
9634
|
//#endregion
|
|
9436
|
-
export { ACCOUNT_ROUTE_PREFIX, AccountService, Config, INSUFFICIENT_BALANCE_CODE, MediaError, MediaHostService, GATEWAY_SEARCH_PROVIDER_ID as SEARCH_PROVIDER_ID, SIGN_IN_REQUIRED_CODE, VIBEDEV_ROUTE, apply, inject, name, parseMediaCatalog, version };
|
|
9635
|
+
export { ACCOUNT_ROUTE_PREFIX, AccountService, Config, INSUFFICIENT_BALANCE_CODE, LATENCY_ROUTE, MediaError, MediaHostService, GATEWAY_SEARCH_PROVIDER_ID as SEARCH_PROVIDER_ID, SIGN_IN_REQUIRED_CODE, VIBEDEV_ROUTE, apply, inject, name, parseMediaCatalog, version };
|
|
9437
9636
|
|
|
9438
9637
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vibedev-si/dsh-vibedev",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.5",
|
|
4
4
|
"description": "VibeDev 账号与模型:在 DeepSeek Harness 里登录 VibeDev,与 DeepSeek 账号并存,使用 VibeDev 网关的模型(Claude、GPT 等),并生成图片、视频、音乐和播客 · VibeDev account and models for DeepSeek Harness: sign in to VibeDev next to the DeepSeek account and use the VibeDev gateway's models (Claude, GPT and more), plus image, video, music and podcast generation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|