Files
PAGIVA/scripts/chrome-keyboard.cs
T
Loma Bot Ops 6994ba1a4e Move AI to Codex CLI + ChatGPT browser workers with a durable post queue
Text generation now runs through a PM2 automation worker (codex exec) and
images through the ChatGPT Chrome extension, replacing the Anthropic API.
generateStructured keeps its signature; results are Zod-validated on the web
side and the worker is reached with a bearer secret.

- AI image assistant in the post editor: editable prompt step, then image
  (square or 3:2 only, since Instagram rejects 2:3), stored via putBlob
- Full-post queue (PostGenerationJob + migration): enqueue returns 202, FIFO
  claim under an advisory lock, heartbeat, stale-job failure, per-user
  request key, cancel while QUEUED; output is always a DRAFT with audit rows
- Separate 10 images/page/day quota; text quota excludes images
- Worker reports Codex usage-limit as 429 with a clear Thai message
- Docs: AI-AUTOMATION.md, ARCHITECTURE/README/.env.example updated

Known gap: the live FIFO queue check (scripts/check-post-queue.mjs) has not
passed yet; the last run hit the Codex account usage limit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-22 21:52:09 +07:00

76 lines
3.1 KiB
C#

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
// Native Unicode input avoids COM SendKeys timing and clipboard-paste restrictions.
public static class ChromeKeyboard
{
[DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")] private static extern uint GetWindowThreadProcessId(IntPtr h, out uint processId);
[DllImport("kernel32.dll")] private static extern uint GetCurrentThreadId();
[DllImport("user32.dll")] private static extern bool AttachThreadInput(uint source, uint target, bool attach);
[DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr h);
[DllImport("user32.dll")] private static extern bool BringWindowToTop(IntPtr h);
public static void ActivateWindow(IntPtr window)
{
if (GetForegroundWindow() == window) return;
if (SetForegroundWindow(window)) return;
uint ignored;
uint current = GetCurrentThreadId();
uint foreground = GetWindowThreadProcessId(GetForegroundWindow(), out ignored);
uint target = GetWindowThreadProcessId(window, out ignored);
bool attachedForeground = foreground != current && AttachThreadInput(current, foreground, true);
bool attachedTarget = target != current && target != foreground && AttachThreadInput(current, target, true);
try { BringWindowToTop(window); SetForegroundWindow(window); }
finally
{
if (attachedTarget) AttachThreadInput(current, target, false);
if (attachedForeground) AttachThreadInput(current, foreground, false);
}
}
[StructLayout(LayoutKind.Sequential)]
private struct KeyboardInput
{
public ushort key, scan;
public uint flags, time;
public UIntPtr extra;
}
[StructLayout(LayoutKind.Sequential)]
private struct MouseInput
{
public int x, y;
public uint data, flags, time;
public UIntPtr extra;
}
[StructLayout(LayoutKind.Explicit)]
private struct InputUnion
{
[FieldOffset(0)] public KeyboardInput keyboard;
[FieldOffset(0)] public MouseInput mouse;
}
[StructLayout(LayoutKind.Sequential)]
private struct Input
{
public uint type;
public InputUnion data;
}
[DllImport("user32.dll", SetLastError = true)]
private static extern uint SendInput(uint count, Input[] inputs, int size);
public static void TypeText(string text)
{
var inputs = new Input[text.Length * 2];
for (int i = 0; i < text.Length; i++)
{
inputs[i * 2].type = inputs[i * 2 + 1].type = 1;
inputs[i * 2].data.keyboard.scan = inputs[i * 2 + 1].data.keyboard.scan = text[i];
inputs[i * 2].data.keyboard.flags = 4;
inputs[i * 2 + 1].data.keyboard.flags = 6;
}
if (SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(Input))) != inputs.Length)
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not type the complete image prompt into Chrome.");
}
}