Skip to content

Commit 1ab5e88

Browse files
author
Jon Bernard
committed
changes to the node ldk to make bi direction communication work with whispers
1 parent 13865f1 commit 1ab5e88

File tree

10 files changed

+163
-24
lines changed

10 files changed

+163
-24
lines changed

ldk/node/dist/bin/build.js

100755100644
File mode changed.

ldk/node/dist/hostClients/networkClient.js

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ldk/node/dist/hostClients/networkService.d.ts

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ldk/node/dist/hostClients/whisperClient.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ldk/node/examples/whisper-disambiguation/package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ldk/node/examples/whisper-disambiguation/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"devDependencies": {
1313
"@types/jest": "26.0.13",
1414
"@types/node": "14.10.0",
15+
"@types/ramda": "^0.27.34",
1516
"@typescript-eslint/eslint-plugin": "4.1.0",
1617
"@typescript-eslint/parser": "4.1.0",
1718
"eslint": "7.8.1",
@@ -30,6 +31,7 @@
3031
},
3132
"dependencies": {
3233
"@oliveai/ldk": "file:../..",
33-
"moment": "^2.29.1"
34+
"moment": "^2.29.1",
35+
"ramda": "0.27.1"
3436
}
3537
}

ldk/node/examples/whisper-disambiguation/src/index.ts

Lines changed: 114 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
1+
import { find, propEq } from 'ramda';
22
import moment from "moment";
33

44
import { HostServices, Logger, Loop, serveLoop } from '../../../dist';
55
import {
66
WhisperDisambiguationElements,
7-
// WhisperDisambiguationOption,
7+
WhisperListStyle,
8+
WhisperListAlign,
89
} from '../../../dist/hostClients/whisperService';
910

10-
import {RecallJSON} from './transform'
11+
import { decodeRecall, Recall, RecallJSON } from './transform'
1112

1213
const logger = new Logger('olive-helps-node-example-network');
1314

15+
export interface RecallMap {
16+
[key: string]: Recall
17+
}
18+
1419
export interface Element {
1520
[key: string]: WhisperDisambiguationElements
1621
}
@@ -30,15 +35,18 @@ class ExampleLoop implements Loop {
3035
method: "GET",
3136
body: "",
3237
}).then((response) => {
33-
const { results } = JSON.parse(Buffer.from(response.data).toString('utf8'));
38+
const { results: jsonResults } = JSON.parse(Buffer.from(response.data).toString('utf8'));
3439
const itemList = {} as Element;
40+
const results = {} as RecallMap;
3541

36-
results.forEach((resultItem: RecallJSON, index: number) => {
42+
jsonResults.forEach((resultItem: RecallJSON, index: number) => {
3743
itemList[resultItem.recall_number] = {
38-
label: `😝${resultItem.recalling_firm} (${resultItem.recall_initiation_date}) jb`,
44+
label: `😝${resultItem.recalling_firm} (${resultItem.recall_initiation_date})`,
3945
order: index + 1,
4046
type: 'option',
4147
}
48+
49+
results[resultItem.recall_number] = decodeRecall(resultItem)
4250
})
4351

4452
this.host.whisper.disambiguationWhisper({
@@ -48,7 +56,106 @@ class ExampleLoop implements Loop {
4856
},
4957
(error, input) => {
5058
if (input) {
51-
logger.info(`Zhu Li Do The Thing With ${input.key}`);
59+
const recallItem: Recall = results[input.key];
60+
61+
logger.info(`Emitting disambiguation whisper with the key ${input.key}`, JSON.stringify(recallItem), JSON.stringify({
62+
align: WhisperListAlign.LEFT,
63+
body: recallItem.Description,
64+
header: recallItem.Firm,
65+
order: 0,
66+
style: WhisperListStyle.NONE,
67+
type: 'message',
68+
}));
69+
70+
if (recallItem) {
71+
this.host.whisper.listWhisper({
72+
label: 'Latest FDA Food Recall',
73+
markdown: '',
74+
elements: {
75+
topMessage: {
76+
align: WhisperListAlign.LEFT,
77+
body: recallItem.Description,
78+
header: recallItem.Firm,
79+
order: 0,
80+
style: WhisperListStyle.NONE,
81+
type: 'message',
82+
},
83+
sectionDivider: {
84+
order: 1,
85+
type: 'divider',
86+
},
87+
reason: {
88+
value: recallItem.Reason,
89+
label: 'Reason',
90+
order: 2,
91+
type: 'pair',
92+
},
93+
distribution: {
94+
value: recallItem.Distribution,
95+
label: 'Distribution',
96+
order: 3,
97+
type: 'pair',
98+
},
99+
quantity: {
100+
value: recallItem.Quantity,
101+
label: 'Quantity',
102+
order: 4,
103+
type: 'pair',
104+
},
105+
codes: {
106+
extra: true,
107+
value: recallItem.Codes,
108+
label: 'Codes',
109+
order: 5,
110+
type: 'pair',
111+
},
112+
id: {
113+
extra: true,
114+
value: recallItem.ID,
115+
label: 'Recall number',
116+
order: 6,
117+
type: 'pair',
118+
},
119+
date: {
120+
extra: true,
121+
value: recallItem.Date,
122+
label: 'Date initiated',
123+
order: 7,
124+
type: 'pair',
125+
},
126+
recallType: {
127+
extra: true,
128+
value: recallItem.RecallType,
129+
label: 'Recall type',
130+
order: 8,
131+
type: 'pair',
132+
},
133+
type: {
134+
extra: true,
135+
value: recallItem.Type,
136+
label: 'Product type',
137+
order: 9,
138+
type: 'pair',
139+
},
140+
classification: {
141+
extra: true,
142+
value: recallItem.Classification,
143+
label: 'Classification',
144+
order: 10,
145+
type: 'pair',
146+
},
147+
address: {
148+
extra: true,
149+
value: `${recallItem.Address1} ${recallItem.Address2 ? `${recallItem.Address2} ` : ''}${recallItem.City}, ${recallItem.State} ${recallItem.Zip} ${recallItem.Country}`,
150+
label: 'Company address',
151+
order: 11,
152+
type: 'pair',
153+
},
154+
},
155+
});
156+
} else {
157+
logger.info(`Could not find recall with the key ${input.key}`);
158+
}
52159
}
53160
},
54161
);

ldk/node/src/hostClients/networkClient.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ export class NetworkClient
5757
msg.setUrl(req.url);
5858
msg.setMethod(req.method);
5959
msg.setBody(req.body);
60-
addHeadersToMessage(msg, req.headers);
60+
if (req.headers) {
61+
addHeadersToMessage(msg, req.headers);
62+
}
6163
return msg;
6264
},
6365
(response: messages.HTTPResponseMsg) => ({

ldk/node/src/hostClients/networkService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export interface HttpRequest {
33
method: string;
44
body: string | Uint8Array;
55

6-
headers: Record<string, Array<string>>;
6+
headers?: Record<string, Array<string>>;
77
}
88

99
export interface HttpResponse {

ldk/node/src/hostClients/whisperClient.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,32 @@ class WhisperClient
8181
whisper: WhisperDisambiguationConfig,
8282
listener: StreamListener<WhisperDisambiguationEvent>,
8383
): StoppableStream<WhisperDisambiguationEvent> {
84-
const msg = generateWhisperDisambiguation(whisper);
85-
const stream = this.client.whisperDisambiguation(msg);
84+
const message = generateWhisperDisambiguation(whisper)
85+
.setSession(this.createSessionMessage());
8686
return new TransformingStream<
8787
messages.WhisperDisambiguationStreamResponse,
8888
WhisperDisambiguationEvent
89-
>(stream, (response) => transformDisambiguationResponse(response), listener);
89+
>(
90+
this.client.whisperDisambiguation(message),
91+
(response) => transformDisambiguationResponse(response),
92+
listener,
93+
);
9094
}
9195

9296
formWhisper(
9397
whisper: WhisperFormConfig,
9498
listener: StreamListener<WhisperFormUpdateEvent | WhisperFormSubmitEvent>,
9599
): StoppableStream<WhisperFormUpdateEvent | WhisperFormSubmitEvent> {
96-
const msg = generateWhisperForm(whisper);
97-
const stream = this.client.whisperForm(msg);
100+
const message = generateWhisperForm(whisper)
101+
.setSession(this.createSessionMessage());
98102
return new TransformingStream<
99103
messages.WhisperFormStreamResponse,
100104
WhisperFormSubmitEvent | WhisperFormUpdateEvent
101-
>(stream, (response) => transformResponse(response), listener);
105+
>(
106+
this.client.whisperForm(message),
107+
(response) => transformResponse(response),
108+
listener,
109+
);
102110
}
103111

104112
protected generateClient(): GRPCClientConstructor<WhisperGRPCClient> {

0 commit comments

Comments
 (0)