Skip to content

Commit

Permalink
Refactor rendering of shell output and update type definitions for de…
Browse files Browse the repository at this point in the history
…f function
  • Loading branch information
pelikhan committed Aug 27, 2024
1 parent f634586 commit bcfa1f4
Show file tree
Hide file tree
Showing 18 changed files with 171 additions and 59 deletions.
12 changes: 9 additions & 3 deletions docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions packages/core/src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ import {
ChatCompletionUserMessageParam,
CreateChatCompletionRequest,
} from "./chattypes"
import { renderMessageContent, renderMessagesToMarkdown } from "./chatrender"
import {
renderMessageContent,
renderMessagesToMarkdown,
renderShellOutput,
} from "./chatrender"
import { promptParametersSchemaToJSONSchema } from "./parameters"
import { fenceMD } from "./markdown"
import { YAMLStringify } from "./yaml"
Expand Down Expand Up @@ -204,14 +208,7 @@ async function runToolCalls(
typeof output === "object" &&
(output as ShellOutput).exitCode !== undefined
) {
const { stdout, stderr, exitCode } = output as ShellOutput
toolContent = `EXIT_CODE: ${exitCode}
STDOUT:
${stdout || ""}
STDERR:
${stderr || ""}`
toolContent = renderShellOutput(output as ShellOutput)
} else if (
typeof output === "object" &&
(output as WorkspaceFile).filename &&
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/chatrender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import { JSON5TryParse } from "./json5"
import { details, fenceMD } from "./markdown"
import { YAMLStringify } from "./yaml"

export function renderShellOutput(output: ShellOutput) {
const { exitCode, stdout, stderr } = output
return [
`EXIT_CODE: ${exitCode}`,
stdout ? `STDOUT:${fenceMD(stdout, "text")}` : undefined,
stderr ? `STDERR:${fenceMD(stderr, "text")}` : undefined,
]
.filter((s) => s)
.join("\n\n")
}

export function renderMessageContent(
msg:
| ChatCompletionAssistantMessageParam
Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 28 additions & 8 deletions packages/core/src/runpromptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { consoleLogFormat } from "./logging"
import { resolveFileDataUri } from "./file"
import { isGlobMatch } from "./glob"
import { logVerbose } from "./util"
import { renderShellOutput } from "./chatrender"

export function createChatTurnGenerationContext(
options: GenerationOptions,
Expand Down Expand Up @@ -69,14 +70,6 @@ export function createChatTurnGenerationContext(
if (body.length === 0 && !doptions.ignoreEmpty)
throw new Error(`def ${name} is empty`)
body.forEach((f) => ctx.def(name, f, defOptions))
} else if (typeof body === "object" && body.filename) {
const { glob, endsWith } = defOptions || {}
const filename = body.filename
if (glob && filename) {
if (!isGlobMatch(filename, glob)) return undefined
}
if (endsWith && !filename.endsWith(endsWith)) return undefined
appendChild(node, createDefNode(name, body, doptions))
} else if (typeof body === "string") {
if (body.trim() === "" && !doptions.ignoreEmpty)
throw new Error(`def ${name} is empty`)
Expand All @@ -88,6 +81,33 @@ export function createChatTurnGenerationContext(
doptions
)
)
} else if (
typeof body === "object" &&
(body as WorkspaceFile).filename
) {
const file = body as WorkspaceFile
const { glob, endsWith } = defOptions || {}
const { filename } = file
if (glob && filename) {
if (!isGlobMatch(filename, glob)) return undefined
}
if (endsWith && !filename.endsWith(endsWith)) return undefined
appendChild(node, createDefNode(name, file, doptions))
} else if (
typeof body === "object" &&
(body as ShellOutput).exitCode !== undefined
) {
appendChild(
node,
createDefNode(
name,
{
filename: "",
content: renderShellOutput(body as ShellOutput),
},
{ ...doptions, lineNumbers: false }
)
)
}

// TODO: support clause
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,11 @@ interface ChatTurnGenerationContext {
writeText(body: Awaitable<string>, options?: WriteTextOptions): void
$(strings: TemplateStringsArray, ...args: any[]): void
fence(body: StringLike, options?: FenceOptions): void
def(name: string, body: StringLike, options?: DefOptions): string
def(
name: string,
body: string | WorkspaceFile | WorkspaceFile[] | ShellOutput,
options?: DefOptions
): string
defData(
name: string,
data: object[] | object,
Expand All @@ -1414,7 +1418,9 @@ interface ChatGenerationContext extends ChatTurnGenerationContext {
options?: DefSchemaOptions
): string
defImages(files: StringLike, options?: DefImagesOptions): void
defTool(tool: ToolCallback | AgenticToolCallback | AgenticToolProviderCallback): void
defTool(
tool: ToolCallback | AgenticToolCallback | AgenticToolProviderCallback
): void
defTool(
name: string,
description: string,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/prompt_type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ declare function fence(body: StringLike, options?: FenceOptions): void
*/
declare function def(
name: string,
body: StringLike,
body: string | WorkspaceFile | WorkspaceFile[] | ShellOutput,
options?: DefOptions
): string

Expand Down
12 changes: 9 additions & 3 deletions packages/sample/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions packages/sample/genaisrc/node/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions packages/sample/genaisrc/python/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions packages/sample/genaisrc/style/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bcfa1f4

Please sign in to comment.