@inflatable-cookie/longhorn 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inflatable-cookie/longhorn",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -10,6 +10,7 @@ export {
10
10
  TRUNCATED_ROLE,
11
11
  installAgentControlShim,
12
12
  type AgentControlApi,
13
+ type FileInputFile,
13
14
  type PageEvent,
14
15
  type PageState,
15
16
  type SemanticNode,
@@ -20,8 +21,11 @@ export {
20
21
  } from "./shim.ts";
21
22
  export {
22
23
  BEGIN_SELECTION_COMMAND,
24
+ SELECTION_ORIGIN_AGENT,
25
+ SELECTION_ORIGIN_HUMAN,
23
26
  SELECTION_RESOURCE_URI,
24
27
  bindFileSelection,
28
+ currentSelectionOrigin,
25
29
  type DialogFilter,
26
30
  type FileSelectionApi,
27
31
  type FileSelectionPorts,
@@ -30,4 +34,5 @@ export {
30
34
  type OpenDialogResult,
31
35
  type SaveDialogOptions,
32
36
  type SaveDialogResult,
37
+ type SelectionOrigin,
33
38
  } from "./selection.ts";
@@ -12,6 +12,15 @@ export const BEGIN_SELECTION_COMMAND = "longhorn_agent_control_begin_selection";
12
12
  /** Pending-selection MCP resource. */
13
13
  export const SELECTION_RESOURCE_URI = "longhorn://agent-control/selection";
14
14
 
15
+ /** Wire token for agent origin. Matches the Rust `SELECTION_ORIGIN_AGENT`. */
16
+ export const SELECTION_ORIGIN_AGENT = "agent";
17
+
18
+ /** Wire token for human origin. Matches the Rust `SELECTION_ORIGIN_HUMAN`. */
19
+ export const SELECTION_ORIGIN_HUMAN = "human";
20
+
21
+ /** Serializable origin a consumer passes into its own command arguments. */
22
+ export type SelectionOrigin = typeof SELECTION_ORIGIN_AGENT | typeof SELECTION_ORIGIN_HUMAN;
23
+
15
24
  export type DialogFilter = {
16
25
  name: string;
17
26
  extensions: string[];
@@ -53,6 +62,16 @@ function isAgentOriginated(world: FileSelectionWorld): boolean {
53
62
  return world[SHIM_GLOBAL]?.isAgentOriginated?.() === true;
54
63
  }
55
64
 
65
+ /**
66
+ * Page origin as a command-argument token. Reads the installed shim;
67
+ * with no shim, or when the shim reports human, the token is `"human"`.
68
+ */
69
+ export function currentSelectionOrigin(world: object = globalThis): SelectionOrigin {
70
+ return isAgentOriginated(world as FileSelectionWorld)
71
+ ? SELECTION_ORIGIN_AGENT
72
+ : SELECTION_ORIGIN_HUMAN;
73
+ }
74
+
56
75
  function beginOptions(
57
76
  kind: "open" | "save",
58
77
  options: OpenDialogOptions | SaveDialogOptions | undefined,
@@ -82,6 +82,12 @@ export type WaitPredicate =
82
82
  | { predicate: "pageUrlContains"; needle: string }
83
83
  | { predicate: "pageTitleContains"; needle: string };
84
84
 
85
+ export type FileInputFile = {
86
+ name: string;
87
+ mediaType?: string;
88
+ contentBase64: string;
89
+ };
90
+
85
91
  export type AgentControlApi = {
86
92
  snapshot: () => ShimResult<{ page: PageState; root: SemanticNode }>;
87
93
  click: (element: string) => ShimResult<Record<string, never>>;
@@ -97,6 +103,7 @@ export type AgentControlApi = {
97
103
  element?: string | null,
98
104
  ) => ShimResult<Record<string, never>>;
99
105
  drag: (source: string, target: string) => ShimResult<Record<string, never>>;
106
+ setFileInput: (element: string, files: FileInputFile[]) => ShimResult<Record<string, never>>;
100
107
  waitFor: (predicate: WaitPredicate) => ShimResult<{ holds: boolean }>;
101
108
  markAgentOrigin: () => void;
102
109
  isAgentOriginated: () => boolean;
@@ -116,6 +123,8 @@ export type ShimWorld = {
116
123
  KeyboardEvent: typeof KeyboardEvent;
117
124
  Event: typeof Event;
118
125
  InputEvent?: typeof InputEvent;
126
+ File?: typeof File;
127
+ DataTransfer?: typeof DataTransfer;
119
128
  addEventListener: (
120
129
  type: string,
121
130
  listener: (event: Event) => void,
@@ -130,6 +139,10 @@ function unresolved(element: string): ShimResult<never> {
130
139
  return { ok: false, error: { error: "unresolvedRef", element } };
131
140
  }
132
141
 
142
+ function unsupported(message: string): ShimResult<never> {
143
+ return { ok: false, error: { error: "unsupported", message } };
144
+ }
145
+
133
146
  function ok(): ActionOk {
134
147
  return { ok: true } as ActionOk;
135
148
  }
@@ -615,6 +628,87 @@ function drag(
615
628
  return ok();
616
629
  }
617
630
 
631
+ function decodeBase64(content: string): Uint8Array<ArrayBuffer> | null {
632
+ try {
633
+ const binary = atob(content);
634
+ const bytes = new Uint8Array(binary.length);
635
+ for (let i = 0; i < binary.length; i += 1) bytes[i] = binary.charCodeAt(i);
636
+ return bytes;
637
+ } catch {
638
+ return null;
639
+ }
640
+ }
641
+
642
+ function acceptMatches(fileName: string, mediaType: string, accept: string): boolean {
643
+ const tokens = accept
644
+ .split(",")
645
+ .map((token) => token.trim())
646
+ .filter((token) => token.length > 0);
647
+ if (tokens.length === 0) return true;
648
+ const name = fileName.toLowerCase();
649
+ const type = mediaType.toLowerCase();
650
+ return tokens.some((token) => {
651
+ const needle = token.toLowerCase();
652
+ if (needle.startsWith(".")) return name.endsWith(needle);
653
+ if (needle.endsWith("/*")) return type.startsWith(needle.slice(0, -1));
654
+ return type === needle;
655
+ });
656
+ }
657
+
658
+ function setFileInput(
659
+ world: ShimWorld,
660
+ element: string,
661
+ files: FileInputFile[],
662
+ ): ShimResult<Record<string, never>> {
663
+ const node = resolveRef(world.document, element);
664
+ if (!node) return unresolved(element);
665
+ const inputType = (prop<string>(node, "type") ?? node.getAttribute("type") ?? "").toLowerCase();
666
+ if (node.tagName !== "INPUT" || inputType !== "file") {
667
+ return unsupported("set_file_input requires an input type=file");
668
+ }
669
+ if (!Array.isArray(files) || files.length === 0) {
670
+ return unsupported("set_file_input requires at least one file");
671
+ }
672
+ const multiple = prop<boolean>(node, "multiple") === true || node.hasAttribute("multiple");
673
+ if (files.length > 1 && !multiple) {
674
+ return unsupported("set_file_input received multiple files for an input without multiple");
675
+ }
676
+ const FileCtor = world.File ?? (typeof File === "function" ? File : undefined);
677
+ const DataTransferCtor =
678
+ world.DataTransfer ?? (typeof DataTransfer === "function" ? DataTransfer : undefined);
679
+ if (!FileCtor || !DataTransferCtor) {
680
+ return unsupported("set_file_input needs File and DataTransfer in the page");
681
+ }
682
+ const accept = node.getAttribute("accept") ?? "";
683
+ const transfer = new DataTransferCtor();
684
+ for (const file of files) {
685
+ if (!file.name) return unsupported("set_file_input file name must be non-empty");
686
+ const bytes = decodeBase64(file.contentBase64);
687
+ if (!bytes) {
688
+ return unsupported(`set_file_input file ${JSON.stringify(file.name)} is not valid base64`);
689
+ }
690
+ const mediaType = file.mediaType ?? "";
691
+ if (!acceptMatches(file.name, mediaType, accept)) {
692
+ return unsupported(`set_file_input file ${JSON.stringify(file.name)} does not match accept`);
693
+ }
694
+ transfer.items.add(new FileCtor([bytes], file.name, mediaType ? { type: mediaType } : undefined));
695
+ }
696
+ try {
697
+ (node as HTMLInputElement).files = transfer.files;
698
+ } catch (error) {
699
+ return unsupported(
700
+ `the page refused FileList assignment from a constructed DataTransfer: ${String(error)}`,
701
+ );
702
+ }
703
+ const assigned = prop<ArrayLike<unknown>>(node, "files");
704
+ if (!assigned || assigned.length !== files.length) {
705
+ return unsupported("the page refused FileList assignment from a constructed DataTransfer");
706
+ }
707
+ node.dispatchEvent(new world.Event("input", { bubbles: true }));
708
+ node.dispatchEvent(new world.Event("change", { bubbles: true }));
709
+ return ok();
710
+ }
711
+
618
712
  function waitFor(world: ShimWorld, predicate: WaitPredicate): ShimResult<{ holds: boolean }> {
619
713
  switch (predicate.predicate) {
620
714
  case "refResolve":
@@ -756,6 +850,10 @@ export function installAgentControlShim(world: ShimWorld): AgentControlApi {
756
850
  origin.markAgentOrigin();
757
851
  return drag(world, source, target);
758
852
  },
853
+ setFileInput: (element, files) => {
854
+ origin.markAgentOrigin();
855
+ return setFileInput(world, element, files);
856
+ },
759
857
  waitFor: (predicate) => waitFor(world, predicate),
760
858
  markAgentOrigin: origin.markAgentOrigin,
761
859
  isAgentOriginated: origin.isAgentOriginated,