@ropoctl/genai-node
v0.1.0
Published
Node.js N-API bindings for rust-genai
Downloads
294
Readme
@ropoctl/genai-node
Node.js N-API bindings for the Rust genai crate.
This mirrors the sibling genai-pyo3 package shape, but exposes a JavaScript API backed by a native .node addon. Bun supports Node-API/N-API native addons, so the same distribution model works for Node.js and Bun.
Build
npm install @ropoctl/genai-nodeFor local development:
npm install
npm run build:nativeThe build emits index.node, and index.js wraps it with a small ergonomic API.
TypeScript
The package ships index.d.ts, and package.json points to it with the types field.
import { Client, type ChatRequest } from './';
const request: ChatRequest = {
messages: [{ role: 'user', content: 'Say hello' }]
};
const response = await new Client().chat('gpt-4o-mini', request);
console.log(response.text);Type-check the examples:
npm run check:tsRun the TypeScript examples:
npm run example:ts:chat
npm run example:ts:streamQuick Start
const { Client } = require('./');
async function main() {
const client = Client.withApiKey('openai', process.env.OPENAI_API_KEY);
const response = await client.chat('gpt-4o-mini', {
messages: [{ role: 'user', content: 'Say hello in one short sentence' }]
}, {
temperature: 0.2
});
console.log(response.text);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});Streaming
const { Client } = require('./');
async function main() {
const client = new Client();
const stream = await client.streamChat('gpt-4o-mini', {
messages: [{ role: 'user', content: 'Write three short bullet points about Rust' }]
}, {
captureContent: true,
captureUsage: true
});
for await (const event of stream) {
if (event.kind === 'chunk') {
process.stdout.write(event.content);
}
if (event.kind === 'end') {
console.log('\nusage:', event.end && event.end.capturedUsage);
}
}
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});Provider Overrides
const client = Client.withApiKeyAndBaseUrl(
'gemini',
process.env.GEMINI_API_KEY,
'http://localhost:8080/v1beta'
);The provider names match the Rust genai adapter kinds: openai, openai_resp, gemini, anthropic, fireworks, together, groq, mimo, nebius, xai, deepseek, zai, bigmodel, aliyun, cohere, and ollama.
Layout
src/model.rs: shared Rust conversion andgenairequest/response mappingsrc/native.rs: N-API.nodebindingindex.js: JavaScript wrapperindex.d.ts: TypeScript declarations for the wrapperexamples/: small Node examples
