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

support for module .mjs files #561

Merged
merged 66 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
e3785c3
use tsx to run .mjs files
pelikhan Jun 21, 2024
045d828
mark esbuild as external
pelikhan Jun 21, 2024
3510265
add tsx dep
pelikhan Jun 21, 2024
ac47560
added module test
pelikhan Jun 21, 2024
581d784
refine assertion
pelikhan Jun 21, 2024
6cdfb15
packaging tsx
pelikhan Jun 21, 2024
a4b67ba
alwasy importPrompt?
pelikhan Jun 21, 2024
b5649e5
add tidy examples
pelikhan Jun 21, 2024
1b53287
updated globs
pelikhan Jun 21, 2024
cf420d3
various docs
pelikhan Jun 21, 2024
aaae594
typo
pelikhan Jun 21, 2024
350974b
more docs tweak
pelikhan Jun 21, 2024
244b0cf
updatd samples
pelikhan Jun 21, 2024
75e3ab4
notes about .mjs
pelikhan Jun 21, 2024
a87e286
move a few tets to mjs
pelikhan Jun 21, 2024
791a0fc
updated test
pelikhan Jun 21, 2024
f31da6b
docs
pelikhan Jun 21, 2024
0d4047e
docs
pelikhan Jun 21, 2024
3fbf181
fix tests
pelikhan Jun 21, 2024
d06ba4c
more mjs file
pelikhan Jun 21, 2024
0e4c978
added more docs
pelikhan Jun 21, 2024
65ea9bb
package more deps
pelikhan Jun 21, 2024
61f07a3
add *.mjs to generated jsconfig
pelikhan Jun 21, 2024
44cc9ee
calm down docs reviewer
pelikhan Jun 21, 2024
9202c50
module voodoo
pelikhan Jun 21, 2024
2ab5b93
updated jsconfig.json
pelikhan Jun 21, 2024
6b31b86
more logging of imports
pelikhan Jun 21, 2024
3122c55
absolute path fix
pelikhan Jun 21, 2024
a8d38ab
execution in server
pelikhan Jun 21, 2024
c8caffd
streaming of trace back to vscode
pelikhan Jun 22, 2024
bf636c7
handle cancellation
pelikhan Jun 22, 2024
2adc07a
add start stop script
pelikhan Jun 22, 2024
9572d04
more tweaking of format
pelikhan Jun 23, 2024
ea918bd
more plumbing
pelikhan Jun 24, 2024
cccf8dc
removing virtual file
pelikhan Jun 24, 2024
e9268d4
more fixes
pelikhan Jun 25, 2024
4ddb91c
removing batch cli command, removing gpspecs
pelikhan Jun 25, 2024
34a0c0f
more fixing
pelikhan Jun 25, 2024
0139a28
typo
pelikhan Jun 25, 2024
a4a42bb
updating fragment
pelikhan Jun 25, 2024
9df798d
default files on phi3
pelikhan Jun 26, 2024
9741cf9
shorter summary
pelikhan Jun 26, 2024
3bde676
tweak summary
pelikhan Jun 27, 2024
4575384
more plumbing
pelikhan Jun 27, 2024
54edf5b
better handling of building files
pelikhan Jun 27, 2024
d1a856d
catch exception in commands
pelikhan Jun 27, 2024
a215270
piping more messages to vscode
pelikhan Jun 27, 2024
5ac9327
wire cli info
pelikhan Jun 29, 2024
1371029
more logging on run
pelikhan Jun 29, 2024
b184d76
fixing removal of spec
pelikhan Jun 29, 2024
8d0c456
more script fixes
pelikhan Jun 29, 2024
451e969
nire script fixes
pelikhan Jun 29, 2024
c360163
build fixes
pelikhan Jun 29, 2024
96d4fec
upgrading dependencies
pelikhan Jun 29, 2024
6635661
removing evalprompt
pelikhan Jun 29, 2024
e8b59d4
test logging
pelikhan Jun 30, 2024
3a76403
more logging
pelikhan Jun 30, 2024
422e03d
Revert "removing evalprompt"
pelikhan Jun 30, 2024
8f255ac
revert eval import
pelikhan Jun 30, 2024
4698ce7
updated command.md
pelikhan Jun 30, 2024
353df6f
use eval for .js files
pelikhan Jun 30, 2024
848f457
add genai-test command
pelikhan Jun 30, 2024
618c983
removing batch commands
pelikhan Jun 30, 2024
c74c0ec
logging of csv output
pelikhan Jun 30, 2024
41f91f5
emoji for imports
pelikhan Jun 30, 2024
25ae6d5
add ini test
pelikhan Jun 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 77 additions & 15 deletions THIRD_PARTY_LICENSES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4297,7 +4297,7 @@ Apache License
The following npm packages may be included in this product:

- @aws-sdk/client-cognito-identity@3.600.0
- @aws-sdk/client-sagemaker@3.600.0
- @aws-sdk/client-sagemaker@3.602.0
- @aws-sdk/client-sso-oidc@3.600.0
- @aws-sdk/client-sso@3.598.0
- @aws-sdk/client-sts@3.600.0
Expand Down Expand Up @@ -5013,8 +5013,8 @@ The following npm packages may be included in this product:
- @types/nlcst@2.0.3
- @types/node-fetch@2.6.11
- @types/node@17.0.45
- @types/node@18.19.37
- @types/node@20.14.6
- @types/node@18.19.38
- @types/node@20.14.7
- @types/papaparse@5.3.14
- @types/pg@8.11.6
- @types/qs@6.9.15
Expand Down Expand Up @@ -5054,7 +5054,7 @@ MIT License

The following npm package may be included in this product:

- genaiscript-vscode@1.40.4
- genaiscript-vscode@1.41.7

This package contains the following license and notice below:

Expand Down Expand Up @@ -18595,11 +18595,12 @@ third-party archives.

-----------

The following npm package may be included in this product:
The following npm packages may be included in this product:

- typescript@5.4.5
- typescript@5.5.2

This package contains the following license and notice below:
These packages each contain the following license and notice below:

Apache License

Expand Down Expand Up @@ -22857,7 +22858,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI

The following npm package may be included in this product:

- electron-to-chromium@1.4.807
- electron-to-chromium@1.4.808

This package contains the following license and notice below:

Expand Down Expand Up @@ -25157,11 +25158,11 @@ The following npm packages may be included in this product:
- de-indent@1.0.2
- degenerator@5.0.1
- dlv@1.1.3
- docs@1.40.4
- docs@1.41.7
- eastasianwidth@0.2.0
- genaiscript-core@1.40.4
- genaiscript-sample@1.40.4
- genaiscript@1.40.4
- genaiscript-core@1.41.7
- genaiscript-sample@1.41.7
- genaiscript@1.41.7
- hast-util-is-body-ok-link@3.0.0
- hast-util-to-string@3.0.0
- html-whitespace-sensitive-tag-names@3.0.0
Expand Down Expand Up @@ -28304,7 +28305,7 @@ The following npm packages may be included in this product:
- @volar/source-map@2.3.0
- @volar/typescript@2.2.5
- @vue/language-core@2.0.21
- typescript-auto-import-cache@0.3.2
- typescript-auto-import-cache@0.3.3

These packages each contain the following license and notice below:

Expand Down Expand Up @@ -28736,7 +28737,7 @@ SOFTWARE.

The following npm package may be included in this product:

- @astrojs/starlight@0.24.3
- @astrojs/starlight@0.24.4

This package contains the following license and notice below:

Expand Down Expand Up @@ -29240,6 +29241,67 @@ SOFTWARE.

-----------

The following npm package may be included in this product:

- tsx@4.15.7

This package contains the following license and notice below:

MIT License

Copyright (c) Hiroki Osame <hiroki.osame@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

-----------

The following npm packages may be included in this product:

- get-tsconfig@4.7.5
- resolve-pkg-maps@1.0.0

These packages each contain the following license and notice below:

MIT License

Copyright (c) Hiroki Osame <hiroki.osame@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

-----------

The following npm packages may be included in this product:

- hasown@2.0.2
Expand Down Expand Up @@ -31992,7 +32054,7 @@ THE SOFTWARE.

The following npm package may be included in this product:

- is-core-module@2.13.1
- is-core-module@2.14.0

This package contains the following license and notice below:

Expand Down Expand Up @@ -34141,7 +34203,7 @@ THE SOFTWARE.

The following npm package may be included in this product:

- vue-router@4.3.3
- vue-router@4.4.0

This package contains the following license and notice below:

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"pdfjs-dist": "4.3.136",
"promptfoo": "^0.65.2",
"tree-sitter-wasms": "^0.1.11",
"tsx": "^4.15.7",
"typescript": "5.4.5",
"web-tree-sitter": "^0.22.2",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz"
Expand Down Expand Up @@ -68,11 +69,10 @@
"prompts": "^2.4.2",
"replace-ext": "^2.0.0",
"semver": "^7.6.2",
"tsx": "^4.15.6",
"zx": "^8.1.2"
},
"scripts": {
"compile": "esbuild src/main.ts --bundle --platform=node --target=node20 --outfile=built/genaiscript.cjs --external:dockerode --external:pdfjs-dist --external:llamaindex --external:web-tree-sitter --external:tree-sitter-wasms --external:promptfoo && node ../../scripts/patch-cli.mjs",
"compile": "esbuild src/main.ts --bundle --platform=node --target=node20 --outfile=built/genaiscript.cjs --external:tsx --external:dockerode --external:pdfjs-dist --external:llamaindex --external:web-tree-sitter --external:tree-sitter-wasms --external:promptfoo && node ../../scripts/patch-cli.mjs",
"postcompile": "node built/genaiscript.cjs info help > ../../docs/src/content/docs/reference/cli/commands.md",
"go": "yarn compile && node built/genaiscript.cjs",
"test": "node --import tsx --test src/**.test.ts",
Expand Down
5 changes: 3 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"gpt-tokenizer": "^2.1.2",
"html-escaper": "^3.0.3",
"html-to-text": "^9.0.5",
"ignore": "^5.3.1",
"inflection": "^3.0.0",
"ini": "^4.1.3",
"json5": "^2.2.3",
Expand All @@ -55,10 +56,10 @@
"toml": "^3.0.0",
"tree-sitter-wasms": "^0.1.11",
"ts-dedent": "^2.2.0",
"tsx": "^4.15.7",
"typescript": "5.4.5",
"xlsx": "^0.18.5",
"yaml": "^2.4.5",
"ignore": "^5.3.1"
"yaml": "^2.4.5"
},
"scripts": {
"typecheck": "tsc -p src",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/aici.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ChatCompletionContentPartText,
ChatCompletionHandler,
ChatCompletionResponse,
LanguageModel,
Expand All @@ -9,7 +10,6 @@ import { fromHex, logError, normalizeInt, utf8Decode } from "./util"
import { AICI_CONTROLLER, TOOL_ID } from "./constants"
import { LanguageModelConfiguration, host } from "./host"
import { NotSupportedError, RequestError } from "./error"
import { ChatCompletionContentPartText } from "openai/resources"
import { createFetch } from "./fetch"
import { parseModelIdentifier } from "./models"

Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import { estimateChatTokens } from "./tokens"
import { createChatTurnGenerationContext } from "./runpromptcontext"
import { dedent } from "./indent"

export type ChatCompletionContentPartText = OpenAI.Chat.Completions.ChatCompletionContentPartText

export type ChatCompletionContentPart = OpenAI.Chat.Completions.ChatCompletionContentPart

export type ChatCompletionTool = OpenAI.Chat.Completions.ChatCompletionTool

export type ChatCompletionChunk = OpenAI.Chat.Completions.ChatCompletionChunk
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const GENAI_JS_EXT = ".genai.js"
export const GENAI_MJS_EXT = ".genai.mjs"
export const GENAI_JS_GLOB = "**/*.genai.*js"
export const GENAI_JS_REGEX = /\.genai\.m?js$/i
export const MODULE_JS_REGEX = /\.mjs$/i
export const GPSPEC_REGEX = /\.gpspec\.md$/i
export const HTTPS_REGEX = /^https:\/\//i
export const CSV_REGEX = /\.(t|c)sv$/i
Expand Down
13 changes: 8 additions & 5 deletions packages/core/src/expander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { assert, normalizeFloat, normalizeInt, unique } from "./util"
import { MarkdownTrace } from "./trace"
import { errorMessage, isCancelError } from "./error"
import { estimateTokens } from "./tokens"
import { MAX_TOOL_CALLS, MODEL_PROVIDER_AICI, SYSTEM_FENCE } from "./constants"
import {
MAX_TOOL_CALLS,
MODEL_PROVIDER_AICI,
MODULE_JS_REGEX,
SYSTEM_FENCE,
} from "./constants"
import { PromptImage, renderPromptNode } from "./promptdom"
import { GenerationOptions, createPromptContext } from "./promptcontext"
import { evalPrompt } from "./evalprompt"
Expand Down Expand Up @@ -116,11 +121,9 @@ async function callExpander(
}

try {
if (/^export\s+default\s+/m.test(r.jsSource)) {
if (!/\.mjs$/i.test(r.filename))
throw new Error("export default requires .mjs file")
if (MODULE_JS_REGEX.test(r.filename))
await importPrompt(ctx, r, { logCb })
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
} else {
else {
await evalPrompt(ctx, r, {
sourceMaps: true,
logCb,
Expand Down
13 changes: 7 additions & 6 deletions packages/core/src/importprompt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assert } from "console"
import { host } from "./host"
import { logError } from "./util"
import { tsImport } from "tsx/esm/api"

function resolveGlobal(): any {
if (typeof window !== "undefined")
Expand All @@ -25,20 +26,20 @@ export async function importPrompt(
try {
// override global context
for (const field of Object.keys(ctx0)) {
assert(!glb[field])
assert(field === "console" || !glb[field], `overriding global field ${field}`)
oldGlb[field] = glb[field]
glb[field] = (ctx0 as any)[field]
}

const modulePath = filename.startsWith("/")
? filename
: host.path.join(host.projectFolder(), filename)
const module = await import(modulePath)
const parentURL = import.meta.url || new URL(__filename, "file://").href
const module = await tsImport(modulePath, {
parentURL,
})
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
const main = module.default
if (!main) throw new Error("default import function missing")
if (typeof main !== "function")
throw new Error("default export must be a function")
await main(ctx0)
if (typeof main === "function") await main(ctx0)
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
logError(err)
throw err
Expand Down
9 changes: 4 additions & 5 deletions packages/core/src/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { ChatCompletionMessageParam } from "./chat"
import {
ChatCompletionContentPart,
ChatCompletionContentPartText,
type ChatCompletionTool,
} from "openai/resources"
ChatCompletionMessageParam,
ChatCompletionTool,
} from "./chat"
import { encodeChat, encode } from "gpt-tokenizer"
import { logError, logVerbose } from "./util"
import { parseModelIdentifier } from "./models"
import { logVerbose } from "./util"

export function estimateTokens(model: string, text: string) {
if (!text?.length) return 0
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function assert(
debugData?: any
) {
if (!cond) {
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
if (debugData) console.error(`assertion failed ${msg}`, debugData)
if (debugData) console.error(msg || `assertion failed`, debugData)
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
pelikhan marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line no-debugger
debugger
throw new Error(msg)
Expand Down
13 changes: 13 additions & 0 deletions packages/sample/genaisrc/mjs.genai.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
script({
title: "top-level-mjs",
model: "openai:gpt-3.5-turbo",
files: ["src/rag/markdown.md"],
tests: {
files: ["src/rag/markdown.md"],
keywords: "markdown",
},
})

import { summarize } from "./summarizer.mjs"

summarize(env.files)
4 changes: 4 additions & 0 deletions packages/sample/genaisrc/summarizer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function summarize(files) {
def("FILE", files)
$`Summarize each file. Be concise.`
}
2 changes: 1 addition & 1 deletion packages/tsconfig-base.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"moduleResolution": "node",
"moduleResolution": "bundler",
"target": "ES2022",
"module": "ES2022",
"lib": [
Expand Down
3 changes: 2 additions & 1 deletion packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@
"vscode:update-dts": "npx @vscode/dts dev && mv vscode.*.d.ts src/",
"vscode:prepublish": "yarn run compile",
"compile:icons": "node updatefonts.mjs",
"compile:extension": "esbuild src/extension.ts --sourcemap --bundle --format=cjs --platform=node --target=node20 --outfile=built/extension.js --external:vscode --external:pdfjs-dist --external:llamaindex --external:web-tree-sitter --external:tree-sitter-wasms",
"compile:extension": "esbuild src/extension.ts --sourcemap --bundle --format=cjs --platform=node --target=node20 --outfile=built/extension.js --external:tsx --external:vscode --external:pdfjs-dist --external:llamaindex --external:web-tree-sitter --external:tree-sitter-wasms",
"compile": "yarn compile:icons && yarn compile:extension",
"lint": "eslint src --ext ts",
"run-in-browser": "vscode-test-web --browserType=chromium --extensionDevelopmentPath=. ../sample",
Expand Down Expand Up @@ -391,6 +391,7 @@
"mammoth": "^1.8.0",
"pdfjs-dist": "4.3.136",
"tree-sitter-wasms": "^0.1.11",
"tsx": "^4.15.7",
"web-tree-sitter": "^0.22.6"
},
"optionalDependencies": {
Expand Down
Loading
Loading