Skip to content

Commit c6ad69e

Browse files
author
Jon Bernard
committed
add DAB example WIP
1 parent 6b67d23 commit c6ad69e

File tree

9 files changed

+17313
-2
lines changed

9 files changed

+17313
-2
lines changed

ldk/node/examples/network/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ExampleLoop implements Loop {
3535
topMessage: {
3636
align: WhisperListAlign.LEFT,
3737
body: recallItem.product_description,
38-
header: recallItem.Firm,
38+
header: recallItem.recalling_firm,
3939
order: 0,
4040
style: WhisperListStyle.NONE,
4141
type: 'message',
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build
2+
dist
3+
node_modules

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

Lines changed: 17110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "olive-helps-example-whisper-disambiguation",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "src/index.ts",
6+
"scripts": {
7+
"build": "rm -rf dist && tsc",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"devDependencies": {
13+
"@types/jest": "26.0.13",
14+
"@types/node": "14.10.0",
15+
"@typescript-eslint/eslint-plugin": "4.1.0",
16+
"@typescript-eslint/parser": "4.1.0",
17+
"eslint": "7.8.1",
18+
"eslint-config-airbnb-base": "14.2.0",
19+
"eslint-config-prettier": "6.11.0",
20+
"eslint-plugin-import": "2.22.0",
21+
"eslint-plugin-jsdoc": "30.4.0",
22+
"jest": "26.4.2",
23+
"pkg": "4.4.9",
24+
"prettier": "2.1.1",
25+
"ts-jest": "26.3.0",
26+
"ts-node": "9.0.0",
27+
"typedoc": "0.17.8",
28+
"typedoc-plugin-pages": "1.0.1",
29+
"typescript": "4.0.2"
30+
},
31+
"dependencies": {
32+
"@oliveai/ldk": "file:../..",
33+
"moment": "^2.29.1"
34+
}
35+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"author": "Olive",
3+
"created": "2020-06-22T00:00:00Z",
4+
"description": "This is an example loop written in the Node LDK to demonstrate Whisper Disambiguation functionality.",
5+
"id": "b538b718-f2d1-4033-bb5f-8c9ac2c3da75",
6+
"name": "Node LDK - Example Whisper Disambiguation",
7+
"organization": "OliveAI",
8+
"specification": "1",
9+
"updated": "2020-07-23T00:00:00Z",
10+
"version": "1.0.0"
11+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
import moment from "moment";
3+
4+
import { HostServices, Logger, Loop, serveLoop } from '../../../dist';
5+
import {
6+
WhisperDisambiguationElements,
7+
// WhisperDisambiguationOption,
8+
} from '../../../dist/hostClients/whisperService';
9+
10+
import {RecallJSON} from './transform'
11+
12+
const logger = new Logger('olive-helps-node-example-network');
13+
14+
export interface Element {
15+
[key: string]: WhisperDisambiguationElements
16+
}
17+
18+
class ExampleLoop implements Loop {
19+
private _host: HostServices | undefined;
20+
21+
start(host: HostServices): void {
22+
this._host = host;
23+
const now = moment();
24+
const url = `https://api.fda.gov/food/enforcement.json?search=report_date:[${now.subtract(3, 'months').startOf('month').format('YYYYMMDD')}+TO+${now.endOf("month").format('YYYYMMDD')}]&limit=10`;
25+
26+
logger.info('Emitting list whisper', url);
27+
28+
try {
29+
this.host.network.httpRequest({
30+
url,
31+
method: "GET",
32+
body: "",
33+
}).then((response) => {
34+
const { results } = JSON.parse(Buffer.from(response.data).toString('utf8'));
35+
const itemList = {} as Element;
36+
37+
results.forEach((resultItem: RecallJSON, index: number) => {
38+
itemList[resultItem.recall_number] = {
39+
label: `😝${resultItem.recalling_firm} (${resultItem.recall_initiation_date}) jb`,
40+
order: index + 1,
41+
type: 'option',
42+
}
43+
})
44+
45+
this.host.whisper.disambiguationWhisper({
46+
label: 'Latest FDA Food Recall',
47+
markdown: '',
48+
elements: itemList,
49+
},
50+
// I don't know what goes here (An argument for 'listener' was not provided.)
51+
);
52+
})
53+
} catch (e) {
54+
logger.error('Error using network service', 'error', e.toString());
55+
}
56+
}
57+
58+
stop(): void {
59+
logger.info('Stopping');
60+
this._host = undefined;
61+
process.exit(0);
62+
}
63+
64+
private get host(): HostServices {
65+
if (this._host == null) {
66+
throw new Error('Cannot Retrieve Host Before Set');
67+
}
68+
return this._host;
69+
}
70+
}
71+
72+
const loop = new ExampleLoop();
73+
serveLoop(loop);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
export interface Recall {
2+
Address1: string;
3+
Address2: string;
4+
Codes: string;
5+
Country: string;
6+
City: string;
7+
Classification: string;
8+
Date: string;
9+
Description: string;
10+
Distribution: string;
11+
Firm: string;
12+
ID: string;
13+
Quantity: string;
14+
Reason: string;
15+
RecallType: string;
16+
State: string;
17+
Type: string;
18+
Zip: string;
19+
}
20+
21+
export interface RecallJSON {
22+
'address_1': string;
23+
'address_2': string;
24+
'code_info': string;
25+
country: string;
26+
city: string;
27+
classification: string;
28+
'recall_initiation_date': string;
29+
'product_description': string;
30+
'distribution_pattern': string;
31+
'recalling_firm': string;
32+
'recall_number': string;
33+
'product_quantity': string;
34+
'reason_for_recall': string;
35+
'voluntary_mandated': string;
36+
state: string;
37+
'product_type': string;
38+
'postal_code': string;
39+
}
40+
41+
/**
42+
* @param json
43+
*/
44+
export function decodeRecall(json: RecallJSON): Recall {
45+
return {
46+
Address1: json.address_1,
47+
Address2: json.address_2,
48+
Codes: json.code_info,
49+
Country: json.country,
50+
City: json.city,
51+
Classification: json.classification,
52+
Date: json.recall_initiation_date,
53+
Description: json.product_description,
54+
Distribution: json.distribution_pattern,
55+
Firm: json.recalling_firm,
56+
ID: json.recall_number,
57+
Quantity: json.product_quantity,
58+
Reason: json.reason_for_recall,
59+
RecallType: json.voluntary_mandated,
60+
State: json.state,
61+
Type: json.product_type,
62+
Zip: json.postal_code,
63+
};
64+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "dist",
4+
"allowJs": true,
5+
"target": "ES2018",
6+
"module": "CommonJS",
7+
"moduleResolution": "Node",
8+
"esModuleInterop": true,
9+
"rootDir": "./src",
10+
"declaration": true,
11+
"strict": true,
12+
"typeRoots": ["./node_modules/@types"]
13+
},
14+
"files": ["./src/index.ts"]
15+
}

ldk/node/src/hostClients/whisperMessageBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ export const generateWhisperDisambiguationElement = (
261261
break;
262262
}
263263
default: {
264-
throw new Error('Unexpected Input Type');
264+
throw new Error(`Unexpected Input Type ${JSON.stringify(element)}`);
265265
}
266266
}
267267
if (element.order && element.order > 0) {

0 commit comments

Comments
 (0)