@zhin.js/core 1.1.42 → 1.1.43

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 (42) hide show
  1. package/lib/built/roles.d.ts +6 -1
  2. package/lib/built/roles.js +11 -20
  3. package/lib/built/segment-contract/validate.d.ts +301 -158
  4. package/lib/built/user-interaction.js +9 -3
  5. package/lib/feature/middleware-public-api.d.ts +5 -0
  6. package/lib/feature/middleware-public-api.js +5 -0
  7. package/lib/feature/middleware.d.ts +20 -2
  8. package/lib/feature/middleware.js +6 -2
  9. package/lib/plugin-runtime/im/contracts.d.ts +14 -0
  10. package/lib/plugin-runtime/im/contracts.js +3 -1
  11. package/lib/plugin-runtime/im/conversation-runtime.d.ts +13 -1
  12. package/lib/plugin-runtime/im/conversation-runtime.js +47 -1
  13. package/lib/plugin-runtime/im/endpoint-runtime.d.ts +8 -3
  14. package/lib/plugin-runtime/im/endpoint-runtime.js +46 -3
  15. package/lib/plugin-runtime/im/im-runtime.d.ts +12 -1
  16. package/lib/plugin-runtime/im/im-runtime.js +18 -1
  17. package/lib/plugin-runtime/im/inbound-dedup.d.ts +51 -0
  18. package/lib/plugin-runtime/im/inbound-dedup.js +184 -0
  19. package/lib/plugin-runtime/im/inbound-origin.d.ts +101 -0
  20. package/lib/plugin-runtime/im/inbound-origin.js +76 -0
  21. package/lib/plugin-runtime/im/inbound-runtime.d.ts +16 -2
  22. package/lib/plugin-runtime/im/inbound-runtime.js +235 -11
  23. package/lib/plugin-runtime/im/index.d.ts +3 -1
  24. package/lib/plugin-runtime/im/index.js +1 -0
  25. package/lib/plugin-runtime/im/interaction-runtime.d.ts +11 -0
  26. package/lib/plugin-runtime/im/interaction-runtime.js +17 -0
  27. package/lib/plugin-runtime/im/interactive-router.d.ts +18 -1
  28. package/lib/plugin-runtime/im/interactive-router.js +62 -8
  29. package/lib/plugin-runtime/im/jsx-button-registry.js +6 -2
  30. package/lib/plugin-runtime/im/message-dispatcher.js +10 -0
  31. package/lib/plugin-runtime/im/message-events.d.ts +7 -0
  32. package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +2 -0
  33. package/lib/plugin-runtime/im/outbound-delivery-runtime.js +37 -45
  34. package/lib/plugin-runtime/im/outbound-ledger.d.ts +24 -0
  35. package/lib/plugin-runtime/im/outbound-ledger.js +114 -0
  36. package/lib/plugin-runtime/im/outbound-projection.d.ts +15 -0
  37. package/lib/plugin-runtime/im/outbound-projection.js +55 -0
  38. package/lib/plugin-runtime/im/outbound-renderer.d.ts +3 -0
  39. package/lib/plugin-runtime/im/outbound-renderer.js +18 -0
  40. package/lib/plugin-runtime/im/outbound-segments.d.ts +3 -0
  41. package/lib/plugin-runtime/im/outbound-segments.js +34 -5
  42. package/package.json +15 -15
@@ -148,13 +148,14 @@ function adapterTypeName(packageName) {
148
148
  * - anything else (plain strings, legacy `{ text }` shorthands) passes through.
149
149
  */
150
150
  export async function normalizeOutboundPayload(payload, renderer, options) {
151
+ options?.signal?.throwIfAborted();
151
152
  const mediaPolicy = options?.mediaPolicy ?? DEFAULT_MEDIA_POLICY;
152
153
  if (Array.isArray(payload)) {
153
154
  const resolved = await Promise.all(flattenOutboundArray(payload).map((item) => normalizeOneSegment(item, renderer, mediaPolicy, options)));
154
- return applyOutboundMediaPolicy(resolved, mediaPolicy);
155
+ return applyOutboundMediaPolicy(resolved, mediaPolicy, options?.onFallback);
155
156
  }
156
157
  if (isOutboundSegment(payload)) {
157
- return applyOutboundMediaPolicy([await normalizeOneSegment(payload, renderer, mediaPolicy, options)], mediaPolicy);
158
+ return applyOutboundMediaPolicy([await normalizeOneSegment(payload, renderer, mediaPolicy, options)], mediaPolicy, options?.onFallback);
158
159
  }
159
160
  return payload;
160
161
  }
@@ -181,17 +182,20 @@ async function renderHtmlSegment(segment, renderer, mediaPolicy, options) {
181
182
  const html = typeof data.html === 'string' ? data.html : '';
182
183
  // url-or-text 端点无法投递 base64 图片(本层无上传通道),直接文本降级,跳过渲染。
183
184
  if (options?.htmlPolicy === 'text') {
185
+ if (html)
186
+ options.onFallback?.('html_text_policy');
184
187
  return { type: 'text', data: { text: htmlSegmentFallbackText(data, html) } };
185
188
  }
186
189
  if (html && renderer && mediaPolicy !== 'url-or-text') {
187
190
  try {
188
- const result = await renderer.render(html, {
191
+ const result = await awaitHtmlRender(renderer.render(html, {
192
+ ...(options?.signal ? { signal: options.signal } : {}),
189
193
  width: typeof data.width === 'number' ? data.width : DEFAULT_CARD_WIDTH,
190
194
  format: 'png',
191
195
  ...(typeof data.backgroundColor === 'string'
192
196
  ? { backgroundColor: data.backgroundColor }
193
197
  : {}),
194
- });
198
+ }), options?.signal);
195
199
  if (result.format === 'png' && result.data && typeof result.data === 'object') {
196
200
  const base64 = Buffer.from(result.data).toString('base64');
197
201
  return {
@@ -207,22 +211,46 @@ async function renderHtmlSegment(segment, renderer, mediaPolicy, options) {
207
211
  };
208
212
  }
209
213
  options?.onHtmlFallback?.('renderer_failed');
214
+ options?.onFallback?.('renderer_failed');
210
215
  }
211
216
  catch {
217
+ options?.signal?.throwIfAborted();
212
218
  options?.onHtmlFallback?.('renderer_failed');
219
+ options?.onFallback?.('renderer_failed');
213
220
  }
214
221
  }
215
222
  else if (html) {
216
223
  options?.onHtmlFallback?.(mediaPolicy === 'url-or-text' ? 'media_unsupported' : 'renderer_unavailable');
224
+ options?.onFallback?.(mediaPolicy === 'url-or-text' ? 'media_unsupported' : 'renderer_unavailable');
217
225
  }
218
226
  return { type: 'text', data: { text: htmlSegmentFallbackText(data, html) } };
219
227
  }
228
+ /** Abort the caller promptly even when the optional host cannot cancel its work. */
229
+ async function awaitHtmlRender(work, signal) {
230
+ if (!signal)
231
+ return work;
232
+ let onAbort;
233
+ try {
234
+ return await new Promise((resolve, reject) => {
235
+ onAbort = () => reject(signal.reason);
236
+ signal.addEventListener('abort', onAbort, { once: true });
237
+ // Attach both handlers even when already aborted: late host rejection is
238
+ // observed and cannot become an unhandled rejection after cancellation.
239
+ work.then(resolve, reject);
240
+ if (signal.aborted)
241
+ onAbort();
242
+ });
243
+ }
244
+ finally {
245
+ signal.removeEventListener('abort', onAbort);
246
+ }
247
+ }
220
248
  /**
221
249
  * 媒体协商:仅 `url-or-text` 需要在本层动作——媒体段的非 URL MediaRef
222
250
  * (base64 / 本地路径)无法投递,降级为文本(alt / file_name 优先)。
223
251
  */
224
252
  const MEDIA_SEGMENT_TYPES = new Set(['image', 'audio', 'video', 'file']);
225
- function applyOutboundMediaPolicy(segments, mediaPolicy) {
253
+ function applyOutboundMediaPolicy(segments, mediaPolicy, onFallback) {
226
254
  if (mediaPolicy !== 'url-or-text')
227
255
  return segments;
228
256
  return segments.map((segment) => {
@@ -232,6 +260,7 @@ function applyOutboundMediaPolicy(segments, mediaPolicy) {
232
260
  const media = data.media;
233
261
  if (!isMediaRef(media) || media.kind === 'url')
234
262
  return segment;
263
+ onFallback?.('media_unsupported');
235
264
  const label = typeof data.alt === 'string' && data.alt
236
265
  ? data.alt
237
266
  : typeof data.name === 'string' && data.name
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhin.js/core",
3
- "version": "1.1.42",
3
+ "version": "1.1.43",
4
4
  "description": "Zhin机器人核心框架",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -68,29 +68,29 @@
68
68
  "dependencies": {
69
69
  "qrcode": "^1.5.4",
70
70
  "segment-matcher": "^1.0.5",
71
- "@zhin.js/adapter": "1.1.17",
72
- "@zhin.js/command": "1.1.5",
73
- "@zhin.js/component": "1.1.4",
71
+ "@zhin.js/adapter": "1.1.18",
72
+ "@zhin.js/command": "1.1.6",
74
73
  "@zhin.js/database": "1.1.3",
75
- "@zhin.js/handler": "1.1.3",
74
+ "@zhin.js/component": "1.1.5",
75
+ "@zhin.js/handler": "1.1.4",
76
76
  "@zhin.js/im-contract": "1.1.3",
77
- "@zhin.js/interaction": "1.1.0",
78
- "@zhin.js/kernel": "1.1.2",
77
+ "@zhin.js/interaction": "1.1.1",
79
78
  "@zhin.js/logger": "1.1.1",
80
- "@zhin.js/middleware": "1.1.4",
81
- "@zhin.js/permission": "1.1.4",
82
- "@zhin.js/plugin-runtime": "1.1.12",
83
- "@zhin.js/schedule-feature": "1.1.3",
84
- "@zhin.js/schema": "1.1.1",
85
- "@zhin.js/jsx": "1.1.2",
86
- "@zhin.js/feature-kit": "1.1.3"
79
+ "@zhin.js/middleware": "1.1.5",
80
+ "@zhin.js/permission": "1.1.5",
81
+ "@zhin.js/schedule-feature": "1.1.4",
82
+ "@zhin.js/schema": "1.1.2",
83
+ "@zhin.js/plugin-runtime": "1.1.13",
84
+ "@zhin.js/feature-kit": "1.1.4",
85
+ "@zhin.js/kernel": "1.1.3",
86
+ "@zhin.js/jsx": "1.1.2"
87
87
  },
88
88
  "devDependencies": {
89
89
  "@types/node": "^26.1.2",
90
90
  "@types/qrcode": "^1.5.6",
91
91
  "ajv": "8.18.0",
92
92
  "typescript": "^6.0.3",
93
- "@zhin.js/ai": "1.1.37"
93
+ "@zhin.js/ai": "1.1.38"
94
94
  },
95
95
  "repository": {
96
96
  "type": "git",