diff --git a/packages/core/src/image/notice.ts b/packages/core/src/image/notice.ts new file mode 100644 index 00000000000..dfa23d1c3f9 --- /dev/null +++ b/packages/core/src/image/notice.ts @@ -0,0 +1,6 @@ +export * as ImageNotice from "./notice.js" + +/** The completion notice for a background image job, tagged with its job ID like `` notices. */ +export function text(input: { id: string; state: string; body: string }) { + return `\n${input.body}\n` +} diff --git a/packages/core/src/session/execution/restart.ts b/packages/core/src/session/execution/restart.ts index bf4aa65b35a..dd1eb2f3daa 100644 --- a/packages/core/src/session/execution/restart.ts +++ b/packages/core/src/session/execution/restart.ts @@ -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, ) { - 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, diff --git a/packages/core/src/tool/plugin/image-generate.ts b/packages/core/src/tool/plugin/image-generate.ts index bd7d1cc5a31..b7a2ca829ec 100644 --- a/packages/core/src/tool/plugin/image-generate.ts +++ b/packages/core/src/tool/plugin/image-generate.ts @@ -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) => diff --git a/packages/core/test/session-execution.test.ts b/packages/core/test/session-execution.test.ts index 4cd23d9af22..9c3ac71950e 100644 --- a/packages/core/test/session-execution.test.ts +++ b/packages/core/test/session-execution.test.ts @@ -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.", + '\nImage request finished. Saved image: /tmp/done.png (test/painter).\n', + '\nImage request for /tmp/running.png was cancelled because the server restarted.\n', ]) expect(yield* restarted.pendingBackground).toEqual([]) }), diff --git a/packages/core/test/tool-image-generate.test.ts b/packages/core/test/tool-image-generate.test.ts index 0ec830a118d..478139392f4 100644 --- a/packages/core/test/tool-image-generate.test.ts +++ b/packages/core/test/tool-image-generate.test.ts @@ -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.`, + `\nImage request finished. Saved image: ${file} (test/painter).\nUse the read tool on this path to view the image.\n`, ) 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`, + `\nImage request failed for ${path.join(directory, "out")}: The prompt was rejected\n`, ) 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( + `\nImage request for ${path.join(directory, "out")} was cancelled.\n`, + ) expect(yield* Effect.promise(() => Bun.file(path.join(directory, "out.png")).exists())).toBe(false) }), )