@fonoster/community 0.30.0 → 0.31.0

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/adapters.js CHANGED
@@ -63,6 +63,9 @@ exports.ADAPTERS = {
63
63
  openai: { llm: openAiCompatible("openai") },
64
64
  groq: { llm: openAiCompatible("groq") },
65
65
  ollama: { keyless: true, llm: openAiCompatible("ollama") },
66
+ // Noise cancelling (`nc`): served by the appbridge's aiCoustics filter with the provider's key,
67
+ // so the entry only makes the adapter known to the registry and the gateway config.
68
+ aicoustics: {},
66
69
  fake: {
67
70
  keyless: true,
68
71
  tts: (o) => {
package/dist/llm.d.ts CHANGED
@@ -33,12 +33,26 @@ export type ChatMessage = {
33
33
  tool_call_id: string;
34
34
  content: string;
35
35
  };
36
+ /** Token counts as the vendor reports them for one request (add-usage-metering). */
37
+ export type TokenUsage = {
38
+ inputTokens: number;
39
+ outputTokens: number;
40
+ /** Input tokens served from the vendor's prompt cache (part of inputTokens). */
41
+ cachedTokens: number;
42
+ };
43
+ /**
44
+ * A stream ends with at most one `usage` chunk, after the text and tool calls, when the vendor
45
+ * reports usage. Its absence means the vendor reported none (or the stream was cut short).
46
+ */
36
47
  export type ThinkChunk = {
37
48
  type: "text";
38
49
  text: string;
39
50
  } | {
40
51
  type: "tool_calls";
41
52
  calls: ToolCall[];
53
+ } | {
54
+ type: "usage";
55
+ usage: TokenUsage;
42
56
  };
43
57
  export type ToolSpec = {
44
58
  name: string;
@@ -53,7 +53,9 @@ class OpenAiCompatibleThink {
53
53
  stream(messages, tools, signal) {
54
54
  return __asyncGenerator(this, arguments, function* stream_1() {
55
55
  var _a, _b, _c, _d, _e, _f, _g, _h;
56
- const body = Object.assign(Object.assign({ model: this.model, stream: true, temperature: this.options.temperature, max_tokens: this.options.maxTokens, messages: messages.map(toOpenAi) }, (tools.length
56
+ const body = Object.assign(Object.assign({ model: this.model, stream: true,
57
+ // The final chunk carries the request's token usage (add-usage-metering D6).
58
+ stream_options: { include_usage: true }, temperature: this.options.temperature, max_tokens: this.options.maxTokens, messages: messages.map(toOpenAi) }, (tools.length
57
59
  ? { tools: tools.map((t) => ({ type: "function", function: t })) }
58
60
  : {})), (this.options.jsonMode
59
61
  ? { response_format: { type: "json_object" } }
@@ -79,6 +81,7 @@ class OpenAiCompatibleThink {
79
81
  if (!res.ok || !res.body)
80
82
  throw new llm_1.ThinkError("provider_unavailable", `language model answered HTTP ${res.status}`);
81
83
  const calls = new Map();
84
+ let usage;
82
85
  const decoder = new util_1.TextDecoder();
83
86
  let buffer = "";
84
87
  const reader = res.body.getReader();
@@ -105,7 +108,10 @@ class OpenAiCompatibleThink {
105
108
  const data = line.slice(5).trim();
106
109
  if (data === "[DONE]")
107
110
  continue;
108
- const delta = (_c = (_b = (_a = JSON.parse(data).choices) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.delta) !== null && _c !== void 0 ? _c : {};
111
+ const parsed = JSON.parse(data);
112
+ if (parsed.usage)
113
+ usage = toTokenUsage(parsed.usage);
114
+ const delta = (_c = (_b = (_a = parsed.choices) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.delta) !== null && _c !== void 0 ? _c : {};
109
115
  if (delta.content)
110
116
  yield yield __await({ type: "text", text: delta.content });
111
117
  for (const tc of (_d = delta.tool_calls) !== null && _d !== void 0 ? _d : []) {
@@ -124,10 +130,21 @@ class OpenAiCompatibleThink {
124
130
  }
125
131
  if (calls.size)
126
132
  yield yield __await({ type: "tool_calls", calls: [...calls.values()] });
133
+ if (usage)
134
+ yield yield __await({ type: "usage", usage });
127
135
  });
128
136
  }
129
137
  }
130
138
  exports.OpenAiCompatibleThink = OpenAiCompatibleThink;
139
+ /** OpenAI's usage object as TokenUsage; cached tokens are optional in OpenAI-compatible APIs. */
140
+ function toTokenUsage(u) {
141
+ var _a, _b, _c, _d;
142
+ return {
143
+ inputTokens: (_a = u.prompt_tokens) !== null && _a !== void 0 ? _a : 0,
144
+ outputTokens: (_b = u.completion_tokens) !== null && _b !== void 0 ? _b : 0,
145
+ cachedTokens: (_d = (_c = u.prompt_tokens_details) === null || _c === void 0 ? void 0 : _c.cached_tokens) !== null && _d !== void 0 ? _d : 0
146
+ };
147
+ }
131
148
  function toOpenAi(m) {
132
149
  var _a;
133
150
  if (m.role === "assistant" && ((_a = m.tool_calls) === null || _a === void 0 ? void 0 : _a.length)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fonoster/community",
3
- "version": "0.30.0",
3
+ "version": "0.31.0",
4
4
  "description": "Vendor adapters (STT, TTS, LLM) behind shared interfaces, one subpath per vendor",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/fonoster#readme",
@@ -59,8 +59,7 @@
59
59
  },
60
60
  "scripts": {
61
61
  "prebuild": "rimraf ./dist tsconfig.tsbuildinfo",
62
- "build": "tsc -b tsconfig.json",
63
- "clean": "rimraf ./dist node_modules tsconfig.tsbuildinfo"
62
+ "build": "tsc -b tsconfig.json"
64
63
  },
65
64
  "dependencies": {
66
65
  "ws": "^8.18.0"
@@ -78,5 +77,5 @@
78
77
  "bugs": {
79
78
  "url": "https://github.com/fonoster/fonoster/issues"
80
79
  },
81
- "gitHead": "08c9cfd7932675b6b16111daa4d03f14b4e287d1"
80
+ "gitHead": "f0a5102dddeea0f70f659234108805bc10690f2e"
82
81
  }