File size: 8,838 Bytes
7def60a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package main

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"net/http"

	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
)

const (
	localAI     string = "http://localhost:8080"
	rootStatus  string = "[::b]<space>[::-]: Add Task  [::b]/[::-]: Search Task  [::b]<C-c>[::-]: Exit"
	inputStatus string = "Press [::b]<enter>[::-] to submit the task, [::b]<esc>[::-] to cancel"
)

type Task struct {
	Description string
	Similarity  float32
}

type AppState int

const (
	StateRoot AppState = iota
	StateInput
	StateSearch
)

type App struct {
	state AppState
	tasks []Task
	app   *tview.Application
	flex  *tview.Flex
	table *tview.Table
}

func NewApp() *App {
	return &App{
		state: StateRoot,
		tasks: []Task{
			{Description: "Take the dog for a walk (after I get a dog)"},
			{Description: "Go to the toilet"},
			{Description: "Allow TODOs to be marked completed or removed"},
		},
	}
}

func getEmbeddings(description string) ([]float32, error) {
	// Define the request payload
	payload := map[string]interface{}{
		"model": "bert-cpp-minilm-v6",
		"input": description,
	}

	// Marshal the payload into JSON
	jsonPayload, err := json.Marshal(payload)
	if err != nil {
		return nil, err
	}

	// Make the HTTP request to the local OpenAI embeddings API
	resp, err := http.Post(localAI+"/embeddings", "application/json", bytes.NewBuffer(jsonPayload))
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	// Check if the request was successful
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("request to embeddings API failed with status code: %d", resp.StatusCode)
	}

	// Parse the response body
	var result struct {
		Data []struct {
			Embedding []float32 `json:"embedding"`
		} `json:"data"`
	}
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return nil, err
	}

	// Return the embedding
	if len(result.Data) > 0 {
		return result.Data[0].Embedding, nil
	}
	return nil, errors.New("no embedding received from API")
}

type StoresSet struct {
	Store string `json:"store,omitempty" yaml:"store,omitempty"`

	Keys   [][]float32 `json:"keys" yaml:"keys"`
	Values []string    `json:"values" yaml:"values"`
}

func postTasksToExternalService(tasks []Task) error {
	keys := make([][]float32, 0, len(tasks))
	// Get the embeddings for the task description
	for _, task := range tasks {
		embedding, err := getEmbeddings(task.Description)
		if err != nil {
			return err
		}
		keys = append(keys, embedding)
	}

	values := make([]string, 0, len(tasks))
	for _, task := range tasks {
		values = append(values, task.Description)
	}

	// Construct the StoresSet object
	storesSet := StoresSet{
		Store:  "tasks_store", // Assuming you have a specific store name
		Keys:   keys,
		Values: values,
	}

	// Marshal the StoresSet object into JSON
	jsonData, err := json.Marshal(storesSet)
	if err != nil {
		return err
	}

	// Make the HTTP POST request to the external service
	resp, err := http.Post(localAI+"/stores/set", "application/json", bytes.NewBuffer(jsonData))
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	// Check if the request was successful
	if resp.StatusCode != http.StatusOK {
		// read resp body into string
		body, err := io.ReadAll(resp.Body)
		if err != nil {
			return err
		}
		return fmt.Errorf("store request failed with status code: %d: %s", resp.StatusCode, body)
	}

	return nil
}

type StoresFind struct {
	Store string `json:"store,omitempty" yaml:"store,omitempty"`

	Key  []float32 `json:"key" yaml:"key"`
	Topk int       `json:"topk" yaml:"topk"`
}

type StoresFindResponse struct {
	Keys         [][]float32 `json:"keys" yaml:"keys"`
	Values       []string    `json:"values" yaml:"values"`
	Similarities []float32   `json:"similarities" yaml:"similarities"`
}

func findSimilarTexts(inputText string, topk int) (StoresFindResponse, error) {
	// Initialize an empty response object
	response := StoresFindResponse{}

	// Get the embedding for the input text
	embedding, err := getEmbeddings(inputText)
	if err != nil {
		return response, err
	}

	// Construct the StoresFind object
	storesFind := StoresFind{
		Store: "tasks_store", // Assuming you have a specific store name
		Key:   embedding,
		Topk:  topk,
	}

	// Marshal the StoresFind object into JSON
	jsonData, err := json.Marshal(storesFind)
	if err != nil {
		return response, err
	}

	// Make the HTTP POST request to the external service's /stores/find endpoint
	resp, err := http.Post(localAI+"/stores/find", "application/json", bytes.NewBuffer(jsonData))
	if err != nil {
		return response, err
	}
	defer resp.Body.Close()

	// Check if the request was successful
	if resp.StatusCode != http.StatusOK {
		return response, fmt.Errorf("request to /stores/find failed with status code: %d", resp.StatusCode)
	}

	// Parse the response body to retrieve similar texts and similarities
	if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
		return response, err
	}

	return response, nil
}

func (app *App) updateUI() {
	// Clear the flex layout
	app.flex.Clear()
	app.flex.SetDirection(tview.FlexColumn)
	app.flex.AddItem(nil, 0, 1, false)

	midCol := tview.NewFlex()
	midCol.SetDirection(tview.FlexRow)
	midCol.AddItem(nil, 0, 1, false)

	// Create a new table.
	app.table.Clear()
	app.table.SetBorders(true)

	// Set table headers
	app.table.SetCell(0, 0, tview.NewTableCell("Description").SetAlign(tview.AlignLeft).SetExpansion(1).SetAttributes(tcell.AttrBold))
	app.table.SetCell(0, 1, tview.NewTableCell("Similarity").SetAlign(tview.AlignCenter).SetExpansion(0).SetAttributes(tcell.AttrBold))

	// Add the tasks to the table.
	for i, task := range app.tasks {
		row := i + 1
		app.table.SetCell(row, 0, tview.NewTableCell(task.Description))
		app.table.SetCell(row, 1, tview.NewTableCell(fmt.Sprintf("%.2f", task.Similarity)))
	}

	if app.state == StateInput {
		inputField := tview.NewInputField()
		inputField.
			SetLabel("New Task: ").
			SetFieldWidth(0).
			SetDoneFunc(func(key tcell.Key) {
				if key == tcell.KeyEnter {
					task := Task{Description: inputField.GetText()}
					app.tasks = append(app.tasks, task)
					app.state = StateRoot
					err := postTasksToExternalService([]Task{task})
					if err != nil {
						panic(err)
					}
				}
				app.updateUI()
			})
		midCol.AddItem(inputField, 3, 2, true)
		app.app.SetFocus(inputField)
	} else if app.state == StateSearch {
		searchField := tview.NewInputField()
		searchField.SetLabel("Search: ").
			SetFieldWidth(0).
			SetDoneFunc(func(key tcell.Key) {
				if key == tcell.KeyEnter {
					similar, err := findSimilarTexts(searchField.GetText(), 100)
					if err != nil {
						panic(err)
					}
					app.tasks = make([]Task, len(similar.Keys))
					for i, v := range similar.Values {
						app.tasks[i] = Task{Description: v, Similarity: similar.Similarities[i]}
					}
				}
				app.updateUI()
			})
		midCol.AddItem(searchField, 3, 2, true)
		app.app.SetFocus(searchField)
	} else {
		midCol.AddItem(nil, 3, 1, false)
	}

	midCol.AddItem(app.table, 0, 2, true)

	// Add the status bar to the flex layout
	statusBar := tview.NewTextView().
		SetText(rootStatus).
		SetDynamicColors(true).
		SetTextAlign(tview.AlignCenter)
	if app.state == StateInput {
		statusBar.SetText(inputStatus)
	}
	midCol.AddItem(statusBar, 1, 1, false)
	midCol.AddItem(nil, 0, 1, false)

	app.flex.AddItem(midCol, 0, 10, true)
	app.flex.AddItem(nil, 0, 1, false)

	// Set the flex as the root element
	app.app.SetRoot(app.flex, true)
}

func main() {
	app := NewApp()
	tApp := tview.NewApplication()
	flex := tview.NewFlex().SetDirection(tview.FlexRow)
	table := tview.NewTable()

	app.app = tApp
	app.flex = flex
	app.table = table

	app.updateUI() // Initial UI setup

	app.app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		switch app.state {
		case StateRoot:
			// Handle key events when in the root state
			switch event.Key() {
			case tcell.KeyRune:
				switch event.Rune() {
				case ' ':
					app.state = StateInput
					app.updateUI()
					return nil // Event is handled
				case '/':
					app.state = StateSearch
					app.updateUI()
					return nil // Event is handled
				}
			}

		case StateInput:
			// Handle key events when in the input state
			if event.Key() == tcell.KeyEsc {
				// Exit input state without adding a task
				app.state = StateRoot
				app.updateUI()
				return nil // Event is handled
			}

		case StateSearch:
			// Handle key events when in the search state
			if event.Key() == tcell.KeyEsc {
				// Exit search state
				app.state = StateRoot
				app.updateUI()
				return nil // Event is handled
			}
		}

		// Return the event for further processing by tview
		return event
	})

	if err := postTasksToExternalService(app.tasks); err != nil {
		panic(err)
	}

	// Start the application
	if err := app.app.Run(); err != nil {
		panic(err)
	}
}