@simpleplatform/sdk 2.4.2 → 3.0.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/README.md +459 -44
- package/dist/ai.d.ts +10 -0
- package/dist/ai.js +5 -1
- package/dist/space/actions.d.ts +59 -0
- package/dist/space/actions.js +69 -0
- package/dist/space/browser-transport.d.ts +18 -0
- package/dist/space/browser-transport.js +229 -0
- package/dist/space/core.d.ts +43 -201
- package/dist/space/core.js +70 -408
- package/dist/space/documents.d.ts +25 -0
- package/dist/space/documents.js +72 -0
- package/dist/space/index.d.ts +6 -31
- package/dist/space/index.js +61 -90
- package/dist/space/navigation.d.ts +25 -0
- package/dist/space/navigation.js +32 -0
- package/dist/space/protocol.d.ts +66 -0
- package/dist/space/protocol.js +77 -0
- package/dist/space/record-form.d.ts +94 -0
- package/dist/space/record-form.js +217 -0
- package/dist/space/record.d.ts +137 -0
- package/dist/space/record.js +231 -0
- package/dist/space/status.d.ts +2 -0
- package/dist/space/status.js +7 -0
- package/dist/space/tabs.d.ts +64 -0
- package/dist/space/tabs.js +236 -0
- package/dist/space/tasks.d.ts +84 -0
- package/dist/space/tasks.js +129 -0
- package/dist/space/ui.d.ts +67 -0
- package/dist/space/ui.js +123 -0
- package/dist/tasks.d.ts +18 -0
- package/dist/tasks.js +34 -0
- package/package.json +5 -1
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { invalidRequest, invalidResponse, isNonBlankString, isNonNegativeInteger, isObjectRecord, PROTOCOL_VERSION, readResponse, requestWithin, SpaceProtocolError, } from './protocol.js';
|
|
2
|
+
import { SPACE_STATUS_TONES } from './status.js';
|
|
3
|
+
export const TABS_PROTOCOL_VERSION = 1;
|
|
4
|
+
export const DEFAULT_TABS_TIMEOUT_MS = 10000;
|
|
5
|
+
export const LUCIDE_ICON_NAME = /^[a-z0-9]+(-[a-z0-9]+)*$/;
|
|
6
|
+
export const TAB_ID = /^[a-z0-9][a-z0-9_-]{0,63}$/;
|
|
7
|
+
export function validateTabs(options) {
|
|
8
|
+
if (!isObjectRecord(options))
|
|
9
|
+
throw invalidRequest('Tabs options must be an object.');
|
|
10
|
+
if (typeof options.onChange !== 'function')
|
|
11
|
+
throw invalidRequest('Tabs onChange must be a function.');
|
|
12
|
+
if (!Array.isArray(options.tabs))
|
|
13
|
+
throw invalidRequest('Tabs must be an array.');
|
|
14
|
+
if (options.tabs.length > 32)
|
|
15
|
+
throw invalidRequest('Tabs must contain between 0 and 32 tabs.');
|
|
16
|
+
const seenIds = new Set();
|
|
17
|
+
let defaultCount = 0;
|
|
18
|
+
for (const tab of options.tabs) {
|
|
19
|
+
if (!isObjectRecord(tab))
|
|
20
|
+
throw invalidRequest('Each tab must be an object.');
|
|
21
|
+
if (typeof tab.id !== 'string' || !TAB_ID.test(tab.id))
|
|
22
|
+
throw invalidRequest(`Tab id "${String(tab.id)}" is invalid. Tab ids must match ^[a-z0-9][a-z0-9_-]{0,63}$.`);
|
|
23
|
+
if (seenIds.has(tab.id))
|
|
24
|
+
throw invalidRequest(`Tab ids must be unique: "${tab.id}"`);
|
|
25
|
+
seenIds.add(tab.id);
|
|
26
|
+
if (typeof tab.title !== 'string' || !isNonBlankString(tab.title) || tab.title.length > 80)
|
|
27
|
+
throw invalidRequest(`Tab "${tab.id}" title must be a non-empty string of at most 80 characters.`);
|
|
28
|
+
if (tab.default !== undefined) {
|
|
29
|
+
if (typeof tab.default !== 'boolean')
|
|
30
|
+
throw invalidRequest(`Tab "${tab.id}" default must be a boolean.`);
|
|
31
|
+
if (tab.default)
|
|
32
|
+
defaultCount++;
|
|
33
|
+
}
|
|
34
|
+
if (tab.icon !== undefined) {
|
|
35
|
+
if (typeof tab.icon !== 'string' || !LUCIDE_ICON_NAME.test(tab.icon))
|
|
36
|
+
throw invalidRequest(`Tab "${tab.id}" icon must be a kebab-case Lucide icon name.`);
|
|
37
|
+
}
|
|
38
|
+
if (tab.badge !== undefined) {
|
|
39
|
+
const isStringBadge = typeof tab.badge === 'string';
|
|
40
|
+
const isNumberBadge = typeof tab.badge === 'number';
|
|
41
|
+
if (isStringBadge) {
|
|
42
|
+
if (!isNonBlankString(tab.badge) || tab.badge.length > 20)
|
|
43
|
+
throw invalidRequest(`Tab "${tab.id}" badge must be a non-empty string of at most 20 characters.`);
|
|
44
|
+
}
|
|
45
|
+
else if (isNumberBadge) {
|
|
46
|
+
if (!isNonNegativeInteger(tab.badge))
|
|
47
|
+
throw invalidRequest(`Tab "${tab.id}" badge must be a finite non-negative integer.`);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
throw invalidRequest(`Tab "${tab.id}" badge must be a non-empty string or a non-negative integer.`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (tab.badgeTone !== undefined
|
|
54
|
+
&& (typeof tab.badgeTone !== 'string' || !SPACE_STATUS_TONES.includes(tab.badgeTone))) {
|
|
55
|
+
throw invalidRequest(`Tab "${tab.id}" badgeTone must be one of: ${SPACE_STATUS_TONES.join(', ')}.`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (defaultCount > 1)
|
|
59
|
+
throw invalidRequest('Only one tab may be marked as default.');
|
|
60
|
+
const wireTabs = options.tabs.map((tab, index) => {
|
|
61
|
+
const isDefault = Boolean(tab.default || (defaultCount === 0 && index === 0));
|
|
62
|
+
return {
|
|
63
|
+
...(tab.badge !== undefined ? { badge: tab.badge } : {}),
|
|
64
|
+
...(tab.badgeTone !== undefined ? { badgeTone: tab.badgeTone } : {}),
|
|
65
|
+
...(isDefault ? { default: true } : {}),
|
|
66
|
+
...(tab.icon !== undefined ? { icon: tab.icon } : {}),
|
|
67
|
+
id: tab.id,
|
|
68
|
+
title: tab.title,
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
onChange: options.onChange,
|
|
73
|
+
tabs: wireTabs,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
export function createTabsClient(isRecordSpace, tabsTransport, nextRequestId = () => `space-tabs-${Math.random().toString(36).slice(2)}`) {
|
|
77
|
+
let hasRegistered = false;
|
|
78
|
+
let pendingRegistrationRequestId = null;
|
|
79
|
+
let committedRegistrationRequestId = null;
|
|
80
|
+
let declaredTabs = null;
|
|
81
|
+
let currentSelectedTabId = null;
|
|
82
|
+
let currentOnChange = null;
|
|
83
|
+
const invokeOnChange = (tabId) => {
|
|
84
|
+
if (currentOnChange) {
|
|
85
|
+
try {
|
|
86
|
+
const result = currentOnChange(tabId);
|
|
87
|
+
if (result && typeof result.catch === 'function') {
|
|
88
|
+
result.catch(reportOnChangeError);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
// Callback errors must not break transport/event delivery
|
|
93
|
+
reportOnChangeError(error);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
const handleTabSelected = (event) => {
|
|
98
|
+
if (tabsTransport?.isClosed?.())
|
|
99
|
+
return;
|
|
100
|
+
if (!event || typeof event !== 'object')
|
|
101
|
+
return;
|
|
102
|
+
const { registrationRequestId, selectedTabId } = event;
|
|
103
|
+
if (typeof registrationRequestId !== 'string' || typeof selectedTabId !== 'string')
|
|
104
|
+
return;
|
|
105
|
+
// "drop events from old registrations immediately after a newer set begins"
|
|
106
|
+
// "Ignore events for superseded registration IDs."
|
|
107
|
+
if (registrationRequestId !== pendingRegistrationRequestId)
|
|
108
|
+
return;
|
|
109
|
+
// "only commit selected state for the latest acknowledged registration"
|
|
110
|
+
if (!hasRegistered || registrationRequestId !== committedRegistrationRequestId)
|
|
111
|
+
return;
|
|
112
|
+
if (!declaredTabs || !declaredTabs.has(selectedTabId))
|
|
113
|
+
return;
|
|
114
|
+
if (selectedTabId === currentSelectedTabId)
|
|
115
|
+
return;
|
|
116
|
+
currentSelectedTabId = selectedTabId;
|
|
117
|
+
invokeOnChange(selectedTabId);
|
|
118
|
+
};
|
|
119
|
+
if (tabsTransport?.subscribeTabSelected) {
|
|
120
|
+
tabsTransport.subscribeTabSelected(handleTabSelected);
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
async select(tabId) {
|
|
124
|
+
if (!isRecordSpace) {
|
|
125
|
+
throw new SpaceProtocolError({
|
|
126
|
+
code: 'unavailable',
|
|
127
|
+
message: 'Tabs are available only when this Space is configured as a record view.',
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
if (!tabsTransport) {
|
|
131
|
+
throw new SpaceProtocolError({
|
|
132
|
+
code: 'unavailable',
|
|
133
|
+
message: 'The tabs bridge is unavailable for this record Space.',
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
if (!hasRegistered || !declaredTabs || !committedRegistrationRequestId)
|
|
137
|
+
throw invalidRequest('Cannot select a tab before tabs have been registered.');
|
|
138
|
+
if (typeof tabId !== 'string' || !declaredTabs.has(tabId))
|
|
139
|
+
throw invalidRequest(`Unknown tab id: "${String(tabId)}"`);
|
|
140
|
+
if (tabId === currentSelectedTabId)
|
|
141
|
+
return;
|
|
142
|
+
const request = {
|
|
143
|
+
operation: 'ui.tabs.select',
|
|
144
|
+
payload: {
|
|
145
|
+
registrationRequestId: committedRegistrationRequestId,
|
|
146
|
+
tabId,
|
|
147
|
+
},
|
|
148
|
+
protocol: PROTOCOL_VERSION,
|
|
149
|
+
requestId: nextRequestId(),
|
|
150
|
+
};
|
|
151
|
+
const response = await requestWithin(tabsTransport, request, DEFAULT_TABS_TIMEOUT_MS);
|
|
152
|
+
// A newer set makes this command obsolete. Ignore its late host response,
|
|
153
|
+
// including an error for the registration the host has already replaced.
|
|
154
|
+
if (!hasRegistered || committedRegistrationRequestId !== request.payload.registrationRequestId)
|
|
155
|
+
return;
|
|
156
|
+
const result = readResponse(response, request);
|
|
157
|
+
if (!isObjectRecord(result) || result.selectedTabId !== tabId)
|
|
158
|
+
throw invalidResponse('The host tabs selection response is malformed or did not match the requested tab.');
|
|
159
|
+
// Ignore if superseded while request was in-flight
|
|
160
|
+
if (committedRegistrationRequestId !== request.payload.registrationRequestId)
|
|
161
|
+
return;
|
|
162
|
+
if (currentSelectedTabId !== tabId) {
|
|
163
|
+
currentSelectedTabId = tabId;
|
|
164
|
+
invokeOnChange(tabId);
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
async set(options) {
|
|
168
|
+
if (!isRecordSpace) {
|
|
169
|
+
throw new SpaceProtocolError({
|
|
170
|
+
code: 'unavailable',
|
|
171
|
+
message: 'Tabs are available only when this Space is configured as a record view.',
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
if (!tabsTransport) {
|
|
175
|
+
throw new SpaceProtocolError({
|
|
176
|
+
code: 'unavailable',
|
|
177
|
+
message: 'The tabs bridge is unavailable for this record Space.',
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
const { onChange, tabs: wireTabs } = validateTabs(options);
|
|
181
|
+
const registrationRequestId = nextRequestId();
|
|
182
|
+
// On every valid set start, supersede and clear client active registration state immediately
|
|
183
|
+
pendingRegistrationRequestId = registrationRequestId;
|
|
184
|
+
currentOnChange = onChange;
|
|
185
|
+
hasRegistered = false;
|
|
186
|
+
committedRegistrationRequestId = null;
|
|
187
|
+
declaredTabs = null;
|
|
188
|
+
currentSelectedTabId = null;
|
|
189
|
+
const request = {
|
|
190
|
+
operation: 'ui.tabs.set',
|
|
191
|
+
payload: { tabs: wireTabs },
|
|
192
|
+
protocol: PROTOCOL_VERSION,
|
|
193
|
+
requestId: registrationRequestId,
|
|
194
|
+
};
|
|
195
|
+
let selectedTabId;
|
|
196
|
+
try {
|
|
197
|
+
const response = await requestWithin(tabsTransport, request, DEFAULT_TABS_TIMEOUT_MS);
|
|
198
|
+
const result = readResponse(response, request);
|
|
199
|
+
if (!isObjectRecord(result))
|
|
200
|
+
throw invalidResponse('The tabs response is malformed.');
|
|
201
|
+
if (wireTabs.length === 0) {
|
|
202
|
+
if (result.selectedTabId !== null)
|
|
203
|
+
throw invalidResponse('The host selectedTabId is malformed or does not match any registered tab.');
|
|
204
|
+
selectedTabId = null;
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
if (typeof result.selectedTabId !== 'string' || !wireTabs.some(t => t.id === result.selectedTabId))
|
|
208
|
+
throw invalidResponse('The host selectedTabId is malformed or does not match any registered tab.');
|
|
209
|
+
selectedTabId = result.selectedTabId;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
// Replacing a registration supersedes its in-flight request. Ignore a
|
|
214
|
+
// late host or transport error from that obsolete registration.
|
|
215
|
+
if (registrationRequestId !== pendingRegistrationRequestId)
|
|
216
|
+
return { selectedTabId: null };
|
|
217
|
+
throw error;
|
|
218
|
+
}
|
|
219
|
+
// "Ignore a late response from an older set."
|
|
220
|
+
if (registrationRequestId !== pendingRegistrationRequestId)
|
|
221
|
+
return { selectedTabId };
|
|
222
|
+
// "only commit selected state for the latest acknowledged registration"
|
|
223
|
+
const tabMap = new Map();
|
|
224
|
+
for (const t of wireTabs)
|
|
225
|
+
tabMap.set(t.id, t);
|
|
226
|
+
declaredTabs = tabMap;
|
|
227
|
+
committedRegistrationRequestId = registrationRequestId;
|
|
228
|
+
currentSelectedTabId = selectedTabId;
|
|
229
|
+
hasRegistered = true;
|
|
230
|
+
return { selectedTabId };
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
function reportOnChangeError(error) {
|
|
235
|
+
console.error('The Space tabs onChange callback failed.', error);
|
|
236
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { ProtocolRequest, SpaceTransport } from './protocol.js';
|
|
2
|
+
/** A value that survives a JSON round trip unchanged. */
|
|
3
|
+
export type JsonValue = boolean | JsonObject | null | number | string | readonly JsonValue[];
|
|
4
|
+
/** A JSON object: not `null`, not an array, and not a single value. */
|
|
5
|
+
export interface JsonObject {
|
|
6
|
+
readonly [key: string]: JsonValue;
|
|
7
|
+
}
|
|
8
|
+
/** The platform-standard status of a task. */
|
|
9
|
+
export type TaskStatus = 'cancelled' | 'completed' | 'failed' | 'in_progress' | 'queued' | 'waiting';
|
|
10
|
+
export interface TaskCreateInput {
|
|
11
|
+
/** The user the task is assigned to. Omit it to assign the task to its creator. */
|
|
12
|
+
assignedToId?: string;
|
|
13
|
+
/**
|
|
14
|
+
* The typed input the task type declares for its tasks. It is always a JSON
|
|
15
|
+
* object, even when the task type needs nothing: then it is `{}`.
|
|
16
|
+
*
|
|
17
|
+
* `_metadata` at its root is reserved for the platform and is not part of the
|
|
18
|
+
* task type's input. To start the task only after other tasks have ended, list
|
|
19
|
+
* them: `{ _metadata: { start_after: [{ task_id, on? }] } }`. `on` names states
|
|
20
|
+
* of the listed task's own task type that end it; left out, the states that map
|
|
21
|
+
* to `completed`. The task waits, with no model call, until every listed task
|
|
22
|
+
* has ended in a state its entry allows, and is cancelled if one ends in a
|
|
23
|
+
* state its entry does not allow. Its doer is given the `read-task-output`
|
|
24
|
+
* tool for the listed tasks. See "Space tasks" in the README.
|
|
25
|
+
*/
|
|
26
|
+
input: JsonObject;
|
|
27
|
+
/** The ID of the task type the task is created from. */
|
|
28
|
+
taskTypeId: string;
|
|
29
|
+
title: string;
|
|
30
|
+
}
|
|
31
|
+
export interface TaskCreateResult {
|
|
32
|
+
task: {
|
|
33
|
+
id: string;
|
|
34
|
+
revision: number;
|
|
35
|
+
status: TaskStatus;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export interface TaskReplyInput {
|
|
39
|
+
content: string;
|
|
40
|
+
/** The message this reply answers, when it answers one. */
|
|
41
|
+
inReplyToMessageId?: string;
|
|
42
|
+
taskId: string;
|
|
43
|
+
}
|
|
44
|
+
export interface TaskReplyResult {
|
|
45
|
+
messageId: string;
|
|
46
|
+
/** The task's revision after the reply was recorded. */
|
|
47
|
+
taskRevision: number;
|
|
48
|
+
}
|
|
49
|
+
export interface SimpleTasksClient {
|
|
50
|
+
create: (task: TaskCreateInput) => Promise<TaskCreateResult>;
|
|
51
|
+
reply: (reply: TaskReplyInput) => Promise<TaskReplyResult>;
|
|
52
|
+
}
|
|
53
|
+
export interface TaskCreateRequest extends ProtocolRequest<TaskCreateInput> {
|
|
54
|
+
operation: 'task.create';
|
|
55
|
+
}
|
|
56
|
+
export interface TaskReplyRequest extends ProtocolRequest<TaskReplyInput> {
|
|
57
|
+
operation: 'task.reply';
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Checks a task before it is sent, so a plain-JavaScript caller learns what is
|
|
61
|
+
* wrong without a host round trip. The payload names only the members the
|
|
62
|
+
* operation defines; an optional member left out is not sent at all.
|
|
63
|
+
*/
|
|
64
|
+
export declare function readTaskCreatePayload(task: TaskCreateInput): TaskCreateInput;
|
|
65
|
+
export declare function readTaskReplyPayload(reply: TaskReplyInput): TaskReplyInput;
|
|
66
|
+
/**
|
|
67
|
+
* The task.create contract takes an object for input, never null, an array, or
|
|
68
|
+
* a single value, so a caller learns that here instead of from the host.
|
|
69
|
+
*/
|
|
70
|
+
export declare function isJsonObject(value: unknown): value is JsonObject;
|
|
71
|
+
/**
|
|
72
|
+
* Accepts only what JSON carries unchanged. A structured clone would pass a
|
|
73
|
+
* Date, Map, or class instance through a MessagePort and a JSON transport
|
|
74
|
+
* would not, so the task input is held to JSON on every transport.
|
|
75
|
+
*/
|
|
76
|
+
export declare function isJsonValue(value: unknown, ancestors?: Set<object>): value is JsonValue;
|
|
77
|
+
export declare const TASK_STATUSES: ReadonlySet<unknown>;
|
|
78
|
+
export declare function isTaskCreateResult(value: unknown): value is TaskCreateResult;
|
|
79
|
+
export declare function isTaskReplyResult(value: unknown): value is TaskReplyResult;
|
|
80
|
+
/**
|
|
81
|
+
* Tasks are not tied to a page record, so they are available in any Space
|
|
82
|
+
* whose host negotiated the task protocol, standalone or record.
|
|
83
|
+
*/
|
|
84
|
+
export declare function createTasksClient(transport: SpaceTransport | undefined, nextRequestId: () => string): SimpleTasksClient;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { invalidRequest, invalidResponse, isNonBlankString, isNonNegativeInteger, isObjectRecord, PROTOCOL_VERSION, readResponse, SpaceProtocolError, } from './protocol.js';
|
|
2
|
+
/**
|
|
3
|
+
* Checks a task before it is sent, so a plain-JavaScript caller learns what is
|
|
4
|
+
* wrong without a host round trip. The payload names only the members the
|
|
5
|
+
* operation defines; an optional member left out is not sent at all.
|
|
6
|
+
*/
|
|
7
|
+
export function readTaskCreatePayload(task) {
|
|
8
|
+
if (!isObjectRecord(task))
|
|
9
|
+
throw invalidRequest('A task needs a title, a task type, and its input.');
|
|
10
|
+
if (!isNonBlankString(task.title))
|
|
11
|
+
throw invalidRequest('A task needs a title.');
|
|
12
|
+
if (!isNonBlankString(task.taskTypeId))
|
|
13
|
+
throw invalidRequest('A task needs the ID of its task type.');
|
|
14
|
+
if (!isJsonObject(task.input))
|
|
15
|
+
throw invalidRequest('A task input must be a JSON object, and everything in it a JSON value.');
|
|
16
|
+
if (task.assignedToId !== undefined && !isNonBlankString(task.assignedToId))
|
|
17
|
+
throw invalidRequest('A task assignee must be a user ID.');
|
|
18
|
+
return {
|
|
19
|
+
...(task.assignedToId === undefined ? {} : { assignedToId: task.assignedToId }),
|
|
20
|
+
input: task.input,
|
|
21
|
+
taskTypeId: task.taskTypeId,
|
|
22
|
+
title: task.title,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export function readTaskReplyPayload(reply) {
|
|
26
|
+
if (!isObjectRecord(reply))
|
|
27
|
+
throw invalidRequest('A task reply needs a task ID and its content.');
|
|
28
|
+
if (!isNonBlankString(reply.taskId))
|
|
29
|
+
throw invalidRequest('A task reply needs the ID of its task.');
|
|
30
|
+
if (!isNonBlankString(reply.content))
|
|
31
|
+
throw invalidRequest('A task reply needs content.');
|
|
32
|
+
if (reply.inReplyToMessageId !== undefined && !isNonBlankString(reply.inReplyToMessageId))
|
|
33
|
+
throw invalidRequest('A task reply can answer only a message ID.');
|
|
34
|
+
return {
|
|
35
|
+
content: reply.content,
|
|
36
|
+
...(reply.inReplyToMessageId === undefined ? {} : { inReplyToMessageId: reply.inReplyToMessageId }),
|
|
37
|
+
taskId: reply.taskId,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The task.create contract takes an object for input, never null, an array, or
|
|
42
|
+
* a single value, so a caller learns that here instead of from the host.
|
|
43
|
+
*/
|
|
44
|
+
export function isJsonObject(value) {
|
|
45
|
+
return isObjectRecord(value) && isJsonValue(value);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Accepts only what JSON carries unchanged. A structured clone would pass a
|
|
49
|
+
* Date, Map, or class instance through a MessagePort and a JSON transport
|
|
50
|
+
* would not, so the task input is held to JSON on every transport.
|
|
51
|
+
*/
|
|
52
|
+
export function isJsonValue(value, ancestors = new Set()) {
|
|
53
|
+
if (value === null || typeof value === 'boolean' || typeof value === 'string')
|
|
54
|
+
return true;
|
|
55
|
+
if (typeof value === 'number')
|
|
56
|
+
return Number.isFinite(value);
|
|
57
|
+
if (!value || typeof value !== 'object' || ancestors.has(value))
|
|
58
|
+
return false;
|
|
59
|
+
const prototype = Object.getPrototypeOf(value);
|
|
60
|
+
if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null)
|
|
61
|
+
return false;
|
|
62
|
+
ancestors.add(value);
|
|
63
|
+
const valid = Object.values(value).every(child => isJsonValue(child, ancestors));
|
|
64
|
+
ancestors.delete(value);
|
|
65
|
+
return valid;
|
|
66
|
+
}
|
|
67
|
+
export const TASK_STATUSES = new Set([
|
|
68
|
+
'cancelled',
|
|
69
|
+
'completed',
|
|
70
|
+
'failed',
|
|
71
|
+
'in_progress',
|
|
72
|
+
'queued',
|
|
73
|
+
'waiting',
|
|
74
|
+
]);
|
|
75
|
+
export function isTaskCreateResult(value) {
|
|
76
|
+
if (!isObjectRecord(value) || !isObjectRecord(value.task))
|
|
77
|
+
return false;
|
|
78
|
+
const { id, revision, status } = value.task;
|
|
79
|
+
return isNonBlankString(id) && isNonNegativeInteger(revision) && TASK_STATUSES.has(status);
|
|
80
|
+
}
|
|
81
|
+
export function isTaskReplyResult(value) {
|
|
82
|
+
return isObjectRecord(value) && isNonBlankString(value.messageId) && isNonNegativeInteger(value.taskRevision);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Tasks are not tied to a page record, so they are available in any Space
|
|
86
|
+
* whose host negotiated the task protocol, standalone or record.
|
|
87
|
+
*/
|
|
88
|
+
export function createTasksClient(transport, nextRequestId) {
|
|
89
|
+
const requireTransport = () => {
|
|
90
|
+
if (!transport) {
|
|
91
|
+
throw new SpaceProtocolError({
|
|
92
|
+
code: 'unavailable',
|
|
93
|
+
message: 'Tasks are unavailable because the Space host did not negotiate the task protocol.',
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return transport;
|
|
97
|
+
};
|
|
98
|
+
return {
|
|
99
|
+
async create(task) {
|
|
100
|
+
const taskTransport = requireTransport();
|
|
101
|
+
const request = {
|
|
102
|
+
operation: 'task.create',
|
|
103
|
+
payload: readTaskCreatePayload(task),
|
|
104
|
+
protocol: PROTOCOL_VERSION,
|
|
105
|
+
requestId: nextRequestId(),
|
|
106
|
+
};
|
|
107
|
+
const response = await taskTransport.request(request);
|
|
108
|
+
const result = readResponse(response, request);
|
|
109
|
+
if (!isTaskCreateResult(result))
|
|
110
|
+
throw invalidResponse('The task-create response is malformed.');
|
|
111
|
+
const { id, revision, status } = result.task;
|
|
112
|
+
return { task: { id, revision, status } };
|
|
113
|
+
},
|
|
114
|
+
async reply(reply) {
|
|
115
|
+
const taskTransport = requireTransport();
|
|
116
|
+
const request = {
|
|
117
|
+
operation: 'task.reply',
|
|
118
|
+
payload: readTaskReplyPayload(reply),
|
|
119
|
+
protocol: PROTOCOL_VERSION,
|
|
120
|
+
requestId: nextRequestId(),
|
|
121
|
+
};
|
|
122
|
+
const response = await taskTransport.request(request);
|
|
123
|
+
const result = readResponse(response, request);
|
|
124
|
+
if (!isTaskReplyResult(result))
|
|
125
|
+
throw invalidResponse('The task-reply response is malformed.');
|
|
126
|
+
return { messageId: result.messageId, taskRevision: result.taskRevision };
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { SpaceStatusTone } from './status.js';
|
|
2
|
+
export declare const HEADER_ACTIONS_PROTOCOL_VERSION: 1;
|
|
3
|
+
export declare const HEADER_STATUS_PROTOCOL_VERSION: 1;
|
|
4
|
+
export declare const TOAST_PROTOCOL_VERSION: 1;
|
|
5
|
+
export type { SpaceStatusTone } from './status.js';
|
|
6
|
+
export { createTabsClient, DEFAULT_TABS_TIMEOUT_MS, LUCIDE_ICON_NAME, TAB_ID, TABS_PROTOCOL_VERSION, validateTabs, } from './tabs.js';
|
|
7
|
+
export type { SetTabsOptions, SimpleTabsClient, SpaceTab, SpaceTabsSelectionEvent, SpaceTabsTransport, TabsSelectPayload, TabsSelectRequest, TabsSelectResult, TabsSetPayload, TabsSetRequest, TabsSetResult, WireTab, } from './tabs.js';
|
|
8
|
+
export type HeaderActionType = 'destructive' | 'outline' | 'primary' | 'secondary';
|
|
9
|
+
/**
|
|
10
|
+
* A platform-rendered action supplied by a mounted Record Space.
|
|
11
|
+
*
|
|
12
|
+
* The callback stays in the Space. The host receives only presentation state
|
|
13
|
+
* and asks the Space to invoke the callback when its button is clicked.
|
|
14
|
+
*/
|
|
15
|
+
export interface HeaderAction {
|
|
16
|
+
disabled?: boolean;
|
|
17
|
+
icon?: string;
|
|
18
|
+
id: string;
|
|
19
|
+
label: string;
|
|
20
|
+
loading?: boolean;
|
|
21
|
+
onClick: () => Promise<void> | void;
|
|
22
|
+
type?: HeaderActionType;
|
|
23
|
+
}
|
|
24
|
+
export interface SimpleHeaderActionsClient {
|
|
25
|
+
set: (actions: readonly HeaderAction[]) => void;
|
|
26
|
+
}
|
|
27
|
+
export interface SpaceHeaderStatus {
|
|
28
|
+
description?: string;
|
|
29
|
+
label: string;
|
|
30
|
+
tone: SpaceStatusTone;
|
|
31
|
+
}
|
|
32
|
+
export interface SimpleHeaderStatusClient {
|
|
33
|
+
set: (status: SpaceHeaderStatus | null) => void;
|
|
34
|
+
}
|
|
35
|
+
export interface SimpleHeaderClient {
|
|
36
|
+
actions: SimpleHeaderActionsClient;
|
|
37
|
+
status: SimpleHeaderStatusClient;
|
|
38
|
+
}
|
|
39
|
+
export interface SpaceHeaderTransport {
|
|
40
|
+
setActions: (actions: readonly HeaderAction[]) => void;
|
|
41
|
+
}
|
|
42
|
+
export interface SpaceHeaderStatusTransport {
|
|
43
|
+
setStatus: (status: SpaceHeaderStatus | null) => void;
|
|
44
|
+
}
|
|
45
|
+
export type SpaceToastVariant = 'default' | 'destructive';
|
|
46
|
+
export type SpaceToastOptions = {
|
|
47
|
+
variant?: SpaceToastVariant;
|
|
48
|
+
} & ({
|
|
49
|
+
description: string;
|
|
50
|
+
title?: string;
|
|
51
|
+
} | {
|
|
52
|
+
description?: string;
|
|
53
|
+
title: string;
|
|
54
|
+
});
|
|
55
|
+
export interface SimpleToastClient {
|
|
56
|
+
show: (options: SpaceToastOptions) => void;
|
|
57
|
+
}
|
|
58
|
+
export interface SpaceToastTransport {
|
|
59
|
+
showToast: (options: SpaceToastOptions) => void;
|
|
60
|
+
}
|
|
61
|
+
export declare function validateHeaderActions(actions: readonly HeaderAction[]): readonly HeaderAction[];
|
|
62
|
+
export declare const MAX_TOAST_TITLE_LENGTH = 160;
|
|
63
|
+
export declare const MAX_TOAST_DESCRIPTION_LENGTH = 1000;
|
|
64
|
+
export declare function validateSpaceToastOptions(options: SpaceToastOptions): SpaceToastOptions;
|
|
65
|
+
export declare function createHeaderClient(isRecordSpace: boolean, headerTransport?: SpaceHeaderTransport, headerStatusTransport?: SpaceHeaderStatusTransport): SimpleHeaderClient;
|
|
66
|
+
export declare function validateHeaderStatus(status: SpaceHeaderStatus | null): SpaceHeaderStatus | null;
|
|
67
|
+
export declare function createToastClient(toastTransport?: SpaceToastTransport): SimpleToastClient;
|
package/dist/space/ui.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { invalidRequest, isObjectRecord, SpaceProtocolError } from './protocol.js';
|
|
2
|
+
import { SPACE_STATUS_TONES } from './status.js';
|
|
3
|
+
export const HEADER_ACTIONS_PROTOCOL_VERSION = 1;
|
|
4
|
+
export const HEADER_STATUS_PROTOCOL_VERSION = 1;
|
|
5
|
+
export const TOAST_PROTOCOL_VERSION = 1;
|
|
6
|
+
export { createTabsClient, DEFAULT_TABS_TIMEOUT_MS, LUCIDE_ICON_NAME, TAB_ID, TABS_PROTOCOL_VERSION, validateTabs, } from './tabs.js';
|
|
7
|
+
export function validateHeaderActions(actions) {
|
|
8
|
+
const ids = new Set();
|
|
9
|
+
return actions.map((action) => {
|
|
10
|
+
if (!action || typeof action !== 'object'
|
|
11
|
+
|| typeof action.id !== 'string' || !action.id.trim()
|
|
12
|
+
|| typeof action.label !== 'string' || !action.label.trim()
|
|
13
|
+
|| typeof action.onClick !== 'function') {
|
|
14
|
+
throw new TypeError('Each header action requires a non-empty id, a non-empty label, and an onClick callback.');
|
|
15
|
+
}
|
|
16
|
+
if (ids.has(action.id)) {
|
|
17
|
+
throw new TypeError(`Header action ids must be unique: ${action.id}`);
|
|
18
|
+
}
|
|
19
|
+
ids.add(action.id);
|
|
20
|
+
if (action.type && !['destructive', 'outline', 'primary', 'secondary'].includes(action.type)) {
|
|
21
|
+
throw new TypeError(`Unsupported header action type: ${action.type}`);
|
|
22
|
+
}
|
|
23
|
+
if (action.disabled !== undefined && typeof action.disabled !== 'boolean')
|
|
24
|
+
throw new TypeError(`Header action disabled must be a boolean: ${action.id}`);
|
|
25
|
+
if (action.loading !== undefined && typeof action.loading !== 'boolean')
|
|
26
|
+
throw new TypeError(`Header action loading must be a boolean: ${action.id}`);
|
|
27
|
+
if (action.icon !== undefined && typeof action.icon !== 'string')
|
|
28
|
+
throw new TypeError(`Header action icon must be a string: ${action.id}`);
|
|
29
|
+
return { ...action };
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
export const MAX_TOAST_TITLE_LENGTH = 160;
|
|
33
|
+
export const MAX_TOAST_DESCRIPTION_LENGTH = 1000;
|
|
34
|
+
export function validateSpaceToastOptions(options) {
|
|
35
|
+
if (!options || typeof options !== 'object' || Array.isArray(options))
|
|
36
|
+
throw new TypeError('Toast options must be an object.');
|
|
37
|
+
const candidate = options;
|
|
38
|
+
if (candidate.title !== undefined
|
|
39
|
+
&& (typeof candidate.title !== 'string' || !candidate.title.trim() || candidate.title.length > MAX_TOAST_TITLE_LENGTH)) {
|
|
40
|
+
throw new TypeError(`Toast title must be a non-empty string of at most ${MAX_TOAST_TITLE_LENGTH} characters.`);
|
|
41
|
+
}
|
|
42
|
+
if (candidate.description !== undefined
|
|
43
|
+
&& (typeof candidate.description !== 'string' || !candidate.description.trim() || candidate.description.length > MAX_TOAST_DESCRIPTION_LENGTH)) {
|
|
44
|
+
throw new TypeError(`Toast description must be a non-empty string of at most ${MAX_TOAST_DESCRIPTION_LENGTH} characters.`);
|
|
45
|
+
}
|
|
46
|
+
if (candidate.title === undefined && candidate.description === undefined)
|
|
47
|
+
throw new TypeError('A toast requires a title or description.');
|
|
48
|
+
if (candidate.variant !== undefined && candidate.variant !== 'default' && candidate.variant !== 'destructive')
|
|
49
|
+
throw new TypeError(`Unsupported toast variant: ${String(candidate.variant)}`);
|
|
50
|
+
return {
|
|
51
|
+
...(candidate.description === undefined ? {} : { description: candidate.description }),
|
|
52
|
+
...(candidate.title === undefined ? {} : { title: candidate.title }),
|
|
53
|
+
...(candidate.variant === undefined ? {} : { variant: candidate.variant }),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export function createHeaderClient(isRecordSpace, headerTransport, headerStatusTransport) {
|
|
57
|
+
return {
|
|
58
|
+
actions: {
|
|
59
|
+
set(actions) {
|
|
60
|
+
if (!isRecordSpace) {
|
|
61
|
+
throw new SpaceProtocolError({
|
|
62
|
+
code: 'unavailable',
|
|
63
|
+
message: 'Header actions are available only when this Space is configured as a record view.',
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (!headerTransport) {
|
|
67
|
+
throw new SpaceProtocolError({
|
|
68
|
+
code: 'unavailable',
|
|
69
|
+
message: 'The header-action bridge is unavailable for this record Space.',
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
headerTransport.setActions(validateHeaderActions(actions));
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
status: {
|
|
76
|
+
set(status) {
|
|
77
|
+
if (!isRecordSpace) {
|
|
78
|
+
throw new SpaceProtocolError({
|
|
79
|
+
code: 'unavailable',
|
|
80
|
+
message: 'Header status is available only when this Space is configured as a record view.',
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
if (!headerStatusTransport) {
|
|
84
|
+
throw new SpaceProtocolError({
|
|
85
|
+
code: 'unavailable',
|
|
86
|
+
message: 'The header-status bridge is unavailable for this record Space.',
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
headerStatusTransport.setStatus(validateHeaderStatus(status));
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
export function validateHeaderStatus(status) {
|
|
95
|
+
if (status === null)
|
|
96
|
+
return null;
|
|
97
|
+
if (!isObjectRecord(status))
|
|
98
|
+
throw invalidRequest('Header status must be an object or null.');
|
|
99
|
+
if (typeof status.label !== 'string' || !status.label.trim() || status.label.length > 80)
|
|
100
|
+
throw invalidRequest('Header status label must be a non-empty string of at most 80 characters.');
|
|
101
|
+
if (typeof status.tone !== 'string' || !SPACE_STATUS_TONES.includes(status.tone))
|
|
102
|
+
throw invalidRequest(`Header status tone must be one of: ${SPACE_STATUS_TONES.join(', ')}.`);
|
|
103
|
+
if (status.description !== undefined && typeof status.description !== 'string')
|
|
104
|
+
throw invalidRequest('Header status description must be a string.');
|
|
105
|
+
return {
|
|
106
|
+
...(status.description === undefined ? {} : { description: status.description }),
|
|
107
|
+
label: status.label,
|
|
108
|
+
tone: status.tone,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
export function createToastClient(toastTransport) {
|
|
112
|
+
return {
|
|
113
|
+
show(options) {
|
|
114
|
+
if (!toastTransport) {
|
|
115
|
+
throw new SpaceProtocolError({
|
|
116
|
+
code: 'unavailable',
|
|
117
|
+
message: 'The platform-toast bridge is unavailable for this Space.',
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
toastTransport.showToast(validateSpaceToastOptions(options));
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
}
|
package/dist/tasks.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Context } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a task through the platform task service.
|
|
4
|
+
*
|
|
5
|
+
* The task is assigned to the user the action is running as. The platform
|
|
6
|
+
* creates both the task and its first message ids.
|
|
7
|
+
*/
|
|
8
|
+
export declare function create(options: {
|
|
9
|
+
taskTypeId: string;
|
|
10
|
+
title: string;
|
|
11
|
+
input: Record<string, unknown>;
|
|
12
|
+
}, context: Context): Promise<{
|
|
13
|
+
task: {
|
|
14
|
+
id: string;
|
|
15
|
+
revision: number;
|
|
16
|
+
status: string;
|
|
17
|
+
};
|
|
18
|
+
}>;
|