-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.c
275 lines (257 loc) · 8.6 KB
/
action.c
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
/**
* @file action.c
* @author Warren Mann (warren@nonvol.io)
* @brief Actions that are performed in response to certain command line arguments.
* @version 0.1.0
* @date 2024-04-27
* @copyright Copyright (c) 2024
*/
#include <stdio.h>
#include <string.h>
#include <json-c/json_object.h>
#include "chewie.h"
#include "action.h"
#include "api.h"
#include "configure.h"
#include "context.h"
#include "file.h"
#include "function.h"
#include "input.h"
#include "setting.h"
static action_result_t dump_query_history(json_object *settings, json_object *data);
static action_result_t get_embeddings(json_object *settings, json_object *data);
static action_result_t list_apis(json_object *settings, json_object *data);
static action_result_t list_models(json_object *settings, json_object *data);
static action_result_t load_function_file(json_object *settings, json_object *data);
static action_result_t query(json_object *settings, json_object *data);
static action_result_t show_help(json_object *settings, json_object *data);
static action_result_t show_version(json_object *settings, json_object *data);
static action_result_t reset_context(json_object *settings, json_object *data);
static action_result_t update_context(json_object *settings, json_object *data);
static action_t action_version = {
.name = ACTION_KEY_VERSION,
.callback = show_version,
};
static action_t action_help = {
.name = ACTION_KEY_HELP,
.callback = show_help,
};
static action_t action_list_apis = {
.name = ACTION_KEY_LIST_APIS,
.callback = list_apis,
};
static action_t action_list_models = {
.name = ACTION_KEY_LIST_MODELS,
.callback = list_models,
};
static action_t action_load_function_file = {
.name = ACTION_KEY_LOAD_FUNCTION_FILE,
.callback = load_function_file,
};
static action_t action_query = {
.name = ACTION_KEY_QUERY,
.callback = query,
};
static action_t action_reset_context = {
.name = ACTION_KEY_RESET_CONTEXT,
.callback = reset_context,
};
static action_t action_dump_query_history = {
.name = ACTION_KEY_DUMP_QUERY_HISTORY,
.callback = dump_query_history
};
static action_t action_update_context = {
.name = ACTION_KEY_UPDATE_CONTEXT,
.callback = update_context
};
static action_t action_get_embeddings = {
.name = ACTION_KEY_GET_EMBEDDINGS,
.callback = get_embeddings
};
static action_t *action_templates[] = {
&action_version,
&action_help,
&action_list_apis,
&action_list_models,
&action_load_function_file,
&action_reset_context,
&action_dump_query_history,
&action_update_context,
&action_get_embeddings,
&action_query,
NULL
};
static action_t **merged_actions = action_templates;
static action_t **action_merge(action_t **actions1, action_t **actions2);
action_result_t action_execute_all(json_object *actions, json_object *settings) {
debug_enter();
debug("actions: %s\n", json_object_to_json_string_ext(actions, JSON_C_TO_STRING_PRETTY));
for (api_id_t i = 0; i < api_id_max; i++) {
const api_interface_t *api_interface = api_interfaces[i + 1]();
action_t **b = api_interface->get_actions();
if (b != NULL) {
action_t **c = action_merge(merged_actions, b);
if (c == NULL) {
debug_return ACTION_ERROR;
}
if (merged_actions != action_templates) {
free(merged_actions);
}
merged_actions = c;
}
}
json_object *json_obj = NULL;
action_result_t result = ACTION_CONTINUE;
for (int i = 0; merged_actions[i] != NULL; i++) {
action_t *action_template = merged_actions[i];
debug("processing action %s\n", action_template->name);
if (json_object_object_get_ex(actions, action_template->name, &json_obj)) {
if (action_template->callback != NULL) {
action_result_t r = action_template->callback(settings, json_obj);
if (r == ACTION_ERROR) {
result = ACTION_ERROR;
break;
}
if (r == ACTION_END) {
result = ACTION_END;
break;
}
}
}
}
debug_return result;
}
static action_result_t dump_query_history(json_object *actions, json_object *settings_obj) {
debug_enter();
context_dump_history();
debug_return ACTION_END;
}
static action_result_t get_embeddings(json_object *settings, json_object *data) {
debug_enter();
char *query = NULL;
json_object *prompt = NULL;
if (json_object_object_get_ex(settings, SETTING_KEY_PROMPT, &prompt) && prompt != NULL) {
json_object_object_add(settings, SETTING_KEY_PROMPT, prompt);
} else {
query = input_get();
if (query != NULL) {
json_object_object_add(settings, SETTING_KEY_PROMPT, json_object_new_string(query));
free(query);
query = NULL;
}
}
if (query != NULL) {
json_object_object_add(settings, SETTING_KEY_PROMPT, json_object_new_string(query));
free(query);
query = NULL;
}
if (api_interface->get_embeddings(settings)) {
debug_return ACTION_ERROR;
}
debug_return ACTION_END;
}
static action_result_t list_apis(json_object *settings, json_object *data) {
debug_enter();
printf("Available APIs:\n");
for (api_id_t i = 0; i < api_id_max; i++) {
const api_interface_t *api_interface = api_interfaces[i + 1]();
const char *s = api_interface->get_api_name();
printf(" %s\n", s);
}
debug_return ACTION_END;
}
static action_result_t list_models(json_object *settings, json_object *data) {
debug_enter();
debug("data = %s\n", json_object_to_json_string_ext(data, JSON_C_TO_STRING_PRETTY));
if (api_interface->print_model_list(settings)) {
debug_return ACTION_ERROR;
}
debug_return ACTION_END;
}
static action_result_t query(json_object *settings, json_object *data) {
debug_enter();
char *query = NULL;
json_object *prompt = NULL;
json_object_object_get_ex(settings, SETTING_KEY_PROMPT, &prompt);
if (prompt == NULL) {
query = input_get();
if (query != NULL) {
json_object_object_add(settings, SETTING_KEY_PROMPT, json_object_new_string(query));
free(query);
query = NULL;
}
}
api_interface->query(settings);
debug_return ACTION_END;
}
static action_result_t load_function_file(json_object *settings, json_object *data) {
debug_enter();
const char *fn = json_object_get_string(data);
if (fn != NULL) {
debug("lua filename is %s\n", fn);
if (function_load(fn, settings)) {
debug_return ACTION_ERROR;
}
debug_return ACTION_CONTINUE;
}
debug_return ACTION_ERROR;
}
static action_result_t show_help(json_object *settings, json_object *data) {
debug_enter();
option_show_help((option_t **)json_object_get_int64(data));
debug_return ACTION_END;
}
static action_result_t show_version(json_object *settings, json_object *data) {
debug_enter();
printf("%s v. %i.%i.%i\n", program_name, VERSION_MAJOR, VERSION_MINOR, VERSION_REV);
debug_return ACTION_END;
}
static action_result_t reset_context(json_object *settings, json_object *options) {
debug_enter();
json_object *context_fn = NULL;
if (json_object_object_get_ex(settings, SETTING_KEY_CONTEXT_FILENAME, &context_fn)) {
const char *fn = json_object_get_string(context_fn);
if (fn != NULL) {
context_new(fn);
} else {
context_new(context_fn_default);
}
file_truncate(fn);
debug_return ACTION_CONTINUE;
}
debug_return ACTION_ERROR;
}
static action_result_t update_context(json_object *settings, json_object *data) {
debug_enter();
context_update();
debug("settings = %s\n", json_object_to_json_string_ext(settings, JSON_C_TO_STRING_PRETTY));
debug_return ACTION_END;
}
static action_t **action_merge(action_t **actions1, action_t **actions2) {
debug_enter();
int n1 = 0;
int n2 = 0;
if (actions1 != NULL) {
while (actions1[n1] != NULL) {
n1++;
}
}
if (actions2 != NULL) {
while (actions2[n2] != NULL) {
n2++;
}
}
int n = n1 + n2 + 1;
action_t **actions = malloc(n * sizeof(action_t *));
if (actions == NULL) {
debug_return NULL;
}
if (actions1 != NULL) {
memcpy(actions, actions1, n1 * sizeof(action_t *));
}
if (actions2 != NULL) {
memcpy(actions + n1, actions2, n2 * sizeof(action_t *));
}
actions[n - 1] = NULL;
debug_return actions;
}