@withgateway/sdk 0.1.11 → 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 +44 -5
- package/dist/index.d.ts +44 -5
- package/dist/index.js +364 -37
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +348 -31
- 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 +309 -18
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/index.mjs +303 -12
- package/dist/tracing/index.mjs.map +1 -1
- package/package.json +6 -1
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,11 +221,13 @@ __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,
|
|
217
228
|
gateway: () => gateway,
|
|
218
229
|
getExperimentContext: () => getExperimentContext,
|
|
230
|
+
getRegisteredArtifacts: () => getRegisteredArtifacts,
|
|
219
231
|
getRegisteredPrompts: () => getRegisteredPrompts,
|
|
220
232
|
getRegisteredSkills: () => getRegisteredSkills,
|
|
221
233
|
getRegisteredTools: () => getRegisteredTools,
|
|
@@ -223,6 +235,7 @@ __export(src_exports, {
|
|
|
223
235
|
loadSession: () => loadSession,
|
|
224
236
|
memory: () => memory,
|
|
225
237
|
personas: () => personas,
|
|
238
|
+
registerArtifact: () => registerArtifact,
|
|
226
239
|
registerPrompt: () => registerPrompt,
|
|
227
240
|
registerSkill: () => registerSkill,
|
|
228
241
|
renderAgentSystem: () => renderAgentSystem,
|
|
@@ -231,9 +244,11 @@ __export(src_exports, {
|
|
|
231
244
|
resolveUserId: () => resolveUserId,
|
|
232
245
|
runSuite: () => runSuite,
|
|
233
246
|
runWithExperimentContext: () => runWithExperimentContext,
|
|
247
|
+
scores: () => scores,
|
|
234
248
|
sessionObservationFromApi: () => sessionObservationFromApi,
|
|
235
249
|
sessions: () => sessions,
|
|
236
250
|
setExperimentContext: () => setExperimentContext,
|
|
251
|
+
success: () => success,
|
|
237
252
|
suite: () => suite,
|
|
238
253
|
tracing: () => tracing,
|
|
239
254
|
users: () => users
|
|
@@ -1470,7 +1485,7 @@ var SessionRecording = class _SessionRecording {
|
|
|
1470
1485
|
* to re-drive the recorded conversation.
|
|
1471
1486
|
*/
|
|
1472
1487
|
userMessages() {
|
|
1473
|
-
return this.turns().map((
|
|
1488
|
+
return this.turns().map((trace5) => trace5.userMessage());
|
|
1474
1489
|
}
|
|
1475
1490
|
/** Output of the last chronological turn (null when empty). */
|
|
1476
1491
|
finalOutput() {
|
|
@@ -1488,8 +1503,8 @@ var SessionRecording = class _SessionRecording {
|
|
|
1488
1503
|
/** Filter observations across all turns (chronological order). */
|
|
1489
1504
|
select(filter = {}) {
|
|
1490
1505
|
const selected = [];
|
|
1491
|
-
for (const
|
|
1492
|
-
for (const observation of
|
|
1506
|
+
for (const trace5 of this.turns()) {
|
|
1507
|
+
for (const observation of trace5.observations) {
|
|
1493
1508
|
if (filter.type && (observation.type ?? "").toUpperCase() !== filter.type.toUpperCase()) {
|
|
1494
1509
|
continue;
|
|
1495
1510
|
}
|
|
@@ -1509,15 +1524,15 @@ var SessionRecording = class _SessionRecording {
|
|
|
1509
1524
|
sessionId: this.sessionId,
|
|
1510
1525
|
projectId: this.projectId,
|
|
1511
1526
|
environment: this.environment,
|
|
1512
|
-
traces: this.turns().map((
|
|
1513
|
-
id:
|
|
1514
|
-
name:
|
|
1515
|
-
timestamp:
|
|
1516
|
-
input:
|
|
1517
|
-
output:
|
|
1518
|
-
metadata:
|
|
1519
|
-
tags:
|
|
1520
|
-
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 }))
|
|
1521
1536
|
}))
|
|
1522
1537
|
};
|
|
1523
1538
|
}
|
|
@@ -2828,6 +2843,216 @@ var users = {
|
|
|
2828
2843
|
fromEnv: UsersClient.fromEnv
|
|
2829
2844
|
};
|
|
2830
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
|
+
|
|
2831
3056
|
// src/build/index.ts
|
|
2832
3057
|
var import_node_crypto = require("crypto");
|
|
2833
3058
|
function canonicalJson(value) {
|
|
@@ -2846,6 +3071,7 @@ function sha256(text) {
|
|
|
2846
3071
|
var registeredTools = /* @__PURE__ */ new Map();
|
|
2847
3072
|
var registeredPrompts = /* @__PURE__ */ new Map();
|
|
2848
3073
|
var registeredSkills = /* @__PURE__ */ new Map();
|
|
3074
|
+
var registeredArtifacts = /* @__PURE__ */ new Map();
|
|
2849
3075
|
function captureSource() {
|
|
2850
3076
|
const stack = new Error().stack?.split("\n") ?? [];
|
|
2851
3077
|
for (const line of stack.slice(1)) {
|
|
@@ -2879,9 +3105,24 @@ function defineTool(tool) {
|
|
|
2879
3105
|
notifyRegistryChanged();
|
|
2880
3106
|
return tool;
|
|
2881
3107
|
}
|
|
2882
|
-
function
|
|
2883
|
-
|
|
3108
|
+
function registerArtifact(kind, name, options = {}) {
|
|
3109
|
+
const source = captureSource();
|
|
3110
|
+
const identity = {
|
|
3111
|
+
kind,
|
|
3112
|
+
name,
|
|
3113
|
+
...options.version !== void 0 ? { version: options.version } : {},
|
|
3114
|
+
...options.content !== void 0 ? { contentHash: sha256(options.content) } : {},
|
|
3115
|
+
...options.ref !== void 0 ? { ref: options.ref } : {},
|
|
3116
|
+
...source ? { source } : {},
|
|
3117
|
+
...options.metadata ? { metadata: options.metadata } : {}
|
|
3118
|
+
};
|
|
3119
|
+
registeredArtifacts.set(`${kind}:${name}`, identity);
|
|
2884
3120
|
notifyRegistryChanged();
|
|
3121
|
+
return identity;
|
|
3122
|
+
}
|
|
3123
|
+
function registerPrompt(name, content, options = {}) {
|
|
3124
|
+
registeredPrompts.set(name, sha256(content));
|
|
3125
|
+
registerArtifact("prompt", name, { ...options, content });
|
|
2885
3126
|
return content;
|
|
2886
3127
|
}
|
|
2887
3128
|
function getRegisteredTools() {
|
|
@@ -2889,9 +3130,14 @@ function getRegisteredTools() {
|
|
|
2889
3130
|
(a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0
|
|
2890
3131
|
);
|
|
2891
3132
|
}
|
|
2892
|
-
function registerSkill(name, version) {
|
|
3133
|
+
function registerSkill(name, version, options = {}) {
|
|
2893
3134
|
registeredSkills.set(name, version);
|
|
2894
|
-
|
|
3135
|
+
registerArtifact("skill", name, { version, ...options });
|
|
3136
|
+
}
|
|
3137
|
+
function getRegisteredArtifacts() {
|
|
3138
|
+
return [...registeredArtifacts.values()].sort(
|
|
3139
|
+
(a, b) => `${a.kind}:${a.name}` < `${b.kind}:${b.name}` ? -1 : 1
|
|
3140
|
+
);
|
|
2895
3141
|
}
|
|
2896
3142
|
function getRegisteredSkills() {
|
|
2897
3143
|
return Object.fromEntries(
|
|
@@ -2911,6 +3157,7 @@ function resetBuildRegistry() {
|
|
|
2911
3157
|
registeredTools.clear();
|
|
2912
3158
|
registeredPrompts.clear();
|
|
2913
3159
|
registeredSkills.clear();
|
|
3160
|
+
registeredArtifacts.clear();
|
|
2914
3161
|
activeBuilds.length = 0;
|
|
2915
3162
|
if (reRegisterTimer) clearTimeout(reRegisterTimer);
|
|
2916
3163
|
reRegisterTimer = null;
|
|
@@ -2923,11 +3170,62 @@ function sdkVersion() {
|
|
|
2923
3170
|
return "@withgateway/sdk";
|
|
2924
3171
|
}
|
|
2925
3172
|
}
|
|
3173
|
+
function paasGitContext() {
|
|
3174
|
+
const env = process.env;
|
|
3175
|
+
if (env.VERCEL_GIT_COMMIT_SHA) {
|
|
3176
|
+
const isGithub = (env.VERCEL_GIT_PROVIDER ?? "github") === "github";
|
|
3177
|
+
return {
|
|
3178
|
+
provider: "vercel",
|
|
3179
|
+
sha: env.VERCEL_GIT_COMMIT_SHA,
|
|
3180
|
+
repo: isGithub && env.VERCEL_GIT_REPO_OWNER && env.VERCEL_GIT_REPO_SLUG ? `${env.VERCEL_GIT_REPO_OWNER}/${env.VERCEL_GIT_REPO_SLUG}` : void 0,
|
|
3181
|
+
branch: env.VERCEL_GIT_COMMIT_REF,
|
|
3182
|
+
login: env.VERCEL_GIT_COMMIT_AUTHOR_LOGIN
|
|
3183
|
+
};
|
|
3184
|
+
}
|
|
3185
|
+
if (env.RENDER_GIT_COMMIT) {
|
|
3186
|
+
return {
|
|
3187
|
+
provider: "render",
|
|
3188
|
+
sha: env.RENDER_GIT_COMMIT,
|
|
3189
|
+
repo: env.RENDER_GIT_REPO_SLUG,
|
|
3190
|
+
// already "owner/repo"
|
|
3191
|
+
branch: env.RENDER_GIT_BRANCH
|
|
3192
|
+
};
|
|
3193
|
+
}
|
|
3194
|
+
if (env.RAILWAY_GIT_COMMIT_SHA) {
|
|
3195
|
+
return {
|
|
3196
|
+
provider: "railway",
|
|
3197
|
+
sha: env.RAILWAY_GIT_COMMIT_SHA,
|
|
3198
|
+
repo: env.RAILWAY_GIT_REPO_OWNER && env.RAILWAY_GIT_REPO_NAME ? `${env.RAILWAY_GIT_REPO_OWNER}/${env.RAILWAY_GIT_REPO_NAME}` : void 0,
|
|
3199
|
+
branch: env.RAILWAY_GIT_BRANCH,
|
|
3200
|
+
login: env.RAILWAY_GIT_AUTHOR
|
|
3201
|
+
};
|
|
3202
|
+
}
|
|
3203
|
+
if (env.NETLIFY && env.COMMIT_REF) {
|
|
3204
|
+
const repoMatch = /github\.com[/:]([^/]+\/[^/.]+)/.exec(
|
|
3205
|
+
env.REPOSITORY_URL ?? ""
|
|
3206
|
+
);
|
|
3207
|
+
return {
|
|
3208
|
+
provider: "netlify",
|
|
3209
|
+
sha: env.COMMIT_REF,
|
|
3210
|
+
repo: repoMatch?.[1],
|
|
3211
|
+
branch: env.BRANCH
|
|
3212
|
+
};
|
|
3213
|
+
}
|
|
3214
|
+
const bareSha = env.HEROKU_SLUG_COMMIT ?? env.SOURCE_VERSION ?? env.GIT_COMMIT ?? env.COMMIT_SHA;
|
|
3215
|
+
if (bareSha) {
|
|
3216
|
+
return {
|
|
3217
|
+
provider: env.HEROKU_SLUG_COMMIT || env.SOURCE_VERSION ? "heroku" : "generic",
|
|
3218
|
+
sha: bareSha
|
|
3219
|
+
};
|
|
3220
|
+
}
|
|
3221
|
+
return null;
|
|
3222
|
+
}
|
|
2926
3223
|
function codeRef() {
|
|
3224
|
+
const paas = paasGitContext();
|
|
2927
3225
|
const sha = process.env.GATEWAY_COMMIT ?? process.env.GITHUB_SHA ?? process.env.CI_COMMIT_SHA ?? // GitLab
|
|
2928
|
-
process.env.CIRCLE_SHA1;
|
|
3226
|
+
process.env.CIRCLE_SHA1 ?? paas?.sha;
|
|
2929
3227
|
if (!sha) return void 0;
|
|
2930
|
-
const repo = process.env.GITHUB_REPOSITORY ?? process.env.CI_PROJECT_PATH;
|
|
3228
|
+
const repo = process.env.GITHUB_REPOSITORY ?? process.env.CI_PROJECT_PATH ?? paas?.repo;
|
|
2931
3229
|
return repo ? `git:${repo}@${sha}` : `git:${sha}`;
|
|
2932
3230
|
}
|
|
2933
3231
|
function assembleManifest(agentId) {
|
|
@@ -2937,13 +3235,17 @@ function assembleManifest(agentId) {
|
|
|
2937
3235
|
sdk: sdkVersion(),
|
|
2938
3236
|
tools: getRegisteredTools(),
|
|
2939
3237
|
prompts: getRegisteredPrompts(),
|
|
2940
|
-
skills: getRegisteredSkills()
|
|
3238
|
+
skills: getRegisteredSkills(),
|
|
3239
|
+
artifacts: getRegisteredArtifacts()
|
|
2941
3240
|
};
|
|
2942
3241
|
}
|
|
2943
3242
|
function computeBuildHash(manifest) {
|
|
2944
3243
|
const identity = {
|
|
2945
3244
|
...manifest,
|
|
2946
|
-
tools: manifest.tools.map(({ source: _source, ...tool }) => tool)
|
|
3245
|
+
tools: manifest.tools.map(({ source: _source, ...tool }) => tool),
|
|
3246
|
+
artifacts: manifest.artifacts.map(
|
|
3247
|
+
({ source: _source, ...artifact }) => artifact
|
|
3248
|
+
)
|
|
2947
3249
|
};
|
|
2948
3250
|
return sha256(canonicalJson(identity));
|
|
2949
3251
|
}
|
|
@@ -2956,6 +3258,14 @@ function attributionFromEnv() {
|
|
|
2956
3258
|
if (process.env.GITHUB_REPOSITORY) attribution.repo = process.env.GITHUB_REPOSITORY;
|
|
2957
3259
|
const branch = process.env.GITHUB_HEAD_REF ?? process.env.GITHUB_REF_NAME;
|
|
2958
3260
|
if (branch) attribution.branch = branch;
|
|
3261
|
+
const paas = paasGitContext();
|
|
3262
|
+
if (paas) {
|
|
3263
|
+
if (attribution.source === "runtime") attribution.source = `paas:${paas.provider}`;
|
|
3264
|
+
if (!attribution.commitSha && paas.sha) attribution.commitSha = paas.sha;
|
|
3265
|
+
if (!attribution.repo && paas.repo) attribution.repo = paas.repo;
|
|
3266
|
+
if (!attribution.branch && paas.branch) attribution.branch = paas.branch;
|
|
3267
|
+
if (!attribution.githubLogin && paas.login) attribution.githubLogin = paas.login;
|
|
3268
|
+
}
|
|
2959
3269
|
const ref = process.env.GITHUB_REF ?? "";
|
|
2960
3270
|
const prMatch = /refs\/pull\/(\d+)\//.exec(ref);
|
|
2961
3271
|
if (prMatch) attribution.prNumber = Number(prMatch[1]);
|
|
@@ -3026,7 +3336,7 @@ async function registerBuild(params) {
|
|
|
3026
3336
|
|
|
3027
3337
|
// src/tracing/identity.ts
|
|
3028
3338
|
var import_async_hooks2 = require("async_hooks");
|
|
3029
|
-
var
|
|
3339
|
+
var import_api2 = require("@opentelemetry/api");
|
|
3030
3340
|
var USER_ID_ATTRIBUTE = "gateway.user.id";
|
|
3031
3341
|
var currentUserStorage = new import_async_hooks2.AsyncLocalStorage();
|
|
3032
3342
|
function resolveUserId() {
|
|
@@ -3040,8 +3350,8 @@ function identify(userId, options = {}) {
|
|
|
3040
3350
|
if (!cleaned) return;
|
|
3041
3351
|
currentUserStorage.enterWith(cleaned);
|
|
3042
3352
|
try {
|
|
3043
|
-
const span =
|
|
3044
|
-
if (span && (0,
|
|
3353
|
+
const span = import_api2.trace.getActiveSpan();
|
|
3354
|
+
if (span && (0, import_api2.isSpanContextValid)(span.spanContext())) {
|
|
3045
3355
|
span.setAttribute(USER_ID_ATTRIBUTE, cleaned);
|
|
3046
3356
|
}
|
|
3047
3357
|
} catch (error) {
|
|
@@ -3319,12 +3629,19 @@ function initIsolated(config = {}) {
|
|
|
3319
3629
|
const provider = built.provider;
|
|
3320
3630
|
scheduleBuildRegistration(config, built);
|
|
3321
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
|
+
});
|
|
3322
3638
|
debugLog2(
|
|
3323
3639
|
"Isolated tracing provider created (service=%s)",
|
|
3324
3640
|
config.serviceName ?? "gatewaysdk-tracing"
|
|
3325
3641
|
);
|
|
3326
3642
|
return {
|
|
3327
3643
|
tracer,
|
|
3644
|
+
scores: scores2,
|
|
3328
3645
|
async flush() {
|
|
3329
3646
|
try {
|
|
3330
3647
|
await provider.forceFlush();
|
|
@@ -3378,12 +3695,12 @@ function isGatewayConfigured() {
|
|
|
3378
3695
|
}
|
|
3379
3696
|
|
|
3380
3697
|
// src/tracing/observe.ts
|
|
3381
|
-
var
|
|
3698
|
+
var import_api3 = require("@opentelemetry/api");
|
|
3382
3699
|
function asAttr(value) {
|
|
3383
3700
|
return typeof value === "string" ? value : JSON.stringify(value);
|
|
3384
3701
|
}
|
|
3385
3702
|
async function observe(name, fn, options = {}) {
|
|
3386
|
-
const tracer = options.tracer ??
|
|
3703
|
+
const tracer = options.tracer ?? import_api3.trace.getTracer("@withgateway/sdk");
|
|
3387
3704
|
const span = tracer.startSpan(name, {
|
|
3388
3705
|
attributes: {
|
|
3389
3706
|
"openinference.span.kind": options.kind ?? "AGENT",
|
|
@@ -3392,18 +3709,18 @@ async function observe(name, fn, options = {}) {
|
|
|
3392
3709
|
}
|
|
3393
3710
|
});
|
|
3394
3711
|
try {
|
|
3395
|
-
const result = await
|
|
3396
|
-
|
|
3712
|
+
const result = await import_api3.context.with(
|
|
3713
|
+
import_api3.trace.setSpan(import_api3.context.active(), span),
|
|
3397
3714
|
fn
|
|
3398
3715
|
);
|
|
3399
3716
|
if (options.captureOutput !== false && result !== void 0) {
|
|
3400
3717
|
span.setAttribute("output.value", asAttr(result));
|
|
3401
3718
|
}
|
|
3402
|
-
span.setStatus({ code:
|
|
3719
|
+
span.setStatus({ code: import_api3.SpanStatusCode.OK });
|
|
3403
3720
|
return result;
|
|
3404
3721
|
} catch (error) {
|
|
3405
3722
|
span.setStatus({
|
|
3406
|
-
code:
|
|
3723
|
+
code: import_api3.SpanStatusCode.ERROR,
|
|
3407
3724
|
message: error instanceof Error ? error.message : String(error)
|
|
3408
3725
|
});
|
|
3409
3726
|
throw error;
|
|
@@ -3450,7 +3767,7 @@ function observeAnthropic(client2) {
|
|
|
3450
3767
|
}
|
|
3451
3768
|
|
|
3452
3769
|
// src/tracing/claude.ts
|
|
3453
|
-
var
|
|
3770
|
+
var import_api4 = require("@opentelemetry/api");
|
|
3454
3771
|
function cleanToolName(name) {
|
|
3455
3772
|
return name.replace(/^mcp__[^_]+__/, "");
|
|
3456
3773
|
}
|
|
@@ -3464,7 +3781,7 @@ function toolResultText(content) {
|
|
|
3464
3781
|
}
|
|
3465
3782
|
var TURN_INPUT_MAX = 4e3;
|
|
3466
3783
|
async function* observeClaude(stream, options) {
|
|
3467
|
-
const tracer = options.tracer ??
|
|
3784
|
+
const tracer = options.tracer ?? import_api4.trace.getTracer("@withgateway/sdk");
|
|
3468
3785
|
const model = options.model ?? "claude";
|
|
3469
3786
|
const openTools = /* @__PURE__ */ new Map();
|
|
3470
3787
|
let root = null;
|
|
@@ -3477,7 +3794,7 @@ async function* observeClaude(stream, options) {
|
|
|
3477
3794
|
...options.attributes ?? {}
|
|
3478
3795
|
}
|
|
3479
3796
|
});
|
|
3480
|
-
rootCtx =
|
|
3797
|
+
rootCtx = import_api4.trace.setSpan(import_api4.context.active(), root);
|
|
3481
3798
|
}
|
|
3482
3799
|
let turnRoot = null;
|
|
3483
3800
|
let turnCtx = null;
|
|
@@ -3488,7 +3805,7 @@ async function* observeClaude(stream, options) {
|
|
|
3488
3805
|
const endTurn = () => {
|
|
3489
3806
|
if (!turnRoot) return;
|
|
3490
3807
|
if (turnText) turnRoot.setAttribute("output.value", turnText);
|
|
3491
|
-
turnRoot.setStatus({ code:
|
|
3808
|
+
turnRoot.setStatus({ code: import_api4.SpanStatusCode.OK });
|
|
3492
3809
|
turnRoot.end();
|
|
3493
3810
|
turnRoot = null;
|
|
3494
3811
|
turnCtx = null;
|
|
@@ -3510,7 +3827,7 @@ async function* observeClaude(stream, options) {
|
|
|
3510
3827
|
...options.attributes ?? {}
|
|
3511
3828
|
}
|
|
3512
3829
|
});
|
|
3513
|
-
turnCtx =
|
|
3830
|
+
turnCtx = import_api4.trace.setSpan(import_api4.context.active(), turnRoot);
|
|
3514
3831
|
pendingTurnInput = "";
|
|
3515
3832
|
}
|
|
3516
3833
|
const childCtx = options.perTurn ? turnCtx : rootCtx;
|
|
@@ -3572,7 +3889,7 @@ async function* observeClaude(stream, options) {
|
|
|
3572
3889
|
const span = openTools.get(block.tool_use_id);
|
|
3573
3890
|
if (span) {
|
|
3574
3891
|
span.setAttribute("output.value", resultText);
|
|
3575
|
-
span.setStatus({ code:
|
|
3892
|
+
span.setStatus({ code: import_api4.SpanStatusCode.OK });
|
|
3576
3893
|
span.end();
|
|
3577
3894
|
openTools.delete(block.tool_use_id);
|
|
3578
3895
|
}
|
|
@@ -3592,7 +3909,7 @@ async function* observeClaude(stream, options) {
|
|
|
3592
3909
|
endTurn();
|
|
3593
3910
|
} else if (root) {
|
|
3594
3911
|
root.setAttribute("output.value", finalText);
|
|
3595
|
-
root.setStatus({ code:
|
|
3912
|
+
root.setStatus({ code: import_api4.SpanStatusCode.OK });
|
|
3596
3913
|
root.end();
|
|
3597
3914
|
}
|
|
3598
3915
|
}
|
|
@@ -3617,6 +3934,7 @@ var tracing = {
|
|
|
3617
3934
|
};
|
|
3618
3935
|
// Annotate the CommonJS export names for ESM import in node:
|
|
3619
3936
|
0 && (module.exports = {
|
|
3937
|
+
AGENT_ID_KEY,
|
|
3620
3938
|
DEFAULT_TIMEOUT_SECONDS,
|
|
3621
3939
|
DriftError,
|
|
3622
3940
|
ENV_GATEWAY_HOST,
|
|
@@ -3646,6 +3964,10 @@ var tracing = {
|
|
|
3646
3964
|
ReplayReport,
|
|
3647
3965
|
ReplaySuite,
|
|
3648
3966
|
ReplayTask,
|
|
3967
|
+
SUCCESS_METRIC_NAME_KEY,
|
|
3968
|
+
SUCCESS_SIGNAL_KEY,
|
|
3969
|
+
ScoreError,
|
|
3970
|
+
ScoresClient,
|
|
3649
3971
|
SessionLoadError,
|
|
3650
3972
|
SessionRecording,
|
|
3651
3973
|
SessionTrace,
|
|
@@ -3663,11 +3985,13 @@ var tracing = {
|
|
|
3663
3985
|
computeBuildHash,
|
|
3664
3986
|
computeHash,
|
|
3665
3987
|
context,
|
|
3988
|
+
currentTraceId,
|
|
3666
3989
|
defineTool,
|
|
3667
3990
|
experimentContextStorage,
|
|
3668
3991
|
experiments,
|
|
3669
3992
|
gateway,
|
|
3670
3993
|
getExperimentContext,
|
|
3994
|
+
getRegisteredArtifacts,
|
|
3671
3995
|
getRegisteredPrompts,
|
|
3672
3996
|
getRegisteredSkills,
|
|
3673
3997
|
getRegisteredTools,
|
|
@@ -3675,6 +3999,7 @@ var tracing = {
|
|
|
3675
3999
|
loadSession,
|
|
3676
4000
|
memory,
|
|
3677
4001
|
personas,
|
|
4002
|
+
registerArtifact,
|
|
3678
4003
|
registerPrompt,
|
|
3679
4004
|
registerSkill,
|
|
3680
4005
|
renderAgentSystem,
|
|
@@ -3683,9 +4008,11 @@ var tracing = {
|
|
|
3683
4008
|
resolveUserId,
|
|
3684
4009
|
runSuite,
|
|
3685
4010
|
runWithExperimentContext,
|
|
4011
|
+
scores,
|
|
3686
4012
|
sessionObservationFromApi,
|
|
3687
4013
|
sessions,
|
|
3688
4014
|
setExperimentContext,
|
|
4015
|
+
success,
|
|
3689
4016
|
suite,
|
|
3690
4017
|
tracing,
|
|
3691
4018
|
users
|