mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
fix(core): tag image job notices with their job ID (#60098)
This commit is contained in:
committed by
GitHub
Unverified
parent
6118c83b01
commit
28a3974efc
@@ -0,0 +1,6 @@
|
||||
export * as ImageNotice from "./notice.js"
|
||||
|
||||
/** The completion notice for a background image job, tagged with its job ID like `<shell id=…>` notices. */
|
||||
export function text(input: { id: string; state: string; body: string }) {
|
||||
return `<image id="${input.id}" state="${input.state}">\n${input.body}\n</image>`
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import { SessionEvent } from "../event.js"
|
||||
import { SessionExecution } from "../execution.js"
|
||||
import { SessionSchema } from "../schema.js"
|
||||
import { SessionStore } from "../store.js"
|
||||
import { ImageNotice } from "../../image/notice.js"
|
||||
import { ShellResult } from "../../shell/result.js"
|
||||
import { SubagentCompletion } from "../subagent-completion.js"
|
||||
|
||||
@@ -139,7 +140,7 @@ export const layer = (options?: Options) =>
|
||||
background: Job.Background,
|
||||
recovery: Extract<Job.Recovery, { kind: "image" }>,
|
||||
) {
|
||||
const text =
|
||||
const body =
|
||||
background.status === "running"
|
||||
? `Image request for ${recovery.path} was cancelled because the server restarted.`
|
||||
: background.status === "completed"
|
||||
@@ -152,7 +153,11 @@ export const layer = (options?: Options) =>
|
||||
id: background.notificationID,
|
||||
sessionID: recovery.sessionID,
|
||||
description: "Image generation",
|
||||
text,
|
||||
text: ImageNotice.text({
|
||||
id: background.id,
|
||||
state: background.status === "running" ? "cancelled" : background.status,
|
||||
body,
|
||||
}),
|
||||
metadata: {
|
||||
source: "image_generate",
|
||||
jobID: background.id,
|
||||
|
||||
@@ -13,6 +13,7 @@ import { ConfigEntryObserver } from "../../config/plugin/entry-observer.js"
|
||||
import { Environment } from "../../environment/index.js"
|
||||
import { FileAccess } from "../../file-access.js"
|
||||
import { Generate } from "../../generate.js"
|
||||
import { ImageNotice } from "../../image/notice.js"
|
||||
import { Job } from "../../job.js"
|
||||
import { Mime } from "../../mime.js"
|
||||
import { Model } from "../../model.js"
|
||||
@@ -65,6 +66,7 @@ export const Input = Schema.Struct({
|
||||
})
|
||||
|
||||
export const Output = Schema.Struct({
|
||||
id: Schema.String.annotate({ description: "The image job's ID, repeated in its completion notice" }),
|
||||
path: Schema.String.annotate({ description: "Where the image is saved, without its extension" }),
|
||||
model: Schema.String.annotate({ description: "providerID/modelID" }),
|
||||
})
|
||||
@@ -198,22 +200,26 @@ export const Plugin = {
|
||||
})
|
||||
|
||||
const notifyWhenDone = Effect.fn("ImageGenerateTool.notifyWhenDone")(
|
||||
function* (sessionID: SessionSchema.ID, id: string, notificationID: SessionMessage.ID, file: string) {
|
||||
const info = (yield* jobs.wait({ id })).info
|
||||
function* (input: { sessionID: SessionSchema.ID; id: string; notificationID: SessionMessage.ID; file: string }) {
|
||||
const info = (yield* jobs.wait({ id: input.id })).info
|
||||
if (!info || info.status === "running") return
|
||||
yield* sessions.synthetic({
|
||||
id: notificationID,
|
||||
sessionID,
|
||||
id: input.notificationID,
|
||||
sessionID: input.sessionID,
|
||||
description: "Image generation",
|
||||
text:
|
||||
info.status === "completed"
|
||||
? (info.output ?? "")
|
||||
: info.status === "error"
|
||||
? (info.error ?? `Image request failed for ${file}.`)
|
||||
: `Image request for ${file} was cancelled.`,
|
||||
metadata: { source: name, jobID: id, state: info.status, path: file },
|
||||
text: ImageNotice.text({
|
||||
id: input.id,
|
||||
state: info.status,
|
||||
body:
|
||||
info.status === "completed"
|
||||
? (info.output ?? "")
|
||||
: info.status === "error"
|
||||
? (info.error ?? `Image request failed for ${input.file}.`)
|
||||
: `Image request for ${input.file} was cancelled.`,
|
||||
}),
|
||||
metadata: { source: name, jobID: input.id, state: info.status, path: input.file },
|
||||
})
|
||||
yield* jobs.completeBackground(notificationID)
|
||||
yield* jobs.completeBackground(input.notificationID)
|
||||
},
|
||||
Effect.forkIn(scope, { startImmediately: true }),
|
||||
)
|
||||
@@ -345,7 +351,7 @@ export const Plugin = {
|
||||
)
|
||||
const id = `image_${Identifier.create(false)}`
|
||||
const notificationID = SessionMessage.ID.create()
|
||||
const job = yield* jobs.start({
|
||||
yield* jobs.start({
|
||||
id,
|
||||
notificationID,
|
||||
type: name,
|
||||
@@ -354,17 +360,17 @@ export const Plugin = {
|
||||
recovery: { kind: "image", sessionID: context.sessionID, path: stem, model: ref },
|
||||
run,
|
||||
})
|
||||
yield* jobs.background(job.id)
|
||||
yield* notifyWhenDone(context.sessionID, job.id, notificationID, stem)
|
||||
yield* jobs.background(id)
|
||||
yield* notifyWhenDone({ sessionID: context.sessionID, id, notificationID, file: stem })
|
||||
return {
|
||||
output: { path: stem, model: ref },
|
||||
output: { id, path: stem, model: ref },
|
||||
content: [
|
||||
`Image request started in the background with ${ref}. It will be saved as ${stem} with the extension of its real format (.png, .jpg, .webp or .gif); the notice gives the exact path.${resolved.note ?? ""}`,
|
||||
`Image job ${id} started in the background with ${ref}. It will be saved as ${stem} with the extension of its real format (.png, .jpg, .webp or .gif); the notice gives the exact path.${resolved.note ?? ""}`,
|
||||
"You will be notified automatically when it finishes or fails.",
|
||||
"DO NOT sleep, poll, check whether the file exists, or request the same image again while it runs.",
|
||||
"Work on other tasks, or briefly tell the user what you started and end your response.",
|
||||
].join("\n"),
|
||||
metadata: { path: stem, model: ref, jobID: job.id },
|
||||
metadata: { path: stem, model: ref, jobID: id },
|
||||
}
|
||||
}).pipe(
|
||||
Effect.mapError((error) =>
|
||||
|
||||
@@ -571,8 +571,8 @@ describe("SessionRestart background recovery", () => {
|
||||
|
||||
const inbox = yield* SessionInbox.list(database.db, sessionID)
|
||||
expect(inbox.map((item) => (item.type === "synthetic" ? item.payload.text : undefined)).toSorted()).toEqual([
|
||||
"Image request finished. Saved image: /tmp/done.png (test/painter).",
|
||||
"Image request for /tmp/running.png was cancelled because the server restarted.",
|
||||
'<image id="image_done" state="completed">\nImage request finished. Saved image: /tmp/done.png (test/painter).\n</image>',
|
||||
'<image id="image_running" state="cancelled">\nImage request for /tmp/running.png was cancelled because the server restarted.\n</image>',
|
||||
])
|
||||
expect(yield* restarted.pendingBackground).toEqual([])
|
||||
}),
|
||||
|
||||
@@ -238,20 +238,23 @@ describe("image_generate", () => {
|
||||
const result = yield* run({ prompt: "make it blue", images: ["in.png"], path: "out.png" })
|
||||
const file = path.join(directory, "out.png")
|
||||
const stem = path.join(directory, "out")
|
||||
expect(result.output).toEqual({ path: stem, model: "test/painter" })
|
||||
const id = result.output.id
|
||||
expect(id).toStartWith("image_")
|
||||
expect(result.output).toEqual({ id, path: stem, model: "test/painter" })
|
||||
expect(result.content).toEqual([
|
||||
{
|
||||
type: "text",
|
||||
text: [
|
||||
`Image request started in the background with test/painter. It will be saved as ${stem} with the extension of its real format (.png, .jpg, .webp or .gif); the notice gives the exact path.`,
|
||||
`Image job ${id} started in the background with test/painter. It will be saved as ${stem} with the extension of its real format (.png, .jpg, .webp or .gif); the notice gives the exact path.`,
|
||||
"You will be notified automatically when it finishes or fails.",
|
||||
"DO NOT sleep, poll, check whether the file exists, or request the same image again while it runs.",
|
||||
"Work on other tasks, or briefly tell the user what you started and end your response.",
|
||||
].join("\n"),
|
||||
},
|
||||
])
|
||||
// The notice carries the job ID from the start result, like shell notices.
|
||||
expect(yield* notice(result)).toBe(
|
||||
`Image request finished. Saved image: ${file} (test/painter).\nUse the read tool on this path to view the image.`,
|
||||
`<image id="${id}" state="completed">\nImage request finished. Saved image: ${file} (test/painter).\nUse the read tool on this path to view the image.\n</image>`,
|
||||
)
|
||||
expect((yield* Effect.promise(() => fs.readFile(file))).toString("base64")).toBe(PNG)
|
||||
expect(fixture.requests).toEqual([
|
||||
@@ -288,7 +291,7 @@ describe("image_generate", () => {
|
||||
const result = yield* run({ prompt: "a cat" })
|
||||
expect(path.dirname(result.output.path)).toBe(path.join(global.tmp, "images", session.id))
|
||||
const saved = `${result.output.path}.jpg`
|
||||
expect(yield* notice(result)).toBe(
|
||||
expect(yield* notice(result)).toContain(
|
||||
`Image request finished. Saved image: ${saved} (test/painter).\nUse the read tool on this path to view the image.`,
|
||||
)
|
||||
expect(yield* Effect.promise(() => Bun.file(saved).exists())).toBe(true)
|
||||
@@ -327,7 +330,7 @@ describe("image_generate", () => {
|
||||
})
|
||||
const result = yield* run({ prompt: "x", path: "out.png" })
|
||||
expect(yield* notice(result)).toBe(
|
||||
`Image request failed for ${path.join(directory, "out")}: The prompt was rejected`,
|
||||
`<image id="${result.output.id}" state="error">\nImage request failed for ${path.join(directory, "out")}: The prompt was rejected\n</image>`,
|
||||
)
|
||||
expect(yield* Effect.promise(() => Bun.file(path.join(directory, "out.png")).exists())).toBe(false)
|
||||
}),
|
||||
@@ -343,7 +346,9 @@ describe("image_generate", () => {
|
||||
const id = String(result.metadata?.jobID)
|
||||
expect((yield* jobs.get(id))?.status).toBe("running")
|
||||
yield* jobs.cancel(id)
|
||||
expect(yield* notice(result)).toBe(`Image request for ${path.join(directory, "out")} was cancelled.`)
|
||||
expect(yield* notice(result)).toBe(
|
||||
`<image id="${id}" state="cancelled">\nImage request for ${path.join(directory, "out")} was cancelled.\n</image>`,
|
||||
)
|
||||
expect(yield* Effect.promise(() => Bun.file(path.join(directory, "out.png")).exists())).toBe(false)
|
||||
}),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user