Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit a33e074

Browse files
authored
Merge pull request #367 from InNoobWeTrust/feat/1635/decouple-nitro-inference-engine-into-a-library
[janhq/jan#1635] feat: decouple nitro engine into a library
2 parents 4b4f35d + 81246f2 commit a33e074

26 files changed

+1699
-5
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ jobs:
346346
./install_deps.sh
347347
mkdir build && cd build
348348
cmake -DWHISPER_COREML=1 -DNITRO_VERSION=${{ needs.set-nitro-version.outputs.version }} ..
349-
CC=gcc-8 make -j $(sysctl -n hw.ncp)
349+
CC=gcc-8 make -j $(sysctl -n hw.ncpu)
350350
ls -la
351351
352352
- name: Package

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ CMakeFiles
8686
CMakeScripts
8787
Testing
8888
Makefile
89+
!nitro-node/Makefile
8990
cmake_install.cmake
9091
install_manifest.txt
9192
compile_commands.json

nitro-node/.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.vscode
2+
.env
3+
4+
# Jan inference
5+
.yarn
6+
node_modules
7+
*.tgz
8+
yarn.lock
9+
dist
10+
build
11+
.DS_Store
12+
package-lock.json
13+
14+
*.log
15+
16+
# Nitro binary directory
17+
bin/
18+
# Locally downloaded model for testing
19+
test/test_assets/*.gguf

nitro-node/.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
scripts-prepend-node-path=true
2+
engine-strict=true

nitro-node/.yarnrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodeLinker: node-modules

nitro-node/Makefile

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Makefile for Nitro-node - Build and Clean
2+
3+
# Default target, build all
4+
.PHONY: all
5+
6+
all: publish
7+
8+
# Installs yarn dependencies
9+
#install: build-core
10+
install:
11+
ifeq ($(OS),Windows_NT)
12+
yarn config set network-timeout 300000
13+
endif
14+
yarn install
15+
16+
# Build
17+
build: install
18+
yarn run build
19+
20+
# Download Nitro
21+
download-nitro: install
22+
yarn run downloadnitro
23+
24+
test-ci: install
25+
yarn test
26+
27+
# Note, this make target is just for testing on *NIX systems
28+
test: install
29+
@test -e test/test_assets/*.gguf && echo "test/test_assets/*.gguf is already downloaded" || (mkdir -p test/test_assets && cd test/test_assets/ && curl -JLO "https://huggingface.co/TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf")
30+
yarn test
31+
32+
# Builds and pack
33+
pack: build
34+
yarn run build:publish
35+
36+
# Test that installation will also download nitro binaries
37+
test-e2e-installation: pack
38+
ifeq ($(OS),Windows_NT)
39+
$$env:NITRO_NODE_VERSION=(npm version --json | jq '.["@janhq/nitro-node"]' | foreach {$$_.replace('"','')}) $$env:NITRO_NODE_PKG=(Resolve-Path -Path "janhq-nitro-node$$NITRO_NODE_VERSION.tgz") node ..\.github\scripts\e2e-test-install-nitro-node.js
40+
else
41+
NITRO_NODE_VERSION=$$(npm version --json | jq '.["@janhq/nitro-node"]' | tr -d '"') NITRO_NODE_PKG=$$(realpath "janhq-nitro-node-$${NITRO_NODE_VERSION}.tgz") node ../.github/scripts/e2e-test-install-nitro-node.js
42+
endif
43+
44+
clean:
45+
ifeq ($(OS),Windows_NT)
46+
powershell -Command "Remove-Item -Recurse -Force -Path *.tgz, .yarn, yarn.lock, package-lock.json, bin, dist"
47+
powershell -Command "Get-ChildItem -Path . -Include node_modules -Recurse -Directory | Remove-Item -Recurse -Force"
48+
else
49+
rm -rf *.tgz .yarn yarn.lock package-lock.json bin dist
50+
find . -name "node_modules" -type d -prune -exec rm -rf '{}' +
51+
endif

nitro-node/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# NodeJS wrapper for Nitro
2+
3+
**TODO** Documenting on usage

nitro-node/jest.config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { JestConfigWithTsJest } from "ts-jest";
2+
3+
const jestConfig: JestConfigWithTsJest = {
4+
preset: "ts-jest",
5+
testEnvironment: "node",
6+
transformIgnorePatterns: ["/node_modules/"],
7+
globals: {
8+
RELEASE_URL_PREFIX: "https://api.github.com/repos/janhq/nitro/releases/",
9+
TAGGED_RELEASE_URL_PREFIX:
10+
"https://api.github.com/repos/janhq/nitro/releases/tags",
11+
},
12+
};
13+
14+
export default jestConfig;

nitro-node/package.json

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"name": "@janhq/nitro-node",
3+
"version": "1.0.0",
4+
"description": "This NodeJS library is a wrapper for Nitro, a lightweight (3mb) inference engine written in C++. See nitro.jan.ai",
5+
"main": "./dist/index",
6+
"module": "./dist/index.esm.js",
7+
"types": "./dist/types/index",
8+
"author": "Jan <service@jan.ai>",
9+
"license": "AGPL-3.0",
10+
"scripts": {
11+
"test": "jest --verbose --detectOpenHandles",
12+
"build": "tsc --module commonjs && rollup -c rollup.config.ts",
13+
"predownloadnitro": "npm run build",
14+
"downloadnitro": "node dist/scripts/index.cjs",
15+
"build:publish": "npm pack",
16+
"postinstall": "node -r @janhq/nitro-node/postinstall"
17+
},
18+
"exports": {
19+
".": {
20+
"import": "./dist/index.esm.js",
21+
"require": "./dist/index.cjs",
22+
"types": "./dist/types/index.d.ts"
23+
},
24+
"./scripts": {
25+
"import": "./dist/scripts/index.esm.js",
26+
"require": "./dist/scripts/index.cjs",
27+
"types": "./dist/types/scripts/index.d.ts"
28+
},
29+
"./postinstall": "./postinstall.js"
30+
},
31+
"typesVersions": {
32+
"*": {
33+
".": [
34+
"./dist/index.esm.js.map",
35+
"./dist/types/index.d.ts"
36+
],
37+
"scripts": [
38+
"./dist/scripts/index.esm.js.map",
39+
"./dist/types/scripts/index.d.ts"
40+
]
41+
}
42+
},
43+
"devDependencies": {
44+
"@rollup/plugin-commonjs": "^25.0.7",
45+
"@rollup/plugin-json": "^6.1.0",
46+
"@rollup/plugin-node-resolve": "^15.2.3",
47+
"@rollup/plugin-replace": "^5.0.5",
48+
"@types/download": "^8.0.5",
49+
"@types/jest": "^29.5.11",
50+
"@types/node": "^20.11.4",
51+
"@types/os-utils": "^0.0.4",
52+
"@types/tcp-port-used": "^1.0.4",
53+
"jest": "^29.7.0",
54+
"rollup": "^2.38.5",
55+
"rollup-plugin-sourcemaps": "^0.6.3",
56+
"rollup-plugin-typescript2": "^0.36.0",
57+
"ts-jest": "^29.1.2",
58+
"ts-node": "^10.9.2",
59+
"typescript": "^5.3.3"
60+
},
61+
"dependencies": {
62+
"cross-fetch": "^4.0.0",
63+
"download": "^8.0.0",
64+
"fetch-retry": "^5.0.6",
65+
"os-utils": "^0.0.14",
66+
"tcp-port-used": "^1.0.2"
67+
},
68+
"engines": {
69+
"node": ">=18.0.0"
70+
},
71+
"files": [
72+
"postinstall.js",
73+
"dist",
74+
"package.json",
75+
"tsconfig.json",
76+
"README.md"
77+
]
78+
}

nitro-node/postinstall.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Only run if this package is installed as dependency
2+
if (process.env.INIT_CWD === process.cwd()) process.exit();
3+
4+
const path = require("node:path");
5+
const { downloadNitro } = require("@janhq/nitro-node/scripts");
6+
downloadNitro(path.join(__dirname, "bin"));

0 commit comments

Comments
 (0)