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 1 commit
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import Foundation | ||
| #if canImport(FoundationNetworking) | ||
| import FoundationNetworking | ||
| #endif | ||
| import Testing | ||
| import os | ||
| @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 host = UUID().uuidString.lowercased() + ".test" | ||
| let capturedBody = OSAllocatedUnfairLock(initialState: Data?.none) | ||
| ImageURLProtocol.handlers.withLock { handlers in | ||
| handlers[host] = { request in | ||
| let stream = try #require(request.httpBody.map { InputStream(data: $0) } ?? request.httpBodyStream) | ||
| stream.open() | ||
| defer { stream.close() } | ||
| let json = try JSONSerialization.jsonObject(with: stream) | ||
| let body = try JSONSerialization.data(withJSONObject: json) | ||
| capturedBody.withLock { $0 = body } | ||
| return Data(#"{"created":1,"data":[{"url":"https://example.com/puppy.png"}]}"#.utf8) | ||
| } | ||
| } | ||
| defer { ImageURLProtocol.handlers.withLock { $0.removeValue(forKey: host) } } | ||
| let configuration = URLSessionConfiguration.ephemeral | ||
| configuration.protocolClasses = [ImageURLProtocol.self] | ||
| let session = URLSession(configuration: configuration) | ||
| defer { session.invalidateAndCancel() } | ||
| let client = OpenAI(configuration: .init(token: "test", host: host), session: session) | ||
| let store = ImageStore(openAIClient: client) | ||
|
|
||
| store.images(prompt: "A puppy", n: n, size: size) | ||
| try await sleepUntil { !store.imagesQueryInProgress } | ||
|
|
||
| let body = try #require(capturedBody.withLock { $0 }) | ||
| let json = try #require(JSONSerialization.jsonObject(with: body) as? [String: Any]) | ||
| #expect(json["model"] as? String == "gpt-image-1") | ||
| #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 failureIsVisibleAndRetryPublishesBase64ImagesThenCancellationEndsProgress() async throws { | ||
|
mvanhorn marked this conversation as resolved.
Resolved
|
||
| let host = UUID().uuidString.lowercased() + ".test" | ||
| let requests = OSAllocatedUnfairLock(initialState: 0) | ||
| let png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+ip1sAAAAASUVORK5CYII=" | ||
| ImageURLProtocol.handlers.withLock { handlers in | ||
| handlers[host] = { _ in | ||
| let number = requests.withLock { count in | ||
| count += 1 | ||
| return count | ||
| } | ||
| switch number { | ||
| case 1: | ||
| return Data(#"{"error":{"message":"Image generation rejected","type":"invalid_request_error"}}"#.utf8) | ||
| case 2: | ||
| return try JSONSerialization.data(withJSONObject: [ | ||
| "created": 1, "data": [["b64_json": png], ["b64_json": png]] | ||
| ]) | ||
| default: | ||
| return nil | ||
| } | ||
| } | ||
| } | ||
| defer { ImageURLProtocol.handlers.withLock { $0.removeValue(forKey: host) } } | ||
| let configuration = URLSessionConfiguration.ephemeral | ||
| configuration.protocolClasses = [ImageURLProtocol.self] | ||
| let session = URLSession(configuration: configuration) | ||
| defer { session.invalidateAndCancel() } | ||
| let client = OpenAI(configuration: .init(token: "test", host: host), session: session) | ||
| let store = ImageStore(openAIClient: client) | ||
|
|
||
| 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) | ||
|
|
||
| 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.b64Json == png && $0.url == nil }) | ||
|
|
||
| store.images(prompt: "A puppy", n: 2, size: ._1024) | ||
| try await sleepUntil { requests.withLock { $0 == 3 } } | ||
| #expect(store.imagesQueryInProgress) | ||
| store.cancelImagesQuery() | ||
| #expect(!store.imagesQueryInProgress) | ||
| #expect(store.imagesQueryError == nil) | ||
| #expect(store.images.isEmpty) | ||
| } | ||
| } | ||
|
|
||
| private final class ImageURLProtocol: URLProtocol { | ||
|
mvanhorn marked this conversation as resolved.
Resolved
|
||
| static let handlers = OSAllocatedUnfairLock(initialState: [String: @Sendable (URLRequest) throws -> Data?]()) | ||
|
|
||
| override class func canInit(with request: URLRequest) -> Bool { true } | ||
| override class func canonicalRequest(for request: URLRequest) -> URLRequest { request } | ||
|
|
||
| override func startLoading() { | ||
| do { | ||
| let host = request.url!.host! | ||
| let handler = try #require(Self.handlers.withLock { $0[host] }) | ||
| guard let data = try handler(request) else { return } | ||
| let status = data.starts(with: Data(#"{"error"#.utf8)) ? 400 : 200 | ||
| let response = HTTPURLResponse(url: request.url!, statusCode: status, httpVersion: nil, headerFields: ["Content-Type": "application/json"])! | ||
| client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) | ||
| client?.urlProtocol(self, didLoad: data) | ||
| client?.urlProtocolDidFinishLoading(self) | ||
| } catch { | ||
| client?.urlProtocol(self, didFailWithError: error) | ||
| } | ||
| } | ||
|
|
||
| override func stopLoading() {} | ||
| } | ||
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,90 @@ | ||
| import Foundation | ||
| #if canImport(FoundationNetworking) | ||
| import FoundationNetworking | ||
| #endif | ||
| import Testing | ||
| import os | ||
| @testable import DemoChat | ||
| @testable import OpenAI | ||
|
|
||
| @MainActor | ||
| @Suite(.timeLimit(.minutes(1))) | ||
| struct MiscStoreTests { | ||
| @Test(arguments: [200, 400]) | ||
| func moderationUsesOmniAndPreservesConversationHandling(status: Int) async throws { | ||
| let host = UUID().uuidString.lowercased() + ".test" | ||
| let capturedBody = OSAllocatedUnfairLock(initialState: Data?.none) | ||
| ModerationURLProtocol.handlers.withLock { handlers in | ||
| handlers[host] = { request in | ||
| let stream = try #require(request.httpBody.map { InputStream(data: $0) } ?? request.httpBodyStream) | ||
| stream.open() | ||
| defer { stream.close() } | ||
| let json = try JSONSerialization.jsonObject(with: stream) | ||
| let body = try JSONSerialization.data(withJSONObject: json) | ||
| capturedBody.withLock { $0 = body } | ||
| 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 { ModerationURLProtocol.handlers.withLock { $0.removeValue(forKey: host) } } | ||
| let configuration = URLSessionConfiguration.ephemeral | ||
| configuration.protocolClasses = [ModerationURLProtocol.self] | ||
| let session = URLSession(configuration: configuration) | ||
| defer { session.invalidateAndCancel() } | ||
| let client = OpenAI(configuration: .init(token: "test", host: host), session: session) | ||
| let store = MiscStore(openAIClient: client) | ||
|
|
||
| await store.sendModerationMessage(Message(id: "user", role: .user, content: "I love puppies", createdAt: Date())) | ||
|
|
||
| let body = try #require(capturedBody.withLock { $0 }) | ||
| 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) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private final class ModerationURLProtocol: URLProtocol { | ||
| static let handlers = OSAllocatedUnfairLock(initialState: [String: @Sendable (URLRequest) throws -> Data]()) | ||
|
|
||
| override class func canInit(with request: URLRequest) -> Bool { true } | ||
| override class func canonicalRequest(for request: URLRequest) -> URLRequest { request } | ||
|
|
||
| override func startLoading() { | ||
| do { | ||
| let host = request.url!.host! | ||
| let handler = try #require(Self.handlers.withLock { $0[host] }) | ||
| let data = try handler(request) | ||
| let status = data.starts(with: Data(#"{"error"#.utf8)) ? 400 : 200 | ||
| let response = HTTPURLResponse(url: request.url!, statusCode: status, httpVersion: nil, headerFields: ["Content-Type": "application/json"])! | ||
| client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed) | ||
| client?.urlProtocol(self, didLoad: data) | ||
| client?.urlProtocolDidFinishLoading(self) | ||
| } catch { | ||
| client?.urlProtocol(self, didFailWithError: error) | ||
| } | ||
| } | ||
|
|
||
| override func stopLoading() {} | ||
| } |
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.