Skip to content

Commit 496da58

Browse files
authored
Fix output.image in copilot chat (#1400)
* fix output.image in copilot chat * fix echo * logging * patch cached images * docs
1 parent e1d232c commit 496da58

10 files changed

Lines changed: 59 additions & 8 deletions

File tree

genaisrc/docs.genai.mts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ import { prettier } from "./src/prettier.mts"
44

55
script({
66
title: "Generate TypeScript function documentation using AST insertion",
7+
description: `
8+
## Docs!
9+
10+
This script generates and updates TypeScript function using an AST/LLM hybrid approach.
11+
It uses ast-grep to look for undocumented and documented functions,
12+
then uses a combination of LLM, and LLM-as-a-judge to generate and validate the documentation.
13+
It also uses prettier to format the code before and after the generation.
14+
15+
By default,
16+
17+
- no edits are applied on disk. It is recommended to
18+
run this script with \`--vars 'applyEdits=true'\` to apply the edits.
19+
- if a diff is available, it will only process the files with changes.
20+
21+
`,
722
accept: ".ts",
823
files: "src/cowsay.ts",
924
parameters: {
@@ -17,7 +32,7 @@ script({
1732
type: "boolean",
1833
default: false,
1934
description:
20-
"If true, the script will prettify the files efore analysis.",
35+
"If true, the script will prettify the files before analysis.",
2136
},
2237
applyEdits: {
2338
type: "boolean",

packages/cli/src/run.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ export async function runScriptInternal(
217217
): Promise<{ exitCode: number; result?: GenerationResult }> {
218218
const runId = options.runId || generateId()
219219
const runDir = options.out || getRunDir(scriptId, runId)
220+
dbg(`run id: `, runId)
221+
dbg(`run dir: `, runDir)
220222
const cancellationToken = options.cancellationToken
221223
const {
222224
trace = new MarkdownTrace({ cancellationToken, dir: runDir }),
@@ -491,6 +493,7 @@ export async function runScriptInternal(
491493
}
492494

493495
result = await runTemplate(prj, script, fragment, {
496+
runId,
494497
inner: false,
495498
infoCb: (args) => {
496499
const { text } = args

packages/core/src/filecache.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,12 @@ export async function fileCacheImage(
7878
dbg(`image: ${res}`)
7979
return res
8080
}
81+
82+
export function patchCachedImages(
83+
text: string,
84+
patcher: (url: string) => string
85+
) {
86+
const IMG_RX =
87+
/\!\[(?<alt>[^\]]*)\]\((?<url>\.genaiscript\/images\/[^)]+)\)/g
88+
return text.replace(IMG_RX, (_, alt, url) => `![${alt}](${patcher(url)})`)
89+
}

packages/core/src/generation.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface GenerationOptions
1818
ContentSafetyOptions,
1919
ScriptRuntimeOptions {
2020
inner: boolean // Indicates if the process is an inner operation
21+
runId?: string
2122
runDir?: string
2223
cancellationToken?: CancellationToken // Token to cancel the operation
2324
infoCb?: (partialResponse: { text: string }) => void // Callback for providing partial responses

packages/core/src/promptrunner.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,15 @@ export async function runTemplate(
136136
assert(options !== undefined)
137137
assert(options.trace !== undefined)
138138
assert(options.outputTrace !== undefined)
139-
const { label, cliInfo, trace, outputTrace, cancellationToken, model } =
140-
options
139+
const {
140+
label,
141+
cliInfo,
142+
trace,
143+
outputTrace,
144+
cancellationToken,
145+
model,
146+
runId,
147+
} = options
141148
const version = CORE_VERSION
142149
assert(model !== undefined)
143150

@@ -206,6 +213,7 @@ export async function runTemplate(
206213
frames: [],
207214
schemas: {},
208215
usage: undefined,
216+
runId,
209217
} satisfies GenerationResult
210218
}
211219

@@ -246,6 +254,7 @@ export async function runTemplate(
246254
frames: [],
247255
schemas: {},
248256
usage: undefined,
257+
runId,
249258
} satisfies GenerationResult)
250259
}
251260

@@ -363,6 +372,7 @@ export async function runTemplate(
363372
perplexity: chatResult.perplexity,
364373
uncertainty: chatResult.uncertainty,
365374
usage: chatResult.usage,
375+
runId,
366376
}
367377

368378
// If there's an error, provide status text

packages/core/src/server/messages.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ export type GenerationStatus = "success" | "error" | "cancelled" | undefined
225225

226226
// Interface for the result of a generation process
227227
export interface GenerationResult extends GenerationOutput {
228+
/**
229+
* Run identifier
230+
*/
231+
runId: string
228232
/**
229233
* The environment variables passed to the prompt
230234
*/
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
$`Write a short poem.`
1+
$`Write a short poem in code.`

packages/vscode/src/chatparticipant.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import { Fragment } from "../../core/src/generation"
1010
import { convertAnnotationsToItems } from "../../core/src/annotations"
1111
import { deleteUndefinedValues } from "../../core/src/cleaners"
1212
import { templatesToQuickPickItems } from "./fragmentcommands"
13+
import { patchCachedImages } from "../../core/src/filecache"
1314

1415
export async function activateChatParticipant(state: ExtensionState) {
15-
const { context } = state
16+
const { context, host } = state
1617
const { subscriptions } = context
1718

1819
const resolveReference = (
@@ -160,7 +161,14 @@ export async function activateChatParticipant(state: ExtensionState) {
160161

161162
const { text = "", status, statusText } = res || {}
162163
if (status !== "success") md("$(error) " + statusText)
163-
if (text) md("\n\n" + convertAnnotationsToItems(text))
164+
if (text) {
165+
let patched = convertAnnotationsToItems(text)
166+
const dir = state.host.projectUri
167+
patched = patchCachedImages(patched, (url) =>
168+
vscode.Uri.joinPath(dir, url).toString()
169+
)
170+
md("\n\n" + patched)
171+
}
164172
// TODO open url
165173
}
166174
)

packages/vscode/src/servermanager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class TerminalServerManager
9191
}
9292

9393
get authority() {
94-
assert(!!this._port)
94+
if (!this._port) return undefined
9595
return `${SERVER_LOCALHOST}:${this._port}`
9696
}
9797

packages/vscode/src/statusbar.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ export function activateStatusBar(state: ExtensionState) {
3030
}${tokensSoFar ? ` ${tokensSoFar} tokens` : ""}`
3131
)
3232

33+
const authority = server.authority
3334
const md = new vscode.MarkdownString(
3435
toMarkdownString(
35-
status === "running"
36+
authority && status === "running"
3637
? `server: [${server.authority}](${server.browserUrl})`
3738
: `server: ${status}`,
3839
fragment?.files?.[0],

0 commit comments

Comments
 (0)