@im-more/dsh-onebot 0.0.0-stage → 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 +21 -0
- package/cordis.patch.yml +11 -0
- package/lib/index.js +2003 -0
- package/lib/index.js.map +1 -0
- package/lib/types/bot.d.ts +55 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +140 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +40 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +33 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/cqcode.d.ts +29 -0
- package/lib/types/cqcode.d.ts.map +1 -0
- package/lib/types/cqcode.js +73 -0
- package/lib/types/cqcode.js.map +1 -0
- package/lib/types/decode.d.ts +106 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +433 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +32 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +117 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/identity.d.ts +18 -0
- package/lib/types/identity.d.ts.map +1 -0
- package/lib/types/identity.js +27 -0
- package/lib/types/identity.js.map +1 -0
- package/lib/types/index.d.ts +7 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +6 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/modes/segment.d.ts +16 -0
- package/lib/types/modes/segment.d.ts.map +1 -0
- package/lib/types/modes/segment.js +80 -0
- package/lib/types/modes/segment.js.map +1 -0
- package/lib/types/modes/shared.d.ts +101 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +208 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/request.d.ts +64 -0
- package/lib/types/request.d.ts.map +1 -0
- package/lib/types/request.js +108 -0
- package/lib/types/request.js.map +1 -0
- package/lib/types/send.d.ts +93 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +161 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/transport/http.d.ts +29 -0
- package/lib/types/transport/http.d.ts.map +1 -0
- package/lib/types/transport/http.js +158 -0
- package/lib/types/transport/http.js.map +1 -0
- package/lib/types/transport/shared.d.ts +48 -0
- package/lib/types/transport/shared.d.ts.map +1 -0
- package/lib/types/transport/shared.js +75 -0
- package/lib/types/transport/shared.js.map +1 -0
- package/lib/types/transport/ws-reverse.d.ts +42 -0
- package/lib/types/transport/ws-reverse.d.ts.map +1 -0
- package/lib/types/transport/ws-reverse.js +178 -0
- package/lib/types/transport/ws-reverse.js.map +1 -0
- package/lib/types/transport/ws.d.ts +40 -0
- package/lib/types/transport/ws.d.ts.map +1 -0
- package/lib/types/transport/ws.js +233 -0
- package/lib/types/transport/ws.js.map +1 -0
- package/lib/types/types.d.ts +495 -0
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js +191 -0
- package/lib/types/types.js.map +1 -0
- package/package.json +68 -3
- package/src/bot.ts +230 -0
- package/src/config.ts +66 -0
- package/src/cqcode.ts +85 -0
- package/src/decode.ts +524 -0
- package/src/errors.ts +134 -0
- package/src/identity.ts +34 -0
- package/src/index.ts +18 -0
- package/src/modes/segment.ts +109 -0
- package/src/modes/shared.ts +277 -0
- package/src/request.ts +128 -0
- package/src/send.ts +233 -0
- package/src/transport/http.ts +221 -0
- package/src/transport/shared.ts +98 -0
- package/src/transport/ws-reverse.ts +236 -0
- package/src/transport/ws.ts +286 -0
- package/src/types.ts +743 -0
- package/README.md +0 -3
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `segment` mode: the OneBot 11 array message format.
|
|
3
|
+
*
|
|
4
|
+
* Native tags are the ones the upstream encoder produces (`text`, `p`, `br`, `at`, `emoji`, `channel`, `a`, `img`,
|
|
5
|
+
* `audio`, `video`, `file`); everything else takes the core fallback, which keeps its text. There is no second mode:
|
|
6
|
+
* the platform has no other way to render a message.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { ElementRegistry, type Mode } from '@deepseek-ai/cordis-plugin-im-core'
|
|
10
|
+
import {
|
|
11
|
+
assembleDraft,
|
|
12
|
+
fileName,
|
|
13
|
+
serializeDraft,
|
|
14
|
+
textNode,
|
|
15
|
+
type OneBotModeTypes,
|
|
16
|
+
type OneBotNode,
|
|
17
|
+
} from './shared.ts'
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Build the `segment` mode.
|
|
21
|
+
* @param splitMixedContent - pad a space around mixed text and images (`advanced.splitMixedContent`).
|
|
22
|
+
* @returns the only mode of this adapter.
|
|
23
|
+
*/
|
|
24
|
+
export function createSegmentMode(
|
|
25
|
+
splitMixedContent: boolean,
|
|
26
|
+
): Mode<OneBotModeTypes> {
|
|
27
|
+
const elements = new ElementRegistry<OneBotModeTypes>()
|
|
28
|
+
.register('text', (el) => textNode(el.attrs.content))
|
|
29
|
+
// A paragraph opens and closes a line; `assemble` collapses consecutive newlines.
|
|
30
|
+
.register('p', (el, ctx) => [
|
|
31
|
+
{ kind: 'newline' },
|
|
32
|
+
...ctx.render(el.children),
|
|
33
|
+
{ kind: 'newline' },
|
|
34
|
+
])
|
|
35
|
+
.register('br', () => ({ kind: 'newline' }))
|
|
36
|
+
.register(
|
|
37
|
+
'at',
|
|
38
|
+
(el) =>
|
|
39
|
+
el.attrs.type === 'all'
|
|
40
|
+
? { kind: 'segment', segment: { type: 'at', data: { qq: 'all' } } }
|
|
41
|
+
: {
|
|
42
|
+
kind: 'segment',
|
|
43
|
+
segment: {
|
|
44
|
+
type: 'at',
|
|
45
|
+
data: {
|
|
46
|
+
qq: el.attrs.id ?? '',
|
|
47
|
+
...(el.attrs.name === undefined
|
|
48
|
+
? {}
|
|
49
|
+
: { name: el.attrs.name }),
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
{ variants: ['user', 'all'] },
|
|
54
|
+
)
|
|
55
|
+
// A QQ face is a numeric id; any other emoji has no segment and stays its name.
|
|
56
|
+
.register('emoji', (el) =>
|
|
57
|
+
/^\d+$/.test(el.attrs.id)
|
|
58
|
+
? {
|
|
59
|
+
kind: 'segment',
|
|
60
|
+
segment: { type: 'face', data: { id: el.attrs.id } },
|
|
61
|
+
}
|
|
62
|
+
: textNode(el.attrs.name ?? ''),
|
|
63
|
+
)
|
|
64
|
+
.register('channel', (el) => textNode(el.attrs.id))
|
|
65
|
+
.register('a', (el, ctx) => [
|
|
66
|
+
...ctx.render(el.children),
|
|
67
|
+
// OneBot clients do not render links, so the URL has to be visible (upstream issue #23).
|
|
68
|
+
textNode(`(${el.attrs.href})`),
|
|
69
|
+
])
|
|
70
|
+
.register(
|
|
71
|
+
'img',
|
|
72
|
+
(el, ctx) => ({
|
|
73
|
+
kind: 'media',
|
|
74
|
+
type: 'image',
|
|
75
|
+
ref: ctx.resource('image', el.attrs.src, {}),
|
|
76
|
+
}),
|
|
77
|
+
{ variants: ['inline'] },
|
|
78
|
+
)
|
|
79
|
+
.register('audio', (el, ctx) => ({
|
|
80
|
+
kind: 'media',
|
|
81
|
+
type: 'record',
|
|
82
|
+
ref: ctx.resource('audio', el.attrs.src, {}),
|
|
83
|
+
}))
|
|
84
|
+
.register('video', (el, ctx) => ({
|
|
85
|
+
kind: 'media',
|
|
86
|
+
type: 'video',
|
|
87
|
+
ref: ctx.resource('video', el.attrs.src, {}),
|
|
88
|
+
}))
|
|
89
|
+
.register(
|
|
90
|
+
'file',
|
|
91
|
+
(el, ctx): OneBotNode => ({
|
|
92
|
+
kind: 'file',
|
|
93
|
+
name: fileName(el.attrs.name, el.attrs.src),
|
|
94
|
+
ref: ctx.resource(
|
|
95
|
+
'file',
|
|
96
|
+
el.attrs.src,
|
|
97
|
+
el.attrs.name === undefined ? {} : { name: el.attrs.name },
|
|
98
|
+
),
|
|
99
|
+
}),
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
id: 'segment',
|
|
104
|
+
elements,
|
|
105
|
+
limits: {},
|
|
106
|
+
assemble: (nodes) => assembleDraft(nodes, splitMixedContent),
|
|
107
|
+
serialize: (prepared, ctx) => serializeDraft(prepared, ctx),
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ChatRef,
|
|
3
|
+
ModeTypes,
|
|
4
|
+
Prepared,
|
|
5
|
+
ResourceRef,
|
|
6
|
+
SerializeContext,
|
|
7
|
+
} from '@deepseek-ai/cordis-plugin-im-core'
|
|
8
|
+
import type { Dict } from '@deepseek-ai/cosmokit'
|
|
9
|
+
|
|
10
|
+
/** Group chat id prefix; the private prefix is the upstream one. */
|
|
11
|
+
export const GROUP_PREFIX = 'group:'
|
|
12
|
+
/** Private chat id prefix, unchanged from the upstream adapter. */
|
|
13
|
+
export const PRIVATE_PREFIX = 'private:'
|
|
14
|
+
|
|
15
|
+
/** One outbound message segment. */
|
|
16
|
+
export interface OneBotSegment {
|
|
17
|
+
type: string
|
|
18
|
+
data: Dict<string | number>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** One item of a message draft: a ready segment or a media placeholder resolved by `prepare`. */
|
|
22
|
+
export type OneBotItem =
|
|
23
|
+
| { kind: 'segment'; segment: OneBotSegment }
|
|
24
|
+
| { kind: 'media'; type: 'image' | 'record' | 'video'; ref: ResourceRef }
|
|
25
|
+
|
|
26
|
+
/** One draft the mode produces from the node stream. */
|
|
27
|
+
export type OneBotDraft =
|
|
28
|
+
| { kind: 'message'; items: OneBotItem[] }
|
|
29
|
+
| { kind: 'file'; ref: ResourceRef; name: string }
|
|
30
|
+
|
|
31
|
+
/** Exact request data of one platform message. */
|
|
32
|
+
export interface OneBotMessagePayload {
|
|
33
|
+
kind: 'message'
|
|
34
|
+
chat: ChatRef
|
|
35
|
+
segments: OneBotSegment[]
|
|
36
|
+
/** The answered message, on the first part only. */
|
|
37
|
+
replyTo?: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Exact request data of one file upload. */
|
|
41
|
+
export interface OneBotFilePayload {
|
|
42
|
+
kind: 'file'
|
|
43
|
+
chat: ChatRef
|
|
44
|
+
/** The path the implementation reads, from {@link ResolvedResource.key}. */
|
|
45
|
+
key: string
|
|
46
|
+
name: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type OneBotPayload = OneBotMessagePayload | OneBotFilePayload
|
|
50
|
+
|
|
51
|
+
/** Platform node of the `segment` mode: one piece of the outbound segment stream. */
|
|
52
|
+
export type OneBotNode =
|
|
53
|
+
| { kind: 'segment'; segment: OneBotSegment }
|
|
54
|
+
| { kind: 'media'; type: 'image' | 'record' | 'video'; ref: ResourceRef }
|
|
55
|
+
| { kind: 'file'; ref: ResourceRef; name: string }
|
|
56
|
+
| { kind: 'newline' }
|
|
57
|
+
|
|
58
|
+
/** Mode types of the single `segment` mode. */
|
|
59
|
+
export interface OneBotModeTypes extends ModeTypes {
|
|
60
|
+
node: OneBotNode
|
|
61
|
+
draft: OneBotDraft
|
|
62
|
+
payload: OneBotPayload
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A text segment node. */
|
|
66
|
+
export function textNode(text: string): OneBotNode {
|
|
67
|
+
return { kind: 'segment', segment: { type: 'text', data: { text } } }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The accumulated text of a draft's last item, when it is a text segment. */
|
|
71
|
+
function trailingText(items: readonly OneBotItem[]): OneBotSegment | undefined {
|
|
72
|
+
const last = items[items.length - 1]
|
|
73
|
+
if (last === undefined || last.kind !== 'segment') return undefined
|
|
74
|
+
return last.segment.type === 'text' ? last.segment : undefined
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Drop the whitespace-only ends of a draft and the blank text segments around them. */
|
|
78
|
+
function trimItems(input: readonly OneBotItem[]): OneBotItem[] | undefined {
|
|
79
|
+
const items = [...input]
|
|
80
|
+
const textAt = (index: number): OneBotSegment | undefined => {
|
|
81
|
+
const item = items[index]
|
|
82
|
+
if (item === undefined || item.kind !== 'segment') return undefined
|
|
83
|
+
return item.segment.type === 'text' ? item.segment : undefined
|
|
84
|
+
}
|
|
85
|
+
while (items.length > 0) {
|
|
86
|
+
const first = textAt(0)
|
|
87
|
+
if (first === undefined) break
|
|
88
|
+
const text = String(first.data.text).trimStart()
|
|
89
|
+
if (text !== '') {
|
|
90
|
+
first.data.text = text
|
|
91
|
+
break
|
|
92
|
+
}
|
|
93
|
+
items.shift()
|
|
94
|
+
}
|
|
95
|
+
while (items.length > 0) {
|
|
96
|
+
const last = textAt(items.length - 1)
|
|
97
|
+
if (last === undefined) break
|
|
98
|
+
const text = String(last.data.text).trimEnd()
|
|
99
|
+
if (text !== '') {
|
|
100
|
+
last.data.text = text
|
|
101
|
+
break
|
|
102
|
+
}
|
|
103
|
+
items.pop()
|
|
104
|
+
}
|
|
105
|
+
return items.length > 0 ? items : undefined
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Fold the rendered node stream into platform messages.
|
|
110
|
+
*
|
|
111
|
+
* Consecutive inline nodes share one message segment array; `audio` / `video` / `file` start a new message first, as
|
|
112
|
+
* the upstream encoder flushes before them. Paragraph breaks collapse into one newline so `<p>a</p><p>b</p>` becomes
|
|
113
|
+
* `a\nb`. With `splitMixedContent` a space is kept between text and an image, in both directions. The ends of every
|
|
114
|
+
* message are trimmed and an all-whitespace message is dropped.
|
|
115
|
+
*
|
|
116
|
+
* @param nodes - rendered nodes in document order.
|
|
117
|
+
* @param splitMixedContent - the `advanced.splitMixedContent` setting.
|
|
118
|
+
* @returns one draft per platform message; empty when the content renders nothing.
|
|
119
|
+
*/
|
|
120
|
+
export function assembleDraft(
|
|
121
|
+
nodes: readonly OneBotNode[],
|
|
122
|
+
splitMixedContent: boolean,
|
|
123
|
+
): OneBotDraft[] {
|
|
124
|
+
const drafts: OneBotDraft[] = []
|
|
125
|
+
let items: OneBotItem[] = []
|
|
126
|
+
let pendingSpace = false
|
|
127
|
+
|
|
128
|
+
const flush = (): void => {
|
|
129
|
+
const trimmed = trimItems(items)
|
|
130
|
+
if (trimmed !== undefined) drafts.push({ kind: 'message', items: trimmed })
|
|
131
|
+
items = []
|
|
132
|
+
pendingSpace = false
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const pushText = (source: string): void => {
|
|
136
|
+
let text = source
|
|
137
|
+
if (text === '') return
|
|
138
|
+
if (pendingSpace) {
|
|
139
|
+
pendingSpace = false
|
|
140
|
+
if (!/^\s/.test(text)) text = ` ${text}`
|
|
141
|
+
}
|
|
142
|
+
const last = trailingText(items)
|
|
143
|
+
if (last === undefined) {
|
|
144
|
+
items.push({ kind: 'segment', segment: { type: 'text', data: { text } } })
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
last.data.text = `${String(last.data.text)}${text}`
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const ensureNewline = (): void => {
|
|
151
|
+
pendingSpace = false
|
|
152
|
+
const last = trailingText(items)
|
|
153
|
+
if (last === undefined) {
|
|
154
|
+
items.push({
|
|
155
|
+
kind: 'segment',
|
|
156
|
+
segment: { type: 'text', data: { text: '\n' } },
|
|
157
|
+
})
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
const text = String(last.data.text)
|
|
161
|
+
if (!text.endsWith('\n')) last.data.text = `${text}\n`
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const pushMedia = (
|
|
165
|
+
type: 'image' | 'record' | 'video',
|
|
166
|
+
ref: ResourceRef,
|
|
167
|
+
): void => {
|
|
168
|
+
if (splitMixedContent) {
|
|
169
|
+
const last = trailingText(items)
|
|
170
|
+
if (last !== undefined && !/\s$/.test(String(last.data.text))) {
|
|
171
|
+
last.data.text = `${String(last.data.text)} `
|
|
172
|
+
}
|
|
173
|
+
pendingSpace = true
|
|
174
|
+
}
|
|
175
|
+
items.push({ kind: 'media', type, ref })
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
for (const node of nodes) {
|
|
179
|
+
switch (node.kind) {
|
|
180
|
+
case 'newline':
|
|
181
|
+
ensureNewline()
|
|
182
|
+
break
|
|
183
|
+
case 'segment':
|
|
184
|
+
if (node.segment.type === 'text')
|
|
185
|
+
pushText(String(node.segment.data.text ?? ''))
|
|
186
|
+
else items.push({ kind: 'segment', segment: node.segment })
|
|
187
|
+
break
|
|
188
|
+
case 'media':
|
|
189
|
+
if (node.type !== 'image') flush()
|
|
190
|
+
pushMedia(node.type, node.ref)
|
|
191
|
+
break
|
|
192
|
+
case 'file':
|
|
193
|
+
flush()
|
|
194
|
+
drafts.push({ kind: 'file', ref: node.ref, name: node.name })
|
|
195
|
+
break
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
flush()
|
|
199
|
+
return drafts
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** The platform key `prepare` resolved for one placeholder. */
|
|
203
|
+
function resolvedKey(
|
|
204
|
+
prepared: Prepared<OneBotDraft>,
|
|
205
|
+
ref: ResourceRef,
|
|
206
|
+
): string {
|
|
207
|
+
const resolved = prepared.resolved.get(ref)
|
|
208
|
+
if (resolved === undefined) {
|
|
209
|
+
throw new Error('onebot: the resource was not prepared before serialize')
|
|
210
|
+
}
|
|
211
|
+
return resolved.key
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Build the exact request data of one draft.
|
|
216
|
+
*
|
|
217
|
+
* Nothing is escaped: the array message format carries every field value literally. Only the first part answers the
|
|
218
|
+
* quoted message, because a split message would otherwise repeat the quote on every part.
|
|
219
|
+
*
|
|
220
|
+
* @param prepared - the draft with its resources resolved.
|
|
221
|
+
* @param ctx - serialize context (target chat, part index, quote).
|
|
222
|
+
* @returns the request data of one platform message or file upload.
|
|
223
|
+
*/
|
|
224
|
+
export function serializeDraft(
|
|
225
|
+
prepared: Prepared<OneBotDraft>,
|
|
226
|
+
ctx: SerializeContext,
|
|
227
|
+
): OneBotPayload {
|
|
228
|
+
const { draft } = prepared
|
|
229
|
+
const chat = ctx.target.chat
|
|
230
|
+
if (draft.kind === 'file') {
|
|
231
|
+
return {
|
|
232
|
+
kind: 'file',
|
|
233
|
+
chat,
|
|
234
|
+
key: resolvedKey(prepared, draft.ref),
|
|
235
|
+
name: draft.name,
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
const replyTo =
|
|
239
|
+
ctx.index === 0 && ctx.quote !== undefined ? ctx.quote : undefined
|
|
240
|
+
const segments: OneBotSegment[] =
|
|
241
|
+
replyTo === undefined ? [] : [{ type: 'reply', data: { id: replyTo } }]
|
|
242
|
+
for (const item of draft.items) {
|
|
243
|
+
if (item.kind === 'segment') {
|
|
244
|
+
segments.push(item.segment)
|
|
245
|
+
} else {
|
|
246
|
+
segments.push({
|
|
247
|
+
type: item.type,
|
|
248
|
+
data: { file: resolvedKey(prepared, item.ref), cache: 0 },
|
|
249
|
+
})
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
kind: 'message',
|
|
254
|
+
chat,
|
|
255
|
+
segments,
|
|
256
|
+
...(replyTo === undefined ? {} : { replyTo }),
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* The file name of an outbound `<file>` element.
|
|
262
|
+
* @param name - the element's `name` attribute, when set.
|
|
263
|
+
* @param src - the element's source.
|
|
264
|
+
* @returns the declared name, else the last path segment of the source, else `file`.
|
|
265
|
+
*/
|
|
266
|
+
export function fileName(name: string | undefined, src: string): string {
|
|
267
|
+
if (name !== undefined && name !== '') return name
|
|
268
|
+
try {
|
|
269
|
+
const path = new URL(src).pathname
|
|
270
|
+
const segment = path.slice(path.lastIndexOf('/') + 1)
|
|
271
|
+
if (segment !== '') return decodeURIComponent(segment)
|
|
272
|
+
} catch {
|
|
273
|
+
// Not a URL: the source itself is the best name available.
|
|
274
|
+
if (src !== '' && !src.includes('/')) return src
|
|
275
|
+
}
|
|
276
|
+
return 'file'
|
|
277
|
+
}
|
package/src/request.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { Dict } from '@deepseek-ai/cosmokit'
|
|
2
|
+
import type { OneBotBot } from './bot.ts'
|
|
3
|
+
import type { Response as OneBotResponse } from './types.ts'
|
|
4
|
+
import { TimeoutError } from './types.ts'
|
|
5
|
+
|
|
6
|
+
/** One action call sent to the implementation. */
|
|
7
|
+
export interface ActionFrame {
|
|
8
|
+
action: string
|
|
9
|
+
params: Dict
|
|
10
|
+
/** Answer correlation id, unique per connection. */
|
|
11
|
+
echo: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface Pending {
|
|
15
|
+
resolve: (response: OneBotResponse) => void
|
|
16
|
+
reject: (error: Error) => void
|
|
17
|
+
timer: ReturnType<typeof setTimeout>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Action calls over one WebSocket connection.
|
|
22
|
+
*
|
|
23
|
+
* The implementation answers an action with the envelope it was sent, in `echo`; the pending table is per connection
|
|
24
|
+
* so a reconnect can never deliver an answer to the previous socket. A request that is not answered within
|
|
25
|
+
* `ws.responseTimeout` is rejected with the upstream `TimeoutError` and forgotten, and closing the connection rejects
|
|
26
|
+
* everything still waiting.
|
|
27
|
+
*/
|
|
28
|
+
export class ActionRequests {
|
|
29
|
+
private echo = 0
|
|
30
|
+
private readonly pending = new Map<number, Pending>()
|
|
31
|
+
private closed: Error | undefined
|
|
32
|
+
|
|
33
|
+
constructor(
|
|
34
|
+
private readonly send: (frame: ActionFrame) => void,
|
|
35
|
+
private readonly timeoutMs: number,
|
|
36
|
+
) {}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Send one action and wait for its answer.
|
|
40
|
+
* @param action - OneBot 11 action name (`send_group_msg`, …).
|
|
41
|
+
* @param params - action parameters.
|
|
42
|
+
* @returns the answer envelope (the caller reads `retcode` / `data`).
|
|
43
|
+
*/
|
|
44
|
+
request = (action: string, params: Dict): Promise<OneBotResponse> => {
|
|
45
|
+
if (this.closed !== undefined) return Promise.reject(this.closed)
|
|
46
|
+
const echo = ++this.echo
|
|
47
|
+
const frame: ActionFrame = { action, params, echo }
|
|
48
|
+
return new Promise<OneBotResponse>((resolve, reject) => {
|
|
49
|
+
const timer = setTimeout(() => {
|
|
50
|
+
this.pending.delete(echo)
|
|
51
|
+
reject(new TimeoutError(params, action))
|
|
52
|
+
}, this.timeoutMs)
|
|
53
|
+
this.pending.set(echo, { resolve, reject, timer })
|
|
54
|
+
try {
|
|
55
|
+
this.send(frame)
|
|
56
|
+
} catch (error) {
|
|
57
|
+
this.settle(echo, error)
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Deliver one answer to its waiting call.
|
|
64
|
+
* @param response - the parsed frame.
|
|
65
|
+
* @returns whether an outstanding call was waiting for it.
|
|
66
|
+
*/
|
|
67
|
+
resolve(response: OneBotResponse): boolean {
|
|
68
|
+
if (typeof response.echo !== 'number') return false
|
|
69
|
+
const pending = this.pending.get(response.echo)
|
|
70
|
+
if (pending === undefined) return false
|
|
71
|
+
this.pending.delete(response.echo)
|
|
72
|
+
clearTimeout(pending.timer)
|
|
73
|
+
pending.resolve(response)
|
|
74
|
+
return true
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Refuse every waiting call; calls made afterwards fail immediately.
|
|
79
|
+
* @param error - the failure to reject with.
|
|
80
|
+
*/
|
|
81
|
+
rejectAll(error: Error): void {
|
|
82
|
+
this.closed = error
|
|
83
|
+
for (const [echo, pending] of this.pending) {
|
|
84
|
+
this.pending.delete(echo)
|
|
85
|
+
clearTimeout(pending.timer)
|
|
86
|
+
pending.reject(error)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private settle(echo: number, error: unknown): void {
|
|
91
|
+
const pending = this.pending.get(echo)
|
|
92
|
+
if (pending === undefined) return
|
|
93
|
+
this.pending.delete(echo)
|
|
94
|
+
clearTimeout(pending.timer)
|
|
95
|
+
pending.reject(error instanceof Error ? error : new Error(String(error)))
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Make a connection's `request` the bot's action entry point (`bot.internal._request`).
|
|
101
|
+
* @param bot - the adapter instance.
|
|
102
|
+
* @param request - action caller of the live connection.
|
|
103
|
+
*/
|
|
104
|
+
export function installRequest(
|
|
105
|
+
bot: OneBotBot,
|
|
106
|
+
request: (action: string, params: Dict) => Promise<OneBotResponse>,
|
|
107
|
+
): void {
|
|
108
|
+
bot.internal._request = request
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Make every action call of the bot fail without a connection.
|
|
113
|
+
* @param bot - the adapter instance.
|
|
114
|
+
* @param error - the failure calls report.
|
|
115
|
+
*/
|
|
116
|
+
export function rejectRequests(bot: OneBotBot, error: Error): void {
|
|
117
|
+
bot.internal._request = () => Promise.reject(error)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Whether a parsed frame is one action answer.
|
|
122
|
+
* @param value - a parsed JSON frame from the implementation.
|
|
123
|
+
* @returns whether it carries an `echo`.
|
|
124
|
+
*/
|
|
125
|
+
export function isResponse(value: unknown): value is OneBotResponse {
|
|
126
|
+
if (typeof value !== 'object' || value === null) return false
|
|
127
|
+
return typeof (value as { echo?: unknown }).echo === 'number'
|
|
128
|
+
}
|