Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON5 helper #964

Merged
merged 3 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/src/content/docs/reference/scripts/system.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,9 @@
description:
"GitHub Actions workflows support annotations ([Read more...](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message)).",
lineNumbers: true,
})

Check warning on line 425 in docs/src/content/docs/reference/scripts/system.mdx

View workflow job for this annotation

GitHub Actions / build

Missing header for section "Annotations Format". Consider adding a brief description or introduction to the section.
pelikhan marked this conversation as resolved.
Show resolved Hide resolved

$`## Annotation Format

$`## Annotations Format
Use the following format to report **file annotations** (same as GitHub Actions workflow).

::(notice|warning|error) file=<filename>,line=<start line>,endLine=<end line>,code=<error_id>::<message>
Expand Down Expand Up @@ -552,7 +551,8 @@
title: "Generate diagrams"
})

$`Use mermaid syntax if you need to generate state diagrams, class inheritance diagrams, relationships.`
$`## Diagrams Format
Use mermaid syntax if you need to generate state diagrams, class inheritance diagrams, relationships.`
`````


Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/nodehost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ export class NodeHost implements RuntimeHost {
if (typeof value === "string") value = { model: value, source }
const aliases = this._modelAliases[source]
const c = aliases[id] || (aliases[id] = { source })
if (value.model !== undefined) (c as any).model = value.model
if (value.model !== undefined && value.model !== id)
(c as any).model = value.model
if (!isNaN(value.temperature))
(c as any).temperature = value.temperature
}
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/genaisrc/system.annotations.genai.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ system({
lineNumbers: true,
})

$`## Annotation Format

$`## Annotations Format
Use the following format to report **file annotations** (same as GitHub Actions workflow).

::(notice|warning|error) file=<filename>,line=<start line>,endLine=<end line>,code=<error_id>::<message>
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/genaisrc/system.diagrams.genai.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ system({
title: "Generate diagrams"
})

$`Use mermaid syntax if you need to generate state diagrams, class inheritance diagrams, relationships.`
$`## Diagrams Format
Use mermaid syntax if you need to generate state diagrams, class inheritance diagrams, relationships.`
6 changes: 6 additions & 0 deletions packages/core/src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -86,6 +87,11 @@ export function installGlobals() {
stringify: JSONLStringify, // Convert objects to JSONL string
})

glb.JSON5 = Object.freeze<JSON5>({
parse: JSON5TryParse,
stringify: JSON5Stringify
})

// Freeze AICI utilities with a generation function
glb.AICI = Object.freeze<AICI>({
gen: (options: AICIGenOptions) => {
Expand Down
16 changes: 14 additions & 2 deletions packages/core/src/llms.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Model } from "@anthropic-ai/sdk/resources/index.mjs"
import { LARGE_MODEL_ID, SMALL_MODEL_ID, VISION_MODEL_ID } from "./constants"
import { ModelConfiguration, ModelConfigurations } from "./host"
import LLMS from "./llms.json"
Expand All @@ -14,8 +15,19 @@ export function defaultModelConfigurations(): ModelConfigurations {
]
const res = {
...(Object.fromEntries(
aliases.map((alias) => [alias, readModelAlias(alias)])
aliases.map<[string, ModelConfiguration]>((alias) => [
alias,
readModelAlias(alias),
])
) as ModelConfigurations),
...Object.fromEntries(
Object.entries(LLMS.aliases).map<[string, ModelConfiguration]>(
([id, model]) => [
id,
{ model, source: "default" } satisfies ModelConfiguration,
]
)
),
}
return structuredClone(res)

Expand All @@ -30,6 +42,6 @@ export function defaultModelConfigurations(): ModelConfigurations {
model: candidates[0],
candidates,
source: "default",
})
} satisfies ModelConfiguration)
}
}
2 changes: 1 addition & 1 deletion packages/core/src/promptdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function renderDefNode(def: PromptDefNode): string {

let res: string
if (name && fenceFormat === "xml") {
res = `\n<${name}${dtype ? ` lang="${dtype}"` : ""}${filename ? ` file="${filename}"` : ""}${schema ? ` schema=${schema}` : ""}${diffFormat}>\n${body}</${name}>\n`
res = `\n<${name}${dtype ? ` lang="${dtype}"` : ""}${filename ? ` file="${filename}"` : ""}${schema ? ` schema=${schema}` : ""}${diffFormat}>\n${body}<${name}>\n`
} else if (fenceFormat === "none") {
res = `\n${name ? name + ":\n" : ""}${body}\n`
} else {
Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/runpromptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ import {
} from "./parameters"
import { consoleLogFormat, stdout } from "./logging"
import { isGlobMatch } from "./glob"
import { arrayify, logError, logVerbose, logWarn } from "./util"
import {
arrayify,
deleteEmptyValues,
logError,
logVerbose,
logWarn,
} from "./util"
import { renderShellOutput } from "./chatrender"
import { jinjaRender } from "./jinja"
import { mustacheRender } from "./mustache"
Expand Down Expand Up @@ -503,7 +509,8 @@ export function createChatGenerationContext(
...rest,
}
)
return res
if (res.error) throw res.error
return deleteEmptyValues(res)
}
)
}
Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2604,7 +2618,7 @@ interface McpServerConfig {

type McpServersConfig = Record<string, Omit<McpServerConfig, "id" | "options">>

type ZodTypeLike = { _def: any, safeParse: any, refine: any }
type ZodTypeLike = { _def: any; safeParse: any; refine: any }

interface ChatGenerationContext extends ChatTurnGenerationContext {
defSchema(
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/types/prompt_type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ declare var MD: MD
*/
declare var JSONL: JSONL

/**
* JSON5 parsing
*/
declare var JSON5: JSON5

/**
* AICI operations
*/
Expand Down
24 changes: 13 additions & 11 deletions packages/core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,23 @@ export function parseBoolean(s: string) {
}

export function deleteUndefinedValues<T extends Record<string, any>>(o: T): T {
for (const k in o) if (o[k] === undefined) delete o[k]
if (typeof o === "object")
for (const k in o) if (o[k] === undefined) delete o[k]
return o
}

export function deleteEmptyValues<T extends Record<string, any>>(o: T): T {
for (const k in o) {
const v = o[k]
if (
v === undefined ||
v === null ||
v === "" ||
(Array.isArray(v) && !v.length)
)
delete o[k]
}
if (typeof o === "object")
for (const k in o) {
const v = o[k]
if (
v === undefined ||
v === null ||
v === "" ||
(Array.isArray(v) && !v.length)
)
delete o[k]
}
return o
}

Expand Down
17 changes: 17 additions & 0 deletions packages/sample/genaisrc/globals.genai.mjs
Original file line number Diff line number Diff line change
@@ -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"
)
}
Loading