@withgateway/sdk 0.1.3 → 0.1.4

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/dist/index.js CHANGED
@@ -87,14 +87,14 @@ var experimentContextStorage = new import_async_hooks.AsyncLocalStorage();
87
87
  function getExperimentContext() {
88
88
  return experimentContextStorage.getStore();
89
89
  }
90
- function setExperimentContext(context2) {
91
- experimentContextStorage.enterWith(context2);
90
+ function setExperimentContext(context4) {
91
+ experimentContextStorage.enterWith(context4);
92
92
  }
93
93
  function clearExperimentContext() {
94
94
  experimentContextStorage.enterWith(void 0);
95
95
  }
96
- function runWithExperimentContext(context2, fn) {
97
- return experimentContextStorage.run(context2, fn);
96
+ function runWithExperimentContext(context4, fn) {
97
+ return experimentContextStorage.run(context4, fn);
98
98
  }
99
99
 
100
100
  // src/experiments/exposure-logger.ts
@@ -274,17 +274,17 @@ var ExperimentClient = class {
274
274
  variantKey: variant.key,
275
275
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
276
276
  });
277
- const context2 = {
277
+ const context4 = {
278
278
  experimentId: experimentKey,
279
279
  variantKey: variant.key,
280
280
  userId
281
281
  };
282
- setExperimentContext(context2);
282
+ setExperimentContext(context4);
283
283
  return {
284
284
  variant,
285
285
  inExperiment: true,
286
286
  experimentKey,
287
- run: (fn) => runWithExperimentContext(context2, fn)
287
+ run: (fn) => runWithExperimentContext(context4, fn)
288
288
  };
289
289
  }
290
290
  getConfig(experimentKey) {
@@ -1187,10 +1187,10 @@ var GatewaySecurity = class _GatewaySecurity {
1187
1187
  if (resolve.blockedToolIds.includes(toolId)) return false;
1188
1188
  return true;
1189
1189
  }
1190
- normalizeContext(context2) {
1190
+ normalizeContext(context4) {
1191
1191
  return {
1192
- ...context2 ?? {},
1193
- metadata: context2?.metadata ?? {}
1192
+ ...context4 ?? {},
1193
+ metadata: context4?.metadata ?? {}
1194
1194
  };
1195
1195
  }
1196
1196
  authHeader() {
@@ -1234,6 +1234,10 @@ __export(tracing_exports, {
1234
1234
  init: () => init2,
1235
1235
  isGatewayConfigured: () => isGatewayConfigured,
1236
1236
  isInitialized: () => isInitialized,
1237
+ observe: () => observe,
1238
+ observeAnthropic: () => observeAnthropic,
1239
+ observeClaude: () => observeClaude,
1240
+ observeOpenAI: () => observeOpenAI,
1237
1241
  shutdown: () => shutdown2
1238
1242
  });
1239
1243
 
@@ -1425,6 +1429,176 @@ function isGatewayConfigured() {
1425
1429
  return !!(host && publicKey && secretKey);
1426
1430
  }
1427
1431
 
1432
+ // src/tracing/observe.ts
1433
+ var import_api = require("@opentelemetry/api");
1434
+ function asAttr(value) {
1435
+ return typeof value === "string" ? value : JSON.stringify(value);
1436
+ }
1437
+ async function observe(name, fn, options = {}) {
1438
+ const tracer = import_api.trace.getTracer("@withgateway/sdk");
1439
+ const span = tracer.startSpan(name, {
1440
+ attributes: {
1441
+ "openinference.span.kind": options.kind ?? "AGENT",
1442
+ ...options.input !== void 0 ? { "input.value": asAttr(options.input) } : {}
1443
+ }
1444
+ });
1445
+ try {
1446
+ const result = await import_api.context.with(
1447
+ import_api.trace.setSpan(import_api.context.active(), span),
1448
+ fn
1449
+ );
1450
+ if (options.captureOutput !== false && result !== void 0) {
1451
+ span.setAttribute("output.value", asAttr(result));
1452
+ }
1453
+ span.setStatus({ code: import_api.SpanStatusCode.OK });
1454
+ return result;
1455
+ } catch (error) {
1456
+ span.setStatus({
1457
+ code: import_api.SpanStatusCode.ERROR,
1458
+ message: error instanceof Error ? error.message : String(error)
1459
+ });
1460
+ throw error;
1461
+ } finally {
1462
+ span.end();
1463
+ }
1464
+ }
1465
+
1466
+ // src/tracing/instrument.ts
1467
+ function warn2(message, ...args) {
1468
+ console.warn(`[gatewaysdk.tracing] ${message}`, ...args);
1469
+ }
1470
+ function observeOpenAI(client2) {
1471
+ try {
1472
+ const {
1473
+ OpenAIInstrumentation
1474
+ } = require("@arizeai/openinference-instrumentation-openai");
1475
+ const OpenAIClass = client2.constructor;
1476
+ new OpenAIInstrumentation().manuallyInstrument({ OpenAI: OpenAIClass });
1477
+ } catch (error) {
1478
+ warn2(
1479
+ "observeOpenAI: could not load @arizeai/openinference-instrumentation-openai \u2014 OpenAI calls will not be traced.",
1480
+ error
1481
+ );
1482
+ }
1483
+ return client2;
1484
+ }
1485
+ function observeAnthropic(client2) {
1486
+ try {
1487
+ const {
1488
+ AnthropicInstrumentation
1489
+ } = require("@arizeai/openinference-instrumentation-anthropic");
1490
+ const AnthropicClass = client2.constructor;
1491
+ new AnthropicInstrumentation().manuallyInstrument({
1492
+ Messages: AnthropicClass.Messages
1493
+ });
1494
+ } catch (error) {
1495
+ warn2(
1496
+ "observeAnthropic: could not load @arizeai/openinference-instrumentation-anthropic \u2014 Anthropic calls will not be traced.",
1497
+ error
1498
+ );
1499
+ }
1500
+ return client2;
1501
+ }
1502
+
1503
+ // src/tracing/claude.ts
1504
+ var import_api2 = require("@opentelemetry/api");
1505
+ function cleanToolName(name) {
1506
+ return name.replace(/^mcp__[^_]+__/, "");
1507
+ }
1508
+ function toolResultText(content) {
1509
+ if (typeof content === "string") return content;
1510
+ if (Array.isArray(content))
1511
+ return content.map(
1512
+ (b) => b && typeof b === "object" && "text" in b ? String(b.text) : JSON.stringify(b)
1513
+ ).join("");
1514
+ return JSON.stringify(content ?? "");
1515
+ }
1516
+ async function* observeClaude(stream, options) {
1517
+ const tracer = import_api2.trace.getTracer("@withgateway/sdk");
1518
+ const root = tracer.startSpan(options.name, {
1519
+ attributes: {
1520
+ "openinference.span.kind": "AGENT",
1521
+ ...options.input !== void 0 ? { "input.value": options.input } : {}
1522
+ }
1523
+ });
1524
+ const rootCtx = import_api2.trace.setSpan(import_api2.context.active(), root);
1525
+ const openTools = /* @__PURE__ */ new Map();
1526
+ const model = options.model ?? "claude";
1527
+ let finalText = "";
1528
+ try {
1529
+ for await (const message of stream) {
1530
+ if (message?.type === "assistant") {
1531
+ let turnText = "";
1532
+ const toolUses = [];
1533
+ for (const block of message.message?.content ?? []) {
1534
+ if (block?.type === "text") {
1535
+ finalText += block.text;
1536
+ turnText += block.text;
1537
+ }
1538
+ if (block?.type === "tool_use") {
1539
+ toolUses.push({ id: block.id, name: block.name, input: block.input });
1540
+ }
1541
+ }
1542
+ const gen = tracer.startSpan(
1543
+ "assistant",
1544
+ {
1545
+ attributes: {
1546
+ "openinference.span.kind": "LLM",
1547
+ "llm.model_name": model,
1548
+ "llm.provider": "anthropic",
1549
+ "output.value": turnText || toolUses.map((t) => `\u2192 ${cleanToolName(t.name)}`).join(", ")
1550
+ }
1551
+ },
1552
+ rootCtx
1553
+ );
1554
+ const usage = message.message?.usage;
1555
+ if (usage?.input_tokens != null)
1556
+ gen.setAttribute("llm.token_count.prompt", usage.input_tokens);
1557
+ if (usage?.output_tokens != null)
1558
+ gen.setAttribute("llm.token_count.completion", usage.output_tokens);
1559
+ gen.end();
1560
+ for (const t of toolUses) {
1561
+ const span = tracer.startSpan(
1562
+ `tool.${cleanToolName(t.name)}`,
1563
+ {
1564
+ attributes: {
1565
+ "openinference.span.kind": "TOOL",
1566
+ "tool.name": cleanToolName(t.name),
1567
+ "input.value": JSON.stringify(t.input)
1568
+ }
1569
+ },
1570
+ rootCtx
1571
+ );
1572
+ openTools.set(t.id, span);
1573
+ }
1574
+ }
1575
+ if (message?.type === "user") {
1576
+ const content = message.message?.content;
1577
+ if (Array.isArray(content)) {
1578
+ for (const block of content) {
1579
+ if (block?.type === "tool_result") {
1580
+ const span = openTools.get(block.tool_use_id);
1581
+ if (span) {
1582
+ span.setAttribute("output.value", toolResultText(block.content));
1583
+ span.setStatus({ code: import_api2.SpanStatusCode.OK });
1584
+ span.end();
1585
+ openTools.delete(block.tool_use_id);
1586
+ }
1587
+ }
1588
+ }
1589
+ }
1590
+ }
1591
+ yield message;
1592
+ }
1593
+ } finally {
1594
+ for (const span of openTools.values()) span.end();
1595
+ openTools.clear();
1596
+ root.setAttribute("output.value", finalText);
1597
+ root.setStatus({ code: import_api2.SpanStatusCode.OK });
1598
+ root.end();
1599
+ }
1600
+ }
1601
+
1428
1602
  // src/index.ts
1429
1603
  var tracing = {
1430
1604
  ...tracing_exports,