@withgateway/sdk 0.1.5 → 0.1.7

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
@@ -2623,22 +2623,19 @@ __export(tracing_exports, {
2623
2623
 
2624
2624
  // src/tracing/init.ts
2625
2625
  var ENV_VARS = {
2626
- HOST: ["GATEWAY_HOST", "GATEWAY_HOST"],
2627
- PUBLIC_KEY: ["GATEWAY_PUBLIC_KEY", "GATEWAY_PUBLIC_KEY"],
2628
- SECRET_KEY: ["GATEWAY_SECRET_KEY", "GATEWAY_SECRET_KEY"],
2629
- ENDPOINT: ["GATEWAY_OTLP_ENDPOINT", "GATEWAY_OTLP_ENDPOINT"]
2626
+ HOST: "GATEWAY_HOST",
2627
+ PUBLIC_KEY: "GATEWAY_PUBLIC_KEY",
2628
+ SECRET_KEY: "GATEWAY_SECRET_KEY",
2629
+ ENDPOINT: "GATEWAY_OTLP_ENDPOINT"
2630
2630
  };
2631
2631
  var DEFAULT_OTLP_PATH = "/api/public/otel/v1/traces";
2632
2632
  var state = {
2633
2633
  initialized: false,
2634
2634
  provider: null
2635
2635
  };
2636
- function getEnv(keys) {
2637
- for (const key of keys) {
2638
- const value = process.env[key];
2639
- if (value) return value;
2640
- }
2641
- return void 0;
2636
+ function getEnv(key) {
2637
+ const value = process.env[key];
2638
+ return value ? value : void 0;
2642
2639
  }
2643
2640
  function log(message, ...args) {
2644
2641
  if (state.debug) {
@@ -2930,33 +2927,74 @@ function toolResultText(content) {
2930
2927
  ).join("");
2931
2928
  return JSON.stringify(content ?? "");
2932
2929
  }
2930
+ var TURN_INPUT_MAX = 4e3;
2933
2931
  async function* observeClaude(stream, options) {
2934
2932
  const tracer = options.tracer ?? trace3.getTracer("@withgateway/sdk");
2935
- const root = tracer.startSpan(options.name, {
2936
- attributes: {
2937
- "openinference.span.kind": "AGENT",
2938
- ...options.input !== void 0 ? { "input.value": options.input } : {},
2939
- ...options.attributes ?? {}
2940
- }
2941
- });
2942
- const rootCtx = trace3.setSpan(context3.active(), root);
2943
- const openTools = /* @__PURE__ */ new Map();
2944
2933
  const model = options.model ?? "claude";
2934
+ const openTools = /* @__PURE__ */ new Map();
2935
+ let root = null;
2936
+ let rootCtx = null;
2937
+ if (!options.perTurn) {
2938
+ root = tracer.startSpan(options.name, {
2939
+ attributes: {
2940
+ "openinference.span.kind": "AGENT",
2941
+ ...options.input !== void 0 ? { "input.value": options.input } : {},
2942
+ ...options.attributes ?? {}
2943
+ }
2944
+ });
2945
+ rootCtx = trace3.setSpan(context3.active(), root);
2946
+ }
2947
+ let turnRoot = null;
2948
+ let turnCtx = null;
2949
+ let turnIndex = 0;
2950
+ let turnText = "";
2951
+ let pendingTurnInput = options.input ?? "";
2945
2952
  let finalText = "";
2953
+ const endTurn = () => {
2954
+ if (!turnRoot) return;
2955
+ if (turnText) turnRoot.setAttribute("output.value", turnText);
2956
+ turnRoot.setStatus({ code: SpanStatusCode2.OK });
2957
+ turnRoot.end();
2958
+ turnRoot = null;
2959
+ turnCtx = null;
2960
+ };
2946
2961
  try {
2947
2962
  for await (const message of stream) {
2948
2963
  if (message?.type === "assistant") {
2949
- let turnText = "";
2964
+ if (options.perTurn) {
2965
+ for (const span of openTools.values()) span.end();
2966
+ openTools.clear();
2967
+ endTurn();
2968
+ turnIndex += 1;
2969
+ turnText = "";
2970
+ turnRoot = tracer.startSpan(`${options.name} \xB7 turn ${turnIndex}`, {
2971
+ root: true,
2972
+ attributes: {
2973
+ "openinference.span.kind": "AGENT",
2974
+ ...pendingTurnInput ? { "input.value": pendingTurnInput.slice(0, TURN_INPUT_MAX) } : {},
2975
+ ...options.attributes ?? {}
2976
+ }
2977
+ });
2978
+ turnCtx = trace3.setSpan(context3.active(), turnRoot);
2979
+ pendingTurnInput = "";
2980
+ }
2981
+ const childCtx = options.perTurn ? turnCtx : rootCtx;
2982
+ let text = "";
2950
2983
  const toolUses = [];
2951
2984
  for (const block of message.message?.content ?? []) {
2952
2985
  if (block?.type === "text") {
2953
2986
  finalText += block.text;
2954
- turnText += block.text;
2987
+ text += block.text;
2955
2988
  }
2956
2989
  if (block?.type === "tool_use") {
2957
- toolUses.push({ id: block.id, name: block.name, input: block.input });
2990
+ toolUses.push({
2991
+ id: block.id,
2992
+ name: block.name,
2993
+ input: block.input
2994
+ });
2958
2995
  }
2959
2996
  }
2997
+ turnText += text;
2960
2998
  const gen = tracer.startSpan(
2961
2999
  "assistant",
2962
3000
  {
@@ -2964,10 +3002,10 @@ async function* observeClaude(stream, options) {
2964
3002
  "openinference.span.kind": "LLM",
2965
3003
  "llm.model_name": model,
2966
3004
  "llm.provider": "anthropic",
2967
- "output.value": turnText || toolUses.map((t) => `\u2192 ${cleanToolName(t.name)}`).join(", ")
3005
+ "output.value": text || toolUses.map((t) => `\u2192 ${cleanToolName(t.name)}`).join(", ")
2968
3006
  }
2969
3007
  },
2970
- rootCtx
3008
+ childCtx
2971
3009
  );
2972
3010
  const usage = message.message?.usage;
2973
3011
  if (usage?.input_tokens != null)
@@ -2985,7 +3023,7 @@ async function* observeClaude(stream, options) {
2985
3023
  "input.value": JSON.stringify(t.input)
2986
3024
  }
2987
3025
  },
2988
- rootCtx
3026
+ childCtx
2989
3027
  );
2990
3028
  openTools.set(t.id, span);
2991
3029
  }
@@ -2995,13 +3033,17 @@ async function* observeClaude(stream, options) {
2995
3033
  if (Array.isArray(content)) {
2996
3034
  for (const block of content) {
2997
3035
  if (block?.type === "tool_result") {
3036
+ const resultText = toolResultText(block.content);
2998
3037
  const span = openTools.get(block.tool_use_id);
2999
3038
  if (span) {
3000
- span.setAttribute("output.value", toolResultText(block.content));
3039
+ span.setAttribute("output.value", resultText);
3001
3040
  span.setStatus({ code: SpanStatusCode2.OK });
3002
3041
  span.end();
3003
3042
  openTools.delete(block.tool_use_id);
3004
3043
  }
3044
+ if (options.perTurn && pendingTurnInput.length < TURN_INPUT_MAX) {
3045
+ pendingTurnInput += (pendingTurnInput ? "\n" : "") + resultText.slice(0, TURN_INPUT_MAX - pendingTurnInput.length);
3046
+ }
3005
3047
  }
3006
3048
  }
3007
3049
  }
@@ -3011,9 +3053,13 @@ async function* observeClaude(stream, options) {
3011
3053
  } finally {
3012
3054
  for (const span of openTools.values()) span.end();
3013
3055
  openTools.clear();
3014
- root.setAttribute("output.value", finalText);
3015
- root.setStatus({ code: SpanStatusCode2.OK });
3016
- root.end();
3056
+ if (options.perTurn) {
3057
+ endTurn();
3058
+ } else if (root) {
3059
+ root.setAttribute("output.value", finalText);
3060
+ root.setStatus({ code: SpanStatusCode2.OK });
3061
+ root.end();
3062
+ }
3017
3063
  }
3018
3064
  }
3019
3065