Skip to content

Commit

Permalink
[Style] Add GitHub action for linter and pre-commit hook for formatin…
Browse files Browse the repository at this point in the history
…g code
  • Loading branch information
Neet-Nestor committed May 22, 2024
1 parent fe89f36 commit 77ea8f4
Show file tree
Hide file tree
Showing 24 changed files with 2,095 additions and 1,469 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ debug
lib
build
node_modules
.eslintrc.cjs
3rdparty
.eslintrc.cjs
5 changes: 3 additions & 2 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module.exports = {
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
root: true,
rules: {
"@typescript-eslint/no-explicit-any": "off"
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-empty-function": "off"
}
};
27 changes: 27 additions & 0 deletions .github/workflows/linter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Linter

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16'

- name: Install dependencies
run: npm install

- name: Run lint
run: npm run lint
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx lint-staged
6 changes: 6 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"./**/*.{js,ts,jsx,tsx,json,html,css,md}": [
"eslint --fix",
"prettier --write"
]
}
7 changes: 7 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dist
debug
lib
build
node_modules
3rdparty
.eslintrc.cjs
136 changes: 129 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
"type": "module",
"scripts": {
"build": "rollup -c && ./cleanup-index-js.sh",
"lint": "npx eslint .",
"test": "yarn jest"
"lint": "npx eslint ./src/ && npx prettier ./src --check",
"test": "yarn jest",
"format": "prettier --write \"./src/\"",
"prepare": "husky"
},
"files": [
"lib"
Expand Down Expand Up @@ -36,7 +38,11 @@
"@webgpu/types": "^0.1.24",
"buffer": "^5.7.1",
"eslint": "^8.41.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"husky": "^9.0.11",
"jest": "^29.7.0",
"prettier": "3.2.5",
"process": "^0.11.10",
"rollup": "^2.56.2",
"rollup-plugin-ignore": "^1.0.10",
Expand Down
50 changes: 34 additions & 16 deletions src/cache_util.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import * as tvmjs from "tvmjs";
import {
AppConfig,
ModelRecord,
prebuiltAppConfig,
} from "./config";
import { AppConfig, ModelRecord, prebuiltAppConfig } from "./config";

function findModelRecord(modelId: string, appConfig?: AppConfig): ModelRecord {
const matchedItem = appConfig?.model_list.find(
item => item.model_id == modelId
(item) => item.model_id == modelId,
);
if (matchedItem !== undefined) {
return matchedItem;
}
throw Error("Cannot find model_url for " + modelId);
}

export async function hasModelInCache(modelId: string, appConfig?: AppConfig): Promise<boolean> {
export async function hasModelInCache(
modelId: string,
appConfig?: AppConfig,
): Promise<boolean> {
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
Expand All @@ -25,7 +24,10 @@ export async function hasModelInCache(modelId: string, appConfig?: AppConfig): P
return tvmjs.hasNDArrayInCache(modelUrl, "webllm/model", cacheType);
}

export async function deleteModelAllInfoInCache(modelId: string, appConfig?: AppConfig) {
export async function deleteModelAllInfoInCache(
modelId: string,
appConfig?: AppConfig,
) {
// function to delete model all information in cache
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
Expand All @@ -34,30 +36,43 @@ export async function deleteModelAllInfoInCache(modelId: string, appConfig?: App
await deleteModelInCache(modelId, appConfig);
// delete wasm in cache
await deleteModelWasmInCache(modelId, appConfig);
// delete chat config
// delete chat config
await deleteChatConfigInCache(modelId, appConfig);
}


export async function deleteModelInCache(modelId: string, appConfig?: AppConfig) {
export async function deleteModelInCache(
modelId: string,
appConfig?: AppConfig,
) {
// delete the model NDArray In Cache
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
}
const modelRecord = findModelRecord(modelId, appConfig);
let modelCache: tvmjs.ArtifactCacheTemplate;
if (appConfig.useIndexedDBCache) {
tvmjs.deleteNDArrayCache(modelRecord.model_url, "webllm/model", "indexeddb");
tvmjs.deleteNDArrayCache(
modelRecord.model_url,
"webllm/model",
"indexeddb",
);
modelCache = new tvmjs.ArtifactIndexedDBCache("webllm/model");
} else {
tvmjs.deleteNDArrayCache(modelRecord.model_url, "webllm/model", "cache");
modelCache = new tvmjs.ArtifactCache("webllm/model");
}
await modelCache.deleteInCache(new URL("tokenizer.model", modelRecord.model_url).href);
await modelCache.deleteInCache(new URL("tokenizer.json", modelRecord.model_url).href);
await modelCache.deleteInCache(
new URL("tokenizer.model", modelRecord.model_url).href,
);
await modelCache.deleteInCache(
new URL("tokenizer.json", modelRecord.model_url).href,
);
}

export async function deleteChatConfigInCache(modelId: string, appConfig?: AppConfig) {
export async function deleteChatConfigInCache(
modelId: string,
appConfig?: AppConfig,
) {
// delete the chat configuration in Cache
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
Expand All @@ -73,7 +88,10 @@ export async function deleteChatConfigInCache(modelId: string, appConfig?: AppCo
await configCache.deleteInCache(configUrl);
}

export async function deleteModelWasmInCache(modelId: string, appConfig?: AppConfig) {
export async function deleteModelWasmInCache(
modelId: string,
appConfig?: AppConfig,
) {
// delete the wasm in Cache
if (appConfig === undefined) {
appConfig = prebuiltAppConfig;
Expand Down
Loading

0 comments on commit 77ea8f4

Please sign in to comment.