@bevyra/avatar 0.1.0 → 1.0.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/README.md +32 -0
- package/cli.js +25 -0
- package/index.d.ts +14 -21
- package/index.js +88 -95
- package/package.json +8 -4
- package/dist/assets/bevyra-core-Bskcl7l_.js +0 -195
- package/dist/assets/clientOnlyAvatarWorker-C6wTIDwp.js +0 -2119
- package/dist/assets/clientOnlySourceWorker-CNVdGzNy.js +0 -7347
- package/dist/assets/ort.wasm.min-BcBtKW3v.js +0 -1400
- package/dist/assets/ort.webgpu.min-BqBLsdhU.js +0 -1856
- package/dist/assets/rustAvatarWorker-vggdUXxI.js +0 -1702
- package/dist/assets/teacherCorrectorWorker-Deu0CCPs.js +0 -1406
- package/dist/assets/workerHelpers-BkWezgrb.js +0 -31
- package/dist/bevyra-avatar-sdk.js +0 -3367
- package/dist/ort/ort-wasm-simd-threaded.jsep.mjs +0 -136
- package/dist/ort/ort-wasm-simd-threaded.jsep.wasm +0 -0
- package/dist/ort/ort-wasm-simd-threaded.mjs +0 -68
- package/dist/ort/ort-wasm-simd-threaded.wasm +0 -0
- package/dist/runtime/bevyra-core.js +0 -1515
- package/dist/runtime/bevyra-core.wasm +0 -0
- package/dist/runtime/closure_router.onnx +0 -0
- package/dist/runtime/runtime-manifest.json +0 -27
- package/dist/sdk-manifest.json +0 -70
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @bevyra/avatar
|
|
2
|
+
|
|
3
|
+
Run a licensed BeVyra avatar on the same machine as your Node.js app. Supply your own ASR, LLM and TTS. Python 3.10+ is required; this package installs the prebuilt Python renderer wheel on first use.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @bevyra/avatar
|
|
7
|
+
export BEVYRA_API_KEY=your_project_key
|
|
8
|
+
npx bevyra-avatar activate
|
|
9
|
+
npx bevyra-avatar list
|
|
10
|
+
npx bevyra-avatar assign 1 noora
|
|
11
|
+
npx bevyra-avatar download noora
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`assign` applies to plans with avatar slots. `download` obtains signed URLs through BeVyra's API and verifies assets. No AWS credentials are needed.
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
import { AvatarClient } from '@bevyra/avatar';
|
|
18
|
+
const client = new AvatarClient({ apiKey: process.env.BEVYRA_API_KEY });
|
|
19
|
+
client.activate();
|
|
20
|
+
client.download('noora');
|
|
21
|
+
const { url, token } = await client.start();
|
|
22
|
+
const session = await client.connect('noora');
|
|
23
|
+
const clauseId = session.sendAudio(pcm16At16kHz);
|
|
24
|
+
for await (const event of session.events()) {
|
|
25
|
+
// clause.audio precedes binary PCM; clause.frame precedes binary JPEG.
|
|
26
|
+
// Frame N is scheduled at audio time N * 40 ms.
|
|
27
|
+
if (event.type === 'clause.done' && event.clauseId === clauseId) break;
|
|
28
|
+
}
|
|
29
|
+
session.close(); client.close();
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Other local processes can use `url` and `token` to connect to the loopback WebSocket. Keep the token private. `npx bevyra-avatar serve` prints them as JSON. Send complete speech units with natural pauses; `session.interrupt` cancels the current generation. See the [Python package protocol](../avatar-python/README.md).
|
package/cli.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { AvatarClient } from "./index.js";
|
|
3
|
+
|
|
4
|
+
const args = process.argv.slice(2);
|
|
5
|
+
const key = process.env.BEVYRA_API_KEY;
|
|
6
|
+
const client = new AvatarClient({ apiKey: key });
|
|
7
|
+
try {
|
|
8
|
+
const command = args.shift();
|
|
9
|
+
let result;
|
|
10
|
+
if (command === "activate") result = client.activate(args[0]);
|
|
11
|
+
else if (command === "list") result = client.avatars();
|
|
12
|
+
else if (command === "assign") result = client.assign(Number(args[0]), args[1], args.includes("--replace"));
|
|
13
|
+
else if (command === "download") result = client.download(args[0]);
|
|
14
|
+
else if (command === "serve") {
|
|
15
|
+
const { url, token } = await client.start({ port: Number(args[0] || 0) });
|
|
16
|
+
console.log(JSON.stringify({ url, token }));
|
|
17
|
+
process.on("SIGINT", () => { client.close(); process.exit(0); });
|
|
18
|
+
process.on("SIGTERM", () => { client.close(); process.exit(0); });
|
|
19
|
+
await new Promise(() => {});
|
|
20
|
+
} else throw new Error("Usage: bevyra-avatar activate|list|assign <slot> <avatar>|download <avatar>|serve");
|
|
21
|
+
if (result !== undefined) console.log(JSON.stringify(result));
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.error(error.message);
|
|
24
|
+
process.exitCode = 1;
|
|
25
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -1,25 +1,18 @@
|
|
|
1
|
-
export interface
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
onStage?: (stage: string) => void;
|
|
13
|
-
signal?: AbortSignal;
|
|
1
|
+
export interface AvatarClientOptions { apiKey?: string; dataDir?: string }
|
|
2
|
+
export type AvatarEvent = Record<string, unknown> | { type: "data"; bytes: Buffer };
|
|
3
|
+
export declare class AvatarClient {
|
|
4
|
+
constructor(options?: AvatarClientOptions);
|
|
5
|
+
activate(name?: string): { deployment_id: string; plan?: string };
|
|
6
|
+
avatars(): Array<{ id: string; name: string; slot: number | null }>;
|
|
7
|
+
assign(slot: number, avatar: string, replace?: boolean): Record<string, unknown>;
|
|
8
|
+
download(avatar: string): { avatar: string; path: string };
|
|
9
|
+
start(options?: { port?: number }): Promise<{ url: string; token: string }>;
|
|
10
|
+
connect(avatar: string): Promise<AvatarSession>;
|
|
11
|
+
close(): void;
|
|
14
12
|
}
|
|
15
|
-
export declare class
|
|
16
|
-
|
|
17
|
-
writeAudio(pcm: Int16Array, sampleRate: number): Promise<void>;
|
|
18
|
-
endUtterance(): Promise<void>;
|
|
13
|
+
export declare class AvatarSession {
|
|
14
|
+
sendAudio(pcm: Buffer | Int16Array): number;
|
|
19
15
|
interrupt(): void;
|
|
20
|
-
onSpeechStart(listener: () => void): () => void;
|
|
21
|
-
onSpeechEnd(listener: () => void): () => void;
|
|
22
|
-
speechClock(): { response: number; complete: boolean; receivedSeconds: number; heardSeconds: number } | null;
|
|
23
16
|
close(): void;
|
|
17
|
+
events(): AsyncIterable<AvatarEvent>;
|
|
24
18
|
}
|
|
25
|
-
export declare function loadLocalAssets(options: Pick<AvatarOptions, "avatarId" | "bundleBaseUrl" | "assetsBaseUrl" | "signal">): Promise<{bundle: unknown; runtime: unknown}>;
|
package/index.js
CHANGED
|
@@ -1,108 +1,101 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/** Node client for the Python renderer running on the same device. */
|
|
2
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
3
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import readline from "node:readline";
|
|
7
|
+
import WebSocket from "ws";
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
return new URL(url.endsWith("/") ? url : `${url}/`, globalThis.location?.href);
|
|
9
|
-
}
|
|
9
|
+
const VERSION = "1.0.0";
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
function python() {
|
|
12
|
+
if (process.env.BEVYRA_PYTHON) return { executable: process.env.BEVYRA_PYTHON, prefix: [] };
|
|
13
|
+
const root = path.join(homedir(), ".cache", "bevyra-avatar", `python-${VERSION}`);
|
|
14
|
+
const executable = path.join(root, process.platform === "win32" ? "Scripts/python.exe" : "bin/python");
|
|
15
|
+
if (!existsSync(executable)) {
|
|
16
|
+
mkdirSync(path.dirname(root), { recursive: true });
|
|
17
|
+
const system = process.platform === "win32" ? "py" : "python3";
|
|
18
|
+
const prefix = process.platform === "win32" ? ["-3"] : [];
|
|
19
|
+
const created = spawnSync(system, [...prefix, "-m", "venv", root], { stdio: "inherit" });
|
|
20
|
+
if (created.status !== 0) throw new Error("Python 3.10+ is required to install the local renderer");
|
|
21
|
+
}
|
|
22
|
+
const marker = path.join(root, `bevyra-${VERSION}.ready`);
|
|
23
|
+
if (!existsSync(marker)) {
|
|
24
|
+
const installed = spawnSync(executable, ["-m", "pip", "install", "--only-binary=:all:", `bevyra-avatar==${VERSION}`], { stdio: "inherit" });
|
|
25
|
+
if (installed.status !== 0) throw new Error("Could not install the prebuilt BeVyra Python renderer wheel");
|
|
26
|
+
writeFileSync(marker, VERSION);
|
|
27
|
+
}
|
|
28
|
+
return { executable, prefix: [] };
|
|
14
29
|
}
|
|
15
30
|
|
|
16
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
if (
|
|
21
|
-
|
|
22
|
-
return bytes;
|
|
31
|
+
function cli(args, { apiKey, dataDir } = {}) {
|
|
32
|
+
const { executable } = python();
|
|
33
|
+
const command = ["-m", "bevyra_avatar.cli", ...(dataDir ? ["--data-dir", dataDir] : []), ...args];
|
|
34
|
+
const result = spawnSync(executable, command, { encoding: "utf8", env: { ...process.env, ...(apiKey ? { BEVYRA_API_KEY: apiKey } : {}) } });
|
|
35
|
+
if (result.status !== 0) throw new Error((result.stderr || result.stdout || "BeVyra command failed").trim());
|
|
36
|
+
return JSON.parse(result.stdout);
|
|
23
37
|
}
|
|
24
38
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
export class AvatarClient {
|
|
40
|
+
constructor({ apiKey, dataDir } = {}) { this.apiKey = apiKey; this.dataDir = dataDir; this.server = null; }
|
|
41
|
+
activate(name = "local-avatar") { return cli(["activate", "--name", name], this); }
|
|
42
|
+
avatars() { return cli(["list"], this).avatars; }
|
|
43
|
+
assign(slot, avatar, replace = false) { return cli(["assign", String(slot), avatar, ...(replace ? ["--replace"] : [])], this); }
|
|
44
|
+
download(avatar) { return cli(["download", avatar], this); }
|
|
45
|
+
async start({ port = 0 } = {}) {
|
|
46
|
+
if (this.server) throw new Error("Local renderer already started");
|
|
47
|
+
const { executable } = python();
|
|
48
|
+
const args = ["-m", "bevyra_avatar.cli", ...(this.dataDir ? ["--data-dir", this.dataDir] : []), "serve", "--port", String(port)];
|
|
49
|
+
const child = spawn(executable, args, { env: process.env, stdio: ["ignore", "pipe", "inherit"] });
|
|
50
|
+
const lines = readline.createInterface({ input: child.stdout });
|
|
51
|
+
let timer;
|
|
52
|
+
let ready;
|
|
53
|
+
try { ready = await Promise.race([
|
|
54
|
+
new Promise((resolve, reject) => {
|
|
55
|
+
lines.once("line", line => { try { resolve(JSON.parse(line)); } catch { reject(new Error("Invalid Python renderer startup")); } });
|
|
56
|
+
child.once("exit", code => reject(new Error(`Python renderer exited (${code})`)));
|
|
57
|
+
}),
|
|
58
|
+
new Promise((_, reject) => { timer = setTimeout(() => reject(new Error("Python renderer startup timed out")), 90000); }),
|
|
59
|
+
]); }
|
|
60
|
+
catch (error) { child.kill(); throw error; }
|
|
61
|
+
finally { clearTimeout(timer); }
|
|
62
|
+
lines.close();
|
|
63
|
+
this.server = { ...ready, child };
|
|
64
|
+
return { url: ready.url, token: ready.token };
|
|
43
65
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
66
|
+
connect(avatar) {
|
|
67
|
+
if (!this.server) throw new Error("Start the local renderer first");
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
69
|
+
const url = new URL("/v1/stream", this.server.url); url.protocol = "ws:";
|
|
70
|
+
const socket = new WebSocket(url, { headers: { Authorization: `Bearer ${this.server.token}` } });
|
|
71
|
+
socket.once("open", () => { socket.send(JSON.stringify({ type: "session.setup", avatar })); resolve(new AvatarSession(socket)); });
|
|
72
|
+
socket.once("error", reject);
|
|
73
|
+
});
|
|
52
74
|
}
|
|
53
|
-
|
|
54
|
-
if (runtimeManifest.schema !== "bevyra-rust-browser-core-v1") throw new Error("Unsupported avatar runtime");
|
|
55
|
-
const runtimeFiles = {};
|
|
56
|
-
for (const [name, entry] of Object.entries(runtimeManifest.files))
|
|
57
|
-
runtimeFiles[name] = { ...entry, url: new URL(`runtime/${name}`, assetsRoot).href };
|
|
58
|
-
if (!runtimeFiles["bevyra-core.wasm"] || !runtimeFiles["closure_router.onnx"])
|
|
59
|
-
throw new Error("Avatar runtime is incomplete");
|
|
60
|
-
const [coreWasm, closureRouter] = await Promise.all([
|
|
61
|
-
download(runtimeFiles["bevyra-core.wasm"].url, runtimeFiles["bevyra-core.wasm"], signal),
|
|
62
|
-
download(runtimeFiles["closure_router.onnx"].url, runtimeFiles["closure_router.onnx"], signal),
|
|
63
|
-
]);
|
|
64
|
-
const bundleFiles = {};
|
|
65
|
-
for (const [name, entry] of Object.entries(checksums))
|
|
66
|
-
bundleFiles[name] = { ...entry, url: new URL(name, bundleRoot).href };
|
|
67
|
-
const bundleId = await digest(checksumsBytes);
|
|
68
|
-
return {
|
|
69
|
-
bundle: {
|
|
70
|
-
contract: { bundleId, avatarId, format: "portable-ort-v2", expiresAt: "9999-12-31T00:00:00Z",
|
|
71
|
-
entrypoint: "web-bundle.json", files: bundleFiles },
|
|
72
|
-
files, manifests, cache: "miss", bytes: Array.from(files.values()).reduce((n, x) => n + x.byteLength, 0),
|
|
73
|
-
downloadMs: 0, bundleSha256: bundleId,
|
|
74
|
-
},
|
|
75
|
-
runtime: {
|
|
76
|
-
contract: { runtimeId: runtimeManifest.files["bevyra-core.wasm"].sha256, schema: runtimeManifest.schema,
|
|
77
|
-
expiresAt: "9999-12-31T00:00:00Z", capabilities: {
|
|
78
|
-
bundleFormats: runtimeManifest.bundleFormats, planner: runtimeManifest.planner,
|
|
79
|
-
audioFrontend: runtimeManifest.audioFrontend, simd128: runtimeManifest.simd128,
|
|
80
|
-
}, files: runtimeFiles },
|
|
81
|
-
coreWasm, closureRouter, cache: "miss", bytes: coreWasm.byteLength + closureRouter.byteLength, downloadMs: 0,
|
|
82
|
-
},
|
|
83
|
-
};
|
|
75
|
+
close() { if (this.server) { this.server.child.kill(); this.server = null; } }
|
|
84
76
|
}
|
|
85
77
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
background: options.background, resolution: options.resolution, body: options.body,
|
|
94
|
-
onStage: options.onStage, signal: options.signal, renderer: "device",
|
|
95
|
-
ortWasmPaths: new URL("ort/", directory(options.assetsBaseUrl)).href,
|
|
96
|
-
assetProvider: () => loadLocalAssets(options),
|
|
78
|
+
export class AvatarSession {
|
|
79
|
+
constructor(socket) {
|
|
80
|
+
this.socket = socket; this.pending = [];
|
|
81
|
+
this.waiters = []; this.closed = false; this.generation = 0; this.clauseId = 0;
|
|
82
|
+
socket.on("message", (data, binary) => {
|
|
83
|
+
const item = binary ? { type: "data", bytes: Buffer.from(data) } : JSON.parse(data.toString());
|
|
84
|
+
const waiter = this.waiters.shift(); waiter ? waiter(item) : this.pending.push(item);
|
|
97
85
|
});
|
|
98
|
-
|
|
99
|
-
|
|
86
|
+
socket.on("close", () => { this.closed = true; while (this.waiters.length) this.waiters.shift()(null); });
|
|
87
|
+
}
|
|
88
|
+
sendAudio(pcm) {
|
|
89
|
+
if (!(Buffer.isBuffer(pcm) || pcm instanceof Int16Array)) throw new TypeError("Expected PCM16 Buffer or Int16Array");
|
|
90
|
+
const bytes = Buffer.isBuffer(pcm) ? pcm : Buffer.from(pcm.buffer, pcm.byteOffset, pcm.byteLength);
|
|
91
|
+
if (bytes.length % 2) throw new Error("PCM16 must have an even byte length");
|
|
92
|
+
const clauseId = ++this.clauseId;
|
|
93
|
+
this.socket.send(JSON.stringify({ type: "clause.start", clauseId, generation: this.generation, sampleCount: bytes.length / 2 }));
|
|
94
|
+
this.socket.send(bytes, { binary: true });
|
|
95
|
+
this.socket.send(JSON.stringify({ type: "clause.end", clauseId }));
|
|
96
|
+
return clauseId;
|
|
100
97
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
onSpeechStart(listener) { return this.sdk.onSpeechStart(listener); }
|
|
105
|
-
onSpeechEnd(listener) { return this.sdk.onSpeechEnd(listener); }
|
|
106
|
-
speechClock() { return this.sdk.speechClock(); }
|
|
107
|
-
close() { this.sdk.close(); }
|
|
98
|
+
interrupt() { this.generation++; this.socket.send(JSON.stringify({ type: "session.interrupt", generation: this.generation })); }
|
|
99
|
+
close() { this.socket.close(); }
|
|
100
|
+
async *events() { while (!this.closed || this.pending.length) { const event = this.pending.length ? this.pending.shift() : await new Promise(resolve => this.waiters.push(resolve)); if (event === null) return; yield event; } }
|
|
108
101
|
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bevyra/avatar",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "API-authorized local BeVyra avatar service for Node.js",
|
|
5
|
+
"homepage": "https://bevyra.ai/docs",
|
|
6
|
+
"bugs": {"url": "https://bevyra.ai/docs"},
|
|
5
7
|
"type": "module",
|
|
6
8
|
"exports": {".": {"types": "./index.d.ts", "default": "./index.js"}},
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
+
"bin": {"bevyra-avatar": "./cli.js"},
|
|
10
|
+
"files": ["index.js", "index.d.ts", "cli.js", "README.md"],
|
|
11
|
+
"scripts": {"test": "node --test test/local.test.mjs", "pack:check": "npm pack --dry-run --json"},
|
|
9
12
|
"engines": {"node": ">=20"},
|
|
13
|
+
"dependencies": {"ws": "^8.18.0"},
|
|
10
14
|
"license": "Proprietary",
|
|
11
15
|
"publishConfig": {"access": "public"}
|
|
12
16
|
}
|