tolvyn 1.0.1 → 1.0.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/README.md +63 -0
- package/dist/cjs/anthropic.d.ts +20 -0
- package/dist/cjs/anthropic.d.ts.map +1 -0
- package/dist/cjs/anthropic.js +50 -0
- package/dist/cjs/anthropic.js.map +1 -0
- package/dist/cjs/client.d.ts +21 -0
- package/dist/cjs/client.d.ts.map +1 -0
- package/dist/cjs/client.js +83 -0
- package/dist/cjs/client.js.map +1 -0
- package/dist/cjs/failopen.d.ts +7 -0
- package/dist/cjs/failopen.d.ts.map +1 -0
- package/dist/cjs/failopen.js +82 -0
- package/dist/cjs/failopen.js.map +1 -0
- package/dist/cjs/google.d.ts +23 -0
- package/dist/cjs/google.d.ts.map +1 -0
- package/dist/cjs/google.js +47 -0
- package/dist/cjs/google.js.map +1 -0
- package/dist/cjs/index.d.ts +7 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +10 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/anthropic.d.ts +20 -0
- package/dist/esm/anthropic.d.ts.map +1 -0
- package/dist/esm/anthropic.js +43 -0
- package/dist/esm/anthropic.js.map +1 -0
- package/dist/esm/client.d.ts +21 -0
- package/dist/esm/client.d.ts.map +1 -0
- package/dist/esm/client.js +76 -0
- package/dist/esm/client.js.map +1 -0
- package/dist/esm/failopen.d.ts +7 -0
- package/dist/esm/failopen.d.ts.map +1 -0
- package/dist/esm/failopen.js +77 -0
- package/dist/esm/failopen.js.map +1 -0
- package/dist/esm/google.d.ts +23 -0
- package/dist/esm/google.d.ts.map +1 -0
- package/dist/esm/google.js +43 -0
- package/dist/esm/google.js.map +1 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +44 -8
- package/src/anthropic.ts +70 -0
- package/src/client.ts +115 -0
- package/src/failopen.ts +94 -0
- package/src/google.ts +70 -0
- package/src/index.ts +6 -0
- package/index.js +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# tolvyn · npm
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/tolvyn)
|
|
4
|
+
|
|
5
|
+
Drop-in replacement for `openai` and `@anthropic-ai/sdk`.
|
|
6
|
+
One line change. Every AI call metered, attributed, and governed.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install tolvyn
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Quick start
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
// Before
|
|
18
|
+
import OpenAI from "openai";
|
|
19
|
+
const client = new OpenAI();
|
|
20
|
+
|
|
21
|
+
// After — one line change
|
|
22
|
+
import { OpenAI } from "tolvyn";
|
|
23
|
+
const client = new OpenAI({
|
|
24
|
+
tolvynApiKey: "tlv_live_...",
|
|
25
|
+
team: "backend",
|
|
26
|
+
service: "summariser",
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
// Everything else stays the same
|
|
30
|
+
const response = await client.chat.completions.create({
|
|
31
|
+
model: "gpt-4o",
|
|
32
|
+
messages: [{ role: "user", content: "Hello" }],
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Works the same way for Anthropic:
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { Anthropic } from "tolvyn";
|
|
40
|
+
const client = new Anthropic({ tolvynApiKey: "tlv_live_...", team: "ml", service: "classifier" });
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
CommonJS:
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
const { OpenAI } = require("tolvyn");
|
|
47
|
+
const client = new OpenAI({ tolvynApiKey: process.env.TOLVYN_API_KEY, team: "backend" });
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## What you get
|
|
51
|
+
|
|
52
|
+
- **Cost metering** — every request logged with exact token counts and cost in microdollars
|
|
53
|
+
- **Team attribution** — see spend by team and service, not just a total invoice number
|
|
54
|
+
- **Budget enforcement** — set hard limits that block requests before they hit your provider
|
|
55
|
+
- **Immutable ledger** — hash-chained audit trail, verifiable at any time
|
|
56
|
+
- **Drop-in** — no changes to your existing API calls, models, or response handling
|
|
57
|
+
|
|
58
|
+
Full docs: [docs.tolvyn.io/nodejs-sdk](https://docs.tolvyn.io/nodejs-sdk)
|
|
59
|
+
Free trial: [tolvyn.io](https://tolvyn.io)
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
© 2026 TOLVYN. All rights reserved.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TOLVYN Anthropic wrapper — thin drop-in over @anthropic-ai/sdk.
|
|
3
|
+
*/
|
|
4
|
+
import AnthropicBase, { ClientOptions } from '@anthropic-ai/sdk';
|
|
5
|
+
export interface TolvynAnthropicOptions extends Omit<ClientOptions, 'apiKey' | 'baseURL'> {
|
|
6
|
+
tolvynApiKey?: string;
|
|
7
|
+
proxyUrl?: string;
|
|
8
|
+
team?: string;
|
|
9
|
+
service?: string;
|
|
10
|
+
feature?: string;
|
|
11
|
+
agent?: string;
|
|
12
|
+
failOpen?: boolean;
|
|
13
|
+
anthropicApiKey?: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class Anthropic extends AnthropicBase {
|
|
16
|
+
readonly _tolvynFailOpen: boolean;
|
|
17
|
+
readonly _tolvynFallbackKey: string | undefined;
|
|
18
|
+
constructor(options?: TolvynAnthropicOptions);
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=anthropic.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/anthropic.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,aAAa,EAAE,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAMjE,MAAM,WAAW,sBACf,SAAQ,IAAI,CAAC,aAAa,EAAE,QAAQ,GAAG,SAAS,CAAC;IACjD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,qBAAa,SAAU,SAAQ,aAAa;IAC1C,SAAgB,eAAe,EAAE,OAAO,CAAC;IACzC,SAAgB,kBAAkB,EAAE,MAAM,GAAG,SAAS,CAAC;gBAE3C,OAAO,GAAE,sBAA2B;CA4CjD"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Anthropic = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* TOLVYN Anthropic wrapper — thin drop-in over @anthropic-ai/sdk.
|
|
9
|
+
*/
|
|
10
|
+
const sdk_1 = __importDefault(require("@anthropic-ai/sdk"));
|
|
11
|
+
const failopen_1 = require("./failopen");
|
|
12
|
+
const ANTHROPIC_DEFAULT_PROXY_URL = 'https://proxy.tolvyn.io/v1/proxy/anthropic/';
|
|
13
|
+
const ANTHROPIC_DIRECT_URL = 'https://api.anthropic.com';
|
|
14
|
+
class Anthropic extends sdk_1.default {
|
|
15
|
+
constructor(options = {}) {
|
|
16
|
+
const tolvynApiKey = options.tolvynApiKey ?? process.env['TOLVYN_API_KEY'];
|
|
17
|
+
if (!tolvynApiKey) {
|
|
18
|
+
throw new Error('tolvynApiKey required. Set TOLVYN_API_KEY env var or pass tolvynApiKey.');
|
|
19
|
+
}
|
|
20
|
+
const proxyUrl = options.proxyUrl ??
|
|
21
|
+
process.env['TOLVYN_PROXY_URL'] ??
|
|
22
|
+
ANTHROPIC_DEFAULT_PROXY_URL;
|
|
23
|
+
const defaultHeaders = {};
|
|
24
|
+
if (options.team)
|
|
25
|
+
defaultHeaders['X-Tolvyn-Team'] = options.team;
|
|
26
|
+
if (options.service)
|
|
27
|
+
defaultHeaders['X-Tolvyn-Service'] = options.service;
|
|
28
|
+
if (options.feature)
|
|
29
|
+
defaultHeaders['X-Tolvyn-Feature'] = options.feature;
|
|
30
|
+
if (options.agent)
|
|
31
|
+
defaultHeaders['X-Tolvyn-Agent'] = options.agent;
|
|
32
|
+
const fallbackKey = options.anthropicApiKey ?? process.env['ANTHROPIC_API_KEY'];
|
|
33
|
+
const failOpen = options.failOpen ?? true;
|
|
34
|
+
const { tolvynApiKey: _tk, proxyUrl: _pu, team: _t, service: _sv, feature: _f, agent: _a, failOpen: _fo, anthropicApiKey: _aak, ...rest } = options;
|
|
35
|
+
const superOptions = {
|
|
36
|
+
...rest,
|
|
37
|
+
apiKey: tolvynApiKey,
|
|
38
|
+
baseURL: proxyUrl,
|
|
39
|
+
defaultHeaders,
|
|
40
|
+
};
|
|
41
|
+
if (failOpen && fallbackKey && !superOptions.fetch) {
|
|
42
|
+
superOptions.fetch = (0, failopen_1.makeFailOpenFetch)(fallbackKey, ANTHROPIC_DIRECT_URL, 'Anthropic');
|
|
43
|
+
}
|
|
44
|
+
super(superOptions);
|
|
45
|
+
this._tolvynFailOpen = failOpen;
|
|
46
|
+
this._tolvynFallbackKey = fallbackKey;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.Anthropic = Anthropic;
|
|
50
|
+
//# sourceMappingURL=anthropic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic.js","sourceRoot":"","s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K,EAAE,EAAE,EAAE,QAAQ,EAAE,GAAG,EAAE,eAAe,EAAE,IAAI,EAC5D,GAAG,IAAI,EACR,GAAG,OAAO,CAAC;QAEZ,MAAM,YAAY,GAAkB;YAClC,GAAG,IAAI;YACP,MAAM,EAAE,YAAY;YACpB,OAAO,EAAE,QAAQ;YACjB,cAAc;SACf,CAAC;QAEF,IAAI,QAAQ,IAAI,WAAW,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC;YACnD,YAAY,CAAC,KAAK,GAAG,IAAA,4BAAiB,EAAC,WAAW,EAAE,oBAAoB,EAAE,WAAW,CAAC,CAAC;QACzF,CAAC;QAED,KAAK,CAAC,YAAY,CAAC,CAAC;QAEpB,IAAI,CAAC,eAAe,GAAG,QAAQ,CAAC;QAChC,IAAI,CAAC,kBAAkB,GAAG,WAAW,CAAC;IACxC,CAAC;CACF;AAhDD,8BAgDC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TOLVYN OpenAI wrapper — thin drop-in over the official openai package.
|
|
3
|
+
*/
|
|
4
|
+
import OpenAIBase, { ClientOptions } from 'openai';
|
|
5
|
+
export interface TolvynOpenAIOptions extends Omit<ClientOptions, 'apiKey' | 'baseURL'> {
|
|
6
|
+
tolvynApiKey?: string;
|
|
7
|
+
proxyUrl?: string;
|
|
8
|
+
team?: string;
|
|
9
|
+
service?: string;
|
|
10
|
+
feature?: string;
|
|
11
|
+
agent?: string;
|
|
12
|
+
failOpen?: boolean;
|
|
13
|
+
openAIApiKey?: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class OpenAI extends OpenAIBase {
|
|
16
|
+
readonly _tolvynFailOpen: boolean;
|
|
17
|
+
readonly _tolvynFallbackKey: string | undefined;
|
|
18
|
+
readonly _tolvynProxyUrl: string;
|
|
19
|
+
constructor(options?: TolvynOpenAIOptions);
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,UAAU,EAAE,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAMnD,MAAM,WAAW,mBACf,SAAQ,IAAI,CAAC,aAAa,EAAE,QAAQ,GAAG,SAAS,CAAC;IACjD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AA6CD,qBAAa,MAAO,SAAQ,UAAU;IACpC,SAAgB,eAAe,EAAE,OAAO,CAAC;IACzC,SAAgB,kBAAkB,EAAE,MAAM,GAAG,SAAS,CAAC;IACvD,SAAgB,eAAe,EAAE,MAAM,CAAC;gBAE5B,OAAO,GAAE,mBAAwB;CA6C9C"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.OpenAI = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* TOLVYN OpenAI wrapper — thin drop-in over the official openai package.
|
|
9
|
+
*/
|
|
10
|
+
const openai_1 = __importDefault(require("openai"));
|
|
11
|
+
const failopen_1 = require("./failopen");
|
|
12
|
+
const OPENAI_DEFAULT_PROXY_URL = 'https://proxy.tolvyn.io/v1/proxy/openai/';
|
|
13
|
+
const OPENAI_DIRECT_URL = 'https://api.openai.com/v1';
|
|
14
|
+
function makeFailOpenFetch(fallbackKey, directUrl, provider) {
|
|
15
|
+
return async function failOpenFetch(input, init) {
|
|
16
|
+
try {
|
|
17
|
+
const res = await fetch(input, init);
|
|
18
|
+
if (res.status === 503) {
|
|
19
|
+
throw Object.assign(new Error('503 from proxy'), { status: 503 });
|
|
20
|
+
}
|
|
21
|
+
return res;
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
if ((0, failopen_1.shouldNotFailOpen)(err) || !(0, failopen_1.isProxyError)(err))
|
|
25
|
+
throw err;
|
|
26
|
+
console.error(`TOLVYN proxy unreachable — routing direct to ${provider} (fail-open)`);
|
|
27
|
+
const originalUrl = typeof input === 'string'
|
|
28
|
+
? input
|
|
29
|
+
: input instanceof URL
|
|
30
|
+
? input.href
|
|
31
|
+
: input.url;
|
|
32
|
+
const url = new URL(originalUrl);
|
|
33
|
+
const directBase = new URL(directUrl);
|
|
34
|
+
url.hostname = directBase.hostname;
|
|
35
|
+
url.protocol = directBase.protocol;
|
|
36
|
+
url.port = directBase.port;
|
|
37
|
+
url.pathname = url.pathname; // keep path intact
|
|
38
|
+
const newInit = { ...(init ?? {}) };
|
|
39
|
+
const headers = new Headers(init?.headers ?? {});
|
|
40
|
+
headers.set('Authorization', `Bearer ${fallbackKey}`);
|
|
41
|
+
newInit.headers = headers;
|
|
42
|
+
return fetch(url.toString(), newInit);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
class OpenAI extends openai_1.default {
|
|
47
|
+
constructor(options = {}) {
|
|
48
|
+
const tolvynApiKey = options.tolvynApiKey ?? process.env['TOLVYN_API_KEY'];
|
|
49
|
+
if (!tolvynApiKey) {
|
|
50
|
+
throw new Error('tolvynApiKey required. Set TOLVYN_API_KEY env var or pass tolvynApiKey.');
|
|
51
|
+
}
|
|
52
|
+
const proxyUrl = options.proxyUrl ??
|
|
53
|
+
process.env['TOLVYN_PROXY_URL'] ??
|
|
54
|
+
OPENAI_DEFAULT_PROXY_URL;
|
|
55
|
+
const defaultHeaders = {};
|
|
56
|
+
if (options.team)
|
|
57
|
+
defaultHeaders['X-Tolvyn-Team'] = options.team;
|
|
58
|
+
if (options.service)
|
|
59
|
+
defaultHeaders['X-Tolvyn-Service'] = options.service;
|
|
60
|
+
if (options.feature)
|
|
61
|
+
defaultHeaders['X-Tolvyn-Feature'] = options.feature;
|
|
62
|
+
if (options.agent)
|
|
63
|
+
defaultHeaders['X-Tolvyn-Agent'] = options.agent;
|
|
64
|
+
const fallbackKey = options.openAIApiKey ?? process.env['OPENAI_API_KEY'];
|
|
65
|
+
const failOpen = options.failOpen ?? true;
|
|
66
|
+
const { tolvynApiKey: _tk, proxyUrl: _pu, team: _t, service: _sv, feature: _f, agent: _a, failOpen: _fo, openAIApiKey: _oak, ...rest } = options;
|
|
67
|
+
const superOptions = {
|
|
68
|
+
...rest,
|
|
69
|
+
apiKey: tolvynApiKey,
|
|
70
|
+
baseURL: proxyUrl,
|
|
71
|
+
defaultHeaders,
|
|
72
|
+
};
|
|
73
|
+
if (failOpen && fallbackKey && !superOptions.fetch) {
|
|
74
|
+
superOptions.fetch = makeFailOpenFetch(fallbackKey, OPENAI_DIRECT_URL, 'OpenAI');
|
|
75
|
+
}
|
|
76
|
+
super(superOptions);
|
|
77
|
+
this._tolvynFailOpen = failOpen;
|
|
78
|
+
this._tolvynFallbackKey = fallbackKey;
|
|
79
|
+
this._tolvynProxyUrl = proxyUrl;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
exports.OpenAI = OpenAI;
|
|
83
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":";;;;;;AAAA;;GAEG;AACH,oDAAmD;AACnD,yCAA6D;AAE7D,MAAM,wBAAwB,GAAG,0CAA0C,CAAC;AAC5E,MAAM,iBAAiB,GAAG,2BAA2B,CAAC;AActD,SAAS,iBAAiB,CACxB,WAAmB,EACn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AC;QAE1C,MAAM,EACJ,YAAY,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EACxD,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,QAAQ,EAAE,GAAG,EAAE,YAAY,EAAE,IAAI,EACzD,GAAG,IAAI,EACR,GAAG,OAAO,CAAC;QAEZ,MAAM,YAAY,GAAkB;YAClC,GAAG,IAAI;YACP,MAAM,EAAE,YAAY;YACpB,OAAO,EAAE,QAAQ;YACjB,cAAc;SACf,CAAC;QAEF,IAAI,QAAQ,IAAI,WAAW,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC;YACnD,YAAY,CAAC,KAAK,GAAG,iBAAiB,CAAC,WAAW,EAAE,iBAAiB,EAAE,QAAQ,CAAC,CAAC;QACnF,CAAC;QAED,KAAK,CAAC,YAAY,CAAC,CAAC;QAEpB,IAAI,CAAC,eAAe,GAAG,QAAQ,CAAC;QAChC,IAAI,CAAC,kBAAkB,GAAG,WAAW,CAAC;QACtC,IAAI,CAAC,eAAe,GAAG,QAAQ,CAAC;IAClC,CAAC;CACF;AAlDD,wBAkDC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fail-open helpers: detect proxy unreachability and retry direct.
|
|
3
|
+
*/
|
|
4
|
+
export declare function isProxyError(error: unknown): boolean;
|
|
5
|
+
export declare function shouldNotFailOpen(error: unknown): boolean;
|
|
6
|
+
export declare function makeFailOpenFetch(fallbackKey: string, directUrl: string, provider: string): typeof globalThis.fetch;
|
|
7
|
+
//# sourceMappingURL=failopen.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"failopen.d.ts","sourceRoot":"","sources":["../../src/failopen.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAoCpD;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CASzD;AAED,wBAAgB,iBAAiB,CAC/B,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,GACf,OAAO,UAAU,CAAC,KAAK,CAoCzB"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Fail-open helpers: detect proxy unreachability and retry direct.
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.isProxyError = isProxyError;
|
|
7
|
+
exports.shouldNotFailOpen = shouldNotFailOpen;
|
|
8
|
+
exports.makeFailOpenFetch = makeFailOpenFetch;
|
|
9
|
+
function isProxyError(error) {
|
|
10
|
+
if (!error || typeof error !== 'object')
|
|
11
|
+
return false;
|
|
12
|
+
const err = error;
|
|
13
|
+
// Node.js connection errors (ECONNREFUSED, ETIMEDOUT, etc.)
|
|
14
|
+
const code = err['code'];
|
|
15
|
+
if (typeof code === 'string' && (code === 'ECONNREFUSED' ||
|
|
16
|
+
code === 'ECONNRESET' ||
|
|
17
|
+
code === 'ETIMEDOUT' ||
|
|
18
|
+
code === 'ENOTFOUND' ||
|
|
19
|
+
code.startsWith('ERR_'))) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
// HTTP 503 from proxy
|
|
23
|
+
const status = err['status'] ?? err['statusCode'];
|
|
24
|
+
if (status === 503)
|
|
25
|
+
return true;
|
|
26
|
+
// fetch-level errors
|
|
27
|
+
const message = typeof err['message'] === 'string' ? err['message'] : '';
|
|
28
|
+
if (message.includes('ECONNREFUSED') ||
|
|
29
|
+
message.includes('ETIMEDOUT') ||
|
|
30
|
+
message.includes('fetch failed') ||
|
|
31
|
+
message.includes('connect ECONNREFUSED')) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
// Check cause (Node 18+ wraps errors)
|
|
35
|
+
const cause = err['cause'];
|
|
36
|
+
if (cause && isProxyError(cause))
|
|
37
|
+
return true;
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
function shouldNotFailOpen(error) {
|
|
41
|
+
// Never fail-open on real API errors (401, 429, other 4xx except 503).
|
|
42
|
+
if (!error || typeof error !== 'object')
|
|
43
|
+
return false;
|
|
44
|
+
const err = error;
|
|
45
|
+
const status = err['status'] ?? err['statusCode'];
|
|
46
|
+
if (typeof status === 'number' && status >= 400 && status < 500 && status !== 503) {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
function makeFailOpenFetch(fallbackKey, directUrl, provider) {
|
|
52
|
+
return async function failOpenFetch(input, init) {
|
|
53
|
+
try {
|
|
54
|
+
const res = await fetch(input, init);
|
|
55
|
+
if (res.status === 503) {
|
|
56
|
+
throw Object.assign(new Error('503 from proxy'), { status: 503 });
|
|
57
|
+
}
|
|
58
|
+
return res;
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
if (shouldNotFailOpen(err) || !isProxyError(err))
|
|
62
|
+
throw err;
|
|
63
|
+
console.error(`TOLVYN proxy unreachable — routing direct to ${provider} (fail-open)`);
|
|
64
|
+
const originalUrl = typeof input === 'string'
|
|
65
|
+
? input
|
|
66
|
+
: input instanceof URL
|
|
67
|
+
? input.href
|
|
68
|
+
: input.url;
|
|
69
|
+
const url = new URL(originalUrl);
|
|
70
|
+
const directBase = new URL(directUrl);
|
|
71
|
+
url.hostname = directBase.hostname;
|
|
72
|
+
url.protocol = directBase.protocol;
|
|
73
|
+
url.port = directBase.port;
|
|
74
|
+
const newInit = { ...(init ?? {}) };
|
|
75
|
+
const headers = new Headers(init?.headers ?? {});
|
|
76
|
+
headers.set('Authorization', `Bearer ${fallbackKey}`);
|
|
77
|
+
newInit.headers = headers;
|
|
78
|
+
return fetch(url.toString(), newInit);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+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CAAC,KAAK;gBACP,CAAC,CAAC,KAAK,YAAY,GAAG;oBACtB,CAAC,CAAC,KAAK,CAAC,IAAI;oBACZ,CAAC,CAAE,KAAiB,CAAC,GAAG,CAAC;YAC7B,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,WAAW,CAAC,CAAC;YACjC,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,SAAS,CAAC,CAAC;YACtC,GAAG,CAAC,QAAQ,GAAG,UAAU,CAAC,QAAQ,CAAC;YACnC,GAAG,CAAC,QAAQ,GAAG,UAAU,CAAC,QAAQ,CAAC;YACnC,GAAG,CAAC,IAAI,GAAG,UAAU,CAAC,IAAI,CAAC;YAE3B,MAAM,OAAO,GAAgB,EAAE,GAAG,CAAC,IAAI,IAAI,EAAE,CAAC,EAAE,CAAC;YACjD,MAAM,OAAO,GAAG,IAAI,OAAO,CAAE,IAAI,EAAE,OAAuB,IAAI,EAAE,CAAC,CAAC;YAClE,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,UAAU,WAAW,EAAE,CAAC,CAAC;YACtD,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC;YAE1B,OAAO,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,OAAO,CAAC,CAAC;QACxC,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TOLVYN Google wrapper — thin drop-in over @google/generative-ai.
|
|
3
|
+
*/
|
|
4
|
+
import { GoogleGenerativeAI, ModelParams, RequestOptions } from '@google/generative-ai';
|
|
5
|
+
export interface TolvynGoogleOptions {
|
|
6
|
+
tolvynApiKey?: string;
|
|
7
|
+
proxyUrl?: string;
|
|
8
|
+
team?: string;
|
|
9
|
+
service?: string;
|
|
10
|
+
feature?: string;
|
|
11
|
+
agent?: string;
|
|
12
|
+
failOpen?: boolean;
|
|
13
|
+
googleApiKey?: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class TolvynGoogle extends GoogleGenerativeAI {
|
|
16
|
+
readonly _tolvynFailOpen: boolean;
|
|
17
|
+
readonly _tolvynFallbackKey: string | undefined;
|
|
18
|
+
private readonly _tolvynProxyUrl;
|
|
19
|
+
private readonly _tolvynHeaders;
|
|
20
|
+
constructor(options?: TolvynGoogleOptions);
|
|
21
|
+
getGenerativeModel(modelParams: ModelParams, requestOptions?: RequestOptions): import("@google/generative-ai").GenerativeModel;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=google.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google.d.ts","sourceRoot":"","sources":["../../src/google.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EACL,kBAAkB,EAClB,WAAW,EACX,cAAc,EACf,MAAM,uBAAuB,CAAC;AAM/B,MAAM,WAAW,mBAAmB;IAClC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,qBAAa,YAAa,SAAQ,kBAAkB;IAClD,SAAgB,eAAe,EAAE,OAAO,CAAC;IACzC,SAAgB,kBAAkB,EAAE,MAAM,GAAG,SAAS,CAAC;IACvD,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAS;IACzC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAyB;gBAE5C,OAAO,GAAE,mBAAwB;IA4BpC,kBAAkB,CACzB,WAAW,EAAE,WAAW,EACxB,cAAc,CAAC,EAAE,cAAc;CASlC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TolvynGoogle = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* TOLVYN Google wrapper — thin drop-in over @google/generative-ai.
|
|
6
|
+
*/
|
|
7
|
+
const generative_ai_1 = require("@google/generative-ai");
|
|
8
|
+
// The proxy base URL is prepended to Google API paths (/v1beta/models/...).
|
|
9
|
+
// The TOLVYN proxy strips /v1/proxy/google and forwards the remainder to Google.
|
|
10
|
+
const GOOGLE_DEFAULT_PROXY_URL = 'https://proxy.tolvyn.io/v1/proxy/google';
|
|
11
|
+
class TolvynGoogle extends generative_ai_1.GoogleGenerativeAI {
|
|
12
|
+
constructor(options = {}) {
|
|
13
|
+
const tolvynApiKey = options.tolvynApiKey ?? process.env['TOLVYN_API_KEY'];
|
|
14
|
+
if (!tolvynApiKey) {
|
|
15
|
+
throw new Error('tolvynApiKey required. Set TOLVYN_API_KEY env var or pass tolvynApiKey.');
|
|
16
|
+
}
|
|
17
|
+
// GoogleGenerativeAI sends this value as x-goog-api-key.
|
|
18
|
+
// The TOLVYN proxy's extractBearer reads x-goog-api-key as a fallback.
|
|
19
|
+
super(tolvynApiKey);
|
|
20
|
+
this._tolvynProxyUrl =
|
|
21
|
+
options.proxyUrl ??
|
|
22
|
+
process.env['TOLVYN_PROXY_URL'] ??
|
|
23
|
+
GOOGLE_DEFAULT_PROXY_URL;
|
|
24
|
+
this._tolvynHeaders = {};
|
|
25
|
+
if (options.team)
|
|
26
|
+
this._tolvynHeaders['X-Tolvyn-Team'] = options.team;
|
|
27
|
+
if (options.service)
|
|
28
|
+
this._tolvynHeaders['X-Tolvyn-Service'] = options.service;
|
|
29
|
+
if (options.feature)
|
|
30
|
+
this._tolvynHeaders['X-Tolvyn-Feature'] = options.feature;
|
|
31
|
+
if (options.agent)
|
|
32
|
+
this._tolvynHeaders['X-Tolvyn-Agent'] = options.agent;
|
|
33
|
+
this._tolvynFailOpen = options.failOpen ?? true;
|
|
34
|
+
this._tolvynFallbackKey =
|
|
35
|
+
options.googleApiKey ?? process.env['GOOGLE_API_KEY'];
|
|
36
|
+
}
|
|
37
|
+
getGenerativeModel(modelParams, requestOptions) {
|
|
38
|
+
const mergedOptions = {
|
|
39
|
+
baseUrl: this._tolvynProxyUrl,
|
|
40
|
+
customHeaders: this._tolvynHeaders,
|
|
41
|
+
...requestOptions,
|
|
42
|
+
};
|
|
43
|
+
return super.getGenerativeModel(modelParams, mergedOptions);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.TolvynGoogle = TolvynGoogle;
|
|
47
|
+
//# sourceMappingURL=google.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google.js","sourceRoot":"","sources":["../../src/google.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,yDAI+B;AAE/B,4EAA4E;AAC5E,iFAAiF;AACjF,MAAM,wBAAwB,GAAG,yCAAyC,CAAC;AAa3E,MAAa,YAAa,SAAQ,kCAAkB;IAMlD,YAAY,UAA+B,EAAE;QAC3C,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;QAC3E,IAAI,CAAC,YAAY,EAAE,CAAC;YAClB,MAAM,IAAI,KAAK,CACb,yEAAyE,CAC1E,CAAC;QACJ,CAAC;QAED,yDAAyD;QACzD,uEAAuE;QACvE,KAAK,CAAC,YAAY,CAAC,CAAC;QAEpB,IAAI,CAAC,eAAe;YAClB,OAAO,CAAC,QAAQ;gBAChB,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC;gBAC/B,wBAAwB,CAAC;QAE3B,IAAI,CAAC,cAAc,GAAG,EAAE,CAAC;QACzB,IAAI,OAAO,CAAC,IAAI;YAAK,IAAI,CAAC,cAAc,CAAC,eAAe,CAAC,GAAM,OAAO,CAAC,IAAI,CAAC;QAC5E,IAAI,OAAO,CAAC,OAAO;YAAE,IAAI,CAAC,cAAc,CAAC,kBAAkB,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC;QAC/E,IAAI,OAAO,CAAC,OAAO;YAAE,IAAI,CAAC,cAAc,CAAC,kBAAkB,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC;QAC/E,IAAI,OAAO,CAAC,KAAK;YAAI,IAAI,CAAC,cAAc,CAAC,gBAAgB,CAAC,GAAK,OAAO,CAAC,KAAK,CAAC;QAE7E,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,QAAQ,IAAI,IAAI,CAAC;QAChD,IAAI,CAAC,kBAAkB;YACrB,OAAO,CAAC,YAAY,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;IAC1D,CAAC;IAEQ,kBAAkB,CACzB,WAAwB,EACxB,cAA+B;QAE/B,MAAM,aAAa,GAAmB;YACpC,OAAO,EAAE,IAAI,CAAC,eAAe;YAC7B,aAAa,EAAE,IAAI,CAAC,cAAc;YAClC,GAAG,cAAc;SAClB,CAAC;QACF,OAAO,KAAK,CAAC,kBAAkB,CAAC,WAAW,EAAE,aAAa,CAAC,CAAC;IAC9D,CAAC;CACF;AA7CD,oCA6CC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { OpenAI } from './client';
|
|
2
|
+
export type { TolvynOpenAIOptions } from './client';
|
|
3
|
+
export { Anthropic } from './anthropic';
|
|
4
|
+
export type { TolvynAnthropicOptions } from './anthropic';
|
|
5
|
+
export { TolvynGoogle as Google } from './google';
|
|
6
|
+
export type { TolvynGoogleOptions } from './google';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,YAAY,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,YAAY,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,YAAY,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC;AAClD,YAAY,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Google = exports.Anthropic = exports.OpenAI = void 0;
|
|
4
|
+
var client_1 = require("./client");
|
|
5
|
+
Object.defineProperty(exports, "OpenAI", { enumerable: true, get: function () { return client_1.OpenAI; } });
|
|
6
|
+
var anthropic_1 = require("./anthropic");
|
|
7
|
+
Object.defineProperty(exports, "Anthropic", { enumerable: true, get: function () { return anthropic_1.Anthropic; } });
|
|
8
|
+
var google_1 = require("./google");
|
|
9
|
+
Object.defineProperty(exports, "Google", { enumerable: true, get: function () { return google_1.TolvynGoogle; } });
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAAkC;AAAzB,gGAAA,MAAM,OAAA;AAEf,yCAAwC;AAA/B,sGAAA,SAAS,OAAA;AAElB,mCAAkD;AAAzC,gGAAA,YAAY,OAAU"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TOLVYN Anthropic wrapper — thin drop-in over @anthropic-ai/sdk.
|
|
3
|
+
*/
|
|
4
|
+
import AnthropicBase, { ClientOptions } from '@anthropic-ai/sdk';
|
|
5
|
+
export interface TolvynAnthropicOptions extends Omit<ClientOptions, 'apiKey' | 'baseURL'> {
|
|
6
|
+
tolvynApiKey?: string;
|
|
7
|
+
proxyUrl?: string;
|
|
8
|
+
team?: string;
|
|
9
|
+
service?: string;
|
|
10
|
+
feature?: string;
|
|
11
|
+
agent?: string;
|
|
12
|
+
failOpen?: boolean;
|
|
13
|
+
anthropicApiKey?: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class Anthropic extends AnthropicBase {
|
|
16
|
+
readonly _tolvynFailOpen: boolean;
|
|
17
|
+
readonly _tolvynFallbackKey: string | undefined;
|
|
18
|
+
constructor(options?: TolvynAnthropicOptions);
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=anthropic.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/anthropic.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,aAAa,EAAE,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAMjE,MAAM,WAAW,sBACf,SAAQ,IAAI,CAAC,aAAa,EAAE,QAAQ,GAAG,SAAS,CAAC;IACjD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,qBAAa,SAAU,SAAQ,aAAa;IAC1C,SAAgB,eAAe,EAAE,OAAO,CAAC;IACzC,SAAgB,kBAAkB,EAAE,MAAM,GAAG,SAAS,CAAC;gBAE3C,OAAO,GAAE,sBAA2B;CA4CjD"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TOLVYN Anthropic wrapper — thin drop-in over @anthropic-ai/sdk.
|
|
3
|
+
*/
|
|
4
|
+
import AnthropicBase from '@anthropic-ai/sdk';
|
|
5
|
+
import { makeFailOpenFetch } from './failopen';
|
|
6
|
+
const ANTHROPIC_DEFAULT_PROXY_URL = 'https://proxy.tolvyn.io/v1/proxy/anthropic/';
|
|
7
|
+
const ANTHROPIC_DIRECT_URL = 'https://api.anthropic.com';
|
|
8
|
+
export class Anthropic extends AnthropicBase {
|
|
9
|
+
constructor(options = {}) {
|
|
10
|
+
const tolvynApiKey = options.tolvynApiKey ?? process.env['TOLVYN_API_KEY'];
|
|
11
|
+
if (!tolvynApiKey) {
|
|
12
|
+
throw new Error('tolvynApiKey required. Set TOLVYN_API_KEY env var or pass tolvynApiKey.');
|
|
13
|
+
}
|
|
14
|
+
const proxyUrl = options.proxyUrl ??
|
|
15
|
+
process.env['TOLVYN_PROXY_URL'] ??
|
|
16
|
+
ANTHROPIC_DEFAULT_PROXY_URL;
|
|
17
|
+
const defaultHeaders = {};
|
|
18
|
+
if (options.team)
|
|
19
|
+
defaultHeaders['X-Tolvyn-Team'] = options.team;
|
|
20
|
+
if (options.service)
|
|
21
|
+
defaultHeaders['X-Tolvyn-Service'] = options.service;
|
|
22
|
+
if (options.feature)
|
|
23
|
+
defaultHeaders['X-Tolvyn-Feature'] = options.feature;
|
|
24
|
+
if (options.agent)
|
|
25
|
+
defaultHeaders['X-Tolvyn-Agent'] = options.agent;
|
|
26
|
+
const fallbackKey = options.anthropicApiKey ?? process.env['ANTHROPIC_API_KEY'];
|
|
27
|
+
const failOpen = options.failOpen ?? true;
|
|
28
|
+
const { tolvynApiKey: _tk, proxyUrl: _pu, team: _t, service: _sv, feature: _f, agent: _a, failOpen: _fo, anthropicApiKey: _aak, ...rest } = options;
|
|
29
|
+
const superOptions = {
|
|
30
|
+
...rest,
|
|
31
|
+
apiKey: tolvynApiKey,
|
|
32
|
+
baseURL: proxyUrl,
|
|
33
|
+
defaultHeaders,
|
|
34
|
+
};
|
|
35
|
+
if (failOpen && fallbackKey && !superOptions.fetch) {
|
|
36
|
+
superOptions.fetch = makeFailOpenFetch(fallbackKey, ANTHROPIC_DIRECT_URL, 'Anthropic');
|
|
37
|
+
}
|
|
38
|
+
super(superOptions);
|
|
39
|
+
this._tolvynFailOpen = failOpen;
|
|
40
|
+
this._tolvynFallbackKey = fallbackKey;
|
|
41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,GAAG,OAAO,CAAC,QAAQ,IAAI,IAAI,CAAC;QAE1C,MAAM,EACJ,YAAY,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EACxD,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,QAAQ,EAAE,GAAG,EAAE,eAAe,EAAE,IAAI,EAC5D,GAAG,IAAI,EACR,GAAG,OAAO,CAAC;QAEZ,MAAM,YAAY,GAAkB;YAClC,GAAG,IAAI;YACP,MAAM,EAAE,YAAY;YACpB,OAAO,EAAE,QAAQ;YACjB,cAAc;SACf,CAAC;QAEF,IAAI,QAAQ,IAAI,WAAW,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC;YACnD,YAAY,CAAC,KAAK,GAAG,iBAAiB,CAAC,WAAW,EAAE,oBAAoB,EAAE,WAAW,CAAC,CAAC;QACzF,CAAC;QAED,KAAK,CAAC,YAAY,CAAC,CAAC;QAEpB,IAAI,CAAC,eAAe,GAAG,QAAQ,CAAC;QAChC,IAAI,CAAC,kBAAkB,GAAG,WAAW,CAAC;IACxC,CAAC;CACF"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TOLVYN OpenAI wrapper — thin drop-in over the official openai package.
|
|
3
|
+
*/
|
|
4
|
+
import OpenAIBase, { ClientOptions } from 'openai';
|
|
5
|
+
export interface TolvynOpenAIOptions extends Omit<ClientOptions, 'apiKey' | 'baseURL'> {
|
|
6
|
+
tolvynApiKey?: string;
|
|
7
|
+
proxyUrl?: string;
|
|
8
|
+
team?: string;
|
|
9
|
+
service?: string;
|
|
10
|
+
feature?: string;
|
|
11
|
+
agent?: string;
|
|
12
|
+
failOpen?: boolean;
|
|
13
|
+
openAIApiKey?: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class OpenAI extends OpenAIBase {
|
|
16
|
+
readonly _tolvynFailOpen: boolean;
|
|
17
|
+
readonly _tolvynFallbackKey: string | undefined;
|
|
18
|
+
readonly _tolvynProxyUrl: string;
|
|
19
|
+
constructor(options?: TolvynOpenAIOptions);
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,UAAU,EAAE,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAMnD,MAAM,WAAW,mBACf,SAAQ,IAAI,CAAC,aAAa,EAAE,QAAQ,GAAG,SAAS,CAAC;IACjD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AA6CD,qBAAa,MAAO,SAAQ,UAAU;IACpC,SAAgB,eAAe,EAAE,OAAO,CAAC;IACzC,SAAgB,kBAAkB,EAAE,MAAM,GAAG,SAAS,CAAC;IACvD,SAAgB,eAAe,EAAE,MAAM,CAAC;gBAE5B,OAAO,GAAE,mBAAwB;CA6C9C"}
|