@zhin.js/core 1.1.37 → 1.1.39
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/lib/built/ai-access.d.ts +2 -2
- package/lib/built/ai-access.js +4 -4
- package/lib/built/ai-outbound/types.d.ts +1 -1
- package/lib/built/ai-trigger.d.ts +1 -1
- package/lib/built/ai-trigger.js +17 -10
- package/lib/built/interactive-segments/action.d.ts +3 -3
- package/lib/built/interactive-segments/action.js +1 -1
- package/lib/built/message-enrich.d.ts +19 -22
- package/lib/built/message-enrich.js +47 -42
- package/lib/built/segment-contract/assert.js +2 -1
- package/lib/built/segment-contract/delivery.js +1 -0
- package/lib/built/segment-contract/index.d.ts +2 -2
- package/lib/built/segment-contract/index.js +1 -1
- package/lib/built/segment-contract/json-schema.d.ts +1 -1
- package/lib/built/segment-contract/json-schema.js +33 -2
- package/lib/built/segment-contract/preview.js +7 -0
- package/lib/built/segment-contract/types.d.ts +2 -2
- package/lib/built/segment-contract/validate.d.ts +41 -0
- package/lib/built/segment-contract/validate.js +36 -0
- package/lib/built/tool-access.d.ts +2 -2
- package/lib/built/tool-access.js +2 -2
- package/lib/im-scene.d.ts +4 -3
- package/lib/im-scene.js +9 -9
- package/lib/im-session-id.d.ts +20 -12
- package/lib/im-session-id.js +8 -8
- package/lib/index.d.ts +2 -1
- package/lib/index.js +1 -1
- package/lib/message.d.ts +1 -29
- package/lib/message.js +1 -27
- package/lib/notice.d.ts +34 -32
- package/lib/notice.js +21 -7
- package/lib/plugin-runtime/im/contracts.d.ts +23 -1
- package/lib/plugin-runtime/im/contracts.js +4 -1
- package/lib/plugin-runtime/im/conversation-runtime.js +22 -31
- package/lib/plugin-runtime/im/inbound-runtime.d.ts +2 -2
- package/lib/plugin-runtime/im/inbound-runtime.js +58 -29
- package/lib/plugin-runtime/im/index.d.ts +3 -0
- package/lib/plugin-runtime/im/index.js +3 -0
- package/lib/plugin-runtime/im/public-api.d.ts +1 -1
- package/lib/request.d.ts +27 -35
- package/lib/request.js +14 -6
- package/lib/side-event/base.d.ts +64 -43
- package/lib/side-event/base.js +66 -19
- package/lib/side-event/dispatch.js +40 -38
- package/lib/side-event/normalize.d.ts +9 -8
- package/lib/side-event/normalize.js +9 -8
- package/lib/side-event/types.d.ts +3 -6
- package/lib/side-event/types.js +0 -5
- package/lib/system-event.d.ts +17 -11
- package/lib/system-event.js +8 -6
- package/lib/types.d.ts +3 -2
- package/lib/utils.d.ts +1 -1
- package/package.json +13 -13
package/lib/built/ai-access.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* AI Access Gate — 控制 LLM 回复路径是否对当前会话开放(平台 AIGC 合规等)
|
|
3
3
|
*/
|
|
4
|
-
import type { Message } from '../
|
|
4
|
+
import type { Message } from '../plugin-runtime/im/contracts.js';
|
|
5
5
|
export type AIAccessMode = 'open' | 'closed' | 'whitelist';
|
|
6
6
|
/** Endpoint 或全局 ai.access 配置块 */
|
|
7
7
|
export interface AIAccessScopeConfig {
|
|
@@ -33,4 +33,4 @@ export declare function resolveAIAccessConfig(global: AIAccessConfig | undefined
|
|
|
33
33
|
* 判定当前消息是否允许进入 AI Handler(LLM 回复路径)。
|
|
34
34
|
* 不影响命令、游戏等其它 Dispatcher 分支。
|
|
35
35
|
*/
|
|
36
|
-
export declare function checkAIAccess(message: Message
|
|
36
|
+
export declare function checkAIAccess(message: Message, globalConfig: AIAccessConfig | undefined, endpointScope?: AIAccessScopeConfig): AIAccessResult;
|
package/lib/built/ai-access.js
CHANGED
|
@@ -11,19 +11,19 @@ export function resolveAIAccessConfig(global, endpointScope) {
|
|
|
11
11
|
};
|
|
12
12
|
}
|
|
13
13
|
function isWhitelisted(message, config) {
|
|
14
|
-
const userId = String(message
|
|
14
|
+
const userId = String(message.sender?.id ?? '');
|
|
15
15
|
if (userId && config.users.includes(userId))
|
|
16
16
|
return true;
|
|
17
|
-
const scope = message
|
|
17
|
+
const scope = message.conversation.kind;
|
|
18
18
|
if (scope === 'group' || scope === 'channel') {
|
|
19
|
-
const channelId = String(message
|
|
19
|
+
const channelId = String(message.conversation.id ?? '');
|
|
20
20
|
if (channelId && config.groups.includes(channelId))
|
|
21
21
|
return true;
|
|
22
22
|
}
|
|
23
23
|
return false;
|
|
24
24
|
}
|
|
25
25
|
function denyResult(message, config, reason) {
|
|
26
|
-
const scope = message
|
|
26
|
+
const scope = message.conversation.kind;
|
|
27
27
|
const replyMessage = scope === 'private' ? config.denyMessage : undefined;
|
|
28
28
|
return { allowed: false, reason, replyMessage };
|
|
29
29
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Message } from '../../
|
|
1
|
+
import type { Message } from '../../plugin-runtime/im/contracts.js';
|
|
2
2
|
import type { MessageElement } from '../../types.js';
|
|
3
3
|
import type { InteractivePolicy } from '../interactive-segments/types.js';
|
|
4
4
|
import type { Segment } from '../segment-contract/types.js';
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* 3. 私聊直接对话 - 私聊时直接对话
|
|
9
9
|
* 4. 关键词触发 - 非群/频道场景下匹配关键词时触发
|
|
10
10
|
*/
|
|
11
|
-
import { Message } from "../
|
|
11
|
+
import type { Message } from "../plugin-runtime/im/contracts.js";
|
|
12
12
|
import type { MessageElement, ToolScope } from "../types.js";
|
|
13
13
|
import { type SenderRole } from './roles.js';
|
|
14
14
|
/**
|
package/lib/built/ai-trigger.js
CHANGED
|
@@ -34,6 +34,11 @@ function normalizeAtIds(endpointAtIds) {
|
|
|
34
34
|
const ids = endpointAtIds?.length ? endpointAtIds : [];
|
|
35
35
|
return [...new Set(ids.map((id) => String(id)).filter(Boolean))];
|
|
36
36
|
}
|
|
37
|
+
function messageElements(message) {
|
|
38
|
+
if (message.segments?.length)
|
|
39
|
+
return [...message.segments];
|
|
40
|
+
return message.content ? [{ type: 'text', data: { text: message.content } }] : [];
|
|
41
|
+
}
|
|
37
42
|
function segmentAtUserId(seg) {
|
|
38
43
|
return readMentionSegmentTarget(seg);
|
|
39
44
|
}
|
|
@@ -63,7 +68,9 @@ function stripTextAtEndpoint(text, endpointKeys) {
|
|
|
63
68
|
* 收集用于 @ 匹配的 Endpoint 标识符
|
|
64
69
|
*/
|
|
65
70
|
export function collectEndpointAtIds(message, extraIds) {
|
|
66
|
-
const ids = new Set([
|
|
71
|
+
const ids = new Set([
|
|
72
|
+
String(message.endpointId ?? message.conversation.endpoint.id),
|
|
73
|
+
]);
|
|
67
74
|
for (const id of extraIds ?? []) {
|
|
68
75
|
if (id)
|
|
69
76
|
ids.add(String(id));
|
|
@@ -75,10 +82,10 @@ export function collectEndpointAtIds(message, extraIds) {
|
|
|
75
82
|
*/
|
|
76
83
|
export function isAtEndpoint(message, endpointAtIds) {
|
|
77
84
|
const endpointKeys = normalizeAtIds(endpointAtIds?.length ? endpointAtIds : collectEndpointAtIds(message));
|
|
78
|
-
if (message
|
|
85
|
+
if (messageElements(message).some((seg) => isAtSegmentForEndpoint(seg, endpointKeys))) {
|
|
79
86
|
return true;
|
|
80
87
|
}
|
|
81
|
-
for (const seg of message
|
|
88
|
+
for (const seg of messageElements(message)) {
|
|
82
89
|
if (seg.type === 'text' && seg.data?.text && textMentionsEndpoint(seg.data.text, endpointKeys)) {
|
|
83
90
|
return true;
|
|
84
91
|
}
|
|
@@ -90,7 +97,7 @@ export function isAtEndpoint(message, endpointAtIds) {
|
|
|
90
97
|
* 使用 segment.toString 将 MessageElement 转为 XML 格式
|
|
91
98
|
*/
|
|
92
99
|
export function extractTextContent(message) {
|
|
93
|
-
return segment.toString(message
|
|
100
|
+
return message.segments?.length ? segment.toString(messageElements(message)) : message.content;
|
|
94
101
|
}
|
|
95
102
|
/**
|
|
96
103
|
* 解析 AI 回复中的富媒体内容
|
|
@@ -116,7 +123,7 @@ export function parseRichMediaContent(content) {
|
|
|
116
123
|
*/
|
|
117
124
|
export function removeAtEndpoint(message, endpointAtIds) {
|
|
118
125
|
const endpointKeys = normalizeAtIds(endpointAtIds?.length ? endpointAtIds : collectEndpointAtIds(message));
|
|
119
|
-
return message
|
|
126
|
+
return messageElements(message)
|
|
120
127
|
.filter((seg) => !isAtSegmentForEndpoint(seg, endpointKeys))
|
|
121
128
|
.map((seg) => {
|
|
122
129
|
if (seg.type !== 'text' || !seg.data?.text)
|
|
@@ -145,15 +152,15 @@ function collectEndpointTrustedIds(endpointConfig) {
|
|
|
145
152
|
* 解析发送者角色集合(IM 群角色 + trigger + endpoints[].$config)
|
|
146
153
|
*/
|
|
147
154
|
export function resolveSenderRoles(message, config, endpointConfig) {
|
|
148
|
-
const scope = message
|
|
155
|
+
const scope = message.conversation.kind;
|
|
149
156
|
// process 适配器(stdin/本机 CLI):操作者即宿主用户,恒为 master
|
|
150
|
-
if (message
|
|
157
|
+
if ((message.clientAdapter ?? message.conversation.endpoint.adapter) === 'process') {
|
|
151
158
|
return {
|
|
152
159
|
scope,
|
|
153
160
|
roles: normalizeSenderRoles(['master']),
|
|
154
161
|
};
|
|
155
162
|
}
|
|
156
|
-
const senderId = String(message
|
|
163
|
+
const senderId = String(message.sender?.id ?? '');
|
|
157
164
|
const roles = [];
|
|
158
165
|
const masters = config.masters || [];
|
|
159
166
|
const globalTrusted = config.trusted || [];
|
|
@@ -181,7 +188,7 @@ export function shouldTriggerAI(message, config, options) {
|
|
|
181
188
|
return { triggered: false, content: '' };
|
|
182
189
|
}
|
|
183
190
|
const text = extractTextContent(message);
|
|
184
|
-
const scope = message
|
|
191
|
+
const scope = message.conversation.kind;
|
|
185
192
|
const isSharedSession = scope === 'group' || scope === 'channel';
|
|
186
193
|
// 检查忽略前缀
|
|
187
194
|
for (const prefix of fullConfig.ignorePrefixes) {
|
|
@@ -207,7 +214,7 @@ export function shouldTriggerAI(message, config, options) {
|
|
|
207
214
|
};
|
|
208
215
|
}
|
|
209
216
|
// 3. 检查私聊触发
|
|
210
|
-
if (fullConfig.respondToPrivate && message
|
|
217
|
+
if (fullConfig.respondToPrivate && message.conversation.kind === 'private') {
|
|
211
218
|
if (text.trim()) {
|
|
212
219
|
return { triggered: true, content: text.trim() };
|
|
213
220
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { Message } from '../../
|
|
1
|
+
import type { Message } from '../../plugin-runtime/im/contracts.js';
|
|
2
2
|
import type { MessageElement } from '../../types.js';
|
|
3
3
|
import { type ActionSegmentData } from './types.js';
|
|
4
4
|
export declare function actionSegment(data: ActionSegmentData): MessageElement;
|
|
5
|
-
export declare function getActionFromMessage(message: Message
|
|
5
|
+
export declare function getActionFromMessage(message: Message): ActionSegmentData | undefined;
|
|
6
6
|
/** 入站互动 action(按钮点击等),不应计入用户发言/消息统计 */
|
|
7
|
-
export declare function isActionMessage(message: Message
|
|
7
|
+
export declare function isActionMessage(message: Message): boolean;
|
|
8
8
|
/** 去掉 @bot、XML at 段、首尾空白(QQ 指令预填文本归一化) */
|
|
9
9
|
export declare function stripInteractiveCommandText(raw: string): string;
|
|
10
10
|
/** 从文本降级输入解析 payload(需在 active session 的 fallback.map 中查找) */
|
|
@@ -3,7 +3,7 @@ export function actionSegment(data) {
|
|
|
3
3
|
return { type: ACTION_SEGMENT_TYPE, data };
|
|
4
4
|
}
|
|
5
5
|
export function getActionFromMessage(message) {
|
|
6
|
-
for (const item of message
|
|
6
|
+
for (const item of message.segments ?? []) {
|
|
7
7
|
if (typeof item === 'string')
|
|
8
8
|
continue;
|
|
9
9
|
if (isActionSegment(item))
|
|
@@ -1,32 +1,29 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Message enrich —
|
|
2
|
+
* Message enrich — inbound authorization snapshots and synthetic communication context.
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
5
|
-
import type { SendContent } from '../
|
|
4
|
+
import type { ConversationRef, DeliveryReceipt } from '@zhin.js/im-contract';
|
|
5
|
+
import type { Message, MessageSenderRef, Segment, SendContent } from '../plugin-runtime/im/contracts.js';
|
|
6
6
|
import type { SenderRole } from './roles.js';
|
|
7
|
-
/** Agent
|
|
7
|
+
/** Agent turns may attach host-owned metadata to the canonical runtime message. */
|
|
8
8
|
export type AgentTurnMessage = Message<{
|
|
9
9
|
extra?: Record<string, unknown>;
|
|
10
10
|
}>;
|
|
11
|
-
export declare function senderRolesFromMessage(message: Message
|
|
11
|
+
export declare function senderRolesFromMessage(message: Message): readonly SenderRole[];
|
|
12
12
|
export interface SyntheticMessageInput {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
channel: MessageChannel;
|
|
23
|
-
id?: string;
|
|
24
|
-
reply?: (content: SendContent, quote?: boolean | string) => Promise<string>;
|
|
13
|
+
conversation: ConversationRef;
|
|
14
|
+
sender?: MessageSenderRef;
|
|
15
|
+
content?: string;
|
|
16
|
+
segments?: readonly Segment[];
|
|
17
|
+
messageId?: string;
|
|
18
|
+
generation?: number;
|
|
19
|
+
endpointId?: string;
|
|
20
|
+
clientAdapter?: string;
|
|
21
|
+
reply?: (content: SendContent) => Promise<DeliveryReceipt>;
|
|
25
22
|
extra?: Record<string, unknown>;
|
|
26
23
|
}
|
|
27
|
-
/** cron
|
|
24
|
+
/** Construct a canonical Message for cron, subagent, and mission turns without live ingress. */
|
|
28
25
|
export declare function createSyntheticMessage(input: SyntheticMessageInput): AgentTurnMessage;
|
|
29
|
-
/**
|
|
30
|
-
export declare function commMessageFromHookContext(context: Record<string, unknown>): Message
|
|
31
|
-
/**
|
|
32
|
-
export declare function resolveContextKey(message: Message
|
|
26
|
+
/** Read a canonical communication message from an AI hook context. */
|
|
27
|
+
export declare function commMessageFromHookContext(context: Record<string, unknown>): Message | undefined;
|
|
28
|
+
/** Resolve tool context injection keys from the canonical Message contract. */
|
|
29
|
+
export declare function resolveContextKey(message: Message, key: string): unknown;
|
|
@@ -1,67 +1,72 @@
|
|
|
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
|
-
}
|
|
1
|
+
import { segmentsToPlainText } from './segment-contract/text.js';
|
|
12
2
|
export function senderRolesFromMessage(message) {
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
return frameworkRolesFromSenderFlags(sender);
|
|
16
|
-
}
|
|
17
|
-
return ['user'];
|
|
3
|
+
const roles = message.sender?.roles?.filter((role) => role === 'master' || role === 'trusted' || role === 'user');
|
|
4
|
+
return roles?.length ? roles : ['user'];
|
|
18
5
|
}
|
|
19
|
-
/** cron
|
|
6
|
+
/** Construct a canonical Message for cron, subagent, and mission turns without live ingress. */
|
|
20
7
|
export function createSyntheticMessage(input) {
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
8
|
+
const messageId = input.messageId ?? `synthetic:${Date.now()}`;
|
|
9
|
+
const unsupportedReply = async () => Object.freeze({
|
|
10
|
+
status: 'unsupported',
|
|
11
|
+
failure: Object.freeze({
|
|
12
|
+
code: 'synthetic_reply_unavailable',
|
|
13
|
+
message: 'Synthetic Message has no outbound delivery port',
|
|
14
|
+
retryable: false,
|
|
15
|
+
}),
|
|
16
|
+
});
|
|
17
|
+
const reply = input.reply ?? unsupportedReply;
|
|
18
|
+
const segments = input.segments ? Object.freeze([...input.segments]) : undefined;
|
|
19
|
+
const content = segments ? segmentsToPlainText(segments) : (input.content ?? '');
|
|
20
|
+
return Object.freeze({
|
|
21
|
+
conversation: input.conversation,
|
|
22
|
+
content,
|
|
23
|
+
generation: input.generation ?? 0,
|
|
24
|
+
metadata: Object.freeze({}),
|
|
25
|
+
segments,
|
|
26
|
+
sender: input.sender,
|
|
27
|
+
message: Object.freeze({ conversation: input.conversation, id: messageId }),
|
|
28
|
+
endpointId: input.endpointId,
|
|
29
|
+
clientAdapter: input.clientAdapter,
|
|
30
|
+
id: messageId,
|
|
31
|
+
extra: input.extra,
|
|
32
|
+
get $client() {
|
|
33
|
+
throw new Error('Synthetic Message has no Endpoint Client context');
|
|
34
|
+
},
|
|
29
35
|
$reply: reply,
|
|
30
|
-
$
|
|
31
|
-
$
|
|
32
|
-
$
|
|
33
|
-
$
|
|
36
|
+
$replyFrom: () => unsupportedReply(),
|
|
37
|
+
$sendTo: () => unsupportedReply(),
|
|
38
|
+
$replyToPrivate: () => unsupportedReply(),
|
|
39
|
+
$replyToGroup: () => unsupportedReply(),
|
|
40
|
+
$replyToChannel: () => unsupportedReply(),
|
|
34
41
|
});
|
|
35
42
|
}
|
|
36
|
-
/**
|
|
43
|
+
/** Read a canonical communication message from an AI hook context. */
|
|
37
44
|
export function commMessageFromHookContext(context) {
|
|
38
45
|
const raw = context.commMessage;
|
|
39
|
-
if (raw && typeof raw === 'object' && '
|
|
46
|
+
if (raw && typeof raw === 'object' && 'conversation' in raw && 'content' in raw) {
|
|
40
47
|
return raw;
|
|
41
48
|
}
|
|
42
49
|
return undefined;
|
|
43
50
|
}
|
|
44
|
-
/**
|
|
51
|
+
/** Resolve tool context injection keys from the canonical Message contract. */
|
|
45
52
|
export function resolveContextKey(message, key) {
|
|
46
53
|
switch (key) {
|
|
47
54
|
case 'platform':
|
|
48
|
-
return message
|
|
55
|
+
return message.clientAdapter ?? message.conversation.endpoint.adapter;
|
|
49
56
|
case 'endpointKey':
|
|
50
|
-
return message
|
|
57
|
+
return message.endpointId ?? message.conversation.endpoint.id;
|
|
51
58
|
case 'messageId':
|
|
52
|
-
return message
|
|
59
|
+
return message.id;
|
|
53
60
|
case 'sceneId':
|
|
54
|
-
return message
|
|
61
|
+
return message.conversation.id ?? message.sender?.id;
|
|
55
62
|
case 'senderId':
|
|
56
|
-
return message
|
|
63
|
+
return message.sender?.id;
|
|
57
64
|
case 'scope':
|
|
58
|
-
return message
|
|
65
|
+
return message.conversation.kind;
|
|
59
66
|
default: {
|
|
60
|
-
if (key in message)
|
|
67
|
+
if (key in message)
|
|
61
68
|
return message[key];
|
|
62
|
-
|
|
63
|
-
const extra = message.extra;
|
|
64
|
-
return extra?.[key];
|
|
69
|
+
return message.extra?.[key];
|
|
65
70
|
}
|
|
66
71
|
}
|
|
67
72
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { isStrictCanonicalSegment } from './validate.js';
|
|
2
2
|
const STRICT_CANONICAL_TYPES = new Set([
|
|
3
|
-
'text', 'mention', 'image', 'audio', 'video', 'file', 'reply', 'forward',
|
|
3
|
+
'text', 'mention', 'image', 'audio', 'video', 'file', 'reply', 'forward',
|
|
4
|
+
'face', 'dice', 'rps', 'share',
|
|
4
5
|
]);
|
|
5
6
|
// These are Core-owned wire extensions rather than canonical data types. Keep
|
|
6
7
|
// their gradual contracts isolated here; third-party extensions must be
|
|
@@ -1,5 +1,5 @@
|
|
|
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';
|
|
1
|
+
export type { Segment, SegmentBase, MediaRef, TextSegment, MentionSegment, ImageSegment, AudioSegment, VideoSegment, FileSegment, ReplySegment, ForwardSegment, ShareSegment, FaceSegment, DiceSegment, RpsSegment, } from './types.js';
|
|
2
|
+
export { mediaRefSchema, textSegmentSchema, mentionSegmentSchema, imageSegmentSchema, audioSegmentSchema, videoSegmentSchema, fileSegmentSchema, replySegmentSchema, forwardSegmentSchema, faceSegmentSchema, diceSegmentSchema, rpsSegmentSchema, shareSegmentSchema, canonicalSegmentSchema, segmentArraySchema, } from './validate.js';
|
|
3
3
|
export { assertCanonicalSegments, isCanonicalSegment } from './assert.js';
|
|
4
4
|
export { mediaRefJsonSchema, outboundSegmentJsonSchema, aiOutboundJsonSchema, STRICT_OUTBOUND_SEGMENT_TYPES, } from './json-schema.js';
|
|
5
5
|
export { segmentsForImDelivery } from './delivery.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { mediaRefSchema, textSegmentSchema, mentionSegmentSchema, imageSegmentSchema, audioSegmentSchema, videoSegmentSchema, fileSegmentSchema, replySegmentSchema, forwardSegmentSchema, faceSegmentSchema, diceSegmentSchema, rpsSegmentSchema, canonicalSegmentSchema, segmentArraySchema, } from './validate.js';
|
|
1
|
+
export { mediaRefSchema, textSegmentSchema, mentionSegmentSchema, imageSegmentSchema, audioSegmentSchema, videoSegmentSchema, fileSegmentSchema, replySegmentSchema, forwardSegmentSchema, faceSegmentSchema, diceSegmentSchema, rpsSegmentSchema, shareSegmentSchema, canonicalSegmentSchema, segmentArraySchema, } from './validate.js';
|
|
2
2
|
export { assertCanonicalSegments, isCanonicalSegment } from './assert.js';
|
|
3
3
|
export { mediaRefJsonSchema, outboundSegmentJsonSchema, aiOutboundJsonSchema, STRICT_OUTBOUND_SEGMENT_TYPES, } from './json-schema.js';
|
|
4
4
|
export { segmentsForImDelivery } from './delivery.js';
|
|
@@ -13,7 +13,7 @@ type JsonSchemaObject = Record<string, unknown>;
|
|
|
13
13
|
/** MediaRef(canonical 媒体引用):types.ts 的 MediaRef 接口 */
|
|
14
14
|
export declare const mediaRefJsonSchema: JsonSchemaObject;
|
|
15
15
|
/** 严格段类型集合(SSOT:assert.ts STRICT_CANONICAL_TYPES) */
|
|
16
|
-
export declare const STRICT_OUTBOUND_SEGMENT_TYPES: readonly ["text", "mention", "image", "reply", "forward", "face", "dice", "rps"];
|
|
16
|
+
export declare const STRICT_OUTBOUND_SEGMENT_TYPES: readonly ["text", "mention", "image", "audio", "video", "file", "reply", "forward", "face", "dice", "rps", "share"];
|
|
17
17
|
/**
|
|
18
18
|
* 单条 outbound 消息段的 JSON Schema。
|
|
19
19
|
* 严格段镜像 validate.ts 的 data 约束;宽松段走 generic 分支。
|
|
@@ -50,7 +50,7 @@ const looseBranch = {
|
|
|
50
50
|
properties: {
|
|
51
51
|
type: {
|
|
52
52
|
type: 'string',
|
|
53
|
-
enum: ['
|
|
53
|
+
enum: ['voice', 'record', 'link', 'markdown', 'html', 'keyboard', 'action'],
|
|
54
54
|
},
|
|
55
55
|
data: { type: 'object' },
|
|
56
56
|
platform: platformJsonSchema,
|
|
@@ -60,7 +60,8 @@ const looseBranch = {
|
|
|
60
60
|
};
|
|
61
61
|
/** 严格段类型集合(SSOT:assert.ts STRICT_CANONICAL_TYPES) */
|
|
62
62
|
export const STRICT_OUTBOUND_SEGMENT_TYPES = [
|
|
63
|
-
'text', 'mention', 'image', '
|
|
63
|
+
'text', 'mention', 'image', 'audio', 'video', 'file', 'reply', 'forward',
|
|
64
|
+
'face', 'dice', 'rps', 'share',
|
|
64
65
|
];
|
|
65
66
|
/**
|
|
66
67
|
* 单条 outbound 消息段的 JSON Schema。
|
|
@@ -80,6 +81,19 @@ export const outboundSegmentJsonSchema = {
|
|
|
80
81
|
media: mediaRefJsonSchema,
|
|
81
82
|
alt: { type: 'string' },
|
|
82
83
|
}, ['media'])),
|
|
84
|
+
strictBranch('audio', dataObject({
|
|
85
|
+
media: mediaRefJsonSchema,
|
|
86
|
+
duration: { type: 'number' },
|
|
87
|
+
}, ['media'])),
|
|
88
|
+
strictBranch('video', dataObject({
|
|
89
|
+
media: mediaRefJsonSchema,
|
|
90
|
+
duration: { type: 'number' },
|
|
91
|
+
alt: { type: 'string' },
|
|
92
|
+
}, ['media'])),
|
|
93
|
+
strictBranch('file', dataObject({
|
|
94
|
+
media: mediaRefJsonSchema,
|
|
95
|
+
name: { type: 'string' },
|
|
96
|
+
}, ['media'])),
|
|
83
97
|
strictBranch('reply', dataObject({
|
|
84
98
|
message_id: { type: 'string', description: '被引用消息的平台消息 id' },
|
|
85
99
|
}, ['message_id'])),
|
|
@@ -98,6 +112,23 @@ export const outboundSegmentJsonSchema = {
|
|
|
98
112
|
strictBranch('rps', dataObject({
|
|
99
113
|
result: { type: 'number' },
|
|
100
114
|
}, [])),
|
|
115
|
+
strictBranch('share', dataObject({
|
|
116
|
+
url: { type: 'string', description: '分享跳转地址' },
|
|
117
|
+
title: { type: 'string', description: '分享标题' },
|
|
118
|
+
description: { type: 'string', description: '分享摘要' },
|
|
119
|
+
image: { type: 'string', description: '预览图地址' },
|
|
120
|
+
audio: { type: 'string', description: '音频地址' },
|
|
121
|
+
content: { type: 'string', description: '消息列表中的显示文字' },
|
|
122
|
+
artist: { type: 'string', description: '音乐分享的艺术家' },
|
|
123
|
+
duration: { type: 'number', description: '音频时长(秒)' },
|
|
124
|
+
config: dataObject({
|
|
125
|
+
appid: { type: 'number' },
|
|
126
|
+
package: { type: 'string' },
|
|
127
|
+
sign: { type: 'string' },
|
|
128
|
+
icon: { type: 'string' },
|
|
129
|
+
version: { type: 'string' },
|
|
130
|
+
}, ['appid']),
|
|
131
|
+
}, ['url', 'title'])),
|
|
101
132
|
looseBranch,
|
|
102
133
|
],
|
|
103
134
|
};
|
|
@@ -106,6 +106,13 @@ export function formatSegmentPreview(item) {
|
|
|
106
106
|
return clip(`${text} <${url}>`);
|
|
107
107
|
return clip(url || text || '[link]');
|
|
108
108
|
}
|
|
109
|
+
case 'share': {
|
|
110
|
+
const url = typeof data.url === 'string' ? data.url : '';
|
|
111
|
+
const title = typeof data.title === 'string' ? data.title : '';
|
|
112
|
+
if (title && url)
|
|
113
|
+
return clip(`[share] ${title} <${url}>`);
|
|
114
|
+
return clip(title || url || '[share]');
|
|
115
|
+
}
|
|
109
116
|
case 'dice':
|
|
110
117
|
return data.result != null ? `{dice}(${data.result})` : '{dice}';
|
|
111
118
|
case 'rps':
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** Canonical Segment SSOT lives in @zhin.js/im-contract. */
|
|
2
2
|
import type { MediaRef, SegmentBase } from '@zhin.js/im-contract';
|
|
3
|
-
export type { ForwardSegment, MediaRef, MentionSegment, ReplySegment, Segment, SegmentBase, TextSegment, } from '@zhin.js/im-contract';
|
|
3
|
+
export type { ForwardSegment, MediaRef, MentionSegment, ReplySegment, ShareSegment, Segment, SegmentBase, TextSegment, } from '@zhin.js/im-contract';
|
|
4
4
|
/**
|
|
5
5
|
* 媒体引用占位(全框架唯一媒体表达)。
|
|
6
6
|
* kind=file:平台侧不透明文件引用(如 Telegram file_id、Milky resource_id),
|
|
@@ -54,5 +54,5 @@ export interface RpsSegment extends SegmentBase {
|
|
|
54
54
|
result?: number;
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
|
-
/** 规范态 segment(严格校验 text / mention / image / audio / video / file / reply / forward / face / dice / rps) */
|
|
57
|
+
/** 规范态 segment(严格校验 text / mention / image / audio / video / file / reply / forward / face / dice / rps / share) */
|
|
58
58
|
export type LegacySpecificSegment = ImageSegment | AudioSegment | VideoSegment | FileSegment | FaceSegment | DiceSegment | RpsSegment;
|
|
@@ -223,6 +223,47 @@ export declare const rpsSegmentSchema: Schema<{
|
|
|
223
223
|
} | undefined;
|
|
224
224
|
platform?: Record<string, any> | undefined;
|
|
225
225
|
}>;
|
|
226
|
+
export declare const shareSegmentSchema: Schema<{
|
|
227
|
+
type?: "share" | undefined;
|
|
228
|
+
data?: {
|
|
229
|
+
url?: string | undefined;
|
|
230
|
+
title?: string | undefined;
|
|
231
|
+
description?: string | undefined;
|
|
232
|
+
image?: string | undefined;
|
|
233
|
+
audio?: string | undefined;
|
|
234
|
+
content?: string | undefined;
|
|
235
|
+
artist?: string | undefined;
|
|
236
|
+
duration?: number | undefined;
|
|
237
|
+
config?: {
|
|
238
|
+
appid?: number | undefined;
|
|
239
|
+
package?: string | undefined;
|
|
240
|
+
sign?: string | undefined;
|
|
241
|
+
icon?: string | undefined;
|
|
242
|
+
version?: string | undefined;
|
|
243
|
+
} | undefined;
|
|
244
|
+
} | undefined;
|
|
245
|
+
platform?: Record<string, any> | undefined;
|
|
246
|
+
}, {
|
|
247
|
+
type?: "share" | undefined;
|
|
248
|
+
data?: {
|
|
249
|
+
url?: string | undefined;
|
|
250
|
+
title?: string | undefined;
|
|
251
|
+
description?: string | undefined;
|
|
252
|
+
image?: string | undefined;
|
|
253
|
+
audio?: string | undefined;
|
|
254
|
+
content?: string | undefined;
|
|
255
|
+
artist?: string | undefined;
|
|
256
|
+
duration?: number | undefined;
|
|
257
|
+
config?: {
|
|
258
|
+
appid?: number | undefined;
|
|
259
|
+
package?: string | undefined;
|
|
260
|
+
sign?: string | undefined;
|
|
261
|
+
icon?: string | undefined;
|
|
262
|
+
version?: string | undefined;
|
|
263
|
+
} | undefined;
|
|
264
|
+
} | undefined;
|
|
265
|
+
platform?: Record<string, any> | undefined;
|
|
266
|
+
}>;
|
|
226
267
|
export declare const canonicalSegmentSchema: Schema<any, any>;
|
|
227
268
|
export declare const segmentArraySchema: Schema<any[], any[]>;
|
|
228
269
|
/** 严格 canonical 段校验(@zhin.js/schema validate-only) */
|
|
@@ -96,6 +96,28 @@ export const rpsSegmentSchema = Schema.object({
|
|
|
96
96
|
data: Schema.object({ result: Schema.number() }).required(),
|
|
97
97
|
platform: platformSchema,
|
|
98
98
|
});
|
|
99
|
+
const shareConfigSchema = Schema.object({
|
|
100
|
+
appid: Schema.number().required(),
|
|
101
|
+
package: Schema.string(),
|
|
102
|
+
sign: Schema.string(),
|
|
103
|
+
icon: Schema.string(),
|
|
104
|
+
version: Schema.string(),
|
|
105
|
+
});
|
|
106
|
+
export const shareSegmentSchema = Schema.object({
|
|
107
|
+
type: Schema.const('share'),
|
|
108
|
+
data: Schema.object({
|
|
109
|
+
url: Schema.string().required(),
|
|
110
|
+
title: Schema.string().required(),
|
|
111
|
+
description: Schema.string(),
|
|
112
|
+
image: Schema.string(),
|
|
113
|
+
audio: Schema.string(),
|
|
114
|
+
content: Schema.string(),
|
|
115
|
+
artist: Schema.string(),
|
|
116
|
+
duration: Schema.number(),
|
|
117
|
+
config: shareConfigSchema,
|
|
118
|
+
}).required(),
|
|
119
|
+
platform: platformSchema,
|
|
120
|
+
});
|
|
99
121
|
export const canonicalSegmentSchema = Schema.discriminatedUnion('type', {
|
|
100
122
|
text: {
|
|
101
123
|
data: Schema.object({ text: Schema.string().required() }).required(),
|
|
@@ -164,6 +186,20 @@ export const canonicalSegmentSchema = Schema.discriminatedUnion('type', {
|
|
|
164
186
|
data: Schema.object({ result: Schema.number() }).required(),
|
|
165
187
|
platform: platformSchema,
|
|
166
188
|
},
|
|
189
|
+
share: {
|
|
190
|
+
data: Schema.object({
|
|
191
|
+
url: Schema.string().required(),
|
|
192
|
+
title: Schema.string().required(),
|
|
193
|
+
description: Schema.string(),
|
|
194
|
+
image: Schema.string(),
|
|
195
|
+
audio: Schema.string(),
|
|
196
|
+
content: Schema.string(),
|
|
197
|
+
artist: Schema.string(),
|
|
198
|
+
duration: Schema.number(),
|
|
199
|
+
config: shareConfigSchema,
|
|
200
|
+
}).required(),
|
|
201
|
+
platform: platformSchema,
|
|
202
|
+
},
|
|
167
203
|
});
|
|
168
204
|
export const segmentArraySchema = Schema.list(canonicalSegmentSchema);
|
|
169
205
|
/** 严格 canonical 段校验(@zhin.js/schema validate-only) */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type PermissionHost } from '@zhin.js/permission';
|
|
2
|
-
import type { Message } from '../
|
|
2
|
+
import type { Message } from '../plugin-runtime/im/contracts.js';
|
|
3
3
|
import type { Tool } from '../types.js';
|
|
4
4
|
/** Checks whether a Tool is visible to the current IM message context. */
|
|
5
|
-
export declare function canAccessTool(tool: Tool, message?: Message
|
|
5
|
+
export declare function canAccessTool(tool: Tool, message?: Message, host?: PermissionHost | null): Promise<boolean>;
|
package/lib/built/tool-access.js
CHANGED
|
@@ -3,8 +3,8 @@ import { toPermissionSubject } from '@zhin.js/permission';
|
|
|
3
3
|
export async function canAccessTool(tool, message, host) {
|
|
4
4
|
if (!message)
|
|
5
5
|
return !tool.platforms?.length && !tool.scopes?.length && !tool.permissions?.length;
|
|
6
|
-
const adapter = String(message
|
|
7
|
-
const scope =
|
|
6
|
+
const adapter = String(message.clientAdapter ?? message.conversation.endpoint.adapter);
|
|
7
|
+
const scope = message.conversation.kind;
|
|
8
8
|
if (tool.platforms?.length && !tool.platforms.includes(adapter))
|
|
9
9
|
return false;
|
|
10
10
|
if (tool.scopes?.length && !tool.scopes.includes(scope))
|
package/lib/im-scene.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { MessageType } from './message.js';
|
|
2
|
+
import type { Message } from './plugin-runtime/im/contracts.js';
|
|
2
3
|
import type { SendContent, SendOptions } from './types.js';
|
|
3
4
|
export type IMSceneKind = MessageType;
|
|
4
5
|
/** Parent scene kind (distinct from sub-channel MessageType.channel). */
|
|
@@ -23,7 +24,7 @@ export interface IMDeliveryTarget {
|
|
|
23
24
|
}
|
|
24
25
|
/** Normalize legacy parent.type channel → guild. */
|
|
25
26
|
export declare function normalizeIMSceneParentKind(value: unknown): IMSceneParentKind | undefined;
|
|
26
|
-
export declare function sceneRefFromMessage(message: Partial<Message
|
|
27
|
+
export declare function sceneRefFromMessage(message: Partial<Message>): IMSceneRef | undefined;
|
|
27
28
|
export declare function sceneRefToSendOptions(target: IMDeliveryTarget, content: SendContent): SendOptions;
|
|
28
29
|
export declare function resolveIMSceneSessionId(scene: IMSceneRef): string;
|
|
29
|
-
export declare function messageToIMDeliveryTarget(message: Partial<Message
|
|
30
|
+
export declare function messageToIMDeliveryTarget(message: Partial<Message>): IMDeliveryTarget | undefined;
|