From 054e9390a6b91ef89be6e316acd505124414a1e2 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Sat, 22 Apr 2023 16:47:52 +0800 Subject: [PATCH] Update example CLI Declare main package as dependency. --- examples/cli/package.json | 3 ++- examples/cli/src/index.ts | 25 ++++++++++++++----------- examples/cli/tsconfig.eslint.json | 5 +---- examples/cli/tsconfig.json | 5 +---- examples/cli/yarn.lock | 9 +++++++++ 5 files changed, 27 insertions(+), 20 deletions(-) diff --git a/examples/cli/package.json b/examples/cli/package.json index 58ba2c5..abccd38 100644 --- a/examples/cli/package.json +++ b/examples/cli/package.json @@ -52,7 +52,8 @@ "dependencies": { "@clack/core": "^0.3.2", "esbuild-plugin-wasm": "^1.0.0", - "yargs": "^17.7.1" + "yargs": "^17.7.1", + "@modal-sh/mio-ai": "link:../.." }, "types": "./dist/types/index.d.ts", "main": "./dist/cjs/production/index.js", diff --git a/examples/cli/src/index.ts b/examples/cli/src/index.ts index e387564..47d12c0 100644 --- a/examples/cli/src/index.ts +++ b/examples/cli/src/index.ts @@ -42,12 +42,11 @@ const receiveData = ( }); memory.push(assistantMessage as OpenAi.MessageObject); process.stdout.write('\n\n'); - // const { length: promptTokens } = OpenAi.getPromptTokens( - // normalizedChatMessage, - // model, - // ); - // process.stdout.write(`info:\n${promptTokens} prompt tokens\n${completionTokens} completion tokens`); - process.stdout.write(`info:\n${completionTokens} completion tokens`); + const { length: promptTokens } = OpenAi.getPromptTokens( + normalizedChatMessage, + model, + ); + process.stdout.write(`info:\n${promptTokens} prompt tokens\n${completionTokens} completion tokens`); if (argv.memory) { process.stdout.write(`\n${memory.length} memory items`); } @@ -87,7 +86,7 @@ const receiveData = ( }); }); -const main = (argv: Argv) => new Promise(async (resolve) => { +const main = async (argv: Argv) => { let done = false; let resolveResult = 0; const memory = [] as OpenAi.MessageObject[]; @@ -111,6 +110,7 @@ const main = (argv: Argv) => new Promise(async (resolve) => { return `${OpenAi.MessageRole.USER}:\n${this.valueWithCursor ?? ''}`; }, }); + // eslint-disable-next-line no-await-in-loop const contentRaw = await textPrompt.prompt(); if (isCancel(contentRaw)) { done = true; @@ -121,6 +121,7 @@ const main = (argv: Argv) => new Promise(async (resolve) => { break; } process.stdout.write('\n'); + // eslint-disable-next-line no-await-in-loop await receiveData(aiClient, content, argv, memory); } catch { resolveResult = -1; @@ -129,8 +130,8 @@ const main = (argv: Argv) => new Promise(async (resolve) => { } while (!done); process.stdout.write('\ninfo:\nUser exited loop\n\n'); - resolve(resolveResult); -}); + return resolveResult; +}; main( yargs(hideBin(process.argv)) @@ -160,7 +161,9 @@ main( .then((result) => { process.exit(result); }) - .catch((error) => { - console.error(error); + .catch((errRaw) => { + const error = errRaw as Error; + process.stderr.write(error.message); + process.stderr.write('\n'); process.exit(-1); }); diff --git a/examples/cli/tsconfig.eslint.json b/examples/cli/tsconfig.eslint.json index 202c57f..dc8caec 100644 --- a/examples/cli/tsconfig.eslint.json +++ b/examples/cli/tsconfig.eslint.json @@ -16,9 +16,6 @@ "moduleResolution": "node", "jsx": "react", "esModuleInterop": true, - "target": "es2018", - "paths": { - "@modal-sh/mio-ai": ["./src/packages/@modal-sh/mio-ai"] - } + "target": "esnext" } } diff --git a/examples/cli/tsconfig.json b/examples/cli/tsconfig.json index 2b3d44d..88581b7 100644 --- a/examples/cli/tsconfig.json +++ b/examples/cli/tsconfig.json @@ -16,9 +16,6 @@ "moduleResolution": "node", "jsx": "react", "esModuleInterop": true, - "target": "esnext", - "paths": { - "@modal-sh/mio-ai": ["./src/packages/@modal-sh/mio-ai/src/index.ts"], - } + "target": "esnext" } } diff --git a/examples/cli/yarn.lock b/examples/cli/yarn.lock index 078eae7..32c7498 100644 --- a/examples/cli/yarn.lock +++ b/examples/cli/yarn.lock @@ -241,6 +241,11 @@ picocolors "^1.0.0" sisteransi "^1.0.5" +"@dqbd/tiktoken@^1.0.6": + version "1.0.6" + resolved "https://js.pack.modal.sh/@dqbd%2ftiktoken/-/tiktoken-1.0.6.tgz#96bfd0a4909726c61551a8c783493f01841bd163" + integrity sha512-umSdeZTy/SbPPKVuZKV/XKyFPmXSN145CcM3iHjBbmhlohBJg7vaDp4cPCW+xNlWL6L2U1sp7T2BD+di2sUKdA== + "@esbuild/android-arm64@0.17.17": version "0.17.17" resolved "https://registry.yarnpkg.com/@esbuild/android-arm64/-/android-arm64-0.17.17.tgz#164b054d58551f8856285f386e1a8f45d9ba3a31" @@ -444,6 +449,10 @@ resolved "https://registry.yarnpkg.com/@mdn/browser-compat-data/-/browser-compat-data-5.2.50.tgz#f83a9a97f50e06402b8b633bf6bd3247baed1302" integrity sha512-3qGRdT2lrIloY8xotSRTW7k3oIRJAO7WSsJymTLN1sqeB1qiWPcbQdAiDG/IQWCA2QCVQewTeT2kfZf/you94w== +"@modal-sh/mio-ai@link:../..": + version "0.0.0" + uid "" + "@next/eslint-plugin-next@^13.2.4": version "13.3.0" resolved "https://registry.yarnpkg.com/@next/eslint-plugin-next/-/eslint-plugin-next-13.3.0.tgz#3a4742b0817575cc0dd4d152cb10363584c215ac"