tolvyn 1.0.2 → 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 CHANGED
@@ -1,16 +1,17 @@
1
- # tolvyn (Node.js / TypeScript SDK)
1
+ # tolvyn · npm
2
2
 
3
- Drop-in replacement for the `openai` and `@anthropic-ai/sdk` packages. Add cost metering, team attribution, and budget enforcement to your AI calls in one line.
3
+ [![npm version](https://img.shields.io/npm/v/tolvyn.svg)](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
4
9
 
5
10
  ```bash
6
11
  npm install tolvyn
7
12
  ```
8
13
 
9
- ---
10
-
11
- ## Quick Start
12
-
13
- ### OpenAI (ESM / TypeScript)
14
+ ## Quick start
14
15
 
15
16
  ```typescript
16
17
  // Before
@@ -20,7 +21,7 @@ const client = new OpenAI();
20
21
  // After — one line change
21
22
  import { OpenAI } from "tolvyn";
22
23
  const client = new OpenAI({
23
- tolvynApiKey: "tlv_live_...", // or set TOLVYN_API_KEY env var
24
+ tolvynApiKey: "tlv_live_...",
24
25
  team: "backend",
25
26
  service: "summariser",
26
27
  });
@@ -32,152 +33,31 @@ const response = await client.chat.completions.create({
32
33
  });
33
34
  ```
34
35
 
35
- ### OpenAI (CommonJS)
36
-
37
- ```javascript
38
- const { OpenAI } = require("tolvyn");
39
-
40
- const client = new OpenAI({
41
- tolvynApiKey: process.env.TOLVYN_API_KEY,
42
- team: "backend",
43
- service: "summariser",
44
- });
45
- ```
46
-
47
- ### Anthropic (ESM / TypeScript)
36
+ Works the same way for Anthropic:
48
37
 
49
38
  ```typescript
50
- // Before
51
- import Anthropic from "@anthropic-ai/sdk";
52
- const client = new Anthropic();
53
-
54
- // After
55
39
  import { Anthropic } from "tolvyn";
56
- const client = new Anthropic({
57
- tolvynApiKey: "tlv_live_...",
58
- team: "ml-team",
59
- service: "classifier",
60
- });
61
-
62
- const message = await client.messages.create({
63
- model: "claude-sonnet-4-6",
64
- max_tokens: 1024,
65
- messages: [{ role: "user", content: "Hello" }],
66
- });
40
+ const client = new Anthropic({ tolvynApiKey: "tlv_live_...", team: "ml", service: "classifier" });
67
41
  ```
68
42
 
69
- ---
70
-
71
- ## Classes
72
-
73
- | Class | Extends | Provider |
74
- |--------------------|-------------------------------|-----------|
75
- | `tolvyn.OpenAI` | `openai` OpenAI | OpenAI |
76
- | `tolvyn.Anthropic` | `@anthropic-ai/sdk` Anthropic | Anthropic |
77
-
78
- Both classes are strict drop-ins. Every method, event, and behaviour of the underlying SDK is preserved.
79
-
80
- ---
81
-
82
- ## Parameters
83
-
84
- ### `OpenAI` (`TolvynOpenAIOptions`)
85
-
86
- Extends `openai.ClientOptions` (omitting `apiKey` and `baseURL`).
87
-
88
- | Parameter | Type | Default | Description |
89
- |----------------|-----------------------|-------------|-----------------------------------------------------------------------------------------------------------|
90
- | `tolvynApiKey` | `string or undefined` | `undefined` | Your TOLVYN API key. Falls back to `TOLVYN_API_KEY` env var. Required. |
91
- | `proxyUrl` | `string or undefined` | `undefined` | TOLVYN proxy URL. Falls back to `TOLVYN_PROXY_URL` env var, then `http://localhost:8081/v1/proxy/openai`. |
92
- | `team` | `string or undefined` | `undefined` | Team name for cost attribution. Sent as `X-Tolvyn-Team` header. |
93
- | `service` | `string or undefined` | `undefined` | Service name. Sent as `X-Tolvyn-Service` header. |
94
- | `feature` | `string or undefined` | `undefined` | Feature name. Sent as `X-Tolvyn-Feature` header. |
95
- | `agent` | `string or undefined` | `undefined` | Agent name. Sent as `X-Tolvyn-Agent` header. |
96
- | `failOpen` | `boolean` | `true` | If `true` and the proxy is unreachable, retry directly against OpenAI using `openAIApiKey`. |
97
- | `openAIApiKey` | `string or undefined` | `undefined` | OpenAI key used only for fail-open fallback. Falls back to `OPENAI_API_KEY` env var. |
98
- | `...rest` | any | — | All other `openai.ClientOptions` fields are forwarded to the underlying client. |
99
-
100
- ### `Anthropic` (`TolvynAnthropicOptions`)
101
-
102
- Extends `@anthropic-ai/sdk` ClientOptions (omitting `apiKey` and `baseURL`).
103
-
104
- | Parameter | Type | Default | Description |
105
- |------------------|-----------------------|-------------|--------------------------------------------------------------------------------------------------------------|
106
- | `tolvynApiKey` | `string or undefined` | `undefined` | Your TOLVYN API key. Falls back to `TOLVYN_API_KEY` env var. Required. |
107
- | `proxyUrl` | `string or undefined` | `undefined` | TOLVYN proxy URL. Falls back to `TOLVYN_PROXY_URL` env var, then `http://localhost:8081/v1/proxy/anthropic`. |
108
- | `team` | `string or undefined` | `undefined` | Team name for cost attribution. |
109
- | `service` | `string or undefined` | `undefined` | Service name. |
110
- | `feature` | `string or undefined` | `undefined` | Feature name. |
111
- | `agent` | `string or undefined` | `undefined` | Agent name. |
112
- | `failOpen` | `boolean` | `true` | If `true` and the proxy is unreachable, retry directly against Anthropic. |
113
- | `anthropicApiKey`| `string or undefined` | `undefined` | Anthropic key used only for fail-open fallback. Falls back to `ANTHROPIC_API_KEY` env var. |
114
- | `...rest` | any | — | All other Anthropic ClientOptions fields are forwarded. |
115
-
116
- ---
117
-
118
- ## Tagging
119
-
120
- ```typescript
121
- const client = new OpenAI({
122
- tolvynApiKey: "tlv_live_...",
123
- team: "search-team", // Maps to a team in TOLVYN → budget applies
124
- service: "semantic-search", // Sub-component (e.g. microservice name)
125
- feature: "query-expansion", // Feature within the service
126
- agent: "reranker-v2", // Agent name for multi-agent pipelines
127
- });
128
- ```
129
-
130
- All four tags are optional and independent. They appear in `tolvyn tail` output, the dashboard, and usage breakdown endpoints.
131
-
132
- ---
133
-
134
- ## Fail-open behaviour
135
-
136
- By default, `failOpen: true`. When the TOLVYN proxy is unreachable (connection refused, HTTP 503), the SDK retries the request directly against the AI provider using the fallback API key.
137
-
138
- A proxy outage **never breaks your application**. Requests that bypass the proxy are not metered; they appear in the provider's billing but not in TOLVYN.
139
-
140
- To hard-fail on proxy errors:
141
-
142
- ```typescript
143
- const client = new OpenAI({ tolvynApiKey: "tlv_live_...", failOpen: false });
144
- ```
145
-
146
- ---
147
-
148
- ## Imports
149
-
150
- ### ESM
151
-
152
- ```typescript
153
- import { OpenAI, Anthropic } from "tolvyn";
154
- import type { TolvynOpenAIOptions, TolvynAnthropicOptions } from "tolvyn";
155
- ```
156
-
157
- ### CommonJS
43
+ CommonJS:
158
44
 
159
45
  ```javascript
160
- const { OpenAI, Anthropic } = require("tolvyn");
46
+ const { OpenAI } = require("tolvyn");
47
+ const client = new OpenAI({ tolvynApiKey: process.env.TOLVYN_API_KEY, team: "backend" });
161
48
  ```
162
49
 
163
- Both ESM (`dist/esm/`) and CJS (`dist/cjs/`) builds are included.
164
-
165
- ---
50
+ ## What you get
166
51
 
167
- ## Environment variables
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
168
57
 
169
- | Variable | Used by | Description |
170
- |---------------------|-------------------|--------------------------------------|
171
- | `TOLVYN_API_KEY` | All classes | TOLVYN API key |
172
- | `TOLVYN_PROXY_URL` | All classes | Proxy URL override |
173
- | `OPENAI_API_KEY` | OpenAI classes | OpenAI key for fail-open fallback |
174
- | `ANTHROPIC_API_KEY` | Anthropic classes | Anthropic key for fail-open fallback |
58
+ Full docs: [docs.tolvyn.io/nodejs-sdk](https://docs.tolvyn.io/nodejs-sdk)
59
+ Free trial: [tolvyn.io](https://tolvyn.io)
175
60
 
176
61
  ---
177
62
 
178
- ## Requirements
179
-
180
- - Node.js 18+
181
- - `openai >= 4.0.0` (peer dependency)
182
- - `@anthropic-ai/sdk >= 0.20.0` (peer dependency)
183
- - TypeScript 5.0+ (if using TypeScript)
63
+ © 2026 TOLVYN. All rights reserved.
@@ -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"}
@@ -2,4 +2,6 @@ export { OpenAI } from './client';
2
2
  export type { TolvynOpenAIOptions } from './client';
3
3
  export { Anthropic } from './anthropic';
4
4
  export type { TolvynAnthropicOptions } from './anthropic';
5
+ export { TolvynGoogle as Google } from './google';
6
+ export type { TolvynGoogleOptions } from './google';
5
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +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"}
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"}
package/dist/cjs/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Anthropic = exports.OpenAI = void 0;
3
+ exports.Google = exports.Anthropic = exports.OpenAI = void 0;
4
4
  var client_1 = require("./client");
5
5
  Object.defineProperty(exports, "OpenAI", { enumerable: true, get: function () { return client_1.OpenAI; } });
6
6
  var anthropic_1 = require("./anthropic");
7
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; } });
8
10
  //# sourceMappingURL=index.js.map
@@ -1 +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"}
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,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,43 @@
1
+ /**
2
+ * TOLVYN Google wrapper — thin drop-in over @google/generative-ai.
3
+ */
4
+ import { GoogleGenerativeAI, } from '@google/generative-ai';
5
+ // The proxy base URL is prepended to Google API paths (/v1beta/models/...).
6
+ // The TOLVYN proxy strips /v1/proxy/google and forwards the remainder to Google.
7
+ const GOOGLE_DEFAULT_PROXY_URL = 'https://proxy.tolvyn.io/v1/proxy/google';
8
+ export class TolvynGoogle extends GoogleGenerativeAI {
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
+ // GoogleGenerativeAI sends this value as x-goog-api-key.
15
+ // The TOLVYN proxy's extractBearer reads x-goog-api-key as a fallback.
16
+ super(tolvynApiKey);
17
+ this._tolvynProxyUrl =
18
+ options.proxyUrl ??
19
+ process.env['TOLVYN_PROXY_URL'] ??
20
+ GOOGLE_DEFAULT_PROXY_URL;
21
+ this._tolvynHeaders = {};
22
+ if (options.team)
23
+ this._tolvynHeaders['X-Tolvyn-Team'] = options.team;
24
+ if (options.service)
25
+ this._tolvynHeaders['X-Tolvyn-Service'] = options.service;
26
+ if (options.feature)
27
+ this._tolvynHeaders['X-Tolvyn-Feature'] = options.feature;
28
+ if (options.agent)
29
+ this._tolvynHeaders['X-Tolvyn-Agent'] = options.agent;
30
+ this._tolvynFailOpen = options.failOpen ?? true;
31
+ this._tolvynFallbackKey =
32
+ options.googleApiKey ?? process.env['GOOGLE_API_KEY'];
33
+ }
34
+ getGenerativeModel(modelParams, requestOptions) {
35
+ const mergedOptions = {
36
+ baseUrl: this._tolvynProxyUrl,
37
+ customHeaders: this._tolvynHeaders,
38
+ ...requestOptions,
39
+ };
40
+ return super.getGenerativeModel(modelParams, mergedOptions);
41
+ }
42
+ }
43
+ //# sourceMappingURL=google.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"google.js","sourceRoot":"","sources":["../../src/google.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EACL,kBAAkB,GAGnB,MAAM,uBAAuB,CAAC;AAE/B,4EAA4E;AAC5E,iFAAiF;AACjF,MAAM,wBAAwB,GAAG,yCAAyC,CAAC;AAa3E,MAAM,OAAO,YAAa,SAAQ,kBAAkB;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"}
@@ -2,4 +2,6 @@ export { OpenAI } from './client';
2
2
  export type { TolvynOpenAIOptions } from './client';
3
3
  export { Anthropic } from './anthropic';
4
4
  export type { TolvynAnthropicOptions } from './anthropic';
5
+ export { TolvynGoogle as Google } from './google';
6
+ export type { TolvynGoogleOptions } from './google';
5
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +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"}
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"}
package/dist/esm/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export { OpenAI } from './client';
2
2
  export { Anthropic } from './anthropic';
3
+ export { TolvynGoogle as Google } from './google';
3
4
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,OAAO,EAAE,YAAY,IAAI,MAAM,EAAE,MAAM,UAAU,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tolvyn",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Drop-in replacement for the OpenAI/Anthropic SDK — routes through TOLVYN for cost attribution, budget enforcement, and audit logging.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",
@@ -23,6 +23,7 @@
23
23
  },
24
24
  "dependencies": {
25
25
  "@anthropic-ai/sdk": "^0.20.0",
26
+ "@google/generative-ai": "^0.21.0",
26
27
  "openai": "^4.0.0"
27
28
  },
28
29
  "devDependencies": {
@@ -35,7 +36,9 @@
35
36
  "jest": {
36
37
  "preset": "ts-jest",
37
38
  "testEnvironment": "node",
38
- "testMatch": ["**/tests/**/*.test.ts"],
39
+ "testMatch": [
40
+ "**/tests/**/*.test.ts"
41
+ ],
39
42
  "globals": {
40
43
  "ts-jest": {
41
44
  "tsconfig": "tsconfig.cjs.json"
package/src/google.ts ADDED
@@ -0,0 +1,70 @@
1
+ /**
2
+ * TOLVYN Google wrapper — thin drop-in over @google/generative-ai.
3
+ */
4
+ import {
5
+ GoogleGenerativeAI,
6
+ ModelParams,
7
+ RequestOptions,
8
+ } from '@google/generative-ai';
9
+
10
+ // The proxy base URL is prepended to Google API paths (/v1beta/models/...).
11
+ // The TOLVYN proxy strips /v1/proxy/google and forwards the remainder to Google.
12
+ const GOOGLE_DEFAULT_PROXY_URL = 'https://proxy.tolvyn.io/v1/proxy/google';
13
+
14
+ export interface TolvynGoogleOptions {
15
+ tolvynApiKey?: string;
16
+ proxyUrl?: string;
17
+ team?: string;
18
+ service?: string;
19
+ feature?: string;
20
+ agent?: string;
21
+ failOpen?: boolean;
22
+ googleApiKey?: string;
23
+ }
24
+
25
+ export class TolvynGoogle extends GoogleGenerativeAI {
26
+ public readonly _tolvynFailOpen: boolean;
27
+ public readonly _tolvynFallbackKey: string | undefined;
28
+ private readonly _tolvynProxyUrl: string;
29
+ private readonly _tolvynHeaders: Record<string, string>;
30
+
31
+ constructor(options: TolvynGoogleOptions = {}) {
32
+ const tolvynApiKey = options.tolvynApiKey ?? process.env['TOLVYN_API_KEY'];
33
+ if (!tolvynApiKey) {
34
+ throw new Error(
35
+ 'tolvynApiKey required. Set TOLVYN_API_KEY env var or pass tolvynApiKey.'
36
+ );
37
+ }
38
+
39
+ // GoogleGenerativeAI sends this value as x-goog-api-key.
40
+ // The TOLVYN proxy's extractBearer reads x-goog-api-key as a fallback.
41
+ super(tolvynApiKey);
42
+
43
+ this._tolvynProxyUrl =
44
+ options.proxyUrl ??
45
+ process.env['TOLVYN_PROXY_URL'] ??
46
+ GOOGLE_DEFAULT_PROXY_URL;
47
+
48
+ this._tolvynHeaders = {};
49
+ if (options.team) this._tolvynHeaders['X-Tolvyn-Team'] = options.team;
50
+ if (options.service) this._tolvynHeaders['X-Tolvyn-Service'] = options.service;
51
+ if (options.feature) this._tolvynHeaders['X-Tolvyn-Feature'] = options.feature;
52
+ if (options.agent) this._tolvynHeaders['X-Tolvyn-Agent'] = options.agent;
53
+
54
+ this._tolvynFailOpen = options.failOpen ?? true;
55
+ this._tolvynFallbackKey =
56
+ options.googleApiKey ?? process.env['GOOGLE_API_KEY'];
57
+ }
58
+
59
+ override getGenerativeModel(
60
+ modelParams: ModelParams,
61
+ requestOptions?: RequestOptions
62
+ ) {
63
+ const mergedOptions: RequestOptions = {
64
+ baseUrl: this._tolvynProxyUrl,
65
+ customHeaders: this._tolvynHeaders,
66
+ ...requestOptions,
67
+ };
68
+ return super.getGenerativeModel(modelParams, mergedOptions);
69
+ }
70
+ }
package/src/index.ts CHANGED
@@ -2,3 +2,5 @@ export { OpenAI } from './client';
2
2
  export type { TolvynOpenAIOptions } from './client';
3
3
  export { Anthropic } from './anthropic';
4
4
  export type { TolvynAnthropicOptions } from './anthropic';
5
+ export { TolvynGoogle as Google } from './google';
6
+ export type { TolvynGoogleOptions } from './google';