Repository navigation
fix: restore Demo moderation and image creation #485
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
Merged
nezhyborets
merged 11 commits into
MacPaw:main
from
mvanhorn:fix/478-demo-moderation-image-creation
Nov 10, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3648e72
Bug: Restore Demo moderation and image creation
mvanhorn 3364878
Demo: create images with gpt-image-1.5
mvanhorn 7dc7f20
Add Model.omniModerationLatest and use it in the Demo
mvanhorn c24e58c
Demo: decode created images once, when the response arrives
mvanhorn d641592
Demo: decode created images outside the main actor
mvanhorn dee1c2c
Demo: split the image store failure, retry and cancellation test
mvanhorn 9257b9d
Demo: say when a cancelled error reaches the image store
mvanhorn f0f8f24
Demo: say why image creation offers only these sizes
mvanhorn 1c27bdb
Leave Model.omniModerationLatest undocumented
mvanhorn 0f0fb26
Demo: share one URL stub between the image and moderation tests
mvanhorn 4bd7550
Demo: keep ImageStore platform-agnostic with CGImage
mvanhorn 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import Foundation | ||
| import Testing | ||
| @testable import DemoChat | ||
| @testable import OpenAI | ||
|
|
||
| @MainActor | ||
| @Suite(.timeLimit(.minutes(1))) | ||
| struct ImageStoreTests { | ||
| @Test(arguments: [ImagesQuery.Size._1024, ._1536x1024, ._1024x1536, .auto], [1, 10]) | ||
| func creationSendsExplicitModel(size: ImagesQuery.Size, n: Int) async throws { | ||
| let mock = MockURL { _ in | ||
| Data(#"{"created":1,"data":[{"url":"https://example.com/puppy.png"}]}"#.utf8) | ||
| } | ||
| defer { mock.remove() } | ||
| let store = ImageStore(openAIClient: mock.makeClient()) | ||
|
|
||
| store.images(prompt: "A puppy", n: n, size: size) | ||
| try await sleepUntil { !store.imagesQueryInProgress } | ||
|
|
||
| let body = try #require(mock.lastBody) | ||
| let json = try #require(JSONSerialization.jsonObject(with: body) as? [String: Any]) | ||
| #expect(json["model"] as? String == "gpt-image-1.5") | ||
| #expect(json["prompt"] as? String == "A puppy") | ||
| #expect(json["n"] as? Int == n) | ||
| #expect(json["size"] as? String == size.rawValue) | ||
| #expect(json["response_format"] == nil) | ||
| #expect(store.images.first?.url == "https://example.com/puppy.png") | ||
| #expect(store.imagesQueryError == nil) | ||
| } | ||
|
|
||
| @Test | ||
| func failureIsVisible() async throws { | ||
| let mock = MockURL { _ in | ||
| Data(#"{"error":{"message":"Image generation rejected","type":"invalid_request_error"}}"#.utf8) | ||
| } | ||
| defer { mock.remove() } | ||
| let store = ImageStore(openAIClient: mock.makeClient()) | ||
|
|
||
| store.images(prompt: "A puppy", n: 2, size: ._1024) | ||
| try await sleepUntil { !store.imagesQueryInProgress } | ||
| #expect(store.imagesQueryError?.localizedDescription == "Image generation rejected") | ||
| #expect(store.images.isEmpty) | ||
| } | ||
|
|
||
| @Test | ||
| func retryClearsErrorAndPublishesBase64Images() async throws { | ||
| let png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+ip1sAAAAASUVORK5CYII=" | ||
| let mock = MockURL { _ in | ||
| Data(#"{"error":{"message":"Image generation rejected","type":"invalid_request_error"}}"#.utf8) | ||
| } | ||
| defer { mock.remove() } | ||
| let store = ImageStore(openAIClient: mock.makeClient()) | ||
|
|
||
| store.images(prompt: "A puppy", n: 2, size: ._1024) | ||
| try await sleepUntil { !store.imagesQueryInProgress } | ||
| #expect(store.imagesQueryError != nil) | ||
|
|
||
| mock.respond = { _ in | ||
| try JSONSerialization.data(withJSONObject: [ | ||
| "created": 1, "data": [["b64_json": png], ["b64_json": png]] | ||
| ]) | ||
| } | ||
|
|
||
| store.images(prompt: "A puppy", n: 2, size: ._1024) | ||
| #expect(store.imagesQueryError == nil) | ||
| try await sleepUntil { !store.imagesQueryInProgress } | ||
| #expect(store.imagesQueryError == nil) | ||
| #expect(store.images.count == 2) | ||
| #expect(store.images.allSatisfy { $0.hasBase64 && $0.url == nil && $0.cgImage?.width == 1 && $0.cgImage?.height == 1 }) | ||
| } | ||
|
|
||
| @Test | ||
| func cancellationEndsProgress() async throws { | ||
| // Returning nil never answers the request, so it stays in flight until cancelled | ||
| let mock = MockURL { _ in nil } | ||
| defer { mock.remove() } | ||
| let store = ImageStore(openAIClient: mock.makeClient()) | ||
|
|
||
| store.images(prompt: "A puppy", n: 2, size: ._1024) | ||
| try await sleepUntil { mock.requestCount == 1 } | ||
| #expect(store.imagesQueryInProgress) | ||
| store.cancelImagesQuery() | ||
| #expect(!store.imagesQueryInProgress) | ||
| #expect(store.imagesQueryError == nil) | ||
| #expect(store.images.isEmpty) | ||
| } | ||
| } |
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,48 @@ | ||
| import Foundation | ||
| import Testing | ||
| @testable import DemoChat | ||
| @testable import OpenAI | ||
|
|
||
| @MainActor | ||
| @Suite(.timeLimit(.minutes(1))) | ||
| struct MiscStoreTests { | ||
| @Test(arguments: [200, 400]) | ||
| func moderationUsesOmniAndPreservesConversationHandling(status: Int) async throws { | ||
| let mock = MockURL { _ in | ||
| if status == 400 { | ||
| return Data(#"{"error":{"message":"Moderation rejected","type":"invalid_request_error"}}"#.utf8) | ||
| } | ||
| let keys = ["harassment", "harassment/threatening", "hate", "hate/threatening", | ||
| "self-harm", "self-harm/intent", "self-harm/instructions", "sexual", | ||
| "sexual/minors", "violence", "violence/graphic", "illicit", "illicit/violent"] | ||
| return try JSONSerialization.data(withJSONObject: [ | ||
| "id": "modr-test", "model": "omni-moderation-latest", | ||
| "results": [[ | ||
| "flagged": false, | ||
| "categories": Dictionary(uniqueKeysWithValues: keys.map { ($0, false) }), | ||
| "category_scores": Dictionary(uniqueKeysWithValues: keys.map { ($0, 0.0) }), | ||
| "category_applied_input_types": ["illicit": ["text"]] | ||
| ]] | ||
| ]) | ||
| } | ||
| defer { mock.remove() } | ||
| let store = MiscStore(openAIClient: mock.makeClient()) | ||
|
|
||
| await store.sendModerationMessage(Message(id: "user", role: .user, content: "I love puppies", createdAt: Date())) | ||
|
|
||
| let body = try #require(mock.lastBody) | ||
| let query = try JSONDecoder().decode(ModerationsQuery.self, from: body) | ||
| #expect(query.model == "omni-moderation-latest") | ||
| #expect(query.input == "I love puppies") | ||
| #expect(store.moderationConversation.messages.first?.content == "I love puppies") | ||
| if status == 200 { | ||
| #expect(store.moderationConversationError == nil) | ||
| #expect(store.moderationConversation.messages.last?.role == .assistant) | ||
| #expect(store.moderationConversation.messages.last?.content.contains("🟢 harassment") == true) | ||
| } else { | ||
| #expect(store.moderationConversationError?.localizedDescription == "Moderation rejected") | ||
| #expect(store.moderationConversation.messages.count == 1) | ||
| } | ||
| } | ||
| } | ||
|
|
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.