@lobu/runtime-device 24.4.0 → 24.6.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/dist/component.json +1 -1
- package/dist/dependencies.bun.lock +77 -64
- package/dist/verify.mjs +1 -1
- package/npm-shrinkwrap.json +176 -141
- package/package.json +3 -2
- package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +65 -11
- package/vendor/cli/dist/commands/_lib/apply/apply-cmd.js +20 -13
- package/vendor/cli/dist/commands/_lib/apply/client.d.ts +48 -100
- package/vendor/cli/dist/commands/_lib/apply/client.js +76 -27
- package/vendor/cli/dist/commands/_lib/apply/deployment.js +1 -1
- package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +7 -11
- package/vendor/cli/dist/commands/_lib/apply/desired-state.js +9 -40
- package/vendor/cli/dist/commands/_lib/apply/diff.d.ts +4 -4
- package/vendor/cli/dist/commands/_lib/apply/diff.js +37 -33
- package/vendor/cli/dist/commands/_lib/apply/map-config.d.ts +1 -1
- package/vendor/cli/dist/commands/_lib/apply/map-config.js +79 -92
- package/vendor/cli/dist/commands/_lib/apply/shared.d.ts +3 -6
- package/vendor/cli/dist/commands/_lib/apply/shared.js +8 -19
- package/vendor/cli/dist/commands/_lib/view-bundler.js +9 -1
- package/vendor/cli/dist/commands/dev.js +21 -7
- package/vendor/cli/dist/commands/doctor.js +1 -8
- package/vendor/cli/dist/commands/init.js +4 -5
- package/vendor/cli/dist/config/define.d.ts +27 -28
- package/vendor/cli/dist/config/define.js +4 -5
- package/vendor/cli/dist/connectors/linkedin-identity.ts +43 -0
- package/vendor/cli/dist/connectors/linkedin.ts +154 -3
- package/vendor/cli/dist/connectors/postgres.ts +77 -48
- package/vendor/cli/dist/connectors/x-like-follow.ts +329 -0
- package/vendor/cli/dist/connectors/x.ts +709 -80
- package/vendor/cli/dist/internal/api-client.d.ts +2 -0
- package/vendor/cli/dist/internal/api-client.js +6 -1
- package/vendor/cli/dist/internal/connector-compiler.js +3 -1
- package/vendor/cli/dist/internal/node-binary.d.ts +22 -0
- package/vendor/cli/dist/internal/node-binary.js +73 -0
- package/vendor/cli/dist/internal/node-version.d.ts +7 -12
- package/vendor/cli/dist/internal/node-version.js +16 -20
- package/vendor/cli/dist/runtime-components.json +4 -4
- package/vendor/cli/dist/templates/AGENTS.md.tmpl +2 -2
- package/vendor/cli/package.json +3 -2
- package/vendor/connector-sdk/dist/reaction-client-types.d.ts +8 -2
- package/vendor/connector-sdk/dist/reaction-sdk.d.ts +5 -1
- package/vendor/connector-sdk/package.json +1 -1
- package/vendor/connector-worker/dist/compile/index.d.ts +4 -3
- package/vendor/connector-worker/dist/compile/index.js +6 -3
- package/vendor/connector-worker/dist/daemon/acp-adapters/claude.js +1 -1
- package/vendor/connector-worker/dist/daemon/agent-turn.js +21 -10
- package/vendor/connector-worker/dist/daemon/client.d.ts +25 -8
- package/vendor/connector-worker/dist/daemon/client.js +25 -3
- package/vendor/connector-worker/dist/daemon/mac-device-daemon.js +7 -1
- package/vendor/connector-worker/dist/daemon/poll-loop.d.ts +17 -0
- package/vendor/connector-worker/dist/daemon/poll-loop.js +75 -6
- package/vendor/connector-worker/dist/daemon/resource-admission.d.ts +19 -0
- package/vendor/connector-worker/dist/daemon/resource-admission.js +59 -0
- package/vendor/connector-worker/dist/daemon/resources.d.ts +20 -0
- package/vendor/connector-worker/dist/daemon/resources.js +163 -0
- package/vendor/connector-worker/dist/daemon/start.js +29 -4
- package/vendor/connector-worker/dist/daemon/worker.d.ts +2 -0
- package/vendor/connector-worker/dist/daemon/worker.js +4 -0
- package/vendor/connector-worker/dist/executor/select.d.ts +1 -1
- package/vendor/connector-worker/dist/executor/select.js +1 -1
- package/vendor/connector-worker/dist/isolate/ivm-types.d.ts +1 -2
- package/vendor/connector-worker/dist/isolate/ivm-types.js +1 -2
- package/vendor/connector-worker/dist/isolate/load.d.ts +10 -12
- package/vendor/connector-worker/dist/isolate/load.js +23 -27
- package/vendor/connector-worker/dist/self-check/index.d.ts +2 -2
- package/vendor/connector-worker/dist/self-check/index.js +2 -2
- package/vendor/connector-worker/package.json +3 -4
- package/vendor/core/dist/contracts/agent-settings.d.ts +61 -6
- package/vendor/core/dist/contracts/agent-settings.js +10 -7
- package/vendor/core/dist/contracts/tools/manage-agents.d.ts +473 -98
- package/vendor/core/dist/contracts/tools/manage-agents.js +111 -2
- package/vendor/core/dist/contracts/tools/manage-automations.d.ts +21 -18
- package/vendor/core/dist/contracts/tools/manage-automations.js +15 -15
- package/vendor/core/dist/contracts/tools/manage-entity-schema.js +2 -2
- package/vendor/core/dist/contracts/tools/manage-inference-providers.d.ts +445 -0
- package/vendor/core/dist/contracts/tools/manage-inference-providers.js +133 -0
- package/vendor/core/dist/contracts/tools/manage-views.d.ts +4 -4
- package/vendor/core/dist/contracts/tools/manage-views.js +5 -3
- package/vendor/core/dist/contracts/tools/view-host.d.ts +7 -0
- package/vendor/core/dist/contracts/tools/view-host.js +28 -0
- package/vendor/core/dist/contracts/tools/view-runtime.d.ts +2 -4
- package/vendor/core/dist/contracts/tools/view-runtime.js +19 -7
- package/vendor/core/dist/contracts/worker/device-automation.js +4 -2
- package/vendor/core/dist/error-report.d.ts +73 -0
- package/vendor/core/dist/error-report.js +346 -0
- package/vendor/core/dist/index.d.ts +3 -1
- package/vendor/core/dist/index.js +4 -1
- package/vendor/core/dist/logger.js +7 -4
- package/vendor/core/dist/otel-fetch.d.ts +19 -0
- package/vendor/core/dist/otel-fetch.js +96 -0
- package/vendor/core/dist/otel.d.ts +9 -0
- package/vendor/core/dist/otel.js +28 -7
- package/vendor/core/dist/reserved.d.ts +1 -1
- package/vendor/core/dist/reserved.js +6 -4
- package/vendor/core/dist/tool-result.d.ts +8 -0
- package/vendor/core/dist/tool-result.js +61 -0
- package/vendor/core/dist/utils/diagnostic-scrubber.js +1 -1
- package/vendor/core/dist/worker/auth.js +4 -0
- package/vendor/core/package.json +16 -1
- package/vendor/views/dist/api.d.ts +290 -0
- package/vendor/views/dist/api.js +714 -0
- package/vendor/views/dist/bridge.d.ts +157 -0
- package/vendor/views/dist/bridge.js +372 -0
- package/vendor/views/dist/components.d.ts +172 -0
- package/vendor/views/dist/components.js +376 -0
- package/vendor/views/dist/index.d.ts +13 -0
- package/vendor/views/dist/index.js +3 -0
- package/vendor/views/dist/native.d.ts +327 -0
- package/vendor/views/dist/native.js +57 -0
- package/vendor/views/package.json +53 -0
- /package/vendor/cli/dist/vendor/ora/{.bun-tag-fde20e9a6df212fa → .bun-tag-77bb9b258a474a18} +0 -0
- /package/vendor/ora/{.bun-tag-fde20e9a6df212fa → .bun-tag-77bb9b258a474a18} +0 -0
- /package/vendor/postgres/{.bun-tag-22d644c2873790db → .bun-tag-c81dacd34e959a7a} +0 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hand-written MCP Apps guest transport for Lobu views.
|
|
3
|
+
*
|
|
4
|
+
* No protocol SDK dependencies: the spike measured zod + MCP SDK + ext-apps at
|
|
5
|
+
* 378 KB of a 577 KB view bundle, so the guest speaks the wire protocol
|
|
6
|
+
* directly instead of importing `@modelcontextprotocol/ext-apps`. The method
|
|
7
|
+
* names below mirror that SDK (protocol version `2026-01-26`): the host side
|
|
8
|
+
* (`AppBridge` in owletto, Claude, any MCP Apps host) is unchanged.
|
|
9
|
+
*
|
|
10
|
+
* Wire shape is plain JSON-RPC 2.0 over `window.postMessage` to the parent
|
|
11
|
+
* frame: requests carry `{ jsonrpc: "2.0", id, method, params }`, responses
|
|
12
|
+
* `{ jsonrpc: "2.0", id, result }` (or `error`), notifications `{ jsonrpc:
|
|
13
|
+
* "2.0", method, params }`. Anything else in the inbox is ignored, exactly
|
|
14
|
+
* like the SDK's transport.
|
|
15
|
+
*/
|
|
16
|
+
import type { EntityGetInput } from "@lobu/core/contracts/tools/manage-entity";
|
|
17
|
+
export declare class ViewBridgeError extends Error {
|
|
18
|
+
readonly code: number;
|
|
19
|
+
constructor(message: string, code: number);
|
|
20
|
+
}
|
|
21
|
+
export interface ToolResult {
|
|
22
|
+
isError?: boolean;
|
|
23
|
+
structuredContent?: unknown;
|
|
24
|
+
content?: unknown;
|
|
25
|
+
}
|
|
26
|
+
export interface HostContext {
|
|
27
|
+
theme?: unknown;
|
|
28
|
+
[key: string]: unknown;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Bootstrap state the generic loader injects into the per-view document
|
|
32
|
+
* (`window.__lobuViewHandoff`) before replacing its own: the authenticated
|
|
33
|
+
* initialize result's context, the last complete tool input, event
|
|
34
|
+
* notifications that arrived after it, and the loader's request-id counter.
|
|
35
|
+
* Adopting it lets the mounted guest continue the loader's session — a host
|
|
36
|
+
* that delivers the opening input once has already fulfilled it. The
|
|
37
|
+
* readiness gate is NOT bypassed: queries still wait for a tool input, now
|
|
38
|
+
* satisfied by the authentic cached one through the normal listener path.
|
|
39
|
+
*/
|
|
40
|
+
export interface ViewHandoff {
|
|
41
|
+
v?: unknown;
|
|
42
|
+
hostContext?: unknown;
|
|
43
|
+
toolInput?: unknown;
|
|
44
|
+
queue?: unknown;
|
|
45
|
+
nextId?: unknown;
|
|
46
|
+
}
|
|
47
|
+
export interface BridgeOptions {
|
|
48
|
+
appName?: string;
|
|
49
|
+
appVersion?: string;
|
|
50
|
+
/** Parent origin. Default `*`: the frame is sandboxed opaque-origin, so the
|
|
51
|
+
* host cannot be allow-listed; the host authenticates the frame, not vice
|
|
52
|
+
* versa — same posture as the SDK's `PostMessageTransport`. */
|
|
53
|
+
targetOrigin?: string;
|
|
54
|
+
/** Milliseconds before a pending request rejects. Default 60_000. */
|
|
55
|
+
requestTimeoutMs?: number;
|
|
56
|
+
}
|
|
57
|
+
type JsonRpcId = string | number;
|
|
58
|
+
/**
|
|
59
|
+
* Minimal MCP Apps guest. Construct, register listeners, then `connect()`.
|
|
60
|
+
* Listeners registered after `connect()` resolves can miss the host's one-shot
|
|
61
|
+
* `tool-input` (the SDK warns for the same reason), so the view layer wires
|
|
62
|
+
* them synchronously at mount.
|
|
63
|
+
*/
|
|
64
|
+
export declare class ViewBridge {
|
|
65
|
+
private target;
|
|
66
|
+
private expectedSource;
|
|
67
|
+
private targetOrigin;
|
|
68
|
+
private timeoutMs;
|
|
69
|
+
private appInfo;
|
|
70
|
+
private nextId;
|
|
71
|
+
private pending;
|
|
72
|
+
private toolInputListeners;
|
|
73
|
+
private toolResultListeners;
|
|
74
|
+
private toolCancelledListeners;
|
|
75
|
+
private hostContextListeners;
|
|
76
|
+
private dataChangedListeners;
|
|
77
|
+
/** Set by the first `tool-input` and never cleared: queries must not fire
|
|
78
|
+
* before it (the host seeds scope + params there; anything read earlier
|
|
79
|
+
* runs against defaults and double-fetches). */
|
|
80
|
+
private toolInputReceived;
|
|
81
|
+
private workspace;
|
|
82
|
+
private hostContext;
|
|
83
|
+
private didConnect;
|
|
84
|
+
private handoff;
|
|
85
|
+
private onMessage;
|
|
86
|
+
private listening;
|
|
87
|
+
constructor(target?: Window, expectedSource?: Window | null, opts?: BridgeOptions);
|
|
88
|
+
/**
|
|
89
|
+
* Adopt the loader's bootstrap state, if this document was mounted through
|
|
90
|
+
* the generic loader. Consumed once: a stale blob must never reseed a later
|
|
91
|
+
* navigation. Malformed blobs are ignored — the guest then waits for live
|
|
92
|
+
* host input like a directly mounted shell.
|
|
93
|
+
*/
|
|
94
|
+
private readHandoff;
|
|
95
|
+
/** Latest host context (theme, display mode, …), merged from every
|
|
96
|
+
* `host-context-changed` notification. */
|
|
97
|
+
getHostContext(): HostContext;
|
|
98
|
+
hasToolInput(): boolean;
|
|
99
|
+
onToolInput(listener: (args: Record<string, unknown>) => void): () => void;
|
|
100
|
+
onToolResult(listener: (result: ToolResult) => void): () => void;
|
|
101
|
+
onToolCancelled(listener: (reason: string | null) => void): () => void;
|
|
102
|
+
onHostContext(listener: (ctx: HostContext) => void): () => void;
|
|
103
|
+
/** The host saw workspace data change (Lobu hosts only; other hosts never
|
|
104
|
+
* send it, and reads then refresh on their own triggers). */
|
|
105
|
+
onDataChanged(listener: () => void): () => void;
|
|
106
|
+
/**
|
|
107
|
+
* Run the `ui/initialize` handshake, then announce readiness. Resolves with
|
|
108
|
+
* the host context. Rejects when the host answers with an error or stays
|
|
109
|
+
* silent past the deadline — the view renders its defaults, never a hang.
|
|
110
|
+
*/
|
|
111
|
+
connect(): Promise<HostContext>;
|
|
112
|
+
/**
|
|
113
|
+
* Continue the loader's session: seed request numbering from its counter,
|
|
114
|
+
* then deliver the cached tool input and queued event notifications through
|
|
115
|
+
* the normal listener paths — the readiness gate sees a real input, not a
|
|
116
|
+
* bypass. The fresh initialize result above keeps precedence for context.
|
|
117
|
+
*/
|
|
118
|
+
private adoptHandoff;
|
|
119
|
+
/** True once the `ui/initialize` handshake completed. */
|
|
120
|
+
isConnected(): boolean;
|
|
121
|
+
/** Proxy one `tools/call` to the host and return the FULL result (a view
|
|
122
|
+
* reads `structuredContent`; the boolean-only hook on the card path is
|
|
123
|
+
* what this deliberately does not copy). */
|
|
124
|
+
callTool(name: string, args: Record<string, unknown>): Promise<ToolResult>;
|
|
125
|
+
openRecord(record: EntityGetInput, mode: "peek" | "page"): Promise<{
|
|
126
|
+
mode: "peek" | "page";
|
|
127
|
+
}>;
|
|
128
|
+
/** Read one `resources/read` URI and return the first text payload. The
|
|
129
|
+
* generic loader uses this to fetch `ui://lobu/views/<key>` through the
|
|
130
|
+
* host instead of a custom message. */
|
|
131
|
+
readResourceText(uri: string): Promise<string>;
|
|
132
|
+
/** Push `{ structuredContent }` into the model's context. Hosts without the
|
|
133
|
+
* capability reject; callers keep params frame-local on failure. */
|
|
134
|
+
updateModelContext(structuredContent: Record<string, unknown>): Promise<void>;
|
|
135
|
+
sendMessage(text: string): Promise<ToolResult>;
|
|
136
|
+
openLink(url: string): Promise<ToolResult>;
|
|
137
|
+
/** Ask the host for `mode`; resolves with the mode it actually set, which
|
|
138
|
+
* may differ when the host does not offer the one requested. */
|
|
139
|
+
requestDisplayMode(mode: "inline" | "fullscreen"): Promise<string>;
|
|
140
|
+
/** Report the document size so the host can fit the frame. Fire-and-forget:
|
|
141
|
+
* a host that ignores it keeps its default height. */
|
|
142
|
+
notifySize(width: number, height: number): void;
|
|
143
|
+
close(): void;
|
|
144
|
+
private startListening;
|
|
145
|
+
private request;
|
|
146
|
+
private notify;
|
|
147
|
+
private respond;
|
|
148
|
+
private respondError;
|
|
149
|
+
private handleMessage;
|
|
150
|
+
private handleResponse;
|
|
151
|
+
private handleNotification;
|
|
152
|
+
/** Answer an inbound host request (ping, teardown). Wired through the same
|
|
153
|
+
* inbox: JSON-RPC requests carry both `id` and `method`. */
|
|
154
|
+
handleRequest(id: JsonRpcId, method: string): boolean;
|
|
155
|
+
}
|
|
156
|
+
export {};
|
|
157
|
+
//# sourceMappingURL=bridge.d.ts.map
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hand-written MCP Apps guest transport for Lobu views.
|
|
3
|
+
*
|
|
4
|
+
* No protocol SDK dependencies: the spike measured zod + MCP SDK + ext-apps at
|
|
5
|
+
* 378 KB of a 577 KB view bundle, so the guest speaks the wire protocol
|
|
6
|
+
* directly instead of importing `@modelcontextprotocol/ext-apps`. The method
|
|
7
|
+
* names below mirror that SDK (protocol version `2026-01-26`): the host side
|
|
8
|
+
* (`AppBridge` in owletto, Claude, any MCP Apps host) is unchanged.
|
|
9
|
+
*
|
|
10
|
+
* Wire shape is plain JSON-RPC 2.0 over `window.postMessage` to the parent
|
|
11
|
+
* frame: requests carry `{ jsonrpc: "2.0", id, method, params }`, responses
|
|
12
|
+
* `{ jsonrpc: "2.0", id, result }` (or `error`), notifications `{ jsonrpc:
|
|
13
|
+
* "2.0", method, params }`. Anything else in the inbox is ignored, exactly
|
|
14
|
+
* like the SDK's transport.
|
|
15
|
+
*/
|
|
16
|
+
import { VIEW_OPEN_RECORD_METHOD, VIEW_WORKSPACE_META_KEY, } from "@lobu/core/contracts/tools/view-host";
|
|
17
|
+
export class ViewBridgeError extends Error {
|
|
18
|
+
code;
|
|
19
|
+
constructor(message, code) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.code = code;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/** Event notifications worth replaying to the mounted guest (context merges
|
|
25
|
+
* into adopted state instead; anything else is ignored). */
|
|
26
|
+
const HANDOFF_NOTIFICATIONS = new Set([
|
|
27
|
+
"ui/notifications/tool-result",
|
|
28
|
+
"ui/notifications/tool-cancelled",
|
|
29
|
+
]);
|
|
30
|
+
const PROTOCOL_VERSION = "2026-01-26";
|
|
31
|
+
/** Lobu host → guest: workspace data changed, re-read. Not an MCP Apps
|
|
32
|
+
* method; hosts that do not speak it simply never send it. */
|
|
33
|
+
const DATA_CHANGED_NOTIFICATION = "lobu/notifications/data-changed";
|
|
34
|
+
const DEFAULT_TIMEOUT_MS = 60_000;
|
|
35
|
+
function isRecord(value) {
|
|
36
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Minimal MCP Apps guest. Construct, register listeners, then `connect()`.
|
|
40
|
+
* Listeners registered after `connect()` resolves can miss the host's one-shot
|
|
41
|
+
* `tool-input` (the SDK warns for the same reason), so the view layer wires
|
|
42
|
+
* them synchronously at mount.
|
|
43
|
+
*/
|
|
44
|
+
export class ViewBridge {
|
|
45
|
+
target;
|
|
46
|
+
expectedSource;
|
|
47
|
+
targetOrigin;
|
|
48
|
+
timeoutMs;
|
|
49
|
+
appInfo;
|
|
50
|
+
nextId = 1;
|
|
51
|
+
pending = new Map();
|
|
52
|
+
toolInputListeners = new Set();
|
|
53
|
+
toolResultListeners = new Set();
|
|
54
|
+
toolCancelledListeners = new Set();
|
|
55
|
+
hostContextListeners = new Set();
|
|
56
|
+
dataChangedListeners = new Set();
|
|
57
|
+
/** Set by the first `tool-input` and never cleared: queries must not fire
|
|
58
|
+
* before it (the host seeds scope + params there; anything read earlier
|
|
59
|
+
* runs against defaults and double-fetches). */
|
|
60
|
+
toolInputReceived = false;
|
|
61
|
+
workspace = null;
|
|
62
|
+
hostContext = {};
|
|
63
|
+
didConnect = false;
|
|
64
|
+
handoff = null;
|
|
65
|
+
onMessage = (event) => this.handleMessage(event);
|
|
66
|
+
listening = false;
|
|
67
|
+
constructor(target, expectedSource, opts) {
|
|
68
|
+
const w = globalThis;
|
|
69
|
+
this.target = target ?? w.window?.parent ?? w.parent;
|
|
70
|
+
this.expectedSource =
|
|
71
|
+
expectedSource ?? w.window?.parent ?? null;
|
|
72
|
+
this.targetOrigin = opts?.targetOrigin ?? "*";
|
|
73
|
+
this.timeoutMs = opts?.requestTimeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
74
|
+
this.appInfo = {
|
|
75
|
+
name: opts?.appName ?? "Lobu view",
|
|
76
|
+
version: opts?.appVersion ?? "0.0.1",
|
|
77
|
+
};
|
|
78
|
+
this.readHandoff();
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Adopt the loader's bootstrap state, if this document was mounted through
|
|
82
|
+
* the generic loader. Consumed once: a stale blob must never reseed a later
|
|
83
|
+
* navigation. Malformed blobs are ignored — the guest then waits for live
|
|
84
|
+
* host input like a directly mounted shell.
|
|
85
|
+
*/
|
|
86
|
+
readHandoff() {
|
|
87
|
+
const w = globalThis;
|
|
88
|
+
const win = w.window;
|
|
89
|
+
if (!win || typeof win !== "object")
|
|
90
|
+
return;
|
|
91
|
+
const raw = win.__lobuViewHandoff;
|
|
92
|
+
try {
|
|
93
|
+
delete win.__lobuViewHandoff;
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
// Non-configurable property: single-run document, ignore.
|
|
97
|
+
}
|
|
98
|
+
if (!isRecord(raw))
|
|
99
|
+
return;
|
|
100
|
+
this.handoff = raw;
|
|
101
|
+
if (isRecord(this.handoff.hostContext)) {
|
|
102
|
+
this.hostContext = { ...this.handoff.hostContext };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** Latest host context (theme, display mode, …), merged from every
|
|
106
|
+
* `host-context-changed` notification. */
|
|
107
|
+
getHostContext() {
|
|
108
|
+
return { ...this.hostContext };
|
|
109
|
+
}
|
|
110
|
+
hasToolInput() {
|
|
111
|
+
return this.toolInputReceived;
|
|
112
|
+
}
|
|
113
|
+
onToolInput(listener) {
|
|
114
|
+
this.toolInputListeners.add(listener);
|
|
115
|
+
return () => this.toolInputListeners.delete(listener);
|
|
116
|
+
}
|
|
117
|
+
onToolResult(listener) {
|
|
118
|
+
this.toolResultListeners.add(listener);
|
|
119
|
+
return () => this.toolResultListeners.delete(listener);
|
|
120
|
+
}
|
|
121
|
+
onToolCancelled(listener) {
|
|
122
|
+
this.toolCancelledListeners.add(listener);
|
|
123
|
+
return () => this.toolCancelledListeners.delete(listener);
|
|
124
|
+
}
|
|
125
|
+
onHostContext(listener) {
|
|
126
|
+
this.hostContextListeners.add(listener);
|
|
127
|
+
return () => this.hostContextListeners.delete(listener);
|
|
128
|
+
}
|
|
129
|
+
/** The host saw workspace data change (Lobu hosts only; other hosts never
|
|
130
|
+
* send it, and reads then refresh on their own triggers). */
|
|
131
|
+
onDataChanged(listener) {
|
|
132
|
+
this.dataChangedListeners.add(listener);
|
|
133
|
+
return () => this.dataChangedListeners.delete(listener);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Run the `ui/initialize` handshake, then announce readiness. Resolves with
|
|
137
|
+
* the host context. Rejects when the host answers with an error or stays
|
|
138
|
+
* silent past the deadline — the view renders its defaults, never a hang.
|
|
139
|
+
*/
|
|
140
|
+
async connect() {
|
|
141
|
+
this.startListening();
|
|
142
|
+
const result = (await this.request("ui/initialize", {
|
|
143
|
+
appInfo: this.appInfo,
|
|
144
|
+
appCapabilities: { availableDisplayModes: ["inline", "fullscreen"] },
|
|
145
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
146
|
+
}));
|
|
147
|
+
const ctx = (result.hostContext ?? {});
|
|
148
|
+
this.hostContext = { ...this.hostContext, ...ctx };
|
|
149
|
+
this.didConnect = true;
|
|
150
|
+
this.notify({ method: "ui/notifications/initialized", params: {} });
|
|
151
|
+
this.adoptHandoff();
|
|
152
|
+
return this.getHostContext();
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Continue the loader's session: seed request numbering from its counter,
|
|
156
|
+
* then deliver the cached tool input and queued event notifications through
|
|
157
|
+
* the normal listener paths — the readiness gate sees a real input, not a
|
|
158
|
+
* bypass. The fresh initialize result above keeps precedence for context.
|
|
159
|
+
*/
|
|
160
|
+
adoptHandoff() {
|
|
161
|
+
const h = this.handoff;
|
|
162
|
+
this.handoff = null;
|
|
163
|
+
if (!h)
|
|
164
|
+
return;
|
|
165
|
+
if (typeof h.nextId === "number" && Number.isFinite(h.nextId)) {
|
|
166
|
+
this.nextId = Math.max(this.nextId, Math.floor(h.nextId));
|
|
167
|
+
}
|
|
168
|
+
if (isRecord(h.toolInput)) {
|
|
169
|
+
this.toolInputReceived = true;
|
|
170
|
+
const args = h.toolInput;
|
|
171
|
+
this.workspace =
|
|
172
|
+
typeof args.organization === "string" ? args.organization : null;
|
|
173
|
+
for (const listener of [...this.toolInputListeners])
|
|
174
|
+
listener(args);
|
|
175
|
+
}
|
|
176
|
+
if (Array.isArray(h.queue)) {
|
|
177
|
+
for (const item of h.queue) {
|
|
178
|
+
if (!isRecord(item) || typeof item.method !== "string")
|
|
179
|
+
continue;
|
|
180
|
+
if (!HANDOFF_NOTIFICATIONS.has(item.method))
|
|
181
|
+
continue;
|
|
182
|
+
this.handleNotification(item.method, isRecord(item.params) ? item.params : {});
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/** True once the `ui/initialize` handshake completed. */
|
|
187
|
+
isConnected() {
|
|
188
|
+
return this.didConnect;
|
|
189
|
+
}
|
|
190
|
+
/** Proxy one `tools/call` to the host and return the FULL result (a view
|
|
191
|
+
* reads `structuredContent`; the boolean-only hook on the card path is
|
|
192
|
+
* what this deliberately does not copy). */
|
|
193
|
+
callTool(name, args) {
|
|
194
|
+
return this.request("tools/call", {
|
|
195
|
+
name,
|
|
196
|
+
arguments: args,
|
|
197
|
+
...(this.workspace
|
|
198
|
+
? { _meta: { [VIEW_WORKSPACE_META_KEY]: this.workspace } }
|
|
199
|
+
: {}),
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
openRecord(record, mode) {
|
|
203
|
+
return this.request(VIEW_OPEN_RECORD_METHOD, { record, mode });
|
|
204
|
+
}
|
|
205
|
+
/** Read one `resources/read` URI and return the first text payload. The
|
|
206
|
+
* generic loader uses this to fetch `ui://lobu/views/<key>` through the
|
|
207
|
+
* host instead of a custom message. */
|
|
208
|
+
async readResourceText(uri) {
|
|
209
|
+
const result = (await this.request("resources/read", { uri }));
|
|
210
|
+
const text = result.contents?.[0]?.text;
|
|
211
|
+
if (typeof text !== "string" || text.length === 0) {
|
|
212
|
+
throw new Error(`Resource ${uri} returned no text content`);
|
|
213
|
+
}
|
|
214
|
+
return text;
|
|
215
|
+
}
|
|
216
|
+
/** Push `{ structuredContent }` into the model's context. Hosts without the
|
|
217
|
+
* capability reject; callers keep params frame-local on failure. */
|
|
218
|
+
updateModelContext(structuredContent) {
|
|
219
|
+
return this.request("ui/update-model-context", { structuredContent }).then(() => undefined);
|
|
220
|
+
}
|
|
221
|
+
sendMessage(text) {
|
|
222
|
+
return this.request("ui/message", {
|
|
223
|
+
role: "user",
|
|
224
|
+
content: [{ type: "text", text }],
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
openLink(url) {
|
|
228
|
+
return this.request("ui/open-link", { url });
|
|
229
|
+
}
|
|
230
|
+
/** Ask the host for `mode`; resolves with the mode it actually set, which
|
|
231
|
+
* may differ when the host does not offer the one requested. */
|
|
232
|
+
async requestDisplayMode(mode) {
|
|
233
|
+
const result = (await this.request("ui/request-display-mode", {
|
|
234
|
+
mode,
|
|
235
|
+
}));
|
|
236
|
+
return typeof result.mode === "string" ? result.mode : mode;
|
|
237
|
+
}
|
|
238
|
+
/** Report the document size so the host can fit the frame. Fire-and-forget:
|
|
239
|
+
* a host that ignores it keeps its default height. */
|
|
240
|
+
notifySize(width, height) {
|
|
241
|
+
this.notify({
|
|
242
|
+
method: "ui/notifications/size-changed",
|
|
243
|
+
params: { width, height },
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
close() {
|
|
247
|
+
if (this.listening) {
|
|
248
|
+
window.removeEventListener("message", this.onMessage);
|
|
249
|
+
this.listening = false;
|
|
250
|
+
}
|
|
251
|
+
for (const [, pending] of this.pending) {
|
|
252
|
+
clearTimeout(pending.timer);
|
|
253
|
+
pending.reject(new Error("ViewBridge closed"));
|
|
254
|
+
}
|
|
255
|
+
this.pending.clear();
|
|
256
|
+
}
|
|
257
|
+
startListening() {
|
|
258
|
+
if (this.listening)
|
|
259
|
+
return;
|
|
260
|
+
window.addEventListener("message", this.onMessage);
|
|
261
|
+
this.listening = true;
|
|
262
|
+
}
|
|
263
|
+
request(method, params) {
|
|
264
|
+
const id = this.nextId++;
|
|
265
|
+
const message = { jsonrpc: "2.0", id, method, params };
|
|
266
|
+
return new Promise((resolve, reject) => {
|
|
267
|
+
const timer = setTimeout(() => {
|
|
268
|
+
this.pending.delete(id);
|
|
269
|
+
reject(new Error(`ViewBridge request "${method}" timed out`));
|
|
270
|
+
}, this.timeoutMs);
|
|
271
|
+
this.pending.set(id, {
|
|
272
|
+
resolve: resolve,
|
|
273
|
+
reject,
|
|
274
|
+
timer,
|
|
275
|
+
});
|
|
276
|
+
this.target.postMessage(message, this.targetOrigin);
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
notify(message) {
|
|
280
|
+
this.target.postMessage({ jsonrpc: "2.0", ...message }, this.targetOrigin);
|
|
281
|
+
}
|
|
282
|
+
respond(id, result) {
|
|
283
|
+
this.target.postMessage({ jsonrpc: "2.0", id, result }, this.targetOrigin);
|
|
284
|
+
}
|
|
285
|
+
respondError(id, code, message) {
|
|
286
|
+
this.target.postMessage({ jsonrpc: "2.0", id, error: { code, message } }, this.targetOrigin);
|
|
287
|
+
}
|
|
288
|
+
handleMessage(event) {
|
|
289
|
+
if (this.expectedSource && event.source !== this.expectedSource)
|
|
290
|
+
return;
|
|
291
|
+
const data = event.data;
|
|
292
|
+
if (!isRecord(data) || data.jsonrpc !== "2.0")
|
|
293
|
+
return;
|
|
294
|
+
const hasId = ("id" in data && typeof data.id === "string") ||
|
|
295
|
+
("id" in data && typeof data.id === "number");
|
|
296
|
+
// An inbound host request carries both `id` and `method` (ping,
|
|
297
|
+
// teardown); a response to one of ours carries `id` without `method`.
|
|
298
|
+
if (hasId && typeof data.method === "string") {
|
|
299
|
+
this.handleRequest(data.id, data.method);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
if (hasId) {
|
|
303
|
+
this.handleResponse(data.id, data);
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
if (typeof data.method !== "string")
|
|
307
|
+
return;
|
|
308
|
+
this.handleNotification(data.method, isRecord(data.params) ? data.params : {});
|
|
309
|
+
}
|
|
310
|
+
handleResponse(id, data) {
|
|
311
|
+
const pending = this.pending.get(id);
|
|
312
|
+
if (!pending)
|
|
313
|
+
return;
|
|
314
|
+
this.pending.delete(id);
|
|
315
|
+
clearTimeout(pending.timer);
|
|
316
|
+
if ("error" in data && isRecord(data.error)) {
|
|
317
|
+
pending.reject(new ViewBridgeError(String(data.error.message ?? "Host request failed"), Number(data.error.code)));
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
pending.resolve(data.result);
|
|
321
|
+
}
|
|
322
|
+
handleNotification(method, params) {
|
|
323
|
+
switch (method) {
|
|
324
|
+
case "ui/notifications/tool-input": {
|
|
325
|
+
this.toolInputReceived = true;
|
|
326
|
+
const args = isRecord(params.arguments) ? params.arguments : {};
|
|
327
|
+
this.workspace =
|
|
328
|
+
typeof args.organization === "string" ? args.organization : null;
|
|
329
|
+
for (const listener of [...this.toolInputListeners])
|
|
330
|
+
listener(args);
|
|
331
|
+
break;
|
|
332
|
+
}
|
|
333
|
+
case "ui/notifications/tool-result": {
|
|
334
|
+
const result = params;
|
|
335
|
+
for (const listener of [...this.toolResultListeners])
|
|
336
|
+
listener(result);
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
case "ui/notifications/tool-cancelled": {
|
|
340
|
+
const reason = typeof params.reason === "string" ? params.reason : null;
|
|
341
|
+
for (const listener of [...this.toolCancelledListeners])
|
|
342
|
+
listener(reason);
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
case "ui/notifications/host-context-changed": {
|
|
346
|
+
this.hostContext = { ...this.hostContext, ...params };
|
|
347
|
+
const snapshot = this.getHostContext();
|
|
348
|
+
for (const listener of [...this.hostContextListeners])
|
|
349
|
+
listener(snapshot);
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
case DATA_CHANGED_NOTIFICATION: {
|
|
353
|
+
for (const listener of [...this.dataChangedListeners])
|
|
354
|
+
listener();
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
default:
|
|
358
|
+
break;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
/** Answer an inbound host request (ping, teardown). Wired through the same
|
|
362
|
+
* inbox: JSON-RPC requests carry both `id` and `method`. */
|
|
363
|
+
handleRequest(id, method) {
|
|
364
|
+
if (method === "ping" || method === "ui/resource-teardown") {
|
|
365
|
+
this.respond(id, {});
|
|
366
|
+
return true;
|
|
367
|
+
}
|
|
368
|
+
this.respondError(id, -32601, `Method not found: ${method}`);
|
|
369
|
+
return false;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
//# sourceMappingURL=bridge.js.map
|