@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/context/index.mjs +3 -0
- package/dist/context/index.mjs.map +1 -1
- package/dist/experiments/index.mjs +2 -0
- package/dist/experiments/index.mjs.map +1 -1
- package/dist/{index-Caw3XVub.d.mts → index-yJgb8wh6.d.mts} +82 -2
- package/dist/{index-Caw3XVub.d.ts → index-yJgb8wh6.d.ts} +82 -2
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +184 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +190 -10
- package/dist/index.mjs.map +1 -1
- package/dist/memory/index.mjs +3 -0
- package/dist/memory/index.mjs.map +1 -1
- package/dist/security/index.mjs +2 -0
- package/dist/security/index.mjs.map +1 -1
- package/dist/tracing/index.d.mts +1 -1
- package/dist/tracing/index.d.ts +1 -1
- package/dist/tracing/index.js +178 -0
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/index.mjs +180 -0
- package/dist/tracing/index.mjs.map +1 -1
- package/package.json +5 -1
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { createRequire as __gatewayCreateRequire } from "module";
|
|
2
|
+
const require = __gatewayCreateRequire(import.meta.url);
|
|
1
3
|
var __defProp = Object.defineProperty;
|
|
2
4
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
3
5
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
@@ -46,14 +48,14 @@ var experimentContextStorage = new AsyncLocalStorage();
|
|
|
46
48
|
function getExperimentContext() {
|
|
47
49
|
return experimentContextStorage.getStore();
|
|
48
50
|
}
|
|
49
|
-
function setExperimentContext(
|
|
50
|
-
experimentContextStorage.enterWith(
|
|
51
|
+
function setExperimentContext(context4) {
|
|
52
|
+
experimentContextStorage.enterWith(context4);
|
|
51
53
|
}
|
|
52
54
|
function clearExperimentContext() {
|
|
53
55
|
experimentContextStorage.enterWith(void 0);
|
|
54
56
|
}
|
|
55
|
-
function runWithExperimentContext(
|
|
56
|
-
return experimentContextStorage.run(
|
|
57
|
+
function runWithExperimentContext(context4, fn) {
|
|
58
|
+
return experimentContextStorage.run(context4, fn);
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
// src/experiments/exposure-logger.ts
|
|
@@ -233,17 +235,17 @@ var ExperimentClient = class {
|
|
|
233
235
|
variantKey: variant.key,
|
|
234
236
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
235
237
|
});
|
|
236
|
-
const
|
|
238
|
+
const context4 = {
|
|
237
239
|
experimentId: experimentKey,
|
|
238
240
|
variantKey: variant.key,
|
|
239
241
|
userId
|
|
240
242
|
};
|
|
241
|
-
setExperimentContext(
|
|
243
|
+
setExperimentContext(context4);
|
|
242
244
|
return {
|
|
243
245
|
variant,
|
|
244
246
|
inExperiment: true,
|
|
245
247
|
experimentKey,
|
|
246
|
-
run: (fn) => runWithExperimentContext(
|
|
248
|
+
run: (fn) => runWithExperimentContext(context4, fn)
|
|
247
249
|
};
|
|
248
250
|
}
|
|
249
251
|
getConfig(experimentKey) {
|
|
@@ -1146,10 +1148,10 @@ var GatewaySecurity = class _GatewaySecurity {
|
|
|
1146
1148
|
if (resolve.blockedToolIds.includes(toolId)) return false;
|
|
1147
1149
|
return true;
|
|
1148
1150
|
}
|
|
1149
|
-
normalizeContext(
|
|
1151
|
+
normalizeContext(context4) {
|
|
1150
1152
|
return {
|
|
1151
|
-
...
|
|
1152
|
-
metadata:
|
|
1153
|
+
...context4 ?? {},
|
|
1154
|
+
metadata: context4?.metadata ?? {}
|
|
1153
1155
|
};
|
|
1154
1156
|
}
|
|
1155
1157
|
authHeader() {
|
|
@@ -1193,6 +1195,10 @@ __export(tracing_exports, {
|
|
|
1193
1195
|
init: () => init2,
|
|
1194
1196
|
isGatewayConfigured: () => isGatewayConfigured,
|
|
1195
1197
|
isInitialized: () => isInitialized,
|
|
1198
|
+
observe: () => observe,
|
|
1199
|
+
observeAnthropic: () => observeAnthropic,
|
|
1200
|
+
observeClaude: () => observeClaude,
|
|
1201
|
+
observeOpenAI: () => observeOpenAI,
|
|
1196
1202
|
shutdown: () => shutdown2
|
|
1197
1203
|
});
|
|
1198
1204
|
|
|
@@ -1384,6 +1390,180 @@ function isGatewayConfigured() {
|
|
|
1384
1390
|
return !!(host && publicKey && secretKey);
|
|
1385
1391
|
}
|
|
1386
1392
|
|
|
1393
|
+
// src/tracing/observe.ts
|
|
1394
|
+
import { trace, context as context2, SpanStatusCode } from "@opentelemetry/api";
|
|
1395
|
+
function asAttr(value) {
|
|
1396
|
+
return typeof value === "string" ? value : JSON.stringify(value);
|
|
1397
|
+
}
|
|
1398
|
+
async function observe(name, fn, options = {}) {
|
|
1399
|
+
const tracer = trace.getTracer("@withgateway/sdk");
|
|
1400
|
+
const span = tracer.startSpan(name, {
|
|
1401
|
+
attributes: {
|
|
1402
|
+
"openinference.span.kind": options.kind ?? "AGENT",
|
|
1403
|
+
...options.input !== void 0 ? { "input.value": asAttr(options.input) } : {}
|
|
1404
|
+
}
|
|
1405
|
+
});
|
|
1406
|
+
try {
|
|
1407
|
+
const result = await context2.with(
|
|
1408
|
+
trace.setSpan(context2.active(), span),
|
|
1409
|
+
fn
|
|
1410
|
+
);
|
|
1411
|
+
if (options.captureOutput !== false && result !== void 0) {
|
|
1412
|
+
span.setAttribute("output.value", asAttr(result));
|
|
1413
|
+
}
|
|
1414
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
1415
|
+
return result;
|
|
1416
|
+
} catch (error) {
|
|
1417
|
+
span.setStatus({
|
|
1418
|
+
code: SpanStatusCode.ERROR,
|
|
1419
|
+
message: error instanceof Error ? error.message : String(error)
|
|
1420
|
+
});
|
|
1421
|
+
throw error;
|
|
1422
|
+
} finally {
|
|
1423
|
+
span.end();
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
// src/tracing/instrument.ts
|
|
1428
|
+
function warn2(message, ...args) {
|
|
1429
|
+
console.warn(`[gatewaysdk.tracing] ${message}`, ...args);
|
|
1430
|
+
}
|
|
1431
|
+
function observeOpenAI(client2) {
|
|
1432
|
+
try {
|
|
1433
|
+
const {
|
|
1434
|
+
OpenAIInstrumentation
|
|
1435
|
+
} = __require("@arizeai/openinference-instrumentation-openai");
|
|
1436
|
+
const OpenAIClass = client2.constructor;
|
|
1437
|
+
new OpenAIInstrumentation().manuallyInstrument({ OpenAI: OpenAIClass });
|
|
1438
|
+
} catch (error) {
|
|
1439
|
+
warn2(
|
|
1440
|
+
"observeOpenAI: could not load @arizeai/openinference-instrumentation-openai \u2014 OpenAI calls will not be traced.",
|
|
1441
|
+
error
|
|
1442
|
+
);
|
|
1443
|
+
}
|
|
1444
|
+
return client2;
|
|
1445
|
+
}
|
|
1446
|
+
function observeAnthropic(client2) {
|
|
1447
|
+
try {
|
|
1448
|
+
const {
|
|
1449
|
+
AnthropicInstrumentation
|
|
1450
|
+
} = __require("@arizeai/openinference-instrumentation-anthropic");
|
|
1451
|
+
const AnthropicClass = client2.constructor;
|
|
1452
|
+
new AnthropicInstrumentation().manuallyInstrument({
|
|
1453
|
+
Messages: AnthropicClass.Messages
|
|
1454
|
+
});
|
|
1455
|
+
} catch (error) {
|
|
1456
|
+
warn2(
|
|
1457
|
+
"observeAnthropic: could not load @arizeai/openinference-instrumentation-anthropic \u2014 Anthropic calls will not be traced.",
|
|
1458
|
+
error
|
|
1459
|
+
);
|
|
1460
|
+
}
|
|
1461
|
+
return client2;
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
// src/tracing/claude.ts
|
|
1465
|
+
import {
|
|
1466
|
+
trace as trace2,
|
|
1467
|
+
context as context3,
|
|
1468
|
+
SpanStatusCode as SpanStatusCode2
|
|
1469
|
+
} from "@opentelemetry/api";
|
|
1470
|
+
function cleanToolName(name) {
|
|
1471
|
+
return name.replace(/^mcp__[^_]+__/, "");
|
|
1472
|
+
}
|
|
1473
|
+
function toolResultText(content) {
|
|
1474
|
+
if (typeof content === "string") return content;
|
|
1475
|
+
if (Array.isArray(content))
|
|
1476
|
+
return content.map(
|
|
1477
|
+
(b) => b && typeof b === "object" && "text" in b ? String(b.text) : JSON.stringify(b)
|
|
1478
|
+
).join("");
|
|
1479
|
+
return JSON.stringify(content ?? "");
|
|
1480
|
+
}
|
|
1481
|
+
async function* observeClaude(stream, options) {
|
|
1482
|
+
const tracer = trace2.getTracer("@withgateway/sdk");
|
|
1483
|
+
const root = tracer.startSpan(options.name, {
|
|
1484
|
+
attributes: {
|
|
1485
|
+
"openinference.span.kind": "AGENT",
|
|
1486
|
+
...options.input !== void 0 ? { "input.value": options.input } : {}
|
|
1487
|
+
}
|
|
1488
|
+
});
|
|
1489
|
+
const rootCtx = trace2.setSpan(context3.active(), root);
|
|
1490
|
+
const openTools = /* @__PURE__ */ new Map();
|
|
1491
|
+
const model = options.model ?? "claude";
|
|
1492
|
+
let finalText = "";
|
|
1493
|
+
try {
|
|
1494
|
+
for await (const message of stream) {
|
|
1495
|
+
if (message?.type === "assistant") {
|
|
1496
|
+
let turnText = "";
|
|
1497
|
+
const toolUses = [];
|
|
1498
|
+
for (const block of message.message?.content ?? []) {
|
|
1499
|
+
if (block?.type === "text") {
|
|
1500
|
+
finalText += block.text;
|
|
1501
|
+
turnText += block.text;
|
|
1502
|
+
}
|
|
1503
|
+
if (block?.type === "tool_use") {
|
|
1504
|
+
toolUses.push({ id: block.id, name: block.name, input: block.input });
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
const gen = tracer.startSpan(
|
|
1508
|
+
"assistant",
|
|
1509
|
+
{
|
|
1510
|
+
attributes: {
|
|
1511
|
+
"openinference.span.kind": "LLM",
|
|
1512
|
+
"llm.model_name": model,
|
|
1513
|
+
"llm.provider": "anthropic",
|
|
1514
|
+
"output.value": turnText || toolUses.map((t) => `\u2192 ${cleanToolName(t.name)}`).join(", ")
|
|
1515
|
+
}
|
|
1516
|
+
},
|
|
1517
|
+
rootCtx
|
|
1518
|
+
);
|
|
1519
|
+
const usage = message.message?.usage;
|
|
1520
|
+
if (usage?.input_tokens != null)
|
|
1521
|
+
gen.setAttribute("llm.token_count.prompt", usage.input_tokens);
|
|
1522
|
+
if (usage?.output_tokens != null)
|
|
1523
|
+
gen.setAttribute("llm.token_count.completion", usage.output_tokens);
|
|
1524
|
+
gen.end();
|
|
1525
|
+
for (const t of toolUses) {
|
|
1526
|
+
const span = tracer.startSpan(
|
|
1527
|
+
`tool.${cleanToolName(t.name)}`,
|
|
1528
|
+
{
|
|
1529
|
+
attributes: {
|
|
1530
|
+
"openinference.span.kind": "TOOL",
|
|
1531
|
+
"tool.name": cleanToolName(t.name),
|
|
1532
|
+
"input.value": JSON.stringify(t.input)
|
|
1533
|
+
}
|
|
1534
|
+
},
|
|
1535
|
+
rootCtx
|
|
1536
|
+
);
|
|
1537
|
+
openTools.set(t.id, span);
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
if (message?.type === "user") {
|
|
1541
|
+
const content = message.message?.content;
|
|
1542
|
+
if (Array.isArray(content)) {
|
|
1543
|
+
for (const block of content) {
|
|
1544
|
+
if (block?.type === "tool_result") {
|
|
1545
|
+
const span = openTools.get(block.tool_use_id);
|
|
1546
|
+
if (span) {
|
|
1547
|
+
span.setAttribute("output.value", toolResultText(block.content));
|
|
1548
|
+
span.setStatus({ code: SpanStatusCode2.OK });
|
|
1549
|
+
span.end();
|
|
1550
|
+
openTools.delete(block.tool_use_id);
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
yield message;
|
|
1557
|
+
}
|
|
1558
|
+
} finally {
|
|
1559
|
+
for (const span of openTools.values()) span.end();
|
|
1560
|
+
openTools.clear();
|
|
1561
|
+
root.setAttribute("output.value", finalText);
|
|
1562
|
+
root.setStatus({ code: SpanStatusCode2.OK });
|
|
1563
|
+
root.end();
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1387
1567
|
// src/index.ts
|
|
1388
1568
|
var tracing = {
|
|
1389
1569
|
...tracing_exports,
|