@zhin.js/core 1.1.56 → 1.1.58
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/generic-segment-mapper.js +2 -0
- package/lib/built/html-to-text.js +2 -2
- package/lib/built/rich-segments/markdown-to-text.js +19 -0
- package/lib/built/segment-contract/validate.d.ts +1442 -300
- package/lib/built/user-interaction.d.ts +1 -1
- package/lib/built/user-interaction.js +3 -1
- package/lib/feature/command.d.ts +2 -4
- package/lib/feature/command.js +1 -2
- package/lib/feature/middleware.d.ts +2 -2
- package/lib/feature/middleware.js +1 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -0
- package/lib/plugin-runtime/im/command-feedback.d.ts +20 -1
- package/lib/plugin-runtime/im/command-feedback.js +63 -0
- package/lib/plugin-runtime/im/contracts.d.ts +60 -5
- package/lib/plugin-runtime/im/contracts.js +6 -2
- package/lib/plugin-runtime/im/conversation-runtime.d.ts +2 -2
- package/lib/plugin-runtime/im/conversation-runtime.js +6 -4
- package/lib/plugin-runtime/im/endpoint-runtime.d.ts +2 -3
- package/lib/plugin-runtime/im/endpoint-runtime.js +2 -2
- package/lib/plugin-runtime/im/host/component-host-installer.d.ts +2 -0
- package/lib/plugin-runtime/im/host/component-host-installer.js +14 -0
- package/lib/plugin-runtime/im/host/conversation-migration.d.ts +5 -0
- package/lib/plugin-runtime/im/host/conversation-migration.js +75 -0
- package/lib/plugin-runtime/im/host/database-config.d.ts +10 -0
- package/lib/plugin-runtime/im/host/database-config.js +114 -0
- package/lib/plugin-runtime/im/host/database-host-installer.d.ts +13 -0
- package/lib/plugin-runtime/im/host/database-host-installer.js +389 -0
- package/lib/plugin-runtime/im/host/database-state.d.ts +9 -0
- package/lib/plugin-runtime/im/host/database-state.js +91 -0
- package/lib/plugin-runtime/im/host/index.d.ts +8 -0
- package/lib/plugin-runtime/im/host/index.js +8 -0
- package/lib/plugin-runtime/im/host/locale-host-installer.d.ts +3 -0
- package/lib/plugin-runtime/im/host/locale-host-installer.js +60 -0
- package/lib/plugin-runtime/im/host/outbound-host-installer.d.ts +8 -0
- package/lib/plugin-runtime/im/host/outbound-host-installer.js +153 -0
- package/lib/plugin-runtime/im/host/sender-enrichment.d.ts +33 -0
- package/lib/plugin-runtime/im/host/sender-enrichment.js +215 -0
- package/lib/plugin-runtime/im/im-runtime.d.ts +7 -3
- package/lib/plugin-runtime/im/im-runtime.js +9 -7
- package/lib/plugin-runtime/im/inbound-dedup.d.ts +33 -0
- package/lib/plugin-runtime/im/inbound-dedup.js +139 -1
- package/lib/plugin-runtime/im/inbound-origin.d.ts +51 -2
- package/lib/plugin-runtime/im/inbound-origin.js +108 -1
- package/lib/plugin-runtime/im/inbound-runtime.d.ts +10 -13
- package/lib/plugin-runtime/im/inbound-runtime.js +88 -349
- package/lib/plugin-runtime/im/inbound-spine.d.ts +194 -0
- package/lib/plugin-runtime/im/inbound-spine.js +333 -0
- package/lib/plugin-runtime/im/index.d.ts +5 -7
- package/lib/plugin-runtime/im/index.js +5 -6
- package/lib/plugin-runtime/im/interaction-runtime.d.ts +8 -10
- package/lib/plugin-runtime/im/interaction-runtime.js +86 -28
- package/lib/plugin-runtime/im/interactive-router.d.ts +55 -8
- package/lib/plugin-runtime/im/interactive-router.js +244 -35
- package/lib/plugin-runtime/im/message-dispatcher.d.ts +1 -1
- package/lib/plugin-runtime/im/message-dispatcher.js +58 -7
- package/lib/plugin-runtime/im/message-events.d.ts +33 -3
- package/lib/plugin-runtime/im/message-events.js +34 -0
- package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +4 -4
- package/lib/plugin-runtime/im/outbound-delivery-runtime.js +75 -29
- package/lib/plugin-runtime/im/outbound-renderer.d.ts +32 -2
- package/lib/plugin-runtime/im/outbound-renderer.js +95 -4
- package/lib/runtime.d.ts +2 -0
- package/lib/runtime.js +2 -0
- package/lib/venue/contracts.d.ts +21 -0
- package/lib/venue/contracts.js +3 -0
- package/lib/venue/floor-controller.d.ts +11 -0
- package/lib/venue/floor-controller.js +63 -0
- package/lib/venue/index.d.ts +13 -0
- package/lib/venue/index.js +11 -0
- package/lib/venue/membership-port.d.ts +23 -0
- package/lib/venue/membership-port.js +141 -0
- package/lib/venue/projector.d.ts +38 -0
- package/lib/venue/projector.js +45 -0
- package/lib/venue/registry.d.ts +16 -0
- package/lib/venue/registry.js +63 -0
- package/lib/venue/router.d.ts +19 -0
- package/lib/venue/router.js +101 -0
- package/lib/venue/send-limiter.d.ts +17 -0
- package/lib/venue/send-limiter.js +73 -0
- package/lib/venue/validator.d.ts +6 -0
- package/lib/venue/validator.js +160 -0
- package/package.json +16 -17
- package/lib/plugin-runtime/im/captured-endpoint-operation.d.ts +0 -34
- package/lib/plugin-runtime/im/captured-endpoint-operation.js +0 -1
- package/lib/plugin-runtime/im/command-menu-navigation.d.ts +0 -13
- package/lib/plugin-runtime/im/command-menu-navigation.js +0 -43
- package/lib/plugin-runtime/im/command-menu-presentation.d.ts +0 -14
- package/lib/plugin-runtime/im/command-menu-presentation.js +0 -25
- package/lib/plugin-runtime/im/group-rule-violation.d.ts +0 -52
- package/lib/plugin-runtime/im/group-rule-violation.js +0 -10
- package/lib/plugin-runtime/im/ingress-route.d.ts +0 -10
- package/lib/plugin-runtime/im/ingress-route.js +0 -10
- package/lib/plugin-runtime/im/interaction-operation.d.ts +0 -12
- package/lib/plugin-runtime/im/interaction-operation.js +0 -2
- package/lib/plugin-runtime/im/jsx-button-registry.d.ts +0 -17
- package/lib/plugin-runtime/im/jsx-button-registry.js +0 -98
- package/lib/plugin-runtime/im/locale.d.ts +0 -5
- package/lib/plugin-runtime/im/locale.js +0 -12
- package/lib/plugin-runtime/im/login-assist-host.d.ts +0 -6
- package/lib/plugin-runtime/im/login-assist-host.js +0 -6
- package/lib/plugin-runtime/im/member-welcome.d.ts +0 -8
- package/lib/plugin-runtime/im/member-welcome.js +0 -11
- package/lib/plugin-runtime/im/message-bus.d.ts +0 -38
- package/lib/plugin-runtime/im/message-bus.js +0 -40
- package/lib/plugin-runtime/im/operation-endpoint.d.ts +0 -6
- package/lib/plugin-runtime/im/operation-endpoint.js +0 -2
- package/lib/plugin-runtime/im/operation-resources.d.ts +0 -15
- package/lib/plugin-runtime/im/operation-resources.js +0 -2
- package/lib/plugin-runtime/im/outbound-ledger.d.ts +0 -24
- package/lib/plugin-runtime/im/outbound-ledger.js +0 -114
- package/lib/plugin-runtime/im/outbound-presentation.d.ts +0 -19
- package/lib/plugin-runtime/im/outbound-presentation.js +0 -41
- package/lib/plugin-runtime/im/outbound-projection.d.ts +0 -17
- package/lib/plugin-runtime/im/outbound-projection.js +0 -55
- package/lib/plugin-runtime/im/runtime-middleware.d.ts +0 -5
- package/lib/plugin-runtime/im/runtime-middleware.js +0 -18
|
@@ -1,12 +1,55 @@
|
|
|
1
1
|
import { type CapabilityId } from '@zhin.js/plugin-runtime';
|
|
2
|
-
import type { ConversationRef } from '@zhin.js/im-contract';
|
|
2
|
+
import type { ConversationRef, DeliveryReceipt } from '@zhin.js/im-contract';
|
|
3
3
|
import type { PeerRegistry } from '@zhin.js/adapter';
|
|
4
|
-
|
|
4
|
+
/**
|
|
5
|
+
* scope 键不含 endpoint id:同一个群里不同 Bot 的 conversationRefKey 不同,
|
|
6
|
+
* 但它们互相的回显仍属同一会话。
|
|
7
|
+
*/
|
|
8
|
+
export declare function ledgerScopeOf(conversation: ConversationRef): string;
|
|
9
|
+
export declare function digestText(text: string): string | undefined;
|
|
10
|
+
export interface LedgerMatch {
|
|
11
|
+
readonly endpointId: string;
|
|
12
|
+
}
|
|
13
|
+
export declare class OutboundLedger {
|
|
14
|
+
#private;
|
|
15
|
+
private readonly now;
|
|
16
|
+
constructor(now?: () => number);
|
|
17
|
+
get size(): number;
|
|
18
|
+
/** 仅在 `receipt.status === 'sent'` 时记录;`payload` 应为最终投影后的 payload。 */
|
|
19
|
+
record(conversation: ConversationRef, receipt: DeliveryReceipt, payload: unknown): void;
|
|
20
|
+
matchById(scope: string, messageId: unknown): LedgerMatch | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* 摘要兜底(“账号 id 对不上”时):30 秒窗口、一次性。
|
|
23
|
+
* 权衡:人类在窗口内原样复读 Bot 刚发的文本,会被当作回显丢弃一次。
|
|
24
|
+
*/
|
|
25
|
+
matchByDigest(scope: string, text: string, now?: number): LedgerMatch | undefined;
|
|
26
|
+
}
|
|
5
27
|
/** 入站消息来源。仅 `human` 能触发 agent turn;其余只记录。 */
|
|
6
28
|
export type MessageOrigin = 'self' | 'member' | 'peer_bot' | 'human';
|
|
7
29
|
/** @internal */
|
|
8
30
|
export declare const MESSAGE_ORIGINS: readonly MessageOrigin[];
|
|
9
31
|
export declare function isMessageOrigin(value: unknown): value is MessageOrigin;
|
|
32
|
+
export interface RoomClaimInput {
|
|
33
|
+
readonly platform: string;
|
|
34
|
+
readonly sceneType?: string;
|
|
35
|
+
readonly sceneId: string;
|
|
36
|
+
readonly threadId?: string;
|
|
37
|
+
}
|
|
38
|
+
export interface RoomArbitrationInput extends RoomClaimInput {
|
|
39
|
+
readonly endpointId: string;
|
|
40
|
+
readonly rawText?: string;
|
|
41
|
+
readonly mentions?: readonly string[];
|
|
42
|
+
readonly replyToSenderId?: string;
|
|
43
|
+
}
|
|
44
|
+
export interface ResolvedRoomInfo {
|
|
45
|
+
readonly roomId: string;
|
|
46
|
+
readonly defaultEndpointId: string;
|
|
47
|
+
readonly memberEndpoints: readonly string[];
|
|
48
|
+
}
|
|
49
|
+
export interface RoomReadinessResult {
|
|
50
|
+
readonly ready: boolean;
|
|
51
|
+
readonly unreadyEndpoints?: readonly string[];
|
|
52
|
+
}
|
|
10
53
|
/**
|
|
11
54
|
* Room 成员端口。由 Room 激活(后续条目,workroom)通过 root resources 提供;
|
|
12
55
|
* 未提供时没有任何 member:Registry 命中的账号全部是 `peer_bot`。
|
|
@@ -41,6 +84,12 @@ export interface RoomMembershipPort {
|
|
|
41
84
|
* `fallbackEndpointIds` 中的成员在宽限期后按优先级接管。决策按消息缓存,每条消息只决定一次。
|
|
42
85
|
*/
|
|
43
86
|
route?(input: RoomInboundRouteInput): RoomRouteDecision | undefined;
|
|
87
|
+
/** Resolve a conversation to its Venue / Room definition summary. */
|
|
88
|
+
resolveRoom?(input: RoomClaimInput): ResolvedRoomInfo | undefined;
|
|
89
|
+
/** Check whether an endpoint in a Venue / Room is directly addressed by @mention or reply. */
|
|
90
|
+
isAddressedEndpoint?(input: RoomArbitrationInput): boolean;
|
|
91
|
+
/** Fail-closed readiness check for Venue member endpoints (ADR 0048 §3.4 invariant 7). */
|
|
92
|
+
isRoomReady?(input: RoomClaimInput): RoomReadinessResult;
|
|
44
93
|
}
|
|
45
94
|
/** core 用可信数据构造的路由输入;不信任正文。 */
|
|
46
95
|
export interface RoomInboundRouteInput {
|
|
@@ -1,6 +1,113 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
1
2
|
import { createToken } from '@zhin.js/plugin-runtime';
|
|
2
3
|
import { getLogger } from '@zhin.js/logger';
|
|
3
|
-
import {
|
|
4
|
+
import { segmentsToPlainText } from '../../built/segment-contract/text.js';
|
|
5
|
+
const MAX_ENTRIES = 2000;
|
|
6
|
+
const TTL_MS = 10 * 60_000;
|
|
7
|
+
const DIGEST_WINDOW_MS = 30_000;
|
|
8
|
+
const MIN_DIGEST_TEXT = 4;
|
|
9
|
+
/**
|
|
10
|
+
* scope 键不含 endpoint id:同一个群里不同 Bot 的 conversationRefKey 不同,
|
|
11
|
+
* 但它们互相的回显仍属同一会话。
|
|
12
|
+
*/
|
|
13
|
+
export function ledgerScopeOf(conversation) {
|
|
14
|
+
return [
|
|
15
|
+
conversation.endpoint.adapter,
|
|
16
|
+
conversation.kind,
|
|
17
|
+
conversation.id,
|
|
18
|
+
conversation.parent?.kind ?? '',
|
|
19
|
+
conversation.parent?.id ?? '',
|
|
20
|
+
conversation.threadId ?? '',
|
|
21
|
+
].join('\0');
|
|
22
|
+
}
|
|
23
|
+
export function digestText(text) {
|
|
24
|
+
const normalized = text.replace(/\s+/g, ' ').trim();
|
|
25
|
+
if (normalized.length < MIN_DIGEST_TEXT)
|
|
26
|
+
return undefined;
|
|
27
|
+
return createHash('sha256').update(normalized).digest('hex').slice(0, 16);
|
|
28
|
+
}
|
|
29
|
+
function payloadText(payload) {
|
|
30
|
+
if (typeof payload === 'string')
|
|
31
|
+
return payload;
|
|
32
|
+
if (Array.isArray(payload)) {
|
|
33
|
+
return segmentsToPlainText(payload.filter((item) => !!item && typeof item === 'object' && typeof item.type === 'string'));
|
|
34
|
+
}
|
|
35
|
+
return '';
|
|
36
|
+
}
|
|
37
|
+
export class OutboundLedger {
|
|
38
|
+
now;
|
|
39
|
+
#entries = [];
|
|
40
|
+
constructor(now = Date.now) {
|
|
41
|
+
this.now = now;
|
|
42
|
+
}
|
|
43
|
+
get size() {
|
|
44
|
+
return this.#entries.length;
|
|
45
|
+
}
|
|
46
|
+
/** 仅在 `receipt.status === 'sent'` 时记录;`payload` 应为最终投影后的 payload。 */
|
|
47
|
+
record(conversation, receipt, payload) {
|
|
48
|
+
if (receipt.status !== 'sent')
|
|
49
|
+
return;
|
|
50
|
+
const at = this.now();
|
|
51
|
+
this.#prune(at);
|
|
52
|
+
const rawId = receipt.message?.id;
|
|
53
|
+
const messageId = rawId === undefined || rawId === null || String(rawId) === '' ? undefined : String(rawId);
|
|
54
|
+
const digest = digestText(payloadText(payload));
|
|
55
|
+
if (messageId === undefined && digest === undefined)
|
|
56
|
+
return;
|
|
57
|
+
this.#entries.push({
|
|
58
|
+
scope: ledgerScopeOf(conversation),
|
|
59
|
+
endpointId: conversation.endpoint.id,
|
|
60
|
+
...(messageId !== undefined ? { messageId } : {}),
|
|
61
|
+
...(digest !== undefined ? { digest } : {}),
|
|
62
|
+
at,
|
|
63
|
+
digestConsumed: false,
|
|
64
|
+
});
|
|
65
|
+
while (this.#entries.length > MAX_ENTRIES)
|
|
66
|
+
this.#entries.shift();
|
|
67
|
+
}
|
|
68
|
+
matchById(scope, messageId) {
|
|
69
|
+
if (typeof messageId !== 'string' && typeof messageId !== 'number')
|
|
70
|
+
return undefined;
|
|
71
|
+
const id = String(messageId);
|
|
72
|
+
if (id === '')
|
|
73
|
+
return undefined;
|
|
74
|
+
const at = this.now();
|
|
75
|
+
for (let i = this.#entries.length - 1; i >= 0; i--) {
|
|
76
|
+
const entry = this.#entries[i];
|
|
77
|
+
if (at - entry.at > TTL_MS)
|
|
78
|
+
continue;
|
|
79
|
+
if (entry.scope === scope && entry.messageId === id)
|
|
80
|
+
return { endpointId: entry.endpointId };
|
|
81
|
+
}
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* 摘要兜底(“账号 id 对不上”时):30 秒窗口、一次性。
|
|
86
|
+
* 权衡:人类在窗口内原样复读 Bot 刚发的文本,会被当作回显丢弃一次。
|
|
87
|
+
*/
|
|
88
|
+
matchByDigest(scope, text, now = this.now()) {
|
|
89
|
+
const digest = digestText(typeof text === 'string' ? text : '');
|
|
90
|
+
if (digest === undefined)
|
|
91
|
+
return undefined;
|
|
92
|
+
for (let i = this.#entries.length - 1; i >= 0; i--) {
|
|
93
|
+
const entry = this.#entries[i];
|
|
94
|
+
if (now - entry.at > DIGEST_WINDOW_MS)
|
|
95
|
+
continue;
|
|
96
|
+
if (entry.digestConsumed || entry.scope !== scope || entry.digest !== digest)
|
|
97
|
+
continue;
|
|
98
|
+
entry.digestConsumed = true;
|
|
99
|
+
return { endpointId: entry.endpointId };
|
|
100
|
+
}
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
#prune(at) {
|
|
104
|
+
let drop = 0;
|
|
105
|
+
while (drop < this.#entries.length && at - this.#entries[drop].at > TTL_MS)
|
|
106
|
+
drop++;
|
|
107
|
+
if (drop > 0)
|
|
108
|
+
this.#entries.splice(0, drop);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
4
111
|
const logger = getLogger('im.inbound.origin');
|
|
5
112
|
/** @internal */
|
|
6
113
|
export const MESSAGE_ORIGINS = Object.freeze(['self', 'member', 'peer_bot', 'human']);
|
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
import type { CaptureEndpointOperationInput, CapturedEndpointOperation } from './captured-endpoint-operation.js';
|
|
2
|
-
import type { GenerationAdmissionGate, PluginId, RuntimeSnapshot, SnapshotLease } from '@zhin.js/plugin-runtime';
|
|
3
1
|
import type { EndpointEvent, PeerRegistry } from '@zhin.js/adapter';
|
|
4
|
-
import { type OperationResourceView } from '@zhin.js/feature-kit';
|
|
5
|
-
import type { DeliveryReceipt } from '@zhin.js/im-contract';
|
|
6
|
-
import type { UserInteraction } from '@zhin.js/interaction';
|
|
7
2
|
import type { CommandExecutionBudget, CommandOperationController } from '@zhin.js/command/internal/execution';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
3
|
+
import type { OperationResourceView } from '@zhin.js/feature-kit';
|
|
4
|
+
import type { DeliveryReceipt, UserInteraction } from '@zhin.js/im-contract';
|
|
5
|
+
import type { GenerationAdmissionGate, PluginId, RuntimeSnapshot, SnapshotLease } from '@zhin.js/plugin-runtime';
|
|
6
|
+
import type { Notice } from '../../notice.js';
|
|
7
|
+
import type { Request } from '../../request.js';
|
|
8
|
+
import { RuntimeMessage, type CaptureEndpointOperationInput, type CapturedEndpointOperation, type IncomingMessage, type Message, type MessageSenderRef, type SendRequest } from './contracts.js';
|
|
9
|
+
import type { InboundDeduper } from './inbound-dedup.js';
|
|
10
|
+
import type { InboundOriginClassifier } from './inbound-origin.js';
|
|
11
|
+
import { type InboundInteractionPort } from './inbound-spine.js';
|
|
11
12
|
import type { RuntimeMessageDiagnostics } from './message-diagnostics.js';
|
|
12
|
-
import type { RuntimeInteractionCoordinator } from './interaction-runtime.js';
|
|
13
13
|
import { type RuntimeMessageEvent } from './message-events.js';
|
|
14
|
-
import type { InboundDeduper } from './inbound-dedup.js';
|
|
15
|
-
import { type InboundOriginClassifier } from './inbound-origin.js';
|
|
16
|
-
type InboundInteractionPort = Pick<RuntimeInteractionCoordinator<GenerationAdmissionGate>, 'create' | 'createForMessage' | 'createFromUnknown' | 'dispatch' | 'resolveClaim'> & Partial<Pick<RuntimeInteractionCoordinator<GenerationAdmissionGate>, 'hasPendingAnswer'>>;
|
|
17
14
|
interface InboundRuntimeContext {
|
|
18
15
|
readonly commandExecution?: CommandExecutionBudget;
|
|
19
16
|
runCommandOperation?<T>(controller: CommandOperationController, operation: () => T): T;
|
|
@@ -22,7 +19,7 @@ interface InboundRuntimeContext {
|
|
|
22
19
|
captureOperation(input: CaptureEndpointOperationInput): CapturedEndpointOperation;
|
|
23
20
|
createOperationResources?(input: Readonly<{
|
|
24
21
|
lease: SnapshotLease;
|
|
25
|
-
message:
|
|
22
|
+
message: RuntimeMessage;
|
|
26
23
|
owner: PluginId;
|
|
27
24
|
signal: AbortSignal;
|
|
28
25
|
}>): OperationResourceView | undefined;
|