@zhin.js/core 1.1.38 → 1.1.40
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/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/endpoint-runtime.d.ts +6 -1
- package/lib/plugin-runtime/im/endpoint-runtime.js +27 -5
- 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/message-dispatcher.js +28 -4
- package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +1 -1
- package/lib/plugin-runtime/im/outbound-delivery-runtime.js +54 -12
- package/lib/plugin-runtime/im/outbound-renderer.js +1 -1
- package/lib/plugin-runtime/im/outbound-segments.d.ts +2 -0
- package/lib/plugin-runtime/im/outbound-segments.js +8 -0
- package/lib/plugin-runtime/im/public-api.d.ts +1 -1
- package/lib/plugin-runtime/im/runtime-middleware.js +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 +10 -10
package/lib/notice.d.ts
CHANGED
|
@@ -1,37 +1,39 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type
|
|
1
|
+
import type { ComposedNoticeName } from './side-event/types.js';
|
|
2
|
+
import { RuntimeSideEvent, type IncomingSideEvent, type SideEventBase, type EndpointEventContext } from './side-event/base.js';
|
|
3
3
|
export type { NoticeKind, ComposedNoticeName } from './side-event/types.js';
|
|
4
|
-
export type { SideEventBase } from './side-event/base.js';
|
|
5
|
-
export { composeSideEventName, formatSideEventName, matchesSideEventName, parseSideEventName,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
* $scene_id: groupId,
|
|
17
|
-
* $scene_type: 'group',
|
|
18
|
-
* $sub_type: 'member_decrease',
|
|
19
|
-
* $actor: { id: operatorId, name: '管理员' },
|
|
20
|
-
* $target: { id: userId, name: '被踢者' },
|
|
21
|
-
* $timestamp: Date.now(),
|
|
22
|
-
* });
|
|
23
|
-
* // formatSideEventName(notice) === 'notice.group.member_decrease'
|
|
24
|
-
* this.adapter.emit('notice.receive', notice);
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
4
|
+
export type { SideEventBase, IncomingSideEvent } from './side-event/base.js';
|
|
5
|
+
export { composeSideEventName, formatSideEventName, matchesSideEventName, parseSideEventName, sideEventConversation, sideEventSendChannel } from './side-event/base.js';
|
|
6
|
+
export interface IncomingNotice extends IncomingSideEvent {
|
|
7
|
+
readonly type: 'notice';
|
|
8
|
+
readonly name: ComposedNoticeName;
|
|
9
|
+
readonly messageId?: string;
|
|
10
|
+
readonly reaction?: string;
|
|
11
|
+
readonly operation?: 'added' | 'removed';
|
|
12
|
+
readonly durationSeconds?: number;
|
|
13
|
+
readonly role?: string;
|
|
14
|
+
readonly enabled?: boolean;
|
|
15
|
+
}
|
|
27
16
|
export interface NoticeBase extends SideEventBase {
|
|
28
|
-
|
|
29
|
-
|
|
17
|
+
readonly type: 'notice';
|
|
18
|
+
readonly name: ComposedNoticeName;
|
|
19
|
+
readonly messageId?: string;
|
|
20
|
+
readonly reaction?: string;
|
|
21
|
+
readonly operation?: 'added' | 'removed';
|
|
22
|
+
readonly durationSeconds?: number;
|
|
23
|
+
readonly role?: string;
|
|
24
|
+
readonly enabled?: boolean;
|
|
30
25
|
}
|
|
31
|
-
/**
|
|
32
|
-
* 完整通知类型,支持平台原始数据扩展
|
|
33
|
-
*/
|
|
34
26
|
export type Notice<T extends object = {}> = NoticeBase & T;
|
|
35
|
-
|
|
36
|
-
|
|
27
|
+
/** @internal Generation-scoped implementation of the public Notice contract. */
|
|
28
|
+
export declare class RuntimeNotice extends RuntimeSideEvent implements NoticeBase {
|
|
29
|
+
readonly type: 'notice';
|
|
30
|
+
readonly name: ComposedNoticeName;
|
|
31
|
+
readonly messageId?: string;
|
|
32
|
+
readonly reaction?: string;
|
|
33
|
+
readonly operation?: 'added' | 'removed';
|
|
34
|
+
readonly durationSeconds?: number;
|
|
35
|
+
readonly role?: string;
|
|
36
|
+
readonly enabled?: boolean;
|
|
37
|
+
constructor(input: IncomingNotice, context: EndpointEventContext);
|
|
37
38
|
}
|
|
39
|
+
export declare const Notice: typeof RuntimeNotice;
|
package/lib/notice.js
CHANGED
|
@@ -1,8 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { RuntimeSideEvent } from './side-event/base.js';
|
|
2
|
+
export { composeSideEventName, formatSideEventName, matchesSideEventName, parseSideEventName, sideEventConversation, sideEventSendChannel } from './side-event/base.js';
|
|
3
|
+
/** @internal Generation-scoped implementation of the public Notice contract. */
|
|
4
|
+
export class RuntimeNotice extends RuntimeSideEvent {
|
|
5
|
+
messageId;
|
|
6
|
+
reaction;
|
|
7
|
+
operation;
|
|
8
|
+
durationSeconds;
|
|
9
|
+
role;
|
|
10
|
+
enabled;
|
|
11
|
+
constructor(input, context) {
|
|
12
|
+
super(input, context);
|
|
13
|
+
this.messageId = input.messageId;
|
|
14
|
+
this.reaction = input.reaction;
|
|
15
|
+
this.operation = input.operation;
|
|
16
|
+
this.durationSeconds = input.durationSeconds;
|
|
17
|
+
this.role = input.role;
|
|
18
|
+
this.enabled = input.enabled;
|
|
19
|
+
Object.freeze(this);
|
|
6
20
|
}
|
|
7
|
-
|
|
8
|
-
|
|
21
|
+
}
|
|
22
|
+
export const Notice = RuntimeNotice;
|
|
@@ -103,7 +103,27 @@ export interface MessageDispatchResult {
|
|
|
103
103
|
readonly owner?: PluginId;
|
|
104
104
|
readonly value?: unknown;
|
|
105
105
|
}
|
|
106
|
-
|
|
106
|
+
/** Canonical message value delivered to inbound middleware and handlers. */
|
|
107
|
+
export interface MessageBase extends IncomingMessage {
|
|
108
|
+
readonly generation: number;
|
|
109
|
+
readonly metadata: Readonly<Record<string, unknown>>;
|
|
110
|
+
readonly id?: string;
|
|
111
|
+
readonly clientAdapter?: string;
|
|
112
|
+
readonly $client: unknown;
|
|
113
|
+
readonly $reply: (content: SendContent) => Promise<DeliveryReceipt>;
|
|
114
|
+
readonly $replyFrom: (requester: PluginId, content: SendContent) => Promise<DeliveryReceipt>;
|
|
115
|
+
readonly $sendTo: (conversation: ConversationAddress, content: SendContent) => Promise<DeliveryReceipt>;
|
|
116
|
+
readonly $replyToPrivate: (content: SendContent, from?: boolean | {
|
|
117
|
+
readonly kind: 'group' | 'channel';
|
|
118
|
+
readonly id: string;
|
|
119
|
+
}) => Promise<DeliveryReceipt>;
|
|
120
|
+
readonly $replyToGroup: (groupId: string, content: SendContent) => Promise<DeliveryReceipt>;
|
|
121
|
+
readonly $replyToChannel: (channelId: string, guildId: string, content: SendContent, threadId?: string) => Promise<DeliveryReceipt>;
|
|
122
|
+
}
|
|
123
|
+
/** Canonical runtime message with optional plugin-owned extensions. */
|
|
124
|
+
export type Message<T extends object = {}> = MessageBase & T;
|
|
125
|
+
/** @internal Generation-scoped implementation of the public Message contract. */
|
|
126
|
+
export declare class RuntimeMessage implements MessageBase {
|
|
107
127
|
#private;
|
|
108
128
|
readonly conversation: ConversationRef;
|
|
109
129
|
readonly content: string;
|
|
@@ -188,4 +208,6 @@ export declare class Message {
|
|
|
188
208
|
*/
|
|
189
209
|
readonly $replyToChannel: (channelId: string, guildId: string, content: SendContent, threadId?: string) => Promise<DeliveryReceipt>;
|
|
190
210
|
}
|
|
211
|
+
/** Constructor for canonical runtime Message values. */
|
|
212
|
+
export declare const Message: typeof RuntimeMessage;
|
|
191
213
|
export declare function createOutboundEnvelope(request: Omit<OutboundEnvelope, 'payload' | '$client' | 'replace'>, initialPayload: unknown, resolveClient?: () => unknown): OutboundEnvelope;
|
|
@@ -32,7 +32,8 @@ export function isSegmentContent(value) {
|
|
|
32
32
|
&& typeof value.data === 'object'
|
|
33
33
|
&& value.data !== null;
|
|
34
34
|
}
|
|
35
|
-
|
|
35
|
+
/** @internal Generation-scoped implementation of the public Message contract. */
|
|
36
|
+
export class RuntimeMessage {
|
|
36
37
|
conversation;
|
|
37
38
|
content;
|
|
38
39
|
generation;
|
|
@@ -158,6 +159,8 @@ export class Message {
|
|
|
158
159
|
*/
|
|
159
160
|
$replyToChannel;
|
|
160
161
|
}
|
|
162
|
+
/** Constructor for canonical runtime Message values. */
|
|
163
|
+
export const Message = RuntimeMessage;
|
|
161
164
|
export function createOutboundEnvelope(request, initialPayload, resolveClient = () => undefined) {
|
|
162
165
|
let payload = initialPayload;
|
|
163
166
|
return Object.freeze({
|
|
@@ -84,36 +84,27 @@ export class ConversationRuntime {
|
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
function conversationEventFromNotice(notice) {
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
: sceneType === 'channel' ? 'channel'
|
|
90
|
-
: sceneType === 'friend' || sceneType === 'private' ? 'private'
|
|
91
|
-
: undefined;
|
|
92
|
-
if (!kind || !notice.$scene_id || !notice.$id)
|
|
87
|
+
const conversation = notice.conversation;
|
|
88
|
+
if (!conversation || !notice.id)
|
|
93
89
|
return undefined;
|
|
94
|
-
const
|
|
95
|
-
endpoint: Object.freeze({ adapter: String(notice.$adapter), id: String(notice.$endpoint) }),
|
|
96
|
-
kind,
|
|
97
|
-
id: String(notice.$scene_id),
|
|
98
|
-
});
|
|
99
|
-
const actor = notice.$actor?.id
|
|
90
|
+
const actor = notice.actor?.id
|
|
100
91
|
? Object.freeze({
|
|
101
|
-
id: String(notice
|
|
102
|
-
...(notice
|
|
92
|
+
id: String(notice.actor.id),
|
|
93
|
+
...(notice.actor.name ? { displayName: notice.actor.name } : {}),
|
|
103
94
|
})
|
|
104
95
|
: undefined;
|
|
105
|
-
const target = notice
|
|
96
|
+
const target = notice.target?.id
|
|
106
97
|
? Object.freeze({
|
|
107
|
-
id: String(notice
|
|
108
|
-
...(notice
|
|
98
|
+
id: String(notice.target.id),
|
|
99
|
+
...(notice.target.name ? { displayName: notice.target.name } : {}),
|
|
109
100
|
})
|
|
110
101
|
: undefined;
|
|
111
102
|
const base = Object.freeze({
|
|
112
|
-
eventId: `notice:${conversationRefKey(conversation)}:${String(notice
|
|
103
|
+
eventId: `notice:${conversationRefKey(conversation)}:${String(notice.id)}`,
|
|
113
104
|
conversation,
|
|
114
|
-
timestamp: notice
|
|
105
|
+
timestamp: notice.timestamp,
|
|
115
106
|
});
|
|
116
|
-
switch (notice
|
|
107
|
+
switch (notice.name.split('.').slice(2).join('.')) {
|
|
117
108
|
case 'member_increase':
|
|
118
109
|
case 'increase':
|
|
119
110
|
return target
|
|
@@ -132,7 +123,7 @@ function conversationEventFromNotice(notice) {
|
|
|
132
123
|
case 'ban':
|
|
133
124
|
if (!target)
|
|
134
125
|
return undefined;
|
|
135
|
-
return notice
|
|
126
|
+
return notice.durationSeconds === 0
|
|
136
127
|
? Object.freeze({
|
|
137
128
|
...base,
|
|
138
129
|
type: 'member.unmuted',
|
|
@@ -144,37 +135,37 @@ function conversationEventFromNotice(notice) {
|
|
|
144
135
|
type: 'member.muted',
|
|
145
136
|
member: target,
|
|
146
137
|
...(actor ? { actor } : {}),
|
|
147
|
-
durationSeconds: notice
|
|
138
|
+
durationSeconds: notice.durationSeconds ?? 0,
|
|
148
139
|
});
|
|
149
140
|
case 'admin_change':
|
|
150
|
-
return target && notice
|
|
141
|
+
return target && notice.role && typeof notice.enabled === 'boolean'
|
|
151
142
|
? Object.freeze({
|
|
152
143
|
...base,
|
|
153
144
|
type: 'member.role_changed',
|
|
154
145
|
member: target,
|
|
155
146
|
...(actor ? { actor } : {}),
|
|
156
|
-
role: notice
|
|
157
|
-
enabled: notice
|
|
147
|
+
role: notice.role,
|
|
148
|
+
enabled: notice.enabled,
|
|
158
149
|
})
|
|
159
150
|
: undefined;
|
|
160
151
|
case 'recall':
|
|
161
|
-
return notice
|
|
152
|
+
return notice.messageId
|
|
162
153
|
? Object.freeze({
|
|
163
154
|
...base,
|
|
164
155
|
type: 'message.recalled',
|
|
165
|
-
message: Object.freeze({ conversation, id: notice
|
|
156
|
+
message: Object.freeze({ conversation, id: notice.messageId }),
|
|
166
157
|
...(actor ? { actor } : {}),
|
|
167
158
|
})
|
|
168
159
|
: undefined;
|
|
169
160
|
case 'emoji_reaction':
|
|
170
|
-
return notice
|
|
161
|
+
return notice.messageId && notice.reaction && notice.operation
|
|
171
162
|
? Object.freeze({
|
|
172
163
|
...base,
|
|
173
164
|
type: 'message.reaction_changed',
|
|
174
|
-
message: Object.freeze({ conversation, id: notice
|
|
165
|
+
message: Object.freeze({ conversation, id: notice.messageId }),
|
|
175
166
|
...(actor ? { actor } : {}),
|
|
176
|
-
reaction: notice
|
|
177
|
-
operation: notice
|
|
167
|
+
reaction: notice.reaction,
|
|
168
|
+
operation: notice.operation,
|
|
178
169
|
})
|
|
179
170
|
: undefined;
|
|
180
171
|
case 'poke':
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AdapterIndex, type AdapterEndpointPhase, type AdapterOperation, type EndpointManagement, type EndpointManagementCapability } from '@zhin.js/adapter';
|
|
1
|
+
import { AdapterIndex, type AdapterEndpointPhase, type AdapterOperation, type Endpoint, type EndpointTransportState, type EndpointManagement, type EndpointManagementCapability } from '@zhin.js/adapter';
|
|
2
2
|
import type { RuntimeSnapshot, SnapshotLease } from '@zhin.js/plugin-runtime';
|
|
3
3
|
import type { ConversationRef, DeliveryReceipt, EndpointCapabilities, MessageRef } from '@zhin.js/im-contract';
|
|
4
4
|
import type { ConversationAddress, SendRequest } from './contracts.js';
|
|
@@ -8,6 +8,9 @@ export interface EndpointRuntimeSummary {
|
|
|
8
8
|
readonly adapter: string;
|
|
9
9
|
readonly owner: string;
|
|
10
10
|
readonly connected: boolean;
|
|
11
|
+
readonly admitted: boolean;
|
|
12
|
+
readonly transportState: EndpointTransportState | 'unknown';
|
|
13
|
+
readonly eventDiagnostics: Endpoint['eventDiagnostics'];
|
|
11
14
|
readonly status: 'online' | 'offline';
|
|
12
15
|
readonly phase: AdapterEndpointPhase;
|
|
13
16
|
readonly operations: readonly AdapterOperation[];
|
|
@@ -75,5 +78,7 @@ export declare class EndpointRuntime {
|
|
|
75
78
|
withManagement<T>(adapter: string, endpointKey: string, run: (management: EndpointManagement) => T | Promise<T>): Promise<T | null>;
|
|
76
79
|
}
|
|
77
80
|
export declare function requireAdapters(snapshot: RuntimeSnapshot): AdapterIndex;
|
|
81
|
+
/** Resolves public list identities against the exact operation snapshot; ambiguous aliases never pick an account. */
|
|
82
|
+
export declare function resolveEndpointIdentity(snapshot: RuntimeSnapshot, index: AdapterIndex, adapter: string, endpointKey: string): import("@zhin.js/plugin-runtime").CapabilityId | undefined;
|
|
78
83
|
export declare function adapterTypeName(packageName: string | undefined): string | undefined;
|
|
79
84
|
export {};
|
|
@@ -12,6 +12,9 @@ export class EndpointRuntime {
|
|
|
12
12
|
adapter: adapterTypeName(snapshot.tree.get(row.owner)?.packageName) ?? row.name,
|
|
13
13
|
owner: row.owner,
|
|
14
14
|
connected: row.connected,
|
|
15
|
+
admitted: row.admitted,
|
|
16
|
+
transportState: row.transportState,
|
|
17
|
+
eventDiagnostics: row.eventDiagnostics,
|
|
15
18
|
status: row.status,
|
|
16
19
|
phase: row.phase,
|
|
17
20
|
operations: row.operations,
|
|
@@ -21,14 +24,14 @@ export class EndpointRuntime {
|
|
|
21
24
|
capabilities(input) {
|
|
22
25
|
return this.#withLease((snapshot) => {
|
|
23
26
|
const index = requireAdapters(snapshot);
|
|
24
|
-
const id = index
|
|
27
|
+
const id = resolveEndpointIdentity(snapshot, index, input.adapter, input.endpointKey);
|
|
25
28
|
return id ? index.capabilities(id) : undefined;
|
|
26
29
|
}, undefined);
|
|
27
30
|
}
|
|
28
31
|
get(adapter, endpointKey) {
|
|
29
32
|
return this.#withLease((snapshot) => {
|
|
30
33
|
const index = requireAdapters(snapshot);
|
|
31
|
-
const id = index
|
|
34
|
+
const id = resolveEndpointIdentity(snapshot, index, adapter, endpointKey);
|
|
32
35
|
if (!id)
|
|
33
36
|
return null;
|
|
34
37
|
const row = index.describe().find((item) => item.id === id);
|
|
@@ -38,6 +41,9 @@ export class EndpointRuntime {
|
|
|
38
41
|
name: row.name,
|
|
39
42
|
adapter: adapterTypeName(snapshot.tree.get(row.owner)?.packageName) ?? row.name,
|
|
40
43
|
connected: row.connected,
|
|
44
|
+
admitted: row.admitted,
|
|
45
|
+
transportState: row.transportState,
|
|
46
|
+
eventDiagnostics: row.eventDiagnostics,
|
|
41
47
|
status: row.status,
|
|
42
48
|
phase: row.phase,
|
|
43
49
|
operations: row.operations,
|
|
@@ -49,7 +55,7 @@ export class EndpointRuntime {
|
|
|
49
55
|
const lease = this.context.acquire();
|
|
50
56
|
try {
|
|
51
57
|
const index = requireAdapters(lease.value);
|
|
52
|
-
const resolved = index
|
|
58
|
+
const resolved = resolveEndpointIdentity(lease.value, index, input.adapter, input.endpointKey);
|
|
53
59
|
if (!resolved)
|
|
54
60
|
throw new Error('endpoint not found');
|
|
55
61
|
const requester = index.owner(resolved);
|
|
@@ -95,7 +101,9 @@ export class EndpointRuntime {
|
|
|
95
101
|
return null;
|
|
96
102
|
}
|
|
97
103
|
try {
|
|
98
|
-
const
|
|
104
|
+
const index = requireAdapters(lease.value);
|
|
105
|
+
const id = resolveEndpointIdentity(lease.value, index, adapter, endpointKey);
|
|
106
|
+
const endpoint = id ? index.connection(String(id), String(id)) : undefined;
|
|
99
107
|
if (!endpoint)
|
|
100
108
|
return null;
|
|
101
109
|
return await run(resolveEndpointManagement(endpoint) ?? Object.freeze({}));
|
|
@@ -114,7 +122,7 @@ export class EndpointRuntime {
|
|
|
114
122
|
}
|
|
115
123
|
try {
|
|
116
124
|
const index = requireAdapters(lease.value);
|
|
117
|
-
const id = index
|
|
125
|
+
const id = resolveEndpointIdentity(lease.value, index, adapter, endpointKey);
|
|
118
126
|
const control = id ? index.control(id, operation) : undefined;
|
|
119
127
|
return control ? await run(control) : fallback;
|
|
120
128
|
}
|
|
@@ -148,6 +156,20 @@ export function requireAdapters(snapshot) {
|
|
|
148
156
|
}
|
|
149
157
|
return projection;
|
|
150
158
|
}
|
|
159
|
+
/** Resolves public list identities against the exact operation snapshot; ambiguous aliases never pick an account. */
|
|
160
|
+
export function resolveEndpointIdentity(snapshot, index, adapter, endpointKey) {
|
|
161
|
+
const live = new Map(index.describe().map(row => [row.id, row.name]));
|
|
162
|
+
const matches = index.list().filter(record => {
|
|
163
|
+
const localName = record.id.split('\0').pop() ?? record.id;
|
|
164
|
+
const alias = adapterTypeName(snapshot.tree.get(record.owner)?.packageName);
|
|
165
|
+
const adapterMatches = alias === adapter || record.name === adapter || String(record.id) === adapter
|
|
166
|
+
|| localName === adapter || String(record.owner) === adapter || record.owner.endsWith(`/${adapter}`);
|
|
167
|
+
const endpointMatches = record.name === endpointKey || String(record.id) === endpointKey
|
|
168
|
+
|| record.id.endsWith(`/${endpointKey}`) || live.get(record.id) === endpointKey;
|
|
169
|
+
return adapterMatches && endpointMatches;
|
|
170
|
+
});
|
|
171
|
+
return matches.length === 1 ? matches[0].id : undefined;
|
|
172
|
+
}
|
|
151
173
|
export function adapterTypeName(packageName) {
|
|
152
174
|
if (!packageName)
|
|
153
175
|
return undefined;
|
|
@@ -2,8 +2,8 @@ import type { GenerationAdmissionGate, RuntimeSnapshot, SnapshotLease } from '@z
|
|
|
2
2
|
import type { EndpointEvent } from '@zhin.js/adapter';
|
|
3
3
|
import type { DeliveryReceipt } from '@zhin.js/im-contract';
|
|
4
4
|
import type { UserInteraction } from '@zhin.js/interaction';
|
|
5
|
-
import type
|
|
6
|
-
import { Message, type IncomingMessage, type MessageSenderRef, type SendRequest } from './contracts.js';
|
|
5
|
+
import { type Notice } from '../../notice.js';
|
|
6
|
+
import { type Message, type IncomingMessage, type MessageSenderRef, type SendRequest } from './contracts.js';
|
|
7
7
|
import type { RuntimeInteractionCoordinator } from './interaction-runtime.js';
|
|
8
8
|
import { type RuntimeMessageEvent } from './message-events.js';
|
|
9
9
|
type InboundInteractionPort = Pick<RuntimeInteractionCoordinator<GenerationAdmissionGate>, 'create' | 'createForMessage' | 'createFromUnknown' | 'dispatch' | 'resolveClaim'>;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { isHandlerIndex, handlerFeatureId } from '../../feature/handler.js';
|
|
2
2
|
import { formatCompact, getLogger, truncatePreview } from '@zhin.js/logger';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { RuntimeNotice } from '../../notice.js';
|
|
4
|
+
import { RuntimeRequest } from '../../request.js';
|
|
5
|
+
import { RuntimeSystemEvent } from '../../system-event.js';
|
|
6
|
+
import { RuntimeMessage, } from './contracts.js';
|
|
5
7
|
import { defaultCommandPrefixResolver, MessageDispatcher } from './message-dispatcher.js';
|
|
6
8
|
import { requireAdapters } from './endpoint-runtime.js';
|
|
7
9
|
import { formatConversationLog, previewMessageContent, } from './message-events.js';
|
|
@@ -26,11 +28,11 @@ export class InboundRuntime {
|
|
|
26
28
|
case 'message.receive':
|
|
27
29
|
return this.#receiveMessage(event, admission);
|
|
28
30
|
case 'notice.receive':
|
|
29
|
-
return this.#
|
|
31
|
+
return this.#receiveCanonicalEndpointEvent(event);
|
|
30
32
|
case 'request.receive':
|
|
31
|
-
return this.#
|
|
33
|
+
return this.#receiveCanonicalEndpointEvent(event);
|
|
32
34
|
case 'system.receive':
|
|
33
|
-
return this.#
|
|
35
|
+
return this.#receiveCanonicalEndpointEvent(event);
|
|
34
36
|
default:
|
|
35
37
|
return this.#receiveSideEvent(event);
|
|
36
38
|
}
|
|
@@ -52,7 +54,7 @@ export class InboundRuntime {
|
|
|
52
54
|
const enrichedSender = this.context.enrichSender
|
|
53
55
|
? this.context.enrichSender(input.sender, conversation, lease.value)
|
|
54
56
|
: input.sender;
|
|
55
|
-
const message = new
|
|
57
|
+
const message = new RuntimeMessage(conversation, input.content, lease.value.generation, (content, replyRequester = requester, targetConversation) => {
|
|
56
58
|
if (!active)
|
|
57
59
|
throw new Error('Message reply scope has ended');
|
|
58
60
|
const effectiveConversation = targetConversation
|
|
@@ -141,14 +143,49 @@ export class InboundRuntime {
|
|
|
141
143
|
this.context.release(lease);
|
|
142
144
|
}
|
|
143
145
|
}
|
|
144
|
-
async #
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
146
|
+
async #receiveCanonicalEndpointEvent(source) {
|
|
147
|
+
const lease = this.context.acquire();
|
|
148
|
+
let active = true;
|
|
149
|
+
try {
|
|
150
|
+
const context = {
|
|
151
|
+
endpoint: {
|
|
152
|
+
id: source.endpoint.id,
|
|
153
|
+
adapter: String(requireAdapters(lease.value).owner(source.endpoint.id)),
|
|
154
|
+
},
|
|
155
|
+
generation: lease.value.generation,
|
|
156
|
+
client: () => {
|
|
157
|
+
if (!active)
|
|
158
|
+
throw new Error('Endpoint event Client context expired with its generation operation');
|
|
159
|
+
return source.client;
|
|
160
|
+
},
|
|
161
|
+
};
|
|
162
|
+
const dispatch = async (payload) => {
|
|
163
|
+
await this.#runHandlers(lease.value, source.name, [withEndpointEventPayload(source, payload)]);
|
|
164
|
+
};
|
|
165
|
+
if (source.payload.type !== source.name.split('.')[0]) {
|
|
166
|
+
throw new TypeError('Endpoint event payload type does not match its gateway event');
|
|
167
|
+
}
|
|
168
|
+
switch (source.payload.type) {
|
|
169
|
+
case 'notice': {
|
|
170
|
+
const notice = new RuntimeNotice(source.payload, context);
|
|
171
|
+
await this.context.recordNotice(notice);
|
|
172
|
+
await dispatch(notice);
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
case 'request':
|
|
176
|
+
await withRequestActionScope(source.payload, async (input) => {
|
|
177
|
+
await dispatch(new RuntimeRequest(input, context));
|
|
178
|
+
});
|
|
179
|
+
break;
|
|
180
|
+
case 'system':
|
|
181
|
+
await dispatch(new RuntimeSystemEvent(source.payload, context));
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
finally {
|
|
186
|
+
active = false;
|
|
187
|
+
this.context.release(lease);
|
|
188
|
+
}
|
|
152
189
|
}
|
|
153
190
|
async #receiveSideEvent(event) {
|
|
154
191
|
const lease = this.context.acquire();
|
|
@@ -167,12 +204,11 @@ export class InboundRuntime {
|
|
|
167
204
|
resolveInteraction: (name, interactionArgs) => {
|
|
168
205
|
const eventContext = interactionArgs[0];
|
|
169
206
|
const payload = eventContext?.payload;
|
|
170
|
-
if (name === 'message.receive' && payload instanceof
|
|
207
|
+
if (name === 'message.receive' && payload instanceof RuntimeMessage) {
|
|
171
208
|
return this.createInteraction(payload);
|
|
172
209
|
}
|
|
173
210
|
if (name === 'notice.receive'
|
|
174
|
-
|| name === 'request.receive'
|
|
175
|
-
|| name === 'system.receive') {
|
|
211
|
+
|| name === 'request.receive') {
|
|
176
212
|
return this.#createInteractionForSideEvent(payload, snapshot);
|
|
177
213
|
}
|
|
178
214
|
return undefined;
|
|
@@ -181,21 +217,14 @@ export class InboundRuntime {
|
|
|
181
217
|
await index.dispatch(event, args, options);
|
|
182
218
|
}
|
|
183
219
|
#createInteractionForSideEvent(payload, snapshot) {
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
if (!adapter || !endpointKey)
|
|
220
|
+
const conversation = payload.conversation;
|
|
221
|
+
if (!conversation)
|
|
187
222
|
return undefined;
|
|
188
|
-
const channel = sideEventSendChannel(payload);
|
|
189
|
-
const conversation = Object.freeze({
|
|
190
|
-
endpoint: Object.freeze({ adapter, id: endpointKey }),
|
|
191
|
-
kind: channel.type,
|
|
192
|
-
id: channel.id || endpointKey,
|
|
193
|
-
});
|
|
194
223
|
const requester = snapshot.root;
|
|
195
224
|
const source = Object.freeze({
|
|
196
225
|
conversation,
|
|
197
|
-
...(payload
|
|
198
|
-
? { sender: Object.freeze({ id: payload
|
|
226
|
+
...(payload.actor?.id
|
|
227
|
+
? { sender: Object.freeze({ id: payload.actor.id }) }
|
|
199
228
|
: {}),
|
|
200
229
|
$reply: (content) => this.context.deliver({
|
|
201
230
|
conversation,
|
|
@@ -226,7 +255,7 @@ async function withRequestActionScope(request, dispatch) {
|
|
|
226
255
|
void operation.then(() => actions.delete(operation), () => actions.delete(operation));
|
|
227
256
|
return operation;
|
|
228
257
|
};
|
|
229
|
-
const scoped = Object.
|
|
258
|
+
const scoped = Object.freeze({ ...request,
|
|
230
259
|
$approve: async (remark) => run(() => request.$approve(remark)),
|
|
231
260
|
$reject: async (reason) => run(() => request.$reject(reason)),
|
|
232
261
|
});
|
|
@@ -9,3 +9,6 @@ export * from './message-dispatcher.js';
|
|
|
9
9
|
export type { RuntimeMessageEvent, RuntimeMessageEventSource, } from './message-events.js';
|
|
10
10
|
export * from './outbound-renderer.js';
|
|
11
11
|
export * from './outbound-segments.js';
|
|
12
|
+
export * from '../../notice.js';
|
|
13
|
+
export * from '../../request.js';
|
|
14
|
+
export * from '../../system-event.js';
|
|
@@ -7,3 +7,6 @@ export * from './message-bus.js';
|
|
|
7
7
|
export * from './message-dispatcher.js';
|
|
8
8
|
export * from './outbound-renderer.js';
|
|
9
9
|
export * from './outbound-segments.js';
|
|
10
|
+
export * from '../../notice.js';
|
|
11
|
+
export * from '../../request.js';
|
|
12
|
+
export * from '../../system-event.js';
|
|
@@ -1,15 +1,31 @@
|
|
|
1
|
+
import { adapterFeatureId, isAdapterIndex } from '@zhin.js/adapter';
|
|
1
2
|
import { commandFeatureId, isCommandIndex, } from '@zhin.js/command';
|
|
2
3
|
import { formatCompact, getLogger, truncatePreview } from '@zhin.js/logger';
|
|
3
4
|
const logger = getLogger('command');
|
|
4
|
-
function ownerOfMessage(message) {
|
|
5
|
-
|
|
5
|
+
function ownerOfMessage(message, snapshot) {
|
|
6
|
+
// adapter is a platform literal (e.g. terminal), not the Plugin owner.
|
|
7
|
+
const slot = snapshot.capabilities.get(message.conversation.endpoint.id);
|
|
8
|
+
if (slot)
|
|
9
|
+
return slot.owner;
|
|
10
|
+
// Expanded endpoint records have identities distinct from their source slot.
|
|
11
|
+
// Resolve their ownership through the generation's canonical AdapterIndex.
|
|
12
|
+
const adapters = snapshot.projections.get(adapterFeatureId);
|
|
13
|
+
if (!isAdapterIndex(adapters))
|
|
14
|
+
return undefined;
|
|
15
|
+
try {
|
|
16
|
+
return adapters.owner(message.conversation.endpoint.id);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
6
21
|
}
|
|
7
22
|
/**
|
|
8
23
|
* 默认解析:读消息所属适配器实例 config 的 `commandPrefix`(默认 `''`);
|
|
9
24
|
* 实例声明 `endpoints` 数组时,按消息 endpoint 名找 entry,`entry.commandPrefix` 覆盖顶层。
|
|
10
25
|
*/
|
|
11
26
|
export const defaultCommandPrefixResolver = (message, snapshot) => {
|
|
12
|
-
const
|
|
27
|
+
const owner = ownerOfMessage(message, snapshot);
|
|
28
|
+
const config = (owner ? snapshot.config.get(owner) : undefined);
|
|
13
29
|
if (!config)
|
|
14
30
|
return '';
|
|
15
31
|
const endpointId = message.endpointId;
|
|
@@ -43,7 +59,7 @@ export class MessageDispatcher {
|
|
|
43
59
|
? stripCommandPrefix(message.segments, prefix)
|
|
44
60
|
: undefined;
|
|
45
61
|
const matchInput = structuredInput ?? input;
|
|
46
|
-
const result = await commands.dispatch(matchInput, message, interactionFactory, prefix);
|
|
62
|
+
const result = await commands.dispatch(matchInput, message, interactionFactory, prefix, snapshot);
|
|
47
63
|
if (result.matched && result.value !== undefined) {
|
|
48
64
|
if (!result.owner)
|
|
49
65
|
throw new Error('Matched Command is missing its owner');
|
|
@@ -70,6 +86,7 @@ export class MessageDispatcher {
|
|
|
70
86
|
function stripCommandPrefix(segments, prefix) {
|
|
71
87
|
let pendingPrefix = prefix;
|
|
72
88
|
let atStart = true;
|
|
89
|
+
let trimLeading = true;
|
|
73
90
|
const result = [];
|
|
74
91
|
for (const segment of segments) {
|
|
75
92
|
if (!atStart) {
|
|
@@ -84,6 +101,13 @@ function stripCommandPrefix(segments, prefix) {
|
|
|
84
101
|
continue;
|
|
85
102
|
}
|
|
86
103
|
let text = segment.data.text;
|
|
104
|
+
// Match the leading whitespace normalization of message.content without
|
|
105
|
+
// discarding structured arguments when the prefix follows whitespace.
|
|
106
|
+
if (trimLeading) {
|
|
107
|
+
text = text.trimStart();
|
|
108
|
+
if (text)
|
|
109
|
+
trimLeading = false;
|
|
110
|
+
}
|
|
87
111
|
if (pendingPrefix) {
|
|
88
112
|
if (text.startsWith(pendingPrefix)) {
|
|
89
113
|
text = text.slice(pendingPrefix.length);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type RuntimeSnapshot } from '@zhin.js/plugin-runtime';
|
|
2
|
-
import type
|
|
2
|
+
import { type ConversationRef, type DeliveryReceipt } from '@zhin.js/im-contract';
|
|
3
3
|
import { type SendRequest } from './contracts.js';
|
|
4
4
|
import { OutboundRenderer } from './outbound-renderer.js';
|
|
5
5
|
import { type RuntimeMessageEvent } from './message-events.js';
|