Skip to content

Commit 9524d76

Browse files
committed
feat: update XML parsing functions to return promises; refactor imports for dynamic loading
1 parent af02396 commit 9524d76

13 files changed

Lines changed: 30 additions & 26 deletions

File tree

packages/core/src/chat.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import { resolveTokenEncoder } from "./encoders.js";
5454
import { approximateTokens, truncateTextToTokens } from "./tokens.js";
5555
import { computeFileEdits } from "./fileedits.js";
5656
import { HTMLEscape } from "./htmlescape.js";
57-
import { XMLTryParse } from "./xml.js";
5857
import {
5958
computePerplexity,
6059
computeStructuralUncertainty,
@@ -1419,7 +1418,7 @@ export function tracePromptResult(
14191418
if (text) {
14201419
const language = JSON5TryParse(text)
14211420
? "json"
1422-
: XMLTryParse(text)
1421+
: /^</.test(text)
14231422
? "xml"
14241423
: /^(-|\*|#+|```)\s/im.test(text)
14251424
? "markdown"

packages/core/src/data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export async function dataTryParse(
5858
else if (TOML_REGEX.test(filename)) data = TOMLTryParse(content);
5959
else if (JSON5_REGEX.test(filename)) data = JSON5TryParse(content, { repair: true });
6060
else if (YAML_REGEX.test(filename)) data = YAMLTryParse(content);
61-
else if (XML_REGEX.test(filename)) data = XMLTryParse(content, options);
61+
else if (XML_REGEX.test(filename)) data = await XMLTryParse(content, options);
6262
else if (JSONL_REGEX.test(filename)) data = JSONLTryParse(content);
6363
else if (MD_REGEX.test(filename) || MDX_REGEX.test(filename))
6464
data = YAMLTryParse(splitMarkdown(content).frontmatter);

packages/core/src/docx.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { ensureDir } from "./fs.js";
1717
import { measure } from "./performance.js";
1818
import { dotGenaiscriptPath } from "./workdir.js";
1919
import type { DocxParseOptions, WorkspaceFile } from "./types.js";
20-
import { extractRawText, convertToHtml } from "mammoth";
2120

2221
async function computeHashFolder(
2322
filename: string,
@@ -74,6 +73,8 @@ export async function DOCXTryParse(
7473
try {
7574
const input = content ? { buffer: Buffer.from(content) } : { path: host.resolvePath(filename) };
7675

76+
const { extractRawText, convertToHtml } = await import("mammoth");
77+
7778
let text: string;
7879
if (format === "html" || format === "markdown") {
7980
const results = await convertToHtml(input);

packages/core/src/ffmpeg.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { writeFile, readFile } from "node:fs/promises";
1717
import { errorMessage, serializeError } from "./error.js";
1818
import { fromBase64 } from "./base64.js";
1919
import { fileTypeFromBuffer } from "./filetype.js";
20-
import { appendFile, readdir, stat } from "node:fs/promises";
20+
import { appendFile, readdir } from "node:fs/promises";
2121
import prettyBytes from "pretty-bytes";
2222
import { filenameOrFileToFilename } from "./unwrappers.js";
2323
import { roundWithPrecision } from "./precision.js";
@@ -37,7 +37,6 @@ import type {
3737
VideoProbeResult,
3838
WorkspaceFile,
3939
} from "./types.js";
40-
import cmd from "fluent-ffmpeg";
4140

4241
const ffmpegLimit = pLimit(1);
4342
const WILD_CARD = "%06d";
@@ -53,6 +52,7 @@ interface FFmpegCommandResult {
5352
}
5453

5554
async function ffmpegCommand(options?: { timeout?: number }) {
55+
const cmd = (await import("fluent-ffmpeg")).default;
5656
return cmd(options);
5757
}
5858

@@ -233,7 +233,7 @@ export class FFmepgClient implements Ffmpeg {
233233
}
234234
const res = await this.run(
235235
filename,
236-
async (cmd, fopts) => {
236+
async (cmd) => {
237237
cmd.noVideo();
238238
if (transcription) {
239239
// https://community.openai.com/t/whisper-api-increase-file-limit-25-mb/566754

packages/core/src/math.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License.
33

44
import { TraceOptions } from "./trace.js";
5-
import { evaluate } from "mathjs";
65

76
/**
87
* Asynchronously evaluates a mathematical expression.
@@ -28,6 +27,8 @@ export async function MathTryEvaluate(
2827
// Return defaultValue if expression is empty
2928
if (!expr) return defaultValue;
3029

30+
const { evaluate } = await import("mathjs");
31+
3132
// Evaluate the expression and return the result
3233
const res = evaluate(expr, scope);
3334
return res;

packages/core/src/parsers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ export function createParsers(): Parsers {
9494
YAMLTryParse(filenameOrFileToContent(text), options?.defaultValue),
9595
options,
9696
),
97-
XML: (text, options) => {
97+
XML: async (text, options) => {
9898
const { defaultValue, ...rest } = options || {};
9999
return tryValidateJSONWithSchema(
100-
XMLTryParse(filenameOrFileToContent(text), defaultValue, rest),
100+
await XMLTryParse(filenameOrFileToContent(text), defaultValue, rest),
101101
options,
102102
);
103103
},

packages/core/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3265,7 +3265,7 @@ export interface XMLObject {
32653265
* Parses an XML payload to an object
32663266
* @param text
32673267
*/
3268-
parse(text: string | WorkspaceFile, options?: XMLParseOptions): any;
3268+
parse(text: string | WorkspaceFile, options?: XMLParseOptions): Promise<any>;
32693269
}
32703270

32713271
export interface JSONSchemaUtilities {

packages/core/src/workspace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export function createWorkspaceFileSystem(): Omit<WorkspaceFileSystem, "grep" |
135135
},
136136
readXML: async (f: string | Awaitable<WorkspaceFile>, options?: XMLParseOptions) => {
137137
const file = await fs.readText(f);
138-
const res = XMLTryParse(file.content, options);
138+
const res = await XMLTryParse(file.content, options);
139139
return tryValidateJSONWithSchema(res, options);
140140
},
141141
readCSV: async <T extends object>(

packages/core/src/xlsx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// Import the logInfo function for logging purposes
55
import { logInfo } from "./util.js";
66
import type { ParseXLSXOptions, WorkbookSheet } from "./types.js";
7-
import { read, utils } from "xlsx";
87

98
/**
109
* Parses XLSX data into an array of workbook sheets.
@@ -20,6 +19,7 @@ export async function XLSXParse(
2019
// Destructure options to separate sheet-specific option
2120
const { sheet, ...rest } = options || {};
2221
// Dynamically import 'xlsx' library's read and utils modules
22+
const { read, utils } = await import("xlsx");
2323
// Read the workbook from the data with 'array' type
2424
const workbook = read(data, { type: "array" });
2525
// Filter and map the sheet names to WorkbookSheet objects

packages/core/src/xml.ts

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

4-
import { XMLParser } from "fast-xml-parser";
54
import { unfence } from "./unwrappers.js";
65
import { filenameOrFileToContent } from "./unwrappers.js";
76
import type { WorkspaceFile, XMLParseOptions } from "./types.js";
@@ -16,14 +15,14 @@ const dbg = genaiscriptDebug("xml");
1615
* @param options - Optional configuration for the XML parser
1716
* @returns The parsed XML object or defaultValue if an error occurs
1817
*/
19-
export function XMLTryParse(
18+
export async function XMLTryParse(
2019
text: string | WorkspaceFile,
2120
defaultValue?: unknown,
2221
options?: XMLParseOptions,
2322
) {
2423
try {
2524
// Try parsing the text and return the result or defaultValue
26-
return XMLParse(text, options) ?? defaultValue;
25+
return (await XMLParse(text, options)) ?? defaultValue;
2726
} catch (e) {
2827
// Return the default value if parsing fails
2928
dbg(`error: %s`, e?.message);
@@ -38,11 +37,13 @@ export function XMLTryParse(
3837
* @param options - Configuration options for the XML parser. These options are merged with the default parser settings.
3938
* @returns The parsed XML object.
4039
*/
41-
export function XMLParse(text: string | WorkspaceFile, options?: XMLParseOptions) {
40+
export async function XMLParse(text: string | WorkspaceFile, options?: XMLParseOptions) {
4241
text = filenameOrFileToContent(text);
4342
// Remove specific markers from the XML string for cleaner processing
4443
const cleaned = unfence(text, "xml");
4544

45+
const { XMLParser } = await import("fast-xml-parser");
46+
4647
// Create a new XMLParser instance with the specified options
4748
const parser = new XMLParser({
4849
ignoreAttributes: false, // Do not ignore XML attributes

0 commit comments

Comments
 (0)