blob: e833f70fb72d9e4cfed6243c42981d72e8d2d3cd (
plain)
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
|
#include "mcp/tools/common/context.h"
#include <stdlib.h>
#include "base/allocator.h"
/* mcpc's callback API has no user-data parameter. The stdio server is single-instance. */
static tiny_mcp_tools_context_t *active_context;
tiny_search_index_t *
tiny_mcp_tools_build_search_index(const tiny_store_t *store)
{
tiny_search_index_t *index = tiny_search_index_create();
size_t position;
if (!index)
{
return NULL;
}
for (position = 0; position < tiny_store_entry_count(store); ++position)
{
if (!tiny_search_index_add(index, &tiny_store_entry(store, position)->message, position))
{
tiny_search_index_free(index);
return NULL;
}
}
return index;
}
tiny_mcp_tools_context_t *
tiny_mcp_tools_context_new(tiny_store_t *store)
{
tiny_mcp_tools_context_t *context;
if (!store)
{
return NULL;
}
context = malloc(sizeof(*context));
if (!context)
{
return NULL;
}
context->store = store;
context->search_index = tiny_mcp_tools_build_search_index(store);
context->history_index = tiny_history_index_build(store);
context->memory_index = tiny_memory_index_build(store);
if (!context->search_index || !context->history_index || !context->memory_index)
{
tiny_search_index_free(context->search_index);
tiny_history_index_free(context->history_index);
tiny_memory_index_free(context->memory_index);
free(context);
return NULL;
}
return context;
}
void
tiny_mcp_tools_context_free(tiny_mcp_tools_context_t *context)
{
if (!context)
{
return;
}
tiny_search_index_free(context->search_index);
tiny_history_index_free(context->history_index);
tiny_memory_index_free(context->memory_index);
free(context);
}
bool
tiny_mcp_tools_ensure_memory_index(tiny_mcp_tools_context_t *context)
{
if (!context)
{
return false;
}
if (!context->memory_index)
{
context->memory_index = tiny_memory_index_build(context->store);
}
return context->memory_index != NULL;
}
void
tiny_mcp_tools_invalidate_indexes(tiny_mcp_tools_context_t *context)
{
if (!context)
{
return;
}
tiny_search_index_free(context->search_index);
tiny_history_index_free(context->history_index);
tiny_memory_index_free(context->memory_index);
context->search_index = NULL;
context->history_index = NULL;
context->memory_index = NULL;
}
bool
tiny_mcp_tools_context_activate(tiny_mcp_tools_context_t *context)
{
if (!context || active_context)
{
return false;
}
active_context = context;
return true;
}
void
tiny_mcp_tools_context_deactivate(tiny_mcp_tools_context_t *context)
{
if (active_context == context)
{
active_context = NULL;
}
}
tiny_mcp_tools_context_t *
tiny_mcp_tools_context_current(void)
{
return active_context;
}
bool
tiny_mcp_tools_ensure_history_index(tiny_mcp_tools_context_t *context)
{
if (!context)
{
return false;
}
if (!context->history_index)
{
context->history_index = tiny_history_index_build(context->store);
}
return context->history_index != NULL;
}
|