-
Notifications
You must be signed in to change notification settings - Fork 134
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
Support for direct call of genaiscript from js #925
Changes from 10 commits
dfe3201
fcee692
1f68108
b6a788c
8449ce2
953f19a
e235171
77752b4
183e40a
54fd51f
abafab7
b816815
32e767e
93fee36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
title: Node.JS API | ||
sidebar: | ||
order: 50 | ||
--- | ||
|
||
import { Tabs, TabItem } from "@astrojs/starlight/components" | ||
|
||
This page describes how to import and use the [cli](/genaiscript/reference/cli) as an API in your Node.JS application. | ||
|
||
Assuming you have have added the cli as a dependency in your project, you can import the cli as follows: | ||
|
||
<Tabs> | ||
<TabItem value="npm" label="npm"> | ||
|
||
```sh | ||
npm install -D genaiscript | ||
``` | ||
|
||
</TabItem> | ||
|
||
<TabItem value="yarn" label="yarn"> | ||
|
||
```sh | ||
yarn add -D genaiscript | ||
``` | ||
|
||
</TabItem> | ||
|
||
</Tabs> | ||
|
||
The API can be imported using imports (CommonJS is not supported) from **"genaiscript/api"**. | ||
|
||
```javascript | ||
import { runScript } from "genaiscript/api" | ||
``` | ||
|
||
The imported API is a tiny-zero dependency wrapper that takes the arguments and spawns a [Node.JS worker thread](https://nodejs.org/api/worker_threads.html) to run GenAIScript. | ||
|
||
- No pollutation of the globals | ||
- No side effects on the process | ||
|
||
## `run` | ||
|
||
The `run` function wraps the [cli run](/genaiscript/reference/cli/run) command. | ||
|
||
```javascript | ||
import { run } from "genaiscript/api" | ||
|
||
const results = await run("summarize", ["myfile.txt"]) | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Front matter is missing at the beginning of the file. It should include a title and sidebar order.
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,11 +23,11 @@ | |
|
||
The CLI is a Node.JS package hosted on [npm](https://www.npmjs.com/package/genaiscript). | ||
|
||
- Install [Node.JS LTS](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) (Node.JS includes npm and npx). | ||
- Install [Node.JS LTS](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) (Node.JS includes npm and npx). | ||
|
||
## Installation | ||
|
||
- Install locally as a `devDependency` in your project. | ||
- Install locally as a `devDependency` in your project. | ||
|
||
<Tabs> | ||
<TabItem label="npm" icon="seti:npm"> | ||
|
@@ -46,13 +46,13 @@ | |
</TabItem> | ||
</Tabs> | ||
|
||
- Install it globally. | ||
- Install it globally. | ||
|
||
```sh "-g" | ||
npm install -g genaiscript | ||
``` | ||
|
||
- Check that your node version is at least 20._ and npm 10._ by running this command. | ||
- Check that your node version is at least 20._ and npm 10._ by running this command. | ||
|
||
```sh | ||
node -v | ||
|
@@ -77,13 +77,13 @@ | |
npx genaiscript ... | ||
``` | ||
|
||
- Add `--yes` to skip the confirmation prompt, which is useful in a CI scenario. | ||
- Add `--yes` to skip the confirmation prompt, which is useful in a CI scenario. | ||
|
||
```sh "--yes" | ||
npx --yes genaiscript ... | ||
``` | ||
|
||
- Specify the version range to avoid unexpected behavior with cached installations of the CLI using npx. | ||
- Specify the version range to avoid unexpected behavior with cached installations of the CLI using npx. | ||
|
||
```sh "@^1.16.0" | ||
npx --yes genaiscript@^1.16.0 ... | ||
|
@@ -129,9 +129,13 @@ | |
```sh | ||
npx genaiscript scripts model [script] | ||
``` | ||
|
||
where [script] can be a script id or a file path. | ||
|
||
## Using a the CLI as a Node.JS API | ||
|
||
The CLI can be imported and [used as an API in your Node.JS application](/genaiscript/reference/cli/api). | ||
|
||
Check warning on line 138 in docs/src/content/docs/reference/cli/index.mdx GitHub Actions / build
|
||
## Topics | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The link to the API documentation should be more descriptive than "Using a the CLI as a Node.JS API". Consider using a more specific title.
|
||
<DirectoryLinks directory="reference/cli" /> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { GenerationResult } from "../../core/src/generation" | ||
import type { PromptScriptRunOptions } from "../../core/src/server/messages" | ||
import { Worker } from "node:worker_threads" | ||
import { fileURLToPath } from "url" | ||
import { dirname, join } from "node:path" | ||
|
||
/** | ||
* Runs a GenAIScript script with the given files and options. | ||
* This function acts similarly to the `run` command in the CLI. | ||
* @param scriptId script identifier or full file path | ||
* @param files list of file paths to run the script on, leave empty if not needed | ||
* @param options | ||
* @returns | ||
*/ | ||
export async function run( | ||
scriptId: string, | ||
files?: string[], | ||
options?: Partial<PromptScriptRunOptions> | ||
): Promise<{ | ||
exitCode: number | ||
result?: GenerationResult | ||
}> { | ||
const { label } = options || {} | ||
const workerData = { | ||
type: "run", | ||
scriptId, | ||
files: files || [], | ||
options, | ||
} | ||
|
||
const filename = | ||
typeof __filename === "undefined" | ||
// ignore esbuild warning | ||
? join(dirname(fileURLToPath(import.meta.url)), "genaiscript.cjs") | ||
: __filename | ||
const worker = new Worker(filename, { workerData, name: label }) | ||
return new Promise((resolve, reject) => { | ||
worker.on("online", () => process.stderr.write(`worker: online\n`)) | ||
worker.on("message", resolve) | ||
worker.on("error", reject) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,24 @@ | ||
// Main entry point for the CLI application | ||
|
||
// Import necessary modules and functions | ||
import { installGlobals } from "../../core/src/globals" | ||
import { cli } from "./cli" | ||
import { workerData } from "node:worker_threads" | ||
import { worker } from "./worker" | ||
import { run } from "./api" | ||
import { PromptScriptRunOptions } from "../../core/src/server/messages" | ||
import { GenerationResult } from "../../core/src/generation" | ||
|
||
// Initialize global settings or variables for the application | ||
// This might include setting up global error handlers or configurations | ||
installGlobals() | ||
export { run, type PromptScriptRunOptions, type GenerationResult } | ||
|
||
// Execute the command-line interface logic | ||
// This function likely handles parsing input arguments and executing commands | ||
cli() | ||
// if this file is not the entry point, skip cli | ||
if (require.main === module) { | ||
// Initialize global settings or variables for the application | ||
// This might include setting up global error handlers or configurations | ||
installGlobals() | ||
if (workerData) { | ||
// Executes a worker | ||
worker() | ||
} else { | ||
// Execute the command-line interface logic | ||
// This function likely handles parsing input arguments and executing commands | ||
cli() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { workerData, parentPort } from "node:worker_threads" | ||
import { runScriptInternal } from "./run" | ||
import { NodeHost } from "./nodehost" | ||
|
||
export async function worker() { | ||
await NodeHost.install(undefined) // Install NodeHost with environment options | ||
|
||
const { type, ...data } = workerData as { | ||
type: string | ||
} & object | ||
switch (type) { | ||
case "run": { | ||
const { scriptId, files, ...options } = data as { | ||
scriptId: string | ||
files: string[] | ||
} & object | ||
const { result } = await runScriptInternal(scriptId, files, options) | ||
parentPort.postMessage(result) | ||
break | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Front matter is missing at the beginning of the file. It should include a title and sidebar order.