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.
Files changed (85) hide show
  1. package/CHANGELOG.md +111 -1
  2. package/README.md +325 -140
  3. package/cordis.patch.yml +5 -35
  4. package/docs/RELEASE.md +63 -46
  5. package/docs/USAGE.md +25 -9
  6. package/docs/presets.example.yml +7 -43
  7. package/index.js +3 -170
  8. package/locale/en.json +1 -1
  9. package/locale/zh.json +1 -1
  10. package/package.json +6 -19
  11. package/src/client/client.js +3176 -0
  12. package/{config.js → src/host/config.js} +39 -4
  13. package/src/host/constants.js +7 -0
  14. package/src/host/gates.js +34 -0
  15. package/src/host/identity.js +145 -0
  16. package/{im → src/host/im}/bindings.js +1 -1
  17. package/src/host/index.js +173 -0
  18. package/{jurisdiction.js → src/host/jurisdiction.js} +1 -1
  19. package/src/host/maintenance/collect.js +362 -0
  20. package/src/host/maintenance/completions.js +56 -0
  21. package/src/host/maintenance/consumption.js +67 -0
  22. package/src/host/maintenance/cursors.js +53 -0
  23. package/src/host/maintenance/engine.js +392 -0
  24. package/src/host/maintenance/llm.js +381 -0
  25. package/src/host/maintenance/lock.js +105 -0
  26. package/src/host/maintenance/prompts.js +255 -0
  27. package/src/host/maintenance/runs.js +47 -0
  28. package/src/host/maintenance/schedule.js +173 -0
  29. package/src/host/maintenance/session-cleanup.js +289 -0
  30. package/src/host/maintenance/session-watcher.js +107 -0
  31. package/src/host/maintenance/sleep.js +3 -0
  32. package/src/host/maintenance/stages.js +241 -0
  33. package/src/host/maintenance/state.js +192 -0
  34. package/src/host/prompts.js +96 -0
  35. package/src/host/records.js +175 -0
  36. package/{reme.js → src/host/reme.js} +3 -1
  37. package/src/host/runtime-manager.js +281 -0
  38. package/{runtime.js → src/host/runtime.js} +28 -50
  39. package/src/host/settings/defaults.js +55 -0
  40. package/src/host/settings/errors.js +29 -0
  41. package/src/host/settings/home.js +316 -0
  42. package/src/host/settings/http.js +164 -0
  43. package/src/host/settings/im.js +148 -0
  44. package/src/host/settings/lifecycle.js +373 -0
  45. package/src/host/settings/persona.js +113 -0
  46. package/src/host/settings/routes.js +229 -0
  47. package/src/host/settings/status.js +192 -0
  48. package/src/host/state/appraisals.js +56 -0
  49. package/src/host/state/feel.js +78 -0
  50. package/src/host/state/index.js +9 -0
  51. package/src/host/state/mood.js +103 -0
  52. package/src/host/state/notes.js +18 -0
  53. package/{life.js → src/host/state/outing.js} +13 -136
  54. package/src/host/state/values.js +9 -0
  55. package/{stickers → src/host/stickers}/tool.js +1 -1
  56. package/src/host/storage/fs.js +29 -0
  57. package/src/host/storage/index.js +8 -0
  58. package/src/host/storage/paths.js +20 -0
  59. package/src/host/storage/revision.js +5 -0
  60. package/src/host/storage/session-state.js +38 -0
  61. package/src/host/storage/time.js +127 -0
  62. package/src/host/tools/index.js +152 -0
  63. package/src/host/tools/read.js +252 -0
  64. package/src/host/tools/shared.js +23 -0
  65. package/src/host/tools/wiki.js +35 -0
  66. package/src/host/tools/write.js +164 -0
  67. package/{usage → src/host/usage}/aggregate.js +4 -1
  68. package/src/host/usage/ledger.js +186 -0
  69. package/src/host/usage/serve.js +116 -0
  70. package/client.js +0 -618
  71. package/gates.js +0 -18
  72. package/identity.js +0 -97
  73. package/prompts.js +0 -28
  74. package/storage.js +0 -95
  75. package/tools.js +0 -540
  76. package/usage/ledger.js +0 -126
  77. package/usage/serve.js +0 -38
  78. /package/{im → src/host/im}/say.js +0 -0
  79. /package/{memory → src/host/memory}/capture.js +0 -0
  80. /package/{memory → src/host/memory}/messages.js +0 -0
  81. /package/{memory → src/host/memory}/outbox.js +0 -0
  82. /package/{stickers → src/host/stickers}/load.js +0 -0
  83. /package/{stickers → src/host/stickers}/select.js +0 -0
  84. /package/{stickers → src/host/stickers}/serve.js +0 -0
  85. /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
+ }