@im-more/dsh-kook 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 +12 -0
- package/lib/index.js +2380 -0
- package/lib/index.js.map +1 -0
- package/lib/types/bot.d.ts +53 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +138 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +46 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +52 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/decode.d.ts +88 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +564 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +46 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +159 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/identity.d.ts +17 -0
- package/lib/types/identity.d.ts.map +1 -0
- package/lib/types/identity.js +24 -0
- package/lib/types/identity.js.map +1 -0
- package/lib/types/index.d.ts +15 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +15 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/kmarkdown.d.ts +76 -0
- package/lib/types/kmarkdown.d.ts.map +1 -0
- package/lib/types/kmarkdown.js +201 -0
- package/lib/types/kmarkdown.js.map +1 -0
- package/lib/types/modes/kmarkdown.d.ts +17 -0
- package/lib/types/modes/kmarkdown.d.ts.map +1 -0
- package/lib/types/modes/kmarkdown.js +166 -0
- package/lib/types/modes/kmarkdown.js.map +1 -0
- package/lib/types/modes/shared.d.ts +192 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +371 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/request.d.ts +53 -0
- package/lib/types/request.d.ts.map +1 -0
- package/lib/types/request.js +147 -0
- package/lib/types/request.js.map +1 -0
- package/lib/types/send.d.ts +57 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +141 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/types.d.ts +807 -0
- package/lib/types/types.d.ts.map +1 -0
- package/lib/types/types.js +168 -0
- package/lib/types/types.js.map +1 -0
- package/lib/types/webhook.d.ts +35 -0
- package/lib/types/webhook.d.ts.map +1 -0
- package/lib/types/webhook.js +132 -0
- package/lib/types/webhook.js.map +1 -0
- package/lib/types/ws.d.ts +54 -0
- package/lib/types/ws.d.ts.map +1 -0
- package/lib/types/ws.js +358 -0
- package/lib/types/ws.js.map +1 -0
- package/package.json +61 -3
- package/src/bot.ts +217 -0
- package/src/config.ts +84 -0
- package/src/decode.ts +660 -0
- package/src/errors.ts +188 -0
- package/src/identity.ts +32 -0
- package/src/index.ts +80 -0
- package/src/kmarkdown.ts +246 -0
- package/src/modes/kmarkdown.ts +204 -0
- package/src/modes/shared.ts +498 -0
- package/src/request.ts +196 -0
- package/src/send.ts +216 -0
- package/src/types.ts +764 -0
- package/src/webhook.ts +170 -0
- package/src/ws.ts +431 -0
- package/README.md +0 -3
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Outbound hooks: the send pipeline's payload, deletion, member reads and message reads.
|
|
3
|
+
*
|
|
4
|
+
* A channel message goes to `/message/create` (or `/message/view`, `/message/delete`); a direct one goes to the
|
|
5
|
+
* upstream adapter's `/user-chat/*` routes. The two are told apart exactly as upstream tells them apart: a channel id
|
|
6
|
+
* is 16–19 digits and a direct chat code is 32 hexadecimal characters, so the longer id is the direct chat.
|
|
7
|
+
*/
|
|
8
|
+
import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core';
|
|
9
|
+
import { decodeMessage, userRefOf } from "./decode.js";
|
|
10
|
+
import { KOOK_LIMITS } from "./modes/shared.js";
|
|
11
|
+
import { invoke } from "./request.js";
|
|
12
|
+
/** Whether a chat id names a direct chat: a `chat_code` (32 hex characters) is longer than a channel id. */
|
|
13
|
+
export function isDirectChannel(chatId) {
|
|
14
|
+
return chatId.length > 30;
|
|
15
|
+
}
|
|
16
|
+
/** The platform path of one message operation, by chat kind. */
|
|
17
|
+
function pathOf(chatId, channel, direct) {
|
|
18
|
+
return isDirectChannel(chatId) ? direct : channel;
|
|
19
|
+
}
|
|
20
|
+
/** The published part of one sent message. */
|
|
21
|
+
function partOf(receipt, kind, target) {
|
|
22
|
+
if (receipt?.msg_id === undefined || receipt.msg_id === '') {
|
|
23
|
+
// The request reached the platform, so an answer without a receipt is a published outcome, not a refusal.
|
|
24
|
+
throw new PlatformError('invalid', 'kook: the platform answered without a msg_id', { published: 'unknown' });
|
|
25
|
+
}
|
|
26
|
+
const referrer = {
|
|
27
|
+
chatId: target.chat.id,
|
|
28
|
+
channelType: target.chat.type === 'group' ? 'GROUP' : 'PERSON',
|
|
29
|
+
messageId: receipt.msg_id,
|
|
30
|
+
};
|
|
31
|
+
return {
|
|
32
|
+
id: receipt.msg_id,
|
|
33
|
+
kind,
|
|
34
|
+
time: receipt.msg_timestamp === undefined ? Date.now() : receipt.msg_timestamp,
|
|
35
|
+
referrer,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Send one serialized payload.
|
|
40
|
+
*
|
|
41
|
+
* @param request - endpoints for this bot.
|
|
42
|
+
* @param payload - the serialized draft (`type: 9` text or `type: 10` card).
|
|
43
|
+
* @param target - the chat the payload goes to.
|
|
44
|
+
* @param _ctx - part index and quote, already applied by `serialize`.
|
|
45
|
+
* @returns the published part.
|
|
46
|
+
*/
|
|
47
|
+
export async function sendPayload(request, payload, target, _ctx) {
|
|
48
|
+
// KOOK chat messages have no threads: `thread: true` has nothing to follow (ignored), an explicit id is refused.
|
|
49
|
+
if (typeof target.thread === 'string') {
|
|
50
|
+
throw new PlatformError('invalid', 'kook has no message threads', {
|
|
51
|
+
published: 'no',
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
const direct = isDirectChannel(target.chat.id);
|
|
55
|
+
const body = {
|
|
56
|
+
type: payload.type,
|
|
57
|
+
content: payload.content,
|
|
58
|
+
};
|
|
59
|
+
if (direct)
|
|
60
|
+
body.chat_code = target.chat.id;
|
|
61
|
+
else
|
|
62
|
+
body.target_id = target.chat.id;
|
|
63
|
+
// `serialize` only puts the quote on the first part of a split message.
|
|
64
|
+
if (payload.quote !== undefined)
|
|
65
|
+
body.quote = payload.quote;
|
|
66
|
+
const receipt = await invoke(request, 'POST', direct ? '/user-chat/create-msg' : '/message/create', body);
|
|
67
|
+
return partOf(receipt, payload.kind, target);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Recall one published part.
|
|
71
|
+
* @param request - endpoints for this bot.
|
|
72
|
+
* @param part - the part to recall.
|
|
73
|
+
* @param handle - the message the part belongs to, which names the chat.
|
|
74
|
+
*/
|
|
75
|
+
export async function deletePart(request, part, handle) {
|
|
76
|
+
await invoke(request, 'POST', pathOf(handle.target.chat.id, '/message/delete', '/user-chat/delete-msg'), { msg_id: part.id });
|
|
77
|
+
}
|
|
78
|
+
/** The page number a cursor names; anything unreadable starts at the first page. */
|
|
79
|
+
function pageOf(cursor) {
|
|
80
|
+
const page = Number(cursor ?? '');
|
|
81
|
+
return Number.isFinite(page) && page >= 1 ? Math.floor(page) : 1;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* List the members of a chat.
|
|
85
|
+
*
|
|
86
|
+
* A group chat pages `/guild/user-list`; the guild id comes from `/channel/view` because the chat only knows its
|
|
87
|
+
* channel. A direct chat has exactly one member, which `/user-chat/view` reports.
|
|
88
|
+
*
|
|
89
|
+
* @param request - endpoints for this bot.
|
|
90
|
+
* @param chat - the chat the caller asked about.
|
|
91
|
+
* @param cursor - page number of the previous page's `next`.
|
|
92
|
+
* @param limit - page size the platform should use (at most 50).
|
|
93
|
+
* @returns one page of members.
|
|
94
|
+
*/
|
|
95
|
+
export async function members(request, chat, cursor, limit) {
|
|
96
|
+
if (isDirectChannel(chat.id)) {
|
|
97
|
+
const view = await invoke(request, 'GET', '/user-chat/view', {
|
|
98
|
+
chat_code: chat.id,
|
|
99
|
+
});
|
|
100
|
+
const target = view?.target_info;
|
|
101
|
+
return { data: target === undefined ? [] : [userRefOf(target)] };
|
|
102
|
+
}
|
|
103
|
+
const page = pageOf(cursor);
|
|
104
|
+
const pageSize = Math.max(1, Math.min(limit ?? KOOK_LIMITS.membersPageSize, KOOK_LIMITS.membersPageSize));
|
|
105
|
+
const channel = await invoke(request, 'GET', '/channel/view', {
|
|
106
|
+
target_id: chat.id,
|
|
107
|
+
});
|
|
108
|
+
if (channel?.guild_id === undefined || channel.guild_id === '') {
|
|
109
|
+
throw new PlatformError('not-found', `kook: channel ${chat.id} names no guild`, { published: 'no' });
|
|
110
|
+
}
|
|
111
|
+
const list = await invoke(request, 'GET', '/guild/user-list', {
|
|
112
|
+
guild_id: channel.guild_id,
|
|
113
|
+
page,
|
|
114
|
+
page_size: pageSize,
|
|
115
|
+
});
|
|
116
|
+
const data = list.items.map((member) => userRefOf(member));
|
|
117
|
+
const total = list.meta?.page_total ?? page;
|
|
118
|
+
return { data, ...(page < total ? { next: String(page + 1) } : {}) };
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Read one message by id.
|
|
122
|
+
*
|
|
123
|
+
* The read answer names the channel but not its type, so the requested chat is what the decoded message reports; the
|
|
124
|
+
* host verifies that its id matches the chat it asked about.
|
|
125
|
+
*
|
|
126
|
+
* @param request - endpoints for this bot.
|
|
127
|
+
* @param bot - the adapter, needed to decode the answer.
|
|
128
|
+
* @param chat - the chat the caller asked about.
|
|
129
|
+
* @param id - platform message id.
|
|
130
|
+
* @returns the decoded message.
|
|
131
|
+
*/
|
|
132
|
+
export async function getMessage(request, bot, chat, id) {
|
|
133
|
+
const path = isDirectChannel(chat.id) ? '/user-chat/view' : '/message/view';
|
|
134
|
+
// Both routes take `msg_id` for a message read; `chat_code`/`target_id` are the conversation reads.
|
|
135
|
+
const raw = await invoke(request, 'GET', path, { msg_id: id });
|
|
136
|
+
if (raw === undefined || raw === null || typeof raw !== 'object') {
|
|
137
|
+
throw new PlatformError('not-found', `kook: message ${id} is not in ${chat.id}`, { published: 'no' });
|
|
138
|
+
}
|
|
139
|
+
return decodeMessage(bot, raw, chat);
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=send.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"send.js","sourceRoot":"","sources":["../../src/send.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAA;AAYlE,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAEtD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAE/C,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAUrC,4GAA4G;AAC5G,MAAM,UAAU,eAAe,CAAC,MAAc;IAC5C,OAAO,MAAM,CAAC,MAAM,GAAG,EAAE,CAAA;AAC3B,CAAC;AAED,gEAAgE;AAChE,SAAS,MAAM,CAAC,MAAc,EAAE,OAAe,EAAE,MAAc;IAC7D,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAA;AACnD,CAAC;AAED,8CAA8C;AAC9C,SAAS,MAAM,CACb,OAAkC,EAClC,IAAY,EACZ,MAAc;IAEd,IAAI,OAAO,EAAE,MAAM,KAAK,SAAS,IAAI,OAAO,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;QAC3D,0GAA0G;QAC1G,MAAM,IAAI,aAAa,CACrB,SAAS,EACT,8CAA8C,EAC9C,EAAE,SAAS,EAAE,SAAS,EAAE,CACzB,CAAA;IACH,CAAC;IACD,MAAM,QAAQ,GAAiB;QAC7B,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE;QACtB,WAAW,EAAE,MAAM,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