char-slop/ai-dots

ai dotfiles

git clone https://git.t4t.associates/char-slop/ai-dots

Charlotte Somcomputer use for windowsdc2c12a

main
5.1 KiB154 linesraw
1import { StringEnum, Type, type Static } from "@earendil-works/pi-ai";
2
3const heldButtons = Type.Array(StringEnum(["left", "middle", "right"] as const), {
4  description: "Buttons held for this action",
5});
6
7export const parameters = Type.Object(
8  {
9    actions: Type.Array(
10      Type.Union([
11        Type.Object(
12          {
13            action: StringEnum(["pointer"] as const),
14            x: Type.Integer({
15              minimum: 0,
16              maximum: 1919,
17              description: "Absolute screen x-coordinate: 0 is left, 1919 is right",
18            }),
19            y: Type.Integer({
20              minimum: 0,
21              maximum: 1079,
22              description: "Absolute screen y-coordinate: 0 is top, 1079 is bottom",
23            }),
24            buttons: Type.Optional(heldButtons),
25          },
26          { additionalProperties: false },
27        ),
28        Type.Object(
29          {
30            action: StringEnum(["scroll"] as const),
31            x: Type.Integer({
32              minimum: 0,
33              maximum: 1919,
34              description: "Absolute screen x-coordinate: 0 is left, 1919 is right",
35            }),
36            y: Type.Integer({
37              minimum: 0,
38              maximum: 1079,
39              description: "Absolute screen y-coordinate: 0 is top, 1079 is bottom",
40            }),
41            direction: StringEnum(["up", "down", "left", "right"] as const, {
42              description: "Wheel direction",
43            }),
44            buttons: Type.Optional(heldButtons),
45            ticks: Type.Optional(
46              Type.Integer({
47                minimum: 1,
48                default: 1,
49                description: "Wheel notches; omit for one",
50              }),
51            ),
52          },
53          { additionalProperties: false },
54        ),
55        Type.Object(
56          {
57            action: StringEnum(["type"] as const),
58            text: Type.String({
59              description:
60                "Literal text to type; use a key action for named keys and shortcuts. On Wayland, characters missing from the remote keymap are dropped, not transliterated. Windows supports Unicode independently of the keyboard layout",
61            }),
62          },
63          { additionalProperties: false },
64        ),
65        Type.Object(
66          {
67            action: StringEnum(["key"] as const),
68            key: Type.String({
69              description:
70                "One XKB-style key name, such as Return, Escape, Tab, BackSpace, Delete, Left, Page_Down, F5, or a. Put shortcut modifiers in modifiers; do not put them in key",
71              pattern: "^[A-Za-z0-9_]+$",
72            }),
73            modifiers: Type.Optional(
74              Type.Array(StringEnum(["shift", "ctrl", "alt", "logo"] as const), {
75                description: "Modifiers held while pressing the key",
76                uniqueItems: true,
77              }),
78            ),
79          },
80          { additionalProperties: false },
81        ),
82        Type.Object(
83          {
84            action: StringEnum(["new_browser_tab"] as const),
85            url: Type.String({
86              description:
87                "HTTP or HTTPS URL to open in a new Firefox tab every time on Wayland, or in the default browser on Windows (which controls tab reuse); prefer web_fetch for ordinary page reading",
88              pattern: "^https?://",
89            }),
90          },
91          { additionalProperties: false },
92        ),
93        Type.Object(
94          {
95            action: StringEnum(["recall"] as const),
96            id: Type.String({
97              description: "ID from an earlier screenshot result",
98              pattern: "^sc_[0-9a-f]{16}$",
99            }),
100          },
101          { additionalProperties: false },
102        ),
103        Type.Object(
104          { action: StringEnum(["screenshot"] as const) },
105          { additionalProperties: false },
106        ),
107        Type.Object(
108          {
109            action: StringEnum(["sleep"] as const),
110            ms: Type.Integer({
111              minimum: 0,
112              maximum: 30_000,
113              description: "Time to wait, in milliseconds",
114            }),
115          },
116          { additionalProperties: false },
117        ),
118      ]),
119      { minItems: 1, maxItems: 20 },
120    ),
121  },
122  { additionalProperties: false },
123);
124
125export type Action = Static<typeof parameters>["actions"][number];
126export type DesktopAction = Exclude<Action, { action: "recall" | "sleep" }>;
127
128export interface DesktopBackend {
129  execute(action: DesktopAction, signal?: AbortSignal): Promise<Buffer | void>;
130  close(): Promise<void>;
131}
132
133export function checkSignal(signal?: AbortSignal): void {
134  if (signal?.aborted) throw new Error("Operation aborted");
135}
136
137export function wait(ms: number, signal?: AbortSignal): Promise<void> {
138  return new Promise((resolve, reject) => {
139    if (signal?.aborted) {
140      reject(new Error("Operation aborted"));
141      return;
142    }
143
144    const timer = setTimeout(() => {
145      signal?.removeEventListener("abort", abort);
146      resolve();
147    }, ms);
148    const abort = () => {
149      clearTimeout(timer);
150      reject(new Error("Operation aborted"));
151    };
152    signal?.addEventListener("abort", abort, { once: true });
153  });
154}