@interopio/browser-platform 3.0.1
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/browser.platform.d.ts +455 -0
- package/changelog.md +69 -0
- package/dist/browser.platform.es.js +46074 -0
- package/dist/browser.platform.es.js.map +1 -0
- package/dist/browser.platform.umd.js +46082 -0
- package/dist/browser.platform.umd.js.map +1 -0
- package/license.md +21 -0
- package/package.json +78 -0
- package/readme.md +9 -0
- package/rollup.config.js +45 -0
- package/src/common/constants.ts +46 -0
- package/src/common/defaultConfig.ts +56 -0
- package/src/common/gateway.d.ts +32 -0
- package/src/common/types.ts +222 -0
- package/src/connection/gateway.ts +132 -0
- package/src/connection/no-gateway.ts +1 -0
- package/src/connection/portsBridge.ts +441 -0
- package/src/connection/preferred.ts +247 -0
- package/src/controllers/domains.ts +156 -0
- package/src/controllers/glue.ts +604 -0
- package/src/controllers/interception.ts +53 -0
- package/src/controllers/license.ts +50 -0
- package/src/controllers/main.ts +256 -0
- package/src/controllers/plugins.ts +121 -0
- package/src/controllers/serviceWorker.ts +193 -0
- package/src/controllers/state.ts +84 -0
- package/src/controllers/system.ts +99 -0
- package/src/controllers/transactions.ts +80 -0
- package/src/factory.ts +40 -0
- package/src/fallbacks/browser.ts +47 -0
- package/src/fallbacks/dekstop.ts +21 -0
- package/src/index.ts +18 -0
- package/src/libs/applications/appStore/directory.ts +235 -0
- package/src/libs/applications/appStore/remoteWatcher.ts +112 -0
- package/src/libs/applications/controller.ts +469 -0
- package/src/libs/applications/decoders.ts +98 -0
- package/src/libs/applications/defaults.ts +6 -0
- package/src/libs/applications/types.ts +98 -0
- package/src/libs/channels/controller.ts +109 -0
- package/src/libs/channels/decoders.ts +19 -0
- package/src/libs/extension/controller.ts +120 -0
- package/src/libs/extension/decoders.ts +18 -0
- package/src/libs/extension/types.ts +11 -0
- package/src/libs/intents/constants.ts +9 -0
- package/src/libs/intents/controller.ts +500 -0
- package/src/libs/intents/decoders.ts +96 -0
- package/src/libs/intents/resolverHelper.ts +264 -0
- package/src/libs/intents/types.ts +119 -0
- package/src/libs/layouts/constants.ts +3 -0
- package/src/libs/layouts/controller.ts +476 -0
- package/src/libs/layouts/decoders.ts +132 -0
- package/src/libs/layouts/global/builder.ts +400 -0
- package/src/libs/layouts/global/resetter.ts +116 -0
- package/src/libs/layouts/global/restorer.ts +217 -0
- package/src/libs/layouts/global/validator.ts +169 -0
- package/src/libs/layouts/idbStore.ts +63 -0
- package/src/libs/layouts/types.ts +133 -0
- package/src/libs/manager/controller.ts +175 -0
- package/src/libs/manager/decoders.ts +6 -0
- package/src/libs/manager/defaults.ts +5 -0
- package/src/libs/manager/identity.ts +92 -0
- package/src/libs/manager/session.ts +195 -0
- package/src/libs/manager/types.ts +51 -0
- package/src/libs/notifications/controller.ts +447 -0
- package/src/libs/notifications/decoders.ts +166 -0
- package/src/libs/notifications/types.ts +49 -0
- package/src/libs/notifications/utils.ts +7 -0
- package/src/libs/search/controller.ts +232 -0
- package/src/libs/search/decoders.ts +6 -0
- package/src/libs/search/repositories/applications.ts +62 -0
- package/src/libs/search/repositories/layouts.ts +48 -0
- package/src/libs/search/repositories/workspaces.ts +47 -0
- package/src/libs/search/types.ts +9 -0
- package/src/libs/themes/constants.ts +9 -0
- package/src/libs/themes/controller.ts +153 -0
- package/src/libs/themes/decoders.ts +28 -0
- package/src/libs/themes/types.ts +28 -0
- package/src/libs/windows/controller.ts +498 -0
- package/src/libs/windows/decoders.ts +64 -0
- package/src/libs/windows/types.ts +59 -0
- package/src/libs/workspaces/controller.ts +922 -0
- package/src/libs/workspaces/decoders.ts +868 -0
- package/src/libs/workspaces/defaultConfig.ts +11 -0
- package/src/libs/workspaces/frames.ts +239 -0
- package/src/libs/workspaces/hibernationWatcher.ts +231 -0
- package/src/libs/workspaces/types.ts +506 -0
- package/src/platform.ts +156 -0
- package/src/shared/decoders.ts +479 -0
- package/src/shared/fetchTimeout.ts +25 -0
- package/src/shared/ioc.ts +580 -0
- package/src/shared/logger.ts +19 -0
- package/src/shared/promisePlus.ts +62 -0
- package/src/shared/sequelizer.ts +48 -0
- package/src/shared/utils.ts +86 -0
- package/src/storage/local.ts +111 -0
- package/src/storage/session.ts +436 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { IOConnectCore } from "@interopio/core";
|
|
3
|
+
import { IOConnectBrowser } from "@interopio/browser";
|
|
4
|
+
import { ApplicationStartConfig, BridgeOperation, LibController, OperationCheckConfig, OperationCheckResult } from "../../common/types";
|
|
5
|
+
import { GlueController } from "../../controllers/glue";
|
|
6
|
+
import { IoC } from "../../shared/ioc";
|
|
7
|
+
import { intentsOperationTypesDecoder, wrappedIntentsDecoder, wrappedIntentFilterDecoder, intentRequestDecoder, intentResultDecoder, raiseIntentRequestDecoder } from "./decoders";
|
|
8
|
+
import { IntentsOperationTypes, AppDefinitionWithIntents, IntentInfo, IntentStore, WrappedIntentFilter, WrappedIntents, RaiseIntentRequestWithResolverConfig, IntentRequestResolverConfig, ShouldResolverOpen, ResolverInstance, CoreRaiseIntentArgs } from "./types";
|
|
9
|
+
import logger from "../../shared/logger";
|
|
10
|
+
import { GlueWebIntentsPrefix } from "../../common/constants";
|
|
11
|
+
import { AppDirectory } from "../applications/appStore/directory";
|
|
12
|
+
import { operationCheckConfigDecoder, operationCheckResultDecoder } from "../../shared/decoders";
|
|
13
|
+
import { IntentsResolverHelper } from "./resolverHelper";
|
|
14
|
+
import { DEFAULT_RAISE_TIMEOUT_MS, DEFAULT_METHOD_RESPONSE_TIMEOUT_MS } from "./constants";
|
|
15
|
+
import { PromiseWrap } from "../../shared/promisePlus";
|
|
16
|
+
|
|
17
|
+
export class IntentsController implements LibController {
|
|
18
|
+
private operations: { [key in IntentsOperationTypes]: BridgeOperation } = {
|
|
19
|
+
getIntents: { name: "getIntents", resultDecoder: wrappedIntentsDecoder, execute: this.getWrappedIntents.bind(this) },
|
|
20
|
+
findIntent: { name: "findIntent", dataDecoder: wrappedIntentFilterDecoder, resultDecoder: wrappedIntentsDecoder, execute: this.findIntent.bind(this) },
|
|
21
|
+
raiseIntent: { name: "raiseIntent", dataDecoder: intentRequestDecoder, resultDecoder: intentResultDecoder, execute: this.handleRaiseIntent.bind(this) },
|
|
22
|
+
raise: { name: "raise", dataDecoder: raiseIntentRequestDecoder, resultDecoder: intentResultDecoder, execute: this.raise.bind(this) },
|
|
23
|
+
operationCheck: { name: "operationCheck", dataDecoder: operationCheckConfigDecoder, resultDecoder: operationCheckResultDecoder, execute: this.handleOperationCheck.bind(this) }
|
|
24
|
+
};
|
|
25
|
+
private started = false;
|
|
26
|
+
|
|
27
|
+
constructor(
|
|
28
|
+
private readonly glueController: GlueController,
|
|
29
|
+
private readonly resolverHelper: IntentsResolverHelper,
|
|
30
|
+
private readonly appDirectory: AppDirectory,
|
|
31
|
+
private readonly ioc: IoC
|
|
32
|
+
) { }
|
|
33
|
+
|
|
34
|
+
private get logger(): IOConnectCore.Logger.API | undefined {
|
|
35
|
+
return logger.get("intents.controller");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public handlePlatformShutdown(): void {
|
|
39
|
+
this.started = false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public async start(): Promise<void> {
|
|
43
|
+
this.started = true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public async handleControl(args: any): Promise<any> {
|
|
47
|
+
if (!this.started) {
|
|
48
|
+
new Error("Cannot handle this intents control message, because the controller has not been started");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const intentsData = args.data;
|
|
52
|
+
|
|
53
|
+
const commandId = args.commandId;
|
|
54
|
+
|
|
55
|
+
const callerId = args.callerId;
|
|
56
|
+
|
|
57
|
+
const operationValidation = intentsOperationTypesDecoder.run(args.operation);
|
|
58
|
+
|
|
59
|
+
if (!operationValidation.ok) {
|
|
60
|
+
throw new Error(`This intents request cannot be completed, because the operation name did not pass validation: ${JSON.stringify(operationValidation.error)}`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const operationName = operationValidation.result;
|
|
64
|
+
|
|
65
|
+
const incomingValidation = this.operations[operationName].dataDecoder?.run(intentsData);
|
|
66
|
+
|
|
67
|
+
if (incomingValidation && !incomingValidation.ok) {
|
|
68
|
+
throw new Error(`Intents request for ${operationName} rejected, because the provided arguments did not pass the validation: ${JSON.stringify(incomingValidation.error)}`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
this.logger?.debug(`[${commandId}] ${operationName} command is valid with data: ${JSON.stringify(intentsData)}`);
|
|
72
|
+
|
|
73
|
+
const result = await this.operations[operationName].execute(intentsData, commandId, callerId);
|
|
74
|
+
|
|
75
|
+
const resultValidation = this.operations[operationName].resultDecoder?.run(result);
|
|
76
|
+
|
|
77
|
+
if (resultValidation && !resultValidation.ok) {
|
|
78
|
+
throw new Error(`Intents request for ${operationName} could not be completed, because the operation result did not pass the validation: ${JSON.stringify(resultValidation.error)}`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
this.logger?.trace(`[${commandId}] ${operationName} command was executed successfully`);
|
|
82
|
+
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private async handleOperationCheck(config: OperationCheckConfig): Promise<OperationCheckResult> {
|
|
87
|
+
const operations = Object.keys(this.operations);
|
|
88
|
+
|
|
89
|
+
const isSupported = operations.some((operation) => operation.toLowerCase() === config.operation.toLowerCase());
|
|
90
|
+
|
|
91
|
+
return { isSupported };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private extractAppIntents(apps: AppDefinitionWithIntents[]): IntentStore {
|
|
95
|
+
const intents: IntentStore = {};
|
|
96
|
+
|
|
97
|
+
const appsWithIntents = apps.filter((app) => app.intents.length > 0);
|
|
98
|
+
// Gather app handlers from application definitions.
|
|
99
|
+
for (const app of appsWithIntents) {
|
|
100
|
+
for (const intentDef of app.intents) {
|
|
101
|
+
if (!intents[intentDef.name]) {
|
|
102
|
+
intents[intentDef.name] = [];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const handler: IOConnectBrowser.Intents.IntentHandler = {
|
|
106
|
+
applicationName: app.name,
|
|
107
|
+
applicationTitle: app.title,
|
|
108
|
+
applicationDescription: app.caption,
|
|
109
|
+
displayName: intentDef.displayName,
|
|
110
|
+
contextTypes: intentDef.contexts,
|
|
111
|
+
applicationIcon: app.icon,
|
|
112
|
+
type: "app",
|
|
113
|
+
resultType: intentDef.resultType
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
intents[intentDef.name].push(handler);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return intents;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private async getInstanceIntents(apps: AppDefinitionWithIntents[], commandId: string): Promise<IntentStore> {
|
|
124
|
+
const intents: IntentStore = {};
|
|
125
|
+
|
|
126
|
+
// Discover all running instances that provide intents, and add them to the corresponding intent.
|
|
127
|
+
for (const server of this.glueController.getServers()) {
|
|
128
|
+
const serverIntentsMethods = (server.getMethods?.() || []).filter((method) => method.name.startsWith(GlueWebIntentsPrefix));
|
|
129
|
+
|
|
130
|
+
await Promise.all(serverIntentsMethods.map(async (method) => {
|
|
131
|
+
const intentName = method.name.replace(GlueWebIntentsPrefix, "");
|
|
132
|
+
if (!intents[intentName]) {
|
|
133
|
+
intents[intentName] = [];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const info = method.flags.intent as Omit<IOConnectBrowser.Intents.AddIntentListenerRequest, "intent">;
|
|
137
|
+
|
|
138
|
+
const app = apps.find((appDef) => appDef.name === server.application);
|
|
139
|
+
let appIntent: IntentInfo | undefined;
|
|
140
|
+
// app can be undefined in the case of a dynamic intent.
|
|
141
|
+
if (app && app.intents) {
|
|
142
|
+
appIntent = app.intents.find((appDefIntent) => appDefIntent.name === intentName);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
let title: string | undefined;
|
|
146
|
+
|
|
147
|
+
if (this.glueController.isValidWindowId(server.windowId)) {
|
|
148
|
+
title = await this.ioc.windowsController.getWindowTitle(server.windowId!, commandId);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const handler: IOConnectBrowser.Intents.IntentHandler = {
|
|
152
|
+
// IFrames do not have windowIds but can still register intents.
|
|
153
|
+
instanceId: server.windowId || server.instance,
|
|
154
|
+
applicationName: server.application || "",
|
|
155
|
+
applicationIcon: info.icon || app?.icon,
|
|
156
|
+
applicationTitle: app?.title || "",
|
|
157
|
+
applicationDescription: info.description || app?.caption,
|
|
158
|
+
displayName: info.displayName || appIntent?.displayName,
|
|
159
|
+
contextTypes: info.contextTypes || appIntent?.contexts,
|
|
160
|
+
instanceTitle: title,
|
|
161
|
+
type: "instance",
|
|
162
|
+
resultType: appIntent?.resultType || info.resultType
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
intents[intentName].push(handler);
|
|
166
|
+
}));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return intents;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private mergeIntentStores(storeOne: IntentStore, storeTwo: IntentStore): IntentStore {
|
|
173
|
+
const intents: IntentStore = {};
|
|
174
|
+
|
|
175
|
+
for (const name of new Set([...Object.keys(storeOne), ...Object.keys(storeTwo)])) {
|
|
176
|
+
intents[name] = [...(storeOne[name] || []), ...(storeTwo[name] || [])];
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
return intents;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private wrapIntents(intents: IOConnectBrowser.Intents.Intent[]): WrappedIntents {
|
|
183
|
+
return {
|
|
184
|
+
intents
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private async getIntents(commandId: string): Promise<IOConnectBrowser.Intents.Intent[]> {
|
|
189
|
+
/*
|
|
190
|
+
Gathers all intents from:
|
|
191
|
+
1. Application definitions
|
|
192
|
+
2. Running instances (application can register dynamic intents by calling `addIntentListener()` that aren't predefined inside of their application definitions)
|
|
193
|
+
It also populates intent handlers (actual entities that can handle the intent).
|
|
194
|
+
*/
|
|
195
|
+
const apps: AppDefinitionWithIntents[] = (await this.appDirectory.getAll()).map((app) => {
|
|
196
|
+
return {
|
|
197
|
+
name: app.name,
|
|
198
|
+
title: app.title || "",
|
|
199
|
+
icon: app.icon,
|
|
200
|
+
caption: app.caption,
|
|
201
|
+
intents: app.userProperties.intents || []
|
|
202
|
+
};
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const appIntentsStore = this.extractAppIntents(apps);
|
|
206
|
+
this.logger?.trace(`[${commandId}] got app intents`);
|
|
207
|
+
|
|
208
|
+
const instanceIntentsStore = await this.getInstanceIntents(apps, commandId);
|
|
209
|
+
this.logger?.trace(`[${commandId}] got instance intents`);
|
|
210
|
+
|
|
211
|
+
const allIntentsStore = this.mergeIntentStores(appIntentsStore, instanceIntentsStore);
|
|
212
|
+
|
|
213
|
+
const intents = Object.keys(allIntentsStore).map((name) => ({ name, handlers: allIntentsStore[name] }));
|
|
214
|
+
|
|
215
|
+
return intents;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private async getWrappedIntents(commandId: string): Promise<WrappedIntents> {
|
|
219
|
+
this.logger?.trace(`[${commandId}] handling getIntents command`);
|
|
220
|
+
|
|
221
|
+
const intents = await this.getIntents(commandId);
|
|
222
|
+
|
|
223
|
+
this.logger?.trace(`[${commandId}] getIntents command completed`);
|
|
224
|
+
|
|
225
|
+
return this.wrapIntents(intents);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
private async findIntent(wrappedIntentFilter: WrappedIntentFilter, commandId: string): Promise<WrappedIntents> {
|
|
229
|
+
this.logger?.trace(`[${commandId}] handling findIntent command`);
|
|
230
|
+
|
|
231
|
+
const intentFilter = wrappedIntentFilter.filter;
|
|
232
|
+
|
|
233
|
+
let intents = await this.getIntents(commandId);
|
|
234
|
+
|
|
235
|
+
if (!intentFilter) {
|
|
236
|
+
return this.wrapIntents(intents);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (typeof intentFilter === "string") {
|
|
240
|
+
return this.wrapIntents(intents.filter((intent) => intent.name === intentFilter));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (intentFilter.contextType) {
|
|
244
|
+
const ctToLower = intentFilter.contextType.toLowerCase();
|
|
245
|
+
intents = intents.filter((intent) => intent.handlers.some((handler) => handler.contextTypes?.some((ct) => ct.toLowerCase() === ctToLower)));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (intentFilter.name) {
|
|
249
|
+
intents = intents.filter((intent) => intent.name === intentFilter.name);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (intentFilter.resultType) {
|
|
253
|
+
const resultTypeToLower = intentFilter.resultType.toLowerCase();
|
|
254
|
+
intents = intents.filter((intent) => intent.handlers.some(handler => handler.resultType?.toLowerCase() === resultTypeToLower));
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
this.logger?.trace(`[${commandId}] findIntent command completed`);
|
|
258
|
+
|
|
259
|
+
return this.wrapIntents(intents);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
private async getIntent(intent: string, commandId: string): Promise<IOConnectBrowser.Intents.Intent | undefined> {
|
|
263
|
+
return (await this.getIntents(commandId)).find((registeredIntent) => registeredIntent.name === intent);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private async startApp(config: ApplicationStartConfig, commandId: string): Promise<string> {
|
|
267
|
+
const instance = await this.ioc.applicationsController.handleApplicationStart(config, commandId);
|
|
268
|
+
|
|
269
|
+
return instance.id;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
private instanceIdToInteropInstance(instanceId: string): string | undefined {
|
|
273
|
+
const servers = this.glueController.getServers();
|
|
274
|
+
|
|
275
|
+
return servers.find((server) => server.windowId === instanceId || server.instance === instanceId)?.instance;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
private handleRaiseIntent(intentRequest: IOConnectBrowser.Intents.IntentRequest, commandId: string, callerId?: string): Promise<IOConnectBrowser.Intents.IntentResult> {
|
|
279
|
+
return this.raiseIntent(intentRequest, commandId, callerId);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
private async raiseIntent(intentRequest: IOConnectBrowser.Intents.IntentRequest, commandId: string, callerId?: string, timeout?: number): Promise<IOConnectBrowser.Intents.IntentResult> {
|
|
283
|
+
this.logger?.trace(`[${commandId}] handling raiseIntent command with intentRequest: ${JSON.stringify(intentRequest)}`);
|
|
284
|
+
|
|
285
|
+
const intentName = intentRequest.intent;
|
|
286
|
+
const intentDef = await this.getIntent(intentName, commandId);
|
|
287
|
+
|
|
288
|
+
if (!intentDef) {
|
|
289
|
+
throw new Error(`Intent ${intentName} not found!`);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
this.logger?.trace(`Raised intent definition: ${JSON.stringify(intentDef)}`);
|
|
293
|
+
|
|
294
|
+
const firstFoundAppHandler = intentRequest.handlers
|
|
295
|
+
? this.findHandlerByFilter(intentRequest.handlers, { type: "app" })
|
|
296
|
+
: this.findHandlerByFilter(intentDef.handlers, { type: "app" });
|
|
297
|
+
|
|
298
|
+
const firstFoundInstanceHandler = intentRequest.handlers
|
|
299
|
+
? this.findHandlerByFilter(intentRequest.handlers, { type: "instance" })
|
|
300
|
+
: this.findHandlerByFilter(intentDef.handlers, { type: "instance" });
|
|
301
|
+
|
|
302
|
+
let handler: IOConnectBrowser.Intents.IntentHandler | undefined;
|
|
303
|
+
|
|
304
|
+
// target the first found instance or start a new one if there isn't
|
|
305
|
+
if (!intentRequest.target || intentRequest.target === "reuse") {
|
|
306
|
+
handler = firstFoundInstanceHandler || firstFoundAppHandler;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// start new instance of the first handler in handlers / intentDef.handlers
|
|
310
|
+
if (intentRequest.target === "startNew") {
|
|
311
|
+
handler = firstFoundAppHandler;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// open a new app from the list of intentRequest.handlers (if passed) or intentDef.handlers
|
|
315
|
+
if (typeof intentRequest.target === "object" && intentRequest.target.app) {
|
|
316
|
+
handler = this.findHandlerByFilter(intentDef.handlers, { app: intentRequest.target.app });
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// target the instance in the list of intentRequest.handlers (if passed) or intentDef.handlers
|
|
320
|
+
if (typeof intentRequest.target === "object" && intentRequest.target.instance) {
|
|
321
|
+
handler = this.findHandlerByFilter(intentDef.handlers, { instance: intentRequest.target.instance, app: intentRequest.target.app });
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
if (!handler) {
|
|
325
|
+
throw new Error(`Can not raise intent for request ${JSON.stringify(intentRequest)} - can not find intent handler!`);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
const result = await this.raiseIntentToTargetHandler(intentRequest, handler, commandId, timeout);
|
|
329
|
+
|
|
330
|
+
return result;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
private findHandlerByFilter(handlers: IOConnectBrowser.Intents.IntentHandler[], filter: { app?: string, instance?: string, type?: "app" | "instance" }) {
|
|
334
|
+
if (filter.type) {
|
|
335
|
+
return handlers.find(handler => handler.type === filter.type);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (filter.instance) {
|
|
339
|
+
return handlers.find(handler => filter.app
|
|
340
|
+
? handler.applicationName === filter.app && handler.instanceId === filter.instance
|
|
341
|
+
: handler.instanceId === filter.instance
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (filter.app) {
|
|
346
|
+
return handlers.find(handler => handler.applicationName === filter.app);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private async raiseIntentToTargetHandler(request: IOConnectBrowser.Intents.IntentRequest, handler: IOConnectBrowser.Intents.IntentHandler, commandId: string, timeout?: number): Promise<IOConnectBrowser.Intents.IntentResult> {
|
|
351
|
+
this.logger?.trace(`Raising intent to target handler:${JSON.stringify(handler)}`);
|
|
352
|
+
|
|
353
|
+
const instanceId = handler.instanceId || await this.startApp({ name: handler.applicationName, ...request.options, context: request.context }, commandId);
|
|
354
|
+
|
|
355
|
+
const methodName = `${GlueWebIntentsPrefix}${request.intent}`;
|
|
356
|
+
|
|
357
|
+
this.logger?.trace(`Searching for interop server offering method ${methodName}`);
|
|
358
|
+
|
|
359
|
+
// add 1sec to make sure 'raise' will reject due to timeout hit instead of method invocation
|
|
360
|
+
const invokeOptions = {
|
|
361
|
+
methodResponseTimeoutMs: timeout ? timeout + 1000 : DEFAULT_METHOD_RESPONSE_TIMEOUT_MS,
|
|
362
|
+
waitTimeoutMs: timeout ? timeout + 1000 : DEFAULT_METHOD_RESPONSE_TIMEOUT_MS
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
const result = await this.glueController.invokeMethod<any>(methodName, request.context, { instance: this.instanceIdToInteropInstance(instanceId) }, invokeOptions);
|
|
366
|
+
|
|
367
|
+
this.logger?.trace(`[${commandId}] raiseIntent command completed. Returning result: ${JSON.stringify(result)}`);
|
|
368
|
+
|
|
369
|
+
return {
|
|
370
|
+
request,
|
|
371
|
+
handler: { ...handler, instanceId, type: "instance" },
|
|
372
|
+
result: result.returned
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
private async raise(request: RaiseIntentRequestWithResolverConfig, commandId: string, callerId?: string): Promise<IOConnectBrowser.Intents.IntentResult> {
|
|
377
|
+
this.logger?.trace(`[${commandId}] Receive raise command with config: ${JSON.stringify(request)}`);
|
|
378
|
+
|
|
379
|
+
if (!callerId) {
|
|
380
|
+
throw new Error("Cannot raise intent - callerId is not defined");
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const timeout = request.intentRequest.timeout || DEFAULT_RAISE_TIMEOUT_MS;
|
|
384
|
+
|
|
385
|
+
const resolverInstance: ResolverInstance = { instanceId: undefined };
|
|
386
|
+
|
|
387
|
+
const coreRaiseIntentFn = this.coreRaiseIntent.bind(this, { request, resolverInstance, timeout, commandId, callerId });
|
|
388
|
+
|
|
389
|
+
if (request.intentRequest.waitUserResponseIndefinitely) {
|
|
390
|
+
return coreRaiseIntentFn();
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// start timeout here -> default (waitUserResponseIndefinitely: false)
|
|
394
|
+
const resultPromise = PromiseWrap<IOConnectBrowser.Intents.IntentResult>(coreRaiseIntentFn,
|
|
395
|
+
timeout, `Timeout of ${timeout}ms hit for intent request ${JSON.stringify(request.intentRequest)}`
|
|
396
|
+
);
|
|
397
|
+
|
|
398
|
+
resultPromise.catch(() => this.handleRaiseOnError(resolverInstance.instanceId));
|
|
399
|
+
|
|
400
|
+
return resultPromise;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
private async coreRaiseIntent({request, resolverInstance, timeout, commandId, callerId}: CoreRaiseIntentArgs): Promise<IOConnectBrowser.Intents.IntentResult> {
|
|
404
|
+
const { resolverConfig, intentRequest } = request;
|
|
405
|
+
|
|
406
|
+
const intent = (await this.findIntent({ filter: { name: intentRequest.intent } }, commandId)).intents.find(intent => intent.name === intentRequest.intent);
|
|
407
|
+
|
|
408
|
+
if (!intent) {
|
|
409
|
+
throw new Error(`Intent with name ${intentRequest.intent} not found`);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
this.logger?.trace(`[${commandId}] Intent to be handled: ${JSON.stringify(intent)}`);
|
|
413
|
+
|
|
414
|
+
const { open, reason } = this.checkIfResolverShouldBeOpened(intent, intentRequest, resolverConfig);
|
|
415
|
+
|
|
416
|
+
if (!open) {
|
|
417
|
+
this.logger?.trace(`[${commandId}] Intent Resolver UI won't be used. Reason: ${reason}`);
|
|
418
|
+
|
|
419
|
+
// start timeout here -> waitUserResponseIndefinitely: true, but resolver will not be opened
|
|
420
|
+
return intentRequest.waitUserResponseIndefinitely
|
|
421
|
+
? PromiseWrap<IOConnectBrowser.Intents.IntentResult>(() => this.raiseIntent(intentRequest, commandId, callerId, timeout), timeout, `Timeout of ${timeout}ms hit for raise to resolve`)
|
|
422
|
+
: this.raiseIntent(intentRequest, commandId, callerId, timeout);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
this.logger?.trace(`[${commandId}] Starting Intent Resolver app for intent request: ${request}`);
|
|
426
|
+
|
|
427
|
+
const resolverHandler = await this.resolverHelper.startResolverApp({ requestWithResolverInfo: request, callerId, commandId, resolverInstance });
|
|
428
|
+
|
|
429
|
+
this.logger?.trace(`Raising intent to target handler: ${JSON.stringify(resolverHandler)} ${request.intentRequest.waitUserResponseIndefinitely ? `with timeout of ${request.intentRequest.timeout || DEFAULT_RAISE_TIMEOUT_MS}`: ""}`);
|
|
430
|
+
|
|
431
|
+
if (intentRequest.waitUserResponseIndefinitely) {
|
|
432
|
+
return PromiseWrap(() => this.raiseIntentToTargetHandler(intentRequest, resolverHandler, commandId, timeout), timeout, `Timeout of ${timeout}ms hit for raise to resolve`);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const result = await this.raiseIntentToTargetHandler(request.intentRequest, resolverHandler, commandId, timeout);
|
|
436
|
+
|
|
437
|
+
this.logger?.trace(`Result from raise() method for intent ${JSON.stringify(request.intentRequest.intent)}: ${JSON.stringify(result)}`);
|
|
438
|
+
|
|
439
|
+
return result;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
private handleRaiseOnError(instanceId?: string) {
|
|
443
|
+
if (!instanceId) {
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
this.resolverHelper.stopResolverInstance(instanceId);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
private checkIfIntentHasMoreThanOneHandler(intent: IOConnectBrowser.Intents.Intent, request: IOConnectBrowser.Intents.IntentRequest): boolean {
|
|
451
|
+
// no target => show resolver if request.handlers > 1 || intent.handlers.length > 1
|
|
452
|
+
// reuse => show resolver if 2+ instances in request.handlers || show resolver if intent.handlers.length > 1
|
|
453
|
+
// startNew => show resolver if 2+ apps in request.handler || intent.handlers as apps > 1
|
|
454
|
+
// app => do not show resolver
|
|
455
|
+
|
|
456
|
+
if (!request.target) {
|
|
457
|
+
return request.handlers
|
|
458
|
+
? request.handlers.length > 1
|
|
459
|
+
: intent.handlers.length > 1;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
if (request.target === "reuse") {
|
|
463
|
+
return request.handlers
|
|
464
|
+
? request.handlers.filter(handler => handler.type === "instance" && handler.instanceId).length > 1 || request.handlers.filter(handler => handler.type === "app").length > 1
|
|
465
|
+
: intent.handlers.filter(handler => handler.type === "instance" && handler.instanceId).length > 1 || intent.handlers.filter(handler => handler.type === "app").length > 1;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (request.target === "startNew") {
|
|
469
|
+
return request.handlers
|
|
470
|
+
? request.handlers.filter(handler => handler.type === "app").length > 1
|
|
471
|
+
: intent.handlers.filter(handler => handler.type === "app").length > 1;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
if (typeof request.target === "object") {
|
|
475
|
+
return false;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
return false;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
private checkIfResolverShouldBeOpened(intent: IOConnectBrowser.Intents.Intent, intentRequest: IOConnectBrowser.Intents.IntentRequest, resolverConfig: IntentRequestResolverConfig): ShouldResolverOpen {
|
|
482
|
+
if (!resolverConfig.enabled) {
|
|
483
|
+
return { open: false, reason: "Intent Resolver is disabled. Raising intent to first found handler" };
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const intentsResolverApp = this.glueController.clientGlue.appManager.application(resolverConfig.appName);
|
|
487
|
+
|
|
488
|
+
if (!intentsResolverApp) {
|
|
489
|
+
return { open: false, reason: `Application with name ${resolverConfig.appName} not found` };
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const hasMoreThanOneHandler = this.checkIfIntentHasMoreThanOneHandler(intent, intentRequest);
|
|
493
|
+
|
|
494
|
+
if (!hasMoreThanOneHandler) {
|
|
495
|
+
return { open: false, reason: "Raised intent has only one handler" };
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
return { open: true };
|
|
499
|
+
}
|
|
500
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { IOConnectBrowser } from "@interopio/browser";
|
|
2
|
+
import { Decoder, object, array, optional, anyJson, oneOf, constant, string, boolean, number } from "decoder-validate";
|
|
3
|
+
import { nonEmptyStringDecoder, nonNegativeNumberDecoder, windowOpenSettingsDecoder } from "../../shared/decoders";
|
|
4
|
+
import { IntentRequestResolverConfig, IntentResolverResponse, IntentsOperationTypes, RaiseIntentRequestWithResolverConfig, ResolverIntentHandler, WrappedIntentFilter, WrappedIntents } from "./types";
|
|
5
|
+
|
|
6
|
+
export const intentsOperationTypesDecoder: Decoder<IntentsOperationTypes> = oneOf<"findIntent" | "getIntents" | "raiseIntent" | "raise" | "operationCheck">(
|
|
7
|
+
constant("findIntent"),
|
|
8
|
+
constant("getIntents"),
|
|
9
|
+
constant("raiseIntent"),
|
|
10
|
+
constant("raise"),
|
|
11
|
+
constant("operationCheck")
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
const intentHandlerDecoder: Decoder<IOConnectBrowser.Intents.IntentHandler> = object({
|
|
15
|
+
applicationName: nonEmptyStringDecoder,
|
|
16
|
+
applicationTitle: string(),
|
|
17
|
+
applicationDescription: optional(string()),
|
|
18
|
+
applicationIcon: optional(string()),
|
|
19
|
+
type: oneOf<"app" | "instance">(constant("app"), constant("instance")),
|
|
20
|
+
displayName: optional(string()),
|
|
21
|
+
contextTypes: optional(array(nonEmptyStringDecoder)),
|
|
22
|
+
instanceId: optional(string()),
|
|
23
|
+
instanceTitle: optional(string()),
|
|
24
|
+
resultType: optional(nonEmptyStringDecoder)
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const intentDecoder: Decoder<IOConnectBrowser.Intents.Intent> = object({
|
|
28
|
+
name: nonEmptyStringDecoder,
|
|
29
|
+
handlers: array(intentHandlerDecoder)
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const intentTargetDecoder: Decoder<"startNew" | "reuse" | { app?: string; instance?: string }> = oneOf<"startNew" | "reuse" | { app?: string; instance?: string }>(
|
|
33
|
+
constant("startNew"),
|
|
34
|
+
constant("reuse"),
|
|
35
|
+
object({
|
|
36
|
+
app: optional(nonEmptyStringDecoder),
|
|
37
|
+
instance: optional(nonEmptyStringDecoder)
|
|
38
|
+
})
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const intentContextDecoder: Decoder<IOConnectBrowser.Intents.IntentContext> = object({
|
|
42
|
+
type: optional(nonEmptyStringDecoder),
|
|
43
|
+
data: optional(object())
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
export const intentsDecoder: Decoder<IOConnectBrowser.Intents.Intent[]> = array(intentDecoder);
|
|
47
|
+
|
|
48
|
+
export const wrappedIntentsDecoder: Decoder<WrappedIntents> = object({
|
|
49
|
+
intents: intentsDecoder
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
export const wrappedIntentFilterDecoder: Decoder<WrappedIntentFilter> = object({
|
|
53
|
+
filter: optional(object({
|
|
54
|
+
name: optional(nonEmptyStringDecoder),
|
|
55
|
+
contextType: optional(nonEmptyStringDecoder),
|
|
56
|
+
resultType: optional(nonEmptyStringDecoder)
|
|
57
|
+
}))
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
export const resolverIntentHandlerDecoder: Decoder<ResolverIntentHandler> = object({
|
|
61
|
+
applicationName: nonEmptyStringDecoder,
|
|
62
|
+
applicationIcon: optional(string()),
|
|
63
|
+
instanceId: optional(string()),
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
export const intentRequestDecoder: Decoder<IOConnectBrowser.Intents.IntentRequest> = object({
|
|
67
|
+
intent: nonEmptyStringDecoder,
|
|
68
|
+
target: optional(intentTargetDecoder),
|
|
69
|
+
context: optional(intentContextDecoder),
|
|
70
|
+
options: optional(windowOpenSettingsDecoder),
|
|
71
|
+
handlers: optional(array(intentHandlerDecoder)),
|
|
72
|
+
timeout: optional(nonNegativeNumberDecoder),
|
|
73
|
+
waitUserResponseIndefinitely: optional(boolean())
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const intentRequestResolverConfigDecoder: Decoder<IntentRequestResolverConfig> = object({
|
|
77
|
+
enabled: optional(boolean()),
|
|
78
|
+
appName: string(),
|
|
79
|
+
waitResponseTimeout: number()
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
export const raiseIntentRequestDecoder: Decoder<RaiseIntentRequestWithResolverConfig> = object({
|
|
83
|
+
intentRequest: intentRequestDecoder,
|
|
84
|
+
resolverConfig: intentRequestResolverConfigDecoder
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
export const intentResultDecoder: Decoder<IOConnectBrowser.Intents.IntentResult> = object({
|
|
88
|
+
request: intentRequestDecoder,
|
|
89
|
+
handler: intentHandlerDecoder,
|
|
90
|
+
result: anyJson()
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
export const intentResolverResponseDecoder: Decoder<IntentResolverResponse> = object({
|
|
94
|
+
intent: nonEmptyStringDecoder,
|
|
95
|
+
handler: intentHandlerDecoder
|
|
96
|
+
});
|