@zhin.js/core 1.1.35 → 1.1.38

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 (197) hide show
  1. package/README.md +79 -193
  2. package/lib/built/ai-outbound/index.d.ts +0 -1
  3. package/lib/built/ai-outbound/index.js +0 -1
  4. package/lib/built/ai-outbound/types.d.ts +0 -9
  5. package/lib/built/ai-trigger.d.ts +0 -5
  6. package/lib/built/ai-trigger.js +1 -27
  7. package/lib/built/common-adapter-tools.d.ts +0 -2
  8. package/lib/built/common-adapter-tools.js +0 -4
  9. package/lib/built/html-segment-fallback.d.ts +2 -5
  10. package/lib/built/html-segment-fallback.js +0 -15
  11. package/lib/built/interactive-segments/index.d.ts +0 -2
  12. package/lib/built/interactive-segments/index.js +0 -2
  13. package/lib/built/interactive-segments/keyboard-segment.d.ts +0 -1
  14. package/lib/built/interactive-segments/keyboard-segment.js +0 -1
  15. package/lib/built/interactive-segments/onebot-keyboard.d.ts +0 -1
  16. package/lib/built/interactive-segments/onebot-keyboard.js +0 -2
  17. package/lib/built/interactive-segments/resolve.d.ts +0 -1
  18. package/lib/built/interactive-segments/resolve.js +0 -1
  19. package/lib/built/interactive-segments/types.d.ts +0 -5
  20. package/lib/built/login-assist.d.ts +1 -8
  21. package/lib/built/message-enrich.d.ts +2 -8
  22. package/lib/built/message-enrich.js +3 -11
  23. package/lib/built/segment-contract/assert.js +2 -1
  24. package/lib/built/segment-contract/delivery.js +1 -0
  25. package/lib/built/segment-contract/index.d.ts +2 -2
  26. package/lib/built/segment-contract/index.js +1 -1
  27. package/lib/built/segment-contract/json-schema.d.ts +1 -1
  28. package/lib/built/segment-contract/json-schema.js +33 -2
  29. package/lib/built/segment-contract/preview.js +7 -0
  30. package/lib/built/segment-contract/types.d.ts +2 -2
  31. package/lib/built/segment-contract/validate.d.ts +43 -2
  32. package/lib/built/segment-contract/validate.js +37 -3
  33. package/lib/built/tool-access.d.ts +5 -0
  34. package/lib/built/tool-access.js +17 -0
  35. package/lib/built/user-interaction.js +0 -4
  36. package/lib/feature/handler.d.ts +1 -5
  37. package/lib/index.d.ts +4 -45
  38. package/lib/index.js +3 -39
  39. package/lib/message.d.ts +1 -2
  40. package/lib/models/system-log.d.ts +0 -2
  41. package/lib/models/system-log.js +1 -8
  42. package/lib/models/user.d.ts +0 -2
  43. package/lib/models/user.js +1 -7
  44. package/lib/notice.d.ts +1 -2
  45. package/lib/plugin-runtime/im/conversation-runtime.d.ts +19 -0
  46. package/lib/plugin-runtime/im/conversation-runtime.js +312 -0
  47. package/lib/plugin-runtime/im/endpoint-runtime.d.ts +79 -0
  48. package/lib/plugin-runtime/im/endpoint-runtime.js +162 -0
  49. package/lib/plugin-runtime/im/im-runtime.d.ts +13 -106
  50. package/lib/plugin-runtime/im/im-runtime.js +58 -1061
  51. package/lib/plugin-runtime/im/inbound-runtime.d.ts +31 -0
  52. package/lib/plugin-runtime/im/inbound-runtime.js +252 -0
  53. package/lib/plugin-runtime/im/index.d.ts +4 -2
  54. package/lib/plugin-runtime/im/index.js +2 -2
  55. package/lib/plugin-runtime/im/ingress-route.d.ts +10 -0
  56. package/lib/plugin-runtime/im/ingress-route.js +10 -0
  57. package/lib/plugin-runtime/im/interaction-runtime.d.ts +23 -0
  58. package/lib/plugin-runtime/im/interaction-runtime.js +181 -0
  59. package/lib/plugin-runtime/im/interactive-router.d.ts +17 -0
  60. package/lib/plugin-runtime/im/interactive-router.js +98 -0
  61. package/lib/plugin-runtime/im/message-bus.d.ts +3 -3
  62. package/lib/plugin-runtime/im/message-bus.js +3 -3
  63. package/lib/plugin-runtime/im/message-events.d.ts +26 -0
  64. package/lib/plugin-runtime/im/message-events.js +61 -0
  65. package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +19 -0
  66. package/lib/plugin-runtime/im/outbound-delivery-runtime.js +165 -0
  67. package/lib/plugin-runtime/im/outbound-segments.d.ts +2 -3
  68. package/lib/plugin-runtime/im/outbound-segments.js +3 -4
  69. package/lib/plugin-runtime/im/runtime-middleware.d.ts +3 -0
  70. package/lib/plugin-runtime/im/runtime-middleware.js +13 -0
  71. package/lib/request.d.ts +1 -2
  72. package/lib/side-event/base.d.ts +1 -2
  73. package/lib/side-event/normalize.d.ts +1 -5
  74. package/lib/side-event/normalize.js +1 -11
  75. package/lib/system-event.d.ts +1 -2
  76. package/lib/types.d.ts +9 -116
  77. package/lib/utils.d.ts +51 -24
  78. package/lib/utils.js +11 -23
  79. package/package.json +18 -28
  80. package/lib/adapter.d.ts +0 -145
  81. package/lib/adapter.js +0 -377
  82. package/lib/built/adapter-process.d.ts +0 -58
  83. package/lib/built/adapter-process.js +0 -128
  84. package/lib/built/agent-preset.d.ts +0 -64
  85. package/lib/built/agent-preset.js +0 -79
  86. package/lib/built/ai-outbound/adapter-access.d.ts +0 -3
  87. package/lib/built/ai-outbound/adapter-access.js +0 -9
  88. package/lib/built/authorization.d.ts +0 -10
  89. package/lib/built/authorization.js +0 -76
  90. package/lib/built/command.d.ts +0 -72
  91. package/lib/built/command.js +0 -105
  92. package/lib/built/component.d.ts +0 -67
  93. package/lib/built/component.js +0 -97
  94. package/lib/built/config.d.ts +0 -98
  95. package/lib/built/config.js +0 -372
  96. package/lib/built/connect-endpoint-instance.d.ts +0 -17
  97. package/lib/built/connect-endpoint-instance.js +0 -42
  98. package/lib/built/database.d.ts +0 -68
  99. package/lib/built/database.js +0 -106
  100. package/lib/built/dispatcher.d.ts +0 -141
  101. package/lib/built/dispatcher.js +0 -383
  102. package/lib/built/endpoint-commands.d.ts +0 -3
  103. package/lib/built/endpoint-commands.js +0 -99
  104. package/lib/built/endpoint-lifecycle-service.d.ts +0 -28
  105. package/lib/built/endpoint-lifecycle-service.js +0 -242
  106. package/lib/built/endpoint-lifecycle.d.ts +0 -27
  107. package/lib/built/endpoint-lifecycle.js +0 -36
  108. package/lib/built/endpoint-manager.d.ts +0 -34
  109. package/lib/built/endpoint-manager.js +0 -1
  110. package/lib/built/html-renderer-loader.d.ts +0 -13
  111. package/lib/built/html-renderer-loader.js +0 -33
  112. package/lib/built/inbound-pipeline.d.ts +0 -37
  113. package/lib/built/inbound-pipeline.js +0 -63
  114. package/lib/built/inbound-runner.d.ts +0 -22
  115. package/lib/built/inbound-runner.js +0 -34
  116. package/lib/built/interactive-segment-contract.d.ts +0 -6
  117. package/lib/built/interactive-segment-contract.js +0 -9
  118. package/lib/built/interactive-segments/fallback-store.d.ts +0 -29
  119. package/lib/built/interactive-segments/fallback-store.js +0 -63
  120. package/lib/built/interactive-segments/handlers.d.ts +0 -16
  121. package/lib/built/interactive-segments/handlers.js +0 -74
  122. package/lib/built/introspection-format.d.ts +0 -10
  123. package/lib/built/introspection-format.js +0 -28
  124. package/lib/built/management-command-guard.d.ts +0 -15
  125. package/lib/built/management-command-guard.js +0 -25
  126. package/lib/built/message-filter.d.ts +0 -176
  127. package/lib/built/message-filter.js +0 -239
  128. package/lib/built/optional-peer-import.d.ts +0 -3
  129. package/lib/built/optional-peer-import.js +0 -33
  130. package/lib/built/outbound-media-contract.d.ts +0 -22
  131. package/lib/built/outbound-media-contract.js +0 -10
  132. package/lib/built/permission.d.ts +0 -51
  133. package/lib/built/permission.js +0 -99
  134. package/lib/built/permit-check.d.ts +0 -7
  135. package/lib/built/permit-check.js +0 -35
  136. package/lib/built/permit-parse.d.ts +0 -16
  137. package/lib/built/permit-parse.js +0 -32
  138. package/lib/built/platform-permit.d.ts +0 -20
  139. package/lib/built/platform-permit.js +0 -91
  140. package/lib/built/rich-segments/adapter-policies.d.ts +0 -9
  141. package/lib/built/rich-segments/adapter-policies.js +0 -27
  142. package/lib/built/rich-segments/base.d.ts +0 -11
  143. package/lib/built/rich-segments/base.js +0 -24
  144. package/lib/built/rich-segments/builtins.d.ts +0 -4
  145. package/lib/built/rich-segments/builtins.js +0 -40
  146. package/lib/built/rich-segments/capabilities.d.ts +0 -6
  147. package/lib/built/rich-segments/capabilities.js +0 -38
  148. package/lib/built/rich-segments/html-segment.d.ts +0 -15
  149. package/lib/built/rich-segments/html-segment.js +0 -62
  150. package/lib/built/rich-segments/index.d.ts +0 -13
  151. package/lib/built/rich-segments/index.js +0 -13
  152. package/lib/built/rich-segments/markdown-segment.d.ts +0 -14
  153. package/lib/built/rich-segments/markdown-segment.js +0 -58
  154. package/lib/built/rich-segments/qrcode-segment.d.ts +0 -18
  155. package/lib/built/rich-segments/qrcode-segment.js +0 -73
  156. package/lib/built/rich-segments/registry.d.ts +0 -16
  157. package/lib/built/rich-segments/registry.js +0 -52
  158. package/lib/built/rich-segments/resolve.d.ts +0 -5
  159. package/lib/built/rich-segments/resolve.js +0 -87
  160. package/lib/built/rich-segments/tts-segment.d.ts +0 -13
  161. package/lib/built/rich-segments/tts-segment.js +0 -51
  162. package/lib/built/rich-segments/types.d.ts +0 -79
  163. package/lib/built/rich-segments/types.js +0 -17
  164. package/lib/built/runtime-io.d.ts +0 -9
  165. package/lib/built/runtime-io.js +0 -67
  166. package/lib/built/schedule.d.ts +0 -54
  167. package/lib/built/schedule.js +0 -112
  168. package/lib/built/schema-endpoint-manager.d.ts +0 -17
  169. package/lib/built/schema-endpoint-manager.js +0 -73
  170. package/lib/built/schema-feature.d.ts +0 -77
  171. package/lib/built/schema-feature.js +0 -130
  172. package/lib/built/skill.d.ts +0 -120
  173. package/lib/built/skill.js +0 -151
  174. package/lib/built/speech-loader.d.ts +0 -14
  175. package/lib/built/speech-loader.js +0 -32
  176. package/lib/built/tool.d.ts +0 -151
  177. package/lib/built/tool.js +0 -388
  178. package/lib/command.d.ts +0 -50
  179. package/lib/command.js +0 -106
  180. package/lib/endpoint-capabilities.d.ts +0 -46
  181. package/lib/endpoint-capabilities.js +0 -55
  182. package/lib/endpoint.d.ts +0 -19
  183. package/lib/endpoint.js +0 -3
  184. package/lib/host-plugin-registry.d.ts +0 -8
  185. package/lib/host-plugin-registry.js +0 -12
  186. package/lib/plugin-context.d.ts +0 -27
  187. package/lib/plugin-context.js +0 -64
  188. package/lib/plugin-runtime/im/interactive.d.ts +0 -26
  189. package/lib/plugin-runtime/im/interactive.js +0 -46
  190. package/lib/plugin-runtime/im/service-tokens.d.ts +0 -18
  191. package/lib/plugin-runtime/im/service-tokens.js +0 -13
  192. package/lib/plugin.d.ts +0 -268
  193. package/lib/plugin.js +0 -583
  194. package/lib/schema-interaction.d.ts +0 -117
  195. package/lib/schema-interaction.js +0 -247
  196. package/lib/tool-zod.d.ts +0 -43
  197. package/lib/tool-zod.js +0 -184
@@ -1,58 +0,0 @@
1
- import { segment } from '../../utils.js';
2
- import { RichSegment } from './base.js';
3
- import { markdownToHtmlForRender, markdownToPlainText } from './markdown-to-text.js';
4
- import { RICH_SEGMENT_MODE } from './types.js';
5
- async function resolveHtmlRenderer(ctx) {
6
- if (!ctx)
7
- return undefined;
8
- return ctx.resolveCapability('html-renderer');
9
- }
10
- export class MarkdownSegment extends RichSegment {
11
- segmentType = 'markdown';
12
- type = 'markdown';
13
- async render(mode, ctx) {
14
- if (mode === RICH_SEGMENT_MODE.ORIGIN) {
15
- return { type: 'markdown', data: { content: this.data.content } };
16
- }
17
- if (mode === RICH_SEGMENT_MODE.TEXT) {
18
- return segment.text(markdownToPlainText(this.data.content));
19
- }
20
- if (mode === RICH_SEGMENT_MODE.IMAGE) {
21
- const renderer = await resolveHtmlRenderer(ctx);
22
- if (!renderer) {
23
- return this.render(RICH_SEGMENT_MODE.TEXT, ctx);
24
- }
25
- try {
26
- const html = markdownToHtmlForRender(this.data.content);
27
- const result = await renderer.render(html, {
28
- width: typeof this.data.width === 'number' ? this.data.width : 540,
29
- format: 'png',
30
- backgroundColor: typeof this.data.backgroundColor === 'string' ? this.data.backgroundColor : '#ffffff',
31
- });
32
- if (result.format !== 'png' || typeof result.data !== 'object') {
33
- return this.render(RICH_SEGMENT_MODE.TEXT, ctx);
34
- }
35
- const base64 = Buffer.from(result.data).toString('base64');
36
- const fileName = typeof this.data.fileName === 'string' ? this.data.fileName : 'markdown.png';
37
- return segment('image', { url: `base64://${base64}`, name: fileName });
38
- }
39
- catch {
40
- return this.render(RICH_SEGMENT_MODE.TEXT, ctx);
41
- }
42
- }
43
- return this.render(RICH_SEGMENT_MODE.TEXT, ctx);
44
- }
45
- }
46
- export function wrapMarkdownSegment(data) {
47
- const content = typeof data.content === 'string'
48
- ? data.content
49
- : typeof data.text === 'string'
50
- ? data.text
51
- : '';
52
- return new MarkdownSegment({
53
- content,
54
- width: typeof data.width === 'number' ? data.width : undefined,
55
- backgroundColor: typeof data.backgroundColor === 'string' ? data.backgroundColor : undefined,
56
- fileName: typeof data.fileName === 'string' ? data.fileName : undefined,
57
- });
58
- }
@@ -1,18 +0,0 @@
1
- import type { MessageElement } from '../../types.js';
2
- import { type GenerateQrCodeOptions } from '../generated-qrcode.js';
3
- import { RichSegment } from './base.js';
4
- import { type RichSegmentRenderContext, type RichSegmentRenderResult } from './types.js';
5
- export interface QrcodeSegmentData extends GenerateQrCodeOptions {
6
- text: string;
7
- /** process origin 模式终端渲染时使用 */
8
- small?: boolean;
9
- }
10
- export declare function readQrcodeSegmentData(data: Record<string, unknown>): QrcodeSegmentData;
11
- export declare class QrcodeSegment extends RichSegment<QrcodeSegmentData> {
12
- readonly segmentType: "qrcode";
13
- readonly type: "qrcode";
14
- render(mode: string, _ctx?: RichSegmentRenderContext): Promise<RichSegmentRenderResult>;
15
- }
16
- export declare function wrapQrcodeSegment(data: Record<string, unknown>): QrcodeSegment;
17
- /** process endpoint:origin qrcode 段 → 终端 ASCII + 文本占位 */
18
- export declare function interpretOriginQrcodeForProcess(content: MessageElement | string | (MessageElement | string)[]): Promise<MessageElement | string | (MessageElement | string)[]>;
@@ -1,73 +0,0 @@
1
- import { segment } from '../../utils.js';
2
- import { GeneratedQrCode } from '../generated-qrcode.js';
3
- import { RichSegment } from './base.js';
4
- import { RICH_SEGMENT_MODE } from './types.js';
5
- export function readQrcodeSegmentData(data) {
6
- const text = typeof data.text === 'string' ? data.text : '';
7
- if (!text) {
8
- throw new Error('qrcode segment: missing data.text');
9
- }
10
- return {
11
- text,
12
- width: typeof data.width === 'number' ? data.width : undefined,
13
- margin: typeof data.margin === 'number' ? data.margin : undefined,
14
- errorCorrectionLevel: data.errorCorrectionLevel === 'L'
15
- || data.errorCorrectionLevel === 'M'
16
- || data.errorCorrectionLevel === 'Q'
17
- || data.errorCorrectionLevel === 'H'
18
- ? data.errorCorrectionLevel
19
- : undefined,
20
- small: data.small === true ? true : undefined,
21
- };
22
- }
23
- export class QrcodeSegment extends RichSegment {
24
- segmentType = 'qrcode';
25
- type = 'qrcode';
26
- async render(mode, _ctx) {
27
- if (mode === RICH_SEGMENT_MODE.ORIGIN) {
28
- return this.toJSON();
29
- }
30
- if (mode === RICH_SEGMENT_MODE.TEXT) {
31
- return segment.text(`[二维码] ${this.data.text}`);
32
- }
33
- if (mode === RICH_SEGMENT_MODE.IMAGE) {
34
- const qr = await GeneratedQrCode.generate(this.data.text, this.data);
35
- return segment('image', {
36
- url: qr.toDataUrl(),
37
- base64: qr.toString('base64'),
38
- });
39
- }
40
- return this.render(RICH_SEGMENT_MODE.TEXT, _ctx);
41
- }
42
- }
43
- export function wrapQrcodeSegment(data) {
44
- return new QrcodeSegment(readQrcodeSegmentData(data));
45
- }
46
- /** process endpoint:origin qrcode 段 → 终端 ASCII + 文本占位 */
47
- export async function interpretOriginQrcodeForProcess(content) {
48
- const items = Array.isArray(content) ? content : [content];
49
- const out = [];
50
- let changed = false;
51
- for (const item of items) {
52
- if (typeof item === 'string') {
53
- out.push(item);
54
- continue;
55
- }
56
- if (item.type !== 'qrcode') {
57
- out.push(item);
58
- continue;
59
- }
60
- changed = true;
61
- const payload = readQrcodeSegmentData(item.data ?? {});
62
- const qr = await GeneratedQrCode.generate(payload.text, payload);
63
- await qr.printToTerminal({ small: payload.small ?? true });
64
- out.push(segment.text(`[二维码] ${payload.text}`));
65
- }
66
- if (!changed)
67
- return content;
68
- if (out.length === 0)
69
- return segment.text('');
70
- if (out.length === 1)
71
- return out[0];
72
- return out;
73
- }
@@ -1,16 +0,0 @@
1
- import type { OutboundRichSegmentPolicy, RichSegmentKindDefinition } from './types.js';
2
- export declare class RichSegmentRegistry {
3
- #private;
4
- register(definition: RichSegmentKindDefinition): void;
5
- has(kind: string): boolean;
6
- get(kind: string): RichSegmentKindDefinition | undefined;
7
- list(): RichSegmentKindDefinition[];
8
- buildDefaultPolicy(): OutboundRichSegmentPolicy;
9
- resolveMode(policy: OutboundRichSegmentPolicy, kind: string): string;
10
- wrap(kind: string, data: Record<string, unknown>): import("./types.js").RichSegmentLike;
11
- /** 测试隔离:清空后需重新 register builtins */
12
- clearForTests(): void;
13
- }
14
- export declare const RICH_SEGMENT_FALLBACK_MODE = "origin";
15
- export declare const richSegmentRegistry: RichSegmentRegistry;
16
- export declare function registerRichSegmentKind(definition: RichSegmentKindDefinition): void;
@@ -1,52 +0,0 @@
1
- export class RichSegmentRegistry {
2
- #kinds = new Map();
3
- register(definition) {
4
- if (this.#kinds.has(definition.kind)) {
5
- throw new Error(`Rich segment kind already registered: ${definition.kind}`);
6
- }
7
- if (!definition.modes.includes(definition.defaultMode)) {
8
- throw new Error(`Rich segment ${definition.kind}: defaultMode "${definition.defaultMode}" not in modes`);
9
- }
10
- this.#kinds.set(definition.kind, definition);
11
- }
12
- has(kind) {
13
- return this.#kinds.has(kind);
14
- }
15
- get(kind) {
16
- return this.#kinds.get(kind);
17
- }
18
- list() {
19
- return [...this.#kinds.values()];
20
- }
21
- buildDefaultPolicy() {
22
- const policy = {};
23
- for (const def of this.#kinds.values()) {
24
- policy[def.kind] = def.defaultMode;
25
- }
26
- return policy;
27
- }
28
- resolveMode(policy, kind) {
29
- const def = this.#kinds.get(kind);
30
- const requested = policy[kind] ?? def?.defaultMode ?? RICH_SEGMENT_FALLBACK_MODE;
31
- if (def && !def.modes.includes(requested)) {
32
- return def.defaultMode;
33
- }
34
- return requested;
35
- }
36
- wrap(kind, data) {
37
- const def = this.#kinds.get(kind);
38
- if (!def) {
39
- throw new Error(`Unknown rich segment kind: ${kind}`);
40
- }
41
- return def.wrap(data);
42
- }
43
- /** 测试隔离:清空后需重新 register builtins */
44
- clearForTests() {
45
- this.#kinds.clear();
46
- }
47
- }
48
- export const RICH_SEGMENT_FALLBACK_MODE = 'origin';
49
- export const richSegmentRegistry = new RichSegmentRegistry();
50
- export function registerRichSegmentKind(definition) {
51
- richSegmentRegistry.register(definition);
52
- }
@@ -1,5 +0,0 @@
1
- import type { SendContent } from '../../types.js';
2
- import { type OutboundRichSegmentPolicy, type RichSegmentRenderContext } from './types.js';
3
- export declare function hasRichSegment(content: SendContent | undefined): boolean;
4
- /** 按 Adapter 出站策略在 renderSendMessage 首步统一渲染富媒体段 */
5
- export declare function resolveRichSegments(content: SendContent, policy: OutboundRichSegmentPolicy, ctx?: RichSegmentRenderContext): Promise<SendContent>;
@@ -1,87 +0,0 @@
1
- import { isRichSegment, richSegmentKind } from './base.js';
2
- import { richSegmentRegistry } from './registry.js';
3
- import { RICH_SEGMENT_MODE } from './types.js';
4
- import { CONTENT_CHAIN_STAGE } from '@zhin.js/logger';
5
- function asArray(content) {
6
- return Array.isArray(content) ? content : [content];
7
- }
8
- function packSegments(out) {
9
- if (out.length === 0)
10
- return { type: 'text', data: { text: '' } };
11
- if (out.length === 1)
12
- return out[0];
13
- return out;
14
- }
15
- function appendRendered(out, rendered) {
16
- if (Array.isArray(rendered)) {
17
- out.push(...rendered);
18
- return;
19
- }
20
- out.push(rendered);
21
- }
22
- function primarySegmentType(rendered) {
23
- if (typeof rendered === 'string')
24
- return 'text';
25
- if (Array.isArray(rendered)) {
26
- const first = rendered[0];
27
- if (typeof first === 'string')
28
- return 'text';
29
- const t = first?.type;
30
- return typeof t === 'string' ? t : 'unknown';
31
- }
32
- const t = rendered.type;
33
- return typeof t === 'string' ? t : 'unknown';
34
- }
35
- function detectRichSegmentFallback(mode, renderedType) {
36
- if (mode === RICH_SEGMENT_MODE.IMAGE && renderedType !== 'image')
37
- return true;
38
- if (mode === RICH_SEGMENT_MODE.AUDIO && renderedType !== 'audio')
39
- return true;
40
- if (mode === RICH_SEGMENT_MODE.FILE && renderedType !== 'file')
41
- return true;
42
- return false;
43
- }
44
- export function hasRichSegment(content) {
45
- if (content == null)
46
- return false;
47
- return asArray(content).some((item) => {
48
- if (typeof item === 'string')
49
- return false;
50
- return richSegmentKind(item) != null || isRichSegment(item);
51
- });
52
- }
53
- /** 按 Adapter 出站策略在 renderSendMessage 首步统一渲染富媒体段 */
54
- export async function resolveRichSegments(content, policy, ctx) {
55
- if (!hasRichSegment(content)) {
56
- return content;
57
- }
58
- const items = asArray(content);
59
- const out = [];
60
- for (const item of items) {
61
- if (typeof item === 'string') {
62
- out.push(item);
63
- continue;
64
- }
65
- const kind = isRichSegment(item) ? item.segmentType : richSegmentKind(item);
66
- if (!kind || !richSegmentRegistry.has(kind)) {
67
- out.push(item);
68
- continue;
69
- }
70
- const rich = isRichSegment(item)
71
- ? item
72
- : richSegmentRegistry.wrap(kind, item.data ?? {});
73
- const mode = richSegmentRegistry.resolveMode(policy, kind);
74
- const rendered = await rich.render(mode, ctx);
75
- const renderedType = primarySegmentType(rendered);
76
- const fallback = detectRichSegmentFallback(mode, renderedType);
77
- ctx?.logContentChain?.({
78
- stage: CONTENT_CHAIN_STAGE.RICH_SEGMENT,
79
- kind,
80
- mode,
81
- fallback,
82
- result: renderedType,
83
- });
84
- appendRendered(out, rendered);
85
- }
86
- return packSegments(out);
87
- }
@@ -1,13 +0,0 @@
1
- import { RichSegment } from './base.js';
2
- import { type RichSegmentRenderContext, type RichSegmentRenderResult } from './types.js';
3
- export interface TtsSegmentData {
4
- text: string;
5
- voice?: string;
6
- provider?: string;
7
- }
8
- export declare class TtsSegment extends RichSegment<TtsSegmentData> {
9
- readonly segmentType: "tts";
10
- readonly type: "tts";
11
- render(mode: string, ctx?: RichSegmentRenderContext): Promise<RichSegmentRenderResult>;
12
- }
13
- export declare function wrapTtsSegment(data: Record<string, unknown>): TtsSegment;
@@ -1,51 +0,0 @@
1
- import { segment } from '../../utils.js';
2
- import { RichSegment } from './base.js';
3
- import { RICH_SEGMENT_MODE } from './types.js';
4
- async function resolveSpeechPipeline(ctx) {
5
- if (!ctx?.resolveCapability)
6
- return undefined;
7
- return ctx.resolveCapability('speech');
8
- }
9
- export class TtsSegment extends RichSegment {
10
- segmentType = 'tts';
11
- type = 'tts';
12
- async render(mode, ctx) {
13
- if (mode === RICH_SEGMENT_MODE.ORIGIN) {
14
- return this.toJSON();
15
- }
16
- const text = typeof this.data.text === 'string' ? this.data.text : '';
17
- if (mode === RICH_SEGMENT_MODE.TEXT) {
18
- return segment.text(text);
19
- }
20
- if (mode === RICH_SEGMENT_MODE.AUDIO) {
21
- if (!text.trim()) {
22
- return segment.text('');
23
- }
24
- const pipeline = await resolveSpeechPipeline(ctx);
25
- if (!pipeline) {
26
- return this.render(RICH_SEGMENT_MODE.TEXT, ctx);
27
- }
28
- try {
29
- const result = await pipeline.synthesize({
30
- text,
31
- voice: typeof this.data.voice === 'string' ? this.data.voice : undefined,
32
- provider: typeof this.data.provider === 'string' ? this.data.provider : undefined,
33
- });
34
- const base64 = result.data.toString('base64');
35
- const mime = result.format === 'wav' ? 'audio/wav' : 'audio/mpeg';
36
- return segment('audio', { url: `data:${mime};base64,${base64}`, format: result.format });
37
- }
38
- catch {
39
- return this.render(RICH_SEGMENT_MODE.TEXT, ctx);
40
- }
41
- }
42
- return this.render(RICH_SEGMENT_MODE.TEXT, ctx);
43
- }
44
- }
45
- export function wrapTtsSegment(data) {
46
- return new TtsSegment({
47
- text: String(data.text ?? ''),
48
- voice: typeof data.voice === 'string' ? data.voice : undefined,
49
- provider: typeof data.provider === 'string' ? data.provider : undefined,
50
- });
51
- }
@@ -1,79 +0,0 @@
1
- import type { MessageElement } from '../../types.js';
2
- import type { ContentChainLogFields } from '@zhin.js/logger';
3
- /** 内置 kind 常量(外部可增删,registry 为 SSOT) */
4
- export declare const BUILTIN_RICH_SEGMENT_KINDS: {
5
- readonly QRCODE: "qrcode";
6
- readonly HTML: "html";
7
- readonly MARKDOWN: "markdown";
8
- readonly TTS: "tts";
9
- };
10
- export type BuiltinRichSegmentKind = (typeof BUILTIN_RICH_SEGMENT_KINDS)[keyof typeof BUILTIN_RICH_SEGMENT_KINDS];
11
- /** 通用 mode 名;各 kind 在 registry 中声明支持的子集 */
12
- export declare const RICH_SEGMENT_MODE: {
13
- readonly ORIGIN: "origin";
14
- readonly TEXT: "text";
15
- readonly IMAGE: "image";
16
- readonly FILE: "file";
17
- readonly URL: "url";
18
- readonly AUDIO: "audio";
19
- readonly VIDEO: "video";
20
- };
21
- export type RichSegmentMode = (typeof RICH_SEGMENT_MODE)[keyof typeof RICH_SEGMENT_MODE] | string;
22
- /** 任意已注册 kind → 渲染 mode */
23
- export type OutboundRichSegmentPolicy = Record<string, string>;
24
- /** html-renderer 可选包契约 */
25
- export interface HtmlRendererForRichSegment {
26
- render(html: string, options?: {
27
- width?: number;
28
- format?: 'png';
29
- backgroundColor?: string;
30
- }): Promise<{
31
- format: string;
32
- data: Buffer | string;
33
- }>;
34
- }
35
- /** speech 可选包契约(@zhin.js/speech) */
36
- export interface SpeechPipelineForRichSegment {
37
- transcribe(input: {
38
- data: Buffer;
39
- mimeType?: string;
40
- }): Promise<string>;
41
- synthesize(input: {
42
- text: string;
43
- voice?: string;
44
- format?: 'mp3' | 'wav';
45
- speed?: number;
46
- provider?: string;
47
- }): Promise<{
48
- data: Buffer;
49
- format: 'mp3' | 'wav';
50
- }>;
51
- }
52
- /** 未来 media-pipeline / ffmpeg 等 optional 包可复用同一 id */
53
- export type RichSegmentCapabilityId = 'html-renderer' | 'speech' | 'media-pipeline' | string;
54
- export interface RichSegmentRenderContext {
55
- /** 按 id 懒加载能力(core 或 optional 包 register loader) */
56
- resolveCapability: <T>(id: RichSegmentCapabilityId) => Promise<T | undefined>;
57
- /** 可选:rich_segment stage 结构化日志 */
58
- logContentChain?: (fields: ContentChainLogFields) => void;
59
- warn?: (message: string) => void;
60
- }
61
- export type RichSegmentRenderResult = MessageElement | MessageElement[];
62
- export interface RichSegmentLike {
63
- readonly segmentType: string;
64
- render(mode: string, ctx?: RichSegmentRenderContext): Promise<RichSegmentRenderResult>;
65
- }
66
- export interface RichSegmentKindDefinition {
67
- kind: string;
68
- /** policy 未指定时使用的 mode */
69
- defaultMode: string;
70
- /** 该 kind 支持的 mode 列表(policy 非法值会回退 defaultMode) */
71
- modes: readonly string[];
72
- wrap: (data: Record<string, unknown>) => RichSegmentLike;
73
- }
74
- export interface RichSegmentCapabilityLoaderOptions {
75
- warn?: (message: string) => void;
76
- getConfig?: () => Record<string, unknown> | undefined;
77
- logContentChain?: RichSegmentRenderContext['logContentChain'];
78
- }
79
- export type RichSegmentCapabilityLoader = (options: RichSegmentCapabilityLoaderOptions) => Promise<unknown>;
@@ -1,17 +0,0 @@
1
- /** 内置 kind 常量(外部可增删,registry 为 SSOT) */
2
- export const BUILTIN_RICH_SEGMENT_KINDS = {
3
- QRCODE: 'qrcode',
4
- HTML: 'html',
5
- MARKDOWN: 'markdown',
6
- TTS: 'tts',
7
- };
8
- /** 通用 mode 名;各 kind 在 registry 中声明支持的子集 */
9
- export const RICH_SEGMENT_MODE = {
10
- ORIGIN: 'origin',
11
- TEXT: 'text',
12
- IMAGE: 'image',
13
- FILE: 'file',
14
- URL: 'url',
15
- AUDIO: 'audio',
16
- VIDEO: 'video',
17
- };
@@ -1,9 +0,0 @@
1
- /** Node / Deno 共用的进程身份与 stdin 绑定(供 ProcessAdapter 使用) */
2
- export declare function runtimePid(): string;
3
- export declare function runtimeUser(): string;
4
- /** 显式允许在非 TTY 场景绑定 stdin(如 `deno task dev --watch` 子进程) */
5
- export declare function shouldBindProcessStdin(): boolean;
6
- /** 是否适合绑定交互式 stdin(Deploy / 管道场景为 false) */
7
- export declare function isInteractiveStdin(): boolean;
8
- export declare function isDenoDeploy(): boolean;
9
- export declare function bindStdin(onChunk: (chunk: string) => void): () => void;
@@ -1,67 +0,0 @@
1
- /** Node / Deno 共用的进程身份与 stdin 绑定(供 ProcessAdapter 使用) */
2
- export function runtimePid() {
3
- if (typeof Deno !== "undefined")
4
- return String(Deno.pid);
5
- return String(process.pid);
6
- }
7
- export function runtimeUser() {
8
- if (typeof Deno !== "undefined") {
9
- return Deno.env.get("USER") ?? Deno.env.get("USERNAME") ?? "deno";
10
- }
11
- return process.env.USER ?? "user";
12
- }
13
- /** 显式允许在非 TTY 场景绑定 stdin(如 `deno task dev --watch` 子进程) */
14
- export function shouldBindProcessStdin() {
15
- if (isDenoDeploy())
16
- return false;
17
- const force = envFlag("ZHIN_BIND_STDIN");
18
- if (force === "0" || force === "false")
19
- return false;
20
- if (force === "1" || force === "true")
21
- return true;
22
- return isInteractiveStdin();
23
- }
24
- function envFlag(key) {
25
- if (typeof Deno !== "undefined") {
26
- return Deno.env.get(key)?.trim().toLowerCase();
27
- }
28
- return process.env[key]?.trim().toLowerCase();
29
- }
30
- /** 是否适合绑定交互式 stdin(Deploy / 管道场景为 false) */
31
- export function isInteractiveStdin() {
32
- if (typeof Deno !== "undefined") {
33
- try {
34
- return Deno.stdin.isTerminal();
35
- }
36
- catch {
37
- return false;
38
- }
39
- }
40
- return Boolean(process.stdin.isTTY);
41
- }
42
- export function isDenoDeploy() {
43
- return typeof Deno !== "undefined" &&
44
- Deno.env.get("DENO_DEPLOYMENT_ID") != null;
45
- }
46
- export function bindStdin(onChunk) {
47
- if (typeof Deno !== "undefined") {
48
- const ac = new AbortController();
49
- const decoder = new TextDecoder();
50
- void (async () => {
51
- try {
52
- for await (const bytes of Deno.stdin.readable) {
53
- if (ac.signal.aborted)
54
- break;
55
- onChunk(decoder.decode(bytes));
56
- }
57
- }
58
- catch {
59
- /* aborted or stream closed */
60
- }
61
- })();
62
- return () => ac.abort();
63
- }
64
- const handler = (data) => onChunk(data.toString());
65
- process.stdin.on("data", handler);
66
- return () => process.stdin.removeListener("data", handler);
67
- }
@@ -1,54 +0,0 @@
1
- /**
2
- * ScheduleFeature — 管理插件注册的内存定时任务
3
- */
4
- import { Feature, FeatureJSON, type ScheduleKind, type HolidayInput, type ScatterInput } from '@zhin.js/kernel';
5
- export interface ScheduleDescriptor {
6
- kind: ScheduleKind | 'every' | 'at';
7
- /** 6 段 cron(solar/lunar/workday/freeDay/holiday) */
8
- cron?: string;
9
- everyMs?: number;
10
- atMs?: number;
11
- scatterInput?: ScatterInput;
12
- holidayInput?: HolidayInput;
13
- tz?: string;
14
- }
15
- export interface ScheduleHandle {
16
- id: string;
17
- descriptor: ScheduleDescriptor;
18
- dispose: () => void;
19
- }
20
- export interface ScheduleContextExtensions {
21
- addSchedule(descriptor: ScheduleDescriptor, callback: () => void | Promise<void>, id?: string): () => void;
22
- }
23
- declare module '../plugin.js' {
24
- namespace Plugin {
25
- interface Extensions extends ScheduleContextExtensions {
26
- }
27
- interface Contexts {
28
- schedule: ScheduleFeature;
29
- }
30
- }
31
- }
32
- export declare class ScheduleFeature extends Feature<ScheduleHandle> {
33
- readonly name: "schedule";
34
- readonly icon = "Clock";
35
- readonly desc = "\u65E5\u5386\u8C03\u5EA6";
36
- readonly byName: Map<string, ScheduleHandle>;
37
- add(handle: ScheduleHandle, pluginName: string): () => void;
38
- remove(handle: ScheduleHandle): boolean;
39
- get(id: string): ScheduleHandle | undefined;
40
- stopAll(): void;
41
- getStatus(): Array<{
42
- id: string;
43
- kind: string;
44
- expression?: string;
45
- running: boolean;
46
- nextExecution: Date | null;
47
- plugin: string;
48
- }>;
49
- dispose(): void;
50
- toJSON(pluginName?: string): FeatureJSON;
51
- get extensions(): {
52
- addSchedule(descriptor: ScheduleDescriptor, callback: () => void | Promise<void>, id?: string): () => void;
53
- };
54
- }