-
-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Add generate-image agent skill #6140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shatfield4
wants to merge
9
commits into
master
Choose a base branch
from
6139-feat-image-generation-agent-skill
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+452
β12
Open
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4927cdb
add generate-image agent skill + live image card in agent chat
shatfield4 a693e64
Merge branch 'master' into 6139-feat-image-generation-agent-skill
shatfield4 dbfa959
serve agent generated images from storage instead of inline b64
shatfield4 51b8b45
Merge branch 'master' into 6139-feat-image-generation-agent-skill
shatfield4 853ca8a
Merge branch 'master' into 6139-feat-image-generation-agent-skill
shatfield4 f05ff79
add generate-image skill header image
shatfield4 444a451
only skip cleared chats when serving agent generated files
shatfield4 67ad180
move image generation card payload into socket content object
shatfield4 4d8603a
Merge branch 'master' into 6139-feat-image-generation-agent-skill
shatfield4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
server/__tests__/utils/agents/aibitat/plugins/generate-image.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| /* eslint-env jest */ | ||
| const fs = require("fs"); | ||
| const os = require("os"); | ||
| const path = require("path"); | ||
|
|
||
| // `utils/files` resolves its storage paths from STORAGE_DIR at require time. | ||
| process.env.STORAGE_DIR = fs.mkdtempSync( | ||
| path.join(os.tmpdir(), "generate-image-test-") | ||
| ); | ||
| jest.mock("../../../../../utils/ImageGenerators", () => ({ | ||
| generateImageForWorkspace: jest.fn(), | ||
| editImageForWorkspace: jest.fn(), | ||
| })); | ||
| jest.mock("../../../../../utils/helpers", () => ({ | ||
| getImageGeneratorProvider: jest.fn(), | ||
| })); | ||
| jest.mock("../../../../../models/workspaceChats", () => ({ | ||
| WorkspaceChats: { _update: jest.fn() }, | ||
| })); | ||
|
|
||
| const { | ||
| generateImageForWorkspace, | ||
| editImageForWorkspace, | ||
| } = require("../../../../../utils/ImageGenerators"); | ||
| const { WorkspaceChats } = require("../../../../../models/workspaceChats"); | ||
| const { | ||
| generateImage, | ||
| } = require("../../../../../utils/agents/aibitat/plugins/generate-image.js"); | ||
|
|
||
| const SAVED_IMAGE = { | ||
| storageFilename: "img-11111111-2222-3333-4444-555555555555.png", | ||
| filename: "a-fox.png", | ||
| fileSize: 100, | ||
| buffer: Buffer.from("image-bytes"), | ||
| }; | ||
|
|
||
| beforeAll(() => { | ||
| const dir = path.join(process.env.STORAGE_DIR, "generated-images"); | ||
| fs.mkdirSync(dir, { recursive: true }); | ||
| fs.writeFileSync( | ||
| path.join(dir, SAVED_IMAGE.storageFilename), | ||
| SAVED_IMAGE.buffer | ||
| ); | ||
| }); | ||
|
|
||
| afterAll(() => | ||
| fs.rmSync(process.env.STORAGE_DIR, { recursive: true, force: true }) | ||
| ); | ||
|
|
||
| function setupPlugin(chats = []) { | ||
| const aibitat = { | ||
| chats, | ||
| introspect: jest.fn(), | ||
| handlerProps: { log: jest.fn() }, | ||
| socket: { send: jest.fn() }, | ||
| function: (config) => (aibitat._fn = config), | ||
| }; | ||
| generateImage.plugin.call(generateImage).setup(aibitat); | ||
| return { aibitat, handler: aibitat._fn.handler.bind(aibitat._fn) }; | ||
| } | ||
|
|
||
| beforeEach(() => jest.clearAllMocks()); | ||
|
|
||
| describe("generate-image agent skill", () => { | ||
| test("generates an image and registers the card for live + historical render", async () => { | ||
| generateImageForWorkspace.mockResolvedValue(SAVED_IMAGE); | ||
| const { aibitat, handler } = setupPlugin(); | ||
|
|
||
| await handler({ prompt: "a red fox in the snow", size: "512x512" }); | ||
|
|
||
| expect(generateImageForWorkspace).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| prompt: "a red fox in the snow", | ||
| size: "512x512", | ||
| }) | ||
| ); | ||
| const expectedOutput = { | ||
| type: "imageGenerationCard", | ||
| payload: { | ||
| storageFilename: SAVED_IMAGE.storageFilename, | ||
| filename: SAVED_IMAGE.filename, | ||
| fileSize: SAVED_IMAGE.fileSize, | ||
| prompt: "a red fox in the snow", | ||
| }, | ||
| }; | ||
| // The card must only reference the stored file - no image bytes travel over | ||
| // the socket or into the chat record. | ||
| expect(aibitat._pendingOutputs).toEqual([expectedOutput]); | ||
| const [, , extras] = aibitat.socket.send.mock.calls.find( | ||
| ([type]) => type === "imageGenerationCard" | ||
| ); | ||
| expect(extras.outputs).toEqual([expectedOutput]); | ||
| }); | ||
|
|
||
| test("writes the image reference to the reserved chat before showing the card", async () => { | ||
| generateImageForWorkspace.mockResolvedValue(SAVED_IMAGE); | ||
| const { aibitat, handler } = setupPlugin(); | ||
| aibitat.trackedChatId = 42; | ||
|
|
||
| await handler({ prompt: "a fox" }); | ||
|
|
||
| // The serve endpoint only authorizes files referenced by a chat record, so | ||
| // the reference has to land before the frontend requests the image. | ||
| expect(WorkspaceChats._update).toHaveBeenCalledWith(42, { | ||
| response: JSON.stringify({ outputs: aibitat._pendingOutputs }), | ||
| }); | ||
| expect(WorkspaceChats._update.mock.invocationCallOrder[0]).toBeLessThan( | ||
| aibitat.socket.send.mock.invocationCallOrder[1] | ||
| ); | ||
| }); | ||
|
|
||
| test("shows a placeholder card and swaps it for the result", async () => { | ||
| generateImageForWorkspace.mockResolvedValue(SAVED_IMAGE); | ||
| const { aibitat, handler } = setupPlugin(); | ||
|
|
||
| await handler({ prompt: "a fox" }); | ||
|
|
||
| const [pending, card] = aibitat.socket.send.mock.calls; | ||
| expect(pending[0]).toBe("imageGenerationPending"); | ||
| expect(card[0]).toBe("imageGenerationCard"); | ||
| expect(card[2].pendingId).toBe(pending[2].pendingId); | ||
| }); | ||
|
|
||
| test("clears the placeholder card when generation fails", async () => { | ||
| generateImageForWorkspace.mockRejectedValue(new Error("provider is down")); | ||
| const { aibitat, handler } = setupPlugin(); | ||
|
|
||
| const reply = await handler({ prompt: "a fox" }); | ||
|
|
||
| const [pending, failure] = aibitat.socket.send.mock.calls; | ||
| expect(failure[0]).toBe("imageGenerationCard"); | ||
| expect(failure[2]).toEqual({ | ||
| pendingId: pending[2].pendingId, | ||
| failed: true, | ||
| }); | ||
| expect(reply).toContain("provider is down"); | ||
| }); | ||
|
|
||
| test("drops a size the model invented instead of failing the call", async () => { | ||
| generateImageForWorkspace.mockResolvedValue(SAVED_IMAGE); | ||
| const { handler } = setupPlugin(); | ||
|
|
||
| await handler({ prompt: "a fox", size: "large" }); | ||
|
|
||
| expect(generateImageForWorkspace).toHaveBeenCalledWith( | ||
| expect.objectContaining({ size: null }) | ||
| ); | ||
| }); | ||
|
|
||
| test("edits using the images attached to the last user message", async () => { | ||
| editImageForWorkspace.mockResolvedValue(SAVED_IMAGE); | ||
| const { handler } = setupPlugin([ | ||
| { from: "USER", content: "make it blue" }, | ||
| { | ||
| from: "USER", | ||
| content: "here is my photo", | ||
| attachments: [ | ||
| { | ||
| mime: "image/png", | ||
| contentString: `data:image/png;base64,${Buffer.from("source").toString("base64")}`, | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
|
|
||
| await handler({ prompt: "make it blue", edit: true }); | ||
|
|
||
| expect(generateImageForWorkspace).not.toHaveBeenCalled(); | ||
| expect(editImageForWorkspace).toHaveBeenCalledWith( | ||
| expect.objectContaining({ images: [Buffer.from("source")] }) | ||
| ); | ||
| }); | ||
|
|
||
| test("falls back to the image generated earlier in the session when editing", async () => { | ||
| generateImageForWorkspace.mockResolvedValue(SAVED_IMAGE); | ||
| editImageForWorkspace.mockResolvedValue(SAVED_IMAGE); | ||
| const { aibitat, handler } = setupPlugin(); | ||
|
|
||
| await handler({ prompt: "a fox" }); | ||
| await handler({ prompt: "now make it blue", edit: true }); | ||
|
|
||
| expect(aibitat._lastGeneratedImage).toBe(SAVED_IMAGE.storageFilename); | ||
| expect(editImageForWorkspace).toHaveBeenCalledWith( | ||
| expect.objectContaining({ images: [SAVED_IMAGE.buffer] }) | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.