Skip to content

Commit

Permalink
move stable globals out of execution loop
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Aug 23, 2024
1 parent 591a16d commit f83faed
Show file tree
Hide file tree
Showing 18 changed files with 64 additions and 157 deletions.
7 changes: 0 additions & 7 deletions docs/genaisrc/genaiscript.d.ts

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

7 changes: 0 additions & 7 deletions genaisrc/genaiscript.d.ts

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

2 changes: 2 additions & 0 deletions packages/cli/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { installGlobals } from "../../core/src/globals"

Check failure on line 1 in packages/cli/src/main.ts

View workflow job for this annotation

GitHub Actions / build

You have added a new import statement for `installGlobals` from `../../core/src/globals`, but it seems you have not added the corresponding import statement in the file `../../core/src/globals`. Please ensure that `installGlobals` is exported from the `globals` module. 📦
import { cli } from "./cli"

installGlobals()
cli()
7 changes: 0 additions & 7 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.

60 changes: 60 additions & 0 deletions packages/core/src/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { glob } from "glob"
import { YAMLParse, YAMLStringify } from "./yaml"
import { CSVParse, CSVToMarkdown } from "./csv"
import { INIParse, INIStringify } from "./ini"
import { XMLParse } from "./xml"
import {
frontmatterTryParse,
splitMarkdown,
updateFrontmatter,
} from "./frontmatter"
import { JSONLStringify, JSONLTryParse } from "./jsonl"

export function resolveGlobal(): any {
if (typeof window !== "undefined")
return window // Browser environment
else if (typeof self !== "undefined") return self
else if (typeof global !== "undefined") return global // Node.js environment
throw new Error("Could not find global")
}

export function installGlobals() {
const glb = resolveGlobal()

glb.YAML = Object.freeze<YAML>({
stringify: YAMLStringify,
parse: YAMLParse,
})
glb.CSV = Object.freeze<CSV>({
parse: CSVParse,
markdownify: CSVToMarkdown,
})
glb.INI = Object.freeze<INI>({
parse: INIParse,
stringify: INIStringify,
})
glb.XML = Object.freeze<XML>({
parse: XMLParse,
})
glb.MD = Object.freeze<MD>({
frontmatter: (text, format) =>
frontmatterTryParse(text, { format })?.value ?? {},
content: (text) => splitMarkdown(text)?.content,
updateFrontmatter: (text, frontmatter, format): string =>
updateFrontmatter(text, frontmatter, { format }),
})
glb.JSONL = Object.freeze<JSONL>({
parse: JSONLTryParse,
stringify: JSONLStringify,
})
glb.AICI = Object.freeze<AICI>({
gen: (options: AICIGenOptions) => {
// validate options
return {
type: "aici",
name: "gen",
options,
}
},
})
}

Check failure on line 60 in packages/core/src/globals.ts

View workflow job for this annotation

GitHub Actions / build

The function `installGlobals` is adding a lot of properties to the global object. This can lead to potential naming conflicts and makes the code harder to understand and maintain. Consider encapsulating these properties in a single object and adding that to the global scope instead. 🌍
18 changes: 2 additions & 16 deletions packages/core/src/importprompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@ import { assert } from "console"
import { host } from "./host"
import { logError } from "./util"
import { TraceOptions } from "./trace"
import { fileURLToPath, pathToFileURL } from "url"

function resolveGlobal(): any {
if (typeof window !== "undefined")
return window // Browser environment
else if (typeof self !== "undefined") return self
else if (typeof global !== "undefined") return global // Node.js environment
throw new Error("Could not find global")
}
import { pathToFileURL } from "url"
import { resolveGlobal } from "./globals"

Check failure on line 6 in packages/core/src/importprompt.ts

View workflow job for this annotation

GitHub Actions / build

The `resolveGlobal` function is imported from `./globals` but it is not used anywhere in the `importprompt.ts` file. Please remove unused imports to keep the code clean and efficient. 🧹

export async function importPrompt(
ctx0: PromptContext,
Expand All @@ -30,13 +23,6 @@ export async function importPrompt(
"parsers",
"env",
"retrieval",
"YAML",
"INI",
"CSV",
"XML",
"MD",
"JSONL",
"AICI",
"fetchText",
"cancel",
]
Expand Down
43 changes: 0 additions & 43 deletions packages/core/src/promptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,42 +74,6 @@ export async function createPromptContext(
const { generator, ...varsNoGenerator } = vars
const env = { generator, ...structuredClone(varsNoGenerator) }
const parsers = await createParsers({ trace, model })
const YAML = Object.freeze<YAML>({
stringify: YAMLStringify,
parse: YAMLParse,
})
const CSV = Object.freeze<CSV>({
parse: CSVParse,
markdownify: CSVToMarkdown,
})
const INI = Object.freeze<INI>({
parse: INIParse,
stringify: INIStringify,
})
const XML = Object.freeze<XML>({
parse: XMLParse,
})
const MD = Object.freeze<MD>({
frontmatter: (text, format) =>
frontmatterTryParse(text, { format })?.value ?? {},
content: (text) => splitMarkdown(text)?.content,
updateFrontmatter: (text, frontmatter, format): string =>
updateFrontmatter(text, frontmatter, { format }),
})
const JSONL = Object.freeze<JSONL>({
parse: JSONLTryParse,
stringify: JSONLStringify,
})
const AICI = Object.freeze<AICI>({
gen: (options: AICIGenOptions) => {
// validate options
return {
type: "aici",
name: "gen",
options,
}
},
})
const path = runtimeHost.path
const workspace: WorkspaceFileSystem = {
readText: (f) => runtimeHost.workspace.readText(f),
Expand Down Expand Up @@ -254,13 +218,6 @@ export async function createPromptContext(
fs: workspace,
workspace,
parsers,
YAML,
CSV,
INI,
AICI,
XML,
MD,
JSONL,
retrieval,
host: promptHost,
defOutputProcessor,
Expand Down
7 changes: 0 additions & 7 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1708,12 +1708,5 @@ interface PromptContext extends ChatGenerationContext {
*/
fs: WorkspaceFileSystem
workspace: WorkspaceFileSystem
YAML: YAML
XML: XML
JSONL: JSONL
CSV: CSV
INI: INI
AICI: AICI
MD: MD
host: PromptHost
}
7 changes: 0 additions & 7 deletions packages/sample/genaisrc/genaiscript.d.ts

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

7 changes: 0 additions & 7 deletions packages/sample/genaisrc/node/genaiscript.d.ts

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

7 changes: 0 additions & 7 deletions packages/sample/genaisrc/python/genaiscript.d.ts

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

7 changes: 0 additions & 7 deletions packages/sample/genaisrc/style/genaiscript.d.ts

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

7 changes: 0 additions & 7 deletions packages/sample/src/aici/genaiscript.d.ts

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

7 changes: 0 additions & 7 deletions packages/sample/src/errors/genaiscript.d.ts

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

7 changes: 0 additions & 7 deletions packages/sample/src/makecode/genaiscript.d.ts

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

7 changes: 0 additions & 7 deletions packages/sample/src/tla/genaiscript.d.ts

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

7 changes: 0 additions & 7 deletions packages/sample/src/vision/genaiscript.d.ts

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

7 changes: 0 additions & 7 deletions slides/genaisrc/genaiscript.d.ts

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

0 comments on commit f83faed

Please sign in to comment.