-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatai-api.php
More file actions
63 lines (53 loc) · 1.82 KB
/
chatai-api.php
File metadata and controls
63 lines (53 loc) · 1.82 KB
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
<?php namespace ProcessWire;
use stdClass;
/*
This endpoint intentionally returns raw JSON; ProcessWire bootstrapping and
headers are handled by index.php.
*/
$chatai = $modules->get('ChatAI');
$res = new stdClass();
if(!$modules->isInstalled('ChatAI')) {
$res->error = (object)['msg' => 'ChatAI is not installed.'];
return json_encode($res);
}
// Reset request: clears PHP session namespace used by ChatAI
if (($input->get->text('action') === 'reset') || ($input->post->text('action') === 'reset')) {
// Clear all session vars stored via $session->setFor('chatai', ...)
if (method_exists($session, 'removeAllFor')) {
$session->removeAllFor('chatai');
} else {
// Fallback: remove common keys (keep this list aligned with your module)
foreach ([
'count',
'blacklist_strikes',
'system_prompt',
'history',
'chat_id',
'ip',
] as $k) {
if (method_exists($session, 'removeFor')) {
$session->removeFor('chatai', $k);
} else {
// last resort: overwrite
$session->setFor('chatai', $k, null);
}
}
}
return json_encode(['ok' => true]);
}
$session->setFor('chatai', 'ip', $session->getIP());
$post = trim(file_get_contents('php://input'));
if(!$post) return json_encode($res);;
$data = json_decode($post);
$userMessage = $sanitizer->text($data->msg ?? '');
if($userMessage === '') {
$res->error = $chatai->getErrorMessage(2);
return json_encode($res);
}
$res = $chatai->sendMessage(
$userMessage,
$sanitizer->int($data->ln ?? null) ?: null,
$sanitizer->int($data->pid ?? null) ?: null,
$sanitizer->url($data->url ?? '')
);
return json_encode($res);