@zhin.js/core 1.1.49 → 1.1.51
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 +4 -0
- package/lib/ai-completion-contract.d.ts +13 -0
- package/lib/built/ai-trigger.d.ts +4 -2
- package/lib/built/ai-trigger.js +1 -0
- package/lib/built/generic-segment-mapper.js +2 -1
- package/lib/built/interaction-messages.d.ts +1 -0
- package/lib/built/interaction-messages.js +29 -0
- package/lib/built/message-enrich.d.ts +2 -0
- package/lib/built/message-enrich.js +3 -0
- package/lib/built/segment-contract/index.d.ts +1 -1
- package/lib/built/segment-contract/json-schema.d.ts +10 -1
- package/lib/built/segment-contract/json-schema.js +35 -5
- package/lib/built/user-interaction.d.ts +4 -3
- package/lib/built/user-interaction.js +49 -40
- package/lib/feature/command-public-api.d.ts +5 -0
- package/lib/feature/command-public-api.js +5 -0
- package/lib/feature/command.d.ts +4 -0
- package/lib/feature/command.js +2 -0
- package/lib/feature/middleware-public-api.d.ts +1 -0
- package/lib/feature/middleware-public-api.js +1 -0
- package/lib/feature/middleware.d.ts +2 -0
- package/lib/feature/middleware.js +1 -0
- package/lib/notice.d.ts +4 -0
- package/lib/notice.js +2 -0
- package/lib/plugin-runtime/im/command-feedback.d.ts +38 -0
- package/lib/plugin-runtime/im/command-feedback.js +73 -0
- package/lib/plugin-runtime/im/command-menu-navigation.d.ts +1 -1
- package/lib/plugin-runtime/im/command-menu-navigation.js +16 -7
- package/lib/plugin-runtime/im/command-menu-presentation.d.ts +14 -0
- package/lib/plugin-runtime/im/command-menu-presentation.js +25 -0
- package/lib/plugin-runtime/im/contracts.d.ts +25 -2
- package/lib/plugin-runtime/im/contracts.js +21 -1
- package/lib/plugin-runtime/im/conversation-runtime.d.ts +6 -2
- package/lib/plugin-runtime/im/conversation-runtime.js +71 -3
- package/lib/plugin-runtime/im/endpoint-runtime.d.ts +5 -2
- package/lib/plugin-runtime/im/endpoint-runtime.js +3 -1
- package/lib/plugin-runtime/im/im-runtime.d.ts +3 -1
- package/lib/plugin-runtime/im/im-runtime.js +39 -2
- package/lib/plugin-runtime/im/inbound-runtime.d.ts +4 -1
- package/lib/plugin-runtime/im/inbound-runtime.js +20 -6
- package/lib/plugin-runtime/im/index.d.ts +2 -1
- package/lib/plugin-runtime/im/index.js +1 -0
- package/lib/plugin-runtime/im/interaction-runtime.d.ts +2 -0
- package/lib/plugin-runtime/im/interaction-runtime.js +15 -11
- package/lib/plugin-runtime/im/jsx-button-registry.js +2 -1
- package/lib/plugin-runtime/im/locale.d.ts +5 -0
- package/lib/plugin-runtime/im/locale.js +12 -0
- package/lib/plugin-runtime/im/member-welcome.d.ts +8 -0
- package/lib/plugin-runtime/im/member-welcome.js +11 -0
- package/lib/plugin-runtime/im/message-dispatcher.d.ts +16 -2
- package/lib/plugin-runtime/im/message-dispatcher.js +24 -28
- package/lib/plugin-runtime/im/message-events.d.ts +20 -0
- package/lib/plugin-runtime/im/message-events.js +17 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.js +7 -1
- package/lib/plugin-runtime/im/outbound-presentation.d.ts +19 -0
- package/lib/plugin-runtime/im/outbound-presentation.js +41 -0
- package/lib/plugin-runtime/im/outbound-projection.d.ts +2 -0
- package/lib/plugin-runtime/im/outbound-projection.js +3 -3
- package/lib/plugin-runtime/im/outbound-renderer.js +4 -1
- package/lib/plugin-runtime/im/outbound-segments.d.ts +3 -1
- package/lib/plugin-runtime/im/outbound-segments.js +31 -5
- package/lib/side-event/dispatch.js +26 -4
- package/lib/side-event/index.d.ts +1 -0
- package/lib/side-event/index.js +1 -0
- package/lib/side-event/normalize.js +2 -1
- package/lib/side-event/scene-events.d.ts +40 -0
- package/lib/side-event/scene-events.js +147 -0
- package/lib/side-event/types.d.ts +1 -1
- package/lib/side-event/types.js +7 -0
- package/lib/types.d.ts +9 -1
- package/package.json +15 -15
package/README.md
CHANGED
|
@@ -146,3 +146,7 @@ pnpm check:architecture
|
|
|
146
146
|
## 许可证
|
|
147
147
|
|
|
148
148
|
MIT License
|
|
149
|
+
|
|
150
|
+
## 定制内置展示
|
|
151
|
+
|
|
152
|
+
应用 Root 可从 `zhin.js/command` 选择 `commandFeedbackToken`、`commandMenuPresentationToken`,从 `zhin.js/middleware` 选择 `outboundPresentationToken`。保留默认反馈、分页、权限与平台能力;不绑定或返回 `undefined` 继续使用内置展示。具体上下文、JSX 作者和失败边界见[定制内置展示](../../../docs/authoring/behavior-customization.md)。
|
|
@@ -13,6 +13,11 @@ export interface UsageSubject {
|
|
|
13
13
|
readonly groupId?: string;
|
|
14
14
|
readonly owner: boolean;
|
|
15
15
|
}
|
|
16
|
+
/** Original input used to estimate one participant's share of a completion. */
|
|
17
|
+
export interface AiCompletionUsageShare {
|
|
18
|
+
readonly subject: UsageSubject;
|
|
19
|
+
readonly text: string;
|
|
20
|
+
}
|
|
16
21
|
export interface AiCompletionUntrustedInput {
|
|
17
22
|
/** Where the text came from, e.g. `group-chat`; shown to the model as the block's source. */
|
|
18
23
|
readonly source: string;
|
|
@@ -38,6 +43,8 @@ export interface AiCompletionInput {
|
|
|
38
43
|
readonly signal?: AbortSignal;
|
|
39
44
|
/** The IM user (and group) this call is made for; counts against their usage quota. */
|
|
40
45
|
readonly onBehalfOf?: UsageSubject;
|
|
46
|
+
/** Shared attribution, mutually exclusive with onBehalfOf. Denied users must be removed from the prompt before retrying. */
|
|
47
|
+
readonly usageShares?: readonly AiCompletionUsageShare[];
|
|
41
48
|
}
|
|
42
49
|
/** Token cost breakdown; structurally identical to `@zhin.js/ai` `TokenCost`. */
|
|
43
50
|
export interface AiCompletionTokenCost {
|
|
@@ -49,6 +56,8 @@ export interface AiCompletionTokenCost {
|
|
|
49
56
|
}
|
|
50
57
|
/** Token usage; structurally identical to `@zhin.js/ai` `TokenUsage` (asserted by agent type tests). */
|
|
51
58
|
export interface AiCompletionTokenUsage {
|
|
59
|
+
/** Availability metadata mirrors TokenUsage without importing the optional AI package. */
|
|
60
|
+
reported?: Partial<Record<'input' | 'output' | 'totalTokens' | 'cacheRead' | 'cacheWrite' | 'reasoning', boolean>>;
|
|
52
61
|
input: number;
|
|
53
62
|
output: number;
|
|
54
63
|
cacheRead: number;
|
|
@@ -65,6 +74,8 @@ export interface AiCompletionResult {
|
|
|
65
74
|
readonly usage?: AiCompletionTokenUsage;
|
|
66
75
|
}
|
|
67
76
|
export interface AiCompletion {
|
|
77
|
+
/** Older/custom hosts may omit this; callers must not send shared attribution to such hosts. */
|
|
78
|
+
readonly supportsUsageShares?: true;
|
|
68
79
|
completeText(input: AiCompletionInput): Promise<AiCompletionResult>;
|
|
69
80
|
}
|
|
70
81
|
/**
|
|
@@ -77,6 +88,8 @@ export interface AiCompletionQuotaErrorLike extends Error {
|
|
|
77
88
|
readonly name: 'AiCompletionQuotaError';
|
|
78
89
|
readonly scope: 'user' | 'group';
|
|
79
90
|
readonly retryAfterSeconds: number;
|
|
91
|
+
/** Batch preflight rejected these users without starting a provider request or reserving quota. */
|
|
92
|
+
readonly deniedUserIds?: readonly string[];
|
|
80
93
|
}
|
|
81
94
|
/** True for the agent's quota error (`message` is user-facing); works without importing `@zhin.js/agent`. */
|
|
82
95
|
export declare function isAiCompletionQuotaError(error: unknown): error is AiCompletionQuotaErrorLike;
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* 3. 私聊直接对话 - 私聊时直接对话
|
|
9
9
|
* 4. 关键词触发 - 非群/频道场景下匹配关键词时触发
|
|
10
10
|
*/
|
|
11
|
-
import type { AmbientEngagementConfig } from '@zhin.js/ai';
|
|
11
|
+
import type { AmbientEngagementConfig, SceneEventWakeConfig } from '@zhin.js/ai';
|
|
12
12
|
import type { Message } from "../plugin-runtime/im/contracts.js";
|
|
13
13
|
import type { MessageElement, ToolScope } from "../types.js";
|
|
14
14
|
import { type SenderRole } from './roles.js';
|
|
@@ -32,7 +32,7 @@ export interface AITriggerConfig {
|
|
|
32
32
|
timeout?: number;
|
|
33
33
|
/** 思考中提示(可选) */
|
|
34
34
|
thinkingMessage?: string;
|
|
35
|
-
/**
|
|
35
|
+
/** 入口层错误提示模板(可选;不配置时用聊天口吻,主人额外看到原因) */
|
|
36
36
|
errorTemplate?: string;
|
|
37
37
|
/** 全局 master(trigger 级,赋予 master 角色) */
|
|
38
38
|
masters?: string[];
|
|
@@ -40,6 +40,8 @@ export interface AITriggerConfig {
|
|
|
40
40
|
trusted?: string[];
|
|
41
41
|
/** 群聊旁听:没人叫机器人时,由判断模型决定是否接话 */
|
|
42
42
|
ambient?: AmbientEngagementConfig;
|
|
43
|
+
/** 状态感知(#996):高优先社交事件独立唤醒回合(debounce + 限频) */
|
|
44
|
+
sceneEvents?: SceneEventWakeConfig;
|
|
43
45
|
}
|
|
44
46
|
/**
|
|
45
47
|
* AI 触发检查结果
|
package/lib/built/ai-trigger.js
CHANGED
|
@@ -85,7 +85,8 @@ function normalizeReply(seg) {
|
|
|
85
85
|
const messageId = String(data.message_id ?? data.id ?? '').trim();
|
|
86
86
|
if (!messageId)
|
|
87
87
|
return seg;
|
|
88
|
-
|
|
88
|
+
const platform = readPlatform(seg);
|
|
89
|
+
return { type: 'reply', data: { message_id: messageId }, ...(platform ? { platform } : {}) };
|
|
89
90
|
}
|
|
90
91
|
function normalizeLink(seg) {
|
|
91
92
|
const data = seg.data;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const interactionMessages: (locale: import("@zhin.js/plugin-runtime").Locale | undefined, key: "number" | "fallback" | "list" | "timeout" | "minLength" | "maxLength" | "pattern" | "integer" | "min" | "max" | "confirmOrCancel" | "select" | "invalidSelection" | "minSelections" | "maxSelections" | "minItems" | "maxItems" | "invalidNumbers" | "invalidBooleans" | "confirm" | "cancel" | "confirmation" | "selection" | "multiple" | "ended" | "expired", values?: import("@zhin.js/plugin-runtime").MessageValues) => string;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createMessages } from '@zhin.js/plugin-runtime';
|
|
2
|
+
export const interactionMessages = createMessages({
|
|
3
|
+
fallback: { 'zh-CN': '也可以直接回复对应内容。', en: 'You can also reply with the corresponding answer.' },
|
|
4
|
+
minLength: { 'zh-CN': '请至少输入 {count} 个字符', en: 'Enter at least {count} characters' },
|
|
5
|
+
maxLength: { 'zh-CN': '请不要超过 {count} 个字符', en: 'Enter no more than {count} characters' },
|
|
6
|
+
pattern: { 'zh-CN': '输入格式不正确', en: 'The input format is invalid' },
|
|
7
|
+
number: { 'zh-CN': '请输入有效数字', en: 'Enter a valid number' },
|
|
8
|
+
integer: { 'zh-CN': '请输入整数', en: 'Enter an integer' },
|
|
9
|
+
min: { 'zh-CN': '请输入不小于 {count} 的数字', en: 'Enter a number greater than or equal to {count}' },
|
|
10
|
+
max: { 'zh-CN': '请输入不大于 {count} 的数字', en: 'Enter a number less than or equal to {count}' },
|
|
11
|
+
confirmOrCancel: { 'zh-CN': '请选择确认或取消', en: 'Choose confirm or cancel' },
|
|
12
|
+
select: { 'zh-CN': '请选择一个有效选项', en: 'Choose a valid option' },
|
|
13
|
+
invalidSelection: { 'zh-CN': '多选中包含无效选项', en: 'The selection contains an invalid option' },
|
|
14
|
+
minSelections: { 'zh-CN': '请至少选择 {count} 项', en: 'Select at least {count} items' },
|
|
15
|
+
maxSelections: { 'zh-CN': '请最多选择 {count} 项', en: 'Select no more than {count} items' },
|
|
16
|
+
minItems: { 'zh-CN': '请至少输入 {count} 项', en: 'Enter at least {count} items' },
|
|
17
|
+
maxItems: { 'zh-CN': '请最多输入 {count} 项', en: 'Enter no more than {count} items' },
|
|
18
|
+
invalidNumbers: { 'zh-CN': '列表中包含无效数字', en: 'The list contains an invalid number' },
|
|
19
|
+
invalidBooleans: { 'zh-CN': '布尔列表只接受 true/false 或 是/否', en: 'Boolean lists accept only true/false or yes/no' },
|
|
20
|
+
confirm: { 'zh-CN': '确认', en: 'Confirm' },
|
|
21
|
+
cancel: { 'zh-CN': '取消', en: 'Cancel' },
|
|
22
|
+
confirmation: { 'zh-CN': '请选择确认或取消。', en: 'Choose confirm or cancel.' },
|
|
23
|
+
selection: { 'zh-CN': '可以点击按钮,或回复序号/选项名称。', en: 'Click a button, or reply with an option number or name.' },
|
|
24
|
+
multiple: { 'zh-CN': '回复序号或选项名称,多项用“{separator}”分隔。', en: 'Reply with option numbers or names, separated by "{separator}".' },
|
|
25
|
+
list: { 'zh-CN': '多项用“{separator}”分隔。', en: 'Separate items with "{separator}".' },
|
|
26
|
+
ended: { 'zh-CN': '交互已结束', en: 'Interaction ended' },
|
|
27
|
+
expired: { 'zh-CN': '交互已过期或不可用,请重新发起。', en: 'This interaction has expired or is unavailable. Start a new one.' },
|
|
28
|
+
timeout: { 'zh-CN': '输入超时', en: 'Input timed out' },
|
|
29
|
+
});
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
* Message enrich — inbound authorization snapshots and synthetic communication context.
|
|
3
3
|
*/
|
|
4
4
|
import type { ConversationRef, DeliveryReceipt } from '@zhin.js/im-contract';
|
|
5
|
+
import { type Locale } from '@zhin.js/plugin-runtime';
|
|
5
6
|
import type { Message, MessageSenderRef, Segment, SendContent } from '../plugin-runtime/im/contracts.js';
|
|
6
7
|
import type { SenderRole } from './roles.js';
|
|
7
8
|
/** Agent turns may attach host-owned metadata to the canonical runtime message. */
|
|
@@ -10,6 +11,7 @@ export type AgentTurnMessage = Message<{
|
|
|
10
11
|
}>;
|
|
11
12
|
export declare function senderRolesFromMessage(message: Message): readonly SenderRole[];
|
|
12
13
|
export interface SyntheticMessageInput {
|
|
14
|
+
locale?: Locale;
|
|
13
15
|
conversation: ConversationRef;
|
|
14
16
|
sender?: MessageSenderRef;
|
|
15
17
|
content?: string;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { resolveLocale } from '@zhin.js/plugin-runtime';
|
|
1
2
|
import { segmentsToPlainText } from './segment-contract/text.js';
|
|
2
3
|
export function senderRolesFromMessage(message) {
|
|
3
4
|
const roles = message.sender?.roles?.filter((role) => role === 'master' || role === 'trusted' || role === 'user');
|
|
@@ -18,6 +19,8 @@ export function createSyntheticMessage(input) {
|
|
|
18
19
|
const segments = input.segments ? Object.freeze([...input.segments]) : undefined;
|
|
19
20
|
const content = segments ? segmentsToPlainText(segments) : (input.content ?? '');
|
|
20
21
|
return Object.freeze({
|
|
22
|
+
locale: resolveLocale(input.locale),
|
|
23
|
+
formatCommand: () => undefined,
|
|
21
24
|
conversation: input.conversation,
|
|
22
25
|
content,
|
|
23
26
|
generation: input.generation ?? 0,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export type { Segment, SegmentBase, MediaRef, TextSegment, MentionSegment, ImageSegment, AudioSegment, VideoSegment, FileSegment, ReplySegment, ForwardSegment, ShareSegment, FaceSegment, DiceSegment, RpsSegment, } from './types.js';
|
|
2
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
|
-
export { mediaRefJsonSchema, outboundSegmentJsonSchema, aiOutboundJsonSchema, buildAiOutboundJsonSchema, AI_OUTBOUND_SEGMENT_TYPES, DEFAULT_AI_OUTBOUND_SEGMENT_TYPES, STRICT_OUTBOUND_SEGMENT_TYPES, type AiOutboundSegmentType, type AiSegmentExtension, type AiSegmentExtensions, type AiOutboundSegmentProfile, } from './json-schema.js';
|
|
4
|
+
export { mediaRefJsonSchema, outboundSegmentJsonSchema, aiOutboundJsonSchema, buildAiOutboundJsonSchema, AI_OUTBOUND_SEGMENT_TYPES, DEFAULT_AI_OUTBOUND_SEGMENT_TYPES, STRICT_OUTBOUND_SEGMENT_TYPES, type AiOutboundSegmentType, type AiSegmentExtension, type AiSegmentExtensions, type AiOutboundSegmentProfile, type AiReactionProfile, } from './json-schema.js';
|
|
5
5
|
export { segmentsForImDelivery } from './delivery.js';
|
|
6
6
|
export { isMediaRef, collectSegmentMedia, type SegmentMediaRef, } from './media.js';
|
|
7
7
|
export { createImageSegment } from './image.js';
|
|
@@ -36,10 +36,19 @@ export interface AiSegmentExtension {
|
|
|
36
36
|
readonly hint?: string;
|
|
37
37
|
}
|
|
38
38
|
export type AiSegmentExtensions = Readonly<Partial<Record<AiOutboundSegmentType, AiSegmentExtension>>>;
|
|
39
|
+
/** Reactions the model may put on the message it answers (platform value + meaning). */
|
|
40
|
+
export interface AiReactionProfile {
|
|
41
|
+
readonly values: readonly {
|
|
42
|
+
readonly value: string;
|
|
43
|
+
readonly label: string;
|
|
44
|
+
}[];
|
|
45
|
+
}
|
|
39
46
|
/** What one reply endpoint lets the model author: offered types plus their declared extensions. */
|
|
40
47
|
export interface AiOutboundSegmentProfile {
|
|
41
48
|
readonly types: readonly AiOutboundSegmentType[];
|
|
42
49
|
readonly extensions?: AiSegmentExtensions;
|
|
50
|
+
/** Present when the endpoint can react to the current message in this conversation. */
|
|
51
|
+
readonly reaction?: AiReactionProfile;
|
|
43
52
|
}
|
|
44
53
|
/** Offered when the adapter declares no `segments.supported` list. */
|
|
45
54
|
export declare const DEFAULT_AI_OUTBOUND_SEGMENT_TYPES: readonly AiOutboundSegmentType[];
|
|
@@ -48,6 +57,6 @@ export declare const DEFAULT_AI_OUTBOUND_SEGMENT_TYPES: readonly AiOutboundSegme
|
|
|
48
57
|
* is an ordered segment array; the model decides how to split, like a person
|
|
49
58
|
* sending a few short messages. No text/mentions shorthand.
|
|
50
59
|
*/
|
|
51
|
-
export declare function buildAiOutboundJsonSchema(types?: readonly AiOutboundSegmentType[], extensions?: AiSegmentExtensions): JsonSchemaObject;
|
|
60
|
+
export declare function buildAiOutboundJsonSchema(types?: readonly AiOutboundSegmentType[], extensions?: AiSegmentExtensions, reaction?: AiReactionProfile): JsonSchemaObject;
|
|
52
61
|
export declare const aiOutboundJsonSchema: JsonSchemaObject;
|
|
53
62
|
export {};
|
|
@@ -236,12 +236,36 @@ function withModelFields(branch, fields) {
|
|
|
236
236
|
},
|
|
237
237
|
};
|
|
238
238
|
}
|
|
239
|
+
/** Merge only branches whose complete constraints match apart from the type tag.
|
|
240
|
+
* Inline enums work across strict output providers without introducing $ref dialects.
|
|
241
|
+
*/
|
|
242
|
+
function mergeEquivalentBranches(branches) {
|
|
243
|
+
const groups = new Map();
|
|
244
|
+
for (const branch of branches) {
|
|
245
|
+
const properties = branch.properties;
|
|
246
|
+
const tag = properties.type;
|
|
247
|
+
const type = tag.const;
|
|
248
|
+
const shape = { ...branch, properties: { ...properties, type: { type: 'string' } } };
|
|
249
|
+
const key = JSON.stringify(shape);
|
|
250
|
+
const group = groups.get(key);
|
|
251
|
+
if (group) {
|
|
252
|
+
if (!group.types.includes(type))
|
|
253
|
+
group.types.push(type);
|
|
254
|
+
}
|
|
255
|
+
else
|
|
256
|
+
groups.set(key, { branch, types: [type] });
|
|
257
|
+
}
|
|
258
|
+
return [...groups.values()].map(({ branch, types }) => types.length === 1 ? branch : {
|
|
259
|
+
...branch,
|
|
260
|
+
properties: { ...branch.properties, type: { type: 'string', enum: types } },
|
|
261
|
+
});
|
|
262
|
+
}
|
|
239
263
|
/**
|
|
240
264
|
* The model's final reply: one or more chat messages, sent in order. Each message
|
|
241
265
|
* is an ordered segment array; the model decides how to split, like a person
|
|
242
266
|
* sending a few short messages. No text/mentions shorthand.
|
|
243
267
|
*/
|
|
244
|
-
export function buildAiOutboundJsonSchema(types = DEFAULT_AI_OUTBOUND_SEGMENT_TYPES, extensions = {}) {
|
|
268
|
+
export function buildAiOutboundJsonSchema(types = DEFAULT_AI_OUTBOUND_SEGMENT_TYPES, extensions = {}, reaction) {
|
|
245
269
|
const branches = outboundSegmentJsonSchema.anyOf.flatMap(branch => {
|
|
246
270
|
const type = branch.properties.type?.const;
|
|
247
271
|
if (type === undefined || type in aiExtensionBranches || !types.includes(type))
|
|
@@ -264,17 +288,23 @@ export function buildAiOutboundJsonSchema(types = DEFAULT_AI_OUTBOUND_SEGMENT_TY
|
|
|
264
288
|
properties: {
|
|
265
289
|
segments: {
|
|
266
290
|
type: 'array',
|
|
267
|
-
items: { anyOf: branches.map(requiredFieldsOnly) },
|
|
268
|
-
description: 'Ordered canonical segments of one chat message.',
|
|
291
|
+
items: { anyOf: mergeEquivalentBranches(branches.map(requiredFieldsOnly)) },
|
|
269
292
|
},
|
|
270
293
|
},
|
|
271
294
|
required: ['segments'],
|
|
272
295
|
additionalProperties: false,
|
|
273
296
|
},
|
|
274
|
-
description: 'Chat messages sent in order. Use an empty array only when no reply is needed.',
|
|
275
297
|
},
|
|
298
|
+
// Required with "" for none, like extension fields: strict structured outputs reject optional keys.
|
|
299
|
+
...(reaction ? {
|
|
300
|
+
reaction: {
|
|
301
|
+
type: 'string',
|
|
302
|
+
enum: ['', ...reaction.values.map(item => item.value)],
|
|
303
|
+
description: 'Reaction to put on the message being answered; "" for none.',
|
|
304
|
+
},
|
|
305
|
+
} : {}),
|
|
276
306
|
},
|
|
277
|
-
required: ['messages'],
|
|
307
|
+
required: reaction ? ['messages', 'reaction'] : ['messages'],
|
|
278
308
|
additionalProperties: false,
|
|
279
309
|
};
|
|
280
310
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Locale } from '@zhin.js/plugin-runtime';
|
|
1
2
|
import type { UserInteractionContent, UserInteractionRequest } from '@zhin.js/interaction';
|
|
2
3
|
import type { SendContent } from '../plugin-runtime/im/contracts.js';
|
|
3
4
|
import type { ButtonStyle } from './interactive-segments/types.js';
|
|
@@ -23,8 +24,8 @@ export type UserInteractionParseResult = Readonly<{
|
|
|
23
24
|
/** Fail early for interaction definitions that could never produce an unambiguous answer. */
|
|
24
25
|
export declare function assertUserInteractionRequest(request: UserInteractionRequest): void;
|
|
25
26
|
/** Project semantic content into canonical Markdown + keyboard segments. */
|
|
26
|
-
export declare function renderUserInteraction(view: UserInteractionView, actionPayload?: (value: string, index: number) => string): SendContent;
|
|
27
|
+
export declare function renderUserInteraction(view: UserInteractionView, actionPayload?: (value: string, index: number) => string, locale?: Locale): SendContent;
|
|
27
28
|
/** Derive presentation and controls from one discriminated request. */
|
|
28
|
-
export declare function projectUserInteraction(request: UserInteractionRequest, progress?: UserInteractionProgress): UserInteractionView;
|
|
29
|
+
export declare function projectUserInteraction(request: UserInteractionRequest, progress?: UserInteractionProgress, locale?: Locale): UserInteractionView;
|
|
29
30
|
/** Parse and validate one user reply without transport knowledge. */
|
|
30
|
-
export declare function parseUserInteractionAnswer(request: UserInteractionRequest, rawInput: string): UserInteractionParseResult;
|
|
31
|
+
export declare function parseUserInteractionAnswer(request: UserInteractionRequest, rawInput: string, locale?: Locale, action?: boolean): UserInteractionParseResult;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { interactionMessages as t } from './interaction-messages.js';
|
|
1
2
|
/** Fail early for interaction definitions that could never produce an unambiguous answer. */
|
|
2
3
|
export function assertUserInteractionRequest(request) {
|
|
3
4
|
if (!request.title.trim())
|
|
@@ -36,7 +37,7 @@ export function assertUserInteractionRequest(request) {
|
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
39
|
/** Project semantic content into canonical Markdown + keyboard segments. */
|
|
39
|
-
export function renderUserInteraction(view, actionPayload = (value) => value) {
|
|
40
|
+
export function renderUserInteraction(view, actionPayload = (value) => value, locale = 'zh-CN') {
|
|
40
41
|
const markdown = [
|
|
41
42
|
`### ${view.title}`,
|
|
42
43
|
view.description?.trim(),
|
|
@@ -60,7 +61,7 @@ export function renderUserInteraction(view, actionPayload = (value) => value) {
|
|
|
60
61
|
data: {
|
|
61
62
|
rows: chunk(buttons, 5),
|
|
62
63
|
fallback: {
|
|
63
|
-
hint: '
|
|
64
|
+
hint: t(locale, 'fallback'),
|
|
64
65
|
map: Object.fromEntries(buttons.map((button, index) => [String(index + 1), button.payload])),
|
|
65
66
|
},
|
|
66
67
|
},
|
|
@@ -68,15 +69,15 @@ export function renderUserInteraction(view, actionPayload = (value) => value) {
|
|
|
68
69
|
];
|
|
69
70
|
}
|
|
70
71
|
/** Derive presentation and controls from one discriminated request. */
|
|
71
|
-
export function projectUserInteraction(request, progress) {
|
|
72
|
-
const instruction = instructionFor(request);
|
|
72
|
+
export function projectUserInteraction(request, progress, locale = 'zh-CN') {
|
|
73
|
+
const instruction = instructionFor(request, locale);
|
|
73
74
|
const optionList = request.type === 'select' || request.type === 'multiselect'
|
|
74
75
|
? renderOptions(request.options)
|
|
75
76
|
: undefined;
|
|
76
77
|
const optionsHaveDescriptions = request.type === 'select' || request.type === 'multiselect'
|
|
77
78
|
? request.options.some((option) => !!option.description)
|
|
78
79
|
: false;
|
|
79
|
-
const actions = actionsFor(request);
|
|
80
|
+
const actions = actionsFor(request, locale);
|
|
80
81
|
const stepDescription = [
|
|
81
82
|
progress ? `**${progress.index}/${progress.total} · ${request.title}**` : undefined,
|
|
82
83
|
request.description,
|
|
@@ -92,58 +93,62 @@ export function projectUserInteraction(request, progress) {
|
|
|
92
93
|
});
|
|
93
94
|
}
|
|
94
95
|
/** Parse and validate one user reply without transport knowledge. */
|
|
95
|
-
export function parseUserInteractionAnswer(request, rawInput) {
|
|
96
|
+
export function parseUserInteractionAnswer(request, rawInput, locale = 'zh-CN', action = false) {
|
|
96
97
|
const raw = rawInput.trim();
|
|
97
98
|
switch (request.type) {
|
|
98
99
|
case 'text': {
|
|
99
100
|
if (raw.length < (request.minLength ?? 0))
|
|
100
|
-
return invalid(
|
|
101
|
+
return invalid(t(locale, 'minLength', { count: request.minLength }));
|
|
101
102
|
if (request.maxLength !== undefined && raw.length > request.maxLength) {
|
|
102
|
-
return invalid(
|
|
103
|
+
return invalid(t(locale, 'maxLength', { count: request.maxLength }));
|
|
103
104
|
}
|
|
104
105
|
if (request.pattern) {
|
|
105
106
|
request.pattern.lastIndex = 0;
|
|
106
107
|
if (!request.pattern.test(raw))
|
|
107
|
-
return invalid('
|
|
108
|
+
return invalid(t(locale, 'pattern'));
|
|
108
109
|
}
|
|
109
110
|
return valid(raw);
|
|
110
111
|
}
|
|
111
112
|
case 'number': {
|
|
112
113
|
const value = Number(raw);
|
|
113
114
|
if (!raw || !Number.isFinite(value))
|
|
114
|
-
return invalid('
|
|
115
|
+
return invalid(t(locale, 'number'));
|
|
115
116
|
if (request.integer && !Number.isInteger(value))
|
|
116
|
-
return invalid('
|
|
117
|
+
return invalid(t(locale, 'integer'));
|
|
117
118
|
if (request.min !== undefined && value < request.min)
|
|
118
|
-
return invalid(
|
|
119
|
+
return invalid(t(locale, 'min', { count: request.min }));
|
|
119
120
|
if (request.max !== undefined && value > request.max)
|
|
120
|
-
return invalid(
|
|
121
|
+
return invalid(t(locale, 'max', { count: request.max }));
|
|
121
122
|
return valid(value);
|
|
122
123
|
}
|
|
123
124
|
case 'confirm': {
|
|
124
|
-
const normalized = raw.
|
|
125
|
+
const normalized = raw.toLowerCase();
|
|
126
|
+
if (action && normalized === 'yes')
|
|
127
|
+
return valid(true);
|
|
128
|
+
if (action && normalized === 'no')
|
|
129
|
+
return valid(false);
|
|
125
130
|
const confirmLabel = request.confirmLabel?.trim().toLocaleLowerCase();
|
|
126
131
|
const cancelLabel = request.cancelLabel?.trim().toLocaleLowerCase();
|
|
127
|
-
if (CONFIRM_VALUES.has(normalized) || (confirmLabel && normalized === confirmLabel))
|
|
132
|
+
if (CONFIRM_VALUES[locale].has(normalized) || (confirmLabel && normalized === confirmLabel))
|
|
128
133
|
return valid(true);
|
|
129
|
-
if (CANCEL_VALUES.has(normalized) || (cancelLabel && normalized === cancelLabel))
|
|
134
|
+
if (CANCEL_VALUES[locale].has(normalized) || (cancelLabel && normalized === cancelLabel))
|
|
130
135
|
return valid(false);
|
|
131
|
-
return invalid('
|
|
136
|
+
return invalid(t(locale, 'confirmOrCancel'));
|
|
132
137
|
}
|
|
133
138
|
case 'select': {
|
|
134
139
|
const option = resolveOption(request.options, raw);
|
|
135
|
-
return option ? valid(option.value) : invalid('
|
|
140
|
+
return option ? valid(option.value) : invalid(t(locale, 'select'));
|
|
136
141
|
}
|
|
137
142
|
case 'multiselect': {
|
|
138
143
|
const separator = request.separator ?? ',';
|
|
139
144
|
const tokens = raw.split(separator).map((token) => token.trim()).filter(Boolean);
|
|
140
145
|
const selected = uniqueOptions(tokens.map((token) => resolveOption(request.options, token)));
|
|
141
146
|
if (selected.length !== tokens.length)
|
|
142
|
-
return invalid('
|
|
147
|
+
return invalid(t(locale, 'invalidSelection'));
|
|
143
148
|
if (selected.length < (request.minSelections ?? 0))
|
|
144
|
-
return invalid(
|
|
149
|
+
return invalid(t(locale, 'minSelections', { count: request.minSelections }));
|
|
145
150
|
if (request.maxSelections !== undefined && selected.length > request.maxSelections) {
|
|
146
|
-
return invalid(
|
|
151
|
+
return invalid(t(locale, 'maxSelections', { count: request.maxSelections }));
|
|
147
152
|
}
|
|
148
153
|
return valid(Object.freeze(selected.map((option) => option.value)));
|
|
149
154
|
}
|
|
@@ -151,28 +156,28 @@ export function parseUserInteractionAnswer(request, rawInput) {
|
|
|
151
156
|
const separator = request.separator ?? ',';
|
|
152
157
|
const tokens = raw.split(separator).map((token) => token.trim()).filter(Boolean);
|
|
153
158
|
if (tokens.length < (request.minItems ?? 0))
|
|
154
|
-
return invalid(
|
|
159
|
+
return invalid(t(locale, 'minItems', { count: request.minItems }));
|
|
155
160
|
if (request.maxItems !== undefined && tokens.length > request.maxItems)
|
|
156
|
-
return invalid(
|
|
161
|
+
return invalid(t(locale, 'maxItems', { count: request.maxItems }));
|
|
157
162
|
if (request.valueType === 'number') {
|
|
158
163
|
const values = tokens.map(Number);
|
|
159
|
-
return values.every(Number.isFinite) ? valid(Object.freeze(values)) : invalid('
|
|
164
|
+
return values.every(Number.isFinite) ? valid(Object.freeze(values)) : invalid(t(locale, 'invalidNumbers'));
|
|
160
165
|
}
|
|
161
166
|
if (request.valueType === 'boolean') {
|
|
162
|
-
const values = tokens.map(parseBoolean);
|
|
167
|
+
const values = tokens.map(value => parseBoolean(value, locale));
|
|
163
168
|
return values.every((value) => value !== undefined)
|
|
164
169
|
? valid(Object.freeze(values))
|
|
165
|
-
: invalid(
|
|
170
|
+
: invalid(t(locale, 'invalidBooleans'));
|
|
166
171
|
}
|
|
167
172
|
return valid(Object.freeze(tokens));
|
|
168
173
|
}
|
|
169
174
|
}
|
|
170
175
|
}
|
|
171
|
-
function actionsFor(request) {
|
|
176
|
+
function actionsFor(request, locale) {
|
|
172
177
|
if (request.type === 'confirm') {
|
|
173
178
|
return [
|
|
174
|
-
{ label: request.confirmLabel ?? '
|
|
175
|
-
{ label: request.cancelLabel ?? '
|
|
179
|
+
{ label: request.confirmLabel ?? t(locale, 'confirm'), value: 'yes', style: 'primary' },
|
|
180
|
+
{ label: request.cancelLabel ?? t(locale, 'cancel'), value: 'no', style: 'danger' },
|
|
176
181
|
];
|
|
177
182
|
}
|
|
178
183
|
if (request.type === 'select' && request.options.length <= 10) {
|
|
@@ -180,12 +185,12 @@ function actionsFor(request) {
|
|
|
180
185
|
}
|
|
181
186
|
return undefined;
|
|
182
187
|
}
|
|
183
|
-
function instructionFor(request) {
|
|
188
|
+
function instructionFor(request, locale) {
|
|
184
189
|
switch (request.type) {
|
|
185
|
-
case 'confirm': return '
|
|
186
|
-
case 'select': return '
|
|
187
|
-
case 'multiselect': return
|
|
188
|
-
case 'list': return
|
|
190
|
+
case 'confirm': return t(locale, 'confirmation');
|
|
191
|
+
case 'select': return t(locale, 'selection');
|
|
192
|
+
case 'multiselect': return t(locale, 'multiple', { separator: request.separator ?? ',' });
|
|
193
|
+
case 'list': return t(locale, 'list', { separator: request.separator ?? ',' });
|
|
189
194
|
default: return undefined;
|
|
190
195
|
}
|
|
191
196
|
}
|
|
@@ -212,11 +217,15 @@ function uniqueOptions(options) {
|
|
|
212
217
|
result.push(option);
|
|
213
218
|
return result;
|
|
214
219
|
}
|
|
215
|
-
function parseBoolean(raw) {
|
|
216
|
-
const normalized = raw.
|
|
217
|
-
if (
|
|
220
|
+
function parseBoolean(raw, locale) {
|
|
221
|
+
const normalized = raw.toLowerCase();
|
|
222
|
+
if (normalized === 'true')
|
|
223
|
+
return true;
|
|
224
|
+
if (normalized === 'false')
|
|
225
|
+
return false;
|
|
226
|
+
if (CONFIRM_VALUES[locale].has(normalized))
|
|
218
227
|
return true;
|
|
219
|
-
if (CANCEL_VALUES.has(normalized))
|
|
228
|
+
if (CANCEL_VALUES[locale].has(normalized))
|
|
220
229
|
return false;
|
|
221
230
|
return undefined;
|
|
222
231
|
}
|
|
@@ -239,8 +248,8 @@ function validBounds(minimum, maximum) {
|
|
|
239
248
|
&& (maximum === undefined || Number.isFinite(maximum))
|
|
240
249
|
&& (minimum === undefined || maximum === undefined || minimum <= maximum);
|
|
241
250
|
}
|
|
242
|
-
const CONFIRM_VALUES = new Set(['1', '
|
|
243
|
-
const CANCEL_VALUES = new Set(['2', '
|
|
251
|
+
const CONFIRM_VALUES = { 'zh-CN': new Set(['1', '是', '确认', '同意']), en: new Set(['1', 'y', 'yes', 'true', 'confirm', 'agree']) };
|
|
252
|
+
const CANCEL_VALUES = { 'zh-CN': new Set(['2', '否', '取消', '拒绝']), en: new Set(['2', 'n', 'no', 'false', 'cancel', 'reject']) };
|
|
244
253
|
function chunk(items, size) {
|
|
245
254
|
const rows = [];
|
|
246
255
|
for (let index = 0; index < items.length; index += size)
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Command authoring and Root-selected IM presentation contracts.
|
|
3
|
+
* @module zhin.js/command
|
|
4
|
+
*/
|
|
5
|
+
export { defineCommand, commandFeedbackToken, commandMenuPresentationToken, type CommandContext, type CommandConversation, type CommandDefiner, type CommandDefinition, type CommandMessage, type CommandParameterDefinition, type CommandParamSchema, type CommandScene, type CommandSegment, type CommandSender, type CommandSession, type CommandDynamicValue, type CommandParameterType, type CommandParameterValue, type CommandExecutionPolicy, type CommandExecutionLimit, type CommandParams, type CommandOptions, type CommandOptionSchema, type InferCommandParams, type InferCommandOptions, type CommandFeedback, type CommandFeedbackContext, type CommandFeedbackRenderer, type CommandMenuPresentationInput, type CommandMenuPresenter, type CommandMenuPage, type CommandMenuNavigation, type CommandMenuEntry, type CommandMenuAppearance, type CommandValidationIssue, } from './command.js';
|
package/lib/feature/command.d.ts
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
1
|
/** Authoring API for Command Feature — implementation in `@zhin.js/command`. */
|
|
2
2
|
export * from '@zhin.js/command';
|
|
3
|
+
export { commandFeedbackToken } from '../plugin-runtime/im/command-feedback.js';
|
|
4
|
+
export type { CommandFeedback, CommandFeedbackContext, CommandFeedbackRenderer } from '../plugin-runtime/im/command-feedback.js';
|
|
5
|
+
export { commandMenuPresentationToken } from '../plugin-runtime/im/command-menu-presentation.js';
|
|
6
|
+
export type { CommandMenuPresentationInput, CommandMenuPresenter } from '../plugin-runtime/im/command-menu-presentation.js';
|
package/lib/feature/command.js
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
/** Authoring API for Command Feature — implementation in `@zhin.js/command`. */
|
|
2
2
|
export * from '@zhin.js/command';
|
|
3
|
+
export { commandFeedbackToken } from '../plugin-runtime/im/command-feedback.js';
|
|
4
|
+
export { commandMenuPresentationToken } from '../plugin-runtime/im/command-menu-presentation.js';
|
|
@@ -3,3 +3,4 @@
|
|
|
3
3
|
* @module zhin.js/middleware
|
|
4
4
|
*/
|
|
5
5
|
export { defineMiddleware, type MiddlewareContext, type MiddlewareContinuation, type MiddlewareDefinition, type MiddlewareDefinitionBase, type MiddlewareNext, type MiddlewarePhase, type MiddlewareTarget, type OutboundMiddlewareNext, } from './middleware.js';
|
|
6
|
+
export { outboundPresentationToken, type OutboundPresentation, type OutboundPresentationContext, type OutboundPresentationMode, type OutboundPresentationPreferences, } from './middleware.js';
|
|
@@ -18,3 +18,5 @@ export declare function defineMiddleware<TInput = Message, TConfig = unknown>(de
|
|
|
18
18
|
}>): Readonly<Extract<FeatureMiddlewareDefinition<TInput, TConfig>, {
|
|
19
19
|
readonly target: 'inbound';
|
|
20
20
|
}>>;
|
|
21
|
+
export { outboundPresentationToken } from '../plugin-runtime/im/outbound-presentation.js';
|
|
22
|
+
export type { OutboundPresentation, OutboundPresentationContext, OutboundPresentationMode, OutboundPresentationPreferences, } from '../plugin-runtime/im/outbound-presentation.js';
|
package/lib/notice.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export interface IncomingNotice extends IncomingSideEvent {
|
|
|
12
12
|
readonly durationSeconds?: number;
|
|
13
13
|
readonly role?: string;
|
|
14
14
|
readonly enabled?: boolean;
|
|
15
|
+
/** Short new value of a card/title/remark change or a notice body (adapter-truncated, plain text). */
|
|
16
|
+
readonly value?: string;
|
|
15
17
|
}
|
|
16
18
|
export interface NoticeBase extends SideEventBase {
|
|
17
19
|
readonly type: 'notice';
|
|
@@ -22,6 +24,7 @@ export interface NoticeBase extends SideEventBase {
|
|
|
22
24
|
readonly durationSeconds?: number;
|
|
23
25
|
readonly role?: string;
|
|
24
26
|
readonly enabled?: boolean;
|
|
27
|
+
readonly value?: string;
|
|
25
28
|
}
|
|
26
29
|
export type Notice<T extends object = {}> = NoticeBase & T;
|
|
27
30
|
/** @internal Generation-scoped implementation of the public Notice contract. */
|
|
@@ -34,6 +37,7 @@ export declare class RuntimeNotice extends RuntimeSideEvent implements NoticeBas
|
|
|
34
37
|
readonly durationSeconds?: number;
|
|
35
38
|
readonly role?: string;
|
|
36
39
|
readonly enabled?: boolean;
|
|
40
|
+
readonly value?: string;
|
|
37
41
|
constructor(input: IncomingNotice, context: EndpointEventContext);
|
|
38
42
|
}
|
|
39
43
|
export declare const Notice: typeof RuntimeNotice;
|
package/lib/notice.js
CHANGED
|
@@ -8,6 +8,7 @@ export class RuntimeNotice extends RuntimeSideEvent {
|
|
|
8
8
|
durationSeconds;
|
|
9
9
|
role;
|
|
10
10
|
enabled;
|
|
11
|
+
value;
|
|
11
12
|
constructor(input, context) {
|
|
12
13
|
super(input, context);
|
|
13
14
|
this.messageId = input.messageId;
|
|
@@ -16,6 +17,7 @@ export class RuntimeNotice extends RuntimeSideEvent {
|
|
|
16
17
|
this.durationSeconds = input.durationSeconds;
|
|
17
18
|
this.role = input.role;
|
|
18
19
|
this.enabled = input.enabled;
|
|
20
|
+
this.value = input.value;
|
|
19
21
|
Object.freeze(this);
|
|
20
22
|
}
|
|
21
23
|
}
|