@lingda-ai/dsh-plugin-ledger 0.1.3 → 0.1.4

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/lib/wire.js ADDED
@@ -0,0 +1,87 @@
1
+ /**
2
+ * 上报协议(wire format)。
3
+ *
4
+ * 现在一条都不发,但协议形状必须先冻住,因为它是**不可逆**的那一半:
5
+ * - 字段名一旦写进历史流水,后面想从 `inputTokens` 改成 `measures[]`,老数据永远迁不干净;
6
+ * - `eventId` 一旦用于幂等去重,算法变更会让历史事件重复入账。
7
+ *
8
+ * 设计取舍:
9
+ * 1. 计量用 `measures[]`(单位 + 数值)而不是 token 专用字段——订阅制工具(Cursor/Trae/Qoder)
10
+ * 根本没有 token 成本,只有席位成本;把 token 当主键,第二类工具就进不来。
11
+ * 2. `gen_ai.*` 仍保留在本地流水里(OTel 标准 + 便于本地排查),但**不是上报口径**。
12
+ * 3. 上报不带 summary、不带任何文本——服务端结构上收不到用户内容。
13
+ */
14
+ import { createHash } from 'node:crypto'
15
+
16
+ export const WIRE_VERSION = 1
17
+
18
+ /** token 计量单位;未来席位是 `seat.day`、按次是 `request`、时长是 `minute` */
19
+ export const UNIT = Object.freeze({
20
+ tokenInput: 'token.input',
21
+ tokenOutput: 'token.output',
22
+ tokenCacheRead: 'token.cache_read',
23
+ tokenCacheWrite: 'token.cache_write',
24
+ })
25
+
26
+ const UNIT_FIELDS = [
27
+ [UNIT.tokenInput, 'inputTokens'],
28
+ [UNIT.tokenOutput, 'outputTokens'],
29
+ [UNIT.tokenCacheRead, 'cacheReadTokens'],
30
+ [UNIT.tokenCacheWrite, 'cacheWriteTokens'],
31
+ ]
32
+
33
+ /**
34
+ * 幂等键:同一会话同一 turn/step/类型只可能产生一条,重复上报必须落成同一 id。
35
+ * 这条是双端(Desktop + 云主机)重复上报的唯一防线——没有它,账会翻倍。
36
+ */
37
+ export function eventIdOf({ sessionId, turn, step, operation }) {
38
+ const key = [sessionId ?? '', turn ?? 0, step ?? 0, operation ?? ''].join('|')
39
+ return createHash('sha1').update(key).digest('hex').slice(0, 16)
40
+ }
41
+
42
+ /**
43
+ * usage → 中性计量数组。0 值不进数组:既没成本也没信息量,白白撑大流水。
44
+ * 注意"缺字段"与"值为 0"的区别由 usage.complete 承载,不靠 measures 表达。
45
+ */
46
+ export function measuresOf(usage) {
47
+ if (!usage) return []
48
+ const out = []
49
+ for (const [unit, field] of UNIT_FIELDS) {
50
+ const value = usage[field]
51
+ if (Number.isFinite(value) && value > 0) out.push({ unit, value })
52
+ }
53
+ return out
54
+ }
55
+
56
+ /**
57
+ * 本地流水行 → 上报事件(白名单投影,未知字段一律丢弃)。
58
+ * 服务端 strict 校验依赖这个白名单:多出来的字段会被拒,所以这里必须显式列全。
59
+ */
60
+ export function wireEventOf(row, options = {}) {
61
+ if (!row || typeof row !== 'object') return null
62
+ const operation = row['gen_ai.operation.name'] ?? null
63
+ const usage = {
64
+ inputTokens: row['gen_ai.usage.input_tokens'],
65
+ outputTokens: row['gen_ai.usage.output_tokens'],
66
+ cacheReadTokens: row['gen_ai.usage.cache_read_tokens'],
67
+ cacheWriteTokens: row['gen_ai.usage.cache_write_tokens'],
68
+ }
69
+ return {
70
+ wireVersion: WIRE_VERSION,
71
+ eventId: row.eventId ?? eventIdOf({ sessionId: row.sessionId, turn: row.turn, step: row.step, operation }),
72
+ timestamp: row.timestamp,
73
+ source: options.source ?? 'dsh',
74
+ deviceId: row.deviceId ?? null,
75
+ actor: row.actor ?? null,
76
+ project: row.project ?? null,
77
+ sessionId: row.sessionId ?? null,
78
+ turn: row.turn ?? null,
79
+ step: row.step ?? null,
80
+ operation,
81
+ plugin: row['gen_ai.agent.name'] ?? null,
82
+ // 已有 measures 就用它(未来非 token 计量直接写这里),否则从 gen_ai 派生
83
+ measures: Array.isArray(row.measures) && row.measures.length > 0 ? row.measures : measuresOf(usage),
84
+ cost: row.cost ?? null,
85
+ costUnknownReason: row.costUnknownReason ?? null,
86
+ }
87
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lingda-ai/dsh-plugin-ledger",
3
- "version": "0.1.3",
4
- "description": "DSH 插件搜索入口:运行时账本(成本/用量观测)。实现在 @lingda-ai/ledger,本包仅转发装配。",
3
+ "version": "0.1.4",
4
+ "description": "DSH 插件搜索入口:运行时账本(成本/用量观测)。自包含单包,不依赖其他 npm 包,便于从镜像源安装。",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
7
7
  "dsh",
@@ -14,16 +14,36 @@
14
14
  "dsh": {
15
15
  "bundle": {
16
16
  "patch": "./cordis.patch.yml"
17
+ },
18
+ "client": {
19
+ "inject": [
20
+ "@deepseek-ai/dsh-api-session-controller",
21
+ "@deepseek-ai/dsh-client-ui-sidebar-right"
22
+ ],
23
+ "platform": "web"
17
24
  }
18
25
  },
19
26
  "files": [
27
+ "lib",
20
28
  "cordis.patch.yml",
21
- "README.md"
29
+ "README.md",
30
+ "docs"
22
31
  ],
23
32
  "publishConfig": {
24
33
  "access": "public"
25
34
  },
26
- "dependencies": {
27
- "@lingda-ai/ledger": "^0.1.0"
35
+ "peerDependencies": {
36
+ "@deepseek-ai/cordis": "*",
37
+ "@deepseek-ai/dsh-llm": "*"
38
+ },
39
+ "type": "module",
40
+ "main": "./lib/index.js",
41
+ "exports": {
42
+ ".": "./lib/index.js",
43
+ "./client": "./lib/client.js",
44
+ "./package.json": "./package.json"
45
+ },
46
+ "engines": {
47
+ "node": ">=18"
28
48
  }
29
49
  }