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
Merged
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
with:
node-version: "${{ matrix.node-version }}"
cache: yarn
- run: yarn install --frozen-lockfile
- run: yarn install --frozen-lockfile --prefer-offline
- name: typecheck
run: yarn typecheck
- name: compile
Expand Down
666 changes: 609 additions & 57 deletions THIRD_PARTY_LICENSES.md

Large diffs are not rendered by default.

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.

11 changes: 2 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@
"slides"
],
"nohoist": [
"dockerode",
"pdfjs-dist",
"mammoth",
"promptfoo",
"string-ansi",
"canvas",
"sharp",
"astro"
"json5"
]
},
"scripts": {
Expand Down Expand Up @@ -80,7 +73,7 @@
"releaseNotes": "node packages/cli/built/genaiscript.cjs run git-release-notes"
}
},
"devDependencies": {
"dependencies": {
"glob": "^11.0.0",
"zx": "^8.1.4"
}
Expand Down
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.2",
"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.2",
"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
Loading
Loading