@fonoster/community 0.0.0-stage → 0.27.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.
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
3
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
4
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
5
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
6
+ return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
7
+ function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
8
+ function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
9
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
10
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
11
+ function fulfill(value) { resume("next", value); }
12
+ function reject(value) { resume("throw", value); }
13
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
14
+ };
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.OpenAiCompatibleThink = exports.OPENAI_COMPATIBLE = void 0;
17
+ exports.resolveThink = resolveThink;
18
+ /**
19
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
20
+ * http://github.com/fonoster/fonoster
21
+ *
22
+ * This file is part of Fonoster
23
+ *
24
+ * Licensed under the MIT License (the "License");
25
+ * you may not use this file except in compliance with
26
+ * the License. You may obtain a copy of the License at
27
+ *
28
+ * https://opensource.org/licenses/MIT
29
+ *
30
+ * Unless required by applicable law or agreed to in writing, software
31
+ * distributed under the License is distributed on an "AS IS" BASIS,
32
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33
+ * See the License for the specific language governing permissions and
34
+ * limitations under the License.
35
+ */
36
+ const util_1 = require("util");
37
+ const llm_1 = require("../llm");
38
+ // OpenAI-compatible Chat Completions (rework-autopilot A8), which covers OpenAI, Groq, Ollama and
39
+ // the conformance fake LLM. Registered for those catalog adapters in ../adapters.ts.
40
+ /** Adapters whose LLMs speak OpenAI-compatible Chat Completions, with their default endpoints. */
41
+ exports.OPENAI_COMPATIBLE = {
42
+ openai: "https://api.openai.com",
43
+ groq: "https://api.groq.com/openai",
44
+ ollama: "http://localhost:11434",
45
+ fake: undefined
46
+ };
47
+ class OpenAiCompatibleThink {
48
+ constructor(provider, model, options) {
49
+ this.provider = provider;
50
+ this.model = model;
51
+ this.options = options;
52
+ }
53
+ stream(messages, tools, signal) {
54
+ return __asyncGenerator(this, arguments, function* stream_1() {
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
57
+ ? { tools: tools.map((t) => ({ type: "function", function: t })) }
58
+ : {})), (this.options.jsonMode
59
+ ? { response_format: { type: "json_object" } }
60
+ : {}));
61
+ let res;
62
+ try {
63
+ res = yield __await(fetch(`${this.provider.baseUrl.replace(/\/$/, "")}/v1/chat/completions`, {
64
+ method: "POST",
65
+ headers: Object.assign({ "content-type": "application/json" }, (this.provider.apiKey
66
+ ? { authorization: `Bearer ${this.provider.apiKey}` }
67
+ : {})),
68
+ body: JSON.stringify(body),
69
+ signal
70
+ }));
71
+ }
72
+ catch (e) {
73
+ if (signal.aborted)
74
+ throw e;
75
+ throw new llm_1.ThinkError("provider_unavailable", `language model unreachable: ${e.message}`);
76
+ }
77
+ if (res.status === 400 || res.status === 422)
78
+ throw new llm_1.ThinkError("bad_message", `language model rejected the request (HTTP ${res.status})`);
79
+ if (!res.ok || !res.body)
80
+ throw new llm_1.ThinkError("provider_unavailable", `language model answered HTTP ${res.status}`);
81
+ const calls = new Map();
82
+ const decoder = new util_1.TextDecoder();
83
+ let buffer = "";
84
+ const reader = res.body.getReader();
85
+ for (;;) {
86
+ let next;
87
+ try {
88
+ next = yield __await(reader.read());
89
+ }
90
+ catch (e) {
91
+ if (signal.aborted)
92
+ throw e;
93
+ throw new llm_1.ThinkError("provider_unavailable", `language model stream failed: ${e.message}`);
94
+ }
95
+ const { done, value } = next;
96
+ if (done)
97
+ break;
98
+ buffer += decoder.decode(value, { stream: true });
99
+ let newline;
100
+ while ((newline = buffer.indexOf("\n")) >= 0) {
101
+ const line = buffer.slice(0, newline).trim();
102
+ buffer = buffer.slice(newline + 1);
103
+ if (!line.startsWith("data:"))
104
+ continue;
105
+ const data = line.slice(5).trim();
106
+ if (data === "[DONE]")
107
+ 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 : {};
109
+ if (delta.content)
110
+ yield yield __await({ type: "text", text: delta.content });
111
+ for (const tc of (_d = delta.tool_calls) !== null && _d !== void 0 ? _d : []) {
112
+ const call = (_e = calls.get(tc.index)) !== null && _e !== void 0 ? _e : {
113
+ id: (_f = tc.id) !== null && _f !== void 0 ? _f : `call_${tc.index}`,
114
+ name: "",
115
+ arguments: ""
116
+ };
117
+ if ((_g = tc.function) === null || _g === void 0 ? void 0 : _g.name)
118
+ call.name = tc.function.name;
119
+ if ((_h = tc.function) === null || _h === void 0 ? void 0 : _h.arguments)
120
+ call.arguments += tc.function.arguments;
121
+ calls.set(tc.index, call);
122
+ }
123
+ }
124
+ }
125
+ if (calls.size)
126
+ yield yield __await({ type: "tool_calls", calls: [...calls.values()] });
127
+ });
128
+ }
129
+ }
130
+ exports.OpenAiCompatibleThink = OpenAiCompatibleThink;
131
+ function toOpenAi(m) {
132
+ var _a;
133
+ if (m.role === "assistant" && ((_a = m.tool_calls) === null || _a === void 0 ? void 0 : _a.length)) {
134
+ return {
135
+ role: "assistant",
136
+ content: m.content || null,
137
+ tool_calls: m.tool_calls.map((c) => ({
138
+ id: c.id,
139
+ type: "function",
140
+ function: { name: c.name, arguments: c.arguments }
141
+ }))
142
+ };
143
+ }
144
+ return m;
145
+ }
146
+ /** Resolves a catalog id (`<provider>/<model>`) against configured providers. */
147
+ function resolveThink(catalogId, providers, options) {
148
+ const slash = catalogId.indexOf("/");
149
+ const providerName = catalogId.slice(0, slash);
150
+ const provider = providers[providerName];
151
+ if (!provider)
152
+ throw new Error(`no language model provider "${providerName}" for ${catalogId}`);
153
+ return new OpenAiCompatibleThink(provider, catalogId.slice(slash + 1), options);
154
+ }
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
3
+ * http://github.com/fonoster/fonoster
4
+ *
5
+ * This file is part of Fonoster
6
+ *
7
+ * Licensed under the MIT License (the "License");
8
+ * you may not use this file except in compliance with
9
+ * the License. You may obtain a copy of the License at
10
+ *
11
+ * https://opensource.org/licenses/MIT
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ import { EventEmitter } from "events";
20
+ export type WordTiming = {
21
+ word: string;
22
+ startMs: number;
23
+ endMs: number;
24
+ };
25
+ /** Provider failures, named after the session error codes they become (VS17). */
26
+ export declare class SpeechError extends Error {
27
+ readonly code: "provider_unavailable" | "provider_timeout" | "bad_message" | "internal";
28
+ constructor(code: "provider_unavailable" | "provider_timeout" | "bad_message" | "internal", message: string);
29
+ }
30
+ export type Synthesis = {
31
+ /** Internal-format audio (PCM16LE, 16 kHz) as it arrives. */
32
+ audio: AsyncIterable<Buffer>;
33
+ /** Word timings relative to the start of this audio, when the provider reports them. */
34
+ words?: WordTiming[];
35
+ };
36
+ export interface TtsAdapter {
37
+ synthesize(request: {
38
+ text: string;
39
+ voice: string;
40
+ language: string;
41
+ }, signal: AbortSignal): Promise<Synthesis>;
42
+ }
43
+ export type Transcript = {
44
+ text: string;
45
+ final: boolean;
46
+ language: string;
47
+ confidence: number;
48
+ startMs?: number;
49
+ endMs?: number;
50
+ /** Per-word timings when the recognizer reports them (catalog feature `word-timings`). */
51
+ words?: WordTiming[];
52
+ /**
53
+ * Turn signals from a turn-aware recognizer (catalog feature `turn-detection`): its own
54
+ * end-of-thought estimate and whether it declared the end of the turn. Absent otherwise.
55
+ */
56
+ turn?: {
57
+ eot?: number;
58
+ endOfTurn?: boolean;
59
+ };
60
+ };
61
+ /** One streaming recognition session. Emits "transcript" (Transcript) and "error" (SpeechError). */
62
+ export interface SttStream extends EventEmitter {
63
+ write(frame: Buffer): void;
64
+ update(options: {
65
+ language?: string;
66
+ hints?: string[];
67
+ }): void;
68
+ close(): void;
69
+ }
70
+ export interface SttAdapter {
71
+ /** `model` is the provider's own model name (catalog id `<provider>/<model>`). */
72
+ open(options: {
73
+ language: string;
74
+ hints?: string[];
75
+ model?: string;
76
+ }): SttStream;
77
+ }
package/dist/speech.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SpeechError = void 0;
4
+ /** Provider failures, named after the session error codes they become (VS17). */
5
+ class SpeechError extends Error {
6
+ constructor(code, message) {
7
+ super(message);
8
+ this.code = code;
9
+ }
10
+ }
11
+ exports.SpeechError = SpeechError;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
3
+ * http://github.com/fonoster/fonoster
4
+ *
5
+ * This file is part of Fonoster
6
+ *
7
+ * Licensed under the MIT License (the "License");
8
+ * you may not use this file except in compliance with
9
+ * the License. You may obtain a copy of the License at
10
+ *
11
+ * https://opensource.org/licenses/MIT
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ export * from "./llm";
20
+ export * from "./speech";
package/dist/types.js ADDED
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ /**
18
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
19
+ * http://github.com/fonoster/fonoster
20
+ *
21
+ * This file is part of Fonoster
22
+ *
23
+ * Licensed under the MIT License (the "License");
24
+ * you may not use this file except in compliance with
25
+ * the License. You may obtain a copy of the License at
26
+ *
27
+ * https://opensource.org/licenses/MIT
28
+ *
29
+ * Unless required by applicable law or agreed to in writing, software
30
+ * distributed under the License is distributed on an "AS IS" BASIS,
31
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32
+ * See the License for the specific language governing permissions and
33
+ * limitations under the License.
34
+ */
35
+ // The shared adapter interfaces: speech (STT, TTS) and language models (LLM).
36
+ __exportStar(require("./llm"), exports);
37
+ __exportStar(require("./speech"), exports);
package/package.json CHANGED
@@ -1,6 +1,81 @@
1
1
  {
2
2
  "name": "@fonoster/community",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.27.0",
4
+ "description": "Vendor adapters (STT, TTS, LLM) behind shared interfaces, one subpath per vendor",
5
+ "author": "Pedro Sanders <psanders@fonoster.com>",
6
+ "homepage": "https://github.com/fonoster/fonoster#readme",
7
+ "license": "MIT",
8
+ "main": "dist/index",
9
+ "types": "dist/index",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "./types": {
16
+ "types": "./dist/types.d.ts",
17
+ "default": "./dist/types.js"
18
+ },
19
+ "./deepgram": {
20
+ "types": "./dist/deepgram/index.d.ts",
21
+ "default": "./dist/deepgram/index.js"
22
+ },
23
+ "./elevenlabs": {
24
+ "types": "./dist/elevenlabs/index.d.ts",
25
+ "default": "./dist/elevenlabs/index.js"
26
+ },
27
+ "./openai": {
28
+ "types": "./dist/openai/index.d.ts",
29
+ "default": "./dist/openai/index.js"
30
+ },
31
+ "./fake": {
32
+ "types": "./dist/fake/index.d.ts",
33
+ "default": "./dist/fake/index.js"
34
+ },
35
+ "./package.json": "./package.json"
36
+ },
37
+ "typesVersions": {
38
+ "*": {
39
+ "types": [
40
+ "dist/types.d.ts"
41
+ ],
42
+ "deepgram": [
43
+ "dist/deepgram/index.d.ts"
44
+ ],
45
+ "elevenlabs": [
46
+ "dist/elevenlabs/index.d.ts"
47
+ ],
48
+ "openai": [
49
+ "dist/openai/index.d.ts"
50
+ ],
51
+ "fake": [
52
+ "dist/fake/index.d.ts"
53
+ ]
54
+ }
55
+ },
56
+ "directories": {
57
+ "src": "src",
58
+ "test": "test"
59
+ },
60
+ "scripts": {
61
+ "prebuild": "rimraf ./dist tsconfig.tsbuildinfo",
62
+ "build": "tsc -b tsconfig.json",
63
+ "clean": "rimraf ./dist node_modules tsconfig.tsbuildinfo"
64
+ },
65
+ "dependencies": {
66
+ "ws": "^8.18.0"
67
+ },
68
+ "files": [
69
+ "dist"
70
+ ],
71
+ "publishConfig": {
72
+ "access": "public"
73
+ },
74
+ "repository": {
75
+ "type": "git",
76
+ "url": "git+https://github.com/fonoster/fonoster.git"
77
+ },
78
+ "bugs": {
79
+ "url": "https://github.com/fonoster/fonoster/issues"
80
+ }
81
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.