dsh-agy 0.2.3 → 0.2.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 +21 -1
- package/lib/{accounts-BvX5zmPa.mjs → accounts-Blqno5Te.mjs} +102 -44
- package/lib/accounts-Blqno5Te.mjs.map +1 -0
- package/lib/catalog-DVHPJjfc.mjs +199 -0
- package/lib/catalog-DVHPJjfc.mjs.map +1 -0
- package/lib/cli/index.d.mts.map +1 -1
- package/lib/cli/index.mjs +115 -11
- package/lib/cli/index.mjs.map +1 -1
- package/lib/client.d.ts +8 -0
- package/lib/client.js +36 -0
- package/lib/{exchange-CteHnRzO.mjs → exchange-C6y05v4v.mjs} +3 -2
- package/lib/{exchange-CteHnRzO.mjs.map → exchange-C6y05v4v.mjs.map} +1 -1
- package/lib/{import-C7EV_tne.mjs → import-Dmeiu2Xb.mjs} +19 -5
- package/lib/import-Dmeiu2Xb.mjs.map +1 -0
- package/lib/index.mjs +2 -2
- package/lib/models-3VFg0nRK.mjs +137 -0
- package/lib/models-3VFg0nRK.mjs.map +1 -0
- package/lib/{plugin-common-Bmz4Ife-.mjs → plugin-common-CP6Czgh9.mjs} +46 -6
- package/lib/plugin-common-CP6Czgh9.mjs.map +1 -0
- package/lib/proxy-DQPIwUov.mjs +269 -0
- package/lib/proxy-DQPIwUov.mjs.map +1 -0
- package/lib/{translate-C1SMdm5J.mjs → translate-Zf1mdBri.mjs} +27 -6
- package/lib/translate-Zf1mdBri.mjs.map +1 -0
- package/lib/web/plugin.mjs +182 -7
- package/lib/web/plugin.mjs.map +1 -1
- package/package.json +20 -1
- package/lib/accounts-BvX5zmPa.mjs.map +0 -1
- package/lib/import-C7EV_tne.mjs.map +0 -1
- package/lib/models-I1UAEk9x.mjs +0 -222
- package/lib/models-I1UAEk9x.mjs.map +0 -1
- package/lib/plugin-common-Bmz4Ife-.mjs.map +0 -1
- package/lib/translate-C1SMdm5J.mjs.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"import-Dmeiu2Xb.mjs","names":[],"sources":["../src/oauth/authorize.ts","../src/cli/import.ts"],"sourcesContent":["/** Build the Antigravity OAuth authorization URL with PKCE and project metadata. */\n\nimport {\n AGY_SCOPES,\n OAUTH_AUTHORIZE_URL,\n resolveAgyClientCredentials,\n} from './constants.ts'\nimport { encodeState, generatePkcePair } from './pkce.ts'\n\nexport interface AntigravityAuthorization {\n url: string\n verifier: string\n projectId: string\n /** Raw state payload embedded in the URL (for server-side binding). */\n state: string\n}\n\n/**\n * Build the Google OAuth authorization URL for the Antigravity client.\n * The `state` payload carries the PKCE verifier and an optional project id so a\n * headless callback can be pasted back verbatim.\n * @param redirectUri - loopback callback URI (harness webServer route or CLI listener).\n * @param projectId - optional Antigravity project id to bind this login to.\n */\nexport async function authorizeAntigravity(\n redirectUri: string,\n projectId = '',\n): Promise<AntigravityAuthorization> {\n const { verifier, challenge } = generatePkcePair()\n\n const url = new URL(OAUTH_AUTHORIZE_URL)\n const { clientId } = resolveAgyClientCredentials()\n url.searchParams.set('client_id', clientId)\n url.searchParams.set('response_type', 'code')\n url.searchParams.set('redirect_uri', redirectUri)\n url.searchParams.set('scope', AGY_SCOPES.join(' '))\n url.searchParams.set('code_challenge', challenge)\n url.searchParams.set('code_challenge_method', 'S256')\n const state = encodeState({ verifier, projectId: projectId || '' })\n url.searchParams.set('state', state)\n url.searchParams.set('access_type', 'offline')\n url.searchParams.set('prompt', 'consent')\n\n return { url: url.toString(), verifier, projectId: projectId || '', state }\n}\n","/**\n * Import agy credentials: parse the `agy` CLI auth.json token file or a\n * paste credential blob, enrich with the Code Assist backend (email/project),\n * and persist into the account store. Adapted from OmniRoute's agyAuthImport\n * (MIT, see NOTICE.md).\n */\nimport { randomUUID } from 'node:crypto'\nimport { AGY_ENDPOINT_FALLBACKS, getAgyBootstrapClientMetadata, getAgyBootstrapUserAgent } from '../oauth/constants.ts'\nimport { decodeCredentialBlob } from '../oauth/blob.ts'\nimport { normalizeProxyUrl, proxiedFetch } from '../proxy.ts'\nimport type { AccountStore } from '../store/accounts.ts'\nimport type { ManagedAccount } from '../types.ts'\n\nexport class AgyAuthFileError extends Error {\n status: number\n code: string\n\n constructor(message: string, status = 400, code = 'invalid_request') {\n super(message)\n this.name = 'AgyAuthFileError'\n this.status = status\n this.code = code\n }\n}\n\nexport interface ParsedAgyAuth {\n accessToken: string\n refreshToken: string\n tokenType: string\n expiresAt: string | null\n authMethod: string | null\n clientId?: string | null\n}\n\nexport interface EnrichedAgyAuth extends ParsedAgyAuth {\n email: string | null\n projectId: string | null\n clientId?: string | null\n}\nfunction toRecord(value: unknown): Record<string, unknown> {\n return value && typeof value === 'object' && !Array.isArray(value) ? (value as Record<string, unknown>) : {}\n}\n\nfunction toNonEmptyString(value: unknown): string | null {\n if (typeof value !== 'string') return null\n const trimmed = value.trim()\n return trimmed ? trimmed : null\n}\n\n/**\n * Parse the agy CLI token file: tokens nest under `.token` with an ISO\n * `expiry` string and no id_token; a flat top-level shape is accepted too.\n */\nexport function parseAndValidateAgyToken(raw: unknown): ParsedAgyAuth {\n const doc = toRecord(raw)\n const token = toRecord(doc.token ?? doc)\n\n const accessToken = toNonEmptyString(token.access_token)\n const refreshToken = toNonEmptyString(token.refresh_token)\n if (!accessToken) throw new AgyAuthFileError('access_token is missing or empty in the agy token file', 400, 'missing_access_token')\n if (!refreshToken) throw new AgyAuthFileError('refresh_token is missing or empty in the agy token file', 400, 'missing_refresh_token')\n\n let expiresAt: string | null = null\n const isoExpiry = toNonEmptyString(token.expiry) ?? toNonEmptyString(token.expires_at)\n if (isoExpiry) {\n const ms = new Date(isoExpiry).getTime()\n expiresAt = Number.isNaN(ms) ? null : new Date(ms).toISOString()\n } else if (typeof token.expiry_date === 'number' && Number.isFinite(token.expiry_date)) {\n expiresAt = new Date(token.expiry_date).toISOString()\n }\n\n return {\n accessToken,\n refreshToken,\n tokenType: toNonEmptyString(token.token_type) ?? 'Bearer',\n expiresAt,\n authMethod: toNonEmptyString(doc.auth_method) ?? toNonEmptyString(token.auth_method),\n clientId: toNonEmptyString(token.client_id) ?? toNonEmptyString(doc.client_id) ?? toNonEmptyString(token.clientId) ?? toNonEmptyString(doc.clientId),\n }\n}\n\n/** Best-effort enrichment: email (userinfo) + projectId (loadCodeAssist), time-boxed. */\nexport async function enrichWithAntigravityBackend(parsed: ParsedAgyAuth): Promise<EnrichedAgyAuth> {\n let email: string | null = null\n let projectId: string | null = null\n\n const controller = new AbortController()\n const timer = setTimeout(() => controller.abort(), 8000)\n try {\n const res = await proxiedFetch('https://www.googleapis.com/oauth2/v1/userinfo?alt=json', {\n headers: { Authorization: `Bearer ${parsed.accessToken}` },\n signal: controller.signal,\n })\n if (res.ok) {\n email = toNonEmptyString(toRecord(await res.json()).email)\n }\n } catch {\n // best effort — email stays null\n } finally {\n clearTimeout(timer)\n }\n\n const loadController = new AbortController()\n const loadTimer = setTimeout(() => loadController.abort(), 8000)\n try {\n const headers: Record<string, string> = {\n Authorization: `Bearer ${parsed.accessToken}`,\n 'Content-Type': 'application/json',\n 'User-Agent': getAgyBootstrapUserAgent(),\n 'Client-Metadata': getAgyBootstrapClientMetadata(),\n }\n for (const endpoint of AGY_ENDPOINT_FALLBACKS) {\n try {\n const res = await proxiedFetch(`${endpoint}/v1internal:loadCodeAssist`, {\n method: 'POST',\n headers,\n body: JSON.stringify({ metadata: { ideType: 'ANTIGRAVITY' } }),\n signal: loadController.signal,\n })\n if (!res.ok) continue\n const data = toRecord(await res.json())\n const project = data.cloudaicompanionProject\n projectId = (typeof project === 'string' ? toNonEmptyString(project) : null)\n ?? toNonEmptyString(toRecord(project).id)\n if (projectId) break\n } catch {\n // try next endpoint\n }\n }\n } catch {\n // best effort — projectId stays null\n } finally {\n clearTimeout(loadTimer)\n }\n\n return {\n ...parsed,\n email,\n projectId,\n clientId: parsed.clientId ?? null,\n }\n}\n\n/** Parse either a raw token document or a paste blob into an enriched account. */\nexport async function parseImportSource(source: unknown, kind: 'json' | 'blob'): Promise<EnrichedAgyAuth> {\n if (kind === 'blob') {\n if (typeof source !== 'string') throw new AgyAuthFileError('blob must be a string', 400, 'invalid_blob')\n const blob = decodeCredentialBlob(source)\n return enrichWithAntigravityBackend(\n parseAndValidateAgyToken({ token: blob.tokens, auth_method: 'paste-blob' }),\n )\n }\n return enrichWithAntigravityBackend(parseAndValidateAgyToken(source))\n}\n\n/** Batch-import many sources (CLI multi-file / web multi-line paste). Each item\n * is independent: a failure is collected, the rest still import. */\nexport async function importManySources(\n items: Array<{ source: unknown; kind: 'json' | 'blob' }>,\n store: AccountStore,\n options: { email?: string; overwriteExisting?: boolean; proxy?: string } = {},\n): Promise<{ imported: number; replaced: number; errors: string[] }> {\n const result = { imported: 0, replaced: 0, errors: [] as string[] }\n for (const item of items) {\n try {\n const enriched = await parseImportSource(item.source, item.kind)\n const { created } = await upsertImportedAccount(store, enriched, options)\n if (created) result.imported++\n else result.replaced++\n } catch (error) {\n result.errors.push(error instanceof Error ? error.message : String(error))\n }\n }\n return result\n}\n\n/** Upsert the account into the store, deduping by email. */\nexport async function upsertImportedAccount(\n store: AccountStore,\n enriched: EnrichedAgyAuth,\n options: { email?: string; overwriteExisting?: boolean; proxy?: string } = {},\n): Promise<{ account: ManagedAccount; created: boolean }> {\n const resolvedEmail = options.email || enriched.email\n const refresh = `${enriched.refreshToken}|${enriched.projectId ?? ''}`\n // Normalize proxy if explicitly provided (empty -> clear -> undefined)\n let normalizedProxy: string | undefined\n let proxyProvided = options.proxy !== undefined\n if (proxyProvided) {\n const raw = options.proxy ?? ''\n if (!raw.trim()) normalizedProxy = undefined\n else normalizedProxy = normalizeProxyUrl(raw)\n }\n\n return store.mutate((storage) => {\n const existingIndex = resolvedEmail\n ? storage.accounts.findIndex((a) => a.email?.toLowerCase() === resolvedEmail.toLowerCase())\n : -1\n\n if (existingIndex !== -1) {\n if (!options.overwriteExisting) {\n throw new AgyAuthFileError(\n 'An agy account for this email already exists. Pass --overwrite to replace it.',\n 409,\n 'duplicate_account',\n )\n }\n const existing = storage.accounts[existingIndex]!\n const updated: ManagedAccount = {\n ...existing,\n id: existing.id || randomUUID(),\n refresh,\n email: existing.email ?? resolvedEmail ?? undefined,\n clientId: enriched.clientId ? enriched.clientId : existing.clientId,\n addedAt: existing.addedAt,\n lastUsed: Date.now(),\n enabled: true,\n verificationRequired: false,\n verificationRequiredAt: undefined,\n verificationRequiredReason: undefined,\n }\n if (proxyProvided) {\n if (normalizedProxy) updated.proxy = normalizedProxy\n else delete (updated as { proxy?: string }).proxy\n }\n storage.accounts[existingIndex] = updated\n return { account: storage.accounts[existingIndex]!, created: false }\n }\n\n const account: ManagedAccount = {\n id: randomUUID(),\n email: resolvedEmail ?? undefined,\n refresh,\n projectId: enriched.projectId ?? undefined,\n clientId: enriched.clientId ?? undefined,\n addedAt: Date.now(),\n lastUsed: Date.now(),\n enabled: true,\n ...(normalizedProxy ? { proxy: normalizedProxy } : {}),\n }\n // When proxy was explicitly cleared (empty string) for new account, just omit proxy field\n storage.accounts.push(account)\n if (storage.activeIndex >= storage.accounts.length - 1 && storage.accounts.length === 1) {\n storage.activeIndex = 0\n }\n return { account, created: true }\n })\n}\n"],"mappings":";;;;;;;;;;;;;;AAwBA,eAAsB,qBACpB,aACA,YAAY,IACuB;CACnC,MAAM,EAAE,UAAU,cAAc,iBAAiB;CAEjD,MAAM,MAAM,IAAI,IAAI,mBAAmB;CACvC,MAAM,EAAE,aAAa,4BAA4B;CACjD,IAAI,aAAa,IAAI,aAAa,QAAQ;CAC1C,IAAI,aAAa,IAAI,iBAAiB,MAAM;CAC5C,IAAI,aAAa,IAAI,gBAAgB,WAAW;CAChD,IAAI,aAAa,IAAI,SAAS,WAAW,KAAK,GAAG,CAAC;CAClD,IAAI,aAAa,IAAI,kBAAkB,SAAS;CAChD,IAAI,aAAa,IAAI,yBAAyB,MAAM;CACpD,MAAM,QAAQ,YAAY;EAAE;EAAU,WAAW,aAAa;CAAG,CAAC;CAClE,IAAI,aAAa,I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Disabled } from "./accounts-Blqno5Te.mjs";
|
|
2
|
+
import { t as createAgyRuntime } from "./plugin-common-CP6Czgh9.mjs";
|
|
3
3
|
//#region src/index.ts
|
|
4
4
|
const name = "dsh-agy";
|
|
5
5
|
const inject = ["llm"];
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
|
|
2
|
+
import { D as getAgyBootstrapUserAgent, b as AGY_ENDPOINT_FALLBACKS } from "./accounts-Blqno5Te.mjs";
|
|
3
|
+
import { i as proxiedFetch } from "./proxy-DQPIwUov.mjs";
|
|
4
|
+
import { i as isLevelThinkingModel, n as catalogModel, r as isChatCallableModelId, t as AGY_PUBLIC_MODELS } from "./catalog-DVHPJjfc.mjs";
|
|
5
|
+
import { ReasoningEffortId } from "@deepseek-ai/dsh-llm";
|
|
6
|
+
//#region src/adapter/models.ts
|
|
7
|
+
/**
|
|
8
|
+
* Model discovery: dynamic `v1internal:fetchAvailableModels` as the primary
|
|
9
|
+
* source (fresh ids + per-model quotaInfo), the pinned catalog merged in for
|
|
10
|
+
* capability metadata, and catalog fallback when the endpoint is unreachable.
|
|
11
|
+
*/
|
|
12
|
+
var models_exports = /* @__PURE__ */ __exportAll({
|
|
13
|
+
AGY_PROVIDER: () => "agy",
|
|
14
|
+
catalogModelList: () => catalogModelList,
|
|
15
|
+
fetchAvailableModels: () => fetchAvailableModels,
|
|
16
|
+
listAgyModels: () => listAgyModels,
|
|
17
|
+
mergeModelCatalog: () => mergeModelCatalog,
|
|
18
|
+
resolveAgyModel: () => resolveAgyModel
|
|
19
|
+
});
|
|
20
|
+
/** Level-thinking: single id + selectable low/medium/high via thinkingLevel. Default is UI hint, not wire default. */
|
|
21
|
+
const LEVEL_REASONING = Object.freeze({
|
|
22
|
+
efforts: Object.freeze([
|
|
23
|
+
{
|
|
24
|
+
id: ReasoningEffortId("low"),
|
|
25
|
+
name: "Low"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
id: ReasoningEffortId("medium"),
|
|
29
|
+
name: "Medium"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
id: ReasoningEffortId("high"),
|
|
33
|
+
name: "High"
|
|
34
|
+
}
|
|
35
|
+
]),
|
|
36
|
+
defaultEffort: ReasoningEffortId("medium")
|
|
37
|
+
});
|
|
38
|
+
/**
|
|
39
|
+
* Input modalities per model. Image support follows the catalog's own
|
|
40
|
+
* `supportsVision` metadata for known models (gpt-oss-120b-medium is text-only
|
|
41
|
+
* there); unknown dynamic ids default to vision-capable — the upstream schema
|
|
42
|
+
* accepts inlineData across the board, and a wrong guess surfaces as a clear
|
|
43
|
+
* upstream 400 instead of a silent drop.
|
|
44
|
+
*/
|
|
45
|
+
const AGY_INPUT_MODALITIES = ["text", "image"];
|
|
46
|
+
const AGY_TEXT_ONLY_MODALITIES = ["text"];
|
|
47
|
+
function inputModalitiesFor(meta) {
|
|
48
|
+
return [...(meta ? meta.supportsVision === true : true) ? AGY_INPUT_MODALITIES : AGY_TEXT_ONLY_MODALITIES];
|
|
49
|
+
}
|
|
50
|
+
/** Fetch the account's available models from the first reachable endpoint. */
|
|
51
|
+
async function fetchAvailableModels(accessToken, projectId, fetchImpl = proxiedFetch) {
|
|
52
|
+
let lastError = null;
|
|
53
|
+
const body = projectId ? { project: projectId } : {};
|
|
54
|
+
for (const baseEndpoint of AGY_ENDPOINT_FALLBACKS) try {
|
|
55
|
+
const response = await fetchImpl(`${baseEndpoint}/v1internal:fetchAvailableModels`, {
|
|
56
|
+
method: "POST",
|
|
57
|
+
headers: {
|
|
58
|
+
Authorization: `Bearer ${accessToken}`,
|
|
59
|
+
"Content-Type": "application/json",
|
|
60
|
+
"User-Agent": getAgyBootstrapUserAgent()
|
|
61
|
+
},
|
|
62
|
+
body: JSON.stringify(body)
|
|
63
|
+
});
|
|
64
|
+
if (response.ok) return await response.json();
|
|
65
|
+
lastError = /* @__PURE__ */ new Error(`fetchAvailableModels ${response.status} at ${baseEndpoint}`);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
lastError = error;
|
|
68
|
+
}
|
|
69
|
+
throw lastError instanceof Error ? lastError : /* @__PURE__ */ new Error("fetchAvailableModels: all endpoints failed");
|
|
70
|
+
}
|
|
71
|
+
/** Merge dynamic ids with catalog metadata; non-chat models and unknowns keep minimal info. */
|
|
72
|
+
function mergeModelCatalog(dynamic) {
|
|
73
|
+
const entries = [];
|
|
74
|
+
for (const [id, entry] of Object.entries(dynamic.models ?? {})) {
|
|
75
|
+
if (!isChatCallableModelId(id)) continue;
|
|
76
|
+
const meta = catalogModel(id);
|
|
77
|
+
const displayName = (entry.displayName && entry.displayName !== id ? entry.displayName : void 0) ?? meta?.name ?? entry.displayName ?? entry.modelName ?? id;
|
|
78
|
+
entries.push({
|
|
79
|
+
provider: "agy",
|
|
80
|
+
id,
|
|
81
|
+
name: displayName,
|
|
82
|
+
inputModalities: inputModalitiesFor(meta),
|
|
83
|
+
...meta ? { context: { contextWindow: meta.contextLength } } : {}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
return entries;
|
|
87
|
+
}
|
|
88
|
+
/** Catalog-only model list used when the endpoint is unreachable. */
|
|
89
|
+
function catalogModelList() {
|
|
90
|
+
return AGY_PUBLIC_MODELS.map((model) => ({
|
|
91
|
+
provider: "agy",
|
|
92
|
+
id: model.id,
|
|
93
|
+
name: model.name,
|
|
94
|
+
inputModalities: inputModalitiesFor(model),
|
|
95
|
+
context: { contextWindow: model.contextLength }
|
|
96
|
+
}));
|
|
97
|
+
}
|
|
98
|
+
/** Adapter-facing listing: dynamic first, catalog fallback. */
|
|
99
|
+
async function listAgyModels(accessToken, projectId, fetchImpl = proxiedFetch) {
|
|
100
|
+
if (!accessToken) return catalogModelList();
|
|
101
|
+
try {
|
|
102
|
+
const merged = mergeModelCatalog(await fetchAvailableModels(accessToken, projectId, fetchImpl));
|
|
103
|
+
return merged.length > 0 ? merged : catalogModelList();
|
|
104
|
+
} catch {
|
|
105
|
+
return catalogModelList();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/** Resolve one exact model's metadata (catalog-backed; dynamic ids pass through). */
|
|
109
|
+
function resolveAgyModel(provider, model) {
|
|
110
|
+
const meta = catalogModel(model);
|
|
111
|
+
if (isLevelThinkingModel(model)) return {
|
|
112
|
+
provider,
|
|
113
|
+
id: model,
|
|
114
|
+
name: meta?.name ?? model,
|
|
115
|
+
inputModalities: inputModalitiesFor(meta),
|
|
116
|
+
context: { contextWindow: meta?.contextLength ?? 1048576 },
|
|
117
|
+
defaultMaxTokens: meta?.maxOutputTokens ?? 65536,
|
|
118
|
+
reasoning: {
|
|
119
|
+
...LEVEL_REASONING,
|
|
120
|
+
efforts: [...LEVEL_REASONING.efforts]
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
return {
|
|
124
|
+
provider,
|
|
125
|
+
id: model,
|
|
126
|
+
name: meta?.name ?? model,
|
|
127
|
+
inputModalities: inputModalitiesFor(meta),
|
|
128
|
+
...meta ? {
|
|
129
|
+
context: { contextWindow: meta.contextLength },
|
|
130
|
+
defaultMaxTokens: meta.maxOutputTokens
|
|
131
|
+
} : {}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
//#endregion
|
|
135
|
+
export { resolveAgyModel as i, listAgyModels as n, models_exports as r, catalogModelList as t };
|
|
136
|
+
|
|
137
|
+
//# sourceMappingURL=models-3VFg0nRK.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"models-3VFg0nRK.mjs","names":[],"sources":["../src/adapter/models.ts"],"sourcesContent":["/**\n * Model discovery: dynamic `v1internal:fetchAvailableModels` as the primary\n * source (fresh ids + per-model quotaInfo), the pinned catalog merged in for\n * capability metadata, and catalog fallback when the endpoint is unreachable.\n */\n\nimport { ReasoningEffortId, type LlmModelInfo, type LlmModelReasoningInfo, type LlmResolvedModelInfo, type ModelModality } from '@deepseek-ai/dsh-llm'\nimport { AGY_ENDPOINT_FALLBACKS, getAgyBootstrapUserAgent } from '../oauth/constants.ts'\nimport { proxiedFetch } from '../proxy.ts'\nimport { AGY_PUBLIC_MODELS, catalogModel, isChatCallableModelId, isLevelThinkingModel } from './catalog.ts'\n\nexport const AGY_PROVIDER = 'agy'\n\n/** Level-thinking: single id + selectable low/medium/high via thinkingLevel. Default is UI hint, not wire default. */\nconst LEVEL_REASONING: LlmModelReasoningInfo = Object.freeze({\n efforts: Object.freeze([\n { id: ReasoningEffortId('low'), name: 'Low' },\n { id: ReasoningEffortId('medium'), name: 'Medium' },\n { id: ReasoningEffortId('high'), name: 'High' },\n ] as const),\n defaultEffort: ReasoningEffortId('medium'),\n} as const)\n\n/**\n * Input modalities per model. Image support follows the catalog's own\n * `supportsVision` metadata for known models (gpt-oss-120b-medium is text-only\n * there); unknown dynamic ids default to vision-capable — the upstream schema\n * accepts inlineData across the board, and a wrong guess surfaces as a clear\n * upstream 400 instead of a silent drop.\n */\nconst AGY_INPUT_MODALITIES = ['text', 'image'] as const\nconst AGY_TEXT_ONLY_MODALITIES = ['text'] as const\n\nfunction inputModalitiesFor(meta: { supportsVision?: boolean } | undefined): ModelModality[] {\n return [...(meta ? meta.supportsVision === true : true) ? AGY_INPUT_MODALITIES : AGY_TEXT_ONLY_MODALITIES]\n}\n\nexport interface DiscoveredModelEntry {\n quotaInfo?: {\n remainingFraction?: number\n resetTime?: string\n }\n displayName?: string\n modelName?: string\n}\n\nexport interface DiscoveredModels {\n models?: Record<string, DiscoveredModelEntry>\n}\n\n/** Fetch the account's available models from the first reachable endpoint. */\nexport async function fetchAvailableModels(\n accessToken: string,\n projectId?: string,\n fetchImpl: typeof fetch = proxiedFetch,\n): Promise<DiscoveredModels> {\n let lastError: unknown = null\n const body = projectId ? { project: projectId } : {}\n for (const baseEndpoint of AGY_ENDPOINT_FALLBACKS) {\n try {\n const response = await fetchImpl(`${baseEndpoint}/v1internal:fetchAvailableModels`, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${accessToken}`,\n 'Content-Type': 'application/json',\n 'User-Agent': getAgyBootstrapUserAgent(),\n },\n body: JSON.stringify(body),\n })\n if (response.ok) {\n return (await response.json()) as DiscoveredModels\n }\n lastError = new Error(`fetchAvailableModels ${response.status} at ${baseEndpoint}`)\n } catch (error) {\n lastError = error\n }\n }\n throw lastError instanceof Error ? lastError : new Error('fetchAvailableModels: all endpoints failed')\n}\n\n/** Merge dynamic ids with catalog metadata; non-chat models and unknowns keep minimal info. */\nexport function mergeModelCatalog(dynamic: DiscoveredModels): LlmModelInfo[] {\n const entries: LlmModelInfo[] = []\n for (const [id, entry] of Object.entries(dynamic.models ?? {})) {\n if (!isChatCallableModelId(id)) continue\n const meta = catalogModel(id)\n const rawDisplayName = entry.displayName && entry.displayName !== id ? entry.displayName : undefined\n const displayName = rawDisplayName ?? meta?.name ?? entry.displayName ?? entry.modelName ?? id\n entries.push({\n provider: AGY_PROVIDER,\n id,\n name: displayName,\n inputModalities: inputModalitiesFor(meta),\n ...(meta ? { context: { contextWindow: meta.contextLength } } : {}),\n })\n }\n return entries\n}\n\n/** Catalog-only model list used when the endpoint is unreachable. */\nexport function catalogModelList(): LlmModelInfo[] {\n return AGY_PUBLIC_MODELS.map((model) => ({\n provider: AGY_PROVIDER,\n id: model.id,\n name: model.name,\n inputModalities: inputModalitiesFor(model),\n context: { contextWindow: model.contextLength },\n }))\n}\n\n/** Adapter-facing listing: dynamic first, catalog fallback. */\nexport async function listAgyModels(\n accessToken: string | undefined,\n projectId: string | undefined,\n fetchImpl: typeof fetch = proxiedFetch,\n): Promise<readonly LlmModelInfo[]> {\n if (!accessToken) return catalogModelList()\n try {\n const dynamic = await fetchAvailableModels(accessToken, projectId, fetchImpl)\n const merged = mergeModelCatalog(dynamic)\n return merged.length > 0 ? merged : catalogModelList()\n } catch {\n return catalogModelList()\n }\n}\n\n/** Resolve one exact model's metadata (catalog-backed; dynamic ids pass through). */\nexport function resolveAgyModel(provider: string, model: string): LlmResolvedModelInfo {\n const meta = catalogModel(model)\n if (isLevelThinkingModel(model)) {\n return {\n provider,\n id: model,\n name: meta?.name ?? model,\n inputModalities: inputModalitiesFor(meta),\n context: { contextWindow: meta?.contextLength ?? 1048576 },\n defaultMaxTokens: meta?.maxOutputTokens ?? 65536,\n // Return a shallow copy so callers cannot mutate the frozen singleton.\n reasoning: { ...LEVEL_REASONING, efforts: [...LEVEL_REASONING.efforts] },\n }\n }\n return {\n provider,\n id: model,\n name: meta?.name ?? model,\n inputModalities: inputModalitiesFor(meta),\n ...(meta ? { context: { contextWindow: meta.contextLength }, defaultMaxTokens: meta.maxOutputTokens } : {}),\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAcA,MAAM,kBAAyC,OAAO,OAAO;CAC3D,SAAS,OAAO,OAAO;EACrB;GAAE,IAAI,kBAAkB,KAAK;GAAG,MAAM;EAAM;EAC5C;GAAE,IAAI,kBAAkB,QAAQ;GAAG,MAAM;EAAS;EAClD;GAAE,IAAI,kBAAkB,MAAM;GAAG,MAAM;EAAO;CAChD,CAAU;CACV,eAAe,kBAAkB,QAAQ;AAC3C,CAAU;;;;;;;;AASV,MAAM,uBAAuB,CAAC,QAAQ,OAAO;AAC7C,MAAM,2BAA2B,CAAC,MAAM;AAExC,SAAS,mBAAmB,MAAiE;CAC3F,OAAO,CAAC,IAAI,OAAO,KAAK,mBAAmB,OAAO,QAAQ,uBAAuB,wBAAwB;AAC3G;;AAgBA,eAAsB,qBACpB,aACA,WACA,YAA0B,cACC;CAC3B,IAAI,YAAqB;CACzB,MAAM,OAAO,YAAY,EAAE,SAAS,UAAU,IAAI,CAAC;CACnD,KAAK,MAAM,gBAAgB,wBACzB,IAAI;EACF,MAAM,WAAW,MAAM,UAAU,GAAG,aAAa,mCAAmC;GAClF,QAAQ;GACR,SAAS;IACP,eAAe,UAAU;IACzB,gBAAgB;IAChB,cAAc,yBAAyB;GACzC;GACA,MAAM,KAAK,UAAU,IAAI;EAC3B,CAAC;EACD,IAAI,SAAS,IACX,OAAQ,MAA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AM,MAAM,QAAQ;EACpB,iBAAiB,mBAAmB,IAAI;EACxC,SAAS,EAAE,eAAe,MAAM,iBAAiB,QAAQ;EACzD,kBAAkB,MAAM,mBAAmB;EAE3C,WAAW;GAAE,GAAG;GAAiB,SAAS,CAAC,GAAG,gBAAgB,OAAO;EAAE;CACzE;CAEF,OAAO;EACL;EACA,IAAI;EACJ,MAAM,MAAM,QAAQ;EACpB,iBAAiB,mBAAmB,IAAI;EACxC,GAAI,OAAO;GAAE,SAAS,EAAE,eAAe,KAAK,cAAc;GAAG,kBAAkB,KAAK;EAAgB,IAAI,CAAC;CAC3G;AACF"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { A as AgyPoolBlockedError, _ as
|
|
2
|
-
import {
|
|
1
|
+
import { A as AgyPoolBlockedError, T as fetchAgyFirstOk, _ as deriveAntigravitySessionId, a as deriveKey, c as resolveDshHome, h as resolveAntigravityVersion, i as createAesGcmCodec, k as AgyAuthError, l as resolveMasterKeyCodec, o as loadMasterKey, r as MASTER_KEY_REF, s as persistMasterKey, t as JsonAccountStore, u as AgySessionManager, v as generateAntigravityRequestId } from "./accounts-Blqno5Te.mjs";
|
|
2
|
+
import { n as isProxyUnreachableError } from "./proxy-DQPIwUov.mjs";
|
|
3
|
+
import { r as setThoughtSignature, t as toAgyRequestBody } from "./translate-Zf1mdBri.mjs";
|
|
3
4
|
import { t as parseAgySse } from "./parse-D8bV2yqa.mjs";
|
|
4
|
-
import { i as resolveAgyModel, n as listAgyModels, t as catalogModelList } from "./models-
|
|
5
|
+
import { i as resolveAgyModel, n as listAgyModels, t as catalogModelList } from "./models-3VFg0nRK.mjs";
|
|
5
6
|
import { randomBytes } from "node:crypto";
|
|
6
7
|
import { LlmAdapter, LlmError, ProviderRequestId, QUOTA_EXCEEDED_CODE, attributionHeaders } from "@deepseek-ai/dsh-llm";
|
|
7
8
|
//#region src/runtime/classify.ts
|
|
@@ -114,6 +115,10 @@ function classifyFetchError(error) {
|
|
|
114
115
|
kind: "network-error",
|
|
115
116
|
message
|
|
116
117
|
};
|
|
118
|
+
if (isProxyUnreachableError(error)) return {
|
|
119
|
+
kind: "proxy-unreachable",
|
|
120
|
+
message
|
|
121
|
+
};
|
|
117
122
|
return {
|
|
118
123
|
kind: "network-error",
|
|
119
124
|
message
|
|
@@ -127,6 +132,15 @@ function classifyFetchError(error) {
|
|
|
127
132
|
* parsing, failure classification, and rotation reporting. All wire details
|
|
128
133
|
* live in translate.ts / parse.ts / models.ts.
|
|
129
134
|
*/
|
|
135
|
+
/** Collect image refs from user-message content only (spec scope: user images; tool-result nesting out of scope). */
|
|
136
|
+
function collectImageRefs(options) {
|
|
137
|
+
const refs = [];
|
|
138
|
+
for (const message of options.messages) {
|
|
139
|
+
if (message.role !== "user") continue;
|
|
140
|
+
for (const block of message.content) if (block.type === "image") refs.push(block.attachment);
|
|
141
|
+
}
|
|
142
|
+
return refs;
|
|
143
|
+
}
|
|
130
144
|
const UPSTREAM_ERROR_CODE = "UPSTREAM";
|
|
131
145
|
/** Build the impersonation headers for one request (per-request randomization applied by the shell). */
|
|
132
146
|
function buildRequestHeaders(session) {
|
|
@@ -163,7 +177,31 @@ var AgyAdapter = class extends LlmAdapter {
|
|
|
163
177
|
async resolveModel(provider, model) {
|
|
164
178
|
return resolveAgyModel(provider, model);
|
|
165
179
|
}
|
|
180
|
+
/**
|
|
181
|
+
* Pre-resolve every image attachment into base64 bytes before translation.
|
|
182
|
+
* Image input hard-fails with UNSUPPORTED_CONTENT (terminal, never retried)
|
|
183
|
+
* when the store is missing or a read fails — silently dropping images and
|
|
184
|
+
* sending text-only is the exact failure mode this path exists to prevent.
|
|
185
|
+
*/
|
|
186
|
+
async resolveRequestImages(options) {
|
|
187
|
+
const refs = collectImageRefs(options);
|
|
188
|
+
const images = /* @__PURE__ */ new Map();
|
|
189
|
+
if (refs.length === 0) return images;
|
|
190
|
+
const store = this.options.resolveAttachments?.();
|
|
191
|
+
if (!store) throw new LlmError("agy image input requires the durable attachment service (in-harness plugin only)", "UNSUPPORTED_CONTENT");
|
|
192
|
+
for (const ref of refs) try {
|
|
193
|
+
const stored = await store.readImage(ref);
|
|
194
|
+
images.set(ref.attachmentId, {
|
|
195
|
+
mediaType: stored.ref.mediaType,
|
|
196
|
+
data: Buffer.from(stored.data).toString("base64")
|
|
197
|
+
});
|
|
198
|
+
} catch (cause) {
|
|
199
|
+
throw new LlmError(`agy image attachment "${ref.attachmentId}" could not be loaded: ${cause instanceof Error ? cause.message : String(cause)}`, "UNSUPPORTED_CONTENT", { cause: cause instanceof Error ? cause : void 0 });
|
|
200
|
+
}
|
|
201
|
+
return images;
|
|
202
|
+
}
|
|
166
203
|
async *stream(options) {
|
|
204
|
+
const images = await this.resolveRequestImages(options);
|
|
167
205
|
let session;
|
|
168
206
|
try {
|
|
169
207
|
session = await this.options.getSession(options.model);
|
|
@@ -187,7 +225,8 @@ var AgyAdapter = class extends LlmAdapter {
|
|
|
187
225
|
if (!session) throw new LlmError("No agy account configured — run `dsh-agy login` to authenticate.", "NO_CREDENTIAL");
|
|
188
226
|
const body = toAgyRequestBody(options, {
|
|
189
227
|
projectId: session.account.projectId,
|
|
190
|
-
sessionId: deriveAntigravitySessionId(session.account.email) ?? void 0
|
|
228
|
+
sessionId: deriveAntigravitySessionId(session.account.email) ?? void 0,
|
|
229
|
+
...images.size > 0 ? { images } : {}
|
|
191
230
|
});
|
|
192
231
|
const headers = buildRequestHeaders(session);
|
|
193
232
|
let response;
|
|
@@ -301,11 +340,12 @@ async function createAgyRuntime(ctx) {
|
|
|
301
340
|
adapter: new AgyAdapter({
|
|
302
341
|
getSession: (model) => sessions.getSession(model),
|
|
303
342
|
reportFailure: (kind, session, info) => sessions.reportFailure(kind, session, info),
|
|
304
|
-
markSuccess: (session) => sessions.markSuccess(session)
|
|
343
|
+
markSuccess: (session) => sessions.markSuccess(session),
|
|
344
|
+
resolveAttachments: () => ctx.get("attachments")
|
|
305
345
|
})
|
|
306
346
|
};
|
|
307
347
|
}
|
|
308
348
|
//#endregion
|
|
309
349
|
export { createAgyRuntime as t };
|
|
310
350
|
|
|
311
|
-
//# sourceMappingURL=plugin-common-
|
|
351
|
+
//# sourceMappingURL=plugin-common-CP6Czgh9.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin-common-CP6Czgh9.mjs","names":[],"sources":["../src/runtime/classify.ts","../src/adapter/adapter.ts","../src/plugin-common.ts"],"sourcesContent":["/**\n * Upstream failure classification: HTTP status + headers + body → FailureKind.\n * Conservative by design: only auth-failure may revoke an account; everything\n * else cools, rotates, or retries.\n */\n\nimport type { FailureKind } from '../types.ts'\nimport { isProxyUnreachableError } from '../proxy.ts'\n\nexport interface ClassifiedError {\n kind: FailureKind\n status?: number\n /** Server-provided retry delay (ms), when the response carried it. */\n retryAfterMs?: number\n /** Server-provided absolute reset time, when the response carried it. */\n resetTime?: string\n message?: string\n /** 429 sub-category, present when kind is rate-limit. */\n rateLimitCategory?: RateLimitCategory\n}\n\n/**\n * 429 sub-category (mirrors CLIProxyAPI/OmniRoute's classify429):\n * - soft_rate_limit: transient burst, retry immediately on the same account\n * - rate_limited: per-minute limit, short cooldown then retry (same account\n * when there is no other)\n * - quota_exhausted: daily/plan quota gone, long cooldown (24h)\n * - unknown: exponential backoff\n */\nexport type RateLimitCategory = 'soft_rate_limit' | 'rate_limited' | 'quota_exhausted' | 'unknown'\n\nconst QUOTA_EXHAUSTED_KEYWORDS = [\n 'quota_exhausted',\n 'quota exhausted',\n 'quota reached',\n 'enable overages',\n 'individual quota',\n]\n\n/** Classify a 429 body into the four upstream categories. */\nexport function classifyRateLimit(\n bodyText: string | undefined,\n retryAfterMs: number | undefined,\n): RateLimitCategory {\n const text = (bodyText ?? '').toLowerCase()\n if (QUOTA_EXHAUSTED_KEYWORDS.some((keyword) => text.includes(keyword))) {\n return 'quota_exhausted'\n }\n if (retryAfterMs !== undefined && retryAfterMs < 3000) {\n return 'soft_rate_limit'\n }\n if (retryAfterMs !== undefined) {\n return 'rate_limited'\n }\n // No explicit retry hint: \"quota\" wording → daily quota, otherwise unknown.\n return text.includes('quota') || text.includes('resource_exhausted') ? 'quota_exhausted' : 'unknown'\n}\n\nconst RATE_LIMIT_RESET_FIELDS = ['resetTime', 'reset_time', 'resetAt', 'quotaResetTime'] as const\n\nfunction extractResetTime(bodyText: string | undefined): string | undefined {\n if (!bodyText) return undefined\n try {\n const data = JSON.parse(bodyText) as Record<string, unknown>\n for (const field of RATE_LIMIT_RESET_FIELDS) {\n const value = data[field]\n if (typeof value === 'string' && value) return value\n if (typeof value === 'number' && Number.isFinite(value)) {\n // Unix ms when large, seconds when small (relative to now).\n return value > 1_000_000_000_000 ? new Date(value).toISOString() : new Date(Date.now() + value * 1000).toISOString()\n }\n }\n const quotaInfo = data.quotaInfo as Record<string, unknown> | undefined\n if (quotaInfo && typeof quotaInfo.resetTime === 'string') return quotaInfo.resetTime\n } catch {\n // not JSON — no reset info\n }\n return undefined\n}\n\nfunction parseRetryAfter(header: string | null): number | undefined {\n if (!header) return undefined\n const seconds = Number(header)\n if (Number.isFinite(seconds) && seconds >= 0) return seconds * 1000\n const date = Date.parse(header)\n if (!Number.isNaN(date)) return Math.max(0, date - Date.now())\n return undefined\n}\n\n/** Classify a completed HTTP response (non-2xx). */\nexport function classifyHttpError(\n status: number,\n headers: Headers,\n bodyText?: string,\n): ClassifiedError {\n const retryAfterMs = parseRetryAfter(headers.get('retry-after'))\n const resetTime = extractResetTime(bodyText)\n\n if (status === 429) {\n const category = classifyRateLimit(bodyText, retryAfterMs)\n return {\n kind: 'rate-limit',\n rateLimitCategory: category,\n status,\n retryAfterMs,\n resetTime,\n message: bodyText ? bodyText.slice(0, 200) : undefined,\n }\n }\n if (status === 401) {\n return { kind: 'auth-failure', status, message: bodyText ? bodyText.slice(0, 200) : undefined }\n }\n if (status === 403) {\n // Google also reports quota walls as 403 RESOURCE_EXHAUSTED, and the\n // endpoint fallback chain ends on hosts answering 403 for \"no license\"\n // (not bad auth). Only treat a 403 as auth-failure when the body carries\n // no quota wording — a false revoke permanently disables the account.\n const category = classifyRateLimit(bodyText, undefined)\n if (category === 'quota_exhausted') {\n return {\n kind: 'rate-limit',\n rateLimitCategory: category,\n status,\n message: bodyText ? bodyText.slice(0, 200) : undefined,\n }\n }\n return { kind: 'auth-failure', status, message: bodyText ? bodyText.slice(0, 200) : undefined }\n }\n if (status === 404) {\n // Model/route missing — nothing to rotate for; surface as transient request error.\n return { kind: 'transient', status, message: bodyText ? bodyText.slice(0, 200) : undefined }\n }\n if (status >= 500) {\n return { kind: 'transient', status, retryAfterMs, message: bodyText ? bodyText.slice(0, 200) : undefined }\n }\n if (status === 400) {\n // A 400 is a request-construction error (permanent — retrying sends the\n // same broken payload). Only capacity-style phrases are recoverable\n // (context overflow / model unavailable), mirroring OmniRoute's\n // classifyProviderError: everything else surfaces as a terminal\n // request-error so rotation does not waste attempts.\n const text = (bodyText ?? '').toLowerCase()\n const recoverable = (\n (text.includes('context') && (text.includes('overflow') || text.includes('too long') || text.includes('exceeded')))\n || (text.includes('model') && (text.includes('not found') || text.includes('unavailable') || text.includes('not supported')))\n )\n if (recoverable) {\n return { kind: 'transient', status, message: bodyText ? bodyText.slice(0, 200) : undefined }\n }\n return { kind: 'request-error', status, message: bodyText ? bodyText.slice(0, 200) : undefined }\n }\n // 400-range others (401 handled above): request-level errors, no rotation semantics.\n return { kind: 'transient', status, message: bodyText ? bodyText.slice(0, 200) : undefined }\n}\n\n/** Classify a fetch-level failure (DNS, refused, timeout, abort). */\nexport function classifyFetchError(error: unknown): ClassifiedError {\n const message = error instanceof Error ? error.message : String(error)\n if (error instanceof DOMException && error.name === 'AbortError') {\n return { kind: 'network-error', message }\n }\n if (isProxyUnreachableError(error)) {\n return { kind: 'proxy-unreachable', message }\n }\n return { kind: 'network-error', message }\n}\n\n/** Classify the result of a refresh attempt (shared by adapter + verify paths). */\nexport function classifyRefreshFailure(status: number, code?: string): ClassifiedError {\n if (code === 'invalid_grant') return { kind: 'auth-failure', status, message: code }\n if (status === 429) return { kind: 'rate-limit', status, message: code }\n if (status >= 500) return { kind: 'transient', status, message: code }\n return { kind: 'transient', status, message: code }\n}\n\nexport type { FailureKind }\n","/**\n * AgyAdapter: the DSH seam. A thin orchestrator over the deep modules —\n * account session resolution (shell-provided), request translation, SSE\n * parsing, failure classification, and rotation reporting. All wire details\n * live in translate.ts / parse.ts / models.ts.\n */\n\nimport {\n LlmAdapter,\n LlmError,\n ProviderRequestId,\n QUOTA_EXCEEDED_CODE,\n attributionHeaders,\n} from '@deepseek-ai/dsh-llm'\nimport type {\n GenerateOptions,\n LlmModelInfo,\n LlmProviderInfo,\n LlmResolvedModelInfo,\n StreamChunk,\n ToolSchema,\n} from '@deepseek-ai/dsh-llm'\nimport { AgyAuthError, AgyPoolBlockedError } from '../types.ts'\nimport type { AgyAccountSession, FailureKind, ManagedAccount, OAuthAuthDetails } from '../types.ts'\nimport type { RateLimitCategory } from '../runtime/classify.ts'\nimport { fetchAgyFirstOk } from '../oauth/constants.ts'\nimport { classifyFetchError, classifyHttpError } from '../runtime/classify.ts'\nimport { deriveAntigravitySessionId, generateAntigravityRequestId } from '../runtime/identity.ts'\nimport { setThoughtSignature } from '../runtime/signature-cache.ts'\nimport { toAgyRequestBody } from './translate.ts'\nimport type { AgyResolvedImage } from './translate.ts'\nimport { parseAgySse } from './parse.ts'\nimport { AGY_PROVIDER, catalogModelList, listAgyModels, resolveAgyModel } from './models.ts'\n\nexport type { AgyAccountSession }\n\n/**\n * Structural view of the harness attachment service (ctx.attachments).\n * Deliberately not an import of @deepseek-ai/dsh-attachment: the CLI bundle\n * must stay free of harness runtime dependencies, and the real store\n * satisfies this shape.\n */\nexport interface AgyAttachmentStore {\n readImage(ref: {\n attachmentId: string\n mediaType: string\n }): Promise<{ ref: { mediaType: string }; data: Uint8Array }>\n}\n\n/** Collect image refs from user-message content only (spec scope: user images; tool-result nesting out of scope). */\nfunction collectImageRefs(options: GenerateOptions): Array<{ attachmentId: string; mediaType: string }> {\n const refs: Array<{ attachmentId: string; mediaType: string }> = []\n for (const message of options.messages) {\n if (message.role !== 'user') continue\n for (const block of message.content) {\n if (block.type === 'image') refs.push(block.attachment)\n }\n }\n return refs\n}\n\nexport interface AgyAdapterOptions {\n /** Resolve the active account for a request (model-aware: family-scoped quota ranking). */\n getSession(model?: string): Promise<AgyAccountSession | undefined>\n /** Report a classified upstream failure so the shell can cool/rotate/revoke. */\n reportFailure(\n kind: FailureKind,\n session: AgyAccountSession,\n info?: {\n retryAfterMs?: number\n status?: number\n rateLimitCategory?: RateLimitCategory\n /** Server-reported absolute reset time; drives precise cooldowns. */\n resetTime?: string\n /** Requested model id; drives family-scoped rate-limit bookkeeping. */\n model?: string\n },\n ): Promise<void>\n /** Report a clean stream completion (resets the failure counter). */\n markSuccess?(session: AgyAccountSession): Promise<void>\n /** Resolve the harness attachment store; undefined outside the harness (standalone CLI). */\n resolveAttachments?(): AgyAttachmentStore | undefined\n}\n\nconst UPSTREAM_ERROR_CODE = 'UPSTREAM'\n\n/** Build the impersonation headers for one request (per-request randomization applied by the shell). */\nexport function buildRequestHeaders(session: AgyAccountSession): Record<string, string> {\n return {\n authorization: `Bearer ${session.auth.access}`,\n 'content-type': 'application/json',\n accept: 'text/event-stream',\n 'x-goog-request-id': generateAntigravityRequestId(),\n ...attributionHeaders(),\n ...session.impersonation,\n }\n}\n\nexport class AgyAdapter extends LlmAdapter {\n private readonly options: AgyAdapterOptions\n\n constructor(options: AgyAdapterOptions) {\n super()\n this.options = options\n }\n\n override providerInfo(_provider: string): LlmProviderInfo {\n return { id: AGY_PROVIDER, name: 'Antigravity (agy)' }\n }\n\n override async listModels(_provider: string): Promise<readonly LlmModelInfo[]> {\n try {\n const session = await this.options.getSession()\n return await listAgyModels(session?.auth.access, session?.account.projectId)\n } catch (error) {\n if (error instanceof AgyPoolBlockedError || error instanceof AgyAuthError) {\n return catalogModelList()\n }\n throw error\n }\n }\n\n override async resolveModel(provider: string, model: string): Promise<LlmResolvedModelInfo> {\n return resolveAgyModel(provider, model)\n }\n\n /**\n * Pre-resolve every image attachment into base64 bytes before translation.\n * Image input hard-fails with UNSUPPORTED_CONTENT (terminal, never retried)\n * when the store is missing or a read fails — silently dropping images and\n * sending text-only is the exact failure mode this path exists to prevent.\n */\n private async resolveRequestImages(options: GenerateOptions): Promise<Map<string, AgyResolvedImage>> {\n const refs = collectImageRefs(options)\n const images = new Map<string, AgyResolvedImage>()\n if (refs.length === 0) return images\n const store = this.options.resolveAttachments?.()\n if (!store) {\n throw new LlmError(\n 'agy image input requires the durable attachment service (in-harness plugin only)',\n 'UNSUPPORTED_CONTENT',\n )\n }\n for (const ref of refs) {\n try {\n const stored = await store.readImage(ref)\n images.set(ref.attachmentId, {\n mediaType: stored.ref.mediaType,\n data: Buffer.from(stored.data).toString('base64'),\n })\n } catch (cause) {\n throw new LlmError(\n `agy image attachment \"${ref.attachmentId}\" could not be loaded: ${cause instanceof Error ? cause.message : String(cause)}`,\n 'UNSUPPORTED_CONTENT',\n { cause: cause instanceof Error ? cause : undefined },\n )\n }\n }\n return images\n }\n\n override async *stream(options: GenerateOptions): AsyncIterable<StreamChunk> {\n // Spec D1 sequence: resolve images first — a locally-failing image request\n // must surface UNSUPPORTED_CONTENT (user story 8) instead of being masked\n // by account-pool errors, and must not touch pool state at all.\n const images = await this.resolveRequestImages(options)\n let session: AgyAccountSession | undefined\n try {\n session = await this.options.getSession(options.model)\n } catch (error) {\n if (error instanceof AgyAuthError) {\n if (error.kind === 'transport') {\n throw new LlmError(error.message, 'TRANSPORT', { cause: error })\n }\n if (error.kind === 'rate-limit') {\n throw new LlmError(error.message, 'RATE_LIMIT', {\n requestId: ProviderRequestId(generateAntigravityRequestId()),\n })\n }\n throw new LlmError(error.message, 'INVALID_CREDENTIAL', { cause: error })\n }\n if (error instanceof AgyPoolBlockedError) {\n if (error.kind === 'quota-exhausted') {\n throw new LlmError(error.message, QUOTA_EXCEEDED_CODE)\n }\n const delta = Math.ceil(error.blockedUntil - Date.now())\n const providerRetryAfterMs = Number.isFinite(delta) && delta > 0 ? delta : 1\n throw new LlmError(error.message, 'RATE_LIMIT', {\n providerRetryAfterMs,\n requestId: ProviderRequestId(generateAntigravityRequestId()),\n })\n }\n throw error\n }\n if (!session) {\n throw new LlmError(\n 'No agy account configured — run `dsh-agy login` to authenticate.',\n 'NO_CREDENTIAL',\n )\n }\n\n const body = toAgyRequestBody(options, {\n projectId: session.account.projectId,\n sessionId: deriveAntigravitySessionId(session.account.email) ?? undefined,\n ...(images.size > 0 ? { images } : {}),\n })\n const headers = buildRequestHeaders(session)\n\n let response: Response\n try {\n response = await fetchAgyFirstOk('/v1internal:streamGenerateContent?alt=sse', {\n method: 'POST',\n headers,\n body: JSON.stringify(body),\n signal: options.signal,\n })\n } catch (error) {\n const classified = classifyFetchError(error)\n await this.options.reportFailure(classified.kind, session)\n throw new LlmError(classified.message ?? 'agy fetch failed', 'TRANSPORT', { cause: error })\n }\n\n if (!response.ok) {\n const bodyText = await response.text().catch(() => undefined)\n const classified = classifyHttpError(response.status, response.headers, bodyText)\n await this.options.reportFailure(classified.kind, session, {\n retryAfterMs: classified.retryAfterMs,\n status: response.status,\n rateLimitCategory: classified.rateLimitCategory,\n resetTime: classified.resetTime,\n model: options.model,\n })\n if (classified.kind === 'rate-limit') {\n // soft/rate limits are retryable by the harness (RATE_LIMIT + delay);\n // daily quota exhaustion is terminal (QUOTA, 24h cooldown already set).\n if (classified.rateLimitCategory === 'quota_exhausted') {\n throw new LlmError(\n `agy daily quota exhausted (${response.status}): ${classified.message ?? ''}`,\n QUOTA_EXCEEDED_CODE,\n )\n }\n throw new LlmError(\n `agy rate-limited (${response.status}): ${classified.message ?? ''}`,\n 'RATE_LIMIT',\n {\n providerRetryAfterMs: classified.retryAfterMs ?? undefined,\n requestId: ProviderRequestId(generateAntigravityRequestId()),\n },\n )\n }\n if (classified.kind === 'auth-failure') {\n throw new LlmError(\n `agy authentication failed (${response.status}) — run \\`dsh-agy login\\``,\n 'INVALID_CREDENTIAL',\n )\n }\n throw new LlmError(\n `agy upstream error (${response.status}): ${classified.message ?? ''}`,\n UPSTREAM_ERROR_CODE,\n )\n }\n\n if (!response.body) {\n throw new LlmError('agy stream returned no body', UPSTREAM_ERROR_CODE)\n }\n\n try {\n yield* parseAgySse(response.body, {\n signal: options.signal,\n onToolSignature: (toolCallId, signature) => {\n setThoughtSignature(toolCallId, signature)\n },\n })\n await this.options.markSuccess?.(session)\n } catch (error) {\n if (error instanceof DOMException && error.name === 'AbortError') {\n throw new LlmError('agy stream aborted', 'ABORTED', { cause: error })\n }\n await this.options.reportFailure('network-error', session)\n throw new LlmError(\n error instanceof Error ? error.message : 'agy stream parse failed',\n UPSTREAM_ERROR_CODE,\n { cause: error },\n )\n }\n }\n}\n\nexport type { ToolSchema }\n","/**\n * Shared runtime construction for the in-harness plugin entries: master-key\n * codec resolution (credentials seam first, credentials document fallback),\n * account store, session manager, and adapter. Used by the main plugin\n * (adapter registration) and the web plugin (route registration) so both\n * entries operate on the same store.\n */\n\nimport { randomBytes } from 'node:crypto'\nimport type { Context } from '@deepseek-ai/cordis'\nimport { AgyAdapter } from './adapter/adapter.ts'\nimport type { AgyAttachmentStore } from './adapter/adapter.ts'\nimport { AGY_PROVIDER } from './adapter/models.ts'\nimport { resolveAntigravityVersion } from './runtime/version.ts'\nimport { AgySessionManager } from './session.ts'\nimport { JsonAccountStore } from './store/accounts.ts'\nimport type { AccountStore } from './store/accounts.ts'\nimport {\n MASTER_KEY_REF,\n createAesGcmCodec,\n deriveKey,\n loadMasterKey,\n persistMasterKey,\n resolveDshHome,\n resolveMasterKeyCodec,\n} from './store/keyring.ts'\nimport type { SecretCodec } from './store/keyring.ts'\n\nexport interface CredentialsSeam {\n resolve(ref: string): Promise<{ value: string } | undefined>\n set(ref: string, value: string): Promise<void>\n}\n\nfunction codecFrom(masterKey: string): SecretCodec {\n return createAesGcmCodec(deriveKey(masterKey))\n}\n\n/** Resolve or create the master key, preferring the credentials seam when available. */\nexport async function resolveCodec(ctx: Context): Promise<{ codec: SecretCodec; created: boolean }> {\n const dshHome = resolveDshHome()\n const credentials = ctx.get('credentials') as CredentialsSeam | undefined\n\n if (credentials) {\n const resolved = await credentials.resolve(MASTER_KEY_REF)\n if (resolved) return { codec: codecFrom(resolved.value), created: false }\n const fileKey = loadMasterKey(dshHome)\n if (fileKey) return { codec: codecFrom(fileKey), created: false }\n const fresh = randomBytes(32).toString('hex')\n try {\n await credentials.set(MASTER_KEY_REF, fresh)\n return { codec: codecFrom(fresh), created: true }\n } catch {\n // Read-only shadowing: persist to the credentials document directly.\n persistMasterKey(dshHome, fresh)\n return { codec: codecFrom(fresh), created: true }\n }\n }\n\n return resolveMasterKeyCodec(dshHome)\n}\n\n/** Build the store, session manager, and adapter for one plugin entry. */\nexport async function createAgyRuntime(ctx: Context): Promise<{\n store: AccountStore\n sessions: AgySessionManager\n adapter: AgyAdapter\n}> {\n // Warm the version cache (non-blocking) so fingerprint generation inside\n // the rate-limit path never waits on a cold feed.\n void resolveAntigravityVersion().catch(() => {})\n const { codec } = await resolveCodec(ctx)\n const dshHome = resolveDshHome()\n const store = new JsonAccountStore({ file: `${dshHome}/agy-accounts.json`, codec })\n const sessions = new AgySessionManager({ store })\n // Optional background health probe (DSH_AGY_HEALTH_INTERVAL_MS), off by default.\n const healthIntervalMs = Number(process.env.DSH_AGY_HEALTH_INTERVAL_MS ?? 0)\n if (Number.isFinite(healthIntervalMs) && healthIntervalMs > 0) {\n sessions.startHealthProbe(healthIntervalMs)\n }\n const adapter = new AgyAdapter({\n getSession: (model) => sessions.getSession(model),\n reportFailure: (kind, session, info) => sessions.reportFailure(kind, session, info),\n markSuccess: (session) => sessions.markSuccess(session),\n resolveAttachments: () => ctx.get('attachments') as AgyAttachmentStore | undefined,\n })\n return { store, sessions, adapter }\n}\n\nexport { AGY_PROVIDER }\n"],"mappings":";;;;;;;;AA+BA,MAAM,2BAA2B;CAC/B;CACA;CACA;CACA;CACA;AACF;;AAG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AAK;GAAG,SAAS;EAAM;EACxE,MAAM,UAAU,cAAc,OAAO;EACrC,IAAI,SAAS,OAAO;GAAE,OAAO,UAAU,OAAO;GAAG,SAAS;EAAM;EAChE,MAAM,QAAQ,YAAY,EAAE,CAAC,CAAC,SAAS,KAAK;EAC5C,IAAI;GACF,MAAM,YAAY,IAAI,gBAAgB,KAAK;GAC3C,OAAO;IAAE,OAAO,UAAU,KAAK;IAAG,SAAS;GAAK;EAClD,QAAQ;GAEN,iBAAiB,SAAS,KAAK;GAC/B,OAAO;IAAE,OAAO,UAAU,KAAK;IAAG,SAAS;GAAK;EAClD;CACF;CAEA,OAAO,sBAAsB,OAAO;AACtC;;AAGA,eAAsB,iBAAiB,KAIpC;CAGD,0BAA+B,CAAC,CAAC,YAAY,CAAC,CAAC;CAC/C,MAAM,EAAE,UAAU,MAAM,aAAa,GAAG;CACxC,MAAM,UAAU,eAAe;CAC/B,MAAM,QAAQ,IAAI,iBAAiB;EAAE,MAAM,GAAG,QAAQ;EAAqB;CAAM,CAAC;CAClF,MAAM,WAAW,IAAI,kBAAkB,EAAE,MAAM,CAAC;CAEhD,MAAM,mBAAmB,OAAO,QAAQ,IAAI,8BAA8B,CAAC;CAC3E,IAAI,OAAO,SAAS,gBAAgB,KAAK,mBAAmB,GAC1D,SAAS,iBAAiB,gBAAgB;CAQ5C,OAAO;EAAE;EAAO;EAAU,SAAA,IANN,WAAW;GAC7B,aAAa,UAAU,SAAS,WAAW,KAAK;GAChD,gBAAgB,MAAM,SAAS,SAAS,SAAS,cAAc,MAAM,SAAS,IAAI;GAClF,cAAc,YAAY,SAAS,YAAY,OAAO;GACtD,0BAA0B,IAAI,IAAI,aAAa;EACjD,CACgC;CAAE;AACpC"}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
|
|
2
|
+
import { EnvHttpProxyAgent, ProxyAgent } from "undici";
|
|
3
|
+
import { createConnection } from "node:net";
|
|
4
|
+
//#region src/proxy.ts
|
|
5
|
+
/**
|
|
6
|
+
* Proxy-aware fetch: per-account proxy (http/https/socks5) + env fallback.
|
|
7
|
+
* - account.proxy present => per-account dispatcher (fail-closed, not affected by NO_PROXY)
|
|
8
|
+
* - otherwise => EnvHttpProxyAgent (honours HTTP_PROXY/HTTPS_PROXY/NO_PROXY)
|
|
9
|
+
* Applied per-request via dispatcher option so the host's global dispatcher stays untouched.
|
|
10
|
+
*/
|
|
11
|
+
var proxy_exports = /* @__PURE__ */ __exportAll({
|
|
12
|
+
_envAgentForTest: () => envAgent,
|
|
13
|
+
dispatcherForAsync: () => dispatcherForAsync,
|
|
14
|
+
isProxyReachable: () => isProxyReachable,
|
|
15
|
+
isProxyUnreachableError: () => isProxyUnreachableError,
|
|
16
|
+
normalizeProxyUrl: () => normalizeProxyUrl,
|
|
17
|
+
proxiedFetch: () => proxiedFetch,
|
|
18
|
+
proxyUrlForLogs: () => proxyUrlForLogs,
|
|
19
|
+
tagProxyUnreachable: () => tagProxyUnreachable
|
|
20
|
+
});
|
|
21
|
+
const envAgent = new EnvHttpProxyAgent();
|
|
22
|
+
const dispatcherCache = /* @__PURE__ */ new Map();
|
|
23
|
+
const SUPPORTED = /* @__PURE__ */ new Set([
|
|
24
|
+
"http:",
|
|
25
|
+
"https:",
|
|
26
|
+
"socks5:",
|
|
27
|
+
"socks5h:"
|
|
28
|
+
]);
|
|
29
|
+
function defaultPort(protocol) {
|
|
30
|
+
if (protocol === "https:") return "443";
|
|
31
|
+
if (protocol === "socks5:" || protocol === "socks5h:") return "1080";
|
|
32
|
+
return "8080";
|
|
33
|
+
}
|
|
34
|
+
function normalizeProxyUrl(proxyUrl) {
|
|
35
|
+
let raw = proxyUrl.trim();
|
|
36
|
+
if (!raw) throw new Error("[proxy] empty proxy URL");
|
|
37
|
+
if (raw.toLowerCase().startsWith("socks://")) raw = "socks5://" + raw.slice(8);
|
|
38
|
+
const familyMatch = raw.match(/\?family=(ipv4|ipv6)$/);
|
|
39
|
+
const familySuffix = familyMatch ? familyMatch[0] : "";
|
|
40
|
+
const baseRaw = familySuffix ? raw.slice(0, -familySuffix.length) : raw;
|
|
41
|
+
let parsed;
|
|
42
|
+
try {
|
|
43
|
+
parsed = new URL(baseRaw);
|
|
44
|
+
} catch {
|
|
45
|
+
throw new Error(`[proxy] invalid proxy URL: ${proxyUrlForLogs(proxyUrl)}`);
|
|
46
|
+
}
|
|
47
|
+
let protocol = parsed.protocol.toLowerCase();
|
|
48
|
+
if (protocol === "socks5h:") protocol = "socks5:";
|
|
49
|
+
if (!SUPPORTED.has(protocol) && protocol !== "socks5:") {
|
|
50
|
+
if (!SUPPORTED.has(parsed.protocol.toLowerCase())) throw new Error(`[proxy] unsupported protocol: ${parsed.protocol}`);
|
|
51
|
+
}
|
|
52
|
+
if (![
|
|
53
|
+
"http:",
|
|
54
|
+
"https:",
|
|
55
|
+
"socks5:"
|
|
56
|
+
].includes(protocol)) throw new Error(`[proxy] unsupported protocol: ${parsed.protocol}`);
|
|
57
|
+
if (!parsed.hostname) throw new Error("[proxy] missing host");
|
|
58
|
+
let port = parsed.port;
|
|
59
|
+
if (!port) port = defaultPort(protocol);
|
|
60
|
+
const portNum = Number(port);
|
|
61
|
+
if (!Number.isInteger(portNum) || portNum < 1 || portNum > 65535) throw new Error("[proxy] invalid port");
|
|
62
|
+
const auth = parsed.username ? `${encodeURIComponent(parsed.username)}${parsed.password ? `:${encodeURIComponent(parsed.password)}` : ""}@` : "";
|
|
63
|
+
const normalizedBase = `${protocol}//${auth}${parsed.hostname}:${port}`;
|
|
64
|
+
return familySuffix ? `${normalizedBase}${familySuffix}` : normalizedBase;
|
|
65
|
+
}
|
|
66
|
+
function proxyUrlForLogs(proxyUrl) {
|
|
67
|
+
try {
|
|
68
|
+
const fam = proxyUrl.match(/\?family=(ipv4|ipv6)$/);
|
|
69
|
+
const base = fam ? proxyUrl.slice(0, -fam[0].length) : proxyUrl;
|
|
70
|
+
const u = new URL(base);
|
|
71
|
+
const port = u.port || defaultPort(u.protocol);
|
|
72
|
+
return `${u.protocol}//${u.hostname}:${port}`;
|
|
73
|
+
} catch {
|
|
74
|
+
return proxyUrl;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const PROXY_UNREACHABLE_CODES = /* @__PURE__ */ new Set([
|
|
78
|
+
"ECONNREFUSED",
|
|
79
|
+
"ECONNRESET",
|
|
80
|
+
"ETIMEDOUT",
|
|
81
|
+
"ENETUNREACH",
|
|
82
|
+
"EHOSTUNREACH",
|
|
83
|
+
"EPIPE",
|
|
84
|
+
"UND_ERR_CONNECT_TIMEOUT",
|
|
85
|
+
"UND_ERR_SOCKET"
|
|
86
|
+
]);
|
|
87
|
+
function isLoopbackUrl(url) {
|
|
88
|
+
try {
|
|
89
|
+
const h = (typeof url === "string" ? new URL(url) : url).hostname.toLowerCase();
|
|
90
|
+
return h === "localhost" || h === "127.0.0.1" || h === "::1" || h === "::ffff:127.0.0.1";
|
|
91
|
+
} catch {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function isProxyUnreachableError(err) {
|
|
96
|
+
const seen = /* @__PURE__ */ new Set();
|
|
97
|
+
let cur = err;
|
|
98
|
+
for (let depth = 0; cur && depth < 5 && !seen.has(cur); depth++) {
|
|
99
|
+
seen.add(cur);
|
|
100
|
+
if (cur && typeof cur === "object") {
|
|
101
|
+
const code = cur.code;
|
|
102
|
+
if (typeof code === "string" && PROXY_UNREACHABLE_CODES.has(code)) return true;
|
|
103
|
+
const errorCode = cur.errorCode;
|
|
104
|
+
if (errorCode === "proxy_unreachable" || errorCode === "PROXY_UNREACHABLE") return true;
|
|
105
|
+
if (cur.statusCode === 503) {
|
|
106
|
+
const m = cur.message;
|
|
107
|
+
if (typeof m === "string" && m.includes("Proxy unreachable")) return true;
|
|
108
|
+
}
|
|
109
|
+
const msg = cur.message;
|
|
110
|
+
if (typeof msg === "string") {
|
|
111
|
+
if (msg.includes("proxy_unreachable") || msg.includes("PROXY_UNREACHABLE")) return true;
|
|
112
|
+
for (const c of PROXY_UNREACHABLE_CODES) if (msg.includes(c)) return true;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
cur = cur?.cause;
|
|
116
|
+
if (!cur && err?.errors && depth === 0) {
|
|
117
|
+
const errs = err.errors;
|
|
118
|
+
if (Array.isArray(errs)) {
|
|
119
|
+
for (const e of errs) if (isProxyUnreachableError(e)) return true;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
function tagProxyUnreachable(err) {
|
|
126
|
+
if (isProxyUnreachableError(err)) {
|
|
127
|
+
const e = err;
|
|
128
|
+
e.code = "PROXY_UNREACHABLE";
|
|
129
|
+
e.errorCode = "proxy_unreachable";
|
|
130
|
+
}
|
|
131
|
+
return err;
|
|
132
|
+
}
|
|
133
|
+
const FAST_FAIL_TIMEOUT_MS = 2e3;
|
|
134
|
+
const HEALTHY_TTL_MS = 3e4;
|
|
135
|
+
const UNHEALTHY_TTL_MS = 2e3;
|
|
136
|
+
const healthCache = /* @__PURE__ */ new Map();
|
|
137
|
+
const healthInflight = /* @__PURE__ */ new Map();
|
|
138
|
+
function tcpCheck(host, port, timeoutMs) {
|
|
139
|
+
return new Promise((resolve) => {
|
|
140
|
+
const socket = createConnection({
|
|
141
|
+
host,
|
|
142
|
+
port
|
|
143
|
+
}, () => {
|
|
144
|
+
socket.destroy();
|
|
145
|
+
resolve(true);
|
|
146
|
+
});
|
|
147
|
+
socket.setTimeout(timeoutMs);
|
|
148
|
+
socket.on("error", () => resolve(false));
|
|
149
|
+
socket.on("timeout", () => {
|
|
150
|
+
socket.destroy();
|
|
151
|
+
resolve(false);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
async function isProxyReachable(proxyUrl, timeoutMs = FAST_FAIL_TIMEOUT_MS) {
|
|
156
|
+
const key = normalizeProxyUrl(proxyUrl);
|
|
157
|
+
const cached = healthCache.get(key);
|
|
158
|
+
if (cached && Date.now() - cached.at < cached.ttl) return cached.healthy;
|
|
159
|
+
const existing = healthInflight.get(key);
|
|
160
|
+
if (existing) return existing;
|
|
161
|
+
let url;
|
|
162
|
+
try {
|
|
163
|
+
const base = key.replace(/\?family=(ipv4|ipv6)$/, "");
|
|
164
|
+
url = new URL(base);
|
|
165
|
+
} catch {
|
|
166
|
+
healthCache.set(key, {
|
|
167
|
+
healthy: false,
|
|
168
|
+
at: Date.now(),
|
|
169
|
+
ttl: UNHEALTHY_TTL_MS
|
|
170
|
+
});
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
const probe = tcpCheck(url.hostname.replace(/^\[/, "").replace(/\]$/, ""), Number(url.port || defaultPort(url.protocol)), timeoutMs).then((healthy) => {
|
|
174
|
+
healthCache.set(key, {
|
|
175
|
+
healthy,
|
|
176
|
+
at: Date.now(),
|
|
177
|
+
ttl: healthy ? HEALTHY_TTL_MS : UNHEALTHY_TTL_MS
|
|
178
|
+
});
|
|
179
|
+
return healthy;
|
|
180
|
+
});
|
|
181
|
+
healthInflight.set(key, probe);
|
|
182
|
+
try {
|
|
183
|
+
return await probe;
|
|
184
|
+
} finally {
|
|
185
|
+
if (healthInflight.get(key) === probe) healthInflight.delete(key);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
async function createSocksDispatcher(proxyUrl, dispatcherOpts) {
|
|
189
|
+
const { SocksProxyAgent } = await import("socks-proxy-agent");
|
|
190
|
+
return new SocksProxyAgent(proxyUrl);
|
|
191
|
+
}
|
|
192
|
+
function createHttpDispatcher(proxyUrl, dispatcherOpts) {
|
|
193
|
+
const clean = proxyUrl.replace(/\?family=(ipv4|ipv6)$/, "");
|
|
194
|
+
return new ProxyAgent({
|
|
195
|
+
uri: clean,
|
|
196
|
+
proxyTunnel: true,
|
|
197
|
+
...dispatcherOpts
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
const DISPATCHER_OPTS = {
|
|
201
|
+
headersTimeout: 3e4,
|
|
202
|
+
bodyTimeout: 3e4,
|
|
203
|
+
connectTimeout: 1e4,
|
|
204
|
+
keepAliveTimeout: 1,
|
|
205
|
+
keepAliveMaxTimeout: 1,
|
|
206
|
+
pipelining: 0
|
|
207
|
+
};
|
|
208
|
+
async function dispatcherForAsync(proxyUrl) {
|
|
209
|
+
if (!proxyUrl) return envAgent;
|
|
210
|
+
const normalized = normalizeProxyUrl(proxyUrl);
|
|
211
|
+
const cached = dispatcherCache.get(normalized);
|
|
212
|
+
if (cached) return cached;
|
|
213
|
+
const famClean = normalized.replace(/\?family=(ipv4|ipv6)$/, "");
|
|
214
|
+
let dispatcher;
|
|
215
|
+
if (famClean.startsWith("socks5:")) dispatcher = await createSocksDispatcher(normalized, DISPATCHER_OPTS);
|
|
216
|
+
else dispatcher = createHttpDispatcher(normalized, DISPATCHER_OPTS);
|
|
217
|
+
dispatcherCache.set(normalized, dispatcher);
|
|
218
|
+
return dispatcher;
|
|
219
|
+
}
|
|
220
|
+
function getTargetUrlString(input) {
|
|
221
|
+
if (typeof input === "string") return input;
|
|
222
|
+
try {
|
|
223
|
+
if (input instanceof URL) return input.toString();
|
|
224
|
+
if (typeof input.url === "string") return input.url;
|
|
225
|
+
} catch {}
|
|
226
|
+
return "";
|
|
227
|
+
}
|
|
228
|
+
/** fetch() that respects per-account proxyUrl (when given) otherwise env. */
|
|
229
|
+
const proxiedFetch = async (input, init, opts) => {
|
|
230
|
+
const proxyUrl = opts?.proxyUrl ?? init?.proxyUrl;
|
|
231
|
+
const cleanInit = { ...init };
|
|
232
|
+
if (cleanInit.proxyUrl) delete cleanInit.proxyUrl;
|
|
233
|
+
if (!proxyUrl) return fetch(input, {
|
|
234
|
+
...cleanInit,
|
|
235
|
+
dispatcher: envAgent
|
|
236
|
+
});
|
|
237
|
+
const targetStr = getTargetUrlString(input);
|
|
238
|
+
if (targetStr && isLoopbackUrl(targetStr)) return fetch(input, cleanInit);
|
|
239
|
+
try {
|
|
240
|
+
if (!await isProxyReachable(proxyUrl)) {
|
|
241
|
+
const err = /* @__PURE__ */ new Error(`[Proxy Fast-Fail] Proxy unreachable: ${proxyUrlForLogs(proxyUrl)}`);
|
|
242
|
+
err.code = "PROXY_UNREACHABLE";
|
|
243
|
+
err.errorCode = "proxy_unreachable";
|
|
244
|
+
err.statusCode = 503;
|
|
245
|
+
throw err;
|
|
246
|
+
}
|
|
247
|
+
} catch (e) {
|
|
248
|
+
if (e?.errorCode === "proxy_unreachable") throw e;
|
|
249
|
+
if (e instanceof Error && e.message.startsWith("[proxy]")) throw e;
|
|
250
|
+
}
|
|
251
|
+
let dispatcher;
|
|
252
|
+
try {
|
|
253
|
+
dispatcher = await dispatcherForAsync(proxyUrl);
|
|
254
|
+
} catch (e) {
|
|
255
|
+
throw tagProxyUnreachable(e);
|
|
256
|
+
}
|
|
257
|
+
try {
|
|
258
|
+
return await fetch(input, {
|
|
259
|
+
...cleanInit,
|
|
260
|
+
dispatcher
|
|
261
|
+
});
|
|
262
|
+
} catch (err) {
|
|
263
|
+
throw tagProxyUnreachable(err);
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
//#endregion
|
|
267
|
+
export { proxyUrlForLogs as a, proxiedFetch as i, isProxyUnreachableError as n, proxy_exports as o, normalizeProxyUrl as r, isProxyReachable as t };
|
|
268
|
+
|
|
269
|
+
//# sourceMappingURL=proxy-DQPIwUov.mjs.map
|