@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
|
@@ -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":";;;;AAmCA,SAAS,oBAAoB,aAAa;AAMnC,IAAM,qBAAqB;AAC3B,IAAM,0BAA0B;AAChC,IAAM,eAAe;AA+CrB,IAAM,aAAN,cAAyB,MAAM;AAAA,EACpC,YAAY,SAAiB,SAA+B;AAC1D,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,QAAQ,SAAS;AAAA,EACxB;AACF;AAOO,SAAS,iBAAqC;AACnD,MAAI;AACF,UAAM,OAAO,M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AS,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.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: [
|
|
@@ -450,6 +455,216 @@ function debugLog(message, ...args) {
|
|
|
450
455
|
}
|
|
451
456
|
}
|
|
452
457
|
|
|
458
|
+
// src/scores/index.ts
|
|
459
|
+
var import_api2 = require("@opentelemetry/api");
|
|
460
|
+
var SUCCESS_SIGNAL_KEY = "gateway.success.signal";
|
|
461
|
+
var SUCCESS_METRIC_NAME_KEY = "gateway.success.metricName";
|
|
462
|
+
var AGENT_ID_KEY = "gateway.agent.id";
|
|
463
|
+
var ScoreError = class extends Error {
|
|
464
|
+
constructor(message, options) {
|
|
465
|
+
super(message);
|
|
466
|
+
this.name = "ScoreError";
|
|
467
|
+
this.cause = options?.cause;
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
function currentTraceId() {
|
|
471
|
+
try {
|
|
472
|
+
const span = import_api2.trace.getActiveSpan();
|
|
473
|
+
if (!span) return void 0;
|
|
474
|
+
const ctx = span.spanContext();
|
|
475
|
+
if (!ctx || !(0, import_api2.isSpanContextValid)(ctx)) return void 0;
|
|
476
|
+
return ctx.traceId;
|
|
477
|
+
} catch {
|
|
478
|
+
return void 0;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
var ScoresClient = class _ScoresClient {
|
|
482
|
+
host;
|
|
483
|
+
publicKey;
|
|
484
|
+
secretKey;
|
|
485
|
+
timeoutSeconds;
|
|
486
|
+
debug;
|
|
487
|
+
constructor(config) {
|
|
488
|
+
this.host = config.host.replace(/\/$/, "");
|
|
489
|
+
this.publicKey = config.publicKey;
|
|
490
|
+
this.secretKey = config.secretKey;
|
|
491
|
+
this.timeoutSeconds = config.timeoutSeconds ?? 15;
|
|
492
|
+
this.debug = config.debug ?? false;
|
|
493
|
+
}
|
|
494
|
+
/** Create a client from GATEWAY_HOST / GATEWAY_PUBLIC_KEY / GATEWAY_SECRET_KEY. */
|
|
495
|
+
static fromEnv(config = {}) {
|
|
496
|
+
const host = config.host ?? process.env.GATEWAY_HOST;
|
|
497
|
+
const publicKey = config.publicKey ?? process.env.GATEWAY_PUBLIC_KEY;
|
|
498
|
+
const secretKey = config.secretKey ?? process.env.GATEWAY_SECRET_KEY;
|
|
499
|
+
const missing = [
|
|
500
|
+
["host", host],
|
|
501
|
+
["publicKey", publicKey],
|
|
502
|
+
["secretKey", secretKey]
|
|
503
|
+
].filter(([, value]) => !value).map(([name]) => name);
|
|
504
|
+
if (missing.length > 0) {
|
|
505
|
+
throw new ScoreError(
|
|
506
|
+
`Missing Gateway scores configuration: ${missing.join(", ")}`
|
|
507
|
+
);
|
|
508
|
+
}
|
|
509
|
+
return new _ScoresClient({
|
|
510
|
+
...config,
|
|
511
|
+
host,
|
|
512
|
+
publicKey,
|
|
513
|
+
secretKey
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Post a score, throwing {@link ScoreError} on any failure. Builds the exact
|
|
518
|
+
* `POST /api/public/scores` body the Python SDK sends: exactly one of
|
|
519
|
+
* `sessionId` / `traceId` (+ optional `observationId`) / `datasetRunId`.
|
|
520
|
+
* When no target is provided, the active OTel trace id is used; if none is
|
|
521
|
+
* available the call is a no-op (returns without posting), matching Python.
|
|
522
|
+
*/
|
|
523
|
+
async postScore(name, value, options = {}) {
|
|
524
|
+
const body = this.buildScoreBody(name, value, options);
|
|
525
|
+
if (!body) {
|
|
526
|
+
this.log(
|
|
527
|
+
"score '%s' dropped: no sessionId, traceId or datasetRunId available",
|
|
528
|
+
name
|
|
529
|
+
);
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
await this.request("POST", "/api/public/scores", body);
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Build the score request body, or null when no target can be resolved.
|
|
536
|
+
* Split out so it is unit-testable and shared by score()/success().
|
|
537
|
+
*/
|
|
538
|
+
buildScoreBody(name, value, options = {}) {
|
|
539
|
+
const traceId = options.traceId ?? currentTraceId();
|
|
540
|
+
const target = {};
|
|
541
|
+
if (options.sessionId) {
|
|
542
|
+
target.sessionId = options.sessionId;
|
|
543
|
+
} else if (options.datasetRunId) {
|
|
544
|
+
target.datasetRunId = options.datasetRunId;
|
|
545
|
+
} else if (traceId) {
|
|
546
|
+
target.traceId = traceId;
|
|
547
|
+
if (options.observationId) target.observationId = options.observationId;
|
|
548
|
+
} else {
|
|
549
|
+
return null;
|
|
550
|
+
}
|
|
551
|
+
const metadata = {
|
|
552
|
+
source: "gatewaysdk",
|
|
553
|
+
...options.metadata ?? {}
|
|
554
|
+
};
|
|
555
|
+
return {
|
|
556
|
+
name,
|
|
557
|
+
value,
|
|
558
|
+
dataType: options.dataType ?? "NUMERIC",
|
|
559
|
+
environment: options.environment ?? "default",
|
|
560
|
+
...options.comment ? { comment: options.comment } : {},
|
|
561
|
+
metadata,
|
|
562
|
+
...target
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Post a score, FAIL-SOFT: warns and resolves on any failure so a broken
|
|
567
|
+
* transport never throws into (or rejects out of) the caller.
|
|
568
|
+
*/
|
|
569
|
+
async score(name, value, options = {}) {
|
|
570
|
+
try {
|
|
571
|
+
await this.postScore(name, value, options);
|
|
572
|
+
} catch (error) {
|
|
573
|
+
this.warn(`score '${name}' failed`, error);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Emit a SUCCESS SIGNAL that feeds a Gateway Success Metric — a score tagged
|
|
578
|
+
* with `gateway.success.signal` + `gateway.success.metricName` (and the agent
|
|
579
|
+
* identity when `agentId` is set). A boolean `value` records a 1/0 BOOLEAN
|
|
580
|
+
* score (pass/fail); a number records a NUMERIC score. FAIL-SOFT.
|
|
581
|
+
*
|
|
582
|
+
* The metadata shape matches the Python SDK's `Run.success()` exactly, so the
|
|
583
|
+
* platform's Success Metrics aggregation counts signals from either SDK
|
|
584
|
+
* without change.
|
|
585
|
+
*
|
|
586
|
+
* @example
|
|
587
|
+
* ```ts
|
|
588
|
+
* await scores.success("resolved_ticket", true, { agentId: "support-triage", sessionId });
|
|
589
|
+
* await scores.success("handle_time_s", 42, { agentId: "support-triage", sessionId });
|
|
590
|
+
* ```
|
|
591
|
+
*/
|
|
592
|
+
async success(name, value = true, options = {}) {
|
|
593
|
+
const { numericValue, inferredType } = coerceSignalValue(value);
|
|
594
|
+
const { agentId, metricName, metadata, dataType, ...rest } = options;
|
|
595
|
+
const signalMetadata = {
|
|
596
|
+
...metadata ?? {},
|
|
597
|
+
[SUCCESS_SIGNAL_KEY]: true,
|
|
598
|
+
[SUCCESS_METRIC_NAME_KEY]: metricName ?? name,
|
|
599
|
+
...agentId ? { [AGENT_ID_KEY]: agentId } : {}
|
|
600
|
+
};
|
|
601
|
+
await this.score(name, numericValue, {
|
|
602
|
+
...rest,
|
|
603
|
+
dataType: dataType ?? inferredType,
|
|
604
|
+
metadata: signalMetadata
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
async request(method, path, payload) {
|
|
608
|
+
let response;
|
|
609
|
+
try {
|
|
610
|
+
response = await fetch(`${this.host}${path}`, {
|
|
611
|
+
method,
|
|
612
|
+
headers: {
|
|
613
|
+
Authorization: this.authHeader(),
|
|
614
|
+
"Content-Type": "application/json"
|
|
615
|
+
},
|
|
616
|
+
body: JSON.stringify(payload),
|
|
617
|
+
signal: AbortSignal.timeout(this.timeoutSeconds * 1e3)
|
|
618
|
+
});
|
|
619
|
+
} catch (error) {
|
|
620
|
+
throw new ScoreError(
|
|
621
|
+
`${method} ${path} failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
622
|
+
{ cause: error }
|
|
623
|
+
);
|
|
624
|
+
}
|
|
625
|
+
const text = await response.text();
|
|
626
|
+
if (!response.ok) {
|
|
627
|
+
throw new ScoreError(
|
|
628
|
+
`${method} ${path} failed (${response.status}): ${text.slice(0, 500)}`
|
|
629
|
+
);
|
|
630
|
+
}
|
|
631
|
+
this.log("%s %s -> %s", method, path, response.status);
|
|
632
|
+
}
|
|
633
|
+
authHeader() {
|
|
634
|
+
return `Basic ${Buffer.from(`${this.publicKey}:${this.secretKey}`).toString(
|
|
635
|
+
"base64"
|
|
636
|
+
)}`;
|
|
637
|
+
}
|
|
638
|
+
log(message, ...args) {
|
|
639
|
+
if (this.debug) {
|
|
640
|
+
console.log(`[gateway.scores] ${message}`, ...args);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
warn(message, ...args) {
|
|
644
|
+
console.warn(`[gateway.scores] ${message}`, ...args);
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
function coerceSignalValue(value) {
|
|
648
|
+
if (typeof value === "boolean") {
|
|
649
|
+
return { numericValue: value ? 1 : 0, inferredType: "BOOLEAN" };
|
|
650
|
+
}
|
|
651
|
+
return { numericValue: value, inferredType: "NUMERIC" };
|
|
652
|
+
}
|
|
653
|
+
async function success(name, value = true, options = {}) {
|
|
654
|
+
let client;
|
|
655
|
+
try {
|
|
656
|
+
client = ScoresClient.fromEnv();
|
|
657
|
+
} catch (error) {
|
|
658
|
+
console.warn(`[gateway.scores] success '${name}' skipped`, error);
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
await client.success(name, value, options);
|
|
662
|
+
}
|
|
663
|
+
var scores = {
|
|
664
|
+
fromEnv: ScoresClient.fromEnv,
|
|
665
|
+
success
|
|
666
|
+
};
|
|
667
|
+
|
|
453
668
|
// src/build/index.ts
|
|
454
669
|
var import_node_crypto = require("crypto");
|
|
455
670
|
function canonicalJson(value) {
|
|
@@ -854,12 +1069,19 @@ function initIsolated(config = {}) {
|
|
|
854
1069
|
const provider = built.provider;
|
|
855
1070
|
scheduleBuildRegistration(config, built);
|
|
856
1071
|
const tracer = provider.getTracer("@withgateway/sdk");
|
|
1072
|
+
const scores2 = new ScoresClient({
|
|
1073
|
+
host: built.connection.host,
|
|
1074
|
+
publicKey: built.connection.publicKey,
|
|
1075
|
+
secretKey: built.connection.secretKey,
|
|
1076
|
+
debug: config.debug ?? false
|
|
1077
|
+
});
|
|
857
1078
|
debugLog2(
|
|
858
1079
|
"Isolated tracing provider created (service=%s)",
|
|
859
1080
|
config.serviceName ?? "gatewaysdk-tracing"
|
|
860
1081
|
);
|
|
861
1082
|
return {
|
|
862
1083
|
tracer,
|
|
1084
|
+
scores: scores2,
|
|
863
1085
|
async flush() {
|
|
864
1086
|
try {
|
|
865
1087
|
await provider.forceFlush();
|
|
@@ -913,12 +1135,12 @@ function isGatewayConfigured() {
|
|
|
913
1135
|
}
|
|
914
1136
|
|
|
915
1137
|
// src/tracing/observe.ts
|
|
916
|
-
var
|
|
1138
|
+
var import_api3 = require("@opentelemetry/api");
|
|
917
1139
|
function asAttr(value) {
|
|
918
1140
|
return typeof value === "string" ? value : JSON.stringify(value);
|
|
919
1141
|
}
|
|
920
1142
|
async function observe(name, fn, options = {}) {
|
|
921
|
-
const tracer = options.tracer ??
|
|
1143
|
+
const tracer = options.tracer ?? import_api3.trace.getTracer("@withgateway/sdk");
|
|
922
1144
|
const span = tracer.startSpan(name, {
|
|
923
1145
|
attributes: {
|
|
924
1146
|
"openinference.span.kind": options.kind ?? "AGENT",
|
|
@@ -927,18 +1149,18 @@ async function observe(name, fn, options = {}) {
|
|
|
927
1149
|
}
|
|
928
1150
|
});
|
|
929
1151
|
try {
|
|
930
|
-
const result = await
|
|
931
|
-
|
|
1152
|
+
const result = await import_api3.context.with(
|
|
1153
|
+
import_api3.trace.setSpan(import_api3.context.active(), span),
|
|
932
1154
|
fn
|
|
933
1155
|
);
|
|
934
1156
|
if (options.captureOutput !== false && result !== void 0) {
|
|
935
1157
|
span.setAttribute("output.value", asAttr(result));
|
|
936
1158
|
}
|
|
937
|
-
span.setStatus({ code:
|
|
1159
|
+
span.setStatus({ code: import_api3.SpanStatusCode.OK });
|
|
938
1160
|
return result;
|
|
939
1161
|
} catch (error) {
|
|
940
1162
|
span.setStatus({
|
|
941
|
-
code:
|
|
1163
|
+
code: import_api3.SpanStatusCode.ERROR,
|
|
942
1164
|
message: error instanceof Error ? error.message : String(error)
|
|
943
1165
|
});
|
|
944
1166
|
throw error;
|
|
@@ -985,7 +1207,7 @@ function observeAnthropic(client) {
|
|
|
985
1207
|
}
|
|
986
1208
|
|
|
987
1209
|
// src/tracing/claude.ts
|
|
988
|
-
var
|
|
1210
|
+
var import_api4 = require("@opentelemetry/api");
|
|
989
1211
|
function cleanToolName(name) {
|
|
990
1212
|
return name.replace(/^mcp__[^_]+__/, "");
|
|
991
1213
|
}
|
|
@@ -999,7 +1221,7 @@ function toolResultText(content) {
|
|
|
999
1221
|
}
|
|
1000
1222
|
var TURN_INPUT_MAX = 4e3;
|
|
1001
1223
|
async function* observeClaude(stream, options) {
|
|
1002
|
-
const tracer = options.tracer ??
|
|
1224
|
+
const tracer = options.tracer ?? import_api4.trace.getTracer("@withgateway/sdk");
|
|
1003
1225
|
const model = options.model ?? "claude";
|
|
1004
1226
|
const openTools = /* @__PURE__ */ new Map();
|
|
1005
1227
|
let root = null;
|
|
@@ -1012,7 +1234,7 @@ async function* observeClaude(stream, options) {
|
|
|
1012
1234
|
...options.attributes ?? {}
|
|
1013
1235
|
}
|
|
1014
1236
|
});
|
|
1015
|
-
rootCtx =
|
|
1237
|
+
rootCtx = import_api4.trace.setSpan(import_api4.context.active(), root);
|
|
1016
1238
|
}
|
|
1017
1239
|
let turnRoot = null;
|
|
1018
1240
|
let turnCtx = null;
|
|
@@ -1023,7 +1245,7 @@ async function* observeClaude(stream, options) {
|
|
|
1023
1245
|
const endTurn = () => {
|
|
1024
1246
|
if (!turnRoot) return;
|
|
1025
1247
|
if (turnText) turnRoot.setAttribute("output.value", turnText);
|
|
1026
|
-
turnRoot.setStatus({ code:
|
|
1248
|
+
turnRoot.setStatus({ code: import_api4.SpanStatusCode.OK });
|
|
1027
1249
|
turnRoot.end();
|
|
1028
1250
|
turnRoot = null;
|
|
1029
1251
|
turnCtx = null;
|
|
@@ -1045,7 +1267,7 @@ async function* observeClaude(stream, options) {
|
|
|
1045
1267
|
...options.attributes ?? {}
|
|
1046
1268
|
}
|
|
1047
1269
|
});
|
|
1048
|
-
turnCtx =
|
|
1270
|
+
turnCtx = import_api4.trace.setSpan(import_api4.context.active(), turnRoot);
|
|
1049
1271
|
pendingTurnInput = "";
|
|
1050
1272
|
}
|
|
1051
1273
|
const childCtx = options.perTurn ? turnCtx : rootCtx;
|
|
@@ -1107,7 +1329,7 @@ async function* observeClaude(stream, options) {
|
|
|
1107
1329
|
const span = openTools.get(block.tool_use_id);
|
|
1108
1330
|
if (span) {
|
|
1109
1331
|
span.setAttribute("output.value", resultText);
|
|
1110
|
-
span.setStatus({ code:
|
|
1332
|
+
span.setStatus({ code: import_api4.SpanStatusCode.OK });
|
|
1111
1333
|
span.end();
|
|
1112
1334
|
openTools.delete(block.tool_use_id);
|
|
1113
1335
|
}
|
|
@@ -1127,7 +1349,7 @@ async function* observeClaude(stream, options) {
|
|
|
1127
1349
|
endTurn();
|
|
1128
1350
|
} else if (root) {
|
|
1129
1351
|
root.setAttribute("output.value", finalText);
|
|
1130
|
-
root.setStatus({ code:
|
|
1352
|
+
root.setStatus({ code: import_api4.SpanStatusCode.OK });
|
|
1131
1353
|
root.end();
|
|
1132
1354
|
}
|
|
1133
1355
|
}
|