Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split cli #586

Merged
merged 17 commits into from
Jul 19, 2024
4 changes: 2 additions & 2 deletions demo/genaisrc/genaiscript.d.ts

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

4 changes: 2 additions & 2 deletions docs/genaisrc/genaiscript.d.ts

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

10 changes: 1 addition & 9 deletions docs/src/content/docs/reference/scripts/parsers.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ const captures = await parsers.code(file, "(interface_declaration) @i")
The `parsers.math` function uses [mathjs](https://mathjs.org/) to parse a math expression.

```js

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parsers.math function should be used with await as it returns a Promise.

generated by pr-docs-review-commit await_usage

const res = parsers.math("1 + 1")
const res = await parsers.math("1 + 1")
```

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parsers.math function usage example should use await consistently with the function being asynchronous.

generated by pr-docs-review-commit async_mismatch

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parsers.math function call should be awaited with async.

generated by pr-docs-review-commit async_missing


## .env

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate section 'math' in documentation.

generated by pr-docs-review-commit duplicate_section

Expand Down Expand Up @@ -272,14 +272,6 @@ for the current model. This is useful for estimating the number of prompts that
const count = parsers.tokens("...")
```

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The section on parsers.math is duplicated and should be removed as it is already defined under the .env section.

generated by pr-docs-review-commit duplicate_section

## math

The `parsers.math` function uses [mathjs](https://mathjs.org/) to parse a math expression.

```js
const res = parsers.math("1 + 1")
```

## validateJSON

The `parsers.validateJSON` function validates a JSON string against a schema.
Expand Down
2 changes: 1 addition & 1 deletion docs/src/content/docs/reference/scripts/system.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ defTool("math_eval", "Evaluates a math expression", {
required: ["expression"],
}, async (args) => {
const { expression } = args

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parsers.math function should be used with await as it returns a Promise.

generated by pr-docs-review-commit await_usage

return "" + (parsers.math(expression) ?? "?")
return "" + (await parsers.math(expression) ?? "?")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parsers.math function is now awaited, but the containing function is not marked as async.

generated by pr-docs-review-commit async_missing

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parsers.math function usage example should use await consistently with the function being asynchronous.

generated by pr-docs-review-commit async_mismatch

})
`````

Expand Down
4 changes: 2 additions & 2 deletions genaisrc/genaiscript.d.ts

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

161 changes: 81 additions & 80 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,82 +1,83 @@
{
"name": "genaiscript",
"version": "1.45.1",
"main": "built/genaiscript.cjs",
"bin": {
"genaiscript": "./built/genaiscript.cjs"
},
"publisher": "Microsoft",
"repository": {
"url": "https://github.com/microsoft/genaiscript"
},
"homepage": "https://microsoft.github.io/genaiscript",
"keywords": [
"genai",
"ai",
"cli",
"prompt",
"llm",
"generative ai",
"gpt4",
"chatgpt",
"ollama",
"llamacpp"
],
"description": "A CLI for GenAIScript, a generative AI scripting framework.",
"license": "MIT",
"dependencies": {
"@azure/identity": "^4.4.0",
"@lvce-editor/ripgrep": "^1.2.0",
"dockerode": "^4.0.2",
"pdfjs-dist": "4.4.168",
"promptfoo": "^0.72.0",
"tree-sitter-wasms": "^0.1.11",
"tsx": "^4.16.2",
"typescript": "5.5.3",
"vectra": "^0.7.6",
"web-tree-sitter": "^0.22.2",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz"
},
"engines": {
"node": ">=20.0.0"
},
"devDependencies": {
"@types/dockerode": "^3.3.30",
"@types/fs-extra": "^11.0.4",
"@types/memorystream": "^0.3.4",
"@types/node": "^20.14.11",
"@types/papaparse": "^5.3.14",
"@types/prompts": "^2.4.9",
"@types/replace-ext": "^2.0.2",
"@types/semver": "^7.5.8",
"@types/ws": "^8.5.11",
"commander": "^12.1.0",
"dotenv": "^16.4.5",
"esbuild": "^0.23.0",
"execa": "^9.3.0",
"file-type": "^19.1.1",
"fs-extra": "^11.2.0",
"get-stdin": "^9.0.0",
"glob": "^11.0.0",
"mammoth": "^1.8.0",
"memorystream": "^0.3.1",
"node-sarif-builder": "^3.1.0",
"openai": "^4.52.7",
"ora": "^8.0.1",
"pretty-bytes": "^6.1.1",
"prompts": "^2.4.2",
"replace-ext": "^2.0.0",
"semver": "^7.6.3",
"zx": "^8.1.4"
},
"scripts": {
"compile": "esbuild src/main.ts --metafile=./esbuild.meta.json --bundle --platform=node --target=node20 --outfile=built/genaiscript.cjs --external:tsx --external:esbuild --external:get-tsconfig --external:resolve-pkg-maps --external:dockerode --external:pdfjs-dist --external:web-tree-sitter --external:tree-sitter-wasms --external:promptfoo --external:typescript --external:@lvce-editor/ripgrep --external:gpt-3-encoder && node ../../scripts/patch-cli.mjs",
"compile-debug": "esbuild src/main.ts --sourcemap --metafile=./esbuild.meta.json --bundle --platform=node --target=node20 --outfile=built/genaiscript.cjs --external:tsx --external:esbuild --external:get-tsconfig --external:resolve-pkg-maps --external:dockerode --external:pdfjs-dist --external:web-tree-sitter --external:tree-sitter-wasms --external:promptfoo --external:typescript --external:@lvce-editor/ripgrep --external:gpt-3-encoder",
"postcompile": "node built/genaiscript.cjs info help > ../../docs/src/content/docs/reference/cli/commands.md",
"vis:treemap": "npx --yes esbuild-visualizer --metadata esbuild.meta.json --filename esbuild.treemap.html",
"vis:network": "npx --yes esbuild-visualizer --metadata esbuild.meta.json --filename esbuild.network.html --template network",
"go": "yarn compile && node built/genaiscript.cjs",
"test": "node --import tsx --test src/**.test.ts",
"typecheck": "tsc -p src"
}
"name": "genaiscript",
"version": "1.45.1",
"main": "built/genaiscript.cjs",
"bin": {
"genaiscript": "./built/genaiscript.cjs"
},
"publisher": "Microsoft",
"repository": {
"url": "https://github.com/microsoft/genaiscript"
},
"homepage": "https://microsoft.github.io/genaiscript",
"keywords": [
"genai",
"ai",
"cli",
"prompt",
"llm",
"generative ai",
"gpt4",
"chatgpt",
"ollama",
"llamacpp"
],
"description": "A CLI for GenAIScript, a generative AI scripting framework.",
"license": "MIT",
"dependencies": {
"@azure/identity": "^4.4.0",
"@lvce-editor/ripgrep": "^1.2.0",
"dockerode": "^4.0.2",
"mammoth": "^1.8.0",
"mathjs": "^13.0.2",
"pdfjs-dist": "4.4.168",
"promptfoo": "^0.72.0",
"tree-sitter-wasms": "^0.1.11",
"tsx": "^4.16.2",
"typescript": "5.5.3",
"vectra": "^0.7.6",
"web-tree-sitter": "^0.22.2",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz"
},
"engines": {
"node": ">=20.0.0"
},
"devDependencies": {
"@types/dockerode": "^3.3.30",
"@types/fs-extra": "^11.0.4",
"@types/memorystream": "^0.3.4",
"@types/node": "^20.14.11",
"@types/papaparse": "^5.3.14",
"@types/prompts": "^2.4.9",
"@types/replace-ext": "^2.0.2",
"@types/semver": "^7.5.8",
"@types/ws": "^8.5.11",
"commander": "^12.1.0",
"dotenv": "^16.4.5",
"esbuild": "^0.23.0",
"execa": "^9.3.0",
"file-type": "^19.1.1",
"fs-extra": "^11.2.0",
"get-stdin": "^9.0.0",
"glob": "^11.0.0",
"memorystream": "^0.3.1",
"node-sarif-builder": "^3.1.0",
"openai": "^4.52.7",
"ora": "^8.0.1",
"pretty-bytes": "^6.1.1",
"prompts": "^2.4.2",
"replace-ext": "^2.0.0",
"semver": "^7.6.3",
"zx": "^8.1.4"
},
"scripts": {
"compile": "esbuild src/main.ts --metafile=./esbuild.meta.json --bundle --platform=node --target=node20 --outfile=built/genaiscript.cjs --external:tsx --external:esbuild --external:get-tsconfig --external:resolve-pkg-maps --external:dockerode --external:pdfjs-dist --external:web-tree-sitter --external:tree-sitter-wasms --external:promptfoo --external:typescript --external:@lvce-editor/ripgrep --external:gpt-3-encoder --external:mammoth --external:xlsx --external:mathjs --external:@azure/identity&& node ../../scripts/patch-cli.mjs",
"compile-debug": "esbuild src/main.ts --sourcemap --metafile=./esbuild.meta.json --bundle --platform=node --target=node20 --outfile=built/genaiscript.cjs --external:tsx --external:esbuild --external:get-tsconfig --external:resolve-pkg-maps --external:dockerode --external:pdfjs-dist --external:web-tree-sitter --external:tree-sitter-wasms --external:promptfoo --external:typescript --external:@lvce-editor/ripgrep --external:gpt-3-encoder --external:mammoth --external:xlsx --external:mathjs --external:@azure/identity",
"postcompile": "node built/genaiscript.cjs info help > ../../docs/src/content/docs/reference/cli/commands.md",
"vis:treemap": "npx --yes esbuild-visualizer --metadata esbuild.meta.json --filename esbuild.treemap.html",
"vis:network": "npx --yes esbuild-visualizer --metadata esbuild.meta.json --filename esbuild.network.html --template network",
"go": "yarn compile && node built/genaiscript.cjs",
"test": "node --import tsx --test src/**.test.ts",
"typecheck": "tsc -p src"
}
}
10 changes: 10 additions & 0 deletions packages/cli/src/azuretoken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { AZURE_OPENAI_TOKEN_SCOPES } from "../../core/src/constants"

export async function createAzureToken(signal: AbortSignal): Promise<string> {
const { DefaultAzureCredential } = await import("@azure/identity")
const azureToken = await new DefaultAzureCredential().getToken(
AZURE_OPENAI_TOKEN_SCOPES.slice(),
{ abortSignal: signal }
)
return azureToken.token
}
14 changes: 5 additions & 9 deletions packages/cli/src/nodehost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { execa } from "execa"
import { join } from "node:path"
import { createNodePath } from "./nodepath"
import { DockerManager } from "./docker"
import { DefaultAzureCredential, AccessToken } from "@azure/identity"
import { createFileSystem } from "../../core/src/filesystem"
import { filterGitIgnore } from "../../core/src/gitignore"
import {
Expand Down Expand Up @@ -44,6 +43,7 @@ import { createBundledParsers } from "../../core/src/pdf"
import { AbortSignalOptions, TraceOptions } from "../../core/src/trace"
import { logVerbose, unique } from "../../core/src/util"
import { parseModelIdentifier } from "../../core/src/models"
import { createAzureToken } from "./azuretoken"

class NodeServerManager implements ServerManager {
async start(): Promise<void> {
Expand Down Expand Up @@ -138,7 +138,7 @@ export class NodeHost implements RuntimeHost {
await parseDefaultsFromEnv(process.env)
}

private _azureToken: AccessToken
private _azureToken: string
async getLanguageModelConfiguration(
modelId: string,
options?: { token?: boolean } & AbortSignalOptions & TraceOptions
Expand All @@ -152,14 +152,10 @@ export class NodeHost implements RuntimeHost {
!tok.token &&
tok.provider === MODEL_PROVIDER_AZURE
) {
if (!this._azureToken) {
this._azureToken = await new DefaultAzureCredential().getToken(
AZURE_OPENAI_TOKEN_SCOPES.slice(),
{ abortSignal: signal }
)
}
if (!this._azureToken)
this._azureToken = await createAzureToken(signal)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no error handling for the createAzureToken function. If it fails, the error will not be caught and handled properly, which could lead to unexpected behavior. Consider adding a try-catch block around this function call. ๐Ÿ˜Š

generated by pr-review-commit missing_error_handling

if (!this._azureToken) throw new Error("Azure token not available")
tok.token = "Bearer " + this._azureToken.token
tok.token = "Bearer " + this._azureToken
}
return tok
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"tsx": "^4.16.2",
"typescript": "5.5.3",
"vectra": "^0.7.6",
"xlsx": "^0.18.5",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz",
"yaml": "^2.4.5"
},
"scripts": {
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/docx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { host } from "./host"
import { TraceOptions } from "./trace"
import { logError } from "./util"
import { extractRawText } from "mammoth"

/**
* parses docx, require mammoth to be installed
Expand All @@ -15,6 +13,7 @@ export async function DOCXTryParse(
): Promise<string> {
const { trace } = options || {}
try {
const { extractRawText } = await import("mammoth")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dynamic import of "mammoth" module is used. This could lead to performance issues as it might cause delays in execution. Consider importing the module at the top of the file if it does not have side effects. ๐Ÿ˜Š

generated by pr-review-commit dynamic_import

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mammoth module is dynamically imported but there is no check to ensure that the module is actually installed. Consider adding a check to ensure that the module is installed before importing it.

generated by pr-review-commit missing_dependency

const path = !/^\//.test(file)
? host.path.join(host.projectFolder(), file)
: file
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export async function resolveFileContent(
file.content = await DOCXTryParse(filename, options)
} else if (XLSX_REGEX.test(filename)) {
const bytes = await host.readFile(filename)
const sheets = XLSXParse(bytes)
const sheets = await XLSXParse(bytes)
file.content = JSON.stringify(sheets, null, 2)
} else {
const mime = lookupMime(filename)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/genaisrc/genaiscript.d.ts

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

2 changes: 1 addition & 1 deletion packages/core/src/genaisrc/system.math.genai.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ defTool("math_eval", "Evaluates a math expression", {
required: ["expression"],
}, async (args) => {
const { expression } = args
return "" + (parsers.math(expression) ?? "?")
return "" + (await parsers.math(expression) ?? "?")
})
6 changes: 3 additions & 3 deletions packages/core/src/math.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { evaluate } from "mathjs"
import { TraceOptions } from "./trace"

export function MathTryEvaluate(
export async function MathTryEvaluate(
expr: string,
options?: { defaultValue?: number } & TraceOptions
): string | number | undefined {
): Promise<string | number | undefined> {
const { trace, defaultValue } = options || {}
try {
if (!expr) return defaultValue
const { evaluate } = await import("mathjs")
const res = evaluate(expr)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dynamic import of 'mathjs' can lead to performance issues and makes the code harder to statically analyze. Consider importing this module at the top of the file if it does not have side effects. ๐Ÿ˜Š

generated by pr-review-commit dynamic_import

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The evaluate function from mathjs is called without any error handling. If the evaluation fails (for example, if the expression is not valid), an unhandled exception will be thrown. Consider adding error handling to improve the robustness of the code. ๐Ÿ˜Š

generated by pr-review-commit missing_error_handling

return res
} catch (e) {
Expand Down
Loading
Loading