// 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") var targetAgent = "Annie 2.0" var promptText = "" if args.count >= 7 { let potentialAgent = args[5] if potentialAgent.contains("Annie") || potentialAgent.contains("Bob") || potentialAgent.contains("annie") || potentialAgent.contains("bob") { 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 both flat dictionary and nested `result.value` structures. func unwrapCoordinates(_ dict: [String: Any]) -> (Double, Double)? { // Try direct flat keys first 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) } // Try nested result.value if let result = dict["result"] as? [String: Any], let value = result["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) } // Try cJson.value structure if let cJson = dict["result"] as? [String: Any], let cVal = cJson["value"] as? [String: Any], let x = (cVal["x"] as? NSNumber)?.doubleValue ?? (cVal["x"] as? Double), let y = (cVal["y"] as? NSNumber)?.doubleValue ?? (cVal["y"] as? Double) { return (x, y) } 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 }; })() """ let result = 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) try await Task.sleep(nanoseconds: 500_000_000) // Step 0: 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 print("šŸ–±ļø Selecting 'Multi-consistent Characters' Category...") let cardScript = """ (() => { const all = Array.from(document.querySelectorAll('*')); // Match text exactly (case-insensitive) const textEl = all.find(e => e.children.length === 0 && e.innerText && e.innerText.trim().toLowerCase() === 'multi-consistent characters' ); if (!textEl) return { found: false }; // Find the card container (cursor-pointer, group, or flex-col) const card = textEl.closest('.cursor-pointer, .group, .flex-col, div[class*="card"]') || textEl.parentElement; card.scrollIntoView({ behavior: 'instant', block: 'center' }); // Target the upper third / inner icon container to avoid 134px flex dead-zone // Prefer the icon wrapper or the card itself if no distinct icon const iconDiv = card.querySelector('div.rounded-full, div.absolute, svg, img, div[class*="icon"]') || card; const iconRect = iconDiv.getBoundingClientRect(); // Click in upper third of icon area const clickX = iconRect.left + iconRect.width / 2; const clickY = iconRect.top + iconRect.height / 3; return { found: true, x: clickX, y: clickY }; })() """ let cardRes = try await engine.evaluateJavaScript(cardScript) if let coords = unwrapCoordinates(cardRes["result"] as? [String: Any] ?? cardRes), cardRes["found"] as? Bool ?? false { 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: Snapshot current image URLs to filter new renders let snapshotScript = "Array.from(document.querySelectorAll('img[src*=\"user-assets\"]')).map(i => i.src.split('?')[0])" let snapRes = try await engine.evaluateJavaScript(snapshotScript) var knownURLs = Set() if let arr = (snapRes["result"] as? [String: Any])?["value"] as? [String] { knownURLs = Set(arr) } // Step 7: Click Generate Characters button (physical coordinate click) 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["result"] as? [String: Any] ?? genRes) { print("šŸš€ Clicking 'Generate Characters' at (\(Int(coords.0)), \(Int(coords.1)))...") try await engine.click(x: coords.0, y: coords.1) } else { print("āŒ Could not locate Generate Characters button") exit(1) } // Step 8: Poll Personal Designs for Batch Completion let expectedCount = Int(targetQuantity) ?? 1 let scenesDir = "/Volumes/FACTORY_SSD/warezfactory/Art_Cannon/Scenes" try? FileManager.default.createDirectory(atPath: scenesDir, withIntermediateDirectories: true) // Wait for form submission/redirect try await Task.sleep(nanoseconds: 3_000_000_000) // Explicitly 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) let galleryBatchWatcherScript = """ (() => { 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'); }); const imgs = Array.from(document.querySelectorAll('img')).filter(img => { const s = (img.src || '').toLowerCase(); return s.startsWith('http') && !s.includes('settings') && !s.includes('logo') && !s.includes('avatar') && !s.includes('icon') && !s.includes('placeholder') && !s.includes('placehold.co') && (s.includes('user-assets') || s.includes('multiple-characters') || s.includes('3d-2d-style-images') || s.includes('prompt-to-image') || s.includes('consistent-characters')); }); return { isProcessing: isProcessing, images: imgs.map(i => i.src.split('?')[0]) }; })() """ var harvestedURLs: [String] = [] for attempt in 1...30 { try await Task.sleep(nanoseconds: 3_000_000_000) let pollRes = try await engine.evaluateJavaScript(galleryBatchWatcherScript) if let pVal = (pollRes["result"] as? [String: Any])?["value"] as? [String: Any] { let isProcessing = pVal["isProcessing"] as? Bool ?? false let allImgs = pVal["images"] as? [String] ?? [] let newBatch = allImgs.filter { !knownURLs.contains($0) } print("[\(attempt * 3)s] Processing: \(isProcessing) | New Batch Items: \(newBatch.count)/\(expectedCount)") if !isProcessing && newBatch.count >= expectedCount { harvestedURLs = Array(newBatch.prefix(expectedCount)) break } else if !isProcessing && attempt >= 12 && newBatch.count > 0 { harvestedURLs = newBatch break } } } // Step 9: Harvest All Variations & Deliver Preview var previewPaths: [String] = [] for (i, urlStr) in harvestedURLs.enumerated() { let vIndex = i + 1 let masterPath = "\(scenesDir)/\(prefixName)_v\(vIndex).png" let previewPath = "\(scenesDir)/\(prefixName)_v\(vIndex).preview.png" let finalURL = try await engine.downloadAsset(from: urlStr, to: masterPath) print(" • Saved Variation #\(vIndex): \(finalURL.path)") // Generate 512px preview if let source = CGImageSourceCreateWithURL(finalURL as CFURL, nil), let cgImage = CGImageSourceCreateImageAtIndex(source, 0, nil) { 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) } } } } // Write companion metadata JSON let metaDict: [String: Any] = [ "scene_name": "\(prefixName) (Variation \(vIndex))", "characters": ["Willow", "Arlo"], "tool_used": "Multi-consistent Characters (\(targetQuantity)-up Batch)", "dimensions": "1024x1024", "aspect_ratio": targetRatio, "created_at": ISO8601DateFormatter().string(from: Date()), "cdn_url": urlStr ] if let data = try? JSONSerialization.data(withJSONObject: metaDict, options: [.prettyPrinted]) { try? data.write(to: URL(fileURLWithPath: "\(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) (\(harvestedURLs.count) variations)\nšŸ“ Scenes: \(scenesDir)", "image_path": primaryPreview ] DistributedNotificationCenter.default().postNotificationName( NSNotification.Name("com.higginsoft.factory.agent.inbound"), object: targetAgent, userInfo: inboundPayload, deliverImmediately: true ) } await engine.disconnect() exit(0) } RunLoop.main.run()