-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrequest.ts
More file actions
112 lines (96 loc) · 3.06 KB
/
request.ts
File metadata and controls
112 lines (96 loc) · 3.06 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
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
import type {
HackbotClientMessage,
HackbotValidatedRequest,
} from '@typeDefs/hackbot';
const MAX_USER_MESSAGE_CHARS = 200;
const MAX_HISTORY_MESSAGES = 30;
const MAX_MESSAGE_CHARS = 2000;
export const MAX_CONTEXT_HISTORY_MESSAGES = 6;
const MAX_TOTAL_MESSAGE_CHARS =
MAX_CONTEXT_HISTORY_MESSAGES * MAX_MESSAGE_CHARS;
const ALLOWED_MESSAGE_ROLES = new Set(['user', 'assistant']);
export function validateRequestBody(
payload: any
): HackbotValidatedRequest | Response {
const { messages, currentPath } = payload ?? {};
if (!Array.isArray(messages) || messages.length === 0) {
return Response.json({ error: 'Invalid request' }, { status: 400 });
}
if (messages.length > MAX_HISTORY_MESSAGES) {
return Response.json(
{
error: `Too many messages. Please keep history under ${MAX_HISTORY_MESSAGES} messages.`,
},
{ status: 400 }
);
}
const sanitizedMessages: HackbotClientMessage[] = [];
let totalChars = 0;
for (const message of messages) {
const role = message?.role;
const content = message?.content;
if (!ALLOWED_MESSAGE_ROLES.has(role) || typeof content !== 'string') {
return Response.json(
{ error: 'Invalid message history format.' },
{ status: 400 }
);
}
const trimmedContent = content.trim();
if (!trimmedContent) {
// Tool-only replies can persist as empty assistant text in localStorage.
// Drop them from request history instead of failing the whole request.
if (role === 'assistant') continue;
return Response.json(
{ error: 'Invalid message history format.' },
{ status: 400 }
);
}
if (content.length > MAX_MESSAGE_CHARS) {
return Response.json(
{
error: `Message too long. Keep each message under ${MAX_MESSAGE_CHARS} characters.`,
},
{ status: 400 }
);
}
totalChars += content.length;
if (totalChars > MAX_TOTAL_MESSAGE_CHARS) {
return Response.json(
{
error: `Message history too large. Keep total content under ${MAX_TOTAL_MESSAGE_CHARS} characters.`,
},
{ status: 400 }
);
}
sanitizedMessages.push({
role: role as 'user' | 'assistant',
content,
});
}
if (sanitizedMessages.length === 0) {
return Response.json({ error: 'Invalid request' }, { status: 400 });
}
const lastMessage = sanitizedMessages[sanitizedMessages.length - 1];
if (lastMessage.role !== 'user') {
return Response.json(
{ error: 'Last message must be from user.' },
{ status: 400 }
);
}
if (lastMessage.content.length > MAX_USER_MESSAGE_CHARS) {
return Response.json(
{
error: `Message too long. Please keep it under ${MAX_USER_MESSAGE_CHARS} characters.`,
},
{ status: 400 }
);
}
return { sanitizedMessages, lastMessage, currentPath };
}
export function isSimpleGreetingMessage(content: string): boolean {
const normalized = content
.trim()
.replace(/[.!?]+$/, '')
.trim();
return /^(hi|hello|hey|thanks|thank you|ok|okay)$/i.test(normalized);
}