|
1 | 1 | import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 2 |
|
| 3 | +const taskRuntimeInternalMocks = vi.hoisted(() => ({ |
| 4 | + listTasksForOwnerKey: vi.fn(), |
| 5 | +})); |
| 6 | + |
3 | 7 | const taskRuntimeMocks = vi.hoisted(() => ({ |
4 | 8 | createRunningTaskRun: vi.fn(), |
5 | 9 | recordTaskRunProgressByRunId: vi.fn(), |
6 | 10 | completeTaskRunByRunId: vi.fn(), |
7 | 11 | failTaskRunByRunId: vi.fn(), |
8 | 12 | })); |
9 | 13 |
|
| 14 | +vi.mock("../../tasks/runtime-internal.js", () => taskRuntimeInternalMocks); |
10 | 15 | vi.mock("../../tasks/detached-task-runtime.js", () => taskRuntimeMocks); |
11 | 16 |
|
12 | 17 | let imageGenerationRuntime: typeof import("../../image-generation/runtime.js"); |
@@ -304,6 +309,8 @@ describe("createImageGenerateTool", () => { |
304 | 309 | taskRuntimeMocks.recordTaskRunProgressByRunId.mockReset(); |
305 | 310 | taskRuntimeMocks.completeTaskRunByRunId.mockReset(); |
306 | 311 | taskRuntimeMocks.failTaskRunByRunId.mockReset(); |
| 312 | + taskRuntimeInternalMocks.listTasksForOwnerKey.mockReset(); |
| 313 | + taskRuntimeInternalMocks.listTasksForOwnerKey.mockReturnValue([]); |
307 | 314 | }); |
308 | 315 |
|
309 | 316 | afterEach(() => { |
@@ -736,6 +743,131 @@ describe("createImageGenerateTool", () => { |
736 | 743 | ); |
737 | 744 | }); |
738 | 745 |
|
| 746 | + it("allows a distinct image request while another image generation task is active", async () => { |
| 747 | + stubImageGenerationProviders(); |
| 748 | + vi.stubEnv("OPENAI_API_KEY", "openai-test"); |
| 749 | + vi.spyOn(imageGenerationRuntime, "generateImage").mockResolvedValue({ |
| 750 | + provider: "openai", |
| 751 | + model: "gpt-image-1", |
| 752 | + attempts: [], |
| 753 | + ignoredOverrides: [], |
| 754 | + images: [ |
| 755 | + { |
| 756 | + buffer: Buffer.from("png-out"), |
| 757 | + mimeType: "image/png", |
| 758 | + fileName: "second.png", |
| 759 | + }, |
| 760 | + ], |
| 761 | + }); |
| 762 | + taskRuntimeInternalMocks.listTasksForOwnerKey.mockReturnValue([ |
| 763 | + { |
| 764 | + taskId: "task-first-image", |
| 765 | + runtime: "cli", |
| 766 | + taskKind: "image_generation", |
| 767 | + sourceId: "image_generate:openai", |
| 768 | + requesterSessionKey: "agent:main:discord:direct:123", |
| 769 | + ownerKey: "agent:main:discord:direct:123", |
| 770 | + scopeKind: "session", |
| 771 | + task: "First diagram prompt", |
| 772 | + status: "running", |
| 773 | + deliveryStatus: "not_applicable", |
| 774 | + notifyPolicy: "silent", |
| 775 | + createdAt: Date.now(), |
| 776 | + }, |
| 777 | + ]); |
| 778 | + taskRuntimeMocks.createRunningTaskRun.mockReturnValue({ |
| 779 | + taskId: "task-second-image", |
| 780 | + }); |
| 781 | + const scheduled: Array<() => Promise<void>> = []; |
| 782 | + const tool = requireImageGenerateTool( |
| 783 | + createImageGenerateTool({ |
| 784 | + config: { |
| 785 | + agents: { |
| 786 | + defaults: { |
| 787 | + imageGenerationModel: { |
| 788 | + primary: "openai/gpt-image-1", |
| 789 | + }, |
| 790 | + }, |
| 791 | + }, |
| 792 | + }, |
| 793 | + agentDir: "/tmp/agent", |
| 794 | + agentSessionKey: "agent:main:discord:direct:123", |
| 795 | + requesterOrigin: { |
| 796 | + channel: "discord", |
| 797 | + to: "dm:123", |
| 798 | + }, |
| 799 | + scheduleBackgroundWork: (work) => { |
| 800 | + scheduled.push(work); |
| 801 | + }, |
| 802 | + }), |
| 803 | + ); |
| 804 | + |
| 805 | + const result = await tool.execute("call-second", { |
| 806 | + prompt: "Second diagram prompt", |
| 807 | + filename: "second.png", |
| 808 | + model: "openai/gpt-image-1", |
| 809 | + }); |
| 810 | + |
| 811 | + expect(scheduled).toHaveLength(1); |
| 812 | + expect(resultDetails(result).taskId).toBe("task-second-image"); |
| 813 | + expect(taskRuntimeMocks.createRunningTaskRun).toHaveBeenCalledWith( |
| 814 | + expect.objectContaining({ |
| 815 | + task: "Second diagram prompt", |
| 816 | + }), |
| 817 | + ); |
| 818 | + }); |
| 819 | + |
| 820 | + it("returns active status for a duplicate image request with the same prompt", async () => { |
| 821 | + stubImageGenerationProviders(); |
| 822 | + vi.stubEnv("OPENAI_API_KEY", "openai-test"); |
| 823 | + taskRuntimeInternalMocks.listTasksForOwnerKey.mockReturnValue([ |
| 824 | + { |
| 825 | + taskId: "task-existing-image", |
| 826 | + runtime: "cli", |
| 827 | + taskKind: "image_generation", |
| 828 | + sourceId: "image_generate:openai", |
| 829 | + requesterSessionKey: "agent:main:discord:direct:123", |
| 830 | + ownerKey: "agent:main:discord:direct:123", |
| 831 | + scopeKind: "session", |
| 832 | + task: "Same diagram prompt", |
| 833 | + status: "running", |
| 834 | + deliveryStatus: "not_applicable", |
| 835 | + notifyPolicy: "silent", |
| 836 | + createdAt: Date.now(), |
| 837 | + progressSummary: "Generating image", |
| 838 | + }, |
| 839 | + ]); |
| 840 | + const tool = requireImageGenerateTool( |
| 841 | + createImageGenerateTool({ |
| 842 | + config: { |
| 843 | + agents: { |
| 844 | + defaults: { |
| 845 | + imageGenerationModel: { |
| 846 | + primary: "openai/gpt-image-1", |
| 847 | + }, |
| 848 | + }, |
| 849 | + }, |
| 850 | + }, |
| 851 | + agentDir: "/tmp/agent", |
| 852 | + agentSessionKey: "agent:main:discord:direct:123", |
| 853 | + }), |
| 854 | + ); |
| 855 | + |
| 856 | + const result = await tool.execute("call-duplicate", { |
| 857 | + prompt: "Same diagram prompt", |
| 858 | + filename: "same.png", |
| 859 | + model: "openai/gpt-image-1", |
| 860 | + }); |
| 861 | + |
| 862 | + expect(taskRuntimeMocks.createRunningTaskRun).not.toHaveBeenCalled(); |
| 863 | + expect(resultText(result)).toContain( |
| 864 | + "Image generation task task-existing-image is already running", |
| 865 | + ); |
| 866 | + const details = resultDetails(result); |
| 867 | + expect(details.duplicateGuard).toBe(true); |
| 868 | + expect(details.task).toEqual({ taskId: "task-existing-image" }); |
| 869 | + }); |
| 870 | + |
739 | 871 | it("uses configured timeoutMs for image generation and lets calls override it", async () => { |
740 | 872 | stubImageGenerationProviders(); |
741 | 873 | const generateImage = vi.spyOn(imageGenerationRuntime, "generateImage").mockResolvedValue({ |
|
0 commit comments