dsh-ta 0.2.0 → 0.4.0
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/CHANGELOG.md +111 -1
- package/README.md +325 -140
- package/cordis.patch.yml +5 -35
- package/docs/RELEASE.md +63 -46
- package/docs/USAGE.md +25 -9
- package/docs/presets.example.yml +7 -43
- package/index.js +3 -170
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +6 -19
- package/src/client/client.js +3176 -0
- package/{config.js → src/host/config.js} +39 -4
- package/src/host/constants.js +7 -0
- package/src/host/gates.js +34 -0
- package/src/host/identity.js +145 -0
- package/{im → src/host/im}/bindings.js +1 -1
- package/src/host/index.js +173 -0
- package/{jurisdiction.js → src/host/jurisdiction.js} +1 -1
- package/src/host/maintenance/collect.js +362 -0
- package/src/host/maintenance/completions.js +56 -0
- package/src/host/maintenance/consumption.js +67 -0
- package/src/host/maintenance/cursors.js +53 -0
- package/src/host/maintenance/engine.js +392 -0
- package/src/host/maintenance/llm.js +381 -0
- package/src/host/maintenance/lock.js +105 -0
- package/src/host/maintenance/prompts.js +255 -0
- package/src/host/maintenance/runs.js +47 -0
- package/src/host/maintenance/schedule.js +173 -0
- package/src/host/maintenance/session-cleanup.js +289 -0
- package/src/host/maintenance/session-watcher.js +107 -0
- package/src/host/maintenance/sleep.js +3 -0
- package/src/host/maintenance/stages.js +241 -0
- package/src/host/maintenance/state.js +192 -0
- package/src/host/prompts.js +96 -0
- package/src/host/records.js +175 -0
- package/{reme.js → src/host/reme.js} +3 -1
- package/src/host/runtime-manager.js +281 -0
- package/{runtime.js → src/host/runtime.js} +28 -50
- package/src/host/settings/defaults.js +55 -0
- package/src/host/settings/errors.js +29 -0
- package/src/host/settings/home.js +316 -0
- package/src/host/settings/http.js +164 -0
- package/src/host/settings/im.js +148 -0
- package/src/host/settings/lifecycle.js +373 -0
- package/src/host/settings/persona.js +113 -0
- package/src/host/settings/routes.js +229 -0
- package/src/host/settings/status.js +192 -0
- package/src/host/state/appraisals.js +56 -0
- package/src/host/state/feel.js +78 -0
- package/src/host/state/index.js +9 -0
- package/src/host/state/mood.js +103 -0
- package/src/host/state/notes.js +18 -0
- package/{life.js → src/host/state/outing.js} +13 -136
- package/src/host/state/values.js +9 -0
- package/{stickers → src/host/stickers}/tool.js +1 -1
- package/src/host/storage/fs.js +29 -0
- package/src/host/storage/index.js +8 -0
- package/src/host/storage/paths.js +20 -0
- package/src/host/storage/revision.js +5 -0
- package/src/host/storage/session-state.js +38 -0
- package/src/host/storage/time.js +127 -0
- package/src/host/tools/index.js +152 -0
- package/src/host/tools/read.js +252 -0
- package/src/host/tools/shared.js +23 -0
- package/src/host/tools/wiki.js +35 -0
- package/src/host/tools/write.js +164 -0
- package/{usage → src/host/usage}/aggregate.js +4 -1
- package/src/host/usage/ledger.js +186 -0
- package/src/host/usage/serve.js +116 -0
- package/client.js +0 -618
- package/gates.js +0 -18
- package/identity.js +0 -97
- package/prompts.js +0 -28
- package/storage.js +0 -95
- package/tools.js +0 -540
- package/usage/ledger.js +0 -126
- package/usage/serve.js +0 -38
- /package/{im → src/host/im}/say.js +0 -0
- /package/{memory → src/host/memory}/capture.js +0 -0
- /package/{memory → src/host/memory}/messages.js +0 -0
- /package/{memory → src/host/memory}/outbox.js +0 -0
- /package/{stickers → src/host/stickers}/load.js +0 -0
- /package/{stickers → src/host/stickers}/select.js +0 -0
- /package/{stickers → src/host/stickers}/serve.js +0 -0
- /package/{usage → src/host/usage}/pricing.js +0 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { aggregateUsage, usageCsv } from './aggregate.js'
|
|
2
|
+
import { resetUsage } from './ledger.js'
|
|
3
|
+
|
|
4
|
+
const ROOT = '/api/dsh-ta/usage'
|
|
5
|
+
const BODY_LIMIT = 4096
|
|
6
|
+
|
|
7
|
+
function sendJson(response, status, payload) {
|
|
8
|
+
response.writeHead(status, {
|
|
9
|
+
'content-type': 'application/json; charset=utf-8',
|
|
10
|
+
'cache-control': 'no-store',
|
|
11
|
+
})
|
|
12
|
+
response.end(JSON.stringify(payload))
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** 读取很小的 JSON 确认体;超过上限或格式错误都走 4xx。 */
|
|
16
|
+
function readJsonBody(request) {
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
const chunks = []
|
|
19
|
+
let size = 0
|
|
20
|
+
let settled = false
|
|
21
|
+
const fail = (status, message) => {
|
|
22
|
+
if (settled) return
|
|
23
|
+
settled = true
|
|
24
|
+
reject(Object.assign(new Error(message), { status }))
|
|
25
|
+
}
|
|
26
|
+
request.on('data', chunk => {
|
|
27
|
+
if (settled) return
|
|
28
|
+
size += chunk.length
|
|
29
|
+
if (size > BODY_LIMIT) {
|
|
30
|
+
fail(413, '确认信息过大')
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
chunks.push(chunk)
|
|
34
|
+
})
|
|
35
|
+
request.on('end', () => {
|
|
36
|
+
if (settled) return
|
|
37
|
+
settled = true
|
|
38
|
+
const text = Buffer.concat(chunks).toString('utf8')
|
|
39
|
+
if (!text.trim()) {
|
|
40
|
+
resolve({})
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
resolve(JSON.parse(text))
|
|
45
|
+
} catch {
|
|
46
|
+
fail(400, '确认信息不是合法 JSON')
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
request.on('error', () => fail(400, '读取请求失败'))
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function mountUsageRoute(ctx, config) {
|
|
54
|
+
if (typeof ctx.inject !== 'function') return
|
|
55
|
+
ctx.inject(['webServer'], scope => {
|
|
56
|
+
const server = scope.webServer ?? scope.get?.('webServer')
|
|
57
|
+
if (typeof server?.register !== 'function') return
|
|
58
|
+
try {
|
|
59
|
+
scope.effect(() => server.register({
|
|
60
|
+
kind: 'prefix',
|
|
61
|
+
path: ROOT,
|
|
62
|
+
auth: 'required',
|
|
63
|
+
handler: (request, response) => {
|
|
64
|
+
const url = new URL(request.url ?? '/', 'http://localhost')
|
|
65
|
+
const suffix = url.pathname.slice(ROOT.length)
|
|
66
|
+
if (suffix === '/reset' || suffix === '/reset/') {
|
|
67
|
+
if (String(request.method ?? 'GET').toUpperCase() !== 'POST') {
|
|
68
|
+
response.setHeader?.('allow', 'POST')
|
|
69
|
+
sendJson(response, 405, {
|
|
70
|
+
ok: false,
|
|
71
|
+
error: { code: 'METHOD_NOT_ALLOWED', message: '清零只支持 POST' },
|
|
72
|
+
})
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
void readJsonBody(request).then(body => {
|
|
76
|
+
if (body?.confirm !== '清零') {
|
|
77
|
+
sendJson(response, 400, {
|
|
78
|
+
ok: false,
|
|
79
|
+
error: { code: 'RESET_CONFIRM_REQUIRED', message: '请输入“清零”确认' },
|
|
80
|
+
})
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
const result = resetUsage(config)
|
|
84
|
+
sendJson(response, 200, { ok: true, data: { cleared: true, ...result } })
|
|
85
|
+
}).catch(error => {
|
|
86
|
+
sendJson(response, error?.status === 413 ? 413 : 400, {
|
|
87
|
+
ok: false,
|
|
88
|
+
error: {
|
|
89
|
+
code: error?.status === 413 ? 'PAYLOAD_TOO_LARGE' : 'INVALID_JSON',
|
|
90
|
+
message: error instanceof Error ? error.message : '确认失败',
|
|
91
|
+
},
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
const report = aggregateUsage(config, {
|
|
97
|
+
from: url.searchParams.get('from') ?? undefined,
|
|
98
|
+
to: url.searchParams.get('to') ?? undefined,
|
|
99
|
+
})
|
|
100
|
+
if (suffix === '/export.csv') {
|
|
101
|
+
response.writeHead(200, { 'content-type': 'text/csv; charset=utf-8', 'content-disposition': 'attachment; filename="dsh-ta-usage.csv"' })
|
|
102
|
+
response.end(usageCsv(report))
|
|
103
|
+
return
|
|
104
|
+
}
|
|
105
|
+
if (suffix !== '' && suffix !== '/' && suffix !== '/export.json') {
|
|
106
|
+
response.writeHead(404); response.end(); return
|
|
107
|
+
}
|
|
108
|
+
response.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
109
|
+
response.end(JSON.stringify(report))
|
|
110
|
+
},
|
|
111
|
+
}), 'dsh-ta usage route')
|
|
112
|
+
} catch (error) {
|
|
113
|
+
try { ctx.logger?.warn?.(error) } catch { /* logger optional */ }
|
|
114
|
+
}
|
|
115
|
+
})
|
|
116
|
+
}
|