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
357
|
#define _POSIX_C_SOURCE 200809L
#include "hybbx/aichat.h"
#include "hybbx/session.h"
#include "hybbx/service.h"
#include "hybbx/util.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <unistd.h>
#define HYBBX_AICHAT_MAX_USERS 64
typedef struct hybbx_aichat_user_rate {
char username[64];
time_t window_start;
unsigned request_count;
} hybbx_aichat_user_rate_t;
static hybbx_aichat_user_rate_t g_user_rates[HYBBX_AICHAT_MAX_USERS];
static size_t g_user_rate_count = 0;
void hybbx_aichat_config_init(hybbx_aichat_config_t *cfg, const hybbx_config_t *config)
{
if (cfg == NULL) {
return;
}
memset(cfg, 0, sizeof(*cfg));
if (config == NULL) {
return;
}
cfg->enabled = hybbx_config_get_bool(config, "aichat", "enabled", 0);
const char *val;
val = hybbx_config_get(config, "aichat", "provider", "openrouter");
snprintf(cfg->provider, sizeof(cfg->provider), "%s", val ? val : "openrouter");
val = hybbx_config_get(config, "aichat", "api_key", "");
snprintf(cfg->api_key, sizeof(cfg->api_key), "%s", val ? val : "");
val = hybbx_config_get(config, "aichat", "api_url", "https://openrouter.ai/api/v1/chat/completions");
snprintf(cfg->api_url, sizeof(cfg->api_url), "%s", val ? val : "https://openrouter.ai/api/v1/chat/completions");
val = hybbx_config_get(config, "aichat", "model", "meta-llama/llama-3.2-11b-vision-instruct:free");
snprintf(cfg->model, sizeof(cfg->model), "%s", val ? val : "meta-llama/llama-3.2-11b-vision-instruct:free");
cfg->max_tokens = hybbx_config_get_uint(config, "aichat", "max_tokens", 300, 50, 2048);
cfg->rate_limit_per_user_hour = hybbx_config_get_uint(config, "aichat", "rate_limit_per_user_hour", 5, 1, 100);
cfg->timeout_sec = hybbx_config_get_uint(config, "aichat", "timeout_sec", 10, 2, 60);
val = hybbx_config_get(config, "aichat", "site_url", "https://hybbx.un1t.me");
snprintf(cfg->site_url, sizeof(cfg->site_url), "%s", val ? val : "https://hybbx.un1t.me");
val = hybbx_config_get(config, "aichat", "site_name", "hyBBX Network");
snprintf(cfg->site_name, sizeof(cfg->site_name), "%s", val ? val : "hyBBX Network");
}
static int check_and_inc_rate_limit(const char *username, unsigned max_per_hour)
{
time_t now = time(NULL);
size_t i;
int found = -1;
for (i = 0; i < g_user_rate_count; i++) {
if (strcmp(g_user_rates[i].username, username) == 0) {
found = (int)i;
break;
}
}
if (found < 0) {
if (g_user_rate_count < HYBBX_AICHAT_MAX_USERS) {
found = (int)g_user_rate_count++;
snprintf(g_user_rates[found].username, sizeof(g_user_rates[found].username), "%s", username);
g_user_rates[found].window_start = now;
g_user_rates[found].request_count = 0;
} else {
found = 0; /* Fallback wrap */
snprintf(g_user_rates[found].username, sizeof(g_user_rates[found].username), "%s", username);
g_user_rates[found].window_start = now;
g_user_rates[found].request_count = 0;
}
}
/* Reset hourly window if > 3600 seconds elapsed */
if (now - g_user_rates[found].window_start >= 3600) {
g_user_rates[found].window_start = now;
g_user_rates[found].request_count = 0;
}
if (g_user_rates[found].request_count >= max_per_hour) {
return 0; /* Exceeded limit */
}
g_user_rates[found].request_count++;
return 1; /* OK */
}
static void json_escape(const char *in, char *out, size_t out_sz)
{
size_t j = 0;
if (out_sz == 0) return;
for (; in != NULL && *in != '\0' && j + 2 < out_sz; in++) {
if (*in == '"' || *in == '\\') {
out[j++] = '\\';
}
if (*in == '\n' || *in == '\r') {
out[j++] = ' ';
continue;
}
out[j++] = *in;
}
out[j] = '\0';
}
static int openrouter_parse_content(const char *json, char *out, size_t out_sz)
{
const char *p;
size_t j = 0;
int esc = 0;
if (json == NULL || out == NULL || out_sz == 0) return -1;
out[0] = '\0';
p = strstr(json, "\"choices\"");
if (p == NULL) p = json;
p = strstr(p, "\"content\"");
if (p == NULL) {
p = strstr(json, "\"error\"");
if (p != NULL) {
const char *msg = strstr(p, "\"message\"");
if (msg != NULL) {
msg = strchr(msg, ':');
if (msg != NULL) {
while (*msg != '\0' && (*msg == ':' || *msg == ' ' || *msg == '"')) {
msg++;
}
snprintf(out, out_sz, "[AI Error] %.*s", (int)(out_sz - 16), msg);
/* Remove trailing quote */
size_t len = strlen(out);
if (len > 0 && out[len - 1] == '"') out[len - 1] = '\0';
return 0;
}
}
}
snprintf(out, out_sz, "[AI Error] Unable to parse response from AI provider.");
return -1;
}
p = strchr(p, ':');
if (p == NULL) return -1;
while (*p != '\0' && *p != '"') p++;
if (*p == '"') p++;
while (*p != '\0' && j + 1 < out_sz) {
if (esc) {
if (*p == 'n') out[j++] = '\n';
else if (*p == 'r') out[j++] = ' ';
else if (*p == 't') out[j++] = ' ';
else out[j++] = *p;
esc = 0;
} else if (*p == '\\') {
esc = 1;
} else if (*p == '"') {
break;
} else {
out[j++] = *p;
}
p++;
}
out[j] = '\0';
return 0;
}
static void send_wrapped_output(hybbx_session_t *session, const char *text)
{
char line_buf[82];
size_t col = 0;
const char *p = text;
hybbx_session_write_line(session, "--- AI Assistant (OpenRouter) ---");
while (*p != '\0') {
if (*p == '\n') {
line_buf[col] = '\0';
hybbx_session_write_line(session, line_buf);
col = 0;
p++;
continue;
}
line_buf[col++] = *p++;
if (col >= 76) {
/* Find last space if possible */
size_t break_pos = col;
while (break_pos > 40 && line_buf[break_pos - 1] != ' ') {
break_pos--;
}
if (break_pos <= 40) {
break_pos = col;
}
char saved = line_buf[break_pos];
line_buf[break_pos] = '\0';
hybbx_session_write_line(session, line_buf);
/* Shift remainder */
size_t rem = col - break_pos;
if (saved == ' ') {
/* Skip space */
if (rem > 0) rem--;
memmove(line_buf, line_buf + break_pos + 1, rem);
} else {
memmove(line_buf, line_buf + break_pos, rem);
}
col = rem;
}
}
if (col > 0) {
line_buf[col] = '\0';
hybbx_session_write_line(session, line_buf);
}
hybbx_session_write_line(session, "---------------------------------");
}
hybbx_result_t cmd_aichat(hybbx_service_t *service,
hybbx_session_t *session,
const hybbx_parsed_command_t *cmd)
{
hybbx_aichat_config_t cfg;
char prompt[1024] = {0};
char escaped_prompt[2048] = {0};
char json_req[4096] = {0};
char json_resp[8192] = {0};
char ai_text[4096] = {0};
char cmd_buf[8192] = {0};
size_t i;
hybbx_config_t config_obj;
const char *cfg_path = hybbx_service_config_path(service);
if (cfg_path != NULL && cfg_path[0] != '\0') {
if (hybbx_config_load(&config_obj, cfg_path) == HYBBX_OK) {
hybbx_aichat_config_init(&cfg, &config_obj);
hybbx_config_free(&config_obj);
} else {
hybbx_aichat_config_init(&cfg, NULL);
}
} else {
hybbx_aichat_config_init(&cfg, NULL);
}
if (!cfg.enabled) {
hybbx_session_write_line(session,
"[AI] /aichat is disabled on this station. (Enable with [aichat] enabled=yes)");
return HYBBX_OK;
}
if (cmd->argc == 0) {
hybbx_session_write_line(session, "Usage: /aichat <your prompt or question>");
hybbx_session_write_line(session, "Aliases: /ai, /ask");
hybbx_session_write_line(session,
"Notice: Prompts in /aichat are processed via OpenRouter AI. Do not send private keys/secrets.");
return HYBBX_OK;
}
/* Reconstruct prompt string */
for (i = 0; i < cmd->argc; i++) {
if (i > 0) strncat(prompt, " ", sizeof(prompt) - strlen(prompt) - 1);
strncat(prompt, cmd->argv[i], sizeof(prompt) - strlen(prompt) - 1);
}
const char *user = hybbx_session_username(session);
if (user == NULL || user[0] == '\0') {
user = "Guest";
}
if (!check_and_inc_rate_limit(user, cfg.rate_limit_per_user_hour)) {
snprintf(ai_text, sizeof(ai_text),
"[AI] Hourly limit reached (%u/%u per hour for %s). Please try again later.",
cfg.rate_limit_per_user_hour, cfg.rate_limit_per_user_hour, user);
hybbx_session_write_line(session, ai_text);
return HYBBX_OK;
}
if (cfg.api_key[0] == '\0') {
hybbx_session_write_line(session,
"[AI Error] API key missing in [aichat] section of ./local/hybbx.ini");
return HYBBX_OK;
}
hybbx_session_write_line(session, "[AI] Querying OpenRouter assistant...");
json_escape(prompt, escaped_prompt, sizeof(escaped_prompt));
snprintf(json_req, sizeof(json_req),
"{\"model\":\"%s\",\"max_tokens\":%u,\"temperature\":0.7,"
"\"messages\":["
"{\"role\":\"system\",\"content\":\"You are an AI assistant on a text-based Mailbox (BBX) station. Reply strictly in plain ASCII text. Keep responses concise, under 75 columns wide, without markdown headers or complex symbols.\"},"
"{\"role\":\"user\",\"content\":\"%s\"}"
"]}",
cfg.model, cfg.max_tokens, escaped_prompt);
/* Construct curl command line safely via temporary request file */
char tmp_req_path[256];
snprintf(tmp_req_path, sizeof(tmp_req_path), "/tmp/hybbx_aichat_%d_%lu.json",
(int)getpid(), (unsigned long)time(NULL));
FILE *fp_req = fopen(tmp_req_path, "w");
if (fp_req == NULL) {
hybbx_session_write_line(session, "[AI Error] System unable to write temp request file.");
return HYBBX_OK;
}
fputs(json_req, fp_req);
fclose(fp_req);
snprintf(cmd_buf, sizeof(cmd_buf),
"curl -s -m %u -X POST \"%s\" "
"-H \"Authorization: Bearer %s\" "
"-H \"HTTP-Referer: %s\" "
"-H \"X-Title: %s\" "
"-H \"Content-Type: application/json\" "
"-d @\"%s\"",
cfg.timeout_sec, cfg.api_url, cfg.api_key,
cfg.site_url, cfg.site_name, tmp_req_path);
FILE *pipe = popen(cmd_buf, "r");
if (pipe == NULL) {
unlink(tmp_req_path);
hybbx_session_write_line(session, "[AI Error] System unable to launch curl command.");
return HYBBX_OK;
}
size_t bytes_read = fread(json_resp, 1, sizeof(json_resp) - 1, pipe);
json_resp[bytes_read] = '\0';
pclose(pipe);
unlink(tmp_req_path);
if (bytes_read == 0) {
hybbx_session_write_line(session, "[AI Error] No response received from OpenRouter API.");
return HYBBX_OK;
}
if (openrouter_parse_content(json_resp, ai_text, sizeof(ai_text)) != 0) {
hybbx_session_write_line(session, "[AI Error] Failed to parse AI response content.");
return HYBBX_OK;
}
send_wrapped_output(session, ai_text);
return HYBBX_OK;
}
|