-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathcustomConnectionProfiles.js
More file actions
63 lines (58 loc) · 2.3 KB
/
Copy pathcustomConnectionProfiles.js
File metadata and controls
63 lines (58 loc) · 2.3 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
// Copyright (C) 2024–2026 Aiko Hanasaki
// SPDX-License-Identifier: AGPL-3.0-only
/**
* Return whether a SillyTavern Connection Manager profile targets the Custom
* Chat Completion provider.
*
* @param {Object} profile
* @param {Record<string, { selected?: string, source?: string }>} connectApiMap
* @returns {boolean}
*/
export function isCustomConnectionProfile(profile, connectApiMap = {}) {
if (!profile || typeof profile !== 'object') return false;
const api = String(profile.api || '').trim();
const mapping = connectApiMap?.[api];
if (mapping) {
return mapping.selected === 'openai' && mapping.source === 'custom';
}
return profile.mode === 'cc' && api === 'custom';
}
/**
* List Custom Chat Completion profiles from SillyTavern Connection Manager.
*
* @param {Object[]} profiles
* @param {Record<string, { selected?: string, source?: string }>} connectApiMap
* @returns {Object[]}
*/
export function listCustomConnectionProfiles(profiles, connectApiMap = {}) {
if (!Array.isArray(profiles)) return [];
return profiles
.filter(profile => isCustomConnectionProfile(profile, connectApiMap))
.filter(profile => String(profile.id || '').trim())
.sort((a, b) => String(a.name || '').localeCompare(String(b.name || '')));
}
/**
* Resolve the URL and secret reference for a selected Custom connection profile.
* The raw secret is intentionally never exposed to STMB.
*
* @param {Object[]} profiles
* @param {string} profileId
* @param {Record<string, { selected?: string, source?: string }>} connectApiMap
* @returns {{ id: string, name: string, model: string, customUrl: string, secretId?: string } | null}
*/
export function resolveCustomConnectionProfile(profiles, profileId, connectApiMap = {}) {
const id = String(profileId || '').trim();
if (!id) return null;
const profile = listCustomConnectionProfiles(profiles, connectApiMap)
.find(item => String(item.id) === id);
if (!profile) return null;
const customUrl = String(profile['api-url'] || '').trim().replace(/\/+$/, '');
const secretId = String(profile['secret-id'] || '').trim();
return {
id,
name: String(profile.name || id),
model: String(profile.model || '').trim(),
customUrl,
...(secretId ? { secretId } : {}),
};
}