@zhin.js/core 1.1.56 → 1.1.57
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.js +3 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +2 -0
- 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/inbound-dedup.d.ts +1 -0
- package/lib/plugin-runtime/im/inbound-dedup.js +1 -0
- package/lib/plugin-runtime/im/inbound-origin.d.ts +27 -0
- package/lib/plugin-runtime/im/inbound-runtime.js +78 -2
- package/lib/plugin-runtime/im/index.d.ts +1 -0
- package/lib/plugin-runtime/im/index.js +1 -0
- package/lib/plugin-runtime/im/interaction-runtime.d.ts +7 -6
- package/lib/plugin-runtime/im/interaction-runtime.js +85 -24
- package/lib/plugin-runtime/im/interactive-router.d.ts +28 -8
- package/lib/plugin-runtime/im/interactive-router.js +112 -35
- package/lib/plugin-runtime/im/jsx-button-registry.d.ts +16 -2
- package/lib/plugin-runtime/im/jsx-button-registry.js +56 -19
- package/lib/plugin-runtime/im/message-dispatcher.js +57 -5
- package/lib/plugin-runtime/im/outbound-delivery-runtime.d.ts +5 -4
- package/lib/plugin-runtime/im/outbound-delivery-runtime.js +71 -21
- 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 -16
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
function normalizeLimiterKey(key) {
|
|
2
|
+
if (typeof key === 'string')
|
|
3
|
+
return key;
|
|
4
|
+
return `${key.venueId ?? '*'}:${key.endpointId}`;
|
|
5
|
+
}
|
|
6
|
+
export function createVenueSendLimiter(options = {}) {
|
|
7
|
+
const minIntervalMs = Math.max(0, options.minIntervalMs ?? 1000);
|
|
8
|
+
const maxBurst = Math.max(1, Math.floor(options.maxBurst ?? 1));
|
|
9
|
+
const now = options.now ?? Date.now;
|
|
10
|
+
const sleep = options.sleep
|
|
11
|
+
?? ((ms) => new Promise((resolve) => {
|
|
12
|
+
setTimeout(resolve, ms);
|
|
13
|
+
}));
|
|
14
|
+
const buckets = new Map();
|
|
15
|
+
const reserveWaitMs = (rawKey) => {
|
|
16
|
+
if (minIntervalMs <= 0)
|
|
17
|
+
return 0;
|
|
18
|
+
const key = normalizeLimiterKey(rawKey);
|
|
19
|
+
const current = now();
|
|
20
|
+
let bucket = buckets.get(key);
|
|
21
|
+
if (!bucket) {
|
|
22
|
+
bucket = {
|
|
23
|
+
tokens: maxBurst,
|
|
24
|
+
lastRefillAt: current,
|
|
25
|
+
nextAvailableAt: current,
|
|
26
|
+
};
|
|
27
|
+
buckets.set(key, bucket);
|
|
28
|
+
}
|
|
29
|
+
else if (current > bucket.lastRefillAt) {
|
|
30
|
+
const elapsed = current - bucket.lastRefillAt;
|
|
31
|
+
const replenished = elapsed / minIntervalMs;
|
|
32
|
+
bucket.tokens = Math.min(maxBurst, bucket.tokens + replenished);
|
|
33
|
+
bucket.lastRefillAt = current;
|
|
34
|
+
}
|
|
35
|
+
if (bucket.tokens >= 1 && current >= bucket.nextAvailableAt) {
|
|
36
|
+
bucket.tokens -= 1;
|
|
37
|
+
if (bucket.tokens < 1) {
|
|
38
|
+
bucket.nextAvailableAt = current + Math.ceil((1 - bucket.tokens) * minIntervalMs);
|
|
39
|
+
}
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
const scheduledAt = Math.max(current, bucket.nextAvailableAt);
|
|
43
|
+
const waitMs = scheduledAt - current;
|
|
44
|
+
bucket.tokens = 0;
|
|
45
|
+
bucket.lastRefillAt = scheduledAt;
|
|
46
|
+
bucket.nextAvailableAt = scheduledAt + minIntervalMs;
|
|
47
|
+
return waitMs;
|
|
48
|
+
};
|
|
49
|
+
const acquire = async (key) => {
|
|
50
|
+
const waitMs = reserveWaitMs(key);
|
|
51
|
+
if (waitMs > 0) {
|
|
52
|
+
await sleep(waitMs);
|
|
53
|
+
}
|
|
54
|
+
return waitMs;
|
|
55
|
+
};
|
|
56
|
+
const withLimit = async (key, fn) => {
|
|
57
|
+
await acquire(key);
|
|
58
|
+
return await fn();
|
|
59
|
+
};
|
|
60
|
+
return Object.freeze({
|
|
61
|
+
acquire,
|
|
62
|
+
withLimit,
|
|
63
|
+
schedule: withLimit,
|
|
64
|
+
reset(key) {
|
|
65
|
+
if (key === undefined) {
|
|
66
|
+
buckets.clear();
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
buckets.delete(normalizeLimiterKey(key));
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { VenueDefinition } from './contracts.js';
|
|
2
|
+
export declare class VenueValidationError extends Error {
|
|
3
|
+
readonly issues: readonly string[];
|
|
4
|
+
constructor(issues: readonly string[]);
|
|
5
|
+
}
|
|
6
|
+
export declare function validateVenueDefinitions(venues: readonly VenueDefinition[]): void;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
const VALID_SCENE_TYPES = new Set(['private', 'group', 'channel']);
|
|
2
|
+
const VALID_VISIBILITY_MODES = new Set(['visible', 'summary', 'backstage']);
|
|
3
|
+
export class VenueValidationError extends Error {
|
|
4
|
+
issues;
|
|
5
|
+
constructor(issues) {
|
|
6
|
+
super(`Invalid venue definitions:\n${issues.map((issue) => ` - ${issue}`).join('\n')}`);
|
|
7
|
+
this.name = 'VenueValidationError';
|
|
8
|
+
this.issues = Object.freeze([...issues]);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function isNonEmptyString(value) {
|
|
12
|
+
return typeof value === 'string' && value.trim().length > 0;
|
|
13
|
+
}
|
|
14
|
+
export function validateVenueDefinitions(venues) {
|
|
15
|
+
if (!Array.isArray(venues)) {
|
|
16
|
+
throw new VenueValidationError(['venues must be an array']);
|
|
17
|
+
}
|
|
18
|
+
const issues = [];
|
|
19
|
+
const seenVenueIds = new Set();
|
|
20
|
+
const seenConversations = [];
|
|
21
|
+
for (let i = 0; i < venues.length; i++) {
|
|
22
|
+
const venue = venues[i];
|
|
23
|
+
const label = isNonEmptyString(venue?.id) ? `venue "${venue.id.trim()}"` : `venues[${i}]`;
|
|
24
|
+
if (!venue || typeof venue !== 'object') {
|
|
25
|
+
issues.push(`${label}: must be an object`);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (!isNonEmptyString(venue.id)) {
|
|
29
|
+
issues.push(`${label}: id must be a non-empty string`);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
const id = venue.id.trim();
|
|
33
|
+
if (seenVenueIds.has(id)) {
|
|
34
|
+
issues.push(`${label}: duplicate venue id "${id}"`);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
seenVenueIds.add(id);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const conv = venue.conversation;
|
|
41
|
+
if (!conv || typeof conv !== 'object') {
|
|
42
|
+
issues.push(`${label}: conversation is required`);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
const platformOk = isNonEmptyString(conv.platform);
|
|
46
|
+
const sceneIdOk = isNonEmptyString(conv.sceneId);
|
|
47
|
+
if (!platformOk) {
|
|
48
|
+
issues.push(`${label}: conversation.platform must be a non-empty string`);
|
|
49
|
+
}
|
|
50
|
+
if (!sceneIdOk) {
|
|
51
|
+
issues.push(`${label}: conversation.sceneId must be a non-empty string`);
|
|
52
|
+
}
|
|
53
|
+
if (conv.sceneType !== undefined && !VALID_SCENE_TYPES.has(conv.sceneType)) {
|
|
54
|
+
issues.push(`${label}: conversation.sceneType must be one of "private", "group", "channel"`);
|
|
55
|
+
}
|
|
56
|
+
if (conv.threadId !== undefined && !isNonEmptyString(conv.threadId)) {
|
|
57
|
+
issues.push(`${label}: conversation.threadId must be a non-empty string when provided`);
|
|
58
|
+
}
|
|
59
|
+
if (platformOk && sceneIdOk) {
|
|
60
|
+
const platform = conv.platform.trim();
|
|
61
|
+
const sceneType = conv.sceneType;
|
|
62
|
+
const sceneId = conv.sceneId.trim();
|
|
63
|
+
const threadId = conv.threadId?.trim() ?? '';
|
|
64
|
+
const conflict = seenConversations.find((existing) => existing.platform === platform
|
|
65
|
+
&& existing.sceneId === sceneId
|
|
66
|
+
&& existing.threadId === threadId
|
|
67
|
+
&& (existing.sceneType === undefined || sceneType === undefined || existing.sceneType === sceneType));
|
|
68
|
+
if (conflict) {
|
|
69
|
+
issues.push(`${label}: duplicate conversation (${platform}/${sceneType ?? '*'}/${sceneId}${threadId ? `/${threadId}` : ''}) already bound by venue "${conflict.venueId}"`);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
seenConversations.push({
|
|
73
|
+
venueId: isNonEmptyString(venue.id) ? venue.id.trim() : `venues[${i}]`,
|
|
74
|
+
platform,
|
|
75
|
+
sceneType,
|
|
76
|
+
sceneId,
|
|
77
|
+
threadId,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const memberAgents = new Set();
|
|
83
|
+
const memberEndpoints = new Set();
|
|
84
|
+
const memberNamesLower = new Set();
|
|
85
|
+
if (!Array.isArray(venue.members) || venue.members.length === 0) {
|
|
86
|
+
issues.push(`${label}: members must be a non-empty array`);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
for (let m = 0; m < venue.members.length; m++) {
|
|
90
|
+
const member = venue.members[m];
|
|
91
|
+
const memberLabel = `${label}.members[${m}]`;
|
|
92
|
+
if (!member || typeof member !== 'object') {
|
|
93
|
+
issues.push(`${memberLabel}: must be an object`);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (!isNonEmptyString(member.agent)) {
|
|
97
|
+
issues.push(`${memberLabel}: agent must be a non-empty string`);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
const agent = member.agent.trim();
|
|
101
|
+
if (memberAgents.has(agent)) {
|
|
102
|
+
issues.push(`${memberLabel}: duplicate member agent "${agent}" in venue`);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
memberAgents.add(agent);
|
|
106
|
+
memberNamesLower.add(agent.toLowerCase());
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (!isNonEmptyString(member.botEndpoint)) {
|
|
110
|
+
issues.push(`${memberLabel}: botEndpoint must be a non-empty string`);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
const endpoint = member.botEndpoint.trim();
|
|
114
|
+
if (memberEndpoints.has(endpoint)) {
|
|
115
|
+
issues.push(`${memberLabel}: duplicate member botEndpoint "${endpoint}" in venue`);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
memberEndpoints.add(endpoint);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (member.aliases !== undefined) {
|
|
122
|
+
if (!Array.isArray(member.aliases)) {
|
|
123
|
+
issues.push(`${memberLabel}: aliases must be an array of strings`);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
for (let a = 0; a < member.aliases.length; a++) {
|
|
127
|
+
const alias = member.aliases[a];
|
|
128
|
+
if (!isNonEmptyString(alias)) {
|
|
129
|
+
issues.push(`${memberLabel}.aliases[${a}]: alias must be a non-empty string`);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
const lower = alias.trim().toLowerCase();
|
|
133
|
+
if (memberNamesLower.has(lower)) {
|
|
134
|
+
issues.push(`${memberLabel}.aliases[${a}]: duplicate alias "${alias}" in venue`);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
memberNamesLower.add(lower);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (venue.entry !== undefined) {
|
|
146
|
+
if (!isNonEmptyString(venue.entry)) {
|
|
147
|
+
issues.push(`${label}: entry must be a non-empty string when specified`);
|
|
148
|
+
}
|
|
149
|
+
else if (memberAgents.size > 0 && !memberAgents.has(venue.entry.trim())) {
|
|
150
|
+
issues.push(`${label}: entry "${venue.entry}" does not match any member agent in venue`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (venue.visibility !== undefined && !VALID_VISIBILITY_MODES.has(venue.visibility)) {
|
|
154
|
+
issues.push(`${label}: visibility must be "visible", "summary", or "backstage"`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (issues.length > 0) {
|
|
158
|
+
throw new VenueValidationError(issues);
|
|
159
|
+
}
|
|
160
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhin.js/core",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.57",
|
|
4
4
|
"description": "Zhin机器人核心框架",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -57,29 +57,29 @@
|
|
|
57
57
|
"dependencies": {
|
|
58
58
|
"qrcode": "^1.5.4",
|
|
59
59
|
"segment-matcher": "^1.0.5",
|
|
60
|
-
"@zhin.js/
|
|
61
|
-
"@zhin.js/
|
|
62
|
-
"@zhin.js/
|
|
63
|
-
"@zhin.js/
|
|
64
|
-
"@zhin.js/
|
|
65
|
-
"@zhin.js/
|
|
60
|
+
"@zhin.js/adapter": "1.1.29",
|
|
61
|
+
"@zhin.js/command": "1.1.15",
|
|
62
|
+
"@zhin.js/component": "1.1.14",
|
|
63
|
+
"@zhin.js/database": "1.1.7",
|
|
64
|
+
"@zhin.js/handler": "1.1.13",
|
|
65
|
+
"@zhin.js/im-contract": "1.1.12",
|
|
66
66
|
"@zhin.js/interaction": "1.1.2",
|
|
67
|
-
"@zhin.js/kernel": "1.1.
|
|
67
|
+
"@zhin.js/kernel": "1.1.6",
|
|
68
68
|
"@zhin.js/logger": "1.1.2",
|
|
69
|
-
"@zhin.js/middleware": "1.1.
|
|
70
|
-
"@zhin.js/permission": "1.1.
|
|
71
|
-
"@zhin.js/plugin-runtime": "1.1.
|
|
72
|
-
"@zhin.js/schedule-feature": "1.1.
|
|
73
|
-
"@zhin.js/schema": "1.1.
|
|
74
|
-
"@zhin.js/jsx": "1.1.
|
|
75
|
-
"@zhin.js/feature-kit": "1.1.
|
|
69
|
+
"@zhin.js/middleware": "1.1.14",
|
|
70
|
+
"@zhin.js/permission": "1.1.13",
|
|
71
|
+
"@zhin.js/plugin-runtime": "1.1.21",
|
|
72
|
+
"@zhin.js/schedule-feature": "1.1.13",
|
|
73
|
+
"@zhin.js/schema": "1.1.5",
|
|
74
|
+
"@zhin.js/jsx": "1.1.11",
|
|
75
|
+
"@zhin.js/feature-kit": "1.1.13"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
78
|
"@types/node": "^26.1.2",
|
|
79
79
|
"@types/qrcode": "^1.5.6",
|
|
80
80
|
"ajv": "8.18.0",
|
|
81
81
|
"typescript": "^6.0.3",
|
|
82
|
-
"@zhin.js/ai": "1.1.
|
|
82
|
+
"@zhin.js/ai": "1.1.52"
|
|
83
83
|
},
|
|
84
84
|
"repository": {
|
|
85
85
|
"type": "git",
|