Skip to content
This repository was archived by the owner on May 29, 2026. It is now read-only.

Commit 53257ba

Browse files
committed
refactor: integrate resolveRuntime function and update parser usage across modules
1 parent 3bb508a commit 53257ba

6 files changed

Lines changed: 26 additions & 12 deletions

File tree

packages/runtime/src/cast.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
PromptGeneratorOptions,
88
StringLike,
99
} from "@genaiscript/core";
10-
import { resolveChatGenerationContext } from "./runtime.js";
10+
import { resolveChatGenerationContext, resolveRuntime } from "./runtime.js";
1111

1212
/**
1313
* Converts unstructured text or data into structured JSON format.
@@ -28,6 +28,7 @@ export async function cast(
2828
},
2929
): Promise<{ data?: unknown; error?: string; text: string }> {
3030
const ctx = resolveChatGenerationContext(options);
31+
const { parsers } = resolveRuntime();
3132
const { multiple, instructions, label = `cast text to schema`, ...rest } = options || {};
3233
const responseSchema = multiple
3334
? ({
@@ -53,6 +54,6 @@ export async function cast(
5354
label,
5455
},
5556
);
56-
const text = globalPromptContext.parsers.unfence(res.text, "json");
57+
const text = parsers.unfence(res.text, "json");
5758
return res.json ? { text, data: res.json } : { text, error: res.error?.message };
5859
}

packages/runtime/src/filetree.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
WorkspaceFile,
1616
WorkspaceGrepOptions,
1717
} from "@genaiscript/core";
18-
import { resolveChatGenerationContext } from "./runtime.js";
18+
import { resolveRuntime } from "./runtime.js";
1919

2020
/**
2121
* Creates a tree representation of files in the workspace.
@@ -39,12 +39,13 @@ export async function fileTree(
3939
preview?: (file: WorkspaceFile, stats: FileStats) => Awaitable<unknown>;
4040
},
4141
): Promise<string> {
42+
const { workspace, parsers } = resolveRuntime();
4243
const { frontmatter, preview, query, size, ignore, ...rest } = options || {};
4344
const readText = !!(frontmatter || preview);
4445
// TODO
4546
const files = query
46-
? (await globalPromptContext.workspace.grep(query, glob, { ...rest, readText })).files
47-
: await globalPromptContext.workspace.findFiles(glob, {
47+
? (await workspace.grep(query, glob, { ...rest, readText })).files
48+
: await workspace.findFiles(glob, {
4849
ignore,
4950
readText,
5051
});
@@ -68,10 +69,10 @@ export async function fileTree(
6869
const part = parts[index];
6970
let node = currentLevel.find((n) => n.filename === part);
7071
if (!node) {
71-
const stats = await globalPromptContext.workspace.stat(filename);
72+
const stats = await workspace.stat(filename);
7273
const metadata: unknown[] = [];
7374
if (frontmatter && /\.mdx?$/i.test(filename)) {
74-
const fm = globalPromptContext.parsers.frontmatter(file) || {};
75+
const fm = parsers.frontmatter(file) || {};
7576
if (fm)
7677
metadata.push(
7778
...frontmatter

packages/runtime/src/globals.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* A set of parsers for well-known file formats
3+
*/
4+
declare let parsers: Parsers;

packages/runtime/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
export * from "./globals.js";
45
export * from "./version.js";
56
export * from "./docker.js";
67
export * from "./input.js";

packages/runtime/src/markdownifypdf.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {
1414
PromptGeneratorOptions,
1515
WorkspaceFile,
1616
} from "@genaiscript/core";
17-
import { resolveChatGenerationContext } from "./runtime.js";
17+
import { resolveChatGenerationContext, resolveRuntime } from "./runtime.js";
1818

1919
/**
2020
* Converts a PDF file to markdown format with intelligent formatting preservation.
@@ -32,7 +32,8 @@ export async function markdownifyPdf(
3232
ctx?: ChatGenerationContext;
3333
},
3434
) {
35-
const ctx = resolveChatGenerationContext(options);
35+
const generator = resolveChatGenerationContext(options);
36+
const { parsers } = resolveRuntime();
3637
const {
3738
label = `markdownify PDF`,
3839
model = "ocr",
@@ -42,7 +43,7 @@ export async function markdownifyPdf(
4243
} = options || {};
4344

4445
// extract text and render pages as images
45-
const { pages, images = [] } = await globalPromptContext.parsers.PDF(file, {
46+
const { pages, images = [] } = await parsers.PDF(file, {
4647
...rest,
4748
renderAsImage: true,
4849
});
@@ -51,7 +52,7 @@ export async function markdownifyPdf(
5152
const page = pages[i];
5253
const image = images[i];
5354
// mix of text and vision
54-
const res = await ctx.runPrompt(
55+
const res = await generator.runPrompt(
5556
async (_) => {
5657
const previousPages = markdowns.slice(-2).join("\n\n");
5758
if (previousPages.length) _.def("PREVIOUS_PAGES", previousPages);
@@ -76,7 +77,7 @@ export async function markdownifyPdf(
7677
- Do not repeat the <PREVIOUS_PAGES> content.
7778
- Do not include any additional explanations or comments in the markdown formatted extracted text.
7879
`;
79-
if (image) globalPromptContext.$`- For images, generate a short alt-text description.`;
80+
if (image) _.$`- For images, generate a short alt-text description.`;
8081
if (typeof instructions === "string") _.$`${instructions}`;
8182
else if (typeof instructions === "function") await instructions(_);
8283
},

packages/runtime/src/runtime.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ const dbg = genaiscriptDebug("runtime");
3131

3232
let _nodeHost: NodeHost | undefined;
3333

34+
export function resolveRuntime(): RuntimePromptContext {
35+
if (!_nodeHost) throw new Error("Runtime not configured. Call `config` first.");
36+
const globalPromptContext: RuntimePromptContext = globalThis as unknown as RuntimePromptContext;
37+
return globalPromptContext;
38+
}
39+
3440
export function resolveChatGenerationContext(
3541
options?: ChatGenerationContextOptions,
3642
): ChatGenerationContext {

0 commit comments

Comments
 (0)