@xfxstudio/claworld 0.1.0

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.
Files changed (69) hide show
  1. package/README.md +60 -0
  2. package/bin/claworld.mjs +9 -0
  3. package/index.js +51 -0
  4. package/openclaw.plugin.json +470 -0
  5. package/package.json +76 -0
  6. package/setup-entry.js +6 -0
  7. package/src/lib/accepted-chat-kickoff.js +192 -0
  8. package/src/lib/agent-address.js +46 -0
  9. package/src/lib/agent-profile.js +69 -0
  10. package/src/lib/http-auth.js +151 -0
  11. package/src/lib/policy.js +118 -0
  12. package/src/lib/runtime-errors.js +149 -0
  13. package/src/lib/runtime-guidance.js +458 -0
  14. package/src/openclaw/index.js +53 -0
  15. package/src/openclaw/installer/cli.js +349 -0
  16. package/src/openclaw/installer/constants.js +6 -0
  17. package/src/openclaw/installer/core.js +1548 -0
  18. package/src/openclaw/installer/doctor.js +690 -0
  19. package/src/openclaw/installer/workspace-contract.js +403 -0
  20. package/src/openclaw/plugin/account-identity.js +66 -0
  21. package/src/openclaw/plugin/claworld-channel-plugin.js +3118 -0
  22. package/src/openclaw/plugin/config-schema.js +464 -0
  23. package/src/openclaw/plugin/lifecycle.js +114 -0
  24. package/src/openclaw/plugin/managed-config.js +648 -0
  25. package/src/openclaw/plugin/onboarding.js +291 -0
  26. package/src/openclaw/plugin/register.js +961 -0
  27. package/src/openclaw/plugin/relay-client.js +783 -0
  28. package/src/openclaw/plugin/runtime.js +12 -0
  29. package/src/openclaw/protocol/relay-event-protocol.js +31 -0
  30. package/src/openclaw/runtime/canonical-result-builder.js +116 -0
  31. package/src/openclaw/runtime/demo-session-bootstrap.js +37 -0
  32. package/src/openclaw/runtime/feedback-helper.js +145 -0
  33. package/src/openclaw/runtime/inbound-session-router.js +36 -0
  34. package/src/openclaw/runtime/outbound-session-bridge.js +17 -0
  35. package/src/openclaw/runtime/product-shell-helper.js +1712 -0
  36. package/src/openclaw/runtime/runtime-path.js +19 -0
  37. package/src/openclaw/runtime/system-message-orchestrator.js +1 -0
  38. package/src/openclaw/runtime/tool-contracts.js +714 -0
  39. package/src/openclaw/runtime/tool-inventory.js +92 -0
  40. package/src/openclaw/runtime/world-moderation-helper.js +415 -0
  41. package/src/openclaw/runtime/world-session-startup.js +1 -0
  42. package/src/product-shell/catalog/default-world-catalog.js +296 -0
  43. package/src/product-shell/contracts/candidate-feed.js +330 -0
  44. package/src/product-shell/contracts/chat-request-approval-policy.js +98 -0
  45. package/src/product-shell/contracts/world-manifest.js +435 -0
  46. package/src/product-shell/contracts/world-orchestration.js +1024 -0
  47. package/src/product-shell/feedback/feedback-contract.js +13 -0
  48. package/src/product-shell/feedback/feedback-routes.js +98 -0
  49. package/src/product-shell/feedback/feedback-service.js +254 -0
  50. package/src/product-shell/index.js +163 -0
  51. package/src/product-shell/matching/matchmaking-service.js +340 -0
  52. package/src/product-shell/membership/membership-service.js +277 -0
  53. package/src/product-shell/onboarding/onboarding-routes.js +37 -0
  54. package/src/product-shell/onboarding/onboarding-service.js +230 -0
  55. package/src/product-shell/orchestration/session-orchestrator.js +38 -0
  56. package/src/product-shell/results/result-service.js +15 -0
  57. package/src/product-shell/search/search-service.js +359 -0
  58. package/src/product-shell/social/chat-request-approval-policy.js +332 -0
  59. package/src/product-shell/social/chat-request-routes.js +108 -0
  60. package/src/product-shell/social/chat-request-service.js +632 -0
  61. package/src/product-shell/social/friend-routes.js +82 -0
  62. package/src/product-shell/social/friend-service.js +560 -0
  63. package/src/product-shell/social/social-routes.js +21 -0
  64. package/src/product-shell/social/social-service.js +140 -0
  65. package/src/product-shell/worlds/world-admin-service.js +705 -0
  66. package/src/product-shell/worlds/world-authorization.js +135 -0
  67. package/src/product-shell/worlds/world-broadcast-service.js +299 -0
  68. package/src/product-shell/worlds/world-routes.js +410 -0
  69. package/src/product-shell/worlds/world-service.js +89 -0
@@ -0,0 +1,135 @@
1
+ function normalizeText(value, fallback = null) {
2
+ if (value == null) return fallback;
3
+ const normalized = String(value).trim();
4
+ return normalized || fallback;
5
+ }
6
+
7
+ export const WORLD_ROLES = Object.freeze({
8
+ OWNER: 'owner',
9
+ ADMIN: 'admin',
10
+ MEMBER: 'member',
11
+ NONE: 'none',
12
+ });
13
+
14
+ export const WORLD_ACTIONS = Object.freeze({
15
+ VIEW_MANAGEMENT: 'view_world_management',
16
+ MANAGE_WORLD: 'manage_world',
17
+ MANAGE_WORLD_ROLES: 'manage_world_roles',
18
+ CHANGE_ENABLED_STATE: 'change_world_enabled_state',
19
+ BROADCAST: 'broadcast_world',
20
+ SEARCH: 'search_world',
21
+ VIEW_CANDIDATE_FEED: 'view_world_candidate_feed',
22
+ CREATE_CHAT_REQUEST: 'create_world_chat_request',
23
+ });
24
+
25
+ function resolveWorldRole({ isOwner = false, isAdmin = false, isMember = false } = {}) {
26
+ if (isOwner) return WORLD_ROLES.OWNER;
27
+ if (isAdmin) return WORLD_ROLES.ADMIN;
28
+ if (isMember) return WORLD_ROLES.MEMBER;
29
+ return WORLD_ROLES.NONE;
30
+ }
31
+
32
+ function resolveActionRequirement(action) {
33
+ switch (action) {
34
+ case WORLD_ACTIONS.VIEW_MANAGEMENT:
35
+ case WORLD_ACTIONS.MANAGE_WORLD:
36
+ case WORLD_ACTIONS.BROADCAST:
37
+ return {
38
+ allowedRoles: [WORLD_ROLES.OWNER, WORLD_ROLES.ADMIN],
39
+ reason: 'management_role_required',
40
+ };
41
+ case WORLD_ACTIONS.MANAGE_WORLD_ROLES:
42
+ case WORLD_ACTIONS.CHANGE_ENABLED_STATE:
43
+ return {
44
+ allowedRoles: [WORLD_ROLES.OWNER],
45
+ reason: 'owner_role_required',
46
+ };
47
+ case WORLD_ACTIONS.SEARCH:
48
+ case WORLD_ACTIONS.VIEW_CANDIDATE_FEED:
49
+ case WORLD_ACTIONS.CREATE_CHAT_REQUEST:
50
+ return {
51
+ allowedRoles: [WORLD_ROLES.MEMBER],
52
+ reason: 'active_membership_required',
53
+ requiredMembershipStatus: 'active',
54
+ };
55
+ default:
56
+ return {
57
+ allowedRoles: [],
58
+ reason: 'unknown_action',
59
+ };
60
+ }
61
+ }
62
+
63
+ export function createWorldAuthorizationService({ worldService, membershipService } = {}) {
64
+ function resolveMembership(worldId, agentId, { includeDisabled = false } = {}) {
65
+ const normalizedAgentId = normalizeText(agentId, null);
66
+ if (!normalizedAgentId || !membershipService || typeof membershipService.getMembership !== 'function') {
67
+ return null;
68
+ }
69
+ return membershipService.getMembership({
70
+ worldId,
71
+ agentId: normalizedAgentId,
72
+ includeDisabled,
73
+ });
74
+ }
75
+
76
+ function resolveWorldActorContextForWorld(world, actorAgentId, { includeDisabled = false } = {}) {
77
+ const normalizedActorAgentId = normalizeText(actorAgentId, null);
78
+ const membership = normalizedActorAgentId
79
+ ? resolveMembership(world.worldId, normalizedActorAgentId, { includeDisabled })
80
+ : null;
81
+ const isOwner = normalizedActorAgentId != null && normalizedActorAgentId === world.creatorAgentId;
82
+ const isAdmin = normalizedActorAgentId != null
83
+ && Array.isArray(world.adminAgentIds)
84
+ && world.adminAgentIds.includes(normalizedActorAgentId);
85
+ const isMember = membership?.status === 'active';
86
+ const worldRole = resolveWorldRole({ isOwner, isAdmin, isMember });
87
+
88
+ return {
89
+ world,
90
+ actorAgentId: normalizedActorAgentId,
91
+ worldRole,
92
+ managementRole: isOwner ? WORLD_ROLES.OWNER : isAdmin ? WORLD_ROLES.ADMIN : null,
93
+ membership,
94
+ membershipStatus: membership?.status || null,
95
+ roles: {
96
+ owner: isOwner,
97
+ admin: isAdmin,
98
+ member: isMember,
99
+ },
100
+ };
101
+ }
102
+
103
+ return {
104
+ resolveWorldActorContext({ worldId, actorAgentId, includeDisabled = false } = {}) {
105
+ const world = worldService.requireWorld(worldId, { includeDisabled });
106
+ return resolveWorldActorContextForWorld(world, actorAgentId, { includeDisabled });
107
+ },
108
+
109
+ evaluateWorldAction({ worldId, actorAgentId, action, includeDisabled = false } = {}) {
110
+ const actor = this.resolveWorldActorContext({
111
+ worldId,
112
+ actorAgentId,
113
+ includeDisabled,
114
+ });
115
+ const requirement = resolveActionRequirement(action);
116
+ const allowed = requirement.allowedRoles.includes(actor.worldRole);
117
+
118
+ return {
119
+ ...actor,
120
+ action,
121
+ allowed,
122
+ allowedRoles: requirement.allowedRoles,
123
+ requiredMembershipStatus: requirement.requiredMembershipStatus || null,
124
+ reason: allowed ? 'authorized' : requirement.reason,
125
+ };
126
+ },
127
+
128
+ listManagedWorlds({ actorAgentId, includeDisabled = true } = {}) {
129
+ return worldService
130
+ .listCustomWorlds({ includeDisabled })
131
+ .map((world) => resolveWorldActorContextForWorld(world, actorAgentId, { includeDisabled }))
132
+ .filter((context) => context.managementRole != null);
133
+ },
134
+ };
135
+ }
@@ -0,0 +1,299 @@
1
+ import { v4 as uuidv4 } from 'uuid';
2
+ import { WORLD_ACTIONS } from './world-authorization.js';
3
+
4
+ function normalizeText(value, fallback = null) {
5
+ if (value == null) return fallback;
6
+ const normalized = String(value).trim();
7
+ return normalized || fallback;
8
+ }
9
+
10
+ function cloneJsonObject(value) {
11
+ if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
12
+ try {
13
+ const cloned = JSON.parse(JSON.stringify(value));
14
+ if (!cloned || typeof cloned !== 'object' || Array.isArray(cloned)) return null;
15
+ return cloned;
16
+ } catch {
17
+ return null;
18
+ }
19
+ }
20
+
21
+ function normalizeBoolean(value, fallback = null) {
22
+ if (typeof value === 'boolean') return value;
23
+ return fallback;
24
+ }
25
+
26
+ function normalizeWorldEligibility(value, fallback = 'active') {
27
+ const normalized = normalizeText(value, fallback);
28
+ if (normalized === 'joined') return 'joined';
29
+ return 'active';
30
+ }
31
+
32
+ function normalizeBroadcastAudience(value, fallback = 'members') {
33
+ const normalized = normalizeText(value, fallback);
34
+ if (normalized === 'admins') return 'admins';
35
+ if (normalized === 'admins_and_owner') return 'admins_and_owner';
36
+ return 'members';
37
+ }
38
+
39
+ function createBroadcastId() {
40
+ return `brd_${uuidv4()}`;
41
+ }
42
+
43
+ function createConfigurationError() {
44
+ const error = new Error('world_broadcast_unavailable');
45
+ error.code = 'world_broadcast_unavailable';
46
+ error.status = 500;
47
+ return error;
48
+ }
49
+
50
+ function createInvalidBroadcastRequestError(fieldId, message = `${fieldId} is required`) {
51
+ const error = new Error(`invalid_broadcast_request:${fieldId}`);
52
+ error.code = 'invalid_broadcast_request';
53
+ error.status = 400;
54
+ error.responseBody = {
55
+ error: error.code,
56
+ message: 'broadcast request is invalid',
57
+ fieldErrors: [
58
+ {
59
+ fieldId,
60
+ message,
61
+ },
62
+ ],
63
+ };
64
+ return error;
65
+ }
66
+
67
+ function createBroadcastDisabledError(worldId) {
68
+ const error = new Error(`broadcast_disabled:${worldId}`);
69
+ error.code = 'broadcast_disabled';
70
+ error.status = 403;
71
+ error.responseBody = {
72
+ error: error.code,
73
+ message: 'broadcast is not enabled for this world',
74
+ worldId,
75
+ };
76
+ return error;
77
+ }
78
+
79
+ function createBroadcastNotAllowedError({ worldId, senderAgentId, senderRole } = {}) {
80
+ const error = new Error(`broadcast_not_allowed:${worldId}:${senderAgentId}`);
81
+ error.code = 'broadcast_not_allowed';
82
+ error.status = 403;
83
+ error.responseBody = {
84
+ error: error.code,
85
+ message: 'sender does not have permission to broadcast in this world',
86
+ worldId,
87
+ senderAgentId,
88
+ senderRole,
89
+ allowedRoles: ['owner', 'admin'],
90
+ };
91
+ return error;
92
+ }
93
+
94
+ function resolveMemberStatuses(world = {}) {
95
+ const eligibility = normalizeWorldEligibility(world.eligibility, 'active');
96
+ return eligibility === 'joined' ? new Set(['joined', 'active']) : new Set(['active']);
97
+ }
98
+
99
+ function dedupeAgentIds(agentIds = [], { excludeAgentId = null } = {}) {
100
+ const seen = new Set();
101
+ const items = [];
102
+ for (const rawAgentId of agentIds) {
103
+ const agentId = normalizeText(rawAgentId, null);
104
+ if (!agentId || seen.has(agentId)) continue;
105
+ if (excludeAgentId && agentId === excludeAgentId) continue;
106
+ seen.add(agentId);
107
+ items.push(agentId);
108
+ }
109
+ return items;
110
+ }
111
+
112
+ function normalizeBroadcastCreationError(error, { agentId = null } = {}) {
113
+ const responseBody = error?.responseBody && typeof error.responseBody === 'object'
114
+ ? error.responseBody
115
+ : null;
116
+ return {
117
+ agentId,
118
+ status: 'failed',
119
+ httpStatus: Number.isInteger(error?.status) ? error.status : 500,
120
+ error: normalizeText(responseBody?.error, normalizeText(error?.code, 'chat_request_create_failed')),
121
+ reason: normalizeText(responseBody?.reason, null),
122
+ message: normalizeText(responseBody?.message, normalizeText(error?.message, null)),
123
+ };
124
+ }
125
+
126
+ export function createWorldBroadcastService({
127
+ worldService,
128
+ worldAuthorizationService,
129
+ membershipService,
130
+ chatRequestService = null,
131
+ store = null,
132
+ } = {}) {
133
+ function assertRuntime() {
134
+ if (!worldService || !membershipService || !chatRequestService || !store || typeof chatRequestService.createChatRequest !== 'function') {
135
+ throw createConfigurationError();
136
+ }
137
+ }
138
+
139
+ function resolveAudienceAgentIds(world, { audience, excludeSelf, senderAgentId } = {}) {
140
+ if (audience === 'admins' || audience === 'admins_and_owner') {
141
+ return dedupeAgentIds([
142
+ world.creatorAgentId,
143
+ ...(Array.isArray(world.adminAgentIds) ? world.adminAgentIds : []),
144
+ ], {
145
+ excludeAgentId: excludeSelf ? senderAgentId : null,
146
+ });
147
+ }
148
+
149
+ const memberStatuses = resolveMemberStatuses(world);
150
+ const memberAgentIds = membershipService
151
+ .listMemberships({ worldId: world.worldId })
152
+ .filter((membership) => memberStatuses.has(membership.status))
153
+ .map((membership) => membership.agentId);
154
+
155
+ return dedupeAgentIds(memberAgentIds, {
156
+ excludeAgentId: excludeSelf ? senderAgentId : null,
157
+ });
158
+ }
159
+
160
+ return {
161
+ async broadcastWorld({
162
+ worldId,
163
+ senderAgentId,
164
+ payload,
165
+ audience = null,
166
+ excludeSelf = null,
167
+ } = {}) {
168
+ assertRuntime();
169
+ const world = worldService.requireWorld(worldId);
170
+ const sender = store.getAgent(senderAgentId);
171
+
172
+ if (!sender) {
173
+ throw createInvalidBroadcastRequestError('agentId', 'agentId is required');
174
+ }
175
+ if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
176
+ throw createInvalidBroadcastRequestError('payload', 'payload must be an object');
177
+ }
178
+ const openingMessage = normalizeText(payload.text, null);
179
+ if (!openingMessage) {
180
+ throw createInvalidBroadcastRequestError('payload.text', 'payload.text is required');
181
+ }
182
+
183
+ const worldBroadcast = world.broadcast && typeof world.broadcast === 'object'
184
+ ? world.broadcast
185
+ : {};
186
+ if (worldBroadcast.enabled !== true) {
187
+ throw createBroadcastDisabledError(world.worldId);
188
+ }
189
+
190
+ const authorization = worldAuthorizationService.evaluateWorldAction({
191
+ worldId: world.worldId,
192
+ actorAgentId: senderAgentId,
193
+ action: WORLD_ACTIONS.BROADCAST,
194
+ });
195
+ const senderRole = authorization.worldRole;
196
+ if (!authorization.allowed) {
197
+ throw createBroadcastNotAllowedError({
198
+ worldId: world.worldId,
199
+ senderAgentId,
200
+ senderRole,
201
+ });
202
+ }
203
+
204
+ const effectiveAudience = normalizeBroadcastAudience(audience, worldBroadcast.audience || 'members');
205
+ const effectiveExcludeSelf = normalizeBoolean(excludeSelf, worldBroadcast.excludeSelf !== false) !== false;
206
+ const effectiveEligibility = normalizeWorldEligibility(world.eligibility, 'active');
207
+ const targetAgentIds = resolveAudienceAgentIds(world, {
208
+ audience: effectiveAudience,
209
+ excludeSelf: effectiveExcludeSelf,
210
+ senderAgentId,
211
+ });
212
+ const broadcastId = createBroadcastId();
213
+ const openingPayload = cloneJsonObject(payload) || { text: openingMessage };
214
+
215
+ const requests = [];
216
+ const failures = [];
217
+ for (const targetAgentId of targetAgentIds) {
218
+ const targetAgent = store.getAgent(targetAgentId);
219
+ if (!targetAgent?.address) {
220
+ failures.push({
221
+ agentId: targetAgentId,
222
+ status: 'failed',
223
+ httpStatus: 404,
224
+ error: 'target_not_found',
225
+ reason: 'target_not_found',
226
+ message: 'target agent is missing a relay address',
227
+ });
228
+ continue;
229
+ }
230
+
231
+ try {
232
+ const created = await chatRequestService.createChatRequest({
233
+ fromAgentId: senderAgentId,
234
+ targetAgentId,
235
+ openingMessage,
236
+ openingPayload,
237
+ worldId: world.worldId,
238
+ origin: {
239
+ type: 'world_broadcast',
240
+ broadcastId,
241
+ },
242
+ broadcast: {
243
+ broadcastId,
244
+ worldId: world.worldId,
245
+ audience: effectiveAudience,
246
+ senderRole,
247
+ eligibility: effectiveEligibility,
248
+ excludeSelf: effectiveExcludeSelf,
249
+ },
250
+ source: 'world_broadcast',
251
+ });
252
+
253
+ requests.push({
254
+ agentId: targetAgentId,
255
+ status: created.status,
256
+ verdict: created.verdict,
257
+ chatRequest: created.chatRequest,
258
+ kickoff: created.kickoff || null,
259
+ });
260
+ } catch (error) {
261
+ failures.push(normalizeBroadcastCreationError(error, {
262
+ agentId: targetAgentId,
263
+ }));
264
+ }
265
+ }
266
+
267
+ const pendingCount = requests.filter((item) => item.chatRequest?.status === 'pending').length;
268
+ const autoAcceptedCount = requests.filter((item) => item.chatRequest?.status === 'accepted').length;
269
+ const rejectedCount = requests.filter((item) => item.chatRequest?.status === 'rejected').length;
270
+
271
+ return {
272
+ status: failures.length === 0 ? 'requests_created' : 'requests_created_with_failures',
273
+ worldId: world.worldId,
274
+ senderAgentId,
275
+ senderRole,
276
+ audience: effectiveAudience,
277
+ excludeSelf: effectiveExcludeSelf,
278
+ eligibility: effectiveEligibility,
279
+ broadcastId,
280
+ totalTargets: targetAgentIds.length,
281
+ createdCount: requests.length,
282
+ failedCount: failures.length,
283
+ pendingCount,
284
+ autoAcceptedCount,
285
+ rejectedCount,
286
+ requests,
287
+ failures,
288
+ nextAction:
289
+ pendingCount > 0
290
+ ? 'recipients_review_pending_requests'
291
+ : autoAcceptedCount > 0
292
+ ? 'backend_auto_accepted_requests'
293
+ : rejectedCount > 0
294
+ ? 'requests_rejected_by_policy'
295
+ : 'no_pending_requests_created',
296
+ };
297
+ },
298
+ };
299
+ }