@humain/humain-code 0.5.0 → 0.5.3
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/CHANGELOG.md +37 -1
- package/README.md +3 -1
- package/dist/agent-monitor/client.d.ts +2 -2
- package/dist/agent-monitor/client.d.ts.map +1 -1
- package/dist/agent-monitor/client.js +3 -2
- package/dist/agent-monitor/client.js.map +1 -1
- package/dist/bundle/chunks/anthropic.js +10 -9
- package/dist/bundle/chunks/{chunk-DR37WKFZ.js → chunk-AO6FF5JL.js} +1 -1
- package/dist/bundle/chunks/{chunk-Q3SFYT2X.js → chunk-EY3LZOUP.js} +1356 -1358
- package/dist/bundle/chunks/{chunk-T3HXWZG4.js → chunk-L2VKM23Q.js} +76 -2
- package/dist/bundle/chunks/{chunk-5C4VL2AR.js → chunk-ODJUBJK2.js} +2 -2
- package/dist/bundle/chunks/{chunk-A6YBWW4T.js → chunk-UJLCKIMZ.js} +646 -495
- package/dist/bundle/chunks/{chunk-476XBL3W.js → chunk-Y3OPKKDR.js} +3 -77
- package/dist/bundle/chunks/{cli-VXU7OWFU.js → cli-ENEFJH2B.js} +1 -1
- package/dist/bundle/chunks/{compat-S2IBVV6V.js → compat-CJBWWYHE.js} +1 -1
- package/dist/bundle/chunks/{dist-645GFIWH.js → dist-RKNCFFGM.js} +2 -2
- package/dist/bundle/chunks/github-copilot.js +28 -26
- package/dist/bundle/chunks/openai-chatgpt.js +2 -2
- package/dist/bundle/chunks/openai-codex.js +10 -9
- package/dist/bundle/chunks/openrouter.js +10 -9
- package/dist/bundle/chunks/radius.js +9 -8
- package/dist/bundle/chunks/{runtime-5VFZTMWL.js → runtime-K3F2QDQP.js} +1 -1
- package/dist/bundle/chunks/{virtual-modules-GU6AW5ZK.js → virtual-modules-5D6QIJPN.js} +2 -2
- package/dist/bundle/cli-runtime.js +1 -1
- package/dist/bundle/index.js +4 -4
- package/dist/bundle/rpc-entry.js +1 -1
- package/dist/core/model-resolver.d.ts.map +1 -1
- package/dist/core/model-resolver.js +19 -1
- package/dist/core/model-resolver.js.map +1 -1
- package/dist/core/model-runtime.d.ts.map +1 -1
- package/dist/core/model-runtime.js +24 -2
- package/dist/core/model-runtime.js.map +1 -1
- package/dist/core/safe-credential-error.d.ts +11 -0
- package/dist/core/safe-credential-error.d.ts.map +1 -0
- package/dist/core/safe-credential-error.js +14 -0
- package/dist/core/safe-credential-error.js.map +1 -0
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +4 -1
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/settings-schema.d.ts +3 -0
- package/dist/core/settings-schema.d.ts.map +1 -1
- package/dist/core/settings-schema.js +6 -0
- package/dist/core/settings-schema.js.map +1 -1
- package/dist/humain/gateway/catalog.d.ts +36 -0
- package/dist/humain/gateway/catalog.d.ts.map +1 -0
- package/dist/humain/gateway/catalog.js +74 -0
- package/dist/humain/gateway/catalog.js.map +1 -0
- package/dist/humain/gateway/client.d.ts +58 -0
- package/dist/humain/gateway/client.d.ts.map +1 -0
- package/dist/humain/gateway/client.js +212 -0
- package/dist/humain/gateway/client.js.map +1 -0
- package/dist/humain/gateway/constants.d.ts +23 -0
- package/dist/humain/gateway/constants.d.ts.map +1 -0
- package/dist/humain/gateway/constants.js +32 -0
- package/dist/humain/gateway/constants.js.map +1 -0
- package/dist/humain/gateway/http.d.ts +18 -0
- package/dist/humain/gateway/http.d.ts.map +1 -0
- package/dist/humain/gateway/http.js +274 -0
- package/dist/humain/gateway/http.js.map +1 -0
- package/dist/humain/gateway/login.d.ts +13 -0
- package/dist/humain/gateway/login.d.ts.map +1 -0
- package/dist/humain/gateway/login.js +121 -0
- package/dist/humain/gateway/login.js.map +1 -0
- package/dist/humain/gateway/provider.d.ts +34 -0
- package/dist/humain/gateway/provider.d.ts.map +1 -0
- package/dist/humain/gateway/provider.js +253 -0
- package/dist/humain/gateway/provider.js.map +1 -0
- package/dist/humain/gateway/secret-store.d.ts +19 -0
- package/dist/humain/gateway/secret-store.d.ts.map +1 -0
- package/dist/humain/gateway/secret-store.js +252 -0
- package/dist/humain/gateway/secret-store.js.map +1 -0
- package/dist/humain/gateway/session-store.d.ts +42 -0
- package/dist/humain/gateway/session-store.d.ts.map +1 -0
- package/dist/humain/gateway/session-store.js +175 -0
- package/dist/humain/gateway/session-store.js.map +1 -0
- package/dist/humain/index.d.ts.map +1 -1
- package/dist/humain/index.js +2 -0
- package/dist/humain/index.js.map +1 -1
- package/dist/humain/login-menu.d.ts +14 -0
- package/dist/humain/login-menu.d.ts.map +1 -0
- package/dist/humain/login-menu.js +21 -0
- package/dist/humain/login-menu.js.map +1 -0
- package/dist/humain/node-http.d.ts.map +1 -1
- package/dist/humain/node-http.js +6 -31
- package/dist/humain/node-http.js.map +1 -1
- package/dist/humain/redact-assistant-message.d.ts +4 -0
- package/dist/humain/redact-assistant-message.d.ts.map +1 -0
- package/dist/humain/redact-assistant-message.js +34 -0
- package/dist/humain/redact-assistant-message.js.map +1 -0
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +17 -4
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/login-dialog.d.ts +1 -1
- package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
- package/dist/modes/interactive/components/login-dialog.js +3 -2
- package/dist/modes/interactive/components/login-dialog.js.map +1 -1
- package/dist/modes/interactive/components/model-selector.d.ts +11 -0
- package/dist/modes/interactive/components/model-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/model-selector.js +16 -3
- package/dist/modes/interactive/components/model-selector.js.map +1 -1
- package/dist/modes/interactive/components/radius-login-selector.d.ts +5 -4
- package/dist/modes/interactive/components/radius-login-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/radius-login-selector.js +11 -9
- package/dist/modes/interactive/components/radius-login-selector.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +2 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +53 -36
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/utils/sleep.d.ts +1 -1
- package/dist/utils/sleep.d.ts.map +1 -1
- package/dist/utils/sleep.js +10 -6
- package/dist/utils/sleep.js.map +1 -1
- package/docs/humain-code.md +13 -1
- package/docs/index.md +1 -1
- package/docs/models.md +3 -1
- package/docs/providers.md +58 -2
- package/docs/quickstart.md +8 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/schemas/settings.schema.json +19 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { SafeCredentialError } from "../../core/safe-credential-error.ts";
|
|
2
|
+
interface GatewayIdentity {
|
|
3
|
+
email: string;
|
|
4
|
+
userId: string;
|
|
5
|
+
}
|
|
6
|
+
export type GatewayModelEntry = {
|
|
7
|
+
id: string;
|
|
8
|
+
maxInputTokens?: number;
|
|
9
|
+
maxOutputTokens?: number;
|
|
10
|
+
};
|
|
11
|
+
type GatewayAccessKind = "unrestricted" | "scoped" | "none" | "unknown";
|
|
12
|
+
export interface GatewayAccess {
|
|
13
|
+
accessKind: GatewayAccessKind;
|
|
14
|
+
models: string[];
|
|
15
|
+
modelModes: Record<string, string>;
|
|
16
|
+
harnessLabels: Record<string, Record<string, {
|
|
17
|
+
label: string;
|
|
18
|
+
reason: string | null;
|
|
19
|
+
}>> | null;
|
|
20
|
+
degraded: {
|
|
21
|
+
reason: string;
|
|
22
|
+
} | null;
|
|
23
|
+
}
|
|
24
|
+
interface GatewayRequestOptions {
|
|
25
|
+
fetch?: typeof fetch;
|
|
26
|
+
signal: AbortSignal;
|
|
27
|
+
}
|
|
28
|
+
type GatewayLoginErrorKind = "invalid-request" | "invalid-code" | "update-required" | "malformed-response";
|
|
29
|
+
export declare class GatewayLoginError extends SafeCredentialError {
|
|
30
|
+
constructor(kind: GatewayLoginErrorKind, message?: string);
|
|
31
|
+
}
|
|
32
|
+
export declare class GatewayBudgetExhaustedError extends Error {
|
|
33
|
+
constructor(message: string);
|
|
34
|
+
}
|
|
35
|
+
export declare class GatewaySessionUnavailableError extends SafeCredentialError {
|
|
36
|
+
constructor();
|
|
37
|
+
}
|
|
38
|
+
export declare class GatewayMalformedResponseError extends SafeCredentialError {
|
|
39
|
+
constructor();
|
|
40
|
+
}
|
|
41
|
+
export declare class GatewayRequestError extends SafeCredentialError {
|
|
42
|
+
readonly status: number;
|
|
43
|
+
constructor(status: number);
|
|
44
|
+
}
|
|
45
|
+
type UnknownRecord = Record<string, unknown>;
|
|
46
|
+
export declare function isObject(value: unknown): value is UnknownRecord;
|
|
47
|
+
export declare function exchangeHandoffCode(base: string, input: {
|
|
48
|
+
code: string;
|
|
49
|
+
state: string;
|
|
50
|
+
}, options: GatewayRequestOptions): Promise<{
|
|
51
|
+
token: string;
|
|
52
|
+
identity: GatewayIdentity;
|
|
53
|
+
}>;
|
|
54
|
+
export declare function revokeSession(base: string, token: string, options: GatewayRequestOptions): Promise<"revoked" | "unknown" | "unavailable">;
|
|
55
|
+
export declare function fetchGatewayModels(base: string, token: string, options: GatewayRequestOptions): Promise<GatewayModelEntry[]>;
|
|
56
|
+
export declare function fetchGatewayAccess(base: string, token: string, options: GatewayRequestOptions): Promise<GatewayAccess>;
|
|
57
|
+
export {};
|
|
58
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/humain/gateway/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAG1E,UAAU,eAAe;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CACf;AAED,MAAM,MAAM,iBAAiB,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IAAC,eAAe,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAElG,KAAK,iBAAiB,GAAG,cAAc,GAAG,QAAQ,GAA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;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,EACtC,OAAO,EAAE,qBAAqB,GAC5B,OAAO,CAAC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,eAAe,CAAA;CAAE,CAAC,CA0CvD;AAED,wBAAsB,aAAa,CAClC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,qBAAqB,GAC5B,OAAO,CAAC,SAAS,GAAG,SAAS,GAAG,aAAa,CAAC,CAehD;AAsBD,wBAAsB,kBAAkB,CACvC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,qBAAqB,GAC5B,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAc9B;AAoBD,wBAAsB,kBAAkB,CACvC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,qBAAqB,GAC5B,OAAO,CAAC,aAAa,CAAC,CAqBxB"}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { SafeCredentialError } from "../../core/safe-credential-error.js";
|
|
2
|
+
import { GATEWAY_TOKEN_PATTERN, GATEWAY_USER_AGENT } from "./constants.js";
|
|
3
|
+
const LOGIN_MESSAGES = {
|
|
4
|
+
"invalid-request": "HUMAIN Gateway rejected the sign-in exchange request.",
|
|
5
|
+
"invalid-code": "HUMAIN Gateway sign-in code is invalid or expired. Sign in again.",
|
|
6
|
+
"update-required": "HUMAIN Gateway requires a newer HUMAIN Code CLI. Update and sign in again.",
|
|
7
|
+
"malformed-response": "HUMAIN Gateway returned an unexpected sign-in response.",
|
|
8
|
+
};
|
|
9
|
+
export class GatewayLoginError extends SafeCredentialError {
|
|
10
|
+
constructor(kind, message = LOGIN_MESSAGES[kind]) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = "GatewayLoginError";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export class GatewayBudgetExhaustedError extends Error {
|
|
16
|
+
constructor(message) {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = "GatewayBudgetExhaustedError";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export class GatewaySessionUnavailableError extends SafeCredentialError {
|
|
22
|
+
constructor() {
|
|
23
|
+
super("HUMAIN Gateway CLI session unavailable. Sign in again.");
|
|
24
|
+
this.name = "GatewaySessionUnavailableError";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export class GatewayMalformedResponseError extends SafeCredentialError {
|
|
28
|
+
constructor() {
|
|
29
|
+
super("HUMAIN Gateway returned an unexpected response.");
|
|
30
|
+
this.name = "GatewayMalformedResponseError";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export class GatewayRequestError extends SafeCredentialError {
|
|
34
|
+
status;
|
|
35
|
+
constructor(status) {
|
|
36
|
+
super(`HUMAIN Gateway request failed with status ${status}.`);
|
|
37
|
+
this.name = "GatewayRequestError";
|
|
38
|
+
this.status = status;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export function isObject(value) {
|
|
42
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
43
|
+
}
|
|
44
|
+
function str(value) {
|
|
45
|
+
return typeof value === "string" ? value : undefined;
|
|
46
|
+
}
|
|
47
|
+
function positiveInt(value) {
|
|
48
|
+
return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : undefined;
|
|
49
|
+
}
|
|
50
|
+
async function readJson(response) {
|
|
51
|
+
try {
|
|
52
|
+
return await response.json();
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function baseHeaders(token) {
|
|
59
|
+
const headers = { "User-Agent": GATEWAY_USER_AGENT };
|
|
60
|
+
if (token !== undefined)
|
|
61
|
+
headers.Authorization = `Bearer ${token}`;
|
|
62
|
+
return headers;
|
|
63
|
+
}
|
|
64
|
+
export async function exchangeHandoffCode(base, input, options) {
|
|
65
|
+
const doFetch = options.fetch ?? fetch;
|
|
66
|
+
const payload = JSON.stringify({
|
|
67
|
+
code: input.code,
|
|
68
|
+
state: input.state,
|
|
69
|
+
client: "cli",
|
|
70
|
+
cli_capability: "cli-session-v1",
|
|
71
|
+
});
|
|
72
|
+
// Gateway caps the exchange body at 4 KB; sending more would spend the one-time code on a rejected request.
|
|
73
|
+
if (Buffer.byteLength(payload) > 4096) {
|
|
74
|
+
throw new GatewayLoginError("invalid-request", "The pasted HUMAIN Gateway code is too long.");
|
|
75
|
+
}
|
|
76
|
+
let response;
|
|
77
|
+
try {
|
|
78
|
+
response = await doFetch(`${base}/handoff/exchange`, {
|
|
79
|
+
method: "POST",
|
|
80
|
+
redirect: "error",
|
|
81
|
+
signal: options.signal,
|
|
82
|
+
headers: { ...baseHeaders(), "Content-Type": "application/json" },
|
|
83
|
+
body: payload,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
if (options.signal?.aborted)
|
|
88
|
+
throw error;
|
|
89
|
+
throw new SafeCredentialError("Could not reach HUMAIN Gateway to finish sign-in. Check your network and sign in again.");
|
|
90
|
+
}
|
|
91
|
+
if (response.status === 426)
|
|
92
|
+
throw new GatewayLoginError("update-required");
|
|
93
|
+
if (response.status === 400) {
|
|
94
|
+
const text = (await response.text().catch(() => "")).trim();
|
|
95
|
+
throw new GatewayLoginError(text === "invalid exchange request" ? "invalid-request" : "invalid-code");
|
|
96
|
+
}
|
|
97
|
+
if (!response.ok)
|
|
98
|
+
throw new GatewayRequestError(response.status);
|
|
99
|
+
const body = await readJson(response);
|
|
100
|
+
if (!isObject(body))
|
|
101
|
+
throw new GatewayLoginError("malformed-response");
|
|
102
|
+
const token = str(body.key);
|
|
103
|
+
const email = str(body.user_email) || str(body.email);
|
|
104
|
+
// Gateway may return an empty user_id; the email then identifies the account.
|
|
105
|
+
const userId = str(body.user_id) || email;
|
|
106
|
+
if (token === undefined || !GATEWAY_TOKEN_PATTERN.test(token))
|
|
107
|
+
throw new GatewayLoginError("malformed-response");
|
|
108
|
+
if (!userId || !email)
|
|
109
|
+
throw new GatewayLoginError("malformed-response");
|
|
110
|
+
return { token, identity: { email, userId } };
|
|
111
|
+
}
|
|
112
|
+
export async function revokeSession(base, token, options) {
|
|
113
|
+
const doFetch = options.fetch ?? fetch;
|
|
114
|
+
try {
|
|
115
|
+
const response = await doFetch(`${base}/api/cli/me/session`, {
|
|
116
|
+
method: "DELETE",
|
|
117
|
+
redirect: "error",
|
|
118
|
+
signal: options.signal,
|
|
119
|
+
headers: baseHeaders(token),
|
|
120
|
+
});
|
|
121
|
+
if (response.status === 204)
|
|
122
|
+
return "revoked";
|
|
123
|
+
if (response.status === 401)
|
|
124
|
+
return "unknown";
|
|
125
|
+
return "unavailable";
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return "unavailable";
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
async function authedGet(base, path, token, options) {
|
|
132
|
+
const doFetch = options.fetch ?? fetch;
|
|
133
|
+
const response = await doFetch(`${base}${path}`, {
|
|
134
|
+
method: "GET",
|
|
135
|
+
redirect: "error",
|
|
136
|
+
signal: options.signal,
|
|
137
|
+
headers: baseHeaders(token),
|
|
138
|
+
});
|
|
139
|
+
if (response.status === 401)
|
|
140
|
+
throw new GatewaySessionUnavailableError();
|
|
141
|
+
if (response.status === 429) {
|
|
142
|
+
const body = await readJson(response);
|
|
143
|
+
const error = isObject(body) && isObject(body.error) ? body.error : undefined;
|
|
144
|
+
if (error?.type === "budget_exceeded" || error?.code === "gateway_budget_exhausted") {
|
|
145
|
+
throw new GatewayBudgetExhaustedError(str(error?.message) ?? "HUMAIN Gateway budget exhausted.");
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (!response.ok)
|
|
149
|
+
throw new GatewayRequestError(response.status);
|
|
150
|
+
return readJson(response);
|
|
151
|
+
}
|
|
152
|
+
export async function fetchGatewayModels(base, token, options) {
|
|
153
|
+
const body = await authedGet(base, "/v1/models", token, options);
|
|
154
|
+
if (!isObject(body) || !Array.isArray(body.data))
|
|
155
|
+
throw new GatewayMalformedResponseError();
|
|
156
|
+
const models = [];
|
|
157
|
+
for (const entry of body.data) {
|
|
158
|
+
if (!isObject(entry) || typeof entry.id !== "string" || entry.id === "")
|
|
159
|
+
continue;
|
|
160
|
+
const model = { id: entry.id };
|
|
161
|
+
const maxInputTokens = positiveInt(entry.max_input_tokens);
|
|
162
|
+
const maxOutputTokens = positiveInt(entry.max_output_tokens);
|
|
163
|
+
if (maxInputTokens !== undefined)
|
|
164
|
+
model.maxInputTokens = maxInputTokens;
|
|
165
|
+
if (maxOutputTokens !== undefined)
|
|
166
|
+
model.maxOutputTokens = maxOutputTokens;
|
|
167
|
+
models.push(model);
|
|
168
|
+
}
|
|
169
|
+
return models;
|
|
170
|
+
}
|
|
171
|
+
const ACCESS_KINDS = ["unrestricted", "scoped", "none"];
|
|
172
|
+
function parseHarnessLabels(value) {
|
|
173
|
+
if (!isObject(value))
|
|
174
|
+
return null;
|
|
175
|
+
const result = {};
|
|
176
|
+
for (const [harness, models] of Object.entries(value)) {
|
|
177
|
+
if (!isObject(models))
|
|
178
|
+
continue;
|
|
179
|
+
const inner = {};
|
|
180
|
+
for (const [model, info] of Object.entries(models)) {
|
|
181
|
+
if (isObject(info) && typeof info.label === "string") {
|
|
182
|
+
inner[model] = { label: info.label, reason: str(info.reason) ?? null };
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
result[harness] = inner;
|
|
186
|
+
}
|
|
187
|
+
return result;
|
|
188
|
+
}
|
|
189
|
+
export async function fetchGatewayAccess(base, token, options) {
|
|
190
|
+
const body = await authedGet(base, "/api/cli/me/access", token, options);
|
|
191
|
+
if (!isObject(body))
|
|
192
|
+
throw new GatewayMalformedResponseError();
|
|
193
|
+
const modelModes = {};
|
|
194
|
+
if (isObject(body.model_modes)) {
|
|
195
|
+
for (const [model, mode] of Object.entries(body.model_modes)) {
|
|
196
|
+
if (typeof mode === "string")
|
|
197
|
+
modelModes[model] = mode;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const degraded = body.degraded === null || body.degraded === undefined || body.degraded === false
|
|
201
|
+
? null
|
|
202
|
+
: { reason: isObject(body.degraded) ? (str(body.degraded.reason) ?? "unknown") : "unknown" };
|
|
203
|
+
const kind = str(body.access_kind);
|
|
204
|
+
return {
|
|
205
|
+
accessKind: kind !== undefined && ACCESS_KINDS.includes(kind) ? kind : "unknown",
|
|
206
|
+
models: Array.isArray(body.models) ? body.models.filter((m) => typeof m === "string") : [],
|
|
207
|
+
modelModes,
|
|
208
|
+
harnessLabels: parseHarnessLabels(body.harness_labels),
|
|
209
|
+
degraded,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../../src/humain/gateway/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,qCAAqC,CAAC;AAC1E,OAAO,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AA2B3E,MAAM,cAAc,GAA0C;IAC7D,iBAAiB,EAAE,uDAAuD;IAC1E,cAAc,EAAE,mEAAmE;IACnF,iBAAiB,EAAE,4EAA4E;IAC/F,oBAAoB,EAAE,yDAAyD;CAC/E,CAAC;AAEF,MAAM,OAAO,iBAAkB,SAAQ,mBAAmB;IACzD,YAAY,IAA2B,EAAE,OAAO,GAAG,cAAc,CAAC,IAAI,CAAC;QACtE,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,mBAAmB,CAAC;IACjC,CAAC;CACD;AAED,MAAM,OAAO,2BAA4B,SAAQ,KAAK;IACrD,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,6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;QACV,aAAa,EAAE,kBAAkB,CAAC,IAAI,CAAC,cAAc,CAAC;QACtD,QAAQ;KACR,CAAC;AACH,CAAC","sourcesContent":["import { SafeCredentialError } from \"../../core/safe-credential-error.ts\";\nimport { GATEWAY_TOKEN_PATTERN, GATEWAY_USER_AGENT } from \"./constants.ts\";\n\ninterface GatewayIdentity {\n\temail: string;\n\tuserId: string;\n}\n\nexport type GatewayModelEntry = { id: string; maxInputTokens?: number; maxOutputTokens?: number };\n\ntype GatewayAccessKind = \"unrestricted\" | \"scoped\" | \"none\" | \"unknown\";\n\nexport interface GatewayAccess {\n\taccessKind: GatewayAccessKind;\n\tmodels: string[];\n\tmodelModes: Record<string, string>;\n\t// harness -> model id -> label (wire: harness_labels.code.<model id>)\n\tharnessLabels: Record<string, Record<string, { label: string; reason: string | null }>> | null;\n\tdegraded: { reason: string } | null;\n}\n\ninterface GatewayRequestOptions {\n\tfetch?: typeof fetch;\n\tsignal: AbortSignal;\n}\n\ntype GatewayLoginErrorKind = \"invalid-request\" | \"invalid-code\" | \"update-required\" | \"malformed-response\";\n\nconst LOGIN_MESSAGES: Record<GatewayLoginErrorKind, string> = {\n\t\"invalid-request\": \"HUMAIN Gateway rejected the sign-in exchange request.\",\n\t\"invalid-code\": \"HUMAIN Gateway sign-in code is invalid or expired. Sign in again.\",\n\t\"update-required\": \"HUMAIN Gateway requires a newer HUMAIN Code CLI. Update and sign in again.\",\n\t\"malformed-response\": \"HUMAIN Gateway returned an unexpected sign-in response.\",\n};\n\nexport class GatewayLoginError extends SafeCredentialError {\n\tconstructor(kind: GatewayLoginErrorKind, message = LOGIN_MESSAGES[kind]) {\n\t\tsuper(message);\n\t\tthis.name = \"GatewayLoginError\";\n\t}\n}\n\nexport class GatewayBudgetExhaustedError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"GatewayBudgetExhaustedError\";\n\t}\n}\n\nexport class GatewaySessionUnavailableError extends SafeCredentialError {\n\tconstructor() {\n\t\tsuper(\"HUMAIN Gateway CLI session unavailable. Sign in again.\");\n\t\tthis.name = \"GatewaySessionUnavailableError\";\n\t}\n}\n\nexport class GatewayMalformedResponseError extends SafeCredentialError {\n\tconstructor() {\n\t\tsuper(\"HUMAIN Gateway returned an unexpected response.\");\n\t\tthis.name = \"GatewayMalformedResponseError\";\n\t}\n}\n\nexport class GatewayRequestError extends SafeCredentialError {\n\treadonly status: number;\n\tconstructor(status: number) {\n\t\tsuper(`HUMAIN Gateway request failed with status ${status}.`);\n\t\tthis.name = \"GatewayRequestError\";\n\t\tthis.status = status;\n\t}\n}\n\ntype UnknownRecord = Record<string, unknown>;\n\nexport function isObject(value: unknown): value is UnknownRecord {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nfunction str(value: unknown): string | undefined {\n\treturn typeof value === \"string\" ? value : undefined;\n}\n\nfunction positiveInt(value: unknown): number | undefined {\n\treturn typeof value === \"number\" && Number.isInteger(value) && value > 0 ? value : undefined;\n}\n\nasync function readJson(response: Response): Promise<unknown> {\n\ttry {\n\t\treturn await response.json();\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction baseHeaders(token?: string): Record<string, string> {\n\tconst headers: Record<string, string> = { \"User-Agent\": GATEWAY_USER_AGENT };\n\tif (token !== undefined) headers.Authorization = `Bearer ${token}`;\n\treturn headers;\n}\n\nexport async function exchangeHandoffCode(\n\tbase: string,\n\tinput: { code: string; state: string },\n\toptions: GatewayRequestOptions,\n): Promise<{ token: string; identity: GatewayIdentity }> {\n\tconst doFetch = options.fetch ?? fetch;\n\tconst payload = JSON.stringify({\n\t\tcode: input.code,\n\t\tstate: input.state,\n\t\tclient: \"cli\",\n\t\tcli_capability: \"cli-session-v1\",\n\t});\n\t// Gateway caps the exchange body at 4 KB; sending more would spend the one-time code on a rejected request.\n\tif (Buffer.byteLength(payload) > 4096) {\n\t\tthrow new GatewayLoginError(\"invalid-request\", \"The pasted HUMAIN Gateway code is too long.\");\n\t}\n\tlet response: Response;\n\ttry {\n\t\tresponse = await doFetch(`${base}/handoff/exchange`, {\n\t\t\tmethod: \"POST\",\n\t\t\tredirect: \"error\",\n\t\t\tsignal: options.signal,\n\t\t\theaders: { ...baseHeaders(), \"Content-Type\": \"application/json\" },\n\t\t\tbody: payload,\n\t\t});\n\t} catch (error) {\n\t\tif (options.signal?.aborted) throw error;\n\t\tthrow new SafeCredentialError(\n\t\t\t\"Could not reach HUMAIN Gateway to finish sign-in. Check your network and sign in again.\",\n\t\t);\n\t}\n\tif (response.status === 426) throw new GatewayLoginError(\"update-required\");\n\tif (response.status === 400) {\n\t\tconst text = (await response.text().catch(() => \"\")).trim();\n\t\tthrow new GatewayLoginError(text === \"invalid exchange request\" ? \"invalid-request\" : \"invalid-code\");\n\t}\n\tif (!response.ok) throw new GatewayRequestError(response.status);\n\tconst body = await readJson(response);\n\tif (!isObject(body)) throw new GatewayLoginError(\"malformed-response\");\n\tconst token = str(body.key);\n\tconst email = str(body.user_email) || str(body.email);\n\t// Gateway may return an empty user_id; the email then identifies the account.\n\tconst userId = str(body.user_id) || email;\n\tif (token === undefined || !GATEWAY_TOKEN_PATTERN.test(token)) throw new GatewayLoginError(\"malformed-response\");\n\tif (!userId || !email) throw new GatewayLoginError(\"malformed-response\");\n\treturn { token, identity: { email, userId } };\n}\n\nexport async function revokeSession(\n\tbase: string,\n\ttoken: string,\n\toptions: GatewayRequestOptions,\n): Promise<\"revoked\" | \"unknown\" | \"unavailable\"> {\n\tconst doFetch = options.fetch ?? fetch;\n\ttry {\n\t\tconst response = await doFetch(`${base}/api/cli/me/session`, {\n\t\t\tmethod: \"DELETE\",\n\t\t\tredirect: \"error\",\n\t\t\tsignal: options.signal,\n\t\t\theaders: baseHeaders(token),\n\t\t});\n\t\tif (response.status === 204) return \"revoked\";\n\t\tif (response.status === 401) return \"unknown\";\n\t\treturn \"unavailable\";\n\t} catch {\n\t\treturn \"unavailable\";\n\t}\n}\n\nasync function authedGet(base: string, path: string, token: string, options: GatewayRequestOptions): Promise<unknown> {\n\tconst doFetch = options.fetch ?? fetch;\n\tconst response = await doFetch(`${base}${path}`, {\n\t\tmethod: \"GET\",\n\t\tredirect: \"error\",\n\t\tsignal: options.signal,\n\t\theaders: baseHeaders(token),\n\t});\n\tif (response.status === 401) throw new GatewaySessionUnavailableError();\n\tif (response.status === 429) {\n\t\tconst body = await readJson(response);\n\t\tconst error = isObject(body) && isObject(body.error) ? body.error : undefined;\n\t\tif (error?.type === \"budget_exceeded\" || error?.code === \"gateway_budget_exhausted\") {\n\t\t\tthrow new GatewayBudgetExhaustedError(str(error?.message) ?? \"HUMAIN Gateway budget exhausted.\");\n\t\t}\n\t}\n\tif (!response.ok) throw new GatewayRequestError(response.status);\n\treturn readJson(response);\n}\n\nexport async function fetchGatewayModels(\n\tbase: string,\n\ttoken: string,\n\toptions: GatewayRequestOptions,\n): Promise<GatewayModelEntry[]> {\n\tconst body = await authedGet(base, \"/v1/models\", token, options);\n\tif (!isObject(body) || !Array.isArray(body.data)) throw new GatewayMalformedResponseError();\n\tconst models: GatewayModelEntry[] = [];\n\tfor (const entry of body.data) {\n\t\tif (!isObject(entry) || typeof entry.id !== \"string\" || entry.id === \"\") continue;\n\t\tconst model: GatewayModelEntry = { id: entry.id };\n\t\tconst maxInputTokens = positiveInt(entry.max_input_tokens);\n\t\tconst maxOutputTokens = positiveInt(entry.max_output_tokens);\n\t\tif (maxInputTokens !== undefined) model.maxInputTokens = maxInputTokens;\n\t\tif (maxOutputTokens !== undefined) model.maxOutputTokens = maxOutputTokens;\n\t\tmodels.push(model);\n\t}\n\treturn models;\n}\n\nconst ACCESS_KINDS: readonly string[] = [\"unrestricted\", \"scoped\", \"none\"];\n\nfunction parseHarnessLabels(value: unknown): GatewayAccess[\"harnessLabels\"] {\n\tif (!isObject(value)) return null;\n\tconst result: NonNullable<GatewayAccess[\"harnessLabels\"]> = {};\n\tfor (const [harness, models] of Object.entries(value)) {\n\t\tif (!isObject(models)) continue;\n\t\tconst inner: Record<string, { label: string; reason: string | null }> = {};\n\t\tfor (const [model, info] of Object.entries(models)) {\n\t\t\tif (isObject(info) && typeof info.label === \"string\") {\n\t\t\t\tinner[model] = { label: info.label, reason: str(info.reason) ?? null };\n\t\t\t}\n\t\t}\n\t\tresult[harness] = inner;\n\t}\n\treturn result;\n}\n\nexport async function fetchGatewayAccess(\n\tbase: string,\n\ttoken: string,\n\toptions: GatewayRequestOptions,\n): Promise<GatewayAccess> {\n\tconst body = await authedGet(base, \"/api/cli/me/access\", token, options);\n\tif (!isObject(body)) throw new GatewayMalformedResponseError();\n\tconst modelModes: Record<string, string> = {};\n\tif (isObject(body.model_modes)) {\n\t\tfor (const [model, mode] of Object.entries(body.model_modes)) {\n\t\t\tif (typeof mode === \"string\") modelModes[model] = mode;\n\t\t}\n\t}\n\tconst degraded =\n\t\tbody.degraded === null || body.degraded === undefined || body.degraded === false\n\t\t\t? null\n\t\t\t: { reason: isObject(body.degraded) ? (str(body.degraded.reason) ?? \"unknown\") : \"unknown\" };\n\tconst kind = str(body.access_kind);\n\treturn {\n\t\taccessKind: kind !== undefined && ACCESS_KINDS.includes(kind) ? (kind as GatewayAccessKind) : \"unknown\",\n\t\tmodels: Array.isArray(body.models) ? body.models.filter((m): m is string => typeof m === \"string\") : [],\n\t\tmodelModes,\n\t\tharnessLabels: parseHarnessLabels(body.harness_labels),\n\t\tdegraded,\n\t};\n}\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export declare const HUMAIN_GATEWAY_PROVIDER = "humain-gateway";
|
|
2
|
+
export type GatewayEnvironment = "staging" | "production";
|
|
3
|
+
export declare const GATEWAY_BASE_URLS: Record<GatewayEnvironment, string>;
|
|
4
|
+
export declare const GATEWAY_KNOWN_LIMITS: Record<string, {
|
|
5
|
+
input: number;
|
|
6
|
+
output: number;
|
|
7
|
+
}>;
|
|
8
|
+
/** Provisional client limits for models not in GATEWAY_KNOWN_LIMITS; not guaranteed server limits. */
|
|
9
|
+
export declare const GATEWAY_DEFAULT_LIMITS: {
|
|
10
|
+
input: number;
|
|
11
|
+
output: number;
|
|
12
|
+
};
|
|
13
|
+
export declare const GATEWAY_DEFAULT_MODEL = "zai-org/GLM-5.3-Flash";
|
|
14
|
+
export declare const GATEWAY_USER_AGENT: string;
|
|
15
|
+
export declare const GATEWAY_TOKEN_PATTERN: RegExp;
|
|
16
|
+
/** Credential metadata keys stored in the (token-free) auth.json `env` map. */
|
|
17
|
+
export declare const GATEWAY_ENV_ENVIRONMENT = "HUMAIN_GATEWAY_ENVIRONMENT";
|
|
18
|
+
export declare const GATEWAY_ENV_SESSION_ID = "HUMAIN_GATEWAY_SESSION_ID";
|
|
19
|
+
export declare const GATEWAY_ENV_EMAIL = "HUMAIN_GATEWAY_EMAIL";
|
|
20
|
+
export declare const GATEWAY_ENV_USER_ID = "HUMAIN_GATEWAY_USER_ID";
|
|
21
|
+
/** `/accounts` label from token-free credential metadata; undefined when the environment is missing. */
|
|
22
|
+
export declare function describeGatewayCredential(env: Record<string, string> | undefined): string | undefined;
|
|
23
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/humain/gateway/constants.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,uBAAuB,mBAAmB,CAAC;AAExD,MAAM,MAAM,kBAAkB,GAAG,SAAS,GAAG,YAAY,CAAC;AAE1D,eAAO,MAAM,iBAAiB,EAAE,MAAM,CAAC,kBAAkB,EAAE,MAAM,CAGhE,CAAC;AAEF,eAAO,MAAM,oBAAoB,EAAE,MAAM,CAAC,MAAM,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAMlF,CAAC;AAEF,sGAAsG;AACtG,eAAO,MAAM,sBAAsB;IAAK,KAAK;IAAU,MAAM;CAAS,CAAC;AAEvE,eAAO,MAAM,qBAAqB,0BAA0B,CAAC;AAE7D,eAAO,MAAM,kBAAkB,QAA2B,CAAC;AAE3D,eAAO,MAAM,qBAAqB,QAAwB,CAAC;AAE3D,+EAA+E;AAC/E,eAAO,MAAM,uBAAuB,+BAA+B,CAAC;AACpE,eAAO,MAAM,sBAAsB,8BAA8B,CAAC;AAClE,eAAO,MAAM,iBAAiB,yBAAyB,CAAC;AACxD,eAAO,MAAM,mBAAmB,2BAA2B,CAAC;AAE5D,wGAAwG;AACxG,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAKrG"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { VERSION } from "../../config.js";
|
|
2
|
+
export const HUMAIN_GATEWAY_PROVIDER = "humain-gateway";
|
|
3
|
+
export const GATEWAY_BASE_URLS = {
|
|
4
|
+
staging: "https://gateway.stg.humain.services/ai-gateway",
|
|
5
|
+
production: "https://gateway.humain.services/ai-gateway",
|
|
6
|
+
};
|
|
7
|
+
export const GATEWAY_KNOWN_LIMITS = {
|
|
8
|
+
"moonshotai/Kimi-K3": { input: 1048576, output: 131072 },
|
|
9
|
+
"zai-org/GLM-5.3-Flash": { input: 1048576, output: 131072 },
|
|
10
|
+
"DeepSeek-V4.1-Flash": { input: 1048576, output: 131072 },
|
|
11
|
+
"Qwen3.8-Flash-Next": { input: 262144, output: 32768 },
|
|
12
|
+
"gpt-oss-120b": { input: 131072, output: 32768 },
|
|
13
|
+
};
|
|
14
|
+
/** Provisional client limits for models not in GATEWAY_KNOWN_LIMITS; not guaranteed server limits. */
|
|
15
|
+
export const GATEWAY_DEFAULT_LIMITS = { input: 131072, output: 16384 };
|
|
16
|
+
export const GATEWAY_DEFAULT_MODEL = "zai-org/GLM-5.3-Flash";
|
|
17
|
+
export const GATEWAY_USER_AGENT = `humain-code/${VERSION}`;
|
|
18
|
+
export const GATEWAY_TOKEN_PATTERN = /^hcli-[0-9a-f]{64}$/;
|
|
19
|
+
/** Credential metadata keys stored in the (token-free) auth.json `env` map. */
|
|
20
|
+
export const GATEWAY_ENV_ENVIRONMENT = "HUMAIN_GATEWAY_ENVIRONMENT";
|
|
21
|
+
export const GATEWAY_ENV_SESSION_ID = "HUMAIN_GATEWAY_SESSION_ID";
|
|
22
|
+
export const GATEWAY_ENV_EMAIL = "HUMAIN_GATEWAY_EMAIL";
|
|
23
|
+
export const GATEWAY_ENV_USER_ID = "HUMAIN_GATEWAY_USER_ID";
|
|
24
|
+
/** `/accounts` label from token-free credential metadata; undefined when the environment is missing. */
|
|
25
|
+
export function describeGatewayCredential(env) {
|
|
26
|
+
const environment = env?.[GATEWAY_ENV_ENVIRONMENT];
|
|
27
|
+
if (!environment)
|
|
28
|
+
return undefined;
|
|
29
|
+
const email = env[GATEWAY_ENV_EMAIL];
|
|
30
|
+
return `HUMAIN Gateway session (${email ? `${environment}, ${email}` : environment})`;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/humain/gateway/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE1C,MAAM,CAAC,MAAM,uBAAuB,GAAG,gBAAgB,CAAC;AAIxD,MAAM,CAAC,MAAM,iBAAiB,GAAuC;IACpE,OAAO,EAAE,gDAAgD;IACzD,UAAU,EAAE,4CAA4C;CACxD,CAAC;AAEF,MAAM,CAAC,MAAM,oBAAoB,GAAsD;IACtF,oBAAoB,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE;IACxD,uBAAuB,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE;IAC3D,qBAAqB,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE;IACzD,oBAAoB,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE;IACtD,cAAc,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE;CAChD,CAAC;AAEF,sGAAsG;AACtG,MAAM,CAAC,MAAM,sBAAsB,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;AAEvE,MAAM,CAAC,MAAM,qBAAqB,GAAG,uBAAuB,CAAC;AAE7D,MAAM,CAAC,MAAM,kBAAkB,GAAG,eAAe,OAAO,EAAE,CAAC;AAE3D,MAAM,CAAC,MAAM,qBAAqB,GAAG,qBAAqB,CAAC;AAE3D,+EAA+E;AAC/E,MAAM,CAAC,MAAM,uBAAuB,GAAG,4BAA4B,CAAC;AACpE,MAAM,CAAC,MAAM,sBAAsB,GAAG,2BAA2B,CAAC;AAClE,MAAM,CAAC,MAAM,iBAAiB,GAAG,sBAAsB,CAAC;AACxD,MAAM,CAAC,MAAM,mBAAmB,GAAG,wBAAwB,CAAC;AAE5D,wGAAwG;AACxG,MAAM,UAAU,yBAAyB,CAAC,GAAuC;IAChF,MAAM,WAAW,GAAG,GAAG,EAAE,CAAC,uBAAuB,CAAC,CAAC;IACnD,IAAI,CAAC,WAAW;QAAE,OAAO,SAAS,CAAC;IACnC,MAAM,KAAK,GAAG,GAAG,CAAC,iBAAiB,CAAC,CAAC;IACrC,OAAO,2BAA2B,KAAK,CAAC,CAAC,CAAC,GAAG,WAAW,KAAK,KAAK,EAAE,CAAC,CAAC,CAAC,WAAW,GAAG,CAAC;AACvF,CAAC","sourcesContent":["import { VERSION } from \"../../config.ts\";\n\nexport const HUMAIN_GATEWAY_PROVIDER = \"humain-gateway\";\n\nexport type GatewayEnvironment = \"staging\" | \"production\";\n\nexport const GATEWAY_BASE_URLS: Record<GatewayEnvironment, string> = {\n\tstaging: \"https://gateway.stg.humain.services/ai-gateway\",\n\tproduction: \"https://gateway.humain.services/ai-gateway\",\n};\n\nexport const GATEWAY_KNOWN_LIMITS: Record<string, { input: number; output: number }> = {\n\t\"moonshotai/Kimi-K3\": { input: 1048576, output: 131072 },\n\t\"zai-org/GLM-5.3-Flash\": { input: 1048576, output: 131072 },\n\t\"DeepSeek-V4.1-Flash\": { input: 1048576, output: 131072 },\n\t\"Qwen3.8-Flash-Next\": { input: 262144, output: 32768 },\n\t\"gpt-oss-120b\": { input: 131072, output: 32768 },\n};\n\n/** Provisional client limits for models not in GATEWAY_KNOWN_LIMITS; not guaranteed server limits. */\nexport const GATEWAY_DEFAULT_LIMITS = { input: 131072, output: 16384 };\n\nexport const GATEWAY_DEFAULT_MODEL = \"zai-org/GLM-5.3-Flash\";\n\nexport const GATEWAY_USER_AGENT = `humain-code/${VERSION}`;\n\nexport const GATEWAY_TOKEN_PATTERN = /^hcli-[0-9a-f]{64}$/;\n\n/** Credential metadata keys stored in the (token-free) auth.json `env` map. */\nexport const GATEWAY_ENV_ENVIRONMENT = \"HUMAIN_GATEWAY_ENVIRONMENT\";\nexport const GATEWAY_ENV_SESSION_ID = \"HUMAIN_GATEWAY_SESSION_ID\";\nexport const GATEWAY_ENV_EMAIL = \"HUMAIN_GATEWAY_EMAIL\";\nexport const GATEWAY_ENV_USER_ID = \"HUMAIN_GATEWAY_USER_ID\";\n\n/** `/accounts` label from token-free credential metadata; undefined when the environment is missing. */\nexport function describeGatewayCredential(env: Record<string, string> | undefined): string | undefined {\n\tconst environment = env?.[GATEWAY_ENV_ENVIRONMENT];\n\tif (!environment) return undefined;\n\tconst email = env[GATEWAY_ENV_EMAIL];\n\treturn `HUMAIN Gateway session (${email ? `${environment}, ${email}` : environment})`;\n}\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type ProviderStreams } from "@earendil-works/pi-ai";
|
|
2
|
+
interface GatewayHttpDeps {
|
|
3
|
+
sleep?: (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
4
|
+
random?: () => number;
|
|
5
|
+
/** Test hook; production always uses the 900-second ceiling. */
|
|
6
|
+
requestCeilingMs?: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Decide whether a failed Gateway response is safe to resend. Only responses that prove the
|
|
10
|
+
* request never reached a billed model call qualify; everything else fails to avoid double billing.
|
|
11
|
+
*/
|
|
12
|
+
export declare function classifyGatewayRetry(status: number, body: unknown, headers: Headers, requestedModel: string): {
|
|
13
|
+
retry: boolean;
|
|
14
|
+
delayMs?: number;
|
|
15
|
+
};
|
|
16
|
+
export declare function withHumainGatewayHttp(api: ProviderStreams, deps?: GatewayHttpDeps): ProviderStreams;
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../../src/humain/gateway/http.ts"],"names":[],"mappings":"AAAA,OAAO,EAON,KAAK,eAAe,EAGpB,MAAM,uBAAuB,CAAC;AAe/B,UAAU,eAAe;IACxB,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5D,MAAM,CAAC,EAAE,MAAM,MAAM,CAAC;IACtB,gEAAgE;IAChE,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC1B;AA8BD;;;GAGG;AACH,wBAAgB,oBAAoB,CACnC,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,OAAO,EAChB,cAAc,EAAE,MAAM,GACpB;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,CAgCtC;AAmBD,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,eAAe,EAAE,IAAI,GAAE,eAAoB,GAAG,eAAe,CA6JvG"}
|