@weave-apps/sdk 0.9.0 → 0.11.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/WeaveAPIClient.d.ts +49 -0
- package/dist/WeaveAPIClient.d.ts.map +1 -1
- package/dist/WeaveDOMAPI.d.ts +66 -0
- package/dist/WeaveDOMAPI.d.ts.map +1 -1
- package/dist/WeaveDOMAPI.js +51 -0
- package/dist/apis/api-mono/services/interfaces/WorkflowsTriggersWorkflowCompanyIdPostPost.d.ts +224 -0
- package/dist/apis/api-mono/services/interfaces/WorkflowsTriggersWorkflowCompanyIdPostPost.d.ts.map +1 -0
- package/dist/apis/api-mono/services/interfaces/WorkflowsTriggersWorkflowCompanyIdPostPost.js +66 -0
- package/dist/app-sdk/src/WeaveAPIClient.d.ts +370 -0
- package/dist/app-sdk/src/WeaveAPIClient.d.ts.map +1 -0
- package/dist/app-sdk/src/WeaveAPIClient.js +361 -0
- package/dist/app-sdk/src/WeaveAppInstanceAPI.d.ts +237 -0
- package/dist/app-sdk/src/WeaveAppInstanceAPI.d.ts.map +1 -0
- package/dist/app-sdk/src/WeaveAppInstanceAPI.js +395 -0
- package/dist/app-sdk/src/WeaveBackgroundAPI.d.ts +81 -0
- package/dist/app-sdk/src/WeaveBackgroundAPI.d.ts.map +1 -0
- package/dist/app-sdk/src/WeaveBackgroundAPI.js +165 -0
- package/dist/app-sdk/src/WeaveBaseApp.d.ts +318 -0
- package/dist/app-sdk/src/WeaveBaseApp.d.ts.map +1 -0
- package/dist/app-sdk/src/WeaveBaseApp.js +434 -0
- package/dist/app-sdk/src/WeaveCronAPI.d.ts +68 -0
- package/dist/app-sdk/src/WeaveCronAPI.d.ts.map +1 -0
- package/dist/app-sdk/src/WeaveCronAPI.js +172 -0
- package/dist/app-sdk/src/WeaveDOMAPI.d.ts +593 -0
- package/dist/app-sdk/src/WeaveDOMAPI.d.ts.map +1 -0
- package/dist/app-sdk/src/WeaveDOMAPI.js +774 -0
- package/dist/app-sdk/src/global.d.ts +25 -0
- package/dist/app-sdk/src/global.d.ts.map +1 -0
- package/dist/app-sdk/src/global.js +23 -0
- package/dist/app-sdk/src/index.d.ts +14 -0
- package/dist/app-sdk/src/index.d.ts.map +1 -0
- package/dist/app-sdk/src/index.js +14 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +3 -3
- package/templates/WEAVE_SPEC.md +417 -1
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Weave API Client
|
|
3
|
+
*
|
|
4
|
+
* Client-side API for iframe apps to interact with the Weave backend
|
|
5
|
+
* through a secure proxy in the sidebar.
|
|
6
|
+
*
|
|
7
|
+
* Apps can call AI services and manage their own app data.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* API operation types
|
|
11
|
+
*/
|
|
12
|
+
var APIOperation;
|
|
13
|
+
(function (APIOperation) {
|
|
14
|
+
// AI Service
|
|
15
|
+
APIOperation["AI_CHAT"] = "AI_CHAT";
|
|
16
|
+
// Company Members Service
|
|
17
|
+
APIOperation["COMPANY_MEMBERS_GET"] = "COMPANY_MEMBERS_GET";
|
|
18
|
+
// App Data Service
|
|
19
|
+
APIOperation["APP_DATA_GET_ALL"] = "APP_DATA_GET_ALL";
|
|
20
|
+
APIOperation["APP_DATA_CREATE"] = "APP_DATA_CREATE";
|
|
21
|
+
APIOperation["APP_DATA_GET"] = "APP_DATA_GET";
|
|
22
|
+
APIOperation["APP_DATA_UPDATE"] = "APP_DATA_UPDATE";
|
|
23
|
+
APIOperation["APP_DATA_DELETE"] = "APP_DATA_DELETE";
|
|
24
|
+
// Forms Service
|
|
25
|
+
APIOperation["FORMS_GET_ALL"] = "FORMS_GET_ALL";
|
|
26
|
+
APIOperation["FORMS_GET"] = "FORMS_GET";
|
|
27
|
+
APIOperation["FORMS_GET_DATA"] = "FORMS_GET_DATA";
|
|
28
|
+
APIOperation["FORMS_SUBMIT_DATA"] = "FORMS_SUBMIT_DATA";
|
|
29
|
+
})(APIOperation || (APIOperation = {}));
|
|
30
|
+
/**
|
|
31
|
+
* Weave API Client
|
|
32
|
+
* Provides methods for iframe apps to interact with Weave backend
|
|
33
|
+
*/
|
|
34
|
+
export class WeaveAPIClient {
|
|
35
|
+
constructor() {
|
|
36
|
+
this.pendingRequests = new Map();
|
|
37
|
+
this.messageListener = null;
|
|
38
|
+
this.requestCounter = 0;
|
|
39
|
+
this.timeout = 180000; // 3 minute timeout for API calls (AI can take time on large forms)
|
|
40
|
+
this.appId = null; // App ID set by the app
|
|
41
|
+
// ============================================================================
|
|
42
|
+
// AI Service
|
|
43
|
+
// ============================================================================
|
|
44
|
+
/**
|
|
45
|
+
* Send a chat message to the AI service
|
|
46
|
+
*
|
|
47
|
+
* Note: App ID is automatically injected by the APIBridge.
|
|
48
|
+
* You only need to provide the prompt and optional context.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```javascript
|
|
52
|
+
* const response = await weaveAPI.ai.chat({
|
|
53
|
+
* prompt: 'What is the capital of France?',
|
|
54
|
+
* context: 'User is learning geography'
|
|
55
|
+
* });
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
this.ai = {
|
|
59
|
+
chat: async (request) => {
|
|
60
|
+
return this.sendRequest(APIOperation.AI_CHAT, request);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
// =========================================================================
|
|
64
|
+
// App Data Service
|
|
65
|
+
// =========================================================================
|
|
66
|
+
/**
|
|
67
|
+
* App Data operations - CRUD for app-specific data storage
|
|
68
|
+
*/
|
|
69
|
+
this.appData = {
|
|
70
|
+
/**
|
|
71
|
+
* Get all app data for the current company (paginated)
|
|
72
|
+
*
|
|
73
|
+
* Returns a paginated response with data and metadata.
|
|
74
|
+
* Default limit is 25 items. For apps with large datasets (500-5000+ rows),
|
|
75
|
+
* use pagination to avoid performance issues.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```javascript
|
|
79
|
+
* // Get first page (default: 25 items)
|
|
80
|
+
* const response = await weaveAPI.appData.getAll();
|
|
81
|
+
*
|
|
82
|
+
* // Access the array of items
|
|
83
|
+
* const items = response.data;
|
|
84
|
+
* items.forEach(item => {
|
|
85
|
+
* });
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
getAll: async () => {
|
|
89
|
+
return this.sendRequest(APIOperation.APP_DATA_GET_ALL, {});
|
|
90
|
+
},
|
|
91
|
+
/**
|
|
92
|
+
* Create new app data
|
|
93
|
+
*
|
|
94
|
+
* Note: App ID is automatically injected by the APIBridge.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```javascript
|
|
98
|
+
* const newData = await weaveAPI.appData.create({
|
|
99
|
+
* dataKey: 'user-preferences',
|
|
100
|
+
* data: { theme: 'dark', language: 'en' }
|
|
101
|
+
* });
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
create: async (request) => {
|
|
105
|
+
return this.sendRequest(APIOperation.APP_DATA_CREATE, request);
|
|
106
|
+
},
|
|
107
|
+
/**
|
|
108
|
+
* Get specific app data by ID
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```javascript
|
|
112
|
+
* const data = await weaveAPI.appData.get('data-id-123');
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
get: async (appDataId) => {
|
|
116
|
+
return this.sendRequest(APIOperation.APP_DATA_GET, { appDataId });
|
|
117
|
+
},
|
|
118
|
+
/**
|
|
119
|
+
* Update existing app data
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```javascript
|
|
123
|
+
* const updated = await weaveAPI.appData.update('data-id-123', {
|
|
124
|
+
* data: { theme: 'light' }
|
|
125
|
+
* });
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
update: async (appDataId, request) => {
|
|
129
|
+
return this.sendRequest(APIOperation.APP_DATA_UPDATE, {
|
|
130
|
+
appDataId,
|
|
131
|
+
...request
|
|
132
|
+
});
|
|
133
|
+
},
|
|
134
|
+
/**
|
|
135
|
+
* Delete app data
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```javascript
|
|
139
|
+
* await weaveAPI.appData.delete('data-id-123');
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
delete: async (appDataId) => {
|
|
143
|
+
return this.sendRequest(APIOperation.APP_DATA_DELETE, { appDataId });
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
// =========================================================================
|
|
147
|
+
// Company Members Service
|
|
148
|
+
// =========================================================================
|
|
149
|
+
/**
|
|
150
|
+
* Company member operations for app/user discovery scenarios.
|
|
151
|
+
*/
|
|
152
|
+
this.companyMembers = {
|
|
153
|
+
/**
|
|
154
|
+
* Get company members scoped by current user's location (supports ancestor traversal).
|
|
155
|
+
*
|
|
156
|
+
* Defaults to current app context (`targetType: 'app'`, `targetId` from app ID).
|
|
157
|
+
*/
|
|
158
|
+
getAll: async (options = {}) => {
|
|
159
|
+
return this.sendRequest(APIOperation.COMPANY_MEMBERS_GET, options);
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* Forms API
|
|
164
|
+
*
|
|
165
|
+
* Methods for fetching company forms and submitting form data.
|
|
166
|
+
*/
|
|
167
|
+
this.forms = {
|
|
168
|
+
/**
|
|
169
|
+
* Get all forms for the company
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```javascript
|
|
173
|
+
* const response = await weaveAPI.forms.getAll();
|
|
174
|
+
* console.log(response.data); // Array of forms
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
getAll: async () => {
|
|
178
|
+
return this.sendRequest(APIOperation.FORMS_GET_ALL, {});
|
|
179
|
+
},
|
|
180
|
+
/**
|
|
181
|
+
* Get a specific form by ID
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```javascript
|
|
185
|
+
* const form = await weaveAPI.forms.get('form-id-123');
|
|
186
|
+
* console.log(form.name, form.fields);
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
get: async (formId) => {
|
|
190
|
+
return this.sendRequest(APIOperation.FORMS_GET, { formId });
|
|
191
|
+
},
|
|
192
|
+
/**
|
|
193
|
+
* Get all submissions (app data) for a specific form
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```javascript
|
|
197
|
+
* const response = await weaveAPI.forms.getFormData('form-id-123');
|
|
198
|
+
* console.log(response.data); // Array of form submissions
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
getFormData: async (formId, scopeOptions = {}) => {
|
|
202
|
+
return this.sendRequest(APIOperation.FORMS_GET_DATA, {
|
|
203
|
+
formId,
|
|
204
|
+
...scopeOptions,
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
/**
|
|
208
|
+
* Submit form data
|
|
209
|
+
* Creates app data with targetType: 'form' and targetId: formId
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```javascript
|
|
213
|
+
* await weaveAPI.forms.submitFormData('form-id-123', {
|
|
214
|
+
* dataKey: 'form_submission',
|
|
215
|
+
* data: {
|
|
216
|
+
* formName: 'User Signup',
|
|
217
|
+
* responses: { field1: 'value1', field2: 'value2' }
|
|
218
|
+
* }
|
|
219
|
+
* });
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
submitFormData: async (formId, payload) => {
|
|
223
|
+
return this.sendRequest(APIOperation.FORMS_SUBMIT_DATA, { formId, ...payload });
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
/**
|
|
227
|
+
* Utility functions for common operations
|
|
228
|
+
*
|
|
229
|
+
* Note: These utilities are loaded from the sidebar-loader at runtime.
|
|
230
|
+
* They are available through the window object and don't need to be bundled with apps.
|
|
231
|
+
*/
|
|
232
|
+
this.utils = {
|
|
233
|
+
/**
|
|
234
|
+
* Convert HTML to Markdown
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```javascript
|
|
238
|
+
* const markdown = weaveAPI.utils.htmlToMarkdown('<h1>Hello</h1>');
|
|
239
|
+
* console.log(markdown); // # Hello
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
htmlToMarkdown: (html) => {
|
|
243
|
+
// Access the utility from window object (loaded by sidebar-loader)
|
|
244
|
+
if (typeof window !== 'undefined' && window.__weaveUtils?.htmlToMarkdown) {
|
|
245
|
+
return window.__weaveUtils.htmlToMarkdown(html);
|
|
246
|
+
}
|
|
247
|
+
throw new Error('htmlToMarkdown utility not available. Ensure sidebar-loader is loaded.');
|
|
248
|
+
},
|
|
249
|
+
/**
|
|
250
|
+
* Convert Markdown to HTML (sanitized)
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```javascript
|
|
254
|
+
* const html = weaveAPI.utils.markdownToHtml('# Hello');
|
|
255
|
+
* console.log(html); // <h1>Hello</h1>
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
markdownToHtml: (markdown) => {
|
|
259
|
+
// Access the utility from window object (loaded by sidebar-loader)
|
|
260
|
+
if (typeof window !== 'undefined' && window.__weaveUtils?.markdownToHtml) {
|
|
261
|
+
return window.__weaveUtils.markdownToHtml(markdown);
|
|
262
|
+
}
|
|
263
|
+
throw new Error('markdownToHtml utility not available. Ensure sidebar-loader is loaded.');
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
this.initialize();
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Set the app ID for this client
|
|
270
|
+
* Called automatically by WeaveBaseApp
|
|
271
|
+
*/
|
|
272
|
+
setAppId(appId) {
|
|
273
|
+
this.appId = appId;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Initialize the API client and start listening for responses
|
|
277
|
+
*/
|
|
278
|
+
initialize() {
|
|
279
|
+
this.messageListener = (event) => {
|
|
280
|
+
this.handleResponse(event);
|
|
281
|
+
};
|
|
282
|
+
window.addEventListener('message', this.messageListener);
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Cleanup
|
|
286
|
+
*/
|
|
287
|
+
destroy() {
|
|
288
|
+
if (this.messageListener) {
|
|
289
|
+
window.removeEventListener('message', this.messageListener);
|
|
290
|
+
this.messageListener = null;
|
|
291
|
+
}
|
|
292
|
+
this.pendingRequests.clear();
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Handle response from sidebar
|
|
296
|
+
*/
|
|
297
|
+
handleResponse(event) {
|
|
298
|
+
const data = event.data;
|
|
299
|
+
if (data?.type !== 'WEAVE_API_RESPONSE') {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
const response = data;
|
|
303
|
+
const pending = this.pendingRequests.get(response.id);
|
|
304
|
+
if (!pending) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
this.pendingRequests.delete(response.id);
|
|
308
|
+
if (response.success) {
|
|
309
|
+
pending.resolve(response.data);
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
pending.reject(new Error(response.error || 'Unknown error'));
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Send request to sidebar
|
|
317
|
+
*/
|
|
318
|
+
sendRequest(operation, payload) {
|
|
319
|
+
return new Promise((resolve, reject) => {
|
|
320
|
+
const id = `api-request-${++this.requestCounter}`;
|
|
321
|
+
const request = {
|
|
322
|
+
type: 'WEAVE_API_REQUEST',
|
|
323
|
+
id,
|
|
324
|
+
operation,
|
|
325
|
+
payload,
|
|
326
|
+
appId: this.appId || undefined, // Include app ID if set
|
|
327
|
+
};
|
|
328
|
+
// Store pending request
|
|
329
|
+
this.pendingRequests.set(id, { resolve, reject });
|
|
330
|
+
// Set timeout
|
|
331
|
+
const timeoutId = setTimeout(() => {
|
|
332
|
+
this.pendingRequests.delete(id);
|
|
333
|
+
reject(new Error(`Request timeout: ${operation}`));
|
|
334
|
+
}, this.timeout);
|
|
335
|
+
// Clear timeout on resolve/reject
|
|
336
|
+
const originalResolve = resolve;
|
|
337
|
+
const originalReject = reject;
|
|
338
|
+
this.pendingRequests.set(id, {
|
|
339
|
+
resolve: (value) => {
|
|
340
|
+
clearTimeout(timeoutId);
|
|
341
|
+
originalResolve(value);
|
|
342
|
+
},
|
|
343
|
+
reject: (error) => {
|
|
344
|
+
clearTimeout(timeoutId);
|
|
345
|
+
originalReject(error);
|
|
346
|
+
},
|
|
347
|
+
});
|
|
348
|
+
// Send to current window (sidebar APIBridge will intercept)
|
|
349
|
+
// Apps run in the sidebar's window, not in a separate iframe
|
|
350
|
+
window.postMessage(request, '*');
|
|
351
|
+
APIOperation;
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
// Create singleton instance
|
|
356
|
+
const weaveAPI = new WeaveAPIClient();
|
|
357
|
+
// Export for global usage
|
|
358
|
+
if (typeof window !== 'undefined') {
|
|
359
|
+
window.weaveAPI = weaveAPI;
|
|
360
|
+
}
|
|
361
|
+
export default weaveAPI;
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WeaveAppInstanceAPI
|
|
3
|
+
*
|
|
4
|
+
* Client-side API for Weave apps to discover and communicate with
|
|
5
|
+
* other instances of themselves across browser tabs.
|
|
6
|
+
*
|
|
7
|
+
* Use cases:
|
|
8
|
+
* - Prevent duplicate automation runs across tabs
|
|
9
|
+
* - Coordinate which tab is "controlling" vs "observing"
|
|
10
|
+
* - Send messages between app instances
|
|
11
|
+
*
|
|
12
|
+
* Usage:
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Register this instance
|
|
15
|
+
* const instances = await this.instances.register({ url: window.location.href });
|
|
16
|
+
*
|
|
17
|
+
* // Claim controller status (for automation)
|
|
18
|
+
* const claimed = await this.instances.claimController();
|
|
19
|
+
*
|
|
20
|
+
* // Listen for messages from other instances
|
|
21
|
+
* this.instances.onMessage((msg, sender) => {
|
|
22
|
+
* if (msg.type === 'YIELD_CONTROL') {
|
|
23
|
+
* // Another tab wants control
|
|
24
|
+
* }
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // Broadcast to all other instances
|
|
28
|
+
* await this.instances.broadcast({ type: 'YIELD_CONTROL' });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* Represents a single app instance in a tab
|
|
33
|
+
*/
|
|
34
|
+
export interface AppInstance {
|
|
35
|
+
appId: string;
|
|
36
|
+
tabId: number;
|
|
37
|
+
url: string;
|
|
38
|
+
isController: boolean;
|
|
39
|
+
registeredAt: number;
|
|
40
|
+
metadata?: Record<string, any>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Message sent between app instances
|
|
44
|
+
*/
|
|
45
|
+
export interface InstanceMessage {
|
|
46
|
+
type: string;
|
|
47
|
+
data?: any;
|
|
48
|
+
senderId: number;
|
|
49
|
+
timestamp: number;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Callback for instance messages
|
|
53
|
+
*/
|
|
54
|
+
export type InstanceMessageCallback = (message: InstanceMessage) => void;
|
|
55
|
+
/**
|
|
56
|
+
* Callback for instance changes
|
|
57
|
+
*/
|
|
58
|
+
export type InstanceChangeCallback = (instances: AppInstance[]) => void;
|
|
59
|
+
/**
|
|
60
|
+
* WeaveAppInstanceAPI class
|
|
61
|
+
*
|
|
62
|
+
* Provides methods for apps to discover and communicate with
|
|
63
|
+
* other instances of themselves across browser tabs.
|
|
64
|
+
*/
|
|
65
|
+
export declare class WeaveAppInstanceAPI {
|
|
66
|
+
private appId;
|
|
67
|
+
private _tabId;
|
|
68
|
+
constructor(appId: string);
|
|
69
|
+
/**
|
|
70
|
+
* Get this instance's tab ID (available after register)
|
|
71
|
+
*/
|
|
72
|
+
get tabId(): number | null;
|
|
73
|
+
/**
|
|
74
|
+
* Register this app instance
|
|
75
|
+
* Call this when your app starts to make it discoverable by other instances.
|
|
76
|
+
*
|
|
77
|
+
* @param metadata - Optional metadata to associate with this instance
|
|
78
|
+
* @returns All current instances of this app (including this one)
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* const instances = await this.instances.register({
|
|
82
|
+
* url: window.location.href,
|
|
83
|
+
* startedAt: Date.now()
|
|
84
|
+
* });
|
|
85
|
+
* console.log(`There are ${instances.length} instances of this app`);
|
|
86
|
+
*/
|
|
87
|
+
register(metadata?: Record<string, any>): Promise<AppInstance[]>;
|
|
88
|
+
/**
|
|
89
|
+
* Unregister this app instance
|
|
90
|
+
* Call this when your app is being destroyed.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* await this.instances.unregister();
|
|
94
|
+
*/
|
|
95
|
+
unregister(): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Get all instances of this app
|
|
98
|
+
*
|
|
99
|
+
* @returns All current instances
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* const instances = await this.instances.getInstances();
|
|
103
|
+
* const otherInstances = instances.filter(i => i.tabId !== this.instances.tabId);
|
|
104
|
+
*/
|
|
105
|
+
getInstances(): Promise<AppInstance[]>;
|
|
106
|
+
/**
|
|
107
|
+
* Claim controller status for this instance
|
|
108
|
+
* Only one instance can be controller at a time.
|
|
109
|
+
*
|
|
110
|
+
* @param force - If true, forcibly take control from current controller
|
|
111
|
+
* @returns true if successfully claimed, false if another instance is controller
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* const claimed = await this.instances.claimController();
|
|
115
|
+
* if (claimed) {
|
|
116
|
+
* // This instance is now the controller
|
|
117
|
+
* this.startAutomation();
|
|
118
|
+
* } else {
|
|
119
|
+
* // Another instance is already controlling
|
|
120
|
+
* this.showObserverUI();
|
|
121
|
+
* }
|
|
122
|
+
*/
|
|
123
|
+
claimController(force?: boolean): Promise<boolean>;
|
|
124
|
+
/**
|
|
125
|
+
* Release controller status
|
|
126
|
+
* Call this when you want to give up control.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* await this.instances.releaseController();
|
|
130
|
+
*/
|
|
131
|
+
releaseController(): Promise<void>;
|
|
132
|
+
/**
|
|
133
|
+
* Check if this instance is the controller
|
|
134
|
+
*
|
|
135
|
+
* @returns true if this instance is the controller
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* if (await this.instances.isController()) {
|
|
139
|
+
* // We're in control
|
|
140
|
+
* }
|
|
141
|
+
*/
|
|
142
|
+
isController(): Promise<boolean>;
|
|
143
|
+
/**
|
|
144
|
+
* Get the current controller instance
|
|
145
|
+
*
|
|
146
|
+
* @returns The controller instance, or null if none
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* const controller = await this.instances.getController();
|
|
150
|
+
* if (controller) {
|
|
151
|
+
* console.log(`Controller is in tab ${controller.tabId}`);
|
|
152
|
+
* }
|
|
153
|
+
*/
|
|
154
|
+
getController(): Promise<AppInstance | null>;
|
|
155
|
+
/**
|
|
156
|
+
* Broadcast a message to all other instances
|
|
157
|
+
*
|
|
158
|
+
* @param message - Message to send (type and optional data)
|
|
159
|
+
* @returns Array of tab IDs that received the message
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* await this.instances.broadcast({
|
|
163
|
+
* type: 'YIELD_CONTROL',
|
|
164
|
+
* data: { reason: 'User requested control' }
|
|
165
|
+
* });
|
|
166
|
+
*/
|
|
167
|
+
broadcast(message: {
|
|
168
|
+
type: string;
|
|
169
|
+
data?: any;
|
|
170
|
+
}): Promise<number[]>;
|
|
171
|
+
/**
|
|
172
|
+
* Send a message to the controller instance
|
|
173
|
+
*
|
|
174
|
+
* @param message - Message to send
|
|
175
|
+
* @returns Tab ID of the controller, or null if no controller
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* await this.instances.sendToController({
|
|
179
|
+
* type: 'REQUEST_STATUS',
|
|
180
|
+
* });
|
|
181
|
+
*/
|
|
182
|
+
sendToController(message: {
|
|
183
|
+
type: string;
|
|
184
|
+
data?: any;
|
|
185
|
+
}): Promise<number | null>;
|
|
186
|
+
/**
|
|
187
|
+
* Send a message to a specific tab
|
|
188
|
+
*
|
|
189
|
+
* @param targetTabId - Tab ID to send to
|
|
190
|
+
* @param message - Message to send
|
|
191
|
+
* @returns true if sent successfully
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* await this.instances.sendToTab(123, {
|
|
195
|
+
* type: 'PING',
|
|
196
|
+
* });
|
|
197
|
+
*/
|
|
198
|
+
sendToTab(targetTabId: number, message: {
|
|
199
|
+
type: string;
|
|
200
|
+
data?: any;
|
|
201
|
+
}): Promise<boolean>;
|
|
202
|
+
/**
|
|
203
|
+
* Listen for messages from other instances
|
|
204
|
+
*
|
|
205
|
+
* @param callback - Function to call when a message is received
|
|
206
|
+
* @returns Unsubscribe function
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* const unsubscribe = this.instances.onMessage((msg) => {
|
|
210
|
+
* if (msg.type === 'YIELD_CONTROL') {
|
|
211
|
+
* this.releaseController();
|
|
212
|
+
* this.showObserverUI();
|
|
213
|
+
* }
|
|
214
|
+
* });
|
|
215
|
+
*
|
|
216
|
+
* // Later, to stop listening:
|
|
217
|
+
* unsubscribe();
|
|
218
|
+
*/
|
|
219
|
+
onMessage(callback: InstanceMessageCallback): () => void;
|
|
220
|
+
/**
|
|
221
|
+
* Listen for instance changes (new instances, removed instances)
|
|
222
|
+
*
|
|
223
|
+
* @param callback - Function to call when instances change
|
|
224
|
+
* @returns Unsubscribe function
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* const unsubscribe = this.instances.onInstanceChange((instances) => {
|
|
228
|
+
* console.log(`Now ${instances.length} instances`);
|
|
229
|
+
* const controller = instances.find(i => i.isController);
|
|
230
|
+
* if (controller) {
|
|
231
|
+
* console.log(`Controller is tab ${controller.tabId}`);
|
|
232
|
+
* }
|
|
233
|
+
* });
|
|
234
|
+
*/
|
|
235
|
+
onInstanceChange(callback: InstanceChangeCallback): () => void;
|
|
236
|
+
}
|
|
237
|
+
//# sourceMappingURL=WeaveAppInstanceAPI.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WeaveAppInstanceAPI.d.ts","sourceRoot":"","sources":["../../../src/WeaveAppInstanceAPI.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAeH;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,YAAY,EAAE,OAAO,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAChC;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;CACnB;AAoBD;;GAEG;AACH,MAAM,MAAM,uBAAuB,GAAG,CAAC,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC;AAEzE;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG,CAAC,SAAS,EAAE,WAAW,EAAE,KAAK,IAAI,CAAC;AAqHxE;;;;;GAKG;AACH,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,MAAM,CAAuB;gBAEzB,KAAK,EAAE,MAAM;IAIzB;;OAEG;IACH,IAAI,KAAK,IAAI,MAAM,GAAG,IAAI,CAEzB;IAED;;;;;;;;;;;;;OAaG;IACG,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAetE;;;;;;OAMG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAOjC;;;;;;;;OAQG;IACG,YAAY,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;IAO5C;;;;;;;;;;;;;;;;OAgBG;IACG,eAAe,CAAC,KAAK,GAAE,OAAe,GAAG,OAAO,CAAC,OAAO,CAAC;IAQ/D;;;;;;OAMG;IACG,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC;IAMxC;;;;;;;;;OASG;IACG,YAAY,IAAI,OAAO,CAAC,OAAO,CAAC;IAOtC;;;;;;;;;;OAUG;IACG,aAAa,IAAI,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC;IAOlD;;;;;;;;;;;OAWG;IACG,SAAS,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,GAAG,CAAA;KAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAQzE;;;;;;;;;;OAUG;IACG,gBAAgB,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,GAAG,CAAA;KAAE,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAQrF;;;;;;;;;;;OAWG;IACG,SAAS,CAAC,WAAW,EAAE,MAAM,EAAE,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,GAAG,CAAA;KAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IAS7F;;;;;;;;;;;;;;;;OAgBG;IACH,SAAS,CAAC,QAAQ,EAAE,uBAAuB,GAAG,MAAM,IAAI;IAsBxD;;;;;;;;;;;;;;OAcG;IACH,gBAAgB,CAAC,QAAQ,EAAE,sBAAsB,GAAG,MAAM,IAAI;CAqB/D"}
|