@tensorcost/sdk 0.6.0 → 1.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.
Files changed (105) hide show
  1. package/CHANGELOG.md +79 -8
  2. package/README.md +245 -129
  3. package/dist/adapters/crewai.d.ts +4 -0
  4. package/dist/adapters/crewai.d.ts.map +1 -0
  5. package/dist/adapters/crewai.js +6 -0
  6. package/dist/adapters/crewai.js.map +1 -0
  7. package/dist/adapters/langchain.d.ts +4 -0
  8. package/dist/adapters/langchain.d.ts.map +1 -0
  9. package/dist/adapters/langchain.js +6 -0
  10. package/dist/adapters/langchain.js.map +1 -0
  11. package/dist/adapters/llamaindex.d.ts +4 -0
  12. package/dist/adapters/llamaindex.d.ts.map +1 -0
  13. package/dist/adapters/llamaindex.js +6 -0
  14. package/dist/adapters/llamaindex.js.map +1 -0
  15. package/dist/adapters/vercel-ai.d.ts +4 -0
  16. package/dist/adapters/vercel-ai.d.ts.map +1 -0
  17. package/dist/adapters/vercel-ai.js +6 -0
  18. package/dist/adapters/vercel-ai.js.map +1 -0
  19. package/dist/admit-client.d.ts +39 -0
  20. package/dist/admit-client.d.ts.map +1 -0
  21. package/dist/admit-client.js +93 -0
  22. package/dist/admit-client.js.map +1 -0
  23. package/dist/applied.d.ts +6 -1
  24. package/dist/applied.d.ts.map +1 -1
  25. package/dist/applied.js +13 -1
  26. package/dist/applied.js.map +1 -1
  27. package/dist/circuit-observations.d.ts +12 -0
  28. package/dist/circuit-observations.d.ts.map +1 -0
  29. package/dist/circuit-observations.js +37 -0
  30. package/dist/circuit-observations.js.map +1 -0
  31. package/dist/circuit.d.ts +4 -1
  32. package/dist/circuit.d.ts.map +1 -1
  33. package/dist/circuit.js +13 -7
  34. package/dist/circuit.js.map +1 -1
  35. package/dist/config.d.ts +2 -3
  36. package/dist/config.d.ts.map +1 -1
  37. package/dist/config.js +54 -6
  38. package/dist/config.js.map +1 -1
  39. package/dist/decision.d.ts +11 -0
  40. package/dist/decision.d.ts.map +1 -0
  41. package/dist/decision.js +40 -0
  42. package/dist/decision.js.map +1 -0
  43. package/dist/errors.d.ts +23 -3
  44. package/dist/errors.d.ts.map +1 -1
  45. package/dist/errors.js +24 -0
  46. package/dist/errors.js.map +1 -1
  47. package/dist/index.d.ts +9 -3
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +8 -2
  50. package/dist/index.js.map +1 -1
  51. package/dist/layer.d.ts +33 -0
  52. package/dist/layer.d.ts.map +1 -0
  53. package/dist/layer.js +83 -0
  54. package/dist/layer.js.map +1 -0
  55. package/dist/prewarm.d.ts +12 -0
  56. package/dist/prewarm.d.ts.map +1 -0
  57. package/dist/prewarm.js +22 -0
  58. package/dist/prewarm.js.map +1 -0
  59. package/dist/providers/anthropic.d.ts +10 -0
  60. package/dist/providers/anthropic.d.ts.map +1 -1
  61. package/dist/providers/anthropic.js +134 -57
  62. package/dist/providers/anthropic.js.map +1 -1
  63. package/dist/providers/bedrock.d.ts +1 -1
  64. package/dist/providers/bedrock.d.ts.map +1 -1
  65. package/dist/providers/bedrock.js +16 -34
  66. package/dist/providers/bedrock.js.map +1 -1
  67. package/dist/providers/openai.d.ts +10 -0
  68. package/dist/providers/openai.d.ts.map +1 -1
  69. package/dist/providers/openai.js +143 -61
  70. package/dist/providers/openai.js.map +1 -1
  71. package/dist/providers/vertex.d.ts +1 -1
  72. package/dist/providers/vertex.d.ts.map +1 -1
  73. package/dist/providers/vertex.js +18 -27
  74. package/dist/providers/vertex.js.map +1 -1
  75. package/dist/proxy-client.d.ts +30 -7
  76. package/dist/proxy-client.d.ts.map +1 -1
  77. package/dist/proxy-client.js +123 -19
  78. package/dist/proxy-client.js.map +1 -1
  79. package/dist/runtime.d.ts +23 -0
  80. package/dist/runtime.d.ts.map +1 -0
  81. package/dist/runtime.js +60 -0
  82. package/dist/runtime.js.map +1 -0
  83. package/dist/sdk-layer.d.ts +33 -0
  84. package/dist/sdk-layer.d.ts.map +1 -0
  85. package/dist/sdk-layer.js +107 -0
  86. package/dist/sdk-layer.js.map +1 -0
  87. package/dist/transport.d.ts +3 -1
  88. package/dist/transport.d.ts.map +1 -1
  89. package/dist/transport.js +6 -2
  90. package/dist/transport.js.map +1 -1
  91. package/dist/types.d.ts +82 -15
  92. package/dist/types.d.ts.map +1 -1
  93. package/dist/version.d.ts +9 -0
  94. package/dist/version.d.ts.map +1 -0
  95. package/dist/version.js +10 -0
  96. package/dist/version.js.map +1 -0
  97. package/dist/wrap-overhead.d.ts +32 -0
  98. package/dist/wrap-overhead.d.ts.map +1 -0
  99. package/dist/wrap-overhead.js +60 -0
  100. package/dist/wrap-overhead.js.map +1 -0
  101. package/dist/wrap.d.ts +13 -17
  102. package/dist/wrap.d.ts.map +1 -1
  103. package/dist/wrap.js +59 -74
  104. package/dist/wrap.js.map +1 -1
  105. package/package.json +1 -1
package/dist/types.d.ts CHANGED
@@ -7,9 +7,12 @@
7
7
  */
8
8
  import type { LifecycleEventCallback } from "./telemetry.js";
9
9
  import type { RetryConfig } from "./retry.js";
10
+ import type { ControlLayer, DataGrant } from "./layer.js";
11
+ export type { ControlLayer, DataGrant } from "./layer.js";
10
12
  export type Provider = "openai" | "anthropic" | "bedrock" | "vertex";
11
13
  export type Operation = "chat.completions" | "completions" | "messages" | "bedrock.invoke_model" | "bedrock.converse" | "bedrock.invoke_model_stream" | "bedrock.converse_stream" | "vertex.generate_content" | "vertex.generate_content_stream";
12
14
  export type ObservationStatus = "success" | "error";
15
+ export type ObservationModality = "text" | "image" | "audio" | "video" | "mixed";
13
16
  /**
14
17
  * Wire shape posted to `POST /api/proxy/observation`. Mirrors the
15
18
  * Python SDK's `Observation` model byte-for-byte so the two SDKs
@@ -20,6 +23,8 @@ export interface Observation {
20
23
  provider: Provider;
21
24
  model: string;
22
25
  operation: Operation;
26
+ /** Defaults to "text" when omitted on the wire. */
27
+ modality?: ObservationModality;
23
28
  request_at: string;
24
29
  response_at: string;
25
30
  input_tokens: number | null;
@@ -95,6 +100,23 @@ export interface Observation {
95
100
  * superseded_at IS NULL` — see ai-service's `agent-runs` module.
96
101
  */
97
102
  workflow_id?: string;
103
+ /**
104
+ * A-04 chargeback — end-customer cohort. Optional. Stamped on every
105
+ * observation when configured via `wrap({ customer })` or per-call
106
+ * `.withMeta({ customer })`. Lands in `ai.ai_spend_events.customer`.
107
+ */
108
+ customer?: string;
109
+ /**
110
+ * A-04 chargeback — product feature. Same plumbing as `customer`,
111
+ * landing in `ai.ai_spend_events.feature`.
112
+ */
113
+ feature?: string;
114
+ /**
115
+ * Synchronous SDK wrap overhead in milliseconds — customer-process work
116
+ * only (middleware, token extraction, observation enqueue). Excludes
117
+ * provider RTT and the async HTTP delivery of the observation POST.
118
+ */
119
+ sdk_added_ms?: number;
98
120
  }
99
121
  export interface WrapOptions {
100
122
  /** Long-lived TensorCost API key. Falls back to `TENSORCOST_API_KEY`. */
@@ -139,19 +161,26 @@ export interface WrapOptions {
139
161
  */
140
162
  failOpen?: boolean;
141
163
  /**
142
- * Layer 2 applied mode. When true, the SDK routes every inference
143
- * request through the TensorCost inference-proxy rather than sending
144
- * it directly to the upstream AI provider. The proxy decides
145
- * per-request whether to actually re-route or pass through unchanged.
164
+ * Control-layer ceiling for this wrapped client. A layer is a ceiling,
165
+ * not a setting — effective layer is min(code.maxLayer, console published).
146
166
  *
147
- * Defaults to false. Observe-only telemetry continues to fire
148
- * regardless of this setting.
167
+ * off — no SDK wiring; client returned unchanged
168
+ * observe — telemetry only (default when maxLayer unset)
169
+ * govern — metadata-only admit before direct provider call
170
+ * steer — proxy in path; proxy may steer without full route
171
+ * route — full applied-mode routing through inference-proxy
149
172
  *
150
- * Requires `proxyUrl` (or `TENSORCOST_PROXY_URL` env var) to be set;
151
- * throws `TensorCostConfigError` at `wrap()` time if neither is present.
152
- *
153
- * Not yet supported for AWS Bedrock clients — `wrap()` will throw
154
- * immediately if you enable applied mode with a Bedrock client.
173
+ * Requires `proxyUrl` when maxLayer is steer or route.
174
+ */
175
+ maxLayer?: ControlLayer;
176
+ /**
177
+ * Code-side data-grant ceiling. Effective grants =
178
+ * intersect(maxGrants, console.data_grants). Omit to allow all console grants.
179
+ */
180
+ maxGrants?: readonly DataGrant[];
181
+ /**
182
+ * @deprecated Use `maxLayer: 'route'` instead. When true and maxLayer is
183
+ * unset, resolves to route and emits a console warning.
155
184
  */
156
185
  appliedMode?: boolean;
157
186
  /**
@@ -175,6 +204,11 @@ export interface WrapOptions {
175
204
  * Default: 60 000 (60s). Surfaces as `TensorCostTimeoutError` (kind: "total").
176
205
  */
177
206
  timeoutMs?: number;
207
+ /**
208
+ * Max wait for proxy response headers before fail-open to direct provider.
209
+ * Default: 2 000 (2s). Surfaces as `TensorCostTimeoutError` (kind: "headers").
210
+ */
211
+ headersTimeoutMs?: number;
178
212
  /**
179
213
  * Idle timeout for streaming proxy requests (no bytes received for N ms).
180
214
  * Default: 30 000 (30s). Surfaces as `TensorCostTimeoutError` (kind: "idle").
@@ -265,6 +299,28 @@ export interface WrapOptions {
265
299
  * Falls back to `TENSORCOST_WORKFLOW_ID` env var.
266
300
  */
267
301
  workflowId?: string;
302
+ /**
303
+ * Routing-policy application scope. Sent as `x-tc-application` on proxy
304
+ * requests. Falls back to `TC_APPLICATION` / `TENSORCOST_APPLICATION`.
305
+ */
306
+ application?: string;
307
+ /**
308
+ * Routing-policy tags. Sent as comma-separated `x-tc-tags` on proxy
309
+ * requests. Falls back to `TC_TAGS` / `TENSORCOST_TAGS` (comma-separated).
310
+ */
311
+ tags?: string | string[];
312
+ /**
313
+ * A-04 chargeback — end-customer cohort (max 128 chars). Sent on
314
+ * observations and as `x-tc-customer` in applied mode.
315
+ * Falls back to `TENSORCOST_CUSTOMER`.
316
+ */
317
+ customer?: string;
318
+ /**
319
+ * A-04 chargeback — product feature (max 128 chars). Sent on
320
+ * observations and as `x-tc-feature` in applied mode.
321
+ * Falls back to `TENSORCOST_FEATURE`.
322
+ */
323
+ feature?: string;
268
324
  }
269
325
  export interface ResolvedConfig {
270
326
  apiKey: string;
@@ -284,16 +340,17 @@ export interface ResolvedConfig {
284
340
  */
285
341
  connectionId: string | null;
286
342
  failOpen: boolean;
287
- /** Resolved applied-mode flag. */
343
+ /** Resolved control-layer ceiling. */
344
+ maxLayer: ControlLayer;
345
+ /** @deprecated Derived: maxLayer is steer or route. */
288
346
  appliedMode: boolean;
289
347
  /**
290
- * Resolved proxy base URL. Non-null only when `appliedMode=true`.
291
- * Guaranteed by config resolution — `wrap()` throws before returning
292
- * a config with `appliedMode=true` and `proxyUrl=null`.
348
+ * Resolved proxy base URL. Required when maxLayer is steer or route.
293
349
  */
294
350
  proxyUrl: string | null;
295
351
  retry: RetryConfig;
296
352
  timeoutMs: number;
353
+ headersTimeoutMs: number;
297
354
  idleTimeoutMs: number;
298
355
  onLifecycleEvent: LifecycleEventCallback | undefined;
299
356
  failOpenEnabled: boolean;
@@ -306,5 +363,15 @@ export interface ResolvedConfig {
306
363
  agentId: string | null;
307
364
  /** Resolved wrap-time workflow (run) id default. `null` when not configured. */
308
365
  workflowId: string | null;
366
+ /** Resolved application scope for routing policies. `null` when unset. */
367
+ application: string | null;
368
+ /** Resolved routing tags (normalized list). Empty when unset. */
369
+ tags: string[];
370
+ /** A-04 — resolved customer cohort. `null` when unset. */
371
+ customer: string | null;
372
+ /** A-04 — resolved product feature. `null` when unset. */
373
+ feature: string | null;
374
+ /** Resolved code-side data grant ceiling. Empty = no ceiling. */
375
+ maxGrants: DataGrant[];
309
376
  }
310
377
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AAC7D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,WAAW,GAAG,SAAS,GAAG,QAAQ,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SDK identity — wire headers and observation sdk_version.
3
+ */
4
+ export declare const SDK_VERSION = "tensorcost-node/1.1.0";
5
+ /** Advertise only capabilities this build implements. */
6
+ export declare const SDK_CAPABILITIES: readonly ["streaming", "refusal-v1"];
7
+ export type SdkCapability = (typeof SDK_CAPABILITIES)[number];
8
+ export declare function sdkCapabilityHeader(): string;
9
+ //# sourceMappingURL=version.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,eAAO,MAAM,WAAW,0BAA0B,CAAC;AAEnD,yDAAyD;AACzD,eAAO,MAAM,gBAAgB,sCAAuC,CAAC;AACrE,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,CAAC,CAAC;AAE9D,wBAAgB,mBAAmB,IAAI,MAAM,CAE5C"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Central SDK identity — wire headers and observation sdk_version.
3
+ */
4
+ export const SDK_VERSION = "tensorcost-node/1.1.0";
5
+ /** Advertise only capabilities this build implements. */
6
+ export const SDK_CAPABILITIES = ["streaming", "refusal-v1"];
7
+ export function sdkCapabilityHeader() {
8
+ return SDK_CAPABILITIES.join(",");
9
+ }
10
+ //# sourceMappingURL=version.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,CAAC,MAAM,WAAW,GAAG,uBAAuB,CAAC;AAEnD,yDAAyD;AACzD,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,WAAW,EAAE,YAAY,CAAU,CAAC;AAGrE,MAAM,UAAU,mBAAmB;IACjC,OAAO,gBAAgB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACpC,CAAC"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Measures synchronous SDK wrap overhead in the customer process.
3
+ *
4
+ * All awaited I/O (layer fetch, admit, proxy, provider) must run inside
5
+ * `withAwaitExcluded` so RTT is not counted. Sync work before/after those
6
+ * hops — token extraction, observation enqueue — is included. The async
7
+ * HTTP delivery inside `transport.post`'s microtask is excluded.
8
+ */
9
+ import type { Observation } from "./types.js";
10
+ import type { ObservationTransport } from "./transport.js";
11
+ export declare class WrapOverhead {
12
+ private readonly hookStart;
13
+ private accumulatedMs;
14
+ private activeStart;
15
+ /** Pause the sync clock before an awaited hop. */
16
+ pauseForAwait(): void;
17
+ /** Resume the sync clock after an awaited hop returns. */
18
+ resumeAfterAwait(): void;
19
+ /** Milliseconds of synchronous wrap work (rounded, non-negative). */
20
+ sdkAddedMs(): number;
21
+ }
22
+ /** Run awaited work with wrap clock paused — use for every external I/O hop. */
23
+ export declare function withAwaitExcluded<T>(overhead: WrapOverhead, fn: () => Promise<T>): Promise<T>;
24
+ /**
25
+ * Enqueue an observation with `sdk_added_ms` stamped. Fail-open — never throws.
26
+ * Call after building the observation (token extraction included in overhead).
27
+ */
28
+ export declare function postObservation(transport: ObservationTransport, overhead: WrapOverhead, obs: Observation): void;
29
+ export declare const __test__: {
30
+ WrapOverhead: typeof WrapOverhead;
31
+ };
32
+ //# sourceMappingURL=wrap-overhead.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wrap-overhead.d.ts","sourceRoot":"","sources":["../src/wrap-overhead.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAC9C,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAE3D,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAqB;IAC/C,OAAO,CAAC,aAAa,CAAK;IAC1B,OAAO,CAAC,WAAW,CAAiC;IAEpD,kDAAkD;IAClD,aAAa,IAAI,IAAI;IAMrB,0DAA0D;IAC1D,gBAAgB,IAAI,IAAI;IAKxB,qEAAqE;IACrE,UAAU,IAAI,MAAM;CAOrB;AAED,gFAAgF;AAChF,wBAAsB,iBAAiB,CAAC,CAAC,EACvC,QAAQ,EAAE,YAAY,EACtB,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GACnB,OAAO,CAAC,CAAC,CAAC,CAOZ;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAC7B,SAAS,EAAE,oBAAoB,EAC/B,QAAQ,EAAE,YAAY,EACtB,GAAG,EAAE,WAAW,GACf,IAAI,CAMN;AAED,eAAO,MAAM,QAAQ;;CAAmB,CAAC"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Measures synchronous SDK wrap overhead in the customer process.
3
+ *
4
+ * All awaited I/O (layer fetch, admit, proxy, provider) must run inside
5
+ * `withAwaitExcluded` so RTT is not counted. Sync work before/after those
6
+ * hops — token extraction, observation enqueue — is included. The async
7
+ * HTTP delivery inside `transport.post`'s microtask is excluded.
8
+ */
9
+ export class WrapOverhead {
10
+ constructor() {
11
+ this.hookStart = performance.now();
12
+ this.accumulatedMs = 0;
13
+ this.activeStart = this.hookStart;
14
+ }
15
+ /** Pause the sync clock before an awaited hop. */
16
+ pauseForAwait() {
17
+ if (this.activeStart === null)
18
+ return;
19
+ this.accumulatedMs += performance.now() - this.activeStart;
20
+ this.activeStart = null;
21
+ }
22
+ /** Resume the sync clock after an awaited hop returns. */
23
+ resumeAfterAwait() {
24
+ if (this.activeStart !== null)
25
+ return;
26
+ this.activeStart = performance.now();
27
+ }
28
+ /** Milliseconds of synchronous wrap work (rounded, non-negative). */
29
+ sdkAddedMs() {
30
+ let total = this.accumulatedMs;
31
+ if (this.activeStart !== null) {
32
+ total += performance.now() - this.activeStart;
33
+ }
34
+ return Math.max(0, Math.round(total));
35
+ }
36
+ }
37
+ /** Run awaited work with wrap clock paused — use for every external I/O hop. */
38
+ export async function withAwaitExcluded(overhead, fn) {
39
+ overhead.pauseForAwait();
40
+ try {
41
+ return await fn();
42
+ }
43
+ finally {
44
+ overhead.resumeAfterAwait();
45
+ }
46
+ }
47
+ /**
48
+ * Enqueue an observation with `sdk_added_ms` stamped. Fail-open — never throws.
49
+ * Call after building the observation (token extraction included in overhead).
50
+ */
51
+ export function postObservation(transport, overhead, obs) {
52
+ try {
53
+ transport.post({ ...obs, sdk_added_ms: overhead.sdkAddedMs() });
54
+ }
55
+ catch {
56
+ /* fail-open */
57
+ }
58
+ }
59
+ export const __test__ = { WrapOverhead };
60
+ //# sourceMappingURL=wrap-overhead.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wrap-overhead.js","sourceRoot":"","sources":["../src/wrap-overhead.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,MAAM,OAAO,YAAY;IAAzB;QACmB,cAAS,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;QACvC,kBAAa,GAAG,CAAC,CAAC;QAClB,gBAAW,GAAkB,IAAI,CAAC,SAAS,CAAC;IAuBtD,CAAC;IArBC,kDAAkD;IAClD,aAAa;QACX,IAAI,IAAI,CAAC,WAAW,KAAK,IAAI;YAAE,OAAO;QACtC,IAAI,CAAC,aAAa,IAAI,WAAW,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,WAAW,CAAC;QAC3D,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC;IAC1B,CAAC;IAED,0DAA0D;IAC1D,gBAAgB;QACd,IAAI,IAAI,CAAC,WAAW,KAAK,IAAI;YAAE,OAAO;QACtC,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;IACvC,CAAC;IAED,qEAAqE;IACrE,UAAU;QACR,IAAI,KAAK,GAAG,IAAI,CAAC,aAAa,CAAC;QAC/B,IAAI,IAAI,CAAC,WAAW,KAAK,IAAI,EAAE,CAAC;YAC9B,KAAK,IAAI,WAAW,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,WAAW,CAAC;QAChD,CAAC;QACD,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;IACxC,CAAC;CACF;AAED,gFAAgF;AAChF,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,QAAsB,EACtB,EAAoB;IAEpB,QAAQ,CAAC,aAAa,EAAE,CAAC;IACzB,IAAI,CAAC;QACH,OAAO,MAAM,EAAE,EAAE,CAAC;IACpB,CAAC;YAAS,CAAC;QACT,QAAQ,CAAC,gBAAgB,EAAE,CAAC;IAC9B,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,eAAe,CAC7B,SAA+B,EAC/B,QAAsB,EACtB,GAAgB;IAEhB,IAAI,CAAC;QACH,SAAS,CAAC,IAAI,CAAC,EAAE,GAAG,GAAG,EAAE,YAAY,EAAE,QAAQ,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;IAClE,CAAC;IAAC,MAAM,CAAC;QACP,eAAe;IACjB,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,QAAQ,GAAG,EAAE,YAAY,EAAE,CAAC"}
package/dist/wrap.d.ts CHANGED
@@ -5,24 +5,20 @@
5
5
  * ergonomics non-negotiable: customer changes one line, no other code
6
6
  * paths shift.
7
7
  *
8
- * Applied mode (Layer 2): when `options.appliedMode=true`, the client's
9
- * baseURL is permanently redirected to the TensorCost inference-proxy and
10
- * two custom headers (`x-tc-provider-url`, `x-tc-provider-auth`) are
11
- * injected into `client.defaultHeaders`. The proxy handles per-request
12
- * routing decisions. Observe-only telemetry still fires post-call.
13
- *
14
- * Hardening (v0.4.0): applied-mode requests now go through the proxy-client
15
- * module which adds retries, timeouts, typed errors, telemetry hooks, and
16
- * the fail-open circuit breaker.
17
- *
18
- * Bedrock (v0.4.0): observe-only. Applied mode is not supported for Bedrock —
19
- * wrap(bedrockClient, { appliedMode: true }) throws TensorCostConfigError.
20
- *
21
- * Vertex (v0.6.0): observe-only. Applied mode is not supported for Vertex —
22
- * wrap(vertexClient, { appliedMode: true }) throws TensorCostConfigError.
23
- * The method wrappers on client.models intercept generate_content calls
24
- * transparently.
8
+ * Control layers:
9
+ * off — client returned unchanged (no transport, no hooks)
10
+ * observe — telemetry only
11
+ * govern — admit (metadata) then direct provider call
12
+ * steer/route — proxy in request path (OpenAI / Anthropic only)
25
13
  */
14
+ import { TensorCostConfigError } from "./config.js";
15
+ import { layerRank } from "./layer.js";
26
16
  import type { WrapOptions } from "./types.js";
17
+ declare function steerRouteUnsupportedError(provider: string, maxLayer: string): TensorCostConfigError;
27
18
  export declare function wrap<T>(client: T, options?: WrapOptions): T;
19
+ export declare const __test__: {
20
+ layerRank: typeof layerRank;
21
+ steerRouteUnsupportedError: typeof steerRouteUnsupportedError;
22
+ };
23
+ export {};
28
24
  //# sourceMappingURL=wrap.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"wrap.d.ts","sourceRoot":"","sources":["../src/wrap.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAYH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,wBAAgB,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,OAAO,GAAE,WAAgB,GAAG,CAAC,CAsH/D"}
1
+ {"version":3,"file":"wrap.d.ts","sourceRoot":"","sources":["../src/wrap.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AASpD,OAAO,EAAE,SAAS,EAAiB,MAAM,YAAY,CAAC;AAEtD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,iBAAS,0BAA0B,CACjC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GACf,qBAAqB,CAOvB;AAED,wBAAgB,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,OAAO,GAAE,WAAgB,GAAG,CAAC,CAoH/D;AAED,eAAO,MAAM,QAAQ;;;CAA4C,CAAC"}
package/dist/wrap.js CHANGED
@@ -5,27 +5,14 @@
5
5
  * ergonomics non-negotiable: customer changes one line, no other code
6
6
  * paths shift.
7
7
  *
8
- * Applied mode (Layer 2): when `options.appliedMode=true`, the client's
9
- * baseURL is permanently redirected to the TensorCost inference-proxy and
10
- * two custom headers (`x-tc-provider-url`, `x-tc-provider-auth`) are
11
- * injected into `client.defaultHeaders`. The proxy handles per-request
12
- * routing decisions. Observe-only telemetry still fires post-call.
13
- *
14
- * Hardening (v0.4.0): applied-mode requests now go through the proxy-client
15
- * module which adds retries, timeouts, typed errors, telemetry hooks, and
16
- * the fail-open circuit breaker.
17
- *
18
- * Bedrock (v0.4.0): observe-only. Applied mode is not supported for Bedrock —
19
- * wrap(bedrockClient, { appliedMode: true }) throws TensorCostConfigError.
20
- *
21
- * Vertex (v0.6.0): observe-only. Applied mode is not supported for Vertex —
22
- * wrap(vertexClient, { appliedMode: true }) throws TensorCostConfigError.
23
- * The method wrappers on client.models intercept generate_content calls
24
- * transparently.
8
+ * Control layers:
9
+ * off — client returned unchanged (no transport, no hooks)
10
+ * observe — telemetry only
11
+ * govern — admit (metadata) then direct provider call
12
+ * steer/route — proxy in request path (OpenAI / Anthropic only)
25
13
  */
26
14
  import { resolveConfig } from "./config.js";
27
15
  import { TensorCostConfigError } from "./config.js";
28
- import { ObservationTransport } from "./transport.js";
29
16
  import { detectProvider } from "./providers/detect.js";
30
17
  import { installAppliedMode } from "./applied.js";
31
18
  import * as openaiProvider from "./providers/openai.js";
@@ -33,75 +20,73 @@ import * as anthropicProvider from "./providers/anthropic.js";
33
20
  import * as bedrockProvider from "./providers/bedrock.js";
34
21
  import * as vertexProvider from "./providers/vertex.js";
35
22
  import { CircuitBreaker } from "./circuit.js";
23
+ import { bindCircuitObservations } from "./circuit-observations.js";
24
+ import { layerRank, needsProxyUrl } from "./layer.js";
25
+ import { getSharedRuntime, warmupSharedRuntime } from "./runtime.js";
26
+ function steerRouteUnsupportedError(provider, maxLayer) {
27
+ return new TensorCostConfigError(`TensorCost maxLayer '${maxLayer}' is not supported for ${provider} clients. ` +
28
+ "Bedrock and Vertex use provider-native signing that cannot be intercepted at " +
29
+ "the proxy layer. Use maxLayer 'govern' or lower for telemetry, or wrap an " +
30
+ "OpenAI / Anthropic client for steer/route.");
31
+ }
36
32
  export function wrap(client, options = {}) {
37
- // resolveConfig throws MissingConfigError when apiKey is absent, and
38
- // TensorCostConfigError when appliedMode=true with no proxyUrl.
39
- // Both are intentional non-fail-open cases — see config.ts header.
40
33
  const config = resolveConfig(options);
41
- const transport = new ObservationTransport({
42
- baseUrl: config.baseUrl,
43
- apiKey: config.apiKey,
44
- tenantId: config.tenantId,
45
- failOpen: config.failOpen,
46
- });
47
- const provider = detectProvider(client);
48
- // Bedrock does not support applied mode. SigV4 signing happens inside
49
- // the AWS SDK before our middleware can intercept the request, and the
50
- // signed URL/headers are region-specific. Fail loudly here rather than
51
- // silently ignoring the option — the customer almost certainly made a
52
- // configuration mistake.
53
- if (provider === "bedrock" && config.appliedMode) {
54
- throw new TensorCostConfigError("TensorCost applied mode is not supported for AWS Bedrock clients. " +
55
- "Bedrock uses SigV4 signing that cannot be intercepted at the proxy layer. " +
56
- "Remove { appliedMode: true } or use an OpenAI / Anthropic client instead.");
34
+ // L0 off — no transport, no provider hooks.
35
+ if (config.maxLayer === "off") {
36
+ return client;
57
37
  }
58
- if (provider === "vertex" && config.appliedMode) {
59
- throw new TensorCostConfigError("TensorCost applied mode is not supported for Google Vertex AI clients. " +
60
- "Vertex uses Application Default Credentials and Google-managed transport " +
61
- "that cannot be intercepted at the proxy layer. Remove { appliedMode: true } " +
62
- "or use an OpenAI / Anthropic client instead.");
38
+ const provider = detectProvider(client);
39
+ if (needsProxyUrl(config.maxLayer) &&
40
+ (provider === "bedrock" || provider === "vertex")) {
41
+ throw steerRouteUnsupportedError(provider, config.maxLayer);
63
42
  }
64
- // Applied mode: redirect the client to the proxy before installing
65
- // the observe-only wrappers. Order matters — installAppliedMode mutates
66
- // baseURL and defaultHeaders, and the provider wrappers capture the
67
- // (now-modified) bound methods, so post-call observations correctly
68
- // attribute the response to the original provider model even though
69
- // the network call went through the proxy.
70
- if (config.appliedMode && config.proxyUrl !== null) {
71
- installAppliedMode(client, config.proxyUrl, provider);
43
+ const { transport, sdkLayer } = getSharedRuntime(config);
44
+ warmupSharedRuntime(config);
45
+ if (needsProxyUrl(config.maxLayer) &&
46
+ config.proxyUrl !== null &&
47
+ (provider === "openai" || provider === "anthropic")) {
48
+ installAppliedMode(client, config.proxyUrl, provider, {
49
+ application: config.application,
50
+ tags: config.tags,
51
+ customer: config.customer,
52
+ feature: config.feature,
53
+ });
72
54
  }
73
- // One circuit breaker per wrap() call — shared across all requests
74
- // dispatched through this client instance.
75
- const circuit = new CircuitBreaker();
55
+ const circuit = new CircuitBreaker(undefined, bindCircuitObservations(transport, {
56
+ provider,
57
+ tenantId: config.tenantId,
58
+ environment: config.environment,
59
+ }));
60
+ const hardening = {
61
+ maxLayer: config.maxLayer,
62
+ appliedMode: config.appliedMode,
63
+ proxyUrl: config.proxyUrl,
64
+ baseUrl: config.baseUrl,
65
+ sdkLayer,
66
+ retry: config.retry,
67
+ timeoutMs: config.timeoutMs,
68
+ headersTimeoutMs: config.headersTimeoutMs,
69
+ onLifecycleEvent: config.onLifecycleEvent,
70
+ failOpenEnabled: config.failOpenEnabled,
71
+ circuit,
72
+ application: config.application,
73
+ tags: config.tags,
74
+ customer: config.customer,
75
+ feature: config.feature,
76
+ };
76
77
  if (provider === "openai") {
77
- openaiProvider.install(client, transport, config.tenantId, config.environment, config.connectionId, {
78
- appliedMode: config.appliedMode,
79
- proxyUrl: config.proxyUrl,
80
- retry: config.retry,
81
- timeoutMs: config.timeoutMs,
82
- onLifecycleEvent: config.onLifecycleEvent,
83
- failOpenEnabled: config.failOpenEnabled,
84
- circuit,
85
- }, config.deployment, config.promptTemplateId, config.agentId, config.workflowId);
78
+ openaiProvider.install(client, transport, config.tenantId, config.environment, config.connectionId, hardening, config.deployment, config.promptTemplateId, config.agentId, config.workflowId);
86
79
  }
87
80
  else if (provider === "anthropic") {
88
- anthropicProvider.install(client, transport, config.tenantId, config.environment, config.connectionId, {
89
- appliedMode: config.appliedMode,
90
- proxyUrl: config.proxyUrl,
91
- retry: config.retry,
92
- timeoutMs: config.timeoutMs,
93
- onLifecycleEvent: config.onLifecycleEvent,
94
- failOpenEnabled: config.failOpenEnabled,
95
- circuit,
96
- }, config.deployment, config.promptTemplateId, config.agentId, config.workflowId);
81
+ anthropicProvider.install(client, transport, config.tenantId, config.environment, config.connectionId, hardening, config.deployment, config.promptTemplateId, config.agentId, config.workflowId);
97
82
  }
98
83
  else if (provider === "bedrock") {
99
- bedrockProvider.install(client, transport, config.tenantId, config.environment, config.connectionId, config.deployment, config.promptTemplateId, config.agentId, config.workflowId);
84
+ bedrockProvider.install(client, transport, config.tenantId, config.environment, config.connectionId, config.deployment, config.promptTemplateId, config.agentId, config.workflowId, config.customer, config.feature);
100
85
  }
101
86
  else {
102
- // provider === "vertex"
103
- vertexProvider.install(client, transport, config.tenantId, config.environment, config.connectionId, config.deployment, config.promptTemplateId, config.agentId, config.workflowId);
87
+ vertexProvider.install(client, transport, config.tenantId, config.environment, config.connectionId, config.deployment, config.promptTemplateId, config.agentId, config.workflowId, config.customer, config.feature);
104
88
  }
105
89
  return client;
106
90
  }
91
+ export const __test__ = { layerRank, steerRouteUnsupportedError };
107
92
  //# sourceMappingURL=wrap.js.map
package/dist/wrap.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"wrap.js","sourceRoot":"","sources":["../src/wrap.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM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AAC,QAAQ,EACf,MAAM,CAAC,WAAW,EAClB,MAAM,CAAC,YAAY,EACnB,SAAS,EACT,MAAM,CAAC,UAAU,EACjB,MAAM,CAAC,gBAAgB,EACvB,MAAM,CAAC,OAAO,EACd,MAAM,CAAC,UAAU,CAClB,CAAC;IACJ,CAAC;SAAM,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QAClC,eAAe,CAAC,OAAO,CACrB,MAAM,EACN,SAAS,EACT,MAAM,CAAC,QAAQ,EACf,MAAM,CAAC,WAAW,EAClB,MAAM,CAAC,YAAY,EACnB,MAAM,CAAC,UAAU,EACjB,MAAM,CAAC,gBAAgB,EACvB,MAAM,CAAC,OAAO,EACd,MAAM,CAAC,UAAU,EACjB,MAAM,CAAC,QAAQ,EACf,MAAM,CAAC,OAAO,CACf,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,cAAc,CAAC,OAAO,CACpB,MAAM,EACN,SAAS,EACT,MAAM,CAAC,QAAQ,EACf,MAAM,CAAC,WAAW,EAClB,MAAM,CAAC,YAAY,EACnB,MAAM,CAAC,UAAU,EACjB,MAAM,CAAC,gBAAgB,EACvB,MAAM,CAAC,OAAO,EACd,MAAM,CAAC,UAAU,EACjB,MAAM,CAAC,QAAQ,EACf,MAAM,CAAC,OAAO,CACf,CAAC;IACJ,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,CAAC,MAAM,QAAQ,GAAG,EAAE,SAAS,EAAE,0BAA0B,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tensorcost/sdk",
3
- "version": "0.6.0",
3
+ "version": "1.1.0",
4
4
  "description": "One-line wrapper SDK for TensorCost — fire-and-forget LLM observability for OpenAI, Anthropic, AWS Bedrock, and Google Vertex AI clients.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",