@withgateway/sdk 0.1.6 → 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-DnowA8MG.d.mts → index-CubrUmOe.d.mts} +23 -3
- package/dist/{index-DnowA8MG.d.ts → index-CubrUmOe.d.ts} +23 -3
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +68 -19
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +68 -19
- 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 +68 -19
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/index.mjs +68 -19
- package/dist/tracing/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -2927,33 +2927,74 @@ function toolResultText(content) {
|
|
|
2927
2927
|
).join("");
|
|
2928
2928
|
return JSON.stringify(content ?? "");
|
|
2929
2929
|
}
|
|
2930
|
+
var TURN_INPUT_MAX = 4e3;
|
|
2930
2931
|
async function* observeClaude(stream, options) {
|
|
2931
2932
|
const tracer = options.tracer ?? trace3.getTracer("@withgateway/sdk");
|
|
2932
|
-
const root = tracer.startSpan(options.name, {
|
|
2933
|
-
attributes: {
|
|
2934
|
-
"openinference.span.kind": "AGENT",
|
|
2935
|
-
...options.input !== void 0 ? { "input.value": options.input } : {},
|
|
2936
|
-
...options.attributes ?? {}
|
|
2937
|
-
}
|
|
2938
|
-
});
|
|
2939
|
-
const rootCtx = trace3.setSpan(context3.active(), root);
|
|
2940
|
-
const openTools = /* @__PURE__ */ new Map();
|
|
2941
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 ?? "";
|
|
2942
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
|
+
};
|
|
2943
2961
|
try {
|
|
2944
2962
|
for await (const message of stream) {
|
|
2945
2963
|
if (message?.type === "assistant") {
|
|
2946
|
-
|
|
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 = "";
|
|
2947
2983
|
const toolUses = [];
|
|
2948
2984
|
for (const block of message.message?.content ?? []) {
|
|
2949
2985
|
if (block?.type === "text") {
|
|
2950
2986
|
finalText += block.text;
|
|
2951
|
-
|
|
2987
|
+
text += block.text;
|
|
2952
2988
|
}
|
|
2953
2989
|
if (block?.type === "tool_use") {
|
|
2954
|
-
toolUses.push({
|
|
2990
|
+
toolUses.push({
|
|
2991
|
+
id: block.id,
|
|
2992
|
+
name: block.name,
|
|
2993
|
+
input: block.input
|
|
2994
|
+
});
|
|
2955
2995
|
}
|
|
2956
2996
|
}
|
|
2997
|
+
turnText += text;
|
|
2957
2998
|
const gen = tracer.startSpan(
|
|
2958
2999
|
"assistant",
|
|
2959
3000
|
{
|
|
@@ -2961,10 +3002,10 @@ async function* observeClaude(stream, options) {
|
|
|
2961
3002
|
"openinference.span.kind": "LLM",
|
|
2962
3003
|
"llm.model_name": model,
|
|
2963
3004
|
"llm.provider": "anthropic",
|
|
2964
|
-
"output.value":
|
|
3005
|
+
"output.value": text || toolUses.map((t) => `\u2192 ${cleanToolName(t.name)}`).join(", ")
|
|
2965
3006
|
}
|
|
2966
3007
|
},
|
|
2967
|
-
|
|
3008
|
+
childCtx
|
|
2968
3009
|
);
|
|
2969
3010
|
const usage = message.message?.usage;
|
|
2970
3011
|
if (usage?.input_tokens != null)
|
|
@@ -2982,7 +3023,7 @@ async function* observeClaude(stream, options) {
|
|
|
2982
3023
|
"input.value": JSON.stringify(t.input)
|
|
2983
3024
|
}
|
|
2984
3025
|
},
|
|
2985
|
-
|
|
3026
|
+
childCtx
|
|
2986
3027
|
);
|
|
2987
3028
|
openTools.set(t.id, span);
|
|
2988
3029
|
}
|
|
@@ -2992,13 +3033,17 @@ async function* observeClaude(stream, options) {
|
|
|
2992
3033
|
if (Array.isArray(content)) {
|
|
2993
3034
|
for (const block of content) {
|
|
2994
3035
|
if (block?.type === "tool_result") {
|
|
3036
|
+
const resultText = toolResultText(block.content);
|
|
2995
3037
|
const span = openTools.get(block.tool_use_id);
|
|
2996
3038
|
if (span) {
|
|
2997
|
-
span.setAttribute("output.value",
|
|
3039
|
+
span.setAttribute("output.value", resultText);
|
|
2998
3040
|
span.setStatus({ code: SpanStatusCode2.OK });
|
|
2999
3041
|
span.end();
|
|
3000
3042
|
openTools.delete(block.tool_use_id);
|
|
3001
3043
|
}
|
|
3044
|
+
if (options.perTurn && pendingTurnInput.length < TURN_INPUT_MAX) {
|
|
3045
|
+
pendingTurnInput += (pendingTurnInput ? "\n" : "") + resultText.slice(0, TURN_INPUT_MAX - pendingTurnInput.length);
|
|
3046
|
+
}
|
|
3002
3047
|
}
|
|
3003
3048
|
}
|
|
3004
3049
|
}
|
|
@@ -3008,9 +3053,13 @@ async function* observeClaude(stream, options) {
|
|
|
3008
3053
|
} finally {
|
|
3009
3054
|
for (const span of openTools.values()) span.end();
|
|
3010
3055
|
openTools.clear();
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
root
|
|
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
|
+
}
|
|
3014
3063
|
}
|
|
3015
3064
|
}
|
|
3016
3065
|
|