framework-agents 0.1.3 → 0.1.6

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.js CHANGED
@@ -71,7 +71,7 @@ import {
71
71
  usageCost,
72
72
  user,
73
73
  wireTextOf
74
- } from "./chunk-VIGLIWEY.js";
74
+ } from "./chunk-AJI2QSJE.js";
75
75
 
76
76
  // src/config/index.ts
77
77
  import { existsSync } from "fs";
@@ -864,8 +864,8 @@ var OpenAICompatibleProvider = class {
864
864
  }
865
865
  } : {},
866
866
  messages: options.messages.map(toWireMessage),
867
- ...options.tools.length ? {
868
- tools: options.tools.map((t) => ({
867
+ ...options.tools?.length ? {
868
+ tools: (options.tools ?? []).map((t) => ({
869
869
  type: "function",
870
870
  function: {
871
871
  name: t.name,
@@ -932,91 +932,107 @@ var OpenAICompatibleProvider = class {
932
932
  /** SSE: `data: {...}` lines; delta.content -> onToken, delta.reasoning_content -> onThinking. */
933
933
  async readStream(res, options) {
934
934
  if (!res.body) throw new Error("Streaming response has no body");
935
- const reader = res.body.getReader();
936
- const decoder = new TextDecoder();
937
- let text = "";
938
- let thinking = "";
939
- let inputTokens = 0;
940
- let outputTokens = 0;
941
- let reasoningTokens;
942
- let finishReason;
943
- const toolArgBuffers = /* @__PURE__ */ new Map();
944
- let buffer = "";
945
- const flush = (chunk) => {
946
- buffer += chunk;
947
- const lines = buffer.split("\n");
948
- buffer = lines.pop() ?? "";
949
- for (const line of lines) {
950
- const t = line.trim();
951
- if (!t.startsWith("data:")) continue;
952
- const payload = t.slice(5).trim();
953
- if (payload === "[DONE]") continue;
954
- let evt;
955
- try {
956
- evt = JSON.parse(payload);
957
- } catch {
958
- continue;
959
- }
960
- if (evt.choices?.[0]?.finish_reason) finishReason = evt.choices[0].finish_reason ?? void 0;
961
- if (evt.usage) {
962
- if (evt.usage.prompt_tokens != null) inputTokens = evt.usage.prompt_tokens;
963
- if (evt.usage.completion_tokens != null) outputTokens = evt.usage.completion_tokens;
964
- const rt = reasoningOf(evt.usage);
965
- if (rt != null) reasoningTokens = rt;
966
- continue;
967
- }
968
- const delta = evt.choices?.[0]?.delta;
969
- if (!delta) continue;
970
- const think = thinkingOf(delta);
971
- if (think) {
972
- thinking += think;
973
- options.onThinking?.(think);
974
- }
975
- if (delta.content) {
976
- text += delta.content;
977
- options.onToken?.(delta.content);
978
- }
979
- for (const [i, tc] of (delta.tool_calls ?? []).entries()) {
980
- const key = tc.index ?? i;
981
- const slot = toolArgBuffers.get(key) ?? { id: "", name: "", args: "" };
982
- if (tc.id) slot.id = tc.id;
983
- if (tc.function?.name) slot.name = tc.function.name;
984
- if (tc.function?.arguments) slot.args += tc.function.arguments;
985
- toolArgBuffers.set(key, slot);
935
+ return this._readSse(res.body, options).catch((err) => {
936
+ if (err instanceof AgentError || err instanceof Error && err.name === "AbortError") throw err;
937
+ throw new AgentError("MODEL_ERROR", `Stream broke mid-response: ${err}`, false);
938
+ });
939
+ }
940
+ async _readSse(body, options) {
941
+ let delivered = false;
942
+ try {
943
+ const reader = body.getReader();
944
+ const decoder = new TextDecoder();
945
+ let text = "";
946
+ let thinking = "";
947
+ let inputTokens = 0;
948
+ let outputTokens = 0;
949
+ let reasoningTokens;
950
+ let finishReason;
951
+ const toolArgBuffers = /* @__PURE__ */ new Map();
952
+ let buffer = "";
953
+ const flush = (chunk) => {
954
+ buffer += chunk;
955
+ const lines = buffer.split("\n");
956
+ buffer = lines.pop() ?? "";
957
+ for (const line of lines) {
958
+ const t = line.trim();
959
+ if (!t.startsWith("data:")) continue;
960
+ const payload = t.slice(5).trim();
961
+ if (payload === "[DONE]") continue;
962
+ let evt;
963
+ try {
964
+ evt = JSON.parse(payload);
965
+ } catch {
966
+ continue;
967
+ }
968
+ if (evt.choices?.[0]?.finish_reason) finishReason = evt.choices[0].finish_reason ?? void 0;
969
+ if (evt.usage) {
970
+ if (evt.usage.prompt_tokens != null) inputTokens = evt.usage.prompt_tokens;
971
+ if (evt.usage.completion_tokens != null) outputTokens = evt.usage.completion_tokens;
972
+ const rt = reasoningOf(evt.usage);
973
+ if (rt != null) reasoningTokens = rt;
974
+ continue;
975
+ }
976
+ const delta = evt.choices?.[0]?.delta;
977
+ if (!delta) continue;
978
+ const think = thinkingOf(delta);
979
+ if (think) {
980
+ thinking += think;
981
+ delivered = true;
982
+ options.onThinking?.(think);
983
+ }
984
+ if (delta.content) {
985
+ text += delta.content;
986
+ delivered = true;
987
+ options.onToken?.(delta.content);
988
+ }
989
+ for (const [i, tc] of (delta.tool_calls ?? []).entries()) {
990
+ const key = tc.index ?? i;
991
+ delivered = true;
992
+ const slot = toolArgBuffers.get(key) ?? { id: "", name: "", args: "" };
993
+ if (tc.id) slot.id = tc.id;
994
+ if (tc.function?.name) slot.name = tc.function.name;
995
+ if (tc.function?.arguments) slot.args += tc.function.arguments;
996
+ toolArgBuffers.set(key, slot);
997
+ }
986
998
  }
999
+ };
1000
+ for (; ; ) {
1001
+ const { done, value } = await reader.read();
1002
+ if (done) break;
1003
+ flush(decoder.decode(value, { stream: true }));
987
1004
  }
988
- };
989
- for (; ; ) {
990
- const { done, value } = await reader.read();
991
- if (done) break;
992
- flush(decoder.decode(value, { stream: true }));
993
- }
994
- flush(decoder.decode());
995
- if (!text && !toolArgBuffers.size && thinking && finishReason === "stop") {
996
- throw new AgentError(
997
- "MODEL_ERROR",
998
- "Stream ended at finish_reason=stop having put the entire answer in the reasoning channel (no content, no tool calls, thinking present). The reasoning/content split lost the reply \u2014 retry or raise maxTokens.",
999
- true
1000
- );
1001
- }
1002
- if (!text && !toolArgBuffers.size && finishReason === "length") {
1003
- throw new AgentError(
1004
- "MODEL_ERROR",
1005
- `Stream ended at finish_reason=length with no content (${outputTokens} tokens burned). Raise maxTokens.`,
1006
- false
1007
- );
1008
- }
1009
- return {
1010
- text,
1011
- ...thinking ? { thinking } : {},
1012
- ...finishReason ? { finishReason } : {},
1013
- toolCalls: [...toolArgBuffers.values()].filter((t) => t.name).map((t) => ({ id: t.id || `call_${++callSeq}`, name: t.name, args: safeJson(t.args) })),
1014
- usage: {
1015
- inputTokens,
1016
- outputTokens,
1017
- ...reasoningTokens != null ? { reasoningTokens } : {}
1005
+ flush(decoder.decode());
1006
+ if (!text && !toolArgBuffers.size && thinking && finishReason === "stop") {
1007
+ throw new AgentError(
1008
+ "MODEL_ERROR",
1009
+ "Stream ended at finish_reason=stop having put the entire answer in the reasoning channel (no content, no tool calls, thinking present). The reasoning/content split lost the reply \u2014 retry or raise maxTokens.",
1010
+ true
1011
+ );
1018
1012
  }
1019
- };
1013
+ if (!text && !toolArgBuffers.size && finishReason === "length") {
1014
+ throw new AgentError(
1015
+ "MODEL_ERROR",
1016
+ `Stream ended at finish_reason=length with no content (${outputTokens} tokens burned). Raise maxTokens.`,
1017
+ false
1018
+ );
1019
+ }
1020
+ return {
1021
+ text,
1022
+ ...thinking ? { thinking } : {},
1023
+ ...finishReason ? { finishReason } : {},
1024
+ toolCalls: [...toolArgBuffers.values()].map((t) => ({ id: t.id || `call_${++callSeq}`, name: t.name, args: safeJson(t.args) })),
1025
+ usage: {
1026
+ inputTokens,
1027
+ outputTokens,
1028
+ ...reasoningTokens != null ? { reasoningTokens } : {}
1029
+ }
1030
+ };
1031
+ } catch (err) {
1032
+ if (err instanceof Error && err.name === "AbortError") throw err;
1033
+ if (err instanceof AgentError) throw err;
1034
+ throw new AgentError("MODEL_ERROR", `Stream broke mid-response${delivered ? " (deltas already delivered)" : ""}: ${err}`, !delivered);
1035
+ }
1020
1036
  }
1021
1037
  };
1022
1038
  var callSeq = 0;
@@ -1114,8 +1130,8 @@ var AnthropicProvider = class {
1114
1130
  ...thinkingBudget ? { thinking: { type: "enabled", budget_tokens: thinkingBudget } } : {},
1115
1131
  ...stream ? { stream: true } : {},
1116
1132
  messages,
1117
- ...options.tools.length ? {
1118
- tools: options.tools.map((t) => ({
1133
+ ...options.tools?.length ? {
1134
+ tools: (options.tools ?? []).map((t) => ({
1119
1135
  name: t.name,
1120
1136
  description: t.description,
1121
1137
  input_schema: t.parametersSchema ?? { type: "object" }
@@ -3781,6 +3797,9 @@ ${lines.join("\n")}` }];
3781
3797
  if (fresh.length) await this.store.add(fresh);
3782
3798
  }
3783
3799
  };
3800
+
3801
+ // src/index.ts
3802
+ import * as z13 from "zod";
3784
3803
  export {
3785
3804
  A2AClient,
3786
3805
  A2AServer,
@@ -3946,6 +3965,7 @@ export {
3946
3965
  usageCost,
3947
3966
  user,
3948
3967
  wireTextOf,
3949
- withFileLock
3968
+ withFileLock,
3969
+ z13 as z
3950
3970
  };
3951
3971
  //# sourceMappingURL=index.js.map