-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadMenuModel.js
More file actions
56 lines (47 loc) · 1.78 KB
/
threadMenuModel.js
File metadata and controls
56 lines (47 loc) · 1.78 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
const CodexPinThreadMenuModelFactory = () => {
function normalizeText(value, fallback = '') {
return typeof value === 'string' && value.trim() ? value.trim() : fallback;
}
function buildThreadMenuSignature(sessions = []) {
if (!Array.isArray(sessions) || sessions.length === 0) {
return '';
}
return sessions
.map((session) => {
const sessionId = normalizeText(session?.sessionId);
const title = normalizeText(session?.title, 'Untitled thread');
const isActive = session?.isActive ? '1' : '0';
return `${sessionId}|${title}|${isActive}`;
})
.join('||');
}
function getThreadMenuLabel(options = {}) {
const sessions = Array.isArray(options.sessions) ? options.sessions : [];
const selectedSessionId = normalizeText(options.selectedSessionId);
const fallbackLabel = normalizeText(options.fallbackLabel, '待命中');
if (!selectedSessionId) {
return fallbackLabel;
}
const selectedSession = sessions.find((session) => session?.sessionId === selectedSessionId);
return normalizeText(selectedSession?.title, fallbackLabel);
}
function findSessionById(sessions = [], sessionId) {
const normalizedSessionId = normalizeText(sessionId);
if (!normalizedSessionId || !Array.isArray(sessions)) {
return null;
}
return sessions.find((session) => normalizeText(session?.sessionId) === normalizedSessionId) || null;
}
return {
buildThreadMenuSignature,
findSessionById,
getThreadMenuLabel,
};
};
(function attachThreadMenuModel(root, factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root.CodexPinThreadMenuModel = factory();
}
})(typeof self !== 'undefined' ? self : this, CodexPinThreadMenuModelFactory);