forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
207 lines (187 loc) · 6.48 KB
/
Copy pathutils.js
File metadata and controls
207 lines (187 loc) · 6.48 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
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
/**
* Pure utility functions — no side effects, no env/request dependency.
* Extracted from register-proxy-sw.js for maintainability.
*/
const CORS_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization, MCP-Protocol-Version, Accept, Origin",
"Access-Control-Max-Age": "86400",
};
function jsonResponse(body, status = 200) {
return new Response(JSON.stringify(body), {
status,
headers: { "Content-Type": "application/json", ...CORS_HEADERS },
});
}
function mcpJsonResponse(body, status = 200, extraHeaders = {}) {
return new Response(JSON.stringify(body), {
status,
headers: {
"Content-Type": "application/json",
"MCP-Protocol-Version": "2025-06-18",
...CORS_HEADERS,
...extraHeaders,
},
});
}
function timingSafeEqual(a, b) {
if (a.length !== b.length) return false;
let result = 0;
for (let i = 0; i < a.length; i++) {
result |= a.charCodeAt(i) ^ b.charCodeAt(i);
}
return result === 0;
}
function sanitizeIdentifier(val, maxLen = 50) {
if (!val) return "";
if (val.length > maxLen) val = val.slice(0, maxLen);
return val.replace(/[^\w一-龥\-]/g, "");
}
function parseTimestamp(value) {
const timestamp = Date.parse(String(value || ""));
return Number.isFinite(timestamp) ? timestamp : null;
}
function roundPoints(value) {
return Math.round(value * 100) / 100;
}
const REPUTATION_PERIODS = Object.freeze(Object.create(null, {
"all-time": { value: null, enumerable: true },
monthly: { value: 30, enumerable: true },
weekly: { value: 7, enumerable: true },
}));
function normalizeReputationPeriod(value) {
const period = String(value || "all-time").toLowerCase();
if (period === "all_time" || period === "alltime") return "all-time";
if (period === "month") return "monthly";
if (period === "week") return "weekly";
return period in REPUTATION_PERIODS ? period : null;
}
const RATE_LIMIT_WINDOW = 30_000;
const rateMap = new Map();
function cleanRateMap() {
const now = Date.now();
for (const [key, timestamp] of rateMap) {
if (now - timestamp > RATE_LIMIT_WINDOW) rateMap.delete(key);
}
}
function validateMcpOrigin(request) {
const origin = request.headers.get("Origin") || "";
const MCP_ALLOWED_ORIGINS = [
"https://claude.ai",
"https://cursor.sh",
"https://cursor.com",
"https://openai.com",
"https://chat.openai.com",
"https://chatgpt.com",
"",
];
return MCP_ALLOWED_ORIGINS.includes(origin);
}
// ── Intake kind inference (question vs failure) ──
// Used by misakanet_submit_intake and the search no-match suggestion so that
// how-to / knowledge-gap submissions are routed as `question` instead of being
// treated as malformed failure lessons (see #1396: a PT-BR how-to arrived as
// kind=missing_lesson, got scored 16.9/100 by the lesson auto-review and was
// auto-rejected to badcase). Conservative on purpose: only clear question
// phrasing with NO failure evidence flips the kind.
const INTAKE_KINDS = ["missing_lesson", "stale_lesson", "new_lesson_candidate", "question"];
const QUESTION_HINTS = [
// English
/\bhow (do|can|should|could|would|to|i|we|you|does|did)\b/i,
/\bhow to\b/i,
/\bwhat (is|are|does|should|can|could|would)\b/i,
/\bwhy (does|is|do|are|can|would|did)\b/i,
/\bcan (i|you|we|someone)\b/i,
/\b(is|are) there a (way|better|method)\b/i,
/\btips?\b/i,
/\bguid(e|ance|elines?)\b/i,
/\brecommend\b/i,
/\bhelp (me|with)?\b/i,
// Portuguese
/\bcomo (fazer|resolver|configurar|usar|evitar|sair|sigo|guio|posso|fa[çc]o|devo)\b/i,
/\bpor que\b/i,
/\bpor qu[eê]\b/i,
/\bo que (é|e|fazer|devo|posso)\b/i,
/\bqual (é|e) (a|o|melhor)\b/i,
/\bajuda\b/i,
/\bdicas?\b/i,
/\bconselho\b/i,
/\bmaneira de\b/i,
/\bforma de\b/i,
// Spanish
/\bc[oó]mo (hago|puedo|configuro|resuelvo|evito|salgo|debo)\b/i,
/\bpor qu[ée]\b/i,
/\bqu[ée] (es|hago|puedo|debo)\b/i,
/\bayuda\b/i,
/\bconsejo\b/i,
// Chinese
/怎么|如何|为什么|请问|怎样|该(怎么|如何)|能不能/,
// Generic trailing question mark
/\?\s*$/,
];
// Inline *error evidence* in the problem text — narrow on purpose. Broad
// failure words ("failed", "timeout", "failure") are too topic-y ("how do I
// structure a failure lesson?" is a question, not an error report), while a
// pasted traceback / error code / "Error:" prefix means real failure content
// that must keep the missing_lesson route even if phrased as a question.
const FAILURE_HINTS = [
/\b(traceback|segfault|stack ?trace)\b/i,
/\bexception\b/i,
/\b(enoent|econnrefused|eacces|eperm|econnreset|econnaborted)\b/i,
/(?:^|\n)\s*(?:error|fatal|critical|panic|failed to)[:\s]/i,
/报错|异常|崩溃|堆栈/,
];
function looksLikeQuestion(text) {
return QUESTION_HINTS.some((re) => re.test(String(text || "")));
}
function hasFailureEvidence(text) {
return FAILURE_HINTS.some((re) => re.test(String(text || "")));
}
/**
* Resolve the intake kind for a submission.
*
* - An explicit, valid kind (stale_lesson / new_lesson_candidate / question /
* missing_lesson) is honored as-is, EXCEPT kind="missing_lesson": callers
* are still guided toward it by older copy, so a clear question with zero
* failure evidence is re-routed to "question".
* - An absent kind defaults to "missing_lesson", upgraded to "question" under
* the same rule.
*
* Returns { kind, autoDetected }.
*/
function inferIntakeKind({ kind, problem, error, what_tried, fix, verification } = {}) {
const explicit = String(kind || "").trim();
if (explicit && !INTAKE_KINDS.includes(explicit)) {
return { kind: "missing_lesson", autoDetected: false, invalid: explicit };
}
const problemText = `${problem || ""} ${what_tried || ""}`;
const structuredFailure = Boolean(error || fix || verification);
const isQuestion = looksLikeQuestion(problemText) && !structuredFailure && !hasFailureEvidence(problemText);
if (explicit === "missing_lesson" || !explicit) {
if (isQuestion) return { kind: "question", autoDetected: true };
return { kind: "missing_lesson", autoDetected: false };
}
return { kind: explicit, autoDetected: false };
}
export {
CORS_HEADERS,
jsonResponse,
mcpJsonResponse,
timingSafeEqual,
sanitizeIdentifier,
parseTimestamp,
roundPoints,
REPUTATION_PERIODS,
normalizeReputationPeriod,
RATE_LIMIT_WINDOW,
rateMap,
cleanRateMap,
validateMcpOrigin,
INTAKE_KINDS,
QUESTION_HINTS,
FAILURE_HINTS,
looksLikeQuestion,
hasFailureEvidence,
inferIntakeKind,
};