Merge commit 'd803bfe2b1fe7f5e219e50ac20d6801a0a58ac75' as 'vendor/ruvector'
This commit is contained in:
6
vendor/ruvector/crates/ruvector-tiny-dancer-node/.npmignore
vendored
Normal file
6
vendor/ruvector/crates/ruvector-tiny-dancer-node/.npmignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
target
|
||||
Cargo.toml
|
||||
Cargo.lock
|
||||
.cargo
|
||||
src
|
||||
build.rs
|
||||
44
vendor/ruvector/crates/ruvector-tiny-dancer-node/Cargo.toml
vendored
Normal file
44
vendor/ruvector/crates/ruvector-tiny-dancer-node/Cargo.toml
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
[package]
|
||||
name = "ruvector-tiny-dancer-node"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
repository.workspace = true
|
||||
readme = "README.md"
|
||||
description = "Node.js bindings for Tiny Dancer neural routing via NAPI-RS"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
ruvector-tiny-dancer-core = { version = "2.0", path = "../ruvector-tiny-dancer-core" }
|
||||
|
||||
# Node.js bindings
|
||||
napi = { workspace = true }
|
||||
napi-derive = { workspace = true }
|
||||
|
||||
# Async
|
||||
tokio = { workspace = true }
|
||||
|
||||
# Error handling
|
||||
thiserror = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
|
||||
# Serialization
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
# Time
|
||||
chrono = { workspace = true }
|
||||
|
||||
# Concurrency
|
||||
parking_lot = { workspace = true }
|
||||
|
||||
[build-dependencies]
|
||||
napi-build = "2.1"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
strip = true
|
||||
289
vendor/ruvector/crates/ruvector-tiny-dancer-node/README.md
vendored
Normal file
289
vendor/ruvector/crates/ruvector-tiny-dancer-node/README.md
vendored
Normal file
@@ -0,0 +1,289 @@
|
||||
# Ruvector Tiny Dancer Node
|
||||
|
||||
[](https://www.npmjs.com/package/@ruvector/tiny-dancer-node)
|
||||
[](https://crates.io/crates/ruvector-tiny-dancer-node)
|
||||
[](https://opensource.org/licenses/MIT)
|
||||
|
||||
**Node.js bindings for Tiny Dancer neural routing via NAPI-RS.**
|
||||
|
||||
`ruvector-tiny-dancer-node` provides native Node.js bindings for production-grade AI agent routing. Run FastGRNN neural inference at native speed for intelligent request routing in server-side applications. Part of the [Ruvector](https://github.com/ruvnet/ruvector) ecosystem.
|
||||
|
||||
## Why Tiny Dancer Node?
|
||||
|
||||
- **Native Performance**: Rust speed in Node.js
|
||||
- **Production Ready**: Battle-tested in high-throughput systems
|
||||
- **Async/Await**: Non-blocking inference operations
|
||||
- **TypeScript**: Complete type definitions included
|
||||
- **Multi-Threaded**: Leverage all CPU cores
|
||||
|
||||
## Features
|
||||
|
||||
### Core Capabilities
|
||||
|
||||
- **Neural Inference**: FastGRNN model execution
|
||||
- **Model Training**: Train custom routing models
|
||||
- **Feature Engineering**: Request feature extraction
|
||||
- **Persistent Storage**: SQLite-backed model storage
|
||||
- **Batch Processing**: Efficient batch inference
|
||||
|
||||
### Advanced Features
|
||||
|
||||
- **Model Versioning**: Manage multiple model versions
|
||||
- **A/B Testing**: Route comparison and testing
|
||||
- **Metrics**: Performance and accuracy tracking
|
||||
- **Hot Reload**: Update models without restart
|
||||
- **Distributed**: Coordinate across instances
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
npm install @ruvector/tiny-dancer-node
|
||||
# or
|
||||
yarn add @ruvector/tiny-dancer-node
|
||||
# or
|
||||
pnpm add @ruvector/tiny-dancer-node
|
||||
```
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Basic Routing
|
||||
|
||||
```typescript
|
||||
import { TinyDancer, RouteRequest } from '@ruvector/tiny-dancer-node';
|
||||
|
||||
// Create router instance
|
||||
const router = new TinyDancer({
|
||||
modelPath: './models/router.db',
|
||||
});
|
||||
|
||||
// Initialize
|
||||
await router.init();
|
||||
|
||||
// Route request
|
||||
const result = await router.route({
|
||||
query: "What is the weather like today?",
|
||||
context: {
|
||||
userId: "user-123",
|
||||
sessionLength: 5,
|
||||
},
|
||||
agents: ["weather", "general", "calendar"],
|
||||
});
|
||||
|
||||
console.log(`Route to: ${result.agent} (confidence: ${result.confidence})`);
|
||||
```
|
||||
|
||||
### Model Training
|
||||
|
||||
```typescript
|
||||
import { TinyDancer, TrainingData } from '@ruvector/tiny-dancer-node';
|
||||
|
||||
const router = new TinyDancer();
|
||||
await router.init();
|
||||
|
||||
// Prepare training data
|
||||
const trainingData: TrainingData[] = [
|
||||
{
|
||||
query: "What's the weather?",
|
||||
correctAgent: "weather",
|
||||
context: { category: "weather" },
|
||||
},
|
||||
{
|
||||
query: "Schedule a meeting",
|
||||
correctAgent: "calendar",
|
||||
context: { category: "scheduling" },
|
||||
},
|
||||
// ... more examples
|
||||
];
|
||||
|
||||
// Train model
|
||||
const result = await router.train({
|
||||
data: trainingData,
|
||||
epochs: 100,
|
||||
learningRate: 0.001,
|
||||
validationSplit: 0.2,
|
||||
});
|
||||
|
||||
console.log(`Training accuracy: ${result.accuracy}`);
|
||||
console.log(`Validation accuracy: ${result.validationAccuracy}`);
|
||||
|
||||
// Save model
|
||||
await router.saveModel('./models/custom-router.bin');
|
||||
```
|
||||
|
||||
### Performance Monitoring
|
||||
|
||||
```typescript
|
||||
import { TinyDancer } from '@ruvector/tiny-dancer-node';
|
||||
|
||||
const router = new TinyDancer({ enableMetrics: true });
|
||||
await router.init();
|
||||
|
||||
// Route with metrics
|
||||
const result = await router.route(request);
|
||||
|
||||
// Get performance metrics
|
||||
const metrics = router.getMetrics();
|
||||
console.log(`Average latency: ${metrics.avgLatencyMs}ms`);
|
||||
console.log(`P99 latency: ${metrics.p99LatencyMs}ms`);
|
||||
console.log(`Requests/sec: ${metrics.requestsPerSecond}`);
|
||||
console.log(`Cache hit rate: ${metrics.cacheHitRate}`);
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
### TinyDancer Class
|
||||
|
||||
```typescript
|
||||
class TinyDancer {
|
||||
constructor(config?: TinyDancerConfig);
|
||||
|
||||
// Lifecycle
|
||||
init(): Promise<void>;
|
||||
close(): Promise<void>;
|
||||
|
||||
// Routing
|
||||
route(request: RouteRequest): Promise<RouteResult>;
|
||||
routeBatch(requests: RouteRequest[]): Promise<RouteResult[]>;
|
||||
|
||||
// Training
|
||||
train(options: TrainOptions): Promise<TrainResult>;
|
||||
loadModel(path: string): Promise<void>;
|
||||
saveModel(path: string): Promise<void>;
|
||||
|
||||
// Scoring
|
||||
scoreAgents(request: RouteRequest): Promise<AgentScore[]>;
|
||||
|
||||
// Metrics
|
||||
getMetrics(): RouterMetrics;
|
||||
resetMetrics(): void;
|
||||
}
|
||||
```
|
||||
|
||||
### Types
|
||||
|
||||
```typescript
|
||||
interface TinyDancerConfig {
|
||||
modelPath?: string;
|
||||
enableMetrics?: boolean;
|
||||
cacheSize?: number;
|
||||
numThreads?: number;
|
||||
}
|
||||
|
||||
interface RouteRequest {
|
||||
query: string;
|
||||
context?: Record<string, any>;
|
||||
agents: string[];
|
||||
constraints?: RouteConstraints;
|
||||
}
|
||||
|
||||
interface RouteResult {
|
||||
agent: string;
|
||||
confidence: number;
|
||||
scores: Record<string, number>;
|
||||
latencyMs: number;
|
||||
}
|
||||
|
||||
interface TrainOptions {
|
||||
data: TrainingData[];
|
||||
epochs: number;
|
||||
learningRate: number;
|
||||
validationSplit?: number;
|
||||
batchSize?: number;
|
||||
}
|
||||
|
||||
interface TrainResult {
|
||||
accuracy: number;
|
||||
validationAccuracy: number;
|
||||
loss: number;
|
||||
epochs: number;
|
||||
trainingTimeMs: number;
|
||||
}
|
||||
|
||||
interface RouterMetrics {
|
||||
totalRequests: number;
|
||||
avgLatencyMs: number;
|
||||
p50LatencyMs: number;
|
||||
p99LatencyMs: number;
|
||||
requestsPerSecond: number;
|
||||
cacheHitRate: number;
|
||||
}
|
||||
```
|
||||
|
||||
## Express Integration
|
||||
|
||||
```typescript
|
||||
import express from 'express';
|
||||
import { TinyDancer } from '@ruvector/tiny-dancer-node';
|
||||
|
||||
const app = express();
|
||||
const router = new TinyDancer();
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
app.post('/route', async (req, res) => {
|
||||
const result = await router.route({
|
||||
query: req.body.query,
|
||||
context: req.body.context,
|
||||
agents: ['agent-a', 'agent-b', 'agent-c'],
|
||||
});
|
||||
|
||||
res.json(result);
|
||||
});
|
||||
|
||||
app.listen(3000);
|
||||
```
|
||||
|
||||
## Platform Support
|
||||
|
||||
| Platform | Architecture | Status |
|
||||
|----------|-------------|--------|
|
||||
| Linux | x64 | ✅ |
|
||||
| Linux | arm64 | ✅ |
|
||||
| macOS | x64 | ✅ |
|
||||
| macOS | arm64 (M1/M2) | ✅ |
|
||||
| Windows | x64 | ✅ |
|
||||
|
||||
## Building from Source
|
||||
|
||||
```bash
|
||||
# Clone repository
|
||||
git clone https://github.com/ruvnet/ruvector.git
|
||||
cd ruvector/crates/ruvector-tiny-dancer-node
|
||||
|
||||
# Install dependencies
|
||||
npm install
|
||||
|
||||
# Build native module
|
||||
npm run build
|
||||
|
||||
# Run tests
|
||||
npm test
|
||||
```
|
||||
|
||||
## Related Packages
|
||||
|
||||
- **[ruvector-tiny-dancer-core](../ruvector-tiny-dancer-core/)** - Core Rust implementation
|
||||
- **[ruvector-tiny-dancer-wasm](../ruvector-tiny-dancer-wasm/)** - WebAssembly bindings
|
||||
- **[@ruvector/core](https://www.npmjs.com/package/@ruvector/core)** - Core vector bindings
|
||||
|
||||
## Documentation
|
||||
|
||||
- **[Main README](../../README.md)** - Complete project overview
|
||||
- **[API Documentation](https://docs.rs/ruvector-tiny-dancer-node)** - Full API reference
|
||||
- **[GitHub Repository](https://github.com/ruvnet/ruvector)** - Source code
|
||||
|
||||
## License
|
||||
|
||||
**MIT License** - see [LICENSE](../../LICENSE) for details.
|
||||
|
||||
---
|
||||
|
||||
<div align="center">
|
||||
|
||||
**Part of [Ruvector](https://github.com/ruvnet/ruvector) - Built by [rUv](https://ruv.io)**
|
||||
|
||||
[](https://github.com/ruvnet/ruvector)
|
||||
|
||||
[Documentation](https://docs.rs/ruvector-tiny-dancer-node) | [npm](https://www.npmjs.com/package/@ruvector/tiny-dancer-node) | [GitHub](https://github.com/ruvnet/ruvector)
|
||||
|
||||
</div>
|
||||
3
vendor/ruvector/crates/ruvector-tiny-dancer-node/build.rs
vendored
Normal file
3
vendor/ruvector/crates/ruvector-tiny-dancer-node/build.rs
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
napi_build::setup();
|
||||
}
|
||||
50
vendor/ruvector/crates/ruvector-tiny-dancer-node/package.json
vendored
Normal file
50
vendor/ruvector/crates/ruvector-tiny-dancer-node/package.json
vendored
Normal file
@@ -0,0 +1,50 @@
|
||||
{
|
||||
"name": "ruvector-tiny-dancer-node",
|
||||
"version": "0.1.16",
|
||||
"description": "Node.js bindings for Tiny Dancer neural routing via NAPI-RS",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
"napi": {
|
||||
"name": "ruvector-tiny-dancer",
|
||||
"triples": {
|
||||
"defaults": true,
|
||||
"additional": [
|
||||
"aarch64-unknown-linux-gnu",
|
||||
"aarch64-apple-darwin",
|
||||
"x86_64-pc-windows-msvc"
|
||||
]
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"artifacts": "napi artifacts",
|
||||
"build": "napi build --platform --release",
|
||||
"build:debug": "napi build --platform",
|
||||
"prepublishOnly": "napi prepublish -t npm",
|
||||
"test": "node test.js",
|
||||
"version": "napi version"
|
||||
},
|
||||
"keywords": [
|
||||
"ruvector",
|
||||
"tiny-dancer",
|
||||
"neural-router",
|
||||
"ai-routing",
|
||||
"fastgrnn",
|
||||
"napi-rs"
|
||||
],
|
||||
"author": "ruv.io Team <info@ruv.io>",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/ruvnet/ruvector"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@napi-rs/cli": "^2.16.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 18"
|
||||
},
|
||||
"publishConfig": {
|
||||
"registry": "https://registry.npmjs.org/",
|
||||
"access": "public"
|
||||
}
|
||||
}
|
||||
286
vendor/ruvector/crates/ruvector-tiny-dancer-node/src/lib.rs
vendored
Normal file
286
vendor/ruvector/crates/ruvector-tiny-dancer-node/src/lib.rs
vendored
Normal file
@@ -0,0 +1,286 @@
|
||||
//! Node.js bindings for Tiny Dancer neural routing via NAPI-RS
|
||||
//!
|
||||
//! High-performance Rust neural routing with zero-copy buffer sharing,
|
||||
//! async/await support, and complete TypeScript type definitions.
|
||||
|
||||
#![deny(clippy::all)]
|
||||
#![warn(clippy::pedantic)]
|
||||
|
||||
use napi::bindgen_prelude::*;
|
||||
use napi_derive::napi;
|
||||
use parking_lot::RwLock;
|
||||
use ruvector_tiny_dancer_core::{
|
||||
types::{
|
||||
Candidate as CoreCandidate, RouterConfig as CoreRouterConfig,
|
||||
RoutingDecision as CoreRoutingDecision, RoutingRequest as CoreRoutingRequest,
|
||||
RoutingResponse as CoreRoutingResponse,
|
||||
},
|
||||
Router as CoreRouter,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Router configuration
|
||||
#[napi(object)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RouterConfig {
|
||||
/// Model path
|
||||
pub model_path: String,
|
||||
/// Confidence threshold (0.0 to 1.0)
|
||||
pub confidence_threshold: Option<f64>,
|
||||
/// Maximum uncertainty (0.0 to 1.0)
|
||||
pub max_uncertainty: Option<f64>,
|
||||
/// Enable circuit breaker
|
||||
pub enable_circuit_breaker: Option<bool>,
|
||||
/// Circuit breaker threshold
|
||||
pub circuit_breaker_threshold: Option<u32>,
|
||||
/// Enable quantization
|
||||
pub enable_quantization: Option<bool>,
|
||||
/// Database path
|
||||
pub database_path: Option<String>,
|
||||
}
|
||||
|
||||
impl From<RouterConfig> for CoreRouterConfig {
|
||||
fn from(config: RouterConfig) -> Self {
|
||||
CoreRouterConfig {
|
||||
model_path: config.model_path,
|
||||
confidence_threshold: config.confidence_threshold.unwrap_or(0.85) as f32,
|
||||
max_uncertainty: config.max_uncertainty.unwrap_or(0.15) as f32,
|
||||
enable_circuit_breaker: config.enable_circuit_breaker.unwrap_or(true),
|
||||
circuit_breaker_threshold: config.circuit_breaker_threshold.unwrap_or(5),
|
||||
enable_quantization: config.enable_quantization.unwrap_or(true),
|
||||
database_path: config.database_path,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Candidate for routing
|
||||
#[napi(object)]
|
||||
#[derive(Clone)]
|
||||
pub struct Candidate {
|
||||
/// Candidate ID
|
||||
pub id: String,
|
||||
/// Embedding vector
|
||||
pub embedding: Float32Array,
|
||||
/// Metadata (JSON string)
|
||||
pub metadata: Option<String>,
|
||||
/// Creation timestamp
|
||||
pub created_at: Option<i64>,
|
||||
/// Access count
|
||||
pub access_count: Option<u32>,
|
||||
/// Success rate (0.0 to 1.0)
|
||||
pub success_rate: Option<f64>,
|
||||
}
|
||||
|
||||
impl Candidate {
|
||||
fn to_core(&self) -> Result<CoreCandidate> {
|
||||
let metadata: HashMap<String, serde_json::Value> = if let Some(ref meta_str) = self.metadata
|
||||
{
|
||||
serde_json::from_str(meta_str)
|
||||
.map_err(|e| Error::from_reason(format!("Invalid metadata JSON: {}", e)))?
|
||||
} else {
|
||||
HashMap::new()
|
||||
};
|
||||
|
||||
Ok(CoreCandidate {
|
||||
id: self.id.clone(),
|
||||
embedding: self.embedding.to_vec(),
|
||||
metadata,
|
||||
created_at: self
|
||||
.created_at
|
||||
.unwrap_or_else(|| chrono::Utc::now().timestamp()),
|
||||
access_count: self.access_count.unwrap_or(0) as u64,
|
||||
success_rate: self.success_rate.unwrap_or(0.0) as f32,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Routing request
|
||||
#[napi(object)]
|
||||
pub struct RoutingRequest {
|
||||
/// Query embedding
|
||||
pub query_embedding: Float32Array,
|
||||
/// Candidates to score
|
||||
pub candidates: Vec<Candidate>,
|
||||
/// Optional metadata (JSON string)
|
||||
pub metadata: Option<String>,
|
||||
}
|
||||
|
||||
impl RoutingRequest {
|
||||
fn to_core(&self) -> Result<CoreRoutingRequest> {
|
||||
let candidates: Result<Vec<CoreCandidate>> =
|
||||
self.candidates.iter().map(|c| c.to_core()).collect();
|
||||
|
||||
let metadata = if let Some(ref meta_str) = self.metadata {
|
||||
Some(
|
||||
serde_json::from_str(meta_str)
|
||||
.map_err(|e| Error::from_reason(format!("Invalid metadata JSON: {}", e)))?,
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok(CoreRoutingRequest {
|
||||
query_embedding: self.query_embedding.to_vec(),
|
||||
candidates: candidates?,
|
||||
metadata,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Routing decision
|
||||
#[napi(object)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RoutingDecision {
|
||||
/// Candidate ID
|
||||
pub candidate_id: String,
|
||||
/// Confidence score (0.0 to 1.0)
|
||||
pub confidence: f64,
|
||||
/// Whether to use lightweight model
|
||||
pub use_lightweight: bool,
|
||||
/// Uncertainty estimate (0.0 to 1.0)
|
||||
pub uncertainty: f64,
|
||||
}
|
||||
|
||||
impl From<CoreRoutingDecision> for RoutingDecision {
|
||||
fn from(decision: CoreRoutingDecision) -> Self {
|
||||
Self {
|
||||
candidate_id: decision.candidate_id,
|
||||
confidence: decision.confidence as f64,
|
||||
use_lightweight: decision.use_lightweight,
|
||||
uncertainty: decision.uncertainty as f64,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Routing response
|
||||
#[napi(object)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RoutingResponse {
|
||||
/// Routing decisions
|
||||
pub decisions: Vec<RoutingDecision>,
|
||||
/// Total inference time in microseconds
|
||||
pub inference_time_us: u32,
|
||||
/// Number of candidates processed
|
||||
pub candidates_processed: u32,
|
||||
/// Feature engineering time in microseconds
|
||||
pub feature_time_us: u32,
|
||||
}
|
||||
|
||||
impl From<CoreRoutingResponse> for RoutingResponse {
|
||||
fn from(response: CoreRoutingResponse) -> Self {
|
||||
Self {
|
||||
decisions: response.decisions.into_iter().map(Into::into).collect(),
|
||||
inference_time_us: response.inference_time_us as u32,
|
||||
candidates_processed: response.candidates_processed as u32,
|
||||
feature_time_us: response.feature_time_us as u32,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Tiny Dancer neural router
|
||||
#[napi]
|
||||
pub struct Router {
|
||||
inner: Arc<RwLock<CoreRouter>>,
|
||||
}
|
||||
|
||||
#[napi]
|
||||
impl Router {
|
||||
/// Create a new router with configuration
|
||||
///
|
||||
/// # Example
|
||||
/// ```javascript
|
||||
/// const router = new Router({
|
||||
/// modelPath: './models/fastgrnn.safetensors',
|
||||
/// confidenceThreshold: 0.85,
|
||||
/// maxUncertainty: 0.15,
|
||||
/// enableCircuitBreaker: true
|
||||
/// });
|
||||
/// ```
|
||||
#[napi(constructor)]
|
||||
pub fn new(config: RouterConfig) -> Result<Self> {
|
||||
let core_config: CoreRouterConfig = config.into();
|
||||
let router = CoreRouter::new(core_config)
|
||||
.map_err(|e| Error::from_reason(format!("Failed to create router: {}", e)))?;
|
||||
|
||||
Ok(Self {
|
||||
inner: Arc::new(RwLock::new(router)),
|
||||
})
|
||||
}
|
||||
|
||||
/// Route a request through the neural routing system
|
||||
///
|
||||
/// Returns routing decisions with confidence scores and model recommendations
|
||||
///
|
||||
/// # Example
|
||||
/// ```javascript
|
||||
/// const response = await router.route({
|
||||
/// queryEmbedding: new Float32Array([0.1, 0.2, ...]),
|
||||
/// candidates: [
|
||||
/// { id: '1', embedding: new Float32Array([...]) },
|
||||
/// { id: '2', embedding: new Float32Array([...]) }
|
||||
/// ]
|
||||
/// });
|
||||
/// console.log('Top decision:', response.decisions[0]);
|
||||
/// console.log('Inference time:', response.inferenceTimeUs, 'μs');
|
||||
/// ```
|
||||
#[napi]
|
||||
pub async fn route(&self, request: RoutingRequest) -> Result<RoutingResponse> {
|
||||
let core_request = request.to_core()?;
|
||||
let router = self.inner.clone();
|
||||
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let router = router.read();
|
||||
router.route(core_request)
|
||||
})
|
||||
.await
|
||||
.map_err(|e| Error::from_reason(format!("Task failed: {}", e)))?
|
||||
.map_err(|e| Error::from_reason(format!("Routing failed: {}", e)))
|
||||
.map(Into::into)
|
||||
}
|
||||
|
||||
/// Reload the model from disk (hot-reload)
|
||||
///
|
||||
/// # Example
|
||||
/// ```javascript
|
||||
/// await router.reloadModel();
|
||||
/// ```
|
||||
#[napi]
|
||||
pub async fn reload_model(&self) -> Result<()> {
|
||||
let router = self.inner.clone();
|
||||
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let router = router.read();
|
||||
router.reload_model()
|
||||
})
|
||||
.await
|
||||
.map_err(|e| Error::from_reason(format!("Task failed: {}", e)))?
|
||||
.map_err(|e| Error::from_reason(format!("Model reload failed: {}", e)))
|
||||
}
|
||||
|
||||
/// Check circuit breaker status
|
||||
///
|
||||
/// Returns true if the circuit is closed (healthy), false if open (unhealthy)
|
||||
///
|
||||
/// # Example
|
||||
/// ```javascript
|
||||
/// const isHealthy = router.circuitBreakerStatus();
|
||||
/// ```
|
||||
#[napi]
|
||||
pub fn circuit_breaker_status(&self) -> Option<bool> {
|
||||
let router = self.inner.read();
|
||||
router.circuit_breaker_status()
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the version of the Tiny Dancer library
|
||||
#[napi]
|
||||
pub fn version() -> String {
|
||||
env!("CARGO_PKG_VERSION").to_string()
|
||||
}
|
||||
|
||||
/// Hello function for testing bindings
|
||||
#[napi]
|
||||
pub fn hello() -> String {
|
||||
"Hello from Tiny Dancer Node.js bindings!".to_string()
|
||||
}
|
||||
Reference in New Issue
Block a user