@anycon/sdk 0.1.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/LICENSE +176 -0
- package/README.md +240 -0
- package/README.zh-CN.md +188 -0
- package/dist/async-queue.d.ts +20 -0
- package/dist/async-queue.d.ts.map +1 -0
- package/dist/async-queue.js +95 -0
- package/dist/async-queue.js.map +1 -0
- package/dist/attachments.d.ts +53 -0
- package/dist/attachments.d.ts.map +1 -0
- package/dist/attachments.js +228 -0
- package/dist/attachments.js.map +1 -0
- package/dist/client.d.ts +90 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +280 -0
- package/dist/client.js.map +1 -0
- package/dist/connect.d.ts +16 -0
- package/dist/connect.d.ts.map +1 -0
- package/dist/connect.js +24 -0
- package/dist/connect.js.map +1 -0
- package/dist/default-storage.d.ts +4 -0
- package/dist/default-storage.d.ts.map +1 -0
- package/dist/default-storage.js +15 -0
- package/dist/default-storage.js.map +1 -0
- package/dist/emitter.d.ts +10 -0
- package/dist/emitter.d.ts.map +1 -0
- package/dist/emitter.js +38 -0
- package/dist/emitter.js.map +1 -0
- package/dist/errors.d.ts +112 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +155 -0
- package/dist/errors.js.map +1 -0
- package/dist/hub.d.ts +85 -0
- package/dist/hub.d.ts.map +1 -0
- package/dist/hub.js +362 -0
- package/dist/hub.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/link.d.ts +87 -0
- package/dist/link.d.ts.map +1 -0
- package/dist/link.js +628 -0
- package/dist/link.js.map +1 -0
- package/dist/node.d.ts +213 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +474 -0
- package/dist/node.js.map +1 -0
- package/dist/pair.d.ts +75 -0
- package/dist/pair.d.ts.map +1 -0
- package/dist/pair.js +390 -0
- package/dist/pair.js.map +1 -0
- package/dist/permissions.d.ts +65 -0
- package/dist/permissions.d.ts.map +1 -0
- package/dist/permissions.js +74 -0
- package/dist/permissions.js.map +1 -0
- package/dist/runtime.d.ts +5 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +15 -0
- package/dist/runtime.js.map +1 -0
- package/dist/session.d.ts +208 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +504 -0
- package/dist/session.js.map +1 -0
- package/dist/storage-file.d.ts +21 -0
- package/dist/storage-file.d.ts.map +1 -0
- package/dist/storage-file.js +62 -0
- package/dist/storage-file.js.map +1 -0
- package/dist/storage-idb.d.ts +19 -0
- package/dist/storage-idb.d.ts.map +1 -0
- package/dist/storage-idb.js +64 -0
- package/dist/storage-idb.js.map +1 -0
- package/dist/storage.d.ts +78 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +163 -0
- package/dist/storage.js.map +1 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +3 -0
- package/dist/version.js.map +1 -0
- package/dist/ws.d.ts +41 -0
- package/dist/ws.d.ts.map +1 -0
- package/dist/ws.js +66 -0
- package/dist/ws.js.map +1 -0
- package/package.json +65 -0
- package/src/async-queue.ts +100 -0
- package/src/attachments.ts +301 -0
- package/src/client.ts +351 -0
- package/src/connect.ts +29 -0
- package/src/default-storage.ts +14 -0
- package/src/emitter.ts +40 -0
- package/src/errors.ts +218 -0
- package/src/hub.ts +456 -0
- package/src/index.ts +143 -0
- package/src/link.ts +699 -0
- package/src/node.ts +652 -0
- package/src/pair.ts +527 -0
- package/src/permissions.ts +134 -0
- package/src/runtime.ts +14 -0
- package/src/session.ts +697 -0
- package/src/storage-file.ts +78 -0
- package/src/storage-idb.ts +74 -0
- package/src/storage.ts +231 -0
- package/src/version.ts +2 -0
- package/src/ws.ts +94 -0
package/src/emitter.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
type Handler = (...args: unknown[]) => void
|
|
2
|
+
|
|
3
|
+
/** 极简类型化事件发射器。监听器抛错不影响其他监听器。 */
|
|
4
|
+
export class Emitter<Events extends { [K in keyof Events]: unknown[] }> {
|
|
5
|
+
readonly #handlers = new Map<keyof Events, Set<Handler>>()
|
|
6
|
+
|
|
7
|
+
on<K extends keyof Events>(event: K, handler: (...args: Events[K]) => void): () => void {
|
|
8
|
+
let set = this.#handlers.get(event)
|
|
9
|
+
if (!set) {
|
|
10
|
+
set = new Set()
|
|
11
|
+
this.#handlers.set(event, set)
|
|
12
|
+
}
|
|
13
|
+
const h = handler as unknown as Handler
|
|
14
|
+
set.add(h)
|
|
15
|
+
return () => {
|
|
16
|
+
set.delete(h)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
once<K extends keyof Events>(event: K, handler: (...args: Events[K]) => void): () => void {
|
|
21
|
+
const off = this.on(event, (...args: Events[K]) => {
|
|
22
|
+
off()
|
|
23
|
+
handler(...args)
|
|
24
|
+
})
|
|
25
|
+
return off
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
protected emit<K extends keyof Events>(event: K, ...args: Events[K]): void {
|
|
29
|
+
const set = this.#handlers.get(event)
|
|
30
|
+
if (!set) return
|
|
31
|
+
const handlers = Array.from(set)
|
|
32
|
+
for (const h of handlers) {
|
|
33
|
+
try {
|
|
34
|
+
h(...args)
|
|
35
|
+
} catch {
|
|
36
|
+
// 监听器的异常不能拖垮连接状态机。
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SDK 错误类。协议层的 JSON-RPC 错误(`AnyconRpcError`)按稳定码映射到具体子类:
|
|
3
|
+
* `agent_unavailable` → `AgentUnavailable`,`invalid_settings` → `SettingsInvalid`,
|
|
4
|
+
* `node_busy` → `NodeBusy`,`protocol_mismatch` → `ProtocolMismatch`,附件的拒绝(`data.reason` 是
|
|
5
|
+
* `AttachmentErrorReason` 之一)→ `AttachmentError`,其余 → `RpcError`。
|
|
6
|
+
*/
|
|
7
|
+
import {
|
|
8
|
+
AnyconRpcError,
|
|
9
|
+
ERROR_CODE_NUMBERS,
|
|
10
|
+
type ErrorCode,
|
|
11
|
+
type PairFailReason,
|
|
12
|
+
type RpcErrorData,
|
|
13
|
+
} from '@anycon/protocol'
|
|
14
|
+
|
|
15
|
+
/** SDK 本地错误(非 RPC 答复)的基类。 */
|
|
16
|
+
export class AnyconError extends Error {
|
|
17
|
+
override readonly name: string = 'AnyconError'
|
|
18
|
+
/** 稳定的 snake_case 码,便于程序判断和 i18n。 */
|
|
19
|
+
readonly code: string
|
|
20
|
+
|
|
21
|
+
constructor(code: string, message: string, options?: { cause?: unknown }) {
|
|
22
|
+
super(message, options)
|
|
23
|
+
this.code = code
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 本地没有该 hub 的设备身份,或 hub 不认这个设备 token:需要先 `pair()`。 */
|
|
28
|
+
export class PairingRequired extends AnyconError {
|
|
29
|
+
override readonly name: string = 'PairingRequired'
|
|
30
|
+
constructor(message = 'this device is not paired with the hub yet: call pair() first') {
|
|
31
|
+
super('pairing_required', message)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 配对失败(短码不存在 / 过期、超时、对端断开、握手错误……)。 */
|
|
36
|
+
export class PairingFailed extends AnyconError {
|
|
37
|
+
override readonly name: string = 'PairingFailed'
|
|
38
|
+
readonly reason: PairFailReason | 'handshake' | 'protocol' | 'cancelled'
|
|
39
|
+
constructor(
|
|
40
|
+
reason: PairFailReason | 'handshake' | 'protocol' | 'cancelled',
|
|
41
|
+
message?: string,
|
|
42
|
+
options?: { cause?: unknown },
|
|
43
|
+
) {
|
|
44
|
+
super(`pairing_${reason}`, message ?? `pairing failed: ${reason}`, options)
|
|
45
|
+
this.reason = reason
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 配对时节点给出的公钥与本地已记录的同 id 节点不同。SDK 不会静默覆盖已固定的公钥——这可能是中间人,
|
|
51
|
+
* 也可能是节点重装。确认可信后先 `forgetNode()` 再重新配对。
|
|
52
|
+
*/
|
|
53
|
+
export class NodeKeyChanged extends AnyconError {
|
|
54
|
+
override readonly name: string = 'NodeKeyChanged'
|
|
55
|
+
readonly nodeId: string
|
|
56
|
+
/** 本地已固定的公钥指纹。 */
|
|
57
|
+
readonly storedFingerprint: string | undefined
|
|
58
|
+
/** 这次配对看到的公钥指纹。 */
|
|
59
|
+
readonly presentedFingerprint: string | undefined
|
|
60
|
+
constructor(
|
|
61
|
+
nodeId: string,
|
|
62
|
+
storedFingerprint?: string,
|
|
63
|
+
presentedFingerprint?: string,
|
|
64
|
+
options?: { cause?: unknown },
|
|
65
|
+
) {
|
|
66
|
+
super(
|
|
67
|
+
'node_key_changed',
|
|
68
|
+
`node ${nodeId.slice(0, 8)} is already paired with a different key ` +
|
|
69
|
+
`(stored ${storedFingerprint ?? 'unknown'}, presented ${presentedFingerprint ?? 'unknown'}); ` +
|
|
70
|
+
'it may be a man-in-the-middle or a reinstalled node. Nothing was saved. ' +
|
|
71
|
+
'If you trust the new key, call forgetNode() for this node and pair again.',
|
|
72
|
+
options,
|
|
73
|
+
)
|
|
74
|
+
this.nodeId = nodeId
|
|
75
|
+
this.storedFingerprint = storedFingerprint
|
|
76
|
+
this.presentedFingerprint = presentedFingerprint
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 节点不在线(hub 报 `peer_offline` / `undeliverable`,或 hub 连接断开)。 */
|
|
81
|
+
export class NodeOffline extends AnyconError {
|
|
82
|
+
override readonly name: string = 'NodeOffline'
|
|
83
|
+
readonly node: string | undefined
|
|
84
|
+
constructor(node?: string, message?: string) {
|
|
85
|
+
super('node_offline', message ?? `node ${node ?? ''} is offline`.replace(' ', ' '))
|
|
86
|
+
this.node = node
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Noise 握手失败:设备被吊销,或 daemon 重装后不认识这台设备的公钥。需要重新配对。 */
|
|
91
|
+
export class NodeRejected extends AnyconError {
|
|
92
|
+
override readonly name: string = 'NodeRejected'
|
|
93
|
+
constructor(message = 'the node rejected the Noise handshake: re-pair this device') {
|
|
94
|
+
super('node_rejected', message)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** 与 hub 的连接问题(连不上、认证被拒、被新连接顶掉……)。 */
|
|
99
|
+
export class ConnectionError extends AnyconError {
|
|
100
|
+
override readonly name: string = 'ConnectionError'
|
|
101
|
+
/** WebSocket close code(如有)。 */
|
|
102
|
+
readonly closeCode: number | undefined
|
|
103
|
+
constructor(message: string, options?: { closeCode?: number; cause?: unknown }) {
|
|
104
|
+
super('connection_failed', message, options)
|
|
105
|
+
this.closeCode = options?.closeCode
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 其他 JSON-RPC 错误。`code` 与 protocol.md 的稳定错误码一致。 */
|
|
110
|
+
export class RpcError extends AnyconRpcError {
|
|
111
|
+
override readonly name: string = 'RpcError'
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** agent 未安装 / 未登录 / 被禁用。`reason` 是 `AgentStatus`。 */
|
|
115
|
+
export class AgentUnavailable extends RpcError {
|
|
116
|
+
override readonly name: string = 'AgentUnavailable'
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** 模型 / 思考 / 模式 / 选项不被该 agent 接受。`field` 指出字段。 */
|
|
120
|
+
export class SettingsInvalid extends RpcError {
|
|
121
|
+
override readonly name: string = 'SettingsInvalid'
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** 节点并发上限且未排队。 */
|
|
125
|
+
export class NodeBusy extends RpcError {
|
|
126
|
+
override readonly name: string = 'NodeBusy'
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 附件(blob 通道,ADR-35)被 daemon 拒绝时的 `data.reason`(protocol.md § 5.6):
|
|
131
|
+
*
|
|
132
|
+
* - `too_large`(`invalid`):单张超过 `sessions.max_attachment_bytes`(默认 10 MiB),或一条消息的附件合计超过 20 MiB
|
|
133
|
+
* - `quota`(`busy`):完成但没被引用的 blob 全节点合计超过 1 GiB——先发出去或 `discardAttachment()` 一些
|
|
134
|
+
* - `disk_full`(`busy`):daemon 数据目录所在卷剩余空间不够
|
|
135
|
+
* - `blob`(`not_found`):消息引用的 blob 不存在(24 小时没被引用已被回收、被丢掉了,或文件不见了)——重新上传
|
|
136
|
+
* - `blob_in_use`(`invalid`):blob 已经绑定到**别的**会话的消息上——一张图只能属于一个会话,重新上传
|
|
137
|
+
* - `in_use`(`invalid`):`blob.discard` 一个已被消息引用的 blob
|
|
138
|
+
* - `finalizing`(`invalid`):`put_end` 已经开始收尾之后又来了块
|
|
139
|
+
* - `incomplete` / `hash_mismatch` / `mime_mismatch`(`invalid`):`put_end` 校验失败(缺块 / SHA-256 不符 / 文件头和 mime 不符)
|
|
140
|
+
*/
|
|
141
|
+
export type AttachmentErrorReason =
|
|
142
|
+
| 'too_large'
|
|
143
|
+
| 'quota'
|
|
144
|
+
| 'disk_full'
|
|
145
|
+
| 'blob'
|
|
146
|
+
| 'blob_in_use'
|
|
147
|
+
| 'in_use'
|
|
148
|
+
| 'finalizing'
|
|
149
|
+
| 'incomplete'
|
|
150
|
+
| 'hash_mismatch'
|
|
151
|
+
| 'mime_mismatch'
|
|
152
|
+
|
|
153
|
+
const ATTACHMENT_REASONS: ReadonlySet<string> = new Set<AttachmentErrorReason>([
|
|
154
|
+
'too_large',
|
|
155
|
+
'quota',
|
|
156
|
+
'disk_full',
|
|
157
|
+
'blob',
|
|
158
|
+
'blob_in_use',
|
|
159
|
+
'in_use',
|
|
160
|
+
'finalizing',
|
|
161
|
+
'incomplete',
|
|
162
|
+
'hash_mismatch',
|
|
163
|
+
'mime_mismatch',
|
|
164
|
+
])
|
|
165
|
+
/** 附件的拒绝只会带这几个稳定码。 */
|
|
166
|
+
const ATTACHMENT_CODES: ReadonlySet<string> = new Set(['invalid', 'busy', 'not_found'])
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* 附件被 daemon 拒绝(`blob.*`,或 `session.send` / `session.create` 核对附件时)。`code` 仍是稳定码
|
|
170
|
+
* (`invalid` / `busy` / `not_found`),`reason` 是 `AttachmentErrorReason`,按它决定重传、换图还是提示用户。
|
|
171
|
+
*/
|
|
172
|
+
export class AttachmentError extends RpcError {
|
|
173
|
+
override readonly name: string = 'AttachmentError'
|
|
174
|
+
override get reason(): AttachmentErrorReason {
|
|
175
|
+
return super.reason as AttachmentErrorReason
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** 协议版本不兼容。 */
|
|
180
|
+
export class ProtocolMismatch extends RpcError {
|
|
181
|
+
override readonly name: string = 'ProtocolMismatch'
|
|
182
|
+
readonly ours: number
|
|
183
|
+
readonly theirs: number | undefined
|
|
184
|
+
constructor(ours: number, theirs?: number, message?: string, data?: RpcErrorData | null) {
|
|
185
|
+
super(
|
|
186
|
+
ERROR_CODE_NUMBERS.protocol_mismatch,
|
|
187
|
+
message ?? `protocol mismatch: ours=${ours} theirs=${theirs ?? 'unknown'}`,
|
|
188
|
+
data ?? { code: 'protocol_mismatch' },
|
|
189
|
+
)
|
|
190
|
+
this.ours = ours
|
|
191
|
+
this.theirs = theirs
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** 把协议层 RPC 错误转成对应的 SDK 子类;不是 RPC 错误的原样返回。 */
|
|
196
|
+
export function mapRpcError(err: unknown, ours = 1): unknown {
|
|
197
|
+
if (!(err instanceof AnyconRpcError) || err instanceof RpcError) return err
|
|
198
|
+
const code: ErrorCode | (string & {}) = err.code
|
|
199
|
+
switch (code) {
|
|
200
|
+
case 'agent_unavailable':
|
|
201
|
+
return new AgentUnavailable(err.rpcCode, err.message, err.data)
|
|
202
|
+
case 'invalid_settings':
|
|
203
|
+
return new SettingsInvalid(err.rpcCode, err.message, err.data)
|
|
204
|
+
case 'node_busy':
|
|
205
|
+
return new NodeBusy(err.rpcCode, err.message, err.data)
|
|
206
|
+
case 'protocol_mismatch':
|
|
207
|
+
return new ProtocolMismatch(ours, undefined, err.message, err.data)
|
|
208
|
+
default:
|
|
209
|
+
if (
|
|
210
|
+
err.reason !== undefined &&
|
|
211
|
+
ATTACHMENT_REASONS.has(err.reason) &&
|
|
212
|
+
ATTACHMENT_CODES.has(code)
|
|
213
|
+
) {
|
|
214
|
+
return new AttachmentError(err.rpcCode, err.message, err.data)
|
|
215
|
+
}
|
|
216
|
+
return new RpcError(err.rpcCode, err.message, err.data)
|
|
217
|
+
}
|
|
218
|
+
}
|