@withgateway/sdk 0.1.10 → 0.1.12

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.mjs CHANGED
@@ -21,7 +21,7 @@ var require_package = __commonJS({
21
21
  "package.json"(exports, module) {
22
22
  module.exports = {
23
23
  name: "@withgateway/sdk",
24
- version: "0.1.10",
24
+ version: "0.1.12",
25
25
  description: "Gateway SDK for TypeScript - A/B experiments, tracing, and observability",
26
26
  main: "./dist/index.js",
27
27
  module: "./dist/index.mjs",
@@ -2763,6 +2763,8 @@ function sha256(text) {
2763
2763
  }
2764
2764
  var registeredTools = /* @__PURE__ */ new Map();
2765
2765
  var registeredPrompts = /* @__PURE__ */ new Map();
2766
+ var registeredSkills = /* @__PURE__ */ new Map();
2767
+ var registeredArtifacts = /* @__PURE__ */ new Map();
2766
2768
  function captureSource() {
2767
2769
  const stack = new Error().stack?.split("\n") ?? [];
2768
2770
  for (const line of stack.slice(1)) {
@@ -2793,10 +2795,27 @@ function defineTool(tool) {
2793
2795
  ...source ? { source } : {}
2794
2796
  };
2795
2797
  registeredTools.set(tool.name, identity);
2798
+ notifyRegistryChanged();
2796
2799
  return tool;
2797
2800
  }
2798
- function registerPrompt(name, content) {
2801
+ function registerArtifact(kind, name, options = {}) {
2802
+ const source = captureSource();
2803
+ const identity = {
2804
+ kind,
2805
+ name,
2806
+ ...options.version !== void 0 ? { version: options.version } : {},
2807
+ ...options.content !== void 0 ? { contentHash: sha256(options.content) } : {},
2808
+ ...options.ref !== void 0 ? { ref: options.ref } : {},
2809
+ ...source ? { source } : {},
2810
+ ...options.metadata ? { metadata: options.metadata } : {}
2811
+ };
2812
+ registeredArtifacts.set(`${kind}:${name}`, identity);
2813
+ notifyRegistryChanged();
2814
+ return identity;
2815
+ }
2816
+ function registerPrompt(name, content, options = {}) {
2799
2817
  registeredPrompts.set(name, sha256(content));
2818
+ registerArtifact("prompt", name, { ...options, content });
2800
2819
  return content;
2801
2820
  }
2802
2821
  function getRegisteredTools() {
@@ -2804,6 +2823,22 @@ function getRegisteredTools() {
2804
2823
  (a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0
2805
2824
  );
2806
2825
  }
2826
+ function registerSkill(name, version, options = {}) {
2827
+ registeredSkills.set(name, version);
2828
+ registerArtifact("skill", name, { version, ...options });
2829
+ }
2830
+ function getRegisteredArtifacts() {
2831
+ return [...registeredArtifacts.values()].sort(
2832
+ (a, b) => `${a.kind}:${a.name}` < `${b.kind}:${b.name}` ? -1 : 1
2833
+ );
2834
+ }
2835
+ function getRegisteredSkills() {
2836
+ return Object.fromEntries(
2837
+ [...registeredSkills.entries()].sort(
2838
+ ([a], [b]) => a < b ? -1 : a > b ? 1 : 0
2839
+ )
2840
+ );
2841
+ }
2807
2842
  function getRegisteredPrompts() {
2808
2843
  return Object.fromEntries(
2809
2844
  [...registeredPrompts.entries()].sort(
@@ -2814,6 +2849,11 @@ function getRegisteredPrompts() {
2814
2849
  function resetBuildRegistry() {
2815
2850
  registeredTools.clear();
2816
2851
  registeredPrompts.clear();
2852
+ registeredSkills.clear();
2853
+ registeredArtifacts.clear();
2854
+ activeBuilds.length = 0;
2855
+ if (reRegisterTimer) clearTimeout(reRegisterTimer);
2856
+ reRegisterTimer = null;
2817
2857
  }
2818
2858
  function sdkVersion() {
2819
2859
  try {
@@ -2823,11 +2863,62 @@ function sdkVersion() {
2823
2863
  return "@withgateway/sdk";
2824
2864
  }
2825
2865
  }
2866
+ function paasGitContext() {
2867
+ const env = process.env;
2868
+ if (env.VERCEL_GIT_COMMIT_SHA) {
2869
+ const isGithub = (env.VERCEL_GIT_PROVIDER ?? "github") === "github";
2870
+ return {
2871
+ provider: "vercel",
2872
+ sha: env.VERCEL_GIT_COMMIT_SHA,
2873
+ repo: isGithub && env.VERCEL_GIT_REPO_OWNER && env.VERCEL_GIT_REPO_SLUG ? `${env.VERCEL_GIT_REPO_OWNER}/${env.VERCEL_GIT_REPO_SLUG}` : void 0,
2874
+ branch: env.VERCEL_GIT_COMMIT_REF,
2875
+ login: env.VERCEL_GIT_COMMIT_AUTHOR_LOGIN
2876
+ };
2877
+ }
2878
+ if (env.RENDER_GIT_COMMIT) {
2879
+ return {
2880
+ provider: "render",
2881
+ sha: env.RENDER_GIT_COMMIT,
2882
+ repo: env.RENDER_GIT_REPO_SLUG,
2883
+ // already "owner/repo"
2884
+ branch: env.RENDER_GIT_BRANCH
2885
+ };
2886
+ }
2887
+ if (env.RAILWAY_GIT_COMMIT_SHA) {
2888
+ return {
2889
+ provider: "railway",
2890
+ sha: env.RAILWAY_GIT_COMMIT_SHA,
2891
+ repo: env.RAILWAY_GIT_REPO_OWNER && env.RAILWAY_GIT_REPO_NAME ? `${env.RAILWAY_GIT_REPO_OWNER}/${env.RAILWAY_GIT_REPO_NAME}` : void 0,
2892
+ branch: env.RAILWAY_GIT_BRANCH,
2893
+ login: env.RAILWAY_GIT_AUTHOR
2894
+ };
2895
+ }
2896
+ if (env.NETLIFY && env.COMMIT_REF) {
2897
+ const repoMatch = /github\.com[/:]([^/]+\/[^/.]+)/.exec(
2898
+ env.REPOSITORY_URL ?? ""
2899
+ );
2900
+ return {
2901
+ provider: "netlify",
2902
+ sha: env.COMMIT_REF,
2903
+ repo: repoMatch?.[1],
2904
+ branch: env.BRANCH
2905
+ };
2906
+ }
2907
+ const bareSha = env.HEROKU_SLUG_COMMIT ?? env.SOURCE_VERSION ?? env.GIT_COMMIT ?? env.COMMIT_SHA;
2908
+ if (bareSha) {
2909
+ return {
2910
+ provider: env.HEROKU_SLUG_COMMIT || env.SOURCE_VERSION ? "heroku" : "generic",
2911
+ sha: bareSha
2912
+ };
2913
+ }
2914
+ return null;
2915
+ }
2826
2916
  function codeRef() {
2917
+ const paas = paasGitContext();
2827
2918
  const sha = process.env.GATEWAY_COMMIT ?? process.env.GITHUB_SHA ?? process.env.CI_COMMIT_SHA ?? // GitLab
2828
- process.env.CIRCLE_SHA1;
2919
+ process.env.CIRCLE_SHA1 ?? paas?.sha;
2829
2920
  if (!sha) return void 0;
2830
- const repo = process.env.GITHUB_REPOSITORY ?? process.env.CI_PROJECT_PATH;
2921
+ const repo = process.env.GITHUB_REPOSITORY ?? process.env.CI_PROJECT_PATH ?? paas?.repo;
2831
2922
  return repo ? `git:${repo}@${sha}` : `git:${sha}`;
2832
2923
  }
2833
2924
  function assembleManifest(agentId) {
@@ -2836,13 +2927,18 @@ function assembleManifest(agentId) {
2836
2927
  ...codeRef() ? { codeRef: codeRef() } : {},
2837
2928
  sdk: sdkVersion(),
2838
2929
  tools: getRegisteredTools(),
2839
- prompts: getRegisteredPrompts()
2930
+ prompts: getRegisteredPrompts(),
2931
+ skills: getRegisteredSkills(),
2932
+ artifacts: getRegisteredArtifacts()
2840
2933
  };
2841
2934
  }
2842
2935
  function computeBuildHash(manifest) {
2843
2936
  const identity = {
2844
2937
  ...manifest,
2845
- tools: manifest.tools.map(({ source: _source, ...tool }) => tool)
2938
+ tools: manifest.tools.map(({ source: _source, ...tool }) => tool),
2939
+ artifacts: manifest.artifacts.map(
2940
+ ({ source: _source, ...artifact }) => artifact
2941
+ )
2846
2942
  };
2847
2943
  return sha256(canonicalJson(identity));
2848
2944
  }
@@ -2855,6 +2951,14 @@ function attributionFromEnv() {
2855
2951
  if (process.env.GITHUB_REPOSITORY) attribution.repo = process.env.GITHUB_REPOSITORY;
2856
2952
  const branch = process.env.GITHUB_HEAD_REF ?? process.env.GITHUB_REF_NAME;
2857
2953
  if (branch) attribution.branch = branch;
2954
+ const paas = paasGitContext();
2955
+ if (paas) {
2956
+ if (attribution.source === "runtime") attribution.source = `paas:${paas.provider}`;
2957
+ if (!attribution.commitSha && paas.sha) attribution.commitSha = paas.sha;
2958
+ if (!attribution.repo && paas.repo) attribution.repo = paas.repo;
2959
+ if (!attribution.branch && paas.branch) attribution.branch = paas.branch;
2960
+ if (!attribution.githubLogin && paas.login) attribution.githubLogin = paas.login;
2961
+ }
2858
2962
  const ref = process.env.GITHUB_REF ?? "";
2859
2963
  const prMatch = /refs\/pull\/(\d+)\//.exec(ref);
2860
2964
  if (prMatch) attribution.prNumber = Number(prMatch[1]);
@@ -2869,6 +2973,31 @@ function attributionFromEnv() {
2869
2973
  }
2870
2974
  return attribution;
2871
2975
  }
2976
+ var activeBuilds = [];
2977
+ var reRegisterTimer = null;
2978
+ var RE_REGISTER_DEBOUNCE_MS = 3e3;
2979
+ function trackActiveBuild(context4) {
2980
+ activeBuilds.push(context4);
2981
+ }
2982
+ function notifyRegistryChanged() {
2983
+ if (activeBuilds.length === 0) return;
2984
+ if (reRegisterTimer) clearTimeout(reRegisterTimer);
2985
+ reRegisterTimer = setTimeout(() => {
2986
+ reRegisterTimer = null;
2987
+ for (const context4 of activeBuilds) {
2988
+ const manifest = assembleManifest(context4.agentId);
2989
+ const buildHash = computeBuildHash(manifest);
2990
+ if (buildHash === context4.lastRegisteredHash) continue;
2991
+ context4.lastRegisteredHash = buildHash;
2992
+ void registerBuild({ ...context4, manifest, buildHash }).catch(() => {
2993
+ context4.lastRegisteredHash = void 0;
2994
+ });
2995
+ }
2996
+ }, RE_REGISTER_DEBOUNCE_MS);
2997
+ if (typeof reRegisterTimer === "object" && "unref" in reRegisterTimer) {
2998
+ reRegisterTimer.unref();
2999
+ }
3000
+ }
2872
3001
  async function registerBuild(params) {
2873
3002
  const auth = Buffer.from(`${params.publicKey}:${params.secretKey}`).toString(
2874
3003
  "base64"
@@ -3149,6 +3278,14 @@ function scheduleBuildRegistration(config, built) {
3149
3278
  return;
3150
3279
  }
3151
3280
  const { manifest, buildHash } = built.buildInfo;
3281
+ trackActiveBuild({
3282
+ host: built.connection.host,
3283
+ publicKey: built.connection.publicKey,
3284
+ secretKey: built.connection.secretKey,
3285
+ agentId: config.agent,
3286
+ version: config.version,
3287
+ lastRegisteredHash: buildHash
3288
+ });
3152
3289
  void registerBuild({
3153
3290
  host: built.connection.host,
3154
3291
  publicKey: built.connection.publicKey,
@@ -3537,13 +3674,17 @@ export {
3537
3674
  singleton_exports as experiments,
3538
3675
  gateway,
3539
3676
  getExperimentContext,
3677
+ getRegisteredArtifacts,
3540
3678
  getRegisteredPrompts,
3679
+ getRegisteredSkills,
3541
3680
  getRegisteredTools,
3542
3681
  identify,
3543
3682
  loadSession,
3544
3683
  memory,
3545
3684
  personas,
3685
+ registerArtifact,
3546
3686
  registerPrompt,
3687
+ registerSkill,
3547
3688
  renderAgentSystem,
3548
3689
  replay,
3549
3690
  resetBuildRegistry,