@kadaliao/dsh-bots 0.6.1 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +85 -4
- package/README.zh-CN.md +72 -4
- package/client.js +1319 -37
- package/index.js +878 -18
- package/lib/channels.mjs +32 -0
- package/lib/memory.mjs +508 -0
- package/lib/models.mjs +250 -0
- package/lib/prices.mjs +149 -0
- package/lib/settings.mjs +142 -0
- package/lib/store.mjs +4 -1
- package/lib/topics.mjs +103 -0
- package/lib/usage.mjs +267 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +3 -3
package/lib/models.mjs
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a bot's model can come from, beyond the providers Harness already has:
|
|
3
|
+
*
|
|
4
|
+
* - **Custom endpoints** the user adds in the Bots panel (an OpenAI-, Anthropic-
|
|
5
|
+
* or Gemini-compatible base URL, a key and model ids). Each becomes a Harness
|
|
6
|
+
* provider route `bots-<name>` in the `llm-pi-ai` settings, its key stored in
|
|
7
|
+
* the Harness credential store as `BOTS_<NAME>_API_KEY` — so it serves bots
|
|
8
|
+
* (and any other conversation) through Harness's own adapter.
|
|
9
|
+
* - **Logins of other agents on this machine.** The Codex CLI's ChatGPT login
|
|
10
|
+
* (`~/.codex/auth.json`) becomes Harness's `openai-codex` route. OpenAI rotates
|
|
11
|
+
* the refresh token on every refresh, so the two copies are kept in sync both
|
|
12
|
+
* ways: whichever side refreshed, the other gets the new tokens.
|
|
13
|
+
* Claude Code's login is detected but not offered: Anthropic only allows a
|
|
14
|
+
* Claude subscription in Claude Code and claude.ai.
|
|
15
|
+
*
|
|
16
|
+
* Nothing here returns a key or token to the page.
|
|
17
|
+
*
|
|
18
|
+
* @module @kadaliao/dsh-bots/lib/models
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { existsSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
|
|
22
|
+
import { homedir } from 'node:os';
|
|
23
|
+
import { dirname, join } from 'node:path';
|
|
24
|
+
|
|
25
|
+
export const API_KINDS = ['openai-completions', 'openai-responses', 'anthropic-messages', 'google-generative-ai'];
|
|
26
|
+
export const ROUTE_PREFIX = 'bots-';
|
|
27
|
+
export const CODEX_ROUTE = 'openai-codex';
|
|
28
|
+
export const PI_SCOPE = 'llm-pi-ai';
|
|
29
|
+
|
|
30
|
+
/** The Harness route of a custom endpoint named `name`. */
|
|
31
|
+
export function routeFor(name) {
|
|
32
|
+
const text = String(name ?? '').trim();
|
|
33
|
+
const slug = text
|
|
34
|
+
.normalize('NFKD')
|
|
35
|
+
.toLowerCase()
|
|
36
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
37
|
+
.replace(/^-+|-+$/g, '')
|
|
38
|
+
.slice(0, 32);
|
|
39
|
+
// A name with no Latin letters ("本地模型") gets a short stable suffix, so two of them differ.
|
|
40
|
+
if (slug && !/[^\x00-\x7f]/.test(text)) return `${ROUTE_PREFIX}${slug}`;
|
|
41
|
+
let h = 0;
|
|
42
|
+
for (const ch of text) h = (h * 31 + ch.codePointAt(0)) >>> 0;
|
|
43
|
+
return `${ROUTE_PREFIX}${slug ? `${slug.slice(0, 25)}-` : 'custom-'}${h.toString(36).slice(0, 6)}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The credential reference holding a custom route's key. */
|
|
47
|
+
export const keyRefFor = (route) => `BOTS_${route.slice(ROUTE_PREFIX.length).toUpperCase().replace(/[^A-Z0-9]+/g, '_')}_API_KEY`;
|
|
48
|
+
|
|
49
|
+
const cleanUrl = (u) => {
|
|
50
|
+
const s = String(u ?? '').trim().replace(/\/+$/, '');
|
|
51
|
+
if (!/^https?:\/\/[^\s/]+/i.test(s)) throw new Error('the base URL must start with http:// or https://');
|
|
52
|
+
return s;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The `llm-pi-ai` provider profile of a custom endpoint.
|
|
57
|
+
* @param {{name: string, api: string, baseURL: string, models: (string|{id, name?, contextWindow?})[], route?: string}} e
|
|
58
|
+
*/
|
|
59
|
+
export function profileFor(e) {
|
|
60
|
+
const route = e.route || routeFor(e.name);
|
|
61
|
+
if (!route.startsWith(ROUTE_PREFIX)) throw new Error('not a Bots endpoint');
|
|
62
|
+
const api = API_KINDS.includes(e.api) ? e.api : 'openai-completions';
|
|
63
|
+
const models = [];
|
|
64
|
+
for (const m of Array.isArray(e.models) ? e.models : []) {
|
|
65
|
+
const id = String(typeof m === 'string' ? m : m?.id ?? '').trim();
|
|
66
|
+
if (!id || models.some((x) => x.id === id)) continue;
|
|
67
|
+
const cw = Number(typeof m === 'object' ? m.contextWindow : undefined);
|
|
68
|
+
models.push({ id, ...(typeof m === 'object' && m.name ? { name: String(m.name).slice(0, 120) } : {}), ...(Number.isFinite(cw) && cw >= 1024 ? { contextWindow: Math.round(cw) } : {}) });
|
|
69
|
+
}
|
|
70
|
+
if (models.length === 0) throw new Error('add at least one model id');
|
|
71
|
+
return {
|
|
72
|
+
route,
|
|
73
|
+
profile: {
|
|
74
|
+
displayName: String(e.name ?? '').trim().slice(0, 60) || route,
|
|
75
|
+
api,
|
|
76
|
+
baseURL: cleanUrl(e.baseURL),
|
|
77
|
+
apiKeyEnv: keyRefFor(route),
|
|
78
|
+
models: models.slice(0, 100),
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** A custom route's profile as the page sees it. */
|
|
84
|
+
export function endpointView(route, profile, keyConfigured) {
|
|
85
|
+
return {
|
|
86
|
+
route,
|
|
87
|
+
name: profile?.displayName ?? route,
|
|
88
|
+
api: profile?.api ?? 'openai-completions',
|
|
89
|
+
baseURL: profile?.baseURL ?? '',
|
|
90
|
+
models: (profile?.models ?? []).map((m) => (typeof m === 'string' ? { id: m } : { id: m.id, ...(m.name ? { name: m.name } : {}), ...(m.contextWindow ? { contextWindow: m.contextWindow } : {}) })),
|
|
91
|
+
keyConfigured: Boolean(keyConfigured),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Ask an endpoint which models it serves (OpenAI- and Anthropic-style `/models`). */
|
|
96
|
+
export async function discoverModels({ api, baseURL, key, fetchImpl = fetch, signal }) {
|
|
97
|
+
const base = cleanUrl(baseURL);
|
|
98
|
+
let url;
|
|
99
|
+
const headers = { accept: 'application/json' };
|
|
100
|
+
if (api === 'anthropic-messages') {
|
|
101
|
+
url = /\/v1$/.test(base) ? `${base}/models?limit=1000` : `${base}/v1/models?limit=1000`;
|
|
102
|
+
headers['x-api-key'] = key;
|
|
103
|
+
headers['anthropic-version'] = '2023-06-01';
|
|
104
|
+
} else if (api === 'google-generative-ai') {
|
|
105
|
+
url = `${/\/v1(beta)?$/.test(base) ? base : `${base}/v1beta`}/models?pageSize=1000`;
|
|
106
|
+
headers['x-goog-api-key'] = key;
|
|
107
|
+
} else {
|
|
108
|
+
url = `${base}/models`;
|
|
109
|
+
if (key) headers.authorization = `Bearer ${key}`;
|
|
110
|
+
}
|
|
111
|
+
const res = await fetchImpl(url, { headers, signal: signal ?? AbortSignal.timeout(20_000) });
|
|
112
|
+
if (!res.ok) throw new Error(`${res.status} ${res.statusText || ''} from ${url}`.trim());
|
|
113
|
+
const body = await res.json();
|
|
114
|
+
const rows = Array.isArray(body?.data) ? body.data : Array.isArray(body?.models) ? body.models : Array.isArray(body) ? body : [];
|
|
115
|
+
return rows
|
|
116
|
+
.map((m) => {
|
|
117
|
+
const id = String(m?.id ?? m?.name ?? '').replace(/^models\//, '');
|
|
118
|
+
return id ? { id, ...(m?.display_name || m?.displayName ? { name: String(m.display_name ?? m.displayName) } : {}) } : null;
|
|
119
|
+
})
|
|
120
|
+
.filter(Boolean)
|
|
121
|
+
.sort((a, b) => a.id.localeCompare(b.id));
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// --- logins of other agents ----------------------------------------------------------------
|
|
125
|
+
|
|
126
|
+
function jwtClaims(token) {
|
|
127
|
+
try {
|
|
128
|
+
const part = String(token).split('.')[1];
|
|
129
|
+
return JSON.parse(Buffer.from(part.replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString('utf8'));
|
|
130
|
+
} catch {
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export const codexAuthFile = (home = homedir()) => join(process.env.CODEX_HOME || join(home, '.codex'), 'auth.json');
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The Codex CLI's login: `{ok, email, plan, accountId, access, refresh, expires}`,
|
|
139
|
+
* or `{ok: false, reason}` ('missing' | 'api-key' | 'unreadable').
|
|
140
|
+
*/
|
|
141
|
+
export function readCodexAuth(file = codexAuthFile()) {
|
|
142
|
+
if (!existsSync(file)) return { ok: false, reason: 'missing' };
|
|
143
|
+
let raw;
|
|
144
|
+
try {
|
|
145
|
+
raw = JSON.parse(readFileSync(file, 'utf8'));
|
|
146
|
+
} catch {
|
|
147
|
+
return { ok: false, reason: 'unreadable' };
|
|
148
|
+
}
|
|
149
|
+
const t = raw?.tokens;
|
|
150
|
+
if (!t?.access_token || !t?.refresh_token) return { ok: false, reason: raw?.OPENAI_API_KEY ? 'api-key' : 'unreadable' };
|
|
151
|
+
const claims = jwtClaims(t.access_token) ?? {};
|
|
152
|
+
const id = jwtClaims(t.id_token) ?? {};
|
|
153
|
+
const auth = claims['https://api.openai.com/auth'] ?? id['https://api.openai.com/auth'] ?? {};
|
|
154
|
+
return {
|
|
155
|
+
ok: true,
|
|
156
|
+
email: typeof id.email === 'string' ? id.email : (claims['https://api.openai.com/profile']?.email ?? ''),
|
|
157
|
+
plan: String(auth.chatgpt_plan_type ?? ''),
|
|
158
|
+
accountId: String(t.account_id || auth.chatgpt_account_id || ''),
|
|
159
|
+
access: t.access_token,
|
|
160
|
+
refresh: t.refresh_token,
|
|
161
|
+
expires: Number(claims.exp) > 0 ? Number(claims.exp) * 1000 : Date.now() + 3600_000,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** The pi-ai credential (the Harness grant record's payload) of a Codex login. */
|
|
166
|
+
export const codexCredential = (a) => ({ type: 'oauth', access: a.access, refresh: a.refresh, expires: a.expires, accountId: a.accountId });
|
|
167
|
+
|
|
168
|
+
/** Write refreshed tokens back into the Codex CLI's auth.json, keeping everything else it holds. */
|
|
169
|
+
export function writeCodexTokens(file, cred) {
|
|
170
|
+
const raw = JSON.parse(readFileSync(file, 'utf8'));
|
|
171
|
+
raw.tokens = { ...(raw.tokens ?? {}), access_token: cred.access, refresh_token: cred.refresh, ...(cred.accountId ? { account_id: cred.accountId } : {}) };
|
|
172
|
+
raw.last_refresh = new Date().toISOString();
|
|
173
|
+
let mode = 0o600;
|
|
174
|
+
try {
|
|
175
|
+
mode = statSync(file).mode & 0o777;
|
|
176
|
+
} catch {
|
|
177
|
+
// Default.
|
|
178
|
+
}
|
|
179
|
+
const tmp = join(dirname(file), `.auth.json.dsh-bots-${process.pid}`);
|
|
180
|
+
writeFileSync(tmp, JSON.stringify(raw, null, 2), { encoding: 'utf8', mode });
|
|
181
|
+
renameSync(tmp, file);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Keeps the Codex CLI's login and Harness's copy of it the same.
|
|
186
|
+
* @param {{file?: string, readRecord: () => Promise<object|undefined>, writeRecord: (cred: object) => Promise<void>, log?: (line: string) => void}} io
|
|
187
|
+
*/
|
|
188
|
+
export function createCodexSync(io) {
|
|
189
|
+
const file = io.file ?? codexAuthFile();
|
|
190
|
+
let last = '';
|
|
191
|
+
return {
|
|
192
|
+
/** One comparison: 'same' | 'to-harness' | 'to-codex' | 'missing' | 'conflict'. */
|
|
193
|
+
async syncOnce() {
|
|
194
|
+
const a = readCodexAuth(file);
|
|
195
|
+
if (!a.ok) return 'missing';
|
|
196
|
+
const rec = await io.readRecord();
|
|
197
|
+
const fromCodex = async () => {
|
|
198
|
+
await io.writeRecord(codexCredential(a));
|
|
199
|
+
last = a.refresh;
|
|
200
|
+
return 'to-harness';
|
|
201
|
+
};
|
|
202
|
+
if (!rec || rec.type !== 'oauth' || !rec.refresh) return fromCodex();
|
|
203
|
+
// Two different ChatGPT accounts: neither side's tokens may replace the other's.
|
|
204
|
+
if (rec.accountId && a.accountId && rec.accountId !== a.accountId) {
|
|
205
|
+
io.log?.('codex login: Harness and the Codex CLI are signed in to different ChatGPT accounts; not syncing');
|
|
206
|
+
return 'conflict';
|
|
207
|
+
}
|
|
208
|
+
if (rec.refresh === a.refresh) {
|
|
209
|
+
last = a.refresh;
|
|
210
|
+
if (rec.access !== a.access && a.expires > (rec.expires ?? 0)) {
|
|
211
|
+
await io.writeRecord({ ...rec, access: a.access, expires: a.expires });
|
|
212
|
+
return 'to-harness';
|
|
213
|
+
}
|
|
214
|
+
return 'same';
|
|
215
|
+
}
|
|
216
|
+
// One side refreshed since the last look: the side that still holds the old token is behind.
|
|
217
|
+
const harnessMoved = last !== '' && a.refresh === last;
|
|
218
|
+
const codexMoved = last !== '' && rec.refresh === last;
|
|
219
|
+
if (codexMoved || (!harnessMoved && a.expires >= (rec.expires ?? 0))) return fromCodex();
|
|
220
|
+
writeCodexTokens(file, rec);
|
|
221
|
+
last = rec.refresh;
|
|
222
|
+
io.log?.('codex login: wrote the tokens Harness refreshed back to the Codex CLI');
|
|
223
|
+
return 'to-codex';
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Other agents' logins found on this machine (no secrets): what the panel offers. */
|
|
229
|
+
export function detectLogins(home = homedir()) {
|
|
230
|
+
const codex = readCodexAuth(codexAuthFile(home));
|
|
231
|
+
const out = [
|
|
232
|
+
{
|
|
233
|
+
id: 'codex',
|
|
234
|
+
name: 'Codex CLI',
|
|
235
|
+
product: 'ChatGPT',
|
|
236
|
+
found: codex.ok,
|
|
237
|
+
...(codex.ok ? { account: codex.email, plan: codex.plan } : {}),
|
|
238
|
+
...(!codex.ok && codex.reason !== 'missing' ? { note: codex.reason } : {}),
|
|
239
|
+
supported: true,
|
|
240
|
+
route: CODEX_ROUTE,
|
|
241
|
+
},
|
|
242
|
+
];
|
|
243
|
+
const claude = existsSync(join(home, '.claude', '.credentials.json')) || existsSync(join(home, '.claude.json'));
|
|
244
|
+
out.push({ id: 'claude-code', name: 'Claude Code', product: 'Claude', found: claude, supported: false, reason: 'terms' });
|
|
245
|
+
const gemini = existsSync(join(home, '.gemini', 'oauth_creds.json'));
|
|
246
|
+
if (gemini) out.push({ id: 'gemini-cli', name: 'Gemini CLI', product: 'Gemini', found: true, supported: false, reason: 'harness' });
|
|
247
|
+
const copilot = ['apps.json', 'hosts.json'].some((f) => existsSync(join(home, '.config', 'github-copilot', f)));
|
|
248
|
+
if (copilot) out.push({ id: 'copilot', name: 'GitHub Copilot', product: 'Copilot', found: true, supported: false, reason: 'signin' });
|
|
249
|
+
return out;
|
|
250
|
+
}
|
package/lib/prices.mjs
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a model call cost: tokens × a price per million tokens. Prices come from
|
|
3
|
+
* the user's own table first (settings `prices`), then LiteLLM's public
|
|
4
|
+
* `model_prices_and_context_window.json` (chat rows only, compacted and cached
|
|
5
|
+
* under the bots cache). Costs are worked out when a report is read, never
|
|
6
|
+
* stored, so a corrected price applies to past usage too.
|
|
7
|
+
*
|
|
8
|
+
* @module @kadaliao/dsh-bots/lib/prices
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
|
|
14
|
+
export const PRICE_SOURCES = [
|
|
15
|
+
'https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json',
|
|
16
|
+
'https://cdn.jsdelivr.net/gh/BerriAI/litellm@main/model_prices_and_context_window.json',
|
|
17
|
+
];
|
|
18
|
+
const MAX_AGE_MS = 7 * 24 * 3600_000;
|
|
19
|
+
const RETRY_MS = 10 * 60_000;
|
|
20
|
+
const TEXT_MODES = new Set(['chat', 'completion', 'responses']);
|
|
21
|
+
/** A model sold by several resellers: its own vendor's row wins. */
|
|
22
|
+
const VENDORS = ['openai', 'anthropic', 'deepseek', 'gemini', 'xai', 'mistral', 'moonshot', 'dashscope', 'zai', 'minimax', 'vertex_ai-language-models'];
|
|
23
|
+
|
|
24
|
+
const perMillion = (v) => {
|
|
25
|
+
const n = typeof v === 'string' ? Number.parseFloat(v) : Number(v);
|
|
26
|
+
return Number.isFinite(n) && n >= 0 ? Number((n * 1e6).toPrecision(10)) : undefined;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The text-model rows of a LiteLLM price file: `{prices: {name: price}}`, where
|
|
31
|
+
* name is the row key lowercased, plus each row's bare model name (after the
|
|
32
|
+
* last `/`) when no vendor row claims it first.
|
|
33
|
+
*/
|
|
34
|
+
export function compactPrices(raw) {
|
|
35
|
+
const prices = {};
|
|
36
|
+
const bare = {};
|
|
37
|
+
if (!raw || typeof raw !== 'object') return { prices };
|
|
38
|
+
for (const [key, row] of Object.entries(raw)) {
|
|
39
|
+
if (!row || typeof row !== 'object' || key === 'sample_spec') continue;
|
|
40
|
+
if (row.mode && !TEXT_MODES.has(row.mode)) continue;
|
|
41
|
+
const input = perMillion(row.input_cost_per_token);
|
|
42
|
+
const output = perMillion(row.output_cost_per_token);
|
|
43
|
+
if (input === undefined || output === undefined || input + output === 0) continue;
|
|
44
|
+
const cacheRead = perMillion(row.cache_read_input_token_cost);
|
|
45
|
+
const cacheWrite = perMillion(row.cache_creation_input_token_cost);
|
|
46
|
+
const price = { input, output, ...(cacheRead !== undefined ? { cacheRead } : {}), ...(cacheWrite !== undefined ? { cacheWrite } : {}), currency: 'USD' };
|
|
47
|
+
const name = key.toLowerCase();
|
|
48
|
+
prices[name] = price;
|
|
49
|
+
const short = name.split('/').pop();
|
|
50
|
+
if (short !== name) {
|
|
51
|
+
const rank = VENDORS.indexOf(String(row.litellm_provider ?? ''));
|
|
52
|
+
const prev = bare[short];
|
|
53
|
+
if (!prev || (rank !== -1 && (prev.rank === -1 || rank < prev.rank))) bare[short] = { rank, price };
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
for (const [short, { price }] of Object.entries(bare)) if (!prices[short]) prices[short] = price;
|
|
57
|
+
return { prices };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The price of `model`: the user's table, then the catalog, by the full id,
|
|
62
|
+
* every `/`-suffix of it, and finally the longest catalog name the bare id
|
|
63
|
+
* starts with (`deepseek-chat-0324` → `deepseek-chat`, marked approximate).
|
|
64
|
+
* @returns {{input, output, cacheRead?, cacheWrite?, currency, source: 'user'|'catalog', approximate?: true}|null}
|
|
65
|
+
*/
|
|
66
|
+
export function priceOf(model, overrides = {}, catalog = null) {
|
|
67
|
+
const id = String(model ?? '').trim().toLowerCase();
|
|
68
|
+
if (!id) return null;
|
|
69
|
+
const segs = id.split('/');
|
|
70
|
+
const candidates = segs.map((_, i) => segs.slice(i).join('/'));
|
|
71
|
+
for (const c of candidates) if (overrides[c]) return { ...overrides[c], source: 'user' };
|
|
72
|
+
const table = catalog?.prices;
|
|
73
|
+
if (!table) return null;
|
|
74
|
+
for (const c of candidates) if (table[c]) return { ...table[c], source: 'catalog' };
|
|
75
|
+
const last = segs[segs.length - 1];
|
|
76
|
+
const hit = Object.keys(table)
|
|
77
|
+
.filter((n) => !n.includes('/') && last.startsWith(`${n}-`))
|
|
78
|
+
.sort((a, b) => b.length - a.length)[0];
|
|
79
|
+
return hit ? { ...table[hit], source: 'catalog', approximate: true } : null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Cost of one usage row under `price`: {amount, currency} or null. */
|
|
83
|
+
export function costOf(price, u) {
|
|
84
|
+
if (!price) return null;
|
|
85
|
+
const amount = ((u.i ?? 0) * price.input + (u.cr ?? 0) * (price.cacheRead ?? price.input) + (u.cw ?? 0) * (price.cacheWrite ?? price.input) + (u.o ?? 0) * price.output) / 1e6;
|
|
86
|
+
return { amount, currency: price.currency ?? 'USD' };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The LiteLLM price list as kept under `dir`: cached, refreshed weekly in the background. */
|
|
90
|
+
export function createPriceCatalog(dir, options = {}) {
|
|
91
|
+
const file = join(dir, 'price-catalog.json');
|
|
92
|
+
const sources = options.sources?.length ? options.sources : PRICE_SOURCES;
|
|
93
|
+
const now = options.now ?? Date.now;
|
|
94
|
+
let data = null;
|
|
95
|
+
try {
|
|
96
|
+
const cached = JSON.parse(readFileSync(file, 'utf8'));
|
|
97
|
+
if (cached && cached.prices) data = cached;
|
|
98
|
+
} catch {
|
|
99
|
+
data = null;
|
|
100
|
+
}
|
|
101
|
+
let pending = null;
|
|
102
|
+
let lastFailure = 0;
|
|
103
|
+
let lastError = '';
|
|
104
|
+
async function download() {
|
|
105
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
106
|
+
let reason = 'no source';
|
|
107
|
+
for (const url of sources) {
|
|
108
|
+
try {
|
|
109
|
+
const res = await fetchImpl(url, { signal: AbortSignal.timeout(30_000) });
|
|
110
|
+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
|
111
|
+
const compact = compactPrices(JSON.parse(await res.text()));
|
|
112
|
+
if (Object.keys(compact.prices).length === 0) throw new Error('no text models in it');
|
|
113
|
+
data = { source: url, fetchedAt: now(), ...compact };
|
|
114
|
+
lastError = '';
|
|
115
|
+
try {
|
|
116
|
+
mkdirSync(dir, { recursive: true });
|
|
117
|
+
writeFileSync(file, JSON.stringify(data), 'utf8');
|
|
118
|
+
} catch {
|
|
119
|
+
// Kept in memory.
|
|
120
|
+
}
|
|
121
|
+
return data;
|
|
122
|
+
} catch (error) {
|
|
123
|
+
reason = `${url}: ${error?.message ?? error}`;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
lastError = reason;
|
|
127
|
+
lastFailure = now();
|
|
128
|
+
return data;
|
|
129
|
+
}
|
|
130
|
+
function refresh(force = false) {
|
|
131
|
+
if (options.enabled === false) return Promise.resolve(data);
|
|
132
|
+
if (pending) return pending;
|
|
133
|
+
if (!force && lastFailure && now() - lastFailure < RETRY_MS) return Promise.resolve(data);
|
|
134
|
+
pending = download().finally(() => {
|
|
135
|
+
pending = null;
|
|
136
|
+
});
|
|
137
|
+
return pending;
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
current: () => data,
|
|
141
|
+
refresh,
|
|
142
|
+
/** The list now; a missing or stale one starts a download in the background. */
|
|
143
|
+
peek() {
|
|
144
|
+
if (!data || now() - data.fetchedAt > MAX_AGE_MS) void refresh();
|
|
145
|
+
return data;
|
|
146
|
+
},
|
|
147
|
+
status: () => ({ name: 'LiteLLM', count: data ? Object.keys(data.prices).length : 0, fetchedAt: data?.fetchedAt ?? 0, loading: pending !== null, error: lastError }),
|
|
148
|
+
};
|
|
149
|
+
}
|
package/lib/settings.mjs
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Team-wide settings of the bots plugin, kept in `settings.json` beside the
|
|
3
|
+
* roster: when a bot starts a new topic by itself, how memory is written and
|
|
4
|
+
* consolidated, and the prices the usage view counts with.
|
|
5
|
+
*
|
|
6
|
+
* @module @kadaliao/dsh-bots/lib/settings
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
|
|
10
|
+
import { join } from 'node:path';
|
|
11
|
+
|
|
12
|
+
export const DEFAULTS = Object.freeze({
|
|
13
|
+
topics: Object.freeze({
|
|
14
|
+
/** A conversation quiet for this many hours starts a new topic before its next task; 0 = never. */
|
|
15
|
+
idleHours: 6,
|
|
16
|
+
/** A conversation whose prompt reached this many thousand tokens starts a new topic; 0 = never. */
|
|
17
|
+
maxContextK: 128,
|
|
18
|
+
}),
|
|
19
|
+
memory: Object.freeze({
|
|
20
|
+
/** Summarize every finished task into the bot's memory. */
|
|
21
|
+
auto: true,
|
|
22
|
+
/** The model memory work runs on; null = each bot's own model. */
|
|
23
|
+
model: null,
|
|
24
|
+
/** Consolidate memories once a day ("dreaming"). */
|
|
25
|
+
dream: true,
|
|
26
|
+
/** Local time of the daily consolidation, HH:MM. */
|
|
27
|
+
dreamTime: '03:00',
|
|
28
|
+
}),
|
|
29
|
+
/** Prices per million tokens by model id (lowercase): {input, output, cacheRead?, cacheWrite?, currency}. */
|
|
30
|
+
prices: Object.freeze({}),
|
|
31
|
+
/** Keep a ChatGPT login borrowed from the Codex CLI in sync with ~/.codex/auth.json. */
|
|
32
|
+
codex: Object.freeze({ sync: false, route: false }),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const num = (v, d, min, max) => {
|
|
36
|
+
const n = Number(v);
|
|
37
|
+
return Number.isFinite(n) ? Math.min(max, Math.max(min, n)) : d;
|
|
38
|
+
};
|
|
39
|
+
const clock = (v, d) => {
|
|
40
|
+
const m = /^(\d{1,2}):(\d{2})$/.exec(String(v ?? '').trim());
|
|
41
|
+
return m && Number(m[1]) < 24 && Number(m[2]) < 60 ? `${m[1].padStart(2, '0')}:${m[2]}` : d;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** One price row, or null when it names no usable price. */
|
|
45
|
+
export function normalizePrice(p) {
|
|
46
|
+
if (!p || typeof p !== 'object') return null;
|
|
47
|
+
const n = (v) => {
|
|
48
|
+
const x = Number(v);
|
|
49
|
+
return Number.isFinite(x) && x >= 0 ? Number(x.toPrecision(10)) : undefined;
|
|
50
|
+
};
|
|
51
|
+
const input = n(p.input);
|
|
52
|
+
const output = n(p.output);
|
|
53
|
+
if (input === undefined || output === undefined) return null;
|
|
54
|
+
const cacheRead = n(p.cacheRead);
|
|
55
|
+
const cacheWrite = n(p.cacheWrite);
|
|
56
|
+
return {
|
|
57
|
+
input,
|
|
58
|
+
output,
|
|
59
|
+
...(cacheRead !== undefined ? { cacheRead } : {}),
|
|
60
|
+
...(cacheWrite !== undefined ? { cacheWrite } : {}),
|
|
61
|
+
currency: p.currency === 'CNY' ? 'CNY' : 'USD',
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Every field present, values clamped. */
|
|
66
|
+
export function normalizeSettings(input) {
|
|
67
|
+
const s = input && typeof input === 'object' ? input : {};
|
|
68
|
+
const t = s.topics ?? {};
|
|
69
|
+
const m = s.memory ?? {};
|
|
70
|
+
const prices = {};
|
|
71
|
+
if (s.prices && typeof s.prices === 'object') {
|
|
72
|
+
for (const [k, v] of Object.entries(s.prices).slice(0, 300)) {
|
|
73
|
+
const key = String(k).trim().toLowerCase().slice(0, 200);
|
|
74
|
+
const p = normalizePrice(v);
|
|
75
|
+
if (key && p) prices[key] = p;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const model = m.model && typeof m.model === 'object' && typeof m.model.provider === 'string' && m.model.provider && typeof m.model.model === 'string' && m.model.model ? { provider: m.model.provider.slice(0, 200), model: m.model.model.slice(0, 200) } : null;
|
|
79
|
+
return {
|
|
80
|
+
topics: {
|
|
81
|
+
idleHours: num(t.idleHours, DEFAULTS.topics.idleHours, 0, 24 * 30),
|
|
82
|
+
maxContextK: Math.round(num(t.maxContextK, DEFAULTS.topics.maxContextK, 0, 4096)),
|
|
83
|
+
},
|
|
84
|
+
memory: {
|
|
85
|
+
auto: typeof m.auto === 'boolean' ? m.auto : DEFAULTS.memory.auto,
|
|
86
|
+
model,
|
|
87
|
+
dream: typeof m.dream === 'boolean' ? m.dream : DEFAULTS.memory.dream,
|
|
88
|
+
dreamTime: clock(m.dreamTime, DEFAULTS.memory.dreamTime),
|
|
89
|
+
},
|
|
90
|
+
prices,
|
|
91
|
+
codex: { sync: s.codex?.sync === true, route: s.codex?.route === true },
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Deep-merge a patch into settings (objects merge, everything else replaces). */
|
|
96
|
+
function merge(base, patch) {
|
|
97
|
+
if (!patch || typeof patch !== 'object' || Array.isArray(patch)) return patch === undefined ? base : patch;
|
|
98
|
+
const out = { ...(base && typeof base === 'object' ? base : {}) };
|
|
99
|
+
for (const [k, v] of Object.entries(patch)) out[k] = k === 'prices' ? v : merge(out[k], v);
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The settings file under `dir`, re-read when another process changed it. */
|
|
104
|
+
export function createSettings(dir) {
|
|
105
|
+
const file = join(dir, 'settings.json');
|
|
106
|
+
let seen = -2;
|
|
107
|
+
let value = normalizeSettings({});
|
|
108
|
+
const mtime = () => {
|
|
109
|
+
try {
|
|
110
|
+
return statSync(file).mtimeMs;
|
|
111
|
+
} catch {
|
|
112
|
+
return -1;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const reload = () => {
|
|
116
|
+
const now = mtime();
|
|
117
|
+
if (now === seen) return;
|
|
118
|
+
seen = now;
|
|
119
|
+
try {
|
|
120
|
+
value = normalizeSettings(JSON.parse(readFileSync(file, 'utf8')));
|
|
121
|
+
} catch {
|
|
122
|
+
value = normalizeSettings({});
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
return {
|
|
126
|
+
get() {
|
|
127
|
+
reload();
|
|
128
|
+
return value;
|
|
129
|
+
},
|
|
130
|
+
/** Merge `patch` (prices replace as a whole) and save; returns the new settings. */
|
|
131
|
+
save(patch) {
|
|
132
|
+
reload();
|
|
133
|
+
value = normalizeSettings(merge(value, patch));
|
|
134
|
+
mkdirSync(dir, { recursive: true });
|
|
135
|
+
const tmp = `${file}.tmp`;
|
|
136
|
+
writeFileSync(tmp, JSON.stringify(value, null, 2), 'utf8');
|
|
137
|
+
renameSync(tmp, file);
|
|
138
|
+
seen = mtime();
|
|
139
|
+
return value;
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
package/lib/store.mjs
CHANGED
|
@@ -196,7 +196,10 @@ export function normalizeBot(input, prev, seed = 0) {
|
|
|
196
196
|
const skills = capSkillsOn(dedupeSkills((Array.isArray(b.skills) ? b.skills : []).map(normalizeSkill).filter(Boolean)).slice(0, MAX_SKILLS), prev?.skills);
|
|
197
197
|
const apps = (Array.isArray(b.apps) ? b.apps : []).map(normalizeApp).filter(Boolean).slice(0, 20);
|
|
198
198
|
const deny = Array.isArray(b.tools?.deny) ? [...new Set(b.tools.deny.map(String).filter(Boolean))].slice(0, 200) : [];
|
|
199
|
-
const model =
|
|
199
|
+
const model =
|
|
200
|
+
b.model && typeof b.model === 'object' && str(b.model.provider) && str(b.model.model)
|
|
201
|
+
? { provider: str(b.model.provider, 200), model: str(b.model.model, 200), ...(/^[a-z0-9_-]{1,32}$/.test(str(b.model.reasoningEffort)) ? { reasoningEffort: b.model.reasoningEffort } : {}) }
|
|
202
|
+
: null;
|
|
200
203
|
return {
|
|
201
204
|
id: prev?.id ?? (str(b.id, 40) || newId('bot', now)),
|
|
202
205
|
name,
|
package/lib/topics.mjs
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Topics: a bot's conversation is one Harness Session at a time. Starting a
|
|
3
|
+
* new topic gives the bot a fresh Session (its schedules move along, the old
|
|
4
|
+
* one is archived) so the context does not grow without end; memory carries
|
|
5
|
+
* what matters across. `topics.json` keeps, per bot, the current topic's
|
|
6
|
+
* activity and a short history of the past ones.
|
|
7
|
+
*
|
|
8
|
+
* @module @kadaliao/dsh-bots/lib/topics
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
|
|
14
|
+
const KEEP_HISTORY = 50;
|
|
15
|
+
/** A big conversation is only cut once it has been quiet this long (a teammate's answer may still be on its way). */
|
|
16
|
+
export const PRESSURE_QUIET_MS = 30 * 60_000;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Why the current topic of a bot should end now, or ''.
|
|
20
|
+
* @param {{tasks: number, lastActiveAt: number, lastPrompt: number}|undefined} topic
|
|
21
|
+
* @param {{idleHours: number, maxContextK: number}} rules
|
|
22
|
+
* @param {{now?: number, trigger?: 'user'|'dm'|'sweep'}} [at] - `dm`: a teammate's message (never cut for size: it may answer this bot's own question).
|
|
23
|
+
*/
|
|
24
|
+
export function rotationReason(topic, rules, { now = Date.now(), trigger = 'sweep' } = {}) {
|
|
25
|
+
if (!topic || !(topic.tasks > 0)) return '';
|
|
26
|
+
const quiet = now - (topic.lastActiveAt || now);
|
|
27
|
+
if (rules.idleHours > 0 && quiet >= rules.idleHours * 3_600_000) return 'idle';
|
|
28
|
+
if (rules.maxContextK > 0 && topic.lastPrompt >= rules.maxContextK * 1000) {
|
|
29
|
+
if (trigger === 'user') return 'context';
|
|
30
|
+
if (trigger === 'sweep' && quiet >= PRESSURE_QUIET_MS) return 'context';
|
|
31
|
+
}
|
|
32
|
+
return '';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The topics file under `dir`. */
|
|
36
|
+
export function createTopics(dir) {
|
|
37
|
+
const file = join(dir, 'topics.json');
|
|
38
|
+
let data = null;
|
|
39
|
+
const load = () => {
|
|
40
|
+
if (data) return data;
|
|
41
|
+
try {
|
|
42
|
+
const raw = JSON.parse(readFileSync(file, 'utf8'));
|
|
43
|
+
data = raw && typeof raw === 'object' && raw.bots ? raw : { bots: {} };
|
|
44
|
+
} catch {
|
|
45
|
+
data = { bots: {} };
|
|
46
|
+
}
|
|
47
|
+
return data;
|
|
48
|
+
};
|
|
49
|
+
const save = () => {
|
|
50
|
+
try {
|
|
51
|
+
mkdirSync(dir, { recursive: true });
|
|
52
|
+
writeFileSync(`${file}.tmp`, JSON.stringify(data), 'utf8');
|
|
53
|
+
renameSync(`${file}.tmp`, file);
|
|
54
|
+
} catch {
|
|
55
|
+
// Kept in memory.
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const rec = (botId) => (load().bots[botId] ??= { current: null, history: [] });
|
|
59
|
+
return {
|
|
60
|
+
/** The current topic of a bot, made when its Session is new to us. */
|
|
61
|
+
current(botId, sessionId) {
|
|
62
|
+
const r = rec(botId);
|
|
63
|
+
if (sessionId && r.current?.sessionId !== sessionId) {
|
|
64
|
+
r.current = { sessionId, startedAt: Date.now(), lastActiveAt: Date.now(), lastPrompt: 0, tasks: 0, titles: [] };
|
|
65
|
+
save();
|
|
66
|
+
}
|
|
67
|
+
return r.current;
|
|
68
|
+
},
|
|
69
|
+
peek(botId) {
|
|
70
|
+
return load().bots[botId]?.current ?? null;
|
|
71
|
+
},
|
|
72
|
+
/** A model call read `prompt` tokens. */
|
|
73
|
+
noteCall(botId, sessionId, prompt) {
|
|
74
|
+
const t = this.current(botId, sessionId);
|
|
75
|
+
t.lastPrompt = prompt;
|
|
76
|
+
t.lastActiveAt = Date.now();
|
|
77
|
+
},
|
|
78
|
+
/** A task finished in the topic. */
|
|
79
|
+
noteTask(botId, sessionId, title) {
|
|
80
|
+
const t = this.current(botId, sessionId);
|
|
81
|
+
t.tasks += 1;
|
|
82
|
+
t.lastActiveAt = Date.now();
|
|
83
|
+
if (title) t.titles = [...(t.titles ?? []), String(title).slice(0, 80)].slice(-5);
|
|
84
|
+
save();
|
|
85
|
+
},
|
|
86
|
+
/** The topic of `botId` ended for `reason`; the next one is `sessionId`. */
|
|
87
|
+
rotate(botId, sessionId, reason) {
|
|
88
|
+
const r = rec(botId);
|
|
89
|
+
if (r.current) r.history = [...r.history, { ...r.current, endedAt: Date.now(), reason }].slice(-KEEP_HISTORY);
|
|
90
|
+
r.current = sessionId ? { sessionId, startedAt: Date.now(), lastActiveAt: Date.now(), lastPrompt: 0, tasks: 0, titles: [] } : null;
|
|
91
|
+
save();
|
|
92
|
+
return r.current;
|
|
93
|
+
},
|
|
94
|
+
history(botId) {
|
|
95
|
+
return (load().bots[botId]?.history ?? []).slice().reverse();
|
|
96
|
+
},
|
|
97
|
+
drop(botId) {
|
|
98
|
+
delete load().bots[botId];
|
|
99
|
+
save();
|
|
100
|
+
},
|
|
101
|
+
flush: save,
|
|
102
|
+
};
|
|
103
|
+
}
|