-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
86 lines (81 loc) · 2.55 KB
/
main.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
/**
* @file main.c
* @author Warren Mann ([email protected])
* @brief Main program.
* @version 0.1.0
* @date 2024-04-27
* @copyright Copyright (c) 2024
* @details
* Main program. Parses command line arguments into a JSON object. The results
* of that are used first to execute any actions that are specified, such as
* dumping the query history or listing the available models. Then, assuming
* the given command line actions don't result in exiting the program (listing
* models for example, exits after printing the list), the correct API
* interface is selected and the query is sent to the API.
*/
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <json-c/json.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 "option.h"
#include "setting.h"
#include "ollama.h"
#include "openai.h"
#ifdef DEBUG
int indent_level = 0;
char *indent_string = " ";
#endif
int main(int ac, char **av) {
debug_enter();
json_object *actions_obj = json_object_new_object();
json_object *settings_obj = json_object_new_object();
int result = 1;
if (actions_obj == NULL) {
fprintf(stderr, "Error initializing JSON object\n");
goto term;
}
if (function_init()) {
goto term;
}
if (configure(actions_obj, settings_obj, ac, av)) {
goto term;
}
json_object *api_opt = NULL;
if (json_object_object_get_ex(settings_obj, SETTING_KEY_AI_PROVIDER, &api_opt)) {
const api_id_t api = api_name_to_id(json_object_get_string(api_opt));
api_interface = (api_interface_t *)api_interfaces[api]();
}
if (!json_object_object_get_ex(actions_obj, ACTION_KEY_QUERY, NULL)) {
json_object_object_add(actions_obj, ACTION_KEY_QUERY, NULL);
}
action_result_t action_result = action_execute_all(actions_obj, settings_obj);
debug("Completed actions with result = %i\n", action_result);
if (action_result == ACTION_END || action_result == ACTION_CONTINUE) {
result = 0;
goto term;
} else if (action_result == ACTION_ERROR) {
result = 1;
goto term;
}
term:
if (result == 0) {
context_update();
}
if (actions_obj != NULL) {
json_object_put(actions_obj);
}
if (settings_obj != NULL) {
json_object_put(settings_obj);
}
debug("Exiting with result = %i\n", result);
debug_return result;
}