-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
148 lines (135 loc) · 4.15 KB
/
Copy pathserver.js
File metadata and controls
148 lines (135 loc) · 4.15 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
import express from 'express';
import cors from 'cors';
import { getSessionDir } from './aggregator.js';
import {
findMatchingPricing,
} from './pricing.js';
import { listOpenClawPricedModels, listUnpricedModels } from './openclaw-config.js';
import {
getStats,
getPricingConfig,
updatePricingConfig,
refreshStatsCache,
} from './stats-service.js';
const PORT = 3001;
/**
* 为 models.json 的一条模型记录附加 custom 对比字段
*/
function attachCustomRule(row, customMap) {
const key = `${row.provider}/${row.model}`;
const rule = findMatchingPricing(key, customMap);
const custom = rule
? {
input: rule.input,
output: rule.output,
cacheRead: rule.cacheRead ?? null,
cacheWrite: rule.cacheWrite ?? null,
enabled: rule.enabled !== false,
}
: null;
return {
key,
provider: row.provider,
model: row.model,
displayName: row.displayName,
cost: row.cost,
contextWindow: row.contextWindow,
maxTokens: row.maxTokens,
custom,
};
}
export function createApp() {
const app = express();
app.use(cors());
app.use(express.json());
app.get('/api/stats', async (req, res) => {
try {
const data = await getStats();
res.json(data);
} catch (err) {
console.error('Error aggregating stats:', err);
res.status(500).json({ error: err.message });
}
});
app.get('/api/refresh', async (req, res) => {
try {
const result = await refreshStatsCache();
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// GET /api/pricing - 获取当前价格配置
app.get('/api/pricing', async (req, res) => {
try {
const config = await getPricingConfig();
res.json(config);
} catch (err) {
console.error('Error loading pricing config:', err);
res.status(500).json({ error: err.message });
}
});
// PUT /api/pricing - 更新价格配置
app.put('/api/pricing', async (req, res) => {
try {
const result = await updatePricingConfig(req.body);
res.json(result);
} catch (err) {
console.error('Error updating pricing config:', err);
res.status(400).json({ error: err.message });
}
});
// POST /api/pricing/reset - 重置为默认(空)配置
app.post('/api/pricing/reset', async (req, res) => {
try {
const defaultConfig = {
version: '1.0',
enabled: true,
updated: new Date().toISOString(),
pricing: {}
};
const result = await updatePricingConfig(defaultConfig);
res.json(result);
} catch (err) {
console.error('Error resetting pricing config:', err);
res.status(500).json({ error: err.message });
}
});
// GET /api/openclaw/models - models.json 中有/无单价模型 + 与自定义价对照
app.get('/api/openclaw/models', async (req, res) => {
try {
const [priced, pricingConfig, unpriced] = await Promise.all([
listOpenClawPricedModels(),
getPricingConfig(),
listUnpricedModels(),
]);
const customMap = pricingConfig.pricing || {};
const rows = priced.map((row) => attachCustomRule(row, customMap));
const unpricedModels = unpriced.map((row) => attachCustomRule(row, customMap));
res.json({ models: rows, unpricedModels });
} catch (err) {
console.error('Error listing OpenClaw priced models:', err);
res.status(500).json({ error: err.message });
}
});
// GET /api/pricing/models - 列出所有可用的 Provider/Model 组合(走缓存)
app.get('/api/pricing/models', async (req, res) => {
try {
const data = await getStats();
const models = Object.keys(data.byModel);
res.json({ models });
} catch (err) {
console.error('Error fetching models:', err);
res.status(500).json({ error: err.message });
}
});
return app;
}
// Only listen when run directly with `node server.js`
if (import.meta.url === `file://${process.argv[1]}`) {
const app = createApp();
app.listen(PORT, () => {
console.log(`OpenClaw Usage API running at http://localhost:${PORT}`);
console.log(`Scanning sessions from: ${getSessionDir()}`);
});
}