// Code Custodian File ID: 02C0C228-9E28-47FB-8B5C-4A60A589B197 import Foundation import AppKit import BrowserKit // MARK: - CLI Argument Parsing let args = Array(CommandLine.arguments.dropFirst()) guard args.count >= 6 else { print("āŒ Usage: swift run_multi_character_batch.swift [target_agent] ") exit(1) } let char1Path = args[0] let char2Path = args[1] let targetRatio = args[2] let targetQuantity = args[3] let prefixName = args[4] // Check if argument at index 5 matches an agent name (contains "Annie" or "Bob") // If so, check if argument at index 6 is a version string (contains "." or matches "2.0", "1.0") var targetAgent = "Annie 2.0" var promptText = "" if args.count >= 7 { let potentialAgent = args[5] let isAgentName = potentialAgent.contains("Annie") || potentialAgent.contains("Bob") || potentialAgent.contains("annie") || potentialAgent.contains("bob") if isAgentName { if args.count >= 8 { let potentialVersion = args[6] let isVersion = potentialVersion.contains(".") || potentialVersion == "2.0" || potentialVersion == "1.0" || potentialVersion == "2" || potentialVersion == "1" if isVersion { targetAgent = "\(potentialAgent) \(potentialVersion)" promptText = args.dropFirst(7).joined(separator: " ") } else { targetAgent = potentialAgent promptText = args.dropFirst(6).joined(separator: " ") } } else { targetAgent = potentialAgent promptText = args.dropFirst(6).joined(separator: " ") } } else { targetAgent = "Annie 2.0" promptText = args.dropFirst(5).joined(separator: " ") } } else { targetAgent = "Annie 2.0" promptText = args.dropFirst(5).joined(separator: " ") } print("šŸ“‹ Parsed Args:") print(" Char 1: \(char1Path)") print(" Char 2: \(char2Path)") print(" Ratio: \(targetRatio)") print(" Qty: \(targetQuantity)") print(" Prefix: \(prefixName)") print(" Agent: \(targetAgent)") print(" Prompt: \(promptText)") // MARK: - 240-second Watchdog Timer DispatchQueue.global().asyncAfter(deadline: .now() + 240.0) { print("ā±ļø Watchdog timeout (240s) reached. Exiting cleanly.") exit(0) } // MARK: - Resilient Helper Functions /// Safely extracts (x, y) coordinates from CDP evaluation result. /// Handles flat dictionary, nested `value`, and recursive `result` structures. func unwrapCoordinates(_ dict: [String: Any]) -> (Double, Double)? { // 1. Check for direct flat x, y if let x = (dict["x"] as? NSNumber)?.doubleValue ?? (dict["x"] as? Double), let y = (dict["y"] as? NSNumber)?.doubleValue ?? (dict["y"] as? Double) { return (x, y) } // 2. Check for nested "value" dictionary if let value = dict["value"] as? [String: Any], let x = (value["x"] as? NSNumber)?.doubleValue ?? (value["x"] as? Double), let y = (value["y"] as? NSNumber)?.doubleValue ?? (value["y"] as? Double) { return (x, y) } // 3. Check for "result" dictionary and recurse if let result = dict["result"] as? [String: Any] { return unwrapCoordinates(result) } return nil } /// Dismisses modals, drawers, and backdrop overlays. /// Checks for close buttons, backdrop overlays, and dispatches Escape key. func dismissModalIfPresent(engine: BrowserCDPEngine) async throws { let dismissScript = """ (() => { // 1. Close button with absolute positioning (slide-over drawers) let closeBtn = document.querySelector('button.absolute.right-4.top-4'); // 2. Buttons with "Close" text or sr-only span if (!closeBtn) { closeBtn = Array.from(document.querySelectorAll('button')).find(b => { const txt = (b.innerText || '').trim().toLowerCase(); const srOnly = b.querySelector('span.sr-only')?.textContent?.trim().toLowerCase(); return txt === 'close' || srOnly === 'close'; }); } // 3. Backdrop overlay click (click outside modal to dismiss) const backdrop = document.querySelector('[role="dialog"] + div, .fixed.inset-0.bg-black\\/50, .fixed.inset-0.bg-opacity-50'); if (closeBtn) { closeBtn.click(); return { action: 'closeBtn', success: true }; } else if (backdrop) { // Click backdrop to dismiss backdrop.click(); return { action: 'backdrop', success: true }; } return { action: 'none', success: false }; })() """ _ = try await engine.evaluateJavaScript(dismissScript) // Also dispatch Escape key via CDP as a fallback let escapeScript = """ (() => { const event = new KeyboardEvent('keydown', { key: 'Escape', code: 'Escape', keyCode: 27, which: 27, bubbles: true }); document.dispatchEvent(event); const keyup = new KeyboardEvent('keyup', { key: 'Escape', code: 'Escape', keyCode: 27, which: 27, bubbles: true }); document.dispatchEvent(keyup); })() """ _ = try await engine.evaluateJavaScript(escapeScript) try await Task.sleep(nanoseconds: 500_000_000) } // MARK: - Main Pipeline _ = Task { print("šŸš€ Running Multi-Character Batch Pipeline (Quantity: \(targetQuantity), Ratio: \(targetRatio))...") let engine = BrowserCDPEngine(host: "127.0.0.1", port: 9222) let targets = try await engine.discoverTargets() guard let target = targets.first(where: { $0.url.contains("artistly.ai") && $0.type == "page" }) ?? targets.first(where: { $0.type == "page" }), let wsUrl = target.webSocketDebuggerUrl else { print("āŒ Active Artistly tab not found on port 9222.") exit(1) } try await engine.connect(targetWebSocketUrl: wsUrl) // Step 0: Window Focus Activation - Target.activateTarget to ensure physical mouse events register print("šŸŽÆ Activating target window for physical event registration...") _ = try await engine.sendCommand(method: "Target.activateTarget", params: ["targetId": target.id]) try await Task.sleep(nanoseconds: 500_000_000) // Step 0 (cont): Dismiss any initial drawers try await dismissModalIfPresent(engine: engine) // Step 1: Navigate to Consistent Characters print("🌐 Navigating to https://app.artistly.ai/ai/consistent-characters...") _ = try await engine.sendCommand( method: "Page.navigate", params: ["url": "https://app.artistly.ai/ai/consistent-characters"] ) try await Task.sleep(nanoseconds: 3_000_000_000) try await dismissModalIfPresent(engine: engine) // Step 2: Select Multi-consistent Characters category card // Zero-regex selector: query all candidate cards, find one whose lowercase text // contains both "multi" and "consistent". Fallback to 3rd card at index 2. print("šŸ–±ļø Selecting 'Multi-consistent Characters' Category...") let cardScript = """ (() => { // Broad query for all candidate card elements const cards = Array.from(document.querySelectorAll('div.group, .cursor-pointer, [class*="group"]')); let matchedCard = null; // Primary: find card whose text contains both "multi" and "consistent" for (const card of cards) { const rawText = (card.innerText || card.textContent || '').trim(); if (!rawText) continue; const t = rawText.toLowerCase(); if (t.includes('multi') && t.includes('consistent')) { matchedCard = card; break; } } // Fallback: if no text match, take the 3rd card (index 2) if (!matchedCard && cards.length >= 3) { matchedCard = cards[2]; } if (!matchedCard) return null; matchedCard.scrollIntoView({ behavior: 'instant', block: 'center' }); // Target the inner circular icon element (.rounded-full, svg, .p-6) const iconEl = matchedCard.querySelector('.rounded-full, svg, .p-6') || matchedCard; const r = iconEl.getBoundingClientRect(); return { x: r.left + r.width / 2, y: r.top + r.height / 2 }; })() """ let cardRes = try await engine.evaluateJavaScript(cardScript) if let coords = unwrapCoordinates(cardRes as? [String: Any] ?? [:]) { try await engine.click(x: coords.0, y: coords.1) print(" āœ… Clicked category card at (\(Int(coords.0)), \(Int(coords.1)))") try await Task.sleep(nanoseconds: 2_500_000_000) } else { print("āŒ Could not locate Multi-consistent Characters card") exit(1) } try await dismissModalIfPresent(engine: engine) // Step 3: Inject Multi-Slot Anchors via DOM.setFileInputFiles print("šŸ“ Injecting Slot 1: \(char1Path) and Slot 2: \(char2Path)...") _ = try await engine.sendCommand(method: "DOM.enable") let docRes = try await engine.sendCommand(method: "DOM.getDocument", params: ["depth": -1]) guard let root = docRes["root"] as? [String: Any], let rootNodeId = (root["nodeId"] as? Int) ?? (root["nodeId"] as? NSNumber)?.intValue else { exit(1) } let queryRes = try await engine.sendCommand( method: "DOM.querySelectorAll", params: ["nodeId": rootNodeId, "selector": "input.filepond--browser, input[type='file']"] ) guard let nodeIds = queryRes["nodeIds"] as? [Any], nodeIds.count >= 2, let id0 = (nodeIds[0] as? Int) ?? (nodeIds[0] as? NSNumber)?.intValue, let id1 = (nodeIds[1] as? Int) ?? (nodeIds[1] as? NSNumber)?.intValue else { print("āŒ Could not locate 2 file input nodes in DOM.") exit(1) } _ = try await engine.sendCommand(method: "DOM.setFileInputFiles", params: ["files": [char1Path], "nodeId": id0]) _ = try await engine.sendCommand(method: "DOM.setFileInputFiles", params: ["files": [char2Path], "nodeId": id1]) // Wait for both uploads to settle and textarea to mount print("ā³ Waiting for uploads to settle and textarea to mount...") let waitUploadScript = """ (() => { const busyPonds = Array.from(document.querySelectorAll('.filepond--item, [data-filepond-item-state]')).some(el => { const state = (el.getAttribute('data-filepond-item-state') || '').toLowerCase(); return state.includes('processing') || state.includes('busy') || state.includes('loading'); }); const textarea = document.querySelector('.prompt-field textarea, textarea[placeholder*="garden" i], textarea'); return { ready: !busyPonds && textarea !== null && textarea.getBoundingClientRect().width > 0 }; })() """ for _ in 1...20 { try await Task.sleep(nanoseconds: 500_000_000) let checkRes = try await engine.evaluateJavaScript(waitUploadScript) if let ready = (checkRes["result"] as? [String: Any])?["value"] as? [String: Any], (ready["ready"] as? Bool) == true { break } } try await dismissModalIfPresent(engine: engine) // Step 4: Configure Dimension and Quantity Dropdowns print("āš™ļø Setting Ratio: \(targetRatio) and Quantity: \(targetQuantity)...") let configSelectsScript = """ (() => { const selects = Array.from(document.querySelectorAll('select')); if (selects.length > 0) { const dimSelect = selects[0]; const opt = Array.from(dimSelect.options).find(o => o.value.includes('\(targetRatio)') || o.text.includes('\(targetRatio)') ); if (opt) { dimSelect.value = opt.value; dimSelect.dispatchEvent(new Event('input', { bubbles: true })); dimSelect.dispatchEvent(new Event('change', { bubbles: true })); } } if (selects.length > 1) { const qtySelect = selects[1]; const opt = Array.from(qtySelect.options).find(o => o.value === '\(targetQuantity)' || o.text.trim() === '\(targetQuantity)' ); if (opt) { qtySelect.value = opt.value; qtySelect.dispatchEvent(new Event('input', { bubbles: true })); qtySelect.dispatchEvent(new Event('change', { bubbles: true })); } } })() """ _ = try await engine.evaluateJavaScript(configSelectsScript) // Step 5: Inject Prompt into Textarea (React descriptor setter) func jsonQuote(_ str: String) -> String { let data = (try? JSONSerialization.data(withJSONObject: [str])) ?? Data() let jsonStr = String(data: data, encoding: .utf8) ?? "[\"\"]" return String(jsonStr.dropFirst().dropLast()) } let injectPromptScript = """ (() => { const textarea = document.querySelector('.prompt-field textarea, textarea'); if (!textarea) return false; textarea.scrollIntoView({ behavior: 'instant', block: 'center' }); textarea.focus(); const desc = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value'); if (desc && desc.set) { desc.set.call(textarea, \(jsonQuote(promptText))); } else { textarea.value = \(jsonQuote(promptText)); } textarea.dispatchEvent(new Event('input', { bubbles: true })); textarea.dispatchEvent(new Event('change', { bubbles: true })); return true; })() """ _ = try await engine.evaluateJavaScript(injectPromptScript) try await dismissModalIfPresent(engine: engine) // Step 6: (Removed old snapshot approach - replaced by design ID tracking in Step 8) // Step 7: Form Submission & Button Click // Locate the Generate Characters button (or #generate_image_flux) // Scroll into center view, measure exact bounding box, dispatch physical coordinate click // Dispatch a synthetic .click() as immediate fallback to guarantee form dispatch // Wait 4 seconds for React state submission to settle print("šŸ”˜ Locating and clicking 'Generate Characters' button...") let clickGenScript = """ (() => { let btn = document.querySelector('#generate_image_flux') || Array.from(document.querySelectorAll('button')).find(b => (b.innerText || '').trim().toLowerCase().includes('generate characters') ); if (!btn) return null; btn.scrollIntoView({ behavior: 'instant', block: 'center' }); const r = btn.getBoundingClientRect(); return { x: r.left + r.width / 2, y: r.top + r.height / 2 }; })() """ let genRes = try await engine.evaluateJavaScript(clickGenScript) if let coords = unwrapCoordinates(genRes as? [String: Any] ?? genRes["result"] as? [String: Any] ?? [:]) { print("šŸš€ Clicking 'Generate Characters' at (\(Int(coords.0)), \(Int(coords.1)))...") try await engine.click(x: coords.0, y: coords.1) // Also dispatch synthetic click as fallback to guarantee React form dispatch let syntheticClickScript = """ (() => { const btn = document.querySelector('#generate_image_flux') || Array.from(document.querySelectorAll('button')).find(b => (b.innerText || '').trim().toLowerCase().includes('generate characters') ); if (btn) { btn.click(); return true; } return false; })() """ _ = try await engine.evaluateJavaScript(syntheticClickScript) // Wait 4 seconds for React state submission to settle print("ā³ Waiting 4s for form submission to settle...") try await Task.sleep(nanoseconds: 4_000_000_000) } else { print("āŒ Could not locate Generate Characters button") exit(1) } // Step 8: Streamlined Gallery Polling & Early Harvesting let expectedCount = Int(targetQuantity) ?? 1 let scenesDir = "/Volumes/FACTORY_SSD/warezfactory/Art_Cannon/Scenes" try? FileManager.default.createDirectory(atPath: scenesDir, withIntermediateDirectories: true) // Navigate to gallery print("🌐 Navigating to Personal Designs gallery...") _ = try await engine.sendCommand( method: "Page.navigate", params: ["url": "https://app.artistly.ai/personal-designs"] ) try await Task.sleep(nanoseconds: 3_000_000_000) try await dismissModalIfPresent(engine: engine) // Poll every 3 seconds for up to 60 seconds (20 iterations) var harvestedItems: [[String: String]] = [] // array of {designId, cdnUrl, href} let maxAttempts = 20 // 60 seconds / 3 seconds var iterationCounter = 0 for attempt in 1...maxAttempts { try await Task.sleep(nanoseconds: 3_000_000_000) iterationCounter += 1 // Periodic Gallery Reload: every 3 iterations (~9-10s) to ensure fresh server-rendered cards mount if iterationCounter % 3 == 0 { print("šŸ”„ [\(attempt * 3)s] Periodic gallery reload (iteration \(iterationCounter))...") _ = try await engine.sendCommand(method: "Page.reload", params: ["ignoreCache": true]) // Allow React to re-mount freshly fetched card elements try await Task.sleep(nanoseconds: 1_500_000_000) try await dismissModalIfPresent(engine: engine) } let pollScript = """ (() => { // Check if DOM indicates ongoing generation const isProcessing = Array.from(document.querySelectorAll('*')).some(el => { const src = (el.src || '').toLowerCase(); const alt = (el.alt || '').toLowerCase(); const txt = (el.innerText || '').toLowerCase(); return src.includes('placehold.co') || src.includes('processing') || alt.includes('processing') || txt.startsWith('processing') || txt.includes('generating'); }); // Query all design cards const cards = Array.from(document.querySelectorAll('a[href*="ai-image-modifier?design="]')); const items = cards.map(card => { const href = card.getAttribute('href') || ''; const designIdMatch = href.match(/design=([^&]+)/); const designId = designIdMatch ? designIdMatch[1] : ''; // Resolve parent card container and extract clean image URL const container = card.closest('.relative') || card.parentElement; const img = container ? container.querySelector('img') : null; const cdnUrl = img ? (img.currentSrc || img.src || '').split('?')[0] : ''; return { designId, cdnUrl, href }; }).filter(item => item.designId && item.cdnUrl); return { isProcessing, items }; })() """ let pollRes = try await engine.evaluateJavaScript(pollScript) guard let pVal = (pollRes["result"] as? [String: Any])?["value"] as? [String: Any], let isProcessing = pVal["isProcessing"] as? Bool, let allItems = pVal["items"] as? [[String: Any]] else { continue } // Filter items with valid designId and non-empty cdnUrl var validItems: [[String: String]] = [] for item in allItems { if let designId = item["designId"] as? String, let cdnUrl = item["cdnUrl"] as? String, let href = item["href"] as? String, !designId.isEmpty, !cdnUrl.isEmpty { validItems.append(["designId": designId, "cdnUrl": cdnUrl, "href": href]) } } print("[\(attempt * 3)s] Processing: \(isProcessing) | Valid Items: \(validItems.count)/\(expectedCount)") // Early Exit: if not processing and we have at least expectedCount valid items, // capture the top expectedCount cards directly and break immediately if !isProcessing && validItems.count >= expectedCount { harvestedItems = Array(validItems.prefix(expectedCount)) print("āœ… Batch complete: \(harvestedItems.count) items harvested (early exit at \(attempt * 3)s)") break } // If last iteration and we have some items but not enough, take what we have if attempt == maxAttempts && !validItems.isEmpty { harvestedItems = Array(validItems.prefix(expectedCount)) print("āš ļø Polling timeout: harvested \(harvestedItems.count) items after 60s") break } } if harvestedItems.isEmpty { print("āš ļø No items harvested after polling timeout. Exiting.") exit(1) } // Step 9: Asset Harvesting & Metadata // For each harvested item, download clean uncompressed master PNG // Generate 512px preview PNG (.preview.png) // Write companion _metadata.json including design_id, scene_name, cdn_url, dimensions, prompt // Dispatch inbound notification var previewPaths: [String] = [] for (i, item) in harvestedItems.enumerated() { let vIndex = i + 1 let designId = item["designId"] ?? "" let cdnUrl = item["cdnUrl"] ?? "" let masterPath = "\(scenesDir)/\(prefixName)_v\(vIndex).png" let previewPath = "\(scenesDir)/\(prefixName)_v\(vIndex).preview.png" print("šŸ“„ Downloading Variation #\(vIndex) (designId: \(designId))....") let finalURL = try await engine.downloadAsset(from: cdnUrl, to: masterPath) print(" • Saved Master: \(finalURL.path)") // Get dimensions from the downloaded image var dimensions = "1024x1024" if let source = CGImageSourceCreateWithURL(finalURL as CFURL, nil), let cgImage = CGImageSourceCreateImageAtIndex(source, 0, nil) { dimensions = "\(cgImage.width)x\(cgImage.height)" // Generate 512px preview let targetSize: CGFloat = 512.0 let colorSpace = CGColorSpaceCreateDeviceRGB() if let context = CGContext(data: nil, width: Int(targetSize), height: Int(targetSize), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) { context.interpolationQuality = .high context.draw(cgImage, in: CGRect(x: 0, y: 0, width: targetSize, height: targetSize)) if let scaledCG = context.makeImage() { let rep = NSBitmapImageRep(cgImage: scaledCG) if let pngData = rep.representation(using: .png, properties: [:]) { try? pngData.write(to: URL(fileURLWithPath: previewPath)) previewPaths.append(previewPath) print(" • Generated Preview: \(previewPath)") } } } } // Write companion metadata JSON let metaDict: [String: Any] = [ "design_id": designId, "scene_name": "\(prefixName) (Variation \(vIndex))", "characters": ["Willow", "Arlo"], "tool_used": "Multi-consistent Characters (\(targetQuantity)-up Batch)", "dimensions": dimensions, "aspect_ratio": targetRatio, "created_at": ISO8601DateFormatter().string(from: Date()), "cdn_url": cdnUrl, "prompt": promptText ] if let data = try? JSONSerialization.data(withJSONObject: metaDict, options: [.prettyPrinted]) { try? data.write(to: URL(fileURLWithPath: "\(scenesDir)/\(prefixName)_v\(vIndex)_metadata.json")) print(" • Written Metadata: \(scenesDir)/\(prefixName)_v\(vIndex)_metadata.json") } } // Post inbound notification to target agent if let primaryPreview = previewPaths.first { let inboundPayload: [String: Any] = [ "target_agent": targetAgent, "message": "šŸŽØ Batch Render Complete: \(prefixName) (\(harvestedItems.count) variations)\nšŸ“ Scenes: \(scenesDir)", "image_path": primaryPreview ] DistributedNotificationCenter.default().postNotificationName( NSNotification.Name("com.higginsoft.factory.agent.inbound"), object: targetAgent, userInfo: inboundPayload, deliverImmediately: true ) print("šŸ“¤ Dispatched inbound notification to \(targetAgent)") } await engine.disconnect() exit(0) } RunLoop.main.run()