@zhin.js/core 1.1.53 → 1.1.54

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 (31) hide show
  1. package/README.md +13 -26
  2. package/lib/built/login-assist.d.ts +12 -3
  3. package/lib/built/login-assist.js +32 -26
  4. package/lib/feature/handler.d.ts +12 -2
  5. package/lib/feature/handler.js +2 -1
  6. package/lib/index.d.ts +3 -1
  7. package/lib/index.js +2 -0
  8. package/lib/plugin-runtime/im/captured-endpoint-operation.d.ts +30 -0
  9. package/lib/plugin-runtime/im/captured-endpoint-operation.js +1 -0
  10. package/lib/plugin-runtime/im/command-menu-navigation.d.ts +8 -1
  11. package/lib/plugin-runtime/im/command-menu-navigation.js +3 -3
  12. package/lib/plugin-runtime/im/contracts.d.ts +69 -48
  13. package/lib/plugin-runtime/im/contracts.js +65 -58
  14. package/lib/plugin-runtime/im/endpoint-runtime.d.ts +9 -14
  15. package/lib/plugin-runtime/im/endpoint-runtime.js +156 -40
  16. package/lib/plugin-runtime/im/group-rule-violation.d.ts +5 -0
  17. package/lib/plugin-runtime/im/im-runtime.d.ts +3 -0
  18. package/lib/plugin-runtime/im/im-runtime.js +29 -4
  19. package/lib/plugin-runtime/im/inbound-runtime.d.ts +2 -0
  20. package/lib/plugin-runtime/im/inbound-runtime.js +138 -33
  21. package/lib/plugin-runtime/im/index.d.ts +1 -1
  22. package/lib/plugin-runtime/im/interaction-operation.d.ts +5 -1
  23. package/lib/plugin-runtime/im/interaction-runtime.d.ts +12 -2
  24. package/lib/plugin-runtime/im/interaction-runtime.js +50 -35
  25. package/lib/plugin-runtime/im/message-dispatcher.js +2 -2
  26. package/lib/plugin-runtime/im/operation-endpoint.d.ts +6 -0
  27. package/lib/plugin-runtime/im/operation-endpoint.js +2 -0
  28. package/lib/plugin-runtime/im/outbound-delivery-runtime.js +40 -9
  29. package/lib/plugin-runtime/im/outbound-renderer.d.ts +12 -3
  30. package/lib/plugin-runtime/im/outbound-renderer.js +20 -13
  31. package/package.json +13 -13
@@ -1,6 +1,7 @@
1
1
  import { createHash } from 'node:crypto';
2
- import { resolveLocale } from '@zhin.js/plugin-runtime';
2
+ import { operationEndpoint } from './operation-endpoint.js';
3
3
  import { projectLocale } from './locale.js';
4
+ import { resolveLocale, } from '@zhin.js/plugin-runtime';
4
5
  import { EndpointDeliveryError } from '@zhin.js/im-contract';
5
6
  import { isCanonicalSegment } from '../../built/segment-contract/assert.js';
6
7
  import { collectSegmentMedia } from '../../built/segment-contract/media.js';
@@ -42,18 +43,30 @@ export class OutboundDeliveryRuntime {
42
43
  }
43
44
  async #deliver(request, snapshot, observeRendering) {
44
45
  const adapter = request.conversation.endpoint.id;
46
+ const boundEndpoint = request[operationEndpoint];
47
+ if (boundEndpoint && boundEndpoint.canonicalId !== adapter)
48
+ throw new Error('Outbound Endpoint binding does not match its conversation');
45
49
  const presentation = resolveOutboundPresentation(snapshot, request.conversation, request.requester);
46
50
  const jsxCache = new WeakMap();
47
51
  const buttons = this.context.prepareButtons?.(request, snapshot);
48
- const renderOptions = { jsxCache, bindButton: buttons?.bind };
52
+ const renderOptions = { jsxCache, bindButton: buttons?.bind, signal: boundEndpoint?.signal };
49
53
  let initialPayload;
50
54
  let initialProjection;
51
55
  try {
52
- const rendered = await this.#renderer.render(request.content, request.requester, snapshot, request.conversation, request.incoming, renderOptions);
53
- initialProjection = await projectOutboundPayload(rendered, request.conversation, snapshot, { deferInteractive: true, presentation });
56
+ const rendered = await this.#renderer.render(request.content, {
57
+ requester: request.requester,
58
+ snapshot: snapshot,
59
+ conversation: request.conversation,
60
+ incoming: request.incoming,
61
+ ...renderOptions,
62
+ });
63
+ initialProjection = await projectOutboundPayload(rendered, request.conversation, snapshot, { deferInteractive: true, presentation, signal: renderOptions.signal });
54
64
  initialPayload = initialProjection.payload;
55
65
  }
56
66
  catch (error) {
67
+ // Preparation precedes the transport boundary; cancellation here is confirmed not sent.
68
+ if (renderOptions.signal?.aborted)
69
+ return failedDeliveryReceipt('endpoint_inactive');
57
70
  if (error instanceof EndpointDeliveryError && error.code === 'unsupported_operation' && error.disposition === 'not_sent')
58
71
  return receiptFromEndpointError(error, false);
59
72
  logRejectedPayload(request.conversation, 'render', error);
@@ -71,7 +84,12 @@ export class OutboundDeliveryRuntime {
71
84
  requester: request.requester,
72
85
  generation: snapshot.generation,
73
86
  clientAdapter: adapters.clientAdapter(adapter),
74
- }, initialPayload, () => adapters.clientById(adapter));
87
+ }, initialPayload, () => {
88
+ if (!boundEndpoint)
89
+ return adapters.clientById(adapter);
90
+ boundEndpoint.assertActive();
91
+ return boundEndpoint.endpoint;
92
+ });
75
93
  // Record identities and fallback text before middleware can mutate the array
76
94
  // or its segments. Do not copy/serialize media buffers or streams.
77
95
  const initialItems = Array.isArray(initialPayload) ? initialPayload.map(item => ({
@@ -93,10 +111,16 @@ export class OutboundDeliveryRuntime {
93
111
  // Native payload replacements remain possible; only authoring values need evaluation.
94
112
  const replacement = envelope.payload;
95
113
  const replaced = replacement !== initialPayload && needsAuthoringRender(replacement)
96
- ? await this.#renderer.render(replacement, request.requester, snapshot, request.conversation, request.incoming, renderOptions)
114
+ ? await this.#renderer.render(replacement, {
115
+ requester: request.requester,
116
+ snapshot: snapshot,
117
+ conversation: request.conversation,
118
+ incoming: request.incoming,
119
+ ...renderOptions,
120
+ })
97
121
  : replacement;
98
122
  projection = await projectOutboundPayload(replaced, request.conversation, snapshot, {
99
- presentation,
123
+ presentation, signal: renderOptions.signal,
100
124
  inheritedFallbacks: retainsInitialProjection(replacement) ? initialProjection.rendering.fallbacks : [],
101
125
  rememberFallback: (map) => {
102
126
  for (const value of Object.values(map))
@@ -110,6 +134,10 @@ export class OutboundDeliveryRuntime {
110
134
  buttonPayloads.add(value);
111
135
  }
112
136
  catch (error) {
137
+ if (renderOptions.signal?.aborted) {
138
+ receipt = failedDeliveryReceipt('endpoint_inactive');
139
+ return;
140
+ }
113
141
  const unsupported = error instanceof EndpointDeliveryError && error.code === 'unsupported_operation' && error.disposition === 'not_sent';
114
142
  if (!unsupported)
115
143
  logRejectedPayload(request.conversation, 'middleware_render', error);
@@ -129,11 +157,14 @@ export class OutboundDeliveryRuntime {
129
157
  const contentSources = canonicalContent?.map(snapshotHistorySource);
130
158
  const payloadLength = Array.isArray(payload) ? payload.length : 1;
131
159
  try {
132
- const result = await requireAdapters(snapshot).send(adapter, {
160
+ const sendRequest = {
133
161
  conversation: request.conversation,
134
162
  locale: resolveLocale(request.locale ?? projectLocale(snapshot)),
135
163
  payload,
136
- });
164
+ };
165
+ const result = boundEndpoint
166
+ ? await boundEndpoint.send(sendRequest)
167
+ : await requireAdapters(snapshot).send(adapter, sendRequest);
137
168
  receipt = receiptFromEndpointResult(result, request.conversation, payloadLength);
138
169
  }
139
170
  catch (error) {
@@ -4,14 +4,23 @@ import { type JSXElement } from '@zhin.js/jsx';
4
4
  import { type MessageRenderOptions } from '../../jsx/message-renderer.js';
5
5
  import { type IncomingContext, type SendContent } from './contracts.js';
6
6
  import { type OutboundProjection } from './outbound-projection.js';
7
- export interface OutboundRenderOptions {
7
+ /** One render operation's author, target, dependencies and shared JSX cache. */
8
+ export interface OutboundRenderContext {
9
+ readonly requester: PluginId;
10
+ readonly snapshot: RuntimeSnapshot;
11
+ readonly conversation?: ConversationRef;
12
+ readonly incoming?: IncomingContext;
8
13
  readonly jsxCache?: WeakMap<JSXElement, Promise<unknown>>;
9
14
  readonly bindButton?: MessageRenderOptions['bindButton'];
10
15
  readonly signal?: AbortSignal;
11
16
  }
17
+ /** Preview requires a concrete target but never sends or registers live buttons. */
18
+ export interface OutboundPreviewContext extends Pick<OutboundRenderContext, 'requester' | 'snapshot' | 'signal'> {
19
+ readonly conversation: ConversationRef;
20
+ }
12
21
  export declare class OutboundRenderer {
13
22
  #private;
14
23
  /** Target projection only: no middleware, transport or live button registration. */
15
- preview(content: SendContent, requester: PluginId, snapshot: RuntimeSnapshot, conversation: ConversationRef, options?: Pick<OutboundRenderOptions, 'signal'>): Promise<OutboundProjection>;
16
- render(content: SendContent, requester: PluginId, snapshot: RuntimeSnapshot, conversation?: ConversationRef, incoming?: IncomingContext, options?: OutboundRenderOptions): Promise<unknown>;
24
+ preview(content: SendContent, context: OutboundPreviewContext): Promise<OutboundProjection>;
25
+ render(content: SendContent, context: OutboundRenderContext): Promise<unknown>;
17
26
  }
@@ -12,7 +12,8 @@ const maxComponentDepth = 32;
12
12
  const TEMPLATE_MARKER = '${';
13
13
  export class OutboundRenderer {
14
14
  /** Target projection only: no middleware, transport or live button registration. */
15
- async preview(content, requester, snapshot, conversation, options = {}) {
15
+ async preview(content, context) {
16
+ const { requester, snapshot, conversation, signal } = context;
16
17
  const adapters = requireAdapters(snapshot);
17
18
  const endpointId = conversation.endpoint.id;
18
19
  if (adapters.clientAdapter(endpointId) !== conversation.endpoint.adapter)
@@ -20,23 +21,29 @@ export class OutboundRenderer {
20
21
  if (!adapters.list().find(item => item.id === endpointId)?.capabilities.includes('outbound')) {
21
22
  throw new Error('Preview endpoint does not support outbound messages');
22
23
  }
23
- options.signal?.throwIfAborted();
24
+ signal?.throwIfAborted();
24
25
  const presentation = resolveOutboundPresentation(snapshot, conversation, requester);
25
26
  const buttons = new JsxButtonRegistry().prepare({ content, requester, conversation }, snapshot);
26
- const rendered = await this.render(content, requester, snapshot, conversation, undefined, { ...options, bindButton: buttons.bind });
27
- const result = await projectOutboundPayload(rendered, conversation, snapshot, { ...options, presentation });
28
- options.signal?.throwIfAborted();
27
+ const rendered = await this.render(content, { requester, snapshot, conversation, signal, bindButton: buttons.bind });
28
+ const result = await projectOutboundPayload(rendered, conversation, snapshot, { signal, presentation });
29
+ signal?.throwIfAborted();
29
30
  return result;
30
31
  }
31
- async render(content, requester, snapshot, conversation, incoming, options = {}) {
32
- options.signal?.throwIfAborted();
32
+ async render(content, context) {
33
+ const { requester, snapshot, conversation, incoming, signal, bindButton } = context;
34
+ signal?.throwIfAborted();
33
35
  const host = resolveComponentHost(snapshot);
34
36
  const ctx = conversation ? buildTemplateContext(conversation, incoming) : undefined;
35
- const rendered = await this.#render(content, requester, snapshot, host, ctx, 0, options.jsxCache ?? new WeakMap(), options.signal, new Set(), options.bindButton);
36
- options.signal?.throwIfAborted();
37
+ const operation = { requester, snapshot, host, template: ctx,
38
+ jsxCache: context.jsxCache ?? new WeakMap(), signal, bindButton };
39
+ const rendered = await this.#render(content, { operation, depth: 0,
40
+ arrayAncestors: new Set(), jsxAncestors: new Set() });
41
+ signal?.throwIfAborted();
37
42
  return rendered;
38
43
  }
39
- async #render(content, requester, snapshot, host, ctx, depth, jsxCache, signal, arrayAncestors, bindButton, jsxAncestors = new Set()) {
44
+ async #render(content, traversal) {
45
+ const { operation, depth, arrayAncestors, jsxAncestors } = traversal;
46
+ const { requester, snapshot, host, template: ctx, jsxCache, signal, bindButton } = operation;
40
47
  signal?.throwIfAborted();
41
48
  if (depth > maxComponentDepth)
42
49
  throw new Error('Component render depth exceeded 32');
@@ -49,7 +56,7 @@ export class OutboundRenderer {
49
56
  throw new RangeError('SendContent array depth exceeded 512');
50
57
  // Each parallel branch gets its own ancestry; repeated sibling arrays are valid.
51
58
  const ancestors = new Set(arrayAncestors).add(content);
52
- const items = await Promise.all(content.map((item) => this.#render(item, requester, snapshot, host, ctx, depth, jsxCache, signal, ancestors, bindButton, jsxAncestors)));
59
+ const items = await Promise.all(content.map((item) => this.#render(item, { ...traversal, arrayAncestors: ancestors })));
53
60
  signal?.throwIfAborted();
54
61
  return flattenOutboundArray(items);
55
62
  }
@@ -69,7 +76,7 @@ export class OutboundRenderer {
69
76
  return [];
70
77
  // Resolve every SendContent shape with the same depth and ancestry
71
78
  // guards as ComponentCall. Root raw payloads remain opaque.
72
- return this.#render(rendered, requester, snapshot, host, ctx, depth + 1, jsxCache, signal, arrayAncestors, bindButton, ancestors);
79
+ return this.#render(rendered, { ...traversal, depth: depth + 1, jsxAncestors: ancestors });
73
80
  };
74
81
  segments = typeof content.type === 'string' && !isNativeMessageTag(content.type)
75
82
  ? renderNamedComponent(content.type, content.props)
@@ -97,7 +104,7 @@ export class OutboundRenderer {
97
104
  const rendered = await requireComponents(snapshot).render(requester, content.name, content.props, { snapshot, ...(signal ? { signal } : {}) });
98
105
  if (rendered == null)
99
106
  return [];
100
- return this.#render(rendered, requester, snapshot, host, ctx, depth + 1, jsxCache, signal, arrayAncestors, bindButton, jsxAncestors);
107
+ return this.#render(rendered, { ...traversal, depth: depth + 1 });
101
108
  }
102
109
  throw new TypeError('Unsupported SendContent');
103
110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhin.js/core",
3
- "version": "1.1.53",
3
+ "version": "1.1.54",
4
4
  "description": "Zhin机器人核心框架",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -57,29 +57,29 @@
57
57
  "dependencies": {
58
58
  "qrcode": "^1.5.4",
59
59
  "segment-matcher": "^1.0.5",
60
- "@zhin.js/adapter": "1.1.25",
60
+ "@zhin.js/adapter": "1.1.26",
61
+ "@zhin.js/command": "1.1.12",
62
+ "@zhin.js/component": "1.1.11",
61
63
  "@zhin.js/database": "1.1.5",
62
- "@zhin.js/handler": "1.1.9",
63
- "@zhin.js/component": "1.1.10",
64
- "@zhin.js/command": "1.1.11",
65
- "@zhin.js/interaction": "1.1.2",
64
+ "@zhin.js/handler": "1.1.10",
66
65
  "@zhin.js/im-contract": "1.1.9",
66
+ "@zhin.js/interaction": "1.1.2",
67
67
  "@zhin.js/kernel": "1.1.5",
68
68
  "@zhin.js/logger": "1.1.2",
69
- "@zhin.js/plugin-runtime": "1.1.17",
70
- "@zhin.js/middleware": "1.1.10",
71
- "@zhin.js/permission": "1.1.9",
69
+ "@zhin.js/middleware": "1.1.11",
70
+ "@zhin.js/permission": "1.1.10",
71
+ "@zhin.js/plugin-runtime": "1.1.18",
72
+ "@zhin.js/schedule-feature": "1.1.10",
72
73
  "@zhin.js/schema": "1.1.4",
73
74
  "@zhin.js/jsx": "1.1.8",
74
- "@zhin.js/feature-kit": "1.1.9",
75
- "@zhin.js/schedule-feature": "1.1.9"
75
+ "@zhin.js/feature-kit": "1.1.10"
76
76
  },
77
77
  "devDependencies": {
78
78
  "@types/node": "^26.1.2",
79
79
  "@types/qrcode": "^1.5.6",
80
80
  "ajv": "8.18.0",
81
81
  "typescript": "^6.0.3",
82
- "@zhin.js/ai": "1.1.48"
82
+ "@zhin.js/ai": "1.1.49"
83
83
  },
84
84
  "repository": {
85
85
  "type": "git",
@@ -108,7 +108,7 @@
108
108
  "features": [
109
109
  {
110
110
  "package": "@zhin.js/adapter",
111
- "api": "^1.0.0"
111
+ "api": "^2.0.0"
112
112
  },
113
113
  {
114
114
  "package": "@zhin.js/command",