@tensorcost/sdk 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +74 -0
- package/README.md +150 -5
- package/dist/circuit.d.ts +56 -0
- package/dist/circuit.d.ts.map +1 -0
- package/dist/circuit.js +93 -0
- package/dist/circuit.js.map +1 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +17 -0
- package/dist/config.js.map +1 -1
- package/dist/errors.d.ts +67 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +84 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/providers/anthropic.d.ts +17 -1
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +179 -20
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/providers/bedrock.d.ts +48 -0
- package/dist/providers/bedrock.d.ts.map +1 -0
- package/dist/providers/bedrock.js +277 -0
- package/dist/providers/bedrock.js.map +1 -0
- package/dist/providers/detect.d.ts +9 -3
- package/dist/providers/detect.d.ts.map +1 -1
- package/dist/providers/detect.js +24 -6
- package/dist/providers/detect.js.map +1 -1
- package/dist/providers/index.d.ts +1 -0
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +1 -0
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/openai.d.ts +19 -1
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +217 -20
- package/dist/providers/openai.js.map +1 -1
- package/dist/proxy-client.d.ts +64 -0
- package/dist/proxy-client.d.ts.map +1 -0
- package/dist/proxy-client.js +259 -0
- package/dist/proxy-client.js.map +1 -0
- package/dist/retry.d.ts +50 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +80 -0
- package/dist/retry.js.map +1 -0
- package/dist/telemetry.d.ts +70 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/dist/telemetry.js +41 -0
- package/dist/telemetry.js.map +1 -0
- package/dist/types.d.ts +63 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/wrap.d.ts +9 -0
- package/dist/wrap.d.ts.map +1 -1
- package/dist/wrap.js +47 -2
- package/dist/wrap.js.map +1 -1
- package/package.json +12 -2
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { wrap } from "./wrap.js";
|
|
2
2
|
export { MissingConfigError, TensorCostConfigError } from "./config.js";
|
|
3
3
|
export { UnsupportedClientError } from "./providers/detect.js";
|
|
4
|
-
export
|
|
4
|
+
export { TensorCostError, TensorCostNetworkError, TensorCostTimeoutError, TensorCostProxyError, TensorCostQuotaError, TensorCostProviderError, } from "./errors.js";
|
|
5
|
+
export const version = "0.4.0"; // Bedrock observe-only support added
|
|
5
6
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,EACL,eAAe,EACf,sBAAsB,EACtB,sBAAsB,EACtB,oBAAoB,EACpB,oBAAoB,EACpB,uBAAuB,GACxB,MAAM,aAAa,CAAC;AAarB,MAAM,CAAC,MAAM,OAAO,GAAG,OAAO,CAAC,CAAC,qCAAqC"}
|
|
@@ -4,9 +4,25 @@
|
|
|
4
4
|
* Intercepts `client.messages.create`. Same observation contract as the
|
|
5
5
|
* OpenAI wrapper — only metadata is captured (model, input/output tokens),
|
|
6
6
|
* never the message content itself.
|
|
7
|
+
*
|
|
8
|
+
* Applied-mode hardening (v0.4.0): messages.create routes through the
|
|
9
|
+
* proxy-client module, which handles retries, timeouts, typed errors,
|
|
10
|
+
* telemetry hooks, and the fail-open circuit breaker.
|
|
7
11
|
*/
|
|
8
12
|
import type { ObservationTransport } from "../transport.js";
|
|
9
|
-
|
|
13
|
+
import type { CircuitBreaker } from "../circuit.js";
|
|
14
|
+
import type { LifecycleEventCallback } from "../telemetry.js";
|
|
15
|
+
import type { RetryConfig } from "../retry.js";
|
|
16
|
+
export interface AppliedModeHardeningOpts {
|
|
17
|
+
appliedMode: boolean;
|
|
18
|
+
proxyUrl: string | null;
|
|
19
|
+
retry: RetryConfig;
|
|
20
|
+
timeoutMs: number;
|
|
21
|
+
onLifecycleEvent: LifecycleEventCallback | undefined;
|
|
22
|
+
failOpenEnabled: boolean;
|
|
23
|
+
circuit: CircuitBreaker;
|
|
24
|
+
}
|
|
25
|
+
export declare function install(client: unknown, transport: ObservationTransport, tenantId: string | null, environment: string | null, connectionId: string | null, hardening: AppliedModeHardeningOpts): void;
|
|
10
26
|
export declare const __test__: {
|
|
11
27
|
MARKER: string;
|
|
12
28
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"anthropic.d.ts","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAG5D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEpD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAC9D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAK/C,MAAM,WAAW,wBAAwB;IACvC,WAAW,EAAE,OAAO,CAAC;IACrB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,KAAK,EAAE,WAAW,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,gBAAgB,EAAE,sBAAsB,GAAG,SAAS,CAAC;IACrD,eAAe,EAAE,OAAO,CAAC;IACzB,OAAO,EAAE,cAAc,CAAC;CACzB;AA6PD,wBAAgB,OAAO,CACrB,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,oBAAoB,EAC/B,QAAQ,EAAE,MAAM,GAAG,IAAI,EACvB,WAAW,EAAE,MAAM,GAAG,IAAI,EAC1B,YAAY,EAAE,MAAM,GAAG,IAAI,EAC3B,SAAS,EAAE,wBAAwB,GAClC,IAAI,CAoBN;AAED,eAAO,MAAM,QAAQ;;CAAa,CAAC"}
|
|
@@ -4,9 +4,16 @@
|
|
|
4
4
|
* Intercepts `client.messages.create`. Same observation contract as the
|
|
5
5
|
* OpenAI wrapper — only metadata is captured (model, input/output tokens),
|
|
6
6
|
* never the message content itself.
|
|
7
|
+
*
|
|
8
|
+
* Applied-mode hardening (v0.4.0): messages.create routes through the
|
|
9
|
+
* proxy-client module, which handles retries, timeouts, typed errors,
|
|
10
|
+
* telemetry hooks, and the fail-open circuit breaker.
|
|
7
11
|
*/
|
|
8
12
|
import { randomUUID } from "node:crypto";
|
|
9
|
-
|
|
13
|
+
import { proxyRequest } from "../proxy-client.js";
|
|
14
|
+
import { TensorCostProxyError } from "../errors.js";
|
|
15
|
+
import { emitEvent } from "../telemetry.js";
|
|
16
|
+
const SDK_VERSION = "tensorcost-node/0.4.0";
|
|
10
17
|
const MARKER = "__tensorcost_wrapped__";
|
|
11
18
|
function nowIso() {
|
|
12
19
|
return new Date().toISOString();
|
|
@@ -31,6 +38,168 @@ function wrapMethod(bound, opts) {
|
|
|
31
38
|
const model = (firstArg && typeof firstArg === "object" && firstArg.model) ||
|
|
32
39
|
"unknown";
|
|
33
40
|
const correlationId = randomUUID();
|
|
41
|
+
// Applied-mode path.
|
|
42
|
+
if (opts.hardening.appliedMode && opts.hardening.proxyUrl) {
|
|
43
|
+
// Read the original provider URL from defaultHeaders (set at installAppliedMode).
|
|
44
|
+
const dh = opts.clientRef && typeof opts.clientRef === "object"
|
|
45
|
+
? opts.clientRef["defaultHeaders"]
|
|
46
|
+
: undefined;
|
|
47
|
+
const providerUrl = dh?.["x-tc-provider-url"] ?? "https://api.anthropic.com";
|
|
48
|
+
const providerAuth = dh?.["x-tc-provider-auth"] ?? "";
|
|
49
|
+
const body = {
|
|
50
|
+
...(firstArg && typeof firstArg === "object" ? firstArg : {}),
|
|
51
|
+
model: String(model),
|
|
52
|
+
};
|
|
53
|
+
const circuitWasOpen = opts.hardening.circuit.shouldBypass();
|
|
54
|
+
let proxyResult;
|
|
55
|
+
let proxyFailed = false;
|
|
56
|
+
let proxyFailureError = null;
|
|
57
|
+
if (!circuitWasOpen) {
|
|
58
|
+
try {
|
|
59
|
+
const getToken = opts.getToken ?? (() => opts.transport.getToken());
|
|
60
|
+
const bearerToken = await getToken();
|
|
61
|
+
proxyResult = await proxyRequest({
|
|
62
|
+
proxyUrl: opts.hardening.proxyUrl,
|
|
63
|
+
bearerToken,
|
|
64
|
+
providerUrl,
|
|
65
|
+
providerAuth,
|
|
66
|
+
body,
|
|
67
|
+
model: String(model),
|
|
68
|
+
provider: "anthropic",
|
|
69
|
+
operation: "messages",
|
|
70
|
+
correlationId,
|
|
71
|
+
environment: opts.environment,
|
|
72
|
+
retry: opts.hardening.retry,
|
|
73
|
+
timeoutMs: opts.hardening.timeoutMs,
|
|
74
|
+
onLifecycleEvent: opts.hardening.onLifecycleEvent,
|
|
75
|
+
circuit: opts.hardening.circuit,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
catch (proxyErr) {
|
|
79
|
+
proxyFailed = true;
|
|
80
|
+
proxyFailureError = proxyErr;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
proxyFailed = true;
|
|
85
|
+
}
|
|
86
|
+
if (proxyFailed || circuitWasOpen) {
|
|
87
|
+
if (!opts.hardening.failOpenEnabled) {
|
|
88
|
+
const tcErr = proxyFailureError instanceof TensorCostProxyError
|
|
89
|
+
? proxyFailureError
|
|
90
|
+
: new TensorCostProxyError(proxyFailureError?.message ?? "proxy unavailable", { cause: proxyFailureError ?? undefined, attempt: 1 });
|
|
91
|
+
const obs = {
|
|
92
|
+
sdk_version: SDK_VERSION,
|
|
93
|
+
provider: "anthropic",
|
|
94
|
+
model: String(model),
|
|
95
|
+
operation: "messages",
|
|
96
|
+
request_at: requestAt,
|
|
97
|
+
response_at: nowIso(),
|
|
98
|
+
input_tokens: null,
|
|
99
|
+
output_tokens: null,
|
|
100
|
+
cost_usd_cents: null,
|
|
101
|
+
status: "error",
|
|
102
|
+
error_message: `${tcErr.name}: ${tcErr.message}`,
|
|
103
|
+
correlation_id: correlationId,
|
|
104
|
+
tenant_id: opts.tenantId,
|
|
105
|
+
...(opts.environment !== null ? { environment: opts.environment } : {}),
|
|
106
|
+
...(opts.connectionId !== null ? { connection_id: opts.connectionId } : {}),
|
|
107
|
+
};
|
|
108
|
+
try {
|
|
109
|
+
opts.transport.post(obs);
|
|
110
|
+
}
|
|
111
|
+
catch { /* belt-and-suspenders */ }
|
|
112
|
+
throw tcErr;
|
|
113
|
+
}
|
|
114
|
+
emitEvent(opts.hardening.onLifecycleEvent, {
|
|
115
|
+
kind: "on_fallback",
|
|
116
|
+
provider: "anthropic",
|
|
117
|
+
model: String(model),
|
|
118
|
+
operation: "messages",
|
|
119
|
+
attemptNumber: 1,
|
|
120
|
+
elapsedMs: 0,
|
|
121
|
+
consecutiveFailures: opts.hardening.circuit.failures,
|
|
122
|
+
});
|
|
123
|
+
try {
|
|
124
|
+
const directResponse = await bound.apply(this, args);
|
|
125
|
+
const { input, output } = extractUsage(directResponse);
|
|
126
|
+
const obs = {
|
|
127
|
+
sdk_version: SDK_VERSION,
|
|
128
|
+
provider: "anthropic",
|
|
129
|
+
model: String(model),
|
|
130
|
+
operation: "messages",
|
|
131
|
+
request_at: requestAt,
|
|
132
|
+
response_at: nowIso(),
|
|
133
|
+
input_tokens: input,
|
|
134
|
+
output_tokens: output,
|
|
135
|
+
cost_usd_cents: null,
|
|
136
|
+
status: "success",
|
|
137
|
+
error_message: null,
|
|
138
|
+
correlation_id: correlationId,
|
|
139
|
+
tenant_id: opts.tenantId,
|
|
140
|
+
...(opts.environment !== null ? { environment: opts.environment } : {}),
|
|
141
|
+
...(opts.connectionId !== null ? { connection_id: opts.connectionId } : {}),
|
|
142
|
+
};
|
|
143
|
+
try {
|
|
144
|
+
opts.transport.post(obs);
|
|
145
|
+
}
|
|
146
|
+
catch { /* belt-and-suspenders */ }
|
|
147
|
+
return directResponse;
|
|
148
|
+
}
|
|
149
|
+
catch (directErr) {
|
|
150
|
+
const obs = {
|
|
151
|
+
sdk_version: SDK_VERSION,
|
|
152
|
+
provider: "anthropic",
|
|
153
|
+
model: String(model),
|
|
154
|
+
operation: "messages",
|
|
155
|
+
request_at: requestAt,
|
|
156
|
+
response_at: nowIso(),
|
|
157
|
+
input_tokens: null,
|
|
158
|
+
output_tokens: null,
|
|
159
|
+
cost_usd_cents: null,
|
|
160
|
+
status: "error",
|
|
161
|
+
error_message: `${directErr.name}: ${directErr.message}`,
|
|
162
|
+
correlation_id: correlationId,
|
|
163
|
+
tenant_id: opts.tenantId,
|
|
164
|
+
...(opts.environment !== null ? { environment: opts.environment } : {}),
|
|
165
|
+
...(opts.connectionId !== null ? { connection_id: opts.connectionId } : {}),
|
|
166
|
+
};
|
|
167
|
+
try {
|
|
168
|
+
opts.transport.post(obs);
|
|
169
|
+
}
|
|
170
|
+
catch { /* belt-and-suspenders */ }
|
|
171
|
+
throw directErr;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
// Proxy success.
|
|
175
|
+
const respBody = proxyResult;
|
|
176
|
+
const usageRaw = respBody?.usage;
|
|
177
|
+
const input = typeof usageRaw?.input_tokens === "number" ? usageRaw.input_tokens : null;
|
|
178
|
+
const output = typeof usageRaw?.output_tokens === "number" ? usageRaw.output_tokens : null;
|
|
179
|
+
const obs = {
|
|
180
|
+
sdk_version: SDK_VERSION,
|
|
181
|
+
provider: "anthropic",
|
|
182
|
+
model: String(model),
|
|
183
|
+
operation: "messages",
|
|
184
|
+
request_at: requestAt,
|
|
185
|
+
response_at: nowIso(),
|
|
186
|
+
input_tokens: input,
|
|
187
|
+
output_tokens: output,
|
|
188
|
+
cost_usd_cents: null,
|
|
189
|
+
status: "success",
|
|
190
|
+
error_message: null,
|
|
191
|
+
correlation_id: correlationId,
|
|
192
|
+
tenant_id: opts.tenantId,
|
|
193
|
+
...(opts.environment !== null ? { environment: opts.environment } : {}),
|
|
194
|
+
...(opts.connectionId !== null ? { connection_id: opts.connectionId } : {}),
|
|
195
|
+
};
|
|
196
|
+
try {
|
|
197
|
+
opts.transport.post(obs);
|
|
198
|
+
}
|
|
199
|
+
catch { /* belt-and-suspenders */ }
|
|
200
|
+
return proxyResult;
|
|
201
|
+
}
|
|
202
|
+
// Observe-only path.
|
|
34
203
|
try {
|
|
35
204
|
const response = await bound.apply(this, args);
|
|
36
205
|
const { input, output } = extractUsage(response);
|
|
@@ -48,19 +217,13 @@ function wrapMethod(bound, opts) {
|
|
|
48
217
|
error_message: null,
|
|
49
218
|
correlation_id: correlationId,
|
|
50
219
|
tenant_id: opts.tenantId,
|
|
51
|
-
...(opts.environment !== null
|
|
52
|
-
|
|
53
|
-
: {}),
|
|
54
|
-
...(opts.connectionId !== null
|
|
55
|
-
? { connection_id: opts.connectionId }
|
|
56
|
-
: {}),
|
|
220
|
+
...(opts.environment !== null ? { environment: opts.environment } : {}),
|
|
221
|
+
...(opts.connectionId !== null ? { connection_id: opts.connectionId } : {}),
|
|
57
222
|
};
|
|
58
223
|
try {
|
|
59
224
|
opts.transport.post(obs);
|
|
60
225
|
}
|
|
61
|
-
catch {
|
|
62
|
-
// transport already fail-open
|
|
63
|
-
}
|
|
226
|
+
catch { /* belt-and-suspenders */ }
|
|
64
227
|
return response;
|
|
65
228
|
}
|
|
66
229
|
catch (err) {
|
|
@@ -78,26 +241,20 @@ function wrapMethod(bound, opts) {
|
|
|
78
241
|
error_message: `${err.name}: ${err.message}`,
|
|
79
242
|
correlation_id: correlationId,
|
|
80
243
|
tenant_id: opts.tenantId,
|
|
81
|
-
...(opts.environment !== null
|
|
82
|
-
|
|
83
|
-
: {}),
|
|
84
|
-
...(opts.connectionId !== null
|
|
85
|
-
? { connection_id: opts.connectionId }
|
|
86
|
-
: {}),
|
|
244
|
+
...(opts.environment !== null ? { environment: opts.environment } : {}),
|
|
245
|
+
...(opts.connectionId !== null ? { connection_id: opts.connectionId } : {}),
|
|
87
246
|
};
|
|
88
247
|
try {
|
|
89
248
|
opts.transport.post(obs);
|
|
90
249
|
}
|
|
91
|
-
catch {
|
|
92
|
-
// see above
|
|
93
|
-
}
|
|
250
|
+
catch { /* belt-and-suspenders */ }
|
|
94
251
|
throw err;
|
|
95
252
|
}
|
|
96
253
|
};
|
|
97
254
|
Object.defineProperty(wrapped, MARKER, { value: true, enumerable: false });
|
|
98
255
|
return wrapped;
|
|
99
256
|
}
|
|
100
|
-
export function install(client, transport, tenantId, environment, connectionId) {
|
|
257
|
+
export function install(client, transport, tenantId, environment, connectionId, hardening) {
|
|
101
258
|
if (!client || typeof client !== "object")
|
|
102
259
|
return;
|
|
103
260
|
const c = client;
|
|
@@ -112,6 +269,8 @@ export function install(client, transport, tenantId, environment, connectionId)
|
|
|
112
269
|
tenantId,
|
|
113
270
|
environment,
|
|
114
271
|
connectionId,
|
|
272
|
+
hardening,
|
|
273
|
+
clientRef: client,
|
|
115
274
|
});
|
|
116
275
|
}
|
|
117
276
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"anthropic.js","sourceRoot":"","sources":["../../src/providers/anthropic.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAEpD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAI5C,MAAM,WAAW,GAAG,uBAAuB,CAAC;AAC5C,MAAM,MAAM,GAAG,wBAAwB,CAAC;AAYxC,SAAS,MAAM;IACb,OAAO,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;AAClC,CAAC;AAED,SAAS,YAAY,CACnB,QAAiB;IAEjB,IAAI,CAAC,QAAQ,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;QAC9C,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;IACvC,CAAC;IACD,MAAM,KAAK,GAAI,QAAgC,CAAC,KAAK,CAAC;IACtD,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACxC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;IACvC,CAAC;IACD,MAAM,CAAC,GAAG,KAAgC,CAAC;IAC3C,MAAM,KAAK,GAAG,OAAO,CAAC,CAAC,YAAY,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC;IACzE,MAAM,MAAM,GACV,OAAO,CAAC,CAAC,aAAa,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,aAA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Q,GAAG,EAAE,MAAM,EAAE,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWS Bedrock observe-only client wrapping.
|
|
3
|
+
*
|
|
4
|
+
* Bedrock differs fundamentally from OpenAI and Anthropic: requests go through
|
|
5
|
+
* the AWS Smithy client's `send()` method rather than named sub-resource methods
|
|
6
|
+
* like `chat.completions.create`. The Smithy client also handles SigV4 signing
|
|
7
|
+
* internally, so we cannot intercept the HTTP layer with a baseURL redirect the
|
|
8
|
+
* way applied mode does for the HTTP-Bearer providers.
|
|
9
|
+
*
|
|
10
|
+
* What we CAN do: register a Smithy middleware on `client.middlewareStack` that
|
|
11
|
+
* wraps the inner `next()` handler. Every `client.send(command)` call passes
|
|
12
|
+
* through the middleware stack, so this gives us a clean before/after hook with
|
|
13
|
+
* access to the command input and the resolved output.
|
|
14
|
+
*
|
|
15
|
+
* Supported commands (non-streaming):
|
|
16
|
+
* - InvokeModelCommand → operation "bedrock.invoke_model"
|
|
17
|
+
* - ConverseCommand → operation "bedrock.converse"
|
|
18
|
+
*
|
|
19
|
+
* Streaming commands (token accounting deferred):
|
|
20
|
+
* - InvokeModelWithResponseStreamCommand → operation "bedrock.invoke_model_stream"
|
|
21
|
+
* - ConverseStreamCommand → operation "bedrock.converse_stream"
|
|
22
|
+
*
|
|
23
|
+
* For streaming commands we emit an observation with input_tokens=null,
|
|
24
|
+
* output_tokens=null, and a note in error_message that per-chunk token
|
|
25
|
+
* accounting is not yet implemented. The observation still records latency
|
|
26
|
+
* and success/error status — enough for cost attribution once per-model
|
|
27
|
+
* pricing and a token estimator are wired in.
|
|
28
|
+
*
|
|
29
|
+
* Token-count extraction:
|
|
30
|
+
* Bedrock does not standardise usage across model families. We probe three
|
|
31
|
+
* common shapes in order:
|
|
32
|
+
* 1. Anthropic-on-Bedrock (InvokeModel): body JSON → usage.input_tokens / output_tokens
|
|
33
|
+
* 2. Amazon Nova / Titan (InvokeModel): body JSON → usage.inputTokens / outputTokens
|
|
34
|
+
* 3. Converse API (any model): response.usage.inputTokens / outputTokens
|
|
35
|
+
* If none match we emit 0/0 and log a warning so operators can report gaps.
|
|
36
|
+
*
|
|
37
|
+
* Applied mode:
|
|
38
|
+
* Not supported. SigV4 signing happens inside the AWS SDK before our
|
|
39
|
+
* middleware sees the request, and the signed URL/headers are region-specific.
|
|
40
|
+
* wrap(bedrockClient, { appliedMode: true }) throws TensorCostConfigError
|
|
41
|
+
* immediately in wrap.ts (before this module is called).
|
|
42
|
+
*/
|
|
43
|
+
import type { ObservationTransport } from "../transport.js";
|
|
44
|
+
export declare function install(client: unknown, transport: ObservationTransport, tenantId: string | null, environment: string | null, connectionId: string | null): void;
|
|
45
|
+
export declare const __test__: {
|
|
46
|
+
MIDDLEWARE_NAME: string;
|
|
47
|
+
};
|
|
48
|
+
//# sourceMappingURL=bedrock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bedrock.d.ts","sourceRoot":"","sources":["../../src/providers/bedrock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AAIH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAgK5D,wBAAgB,OAAO,CACrB,MAAM,EAAE,OAAO,EACf,SAAS,EAAE,oBAAoB,EAC/B,QAAQ,EAAE,MAAM,GAAG,IAAI,EACvB,WAAW,EAAE,MAAM,GAAG,IAAI,EAC1B,YAAY,EAAE,MAAM,GAAG,IAAI,GAC1B,IAAI,CAoIN;AAED,eAAO,MAAM,QAAQ;;CAAsB,CAAC"}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AWS Bedrock observe-only client wrapping.
|
|
3
|
+
*
|
|
4
|
+
* Bedrock differs fundamentally from OpenAI and Anthropic: requests go through
|
|
5
|
+
* the AWS Smithy client's `send()` method rather than named sub-resource methods
|
|
6
|
+
* like `chat.completions.create`. The Smithy client also handles SigV4 signing
|
|
7
|
+
* internally, so we cannot intercept the HTTP layer with a baseURL redirect the
|
|
8
|
+
* way applied mode does for the HTTP-Bearer providers.
|
|
9
|
+
*
|
|
10
|
+
* What we CAN do: register a Smithy middleware on `client.middlewareStack` that
|
|
11
|
+
* wraps the inner `next()` handler. Every `client.send(command)` call passes
|
|
12
|
+
* through the middleware stack, so this gives us a clean before/after hook with
|
|
13
|
+
* access to the command input and the resolved output.
|
|
14
|
+
*
|
|
15
|
+
* Supported commands (non-streaming):
|
|
16
|
+
* - InvokeModelCommand → operation "bedrock.invoke_model"
|
|
17
|
+
* - ConverseCommand → operation "bedrock.converse"
|
|
18
|
+
*
|
|
19
|
+
* Streaming commands (token accounting deferred):
|
|
20
|
+
* - InvokeModelWithResponseStreamCommand → operation "bedrock.invoke_model_stream"
|
|
21
|
+
* - ConverseStreamCommand → operation "bedrock.converse_stream"
|
|
22
|
+
*
|
|
23
|
+
* For streaming commands we emit an observation with input_tokens=null,
|
|
24
|
+
* output_tokens=null, and a note in error_message that per-chunk token
|
|
25
|
+
* accounting is not yet implemented. The observation still records latency
|
|
26
|
+
* and success/error status — enough for cost attribution once per-model
|
|
27
|
+
* pricing and a token estimator are wired in.
|
|
28
|
+
*
|
|
29
|
+
* Token-count extraction:
|
|
30
|
+
* Bedrock does not standardise usage across model families. We probe three
|
|
31
|
+
* common shapes in order:
|
|
32
|
+
* 1. Anthropic-on-Bedrock (InvokeModel): body JSON → usage.input_tokens / output_tokens
|
|
33
|
+
* 2. Amazon Nova / Titan (InvokeModel): body JSON → usage.inputTokens / outputTokens
|
|
34
|
+
* 3. Converse API (any model): response.usage.inputTokens / outputTokens
|
|
35
|
+
* If none match we emit 0/0 and log a warning so operators can report gaps.
|
|
36
|
+
*
|
|
37
|
+
* Applied mode:
|
|
38
|
+
* Not supported. SigV4 signing happens inside the AWS SDK before our
|
|
39
|
+
* middleware sees the request, and the signed URL/headers are region-specific.
|
|
40
|
+
* wrap(bedrockClient, { appliedMode: true }) throws TensorCostConfigError
|
|
41
|
+
* immediately in wrap.ts (before this module is called).
|
|
42
|
+
*/
|
|
43
|
+
import { randomUUID } from "node:crypto";
|
|
44
|
+
const SDK_VERSION = "tensorcost-node/0.4.0";
|
|
45
|
+
// Marker placed on the middleware to prevent double-registration when
|
|
46
|
+
// wrap() is called twice on the same client.
|
|
47
|
+
const MIDDLEWARE_NAME = "__tensorcost_bedrock_observe__";
|
|
48
|
+
// ── Helpers ───────────────────────────────────────────────────────────────────
|
|
49
|
+
function nowIso() {
|
|
50
|
+
return new Date().toISOString();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Map a Smithy commandName string to the Operation we record in the
|
|
54
|
+
* observation envelope. Falls through to "bedrock.invoke_model" for
|
|
55
|
+
* unknown command names so we always record something.
|
|
56
|
+
*/
|
|
57
|
+
function commandToOperation(commandName) {
|
|
58
|
+
switch (commandName) {
|
|
59
|
+
case "InvokeModelCommand":
|
|
60
|
+
return { operation: "bedrock.invoke_model", isStreaming: false };
|
|
61
|
+
case "ConverseCommand":
|
|
62
|
+
return { operation: "bedrock.converse", isStreaming: false };
|
|
63
|
+
case "InvokeModelWithResponseStreamCommand":
|
|
64
|
+
return { operation: "bedrock.invoke_model_stream", isStreaming: true };
|
|
65
|
+
case "ConverseStreamCommand":
|
|
66
|
+
return { operation: "bedrock.converse_stream", isStreaming: true };
|
|
67
|
+
default:
|
|
68
|
+
// Unknown Bedrock command — record it under invoke_model so it still
|
|
69
|
+
// shows up in the dashboard rather than being silently dropped.
|
|
70
|
+
return { operation: "bedrock.invoke_model", isStreaming: false };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Extract token counts from an InvokeModel response body.
|
|
75
|
+
*
|
|
76
|
+
* The response body is a Uint8Array containing a JSON document whose
|
|
77
|
+
* shape depends on the model family. We try two common shapes and fall
|
|
78
|
+
* back to null/null rather than throwing.
|
|
79
|
+
*/
|
|
80
|
+
function extractInvokeModelTokens(output) {
|
|
81
|
+
// The `body` field is a Uint8Array (or Buffer in Node).
|
|
82
|
+
const rawBody = output.body;
|
|
83
|
+
if (!rawBody)
|
|
84
|
+
return { input: null, output: null };
|
|
85
|
+
let parsed;
|
|
86
|
+
try {
|
|
87
|
+
const text = typeof rawBody === "string"
|
|
88
|
+
? rawBody
|
|
89
|
+
: Buffer.from(rawBody).toString("utf-8");
|
|
90
|
+
parsed = JSON.parse(text);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
console.warn("tensorcost: could not parse Bedrock InvokeModel response body for token counts");
|
|
94
|
+
return { input: null, output: null };
|
|
95
|
+
}
|
|
96
|
+
const usage = parsed.usage;
|
|
97
|
+
if (!usage || typeof usage !== "object") {
|
|
98
|
+
console.warn("tensorcost: Bedrock InvokeModel response has no usage field — " +
|
|
99
|
+
"token counts will be missing. If this is a new model family, " +
|
|
100
|
+
"please open an issue at https://github.com/vaadhlabs/tensorcost.");
|
|
101
|
+
return { input: null, output: null };
|
|
102
|
+
}
|
|
103
|
+
// Anthropic-on-Bedrock: usage.input_tokens / output_tokens
|
|
104
|
+
if (typeof usage.input_tokens === "number" ||
|
|
105
|
+
typeof usage.output_tokens === "number") {
|
|
106
|
+
return {
|
|
107
|
+
input: typeof usage.input_tokens === "number" ? usage.input_tokens : null,
|
|
108
|
+
output: typeof usage.output_tokens === "number" ? usage.output_tokens : null,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
// Amazon Nova / Titan: usage.inputTokens / outputTokens
|
|
112
|
+
if (typeof usage.inputTokens === "number" ||
|
|
113
|
+
typeof usage.outputTokens === "number") {
|
|
114
|
+
return {
|
|
115
|
+
input: typeof usage.inputTokens === "number" ? usage.inputTokens : null,
|
|
116
|
+
output: typeof usage.outputTokens === "number" ? usage.outputTokens : null,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
console.warn("tensorcost: Bedrock InvokeModel usage shape not recognised — " +
|
|
120
|
+
"token counts will be missing. Known shapes: " +
|
|
121
|
+
"Anthropic (usage.input_tokens/output_tokens), " +
|
|
122
|
+
"Nova/Titan (usage.inputTokens/outputTokens).");
|
|
123
|
+
return { input: null, output: null };
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Extract token counts from a Converse API response.
|
|
127
|
+
*
|
|
128
|
+
* The Converse API has a standardised usage block regardless of model
|
|
129
|
+
* family: response.usage.inputTokens / outputTokens.
|
|
130
|
+
*/
|
|
131
|
+
function extractConverseTokens(output) {
|
|
132
|
+
const usage = output.usage;
|
|
133
|
+
if (!usage || typeof usage !== "object") {
|
|
134
|
+
return { input: null, output: null };
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
input: typeof usage.inputTokens === "number" ? usage.inputTokens : null,
|
|
138
|
+
output: typeof usage.outputTokens === "number" ? usage.outputTokens : null,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
// ── Public install() ──────────────────────────────────────────────────────────
|
|
142
|
+
export function install(client, transport, tenantId, environment, connectionId) {
|
|
143
|
+
if (!client || typeof client !== "object")
|
|
144
|
+
return;
|
|
145
|
+
const c = client;
|
|
146
|
+
const middlewareStack = c.middlewareStack;
|
|
147
|
+
if (!middlewareStack || typeof middlewareStack.add !== "function") {
|
|
148
|
+
console.warn("tensorcost: BedrockRuntimeClient does not expose a middlewareStack — " +
|
|
149
|
+
"observations will not be recorded. Is @aws-sdk/client-bedrock-runtime >= 3.x installed?");
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
// Guard against double-registration when wrap() is called twice on the
|
|
153
|
+
// same client. The identify() API is available from SDK v3 onwards.
|
|
154
|
+
if (typeof middlewareStack.identify === "function") {
|
|
155
|
+
const existing = middlewareStack.identify();
|
|
156
|
+
const alreadyInstalled = existing.some(([name]) => name === MIDDLEWARE_NAME);
|
|
157
|
+
if (alreadyInstalled)
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const middleware = (next, context) => async (args) => {
|
|
161
|
+
const startedAt = performance.now();
|
|
162
|
+
const requestAt = nowIso();
|
|
163
|
+
const correlationId = randomUUID();
|
|
164
|
+
// modelId lives in the command input for both InvokeModel and Converse.
|
|
165
|
+
const modelId = typeof args.input.modelId === "string" ? args.input.modelId : "unknown";
|
|
166
|
+
const { operation, isStreaming } = commandToOperation(context.commandName);
|
|
167
|
+
const commonFields = {
|
|
168
|
+
sdk_version: SDK_VERSION,
|
|
169
|
+
provider: "bedrock",
|
|
170
|
+
model: modelId,
|
|
171
|
+
operation,
|
|
172
|
+
request_at: requestAt,
|
|
173
|
+
correlation_id: correlationId,
|
|
174
|
+
tenant_id: tenantId,
|
|
175
|
+
...(environment !== null ? { environment } : {}),
|
|
176
|
+
...(connectionId !== null ? { connection_id: connectionId } : {}),
|
|
177
|
+
};
|
|
178
|
+
// Streaming commands: we cannot aggregate tokens across chunks without
|
|
179
|
+
// collecting the full stream. Emit a placeholder observation now —
|
|
180
|
+
// per-chunk token accounting is a follow-up workstream.
|
|
181
|
+
if (isStreaming) {
|
|
182
|
+
let status = "success";
|
|
183
|
+
let errorMessage = null;
|
|
184
|
+
let result;
|
|
185
|
+
try {
|
|
186
|
+
result = await next(args);
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
status = "error";
|
|
190
|
+
errorMessage = `${err.name}: ${err.message}`;
|
|
191
|
+
const latencyMs = performance.now() - startedAt;
|
|
192
|
+
const obs = {
|
|
193
|
+
...commonFields,
|
|
194
|
+
response_at: nowIso(),
|
|
195
|
+
input_tokens: null,
|
|
196
|
+
output_tokens: null,
|
|
197
|
+
cost_usd_cents: null,
|
|
198
|
+
status: "error",
|
|
199
|
+
error_message: errorMessage,
|
|
200
|
+
};
|
|
201
|
+
try {
|
|
202
|
+
transport.post(obs);
|
|
203
|
+
}
|
|
204
|
+
catch { /* fail-open */ }
|
|
205
|
+
throw err;
|
|
206
|
+
}
|
|
207
|
+
const latencyMs = performance.now() - startedAt;
|
|
208
|
+
void latencyMs; // recorded via response_at timestamp difference
|
|
209
|
+
const obs = {
|
|
210
|
+
...commonFields,
|
|
211
|
+
response_at: nowIso(),
|
|
212
|
+
input_tokens: null,
|
|
213
|
+
output_tokens: null,
|
|
214
|
+
cost_usd_cents: null,
|
|
215
|
+
status,
|
|
216
|
+
// Signal to operators that this is a streaming placeholder, not a gap.
|
|
217
|
+
error_message: "streaming: per-chunk token accounting not yet implemented",
|
|
218
|
+
};
|
|
219
|
+
try {
|
|
220
|
+
transport.post(obs);
|
|
221
|
+
}
|
|
222
|
+
catch { /* fail-open */ }
|
|
223
|
+
return result;
|
|
224
|
+
}
|
|
225
|
+
// Non-streaming path: InvokeModel + Converse.
|
|
226
|
+
try {
|
|
227
|
+
const result = await next(args);
|
|
228
|
+
const latencyMs = performance.now() - startedAt;
|
|
229
|
+
void latencyMs;
|
|
230
|
+
let input = null;
|
|
231
|
+
let output = null;
|
|
232
|
+
if (operation === "bedrock.converse") {
|
|
233
|
+
({ input, output } = extractConverseTokens(result.output));
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
({ input, output } = extractInvokeModelTokens(result.output));
|
|
237
|
+
}
|
|
238
|
+
const obs = {
|
|
239
|
+
...commonFields,
|
|
240
|
+
response_at: nowIso(),
|
|
241
|
+
input_tokens: input,
|
|
242
|
+
output_tokens: output,
|
|
243
|
+
cost_usd_cents: null,
|
|
244
|
+
status: "success",
|
|
245
|
+
error_message: null,
|
|
246
|
+
};
|
|
247
|
+
try {
|
|
248
|
+
transport.post(obs);
|
|
249
|
+
}
|
|
250
|
+
catch { /* fail-open */ }
|
|
251
|
+
return result;
|
|
252
|
+
}
|
|
253
|
+
catch (err) {
|
|
254
|
+
const obs = {
|
|
255
|
+
...commonFields,
|
|
256
|
+
response_at: nowIso(),
|
|
257
|
+
input_tokens: null,
|
|
258
|
+
output_tokens: null,
|
|
259
|
+
cost_usd_cents: null,
|
|
260
|
+
status: "error",
|
|
261
|
+
error_message: `${err.name}: ${err.message}`,
|
|
262
|
+
};
|
|
263
|
+
try {
|
|
264
|
+
transport.post(obs);
|
|
265
|
+
}
|
|
266
|
+
catch { /* fail-open */ }
|
|
267
|
+
throw err;
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
middlewareStack.add(middleware, {
|
|
271
|
+
step: "build",
|
|
272
|
+
name: MIDDLEWARE_NAME,
|
|
273
|
+
priority: "normal",
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
export const __test__ = { MIDDLEWARE_NAME };
|
|
277
|
+
//# sourceMappingURL=bedrock.js.map
|