@zhin.js/core 1.1.34 → 1.1.37
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.
- package/README.md +84 -142
- package/lib/built/ai-access.d.ts +36 -0
- package/lib/built/ai-access.js +46 -0
- package/lib/built/ai-outbound/extensions/onebot-keyboard.d.ts +2 -0
- package/lib/built/ai-outbound/extensions/onebot-keyboard.js +67 -0
- package/lib/built/ai-outbound/extensions/qq-message.d.ts +3 -0
- package/lib/built/ai-outbound/extensions/qq-message.js +23 -0
- package/lib/built/ai-outbound/index.d.ts +8 -0
- package/lib/built/ai-outbound/index.js +8 -0
- package/lib/built/ai-outbound/parse.d.ts +13 -0
- package/lib/built/ai-outbound/parse.js +104 -0
- package/lib/built/ai-outbound/plain-mention-rewrite.d.ts +6 -0
- package/lib/built/ai-outbound/plain-mention-rewrite.js +37 -0
- package/lib/built/ai-outbound/prompt.d.ts +11 -0
- package/lib/built/ai-outbound/prompt.js +44 -0
- package/lib/built/ai-outbound/resolve.d.ts +4 -0
- package/lib/built/ai-outbound/resolve.js +64 -0
- package/lib/built/ai-outbound/structured-detect.d.ts +3 -0
- package/lib/built/ai-outbound/structured-detect.js +6 -0
- package/lib/built/ai-outbound/types.d.ts +49 -0
- package/lib/built/ai-outbound/types.js +5 -0
- package/lib/built/ai-trigger.d.ts +12 -17
- package/lib/built/ai-trigger.js +39 -70
- package/lib/built/common-adapter-tools.d.ts +33 -29
- package/lib/built/common-adapter-tools.js +79 -67
- package/lib/built/generated-qrcode.d.ts +24 -0
- package/lib/built/generated-qrcode.js +45 -0
- package/lib/built/generic-segment-mapper.d.ts +4 -0
- package/lib/built/generic-segment-mapper.js +190 -0
- package/lib/built/html-segment-fallback.d.ts +2 -5
- package/lib/built/html-segment-fallback.js +0 -16
- package/lib/built/html-to-text.d.ts +0 -1
- package/lib/built/html-to-text.js +15 -15
- package/lib/built/interactive-segments/action.d.ts +17 -0
- package/lib/built/interactive-segments/action.js +69 -0
- package/lib/built/interactive-segments/button-spec.d.ts +10 -0
- package/lib/built/interactive-segments/button-spec.js +21 -0
- package/lib/built/interactive-segments/index.d.ts +6 -0
- package/lib/built/interactive-segments/index.js +6 -0
- package/lib/built/interactive-segments/keyboard-segment.d.ts +10 -0
- package/lib/built/interactive-segments/keyboard-segment.js +11 -0
- package/lib/built/interactive-segments/onebot-keyboard.d.ts +6 -0
- package/lib/built/interactive-segments/onebot-keyboard.js +49 -0
- package/lib/built/interactive-segments/resolve.d.ts +14 -0
- package/lib/built/interactive-segments/resolve.js +109 -0
- package/lib/built/interactive-segments/types.d.ts +45 -0
- package/lib/built/interactive-segments/types.js +9 -0
- package/lib/built/login-assist.d.ts +43 -16
- package/lib/built/login-assist.js +113 -11
- package/lib/built/message-enrich.d.ts +32 -0
- package/lib/built/message-enrich.js +67 -0
- package/lib/built/outbound-media-utils.d.ts +9 -0
- package/lib/built/outbound-media-utils.js +62 -0
- package/lib/built/queue-im-field-contract.d.ts +7 -2
- package/lib/built/queue-im-field-contract.js +13 -4
- package/lib/built/rich-segments/markdown-to-text.d.ts +3 -0
- package/lib/built/rich-segments/markdown-to-text.js +27 -0
- package/lib/built/roles.d.ts +4 -5
- package/lib/built/roles.js +4 -5
- package/lib/built/segment-contract/assert.d.ts +7 -0
- package/lib/built/segment-contract/assert.js +39 -0
- package/lib/built/segment-contract/delivery.d.ts +5 -0
- package/lib/built/segment-contract/delivery.js +40 -0
- package/lib/built/segment-contract/image.d.ts +5 -0
- package/lib/built/segment-contract/image.js +10 -0
- package/lib/built/segment-contract/index.d.ts +10 -0
- package/lib/built/segment-contract/index.js +9 -0
- package/lib/built/segment-contract/json-schema.d.ts +28 -0
- package/lib/built/segment-contract/json-schema.js +128 -0
- package/lib/built/segment-contract/media.d.ts +12 -0
- package/lib/built/segment-contract/media.js +22 -0
- package/lib/built/segment-contract/mention.d.ts +10 -0
- package/lib/built/segment-contract/mention.js +26 -0
- package/lib/built/segment-contract/preview.d.ts +3 -0
- package/lib/built/segment-contract/preview.js +159 -0
- package/lib/built/segment-contract/text.d.ts +7 -0
- package/lib/built/segment-contract/text.js +34 -0
- package/lib/built/segment-contract/types.d.ts +58 -0
- package/lib/built/segment-contract/types.js +1 -0
- package/lib/built/segment-contract/validate.d.ts +229 -0
- package/lib/built/segment-contract/validate.js +172 -0
- package/lib/built/tool-access.d.ts +5 -0
- package/lib/built/tool-access.js +17 -0
- package/lib/built/user-interaction.d.ts +30 -0
- package/lib/built/user-interaction.js +244 -0
- package/lib/component.d.ts +0 -1
- package/lib/component.js +30 -14
- package/lib/errors.d.ts +2 -3
- package/lib/errors.js +4 -5
- package/lib/feature/adapter.d.ts +2 -0
- package/lib/feature/adapter.js +2 -0
- package/lib/feature/command.d.ts +2 -0
- package/lib/feature/command.js +2 -0
- package/lib/feature/component.d.ts +2 -0
- package/lib/feature/component.js +2 -0
- package/lib/feature/handler.d.ts +48 -0
- package/lib/feature/handler.js +26 -0
- package/lib/feature/middleware.d.ts +2 -0
- package/lib/feature/middleware.js +2 -0
- package/lib/im-scene.d.ts +29 -0
- package/lib/im-scene.js +75 -0
- package/lib/im-session-id.d.ts +39 -0
- package/lib/im-session-id.js +47 -0
- package/lib/index.d.ts +29 -36
- package/lib/index.js +27 -41
- package/lib/jsx-dev-runtime.d.ts +0 -1
- package/lib/jsx-dev-runtime.js +0 -1
- package/lib/jsx-runtime.d.ts +1 -1
- package/lib/jsx-runtime.js +0 -1
- package/lib/jsx.d.ts +19 -22
- package/lib/jsx.js +0 -1
- package/lib/message.d.ts +17 -12
- package/lib/message.js +16 -13
- package/lib/models/system-log.d.ts +0 -3
- package/lib/models/system-log.js +1 -9
- package/lib/models/user.d.ts +0 -3
- package/lib/models/user.js +1 -8
- package/lib/notice.d.ts +16 -60
- package/lib/notice.js +1 -4
- package/lib/plugin-runtime/im/contracts.d.ts +191 -0
- package/lib/plugin-runtime/im/contracts.js +169 -0
- package/lib/plugin-runtime/im/conversation-runtime.d.ts +19 -0
- package/lib/plugin-runtime/im/conversation-runtime.js +312 -0
- package/lib/plugin-runtime/im/endpoint-runtime.d.ts +79 -0
- package/lib/plugin-runtime/im/endpoint-runtime.js +162 -0
- package/lib/plugin-runtime/im/im-runtime.d.ts +84 -0
- package/lib/plugin-runtime/im/im-runtime.js +201 -0
- package/lib/plugin-runtime/im/inbound-runtime.d.ts +31 -0
- package/lib/plugin-runtime/im/inbound-runtime.js +252 -0
- package/lib/plugin-runtime/im/index.d.ts +11 -0
- package/lib/plugin-runtime/im/index.js +9 -0
- package/lib/plugin-runtime/im/ingress-route.d.ts +10 -0
- package/lib/plugin-runtime/im/ingress-route.js +10 -0
- package/lib/plugin-runtime/im/interaction-runtime.d.ts +23 -0
- package/lib/plugin-runtime/im/interaction-runtime.js +181 -0
- package/lib/plugin-runtime/im/interactive-router.d.ts +17 -0
- package/lib/plugin-runtime/im/interactive-router.js +98 -0
- package/lib/plugin-runtime/im/login-assist-host.d.ts +6 -0
- package/lib/plugin-runtime/im/login-assist-host.js +6 -0
- package/lib/plugin-runtime/im/message-bus.d.ts +38 -0
- package/lib/plugin-runtime/im/message-bus.js +40 -0
- package/lib/plugin-runtime/im/message-dispatcher.d.ts +18 -0
- package/lib/plugin-runtime/im/message-dispatcher.js +107 -0
- package/lib/plugin-runtime/im/message-events.d.ts +26 -0
- package/lib/plugin-runtime/im/message-events.js +61 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +19 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.js +165 -0
- package/lib/plugin-runtime/im/outbound-renderer.d.ts +7 -0
- package/lib/plugin-runtime/im/outbound-renderer.js +70 -0
- package/lib/plugin-runtime/im/outbound-segments.d.ts +71 -0
- package/lib/plugin-runtime/im/outbound-segments.js +219 -0
- package/lib/plugin-runtime/im/public-api.d.ts +6 -0
- package/lib/plugin-runtime/im/public-api.js +6 -0
- package/lib/plugin-runtime/im/runtime-middleware.d.ts +3 -0
- package/lib/plugin-runtime/im/runtime-middleware.js +13 -0
- package/lib/request.d.ts +17 -62
- package/lib/request.js +0 -4
- package/lib/side-event/base.d.ts +51 -0
- package/lib/side-event/base.js +30 -0
- package/lib/side-event/dispatch.d.ts +15 -0
- package/lib/side-event/dispatch.js +140 -0
- package/lib/side-event/index.d.ts +4 -0
- package/lib/side-event/index.js +4 -0
- package/lib/side-event/normalize.d.ts +38 -0
- package/lib/side-event/normalize.js +111 -0
- package/lib/side-event/types.d.ts +23 -0
- package/lib/side-event/types.js +55 -0
- package/lib/system-event.d.ts +14 -0
- package/lib/system-event.js +7 -0
- package/lib/types.d.ts +35 -193
- package/lib/types.js +0 -1
- package/lib/utils.d.ts +81 -6
- package/lib/utils.js +54 -25
- package/package.json +94 -16
- package/lib/adapter.d.ts +0 -96
- package/lib/adapter.d.ts.map +0 -1
- package/lib/adapter.js +0 -217
- package/lib/adapter.js.map +0 -1
- package/lib/agent-prompt.d.ts +0 -37
- package/lib/agent-prompt.d.ts.map +0 -1
- package/lib/agent-prompt.js +0 -2
- package/lib/agent-prompt.js.map +0 -1
- package/lib/bot.d.ts +0 -38
- package/lib/bot.d.ts.map +0 -1
- package/lib/bot.js +0 -2
- package/lib/bot.js.map +0 -1
- package/lib/built/adapter-process.d.ts +0 -43
- package/lib/built/adapter-process.d.ts.map +0 -1
- package/lib/built/adapter-process.js +0 -81
- package/lib/built/adapter-process.js.map +0 -1
- package/lib/built/agent-preset.d.ts +0 -65
- package/lib/built/agent-preset.d.ts.map +0 -1
- package/lib/built/agent-preset.js +0 -80
- package/lib/built/agent-preset.js.map +0 -1
- package/lib/built/ai-trigger.d.ts.map +0 -1
- package/lib/built/ai-trigger.js.map +0 -1
- package/lib/built/command.d.ts +0 -68
- package/lib/built/command.d.ts.map +0 -1
- package/lib/built/command.js +0 -92
- package/lib/built/command.js.map +0 -1
- package/lib/built/common-adapter-tools.d.ts.map +0 -1
- package/lib/built/common-adapter-tools.js.map +0 -1
- package/lib/built/component.d.ts +0 -69
- package/lib/built/component.d.ts.map +0 -1
- package/lib/built/component.js +0 -99
- package/lib/built/component.js.map +0 -1
- package/lib/built/config.d.ts +0 -99
- package/lib/built/config.d.ts.map +0 -1
- package/lib/built/config.js +0 -355
- package/lib/built/config.js.map +0 -1
- package/lib/built/cron.d.ts +0 -75
- package/lib/built/cron.d.ts.map +0 -1
- package/lib/built/cron.js +0 -122
- package/lib/built/cron.js.map +0 -1
- package/lib/built/database.d.ts +0 -66
- package/lib/built/database.d.ts.map +0 -1
- package/lib/built/database.js +0 -100
- package/lib/built/database.js.map +0 -1
- package/lib/built/dispatcher.d.ts +0 -139
- package/lib/built/dispatcher.d.ts.map +0 -1
- package/lib/built/dispatcher.js +0 -361
- package/lib/built/dispatcher.js.map +0 -1
- package/lib/built/html-segment-fallback.d.ts.map +0 -1
- package/lib/built/html-segment-fallback.js.map +0 -1
- package/lib/built/html-to-text.d.ts.map +0 -1
- package/lib/built/html-to-text.js.map +0 -1
- package/lib/built/inbound-runner.d.ts +0 -23
- package/lib/built/inbound-runner.d.ts.map +0 -1
- package/lib/built/inbound-runner.js +0 -37
- package/lib/built/inbound-runner.js.map +0 -1
- package/lib/built/login-assist.d.ts.map +0 -1
- package/lib/built/login-assist.js.map +0 -1
- package/lib/built/mcp-mesh-registrar.d.ts +0 -9
- package/lib/built/mcp-mesh-registrar.d.ts.map +0 -1
- package/lib/built/mcp-mesh-registrar.js +0 -17
- package/lib/built/mcp-mesh-registrar.js.map +0 -1
- package/lib/built/message-filter.d.ts +0 -177
- package/lib/built/message-filter.d.ts.map +0 -1
- package/lib/built/message-filter.js +0 -241
- package/lib/built/message-filter.js.map +0 -1
- package/lib/built/permission.d.ts +0 -55
- package/lib/built/permission.d.ts.map +0 -1
- package/lib/built/permission.js +0 -112
- package/lib/built/permission.js.map +0 -1
- package/lib/built/prepend-quote-context.d.ts +0 -35
- package/lib/built/prepend-quote-context.d.ts.map +0 -1
- package/lib/built/prepend-quote-context.js +0 -131
- package/lib/built/prepend-quote-context.js.map +0 -1
- package/lib/built/queue-im-field-contract.d.ts.map +0 -1
- package/lib/built/queue-im-field-contract.js.map +0 -1
- package/lib/built/roles.d.ts.map +0 -1
- package/lib/built/roles.js.map +0 -1
- package/lib/built/runtime-io.d.ts +0 -10
- package/lib/built/runtime-io.d.ts.map +0 -1
- package/lib/built/runtime-io.js +0 -68
- package/lib/built/runtime-io.js.map +0 -1
- package/lib/built/schema-feature.d.ts +0 -78
- package/lib/built/schema-feature.d.ts.map +0 -1
- package/lib/built/schema-feature.js +0 -132
- package/lib/built/schema-feature.js.map +0 -1
- package/lib/built/skill.d.ts +0 -121
- package/lib/built/skill.d.ts.map +0 -1
- package/lib/built/skill.js +0 -152
- package/lib/built/skill.js.map +0 -1
- package/lib/built/tool.d.ts +0 -150
- package/lib/built/tool.d.ts.map +0 -1
- package/lib/built/tool.js +0 -383
- package/lib/built/tool.js.map +0 -1
- package/lib/command.d.ts +0 -42
- package/lib/command.d.ts.map +0 -1
- package/lib/command.js +0 -75
- package/lib/command.js.map +0 -1
- package/lib/component.d.ts.map +0 -1
- package/lib/component.js.map +0 -1
- package/lib/errors.d.ts.map +0 -1
- package/lib/errors.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/jsx-dev-runtime.d.ts.map +0 -1
- package/lib/jsx-dev-runtime.js.map +0 -1
- package/lib/jsx-runtime.d.ts.map +0 -1
- package/lib/jsx-runtime.js.map +0 -1
- package/lib/jsx.d.ts.map +0 -1
- package/lib/jsx.js.map +0 -1
- package/lib/message-quote.d.ts +0 -11
- package/lib/message-quote.d.ts.map +0 -1
- package/lib/message-quote.js +0 -91
- package/lib/message-quote.js.map +0 -1
- package/lib/message.d.ts.map +0 -1
- package/lib/message.js.map +0 -1
- package/lib/models/system-log.d.ts.map +0 -1
- package/lib/models/system-log.js.map +0 -1
- package/lib/models/user.d.ts.map +0 -1
- package/lib/models/user.js.map +0 -1
- package/lib/notice.d.ts.map +0 -1
- package/lib/notice.js.map +0 -1
- package/lib/plugin.d.ts +0 -258
- package/lib/plugin.d.ts.map +0 -1
- package/lib/plugin.js +0 -688
- package/lib/plugin.js.map +0 -1
- package/lib/prompt.d.ts +0 -118
- package/lib/prompt.d.ts.map +0 -1
- package/lib/prompt.js +0 -244
- package/lib/prompt.js.map +0 -1
- package/lib/request.d.ts.map +0 -1
- package/lib/request.js.map +0 -1
- package/lib/scheduler/scheduler.d.ts +0 -49
- package/lib/scheduler/scheduler.d.ts.map +0 -1
- package/lib/scheduler/scheduler.js +0 -352
- package/lib/scheduler/scheduler.js.map +0 -1
- package/lib/scheduler/types.d.ts +0 -71
- package/lib/scheduler/types.d.ts.map +0 -1
- package/lib/scheduler/types.js +0 -8
- package/lib/scheduler/types.js.map +0 -1
- package/lib/tool-zod.d.ts +0 -28
- package/lib/tool-zod.d.ts.map +0 -1
- package/lib/tool-zod.js +0 -98
- package/lib/tool-zod.js.map +0 -1
- package/lib/types-generator.d.ts +0 -6
- package/lib/types-generator.d.ts.map +0 -1
- package/lib/types-generator.js +0 -72
- package/lib/types-generator.js.map +0 -1
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js.map +0 -1
- package/lib/utils.d.ts.map +0 -1
- package/lib/utils.js.map +0 -1
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message enrich — 入站鉴权快照与合成通讯上下文
|
|
3
|
+
*/
|
|
4
|
+
import { Message, type MessageChannel } from '../message.js';
|
|
5
|
+
import type { SendContent } from '../types.js';
|
|
6
|
+
import type { SenderRole } from './roles.js';
|
|
7
|
+
/** Agent turn 可挂载在 Message 扩展字段上的元数据 */
|
|
8
|
+
export type AgentTurnMessage = Message<{
|
|
9
|
+
extra?: Record<string, unknown>;
|
|
10
|
+
}>;
|
|
11
|
+
export declare function senderRolesFromMessage(message: Message<any>): readonly SenderRole[];
|
|
12
|
+
export interface SyntheticMessageInput {
|
|
13
|
+
adapter: string;
|
|
14
|
+
endpoint: string;
|
|
15
|
+
sender: {
|
|
16
|
+
id: string;
|
|
17
|
+
name?: string;
|
|
18
|
+
role?: string;
|
|
19
|
+
isMaster?: boolean;
|
|
20
|
+
isTrusted?: boolean;
|
|
21
|
+
};
|
|
22
|
+
channel: MessageChannel;
|
|
23
|
+
id?: string;
|
|
24
|
+
reply?: (content: SendContent, quote?: boolean | string) => Promise<string>;
|
|
25
|
+
extra?: Record<string, unknown>;
|
|
26
|
+
}
|
|
27
|
+
/** cron / subagent / mission 等无真实入站时构造最小 Message */
|
|
28
|
+
export declare function createSyntheticMessage(input: SyntheticMessageInput): AgentTurnMessage;
|
|
29
|
+
/** hook context 中的 commMessage 字段(AIHookEvent.context) */
|
|
30
|
+
export declare function commMessageFromHookContext(context: Record<string, unknown>): Message<any> | undefined;
|
|
31
|
+
/** 兼容 tool parameters 上的 contextKey(endpointKey / sceneId / scope 等) */
|
|
32
|
+
export declare function resolveContextKey(message: Message<any>, key: string): unknown;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message enrich — 入站鉴权快照与合成通讯上下文
|
|
3
|
+
*/
|
|
4
|
+
import { Message } from '../message.js';
|
|
5
|
+
function frameworkRolesFromSenderFlags(sender) {
|
|
6
|
+
if (sender.isMaster)
|
|
7
|
+
return ['master'];
|
|
8
|
+
if (sender.isTrusted)
|
|
9
|
+
return ['trusted'];
|
|
10
|
+
return ['user'];
|
|
11
|
+
}
|
|
12
|
+
export function senderRolesFromMessage(message) {
|
|
13
|
+
const sender = message.$sender;
|
|
14
|
+
if (sender.isMaster !== undefined || sender.isTrusted !== undefined) {
|
|
15
|
+
return frameworkRolesFromSenderFlags(sender);
|
|
16
|
+
}
|
|
17
|
+
return ['user'];
|
|
18
|
+
}
|
|
19
|
+
/** cron / subagent / mission 等无真实入站时构造最小 Message */
|
|
20
|
+
export function createSyntheticMessage(input) {
|
|
21
|
+
const id = input.id ?? `synthetic:${Date.now()}`;
|
|
22
|
+
const reply = input.reply ?? (async () => id);
|
|
23
|
+
return Message.from({ extra: input.extra }, {
|
|
24
|
+
$id: id,
|
|
25
|
+
$adapter: input.adapter,
|
|
26
|
+
$endpoint: input.endpoint,
|
|
27
|
+
$content: [],
|
|
28
|
+
$sender: { ...input.sender },
|
|
29
|
+
$reply: reply,
|
|
30
|
+
$recall: async () => { },
|
|
31
|
+
$channel: input.channel,
|
|
32
|
+
$timestamp: Date.now(),
|
|
33
|
+
$raw: '',
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
/** hook context 中的 commMessage 字段(AIHookEvent.context) */
|
|
37
|
+
export function commMessageFromHookContext(context) {
|
|
38
|
+
const raw = context.commMessage;
|
|
39
|
+
if (raw && typeof raw === 'object' && '$sender' in raw) {
|
|
40
|
+
return raw;
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
/** 兼容 tool parameters 上的 contextKey(endpointKey / sceneId / scope 等) */
|
|
45
|
+
export function resolveContextKey(message, key) {
|
|
46
|
+
switch (key) {
|
|
47
|
+
case 'platform':
|
|
48
|
+
return message.$adapter;
|
|
49
|
+
case 'endpointKey':
|
|
50
|
+
return message.$endpoint;
|
|
51
|
+
case 'messageId':
|
|
52
|
+
return message.$id;
|
|
53
|
+
case 'sceneId':
|
|
54
|
+
return message.$channel?.id ?? message.$sender.id;
|
|
55
|
+
case 'senderId':
|
|
56
|
+
return message.$sender.id;
|
|
57
|
+
case 'scope':
|
|
58
|
+
return message.$channel?.type ?? 'private';
|
|
59
|
+
default: {
|
|
60
|
+
if (key in message) {
|
|
61
|
+
return message[key];
|
|
62
|
+
}
|
|
63
|
+
const extra = message.extra;
|
|
64
|
+
return extra?.[key];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { MessageElement, SendContent } from '../types.js';
|
|
2
|
+
export declare function isRemoteUrl(value: string): boolean;
|
|
3
|
+
/** 从 message 段 data 提取 base64 Buffer(data: URL / base64:// / raw base64) */
|
|
4
|
+
export declare function extractBase64Buffer(data: Record<string, unknown>): Buffer | undefined;
|
|
5
|
+
/** 解析本地文件路径(非 http(s) / base64) */
|
|
6
|
+
export declare function resolveLocalMediaPath(data: Record<string, unknown>): string | undefined;
|
|
7
|
+
export declare function asMessageElements(content: SendContent): MessageElement[];
|
|
8
|
+
export declare function isMediaSegmentType(type: string): boolean;
|
|
9
|
+
export declare function collectOutboundMediaKinds(content: SendContent | undefined): string[];
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const MEDIA_SEGMENT_TYPES = new Set(['image', 'audio', 'video', 'file']);
|
|
2
|
+
export function isRemoteUrl(value) {
|
|
3
|
+
return /^https?:\/\//i.test(value);
|
|
4
|
+
}
|
|
5
|
+
/** 从 message 段 data 提取 base64 Buffer(data: URL / base64:// / raw base64) */
|
|
6
|
+
export function extractBase64Buffer(data) {
|
|
7
|
+
const url = typeof data.url === 'string' ? data.url : undefined;
|
|
8
|
+
const file = typeof data.file === 'string' ? data.file : undefined;
|
|
9
|
+
const base64 = typeof data.base64 === 'string' ? data.base64 : undefined;
|
|
10
|
+
if (url?.startsWith('base64://')) {
|
|
11
|
+
return Buffer.from(url.slice(9), 'base64');
|
|
12
|
+
}
|
|
13
|
+
if (url && /^data:[^/]+\/[^;]+;base64,/i.test(url)) {
|
|
14
|
+
return Buffer.from(url.replace(/^data:[^/]+\/[^;]+;base64,/, ''), 'base64');
|
|
15
|
+
}
|
|
16
|
+
if (file?.startsWith('base64://')) {
|
|
17
|
+
return Buffer.from(file.slice(9), 'base64');
|
|
18
|
+
}
|
|
19
|
+
if (base64) {
|
|
20
|
+
const raw = base64.startsWith('base64://') ? base64.slice(9) : base64;
|
|
21
|
+
return Buffer.from(raw, 'base64');
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
/** 解析本地文件路径(非 http(s) / base64) */
|
|
26
|
+
export function resolveLocalMediaPath(data) {
|
|
27
|
+
const url = typeof data.url === 'string' ? data.url : undefined;
|
|
28
|
+
const file = typeof data.file === 'string' ? data.file : undefined;
|
|
29
|
+
const candidate = file ?? url;
|
|
30
|
+
if (!candidate || isRemoteUrl(candidate))
|
|
31
|
+
return undefined;
|
|
32
|
+
if (candidate.startsWith('base64://') || /^data:/.test(candidate))
|
|
33
|
+
return undefined;
|
|
34
|
+
if (candidate.startsWith('file://') || candidate.startsWith('/') || !candidate.includes('://')) {
|
|
35
|
+
return candidate;
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
export function asMessageElements(content) {
|
|
40
|
+
if (typeof content === 'string') {
|
|
41
|
+
return [{ type: 'text', data: { text: content } }];
|
|
42
|
+
}
|
|
43
|
+
if (!Array.isArray(content)) {
|
|
44
|
+
return [content];
|
|
45
|
+
}
|
|
46
|
+
return content.map((item) => typeof item === 'string' ? { type: 'text', data: { text: item } } : item);
|
|
47
|
+
}
|
|
48
|
+
export function isMediaSegmentType(type) {
|
|
49
|
+
return MEDIA_SEGMENT_TYPES.has(type);
|
|
50
|
+
}
|
|
51
|
+
export function collectOutboundMediaKinds(content) {
|
|
52
|
+
if (content == null)
|
|
53
|
+
return [];
|
|
54
|
+
const kinds = new Set();
|
|
55
|
+
for (const item of asMessageElements(content)) {
|
|
56
|
+
const t = item?.type;
|
|
57
|
+
if (typeof t === 'string' && isMediaSegmentType(t)) {
|
|
58
|
+
kinds.add(t);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return [...kinds];
|
|
62
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { MessageType } from '../message.js';
|
|
2
2
|
import type { SendContent, SendOptions } from '../types.js';
|
|
3
|
+
import { type IMDeliveryTarget } from '../im-scene.js';
|
|
3
4
|
export interface QueueEnvelope<TDetail extends Record<string, unknown> = Record<string, unknown>> {
|
|
4
5
|
kind: string;
|
|
5
6
|
type: string;
|
|
@@ -8,7 +9,7 @@ export interface QueueEnvelope<TDetail extends Record<string, unknown> = Record<
|
|
|
8
9
|
}
|
|
9
10
|
export interface NormalizedQueueOutboundDetail {
|
|
10
11
|
context: string;
|
|
11
|
-
|
|
12
|
+
endpoint: string;
|
|
12
13
|
channelId: string;
|
|
13
14
|
channelType: MessageType;
|
|
14
15
|
content: SendContent;
|
|
@@ -23,5 +24,9 @@ export declare function isMessageType(value: unknown): value is MessageType;
|
|
|
23
24
|
/** Normalize queue outbound detail into the canonical queue-IM shape. */
|
|
24
25
|
export declare function normalizeQueueOutboundDetail(record: Record<string, unknown>): NormalizedQueueOutboundDetail;
|
|
25
26
|
export declare function toSendOptions(detail: NormalizedQueueOutboundDetail): SendOptions;
|
|
27
|
+
export interface QueueIMSceneTargetDetail {
|
|
28
|
+
target: IMDeliveryTarget;
|
|
29
|
+
content: SendContent;
|
|
30
|
+
}
|
|
31
|
+
export declare function sceneTargetToSendOptions(detail: QueueIMSceneTargetDetail): SendOptions;
|
|
26
32
|
export declare function normalizeRecordToSendOptions(record: Record<string, unknown>): SendOptions;
|
|
27
|
-
//# sourceMappingURL=queue-im-field-contract.d.ts.map
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { sceneRefToSendOptions } from '../im-scene.js';
|
|
1
2
|
export class QueueIMFieldContractError extends Error {
|
|
2
3
|
code;
|
|
3
4
|
field;
|
|
@@ -26,7 +27,7 @@ function requireContent(value) {
|
|
|
26
27
|
/** Normalize queue outbound detail into the canonical queue-IM shape. */
|
|
27
28
|
export function normalizeQueueOutboundDetail(record) {
|
|
28
29
|
const context = requireString(record.context, 'context');
|
|
29
|
-
const
|
|
30
|
+
const endpoint = requireString(record.endpoint, 'endpoint');
|
|
30
31
|
const channelId = requireString(record.channelId, 'channelId');
|
|
31
32
|
const channelTypeValue = record.channelType;
|
|
32
33
|
if (!isMessageType(channelTypeValue)) {
|
|
@@ -34,7 +35,7 @@ export function normalizeQueueOutboundDetail(record) {
|
|
|
34
35
|
}
|
|
35
36
|
const normalized = {
|
|
36
37
|
context,
|
|
37
|
-
|
|
38
|
+
endpoint,
|
|
38
39
|
channelId,
|
|
39
40
|
channelType: channelTypeValue,
|
|
40
41
|
content: requireContent(record.content),
|
|
@@ -46,13 +47,21 @@ export function normalizeQueueOutboundDetail(record) {
|
|
|
46
47
|
export function toSendOptions(detail) {
|
|
47
48
|
return {
|
|
48
49
|
context: detail.context,
|
|
49
|
-
|
|
50
|
+
endpoint: detail.endpoint,
|
|
50
51
|
id: detail.channelId,
|
|
51
52
|
type: detail.channelType,
|
|
52
53
|
content: detail.content,
|
|
53
54
|
};
|
|
54
55
|
}
|
|
56
|
+
export function sceneTargetToSendOptions(detail) {
|
|
57
|
+
return sceneRefToSendOptions(detail.target, detail.content);
|
|
58
|
+
}
|
|
55
59
|
export function normalizeRecordToSendOptions(record) {
|
|
60
|
+
if (record.target && typeof record.target === 'object') {
|
|
61
|
+
return sceneTargetToSendOptions({
|
|
62
|
+
target: record.target,
|
|
63
|
+
content: requireContent(record.content),
|
|
64
|
+
});
|
|
65
|
+
}
|
|
56
66
|
return toSendOptions(normalizeQueueOutboundDetail(record));
|
|
57
67
|
}
|
|
58
|
-
//# sourceMappingURL=queue-im-field-contract.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** 将 Markdown 转为可读纯文本(出站 text 降级用) */
|
|
2
|
+
export function markdownToPlainText(md) {
|
|
3
|
+
let text = md;
|
|
4
|
+
text = text.replace(/```[\s\S]*?```/g, (block) => {
|
|
5
|
+
const inner = block.replace(/^```[^\n]*\n?/, '').replace(/\n?```$/, '');
|
|
6
|
+
return inner.trim() ? `${inner.trim()}\n` : '';
|
|
7
|
+
});
|
|
8
|
+
text = text.replace(/`([^`]+)`/g, '$1');
|
|
9
|
+
text = text.replace(/!\[[^\]]*\]\([^)]*\)/g, '');
|
|
10
|
+
text = text.replace(/\[([^\]]+)\]\([^)]*\)/g, '$1');
|
|
11
|
+
text = text.replace(/^#{1,6}\s+/gm, '');
|
|
12
|
+
text = text.replace(/^[ \t]*>[ \t]?/gm, '');
|
|
13
|
+
text = text.replace(/^[ \t]*[-*+][ \t]+/gm, '');
|
|
14
|
+
text = text.replace(/\*\*([^*]+)\*\*/g, '$1');
|
|
15
|
+
text = text.replace(/__([^_]+)__/g, '$1');
|
|
16
|
+
text = text.replace(/\*([^*]+)\*/g, '$1');
|
|
17
|
+
text = text.replace(/_([^_]+)_/g, '$1');
|
|
18
|
+
text = text.replace(/~~([^~]+)~~/g, '$1');
|
|
19
|
+
return text.trim();
|
|
20
|
+
}
|
|
21
|
+
export function markdownToHtmlForRender(md) {
|
|
22
|
+
const escaped = md
|
|
23
|
+
.replace(/&/g, '&')
|
|
24
|
+
.replace(/</g, '<')
|
|
25
|
+
.replace(/>/g, '>');
|
|
26
|
+
return `<pre style="white-space:pre-wrap;font-family:sans-serif;font-size:14px;line-height:1.5;padding:16px;">${escaped}</pre>`;
|
|
27
|
+
}
|
package/lib/built/roles.d.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* SenderRole —
|
|
2
|
+
* SenderRole — 框架层发送者角色(user / master / trusted)
|
|
3
|
+
* 平台群/频道身份见 platform(adapter,perm) 与适配器 checker
|
|
3
4
|
*/
|
|
4
|
-
|
|
5
|
-
export
|
|
5
|
+
export type SenderRole = 'user' | 'trusted' | 'master';
|
|
6
|
+
export declare function isFrameworkSenderRole(role: string): role is SenderRole;
|
|
6
7
|
export declare function expandImpliedRoles(roles: Iterable<SenderRole>): Set<SenderRole>;
|
|
7
8
|
export declare function roleSatisfies(callerRoles: Iterable<SenderRole>, requiredAnyRole: readonly SenderRole[] | undefined): boolean;
|
|
8
|
-
export declare function resolveRolesFromContext(context: Pick<ToolContext, 'roles'>): readonly SenderRole[];
|
|
9
9
|
/** 去重;无其它角色时返回 ['user'] */
|
|
10
10
|
export declare function normalizeSenderRoles(roles: Iterable<SenderRole>): SenderRole[];
|
|
11
11
|
export declare function hasSenderRole(roles: Iterable<SenderRole>, role: SenderRole): boolean;
|
|
12
12
|
export declare function formatSenderRolesForLabel(roles: readonly SenderRole[]): string;
|
|
13
13
|
/** 剥离用户正文里自造的发送者前缀 */
|
|
14
14
|
export declare function stripUserSpoofedSenderPrefix(rawContent: string): string;
|
|
15
|
-
//# sourceMappingURL=roles.d.ts.map
|
package/lib/built/roles.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
const FRAMEWORK_SENDER_ROLES = new Set(['user', 'trusted', 'master']);
|
|
2
|
+
export function isFrameworkSenderRole(role) {
|
|
3
|
+
return FRAMEWORK_SENDER_ROLES.has(role);
|
|
4
|
+
}
|
|
1
5
|
/** 匹配工具要求时的隐含升格(不写入持久化 roles 集合) */
|
|
2
6
|
const ROLE_IMPLIES = {
|
|
3
|
-
group_owner: ['group_admin'],
|
|
4
7
|
master: ['trusted'],
|
|
5
8
|
};
|
|
6
9
|
export function expandImpliedRoles(roles) {
|
|
@@ -18,9 +21,6 @@ export function roleSatisfies(callerRoles, requiredAnyRole) {
|
|
|
18
21
|
const expanded = expandImpliedRoles(callerRoles);
|
|
19
22
|
return requiredAnyRole.some((req) => expanded.has(req));
|
|
20
23
|
}
|
|
21
|
-
export function resolveRolesFromContext(context) {
|
|
22
|
-
return context.roles?.length ? context.roles : ['user'];
|
|
23
|
-
}
|
|
24
24
|
/** 去重;无其它角色时返回 ['user'] */
|
|
25
25
|
export function normalizeSenderRoles(roles) {
|
|
26
26
|
const set = new Set();
|
|
@@ -60,4 +60,3 @@ export function stripUserSpoofedSenderPrefix(rawContent) {
|
|
|
60
60
|
}
|
|
61
61
|
return text;
|
|
62
62
|
}
|
|
63
|
-
//# sourceMappingURL=roles.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Segment } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* 内建 canonical 类型始终走 schema 严格校验。Core 预留扩展和命名空间扩展
|
|
4
|
+
* 只承诺稳定顶层形状;裸未知类型会被拒绝,避免拼写错误绕过 canonical 校验。
|
|
5
|
+
*/
|
|
6
|
+
export declare function isCanonicalSegment(value: unknown): value is Segment;
|
|
7
|
+
export declare function assertCanonicalSegments(segments: unknown): asserts segments is Segment[];
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { isStrictCanonicalSegment } from './validate.js';
|
|
2
|
+
const STRICT_CANONICAL_TYPES = new Set([
|
|
3
|
+
'text', 'mention', 'image', 'audio', 'video', 'file', 'reply', 'forward', 'face', 'dice', 'rps',
|
|
4
|
+
]);
|
|
5
|
+
// These are Core-owned wire extensions rather than canonical data types. Keep
|
|
6
|
+
// their gradual contracts isolated here; third-party extensions must be
|
|
7
|
+
// namespaced so a future canonical type cannot silently collide with them.
|
|
8
|
+
const CORE_EXTENSION_TYPES = new Set([
|
|
9
|
+
'action', 'html', 'keyboard', 'link', 'markdown', 'qrcode', 'record', 'tts', 'voice',
|
|
10
|
+
]);
|
|
11
|
+
const extensionTypePattern = /^[a-z][a-z0-9-]*(?::[a-z][a-z0-9-]*)+$/u;
|
|
12
|
+
function isPlainObject(value) {
|
|
13
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 内建 canonical 类型始终走 schema 严格校验。Core 预留扩展和命名空间扩展
|
|
17
|
+
* 只承诺稳定顶层形状;裸未知类型会被拒绝,避免拼写错误绕过 canonical 校验。
|
|
18
|
+
*/
|
|
19
|
+
export function isCanonicalSegment(value) {
|
|
20
|
+
if (!isPlainObject(value) || typeof value.type !== 'string')
|
|
21
|
+
return false;
|
|
22
|
+
if (!isPlainObject(value.data))
|
|
23
|
+
return false;
|
|
24
|
+
if (value.platform !== undefined && !isPlainObject(value.platform))
|
|
25
|
+
return false;
|
|
26
|
+
if (STRICT_CANONICAL_TYPES.has(value.type))
|
|
27
|
+
return isStrictCanonicalSegment(value);
|
|
28
|
+
return CORE_EXTENSION_TYPES.has(value.type) || extensionTypePattern.test(value.type);
|
|
29
|
+
}
|
|
30
|
+
export function assertCanonicalSegments(segments) {
|
|
31
|
+
if (!Array.isArray(segments)) {
|
|
32
|
+
throw new Error('segments must be an array');
|
|
33
|
+
}
|
|
34
|
+
for (let i = 0; i < segments.length; i++) {
|
|
35
|
+
if (!isCanonicalSegment(segments[i])) {
|
|
36
|
+
throw new Error(`segment[${i}] is not canonical`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { isCanonicalSegment } from './assert.js';
|
|
2
|
+
/** IM 可见段白名单(thinking / tool_call 等 AI-only 段过滤) */
|
|
3
|
+
const IM_VISIBLE_TYPES = new Set([
|
|
4
|
+
'text',
|
|
5
|
+
'mention',
|
|
6
|
+
'image',
|
|
7
|
+
'video',
|
|
8
|
+
'audio',
|
|
9
|
+
'voice',
|
|
10
|
+
'record',
|
|
11
|
+
'file',
|
|
12
|
+
'face',
|
|
13
|
+
'reply',
|
|
14
|
+
'forward',
|
|
15
|
+
'link',
|
|
16
|
+
'dice',
|
|
17
|
+
'rps',
|
|
18
|
+
'markdown',
|
|
19
|
+
'html',
|
|
20
|
+
'qrcode',
|
|
21
|
+
'tts',
|
|
22
|
+
'keyboard',
|
|
23
|
+
]);
|
|
24
|
+
const AI_ONLY_TYPES = new Set(['thinking', 'tool_call']);
|
|
25
|
+
/**
|
|
26
|
+
* 过滤 agent / AI 出站段,仅保留可经 IM 投递的 canonical 段。
|
|
27
|
+
*/
|
|
28
|
+
export function segmentsForImDelivery(segments) {
|
|
29
|
+
const out = [];
|
|
30
|
+
for (const item of segments) {
|
|
31
|
+
if (!isCanonicalSegment(item))
|
|
32
|
+
continue;
|
|
33
|
+
if (AI_ONLY_TYPES.has(item.type))
|
|
34
|
+
continue;
|
|
35
|
+
if (!IM_VISIBLE_TYPES.has(item.type))
|
|
36
|
+
continue;
|
|
37
|
+
out.push(item);
|
|
38
|
+
}
|
|
39
|
+
return out;
|
|
40
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type { Segment, SegmentBase, MediaRef, TextSegment, MentionSegment, ImageSegment, AudioSegment, VideoSegment, FileSegment, ReplySegment, ForwardSegment, FaceSegment, DiceSegment, RpsSegment, } from './types.js';
|
|
2
|
+
export { mediaRefSchema, textSegmentSchema, mentionSegmentSchema, imageSegmentSchema, audioSegmentSchema, videoSegmentSchema, fileSegmentSchema, replySegmentSchema, forwardSegmentSchema, faceSegmentSchema, diceSegmentSchema, rpsSegmentSchema, canonicalSegmentSchema, segmentArraySchema, } from './validate.js';
|
|
3
|
+
export { assertCanonicalSegments, isCanonicalSegment } from './assert.js';
|
|
4
|
+
export { mediaRefJsonSchema, outboundSegmentJsonSchema, aiOutboundJsonSchema, STRICT_OUTBOUND_SEGMENT_TYPES, } from './json-schema.js';
|
|
5
|
+
export { segmentsForImDelivery } from './delivery.js';
|
|
6
|
+
export { isMediaRef, collectSegmentMedia, type SegmentMediaRef, } from './media.js';
|
|
7
|
+
export { createImageSegment } from './image.js';
|
|
8
|
+
export { formatSegmentPreview } from './preview.js';
|
|
9
|
+
export { segmentsToPlainText } from './text.js';
|
|
10
|
+
export { readMentionTarget, readMentionName, readMentionSegmentTarget } from './mention.js';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { mediaRefSchema, textSegmentSchema, mentionSegmentSchema, imageSegmentSchema, audioSegmentSchema, videoSegmentSchema, fileSegmentSchema, replySegmentSchema, forwardSegmentSchema, faceSegmentSchema, diceSegmentSchema, rpsSegmentSchema, canonicalSegmentSchema, segmentArraySchema, } from './validate.js';
|
|
2
|
+
export { assertCanonicalSegments, isCanonicalSegment } from './assert.js';
|
|
3
|
+
export { mediaRefJsonSchema, outboundSegmentJsonSchema, aiOutboundJsonSchema, STRICT_OUTBOUND_SEGMENT_TYPES, } from './json-schema.js';
|
|
4
|
+
export { segmentsForImDelivery } from './delivery.js';
|
|
5
|
+
export { isMediaRef, collectSegmentMedia, } from './media.js';
|
|
6
|
+
export { createImageSegment } from './image.js';
|
|
7
|
+
export { formatSegmentPreview } from './preview.js';
|
|
8
|
+
export { segmentsToPlainText } from './text.js';
|
|
9
|
+
export { readMentionTarget, readMentionName, readMentionSegmentTarget } from './mention.js';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical outbound segment 的 JSON Schema SSOT —— 供 AI structured output
|
|
3
|
+
* (AI SDK `Output.object({ schema })`)约束模型直接输出 zhin 消息段数组。
|
|
4
|
+
*
|
|
5
|
+
* 与 validate.ts(@zhin.js/schema 运行时校验)表达同一组约束的 JSON Schema 形态;
|
|
6
|
+
* 严格段类型集合必须与 assert.ts 的 STRICT_CANONICAL_TYPES 保持一致
|
|
7
|
+
* (tests/segment-contract/json-schema.test.ts 有交叉防漂移测试)。
|
|
8
|
+
*
|
|
9
|
+
* 注意:解析侧 parseOutboundSegment 对严格段要求 canonical 形态
|
|
10
|
+
* (如 image 必须携带 data.media: MediaRef),本 schema 与之对齐。
|
|
11
|
+
*/
|
|
12
|
+
type JsonSchemaObject = Record<string, unknown>;
|
|
13
|
+
/** MediaRef(canonical 媒体引用):types.ts 的 MediaRef 接口 */
|
|
14
|
+
export declare const mediaRefJsonSchema: JsonSchemaObject;
|
|
15
|
+
/** 严格段类型集合(SSOT:assert.ts STRICT_CANONICAL_TYPES) */
|
|
16
|
+
export declare const STRICT_OUTBOUND_SEGMENT_TYPES: readonly ["text", "mention", "image", "reply", "forward", "face", "dice", "rps"];
|
|
17
|
+
/**
|
|
18
|
+
* 单条 outbound 消息段的 JSON Schema。
|
|
19
|
+
* 严格段镜像 validate.ts 的 data 约束;宽松段走 generic 分支。
|
|
20
|
+
*/
|
|
21
|
+
export declare const outboundSegmentJsonSchema: JsonSchemaObject;
|
|
22
|
+
/**
|
|
23
|
+
* AI 结构化出站根对象(ADR 0025 JSON DSL 的 schema 形态)。
|
|
24
|
+
* 与 parseAiOutboundJson / ZhinAiOutboundPayload 对齐:text、mentions、segments
|
|
25
|
+
* 均为可选(provider strict 模式兼容性考虑),"至少一项"由 prompt 与下游校验兜底。
|
|
26
|
+
*/
|
|
27
|
+
export declare const aiOutboundJsonSchema: JsonSchemaObject;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical outbound segment 的 JSON Schema SSOT —— 供 AI structured output
|
|
3
|
+
* (AI SDK `Output.object({ schema })`)约束模型直接输出 zhin 消息段数组。
|
|
4
|
+
*
|
|
5
|
+
* 与 validate.ts(@zhin.js/schema 运行时校验)表达同一组约束的 JSON Schema 形态;
|
|
6
|
+
* 严格段类型集合必须与 assert.ts 的 STRICT_CANONICAL_TYPES 保持一致
|
|
7
|
+
* (tests/segment-contract/json-schema.test.ts 有交叉防漂移测试)。
|
|
8
|
+
*
|
|
9
|
+
* 注意:解析侧 parseOutboundSegment 对严格段要求 canonical 形态
|
|
10
|
+
* (如 image 必须携带 data.media: MediaRef),本 schema 与之对齐。
|
|
11
|
+
*/
|
|
12
|
+
const platformJsonSchema = {
|
|
13
|
+
type: 'object',
|
|
14
|
+
additionalProperties: true,
|
|
15
|
+
description: '平台专有字段(一般无需输出)',
|
|
16
|
+
};
|
|
17
|
+
/** MediaRef(canonical 媒体引用):types.ts 的 MediaRef 接口 */
|
|
18
|
+
export const mediaRefJsonSchema = {
|
|
19
|
+
type: 'object',
|
|
20
|
+
properties: {
|
|
21
|
+
kind: {
|
|
22
|
+
type: 'string',
|
|
23
|
+
enum: ['url', 'path', 'base64', 'file'],
|
|
24
|
+
description: 'url=http(s) 链接;path=本地文件路径;base64=内联 base64 数据;file=平台不透明文件引用(如 Telegram file_id)',
|
|
25
|
+
},
|
|
26
|
+
value: { type: 'string', description: '媒体内容:URL / 文件路径 / 纯 base64' },
|
|
27
|
+
mime_type: { type: 'string', description: '如 image/png、audio/mpeg' },
|
|
28
|
+
},
|
|
29
|
+
required: ['kind', 'value'],
|
|
30
|
+
additionalProperties: false,
|
|
31
|
+
};
|
|
32
|
+
function strictBranch(type, data) {
|
|
33
|
+
return {
|
|
34
|
+
type: 'object',
|
|
35
|
+
properties: {
|
|
36
|
+
type: { const: type },
|
|
37
|
+
data,
|
|
38
|
+
platform: platformJsonSchema,
|
|
39
|
+
},
|
|
40
|
+
required: ['type', 'data'],
|
|
41
|
+
additionalProperties: false,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function dataObject(properties, required) {
|
|
45
|
+
return { type: 'object', properties, required: [...required], additionalProperties: false };
|
|
46
|
+
}
|
|
47
|
+
/** 宽松分支:未纳入严格契约的段类型,仅约束顶层形状(与 isCanonicalSegment 宽松路径一致) */
|
|
48
|
+
const looseBranch = {
|
|
49
|
+
type: 'object',
|
|
50
|
+
properties: {
|
|
51
|
+
type: {
|
|
52
|
+
type: 'string',
|
|
53
|
+
enum: ['video', 'audio', 'voice', 'record', 'file', 'link', 'markdown', 'html', 'keyboard', 'action'],
|
|
54
|
+
},
|
|
55
|
+
data: { type: 'object' },
|
|
56
|
+
platform: platformJsonSchema,
|
|
57
|
+
},
|
|
58
|
+
required: ['type', 'data'],
|
|
59
|
+
additionalProperties: false,
|
|
60
|
+
};
|
|
61
|
+
/** 严格段类型集合(SSOT:assert.ts STRICT_CANONICAL_TYPES) */
|
|
62
|
+
export const STRICT_OUTBOUND_SEGMENT_TYPES = [
|
|
63
|
+
'text', 'mention', 'image', 'reply', 'forward', 'face', 'dice', 'rps',
|
|
64
|
+
];
|
|
65
|
+
/**
|
|
66
|
+
* 单条 outbound 消息段的 JSON Schema。
|
|
67
|
+
* 严格段镜像 validate.ts 的 data 约束;宽松段走 generic 分支。
|
|
68
|
+
*/
|
|
69
|
+
export const outboundSegmentJsonSchema = {
|
|
70
|
+
description: 'zhin 消息段 {type, data, platform?}',
|
|
71
|
+
anyOf: [
|
|
72
|
+
strictBranch('text', dataObject({
|
|
73
|
+
text: { type: 'string', description: '文本内容' },
|
|
74
|
+
}, ['text'])),
|
|
75
|
+
strictBranch('mention', dataObject({
|
|
76
|
+
target: { type: 'string', description: '被 @ 用户的平台 id' },
|
|
77
|
+
name: { type: 'string' },
|
|
78
|
+
}, ['target'])),
|
|
79
|
+
strictBranch('image', dataObject({
|
|
80
|
+
media: mediaRefJsonSchema,
|
|
81
|
+
alt: { type: 'string' },
|
|
82
|
+
}, ['media'])),
|
|
83
|
+
strictBranch('reply', dataObject({
|
|
84
|
+
message_id: { type: 'string', description: '被引用消息的平台消息 id' },
|
|
85
|
+
}, ['message_id'])),
|
|
86
|
+
strictBranch('forward', dataObject({
|
|
87
|
+
forward_id: { type: 'string' },
|
|
88
|
+
title: { type: 'string' },
|
|
89
|
+
messages: { type: 'array', items: { type: 'array' } },
|
|
90
|
+
}, ['forward_id'])),
|
|
91
|
+
strictBranch('face', dataObject({
|
|
92
|
+
id: { anyOf: [{ type: 'string' }, { type: 'number' }], description: '平台表情 id' },
|
|
93
|
+
name: { type: 'string' },
|
|
94
|
+
}, ['id'])),
|
|
95
|
+
strictBranch('dice', dataObject({
|
|
96
|
+
result: { type: 'number' },
|
|
97
|
+
}, [])),
|
|
98
|
+
strictBranch('rps', dataObject({
|
|
99
|
+
result: { type: 'number' },
|
|
100
|
+
}, [])),
|
|
101
|
+
looseBranch,
|
|
102
|
+
],
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* AI 结构化出站根对象(ADR 0025 JSON DSL 的 schema 形态)。
|
|
106
|
+
* 与 parseAiOutboundJson / ZhinAiOutboundPayload 对齐:text、mentions、segments
|
|
107
|
+
* 均为可选(provider strict 模式兼容性考虑),"至少一项"由 prompt 与下游校验兜底。
|
|
108
|
+
*/
|
|
109
|
+
export const aiOutboundJsonSchema = {
|
|
110
|
+
type: 'object',
|
|
111
|
+
properties: {
|
|
112
|
+
text: {
|
|
113
|
+
type: 'string',
|
|
114
|
+
description: '纯文本回复正文;与 segments 至少输出一项。使用 mentions 时必填',
|
|
115
|
+
},
|
|
116
|
+
mentions: {
|
|
117
|
+
type: 'array',
|
|
118
|
+
items: { type: 'string' },
|
|
119
|
+
description: '要 @ 的会话成员引用(昵称或 id,由宿主解析为平台账号);需配合 text 使用',
|
|
120
|
+
},
|
|
121
|
+
segments: {
|
|
122
|
+
type: 'array',
|
|
123
|
+
items: outboundSegmentJsonSchema,
|
|
124
|
+
description: 'zhin 消息段数组,如 [{type:"image",data:{media:{kind:"url",value:"https://…"}}},{type:"text",data:{text:"…"}}]',
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
additionalProperties: false,
|
|
128
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { MediaRef, Segment } from './types.js';
|
|
2
|
+
import { isMediaRef } from './validate.js';
|
|
3
|
+
export { isMediaRef };
|
|
4
|
+
export interface SegmentMediaRef {
|
|
5
|
+
readonly type: string;
|
|
6
|
+
readonly media: MediaRef;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* 从入站 canonical 段收集媒体引用(image / audio / video / file)。
|
|
10
|
+
* 只认 canonical `data.media`;无媒体段时返回空数组(调用方无需特判 undefined)。
|
|
11
|
+
*/
|
|
12
|
+
export declare function collectSegmentMedia(segments: readonly Segment[] | undefined): SegmentMediaRef[];
|