@zhin.js/core 1.1.45 → 1.1.47

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 (46) hide show
  1. package/README.md +6 -0
  2. package/lib/ai-completion-contract.d.ts +121 -0
  3. package/lib/ai-completion-contract.js +68 -0
  4. package/lib/built/ai-outbound/prompt.d.ts +2 -0
  5. package/lib/built/ai-outbound/prompt.js +2 -0
  6. package/lib/built/html-to-text.d.ts +0 -3
  7. package/lib/built/html-to-text.js +130 -39
  8. package/lib/built/interactive-segments/resolve.d.ts +2 -2
  9. package/lib/built/interactive-segments/resolve.js +20 -12
  10. package/lib/built/readable-link.d.ts +4 -0
  11. package/lib/built/readable-link.js +32 -0
  12. package/lib/built/rich-segments/markdown-to-text.d.ts +1 -1
  13. package/lib/built/rich-segments/markdown-to-text.js +123 -11
  14. package/lib/built/segment-contract/index.d.ts +1 -1
  15. package/lib/built/segment-contract/index.js +1 -1
  16. package/lib/built/segment-contract/json-schema.d.ts +30 -1
  17. package/lib/built/segment-contract/json-schema.js +93 -20
  18. package/lib/built/segment-contract/validate.d.ts +3 -0
  19. package/lib/built/segment-contract/validate.js +2 -0
  20. package/lib/built/user-interaction.d.ts +1 -1
  21. package/lib/built/user-interaction.js +4 -5
  22. package/lib/index.d.ts +3 -0
  23. package/lib/index.js +2 -0
  24. package/lib/jsx/message-tags.d.ts +1 -0
  25. package/lib/plugin-runtime/im/command-menu-navigation.d.ts +6 -0
  26. package/lib/plugin-runtime/im/command-menu-navigation.js +33 -0
  27. package/lib/plugin-runtime/im/contracts.d.ts +12 -2
  28. package/lib/plugin-runtime/im/contracts.js +24 -2
  29. package/lib/plugin-runtime/im/im-runtime.d.ts +2 -0
  30. package/lib/plugin-runtime/im/im-runtime.js +24 -4
  31. package/lib/plugin-runtime/im/inbound-runtime.d.ts +5 -0
  32. package/lib/plugin-runtime/im/inbound-runtime.js +193 -135
  33. package/lib/plugin-runtime/im/index.d.ts +1 -0
  34. package/lib/plugin-runtime/im/interaction-operation.d.ts +8 -0
  35. package/lib/plugin-runtime/im/interaction-operation.js +2 -0
  36. package/lib/plugin-runtime/im/interaction-runtime.d.ts +5 -3
  37. package/lib/plugin-runtime/im/interaction-runtime.js +150 -51
  38. package/lib/plugin-runtime/im/message-diagnostics.d.ts +70 -0
  39. package/lib/plugin-runtime/im/message-diagnostics.js +182 -0
  40. package/lib/plugin-runtime/im/message-dispatcher.d.ts +1 -1
  41. package/lib/plugin-runtime/im/message-dispatcher.js +41 -5
  42. package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +2 -0
  43. package/lib/plugin-runtime/im/outbound-delivery-runtime.js +7 -0
  44. package/lib/plugin-runtime/im/outbound-segments.d.ts +8 -0
  45. package/lib/plugin-runtime/im/outbound-segments.js +25 -4
  46. package/package.json +13 -13
@@ -19,6 +19,12 @@ export class OutboundDeliveryRuntime {
19
19
  this.#renderer = renderer;
20
20
  }
21
21
  async deliver(request, snapshot) {
22
+ let rendering;
23
+ const receipt = await this.#deliver(request, snapshot, value => { rendering = value; });
24
+ this.context.diagnostics?.delivery(snapshot.generation, request.conversation, receipt, rendering);
25
+ return receipt;
26
+ }
27
+ async #deliver(request, snapshot, observeRendering) {
22
28
  const adapter = request.conversation.endpoint.id;
23
29
  const jsxCache = new WeakMap();
24
30
  const buttons = this.context.prepareButtons?.(request, snapshot);
@@ -80,6 +86,7 @@ export class OutboundDeliveryRuntime {
80
86
  },
81
87
  });
82
88
  payload = projection.payload;
89
+ observeRendering(projection.rendering);
83
90
  for (const value of collectButtonPayloads(payload))
84
91
  buttonPayloads.add(value);
85
92
  }
@@ -1,5 +1,6 @@
1
1
  import type { CapabilityId, HtmlRendererHost, RuntimeSnapshot } from '@zhin.js/plugin-runtime';
2
2
  import { type InteractivePolicy } from '../../built/interactive-segments/types.js';
3
+ import { type AiOutboundSegmentProfile } from '../../built/segment-contract/index.js';
3
4
  import type { OutboundFallbackReason } from './message-events.js';
4
5
  /**
5
6
  * Outbound payload normalization for the Plugin Runtime IM pipeline.
@@ -63,6 +64,13 @@ export declare function isOutboundSegment(value: unknown): value is OutboundSegm
63
64
  export declare function resolveOutboundInteractivePolicy(adapter: CapabilityId, snapshot: RuntimeSnapshot): InteractivePolicy;
64
65
  /** Read declared support from this operation's snapshot without requiring live admission. */
65
66
  export declare function resolveOutboundSupportedSegments(adapter: CapabilityId, snapshot: RuntimeSnapshot): readonly string[] | undefined;
67
+ /**
68
+ * What a model may author for this endpoint: every segment type the outbound
69
+ * projection would deliver rather than reject (markdown and keyboards outside the
70
+ * declared list still qualify when the endpoint degrades them to text), plus the
71
+ * adapter's `segments.model` extensions for those types.
72
+ */
73
+ export declare function resolveAiOutboundSegments(adapter: CapabilityId, snapshot: RuntimeSnapshot): AiOutboundSegmentProfile;
66
74
  /** Preserve semantic Markdown only when the endpoint explicitly declares native consumption. */
67
75
  export declare function resolveOutboundMarkdownPolicy(adapter: CapabilityId, snapshot: RuntimeSnapshot): 'native' | 'text';
68
76
  export declare function applyOutboundMarkdownPolicy(payload: unknown, policy: 'native' | 'text'): unknown;
@@ -3,7 +3,7 @@ import { flattenOutboundArray } from '../../built/outbound-content-arrays.js';
3
3
  import { toCanonicalSegments } from '../../built/generic-segment-mapper.js';
4
4
  import { effectiveKeyboardFallbackMap, renderKeyboardAsText, } from '../../built/interactive-segments/resolve.js';
5
5
  import { DEFAULT_INTERACTIVE_POLICY, isKeyboardSegment, } from '../../built/interactive-segments/types.js';
6
- import { isMediaRef } from '../../built/segment-contract/index.js';
6
+ import { AI_OUTBOUND_SEGMENT_TYPES, DEFAULT_AI_OUTBOUND_SEGMENT_TYPES, isMediaRef, } from '../../built/segment-contract/index.js';
7
7
  import { markdownToPlainText } from '../../built/rich-segments/markdown-to-text.js';
8
8
  /** HTML transport is an adapter capability, independent of its package name. */
9
9
  export function resolveOutboundHtmlPolicy(adapter, snapshot) {
@@ -79,6 +79,26 @@ export function resolveOutboundSupportedSegments(adapter, snapshot) {
79
79
  const segments = slot.definition.segments;
80
80
  return Array.isArray(segments?.supported) && segments.supported.every(type => typeof type === 'string') ? segments.supported : undefined;
81
81
  }
82
+ /**
83
+ * What a model may author for this endpoint: every segment type the outbound
84
+ * projection would deliver rather than reject (markdown and keyboards outside the
85
+ * declared list still qualify when the endpoint degrades them to text), plus the
86
+ * adapter's `segments.model` extensions for those types.
87
+ */
88
+ export function resolveAiOutboundSegments(adapter, snapshot) {
89
+ const supported = resolveOutboundSupportedSegments(adapter, snapshot);
90
+ const degradesToText = (type) => (type === 'markdown' && resolveOutboundMarkdownPolicy(adapter, snapshot) !== 'native')
91
+ || (type === 'keyboard' && resolveOutboundInteractivePolicy(adapter, snapshot) !== 'native');
92
+ const types = supported
93
+ ? AI_OUTBOUND_SEGMENT_TYPES.filter(type => type === 'text' || degradesToText(type) || supported.includes(type))
94
+ : DEFAULT_AI_OUTBOUND_SEGMENT_TYPES;
95
+ const slot = snapshot.capabilities.get(adapter) ?? snapshot.capabilities.get(baseSlotCapabilityId(adapter));
96
+ const declared = slot?.definition?.segments?.model;
97
+ const extensions = declared
98
+ ? Object.fromEntries(types.filter(type => declared[type]).map(type => [type, declared[type]]))
99
+ : {};
100
+ return Object.keys(extensions).length ? { types, extensions } : { types };
101
+ }
82
102
  /** Preserve semantic Markdown only when the endpoint explicitly declares native consumption. */
83
103
  export function resolveOutboundMarkdownPolicy(adapter, snapshot) {
84
104
  const slot = snapshot.capabilities.get(adapter)
@@ -124,9 +144,10 @@ export function applyOutboundInteractivePolicy(payload, policy, remember) {
124
144
  if (!isKeyboardSegment(item))
125
145
  return item;
126
146
  const data = item.data;
127
- const map = remember?.(effectiveKeyboardFallbackMap(data));
128
- const projected = map ? { ...data, fallback: { hint: data.fallback?.hint ?? '请选择(回复数字):', map } } : data;
129
- return { type: 'text', data: { text: renderKeyboardAsText(projected) } };
147
+ const remembered = remember?.(effectiveKeyboardFallbackMap(data));
148
+ const map = remembered && typeof remembered === 'object' && !Array.isArray(remembered) ? remembered : undefined;
149
+ const projected = map ? { ...data, fallback: { hint: data.fallback?.hint ?? '', map } } : data;
150
+ return { type: 'text', data: { text: renderKeyboardAsText(projected, map) } };
130
151
  });
131
152
  }
132
153
  /** 多 endpoint 展开的 record id(`slot.id~entry`)→ slot id(entry 名禁含 `~`,见 adapter-index)。 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhin.js/core",
3
- "version": "1.1.45",
3
+ "version": "1.1.47",
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.19",
61
- "@zhin.js/command": "1.1.7",
62
- "@zhin.js/component": "1.1.6",
60
+ "@zhin.js/adapter": "1.1.21",
61
+ "@zhin.js/command": "1.1.9",
62
+ "@zhin.js/component": "1.1.8",
63
+ "@zhin.js/handler": "1.1.7",
63
64
  "@zhin.js/database": "1.1.4",
64
- "@zhin.js/handler": "1.1.5",
65
- "@zhin.js/im-contract": "1.1.4",
66
65
  "@zhin.js/interaction": "1.1.2",
67
66
  "@zhin.js/logger": "1.1.2",
68
- "@zhin.js/middleware": "1.1.6",
67
+ "@zhin.js/im-contract": "1.1.5",
69
68
  "@zhin.js/kernel": "1.1.4",
70
- "@zhin.js/plugin-runtime": "1.1.14",
71
- "@zhin.js/permission": "1.1.6",
72
- "@zhin.js/schedule-feature": "1.1.5",
69
+ "@zhin.js/middleware": "1.1.8",
70
+ "@zhin.js/permission": "1.1.7",
71
+ "@zhin.js/plugin-runtime": "1.1.15",
72
+ "@zhin.js/schedule-feature": "1.1.7",
73
73
  "@zhin.js/schema": "1.1.3",
74
- "@zhin.js/feature-kit": "1.1.5",
75
- "@zhin.js/jsx": "1.1.3"
74
+ "@zhin.js/jsx": "1.1.4",
75
+ "@zhin.js/feature-kit": "1.1.7"
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.40"
82
+ "@zhin.js/ai": "1.1.42"
83
83
  },
84
84
  "repository": {
85
85
  "type": "git",