@withgateway/sdk 0.1.8 → 0.1.10
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-CubrUmOe.d.mts → index-CTc8iOrx.d.mts} +13 -0
- package/dist/{index-CubrUmOe.d.ts → index-CTc8iOrx.d.ts} +13 -0
- package/dist/index.d.mts +59 -3
- package/dist/index.d.ts +59 -3
- package/dist/index.js +359 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +352 -3
- package/dist/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 +303 -3
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/index.mjs +304 -3
- package/dist/tracing/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/tracing/index.mjs
CHANGED
|
@@ -1,11 +1,157 @@
|
|
|
1
1
|
import { createRequire as __gatewayCreateRequire } from "module";
|
|
2
2
|
const require = __gatewayCreateRequire(import.meta.url);
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
4
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
4
5
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
5
6
|
}) : x)(function(x) {
|
|
6
7
|
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
7
8
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
8
9
|
});
|
|
10
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
11
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// package.json
|
|
15
|
+
var require_package = __commonJS({
|
|
16
|
+
"package.json"(exports, module) {
|
|
17
|
+
module.exports = {
|
|
18
|
+
name: "@withgateway/sdk",
|
|
19
|
+
version: "0.1.10",
|
|
20
|
+
description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
|
|
21
|
+
main: "./dist/index.js",
|
|
22
|
+
module: "./dist/index.mjs",
|
|
23
|
+
types: "./dist/index.d.ts",
|
|
24
|
+
exports: {
|
|
25
|
+
".": {
|
|
26
|
+
types: "./dist/index.d.ts",
|
|
27
|
+
import: "./dist/index.mjs",
|
|
28
|
+
require: "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./experiments": {
|
|
31
|
+
types: "./dist/experiments/index.d.ts",
|
|
32
|
+
import: "./dist/experiments/index.mjs",
|
|
33
|
+
require: "./dist/experiments/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./context": {
|
|
36
|
+
types: "./dist/context/index.d.ts",
|
|
37
|
+
import: "./dist/context/index.mjs",
|
|
38
|
+
require: "./dist/context/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./memory": {
|
|
41
|
+
types: "./dist/memory/index.d.ts",
|
|
42
|
+
import: "./dist/memory/index.mjs",
|
|
43
|
+
require: "./dist/memory/index.js"
|
|
44
|
+
},
|
|
45
|
+
"./personas": {
|
|
46
|
+
types: "./dist/personas/index.d.ts",
|
|
47
|
+
import: "./dist/personas/index.mjs",
|
|
48
|
+
require: "./dist/personas/index.js"
|
|
49
|
+
},
|
|
50
|
+
"./replay": {
|
|
51
|
+
types: "./dist/replay/index.d.ts",
|
|
52
|
+
import: "./dist/replay/index.mjs",
|
|
53
|
+
require: "./dist/replay/index.js"
|
|
54
|
+
},
|
|
55
|
+
"./sessions": {
|
|
56
|
+
types: "./dist/sessions/index.d.ts",
|
|
57
|
+
import: "./dist/sessions/index.mjs",
|
|
58
|
+
require: "./dist/sessions/index.js"
|
|
59
|
+
},
|
|
60
|
+
"./tracing": {
|
|
61
|
+
types: "./dist/tracing/index.d.ts",
|
|
62
|
+
import: "./dist/tracing/index.mjs",
|
|
63
|
+
require: "./dist/tracing/index.js"
|
|
64
|
+
},
|
|
65
|
+
"./security": {
|
|
66
|
+
types: "./dist/security/index.d.ts",
|
|
67
|
+
import: "./dist/security/index.mjs",
|
|
68
|
+
require: "./dist/security/index.js"
|
|
69
|
+
},
|
|
70
|
+
"./users": {
|
|
71
|
+
types: "./dist/users/index.d.ts",
|
|
72
|
+
import: "./dist/users/index.mjs",
|
|
73
|
+
require: "./dist/users/index.js"
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
files: [
|
|
77
|
+
"dist",
|
|
78
|
+
"README.md"
|
|
79
|
+
],
|
|
80
|
+
publishConfig: {
|
|
81
|
+
access: "public"
|
|
82
|
+
},
|
|
83
|
+
scripts: {
|
|
84
|
+
build: "tsup",
|
|
85
|
+
dev: "tsup --watch",
|
|
86
|
+
typecheck: "tsc --noEmit",
|
|
87
|
+
test: "vitest",
|
|
88
|
+
"test:run": "vitest run",
|
|
89
|
+
lint: "eslint src --ext .ts",
|
|
90
|
+
clean: "rm -rf dist",
|
|
91
|
+
example: "tsx examples/test-ab-experiment.ts",
|
|
92
|
+
"test:crm": "tsx examples/test-crm-ab-experiment.ts",
|
|
93
|
+
"test:crm:balanced": "tsx examples/test-crm-ab-experiment.ts --balanced",
|
|
94
|
+
"test:crm:full": "tsx examples/test-crm-ab-experiment.ts --users 12 --all-tasks --concurrency 3"
|
|
95
|
+
},
|
|
96
|
+
keywords: [
|
|
97
|
+
"gateway",
|
|
98
|
+
"ab-testing",
|
|
99
|
+
"experiments",
|
|
100
|
+
"opentelemetry",
|
|
101
|
+
"tracing",
|
|
102
|
+
"observability",
|
|
103
|
+
"llm"
|
|
104
|
+
],
|
|
105
|
+
author: "Gateway Team",
|
|
106
|
+
license: "MIT",
|
|
107
|
+
engines: {
|
|
108
|
+
node: ">=18.0.0"
|
|
109
|
+
},
|
|
110
|
+
peerDependencies: {
|
|
111
|
+
"@opentelemetry/api": "^1.0.0",
|
|
112
|
+
"@opentelemetry/exporter-trace-otlp-http": ">=0.50.0",
|
|
113
|
+
"@opentelemetry/resources": ">=1.0.0",
|
|
114
|
+
"@opentelemetry/sdk-trace-base": ">=1.0.0",
|
|
115
|
+
"@opentelemetry/sdk-trace-node": ">=1.0.0"
|
|
116
|
+
},
|
|
117
|
+
peerDependenciesMeta: {
|
|
118
|
+
"@opentelemetry/api": {
|
|
119
|
+
optional: true
|
|
120
|
+
},
|
|
121
|
+
"@opentelemetry/sdk-trace-base": {
|
|
122
|
+
optional: true
|
|
123
|
+
},
|
|
124
|
+
"@opentelemetry/sdk-trace-node": {
|
|
125
|
+
optional: true
|
|
126
|
+
},
|
|
127
|
+
"@opentelemetry/exporter-trace-otlp-http": {
|
|
128
|
+
optional: true
|
|
129
|
+
},
|
|
130
|
+
"@opentelemetry/resources": {
|
|
131
|
+
optional: true
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
optionalDependencies: {
|
|
135
|
+
"@arizeai/openinference-instrumentation-anthropic": "^0.1.15",
|
|
136
|
+
"@arizeai/openinference-instrumentation-openai": "^4.1.5"
|
|
137
|
+
},
|
|
138
|
+
devDependencies: {
|
|
139
|
+
"@anthropic-ai/sdk": "^0.79.0",
|
|
140
|
+
"@opentelemetry/api": "^1.9.0",
|
|
141
|
+
"@opentelemetry/exporter-trace-otlp-http": "^0.212.0",
|
|
142
|
+
"@opentelemetry/resources": "^2.6.0",
|
|
143
|
+
"@opentelemetry/sdk-trace-base": "^1.30.1",
|
|
144
|
+
"@opentelemetry/sdk-trace-node": "^2.6.0",
|
|
145
|
+
"@opentelemetry/semantic-conventions": "^1.40.0",
|
|
146
|
+
"@types/node": "^20.0.0",
|
|
147
|
+
tsup: "^8.0.0",
|
|
148
|
+
tsx: "^4.0.0",
|
|
149
|
+
typescript: "^5.0.0",
|
|
150
|
+
vitest: "^2.0.0"
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
});
|
|
9
155
|
|
|
10
156
|
// src/tracing/identity.ts
|
|
11
157
|
import { AsyncLocalStorage } from "async_hooks";
|
|
@@ -274,6 +420,118 @@ function debugLog(message, ...args) {
|
|
|
274
420
|
}
|
|
275
421
|
}
|
|
276
422
|
|
|
423
|
+
// src/build/index.ts
|
|
424
|
+
import { createHash } from "crypto";
|
|
425
|
+
function canonicalJson(value) {
|
|
426
|
+
if (value === null || typeof value !== "object") {
|
|
427
|
+
return JSON.stringify(value);
|
|
428
|
+
}
|
|
429
|
+
if (Array.isArray(value)) {
|
|
430
|
+
return `[${value.map(canonicalJson).join(",")}]`;
|
|
431
|
+
}
|
|
432
|
+
const entries = Object.entries(value).filter(([, v]) => v !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0);
|
|
433
|
+
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonicalJson(v)}`).join(",")}}`;
|
|
434
|
+
}
|
|
435
|
+
function sha256(text) {
|
|
436
|
+
return `sha256:${createHash("sha256").update(text).digest("hex")}`;
|
|
437
|
+
}
|
|
438
|
+
var registeredTools = /* @__PURE__ */ new Map();
|
|
439
|
+
var registeredPrompts = /* @__PURE__ */ new Map();
|
|
440
|
+
function getRegisteredTools() {
|
|
441
|
+
return [...registeredTools.values()].sort(
|
|
442
|
+
(a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
function getRegisteredPrompts() {
|
|
446
|
+
return Object.fromEntries(
|
|
447
|
+
[...registeredPrompts.entries()].sort(
|
|
448
|
+
([a], [b]) => a < b ? -1 : a > b ? 1 : 0
|
|
449
|
+
)
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
function sdkVersion() {
|
|
453
|
+
try {
|
|
454
|
+
const pkg = require_package();
|
|
455
|
+
return `@withgateway/sdk@${pkg.version ?? "unknown"}`;
|
|
456
|
+
} catch {
|
|
457
|
+
return "@withgateway/sdk";
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
function codeRef() {
|
|
461
|
+
const sha = process.env.GATEWAY_COMMIT ?? process.env.GITHUB_SHA ?? process.env.CI_COMMIT_SHA ?? // GitLab
|
|
462
|
+
process.env.CIRCLE_SHA1;
|
|
463
|
+
if (!sha) return void 0;
|
|
464
|
+
const repo = process.env.GITHUB_REPOSITORY ?? process.env.CI_PROJECT_PATH;
|
|
465
|
+
return repo ? `git:${repo}@${sha}` : `git:${sha}`;
|
|
466
|
+
}
|
|
467
|
+
function assembleManifest(agentId) {
|
|
468
|
+
return {
|
|
469
|
+
agentId,
|
|
470
|
+
...codeRef() ? { codeRef: codeRef() } : {},
|
|
471
|
+
sdk: sdkVersion(),
|
|
472
|
+
tools: getRegisteredTools(),
|
|
473
|
+
prompts: getRegisteredPrompts()
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
function computeBuildHash(manifest) {
|
|
477
|
+
const identity = {
|
|
478
|
+
...manifest,
|
|
479
|
+
tools: manifest.tools.map(({ source: _source, ...tool }) => tool)
|
|
480
|
+
};
|
|
481
|
+
return sha256(canonicalJson(identity));
|
|
482
|
+
}
|
|
483
|
+
function attributionFromEnv() {
|
|
484
|
+
const attribution = { source: "runtime" };
|
|
485
|
+
if (process.env.GITHUB_ACTIONS) attribution.source = "ci";
|
|
486
|
+
if (process.env.GITHUB_ACTOR) attribution.githubLogin = process.env.GITHUB_ACTOR;
|
|
487
|
+
const sha = process.env.GATEWAY_COMMIT ?? process.env.GITHUB_SHA;
|
|
488
|
+
if (sha) attribution.commitSha = sha;
|
|
489
|
+
if (process.env.GITHUB_REPOSITORY) attribution.repo = process.env.GITHUB_REPOSITORY;
|
|
490
|
+
const branch = process.env.GITHUB_HEAD_REF ?? process.env.GITHUB_REF_NAME;
|
|
491
|
+
if (branch) attribution.branch = branch;
|
|
492
|
+
const ref = process.env.GITHUB_REF ?? "";
|
|
493
|
+
const prMatch = /refs\/pull\/(\d+)\//.exec(ref);
|
|
494
|
+
if (prMatch) attribution.prNumber = Number(prMatch[1]);
|
|
495
|
+
if (process.env.GATEWAY_PR_TITLE) {
|
|
496
|
+
attribution.prTitle = process.env.GATEWAY_PR_TITLE.slice(0, 300);
|
|
497
|
+
}
|
|
498
|
+
if (process.env.GATEWAY_PR_BODY) {
|
|
499
|
+
attribution.prDescription = process.env.GATEWAY_PR_BODY.slice(0, 2e3);
|
|
500
|
+
}
|
|
501
|
+
if (attribution.repo && attribution.prNumber && !("prUrl" in attribution)) {
|
|
502
|
+
attribution.prUrl = `https://github.com/${String(attribution.repo)}/pull/${String(attribution.prNumber)}`;
|
|
503
|
+
}
|
|
504
|
+
return attribution;
|
|
505
|
+
}
|
|
506
|
+
async function registerBuild(params) {
|
|
507
|
+
const auth = Buffer.from(`${params.publicKey}:${params.secretKey}`).toString(
|
|
508
|
+
"base64"
|
|
509
|
+
);
|
|
510
|
+
const response = await fetch(
|
|
511
|
+
`${params.host.replace(/\/$/, "")}/api/public/agent-builds`,
|
|
512
|
+
{
|
|
513
|
+
method: "POST",
|
|
514
|
+
headers: {
|
|
515
|
+
Authorization: `Basic ${auth}`,
|
|
516
|
+
"Content-Type": "application/json"
|
|
517
|
+
},
|
|
518
|
+
body: JSON.stringify({
|
|
519
|
+
agentId: params.agentId,
|
|
520
|
+
buildHash: params.buildHash,
|
|
521
|
+
...params.version ? { semanticVer: params.version } : {},
|
|
522
|
+
manifest: params.manifest,
|
|
523
|
+
attribution: attributionFromEnv()
|
|
524
|
+
}),
|
|
525
|
+
signal: AbortSignal.timeout(15e3)
|
|
526
|
+
}
|
|
527
|
+
);
|
|
528
|
+
if (!response.ok) {
|
|
529
|
+
throw new Error(
|
|
530
|
+
`build registration failed (${response.status}): ${(await response.text()).slice(0, 300)}`
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
277
535
|
// src/tracing/init.ts
|
|
278
536
|
var ENV_VARS = {
|
|
279
537
|
HOST: "GATEWAY_HOST",
|
|
@@ -352,6 +610,24 @@ function buildProvider(config, logFn) {
|
|
|
352
610
|
if (config.version) {
|
|
353
611
|
resourceAttrs["gateway.version"] = config.version;
|
|
354
612
|
}
|
|
613
|
+
let buildInfo = null;
|
|
614
|
+
if (config.agent) {
|
|
615
|
+
const manifest = assembleManifest(config.agent);
|
|
616
|
+
const buildHash = computeBuildHash(manifest);
|
|
617
|
+
buildInfo = { manifest, buildHash };
|
|
618
|
+
resourceAttrs["gateway.agent.id"] = config.agent;
|
|
619
|
+
resourceAttrs["gateway.build.hash"] = buildHash;
|
|
620
|
+
if (config.version) {
|
|
621
|
+
resourceAttrs["gateway.agent.version"] = config.version;
|
|
622
|
+
}
|
|
623
|
+
logFn(
|
|
624
|
+
"Agent build identity: %s @ %s (%d tools, %d prompts)",
|
|
625
|
+
config.agent,
|
|
626
|
+
buildHash.slice(0, 15),
|
|
627
|
+
manifest.tools.length,
|
|
628
|
+
Object.keys(manifest.prompts).length
|
|
629
|
+
);
|
|
630
|
+
}
|
|
355
631
|
let isLocalhost = false;
|
|
356
632
|
if (endpoint) {
|
|
357
633
|
try {
|
|
@@ -411,7 +687,28 @@ function buildProvider(config, logFn) {
|
|
|
411
687
|
spanProcessors
|
|
412
688
|
});
|
|
413
689
|
}
|
|
414
|
-
return
|
|
690
|
+
return {
|
|
691
|
+
provider,
|
|
692
|
+
buildInfo,
|
|
693
|
+
connection: { host, publicKey, secretKey }
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
function scheduleBuildRegistration(config, built) {
|
|
697
|
+
if (!config.agent || !built.buildInfo || config.registerBuild === false) {
|
|
698
|
+
return;
|
|
699
|
+
}
|
|
700
|
+
const { manifest, buildHash } = built.buildInfo;
|
|
701
|
+
void registerBuild({
|
|
702
|
+
host: built.connection.host,
|
|
703
|
+
publicKey: built.connection.publicKey,
|
|
704
|
+
secretKey: built.connection.secretKey,
|
|
705
|
+
agentId: config.agent,
|
|
706
|
+
version: config.version,
|
|
707
|
+
manifest,
|
|
708
|
+
buildHash
|
|
709
|
+
}).catch((err) => {
|
|
710
|
+
warn("Build registration failed (agent keeps running):", err);
|
|
711
|
+
});
|
|
415
712
|
}
|
|
416
713
|
function init(config = {}) {
|
|
417
714
|
if (state.initialized) {
|
|
@@ -419,8 +716,10 @@ function init(config = {}) {
|
|
|
419
716
|
return;
|
|
420
717
|
}
|
|
421
718
|
state.debug = config.debug ?? false;
|
|
422
|
-
const
|
|
719
|
+
const built = buildProvider(config, log);
|
|
720
|
+
const provider = built.provider;
|
|
423
721
|
provider.register();
|
|
722
|
+
scheduleBuildRegistration(config, built);
|
|
424
723
|
state.provider = provider;
|
|
425
724
|
state.initialized = true;
|
|
426
725
|
log(
|
|
@@ -431,7 +730,9 @@ function init(config = {}) {
|
|
|
431
730
|
function initIsolated(config = {}) {
|
|
432
731
|
const debugLog2 = config.debug ? (message, ...args) => console.log(`[gatewaysdk.tracing:isolated] ${message}`, ...args) : () => {
|
|
433
732
|
};
|
|
434
|
-
const
|
|
733
|
+
const built = buildProvider(config, debugLog2);
|
|
734
|
+
const provider = built.provider;
|
|
735
|
+
scheduleBuildRegistration(config, built);
|
|
435
736
|
const tracer = provider.getTracer("@withgateway/sdk");
|
|
436
737
|
debugLog2(
|
|
437
738
|
"Isolated tracing provider created (service=%s)",
|