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

Commit fd944ae

Browse files
committed
refactor: simplify parser creation and remove unused parameters in prompt context
1 parent 102e738 commit fd944ae

8 files changed

Lines changed: 14 additions & 65 deletions

File tree

packages/core/src/globals.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { resolveGlobal } from "./global.js";
2626
import { MarkdownStringify } from "./markdown.js";
2727
import { diffCreatePatch, diffFindChunk, tryDiffParse } from "./diff.js";
2828
import type { PromptContext } from "./types.js";
29+
import { createParsers } from "./parsers.js";
2930

3031
let _globalsInstalled = false;
3132
/**
@@ -55,6 +56,8 @@ export function installGlobals() {
5556
dbg("install");
5657
const glb = resolveGlobal(); // Get the global context
5758

59+
glb.parsers = createParsers();
60+
5861
// Freeze YAML utilities to prevent modification
5962
glb.YAML = createYAML();
6063

packages/core/src/parsers.ts

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import { CSVTryParse } from "./csv.js";
55
import { filenameOrFileToContent, filenameOrFileToFilename, unfence } from "./unwrappers.js";
66
import { JSON5TryParse, JSONLLMTryParse } from "./json5.js";
7-
import { estimateTokens } from "./tokens.js";
87
import { TOMLTryParse } from "./toml.js";
9-
import { TraceOptions } from "./trace.js";
108
import { YAMLTryParse } from "./yaml.js";
119
import { DOCXTryParse } from "./docx.js";
1210
import { frontmatterTryParse } from "./frontmatter.js";
@@ -24,15 +22,13 @@ import { host } from "./host.js";
2422
import { unzip } from "./zip.js";
2523
import { JSONLTryParse } from "./jsonl.js";
2624
import { resolveFileContent } from "./file.js";
27-
import { resolveTokenEncoder } from "./encoders.js";
2825
import { mustacheRender } from "./mustache.js";
2926
import { jinjaRender } from "./jinja.js";
3027
import { llmifyDiff } from "./llmdiff.js";
3128
import { tidyData } from "./tidy.js";
3229
import { hash } from "./crypto.js";
3330
import { GROQEvaluate } from "./groq.js";
3431
import { unthink } from "./think.js";
35-
import { CancellationOptions } from "./cancellation.js";
3632
import { dedent } from "./indent.js";
3733
import { vttSrtParse } from "./transcription.js";
3834
import { encodeIDs } from "./cleaners.js";
@@ -84,14 +80,7 @@ import type { Parsers, WorkspaceFile } from "./types.js";
8480
* - dedent: Dedents indented text content.
8581
* - encodeIDs: Encodes identifiers for use in various operations.
8682
*/
87-
export async function createParsers(
88-
options: {
89-
model: string;
90-
} & TraceOptions &
91-
CancellationOptions,
92-
): Promise<Parsers> {
93-
const { trace, model, cancellationToken } = options;
94-
const { encode: encoder } = await resolveTokenEncoder(model);
83+
export function createParsers(): Parsers {
9584
return Object.freeze<Parsers>({
9685
JSON5: (text, options) =>
9786
tryValidateJSONWithSchema(
@@ -131,31 +120,15 @@ export async function createParsers(
131120
),
132121
transcription: (text) => vttSrtParse(filenameOrFileToContent(text)),
133122
unzip: async (file, options) => await unzip(await host.readFile(file.filename), options),
134-
tokens: (text) => estimateTokens(filenameOrFileToContent(text), encoder),
135123
fences: (text) => extractFenced(filenameOrFileToContent(text)),
136124
annotations: (text) => parseAnnotations(filenameOrFileToContent(text)),
137-
HTMLToText: (text, options) =>
138-
HTMLToText(filenameOrFileToContent(text), {
139-
...(options || {}),
140-
trace,
141-
cancellationToken,
142-
}),
143-
HTMLToMarkdown: (text, options) =>
144-
HTMLToMarkdown(filenameOrFileToContent(text), {
145-
...(options || {}),
146-
trace,
147-
cancellationToken,
148-
}),
125+
HTMLToText: (text, options) => HTMLToText(filenameOrFileToContent(text), options),
126+
HTMLToMarkdown: (text, options) => HTMLToMarkdown(filenameOrFileToContent(text), options),
149127
DOCX: async (file, options) => await DOCXTryParse(file, options),
150128
PDF: async (file, options) => {
151129
if (!file) return { file: undefined, pages: [], data: [] };
152-
const opts = {
153-
...(options || {}),
154-
trace,
155-
cancellationToken,
156-
};
157130
const filename = typeof file === "string" ? file : file.filename;
158-
const { pages, content } = (await parsePdf(filename, opts)) || {};
131+
const { pages, content } = (await parsePdf(filename, options)) || {};
159132
return {
160133
file: <WorkspaceFile>{
161134
filename,
@@ -171,8 +144,8 @@ export async function createParsers(
171144
const res = await mermaidParse(f);
172145
return res;
173146
},
174-
math: async (expression, scope) => await MathTryEvaluate(expression, { scope, trace }),
175-
validateJSON: (schema, content) => validateJSONWithSchema(content, schema, { trace }),
147+
math: async (expression, scope) => await MathTryEvaluate(expression, { scope }),
148+
validateJSON: (schema, content) => validateJSONWithSchema(content, schema),
176149
mustache: (file, args) => {
177150
const f = filenameOrFileToContent(file);
178151
return mustacheRender(f, args);
@@ -190,7 +163,7 @@ export async function createParsers(
190163
dedent: dedent,
191164
encodeIDs: encodeIDs,
192165
prompty: async (file) => {
193-
await resolveFileContent(file, { trace });
166+
await resolveFileContent(file);
194167
return promptyParse(file.filename, file.content);
195168
},
196169
});

packages/core/src/promptcontext.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ export async function createPromptContext(
7272
};
7373
assert(!!output, "missing output");
7474
// Create parsers for the given trace and model
75-
const parsers = await createParsers({ trace, cancellationToken, model });
7675
const path = runtimeHost.path;
7776
const runDir = ev.runDir;
7877
assert(!!runDir, "missing run directory");
@@ -382,7 +381,6 @@ export async function createPromptContext(
382381
env: undefined, // set later
383382
path,
384383
workspace,
385-
parsers,
386384
retrieval,
387385
host: promptHost,
388386
};

packages/core/src/schema.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,10 @@ export function tryValidateJSONWithSchema<T = unknown>(
246246
* @returns Validation result indicating success status and error details if validation fails.
247247
*/
248248
export function validateJSONWithSchema(
249+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
249250
object: any,
250251
schema: JSONSchema,
251-
options?: { trace: MarkdownTrace },
252+
options?: TraceOptions,
252253
): FileEditValidation {
253254
const { trace } = options || {};
254255
if (!schema)

packages/core/src/types.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2977,12 +2977,6 @@ export interface Parsers {
29772977
*/
29782978
unzip(file: WorkspaceFile, options?: ParseZipOptions): Promise<WorkspaceFile[]>;
29792979

2980-
/**
2981-
* Estimates the number of tokens in the content.
2982-
* @param content content to tokenize
2983-
*/
2984-
tokens(content: string | WorkspaceFile): number;
2985-
29862980
/**
29872981
* Parses fenced code sections in a markdown text
29882982
*/
@@ -6346,7 +6340,6 @@ export interface PromptContext extends ChatGenerationContext {
63466340
script(options: PromptArgs): void;
63476341
system(options: PromptSystemArgs): void;
63486342
path: Path;
6349-
parsers: Parsers;
63506343
retrieval: Retrieval;
63516344
workspace: WorkspaceFileSystem;
63526345
host: PromptHost;
@@ -6355,7 +6348,6 @@ export interface PromptContext extends ChatGenerationContext {
63556348
export type RuntimePromptContext = Pick<
63566349
PromptContext,
63576350
| "host"
6358-
| "parsers"
63596351
| "env"
63606352
| "workspace"
63616353
| "retrieval"

packages/core/src/types/prompt_template.d.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2967,12 +2967,6 @@ interface Parsers {
29672967
*/
29682968
unzip(file: WorkspaceFile, options?: ParseZipOptions): Promise<WorkspaceFile[]>;
29692969

2970-
/**
2971-
* Estimates the number of tokens in the content.
2972-
* @param content content to tokenize
2973-
*/
2974-
tokens(content: string | WorkspaceFile): number;
2975-
29762970
/**
29772971
* Parses fenced code sections in a markdown text
29782972
*/
@@ -6330,7 +6324,6 @@ interface PromptContext extends ChatGenerationContext {
63306324
script(options: PromptArgs): void;
63316325
system(options: PromptSystemArgs): void;
63326326
path: Path;
6333-
parsers: Parsers;
63346327
retrieval: Retrieval;
63356328
workspace: WorkspaceFileSystem;
63366329
host: PromptHost;

packages/core/test/parsers.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,17 @@
33

44
import { describe, test, assert, beforeEach } from "vitest";
55
import { createParsers } from "../src/parsers.js";
6-
import { MarkdownTrace } from "../src/trace.js";
76
import { XLSXParse } from "../src/xlsx.js";
87
import { readFile } from "fs/promises";
98
import { resolve } from "path";
109
import { TestHost } from "../src/testhost.js";
1110
import { writeFile } from "fs/promises";
1211

1312
describe("parsers", async () => {
14-
let trace: MarkdownTrace;
15-
let model: string;
16-
let parsers: Awaited<ReturnType<typeof createParsers>>;
13+
let parsers: Parsers;
1714

1815
beforeEach(async () => {
19-
trace = new MarkdownTrace({});
20-
model = "test model";
21-
parsers = await createParsers({ trace, model });
16+
parsers = createParsers();
2217
TestHost.install();
2318
});
2419

packages/runtime/src/runtime.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ 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-
4034
export function resolveChatGenerationContext(
4135
options?: ChatGenerationContextOptions,
4236
): ChatGenerationContext {

0 commit comments

Comments
 (0)