@openlegion/platform 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +1189 -2
- package/dist/body.d.ts +9 -0
- package/dist/body.js +28 -0
- package/dist/chat-history.d.ts +53 -0
- package/dist/chat-history.js +11 -0
- package/dist/client.d.ts +217 -0
- package/dist/client.js +367 -0
- package/dist/core.d.ts +47 -0
- package/dist/core.js +1 -0
- package/dist/crypto.d.ts +25 -0
- package/dist/crypto.js +81 -0
- package/dist/errors.d.ts +111 -0
- package/dist/errors.js +134 -0
- package/dist/events.d.ts +131 -0
- package/dist/events.js +5 -0
- package/dist/generated/api.d.ts +5505 -0
- package/dist/generated/api.js +446 -0
- package/dist/handler.d.ts +201 -0
- package/dist/handler.js +682 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +12 -0
- package/dist/kit-agents.d.ts +95 -0
- package/dist/kit-agents.js +280 -0
- package/dist/kit-codes.d.ts +279 -0
- package/dist/kit-codes.js +44 -0
- package/dist/kit-connections.d.ts +21 -0
- package/dist/kit-connections.js +189 -0
- package/dist/kit-files.d.ts +8 -0
- package/dist/kit-files.js +117 -0
- package/dist/kit-model-keys.d.ts +8 -0
- package/dist/kit-model-keys.js +86 -0
- package/dist/kit-threads.d.ts +3 -0
- package/dist/kit-threads.js +92 -0
- package/dist/kit-tools.d.ts +21 -0
- package/dist/kit-tools.js +47 -0
- package/dist/kit.d.ts +130 -0
- package/dist/kit.js +155 -0
- package/dist/mcp-server.d.ts +27 -0
- package/dist/mcp-server.js +166 -0
- package/dist/next.d.ts +24 -0
- package/dist/next.js +6 -0
- package/dist/ol-signed.d.ts +82 -0
- package/dist/ol-signed.js +115 -0
- package/dist/react/add-server.d.ts +90 -0
- package/dist/react/add-server.js +183 -0
- package/dist/react/agent-chat.d.ts +925 -0
- package/dist/react/agent-chat.js +1917 -0
- package/dist/react/agent-editor.d.ts +133 -0
- package/dist/react/agent-editor.js +222 -0
- package/dist/react/agent-kit.d.ts +160 -0
- package/dist/react/agent-kit.js +279 -0
- package/dist/react/agent-list.d.ts +247 -0
- package/dist/react/agent-list.js +248 -0
- package/dist/react/approval-card.d.ts +272 -0
- package/dist/react/approval-card.js +372 -0
- package/dist/react/artifact-card.d.ts +38 -0
- package/dist/react/artifact-card.js +39 -0
- package/dist/react/avatar.d.ts +21 -0
- package/dist/react/avatar.js +33 -0
- package/dist/react/card-ledger.d.ts +30 -0
- package/dist/react/card-ledger.js +49 -0
- package/dist/react/chat-timing.d.ts +121 -0
- package/dist/react/chat-timing.js +229 -0
- package/dist/react/computer-view.d.ts +309 -0
- package/dist/react/computer-view.js +543 -0
- package/dist/react/computer-viewer.d.ts +175 -0
- package/dist/react/computer-viewer.js +555 -0
- package/dist/react/computer-wire.d.ts +178 -0
- package/dist/react/computer-wire.js +244 -0
- package/dist/react/connect-button.d.ts +145 -0
- package/dist/react/connect-button.js +212 -0
- package/dist/react/connection-request.d.ts +16 -0
- package/dist/react/connection-request.js +18 -0
- package/dist/react/connections-list.d.ts +90 -0
- package/dist/react/connections-list.js +76 -0
- package/dist/react/errors.d.ts +250 -0
- package/dist/react/errors.js +146 -0
- package/dist/react/files.d.ts +41 -0
- package/dist/react/files.js +78 -0
- package/dist/react/index.d.ts +35 -0
- package/dist/react/index.js +26 -0
- package/dist/react/limit-words.d.ts +80 -0
- package/dist/react/limit-words.js +53 -0
- package/dist/react/markdown.d.ts +42 -0
- package/dist/react/markdown.js +56 -0
- package/dist/react/md-parse.d.ts +106 -0
- package/dist/react/md-parse.js +638 -0
- package/dist/react/model-keys-card.d.ts +94 -0
- package/dist/react/model-keys-card.js +123 -0
- package/dist/react/reload.d.ts +211 -0
- package/dist/react/reload.js +339 -0
- package/dist/react/reveal.d.ts +17 -0
- package/dist/react/reveal.js +22 -0
- package/dist/react/schedule-editor.d.ts +107 -0
- package/dist/react/schedule-editor.js +172 -0
- package/dist/react/schedule-time.d.ts +66 -0
- package/dist/react/schedule-time.js +90 -0
- package/dist/react/styles.css +1274 -0
- package/dist/react/transport.d.ts +49 -0
- package/dist/react/transport.js +380 -0
- package/dist/react/use-connections.d.ts +155 -0
- package/dist/react/use-connections.js +231 -0
- package/dist/react/use-model-keys.d.ts +47 -0
- package/dist/react/use-model-keys.js +57 -0
- package/dist/react/use-threads.d.ts +63 -0
- package/dist/react/use-threads.js +73 -0
- package/dist/react/vault-form.d.ts +92 -0
- package/dist/react/vault-form.js +151 -0
- package/dist/schema.d.ts +47 -0
- package/dist/schema.js +16 -0
- package/dist/secrets.d.ts +15 -0
- package/dist/secrets.js +18 -0
- package/dist/stream.d.ts +129 -0
- package/dist/stream.js +192 -0
- package/dist/testing.d.ts +33 -0
- package/dist/testing.js +40 -0
- package/dist/webhooks.d.ts +31 -0
- package/dist/webhooks.js +72 -0
- package/package.json +85 -3
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The agent's browser as `<ComputerView>` reads it: the kernel's viewer wire, the chat's
|
|
3
|
+
* `data-computer` part, and the small pure pieces between a person's hands and the wire.
|
|
4
|
+
*
|
|
5
|
+
* A COPY OF THE KERNEL'S WIRE (`@openlegion/browser`'s `wire.ts`), held to it by `computer-wire-pin.test.ts`. The
|
|
6
|
+
* kit never imports that package at run time: it ships in `@openlegion/platform`, and the browser package's tokens
|
|
7
|
+
* need `node:crypto`.
|
|
8
|
+
*
|
|
9
|
+
* THE KERNEL IS UNTRUSTED. A state or a frame outside the wire's shapes and caps is dropped, a
|
|
10
|
+
* host is shown only when it is a hostname, and nothing a page wrote (a title, a URL, a control's name) is ever in a
|
|
11
|
+
* sentence the viewer shows.
|
|
12
|
+
*/
|
|
13
|
+
/** The header `control` and `input` carry the view stream's channel in (`wire.ts` `VIEW_CHANNEL_HEADER`). */
|
|
14
|
+
export declare const VIEW_CHANNEL_HEADER = "ol-view-channel";
|
|
15
|
+
/** A view stream carries a comment line at least this often, ms, so a quiet stream still carries bytes. */
|
|
16
|
+
export declare const VIEW_KEEPALIVE_MS = 15000;
|
|
17
|
+
/** A live stream with no bytes at all for this long, ms (three missed keep-alives), is a drop. */
|
|
18
|
+
export declare const VIEW_SILENCE_MS: number;
|
|
19
|
+
/** Appended to the viewer answer's `url`, which is the session's base. */
|
|
20
|
+
export declare const VIEWER_ROUTES: {
|
|
21
|
+
readonly view: "/view";
|
|
22
|
+
readonly control: "/control";
|
|
23
|
+
readonly input: "/input";
|
|
24
|
+
};
|
|
25
|
+
/** The wire's caps: the viewport, a frame's base64, a text event, a key's name and text, a batch, a wheel. */
|
|
26
|
+
export declare const VIEWER_CAPS: {
|
|
27
|
+
readonly width: 1280;
|
|
28
|
+
readonly height: 800;
|
|
29
|
+
readonly jpegChars: number;
|
|
30
|
+
readonly textBytes: 4096;
|
|
31
|
+
readonly keyChars: 32;
|
|
32
|
+
readonly codeChars: 32;
|
|
33
|
+
readonly keyText: 8;
|
|
34
|
+
readonly inputEvents: 64;
|
|
35
|
+
readonly wheel: 10000;
|
|
36
|
+
readonly hostChars: 253;
|
|
37
|
+
};
|
|
38
|
+
/** One SSE event larger than this closes the stream (a frame's base64 and its JSON fit with room). */
|
|
39
|
+
export declare const MAX_SSE_EVENT_CHARS: number;
|
|
40
|
+
/** SSE `event: frame`: a JPEG of the viewport, base64. */
|
|
41
|
+
export interface ViewerFrame {
|
|
42
|
+
seq: number;
|
|
43
|
+
ts: number;
|
|
44
|
+
w: number;
|
|
45
|
+
h: number;
|
|
46
|
+
jpeg: string;
|
|
47
|
+
}
|
|
48
|
+
/** SSE `event: state`, and the answer to a control request. `channel` is on a stream's first state only. */
|
|
49
|
+
export interface ViewerState {
|
|
50
|
+
ownerHasControl: boolean;
|
|
51
|
+
host: string | null;
|
|
52
|
+
ended: boolean;
|
|
53
|
+
channel?: string;
|
|
54
|
+
}
|
|
55
|
+
/** Take-over input (`wire.ts` `InputEvent`): coordinates are CSS pixels of the page's viewport. */
|
|
56
|
+
export type ViewerInput = {
|
|
57
|
+
type: "mouse";
|
|
58
|
+
action: "move" | "down" | "up" | "wheel";
|
|
59
|
+
x: number;
|
|
60
|
+
y: number;
|
|
61
|
+
button?: "left" | "middle" | "right" | "none";
|
|
62
|
+
clickCount?: number;
|
|
63
|
+
deltaX?: number;
|
|
64
|
+
deltaY?: number;
|
|
65
|
+
} | {
|
|
66
|
+
type: "key";
|
|
67
|
+
action: "down" | "up";
|
|
68
|
+
key: string;
|
|
69
|
+
code?: string;
|
|
70
|
+
text?: string;
|
|
71
|
+
} | {
|
|
72
|
+
type: "text";
|
|
73
|
+
text: string;
|
|
74
|
+
};
|
|
75
|
+
/** A `state` event as the wire shapes it, or null. Unknown members are dropped. */
|
|
76
|
+
export declare function viewerStateOf(x: unknown): ViewerState | null;
|
|
77
|
+
/** A `frame` event as the wire shapes it (and base64 the decoder can read), or null. */
|
|
78
|
+
export declare function viewerFrameOf(x: unknown): ViewerFrame | null;
|
|
79
|
+
/** A host a person may read on the site line: lower-cased LDH labels, or null (never a path, query, port or scheme). */
|
|
80
|
+
export declare function safeHost(x: unknown): string | null;
|
|
81
|
+
/** The viewer answer's `url` as a session base: https (or loopback http), with no query, fragment or user; else null. */
|
|
82
|
+
export declare function viewerBaseOf(url: string): string | null;
|
|
83
|
+
export declare function viewerRoute(base: string, route: keyof typeof VIEWER_ROUTES): string;
|
|
84
|
+
/**
|
|
85
|
+
* The view stream's SSE, parsed: `onEvent(event, data)` per dispatched event (`message` when unnamed). `push` answers
|
|
86
|
+
* false once an event outgrows `max`, and from then on: the caller closes the stream.
|
|
87
|
+
*/
|
|
88
|
+
export declare function createSseParser(onEvent: (event: string, data: string) => void, max?: number): {
|
|
89
|
+
push(chunk: string): boolean;
|
|
90
|
+
};
|
|
91
|
+
/** `s` cut to at most `max` UTF-8 bytes, on a character. */
|
|
92
|
+
export declare function utf8Clip(s: string, max: number): string;
|
|
93
|
+
/**
|
|
94
|
+
* A pointer on the drawn picture to the page's CSS pixels. The picture is drawn `object-fit: contain` in `rect`:
|
|
95
|
+
* a point on a letterbox bar is null; a point on the picture's edge is clamped to the viewport.
|
|
96
|
+
*/
|
|
97
|
+
export declare function pointOnFrame(rect: {
|
|
98
|
+
left: number;
|
|
99
|
+
top: number;
|
|
100
|
+
width: number;
|
|
101
|
+
height: number;
|
|
102
|
+
}, frame: {
|
|
103
|
+
w: number;
|
|
104
|
+
h: number;
|
|
105
|
+
}, clientX: number, clientY: number): {
|
|
106
|
+
x: number;
|
|
107
|
+
y: number;
|
|
108
|
+
} | null;
|
|
109
|
+
/** The parts of a keyboard event a key input needs. */
|
|
110
|
+
export interface KeyLike {
|
|
111
|
+
key: string;
|
|
112
|
+
code: string;
|
|
113
|
+
ctrlKey: boolean;
|
|
114
|
+
metaKey: boolean;
|
|
115
|
+
altKey: boolean;
|
|
116
|
+
shiftKey: boolean;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* A key while the person holds control. Every key goes to the page, Tab and Esc included (the page needs them).
|
|
120
|
+
* THE WAY OUT (WCAG 2.1.2): Esc, then Tab, leaves the picture; the Esc was sent, and that Tab is not (`leave`, and
|
|
121
|
+
* the browser's own Tab moves focus). `afterEscape`: the key before this one was Esc.
|
|
122
|
+
*/
|
|
123
|
+
export declare function keyInputOf(e: KeyLike, action: "down" | "up", afterEscape: boolean): {
|
|
124
|
+
send: ViewerInput | null;
|
|
125
|
+
leave: boolean;
|
|
126
|
+
};
|
|
127
|
+
/**
|
|
128
|
+
* Ctrl+V, Cmd+V or Shift+Insert while the person holds control. The kit lets the browser paste, and its paste
|
|
129
|
+
* listener sends the clipboard's text to the page; the chord itself is not sent, or the page would paste its own
|
|
130
|
+
* (empty) clipboard as well.
|
|
131
|
+
*/
|
|
132
|
+
export declare function isPasteChord(e: KeyLike): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* `afterEscape` for the next key, after this key went down. A modifier on its own keeps the mark, so Esc, then
|
|
135
|
+
* Shift+Tab (whose Shift goes down first) leaves backwards as Esc, then Tab leaves forwards.
|
|
136
|
+
*/
|
|
137
|
+
export declare function escapeMarkAfter(key: string, was: boolean): boolean;
|
|
138
|
+
/**
|
|
139
|
+
* One finger on the picture while the person holds control: a tap is a click where it went down; a drag is a wheel
|
|
140
|
+
* the other way, in the page's pixels (the finger pulls the page). `move` answers whether a wheel is now pending, for
|
|
141
|
+
* the caller to `flush` on its own beat; `up` returns whatever is still pending, so a flick shorter than one beat
|
|
142
|
+
* still arrives. DOM-free.
|
|
143
|
+
*/
|
|
144
|
+
export declare function createTouchGesture(): {
|
|
145
|
+
down(id: number, x: number, y: number, at: {
|
|
146
|
+
x: number;
|
|
147
|
+
y: number;
|
|
148
|
+
}): void;
|
|
149
|
+
/** `scale`: the page's pixels per CSS pixel of the picture. */
|
|
150
|
+
move(id: number, x: number, y: number, scale: number): boolean;
|
|
151
|
+
flush(): ViewerInput | null;
|
|
152
|
+
up(id: number): ViewerInput[];
|
|
153
|
+
cancel(): void;
|
|
154
|
+
};
|
|
155
|
+
export declare const COMPUTER_STATUSES: readonly ["live", "owner_control", "parked", "ended"];
|
|
156
|
+
export declare const COMPUTER_END_REASONS: readonly ["run_ended", "closed", "deadline", "park_expired", "stopped", "failed"];
|
|
157
|
+
export type ComputerEndReason = (typeof COMPUTER_END_REASONS)[number];
|
|
158
|
+
/** The `data-computer` part's data. */
|
|
159
|
+
export interface ComputerSession {
|
|
160
|
+
/** `cps_…`. */
|
|
161
|
+
id: string;
|
|
162
|
+
/** `agt_…`, when the part says. */
|
|
163
|
+
agent?: string;
|
|
164
|
+
/** The host the browser is on, checked as a hostname; null when it is not one. */
|
|
165
|
+
site: string | null;
|
|
166
|
+
status: (typeof COMPUTER_STATUSES)[number];
|
|
167
|
+
/** Why it ended, when it has. */
|
|
168
|
+
reason?: ComputerEndReason;
|
|
169
|
+
/**
|
|
170
|
+
* The agent stopped for a person: a security check it does not answer, or a sign-in. While `parked` (the run has
|
|
171
|
+
* ended and the browser waits), also `person`: a person held it when the run ended, which is all it says.
|
|
172
|
+
*/
|
|
173
|
+
waitingFor?: "challenge" | "sign_in" | "person";
|
|
174
|
+
}
|
|
175
|
+
/** A browser session's id (`cps_…`), as the chat's part and a browser act's card name it. Pure. */
|
|
176
|
+
export declare function isComputerSessionId(id: unknown): id is string;
|
|
177
|
+
/** A `data-computer` part's data, or null when it is not one. Unknown members, reasons and waits are dropped. */
|
|
178
|
+
export declare function computerSessionOf(data: unknown): ComputerSession | null;
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
export const VIEW_CHANNEL_HEADER = "ol-view-channel";
|
|
2
|
+
export const VIEW_KEEPALIVE_MS = 15_000;
|
|
3
|
+
export const VIEW_SILENCE_MS = 3 * VIEW_KEEPALIVE_MS;
|
|
4
|
+
export const VIEWER_ROUTES = { view: "/view", control: "/control", input: "/input" };
|
|
5
|
+
export const VIEWER_CAPS = {
|
|
6
|
+
width: 1280,
|
|
7
|
+
height: 800,
|
|
8
|
+
jpegChars: Math.ceil((300 * 1000 * 4) / 3) + 4,
|
|
9
|
+
textBytes: 4096,
|
|
10
|
+
keyChars: 32,
|
|
11
|
+
codeChars: 32,
|
|
12
|
+
keyText: 8,
|
|
13
|
+
inputEvents: 64,
|
|
14
|
+
wheel: 10_000,
|
|
15
|
+
hostChars: 253,
|
|
16
|
+
};
|
|
17
|
+
export const MAX_SSE_EVENT_CHARS = 512 * 1024;
|
|
18
|
+
const CHANNEL_RE = /^[A-Za-z0-9_-]{22,64}$/;
|
|
19
|
+
const BASE64_RE = /^[A-Za-z0-9+/]+={0,2}$/;
|
|
20
|
+
const isInt = (x, min, max = Number.MAX_SAFE_INTEGER) => typeof x === "number" && Number.isInteger(x) && x >= min && x <= max;
|
|
21
|
+
const recordOf = (x) => (x !== null && typeof x === "object" && !Array.isArray(x) ? x : null);
|
|
22
|
+
export function viewerStateOf(x) {
|
|
23
|
+
const o = recordOf(x);
|
|
24
|
+
if (!o || typeof o["ownerHasControl"] !== "boolean" || typeof o["ended"] !== "boolean")
|
|
25
|
+
return null;
|
|
26
|
+
const host = o["host"];
|
|
27
|
+
if (host !== null && (typeof host !== "string" || host.length > VIEWER_CAPS.hostChars))
|
|
28
|
+
return null;
|
|
29
|
+
const channel = o["channel"];
|
|
30
|
+
if (channel !== undefined && (typeof channel !== "string" || !CHANNEL_RE.test(channel)))
|
|
31
|
+
return null;
|
|
32
|
+
return { ownerHasControl: o["ownerHasControl"], host, ended: o["ended"], ...(channel !== undefined ? { channel } : {}) };
|
|
33
|
+
}
|
|
34
|
+
export function viewerFrameOf(x) {
|
|
35
|
+
const o = recordOf(x);
|
|
36
|
+
if (!o)
|
|
37
|
+
return null;
|
|
38
|
+
const { seq, ts, w, h, jpeg } = o;
|
|
39
|
+
if (!isInt(seq, 0) || !isInt(ts, 0) || !isInt(w, 1, VIEWER_CAPS.width) || !isInt(h, 1, VIEWER_CAPS.height))
|
|
40
|
+
return null;
|
|
41
|
+
if (typeof jpeg !== "string" || jpeg.length < 1 || jpeg.length > VIEWER_CAPS.jpegChars || !BASE64_RE.test(jpeg))
|
|
42
|
+
return null;
|
|
43
|
+
return { seq, ts, w, h, jpeg };
|
|
44
|
+
}
|
|
45
|
+
const LABEL_RE = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
46
|
+
export function safeHost(x) {
|
|
47
|
+
if (typeof x !== "string" || x.length < 1 || x.length > VIEWER_CAPS.hostChars)
|
|
48
|
+
return null;
|
|
49
|
+
const h = x.toLowerCase();
|
|
50
|
+
return h.split(".").every((l) => LABEL_RE.test(l)) ? h : null;
|
|
51
|
+
}
|
|
52
|
+
const LOOPBACK = new Set(["127.0.0.1", "localhost", "[::1]"]);
|
|
53
|
+
export function viewerBaseOf(url) {
|
|
54
|
+
let u;
|
|
55
|
+
try {
|
|
56
|
+
u = new URL(url);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
if (u.search !== "" || u.hash !== "" || u.username !== "" || u.password !== "" || url.includes("?") || url.includes("#"))
|
|
62
|
+
return null;
|
|
63
|
+
if (u.protocol !== "https:" && !(u.protocol === "http:" && LOOPBACK.has(u.hostname)))
|
|
64
|
+
return null;
|
|
65
|
+
return `${u.origin}${u.pathname.replace(/\/+$/, "")}`;
|
|
66
|
+
}
|
|
67
|
+
export function viewerRoute(base, route) {
|
|
68
|
+
return `${base}${VIEWER_ROUTES[route]}`;
|
|
69
|
+
}
|
|
70
|
+
export function createSseParser(onEvent, max = MAX_SSE_EVENT_CHARS) {
|
|
71
|
+
let buffer = "";
|
|
72
|
+
let event = "";
|
|
73
|
+
let data = [];
|
|
74
|
+
let size = 0;
|
|
75
|
+
let broken = false;
|
|
76
|
+
const line = (l) => {
|
|
77
|
+
if (l === "") {
|
|
78
|
+
if (data.length > 0)
|
|
79
|
+
onEvent(event || "message", data.join("\n"));
|
|
80
|
+
event = "";
|
|
81
|
+
data = [];
|
|
82
|
+
size = 0;
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (l.startsWith(":"))
|
|
86
|
+
return;
|
|
87
|
+
const at = l.indexOf(":");
|
|
88
|
+
const field = at === -1 ? l : l.slice(0, at);
|
|
89
|
+
const value = at === -1 ? "" : l.slice(at + 1).replace(/^ /, "");
|
|
90
|
+
if (field === "event")
|
|
91
|
+
event = value;
|
|
92
|
+
else if (field === "data")
|
|
93
|
+
data.push(value);
|
|
94
|
+
};
|
|
95
|
+
return {
|
|
96
|
+
push(chunk) {
|
|
97
|
+
if (broken)
|
|
98
|
+
return false;
|
|
99
|
+
buffer += chunk;
|
|
100
|
+
for (;;) {
|
|
101
|
+
const m = /\r\n|\r|\n/.exec(buffer);
|
|
102
|
+
if (!m)
|
|
103
|
+
break;
|
|
104
|
+
if (m[0] === "\r" && m.index === buffer.length - 1)
|
|
105
|
+
break;
|
|
106
|
+
const l = buffer.slice(0, m.index);
|
|
107
|
+
buffer = buffer.slice(m.index + m[0].length);
|
|
108
|
+
size += l.length;
|
|
109
|
+
if (size > max)
|
|
110
|
+
return (broken = true), false;
|
|
111
|
+
line(l);
|
|
112
|
+
}
|
|
113
|
+
if (size + buffer.length > max)
|
|
114
|
+
return (broken = true), false;
|
|
115
|
+
return true;
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
export function utf8Clip(s, max) {
|
|
120
|
+
const enc = new TextEncoder();
|
|
121
|
+
if (enc.encode(s).length <= max)
|
|
122
|
+
return s;
|
|
123
|
+
let out = "";
|
|
124
|
+
let bytes = 0;
|
|
125
|
+
for (const ch of s) {
|
|
126
|
+
const n = enc.encode(ch).length;
|
|
127
|
+
if (bytes + n > max)
|
|
128
|
+
break;
|
|
129
|
+
out += ch;
|
|
130
|
+
bytes += n;
|
|
131
|
+
}
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
export function pointOnFrame(rect, frame, clientX, clientY) {
|
|
135
|
+
if (rect.width <= 0 || rect.height <= 0)
|
|
136
|
+
return null;
|
|
137
|
+
const scale = Math.min(rect.width / frame.w, rect.height / frame.h);
|
|
138
|
+
const dw = frame.w * scale;
|
|
139
|
+
const dh = frame.h * scale;
|
|
140
|
+
const ox = rect.left + (rect.width - dw) / 2;
|
|
141
|
+
const oy = rect.top + (rect.height - dh) / 2;
|
|
142
|
+
const px = clientX - ox;
|
|
143
|
+
const py = clientY - oy;
|
|
144
|
+
if (px < -0.5 || py < -0.5 || px > dw + 0.5 || py > dh + 0.5)
|
|
145
|
+
return null;
|
|
146
|
+
const clamp = (v, hi) => Math.min(hi, Math.max(0, Math.round(v)));
|
|
147
|
+
return { x: clamp(px / scale, Math.min(frame.w, VIEWER_CAPS.width)), y: clamp(py / scale, Math.min(frame.h, VIEWER_CAPS.height)) };
|
|
148
|
+
}
|
|
149
|
+
export function keyInputOf(e, action, afterEscape) {
|
|
150
|
+
if (afterEscape && e.key === "Tab" && action === "down")
|
|
151
|
+
return { send: null, leave: true };
|
|
152
|
+
if (e.key.length < 1 || e.key.length > VIEWER_CAPS.keyChars)
|
|
153
|
+
return { send: null, leave: false };
|
|
154
|
+
const code = e.code !== "" && e.code.length <= VIEWER_CAPS.codeChars ? { code: e.code } : {};
|
|
155
|
+
const printable = action === "down" && [...e.key].length === 1 && !e.ctrlKey && !e.metaKey && e.key.length <= VIEWER_CAPS.keyText;
|
|
156
|
+
return { send: { type: "key", action, key: e.key, ...code, ...(printable ? { text: e.key } : {}) }, leave: false };
|
|
157
|
+
}
|
|
158
|
+
export function isPasteChord(e) {
|
|
159
|
+
if (e.key === "Insert")
|
|
160
|
+
return e.shiftKey && !e.ctrlKey && !e.metaKey && !e.altKey;
|
|
161
|
+
return (e.ctrlKey || e.metaKey) && !e.altKey && e.key.toLowerCase() === "v";
|
|
162
|
+
}
|
|
163
|
+
const MODIFIER_KEYS = new Set(["Shift", "Control", "Alt", "Meta", "AltGraph", "CapsLock"]);
|
|
164
|
+
export function escapeMarkAfter(key, was) {
|
|
165
|
+
if (key === "Escape")
|
|
166
|
+
return true;
|
|
167
|
+
return MODIFIER_KEYS.has(key) ? was : false;
|
|
168
|
+
}
|
|
169
|
+
const TAP_SLOP_PX = 8;
|
|
170
|
+
export function createTouchGesture() {
|
|
171
|
+
let t = null;
|
|
172
|
+
let dx = 0;
|
|
173
|
+
let dy = 0;
|
|
174
|
+
const clamp = (n) => Math.max(-VIEWER_CAPS.wheel, Math.min(VIEWER_CAPS.wheel, Math.round(n)));
|
|
175
|
+
const pending = (at) => {
|
|
176
|
+
const out = Math.round(dx) === 0 && Math.round(dy) === 0 ? null : { type: "mouse", action: "wheel", ...at, deltaX: clamp(dx), deltaY: clamp(dy) };
|
|
177
|
+
dx = 0;
|
|
178
|
+
dy = 0;
|
|
179
|
+
return out;
|
|
180
|
+
};
|
|
181
|
+
return {
|
|
182
|
+
down(id, x, y, at) {
|
|
183
|
+
t = { id, x0: x, y0: y, x, y, at, scrolling: false };
|
|
184
|
+
dx = 0;
|
|
185
|
+
dy = 0;
|
|
186
|
+
},
|
|
187
|
+
move(id, x, y, scale) {
|
|
188
|
+
if (!t || t.id !== id)
|
|
189
|
+
return false;
|
|
190
|
+
if (!t.scrolling && Math.hypot(x - t.x0, y - t.y0) > TAP_SLOP_PX)
|
|
191
|
+
t.scrolling = true;
|
|
192
|
+
if (t.scrolling) {
|
|
193
|
+
dx -= (x - t.x) * scale;
|
|
194
|
+
dy -= (y - t.y) * scale;
|
|
195
|
+
}
|
|
196
|
+
t.x = x;
|
|
197
|
+
t.y = y;
|
|
198
|
+
return t.scrolling;
|
|
199
|
+
},
|
|
200
|
+
flush() {
|
|
201
|
+
return t ? pending(t.at) : null;
|
|
202
|
+
},
|
|
203
|
+
up(id) {
|
|
204
|
+
const was = t;
|
|
205
|
+
if (!was || was.id !== id)
|
|
206
|
+
return [];
|
|
207
|
+
t = null;
|
|
208
|
+
if (was.scrolling) {
|
|
209
|
+
const w = pending(was.at);
|
|
210
|
+
return w ? [w] : [];
|
|
211
|
+
}
|
|
212
|
+
return [
|
|
213
|
+
{ type: "mouse", action: "move", ...was.at },
|
|
214
|
+
{ type: "mouse", action: "down", ...was.at, button: "left", clickCount: 1 },
|
|
215
|
+
{ type: "mouse", action: "up", ...was.at, button: "left", clickCount: 1 },
|
|
216
|
+
];
|
|
217
|
+
},
|
|
218
|
+
cancel() {
|
|
219
|
+
t = null;
|
|
220
|
+
dx = 0;
|
|
221
|
+
dy = 0;
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
export const COMPUTER_STATUSES = ["live", "owner_control", "parked", "ended"];
|
|
226
|
+
export const COMPUTER_END_REASONS = ["run_ended", "closed", "deadline", "park_expired", "stopped", "failed"];
|
|
227
|
+
const SESSION_RE = /^cps_[0-9A-Za-z_-]{1,64}$/;
|
|
228
|
+
export function isComputerSessionId(id) {
|
|
229
|
+
return typeof id === "string" && SESSION_RE.test(id);
|
|
230
|
+
}
|
|
231
|
+
const AGENT_RE = /^agt_[0-9A-Za-z_-]{1,64}$/;
|
|
232
|
+
export function computerSessionOf(data) {
|
|
233
|
+
const o = recordOf(data);
|
|
234
|
+
if (!o || o["object"] !== "computer_session" || typeof o["id"] !== "string" || !SESSION_RE.test(o["id"]))
|
|
235
|
+
return null;
|
|
236
|
+
const status = o["status"];
|
|
237
|
+
if (!COMPUTER_STATUSES.includes(status))
|
|
238
|
+
return null;
|
|
239
|
+
const agent = typeof o["agent"] === "string" && AGENT_RE.test(o["agent"]) ? { agent: o["agent"] } : {};
|
|
240
|
+
const reason = status === "ended" && COMPUTER_END_REASONS.includes(o["reason"]) ? { reason: o["reason"] } : {};
|
|
241
|
+
const w = o["waiting_for"];
|
|
242
|
+
const waiting = w === "challenge" || w === "sign_in" || (w === "person" && status === "parked") ? { waitingFor: w } : {};
|
|
243
|
+
return { id: o["id"], ...agent, site: safeHost(o["site"]), status: status, ...reason, ...waiting };
|
|
244
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { type KitConnection, type KitConnectSession } from "./use-connections.ts";
|
|
2
|
+
/** Every string the button shows. `{app}` is the app's name. */
|
|
3
|
+
export interface ConnectButtonLabels {
|
|
4
|
+
connect: string;
|
|
5
|
+
reconnect: string;
|
|
6
|
+
/** The button's words: the title over it (or, `compact`, the row around it) names the app. `connectedShort` is `compact`'s connected line. */
|
|
7
|
+
connectShort: string;
|
|
8
|
+
reconnectShort: string;
|
|
9
|
+
connectedShort: string;
|
|
10
|
+
startAgain: string;
|
|
11
|
+
/** In the new window, for the moment before the page loads. */
|
|
12
|
+
opening: string;
|
|
13
|
+
waiting: string;
|
|
14
|
+
connected: string;
|
|
15
|
+
cancelled: string;
|
|
16
|
+
failed: string;
|
|
17
|
+
expired: string;
|
|
18
|
+
blocked: string;
|
|
19
|
+
openLink: string;
|
|
20
|
+
rateLimited: string;
|
|
21
|
+
/** A refusal to start that a retry can't fix: the network or the server. */
|
|
22
|
+
startFailed: string;
|
|
23
|
+
/**
|
|
24
|
+
* A refusal only your setup can fix (your page's origin isn't one of your return origins, the app isn't enabled, …):
|
|
25
|
+
* no "try again", since no retry will help. The console says what to fix.
|
|
26
|
+
*/
|
|
27
|
+
setupRefused: string;
|
|
28
|
+
/** Someone else connected this app, and only an owner or admin may replace their account (403 `insufficient_role`). */
|
|
29
|
+
replaceRefused: string;
|
|
30
|
+
/** Above the button when the agent read outside content before it asked. */
|
|
31
|
+
tainted: string;
|
|
32
|
+
/**
|
|
33
|
+
* The one message the chat sends for the person once a connect started here completes, so the agent carries on.
|
|
34
|
+
* `{app}` is the catalog's name for the app, which is OpenLegion's words.
|
|
35
|
+
*/
|
|
36
|
+
done: string;
|
|
37
|
+
/**
|
|
38
|
+
* The same for a server added by its address (`custom-<slug>`). It has no slot: that server's name is text someone
|
|
39
|
+
* typed, and this message runs with the person's own trust.
|
|
40
|
+
*/
|
|
41
|
+
doneCustom: string;
|
|
42
|
+
}
|
|
43
|
+
export declare const DEFAULT_CONNECT_LABELS: ConnectButtonLabels;
|
|
44
|
+
/** Your labels over OpenLegion's: a label left undefined keeps OpenLegion's. */
|
|
45
|
+
export declare function connectLabelsOf(labels: Partial<ConnectButtonLabels> | undefined): ConnectButtonLabels;
|
|
46
|
+
/**
|
|
47
|
+
* Whether to send the carry-on message: only when a connect started from this button (`startedHere`, its session
|
|
48
|
+
* id) has completed, and then once, since `startedHere` comes back cleared. A reload starts with nothing started.
|
|
49
|
+
*/
|
|
50
|
+
export declare function carryOnOf(session: KitConnectSession | null, startedHere: string | null): {
|
|
51
|
+
send: boolean;
|
|
52
|
+
startedHere: string | null;
|
|
53
|
+
};
|
|
54
|
+
/** What the button shows, from what it knows. */
|
|
55
|
+
export type ConnectViewState = {
|
|
56
|
+
kind: "connected";
|
|
57
|
+
account: string | null;
|
|
58
|
+
} | {
|
|
59
|
+
kind: "starting";
|
|
60
|
+
} | {
|
|
61
|
+
kind: "waiting";
|
|
62
|
+
link: string | null;
|
|
63
|
+
} | {
|
|
64
|
+
kind: "ready";
|
|
65
|
+
again: boolean;
|
|
66
|
+
note: "cancelled" | "failed" | "expired" | "rateLimited" | "startFailed" | "setup" | "replaceRefused" | null;
|
|
67
|
+
reconnect: boolean;
|
|
68
|
+
};
|
|
69
|
+
/** The connection open to `agent` (any agent when null), if the app has one. */
|
|
70
|
+
export declare function connectionFor(connections: readonly KitConnection[] | null, agent: string | null): KitConnection | null;
|
|
71
|
+
export declare function connectViewOf(s: {
|
|
72
|
+
connections: readonly KitConnection[] | null;
|
|
73
|
+
session: KitConnectSession | null;
|
|
74
|
+
starting: boolean;
|
|
75
|
+
blockedUrl: string | null;
|
|
76
|
+
startError: "rateLimited" | "startFailed" | "setup" | "replaceRefused" | null;
|
|
77
|
+
agent: string | null;
|
|
78
|
+
reconnect: boolean;
|
|
79
|
+
}): ConnectViewState;
|
|
80
|
+
/** Redirect mode: the path to come back to, naming the app so only its button takes the session. */
|
|
81
|
+
export declare function redirectReturnPath(loc: {
|
|
82
|
+
pathname: string;
|
|
83
|
+
search: string;
|
|
84
|
+
}, app: string): string;
|
|
85
|
+
/** Redirect mode, back on the page: this app's session id from the query, which is then removed. Once per return. */
|
|
86
|
+
export declare function takeReturnedSession(app: string, w: {
|
|
87
|
+
location: {
|
|
88
|
+
pathname: string;
|
|
89
|
+
search: string;
|
|
90
|
+
hash: string;
|
|
91
|
+
};
|
|
92
|
+
history: {
|
|
93
|
+
replaceState(d: unknown, t: string, u: string): void;
|
|
94
|
+
};
|
|
95
|
+
}): string | null;
|
|
96
|
+
/** Whether the click opens a new window: only when there may be a sign-in to show, and not in redirect mode. */
|
|
97
|
+
export declare function opensWindow(o: {
|
|
98
|
+
mode: "window" | "redirect" | undefined;
|
|
99
|
+
auth: "oauth2" | "api_key" | "none" | undefined;
|
|
100
|
+
}): boolean;
|
|
101
|
+
export interface ConnectButtonProps {
|
|
102
|
+
/** The catalog app id, e.g. google. */
|
|
103
|
+
app: string;
|
|
104
|
+
/** Its name, for the words. Default: the id. */
|
|
105
|
+
appName?: string | undefined;
|
|
106
|
+
/** The agent's sentence: what it will do with the app. Shown as plain text. */
|
|
107
|
+
reason?: string | undefined;
|
|
108
|
+
/** agt_…: the connection is opened to this agent (and stays open to any it was). */
|
|
109
|
+
agent?: string | null | undefined;
|
|
110
|
+
reconnect?: boolean | undefined;
|
|
111
|
+
tainted?: boolean | undefined;
|
|
112
|
+
/** How the app connects, when your page knows (the catalog's `auth.kind`): `none` opens no window. */
|
|
113
|
+
auth?: "oauth2" | "api_key" | "none" | undefined;
|
|
114
|
+
/** The row around the button already names the app: no title, and "Connect" / "Connected" without the name. */
|
|
115
|
+
compact?: boolean | undefined;
|
|
116
|
+
/** "window" (default): a new window or tab. "redirect": this page goes, and comes back to its own URL. */
|
|
117
|
+
mode?: "window" | "redirect" | undefined;
|
|
118
|
+
/** The kit's base path. Default "/api/openlegion". */
|
|
119
|
+
api?: string | undefined;
|
|
120
|
+
fetch?: typeof fetch | undefined;
|
|
121
|
+
labels?: Partial<ConnectButtonLabels> | undefined;
|
|
122
|
+
/** Once, when a connect started from this button completes. Never on a reload. */
|
|
123
|
+
onConnected?: ((app: string, appName: string) => void) | undefined;
|
|
124
|
+
onError?: ((err: Error) => void) | undefined;
|
|
125
|
+
/** The app's connection, when your page already read it: the button then reads none itself. */
|
|
126
|
+
connection?: KitConnection | null | undefined;
|
|
127
|
+
className?: string | undefined;
|
|
128
|
+
}
|
|
129
|
+
export declare function ConnectButton(props: ConnectButtonProps): import("react").JSX.Element;
|
|
130
|
+
export interface ConnectButtonViewProps {
|
|
131
|
+
view: ConnectViewState;
|
|
132
|
+
appName: string;
|
|
133
|
+
reason?: string | undefined;
|
|
134
|
+
tainted?: boolean | undefined;
|
|
135
|
+
/** The row around it already names the app: no title, and the short labels. */
|
|
136
|
+
compact?: boolean | undefined;
|
|
137
|
+
labels?: Partial<ConnectButtonLabels> | undefined;
|
|
138
|
+
onConnect: () => void;
|
|
139
|
+
linkRef?: {
|
|
140
|
+
current: HTMLAnchorElement | null;
|
|
141
|
+
} | undefined;
|
|
142
|
+
className?: string | undefined;
|
|
143
|
+
}
|
|
144
|
+
/** The button as drawn for one state: no network, for your tests and stories. */
|
|
145
|
+
export declare function ConnectButtonView(p: ConnectButtonViewProps): import("react").JSX.Element;
|