@im-more/dsh-dingtalk 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 (292) hide show
  1. package/LICENSE +21 -0
  2. package/cordis.patch.yml +11 -0
  3. package/lib/index.js +2765 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/types/api/alitrip.d.ts +397 -0
  6. package/lib/types/api/alitrip.d.ts.map +1 -0
  7. package/lib/types/api/alitrip.js +21 -0
  8. package/lib/types/api/alitrip.js.map +1 -0
  9. package/lib/types/api/attendance.d.ts +63 -0
  10. package/lib/types/api/attendance.d.ts.map +1 -0
  11. package/lib/types/api/attendance.js +8 -0
  12. package/lib/types/api/attendance.js.map +1 -0
  13. package/lib/types/api/badge.d.ts +233 -0
  14. package/lib/types/api/badge.d.ts.map +1 -0
  15. package/lib/types/api/badge.js +19 -0
  16. package/lib/types/api/badge.js.map +1 -0
  17. package/lib/types/api/blackboard.d.ts +17 -0
  18. package/lib/types/api/blackboard.d.ts.map +1 -0
  19. package/lib/types/api/blackboard.js +6 -0
  20. package/lib/types/api/blackboard.js.map +1 -0
  21. package/lib/types/api/calendar.d.ts +621 -0
  22. package/lib/types/api/calendar.d.ts.map +1 -0
  23. package/lib/types/api/calendar.js +57 -0
  24. package/lib/types/api/calendar.js.map +1 -0
  25. package/lib/types/api/card.d.ts +185 -0
  26. package/lib/types/api/card.d.ts.map +1 -0
  27. package/lib/types/api/card.js +13 -0
  28. package/lib/types/api/card.js.map +1 -0
  29. package/lib/types/api/conference.d.ts +404 -0
  30. package/lib/types/api/conference.d.ts.map +1 -0
  31. package/lib/types/api/conference.js +60 -0
  32. package/lib/types/api/conference.js.map +1 -0
  33. package/lib/types/api/connector.d.ts +79 -0
  34. package/lib/types/api/connector.d.ts.map +1 -0
  35. package/lib/types/api/connector.js +8 -0
  36. package/lib/types/api/connector.js.map +1 -0
  37. package/lib/types/api/contact.d.ts +40 -0
  38. package/lib/types/api/contact.d.ts.map +1 -0
  39. package/lib/types/api/contact.js +9 -0
  40. package/lib/types/api/contact.js.map +1 -0
  41. package/lib/types/api/convFile.d.ts +135 -0
  42. package/lib/types/api/convFile.d.ts.map +1 -0
  43. package/lib/types/api/convFile.js +9 -0
  44. package/lib/types/api/convFile.js.map +1 -0
  45. package/lib/types/api/crm.d.ts +662 -0
  46. package/lib/types/api/crm.d.ts.map +1 -0
  47. package/lib/types/api/crm.js +59 -0
  48. package/lib/types/api/crm.js.map +1 -0
  49. package/lib/types/api/customerService.d.ts +128 -0
  50. package/lib/types/api/customerService.d.ts.map +1 -0
  51. package/lib/types/api/customerService.js +13 -0
  52. package/lib/types/api/customerService.js.map +1 -0
  53. package/lib/types/api/datacenter.d.ts +506 -0
  54. package/lib/types/api/datacenter.d.ts.map +1 -0
  55. package/lib/types/api/datacenter.js +61 -0
  56. package/lib/types/api/datacenter.js.map +1 -0
  57. package/lib/types/api/devicemng.d.ts +161 -0
  58. package/lib/types/api/devicemng.d.ts.map +1 -0
  59. package/lib/types/api/devicemng.js +20 -0
  60. package/lib/types/api/devicemng.js.map +1 -0
  61. package/lib/types/api/diot.d.ts +13 -0
  62. package/lib/types/api/diot.d.ts.map +1 -0
  63. package/lib/types/api/diot.js +4 -0
  64. package/lib/types/api/diot.js.map +1 -0
  65. package/lib/types/api/doc.d.ts +141 -0
  66. package/lib/types/api/doc.d.ts.map +1 -0
  67. package/lib/types/api/doc.js +27 -0
  68. package/lib/types/api/doc.js.map +1 -0
  69. package/lib/types/api/drive.d.ts +88 -0
  70. package/lib/types/api/drive.d.ts.map +1 -0
  71. package/lib/types/api/drive.js +11 -0
  72. package/lib/types/api/drive.js.map +1 -0
  73. package/lib/types/api/edu.d.ts +23 -0
  74. package/lib/types/api/edu.d.ts.map +1 -0
  75. package/lib/types/api/edu.js +4 -0
  76. package/lib/types/api/edu.js.map +1 -0
  77. package/lib/types/api/esign.d.ts +34 -0
  78. package/lib/types/api/esign.d.ts.map +1 -0
  79. package/lib/types/api/esign.js +7 -0
  80. package/lib/types/api/esign.js.map +1 -0
  81. package/lib/types/api/exclusive.d.ts +304 -0
  82. package/lib/types/api/exclusive.d.ts.map +1 -0
  83. package/lib/types/api/exclusive.js +27 -0
  84. package/lib/types/api/exclusive.js.map +1 -0
  85. package/lib/types/api/h3yun.d.ts +438 -0
  86. package/lib/types/api/h3yun.d.ts.map +1 -0
  87. package/lib/types/api/h3yun.js +33 -0
  88. package/lib/types/api/h3yun.js.map +1 -0
  89. package/lib/types/api/hrm.d.ts +215 -0
  90. package/lib/types/api/hrm.d.ts.map +1 -0
  91. package/lib/types/api/hrm.js +20 -0
  92. package/lib/types/api/hrm.js.map +1 -0
  93. package/lib/types/api/im.d.ts +773 -0
  94. package/lib/types/api/im.d.ts.map +1 -0
  95. package/lib/types/api/im.js +72 -0
  96. package/lib/types/api/im.js.map +1 -0
  97. package/lib/types/api/industry.d.ts +118 -0
  98. package/lib/types/api/industry.d.ts.map +1 -0
  99. package/lib/types/api/industry.js +14 -0
  100. package/lib/types/api/industry.js.map +1 -0
  101. package/lib/types/api/jzcrm.d.ts +255 -0
  102. package/lib/types/api/jzcrm.d.ts.map +1 -0
  103. package/lib/types/api/jzcrm.js +18 -0
  104. package/lib/types/api/jzcrm.js.map +1 -0
  105. package/lib/types/api/link.d.ts +73 -0
  106. package/lib/types/api/link.d.ts.map +1 -0
  107. package/lib/types/api/link.js +9 -0
  108. package/lib/types/api/link.js.map +1 -0
  109. package/lib/types/api/live.d.ts +132 -0
  110. package/lib/types/api/live.d.ts.map +1 -0
  111. package/lib/types/api/live.js +13 -0
  112. package/lib/types/api/live.js.map +1 -0
  113. package/lib/types/api/microApp.d.ts +240 -0
  114. package/lib/types/api/microApp.d.ts.map +1 -0
  115. package/lib/types/api/microApp.js +29 -0
  116. package/lib/types/api/microApp.js.map +1 -0
  117. package/lib/types/api/oapi.d.ts +3362 -0
  118. package/lib/types/api/oapi.d.ts.map +1 -0
  119. package/lib/types/api/oapi.js +247 -0
  120. package/lib/types/api/oapi.js.map +1 -0
  121. package/lib/types/api/oauth2.d.ts +115 -0
  122. package/lib/types/api/oauth2.d.ts.map +1 -0
  123. package/lib/types/api/oauth2.js +12 -0
  124. package/lib/types/api/oauth2.js.map +1 -0
  125. package/lib/types/api/pedia.d.ts +186 -0
  126. package/lib/types/api/pedia.d.ts.map +1 -0
  127. package/lib/types/api/pedia.js +13 -0
  128. package/lib/types/api/pedia.js.map +1 -0
  129. package/lib/types/api/project.d.ts +1119 -0
  130. package/lib/types/api/project.d.ts.map +1 -0
  131. package/lib/types/api/project.js +135 -0
  132. package/lib/types/api/project.js.map +1 -0
  133. package/lib/types/api/resident.d.ts +107 -0
  134. package/lib/types/api/resident.d.ts.map +1 -0
  135. package/lib/types/api/resident.js +10 -0
  136. package/lib/types/api/resident.js.map +1 -0
  137. package/lib/types/api/robot.d.ts +256 -0
  138. package/lib/types/api/robot.d.ts.map +1 -0
  139. package/lib/types/api/robot.js +21 -0
  140. package/lib/types/api/robot.js.map +1 -0
  141. package/lib/types/api/rooms.d.ts +256 -0
  142. package/lib/types/api/rooms.d.ts.map +1 -0
  143. package/lib/types/api/rooms.js +23 -0
  144. package/lib/types/api/rooms.js.map +1 -0
  145. package/lib/types/api/serviceGroup.d.ts +176 -0
  146. package/lib/types/api/serviceGroup.d.ts.map +1 -0
  147. package/lib/types/api/serviceGroup.js +15 -0
  148. package/lib/types/api/serviceGroup.js.map +1 -0
  149. package/lib/types/api/storage.d.ts +1241 -0
  150. package/lib/types/api/storage.d.ts.map +1 -0
  151. package/lib/types/api/storage.js +128 -0
  152. package/lib/types/api/storage.js.map +1 -0
  153. package/lib/types/api/swform.d.ts +71 -0
  154. package/lib/types/api/swform.d.ts.map +1 -0
  155. package/lib/types/api/swform.js +8 -0
  156. package/lib/types/api/swform.js.map +1 -0
  157. package/lib/types/api/todo.d.ts +169 -0
  158. package/lib/types/api/todo.d.ts.map +1 -0
  159. package/lib/types/api/todo.js +16 -0
  160. package/lib/types/api/todo.js.map +1 -0
  161. package/lib/types/api/wiki.d.ts +195 -0
  162. package/lib/types/api/wiki.d.ts.map +1 -0
  163. package/lib/types/api/wiki.js +11 -0
  164. package/lib/types/api/wiki.js.map +1 -0
  165. package/lib/types/api/workbench.d.ts +46 -0
  166. package/lib/types/api/workbench.d.ts.map +1 -0
  167. package/lib/types/api/workbench.js +14 -0
  168. package/lib/types/api/workbench.js.map +1 -0
  169. package/lib/types/api/yida.d.ts +1838 -0
  170. package/lib/types/api/yida.d.ts.map +1 -0
  171. package/lib/types/api/yida.js +103 -0
  172. package/lib/types/api/yida.js.map +1 -0
  173. package/lib/types/bot.d.ts +62 -0
  174. package/lib/types/bot.d.ts.map +1 -0
  175. package/lib/types/bot.js +134 -0
  176. package/lib/types/bot.js.map +1 -0
  177. package/lib/types/config.d.ts +47 -0
  178. package/lib/types/config.d.ts.map +1 -0
  179. package/lib/types/config.js +53 -0
  180. package/lib/types/config.js.map +1 -0
  181. package/lib/types/decode.d.ts +36 -0
  182. package/lib/types/decode.d.ts.map +1 -0
  183. package/lib/types/decode.js +200 -0
  184. package/lib/types/decode.js.map +1 -0
  185. package/lib/types/errors.d.ts +77 -0
  186. package/lib/types/errors.d.ts.map +1 -0
  187. package/lib/types/errors.js +249 -0
  188. package/lib/types/errors.js.map +1 -0
  189. package/lib/types/index.d.ts +22 -0
  190. package/lib/types/index.d.ts.map +1 -0
  191. package/lib/types/index.js +22 -0
  192. package/lib/types/index.js.map +1 -0
  193. package/lib/types/internal.d.ts +15 -0
  194. package/lib/types/internal.d.ts.map +1 -0
  195. package/lib/types/internal.js +57 -0
  196. package/lib/types/internal.js.map +1 -0
  197. package/lib/types/media.d.ts +57 -0
  198. package/lib/types/media.d.ts.map +1 -0
  199. package/lib/types/media.js +99 -0
  200. package/lib/types/media.js.map +1 -0
  201. package/lib/types/modes/markdown.d.ts +12 -0
  202. package/lib/types/modes/markdown.d.ts.map +1 -0
  203. package/lib/types/modes/markdown.js +148 -0
  204. package/lib/types/modes/markdown.js.map +1 -0
  205. package/lib/types/modes/shared.d.ts +183 -0
  206. package/lib/types/modes/shared.d.ts.map +1 -0
  207. package/lib/types/modes/shared.js +358 -0
  208. package/lib/types/modes/shared.js.map +1 -0
  209. package/lib/types/request.d.ts +63 -0
  210. package/lib/types/request.d.ts.map +1 -0
  211. package/lib/types/request.js +182 -0
  212. package/lib/types/request.js.map +1 -0
  213. package/lib/types/send.d.ts +38 -0
  214. package/lib/types/send.d.ts.map +1 -0
  215. package/lib/types/send.js +128 -0
  216. package/lib/types/send.js.map +1 -0
  217. package/lib/types/token.d.ts +32 -0
  218. package/lib/types/token.d.ts.map +1 -0
  219. package/lib/types/token.js +62 -0
  220. package/lib/types/token.js.map +1 -0
  221. package/lib/types/transport/stream.d.ts +89 -0
  222. package/lib/types/transport/stream.d.ts.map +1 -0
  223. package/lib/types/transport/stream.js +345 -0
  224. package/lib/types/transport/stream.js.map +1 -0
  225. package/lib/types/transport/webhook.d.ts +54 -0
  226. package/lib/types/transport/webhook.d.ts.map +1 -0
  227. package/lib/types/transport/webhook.js +159 -0
  228. package/lib/types/transport/webhook.js.map +1 -0
  229. package/lib/types/types/index.d.ts +150 -0
  230. package/lib/types/types/index.d.ts.map +1 -0
  231. package/lib/types/types/index.js +43 -0
  232. package/lib/types/types/index.js.map +1 -0
  233. package/package.json +61 -3
  234. package/src/api/.eslintrc.yml +2 -0
  235. package/src/api/alitrip.ts +467 -0
  236. package/src/api/attendance.ts +81 -0
  237. package/src/api/badge.ts +285 -0
  238. package/src/api/blackboard.ts +28 -0
  239. package/src/api/calendar.ts +817 -0
  240. package/src/api/card.ts +215 -0
  241. package/src/api/conference.ts +561 -0
  242. package/src/api/connector.ts +97 -0
  243. package/src/api/contact.ts +56 -0
  244. package/src/api/convFile.ts +166 -0
  245. package/src/api/crm.ts +830 -0
  246. package/src/api/customerService.ts +156 -0
  247. package/src/api/datacenter.ts +672 -0
  248. package/src/api/devicemng.ts +202 -0
  249. package/src/api/diot.ts +19 -0
  250. package/src/api/doc.ts +232 -0
  251. package/src/api/drive.ts +109 -0
  252. package/src/api/edu.ts +30 -0
  253. package/src/api/esign.ts +44 -0
  254. package/src/api/exclusive.ts +372 -0
  255. package/src/api/h3yun.ts +537 -0
  256. package/src/api/hrm.ts +272 -0
  257. package/src/api/im.ts +978 -0
  258. package/src/api/industry.ts +153 -0
  259. package/src/api/jzcrm.ts +304 -0
  260. package/src/api/link.ts +94 -0
  261. package/src/api/live.ts +162 -0
  262. package/src/api/microApp.ts +309 -0
  263. package/src/api/oapi.ts +4083 -0
  264. package/src/api/oauth2.ts +146 -0
  265. package/src/api/pedia.ts +222 -0
  266. package/src/api/project.ts +1519 -0
  267. package/src/api/resident.ts +133 -0
  268. package/src/api/robot.ts +326 -0
  269. package/src/api/rooms.ts +334 -0
  270. package/src/api/serviceGroup.ts +216 -0
  271. package/src/api/storage.ts +1701 -0
  272. package/src/api/swform.ts +94 -0
  273. package/src/api/todo.ts +220 -0
  274. package/src/api/wiki.ts +231 -0
  275. package/src/api/workbench.ts +73 -0
  276. package/src/api/yida.ts +2165 -0
  277. package/src/bot.ts +211 -0
  278. package/src/config.ts +88 -0
  279. package/src/decode.ts +250 -0
  280. package/src/errors.ts +282 -0
  281. package/src/index.ts +82 -0
  282. package/src/internal.ts +60 -0
  283. package/src/media.ts +149 -0
  284. package/src/modes/markdown.ts +173 -0
  285. package/src/modes/shared.ts +473 -0
  286. package/src/request.ts +262 -0
  287. package/src/send.ts +195 -0
  288. package/src/token.ts +72 -0
  289. package/src/transport/stream.ts +436 -0
  290. package/src/transport/webhook.ts +198 -0
  291. package/src/types/index.ts +165 -0
  292. package/README.md +0 -3
package/src/errors.ts ADDED
@@ -0,0 +1,282 @@
1
+ /**
2
+ * Platform failures of the DingTalk REST surface and their mapping onto the core taxonomy.
3
+ *
4
+ * DingTalk answers failures in three forms: an HTTP 4xx with `{ code, message, requestid }` (the `/v1.0/*` endpoints), an
5
+ * HTTP 200 with `{ errcode, errmsg }` (the legacy `oapi` endpoints, such as the media upload), and an HTML page from
6
+ * the IP-dimension rate limiter. Only an explicit platform refusal may be reported as `published: 'no'`.
7
+ */
8
+
9
+ import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core'
10
+ import type { PlatformErrorKind } from '@deepseek-ai/cordis-plugin-im-core'
11
+
12
+ /** Fields one failed DingTalk call carries for classification. */
13
+ export interface DingtalkApiErrorInit {
14
+ /** HTTP status of the answer, absent when the request never produced one. */
15
+ status?: number
16
+ /** `code` of a `/v1.0/*` answer or `errcode` of an `oapi` one. */
17
+ code?: string | number
18
+ /** `requestid` of a `/v1.0/*` answer, for a support ticket. */
19
+ requestid?: string
20
+ /** The answer body, kept for diagnostics; long bodies are truncated. */
21
+ bodyText?: string
22
+ /** The answer was not JSON (the HTML page of the IP-dimension rate limiter, a proxy error page). */
23
+ nonJson?: boolean
24
+ cause?: unknown
25
+ }
26
+
27
+ /** One failed DingTalk call, with the fields `classify` needs. */
28
+ export class DingtalkApiError extends Error {
29
+ readonly status: number | undefined
30
+ readonly code: string | number | undefined
31
+ readonly requestid: string | undefined
32
+ readonly bodyText: string | undefined
33
+ readonly nonJson: boolean
34
+
35
+ constructor(message: string, init: DingtalkApiErrorInit = {}) {
36
+ super(message, init.cause === undefined ? undefined : { cause: init.cause })
37
+ this.name = 'DingtalkApiError'
38
+ this.status = init.status
39
+ this.code = init.code
40
+ this.requestid = init.requestid
41
+ this.bodyText = init.bodyText
42
+ this.nonJson = init.nonJson ?? false
43
+ }
44
+ }
45
+
46
+ /** One classification of a platform code or status. */
47
+ interface Verdict {
48
+ kind: PlatformErrorKind
49
+ published: 'no' | 'unknown'
50
+ }
51
+
52
+ const RATE_LIMIT: Verdict = { kind: 'rate-limit', published: 'no' }
53
+ const LIMIT: Verdict = { kind: 'limit', published: 'no' }
54
+ const PERMISSION: Verdict = { kind: 'permission', published: 'no' }
55
+ const NOT_FOUND: Verdict = { kind: 'not-found', published: 'no' }
56
+ const INVALID: Verdict = { kind: 'invalid', published: 'no' }
57
+ /** The platform reports an unknown send result: it may or may not have published, so the outcome stays open. */
58
+ const INVALID_UNKNOWN: Verdict = { kind: 'invalid', published: 'unknown' }
59
+ const RETRYABLE: Verdict = { kind: 'retryable', published: 'unknown' }
60
+
61
+ /**
62
+ * Codes of the documented error tables of `groupMessages/send`, `oToMessages/batchSend` and `groupMessages/recall`
63
+ * (three near-identical tables), plus the shared frequency-limit codes and the legacy `oapi` errcodes.
64
+ */
65
+ const CODE_VERDICTS: readonly (readonly [Verdict, readonly string[]])[] = [
66
+ [
67
+ RATE_LIMIT,
68
+ [
69
+ 'send.too.fast',
70
+ 'send.byToken.tooFast',
71
+ 'too.many.group',
72
+ 'too.many.people',
73
+ '90018',
74
+ '90006',
75
+ '90002',
76
+ 'Forbidden.AccessDenied.QpsLimitForAppkeyAndApi',
77
+ ],
78
+ ],
79
+ [
80
+ LIMIT,
81
+ [
82
+ 'invalidParameter.msgParam.tooLong',
83
+ 'invalidParameter.userIds.overMax',
84
+ 'invalidParameter.processQueryKeys.overMax',
85
+ ],
86
+ ],
87
+ [
88
+ PERMISSION,
89
+ [
90
+ 'InvalidAuthentication',
91
+ 'invalidParameter.token.invalid',
92
+ 'Forbidden.AccessDenied.AccessTokenInvalid',
93
+ 'send.forbidden',
94
+ 'bot.forbidden.sendMessage',
95
+ 'unauthorized',
96
+ 'auth.error',
97
+ 'not.allowed.robotCode',
98
+ 'ip.not.match',
99
+ 'keywords.not.match',
100
+ 'sign.not.match',
101
+ 'only.groupAdmin.canAtAll',
102
+ 'conversation.org.notMatch',
103
+ ],
104
+ ],
105
+ [
106
+ NOT_FOUND,
107
+ [
108
+ 'invalidParameter.robotCode.empty',
109
+ 'invalidParameter.robotCode.missing',
110
+ 'invalidParameter.robotCode.invalid',
111
+ 'invalidParameter.robotCode.notExsit',
112
+ 'invalidParameter.robotCode.auth',
113
+ 'invalidParameter.robotCode.coolApp',
114
+ 'template.not.existed',
115
+ 'template.stopped',
116
+ 'bot.stopped',
117
+ 'resource.not.found',
118
+ 'resource.unavailable',
119
+ 'notFound.robotCode',
120
+ 'invalid.openConversationId',
121
+ 'miss.openConversationId',
122
+ 'group.disbanded',
123
+ 'staffId.notExisted',
124
+ 'robot.oto.notExist',
125
+ 'chatbotId.notAllow.sendOTO',
126
+ 'invalidParameter.processQueryKey.missing',
127
+ 'invalid.processQueryKey',
128
+ 'session.notExisted',
129
+ 'session.expired',
130
+ 'token.notExisted',
131
+ ],
132
+ ],
133
+ [
134
+ INVALID,
135
+ [
136
+ 'invalidParameter.msgKey.empty',
137
+ 'invalidParameter.msgKey.invalid',
138
+ 'invalidParameter.msg.unsupport',
139
+ 'invalidParameter.msgParam.invalid',
140
+ 'invalidParameter.msgBody.invalid',
141
+ 'invalidParameter.param.invalid',
142
+ 'contain.unsafe.url',
143
+ 'contain.notAllowed.text',
144
+ 'contain.notAllowed.picture',
145
+ 'contain.notAllowed.content',
146
+ ],
147
+ ],
148
+ [INVALID_UNKNOWN, ['unknown.send.result']],
149
+ [RETRYABLE, ['system.error', 'send.ding.exception']],
150
+ ]
151
+
152
+ /** Code prefixes whose whole family has one verdict (`miss.param.*`, `invalid.param.actionCard*`, `4xxxx` errcodes). */
153
+ const CODE_PREFIX_VERDICTS: readonly (readonly [Verdict, string])[] = [
154
+ [INVALID, 'miss.param.'],
155
+ [INVALID, 'invalid.param.actionCard'],
156
+ ]
157
+
158
+ const EXACT_CODES: ReadonlyMap<string, Verdict> = new Map(
159
+ CODE_VERDICTS.flatMap(([verdict, codes]) =>
160
+ codes.map((code): [string, Verdict] => [code, verdict]),
161
+ ),
162
+ )
163
+
164
+ /**
165
+ * Verdict of one platform code, if the tables cover it.
166
+ * @param code - `code` of a `/v1.0/*` answer or `errcode` of an `oapi` one.
167
+ * @returns its classification, or undefined for a code this adapter does not know.
168
+ */
169
+ export function verdictOfCode(code: string): Verdict | undefined {
170
+ const exact = EXACT_CODES.get(code)
171
+ if (exact !== undefined) return exact
172
+ for (const [verdict, prefix] of CODE_PREFIX_VERDICTS) {
173
+ if (code.startsWith(prefix)) return verdict
174
+ }
175
+ // The legacy media endpoints answer 400xx for a refused file; the exact codes are not cross-checked one by one.
176
+ if (/^400\d{2}$/.test(code)) return INVALID
177
+ return undefined
178
+ }
179
+
180
+ /** Codes that name the application credentials rather than a policy the caller can fix. */
181
+ const AUTH_CODES: ReadonlySet<string> = new Set([
182
+ 'InvalidAuthentication',
183
+ 'invalidParameter.token.invalid',
184
+ 'Forbidden.AccessDenied.AccessTokenInvalid',
185
+ ])
186
+
187
+ /** Whether a failed call means the access token was refused, so the token is re-minted and the call retried once. */
188
+ export function isAuthFailure(error: unknown): boolean {
189
+ if (!(error instanceof DingtalkApiError)) return false
190
+ if (error.status === 401) return true
191
+ return error.code !== undefined && AUTH_CODES.has(String(error.code))
192
+ }
193
+
194
+ const CREDENTIAL_CODES: ReadonlySet<string> = new Set([
195
+ ...AUTH_CODES,
196
+ 'invalidClientIdOrSecret',
197
+ 'invalidClientId',
198
+ 'invalidClientSecret',
199
+ 'app.not.found',
200
+ ])
201
+
202
+ /**
203
+ * Whether a failed Stream registration names the configured credentials themselves.
204
+ *
205
+ * Such a refusal cannot be fixed by reconnecting: the loop stops and the bot reports the error until the profile is
206
+ * edited.
207
+ *
208
+ * @param error - the caught failure.
209
+ * @returns true for a credential code such as `invalidClientIdOrSecret`.
210
+ */
211
+ export function isCredentialRefusal(error: unknown): boolean {
212
+ return (
213
+ error instanceof DingtalkApiError &&
214
+ error.code !== undefined &&
215
+ CREDENTIAL_CODES.has(String(error.code))
216
+ )
217
+ }
218
+
219
+ /**
220
+ * Whether a failed Stream registration is an authentication or authorisation refusal.
221
+ *
222
+ * A refusal without a credential code may be transient (a gateway hiccup, a rate-limited gateway), so the loop keeps
223
+ * retrying; a run of them means the credentials are wrong.
224
+ *
225
+ * @param error - the caught failure.
226
+ * @returns true for a 401/403 answer or a credential code.
227
+ */
228
+ export function isAuthRefusal(error: unknown): boolean {
229
+ return (
230
+ error instanceof DingtalkApiError &&
231
+ (error.status === 401 || error.status === 403 || isCredentialRefusal(error))
232
+ )
233
+ }
234
+
235
+ /** The verdict of a failed call: the code table first, then the HTTP status, then an assumed transient failure. */
236
+ function verdictOf(error: DingtalkApiError): Verdict {
237
+ // A body that is not JSON came from something in front of the API (the IP-dimension rate limiter, a gateway), so the
238
+ // platform's own status cannot say whether the request was accepted.
239
+ if (error.nonJson) return RETRYABLE
240
+ if (error.code !== undefined) {
241
+ const verdict = verdictOfCode(String(error.code))
242
+ if (verdict !== undefined) return verdict
243
+ }
244
+ if (error.status === 429) return RATE_LIMIT
245
+ if (error.status === 401 || error.status === 403) return PERMISSION
246
+ if (error.status === 404) return NOT_FOUND
247
+ if (error.status !== undefined && error.status >= 400 && error.status < 500)
248
+ return INVALID
249
+ return RETRYABLE
250
+ }
251
+
252
+ /**
253
+ * Map any failure onto the core platform taxonomy.
254
+ *
255
+ * A transport failure, a 5xx answer or a non-JSON answer leaves the outcome open: the request may have reached the
256
+ * platform. Only a documented refusal is reported as unpublished.
257
+ *
258
+ * @param error - the caught failure.
259
+ * @returns the classified platform error.
260
+ */
261
+ export function classifyDingtalkError(error: unknown): PlatformError {
262
+ if (error instanceof PlatformError) return error
263
+ if (error instanceof DingtalkApiError) {
264
+ const verdict = verdictOf(error)
265
+ return new PlatformError(verdict.kind, error.message, {
266
+ ...(error.code === undefined ? {} : { code: error.code }),
267
+ published: verdict.published,
268
+ cause: error,
269
+ })
270
+ }
271
+ if (error instanceof Error && error.name === 'AbortError') {
272
+ return new PlatformError('fatal', error.message, {
273
+ published: 'unknown',
274
+ cause: error,
275
+ })
276
+ }
277
+ const message = error instanceof Error ? error.message : String(error)
278
+ return new PlatformError('retryable', `dingtalk request failed: ${message}`, {
279
+ published: 'unknown',
280
+ cause: error,
281
+ })
282
+ }
package/src/index.ts ADDED
@@ -0,0 +1,82 @@
1
+ /**
2
+ * `@im-more/dsh-dingtalk`: a DingTalk internal-application robot for the DeepSeek Harness IM subsystem.
3
+ *
4
+ * Mounting this plugin adds one `satori` bot (`dingtalk:<appkey>`) with a Stream Mode or HTTP-callback transport and the
5
+ * single `markdown` mode. See `docs/platforms/dingtalk.md` for the application setup and the platform's limits.
6
+ */
7
+
8
+ import * as Dingtalk from './types/index.ts'
9
+
10
+ export {
11
+ Config,
12
+ configError,
13
+ routePath,
14
+ type Config as DingtalkConfig,
15
+ type DingtalkProtocol,
16
+ } from './config.ts'
17
+ export {
18
+ DingtalkBot,
19
+ DingtalkBot as default,
20
+ type DingtalkModes,
21
+ } from './bot.ts'
22
+ export { decodeMessage, type DingtalkReferrer } from './decode.ts'
23
+ export {
24
+ classifyDingtalkError,
25
+ DingtalkApiError,
26
+ isAuthFailure,
27
+ isAuthRefusal,
28
+ isCredentialRefusal,
29
+ verdictOfCode,
30
+ type DingtalkApiErrorInit,
31
+ } from './errors.ts'
32
+ export { Internal } from './internal.ts'
33
+ export {
34
+ downloadUrlOf,
35
+ mediaSource,
36
+ mediaTypeOf,
37
+ uploadMedia,
38
+ type DingtalkMediaUploadAnswer,
39
+ } from './media.ts'
40
+ export {
41
+ assembleDraft,
42
+ checkDraft,
43
+ DINGTALK_LIMITS,
44
+ escape,
45
+ fileTypeOf,
46
+ mediaNode,
47
+ serializeDraft,
48
+ titleOf,
49
+ unescape,
50
+ ZERO_WIDTH,
51
+ type DingtalkDraft,
52
+ type DingtalkMedia,
53
+ type DingtalkMediaKind,
54
+ type DingtalkModeTypes,
55
+ type DingtalkNode,
56
+ type DingtalkPayload,
57
+ } from './modes/shared.ts'
58
+ export { createMarkdownMode } from './modes/markdown.ts'
59
+ export {
60
+ endpointOf,
61
+ invoke,
62
+ type DingtalkBase,
63
+ type DingtalkRequestBody,
64
+ } from './request.ts'
65
+ export { deletePart, sendPayload, type DingtalkSendDeps } from './send.ts'
66
+ export { AccessTokenCache, type DingtalkAccessToken } from './token.ts'
67
+ export {
68
+ CALLBACK_TOPIC,
69
+ CLIENT_USER_AGENT,
70
+ DingtalkStream,
71
+ type AckCode,
72
+ type DingtalkAck,
73
+ type DingtalkConnection,
74
+ type DingtalkFrame,
75
+ } from './transport/stream.ts'
76
+ export {
77
+ DingtalkWebhook,
78
+ matchesSign,
79
+ signOf,
80
+ TIMESTAMP_WINDOW_MS,
81
+ } from './transport/webhook.ts'
82
+ export { Dingtalk }
@@ -0,0 +1,60 @@
1
+ import { type Dict } from '@deepseek-ai/cordis-plugin-im-core'
2
+ import type { Http } from '@deepseek-ai/cordis-plugin-http'
3
+ import type { DingtalkBot } from './bot.ts'
4
+ import { invoke } from './request.ts'
5
+
6
+ /**
7
+ * Route table of the migrated REST layer: one entry per generated `Internal.define({...})` block.
8
+ *
9
+ * `isOldApi` selects the version-family transport: the new `api.dingtalk.com/v1.0` endpoints take a version query
10
+ * parameter and the access token in a header, the legacy `oapi.dingtalk.com` ones take `method` + `access_token` as
11
+ * query parameters.
12
+ */
13
+ export class Internal {
14
+ constructor(private bot: DingtalkBot) { }
15
+
16
+ static define(routes: Dict<Partial<Record<string, Record<string, boolean>>>>) {
17
+ // One method per generated route; the prototype is the method table the declarations augment.
18
+ const methods = Internal.prototype as Internal & Record<string, unknown>
19
+ for (const path in routes) {
20
+ const byMethod = routes[path]!
21
+ for (const key in byMethod) {
22
+ const method = key
23
+ const table = byMethod[method]!
24
+ for (const name of Object.keys(table)) {
25
+ const isOldApi = table[name]
26
+ methods[name] = async function (this: Internal, ...args: any[]) {
27
+ const raw = args.join(', ')
28
+ const url = path.replace(/\{([^}]+)\}/g, () => {
29
+ if (!args.length) throw new Error(`too few arguments for ${path}, received ${raw}`)
30
+ return args.shift()
31
+ })
32
+ const config: Http.RequestConfig = {}
33
+ if (args.length === 1) {
34
+ if (method === 'GET' || method === 'DELETE') {
35
+ config.params = args[0]
36
+ } else {
37
+ config.data = args[0]
38
+ }
39
+ } else if (args.length === 2 && method !== 'GET' && method !== 'DELETE') {
40
+ config.data = args[0]
41
+ config.params = args[1]
42
+ } else if (args.length > 1) {
43
+ throw new Error(`too many arguments for ${path}, received ${raw}`)
44
+ }
45
+ if (isOldApi) {
46
+ config.params = { method, ...config.params }
47
+ }
48
+ return await invoke(
49
+ this.bot,
50
+ isOldApi ? 'oapi' : 'api',
51
+ method,
52
+ url,
53
+ config,
54
+ )
55
+ }
56
+ }
57
+ }
58
+ }
59
+ }
60
+ }
package/src/media.ts ADDED
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Media: the outbound upload endpoint and the inbound `downloadCode` exchange.
3
+ *
4
+ * DingTalk has a standalone upload (`/media/upload`) whose `media_id` is reusable across chats, so outbound media
5
+ * uploads once per content and the per-kind `sample*` template then carries the id. Inbound media is served from the
6
+ * bot's own `satori:` route: a `downloadCode` is exchanged for a short-lived signed `downloadUrl` at read time, so a
7
+ * message read later still resolves while the code is valid.
8
+ */
9
+
10
+ import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core'
11
+ import type {
12
+ PrepareContext,
13
+ ResolvedResource,
14
+ ResourceRef,
15
+ } from '@deepseek-ai/cordis-plugin-im-core'
16
+ import type { DingtalkBot } from './bot.ts'
17
+ import { DingtalkApiError } from './errors.ts'
18
+ import { invoke } from './request.ts'
19
+
20
+ /** One answer of `POST /media/upload`. */
21
+ export interface DingtalkMediaUploadAnswer {
22
+ errcode?: number
23
+ errmsg?: string
24
+ media_id?: string
25
+ type?: string
26
+ created_at?: number
27
+ }
28
+
29
+ /** The platform's upload `type` of a MIME type: `image`, `voice`, `video` or `file`. */
30
+ export function mediaTypeOf(mime: string | undefined): string {
31
+ if (mime === undefined) return 'file'
32
+ if (mime.startsWith('image/')) return 'image'
33
+ if (mime.startsWith('video/')) return 'video'
34
+ if (mime.startsWith('audio/')) return 'voice'
35
+ return 'file'
36
+ }
37
+
38
+ /** File name of a source, for the multipart part of an upload. */
39
+ function fileNameOf(
40
+ ref: ResourceRef,
41
+ fetchedName: string | undefined,
42
+ mime: string | undefined,
43
+ ): string {
44
+ if (ref.meta.name !== undefined && ref.meta.name !== '') return ref.meta.name
45
+ if (fetchedName !== undefined && fetchedName !== '') return fetchedName
46
+ const kind = ref.kind
47
+ const subtype = mime?.split('/')[1]?.split(';')[0]
48
+ return `dingtalk-${kind}${subtype === undefined ? '' : `.${subtype}`}`
49
+ }
50
+
51
+ /**
52
+ * Upload one outbound attachment and return its `media_id`.
53
+ *
54
+ * The endpoint is a legacy `oapi` multipart call: the `access_token` rides the query string (the transport adds it) and
55
+ * the `type` + `media` fields are the body.
56
+ *
57
+ * @param bot - the adapter whose credentials and transport the upload uses.
58
+ * @param ref - the unresolved resource.
59
+ * @param ctx - fetch context of the send pipeline.
60
+ * @returns the platform media key.
61
+ */
62
+ export async function uploadMedia(
63
+ bot: DingtalkBot,
64
+ ref: ResourceRef,
65
+ ctx: PrepareContext,
66
+ ): Promise<ResolvedResource> {
67
+ const fetched = await ctx.fetch(ref.src, ctx.signal)
68
+ const mime = ref.meta.mime ?? fetched.mime
69
+ const name = fileNameOf(ref, fetched.name, mime)
70
+ const form = new FormData()
71
+ form.append('type', mediaTypeOf(mime))
72
+ form.append(
73
+ 'media',
74
+ new Blob([fetched.data], mime === undefined ? {} : { type: mime }),
75
+ name,
76
+ )
77
+ const answer = await invoke<DingtalkMediaUploadAnswer>(
78
+ bot,
79
+ 'oapi',
80
+ 'POST',
81
+ '/media/upload',
82
+ { data: form, signal: ctx.signal },
83
+ )
84
+ if (answer.media_id === undefined || answer.media_id === '') {
85
+ // The request reached the platform and it accepted the call, but no reusable key came back.
86
+ throw new PlatformError(
87
+ 'invalid',
88
+ 'dingtalk: /media/upload answered without a media_id',
89
+ { published: 'unknown' },
90
+ )
91
+ }
92
+ return {
93
+ key: answer.media_id,
94
+ extra: { name, ...(mime === undefined ? {} : { mime }) },
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Exchange an inbound `downloadCode` for the signed URL of its bytes.
100
+ *
101
+ * The URL is short-lived, so the route calls this per read instead of baking the URL into the decoded element.
102
+ *
103
+ * @param bot - the receiving adapter.
104
+ * @param downloadCode - the code the callback carried.
105
+ * @returns the signed download URL.
106
+ */
107
+ export async function downloadUrlOf(
108
+ bot: DingtalkBot,
109
+ downloadCode: string,
110
+ ): Promise<string> {
111
+ const answer = await bot.internal.robotMessageFileDownload({
112
+ downloadCode,
113
+ robotCode: bot.config.appkey,
114
+ })
115
+ if (answer.downloadUrl === '') {
116
+ throw new DingtalkApiError(
117
+ 'dingtalk: messageFiles/download answered without a downloadUrl',
118
+ { status: 200 },
119
+ )
120
+ }
121
+ return answer.downloadUrl
122
+ }
123
+
124
+ /**
125
+ * The `satori:` source of one inbound media element.
126
+ *
127
+ * The platform's own `downloadCode` rides the query string, so a stored message stays readable with no adapter state: a
128
+ * restart, another process of the same profile and a read days later all resolve while the code is valid. The path keeps
129
+ * the message id and the media position, which name the element in a log line or a stored message.
130
+ *
131
+ * @param bot - the receiving adapter.
132
+ * @param messageId - platform message id the element belongs to.
133
+ * @param index - position of the media element in the message content.
134
+ * @param code - the `downloadCode` the route exchanges for a signed URL.
135
+ * @returns the internal URL the host downloads through.
136
+ */
137
+ export function mediaSource(
138
+ bot: DingtalkBot,
139
+ messageId: string,
140
+ index: number,
141
+ code: string,
142
+ ): string {
143
+ // The id is encoded because it is an opaque platform value: a character the route grammar reserves would otherwise
144
+ // split the path. The query helper encodes the code, which is base64 and may carry `/`, `+` and `=`.
145
+ return bot.getInternalUrl(
146
+ `/messages/${encodeURIComponent(messageId)}/resources/${index}`,
147
+ { code },
148
+ )
149
+ }