Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Tool calls typescript (#664)

Aleksandr Golokoz6a85f5536f50
diff --git a/bindings/typescript/examples/toolCalls.ts b/bindings/typescript/examples/toolCalls.ts
new file mode 100644
index 000000000..d209c4d7c
--- /dev/null
+++ b/bindings/typescript/examples/toolCalls.ts
@@ -0,0 +1,82 @@
+import {
+ ChatConfig,
+ ChatMessage,
+ ChatReplyConfig,
+ Engine,
+ EngineConfig,
+ SamplingMethodGreedy,
+ SamplingPolicyCustom,
+ uzuToolFunction,
+} from '@trymirai/uzu';
+import * as z from 'zod';
+
+
+const Coordinate = z.object({
+ latitude: z.number().describe('Latitude in decimal degrees.'),
+ longitude: z.number().describe('Longitude in decimal degrees.'),
+});
+
+type Coordinate = z.infer<typeof Coordinate>;
+
+
+const getCurrentLocation = uzuToolFunction({
+ name: 'get_location',
+ description: 'Return the current location in coordinates',
+ parameters: z.object({}),
+ returns: Coordinate,
+ handler: (): Coordinate => ({
+ latitude: 51.5074,
+ longitude: -0.1278,
+ }),
+});
+
+
+async function calculateCurrentTemperature({latitude, longitude}: Coordinate): Promise<number> {
+ if (!Number.isFinite(Math.hypot(latitude, longitude))) {
+ throw new RangeError('Coordinates must be finite');
+ }
+ return 25;
+}
+
+const getCurrentTemperature = uzuToolFunction({
+ name: 'get_current_temperature',
+ description: 'Return the temperature at the provided coordinates',
+ parameters: Coordinate,
+ returns: z.number(),
+ handler: calculateCurrentTemperature,
+});
+
+
+async function main() {
+ const engine = await Engine.create(EngineConfig.create());
+ const model = await engine.model('mlx-community/Qwen3.5-9B-MLX-8bit');
+ if (!model) {
+ throw new Error('Model not found');
+ }
+
+ for await (const update of await engine.download(model)) {
+ console.log('Download progress:', update.progress);
+ }
+
+ const session = await engine.chat(model, ChatConfig.create());
+ await session.addTool(getCurrentLocation);
+ await session.addTool(getCurrentTemperature);
+
+ const messages = [
+ ChatMessage.system().withText('You are a helpful assistant'),
+ ChatMessage.user().withText('What temperature is it now at my location?'),
+ ];
+ const config = ChatReplyConfig.create().withSamplingPolicy(
+ new SamplingPolicyCustom(new SamplingMethodGreedy()),
+ );
+ const replies = await session.reply(messages, config);
+ const message = replies[replies.length - 1]?.message;
+ if (message) {
+ console.log('Reasoning:', message.reasoning ?? '');
+ console.log('Text:', message.text ?? '');
+ }
+}
+
+main().catch((error: unknown) => {
+ console.error(error);
+});
diff --git a/bindings/typescript/package.json b/bindings/typescript/package.json
index 2decc50d0..19f1cab2a 100644
--- a/bindings/typescript/package.json
+++ b/bindings/typescript/package.json
@@ -54,7 +54,7 @@
"publint": "^0.2.12",
"ts-jest": "^29.1.0",
"ts-node": "^10.5.0",
- "tsc-multi": "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.9/tsc-multi.tgz",
+ "tsc-multi": "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.11/tsc-multi.tgz",
"tsconfig-paths": "^4.0.0",
"tslib": "^2.8.1",
"typescript": "5.8.3",
diff --git a/bindings/typescript/pnpm-lock.yaml b/bindings/typescript/pnpm-lock.yaml
index 0925b8977..efc4a6eb2 100644
--- a/bindings/typescript/pnpm-lock.yaml
+++ b/bindings/typescript/pnpm-lock.yaml
@@ -63,8 +63,8 @@ importers:
specifier: ^10.5.0
tsc-multi:
- specifier: https://github.com/stainless-api/tsc-multi/releases/download/v1.1.9/tsc-multi.tgz
- version: https://github.com/stainless-api/tsc-multi/releases/download/v1.1.9/tsc-multi.tgz([email protected])
+ specifier: https://github.com/stainless-api/tsc-multi/releases/download/v1.1.11/tsc-multi.tgz
+ version: https://github.com/stainless-api/tsc-multi/releases/download/v1.1.11/tsc-multi.tgz([email protected])
tsconfig-paths:
specifier: ^4.0.0
version: 4.2.0
@@ -4098,10 +4098,10 @@ packages:
'@swc/wasm':
optional: true
- tsc-multi@https://github.com/stainless-api/tsc-multi/releases/download/v1.1.9/tsc-multi.tgz:
+ tsc-multi@https://github.com/stainless-api/tsc-multi/releases/download/v1.1.11/tsc-multi.tgz:
resolution:
- { tarball: https://github.com/stainless-api/tsc-multi/releases/download/v1.1.9/tsc-multi.tgz }
- version: 1.1.9
+ { tarball: https://github.com/stainless-api/tsc-multi/releases/download/v1.1.11/tsc-multi.tgz }
+ version: 1.1.11
engines: { node: '>=14' }
hasBin: true
peerDependencies:
@@ -7017,7 +7017,7 @@ snapshots:
optionalDependencies:
'@swc/core': 1.13.5
- tsc-multi@https://github.com/stainless-api/tsc-multi/releases/download/v1.1.9/tsc-multi.tgz([email protected]):
+ tsc-multi@https://github.com/stainless-api/tsc-multi/releases/download/v1.1.11/tsc-multi.tgz([email protected]):
dependencies:
debug: 4.4.3
fast-glob: 3.3.3
diff --git a/bindings/typescript/src/index.ts b/bindings/typescript/src/index.ts
index aa9a87e51..1f0a258c1 100644
--- a/bindings/typescript/src/index.ts
+++ b/bindings/typescript/src/index.ts
@@ -1 +1,2 @@
export * from './napi/index';
+export * from './tool';
diff --git a/bindings/typescript/src/napi/index.d.mts b/bindings/typescript/src/napi/index.d.mts
index f8dad3205..db5e78aff 100644
--- a/bindings/typescript/src/napi/index.d.mts
+++ b/bindings/typescript/src/napi/index.d.mts
@@ -1,6 +1,8 @@
/* auto-generated by NAPI-RS */
/* eslint-disable */
export declare class ChatSession {
+ addTool(tool: NativeTool): Promise<undefined>
+ addTools(tools: Array<NativeTool>): Promise<undefined>
get state(): Promise<ChatSessionState>
get messages(): Promise<Array<ChatMessage>>
reset(): Promise<void>
@@ -35,6 +37,10 @@ export declare class ClassificationSession {
classify(input: Array<ClassificationMessage>): Promise<ClassificationOutput>
}
+export declare class NativeTool {
+ constructor(definition: ToolFunction, invokeJson: (arg0: string, arg1: string) => Promise<string>, cancel: (arg: string) => void)
+}
+
export declare class TextToSpeechSession {
get state(): Promise<TextToSpeechSessionState>
synthesize(input: string): Promise<TextToSpeechOutput>
diff --git a/bindings/typescript/src/napi/index.d.ts b/bindings/typescript/src/napi/index.d.ts
index f8dad3205..db5e78aff 100644
--- a/bindings/typescript/src/napi/index.d.ts
+++ b/bindings/typescript/src/napi/index.d.ts
@@ -1,6 +1,8 @@
/* auto-generated by NAPI-RS */
/* eslint-disable */
export declare class ChatSession {
+ addTool(tool: NativeTool): Promise<undefined>
+ addTools(tools: Array<NativeTool>): Promise<undefined>
get state(): Promise<ChatSessionState>
get messages(): Promise<Array<ChatMessage>>
reset(): Promise<void>
@@ -35,6 +37,10 @@ export declare class ClassificationSession {
classify(input: Array<ClassificationMessage>): Promise<ClassificationOutput>
}
+export declare class NativeTool {
+ constructor(definition: ToolFunction, invokeJson: (arg0: string, arg1: string) => Promise<string>, cancel: (arg: string) => void)
+}
+
export declare class TextToSpeechSession {
get state(): Promise<TextToSpeechSessionState>
synthesize(input: string): Promise<TextToSpeechOutput>
diff --git a/bindings/typescript/src/napi/index.js b/bindings/typescript/src/napi/index.js
index d125d56d4..d646fefb3 100644
--- a/bindings/typescript/src/napi/index.js
+++ b/bindings/typescript/src/napi/index.js
@@ -513,6 +513,7 @@ module.exports.ChatSessionStream = nativeBinding.ChatSessionStream
module.exports.ChatSessionStreamChunkError = nativeBinding.ChatSessionStreamChunkError
module.exports.ChatSessionStreamChunkReplies = nativeBinding.ChatSessionStreamChunkReplies
module.exports.ClassificationSession = nativeBinding.ClassificationSession
+module.exports.NativeTool = nativeBinding.NativeTool
module.exports.TextToSpeechSession = nativeBinding.TextToSpeechSession
module.exports.TextToSpeechSessionStream = nativeBinding.TextToSpeechSessionStream
module.exports.TextToSpeechSessionStreamChunkError = nativeBinding.TextToSpeechSessionStreamChunkError
diff --git a/bindings/typescript/src/napi/index.mjs b/bindings/typescript/src/napi/index.mjs
index ffff55107..6f020d6ba 100644
--- a/bindings/typescript/src/napi/index.mjs
+++ b/bindings/typescript/src/napi/index.mjs
@@ -10,6 +10,7 @@ export const ChatSessionStream = cjs.ChatSessionStream;
export const ChatSessionStreamChunkError = cjs.ChatSessionStreamChunkError;
export const ChatSessionStreamChunkReplies = cjs.ChatSessionStreamChunkReplies;
export const ClassificationSession = cjs.ClassificationSession;
+export const NativeTool = cjs.NativeTool;
export const TextToSpeechSession = cjs.TextToSpeechSession;
export const TextToSpeechSessionStream = cjs.TextToSpeechSessionStream;
export const TextToSpeechSessionStreamChunkError = cjs.TextToSpeechSessionStreamChunkError;
diff --git a/bindings/typescript/src/tool.ts b/bindings/typescript/src/tool.ts
new file mode 100644
index 000000000..4ce0a4131
--- /dev/null
+++ b/bindings/typescript/src/tool.ts
@@ -0,0 +1,144 @@
+import * as z from 'zod';
+
+import { NativeTool, ToolFunction, Value } from './napi/index';
+
+const CANCELLED_INVOCATION_RETENTION_MS = 30_000;
+
+export interface UzuToolContext {
+ readonly signal: AbortSignal;
+}
+
+export interface UzuToolInvokeOptions {
+ readonly signal?: AbortSignal;
+}
+
+export interface UzuToolFunctionOptions<Parameters extends z.ZodObject, ResultSchema extends z.ZodType> {
+ readonly name: string;
+ readonly description?: string;
+ readonly parameters: Parameters;
+ readonly returns: ResultSchema;
+ readonly handler: (
+ parameters: z.output<Parameters>,
+ context: UzuToolContext,
+ ) => z.input<ResultSchema> | Promise<z.input<ResultSchema>>;
+}
+
+export class UzuToolFunction<
+ Parameters extends z.ZodObject,
+ ResultSchema extends z.ZodType,
+> extends NativeTool {
+ readonly name: string;
+ readonly description: string;
+ readonly parameters: Parameters;
+ readonly returns: ResultSchema;
+ readonly parametersSchema: Record<string, unknown>;
+ readonly returnSchema: Record<string, unknown>;
+ readonly handler: UzuToolFunctionOptions<Parameters, ResultSchema>['handler'];
+
+ constructor(options: UzuToolFunctionOptions<Parameters, ResultSchema>) {
+ const name = options.name.trim();
+ if (!name) {
+ throw new TypeError('tool name must not be empty');
+ }
+
+ const description = options.description ?? '';
+ const parametersSchema = z.toJSONSchema(options.parameters);
+ const returnSchema = z.toJSONSchema(options.returns);
+ const activeInvocations = new Map<string, AbortController>();
+ const cancelledInvocations = new Map<string, ReturnType<typeof setTimeout>>();
+
+ const takeCancellation = (invocationId: string): boolean => {
+ const expiration = cancelledInvocations.get(invocationId);
+ if (expiration === undefined) {
+ return false;
+ }
+ clearTimeout(expiration);
+ cancelledInvocations.delete(invocationId);
+ return true;
+ };
+
+ const invokeJson = async (argumentsJson: string, invocationId: string): Promise<string> => {
+ const controller = new AbortController();
+ activeInvocations.set(invocationId, controller);
+ if (takeCancellation(invocationId)) {
+ controller.abort();
+ }
+
+ try {
+ const rawArguments: unknown = JSON.parse(argumentsJson);
+ const parameters = await options.parameters.parseAsync(rawArguments);
+ const rawResult = await options.handler(parameters, {
+ signal: controller.signal,
+ });
+ const result = await options.returns.parseAsync(rawResult);
+ return serializeResult(result);
+ } finally {
+ activeInvocations.delete(invocationId);
+ takeCancellation(invocationId);
+ }
+ };
+ const cancel = (invocationId: string): void => {
+ const controller = activeInvocations.get(invocationId);
+ if (controller) {
+ controller.abort();
+ return;
+ }
+
+ // Invocation and cancellation use separate nonblocking native callbacks, so
+ // cancellation can arrive before invocation starts. Retain it briefly for
+ // that case, but expire IDs from callbacks that arrive after completion.
+ const expiration = setTimeout(() => {
+ cancelledInvocations.delete(invocationId);
+ }, CANCELLED_INVOCATION_RETENTION_MS);
+ expiration.unref?.();
+ const previousExpiration = cancelledInvocations.get(invocationId);
+ if (previousExpiration !== undefined) {
+ clearTimeout(previousExpiration);
+ }
+ cancelledInvocations.set(invocationId, expiration);
+ };
+
+ super(
+ new ToolFunction(
+ name,
+ description,
+ new Value(JSON.stringify(parametersSchema)),
+ new Value(JSON.stringify(returnSchema)),
+ ),
+ invokeJson,
+ cancel,
+ );
+
+ this.name = name;
+ this.description = description;
+ this.parameters = options.parameters;
+ this.returns = options.returns;
+ this.parametersSchema = parametersSchema;
+ this.returnSchema = returnSchema;
+ this.handler = options.handler;
+ }
+
+ async invoke(
+ input: z.input<Parameters>,
+ options: UzuToolInvokeOptions = {},
+ ): Promise<z.output<ResultSchema>> {
+ const parameters = await this.parameters.parseAsync(input);
+ const signal = options.signal ?? new AbortController().signal;
+ const rawResult = await this.handler(parameters, { signal });
+ return this.returns.parseAsync(rawResult);
+ }
+}
+
+export function uzuToolFunction<Parameters extends z.ZodObject, ResultSchema extends z.ZodType>(
+ options: UzuToolFunctionOptions<Parameters, ResultSchema>,
+): UzuToolFunction<Parameters, ResultSchema> {
+ return new UzuToolFunction(options);
+}
+
+function serializeResult(result: unknown): string {
+ const json = JSON.stringify(result === undefined ? null : result);
+ if (json === undefined) {
+ throw new TypeError('tool result must be JSON serializable');
+ }
+ return json;
+}
diff --git a/bindings/typescript/tests/tool-cancellation.test.ts b/bindings/typescript/tests/tool-cancellation.test.ts
new file mode 100644
index 000000000..a93c2fede
--- /dev/null
+++ b/bindings/typescript/tests/tool-cancellation.test.ts
@@ -0,0 +1,45 @@
+import * as z from 'zod';
+
+type InvokeJson = (argumentsJson: string, invocationId: string) => Promise<string>;
+type Cancel = (invocationId: string) => void;
+
+jest.mock('../src/napi/index', () => ({
+ NativeTool: class {
+ readonly testInvokeJson: InvokeJson;
+ readonly testCancel: Cancel;
+
+ constructor(_definition: unknown, invokeJson: InvokeJson, cancel: Cancel) {
+ this.testInvokeJson = invokeJson;
+ this.testCancel = cancel;
+ }
+ },
+ ToolFunction: class {},
+ Value: class {},
+}));
+
+import { uzuToolFunction } from '../src/tool';
+
+interface TestNativeTool {
+ readonly testInvokeJson: InvokeJson;
+ readonly testCancel: Cancel;
+}
+
+test('retains pre-start cancellations but expires late cancellations', async () => {
+ jest.useFakeTimers();
+ const tool = uzuToolFunction({
+ name: 'is_cancelled',
+ parameters: z.object({}),
+ returns: z.boolean(),
+ handler: (_parameters, context) => context.signal.aborted,
+ }) as unknown as TestNativeTool;
+
+ tool.testCancel('before-start');
+ await expect(tool.testInvokeJson('{}', 'before-start')).resolves.toBe('true');
+
+ await expect(tool.testInvokeJson('{}', 'already-finished')).resolves.toBe('false');
+ tool.testCancel('already-finished');
+ jest.runOnlyPendingTimers();
+
+ await expect(tool.testInvokeJson('{}', 'already-finished')).resolves.toBe('false');
+ jest.useRealTimers();
+});
diff --git a/bindings/typescript/tests/tool.test.ts b/bindings/typescript/tests/tool.test.ts
new file mode 100644
index 000000000..521f8c69a
--- /dev/null
+++ b/bindings/typescript/tests/tool.test.ts
@@ -0,0 +1,78 @@
+import { ChatSession, UzuToolFunction, uzuToolFunction } from '@trymirai/uzu';
+import * as z from 'zod';
+
+test('tool factory builds schemas and invokes a typed handler', async () => {
+ const tool = uzuToolFunction({
+ name: 'add',
+ description: 'Add two numbers',
+ parameters: z.object({
+ left: z.number().describe('Left operand.'),
+ right: z.number().describe('Right operand.'),
+ }),
+ returns: z.number(),
+ handler: ({ left, right }) => Promise.resolve(left + right),
+ });
+
+ expect(tool).toBeInstanceOf(UzuToolFunction);
+ expect(tool.name).toBe('add');
+ expect(tool.description).toBe('Add two numbers');
+ expect(tool.parametersSchema).toMatchObject({
+ type: 'object',
+ properties: {
+ left: {
+ type: 'number',
+ description: 'Left operand.',
+ },
+ right: {
+ type: 'number',
+ description: 'Right operand.',
+ },
+ },
+ required: ['left', 'right'],
+ additionalProperties: false,
+ });
+ expect(tool.returnSchema).toMatchObject({
+ type: 'number',
+ });
+ await expect(tool.invoke({ left: 2, right: 3 })).resolves.toBe(5);
+});
+
+test('tool invocation validates arguments and results', async () => {
+ const tool = uzuToolFunction({
+ name: 'length',
+ parameters: z.object({
+ value: z.string(),
+ }),
+ returns: z.number(),
+ handler: ({ value }) => value.length,
+ });
+
+ await expect(tool.invoke({ value: 'uzu' })).resolves.toBe(3);
+ await expect(tool.invoke({ value: 3 } as never)).rejects.toBeInstanceOf(z.ZodError);
+
+ const invalidResult = uzuToolFunction({
+ name: 'invalid_result',
+ parameters: z.object({}),
+ returns: z.number(),
+ handler: () => 'not a number' as never,
+ });
+ await expect(invalidResult.invoke({})).rejects.toBeInstanceOf(z.ZodError);
+});
+
+test('tool handler receives the invocation abort signal', async () => {
+ const controller = new AbortController();
+ const tool = uzuToolFunction({
+ name: 'is_cancelled',
+ parameters: z.object({}),
+ returns: z.boolean(),
+ handler: (_parameters, context) => context.signal.aborted,
+ });
+
+ controller.abort();
+ await expect(tool.invoke({}, { signal: controller.signal })).resolves.toBe(true);
+});
+
+test('chat session exposes tool registration methods', () => {
+ expect(typeof ChatSession.prototype.addTool).toBe('function');
+ expect(typeof ChatSession.prototype.addTools).toBe('function');
+});
diff --git a/crates/nagare/src/chat/bindings_napi.rs b/crates/nagare/src/chat/bindings_napi.rs
new file mode 100644
index 000000000..c9fac8db9
--- /dev/null
+++ b/crates/nagare/src/chat/bindings_napi.rs
@@ -0,0 +1,133 @@
+use std::{
+ future::Future,
+ pin::Pin,
+ sync::Arc,
+ task::{Context, Poll},
+};
+
+use napi::{
+ Status,
+ bindgen_prelude::{AsyncBlock, AsyncBlockBuilder, ClassInstance, Env, FnArgs, Function, Promise},
+ threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode},
+};
+use shoji::types::basic::{ToolFunction, Value};
+use uuid::Uuid;
+
+use super::ChatSession;
+use crate::tool::func_def::{ErrorFuture, ToolDescriptor};
+
+type InvokeArguments = FnArgs<(String, String)>;
+type InvokeFunction = ThreadsafeFunction<InvokeArguments, Promise<String>, InvokeArguments, Status, false, true>;
+type CancelFunction = ThreadsafeFunction<String, (), String, Status, false, true>;
+type JavaScriptFuture = Pin<Box<dyn Future<Output = napi::Result<String>> + Send>>;
+
+struct JavaScriptInvocation {
+ future: JavaScriptFuture,
+ cancellation: Option<(Arc<CancelFunction>, String)>,
+}
+
+impl Future for JavaScriptInvocation {
+ type Output = napi::Result<String>;
+
+ fn poll(
+ mut self: Pin<&mut Self>,
+ context: &mut Context<'_>,
+ ) -> Poll<Self::Output> {
+ let result = self.future.as_mut().poll(context);
+ if result.is_ready() {
+ self.cancellation = None;
+ }
+ result
+ }
+}
+
+impl Drop for JavaScriptInvocation {
+ fn drop(&mut self) {
+ let Some((cancel, invocation_id)) = self.cancellation.take() else {
+ return;
+ };
+ let _ = cancel.call(invocation_id, ThreadsafeFunctionCallMode::NonBlocking);
+ }
+}
+
+#[napi_derive::napi]
+pub struct NativeTool {
+ descriptor: ToolDescriptor,
+}
+
+#[napi_derive::napi]
+impl NativeTool {
+ #[napi(constructor)]
+ pub fn new(
+ definition: ToolFunction,
+ invoke_json: Function<'_, FnArgs<(String, String)>, Promise<String>>,
+ cancel: Function<'_, String, ()>,
+ ) -> napi::Result<Self> {
+ let invoke_json = Arc::new(invoke_json.build_threadsafe_function().weak::<true>().build()?);
+ let cancel = Arc::new(cancel.build_threadsafe_function().weak::<true>().build()?);
+ let descriptor = ToolDescriptor::new(
+ definition.name,
+ definition.description,
+ definition.parameters,
+ definition.return_definition,
+ Box::new(move |arguments| {
+ let invoke_json = invoke_json.clone();
+ let cancel = cancel.clone();
+ Box::new(call_javascript_tool(invoke_json, cancel, arguments))
+ }),
+ );
+ Ok(Self {
+ descriptor,
+ })
+ }
+}
+
+#[napi_derive::napi]
+impl ChatSession {
+ #[napi(js_name = "addTool")]
+ pub fn add_tool_bindings_napi(
+ &self,
+ tool: ClassInstance<'_, NativeTool>,
+ env: Env,
+ ) -> napi::Result<AsyncBlock<()>> {
+ let descriptor = tool.descriptor.clone();
+ let mut session = self.clone();
+ AsyncBlockBuilder::new(async move { session.add_tool(descriptor).await.map_err(Into::into) }).build(&env)
+ }
+
+ #[napi(js_name = "addTools")]
+ pub fn add_tools_bindings_napi(
+ &self,
+ tools: Vec<ClassInstance<'_, NativeTool>>,
+ env: Env,
+ ) -> napi::Result<AsyncBlock<()>> {
+ let descriptors = tools.iter().map(|tool| tool.descriptor.clone()).collect();
+ let mut session = self.clone();
+ AsyncBlockBuilder::new(async move { session.add_tools(descriptors).await.map_err(Into::into) }).build(&env)
+ }
+}
+
+async fn call_javascript_tool(
+ invoke_json: Arc<InvokeFunction>,
+ cancel: Arc<CancelFunction>,
+ arguments: Value,
+) -> Result<Value, ErrorFuture> {
+ let invocation_id = Uuid::new_v4().to_string();
+ let invocation_id_for_call = invocation_id.clone();
+ let future = Box::pin(async move {
+ let arguments = InvokeArguments::from((arguments.json, invocation_id_for_call));
+ let promise = invoke_json.call_async_catch(arguments).await?;
+ promise.await
+ });
+ let invocation = JavaScriptInvocation {
+ future,
+ cancellation: Some((cancel, invocation_id)),
+ };
+ let json = invocation.await.map_err(javascript_error)?;
+ let value = serde_json::from_str::<serde_json::Value>(&json)?;
+ Ok(Value::from(value))
+}
+
+fn javascript_error(error: napi::Error) -> ErrorFuture {
+ error.to_string().into()
+}
diff --git a/crates/nagare/src/chat/mod.rs b/crates/nagare/src/chat/mod.rs
index 80854b52a..3b4c82c2a 100644
--- a/crates/nagare/src/chat/mod.rs
+++ b/crates/nagare/src/chat/mod.rs
@@ -1,3 +1,5 @@
+#[cfg(feature = "bindings-napi")]
+mod bindings_napi;
#[cfg(feature = "bindings-pyo3")]
mod bindings_pyo3;
mod error;