@im-more/dsh-wecom 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.
Files changed (91) hide show
  1. package/LICENSE +21 -0
  2. package/cordis.patch.yml +13 -0
  3. package/lib/index.js +2077 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/types/bot.d.ts +50 -0
  6. package/lib/types/bot.d.ts.map +1 -0
  7. package/lib/types/bot.js +126 -0
  8. package/lib/types/bot.js.map +1 -0
  9. package/lib/types/config.d.ts +48 -0
  10. package/lib/types/config.d.ts.map +1 -0
  11. package/lib/types/config.js +68 -0
  12. package/lib/types/config.js.map +1 -0
  13. package/lib/types/crypto.d.ts +69 -0
  14. package/lib/types/crypto.d.ts.map +1 -0
  15. package/lib/types/crypto.js +129 -0
  16. package/lib/types/crypto.js.map +1 -0
  17. package/lib/types/decode.d.ts +58 -0
  18. package/lib/types/decode.d.ts.map +1 -0
  19. package/lib/types/decode.js +182 -0
  20. package/lib/types/decode.js.map +1 -0
  21. package/lib/types/errors.d.ts +67 -0
  22. package/lib/types/errors.d.ts.map +1 -0
  23. package/lib/types/errors.js +170 -0
  24. package/lib/types/errors.js.map +1 -0
  25. package/lib/types/identity.d.ts +20 -0
  26. package/lib/types/identity.d.ts.map +1 -0
  27. package/lib/types/identity.js +34 -0
  28. package/lib/types/identity.js.map +1 -0
  29. package/lib/types/index.d.ts +19 -0
  30. package/lib/types/index.d.ts.map +1 -0
  31. package/lib/types/index.js +18 -0
  32. package/lib/types/index.js.map +1 -0
  33. package/lib/types/internal.d.ts +154 -0
  34. package/lib/types/internal.d.ts.map +1 -0
  35. package/lib/types/internal.js +115 -0
  36. package/lib/types/internal.js.map +1 -0
  37. package/lib/types/media.d.ts +44 -0
  38. package/lib/types/media.d.ts.map +1 -0
  39. package/lib/types/media.js +116 -0
  40. package/lib/types/media.js.map +1 -0
  41. package/lib/types/modes/markdown.d.ts +20 -0
  42. package/lib/types/modes/markdown.d.ts.map +1 -0
  43. package/lib/types/modes/markdown.js +205 -0
  44. package/lib/types/modes/markdown.js.map +1 -0
  45. package/lib/types/modes/shared.d.ts +227 -0
  46. package/lib/types/modes/shared.d.ts.map +1 -0
  47. package/lib/types/modes/shared.js +487 -0
  48. package/lib/types/modes/shared.js.map +1 -0
  49. package/lib/types/request.d.ts +55 -0
  50. package/lib/types/request.d.ts.map +1 -0
  51. package/lib/types/request.js +202 -0
  52. package/lib/types/request.js.map +1 -0
  53. package/lib/types/send.d.ts +54 -0
  54. package/lib/types/send.d.ts.map +1 -0
  55. package/lib/types/send.js +124 -0
  56. package/lib/types/send.js.map +1 -0
  57. package/lib/types/token.d.ts +34 -0
  58. package/lib/types/token.d.ts.map +1 -0
  59. package/lib/types/token.js +61 -0
  60. package/lib/types/token.js.map +1 -0
  61. package/lib/types/transport/webhook.d.ts +41 -0
  62. package/lib/types/transport/webhook.d.ts.map +1 -0
  63. package/lib/types/transport/webhook.js +216 -0
  64. package/lib/types/transport/webhook.js.map +1 -0
  65. package/lib/types/types.d.ts +58 -0
  66. package/lib/types/types.d.ts.map +1 -0
  67. package/lib/types/types.js +2 -0
  68. package/lib/types/types.js.map +1 -0
  69. package/lib/types/xml.d.ts +40 -0
  70. package/lib/types/xml.d.ts.map +1 -0
  71. package/lib/types/xml.js +82 -0
  72. package/lib/types/xml.js.map +1 -0
  73. package/package.json +65 -3
  74. package/src/bot.ts +195 -0
  75. package/src/config.ts +117 -0
  76. package/src/crypto.ts +172 -0
  77. package/src/decode.ts +238 -0
  78. package/src/errors.ts +199 -0
  79. package/src/identity.ts +41 -0
  80. package/src/index.ts +101 -0
  81. package/src/internal.ts +237 -0
  82. package/src/media.ts +157 -0
  83. package/src/modes/markdown.ts +263 -0
  84. package/src/modes/shared.ts +618 -0
  85. package/src/request.ts +291 -0
  86. package/src/send.ts +172 -0
  87. package/src/token.ts +75 -0
  88. package/src/transport/webhook.ts +292 -0
  89. package/src/types.ts +74 -0
  90. package/src/xml.ts +89 -0
  91. package/README.md +0 -3
@@ -0,0 +1,68 @@
1
+ import z from '@deepseek-ai/schemastery';
2
+ /** Validated WeCom adapter configuration. */
3
+ export const Config = z.object({
4
+ corpId: z.string().required(),
5
+ agentId: z.string().required(),
6
+ secret: z.string().role('secret').required(),
7
+ token: z.string().role('secret').required(),
8
+ aesKey: z.string().role('secret').required(),
9
+ path: z.string().default('/wecom'),
10
+ apiBaseUrl: z.string().role('url').default('https://qyapi.weixin.qq.com'),
11
+ });
12
+ /** Length of an `EncodingAESKey`, which is the Base64 form of the 32-byte AES key without its padding. */
13
+ export const AES_KEY_CHARS = 43;
14
+ /** Maximum length of a callback `Token` the platform accepts. */
15
+ export const TOKEN_MAX_CHARS = 32;
16
+ /** The alphanumerical alphabet both callback credentials are drawn from. */
17
+ const CREDENTIAL_ALPHABET = /^[0-9A-Za-z]+$/;
18
+ /**
19
+ * Normalize a route path: absolute, no trailing slash.
20
+ *
21
+ * @param path - a configured path.
22
+ * @returns the pathname to register on the web server.
23
+ */
24
+ export function routePath(path) {
25
+ const withSlash = path.startsWith('/') ? path : `/${path}`;
26
+ return withSlash.length > 1 ? withSlash.replace(/\/+$/, '') : withSlash;
27
+ }
28
+ /**
29
+ * The configuration the adapter cannot work from, as far as it can tell locally.
30
+ *
31
+ * The check runs when the bot starts, not on the first callback: the callback is the only inbound path, so a profile
32
+ * that cannot answer one has no way to receive anything.
33
+ *
34
+ * @param config - the validated configuration.
35
+ * @returns the configuration error, or undefined when the configuration is usable.
36
+ */
37
+ export function configError(config) {
38
+ if (config.corpId === '') {
39
+ return new Error('wecom: corpId is required (the enterprise id "ww…" of 我的企业 → 企业信息)');
40
+ }
41
+ if (config.agentId === '') {
42
+ return new Error('wecom: agentId is required (the AgentID of the self-built application)');
43
+ }
44
+ if (!/^[1-9][0-9]*$/.test(config.agentId)) {
45
+ return new Error(`wecom: agentId must be a positive integer string, got "${config.agentId}" (message/send takes the numeric AgentID)`);
46
+ }
47
+ if (config.secret === '') {
48
+ return new Error('wecom: secret is required (the application secret, which every access_token is minted with)');
49
+ }
50
+ if (config.token === '') {
51
+ return new Error('wecom: token is required (the callback Token of 接收消息 → 设置API接收)');
52
+ }
53
+ if (config.token.length > TOKEN_MAX_CHARS) {
54
+ return new Error(`wecom: token must be at most ${TOKEN_MAX_CHARS} characters, got ${config.token.length}`);
55
+ }
56
+ if (config.aesKey === '') {
57
+ return new Error('wecom: aesKey is required (the EncodingAESKey of 接收消息 → 设置API接收)');
58
+ }
59
+ if (config.aesKey.length !== AES_KEY_CHARS ||
60
+ !CREDENTIAL_ALPHABET.test(config.aesKey)) {
61
+ return new Error(`wecom: aesKey must be exactly ${AES_KEY_CHARS} letters or digits, got ${config.aesKey.length} characters`);
62
+ }
63
+ if (!config.apiBaseUrl.startsWith('https://')) {
64
+ return new Error(`wecom: apiBaseUrl must be an https URL, got "${config.apiBaseUrl}"`);
65
+ }
66
+ return undefined;
67
+ }
68
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,CAAC,MAAM,0BAA0B,CAAA;AA0BxC,6CAA6C;AAC7C,MAAM,CAAC,MAAM,MAAM,GAAc,CAAC,CAAC,MAAM,CAAC;IACxC,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC7B,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC9B,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,QAAQ,EAAE;IAC5C,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,QAAQ,EAAE;IAC3C,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,QAAQ,EAAE;IAC5C,IAAI,EAAE,CAAC,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F,CAAA;IACH,CAAC;IACD,IAAI,MAAM,CAAC,KAAK,KAAK,EAAE,EAAE,CAAC;QACxB,OAAO,IAAI,KAAK,CACd,iEAAiE,CAClE,CAAA;IACH,CAAC;IACD,IAAI,MAAM,CAAC,KAAK,CAAC,MAAM,GAAG,eAAe,EAAE,CAAC;QAC1C,OAAO,IAAI,KAAK,CACd,gCAAgC,eAAe,oBAAoB,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,CACzF,CAAA;IACH,CAAC;IACD,IAAI,MAAM,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;QACzB,OAAO,IAAI,KAAK,CACd,kEAAkE,CACnE,CAAA;IACH,CAAC;IACD,IACE,MAAM,CAAC,MAAM,CAAC,MAAM,KAAK,aAAa;QACtC,CAAC,mBAAmB,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,EACxC,CAAC;QACD,OAAO,IAAI,KAAK,CACd,iCAAiC,aAAa,2BAA2B,MAAM,CAAC,MAAM,CAAC,MAAM,aAAa,CAC3G,CAAA;IACH,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;QAC9C,OAAO,IAAI,KAAK,CACd,gDAAgD,MAAM,CAAC,UAAU,GAAG,CACrE,CAAA;IACH,CAAC;IACD,OAAO,SAAS,CAAA;AAClB,CAAC"}
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Callback authentication and decryption, implemented on `node:crypto`.
3
+ *
4
+ * The platform hands every callback over in one encrypted envelope, and the adapter is the only place that can say
5
+ * whether the bytes came from the platform:
6
+ *
7
+ * - `msg_signature = sha1(sort(token, timestamp, nonce, msg_encrypt).join(''))`, compared in constant time;
8
+ * - `AES-256-CBC` keyed by `Base64_Decode(EncodingAESKey + "=")` with `IV = AESKey[0:16]` and PKCS#7 padding to
9
+ * **32-byte** blocks (not the usual 16);
10
+ * - the plaintext is `random(16) + msg_len(4, big endian) + msg + receiveid`, and `receiveid` must equal `corpId` for an
11
+ * application callback.
12
+ *
13
+ * `tests/crypto.spec.ts` proves the three steps against the vectors the platform documents.
14
+ */
15
+ /** The padding block size this platform uses: 32 bytes, not the 16 of a plain AES-CBC block. */
16
+ export declare const PKCS7_BLOCK_BYTES = 32;
17
+ /** One callback payload that could not be decrypted. */
18
+ export declare class WecomCryptoError extends Error {
19
+ constructor(message: string, options?: ErrorOptions);
20
+ }
21
+ /**
22
+ * Decode an `EncodingAESKey` into the AES key it stands for.
23
+ *
24
+ * The platform stores the Base64 key without its `=` padding, so the padding is restored before decoding.
25
+ *
26
+ * @param aesKey - the configured 43-character `EncodingAESKey`.
27
+ * @returns the 32-byte AES key.
28
+ * @throws WecomCryptoError when the decoded key is not 32 bytes.
29
+ */
30
+ export declare function aesKeyOf(aesKey: string): Buffer;
31
+ /**
32
+ * The `msg_signature` of one callback.
33
+ *
34
+ * The four components are sorted lexicographically as strings and concatenated without a separator, then hashed with
35
+ * SHA-1 and printed as lowercase hex.
36
+ *
37
+ * @param token - the configured callback token.
38
+ * @param timestamp - the `timestamp` the platform sent.
39
+ * @param nonce - the `nonce` the platform sent.
40
+ * @param encrypt - the `echostr` or outer `Encrypt` value the signature covers.
41
+ * @returns the lowercase hexadecimal SHA-1 digest.
42
+ */
43
+ export declare function signatureOf(token: string, timestamp: string, nonce: string, encrypt: string): string;
44
+ /**
45
+ * Compare a computed signature with the one the request carried without leaking where they differ.
46
+ *
47
+ * @param expected - the signature computed from the request.
48
+ * @param received - the `msg_signature` parameter, if the request carried one.
49
+ * @returns whether the request signature matches.
50
+ */
51
+ export declare function matchesSignature(expected: string, received: string | undefined): boolean;
52
+ /** One decrypted callback payload: the message XML and the id the key belongs to. */
53
+ export interface DecryptedPayload {
54
+ /** The message body, which is itself an `<xml>` document (or the `echostr` plaintext). */
55
+ message: string;
56
+ /** `corpId` for an application callback; compared with the configured enterprise id before the body is read. */
57
+ receiveId: string;
58
+ }
59
+ /**
60
+ * Decrypt one callback payload.
61
+ *
62
+ * @param aesKey - the configured `EncodingAESKey`.
63
+ * @param encrypt - the Base64 ciphertext the callback carried.
64
+ * @returns the message and the `receiveid` it was encrypted for.
65
+ * @throws WecomCryptoError when the ciphertext is malformed, the padding is inconsistent, or the announced length
66
+ * exceeds the plaintext.
67
+ */
68
+ export declare function decryptPayload(aesKey: string, encrypt: string): DecryptedPayload;
69
+ //# sourceMappingURL=crypto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAUH,gGAAgG;AAChG,eAAO,MAAM,iBAAiB,KAAK,CAAA;AAQnC,wDAAwD;AACxD,qBAAa,gBAAiB,SAAQ,KAAK;gBAC7B,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,YAAiB;CAIxD;AAED;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAQ/C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CACzB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,GACd,MAAM,CAIR;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GAAG,SAAS,GAC3B,OAAO,CAMT;AAED,qFAAqF;AACrF,MAAM,WAAW,gBAAgB;IAC/B,0FAA0F;IAC1F,OAAO,EAAE,MAAM,CAAA;IACf,gHAAgH;IAChH,SAAS,EAAE,MAAM,CAAA;CAClB;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,GACd,gBAAgB,CAmDlB"}
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Callback authentication and decryption, implemented on `node:crypto`.
3
+ *
4
+ * The platform hands every callback over in one encrypted envelope, and the adapter is the only place that can say
5
+ * whether the bytes came from the platform:
6
+ *
7
+ * - `msg_signature = sha1(sort(token, timestamp, nonce, msg_encrypt).join(''))`, compared in constant time;
8
+ * - `AES-256-CBC` keyed by `Base64_Decode(EncodingAESKey + "=")` with `IV = AESKey[0:16]` and PKCS#7 padding to
9
+ * **32-byte** blocks (not the usual 16);
10
+ * - the plaintext is `random(16) + msg_len(4, big endian) + msg + receiveid`, and `receiveid` must equal `corpId` for an
11
+ * application callback.
12
+ *
13
+ * `tests/crypto.spec.ts` proves the three steps against the vectors the platform documents.
14
+ */
15
+ import { createDecipheriv, createHash, timingSafeEqual } from 'node:crypto';
16
+ /** Length of the decoded AES key. */
17
+ const AES_KEY_BYTES = 32;
18
+ /** Length of the initialisation vector, which is the first part of the key. */
19
+ const IV_BYTES = 16;
20
+ /** The padding block size this platform uses: 32 bytes, not the 16 of a plain AES-CBC block. */
21
+ export const PKCS7_BLOCK_BYTES = 32;
22
+ /** Length of the random prefix of the plaintext. */
23
+ const RANDOM_BYTES = 16;
24
+ /** Length of the big-endian message length between the random prefix and the message. */
25
+ const LENGTH_BYTES = 4;
26
+ /** One callback payload that could not be decrypted. */
27
+ export class WecomCryptoError extends Error {
28
+ constructor(message, options = {}) {
29
+ super(message, options);
30
+ this.name = 'WecomCryptoError';
31
+ }
32
+ }
33
+ /**
34
+ * Decode an `EncodingAESKey` into the AES key it stands for.
35
+ *
36
+ * The platform stores the Base64 key without its `=` padding, so the padding is restored before decoding.
37
+ *
38
+ * @param aesKey - the configured 43-character `EncodingAESKey`.
39
+ * @returns the 32-byte AES key.
40
+ * @throws WecomCryptoError when the decoded key is not 32 bytes.
41
+ */
42
+ export function aesKeyOf(aesKey) {
43
+ const key = Buffer.from(`${aesKey}=`, 'base64');
44
+ if (key.length !== AES_KEY_BYTES) {
45
+ throw new WecomCryptoError(`wecom: the EncodingAESKey decodes to ${key.length} bytes, expected ${AES_KEY_BYTES}`);
46
+ }
47
+ return key;
48
+ }
49
+ /**
50
+ * The `msg_signature` of one callback.
51
+ *
52
+ * The four components are sorted lexicographically as strings and concatenated without a separator, then hashed with
53
+ * SHA-1 and printed as lowercase hex.
54
+ *
55
+ * @param token - the configured callback token.
56
+ * @param timestamp - the `timestamp` the platform sent.
57
+ * @param nonce - the `nonce` the platform sent.
58
+ * @param encrypt - the `echostr` or outer `Encrypt` value the signature covers.
59
+ * @returns the lowercase hexadecimal SHA-1 digest.
60
+ */
61
+ export function signatureOf(token, timestamp, nonce, encrypt) {
62
+ return createHash('sha1')
63
+ .update([token, timestamp, nonce, encrypt].sort().join(''))
64
+ .digest('hex');
65
+ }
66
+ /**
67
+ * Compare a computed signature with the one the request carried without leaking where they differ.
68
+ *
69
+ * @param expected - the signature computed from the request.
70
+ * @param received - the `msg_signature` parameter, if the request carried one.
71
+ * @returns whether the request signature matches.
72
+ */
73
+ export function matchesSignature(expected, received) {
74
+ if (received === undefined)
75
+ return false;
76
+ const left = Buffer.from(expected, 'utf8');
77
+ const right = Buffer.from(received, 'utf8');
78
+ if (left.length !== right.length)
79
+ return false;
80
+ return timingSafeEqual(left, right);
81
+ }
82
+ /**
83
+ * Decrypt one callback payload.
84
+ *
85
+ * @param aesKey - the configured `EncodingAESKey`.
86
+ * @param encrypt - the Base64 ciphertext the callback carried.
87
+ * @returns the message and the `receiveid` it was encrypted for.
88
+ * @throws WecomCryptoError when the ciphertext is malformed, the padding is inconsistent, or the announced length
89
+ * exceeds the plaintext.
90
+ */
91
+ export function decryptPayload(aesKey, encrypt) {
92
+ const key = aesKeyOf(aesKey);
93
+ const cipher = Buffer.from(encrypt.trim(), 'base64');
94
+ if (cipher.length === 0 || cipher.length % IV_BYTES !== 0) {
95
+ throw new WecomCryptoError(`wecom: the callback ciphertext is ${cipher.length} bytes, which is not a whole number of AES blocks`);
96
+ }
97
+ const decipher = createDecipheriv('aes-256-cbc', key, key.subarray(0, IV_BYTES));
98
+ decipher.setAutoPadding(false);
99
+ let padded;
100
+ try {
101
+ padded = Buffer.concat([decipher.update(cipher), decipher.final()]);
102
+ }
103
+ catch (cause) {
104
+ throw new WecomCryptoError('wecom: the callback payload could not be decrypted', { cause });
105
+ }
106
+ if (padded.length === 0 || padded.length % PKCS7_BLOCK_BYTES !== 0) {
107
+ throw new WecomCryptoError(`wecom: the decrypted payload is ${padded.length} bytes, which is not padded to ${PKCS7_BLOCK_BYTES}-byte blocks`);
108
+ }
109
+ const padding = padded[padded.length - 1];
110
+ if (padding < 1 || padding > PKCS7_BLOCK_BYTES || padding > padded.length) {
111
+ throw new WecomCryptoError(`wecom: the callback padding byte is ${padding}`);
112
+ }
113
+ const plaintext = padded.subarray(0, padded.length - padding);
114
+ if (plaintext.length < RANDOM_BYTES + LENGTH_BYTES) {
115
+ throw new WecomCryptoError(`wecom: the decrypted payload is ${plaintext.length} bytes, shorter than its own header`);
116
+ }
117
+ const length = plaintext.readUInt32BE(RANDOM_BYTES);
118
+ const end = RANDOM_BYTES + LENGTH_BYTES + length;
119
+ if (end > plaintext.length) {
120
+ throw new WecomCryptoError(`wecom: the decrypted payload announces ${length} bytes of message but carries ${plaintext.length - RANDOM_BYTES - LENGTH_BYTES}`);
121
+ }
122
+ return {
123
+ message: plaintext
124
+ .subarray(RANDOM_BYTES + LENGTH_BYTES, end)
125
+ .toString('utf8'),
126
+ receiveId: plaintext.subarray(end).toString('utf8'),
127
+ };
128
+ }
129
+ //# sourceMappingURL=crypto.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAE3E,qCAAqC;AACrC,MAAM,aAAa,GAAG,EAAE,CAAA;AAExB,+EAA+E;AAC/E,MAAM,QAAQ,GAAG,EAAE,CAAA;AAEnB,gGAAgG;AAChG,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAA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CAClH,CAAA;IACH,CAAC;IACD,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAE,CAAA;IAC1C,IAAI,OAAO,GAAG,CAAC,IAAI,OAAO,GAAG,iBAAiB,IAAI,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;QAC1E,MAAM,IAAI,gBAAgB,CAAC,uCAAuC,OAAO,EAAE,CAAC,CAAA;IAC9E,CAAC;IACD,MAAM,SAAS,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC,MAAM,GAAG,OAAO,CAAC,CAAA;IAC7D,IAAI,SAAS,CAAC,MAAM,GAAG,YAAY,GAAG,YAAY,EAAE,CAAC;QACnD,MAAM,IAAI,gBAAgB,CACxB,mCAAmC,SAAS,CAAC,MAAM,qCAAqC,CACzF,CAAA;IACH,CAAC;IACD,MAAM,MAAM,GAAG,SAAS,CAAC,YAAY,CAAC,YAAY,CAAC,CAAA;IACnD,MAAM,GAAG,GAAG,YAAY,GAAG,YAAY,GAAG,MAAM,CAAA;IAChD,IAAI,GAAG,GAAG,SAAS,CAAC,MAAM,EAAE,CAAC;QAC3B,MAAM,IAAI,gBAAgB,CACxB,0CAA0C,MAAM,iCAAiC,SAAS,CAAC,MAAM,GAAG,YAAY,GAAG,YAAY,EAAE,CAClI,CAAA;IACH,CAAC;IACD,OAAO;QACL,OAAO,EAAE,SAAS;aACf,QAAQ,CAAC,YAAY,GAAG,YAAY,EAAE,GAAG,CAAC;aAC1C,QAAQ,CAAC,MAAM,CAAC;QACnB,SAAS,EAAE,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC;KACpD,CAAA;AACH,CAAC"}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Inbound decoding: one decrypted callback becomes an intake `Session` (`message`) or a button interaction.
3
+ *
4
+ * An application message arrives from exactly one member in exactly one conversation — the member's direct chat with the
5
+ * application — so `chat.type` is always `direct`, `chat.id` is the sender's userid, and there is no mention, thread or
6
+ * quote to decode. The content of a media message is the bot's own `satori:` route, because the callback carries only a
7
+ * `media_id` and the bytes have to be fetched with the application's credentials.
8
+ */
9
+ import type { ChatRef, Session, UserRef } from '@deepseek-ai/cordis-plugin-im-core';
10
+ import type { WecomBot } from './bot.ts';
11
+ import type { XmlFields } from './xml.ts';
12
+ /** Adapter-private context of a decoded message, carried through `Target.referrer`. */
13
+ export interface WecomReferrer {
14
+ corpId: string;
15
+ agentId: string;
16
+ userId: string;
17
+ msgId: string;
18
+ }
19
+ /** Adapter-private context of a card click, carried through the button's referrer. */
20
+ export interface WecomCardReferrer {
21
+ taskId: string;
22
+ cardType?: string;
23
+ responseCode?: string;
24
+ userId: string;
25
+ agentId: string;
26
+ }
27
+ /** The chat of a callback sender: the application has only one-to-one conversations. */
28
+ export declare function chatOf(userId: string): ChatRef;
29
+ /** The sender of a callback: the application never learns a display name without another request. */
30
+ export declare function senderOf(userId: string): UserRef;
31
+ /**
32
+ * The `satori:` source of one inbound media object.
33
+ *
34
+ * @param bot - the receiving adapter.
35
+ * @param mediaId - the platform `MediaId`.
36
+ * @returns the internal URL the host downloads through.
37
+ */
38
+ export declare function mediaSource(bot: WecomBot, mediaId: string): string;
39
+ /**
40
+ * The text of one location message.
41
+ *
42
+ * The element vocabulary has no location tag, so the place arrives as a readable line.
43
+ *
44
+ * @param fields - the decrypted message.
45
+ * @returns `[位置] Label (X,Y)`, without the label when the message carries none.
46
+ */
47
+ export declare function locationText(fields: XmlFields): string;
48
+ /** Decode one `template_card_event` into the button session the callback registry looks up. */
49
+ export declare function decodeTemplateCardEvent(bot: WecomBot, fields: XmlFields): Session;
50
+ /**
51
+ * Decode one decrypted callback.
52
+ *
53
+ * @param bot - the receiving adapter.
54
+ * @param fields - the decrypted message fields.
55
+ * @returns the session to publish, or undefined for a message this adapter drops.
56
+ */
57
+ export declare function decodeMessage(bot: WecomBot, fields: XmlFields): Session | undefined;
58
+ //# sourceM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G,SAAS,CA2CrB"}
@@ -0,0 +1,182 @@
1
+ /**
2
+ * Inbound decoding: one decrypted callback becomes an intake `Session` (`message`) or a button interaction.
3
+ *
4
+ * An application message arrives from exactly one member in exactly one conversation — the member's direct chat with the
5
+ * application — so `chat.type` is always `direct`, `chat.id` is the sender's userid, and there is no mention, thread or
6
+ * quote to decode. The content of a media message is the bot's own `satori:` route, because the callback carries only a
7
+ * `media_id` and the bytes have to be fetched with the application's credentials.
8
+ */
9
+ import { h, normalize } from '@deepseek-ai/satori-element';
10
+ /** The chat of a callback sender: the application has only one-to-one conversations. */
11
+ export function chatOf(userId) {
12
+ return { id: userId, type: 'direct' };
13
+ }
14
+ /** The sender of a callback: the application never learns a display name without another request. */
15
+ export function senderOf(userId) {
16
+ return { id: userId, isBot: false };
17
+ }
18
+ /**
19
+ * The millisecond time of a callback.
20
+ *
21
+ * `CreateTime` is in seconds; a body without a usable value falls back to the receipt time rather than to `NaN`.
22
+ */
23
+ function timeOf(fields) {
24
+ const seconds = Number(fields.CreateTime);
25
+ return Number.isFinite(seconds) ? seconds * 1000 : Date.now();
26
+ }
27
+ /**
28
+ * The `satori:` source of one inbound media object.
29
+ *
30
+ * @param bot - the receiving adapter.
31
+ * @param mediaId - the platform `MediaId`.
32
+ * @returns the internal URL the host downloads through.
33
+ */
34
+ export function mediaSource(bot, mediaId) {
35
+ return bot.getInternalUrl(`/assets/${encodeURIComponent(mediaId)}`);
36
+ }
37
+ /**
38
+ * The text of one location message.
39
+ *
40
+ * The element vocabulary has no location tag, so the place arrives as a readable line.
41
+ *
42
+ * @param fields - the decrypted message.
43
+ * @returns `[位置] Label (X,Y)`, without the label when the message carries none.
44
+ */
45
+ export function locationText(fields) {
46
+ const label = fields.Label?.trim() ?? '';
47
+ const x = fields.Location_X ?? '';
48
+ const y = fields.Location_Y ?? '';
49
+ return `[位置]${label === '' ? '' : ` ${label}`} (${x},${y})`;
50
+ }
51
+ /** The elements of one message, or undefined for a `MsgType` this adapter does not decode. */
52
+ function contentOf(bot, fields, msgType) {
53
+ switch (msgType) {
54
+ case 'text':
55
+ return [fields.Content ?? ''];
56
+ case 'image': {
57
+ const mediaId = fields.MediaId ?? '';
58
+ return mediaId === ''
59
+ ? []
60
+ : [h('img', { src: mediaSource(bot, mediaId) })];
61
+ }
62
+ case 'voice': {
63
+ const mediaId = fields.MediaId ?? '';
64
+ return mediaId === ''
65
+ ? []
66
+ : [h('audio', { src: mediaSource(bot, mediaId) })];
67
+ }
68
+ case 'video': {
69
+ const mediaId = fields.MediaId ?? '';
70
+ return mediaId === ''
71
+ ? []
72
+ : [h('video', { src: mediaSource(bot, mediaId) })];
73
+ }
74
+ case 'location':
75
+ return [h('p', locationText(fields))];
76
+ case 'link': {
77
+ const url = fields.Url ?? '';
78
+ if (url === '')
79
+ return [];
80
+ const title = fields.Title?.trim() ?? '';
81
+ const description = fields.Description?.trim() ?? '';
82
+ return [
83
+ h('p', h('a', { href: url }, title === '' ? url : title), ...(description === '' ? [] : ['\n', description])),
84
+ ];
85
+ }
86
+ default:
87
+ return undefined;
88
+ }
89
+ }
90
+ /** Decode one `template_card_event` into the button session the callback registry looks up. */
91
+ export function decodeTemplateCardEvent(bot, fields) {
92
+ const userId = fields.FromUserName ?? '';
93
+ const chat = chatOf(userId);
94
+ const time = timeOf(fields);
95
+ const referrer = {
96
+ taskId: fields.TaskId ?? '',
97
+ ...(fields.CardType === undefined ? {} : { cardType: fields.CardType }),
98
+ ...(fields.ResponseCode === undefined
99
+ ? {}
100
+ : { responseCode: fields.ResponseCode }),
101
+ userId,
102
+ agentId: bot.config.agentId,
103
+ };
104
+ const button = {
105
+ bot,
106
+ id: fields.EventKey ?? '',
107
+ user: senderOf(userId),
108
+ chat,
109
+ referrer,
110
+ // The click has no platform time of its own beyond the envelope, and the platform answers nothing back here.
111
+ time: Date.now(),
112
+ };
113
+ return bot.session({
114
+ type: 'interaction/button',
115
+ // A click carries no `MsgId`, so the dedupe id is composed of the card, the button and the callback time.
116
+ id: `${fields.TaskId ?? ''}:${fields.EventKey ?? ''}:${fields.CreateTime ?? ''}`,
117
+ timestamp: time,
118
+ button,
119
+ raw: fields,
120
+ });
121
+ }
122
+ /** Decode one event message, or drop it when the contract has no consumer for it. */
123
+ function decodeEvent(bot, fields) {
124
+ switch (fields.Event) {
125
+ case 'template_card_event':
126
+ return decodeTemplateCardEvent(bot, fields);
127
+ default:
128
+ // Lifecycle events (`subscribe`, `unsubscribe`, `enter_agent`), menu events, `taskcard_click` and
129
+ // `template_card_menu_event` all arrive here: the IM contract has no "someone opened the application" or "a menu
130
+ // was used" event, and mapping them onto `interaction/button` would produce ids nothing registered.
131
+ bot.ctx.logger.debug('wecom: ignoring a %s event', fields.Event ?? '(none)');
132
+ return undefined;
133
+ }
134
+ }
135
+ /**
136
+ * Decode one decrypted callback.
137
+ *
138
+ * @param bot - the receiving adapter.
139
+ * @param fields - the decrypted message fields.
140
+ * @returns the session to publish, or undefined for a message this adapter drops.
141
+ */
142
+ export function decodeMessage(bot, fields) {
143
+ const msgType = fields.MsgType ?? '';
144
+ if (msgType === 'event')
145
+ return decodeEvent(bot, fields);
146
+ const nodes = contentOf(bot, fields, msgType);
147
+ if (nodes === undefined) {
148
+ bot.ctx.logger.debug('wecom: ignoring a message of type %s', msgType === '' ? '(none)' : msgType);
149
+ return undefined;
150
+ }
151
+ const userId = fields.FromUserName ?? '';
152
+ const msgId = fields.MsgId ?? '';
153
+ const time = timeOf(fields);
154
+ const content = normalize(nodes.length > 0
155
+ ? nodes
156
+ : [h('raw', { platform: 'wecom', type: msgType, data: fields })]);
157
+ const referrer = {
158
+ corpId: bot.config.corpId,
159
+ agentId: bot.config.agentId,
160
+ userId,
161
+ msgId,
162
+ };
163
+ const message = {
164
+ id: msgId,
165
+ chat: chatOf(userId),
166
+ sender: senderOf(userId),
167
+ content,
168
+ // An application message has no mention mechanism: there is no group and no `@` in a one-to-one conversation.
169
+ mentions: { self: false, all: false, users: [] },
170
+ time,
171
+ referrer,
172
+ raw: fields,
173
+ };
174
+ return bot.session({
175
+ type: 'message',
176
+ id: msgId,
177
+ timestamp: time,
178
+ message,
179
+ raw: fields,
180
+ });
181
+ }
182
+ //# sourceMappingURL=decode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decode.js","sourceRoot":"","sources":["../../src/decode.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AASH,OAAO,EAAE,CAAC,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AAsB1D,wFAAwF;AACxF,MAAM,UAAU,MAAM,CAAC,MAAc;IACnC,OAAO,EAAE,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAA;AACvC,CAAC;AAED,qGAAqG;AACrG,MAAM,UAAU,QAAQ,CAAC,MAAc;IACrC,OAAO,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,CAAA;AACrC,CAAC;AAED;;;;GAIG;AACH,SAAS,MAAM,CAAC,MAAiB;IAC/B,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAA;IACzC,OAAO,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAA;AAC/D,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,GAAa,EAAE,OAAe;IACxD,OAAO,GAAG,CAAC,cAAc,CAAC,WAAW,kBAAkB,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;AACrE,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAAC,MAAiB;IAC5C,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,CAAA;IACxC,MAAM,CAAC,GAAG,MAAM,CAAC,UAAU,IAAI,EAAE,CAAA;IACjC,MAAM,CAAC,GAAG,MAAM,CAAC,UAAU,IAAI,EAAE,C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,MAAM;KACZ,CAAA;IACD,OAAO,GAAG,CAAC,OAAO,CAAC;QACjB,IAAI,EAAE,SAAS;QACf,EAAE,EAAE,KAAK;QACT,SAAS,EAAE,IAAI;QACf,OAAO;QACP,GAAG,EAAE,MAAM;KACZ,CAAC,CAAA;AACJ,CAAC"}
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Platform failures of the WeCom REST surface and their mapping onto the core taxonomy.
3
+ *
4
+ * Almost every failure has one shape: HTTP 200 with `{ errcode, errmsg }`, where `errcode === 0` is success. The HTTP
5
+ * layer only fails for a transport problem or a gateway. `published: 'no'` is therefore reserved for a platform refusal
6
+ * this adapter can classify; an answer without a code, a non-JSON page and a 5xx leave the outcome open.
7
+ */
8
+ import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core';
9
+ import type { PlatformErrorKind } from '@deepseek-ai/cordis-plugin-im-core';
10
+ /** Fields one failed WeCom call carries for classification and logs. */
11
+ export interface WecomApiErrorInit {
12
+ /** HTTP status of the answer, absent when the request never produced one. */
13
+ status?: number;
14
+ /** The business code of an HTTP 200 answer; `0` never reaches this class. */
15
+ errcode?: number;
16
+ /** The platform's own message for the code. */
17
+ errmsg?: string;
18
+ /** How much of the answer body is kept for diagnostics. */
19
+ bodyText?: string;
20
+ /** The answer was not JSON (a gateway page, a truncated answer). */
21
+ nonJson?: boolean;
22
+ cause?: unknown;
23
+ }
24
+ /** One failed WeCom API call. */
25
+ export declare class WecomApiError extends Error {
26
+ readonly status: number | undefined;
27
+ readonly errcode: number | undefined;
28
+ readonly errmsg: string | undefined;
29
+ readonly bodyText: string | undefined;
30
+ readonly nonJson: boolean;
31
+ constructor(message: string, init?: WecomApiErrorInit);
32
+ }
33
+ /** One classification of a platform code or status. */
34
+ interface Verdict {
35
+ kind: PlatformErrorKind;
36
+ published: 'no' | 'unknown';
37
+ }
38
+ /**
39
+ * Verdict of one business code, by the documented table and then by family.
40
+ *
41
+ * @param errcode - the `errcode` of an answer.
42
+ * @returns its classification, or undefined for a code inside no family this adapter knows.
43
+ */
44
+ export declare function verdictOfCode(errcode: number): Verdict | undefined;
45
+ /**
46
+ * Whether an `errcode` means the `access_token` was refused.
47
+ *
48
+ * @param errcode - the code of an answer, when it carried one.
49
+ * @returns true for the two codes the platform documents as "refresh and retry".
50
+ */
51
+ export declare function isTokenFailureCode(errcode: number | undefined): boolean;
52
+ /**
53
+ * Whether a failed call means the `access_token` was refused.
54
+ *
55
+ * @param error - the caught failure.
56
+ * @returns true for the two codes the platform documents as "refresh and retry".
57
+ */
58
+ export declare function isTokenFailure(error: unknown): boolean;
59
+ /**
60
+ * Map any failure onto the core platform taxonomy.
61
+ *
62
+ * @param error - the caught failure.
63
+ * @returns the classified platform error.
64
+ */
65
+ export declare function classifyWecomError(error: unknown): PlatformError;
66
+ export {};
67
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAA;AAClE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oCAAoC,CAAA;AAE3E,wEAAwE;AACxE,MAAM,WAAW,iBAAiB;IAChC,6EAA6E;IAC7E,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,6EAA6E;IAC7E,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,+CAA+C;IAC/C,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,2DAA2D;IAC3D,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,oEAAoE;IACpE,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB;AAED,iCAAiC;AACjC,qBAAa,aAAc,SAAQ,KAAK;IACtC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAA;IACnC,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAA;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