-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathDefaultIntentSupport.ts
206 lines (188 loc) · 6.48 KB
/
DefaultIntentSupport.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import {
AppIntent,
AppIdentifier,
IntentResolution,
IntentHandler,
Listener,
ResolveError,
IntentResult,
IntentResolver,
IntentResolutionChoice,
} from '@finos/fdc3-standard';
import { IntentSupport } from './IntentSupport';
import { Messaging } from '../Messaging';
import { DefaultIntentResolution } from './DefaultIntentResolution';
import { DefaultIntentListener } from '../listeners/DefaultIntentListener';
import { DefaultChannel } from '../channels/DefaultChannel';
import { DefaultPrivateChannel } from '../channels/DefaultPrivateChannel';
import { Context } from '@finos/fdc3-context';
import {
FindIntentRequest,
FindIntentResponse,
FindIntentsByContextRequest,
FindIntentsByContextResponse,
RaiseIntentForContextRequest,
RaiseIntentForContextResponse,
RaiseIntentRequest,
RaiseIntentResponse,
RaiseIntentResultResponse,
} from '@finos/fdc3-schema/dist/generated/api/BrowserTypes';
import { throwIfUndefined } from '../util';
const convertIntentResult = async (
{ payload }: RaiseIntentResultResponse,
messaging: Messaging
): Promise<IntentResult> => {
const result = payload.intentResult;
if (result?.channel) {
const { channel } = result;
switch (channel.type) {
case 'private': {
return new DefaultPrivateChannel(messaging, channel.id);
}
case 'app':
case 'user':
default: {
return new DefaultChannel(messaging, channel.id, channel.type, channel.displayMetadata);
}
}
} else if (result?.context) {
return result.context;
} else {
return;
}
};
export class DefaultIntentSupport implements IntentSupport {
readonly messaging: Messaging;
readonly intentResolver: IntentResolver;
constructor(messaging: Messaging, intentResolver: IntentResolver) {
this.messaging = messaging;
this.intentResolver = intentResolver;
}
async findIntent(intent: string, context: Context, resultType: string | undefined): Promise<AppIntent> {
const request: FindIntentRequest = {
type: 'findIntentRequest',
payload: {
intent,
context,
resultType,
},
meta: this.messaging.createMeta(),
};
const result = await this.messaging.exchange<FindIntentResponse>(request, 'findIntentResponse');
const appIntent = result.payload.appIntent!;
if (appIntent.apps.length == 0) {
throw new Error(ResolveError.NoAppsFound);
} else {
return {
intent: appIntent.intent,
apps: appIntent.apps,
};
}
}
async findIntentsByContext(context: Context): Promise<AppIntent[]> {
const request: FindIntentsByContextRequest = {
type: 'findIntentsByContextRequest',
payload: {
context,
},
meta: this.messaging.createMeta(),
};
const result: FindIntentsByContextResponse = await this.messaging.exchange(request, 'findIntentsByContextResponse');
const appIntents = result.payload.appIntents;
if (!appIntents || appIntents.length == 0) {
throw new Error(ResolveError.NoAppsFound);
} else {
return appIntents;
}
}
private async createResultPromise(request: RaiseIntentRequest | RaiseIntentForContextRequest): Promise<IntentResult> {
const rp = await this.messaging.waitFor<RaiseIntentResultResponse>(
m => m.type == 'raiseIntentResultResponse' && m.meta.requestUuid == request.meta.requestUuid
);
const ir = await convertIntentResult(rp, this.messaging);
return ir;
}
async raiseIntent(intent: string, context: Context, app: AppIdentifier): Promise<IntentResolution> {
const meta = this.messaging.createMeta();
const request: RaiseIntentRequest = {
type: 'raiseIntentRequest',
payload: {
intent,
context,
app: app,
},
meta: meta,
};
const resultPromise = this.createResultPromise(request);
const response = await this.messaging.exchange<RaiseIntentResponse>(
request,
'raiseIntentResponse',
ResolveError.IntentDeliveryFailed
);
throwIfUndefined(
response.payload.appIntent ?? response.payload.intentResolution,
'Invalid response from Desktop Agent to raiseIntent, either payload.appIntent or payload.intentResolution must be set!',
response,
ResolveError.NoAppsFound
);
if (response.payload.appIntent) {
// Needs further resolution, we need to invoke the resolver
const result: IntentResolutionChoice | void = await this.intentResolver.chooseIntent(
[response.payload.appIntent],
context
);
if (result) {
return this.raiseIntent(intent, context, result.appId);
} else {
throw new Error(ResolveError.UserCancelled);
}
} else {
// Was resolved
const details = response.payload.intentResolution!;
return new DefaultIntentResolution(this.messaging, resultPromise, details.source, details.intent);
}
}
async raiseIntentForContext(context: Context, app?: AppIdentifier | undefined): Promise<IntentResolution> {
const request: RaiseIntentForContextRequest = {
type: 'raiseIntentForContextRequest',
payload: {
context,
app: app,
},
meta: this.messaging.createMeta(),
};
const resultPromise = this.createResultPromise(request);
const response = await this.messaging.exchange<RaiseIntentForContextResponse>(
request,
'raiseIntentForContextResponse',
ResolveError.IntentDeliveryFailed
);
throwIfUndefined(
response.payload.appIntents ?? response.payload.intentResolution,
'Invalid response from Desktop Agent to raiseIntentForContext, either payload.appIntents or payload.intentResolution must be set!',
response,
ResolveError.NoAppsFound
);
if (response.payload.appIntents) {
// Needs further resolution, we need to invoke the resolver
const result: IntentResolutionChoice | void = await this.intentResolver.chooseIntent(
response.payload.appIntents,
context
);
if (result) {
return this.raiseIntent(result.intent, context, result.appId);
} else {
throw new Error(ResolveError.UserCancelled);
}
} else {
// Was resolved
const details = response.payload.intentResolution!;
return new DefaultIntentResolution(this.messaging, resultPromise, details.source, details.intent);
}
}
async addIntentListener(intent: string, handler: IntentHandler): Promise<Listener> {
const out = new DefaultIntentListener(this.messaging, intent, handler);
await out.register();
return out;
}
}