@gatectr/sdk 0.1.0
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/LICENSE +21 -0
- package/README.md +199 -0
- package/dist/cjs/client.d.ts +50 -0
- package/dist/cjs/client.d.ts.map +1 -0
- package/dist/cjs/client.js +316 -0
- package/dist/cjs/client.js.map +1 -0
- package/dist/cjs/errors.d.ts +42 -0
- package/dist/cjs/errors.d.ts.map +1 -0
- package/dist/cjs/errors.js +84 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/http.d.ts +26 -0
- package/dist/cjs/http.d.ts.map +1 -0
- package/dist/cjs/http.js +164 -0
- package/dist/cjs/http.js.map +1 -0
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +15 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/stream.d.ts +8 -0
- package/dist/cjs/stream.d.ts.map +1 -0
- package/dist/cjs/stream.js +100 -0
- package/dist/cjs/stream.js.map +1 -0
- package/dist/cjs/types.d.ts +155 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +3 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/esm/client.d.ts +50 -0
- package/dist/esm/client.d.ts.map +1 -0
- package/dist/esm/client.js +312 -0
- package/dist/esm/client.js.map +1 -0
- package/dist/esm/errors.d.ts +42 -0
- package/dist/esm/errors.d.ts.map +1 -0
- package/dist/esm/errors.js +75 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/http.d.ts +26 -0
- package/dist/esm/http.d.ts.map +1 -0
- package/dist/esm/http.js +160 -0
- package/dist/esm/http.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/stream.d.ts +8 -0
- package/dist/esm/stream.d.ts.map +1 -0
- package/dist/esm/stream.js +97 -0
- package/dist/esm/stream.js.map +1 -0
- package/dist/esm/types.d.ts +155 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/types.js.map +1 -0
- package/package.json +88 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// sdk-node/src/errors.ts
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.GateCtrNetworkError = exports.GateCtrStreamError = exports.GateCtrTimeoutError = exports.GateCtrApiError = exports.GateCtrConfigError = exports.GateCtrError = void 0;
|
|
5
|
+
/** Base error class for all GateCtr SDK errors */
|
|
6
|
+
class GateCtrError extends Error {
|
|
7
|
+
constructor(message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "GateCtrError";
|
|
10
|
+
// Fix prototype chain for instanceof checks in transpiled code
|
|
11
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.GateCtrError = GateCtrError;
|
|
15
|
+
/** Thrown for invalid or missing configuration (e.g., no API key) */
|
|
16
|
+
class GateCtrConfigError extends GateCtrError {
|
|
17
|
+
constructor(message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "GateCtrConfigError";
|
|
20
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.GateCtrConfigError = GateCtrConfigError;
|
|
24
|
+
/** Thrown when the Platform returns a non-2xx HTTP response */
|
|
25
|
+
class GateCtrApiError extends GateCtrError {
|
|
26
|
+
status;
|
|
27
|
+
code;
|
|
28
|
+
requestId;
|
|
29
|
+
constructor(opts) {
|
|
30
|
+
super(opts.message);
|
|
31
|
+
this.name = "GateCtrApiError";
|
|
32
|
+
this.status = opts.status;
|
|
33
|
+
this.code = opts.code;
|
|
34
|
+
this.requestId = opts.requestId;
|
|
35
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
36
|
+
}
|
|
37
|
+
/** Returns a plain object safe for JSON.stringify in logging pipelines. Never includes the API key. */
|
|
38
|
+
toJSON() {
|
|
39
|
+
return {
|
|
40
|
+
name: this.name,
|
|
41
|
+
message: this.message,
|
|
42
|
+
status: this.status,
|
|
43
|
+
code: this.code,
|
|
44
|
+
requestId: this.requestId,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.GateCtrApiError = GateCtrApiError;
|
|
49
|
+
/** Thrown when a request exceeds the configured timeout */
|
|
50
|
+
class GateCtrTimeoutError extends GateCtrError {
|
|
51
|
+
timeoutMs;
|
|
52
|
+
constructor(timeoutMs) {
|
|
53
|
+
super(`Request timed out after ${String(timeoutMs)}ms`);
|
|
54
|
+
this.name = "GateCtrTimeoutError";
|
|
55
|
+
this.timeoutMs = timeoutMs;
|
|
56
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.GateCtrTimeoutError = GateCtrTimeoutError;
|
|
60
|
+
/** Thrown when a streaming connection fails mid-stream */
|
|
61
|
+
class GateCtrStreamError extends GateCtrError {
|
|
62
|
+
constructor(message, cause) {
|
|
63
|
+
super(message);
|
|
64
|
+
this.name = "GateCtrStreamError";
|
|
65
|
+
if (cause !== undefined) {
|
|
66
|
+
this.cause = cause;
|
|
67
|
+
}
|
|
68
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.GateCtrStreamError = GateCtrStreamError;
|
|
72
|
+
/** Thrown for DNS failures, connection refused, and other transport-level errors */
|
|
73
|
+
class GateCtrNetworkError extends GateCtrError {
|
|
74
|
+
constructor(message, cause) {
|
|
75
|
+
super(message);
|
|
76
|
+
this.name = "GateCtrNetworkError";
|
|
77
|
+
if (cause !== undefined) {
|
|
78
|
+
this.cause = cause;
|
|
79
|
+
}
|
|
80
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.GateCtrNetworkError = GateCtrNetworkError;
|
|
84
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":";AAAA,yBAAyB;;;AAEzB,kDAAkD;AAClD,MAAa,YAAa,SAAQ,KAAK;IACrC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;QAC3B,+DAA+D;QAC/D,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;CACF;AAPD,oCAOC;AAED,qEAAqE;AACrE,MAAa,kBAAmB,SAAQ,YAAY;IAClD,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,oBAAoB,CAAC;QACjC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;CACF;AAND,gDAMC;AAED,+DAA+D;AAC/D,MAAa,eAAgB,SAAQ,YAAY;IACtC,MAAM,CAAS;IACf,IAAI,CAAS;IACb,SAAS,CAAqB;IAEvC,YAAY,IAA2E;QACrF,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;QAC9B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACtB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;QAChC,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpD,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QACrB,CAAC;QACD,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;CACF;AATD,kDASC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface RequestOptions {
|
|
2
|
+
method: "GET" | "POST";
|
|
3
|
+
url: string;
|
|
4
|
+
headers: Record<string, string>;
|
|
5
|
+
body?: unknown;
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
timeoutMs: number;
|
|
8
|
+
maxRetries: number;
|
|
9
|
+
}
|
|
10
|
+
export interface RawResponse {
|
|
11
|
+
status: number;
|
|
12
|
+
headers: Headers;
|
|
13
|
+
body: ReadableStream<Uint8Array> | null;
|
|
14
|
+
json(): Promise<unknown>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Exponential backoff with jitter.
|
|
18
|
+
* delay = min(500 * 2^attempt + jitter(0-100ms), 10_000ms)
|
|
19
|
+
*/
|
|
20
|
+
export declare function backoffMs(attempt: number): number;
|
|
21
|
+
/**
|
|
22
|
+
* Core HTTP request function with retry loop and timeout.
|
|
23
|
+
* Throws typed GateCtr errors — never raw fetch errors.
|
|
24
|
+
*/
|
|
25
|
+
export declare function httpRequest(opts: RequestOptions): Promise<RawResponse>;
|
|
26
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/http.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,KAAK,GAAG,MAAM,CAAC;IACvB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC;IACxC,IAAI,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;CAC1B;AAQD;;;GAGG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAIjD;AA6BD;;;GAGG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,cAAc,GAAG,OAAO,CAAC,WAAW,CAAC,CAuI5E"}
|
package/dist/cjs/http.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.backoffMs = backoffMs;
|
|
4
|
+
exports.httpRequest = httpRequest;
|
|
5
|
+
// sdk-node/src/http.ts
|
|
6
|
+
const errors_js_1 = require("./errors.js");
|
|
7
|
+
/** Status codes that warrant automatic retry */
|
|
8
|
+
const RETRYABLE_STATUSES = new Set([429, 500, 502, 503, 504]);
|
|
9
|
+
/** Status codes that should never be retried */
|
|
10
|
+
const NON_RETRYABLE_STATUSES = new Set([400, 401, 403, 404]);
|
|
11
|
+
/**
|
|
12
|
+
* Exponential backoff with jitter.
|
|
13
|
+
* delay = min(500 * 2^attempt + jitter(0-100ms), 10_000ms)
|
|
14
|
+
*/
|
|
15
|
+
function backoffMs(attempt) {
|
|
16
|
+
const base = 500 * Math.pow(2, attempt);
|
|
17
|
+
const jitter = Math.floor(Math.random() * 101);
|
|
18
|
+
return Math.min(base + jitter, 10_000);
|
|
19
|
+
}
|
|
20
|
+
/** Sleep for the given number of milliseconds */
|
|
21
|
+
function sleep(ms) {
|
|
22
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Combines multiple AbortSignals into one. Safe for Node 18+.
|
|
26
|
+
* The returned signal aborts when any of the input signals abort.
|
|
27
|
+
*/
|
|
28
|
+
function combineSignals(signals) {
|
|
29
|
+
const controller = new AbortController();
|
|
30
|
+
for (const signal of signals) {
|
|
31
|
+
if (signal.aborted) {
|
|
32
|
+
controller.abort(signal.reason);
|
|
33
|
+
return controller.signal;
|
|
34
|
+
}
|
|
35
|
+
signal.addEventListener("abort", () => {
|
|
36
|
+
controller.abort(signal.reason);
|
|
37
|
+
}, { once: true });
|
|
38
|
+
}
|
|
39
|
+
return controller.signal;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Core HTTP request function with retry loop and timeout.
|
|
43
|
+
* Throws typed GateCtr errors — never raw fetch errors.
|
|
44
|
+
*/
|
|
45
|
+
async function httpRequest(opts) {
|
|
46
|
+
const { method, url, headers, body, signal: callerSignal, timeoutMs, maxRetries } = opts;
|
|
47
|
+
let lastError;
|
|
48
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
49
|
+
// Apply backoff before retry attempts (not before the first attempt)
|
|
50
|
+
if (attempt > 0) {
|
|
51
|
+
await sleep(backoffMs(attempt - 1));
|
|
52
|
+
}
|
|
53
|
+
// Create a per-attempt AbortController for timeout
|
|
54
|
+
const timeoutController = new AbortController();
|
|
55
|
+
const timeoutId = setTimeout(() => {
|
|
56
|
+
timeoutController.abort();
|
|
57
|
+
}, timeoutMs);
|
|
58
|
+
// Combine caller signal with timeout signal
|
|
59
|
+
const combinedSignal = callerSignal
|
|
60
|
+
? combineSignals([callerSignal, timeoutController.signal])
|
|
61
|
+
: timeoutController.signal;
|
|
62
|
+
let response;
|
|
63
|
+
try {
|
|
64
|
+
response = await fetch(url, {
|
|
65
|
+
method,
|
|
66
|
+
headers,
|
|
67
|
+
...(body !== undefined ? { body: JSON.stringify(body) } : {}),
|
|
68
|
+
signal: combinedSignal,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
clearTimeout(timeoutId);
|
|
73
|
+
// Distinguish timeout from other network errors
|
|
74
|
+
if (timeoutController.signal.aborted && !callerSignal?.aborted) {
|
|
75
|
+
throw new errors_js_1.GateCtrTimeoutError(timeoutMs);
|
|
76
|
+
}
|
|
77
|
+
// Caller cancelled — rethrow as-is so the caller can handle it
|
|
78
|
+
if (callerSignal?.aborted) {
|
|
79
|
+
throw err;
|
|
80
|
+
}
|
|
81
|
+
// Other network error (DNS, ECONNREFUSED, etc.)
|
|
82
|
+
lastError = new errors_js_1.GateCtrNetworkError(`Network error: ${err instanceof Error ? err.message : String(err)}`, err);
|
|
83
|
+
// Network errors are retryable
|
|
84
|
+
if (attempt < maxRetries) {
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
throw lastError;
|
|
88
|
+
}
|
|
89
|
+
clearTimeout(timeoutId);
|
|
90
|
+
// 2xx — success
|
|
91
|
+
if (response.status >= 200 && response.status < 300) {
|
|
92
|
+
return {
|
|
93
|
+
status: response.status,
|
|
94
|
+
headers: response.headers,
|
|
95
|
+
body: response.body,
|
|
96
|
+
json: () => response.json(),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
// Non-retryable client errors — throw immediately
|
|
100
|
+
if (NON_RETRYABLE_STATUSES.has(response.status)) {
|
|
101
|
+
const requestId = response.headers.get("x-gatectr-request-id") ?? undefined;
|
|
102
|
+
let code = "api_error";
|
|
103
|
+
let message = `HTTP ${String(response.status)}`;
|
|
104
|
+
try {
|
|
105
|
+
const errorBody = (await response.json());
|
|
106
|
+
if (typeof errorBody["code"] === "string")
|
|
107
|
+
code = errorBody["code"];
|
|
108
|
+
if (typeof errorBody["message"] === "string")
|
|
109
|
+
message = errorBody["message"];
|
|
110
|
+
if (typeof errorBody["error"] === "string")
|
|
111
|
+
message = errorBody["error"];
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
// ignore parse errors
|
|
115
|
+
}
|
|
116
|
+
throw new errors_js_1.GateCtrApiError({
|
|
117
|
+
message,
|
|
118
|
+
status: response.status,
|
|
119
|
+
code,
|
|
120
|
+
...(requestId !== undefined ? { requestId } : {}),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
// Retryable server errors
|
|
124
|
+
if (RETRYABLE_STATUSES.has(response.status)) {
|
|
125
|
+
const requestId = response.headers.get("x-gatectr-request-id") ?? undefined;
|
|
126
|
+
let code = "server_error";
|
|
127
|
+
let message = `HTTP ${String(response.status)}`;
|
|
128
|
+
try {
|
|
129
|
+
const errorBody = (await response.json());
|
|
130
|
+
if (typeof errorBody["code"] === "string")
|
|
131
|
+
code = errorBody["code"];
|
|
132
|
+
if (typeof errorBody["message"] === "string")
|
|
133
|
+
message = errorBody["message"];
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
// ignore parse errors
|
|
137
|
+
}
|
|
138
|
+
lastError = new errors_js_1.GateCtrApiError({
|
|
139
|
+
message,
|
|
140
|
+
status: response.status,
|
|
141
|
+
code,
|
|
142
|
+
...(requestId !== undefined ? { requestId } : {}),
|
|
143
|
+
});
|
|
144
|
+
if (attempt < maxRetries) {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
throw lastError;
|
|
148
|
+
}
|
|
149
|
+
// Unknown status — treat as non-retryable
|
|
150
|
+
const requestId = response.headers.get("x-gatectr-request-id") ?? undefined;
|
|
151
|
+
throw new errors_js_1.GateCtrApiError({
|
|
152
|
+
message: `Unexpected HTTP status ${String(response.status)}`,
|
|
153
|
+
status: response.status,
|
|
154
|
+
code: "unexpected_status",
|
|
155
|
+
...(requestId !== undefined ? { requestId } : {}),
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
// Should never reach here, but TypeScript needs it
|
|
159
|
+
const fallback = lastError instanceof Error
|
|
160
|
+
? lastError
|
|
161
|
+
: new errors_js_1.GateCtrNetworkError("Request failed after retries");
|
|
162
|
+
throw fallback;
|
|
163
|
+
}
|
|
164
|
+
//# sourceMappingURL=http.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":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rConfigError, GateCtrApiError, GateCtrTimeoutError, GateCtrStreamError, GateCtrNetworkError, } from "./errors.js";
|
|
3
|
+
export type { GateCtrConfig, PerRequestOptions, Message, GateCtrMetadata, UsageCounts, CompleteParams, CompleteResponse, ChatParams, ChatResponse, StreamParams, StreamChunk, ModelInfo, ModelsResponse, UsageParams, UsageByProject, UsageResponse, } from "./types.js";
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEtC,OAAO,EACL,YAAY,EACZ,kBAAkB,EAClB,eAAe,EACf,mBAAmB,EACnB,kBAAkB,EAClB,mBAAmB,GACpB,MAAM,aAAa,CAAC;AAErB,YAAY,EACV,aAAa,EACb,iBAAiB,EACjB,OAAO,EACP,eAAe,EACf,WAAW,EACX,cAAc,EACd,gBAAgB,EAChB,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,SAAS,EACT,cAAc,EACd,WAAW,EACX,cAAc,EACd,aAAa,GACd,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// sdk-node/src/index.ts
|
|
3
|
+
// Public API surface for @gatectr/sdk
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.GateCtrNetworkError = exports.GateCtrStreamError = exports.GateCtrTimeoutError = exports.GateCtrApiError = exports.GateCtrConfigError = exports.GateCtrError = exports.GateCtr = void 0;
|
|
6
|
+
var client_js_1 = require("./client.js");
|
|
7
|
+
Object.defineProperty(exports, "GateCtr", { enumerable: true, get: function () { return client_js_1.GateCtr; } });
|
|
8
|
+
var errors_js_1 = require("./errors.js");
|
|
9
|
+
Object.defineProperty(exports, "GateCtrError", { enumerable: true, get: function () { return errors_js_1.GateCtrError; } });
|
|
10
|
+
Object.defineProperty(exports, "GateCtrConfigError", { enumerable: true, get: function () { return errors_js_1.GateCtrConfigError; } });
|
|
11
|
+
Object.defineProperty(exports, "GateCtrApiError", { enumerable: true, get: function () { return errors_js_1.GateCtrApiError; } });
|
|
12
|
+
Object.defineProperty(exports, "GateCtrTimeoutError", { enumerable: true, get: function () { return errors_js_1.GateCtrTimeoutError; } });
|
|
13
|
+
Object.defineProperty(exports, "GateCtrStreamError", { enumerable: true, get: function () { return errors_js_1.GateCtrStreamError; } });
|
|
14
|
+
Object.defineProperty(exports, "GateCtrNetworkError", { enumerable: true, get: function () { return errors_js_1.GateCtrNetworkError; } });
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA,wBAAwB;AACxB,sCAAsC;;;AAEtC,yCAAsC;AAA7B,oGAAA,OAAO,OAAA;AAEhB,yCAOqB;AANnB,yGAAA,YAAY,OAAA;AACZ,+GAAA,kBAAkB,OAAA;AAClB,4GAAA,eAAe,OAAA;AACf,gHAAA,mBAAmB,OAAA;AACnB,+GAAA,kBAAkB,OAAA;AAClB,gHAAA,mBAAmB,OAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { StreamChunk } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Parses a Server-Sent Events (SSE) stream from a ReadableStream body.
|
|
4
|
+
* Yields StreamChunk for each data line, stops cleanly on [DONE] sentinel.
|
|
5
|
+
* Propagates GateCtrStreamError on connection abort or parse error.
|
|
6
|
+
*/
|
|
7
|
+
export declare function parseSSE(body: ReadableStream<Uint8Array>, signal?: AbortSignal): AsyncGenerator<StreamChunk>;
|
|
8
|
+
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/stream.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C;;;;GAIG;AACH,wBAAuB,QAAQ,CAC7B,IAAI,EAAE,cAAc,CAAC,UAAU,CAAC,EAChC,MAAM,CAAC,EAAE,WAAW,GACnB,cAAc,CAAC,WAAW,CAAC,CAqG7B"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseSSE = parseSSE;
|
|
4
|
+
// sdk-node/src/stream.ts
|
|
5
|
+
const errors_js_1 = require("./errors.js");
|
|
6
|
+
/**
|
|
7
|
+
* Parses a Server-Sent Events (SSE) stream from a ReadableStream body.
|
|
8
|
+
* Yields StreamChunk for each data line, stops cleanly on [DONE] sentinel.
|
|
9
|
+
* Propagates GateCtrStreamError on connection abort or parse error.
|
|
10
|
+
*/
|
|
11
|
+
async function* parseSSE(body, signal) {
|
|
12
|
+
const decoder = new TextDecoder();
|
|
13
|
+
const reader = body.getReader();
|
|
14
|
+
let buffer = "";
|
|
15
|
+
try {
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
17
|
+
while (true) {
|
|
18
|
+
// Check for cancellation before each read
|
|
19
|
+
if (signal !== undefined && signal.aborted) {
|
|
20
|
+
throw new errors_js_1.GateCtrStreamError("Stream cancelled by caller");
|
|
21
|
+
}
|
|
22
|
+
let done;
|
|
23
|
+
let value;
|
|
24
|
+
try {
|
|
25
|
+
({ done, value } = await reader.read());
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
throw new errors_js_1.GateCtrStreamError(`Stream read error: ${err instanceof Error ? err.message : String(err)}`, err);
|
|
29
|
+
}
|
|
30
|
+
if (done)
|
|
31
|
+
break;
|
|
32
|
+
if (value !== undefined) {
|
|
33
|
+
buffer += decoder.decode(value, { stream: true });
|
|
34
|
+
}
|
|
35
|
+
// Process all complete lines in the buffer
|
|
36
|
+
const lines = buffer.split("\n");
|
|
37
|
+
// Keep the last (potentially incomplete) line in the buffer
|
|
38
|
+
buffer = lines.pop() ?? "";
|
|
39
|
+
for (const line of lines) {
|
|
40
|
+
const trimmed = line.trim();
|
|
41
|
+
// Skip empty lines and comment lines
|
|
42
|
+
if (trimmed === "" || trimmed.startsWith(":"))
|
|
43
|
+
continue;
|
|
44
|
+
// Parse data lines
|
|
45
|
+
if (trimmed.startsWith("data:")) {
|
|
46
|
+
const data = trimmed.slice(5).trim();
|
|
47
|
+
// [DONE] sentinel — stream is complete
|
|
48
|
+
if (data === "[DONE]") {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
// Parse JSON payload
|
|
52
|
+
let parsed;
|
|
53
|
+
try {
|
|
54
|
+
parsed = JSON.parse(data);
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
throw new errors_js_1.GateCtrStreamError(`Failed to parse SSE data as JSON: ${data}`, err);
|
|
58
|
+
}
|
|
59
|
+
const chunk = parsed;
|
|
60
|
+
const choices = chunk["choices"];
|
|
61
|
+
const firstChoice = choices?.[0];
|
|
62
|
+
const delta = firstChoice?.["delta"];
|
|
63
|
+
const content = delta?.["content"];
|
|
64
|
+
const finishReason = firstChoice?.["finish_reason"];
|
|
65
|
+
yield {
|
|
66
|
+
id: typeof chunk["id"] === "string" ? chunk["id"] : "",
|
|
67
|
+
delta: typeof content === "string" ? content : null,
|
|
68
|
+
finishReason: typeof finishReason === "string" ? finishReason : null,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// Flush any remaining buffer content
|
|
74
|
+
if (buffer.trim().startsWith("data:")) {
|
|
75
|
+
const data = buffer.trim().slice(5).trim();
|
|
76
|
+
if (data !== "[DONE]" && data !== "") {
|
|
77
|
+
try {
|
|
78
|
+
const parsed = JSON.parse(data);
|
|
79
|
+
const choices = parsed["choices"];
|
|
80
|
+
const firstChoice = choices?.[0];
|
|
81
|
+
const delta = firstChoice?.["delta"];
|
|
82
|
+
const content = delta?.["content"];
|
|
83
|
+
const finishReason = firstChoice?.["finish_reason"];
|
|
84
|
+
yield {
|
|
85
|
+
id: typeof parsed["id"] === "string" ? parsed["id"] : "",
|
|
86
|
+
delta: typeof content === "string" ? content : null,
|
|
87
|
+
finishReason: typeof finishReason === "string" ? finishReason : null,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
// ignore incomplete final chunk
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
finally {
|
|
97
|
+
reader.releaseLock();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/stream.ts"],"names":[],"mappings":";;AASA,4BAwGC;AAjHD,yBAAyB;AACzB,2CAAiD;AAGjD;;;;GAIG;AACI,KAAK,SAAS,CAAC,CAAC,QAAQ,CAC7B,IAAgC,EAChC,MAAoB;IAEpB,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;IAClC,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC;IAChC,IAAI,MAAM,GAAG,EAAE,CAAC;IAEhB,IAAI,CAAC;QACH,uEAAuE;QACvE,OAAO,IAAI,EAAE,CAAC;YACZ,0CAA0C;YAE1C,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;gBAC3C,MAAM,IAAI,8BAAkB,CAAC,4BAA4B,CAAC,CAAC;YAC7D,CAAC;YAED,IAAI,IAAa,CAAC;YAClB,IAAI,KAA6B,CAAC;YAElC,IAAI,CAAC;gBACH,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;YAC1C,CAAC;YAAC,OAAO,GAAG,EAAE,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AC,OAAO,CAAwC,CAAC;oBAC5E,MAAM,OAAO,GAAG,KAAK,EAAE,CAAC,SAAS,CAAC,CAAC;oBACnC,MAAM,YAAY,GAAG,WAAW,EAAE,CAAC,eAAe,CAAC,CAAC;oBAEpD,MAAM;wBACJ,EAAE,EAAE,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE;wBACxD,KAAK,EAAE,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI;wBACnD,YAAY,EAAE,OAAO,YAAY,KAAK,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI;qBACrE,CAAC;gBACJ,CAAC;gBAAC,MAAM,CAAC;oBACP,gCAAgC;gBAClC,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;YAAS,CAAC;QACT,MAAM,CAAC,WAAW,EAAE,CAAC;IACvB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/** Client configuration passed to the GateCtr constructor */
|
|
2
|
+
export interface GateCtrConfig {
|
|
3
|
+
/** Bearer token for authentication. Falls back to GATECTR_API_KEY env var. */
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
/** Base URL for the GateCtr API. Default: "https://api.gatectr.com/v1" */
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
/** Request timeout in milliseconds. Default: 30000 */
|
|
8
|
+
timeout?: number;
|
|
9
|
+
/** Maximum number of retries for transient errors. Default: 3 */
|
|
10
|
+
maxRetries?: number;
|
|
11
|
+
/** Enable Context Optimizer globally. Default: true */
|
|
12
|
+
optimize?: boolean;
|
|
13
|
+
/** Enable Model Router globally. Default: false */
|
|
14
|
+
route?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/** Per-request GateCtr overrides passed in params.gatectr */
|
|
17
|
+
export interface PerRequestOptions {
|
|
18
|
+
/** Budget ID to enforce for this request */
|
|
19
|
+
budgetId?: string;
|
|
20
|
+
/** Override client-level optimize setting for this request */
|
|
21
|
+
optimize?: boolean;
|
|
22
|
+
/** Override client-level route setting for this request */
|
|
23
|
+
route?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/** OpenAI-compatible message shape */
|
|
26
|
+
export interface Message {
|
|
27
|
+
role: "system" | "user" | "assistant";
|
|
28
|
+
content: string;
|
|
29
|
+
}
|
|
30
|
+
/** GateCtr metadata present on every response */
|
|
31
|
+
export interface GateCtrMetadata {
|
|
32
|
+
/** Unique request ID from X-GateCtr-Request-Id header */
|
|
33
|
+
requestId: string;
|
|
34
|
+
/** Request latency in ms from X-GateCtr-Latency-Ms header */
|
|
35
|
+
latencyMs: number;
|
|
36
|
+
/** Whether the request exceeded the budget, from X-GateCtr-Overage header */
|
|
37
|
+
overage: boolean;
|
|
38
|
+
/** The model that actually processed the request */
|
|
39
|
+
modelUsed: string;
|
|
40
|
+
/** Number of tokens saved by the Context Optimizer */
|
|
41
|
+
tokensSaved: number;
|
|
42
|
+
}
|
|
43
|
+
/** Token usage counts */
|
|
44
|
+
export interface UsageCounts {
|
|
45
|
+
prompt_tokens: number;
|
|
46
|
+
completion_tokens: number;
|
|
47
|
+
total_tokens: number;
|
|
48
|
+
}
|
|
49
|
+
/** Parameters for client.complete() */
|
|
50
|
+
export interface CompleteParams {
|
|
51
|
+
model: string;
|
|
52
|
+
messages: Message[];
|
|
53
|
+
max_tokens?: number;
|
|
54
|
+
temperature?: number;
|
|
55
|
+
/** Per-request GateCtr overrides */
|
|
56
|
+
gatectr?: PerRequestOptions;
|
|
57
|
+
/** AbortSignal to cancel the request */
|
|
58
|
+
signal?: AbortSignal;
|
|
59
|
+
}
|
|
60
|
+
/** Response from client.complete() */
|
|
61
|
+
export interface CompleteResponse {
|
|
62
|
+
id: string;
|
|
63
|
+
object: "text_completion";
|
|
64
|
+
model: string;
|
|
65
|
+
choices: Array<{
|
|
66
|
+
text: string;
|
|
67
|
+
finish_reason: string;
|
|
68
|
+
}>;
|
|
69
|
+
usage: UsageCounts;
|
|
70
|
+
gatectr: GateCtrMetadata;
|
|
71
|
+
}
|
|
72
|
+
/** Parameters for client.chat() */
|
|
73
|
+
export interface ChatParams {
|
|
74
|
+
model: string;
|
|
75
|
+
messages: Message[];
|
|
76
|
+
max_tokens?: number;
|
|
77
|
+
temperature?: number;
|
|
78
|
+
/** Per-request GateCtr overrides */
|
|
79
|
+
gatectr?: PerRequestOptions;
|
|
80
|
+
/** AbortSignal to cancel the request */
|
|
81
|
+
signal?: AbortSignal;
|
|
82
|
+
}
|
|
83
|
+
/** Response from client.chat() */
|
|
84
|
+
export interface ChatResponse {
|
|
85
|
+
id: string;
|
|
86
|
+
object: "chat.completion";
|
|
87
|
+
model: string;
|
|
88
|
+
choices: Array<{
|
|
89
|
+
message: Message;
|
|
90
|
+
finish_reason: string;
|
|
91
|
+
}>;
|
|
92
|
+
usage: UsageCounts;
|
|
93
|
+
gatectr: GateCtrMetadata;
|
|
94
|
+
}
|
|
95
|
+
/** Parameters for client.stream() */
|
|
96
|
+
export interface StreamParams {
|
|
97
|
+
model: string;
|
|
98
|
+
messages: Message[];
|
|
99
|
+
max_tokens?: number;
|
|
100
|
+
temperature?: number;
|
|
101
|
+
/** Per-request GateCtr overrides */
|
|
102
|
+
gatectr?: PerRequestOptions;
|
|
103
|
+
/** AbortSignal to cancel the stream */
|
|
104
|
+
signal?: AbortSignal;
|
|
105
|
+
}
|
|
106
|
+
/** A single SSE chunk yielded by client.stream() */
|
|
107
|
+
export interface StreamChunk {
|
|
108
|
+
/** Completion ID */
|
|
109
|
+
id: string;
|
|
110
|
+
/** Incremental text delta, null on final chunk */
|
|
111
|
+
delta: string | null;
|
|
112
|
+
/** Finish reason, non-null on final chunk */
|
|
113
|
+
finishReason: string | null;
|
|
114
|
+
}
|
|
115
|
+
/** Information about a single available model */
|
|
116
|
+
export interface ModelInfo {
|
|
117
|
+
modelId: string;
|
|
118
|
+
displayName: string;
|
|
119
|
+
provider: string;
|
|
120
|
+
contextWindow: number;
|
|
121
|
+
capabilities: string[];
|
|
122
|
+
}
|
|
123
|
+
/** Response from client.models() */
|
|
124
|
+
export interface ModelsResponse {
|
|
125
|
+
models: ModelInfo[];
|
|
126
|
+
requestId: string;
|
|
127
|
+
}
|
|
128
|
+
/** Optional filters for client.usage() */
|
|
129
|
+
export interface UsageParams {
|
|
130
|
+
/** Start date in YYYY-MM-DD format */
|
|
131
|
+
from?: string;
|
|
132
|
+
/** End date in YYYY-MM-DD format */
|
|
133
|
+
to?: string;
|
|
134
|
+
/** Filter by project ID */
|
|
135
|
+
projectId?: string;
|
|
136
|
+
}
|
|
137
|
+
/** Per-project usage breakdown */
|
|
138
|
+
export interface UsageByProject {
|
|
139
|
+
projectId: string | null;
|
|
140
|
+
totalTokens: number;
|
|
141
|
+
totalRequests: number;
|
|
142
|
+
totalCostUsd: number;
|
|
143
|
+
}
|
|
144
|
+
/** Response from client.usage() */
|
|
145
|
+
export interface UsageResponse {
|
|
146
|
+
totalTokens: number;
|
|
147
|
+
totalRequests: number;
|
|
148
|
+
totalCostUsd: number;
|
|
149
|
+
savedTokens: number;
|
|
150
|
+
from: string;
|
|
151
|
+
to: string;
|
|
152
|
+
byProject: UsageByProject[];
|
|
153
|
+
budgetStatus?: Record<string, unknown>;
|
|
154
|
+
}
|
|
155
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,MAAM,WAAW,aAAa;IAC5B,8EAA8E;IAC9E,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,0EAA0E;IAC1E,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,sDAAsD;IACtD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iEAAiE;IACjE,UAAU,CAAC,EAAE,MAAM,CAAC;IACp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