@withgateway/sdk 0.1.12 → 0.1.14
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 +115 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +1303 -0
- package/dist/cli.js.map +1 -0
- package/dist/cli.mjs +1290 -0
- package/dist/cli.mjs.map +1 -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 +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +878 -32
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +845 -25
- 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 +262 -15
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/index.mjs +256 -9
- package/dist/tracing/index.mjs.map +1 -1
- package/dist/worlds/index.d.mts +377 -0
- package/dist/worlds/index.d.ts +377 -0
- package/dist/worlds/index.js +584 -0
- package/dist/worlds/index.js.map +1 -0
- package/dist/worlds/index.mjs +578 -0
- package/dist/worlds/index.mjs.map +1 -0
- package/package.json +32 -2
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { createRequire as __gatewayCreateRequire } from "module";
|
|
2
|
+
const require = __gatewayCreateRequire(import.meta.url);
|
|
3
|
+
|
|
4
|
+
// src/scores/index.ts
|
|
5
|
+
import { isSpanContextValid, trace } from "@opentelemetry/api";
|
|
6
|
+
var SUCCESS_SIGNAL_KEY = "gateway.success.signal";
|
|
7
|
+
var SUCCESS_METRIC_NAME_KEY = "gateway.success.metricName";
|
|
8
|
+
var AGENT_ID_KEY = "gateway.agent.id";
|
|
9
|
+
var ScoreError = class extends Error {
|
|
10
|
+
constructor(message, options) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = "ScoreError";
|
|
13
|
+
this.cause = options?.cause;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
function currentTraceId() {
|
|
17
|
+
try {
|
|
18
|
+
const span = trace.getActiveSpan();
|
|
19
|
+
if (!span) return void 0;
|
|
20
|
+
const ctx = span.spanContext();
|
|
21
|
+
if (!ctx || !isSpanContextValid(ctx)) return void 0;
|
|
22
|
+
return ctx.traceId;
|
|
23
|
+
} catch {
|
|
24
|
+
return void 0;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
var ScoresClient = class _ScoresClient {
|
|
28
|
+
host;
|
|
29
|
+
publicKey;
|
|
30
|
+
secretKey;
|
|
31
|
+
timeoutSeconds;
|
|
32
|
+
debug;
|
|
33
|
+
constructor(config) {
|
|
34
|
+
this.host = config.host.replace(/\/$/, "");
|
|
35
|
+
this.publicKey = config.publicKey;
|
|
36
|
+
this.secretKey = config.secretKey;
|
|
37
|
+
this.timeoutSeconds = config.timeoutSeconds ?? 15;
|
|
38
|
+
this.debug = config.debug ?? false;
|
|
39
|
+
}
|
|
40
|
+
/** Create a client from GATEWAY_HOST / GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY. */
|
|
41
|
+
static fromEnv(config = {}) {
|
|
42
|
+
const host = config.host ?? process.env.GATEWAY_HOST;
|
|
43
|
+
const publicKey = config.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
|
|
44
|
+
const secretKey = config.secretKey ?? process.env.GATEWAY_SECRET_KEY;
|
|
45
|
+
const missing = [
|
|
46
|
+
["host", host],
|
|
47
|
+
["publicKey", publicKey],
|
|
48
|
+
["secretKey", secretKey]
|
|
49
|
+
].filter(([, value]) => !value).map(([name]) => name);
|
|
50
|
+
if (missing.length > 0) {
|
|
51
|
+
throw new ScoreError(
|
|
52
|
+
`Missing Gateway scores configuration: ${missing.join(", ")}`
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
return new _ScoresClient({
|
|
56
|
+
...config,
|
|
57
|
+
host,
|
|
58
|
+
publicKey,
|
|
59
|
+
secretKey
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Post a score, throwing {@link ScoreError} on any failure. Builds the exact
|
|
64
|
+
* `POST /api/public/scores` body the Python SDK sends: exactly one of
|
|
65
|
+
* `sessionId` / `traceId` (+ optional `observationId`) / `datasetRunId`.
|
|
66
|
+
* When no target is provided, the active OTel trace id is used; if none is
|
|
67
|
+
* available the call is a no-op (returns without posting), matching Python.
|
|
68
|
+
*/
|
|
69
|
+
async postScore(name, value, options = {}) {
|
|
70
|
+
const body = this.buildScoreBody(name, value, options);
|
|
71
|
+
if (!body) {
|
|
72
|
+
this.log(
|
|
73
|
+
"score '%s' dropped: no sessionId, traceId or datasetRunId available",
|
|
74
|
+
name
|
|
75
|
+
);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
await this.request("POST", "/api/public/scores", body);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Build the score request body, or null when no target can be resolved.
|
|
82
|
+
* Split out so it is unit-testable and shared by score()/success().
|
|
83
|
+
*/
|
|
84
|
+
buildScoreBody(name, value, options = {}) {
|
|
85
|
+
const traceId = options.traceId ?? currentTraceId();
|
|
86
|
+
const target = {};
|
|
87
|
+
if (options.sessionId) {
|
|
88
|
+
target.sessionId = options.sessionId;
|
|
89
|
+
} else if (options.datasetRunId) {
|
|
90
|
+
target.datasetRunId = options.datasetRunId;
|
|
91
|
+
} else if (traceId) {
|
|
92
|
+
target.traceId = traceId;
|
|
93
|
+
if (options.observationId) target.observationId = options.observationId;
|
|
94
|
+
} else {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const metadata = {
|
|
98
|
+
source: "gatewaysdk",
|
|
99
|
+
...options.metadata ?? {}
|
|
100
|
+
};
|
|
101
|
+
return {
|
|
102
|
+
name,
|
|
103
|
+
value,
|
|
104
|
+
dataType: options.dataType ?? "NUMERIC",
|
|
105
|
+
environment: options.environment ?? "default",
|
|
106
|
+
...options.comment ? { comment: options.comment } : {},
|
|
107
|
+
metadata,
|
|
108
|
+
...target
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Post a score, FAIL-SOFT: warns and resolves on any failure so a broken
|
|
113
|
+
* transport never throws into (or rejects out of) the caller.
|
|
114
|
+
*/
|
|
115
|
+
async score(name, value, options = {}) {
|
|
116
|
+
try {
|
|
117
|
+
await this.postScore(name, value, options);
|
|
118
|
+
} catch (error) {
|
|
119
|
+
this.warn(`score '${name}' failed`, error);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Emit a SUCCESS SIGNAL that feeds a Gateway Success Metric — a score tagged
|
|
124
|
+
* with `gateway.success.signal` + `gateway.success.metricName` (and the agent
|
|
125
|
+
* identity when `agentId` is set). A boolean `value` records a 1/0 BOOLEAN
|
|
126
|
+
* score (pass/fail); a number records a NUMERIC score. FAIL-SOFT.
|
|
127
|
+
*
|
|
128
|
+
* The metadata shape matches the Python SDK's `Run.success()` exactly, so the
|
|
129
|
+
* platform's Success Metrics aggregation counts signals from either SDK
|
|
130
|
+
* without change.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```ts
|
|
134
|
+
* await scores.success("resolved_ticket", true, { agentId: "support-triage", sessionId });
|
|
135
|
+
* await scores.success("handle_time_s", 42, { agentId: "support-triage", sessionId });
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
async success(name, value = true, options = {}) {
|
|
139
|
+
const { numericValue, inferredType } = coerceSignalValue(value);
|
|
140
|
+
const { agentId, metricName, metadata, dataType, ...rest } = options;
|
|
141
|
+
const signalMetadata = {
|
|
142
|
+
...metadata ?? {},
|
|
143
|
+
[SUCCESS_SIGNAL_KEY]: true,
|
|
144
|
+
[SUCCESS_METRIC_NAME_KEY]: metricName ?? name,
|
|
145
|
+
...agentId ? { [AGENT_ID_KEY]: agentId } : {}
|
|
146
|
+
};
|
|
147
|
+
await this.score(name, numericValue, {
|
|
148
|
+
...rest,
|
|
149
|
+
dataType: dataType ?? inferredType,
|
|
150
|
+
metadata: signalMetadata
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
async request(method, path, payload) {
|
|
154
|
+
let response;
|
|
155
|
+
try {
|
|
156
|
+
response = await fetch(`${this.host}${path}`, {
|
|
157
|
+
method,
|
|
158
|
+
headers: {
|
|
159
|
+
Authorization: this.authHeader(),
|
|
160
|
+
"Content-Type": "application/json"
|
|
161
|
+
},
|
|
162
|
+
body: JSON.stringify(payload),
|
|
163
|
+
signal: AbortSignal.timeout(this.timeoutSeconds * 1e3)
|
|
164
|
+
});
|
|
165
|
+
} catch (error) {
|
|
166
|
+
throw new ScoreError(
|
|
167
|
+
`${method} ${path} failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
168
|
+
{ cause: error }
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
const text = await response.text();
|
|
172
|
+
if (!response.ok) {
|
|
173
|
+
throw new ScoreError(
|
|
174
|
+
`${method} ${path} failed (${response.status}): ${text.slice(0, 500)}`
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
this.log("%s %s -> %s", method, path, response.status);
|
|
178
|
+
}
|
|
179
|
+
authHeader() {
|
|
180
|
+
return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
|
|
181
|
+
"base64"
|
|
182
|
+
)}`;
|
|
183
|
+
}
|
|
184
|
+
log(message, ...args) {
|
|
185
|
+
if (this.debug) {
|
|
186
|
+
console.log(`[gateway.scores] ${message}`, ...args);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
warn(message, ...args) {
|
|
190
|
+
console.warn(`[gateway.scores] ${message}`, ...args);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
function coerceSignalValue(value) {
|
|
194
|
+
if (typeof value === "boolean") {
|
|
195
|
+
return { numericValue: value ? 1 : 0, inferredType: "BOOLEAN" };
|
|
196
|
+
}
|
|
197
|
+
return { numericValue: value, inferredType: "NUMERIC" };
|
|
198
|
+
}
|
|
199
|
+
async function success(name, value = true, options = {}) {
|
|
200
|
+
let client;
|
|
201
|
+
try {
|
|
202
|
+
client = ScoresClient.fromEnv();
|
|
203
|
+
} catch (error) {
|
|
204
|
+
console.warn(`[gateway.scores] success '${name}' skipped`, error);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
await client.success(name, value, options);
|
|
208
|
+
}
|
|
209
|
+
var scores = {
|
|
210
|
+
fromEnv: ScoresClient.fromEnv,
|
|
211
|
+
success
|
|
212
|
+
};
|
|
213
|
+
export {
|
|
214
|
+
AGENT_ID_KEY,
|
|
215
|
+
SUCCESS_METRIC_NAME_KEY,
|
|
216
|
+
SUCCESS_SIGNAL_KEY,
|
|
217
|
+
ScoreError,
|
|
218
|
+
ScoresClient,
|
|
219
|
+
currentTraceId,
|
|
220
|
+
scores,
|
|
221
|
+
success
|
|
222
|
+
};
|
|
223
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/scores/index.ts"],"sourcesContent":["/**\n * Scores & success signals — record evaluation results and ROI-shaped success\n * signals against a Gateway project.\n *\n * A **score** is any evaluation value attached to a trace or session (visible\n * on the Scores page and in trace/session detail). A **success signal** is a\n * score carrying `metadata[\"gateway.success.signal\"]`, which the platform's\n * Success Metrics feature aggregates into a metric's headline count/pass-rate\n * (the `HOOK` metric source). Emit one whenever a real unit of business value\n * happens (\"resolved a ticket\", \"ran an eval\", \"a human accepted an edit\").\n *\n * This mirrors the Python SDK's `Run.score()` / `Run.success()`\n * (`gatewaysdk/sdk/src/gatewaysdk/run.py`). The score shape — name, numeric\n * value, `gateway.success.signal` + `gateway.success.metricName` metadata, and\n * the trace/session target — is IDENTICAL so the platform aggregates signals\n * from either SDK unchanged.\n *\n * ```typescript\n * import { ScoresClient } from \"@withgateway/sdk/scores\";\n *\n * const scores = ScoresClient.fromEnv();\n * // A pass/fail signal that feeds the \"resolved_ticket\" Success Metric:\n * await scores.success(\"resolved_ticket\", true, {\n * agentId: \"support-triage\",\n * sessionId,\n * });\n * // A numeric signal:\n * await scores.success(\"handle_time_s\", 42, { agentId: \"support-triage\", sessionId });\n * ```\n *\n * Under `tracing.initIsolated(...)` the returned handle exposes a `scores`\n * client already bound to that project's keys — that is how the platform\n * self-traces its own agents into dedicated projects.\n */\n\nimport { isSpanContextValid, trace } from \"@opentelemetry/api\";\n\n/** Score value data type, matching Gateway's ingestion schema. */\nexport type ScoreDataType = \"NUMERIC\" | \"CATEGORICAL\" | \"BOOLEAN\";\n\n/** Reserved metadata keys that mark a score as an aggregatable success signal. */\nexport const SUCCESS_SIGNAL_KEY = \"gateway.success.signal\";\nexport const SUCCESS_METRIC_NAME_KEY = \"gateway.success.metricName\";\nexport const AGENT_ID_KEY = \"gateway.agent.id\";\n\nexport type ScoreOptions = {\n /**\n * Trace to link the score to. Auto-detected from the active OpenTelemetry\n * span when omitted and no `sessionId` is given (mirrors the Python SDK).\n */\n traceId?: string;\n /** Session to link the score to. Preferred target when present. */\n sessionId?: string;\n /** Dataset-run to link the score to (dataset-run scores). */\n datasetRunId?: string;\n /** Span-level score — requires `traceId`. */\n observationId?: string;\n /** \"NUMERIC\" | \"CATEGORICAL\" | \"BOOLEAN\"; inferred when omitted. */\n dataType?: ScoreDataType;\n /** Optional comment stored alongside the score. */\n comment?: string;\n /** Extra metadata merged into the score. */\n metadata?: Record<string, unknown>;\n /** Environment tag; defaults to the project's `default` environment. */\n environment?: string;\n};\n\nexport type SuccessOptions = Omit<ScoreOptions, \"dataType\"> & {\n /**\n * The agent this signal belongs to (`gateway.agent.id`). Lets the platform\n * roll the signal into that agent's Success Metric without a trace join.\n */\n agentId?: string;\n /** The Success Metric name; defaults to the signal `name`. */\n metricName?: string;\n /** Override the inferred \"BOOLEAN\"/\"NUMERIC\" data type. */\n dataType?: \"NUMERIC\" | \"BOOLEAN\";\n};\n\nexport type ScoresClientConfig = {\n host: string;\n publicKey: string;\n secretKey: string;\n timeoutSeconds?: number;\n debug?: boolean;\n};\n\nexport type ScoresClientFromEnvConfig = Partial<ScoresClientConfig>;\n\n/** Raised by the low-level {@link ScoresClient.postScore} on a failed request. */\nexport class ScoreError extends Error {\n constructor(message: string, options?: { cause?: unknown }) {\n super(message);\n this.name = \"ScoreError\";\n this.cause = options?.cause;\n }\n}\n\n/**\n * Read the current trace id (32-hex) from the active OpenTelemetry span, or\n * undefined when there is no valid active span. Mirrors the Python SDK's\n * `_get_current_trace_id`.\n */\nexport function currentTraceId(): string | undefined {\n try {\n const span = trace.getActiveSpan();\n if (!span) return undefined;\n const ctx = span.spanContext();\n if (!ctx || !isSpanContextValid(ctx)) return undefined;\n return ctx.traceId;\n } catch {\n return undefined;\n }\n}\n\n/**\n * Client for Gateway's public scores ingestion endpoint (Basic project keys).\n * The `score`/`success` convenience methods are FAIL-SOFT — they never reject\n * into the caller, they warn and resolve — matching the Python SDK's\n * fire-and-forget scoring. The raw {@link postScore} throws on failure for\n * callers that want to handle it.\n */\nexport class ScoresClient {\n private readonly host: string;\n private readonly publicKey: string;\n private readonly secretKey: string;\n private readonly timeoutSeconds: number;\n private readonly debug: boolean;\n\n constructor(config: ScoresClientConfig) {\n this.host = config.host.replace(/\\/$/, \"\");\n this.publicKey = config.publicKey;\n this.secretKey = config.secretKey;\n this.timeoutSeconds = config.timeoutSeconds ?? 15;\n this.debug = config.debug ?? false;\n }\n\n /** Create a client from GATEWAY_HOST / GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY. */\n static fromEnv(config: ScoresClientFromEnvConfig = {}): ScoresClient {\n const host = config.host ?? process.env.GATEWAY_HOST;\n const publicKey = config.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;\n const secretKey = config.secretKey ?? process.env.GATEWAY_SECRET_KEY;\n\n const missing = [\n [\"host\", host],\n [\"publicKey\", publicKey],\n [\"secretKey\", secretKey],\n ]\n .filter(([, value]) => !value)\n .map(([name]) => name);\n\n if (missing.length > 0) {\n throw new ScoreError(\n `Missing Gateway scores configuration: ${missing.join(\", \")}`,\n );\n }\n\n return new ScoresClient({\n ...config,\n host: host!,\n publicKey: publicKey!,\n secretKey: secretKey!,\n });\n }\n\n /**\n * Post a score, throwing {@link ScoreError} on any failure. Builds the exact\n * `POST /api/public/scores` body the Python SDK sends: exactly one of\n * `sessionId` / `traceId` (+ optional `observationId`) / `datasetRunId`.\n * When no target is provided, the active OTel trace id is used; if none is\n * available the call is a no-op (returns without posting), matching Python.\n */\n async postScore(\n name: string,\n value: number | string,\n options: ScoreOptions = {},\n ): Promise<void> {\n const body = this.buildScoreBody(name, value, options);\n if (!body) {\n this.log(\n \"score '%s' dropped: no sessionId, traceId or datasetRunId available\",\n name,\n );\n return;\n }\n await this.request(\"POST\", \"/api/public/scores\", body);\n }\n\n /**\n * Build the score request body, or null when no target can be resolved.\n * Split out so it is unit-testable and shared by score()/success().\n */\n buildScoreBody(\n name: string,\n value: number | string,\n options: ScoreOptions = {},\n ): Record<string, unknown> | null {\n // Prefer sessionId (scores typically grade a whole session/rollout), then\n // datasetRunId, then an explicit traceId, then the ambient OTel trace.\n const traceId = options.traceId ?? currentTraceId();\n const target: Record<string, unknown> = {};\n if (options.sessionId) {\n target.sessionId = options.sessionId;\n } else if (options.datasetRunId) {\n target.datasetRunId = options.datasetRunId;\n } else if (traceId) {\n target.traceId = traceId;\n if (options.observationId) target.observationId = options.observationId;\n } else {\n return null;\n }\n\n // Base metadata mirrors the Python SDK (source tag), then the caller's\n // metadata is merged on top — identical to `score_metadata.update(...)`.\n const metadata: Record<string, unknown> = {\n source: \"gatewaysdk\",\n ...(options.metadata ?? {}),\n };\n\n return {\n name,\n value,\n dataType: options.dataType ?? \"NUMERIC\",\n environment: options.environment ?? \"default\",\n ...(options.comment ? { comment: options.comment } : {}),\n metadata,\n ...target,\n };\n }\n\n /**\n * Post a score, FAIL-SOFT: warns and resolves on any failure so a broken\n * transport never throws into (or rejects out of) the caller.\n */\n async score(\n name: string,\n value: number | string,\n options: ScoreOptions = {},\n ): Promise<void> {\n try {\n await this.postScore(name, value, options);\n } catch (error) {\n this.warn(`score '${name}' failed`, error);\n }\n }\n\n /**\n * Emit a SUCCESS SIGNAL that feeds a Gateway Success Metric — a score tagged\n * with `gateway.success.signal` + `gateway.success.metricName` (and the agent\n * identity when `agentId` is set). A boolean `value` records a 1/0 BOOLEAN\n * score (pass/fail); a number records a NUMERIC score. FAIL-SOFT.\n *\n * The metadata shape matches the Python SDK's `Run.success()` exactly, so the\n * platform's Success Metrics aggregation counts signals from either SDK\n * without change.\n *\n * @example\n * ```ts\n * await scores.success(\"resolved_ticket\", true, { agentId: \"support-triage\", sessionId });\n * await scores.success(\"handle_time_s\", 42, { agentId: \"support-triage\", sessionId });\n * ```\n */\n async success(\n name: string,\n value: boolean | number = true,\n options: SuccessOptions = {},\n ): Promise<void> {\n const { numericValue, inferredType } = coerceSignalValue(value);\n const { agentId, metricName, metadata, dataType, ...rest } = options;\n\n // Required signal keys are written LAST so a caller's `metadata` cannot\n // accidentally clobber them and silently drop the signal from aggregation.\n const signalMetadata: Record<string, unknown> = {\n ...(metadata ?? {}),\n [SUCCESS_SIGNAL_KEY]: true,\n [SUCCESS_METRIC_NAME_KEY]: metricName ?? name,\n ...(agentId ? { [AGENT_ID_KEY]: agentId } : {}),\n };\n\n await this.score(name, numericValue, {\n ...rest,\n dataType: dataType ?? inferredType,\n metadata: signalMetadata,\n });\n }\n\n private async request(\n method: \"POST\",\n path: string,\n payload: Record<string, unknown>,\n ): Promise<void> {\n let response: Response;\n try {\n response = await fetch(`${this.host}${path}`, {\n method,\n headers: {\n Authorization: this.authHeader(),\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(payload),\n signal: AbortSignal.timeout(this.timeoutSeconds * 1000),\n });\n } catch (error) {\n throw new ScoreError(\n `${method} ${path} failed: ${error instanceof Error ? error.message : String(error)}`,\n { cause: error },\n );\n }\n\n const text = await response.text();\n if (!response.ok) {\n throw new ScoreError(\n `${method} ${path} failed (${response.status}): ${text.slice(0, 500)}`,\n );\n }\n this.log(\"%s %s -> %s\", method, path, response.status);\n }\n\n private authHeader(): string {\n return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(\n \"base64\",\n )}`;\n }\n\n private log(message: string, ...args: unknown[]): void {\n if (this.debug) {\n console.log(`[gateway.scores] ${message}`, ...args);\n }\n }\n\n private warn(message: string, ...args: unknown[]): void {\n console.warn(`[gateway.scores] ${message}`, ...args);\n }\n}\n\n/** Map a success value to its numeric score + inferred data type. */\nfunction coerceSignalValue(value: boolean | number): {\n numericValue: number;\n inferredType: \"NUMERIC\" | \"BOOLEAN\";\n} {\n if (typeof value === \"boolean\") {\n return { numericValue: value ? 1 : 0, inferredType: \"BOOLEAN\" };\n }\n return { numericValue: value, inferredType: \"NUMERIC\" };\n}\n\n/**\n * Emit a success signal using credentials from the environment\n * (GATEWAY_HOST / GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY) — the ergonomic\n * top-level entry point for the default `tracing.init()` path, mirroring the\n * Python SDK's `run.success(...)`. FAIL-SOFT end to end: a missing/invalid\n * environment or a transport failure warns and resolves, never throwing into\n * the caller. For isolated providers use the handle's bound client\n * (`handle.scores.success(...)`) instead so the signal lands in that project.\n */\nexport async function success(\n name: string,\n value: boolean | number = true,\n options: SuccessOptions = {},\n): Promise<void> {\n let client: ScoresClient;\n try {\n client = ScoresClient.fromEnv();\n } catch (error) {\n console.warn(`[gateway.scores] success '${name}' skipped`, error);\n return;\n }\n await client.success(name, value, options);\n}\n\n/** Lowercase convenience namespace, matching the `users` export. */\nexport const scores = {\n fromEnv: ScoresClient.fromEnv,\n success,\n};\n"],"mappings":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K;AACjC,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,IAAI;AAAA,QACR,GAAG,MAAM,IAAI,IAAI,YAAY,SAAS,MAAM,MAAM,KAAK,MAAM,GAAG,GAAG,CAAC;AAAA,MACtE;AAAA,IACF;AACA,SAAK,IAAI,eAAe,QAAQ,MAAM,SAAS,MAAM;AAAA,EACvD;AAAA,EAEQ,aAAqB;AAC3B,WAAO,SAAS,OAAO,KAAK,GAAG,KAAK,SAAS,IAAI,KAAK,SAAS,EAAE,EAAE;AAAA,MACjE;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEQ,IAAI,YAAoB,MAAuB;AACrD,QAAI,KAAK,OAAO;AACd,cAAQ,IAAI,oBAAoB,OAAO,IAAI,GAAG,IAAI;AAAA,IACpD;AAAA,EACF;AAAA,EAEQ,KAAK,YAAoB,MAAuB;AACtD,YAAQ,KAAK,oBAAoB,OAAO,IAAI,GAAG,IAAI;AAAA,EACrD;AACF;AAGA,SAAS,kBAAkB,OAGzB;AACA,MAAI,OAAO,UAAU,WAAW;AAC9B,WAAO,EAAE,cAAc,QAAQ,IAAI,GAAG,cAAc,UAAU;AAAA,EAChE;AACA,SAAO,EAAE,cAAc,OAAO,cAAc,UAAU;AACxD;AAWA,eAAsB,QACpB,MACA,QAA0B,MAC1B,UAA0B,CAAC,GACZ;AACf,MAAI;AACJ,MAAI;AACF,aAAS,aAAa,QAAQ;AAAA,EAChC,SAAS,OAAO;AACd,YAAQ,KAAK,6BAA6B,IAAI,aAAa,KAAK;AAChE;AAAA,EACF;AACA,QAAM,OAAO,QAAQ,MAAM,OAAO,OAAO;AAC3C;AAGO,IAAM,SAAS;AAAA,EACpB,SAAS,aAAa;AAAA,EACtB;AACF;","names":[]}
|
package/dist/tracing/index.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export { I as IdentifyOptions, a as IdentitySpanProcessor, O as ObservationKind, b as ObserveClaudeOptions, c as ObserveOptions, T as TracingConfig, d as TracingHandle, e as TracingState, U as USER_ID_ATTRIBUTE, f as flush, i as identify, g as init, h as initIsolated, j as isGatewayConfigured, k as isInitialized, o as observe, l as observeAnthropic, m as observeClaude, n as observeOpenAI, p as resetAmbientUser, r as resolveUserId, s as shutdown } from '../index-
|
|
1
|
+
export { I as IdentifyOptions, a as IdentitySpanProcessor, O as ObservationKind, b as ObserveClaudeOptions, c as ObserveOptions, T as TracingConfig, d as TracingHandle, e as TracingState, U as USER_ID_ATTRIBUTE, f as flush, i as identify, g as init, h as initIsolated, j as isGatewayConfigured, k as isInitialized, o as observe, l as observeAnthropic, m as observeClaude, n as observeOpenAI, p as resetAmbientUser, r as resolveUserId, s as shutdown } from '../index-B5HUQQDE.mjs';
|
|
2
2
|
import '@opentelemetry/api';
|
|
3
|
+
import '../scores/index.mjs';
|
|
3
4
|
import '@opentelemetry/sdk-trace-base';
|
package/dist/tracing/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export { I as IdentifyOptions, a as IdentitySpanProcessor, O as ObservationKind, b as ObserveClaudeOptions, c as ObserveOptions, T as TracingConfig, d as TracingHandle, e as TracingState, U as USER_ID_ATTRIBUTE, f as flush, i as identify, g as init, h as initIsolated, j as isGatewayConfigured, k as isInitialized, o as observe, l as observeAnthropic, m as observeClaude, n as observeOpenAI, p as resetAmbientUser, r as resolveUserId, s as shutdown } from '../index-
|
|
1
|
+
export { I as IdentifyOptions, a as IdentitySpanProcessor, O as ObservationKind, b as ObserveClaudeOptions, c as ObserveOptions, T as TracingConfig, d as TracingHandle, e as TracingState, U as USER_ID_ATTRIBUTE, f as flush, i as identify, g as init, h as initIsolated, j as isGatewayConfigured, k as isInitialized, o as observe, l as observeAnthropic, m as observeClaude, n as observeOpenAI, p as resetAmbientUser, r as resolveUserId, s as shutdown } from '../index-Dpo-t8iO.js';
|
|
2
2
|
import '@opentelemetry/api';
|
|
3
|
+
import '../scores/index.js';
|
|
3
4
|
import '@opentelemetry/sdk-trace-base';
|
package/dist/tracing/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.14",
|
|
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,16 @@ 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"
|
|
88
|
+
},
|
|
89
|
+
"./worlds": {
|
|
90
|
+
types: "./dist/worlds/index.d.ts",
|
|
91
|
+
import: "./dist/worlds/index.mjs",
|
|
92
|
+
require: "./dist/worlds/index.js"
|
|
83
93
|
}
|
|
84
94
|
},
|
|
85
95
|
files: [
|
|
@@ -93,6 +103,7 @@ var require_package = __commonJS({
|
|
|
93
103
|
build: "tsup",
|
|
94
104
|
dev: "tsup --watch",
|
|
95
105
|
typecheck: "tsc --noEmit",
|
|
106
|
+
"typecheck:examples": "tsc -p tsconfig.examples.json --noEmit",
|
|
96
107
|
test: "vitest",
|
|
97
108
|
"test:run": "vitest run",
|
|
98
109
|
lint: "eslint src --ext .ts",
|
|
@@ -153,10 +164,29 @@ var require_package = __commonJS({
|
|
|
153
164
|
"@opentelemetry/sdk-trace-node": "^2.6.0",
|
|
154
165
|
"@opentelemetry/semantic-conventions": "^1.40.0",
|
|
155
166
|
"@types/node": "^20.0.0",
|
|
167
|
+
openai: "^4.70.0",
|
|
156
168
|
tsup: "^8.0.0",
|
|
157
169
|
tsx: "^4.0.0",
|
|
158
170
|
typescript: "^5.0.0",
|
|
159
|
-
vitest: "^2.
|
|
171
|
+
vitest: "^3.2.6"
|
|
172
|
+
},
|
|
173
|
+
overrides: {
|
|
174
|
+
protobufjs: "^8.4.1",
|
|
175
|
+
"vite@6": "^6.4.3",
|
|
176
|
+
postcss: "^8.5.18",
|
|
177
|
+
"picomatch@4": "^4.0.4"
|
|
178
|
+
},
|
|
179
|
+
pnpm: {
|
|
180
|
+
overrides: {
|
|
181
|
+
protobufjs: "^8.4.1",
|
|
182
|
+
vitest: "^3.2.6",
|
|
183
|
+
"vite@6": "^6.4.3",
|
|
184
|
+
postcss: "^8.5.18",
|
|
185
|
+
"picomatch@4": "^4.0.4"
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
bin: {
|
|
189
|
+
gateway: "./dist/cli.js"
|
|
160
190
|
}
|
|
161
191
|
};
|
|
162
192
|
}
|
|
@@ -450,6 +480,216 @@ function debugLog(message, ...args) {
|
|
|
450
480
|
}
|
|
451
481
|
}
|
|
452
482
|
|
|
483
|
+
// src/scores/index.ts
|
|
484
|
+
var import_api2 = require("@opentelemetry/api");
|
|
485
|
+
var SUCCESS_SIGNAL_KEY = "gateway.success.signal";
|
|
486
|
+
var SUCCESS_METRIC_NAME_KEY = "gateway.success.metricName";
|
|
487
|
+
var AGENT_ID_KEY = "gateway.agent.id";
|
|
488
|
+
var ScoreError = class extends Error {
|
|
489
|
+
constructor(message, options) {
|
|
490
|
+
super(message);
|
|
491
|
+
this.name = "ScoreError";
|
|
492
|
+
this.cause = options?.cause;
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
function currentTraceId() {
|
|
496
|
+
try {
|
|
497
|
+
const span = import_api2.trace.getActiveSpan();
|
|
498
|
+
if (!span) return void 0;
|
|
499
|
+
const ctx = span.spanContext();
|
|
500
|
+
if (!ctx || !(0, import_api2.isSpanContextValid)(ctx)) return void 0;
|
|
501
|
+
return ctx.traceId;
|
|
502
|
+
} catch {
|
|
503
|
+
return void 0;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
var ScoresClient = class _ScoresClient {
|
|
507
|
+
host;
|
|
508
|
+
publicKey;
|
|
509
|
+
secretKey;
|
|
510
|
+
timeoutSeconds;
|
|
511
|
+
debug;
|
|
512
|
+
constructor(config) {
|
|
513
|
+
this.host = config.host.replace(/\/$/, "");
|
|
514
|
+
this.publicKey = config.publicKey;
|
|
515
|
+
this.secretKey = config.secretKey;
|
|
516
|
+
this.timeoutSeconds = config.timeoutSeconds ?? 15;
|
|
517
|
+
this.debug = config.debug ?? false;
|
|
518
|
+
}
|
|
519
|
+
/** Create a client from GATEWAY_HOST / GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY. */
|
|
520
|
+
static fromEnv(config = {}) {
|
|
521
|
+
const host = config.host ?? process.env.GATEWAY_HOST;
|
|
522
|
+
const publicKey = config.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
|
|
523
|
+
const secretKey = config.secretKey ?? process.env.GATEWAY_SECRET_KEY;
|
|
524
|
+
const missing = [
|
|
525
|
+
["host", host],
|
|
526
|
+
["publicKey", publicKey],
|
|
527
|
+
["secretKey", secretKey]
|
|
528
|
+
].filter(([, value]) => !value).map(([name]) => name);
|
|
529
|
+
if (missing.length > 0) {
|
|
530
|
+
throw new ScoreError(
|
|
531
|
+
`Missing Gateway scores configuration: ${missing.join(", ")}`
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
return new _ScoresClient({
|
|
535
|
+
...config,
|
|
536
|
+
host,
|
|
537
|
+
publicKey,
|
|
538
|
+
secretKey
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Post a score, throwing {@link ScoreError} on any failure. Builds the exact
|
|
543
|
+
* `POST /api/public/scores` body the Python SDK sends: exactly one of
|
|
544
|
+
* `sessionId` / `traceId` (+ optional `observationId`) / `datasetRunId`.
|
|
545
|
+
* When no target is provided, the active OTel trace id is used; if none is
|
|
546
|
+
* available the call is a no-op (returns without posting), matching Python.
|
|
547
|
+
*/
|
|
548
|
+
async postScore(name, value, options = {}) {
|
|
549
|
+
const body = this.buildScoreBody(name, value, options);
|
|
550
|
+
if (!body) {
|
|
551
|
+
this.log(
|
|
552
|
+
"score '%s' dropped: no sessionId, traceId or datasetRunId available",
|
|
553
|
+
name
|
|
554
|
+
);
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
await this.request("POST", "/api/public/scores", body);
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Build the score request body, or null when no target can be resolved.
|
|
561
|
+
* Split out so it is unit-testable and shared by score()/success().
|
|
562
|
+
*/
|
|
563
|
+
buildScoreBody(name, value, options = {}) {
|
|
564
|
+
const traceId = options.traceId ?? currentTraceId();
|
|
565
|
+
const target = {};
|
|
566
|
+
if (options.sessionId) {
|
|
567
|
+
target.sessionId = options.sessionId;
|
|
568
|
+
} else if (options.datasetRunId) {
|
|
569
|
+
target.datasetRunId = options.datasetRunId;
|
|
570
|
+
} else if (traceId) {
|
|
571
|
+
target.traceId = traceId;
|
|
572
|
+
if (options.observationId) target.observationId = options.observationId;
|
|
573
|
+
} else {
|
|
574
|
+
return null;
|
|
575
|
+
}
|
|
576
|
+
const metadata = {
|
|
577
|
+
source: "gatewaysdk",
|
|
578
|
+
...options.metadata ?? {}
|
|
579
|
+
};
|
|
580
|
+
return {
|
|
581
|
+
name,
|
|
582
|
+
value,
|
|
583
|
+
dataType: options.dataType ?? "NUMERIC",
|
|
584
|
+
environment: options.environment ?? "default",
|
|
585
|
+
...options.comment ? { comment: options.comment } : {},
|
|
586
|
+
metadata,
|
|
587
|
+
...target
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
/**
|
|
591
|
+
* Post a score, FAIL-SOFT: warns and resolves on any failure so a broken
|
|
592
|
+
* transport never throws into (or rejects out of) the caller.
|
|
593
|
+
*/
|
|
594
|
+
async score(name, value, options = {}) {
|
|
595
|
+
try {
|
|
596
|
+
await this.postScore(name, value, options);
|
|
597
|
+
} catch (error) {
|
|
598
|
+
this.warn(`score '${name}' failed`, error);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Emit a SUCCESS SIGNAL that feeds a Gateway Success Metric — a score tagged
|
|
603
|
+
* with `gateway.success.signal` + `gateway.success.metricName` (and the agent
|
|
604
|
+
* identity when `agentId` is set). A boolean `value` records a 1/0 BOOLEAN
|
|
605
|
+
* score (pass/fail); a number records a NUMERIC score. FAIL-SOFT.
|
|
606
|
+
*
|
|
607
|
+
* The metadata shape matches the Python SDK's `Run.success()` exactly, so the
|
|
608
|
+
* platform's Success Metrics aggregation counts signals from either SDK
|
|
609
|
+
* without change.
|
|
610
|
+
*
|
|
611
|
+
* @example
|
|
612
|
+
* ```ts
|
|
613
|
+
* await scores.success("resolved_ticket", true, { agentId: "support-triage", sessionId });
|
|
614
|
+
* await scores.success("handle_time_s", 42, { agentId: "support-triage", sessionId });
|
|
615
|
+
* ```
|
|
616
|
+
*/
|
|
617
|
+
async success(name, value = true, options = {}) {
|
|
618
|
+
const { numericValue, inferredType } = coerceSignalValue(value);
|
|
619
|
+
const { agentId, metricName, metadata, dataType, ...rest } = options;
|
|
620
|
+
const signalMetadata = {
|
|
621
|
+
...metadata ?? {},
|
|
622
|
+
[SUCCESS_SIGNAL_KEY]: true,
|
|
623
|
+
[SUCCESS_METRIC_NAME_KEY]: metricName ?? name,
|
|
624
|
+
...agentId ? { [AGENT_ID_KEY]: agentId } : {}
|
|
625
|
+
};
|
|
626
|
+
await this.score(name, numericValue, {
|
|
627
|
+
...rest,
|
|
628
|
+
dataType: dataType ?? inferredType,
|
|
629
|
+
metadata: signalMetadata
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
async request(method, path, payload) {
|
|
633
|
+
let response;
|
|
634
|
+
try {
|
|
635
|
+
response = await fetch(`${this.host}${path}`, {
|
|
636
|
+
method,
|
|
637
|
+
headers: {
|
|
638
|
+
Authorization: this.authHeader(),
|
|
639
|
+
"Content-Type": "application/json"
|
|
640
|
+
},
|
|
641
|
+
body: JSON.stringify(payload),
|
|
642
|
+
signal: AbortSignal.timeout(this.timeoutSeconds * 1e3)
|
|
643
|
+
});
|
|
644
|
+
} catch (error) {
|
|
645
|
+
throw new ScoreError(
|
|
646
|
+
`${method} ${path} failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
647
|
+
{ cause: error }
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
const text = await response.text();
|
|
651
|
+
if (!response.ok) {
|
|
652
|
+
throw new ScoreError(
|
|
653
|
+
`${method} ${path} failed (${response.status}): ${text.slice(0, 500)}`
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
this.log("%s %s -> %s", method, path, response.status);
|
|
657
|
+
}
|
|
658
|
+
authHeader() {
|
|
659
|
+
return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
|
|
660
|
+
"base64"
|
|
661
|
+
)}`;
|
|
662
|
+
}
|
|
663
|
+
log(message, ...args) {
|
|
664
|
+
if (this.debug) {
|
|
665
|
+
console.log(`[gateway.scores] ${message}`, ...args);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
warn(message, ...args) {
|
|
669
|
+
console.warn(`[gateway.scores] ${message}`, ...args);
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
function coerceSignalValue(value) {
|
|
673
|
+
if (typeof value === "boolean") {
|
|
674
|
+
return { numericValue: value ? 1 : 0, inferredType: "BOOLEAN" };
|
|
675
|
+
}
|
|
676
|
+
return { numericValue: value, inferredType: "NUMERIC" };
|
|
677
|
+
}
|
|
678
|
+
async function success(name, value = true, options = {}) {
|
|
679
|
+
let client;
|
|
680
|
+
try {
|
|
681
|
+
client = ScoresClient.fromEnv();
|
|
682
|
+
} catch (error) {
|
|
683
|
+
console.warn(`[gateway.scores] success '${name}' skipped`, error);
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
await client.success(name, value, options);
|
|
687
|
+
}
|
|
688
|
+
var scores = {
|
|
689
|
+
fromEnv: ScoresClient.fromEnv,
|
|
690
|
+
success
|
|
691
|
+
};
|
|
692
|
+
|
|
453
693
|
// src/build/index.ts
|
|
454
694
|
var import_node_crypto = require("crypto");
|
|
455
695
|
function canonicalJson(value) {
|
|
@@ -854,12 +1094,19 @@ function initIsolated(config = {}) {
|
|
|
854
1094
|
const provider = built.provider;
|
|
855
1095
|
scheduleBuildRegistration(config, built);
|
|
856
1096
|
const tracer = provider.getTracer("@withgateway/sdk");
|
|
1097
|
+
const scores2 = new ScoresClient({
|
|
1098
|
+
host: built.connection.host,
|
|
1099
|
+
publicKey: built.connection.publicKey,
|
|
1100
|
+
secretKey: built.connection.secretKey,
|
|
1101
|
+
debug: config.debug ?? false
|
|
1102
|
+
});
|
|
857
1103
|
debugLog2(
|
|
858
1104
|
"Isolated tracing provider created (service=%s)",
|
|
859
1105
|
config.serviceName ?? "gatewaysdk-tracing"
|
|
860
1106
|
);
|
|
861
1107
|
return {
|
|
862
1108
|
tracer,
|
|
1109
|
+
scores: scores2,
|
|
863
1110
|
async flush() {
|
|
864
1111
|
try {
|
|
865
1112
|
await provider.forceFlush();
|
|
@@ -913,12 +1160,12 @@ function isGatewayConfigured() {
|
|
|
913
1160
|
}
|
|
914
1161
|
|
|
915
1162
|
// src/tracing/observe.ts
|
|
916
|
-
var
|
|
1163
|
+
var import_api3 = require("@opentelemetry/api");
|
|
917
1164
|
function asAttr(value) {
|
|
918
1165
|
return typeof value === "string" ? value : JSON.stringify(value);
|
|
919
1166
|
}
|
|
920
1167
|
async function observe(name, fn, options = {}) {
|
|
921
|
-
const tracer = options.tracer ??
|
|
1168
|
+
const tracer = options.tracer ?? import_api3.trace.getTracer("@withgateway/sdk");
|
|
922
1169
|
const span = tracer.startSpan(name, {
|
|
923
1170
|
attributes: {
|
|
924
1171
|
"openinference.span.kind": options.kind ?? "AGENT",
|
|
@@ -927,18 +1174,18 @@ async function observe(name, fn, options = {}) {
|
|
|
927
1174
|
}
|
|
928
1175
|
});
|
|
929
1176
|
try {
|
|
930
|
-
const result = await
|
|
931
|
-
|
|
1177
|
+
const result = await import_api3.context.with(
|
|
1178
|
+
import_api3.trace.setSpan(import_api3.context.active(), span),
|
|
932
1179
|
fn
|
|
933
1180
|
);
|
|
934
1181
|
if (options.captureOutput !== false && result !== void 0) {
|
|
935
1182
|
span.setAttribute("output.value", asAttr(result));
|
|
936
1183
|
}
|
|
937
|
-
span.setStatus({ code:
|
|
1184
|
+
span.setStatus({ code: import_api3.SpanStatusCode.OK });
|
|
938
1185
|
return result;
|
|
939
1186
|
} catch (error) {
|
|
940
1187
|
span.setStatus({
|
|
941
|
-
code:
|
|
1188
|
+
code: import_api3.SpanStatusCode.ERROR,
|
|
942
1189
|
message: error instanceof Error ? error.message : String(error)
|
|
943
1190
|
});
|
|
944
1191
|
throw error;
|
|
@@ -985,7 +1232,7 @@ function observeAnthropic(client) {
|
|
|
985
1232
|
}
|
|
986
1233
|
|
|
987
1234
|
// src/tracing/claude.ts
|
|
988
|
-
var
|
|
1235
|
+
var import_api4 = require("@opentelemetry/api");
|
|
989
1236
|
function cleanToolName(name) {
|
|
990
1237
|
return name.replace(/^mcp__[^_]+__/, "");
|
|
991
1238
|
}
|
|
@@ -999,7 +1246,7 @@ function toolResultText(content) {
|
|
|
999
1246
|
}
|
|
1000
1247
|
var TURN_INPUT_MAX = 4e3;
|
|
1001
1248
|
async function* observeClaude(stream, options) {
|
|
1002
|
-
const tracer = options.tracer ??
|
|
1249
|
+
const tracer = options.tracer ?? import_api4.trace.getTracer("@withgateway/sdk");
|
|
1003
1250
|
const model = options.model ?? "claude";
|
|
1004
1251
|
const openTools = /* @__PURE__ */ new Map();
|
|
1005
1252
|
let root = null;
|
|
@@ -1012,7 +1259,7 @@ async function* observeClaude(stream, options) {
|
|
|
1012
1259
|
...options.attributes ?? {}
|
|
1013
1260
|
}
|
|
1014
1261
|
});
|
|
1015
|
-
rootCtx =
|
|
1262
|
+
rootCtx = import_api4.trace.setSpan(import_api4.context.active(), root);
|
|
1016
1263
|
}
|
|
1017
1264
|
let turnRoot = null;
|
|
1018
1265
|
let turnCtx = null;
|
|
@@ -1023,7 +1270,7 @@ async function* observeClaude(stream, options) {
|
|
|
1023
1270
|
const endTurn = () => {
|
|
1024
1271
|
if (!turnRoot) return;
|
|
1025
1272
|
if (turnText) turnRoot.setAttribute("output.value", turnText);
|
|
1026
|
-
turnRoot.setStatus({ code:
|
|
1273
|
+
turnRoot.setStatus({ code: import_api4.SpanStatusCode.OK });
|
|
1027
1274
|
turnRoot.end();
|
|
1028
1275
|
turnRoot = null;
|
|
1029
1276
|
turnCtx = null;
|
|
@@ -1045,7 +1292,7 @@ async function* observeClaude(stream, options) {
|
|
|
1045
1292
|
...options.attributes ?? {}
|
|
1046
1293
|
}
|
|
1047
1294
|
});
|
|
1048
|
-
turnCtx =
|
|
1295
|
+
turnCtx = import_api4.trace.setSpan(import_api4.context.active(), turnRoot);
|
|
1049
1296
|
pendingTurnInput = "";
|
|
1050
1297
|
}
|
|
1051
1298
|
const childCtx = options.perTurn ? turnCtx : rootCtx;
|
|
@@ -1107,7 +1354,7 @@ async function* observeClaude(stream, options) {
|
|
|
1107
1354
|
const span = openTools.get(block.tool_use_id);
|
|
1108
1355
|
if (span) {
|
|
1109
1356
|
span.setAttribute("output.value", resultText);
|
|
1110
|
-
span.setStatus({ code:
|
|
1357
|
+
span.setStatus({ code: import_api4.SpanStatusCode.OK });
|
|
1111
1358
|
span.end();
|
|
1112
1359
|
openTools.delete(block.tool_use_id);
|
|
1113
1360
|
}
|
|
@@ -1127,7 +1374,7 @@ async function* observeClaude(stream, options) {
|
|
|
1127
1374
|
endTurn();
|
|
1128
1375
|
} else if (root) {
|
|
1129
1376
|
root.setAttribute("output.value", finalText);
|
|
1130
|
-
root.setStatus({ code:
|
|
1377
|
+
root.setStatus({ code: import_api4.SpanStatusCode.OK });
|
|
1131
1378
|
root.end();
|
|
1132
1379
|
}
|
|
1133
1380
|
}
|