@inflatable-cookie/longhorn 0.2.0 → 0.2.1

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/README.md CHANGED
@@ -15,7 +15,7 @@ Every domain is a subpath. `@inflatable-cookie/longhorn` itself resolves to
15
15
  | Subpath | Domain |
16
16
  | --- | --- |
17
17
  | `/core` | identity, opaque values, transports, diagnostics |
18
- | `/agent-control` | in-page semantic shim (contract 022 control surface). Synthetic events are untrusted (`isTrusted` stays false); `wait_for` is DOM-relative. |
18
+ | `/agent-control` | in-page semantic shim (contract 022 control surface). Synthetic events are untrusted (`isTrusted` stays false); `wait_for` is DOM-relative. `bindFileSelection` is the plugin-dialog replacement at picker call sites. |
19
19
  | `/config` | configuration store, backup, restore, recovery |
20
20
  | `/layout` | containers, regions, panels, sizing |
21
21
  | `/surfaces`, `/surface-transfer` | optional Surface documents and their transfer |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inflatable-cookie/longhorn",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -18,3 +18,16 @@ export {
18
18
  type ToolErrorBody,
19
19
  type WaitPredicate,
20
20
  } from "./shim.ts";
21
+ export {
22
+ BEGIN_SELECTION_COMMAND,
23
+ SELECTION_RESOURCE_URI,
24
+ bindFileSelection,
25
+ type DialogFilter,
26
+ type FileSelectionApi,
27
+ type FileSelectionPorts,
28
+ type FileSelectionWorld,
29
+ type OpenDialogOptions,
30
+ type OpenDialogResult,
31
+ type SaveDialogOptions,
32
+ type SaveDialogResult,
33
+ } from "./selection.ts";
@@ -0,0 +1,94 @@
1
+ //! Consumer call-site replacement for plugin-dialog `open`/`save`.
2
+ //!
3
+ //! Agent-originated calls publish a pending selection before any OS panel
4
+ //! opens. Human-originated calls pass the original options to the plugin
5
+ //! unchanged. Origin is the in-page shim, not an active-server flag.
6
+
7
+ import { SHIM_GLOBAL, type AgentControlApi } from "./shim.ts";
8
+
9
+ /** Renderer invoke name. Host-protocol pairs this with the Rust command. */
10
+ export const BEGIN_SELECTION_COMMAND = "longhorn_agent_control_begin_selection";
11
+
12
+ /** Pending-selection MCP resource. */
13
+ export const SELECTION_RESOURCE_URI = "longhorn://agent-control/selection";
14
+
15
+ export type DialogFilter = {
16
+ name: string;
17
+ extensions: string[];
18
+ };
19
+
20
+ export type OpenDialogOptions = {
21
+ title?: string;
22
+ defaultPath?: string;
23
+ filters?: DialogFilter[];
24
+ multiple?: boolean;
25
+ directory?: boolean;
26
+ };
27
+
28
+ export type SaveDialogOptions = {
29
+ title?: string;
30
+ defaultPath?: string;
31
+ filters?: DialogFilter[];
32
+ };
33
+
34
+ export type OpenDialogResult = string | string[] | null;
35
+ export type SaveDialogResult = string | null;
36
+
37
+ export type FileSelectionPorts = {
38
+ invoke: (command: string, args: Record<string, unknown>) => Promise<unknown>;
39
+ pluginOpen: (options?: OpenDialogOptions) => Promise<OpenDialogResult>;
40
+ pluginSave: (options?: SaveDialogOptions) => Promise<SaveDialogResult>;
41
+ };
42
+
43
+ export type FileSelectionWorld = {
44
+ readonly [SHIM_GLOBAL]?: Pick<AgentControlApi, "isAgentOriginated">;
45
+ };
46
+
47
+ export type FileSelectionApi = {
48
+ open: (options?: OpenDialogOptions) => Promise<OpenDialogResult>;
49
+ save: (options?: SaveDialogOptions) => Promise<SaveDialogResult>;
50
+ };
51
+
52
+ function isAgentOriginated(world: FileSelectionWorld): boolean {
53
+ return world[SHIM_GLOBAL]?.isAgentOriginated?.() === true;
54
+ }
55
+
56
+ function beginOptions(
57
+ kind: "open" | "save",
58
+ options: OpenDialogOptions | SaveDialogOptions | undefined,
59
+ ): Record<string, unknown> {
60
+ const open = kind === "open" ? (options as OpenDialogOptions | undefined) : undefined;
61
+ return {
62
+ kind,
63
+ directory: open?.directory ?? false,
64
+ multiple: open?.multiple ?? false,
65
+ filters: options?.filters ?? [],
66
+ title: options?.title,
67
+ defaultPath: options?.defaultPath,
68
+ };
69
+ }
70
+
71
+ /**
72
+ * Binds plugin-compatible `open`/`save`. Call at consumer picker sites;
73
+ * do not monkey-patch plugin-dialog.
74
+ */
75
+ export function bindFileSelection(
76
+ ports: FileSelectionPorts,
77
+ world: object = globalThis,
78
+ ): FileSelectionApi {
79
+ const originWorld = world as FileSelectionWorld;
80
+ return {
81
+ open: async (options?: OpenDialogOptions) => {
82
+ if (!isAgentOriginated(originWorld)) return ports.pluginOpen(options);
83
+ return (await ports.invoke(BEGIN_SELECTION_COMMAND, {
84
+ options: beginOptions("open", options),
85
+ })) as OpenDialogResult;
86
+ },
87
+ save: async (options?: SaveDialogOptions) => {
88
+ if (!isAgentOriginated(originWorld)) return ports.pluginSave(options);
89
+ return (await ports.invoke(BEGIN_SELECTION_COMMAND, {
90
+ options: beginOptions("save", options),
91
+ })) as SaveDialogResult;
92
+ },
93
+ };
94
+ }
@@ -98,6 +98,8 @@ export type AgentControlApi = {
98
98
  ) => ShimResult<Record<string, never>>;
99
99
  drag: (source: string, target: string) => ShimResult<Record<string, never>>;
100
100
  waitFor: (predicate: WaitPredicate) => ShimResult<{ holds: boolean }>;
101
+ markAgentOrigin: () => void;
102
+ isAgentOriginated: () => boolean;
101
103
  readEvents: (sinceSeq?: number) => {
102
104
  events: PageEvent[];
103
105
  nextSeq: number;
@@ -114,7 +116,11 @@ export type ShimWorld = {
114
116
  KeyboardEvent: typeof KeyboardEvent;
115
117
  Event: typeof Event;
116
118
  InputEvent?: typeof InputEvent;
117
- addEventListener: (type: string, listener: (event: Event) => void) => void;
119
+ addEventListener: (
120
+ type: string,
121
+ listener: (event: Event) => void,
122
+ options?: boolean | { capture?: boolean },
123
+ ) => void;
118
124
  onunhandledrejection?: ((event: Event) => void) | null;
119
125
  } & Record<string, unknown>;
120
126
 
@@ -703,18 +709,56 @@ function installEventRing(world: ShimWorld): AgentControlApi["readEvents"] {
703
709
  });
704
710
  }
705
711
 
712
+ function installOriginTracking(world: ShimWorld): Pick<
713
+ AgentControlApi,
714
+ "markAgentOrigin" | "isAgentOriginated"
715
+ > {
716
+ let agentOriginSeq = 0;
717
+ let humanOriginSeq = 0;
718
+ const markHuman = (event: Event) => {
719
+ if (event.isTrusted) humanOriginSeq = agentOriginSeq + 1;
720
+ };
721
+ world.addEventListener("pointerdown", markHuman, true);
722
+ world.addEventListener("keydown", markHuman, true);
723
+ world.addEventListener("click", markHuman, true);
724
+ return {
725
+ markAgentOrigin: () => {
726
+ agentOriginSeq = humanOriginSeq + 1;
727
+ },
728
+ isAgentOriginated: () => agentOriginSeq > humanOriginSeq,
729
+ };
730
+ }
731
+
706
732
  export function installAgentControlShim(world: ShimWorld): AgentControlApi {
707
733
  const existing = world[SHIM_GLOBAL] as AgentControlApi | undefined;
708
734
  if (existing) return existing;
709
735
  const readEvents = installEventRing(world);
736
+ const origin = installOriginTracking(world);
710
737
  const api: AgentControlApi = {
711
738
  snapshot: () => snapshot(world),
712
- click: (element) => click(world, element),
713
- type: (element, text) => typeInto(world, element, text),
714
- press: (key, modifiers, element) => press(world, key, modifiers, element),
715
- scroll: (deltaX, deltaY, element) => scroll(world, deltaX, deltaY, element),
716
- drag: (source, target) => drag(world, source, target),
739
+ click: (element) => {
740
+ origin.markAgentOrigin();
741
+ return click(world, element);
742
+ },
743
+ type: (element, text) => {
744
+ origin.markAgentOrigin();
745
+ return typeInto(world, element, text);
746
+ },
747
+ press: (key, modifiers, element) => {
748
+ origin.markAgentOrigin();
749
+ return press(world, key, modifiers, element);
750
+ },
751
+ scroll: (deltaX, deltaY, element) => {
752
+ origin.markAgentOrigin();
753
+ return scroll(world, deltaX, deltaY, element);
754
+ },
755
+ drag: (source, target) => {
756
+ origin.markAgentOrigin();
757
+ return drag(world, source, target);
758
+ },
717
759
  waitFor: (predicate) => waitFor(world, predicate),
760
+ markAgentOrigin: origin.markAgentOrigin,
761
+ isAgentOriginated: origin.isAgentOriginated,
718
762
  readEvents,
719
763
  };
720
764
  world[SHIM_GLOBAL] = api;