From 482f48475ee4a94ff3e80c2cb59d73ba074827bd Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Sun, 22 Dec 2024 03:51:07 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20add=20JSON5=20parsing=20and?= =?UTF-8?q?=20stringifying=20utilities?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/globals.ts | 6 ++++++ packages/core/src/types/prompt_template.d.ts | 16 +++++++++++++++- packages/core/src/types/prompt_type.d.ts | 5 +++++ packages/sample/genaisrc/globals.genai.mjs | 17 +++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 packages/sample/genaisrc/globals.genai.mjs diff --git a/packages/core/src/globals.ts b/packages/core/src/globals.ts index 4b93bbaad..cabab2dc9 100644 --- a/packages/core/src/globals.ts +++ b/packages/core/src/globals.ts @@ -17,6 +17,7 @@ import { GitClient } from "./git" import { estimateTokens, truncateTextToTokens } from "./tokens" import { chunk, resolveTokenEncoder } from "./encoders" import { runtimeHost } from "./host" +import { JSON5Stringify, JSON5TryParse } from "./json5" /** * This file defines global utilities and installs them into the global context. @@ -86,6 +87,11 @@ export function installGlobals() { stringify: JSONLStringify, // Convert objects to JSONL string }) + glb.JSON5 = Object.freeze({ + parse: JSON5TryParse, + stringify: JSON5Stringify + }) + // Freeze AICI utilities with a generation function glb.AICI = Object.freeze({ gen: (options: AICIGenOptions) => { diff --git a/packages/core/src/types/prompt_template.d.ts b/packages/core/src/types/prompt_template.d.ts index c4093fd09..d4f69ded6 100644 --- a/packages/core/src/types/prompt_template.d.ts +++ b/packages/core/src/types/prompt_template.d.ts @@ -2184,6 +2184,20 @@ interface INI { stringify(value: any): string } +interface JSON5 { + /** + * Parses a JSON/YAML/XML string to an object + * @param text + */ + parse(text: string | WorkspaceFile): any + + /** + * Renders an object to a JSON5-LLM friendly string + * @param value + */ + stringify(value: any): string +} + interface CSVStringifyOptions { delimiter?: string header?: boolean @@ -2604,7 +2618,7 @@ interface McpServerConfig { type McpServersConfig = Record> -type ZodTypeLike = { _def: any, safeParse: any, refine: any } +type ZodTypeLike = { _def: any; safeParse: any; refine: any } interface ChatGenerationContext extends ChatTurnGenerationContext { defSchema( diff --git a/packages/core/src/types/prompt_type.d.ts b/packages/core/src/types/prompt_type.d.ts index 56c7042f3..f1e0e5444 100644 --- a/packages/core/src/types/prompt_type.d.ts +++ b/packages/core/src/types/prompt_type.d.ts @@ -198,6 +198,11 @@ declare var MD: MD */ declare var JSONL: JSONL +/** + * JSON5 parsing + */ +declare var JSON5: JSON5 + /** * AICI operations */ diff --git a/packages/sample/genaisrc/globals.genai.mjs b/packages/sample/genaisrc/globals.genai.mjs new file mode 100644 index 000000000..74aa6128d --- /dev/null +++ b/packages/sample/genaisrc/globals.genai.mjs @@ -0,0 +1,17 @@ +script({ + tests: {}, + model: "small", +}) + +const data = { + name: "foo", + items: [1, 2, 3], +} + +const json5 = JSON5.parse(JSON5.stringify(data)) + +if (JSON.stringify(json5) !== JSON.stringify(data)) { + throw new Error( + "JSON5.stringify(JSON5.parse(JSON5.stringify(data))) !== data" + ) +}