@eworkbase/sdk 0.1.1 → 0.2.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/index.d.ts +9 -0
- package/dist/index.js +96 -0
- package/dist/types-CYt_KGKR.d.ts +449 -0
- package/dist/ui/index.css.d.ts +2 -0
- package/dist/ui/index.d.ts +118 -0
- package/dist/ui/index.js +195 -0
- package/{ui → dist/ui}/model.d.ts +5 -3
- package/dist/ui/model.js +38 -0
- package/package.json +23 -11
- package/README.md +0 -442
- package/definition.d.ts +0 -7
- package/definition.js +0 -41
- package/host-services.d.ts +0 -168
- package/host-services.js +0 -4
- package/index.d.ts +0 -8
- package/index.js +0 -8
- package/types.d.ts +0 -246
- package/types.js +0 -56
- package/ui/controller.d.ts +0 -5
- package/ui/controller.js +0 -212
- package/ui/index.css.d.ts +0 -1
- package/ui/index.d.ts +0 -7
- package/ui/index.js +0 -35
- package/ui/model.js +0 -50
- package/ui/types.d.ts +0 -108
- package/ui/types.js +0 -4
- /package/{ui → dist/ui}/index.css +0 -0
package/dist/ui/index.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { WindowMessenger, connect } from "penpal";
|
|
2
|
+
//#region src/ui/controller.ts
|
|
3
|
+
const MAX_CONTENT_HEIGHT = 1e4;
|
|
4
|
+
const MAX_FATAL_MESSAGE_LENGTH = 1e3;
|
|
5
|
+
const MIN_CONTENT_HEIGHT = 1;
|
|
6
|
+
function createPluginUiController(definition, options) {
|
|
7
|
+
const onInit = definition?.onInit;
|
|
8
|
+
const connectToHost = options?.connectToHost;
|
|
9
|
+
const targetWindow = options?.targetWindow;
|
|
10
|
+
if (typeof onInit !== "function") throw new TypeError("defineWorkbasePluginUi requires an onInit callback.");
|
|
11
|
+
if (!targetWindow || typeof connectToHost !== "function") throw new TypeError("Plugin UI transport is unavailable.");
|
|
12
|
+
const state = {
|
|
13
|
+
fatalReportPromise: null,
|
|
14
|
+
isInitialized: false,
|
|
15
|
+
recoverableHostErrors: /* @__PURE__ */ new WeakSet()
|
|
16
|
+
};
|
|
17
|
+
const connection = connectToHost({ initialize: async (payload) => {
|
|
18
|
+
assertInitializationPayload(payload);
|
|
19
|
+
state.isInitialized = true;
|
|
20
|
+
await onInit(payload);
|
|
21
|
+
await waitForPluginUiPaint(targetWindow);
|
|
22
|
+
await reportContentHeight(hostPromise, targetWindow);
|
|
23
|
+
} });
|
|
24
|
+
const hostPromise = connection.promise;
|
|
25
|
+
const request = createHostRequest(hostPromise, state);
|
|
26
|
+
bindPluginUiLifecycle({
|
|
27
|
+
connection,
|
|
28
|
+
state,
|
|
29
|
+
targetWindow
|
|
30
|
+
});
|
|
31
|
+
return createPluginUiApi(request, targetWindow);
|
|
32
|
+
}
|
|
33
|
+
function createHostRequest(hostPromise, state) {
|
|
34
|
+
return async (method, params) => {
|
|
35
|
+
if (!state.isInitialized) throw new Error("Plugin UI is not initialized.");
|
|
36
|
+
const response = await (await hostPromise).call(method, params);
|
|
37
|
+
if (isRecord(response) && response.ok === true) return response.result;
|
|
38
|
+
const error = createHostError(response);
|
|
39
|
+
state.recoverableHostErrors.add(error);
|
|
40
|
+
throw error;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function createHostError(response) {
|
|
44
|
+
const responseError = isRecord(response) && isRecord(response.error) ? response.error : null;
|
|
45
|
+
const message = typeof responseError?.message === "string" ? responseError.message : "Host action failed.";
|
|
46
|
+
const code = typeof responseError?.code === "string" ? responseError.code : "host_error";
|
|
47
|
+
return Object.assign(new Error(message), { code });
|
|
48
|
+
}
|
|
49
|
+
function createPluginUiApi(request, targetWindow) {
|
|
50
|
+
const commands = Object.freeze({
|
|
51
|
+
cancel: (executionId) => request("command.cancel", { executionId }),
|
|
52
|
+
execute: (commandId, input, options = {}) => {
|
|
53
|
+
return request("command.execute", {
|
|
54
|
+
commandId,
|
|
55
|
+
executionId: options.executionId ?? targetWindow.crypto.randomUUID(),
|
|
56
|
+
input
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
const status = Object.freeze({ resolve: (statusId, input) => request("status.resolve", {
|
|
61
|
+
input,
|
|
62
|
+
statusId
|
|
63
|
+
}) });
|
|
64
|
+
const view = Object.freeze({ call: (method, input) => request("view.call", {
|
|
65
|
+
input,
|
|
66
|
+
method
|
|
67
|
+
}) });
|
|
68
|
+
return Object.freeze({
|
|
69
|
+
closeOutput: (id) => request("output.close", { id }),
|
|
70
|
+
commands,
|
|
71
|
+
navigate: (to, { replace = false } = {}) => request("navigate", {
|
|
72
|
+
replace,
|
|
73
|
+
to
|
|
74
|
+
}),
|
|
75
|
+
openOutput: (options) => request("output.open", options),
|
|
76
|
+
selectFiles: (options = {}) => request("file.select", options),
|
|
77
|
+
showPrompt: (options) => request("dialog.prompt", options),
|
|
78
|
+
showToast: (options) => request("toast.show", options),
|
|
79
|
+
status,
|
|
80
|
+
updateOutput: (options) => request("output.update", options),
|
|
81
|
+
view
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function bindPluginUiLifecycle({ connection, state, targetWindow }) {
|
|
85
|
+
const handleWindowError = (event) => {
|
|
86
|
+
reportFirstFatal(connection.promise, state, event.error ?? event.message);
|
|
87
|
+
};
|
|
88
|
+
const handleUnhandledRejection = (event) => {
|
|
89
|
+
if (event.reason instanceof Error && state.recoverableHostErrors.has(event.reason)) return;
|
|
90
|
+
reportFirstFatal(connection.promise, state, event.reason);
|
|
91
|
+
};
|
|
92
|
+
const resizeObserver = new targetWindow.ResizeObserver(() => {
|
|
93
|
+
reportContentHeight(connection.promise, targetWindow);
|
|
94
|
+
});
|
|
95
|
+
const cleanup = () => {
|
|
96
|
+
targetWindow.removeEventListener("error", handleWindowError, true);
|
|
97
|
+
targetWindow.removeEventListener("unhandledrejection", handleUnhandledRejection);
|
|
98
|
+
targetWindow.removeEventListener("beforeunload", cleanup);
|
|
99
|
+
resizeObserver.disconnect();
|
|
100
|
+
connection.destroy();
|
|
101
|
+
state.fatalReportPromise = null;
|
|
102
|
+
state.isInitialized = false;
|
|
103
|
+
};
|
|
104
|
+
ignoreConnectionFailure(connection.promise);
|
|
105
|
+
resizeObserver.observe(targetWindow.document.body ?? targetWindow.document.documentElement);
|
|
106
|
+
targetWindow.addEventListener("error", handleWindowError, true);
|
|
107
|
+
targetWindow.addEventListener("unhandledrejection", handleUnhandledRejection);
|
|
108
|
+
targetWindow.addEventListener("beforeunload", cleanup, { once: true });
|
|
109
|
+
}
|
|
110
|
+
async function ignoreConnectionFailure(hostPromise) {
|
|
111
|
+
try {
|
|
112
|
+
await hostPromise;
|
|
113
|
+
} catch {}
|
|
114
|
+
}
|
|
115
|
+
async function waitForPluginUiPaint(targetWindow) {
|
|
116
|
+
await new Promise((resolve) => {
|
|
117
|
+
targetWindow.requestAnimationFrame(() => {
|
|
118
|
+
targetWindow.requestAnimationFrame(() => resolve());
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
async function reportFirstFatal(hostPromise, state, error) {
|
|
123
|
+
if (!state.fatalReportPromise) state.fatalReportPromise = sendFatalReport(hostPromise, getFatalMessage(error).slice(0, MAX_FATAL_MESSAGE_LENGTH));
|
|
124
|
+
await state.fatalReportPromise;
|
|
125
|
+
}
|
|
126
|
+
async function sendFatalReport(hostPromise, message) {
|
|
127
|
+
try {
|
|
128
|
+
await (await hostPromise).reportFatal(message);
|
|
129
|
+
} catch {}
|
|
130
|
+
}
|
|
131
|
+
async function reportContentHeight(hostPromise, targetWindow) {
|
|
132
|
+
const contentElement = targetWindow.document.body ?? targetWindow.document.documentElement;
|
|
133
|
+
const height = Math.max(contentElement.offsetHeight, contentElement.scrollHeight);
|
|
134
|
+
if (!Number.isFinite(height)) return;
|
|
135
|
+
try {
|
|
136
|
+
const host = await hostPromise;
|
|
137
|
+
const contentHeight = Math.min(Math.max(Math.ceil(height), MIN_CONTENT_HEIGHT), MAX_CONTENT_HEIGHT);
|
|
138
|
+
await host.reportContentHeight(contentHeight);
|
|
139
|
+
} catch {}
|
|
140
|
+
}
|
|
141
|
+
function assertInitializationPayload(value) {
|
|
142
|
+
if (!isInitializationPayload(value)) throw new TypeError("Plugin UI initialization payload is invalid.");
|
|
143
|
+
}
|
|
144
|
+
function isInitializationPayload(value) {
|
|
145
|
+
if (!isRecord(value)) return false;
|
|
146
|
+
return isContextIdentity(value.project) && isContextIdentity(value.workspace) && isViewIdentity(value.view);
|
|
147
|
+
}
|
|
148
|
+
function isContextIdentity(value) {
|
|
149
|
+
return isRecord(value) && typeof value.id === "string" && value.id.length > 0;
|
|
150
|
+
}
|
|
151
|
+
function isViewIdentity(value) {
|
|
152
|
+
return isContextIdentity(value) && isRecord(value) && typeof value.pluginId === "string" && value.pluginId.length > 0 && typeof value.title === "string" && value.title.length > 0;
|
|
153
|
+
}
|
|
154
|
+
function isRecord(value) {
|
|
155
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
156
|
+
}
|
|
157
|
+
function getFatalMessage(error) {
|
|
158
|
+
if (error instanceof Error) return error.message;
|
|
159
|
+
if (typeof error === "string" && error.trim().length > 0) return error;
|
|
160
|
+
return "Plugin UI failed.";
|
|
161
|
+
}
|
|
162
|
+
//#endregion
|
|
163
|
+
//#region src/ui/index.ts
|
|
164
|
+
/**
|
|
165
|
+
* Penpal browser entry for trusted Workbase plugin views.
|
|
166
|
+
*/
|
|
167
|
+
const BRIDGE_CHANNEL = "workbase-plugin-ui-v1";
|
|
168
|
+
const HOST_ORIGIN_PARAMETER = "__workbaseHostOrigin";
|
|
169
|
+
function defineWorkbasePluginUi(definition) {
|
|
170
|
+
const targetWindow = window;
|
|
171
|
+
return createPluginUiController(definition, {
|
|
172
|
+
connectToHost: (methods) => createPenpalConnection(targetWindow, methods),
|
|
173
|
+
targetWindow
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
function createPenpalConnection(targetWindow, methods) {
|
|
177
|
+
const connection = connect({
|
|
178
|
+
channel: BRIDGE_CHANNEL,
|
|
179
|
+
messenger: new WindowMessenger({
|
|
180
|
+
allowedOrigins: resolveParentOrigins(targetWindow),
|
|
181
|
+
remoteWindow: targetWindow.parent
|
|
182
|
+
}),
|
|
183
|
+
methods
|
|
184
|
+
});
|
|
185
|
+
return {
|
|
186
|
+
destroy: connection.destroy,
|
|
187
|
+
promise: connection.promise
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
function resolveParentOrigins(targetWindow) {
|
|
191
|
+
const hostOrigin = new URL(targetWindow.location.href).searchParams.get(HOST_ORIGIN_PARAMETER);
|
|
192
|
+
return hostOrigin && hostOrigin !== "null" ? [hostOrigin] : ["*"];
|
|
193
|
+
}
|
|
194
|
+
//#endregion
|
|
195
|
+
export { createPluginUiController, defineWorkbasePluginUi };
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
//#region src/ui/model.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Formats command results and recent Host log entries for built-in plugin views.
|
|
3
4
|
*/
|
|
4
5
|
export interface PluginActionOutput {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
readonly content: string;
|
|
7
|
+
readonly status: "completed" | "failed";
|
|
8
|
+
readonly title: string;
|
|
8
9
|
}
|
|
9
10
|
export declare function formatActionOutput(title: string, result: unknown, logs: unknown): PluginActionOutput;
|
|
10
11
|
export declare function formatActionError(title: string, error: unknown): PluginActionOutput;
|
|
11
12
|
export declare function readableError(error: unknown, fallback: string): string;
|
|
13
|
+
//#endregion
|
package/dist/ui/model.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//#region src/ui/model.ts
|
|
2
|
+
function formatActionOutput(title, result, logs) {
|
|
3
|
+
const logText = formatLogEntries(logs);
|
|
4
|
+
const resultText = formatResult(result);
|
|
5
|
+
return {
|
|
6
|
+
content: logText || resultText || `${title}已完成。`,
|
|
7
|
+
status: "completed",
|
|
8
|
+
title
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function formatActionError(title, error) {
|
|
12
|
+
return {
|
|
13
|
+
content: readableError(error, `${title}失败。`),
|
|
14
|
+
status: "failed",
|
|
15
|
+
title
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function readableError(error, fallback) {
|
|
19
|
+
return error instanceof Error && error.message.trim().length > 0 ? error.message : fallback;
|
|
20
|
+
}
|
|
21
|
+
function formatLogEntries(value) {
|
|
22
|
+
if (!Array.isArray(value)) return "";
|
|
23
|
+
return value.filter(isRecord).map((entry) => {
|
|
24
|
+
const level = typeof entry.level === "string" ? entry.level : "log";
|
|
25
|
+
const message = typeof entry.message === "string" ? entry.message : "";
|
|
26
|
+
return message ? `[${level}] ${message}` : "";
|
|
27
|
+
}).filter(Boolean).join("\n");
|
|
28
|
+
}
|
|
29
|
+
function formatResult(value) {
|
|
30
|
+
if (value === null || value === void 0) return "";
|
|
31
|
+
if (typeof value === "string") return value;
|
|
32
|
+
return JSON.stringify(value, null, 2);
|
|
33
|
+
}
|
|
34
|
+
function isRecord(value) {
|
|
35
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { formatActionError, formatActionOutput, readableError };
|
package/package.json
CHANGED
|
@@ -1,24 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eworkbase/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Typed backend and browser APIs for independently developed Workbase plugins.",
|
|
3
|
+
"version": "0.2.0",
|
|
5
4
|
"type": "module",
|
|
6
5
|
"exports": {
|
|
7
6
|
".": {
|
|
8
|
-
"types": "./index.d.ts",
|
|
9
|
-
"import": "./index.js"
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"import": "./dist/index.js"
|
|
10
9
|
},
|
|
11
10
|
"./ui": {
|
|
12
|
-
"types": "./ui/index.d.ts",
|
|
13
|
-
"import": "./ui/index.js"
|
|
11
|
+
"types": "./dist/ui/index.d.ts",
|
|
12
|
+
"import": "./dist/ui/index.js"
|
|
14
13
|
},
|
|
15
14
|
"./ui/index.css": {
|
|
16
|
-
"types": "./ui/index.css.d.ts",
|
|
17
|
-
"default": "./ui/index.css"
|
|
15
|
+
"types": "./dist/ui/index.css.d.ts",
|
|
16
|
+
"default": "./dist/ui/index.css"
|
|
18
17
|
},
|
|
19
18
|
"./ui/model": {
|
|
20
|
-
"types": "./ui/model.d.ts",
|
|
21
|
-
"import": "./ui/model.js"
|
|
19
|
+
"types": "./dist/ui/model.d.ts",
|
|
20
|
+
"import": "./dist/ui/model.js"
|
|
22
21
|
}
|
|
23
22
|
},
|
|
24
23
|
"engines": {
|
|
@@ -30,5 +29,18 @@
|
|
|
30
29
|
},
|
|
31
30
|
"publishConfig": {
|
|
32
31
|
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"typescript": "^5.8.3",
|
|
38
|
+
"@types/node": "^24.0.3",
|
|
39
|
+
"tsdown": "0.23.0"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsdown",
|
|
43
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
44
|
+
"pack:local": "pnpm build && pnpm pack"
|
|
33
45
|
}
|
|
34
|
-
}
|
|
46
|
+
}
|