From 52ab19d8252c82140502e7835f869d806fcd83a5 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Sun, 22 Dec 2024 04:08:46 +0000 Subject: [PATCH] handle errors in agent --- .../content/docs/reference/scripts/system.mdx | 6 ++--- packages/cli/src/nodehost.ts | 3 ++- .../src/genaisrc/system.annotations.genai.mjs | 3 +-- .../src/genaisrc/system.diagrams.genai.mjs | 3 ++- packages/core/src/llms.ts | 16 +++++++++++-- packages/core/src/promptdom.ts | 2 +- packages/core/src/runpromptcontext.ts | 11 +++++++-- packages/core/src/util.ts | 24 ++++++++++--------- 8 files changed, 45 insertions(+), 23 deletions(-) diff --git a/docs/src/content/docs/reference/scripts/system.mdx b/docs/src/content/docs/reference/scripts/system.mdx index 8590444c21..e63143ef25 100644 --- a/docs/src/content/docs/reference/scripts/system.mdx +++ b/docs/src/content/docs/reference/scripts/system.mdx @@ -424,8 +424,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=,line=,endLine=,code=:: @@ -552,7 +551,8 @@ 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.` ````` diff --git a/packages/cli/src/nodehost.ts b/packages/cli/src/nodehost.ts index 7f11cb05e4..655f553891 100644 --- a/packages/cli/src/nodehost.ts +++ b/packages/cli/src/nodehost.ts @@ -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 } diff --git a/packages/core/src/genaisrc/system.annotations.genai.mjs b/packages/core/src/genaisrc/system.annotations.genai.mjs index dce344fbc3..a734e09dec 100644 --- a/packages/core/src/genaisrc/system.annotations.genai.mjs +++ b/packages/core/src/genaisrc/system.annotations.genai.mjs @@ -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=,line=,endLine=,code=:: diff --git a/packages/core/src/genaisrc/system.diagrams.genai.mjs b/packages/core/src/genaisrc/system.diagrams.genai.mjs index 790f0f2ea5..ee6a5421a3 100644 --- a/packages/core/src/genaisrc/system.diagrams.genai.mjs +++ b/packages/core/src/genaisrc/system.diagrams.genai.mjs @@ -2,4 +2,5 @@ system({ title: "Generate diagrams" }) -$`Use mermaid syntax if you need to generate state diagrams, class inheritance diagrams, relationships.` \ No newline at end of file +$`## Diagrams Format +Use mermaid syntax if you need to generate state diagrams, class inheritance diagrams, relationships.` \ No newline at end of file diff --git a/packages/core/src/llms.ts b/packages/core/src/llms.ts index 62c6d8a333..d3c9743002 100644 --- a/packages/core/src/llms.ts +++ b/packages/core/src/llms.ts @@ -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" @@ -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) @@ -30,6 +42,6 @@ export function defaultModelConfigurations(): ModelConfigurations { model: candidates[0], candidates, source: "default", - }) + } satisfies ModelConfiguration) } } diff --git a/packages/core/src/promptdom.ts b/packages/core/src/promptdom.ts index 71d9f5f63e..9d33568c99 100644 --- a/packages/core/src/promptdom.ts +++ b/packages/core/src/promptdom.ts @@ -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}\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 { diff --git a/packages/core/src/runpromptcontext.ts b/packages/core/src/runpromptcontext.ts index aed17fd0f0..1a934de013 100644 --- a/packages/core/src/runpromptcontext.ts +++ b/packages/core/src/runpromptcontext.ts @@ -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" @@ -503,7 +509,8 @@ export function createChatGenerationContext( ...rest, } ) - return res + if (res.error) throw res.error + return deleteEmptyValues(res) } ) } diff --git a/packages/core/src/util.ts b/packages/core/src/util.ts index a58b110737..f8e2122441 100644 --- a/packages/core/src/util.ts +++ b/packages/core/src/util.ts @@ -62,21 +62,23 @@ export function parseBoolean(s: string) { } export function deleteUndefinedValues>(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>(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 }