@withgateway/sdk 0.1.12 → 0.1.13
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 +37 -0
- package/dist/{index-CTc8iOrx.d.ts → index-B5HUQQDE.d.mts} +9 -0
- package/dist/{index-CTc8iOrx.d.mts → index-Dpo-t8iO.d.ts} +9 -0
- package/dist/index.d.mts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +267 -29
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +253 -23
- package/dist/index.mjs.map +1 -1
- package/dist/scores/index.d.mts +165 -0
- package/dist/scores/index.d.ts +165 -0
- package/dist/scores/index.js +252 -0
- package/dist/scores/index.js.map +1 -0
- package/dist/scores/index.mjs +223 -0
- package/dist/scores/index.mjs.map +1 -0
- package/dist/tracing/index.d.mts +2 -1
- package/dist/tracing/index.d.ts +2 -1
- package/dist/tracing/index.js +236 -14
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/index.mjs +230 -8
- package/dist/tracing/index.mjs.map +1 -1
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -152,6 +152,43 @@ experiments.clearExperimentContext() // Clear context (call after each request)
|
|
|
152
152
|
experiments.shutdown() // Cleanup (call on exit)
|
|
153
153
|
```
|
|
154
154
|
|
|
155
|
+
### Scores & Success Signals
|
|
156
|
+
|
|
157
|
+
Record evaluation scores and ROI-shaped **success signals** against a project.
|
|
158
|
+
A success signal is a score tagged with `gateway.success.signal`, which the
|
|
159
|
+
platform's **Success Metrics** feature aggregates into a metric's headline
|
|
160
|
+
count / pass-rate (the `HOOK` metric source). The score shape is identical to
|
|
161
|
+
the Python SDK's `run.success()`, so signals from either SDK aggregate the same.
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
import { ScoresClient, success } from "@withgateway/sdk/scores";
|
|
165
|
+
|
|
166
|
+
// Env-based (GATEWAY_HOST / GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY):
|
|
167
|
+
await success("resolved_ticket", true, { agentId: "support-triage", sessionId });
|
|
168
|
+
await success("handle_time_s", 42, { agentId: "support-triage", sessionId });
|
|
169
|
+
|
|
170
|
+
// Explicit keys:
|
|
171
|
+
const scores = new ScoresClient({ host, publicKey, secretKey });
|
|
172
|
+
await scores.success("resolved_ticket", true, { agentId: "support-triage", sessionId });
|
|
173
|
+
await scores.score("rubric", 0.9, { sessionId }); // a plain (non-signal) score
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
A boolean `value` is recorded as a 1/0 **BOOLEAN** score (pass/fail); a number
|
|
177
|
+
is a **NUMERIC** score. `metricName` defaults to the signal `name`. Provide
|
|
178
|
+
exactly one target — `sessionId`, `traceId` (+ optional `observationId`), or
|
|
179
|
+
`datasetRunId`; when none is given the active OpenTelemetry trace id is used,
|
|
180
|
+
and if there is none the call is a fail-soft no-op. Every `score`/`success`
|
|
181
|
+
call is **fail-soft**: a bad key or transport failure warns and resolves, never
|
|
182
|
+
throwing into your run. (`postScore` is the raw variant that DOES throw.)
|
|
183
|
+
|
|
184
|
+
Under `tracing.initIsolated(...)`, the returned handle carries a `scores`
|
|
185
|
+
client bound to that provider's project keys:
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
const handle = tracing.initIsolated({ host, publicKey, secretKey, agent: "support-agent" });
|
|
189
|
+
await handle.scores.success("resolved_ticket", true, { agentId: "support-agent", sessionId });
|
|
190
|
+
```
|
|
191
|
+
|
|
155
192
|
### Security Wrapper
|
|
156
193
|
|
|
157
194
|
Use the security wrapper when you want Gateway to control tool visibility,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Context, Tracer } from '@opentelemetry/api';
|
|
2
|
+
import { ScoresClient } from './scores/index.mjs';
|
|
2
3
|
import { SpanProcessor, Span, ReadableSpan } from '@opentelemetry/sdk-trace-base';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -186,6 +187,14 @@ interface TracingState {
|
|
|
186
187
|
*/
|
|
187
188
|
interface TracingHandle {
|
|
188
189
|
tracer: Tracer;
|
|
190
|
+
/**
|
|
191
|
+
* Scores/success-signal client bound to THIS provider's project keys. Emit
|
|
192
|
+
* ROI success signals into the same project the handle's spans export to —
|
|
193
|
+
* `handle.scores.success("resolved_ticket", true, { agentId, sessionId })` —
|
|
194
|
+
* without threading credentials through your own code. Fail-soft, like the
|
|
195
|
+
* rest of the client.
|
|
196
|
+
*/
|
|
197
|
+
scores: ScoresClient;
|
|
189
198
|
/** Force-flush pending spans for this provider only. */
|
|
190
199
|
flush(): Promise<void>;
|
|
191
200
|
/** Flush and shut down this provider only. */
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Context, Tracer } from '@opentelemetry/api';
|
|
2
|
+
import { ScoresClient } from './scores/index.js';
|
|
2
3
|
import { SpanProcessor, Span, ReadableSpan } from '@opentelemetry/sdk-trace-base';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -186,6 +187,14 @@ interface TracingState {
|
|
|
186
187
|
*/
|
|
187
188
|
interface TracingHandle {
|
|
188
189
|
tracer: Tracer;
|
|
190
|
+
/**
|
|
191
|
+
* Scores/success-signal client bound to THIS provider's project keys. Emit
|
|
192
|
+
* ROI success signals into the same project the handle's spans export to —
|
|
193
|
+
* `handle.scores.success("resolved_ticket", true, { agentId, sessionId })` —
|
|
194
|
+
* without threading credentials through your own code. Fail-soft, like the
|
|
195
|
+
* rest of the client.
|
|
196
|
+
*/
|
|
197
|
+
scores: ScoresClient;
|
|
189
198
|
/** Force-flush pending spans for this provider only. */
|
|
190
199
|
flush(): Promise<void>;
|
|
191
200
|
/** Flush and shut down this provider only. */
|
package/dist/index.d.mts
CHANGED
|
@@ -6,8 +6,9 @@ export { DriftError, LocalTool, MissPolicy, REPLAY_ENVIRONMENT, RecordedToolCall
|
|
|
6
6
|
export { GatewayAgentDescriptor, GatewaySecurity, GatewaySecurityConfig, GatewaySecurityContext, GatewaySecurityError, GatewayToolApprovalRequiredError, GatewayToolBlockedError, GatewayToolCallAuthorizeResponse, GatewayToolDescriptor, GatewayToolsetResolveResponse, SecureToolsOptions, gateway } from './security/index.mjs';
|
|
7
7
|
export { DEFAULT_TIMEOUT_SECONDS, ENV_GATEWAY_HOST, ENV_GATEWAY_PUBLIC_KEY, ENV_GATEWAY_SECRET_KEY, LoadSessionOptions, SessionLoadError, SessionObservation, SessionRecording, SessionTrace, basicAuthHeader, loadSession, sessionObservationFromApi, sessions } from './sessions/index.mjs';
|
|
8
8
|
export { UserEvent, UserEventOptions, UserProfile, UserProfileError, UserProfileUpdate, UsersClient, UsersClientConfig, UsersClientFromEnvConfig, users } from './users/index.mjs';
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
export { AGENT_ID_KEY, SUCCESS_METRIC_NAME_KEY, SUCCESS_SIGNAL_KEY, ScoreDataType, ScoreError, ScoreOptions, ScoresClient, ScoresClientConfig, ScoresClientFromEnvConfig, SuccessOptions, currentTraceId, scores, success } from './scores/index.mjs';
|
|
10
|
+
import { _ as _tracingModule } from './index-B5HUQQDE.mjs';
|
|
11
|
+
export { I as IdentifyOptions, i as identify, r as resolveUserId } from './index-B5HUQQDE.mjs';
|
|
11
12
|
import 'async_hooks';
|
|
12
13
|
import '@opentelemetry/api';
|
|
13
14
|
import '@opentelemetry/sdk-trace-base';
|
package/dist/index.d.ts
CHANGED
|
@@ -6,8 +6,9 @@ export { DriftError, LocalTool, MissPolicy, REPLAY_ENVIRONMENT, RecordedToolCall
|
|
|
6
6
|
export { GatewayAgentDescriptor, GatewaySecurity, GatewaySecurityConfig, GatewaySecurityContext, GatewaySecurityError, GatewayToolApprovalRequiredError, GatewayToolBlockedError, GatewayToolCallAuthorizeResponse, GatewayToolDescriptor, GatewayToolsetResolveResponse, SecureToolsOptions, gateway } from './security/index.js';
|
|
7
7
|
export { DEFAULT_TIMEOUT_SECONDS, ENV_GATEWAY_HOST, ENV_GATEWAY_PUBLIC_KEY, ENV_GATEWAY_SECRET_KEY, LoadSessionOptions, SessionLoadError, SessionObservation, SessionRecording, SessionTrace, basicAuthHeader, loadSession, sessionObservationFromApi, sessions } from './sessions/index.js';
|
|
8
8
|
export { UserEvent, UserEventOptions, UserProfile, UserProfileError, UserProfileUpdate, UsersClient, UsersClientConfig, UsersClientFromEnvConfig, users } from './users/index.js';
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
export { AGENT_ID_KEY, SUCCESS_METRIC_NAME_KEY, SUCCESS_SIGNAL_KEY, ScoreDataType, ScoreError, ScoreOptions, ScoresClient, ScoresClientConfig, ScoresClientFromEnvConfig, SuccessOptions, currentTraceId, scores, success } from './scores/index.js';
|
|
10
|
+
import { _ as _tracingModule } from './index-Dpo-t8iO.js';
|
|
11
|
+
export { I as IdentifyOptions, i as identify, r as resolveUserId } from './index-Dpo-t8iO.js';
|
|
11
12
|
import 'async_hooks';
|
|
12
13
|
import '@opentelemetry/api';
|
|
13
14
|
import '@opentelemetry/sdk-trace-base';
|
package/dist/index.js
CHANGED
|
@@ -25,7 +25,7 @@ var require_package = __commonJS({
|
|
|
25
25
|
"package.json"(exports2, module2) {
|
|
26
26
|
module2.exports = {
|
|
27
27
|
name: "@withgateway/sdk",
|
|
28
|
-
version: "0.1.
|
|
28
|
+
version: "0.1.13",
|
|
29
29
|
description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
|
|
30
30
|
main: "./dist/index.js",
|
|
31
31
|
module: "./dist/index.mjs",
|
|
@@ -80,6 +80,11 @@ var require_package = __commonJS({
|
|
|
80
80
|
types: "./dist/users/index.d.ts",
|
|
81
81
|
import: "./dist/users/index.mjs",
|
|
82
82
|
require: "./dist/users/index.js"
|
|
83
|
+
},
|
|
84
|
+
"./scores": {
|
|
85
|
+
types: "./dist/scores/index.d.ts",
|
|
86
|
+
import: "./dist/scores/index.mjs",
|
|
87
|
+
require: "./dist/scores/index.js"
|
|
83
88
|
}
|
|
84
89
|
},
|
|
85
90
|
files: [
|
|
@@ -165,6 +170,7 @@ var require_package = __commonJS({
|
|
|
165
170
|
// src/index.ts
|
|
166
171
|
var src_exports = {};
|
|
167
172
|
__export(src_exports, {
|
|
173
|
+
AGENT_ID_KEY: () => AGENT_ID_KEY,
|
|
168
174
|
DEFAULT_TIMEOUT_SECONDS: () => DEFAULT_TIMEOUT_SECONDS,
|
|
169
175
|
DriftError: () => DriftError,
|
|
170
176
|
ENV_GATEWAY_HOST: () => ENV_GATEWAY_HOST,
|
|
@@ -194,6 +200,10 @@ __export(src_exports, {
|
|
|
194
200
|
ReplayReport: () => ReplayReport,
|
|
195
201
|
ReplaySuite: () => ReplaySuite,
|
|
196
202
|
ReplayTask: () => ReplayTask,
|
|
203
|
+
SUCCESS_METRIC_NAME_KEY: () => SUCCESS_METRIC_NAME_KEY,
|
|
204
|
+
SUCCESS_SIGNAL_KEY: () => SUCCESS_SIGNAL_KEY,
|
|
205
|
+
ScoreError: () => ScoreError,
|
|
206
|
+
ScoresClient: () => ScoresClient,
|
|
197
207
|
SessionLoadError: () => SessionLoadError,
|
|
198
208
|
SessionRecording: () => SessionRecording,
|
|
199
209
|
SessionTrace: () => SessionTrace,
|
|
@@ -211,6 +221,7 @@ __export(src_exports, {
|
|
|
211
221
|
computeBuildHash: () => computeBuildHash,
|
|
212
222
|
computeHash: () => computeHash,
|
|
213
223
|
context: () => context,
|
|
224
|
+
currentTraceId: () => currentTraceId,
|
|
214
225
|
defineTool: () => defineTool,
|
|
215
226
|
experimentContextStorage: () => experimentContextStorage,
|
|
216
227
|
experiments: () => singleton_exports,
|
|
@@ -233,9 +244,11 @@ __export(src_exports, {
|
|
|
233
244
|
resolveUserId: () => resolveUserId,
|
|
234
245
|
runSuite: () => runSuite,
|
|
235
246
|
runWithExperimentContext: () => runWithExperimentContext,
|
|
247
|
+
scores: () => scores,
|
|
236
248
|
sessionObservationFromApi: () => sessionObservationFromApi,
|
|
237
249
|
sessions: () => sessions,
|
|
238
250
|
setExperimentContext: () => setExperimentContext,
|
|
251
|
+
success: () => success,
|
|
239
252
|
suite: () => suite,
|
|
240
253
|
tracing: () => tracing,
|
|
241
254
|
users: () => users
|
|
@@ -1472,7 +1485,7 @@ var SessionRecording = class _SessionRecording {
|
|
|
1472
1485
|
* to re-drive the recorded conversation.
|
|
1473
1486
|
*/
|
|
1474
1487
|
userMessages() {
|
|
1475
|
-
return this.turns().map((
|
|
1488
|
+
return this.turns().map((trace5) => trace5.userMessage());
|
|
1476
1489
|
}
|
|
1477
1490
|
/** Output of the last chronological turn (null when empty). */
|
|
1478
1491
|
finalOutput() {
|
|
@@ -1490,8 +1503,8 @@ var SessionRecording = class _SessionRecording {
|
|
|
1490
1503
|
/** Filter observations across all turns (chronological order). */
|
|
1491
1504
|
select(filter = {}) {
|
|
1492
1505
|
const selected = [];
|
|
1493
|
-
for (const
|
|
1494
|
-
for (const observation of
|
|
1506
|
+
for (const trace5 of this.turns()) {
|
|
1507
|
+
for (const observation of trace5.observations) {
|
|
1495
1508
|
if (filter.type && (observation.type ?? "").toUpperCase() !== filter.type.toUpperCase()) {
|
|
1496
1509
|
continue;
|
|
1497
1510
|
}
|
|
@@ -1511,15 +1524,15 @@ var SessionRecording = class _SessionRecording {
|
|
|
1511
1524
|
sessionId: this.sessionId,
|
|
1512
1525
|
projectId: this.projectId,
|
|
1513
1526
|
environment: this.environment,
|
|
1514
|
-
traces: this.turns().map((
|
|
1515
|
-
id:
|
|
1516
|
-
name:
|
|
1517
|
-
timestamp:
|
|
1518
|
-
input:
|
|
1519
|
-
output:
|
|
1520
|
-
metadata:
|
|
1521
|
-
tags:
|
|
1522
|
-
observations:
|
|
1527
|
+
traces: this.turns().map((trace5) => ({
|
|
1528
|
+
id: trace5.id,
|
|
1529
|
+
name: trace5.name,
|
|
1530
|
+
timestamp: trace5.timestamp,
|
|
1531
|
+
input: trace5.input,
|
|
1532
|
+
output: trace5.output,
|
|
1533
|
+
metadata: trace5.metadata,
|
|
1534
|
+
tags: trace5.tags,
|
|
1535
|
+
observations: trace5.observations.map((o) => ({ ...o }))
|
|
1523
1536
|
}))
|
|
1524
1537
|
};
|
|
1525
1538
|
}
|
|
@@ -2830,6 +2843,216 @@ var users = {
|
|
|
2830
2843
|
fromEnv: UsersClient.fromEnv
|
|
2831
2844
|
};
|
|
2832
2845
|
|
|
2846
|
+
// src/scores/index.ts
|
|
2847
|
+
var import_api = require("@opentelemetry/api");
|
|
2848
|
+
var SUCCESS_SIGNAL_KEY = "gateway.success.signal";
|
|
2849
|
+
var SUCCESS_METRIC_NAME_KEY = "gateway.success.metricName";
|
|
2850
|
+
var AGENT_ID_KEY = "gateway.agent.id";
|
|
2851
|
+
var ScoreError = class extends Error {
|
|
2852
|
+
constructor(message, options) {
|
|
2853
|
+
super(message);
|
|
2854
|
+
this.name = "ScoreError";
|
|
2855
|
+
this.cause = options?.cause;
|
|
2856
|
+
}
|
|
2857
|
+
};
|
|
2858
|
+
function currentTraceId() {
|
|
2859
|
+
try {
|
|
2860
|
+
const span = import_api.trace.getActiveSpan();
|
|
2861
|
+
if (!span) return void 0;
|
|
2862
|
+
const ctx = span.spanContext();
|
|
2863
|
+
if (!ctx || !(0, import_api.isSpanContextValid)(ctx)) return void 0;
|
|
2864
|
+
return ctx.traceId;
|
|
2865
|
+
} catch {
|
|
2866
|
+
return void 0;
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
2869
|
+
var ScoresClient = class _ScoresClient {
|
|
2870
|
+
host;
|
|
2871
|
+
publicKey;
|
|
2872
|
+
secretKey;
|
|
2873
|
+
timeoutSeconds;
|
|
2874
|
+
debug;
|
|
2875
|
+
constructor(config) {
|
|
2876
|
+
this.host = config.host.replace(/\/$/, "");
|
|
2877
|
+
this.publicKey = config.publicKey;
|
|
2878
|
+
this.secretKey = config.secretKey;
|
|
2879
|
+
this.timeoutSeconds = config.timeoutSeconds ?? 15;
|
|
2880
|
+
this.debug = config.debug ?? false;
|
|
2881
|
+
}
|
|
2882
|
+
/** Create a client from GATEWAY_HOST / GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY. */
|
|
2883
|
+
static fromEnv(config = {}) {
|
|
2884
|
+
const host = config.host ?? process.env.GATEWAY_HOST;
|
|
2885
|
+
const publicKey = config.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
|
|
2886
|
+
const secretKey = config.secretKey ?? process.env.GATEWAY_SECRET_KEY;
|
|
2887
|
+
const missing = [
|
|
2888
|
+
["host", host],
|
|
2889
|
+
["publicKey", publicKey],
|
|
2890
|
+
["secretKey", secretKey]
|
|
2891
|
+
].filter(([, value]) => !value).map(([name]) => name);
|
|
2892
|
+
if (missing.length > 0) {
|
|
2893
|
+
throw new ScoreError(
|
|
2894
|
+
`Missing Gateway scores configuration: ${missing.join(", ")}`
|
|
2895
|
+
);
|
|
2896
|
+
}
|
|
2897
|
+
return new _ScoresClient({
|
|
2898
|
+
...config,
|
|
2899
|
+
host,
|
|
2900
|
+
publicKey,
|
|
2901
|
+
secretKey
|
|
2902
|
+
});
|
|
2903
|
+
}
|
|
2904
|
+
/**
|
|
2905
|
+
* Post a score, throwing {@link ScoreError} on any failure. Builds the exact
|
|
2906
|
+
* `POST /api/public/scores` body the Python SDK sends: exactly one of
|
|
2907
|
+
* `sessionId` / `traceId` (+ optional `observationId`) / `datasetRunId`.
|
|
2908
|
+
* When no target is provided, the active OTel trace id is used; if none is
|
|
2909
|
+
* available the call is a no-op (returns without posting), matching Python.
|
|
2910
|
+
*/
|
|
2911
|
+
async postScore(name, value, options = {}) {
|
|
2912
|
+
const body = this.buildScoreBody(name, value, options);
|
|
2913
|
+
if (!body) {
|
|
2914
|
+
this.log(
|
|
2915
|
+
"score '%s' dropped: no sessionId, traceId or datasetRunId available",
|
|
2916
|
+
name
|
|
2917
|
+
);
|
|
2918
|
+
return;
|
|
2919
|
+
}
|
|
2920
|
+
await this.request("POST", "/api/public/scores", body);
|
|
2921
|
+
}
|
|
2922
|
+
/**
|
|
2923
|
+
* Build the score request body, or null when no target can be resolved.
|
|
2924
|
+
* Split out so it is unit-testable and shared by score()/success().
|
|
2925
|
+
*/
|
|
2926
|
+
buildScoreBody(name, value, options = {}) {
|
|
2927
|
+
const traceId = options.traceId ?? currentTraceId();
|
|
2928
|
+
const target = {};
|
|
2929
|
+
if (options.sessionId) {
|
|
2930
|
+
target.sessionId = options.sessionId;
|
|
2931
|
+
} else if (options.datasetRunId) {
|
|
2932
|
+
target.datasetRunId = options.datasetRunId;
|
|
2933
|
+
} else if (traceId) {
|
|
2934
|
+
target.traceId = traceId;
|
|
2935
|
+
if (options.observationId) target.observationId = options.observationId;
|
|
2936
|
+
} else {
|
|
2937
|
+
return null;
|
|
2938
|
+
}
|
|
2939
|
+
const metadata = {
|
|
2940
|
+
source: "gatewaysdk",
|
|
2941
|
+
...options.metadata ?? {}
|
|
2942
|
+
};
|
|
2943
|
+
return {
|
|
2944
|
+
name,
|
|
2945
|
+
value,
|
|
2946
|
+
dataType: options.dataType ?? "NUMERIC",
|
|
2947
|
+
environment: options.environment ?? "default",
|
|
2948
|
+
...options.comment ? { comment: options.comment } : {},
|
|
2949
|
+
metadata,
|
|
2950
|
+
...target
|
|
2951
|
+
};
|
|
2952
|
+
}
|
|
2953
|
+
/**
|
|
2954
|
+
* Post a score, FAIL-SOFT: warns and resolves on any failure so a broken
|
|
2955
|
+
* transport never throws into (or rejects out of) the caller.
|
|
2956
|
+
*/
|
|
2957
|
+
async score(name, value, options = {}) {
|
|
2958
|
+
try {
|
|
2959
|
+
await this.postScore(name, value, options);
|
|
2960
|
+
} catch (error) {
|
|
2961
|
+
this.warn(`score '${name}' failed`, error);
|
|
2962
|
+
}
|
|
2963
|
+
}
|
|
2964
|
+
/**
|
|
2965
|
+
* Emit a SUCCESS SIGNAL that feeds a Gateway Success Metric — a score tagged
|
|
2966
|
+
* with `gateway.success.signal` + `gateway.success.metricName` (and the agent
|
|
2967
|
+
* identity when `agentId` is set). A boolean `value` records a 1/0 BOOLEAN
|
|
2968
|
+
* score (pass/fail); a number records a NUMERIC score. FAIL-SOFT.
|
|
2969
|
+
*
|
|
2970
|
+
* The metadata shape matches the Python SDK's `Run.success()` exactly, so the
|
|
2971
|
+
* platform's Success Metrics aggregation counts signals from either SDK
|
|
2972
|
+
* without change.
|
|
2973
|
+
*
|
|
2974
|
+
* @example
|
|
2975
|
+
* ```ts
|
|
2976
|
+
* await scores.success("resolved_ticket", true, { agentId: "support-triage", sessionId });
|
|
2977
|
+
* await scores.success("handle_time_s", 42, { agentId: "support-triage", sessionId });
|
|
2978
|
+
* ```
|
|
2979
|
+
*/
|
|
2980
|
+
async success(name, value = true, options = {}) {
|
|
2981
|
+
const { numericValue, inferredType } = coerceSignalValue(value);
|
|
2982
|
+
const { agentId, metricName, metadata, dataType, ...rest } = options;
|
|
2983
|
+
const signalMetadata = {
|
|
2984
|
+
...metadata ?? {},
|
|
2985
|
+
[SUCCESS_SIGNAL_KEY]: true,
|
|
2986
|
+
[SUCCESS_METRIC_NAME_KEY]: metricName ?? name,
|
|
2987
|
+
...agentId ? { [AGENT_ID_KEY]: agentId } : {}
|
|
2988
|
+
};
|
|
2989
|
+
await this.score(name, numericValue, {
|
|
2990
|
+
...rest,
|
|
2991
|
+
dataType: dataType ?? inferredType,
|
|
2992
|
+
metadata: signalMetadata
|
|
2993
|
+
});
|
|
2994
|
+
}
|
|
2995
|
+
async request(method, path, payload) {
|
|
2996
|
+
let response;
|
|
2997
|
+
try {
|
|
2998
|
+
response = await fetch(`${this.host}${path}`, {
|
|
2999
|
+
method,
|
|
3000
|
+
headers: {
|
|
3001
|
+
Authorization: this.authHeader(),
|
|
3002
|
+
"Content-Type": "application/json"
|
|
3003
|
+
},
|
|
3004
|
+
body: JSON.stringify(payload),
|
|
3005
|
+
signal: AbortSignal.timeout(this.timeoutSeconds * 1e3)
|
|
3006
|
+
});
|
|
3007
|
+
} catch (error) {
|
|
3008
|
+
throw new ScoreError(
|
|
3009
|
+
`${method} ${path} failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
3010
|
+
{ cause: error }
|
|
3011
|
+
);
|
|
3012
|
+
}
|
|
3013
|
+
const text = await response.text();
|
|
3014
|
+
if (!response.ok) {
|
|
3015
|
+
throw new ScoreError(
|
|
3016
|
+
`${method} ${path} failed (${response.status}): ${text.slice(0, 500)}`
|
|
3017
|
+
);
|
|
3018
|
+
}
|
|
3019
|
+
this.log("%s %s -> %s", method, path, response.status);
|
|
3020
|
+
}
|
|
3021
|
+
authHeader() {
|
|
3022
|
+
return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
|
|
3023
|
+
"base64"
|
|
3024
|
+
)}`;
|
|
3025
|
+
}
|
|
3026
|
+
log(message, ...args) {
|
|
3027
|
+
if (this.debug) {
|
|
3028
|
+
console.log(`[gateway.scores] ${message}`, ...args);
|
|
3029
|
+
}
|
|
3030
|
+
}
|
|
3031
|
+
warn(message, ...args) {
|
|
3032
|
+
console.warn(`[gateway.scores] ${message}`, ...args);
|
|
3033
|
+
}
|
|
3034
|
+
};
|
|
3035
|
+
function coerceSignalValue(value) {
|
|
3036
|
+
if (typeof value === "boolean") {
|
|
3037
|
+
return { numericValue: value ? 1 : 0, inferredType: "BOOLEAN" };
|
|
3038
|
+
}
|
|
3039
|
+
return { numericValue: value, inferredType: "NUMERIC" };
|
|
3040
|
+
}
|
|
3041
|
+
async function success(name, value = true, options = {}) {
|
|
3042
|
+
let client2;
|
|
3043
|
+
try {
|
|
3044
|
+
client2 = ScoresClient.fromEnv();
|
|
3045
|
+
} catch (error) {
|
|
3046
|
+
console.warn(`[gateway.scores] success '${name}' skipped`, error);
|
|
3047
|
+
return;
|
|
3048
|
+
}
|
|
3049
|
+
await client2.success(name, value, options);
|
|
3050
|
+
}
|
|
3051
|
+
var scores = {
|
|
3052
|
+
fromEnv: ScoresClient.fromEnv,
|
|
3053
|
+
success
|
|
3054
|
+
};
|
|
3055
|
+
|
|
2833
3056
|
// src/build/index.ts
|
|
2834
3057
|
var import_node_crypto = require("crypto");
|
|
2835
3058
|
function canonicalJson(value) {
|
|
@@ -3113,7 +3336,7 @@ async function registerBuild(params) {
|
|
|
3113
3336
|
|
|
3114
3337
|
// src/tracing/identity.ts
|
|
3115
3338
|
var import_async_hooks2 = require("async_hooks");
|
|
3116
|
-
var
|
|
3339
|
+
var import_api2 = require("@opentelemetry/api");
|
|
3117
3340
|
var USER_ID_ATTRIBUTE = "gateway.user.id";
|
|
3118
3341
|
var currentUserStorage = new import_async_hooks2.AsyncLocalStorage();
|
|
3119
3342
|
function resolveUserId() {
|
|
@@ -3127,8 +3350,8 @@ function identify(userId, options = {}) {
|
|
|
3127
3350
|
if (!cleaned) return;
|
|
3128
3351
|
currentUserStorage.enterWith(cleaned);
|
|
3129
3352
|
try {
|
|
3130
|
-
const span =
|
|
3131
|
-
if (span && (0,
|
|
3353
|
+
const span = import_api2.trace.getActiveSpan();
|
|
3354
|
+
if (span && (0, import_api2.isSpanContextValid)(span.spanContext())) {
|
|
3132
3355
|
span.setAttribute(USER_ID_ATTRIBUTE, cleaned);
|
|
3133
3356
|
}
|
|
3134
3357
|
} catch (error) {
|
|
@@ -3406,12 +3629,19 @@ function initIsolated(config = {}) {
|
|
|
3406
3629
|
const provider = built.provider;
|
|
3407
3630
|
scheduleBuildRegistration(config, built);
|
|
3408
3631
|
const tracer = provider.getTracer("@withgateway/sdk");
|
|
3632
|
+
const scores2 = new ScoresClient({
|
|
3633
|
+
host: built.connection.host,
|
|
3634
|
+
publicKey: built.connection.publicKey,
|
|
3635
|
+
secretKey: built.connection.secretKey,
|
|
3636
|
+
debug: config.debug ?? false
|
|
3637
|
+
});
|
|
3409
3638
|
debugLog2(
|
|
3410
3639
|
"Isolated tracing provider created (service=%s)",
|
|
3411
3640
|
config.serviceName ?? "gatewaysdk-tracing"
|
|
3412
3641
|
);
|
|
3413
3642
|
return {
|
|
3414
3643
|
tracer,
|
|
3644
|
+
scores: scores2,
|
|
3415
3645
|
async flush() {
|
|
3416
3646
|
try {
|
|
3417
3647
|
await provider.forceFlush();
|
|
@@ -3465,12 +3695,12 @@ function isGatewayConfigured() {
|
|
|
3465
3695
|
}
|
|
3466
3696
|
|
|
3467
3697
|
// src/tracing/observe.ts
|
|
3468
|
-
var
|
|
3698
|
+
var import_api3 = require("@opentelemetry/api");
|
|
3469
3699
|
function asAttr(value) {
|
|
3470
3700
|
return typeof value === "string" ? value : JSON.stringify(value);
|
|
3471
3701
|
}
|
|
3472
3702
|
async function observe(name, fn, options = {}) {
|
|
3473
|
-
const tracer = options.tracer ??
|
|
3703
|
+
const tracer = options.tracer ?? import_api3.trace.getTracer("@withgateway/sdk");
|
|
3474
3704
|
const span = tracer.startSpan(name, {
|
|
3475
3705
|
attributes: {
|
|
3476
3706
|
"openinference.span.kind": options.kind ?? "AGENT",
|
|
@@ -3479,18 +3709,18 @@ async function observe(name, fn, options = {}) {
|
|
|
3479
3709
|
}
|
|
3480
3710
|
});
|
|
3481
3711
|
try {
|
|
3482
|
-
const result = await
|
|
3483
|
-
|
|
3712
|
+
const result = await import_api3.context.with(
|
|
3713
|
+
import_api3.trace.setSpan(import_api3.context.active(), span),
|
|
3484
3714
|
fn
|
|
3485
3715
|
);
|
|
3486
3716
|
if (options.captureOutput !== false && result !== void 0) {
|
|
3487
3717
|
span.setAttribute("output.value", asAttr(result));
|
|
3488
3718
|
}
|
|
3489
|
-
span.setStatus({ code:
|
|
3719
|
+
span.setStatus({ code: import_api3.SpanStatusCode.OK });
|
|
3490
3720
|
return result;
|
|
3491
3721
|
} catch (error) {
|
|
3492
3722
|
span.setStatus({
|
|
3493
|
-
code:
|
|
3723
|
+
code: import_api3.SpanStatusCode.ERROR,
|
|
3494
3724
|
message: error instanceof Error ? error.message : String(error)
|
|
3495
3725
|
});
|
|
3496
3726
|
throw error;
|
|
@@ -3537,7 +3767,7 @@ function observeAnthropic(client2) {
|
|
|
3537
3767
|
}
|
|
3538
3768
|
|
|
3539
3769
|
// src/tracing/claude.ts
|
|
3540
|
-
var
|
|
3770
|
+
var import_api4 = require("@opentelemetry/api");
|
|
3541
3771
|
function cleanToolName(name) {
|
|
3542
3772
|
return name.replace(/^mcp__[^_]+__/, "");
|
|
3543
3773
|
}
|
|
@@ -3551,7 +3781,7 @@ function toolResultText(content) {
|
|
|
3551
3781
|
}
|
|
3552
3782
|
var TURN_INPUT_MAX = 4e3;
|
|
3553
3783
|
async function* observeClaude(stream, options) {
|
|
3554
|
-
const tracer = options.tracer ??
|
|
3784
|
+
const tracer = options.tracer ?? import_api4.trace.getTracer("@withgateway/sdk");
|
|
3555
3785
|
const model = options.model ?? "claude";
|
|
3556
3786
|
const openTools = /* @__PURE__ */ new Map();
|
|
3557
3787
|
let root = null;
|
|
@@ -3564,7 +3794,7 @@ async function* observeClaude(stream, options) {
|
|
|
3564
3794
|
...options.attributes ?? {}
|
|
3565
3795
|
}
|
|
3566
3796
|
});
|
|
3567
|
-
rootCtx =
|
|
3797
|
+
rootCtx = import_api4.trace.setSpan(import_api4.context.active(), root);
|
|
3568
3798
|
}
|
|
3569
3799
|
let turnRoot = null;
|
|
3570
3800
|
let turnCtx = null;
|
|
@@ -3575,7 +3805,7 @@ async function* observeClaude(stream, options) {
|
|
|
3575
3805
|
const endTurn = () => {
|
|
3576
3806
|
if (!turnRoot) return;
|
|
3577
3807
|
if (turnText) turnRoot.setAttribute("output.value", turnText);
|
|
3578
|
-
turnRoot.setStatus({ code:
|
|
3808
|
+
turnRoot.setStatus({ code: import_api4.SpanStatusCode.OK });
|
|
3579
3809
|
turnRoot.end();
|
|
3580
3810
|
turnRoot = null;
|
|
3581
3811
|
turnCtx = null;
|
|
@@ -3597,7 +3827,7 @@ async function* observeClaude(stream, options) {
|
|
|
3597
3827
|
...options.attributes ?? {}
|
|
3598
3828
|
}
|
|
3599
3829
|
});
|
|
3600
|
-
turnCtx =
|
|
3830
|
+
turnCtx = import_api4.trace.setSpan(import_api4.context.active(), turnRoot);
|
|
3601
3831
|
pendingTurnInput = "";
|
|
3602
3832
|
}
|
|
3603
3833
|
const childCtx = options.perTurn ? turnCtx : rootCtx;
|
|
@@ -3659,7 +3889,7 @@ async function* observeClaude(stream, options) {
|
|
|
3659
3889
|
const span = openTools.get(block.tool_use_id);
|
|
3660
3890
|
if (span) {
|
|
3661
3891
|
span.setAttribute("output.value", resultText);
|
|
3662
|
-
span.setStatus({ code:
|
|
3892
|
+
span.setStatus({ code: import_api4.SpanStatusCode.OK });
|
|
3663
3893
|
span.end();
|
|
3664
3894
|
openTools.delete(block.tool_use_id);
|
|
3665
3895
|
}
|
|
@@ -3679,7 +3909,7 @@ async function* observeClaude(stream, options) {
|
|
|
3679
3909
|
endTurn();
|
|
3680
3910
|
} else if (root) {
|
|
3681
3911
|
root.setAttribute("output.value", finalText);
|
|
3682
|
-
root.setStatus({ code:
|
|
3912
|
+
root.setStatus({ code: import_api4.SpanStatusCode.OK });
|
|
3683
3913
|
root.end();
|
|
3684
3914
|
}
|
|
3685
3915
|
}
|
|
@@ -3704,6 +3934,7 @@ var tracing = {
|
|
|
3704
3934
|
};
|
|
3705
3935
|
// Annotate the CommonJS export names for ESM import in node:
|
|
3706
3936
|
0 && (module.exports = {
|
|
3937
|
+
AGENT_ID_KEY,
|
|
3707
3938
|
DEFAULT_TIMEOUT_SECONDS,
|
|
3708
3939
|
DriftError,
|
|
3709
3940
|
ENV_GATEWAY_HOST,
|
|
@@ -3733,6 +3964,10 @@ var tracing = {
|
|
|
3733
3964
|
ReplayReport,
|
|
3734
3965
|
ReplaySuite,
|
|
3735
3966
|
ReplayTask,
|
|
3967
|
+
SUCCESS_METRIC_NAME_KEY,
|
|
3968
|
+
SUCCESS_SIGNAL_KEY,
|
|
3969
|
+
ScoreError,
|
|
3970
|
+
ScoresClient,
|
|
3736
3971
|
SessionLoadError,
|
|
3737
3972
|
SessionRecording,
|
|
3738
3973
|
SessionTrace,
|
|
@@ -3750,6 +3985,7 @@ var tracing = {
|
|
|
3750
3985
|
computeBuildHash,
|
|
3751
3986
|
computeHash,
|
|
3752
3987
|
context,
|
|
3988
|
+
currentTraceId,
|
|
3753
3989
|
defineTool,
|
|
3754
3990
|
experimentContextStorage,
|
|
3755
3991
|
experiments,
|
|
@@ -3772,9 +4008,11 @@ var tracing = {
|
|
|
3772
4008
|
resolveUserId,
|
|
3773
4009
|
runSuite,
|
|
3774
4010
|
runWithExperimentContext,
|
|
4011
|
+
scores,
|
|
3775
4012
|
sessionObservationFromApi,
|
|
3776
4013
|
sessions,
|
|
3777
4014
|
setExperimentContext,
|
|
4015
|
+
success,
|
|
3778
4016
|
suite,
|
|
3779
4017
|
tracing,
|
|
3780
4018
|
users
|