|
| 1 | +package loop |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "time" |
| 7 | + |
| 8 | + ldk "github.com/open-olive/loop-development-kit/ldk/go" |
| 9 | +) |
| 10 | + |
| 11 | +type recall struct { |
| 12 | + Address1 string `json:"address_1"` |
| 13 | + Address2 string `json:"address_2"` |
| 14 | + Codes string `json:"code_info"` |
| 15 | + Country string `json:"country"` |
| 16 | + City string `json:"city"` |
| 17 | + Classification string `json:"classification"` |
| 18 | + Date string `json:"recall_initiation_date"` |
| 19 | + Description string `json:"product_description"` |
| 20 | + Distribution string `json:"distribution_pattern"` |
| 21 | + Firm string `json:"recalling_firm"` |
| 22 | + ID string `json:"recall_number"` |
| 23 | + Quantity string `json:"product_quantity"` |
| 24 | + Reason string `json:"reason_for_recall"` |
| 25 | + RecallType string `json:"voluntary_mandated"` |
| 26 | + State string `json:"state"` |
| 27 | + Type string `json:"product_type"` |
| 28 | + Zip string `json:"postal_code"` |
| 29 | +} |
| 30 | + |
| 31 | +type apiResponse struct { |
| 32 | + Meta interface{} `json:"meta"` |
| 33 | + Results []recall `json:"results"` |
| 34 | +} |
| 35 | + |
| 36 | +// Serve allows Olive Helps to have access the loop |
| 37 | +func Serve() error { |
| 38 | + l := ldk.NewLogger("example-whisper-disambiguation") |
| 39 | + loop, err := NewLoop(l) |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + ldk.ServeLoopPlugin(l, loop) |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +// Loop is a structure for generating SideKick whispers |
| 48 | +type Loop struct { |
| 49 | + ctx context.Context |
| 50 | + cancel context.CancelFunc |
| 51 | + |
| 52 | + sidekick ldk.Sidekick |
| 53 | + logger *ldk.Logger |
| 54 | +} |
| 55 | + |
| 56 | +// NewLoop returns a pointer to a loop |
| 57 | +func NewLoop(logger *ldk.Logger) (*Loop, error) { |
| 58 | + return &Loop{ |
| 59 | + logger: logger, |
| 60 | + }, nil |
| 61 | +} |
| 62 | + |
| 63 | +// LoopStart is called by the host when the plugin is started to provide access to the host process |
| 64 | +func (c *Loop) LoopStart(sidekick ldk.Sidekick) error { |
| 65 | + c.logger.Info("Starting example whisper disambiguation loop") |
| 66 | + c.ctx, c.cancel = context.WithCancel(context.Background()) |
| 67 | + |
| 68 | + c.sidekick = sidekick |
| 69 | + |
| 70 | + now := time.Now() |
| 71 | + |
| 72 | + response, err := sidekick.Network().HTTPRequest(c.ctx, &ldk.HTTPRequest{ |
| 73 | + URL: "https://api.fda.gov/food/enforcement.json?search=report_date:[" + now.AddDate(0, -3, 0).Format("20060102") + "+TO+" + now.Format("20060102") + "]&limit=10", |
| 74 | + Method: "GET", |
| 75 | + Body: nil, |
| 76 | + }) |
| 77 | + if err != nil { |
| 78 | + c.logger.Error("received error from callback", err) |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + if response.ResponseCode == 200 { |
| 83 | + var data apiResponse |
| 84 | + |
| 85 | + if err := json.Unmarshal(response.Data, &data); err != nil { |
| 86 | + c.logger.Error("Error unmarshaling response payload", err) |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + elements := make(map[string]ldk.WhisperContentDisambiguationElement) |
| 91 | + |
| 92 | + for index := range data.Results { |
| 93 | + item := data.Results[index] |
| 94 | + elements[item.ID] = &ldk.WhisperContentDisambiguationElementOption{ |
| 95 | + Label: "😝" + item.Firm + " (" + item.Date + ")", |
| 96 | + Order: uint32(index) + 1, |
| 97 | + OnChange: func(key string) { |
| 98 | + go func() { |
| 99 | + err := c.sidekick.Whisper().List(c.ctx, &ldk.WhisperContentList{ |
| 100 | + Label: item.Firm + " Recall", |
| 101 | + Elements: map[string]ldk.WhisperContentListElement{ |
| 102 | + "topMessage": &ldk.WhisperContentListElementMessage{ |
| 103 | + Style: ldk.WhisperContentListElementStyleNone, |
| 104 | + Body: item.Description, |
| 105 | + Align: ldk.WhisperContentListElementAlignLeft, |
| 106 | + Order: 0, |
| 107 | + }, |
| 108 | + "sectionDivider": &ldk.WhisperContentListElementDivider{ |
| 109 | + Order: 1, |
| 110 | + }, |
| 111 | + "reason": &ldk.WhisperContentListElementPair{ |
| 112 | + Label: "Reason", |
| 113 | + Order: 2, |
| 114 | + Value: item.Reason, |
| 115 | + }, |
| 116 | + "distribution": &ldk.WhisperContentListElementPair{ |
| 117 | + Label: "Distribution", |
| 118 | + Order: 3, |
| 119 | + Value: item.Distribution, |
| 120 | + }, |
| 121 | + "quantity": &ldk.WhisperContentListElementPair{ |
| 122 | + Label: "Quantity", |
| 123 | + Order: 4, |
| 124 | + Value: item.Quantity, |
| 125 | + }, |
| 126 | + "codes": &ldk.WhisperContentListElementPair{ |
| 127 | + Extra: true, |
| 128 | + Label: "Codes", |
| 129 | + Order: 5, |
| 130 | + Value: item.Codes, |
| 131 | + }, |
| 132 | + "id": &ldk.WhisperContentListElementPair{ |
| 133 | + Extra: true, |
| 134 | + Label: "Recall number", |
| 135 | + Order: 6, |
| 136 | + Value: item.ID, |
| 137 | + }, |
| 138 | + "date": &ldk.WhisperContentListElementPair{ |
| 139 | + Extra: true, |
| 140 | + Label: "Date initiated", |
| 141 | + Order: 7, |
| 142 | + Value: item.Date, |
| 143 | + }, |
| 144 | + "recallType": &ldk.WhisperContentListElementPair{ |
| 145 | + Extra: true, |
| 146 | + Label: "Recall type", |
| 147 | + Order: 8, |
| 148 | + Value: item.RecallType, |
| 149 | + }, |
| 150 | + "type": &ldk.WhisperContentListElementPair{ |
| 151 | + Extra: true, |
| 152 | + Label: "Product type", |
| 153 | + Order: 9, |
| 154 | + Value: item.Type, |
| 155 | + }, |
| 156 | + "classification": &ldk.WhisperContentListElementPair{ |
| 157 | + Extra: true, |
| 158 | + Label: "Classification", |
| 159 | + Order: 10, |
| 160 | + Value: item.Classification, |
| 161 | + }, |
| 162 | + "address": &ldk.WhisperContentListElementPair{ |
| 163 | + Extra: true, |
| 164 | + Label: "Company address", |
| 165 | + Order: 11, |
| 166 | + Value: item.Address1 + " " + item.Address2 + " " + item.City + ", " + item.State + " " + item.Zip + " " + item.Country, |
| 167 | + }, |
| 168 | + }, |
| 169 | + }) |
| 170 | + if err != nil { |
| 171 | + c.logger.Error("failed to emit whisper", "error", err) |
| 172 | + } |
| 173 | + }() |
| 174 | + }, |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + elements["header1"] = &ldk.WhisperContentDisambiguationElementText{ |
| 179 | + Body: "# Text header\n## Text subheader", |
| 180 | + Order: 0, |
| 181 | + } |
| 182 | + elements["header2"] = &ldk.WhisperContentDisambiguationElementText{ |
| 183 | + Body: "# Text header", |
| 184 | + Order: 6, |
| 185 | + } |
| 186 | + |
| 187 | + go func() { |
| 188 | + _, err := c.sidekick.Whisper().Disambiguation(c.ctx, &ldk.WhisperContentDisambiguation{ |
| 189 | + Label: "Latest FDA Food Recalls", |
| 190 | + Elements: elements, |
| 191 | + }) |
| 192 | + if err != nil { |
| 193 | + c.logger.Error("failed to emit whisper", "error", err) |
| 194 | + } |
| 195 | + }() |
| 196 | + |
| 197 | + return nil |
| 198 | + } |
| 199 | + |
| 200 | + c.logger.Error("received response code of", response.ResponseCode) |
| 201 | + return nil |
| 202 | +} |
| 203 | + |
| 204 | +// LoopStop is called by the host when the plugin is stopped |
| 205 | +func (c *Loop) LoopStop() error { |
| 206 | + c.logger.Info("controller LoopStop called") |
| 207 | + c.cancel() |
| 208 | + |
| 209 | + return nil |
| 210 | +} |
0 commit comments