@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,332 @@
1
+ import {
2
+ CHAT_REQUEST_APPROVAL_POLICY_SCHEMA_VERSION,
3
+ normalizeChatRequestApprovalPolicy,
4
+ normalizeChatRequestApprovalOriginType,
5
+ } from '../contracts/chat-request-approval-policy.js';
6
+
7
+ function normalizeText(value, fallback = null) {
8
+ if (value == null) return fallback;
9
+ const normalized = String(value).trim();
10
+ return normalized || fallback;
11
+ }
12
+
13
+ function normalizeWorldEligibility(value, fallback = 'active') {
14
+ const normalized = normalizeText(value, fallback);
15
+ return normalized === 'joined' ? 'joined' : 'active';
16
+ }
17
+
18
+ function sortByRecency(items = [], ...fieldNames) {
19
+ return items.slice().sort((left, right) => {
20
+ for (const fieldName of fieldNames) {
21
+ const rightTs = Date.parse(right?.[fieldName] || '');
22
+ const leftTs = Date.parse(left?.[fieldName] || '');
23
+ const normalizedRightTs = Number.isFinite(rightTs) ? rightTs : -Infinity;
24
+ const normalizedLeftTs = Number.isFinite(leftTs) ? leftTs : -Infinity;
25
+ if (normalizedRightTs !== normalizedLeftTs) return normalizedRightTs - normalizedLeftTs;
26
+ }
27
+ return 0;
28
+ });
29
+ }
30
+
31
+ function resolveChatRequestWorldId(request = {}) {
32
+ const requestContext = request.requestContext && typeof request.requestContext === 'object' && !Array.isArray(request.requestContext)
33
+ ? request.requestContext
34
+ : {};
35
+ const conversation = request.conversation && typeof request.conversation === 'object' && !Array.isArray(request.conversation)
36
+ ? request.conversation
37
+ : {};
38
+ return normalizeText(
39
+ conversation.worldId,
40
+ normalizeText(requestContext.conversation?.worldId, null),
41
+ );
42
+ }
43
+
44
+ function resolveChatRequestOriginType(request = {}) {
45
+ const directOrigin = request.origin && typeof request.origin === 'object' && !Array.isArray(request.origin)
46
+ ? request.origin
47
+ : {};
48
+ const requestContext = request.requestContext && typeof request.requestContext === 'object' && !Array.isArray(request.requestContext)
49
+ ? request.requestContext
50
+ : {};
51
+ const origin = requestContext.origin && typeof requestContext.origin === 'object' && !Array.isArray(requestContext.origin)
52
+ ? requestContext.origin
53
+ : {};
54
+ return normalizeChatRequestApprovalOriginType(
55
+ directOrigin.type || origin.type,
56
+ normalizeChatRequestApprovalOriginType(request.source, 'chat_request'),
57
+ );
58
+ }
59
+
60
+ function resolveAllowedMembershipStatuses(world = {}) {
61
+ const eligibility = normalizeWorldEligibility(world?.eligibility, 'active');
62
+ return eligibility === 'joined'
63
+ ? new Set(['joined', 'active'])
64
+ : new Set(['active']);
65
+ }
66
+
67
+ function resolveWorldApprovalFacts({ request = {}, store = null, worldService = null } = {}) {
68
+ const worldId = resolveChatRequestWorldId(request);
69
+ if (!worldId || !store) {
70
+ return {
71
+ worldId,
72
+ worldScoped: Boolean(worldId),
73
+ worldFound: false,
74
+ eligibility: null,
75
+ requesterMembershipStatus: null,
76
+ targetMembershipStatus: null,
77
+ validWorldContext: false,
78
+ };
79
+ }
80
+
81
+ const world = worldService?.getWorld?.(worldId) || null;
82
+ if (!world) {
83
+ return {
84
+ worldId,
85
+ worldScoped: true,
86
+ worldFound: false,
87
+ eligibility: null,
88
+ requesterMembershipStatus: null,
89
+ targetMembershipStatus: null,
90
+ validWorldContext: false,
91
+ };
92
+ }
93
+
94
+ const allowedStatuses = resolveAllowedMembershipStatuses(world);
95
+ const requesterMembership = store
96
+ .listMemberships({ worldId, agentId: request.fromAgentId })
97
+ .find((membership) => allowedStatuses.has(membership.status)) || null;
98
+ const targetMembership = store
99
+ .listMemberships({ worldId, agentId: request.toAgentId })
100
+ .find((membership) => allowedStatuses.has(membership.status)) || null;
101
+
102
+ return {
103
+ worldId,
104
+ worldScoped: true,
105
+ worldFound: true,
106
+ eligibility: normalizeWorldEligibility(world.eligibility, 'active'),
107
+ requesterMembershipStatus: requesterMembership?.status || null,
108
+ targetMembershipStatus: targetMembership?.status || null,
109
+ validWorldContext: Boolean(requesterMembership && targetMembership),
110
+ };
111
+ }
112
+
113
+ function resolveTrustedApprovalFacts({ request = {}, store = null } = {}) {
114
+ if (!store) {
115
+ return {
116
+ trusted: false,
117
+ trustSource: null,
118
+ friendshipId: null,
119
+ approvalGrantId: null,
120
+ };
121
+ }
122
+
123
+ const friendship = sortByRecency(
124
+ store.listFriendships({
125
+ agentId: request.toAgentId,
126
+ peerAgentId: request.fromAgentId,
127
+ status: 'active',
128
+ }),
129
+ 'updatedAt',
130
+ 'acceptedAt',
131
+ 'createdAt',
132
+ )[0] || null;
133
+ const approvalGrant = sortByRecency(
134
+ store.listApprovalGrants({
135
+ ownerAgentId: request.toAgentId,
136
+ peerAgentId: request.fromAgentId,
137
+ status: 'active',
138
+ }),
139
+ 'updatedAt',
140
+ 'createdAt',
141
+ )[0] || null;
142
+
143
+ return {
144
+ trusted: Boolean(friendship || approvalGrant),
145
+ trustSource: friendship ? 'friendship' : approvalGrant ? 'approval_grant' : null,
146
+ friendshipId: friendship?.friendshipId || null,
147
+ approvalGrantId: approvalGrant?.grantId || null,
148
+ };
149
+ }
150
+
151
+ function resolvePresenceState(agentId, { presence = null, store = null } = {}) {
152
+ const normalizedAgentId = normalizeText(agentId, null);
153
+ if (!normalizedAgentId) {
154
+ return {
155
+ online: false,
156
+ connectedAt: null,
157
+ lastHeartbeatAt: null,
158
+ };
159
+ }
160
+ if (presence?.getPresence) return presence.getPresence(normalizedAgentId);
161
+ if (store?.getPresence) return store.getPresence(normalizedAgentId);
162
+ return {
163
+ online: false,
164
+ connectedAt: null,
165
+ lastHeartbeatAt: null,
166
+ };
167
+ }
168
+
169
+ function resolvePolicyFreshness(policyRecord = null, { store = null, presence = null } = {}) {
170
+ if (!policyRecord) return { status: 'missing', fresh: false, reason: 'policy_missing' };
171
+ if (policyRecord.schemaVersion !== CHAT_REQUEST_APPROVAL_POLICY_SCHEMA_VERSION) {
172
+ return { status: 'stale', fresh: false, reason: 'policy_schema_mismatch' };
173
+ }
174
+ if (!normalizeText(policyRecord.syncedAt, null)) {
175
+ return { status: 'stale', fresh: false, reason: 'policy_missing_sync_timestamp' };
176
+ }
177
+ const targetAgentId = normalizeText(policyRecord.agentId, null);
178
+ if (!resolvePresenceState(targetAgentId, { presence, store }).online) {
179
+ return { status: 'stale', fresh: false, reason: 'policy_runtime_offline' };
180
+ }
181
+ return { status: 'active', fresh: true, reason: 'policy_active' };
182
+ }
183
+
184
+ export function createStoredChatRequestApprovalPolicy({
185
+ agentId,
186
+ policy = {},
187
+ syncedAt = null,
188
+ source = null,
189
+ credentialId = null,
190
+ } = {}) {
191
+ const normalizedSource = source && typeof source === 'object' && !Array.isArray(source)
192
+ ? {
193
+ channel: normalizeText(source.channel, null),
194
+ integration: normalizeText(source.integration, null),
195
+ accountId: normalizeText(source.accountId, null),
196
+ }
197
+ : {};
198
+
199
+ return {
200
+ agentId: normalizeText(agentId, null),
201
+ schemaVersion: CHAT_REQUEST_APPROVAL_POLICY_SCHEMA_VERSION,
202
+ policy: normalizeChatRequestApprovalPolicy(policy),
203
+ syncedAt: normalizeText(syncedAt, null),
204
+ credentialId: normalizeText(credentialId, null),
205
+ source: normalizedSource,
206
+ };
207
+ }
208
+
209
+ export function evaluateChatRequestApprovalPolicy({
210
+ policyRecord = null,
211
+ request = {},
212
+ store = null,
213
+ presence = null,
214
+ worldService = null,
215
+ } = {}) {
216
+ const freshness = resolvePolicyFreshness(policyRecord, { store, presence });
217
+ const policy = normalizeChatRequestApprovalPolicy(policyRecord?.policy || {});
218
+ const originType = resolveChatRequestOriginType(request);
219
+ const world = resolveWorldApprovalFacts({ request, store, worldService });
220
+ const trust = resolveTrustedApprovalFacts({ request, store });
221
+
222
+ if (!freshness.fresh) {
223
+ return {
224
+ verdict: 'pending',
225
+ reason: freshness.reason,
226
+ freshness,
227
+ policy,
228
+ facts: {
229
+ originType,
230
+ world,
231
+ trust,
232
+ },
233
+ };
234
+ }
235
+
236
+ if (policy.blocks.originTypes.includes(originType)) {
237
+ return {
238
+ verdict: 'reject',
239
+ reason: 'blocked_origin_type',
240
+ freshness,
241
+ policy,
242
+ facts: {
243
+ originType,
244
+ world,
245
+ trust,
246
+ },
247
+ };
248
+ }
249
+
250
+ if (world.worldId && policy.blocks.worldIds.includes(world.worldId)) {
251
+ return {
252
+ verdict: 'reject',
253
+ reason: 'blocked_world',
254
+ freshness,
255
+ policy,
256
+ facts: {
257
+ originType,
258
+ world,
259
+ trust,
260
+ },
261
+ };
262
+ }
263
+
264
+ switch (policy.mode) {
265
+ case 'open':
266
+ return {
267
+ verdict: 'auto_accept',
268
+ reason: 'open_mode',
269
+ freshness,
270
+ policy,
271
+ facts: {
272
+ originType,
273
+ world,
274
+ trust,
275
+ },
276
+ };
277
+ case 'world_only':
278
+ return {
279
+ verdict: world.validWorldContext ? 'auto_accept' : 'pending',
280
+ reason: world.validWorldContext ? 'world_context_allowed' : 'world_context_required',
281
+ freshness,
282
+ policy,
283
+ facts: {
284
+ originType,
285
+ world,
286
+ trust,
287
+ },
288
+ };
289
+ case 'trusted_only':
290
+ return {
291
+ verdict: trust.trusted ? 'auto_accept' : 'pending',
292
+ reason: trust.trusted ? 'trusted_relationship_allowed' : 'trusted_relationship_required',
293
+ freshness,
294
+ policy,
295
+ facts: {
296
+ originType,
297
+ world,
298
+ trust,
299
+ },
300
+ };
301
+ case 'trusted_or_world':
302
+ return {
303
+ verdict: trust.trusted || world.validWorldContext ? 'auto_accept' : 'pending',
304
+ reason:
305
+ trust.trusted
306
+ ? 'trusted_relationship_allowed'
307
+ : world.validWorldContext
308
+ ? 'world_context_allowed'
309
+ : 'trusted_relationship_or_world_context_required',
310
+ freshness,
311
+ policy,
312
+ facts: {
313
+ originType,
314
+ world,
315
+ trust,
316
+ },
317
+ };
318
+ case 'manual_review':
319
+ default:
320
+ return {
321
+ verdict: 'pending',
322
+ reason: 'manual_review_mode',
323
+ freshness,
324
+ policy,
325
+ facts: {
326
+ originType,
327
+ world,
328
+ trust,
329
+ },
330
+ };
331
+ }
332
+ }
@@ -0,0 +1,108 @@
1
+ import { resolveAuthenticatedAgentId } from '../../lib/http-auth.js';
2
+
3
+ function sendChatRequestError(res, error) {
4
+ const status = Number.isInteger(error?.status) ? error.status : 500;
5
+ if (error?.responseBody && typeof error.responseBody === 'object') {
6
+ return res.status(status).json(error.responseBody);
7
+ }
8
+ const code = typeof error?.code === 'string' ? error.code : 'internal_error';
9
+ return res.status(status).json({ error: code, message: error?.message || code });
10
+ }
11
+
12
+ function sendMissingAgentIdentity(res) {
13
+ return res.status(401).json({
14
+ error: 'not_authenticated',
15
+ reason: 'agent_identity_required',
16
+ });
17
+ }
18
+
19
+ export function registerChatRequestRoutes(app, { chatRequestService, store }) {
20
+ app.put('/v1/chat-requests/approval-policy', async (req, res) => {
21
+ const authAgent = resolveAuthenticatedAgentId({
22
+ store,
23
+ req,
24
+ providedAgentId: req.body?.agentId,
25
+ fieldName: 'agentId',
26
+ });
27
+ if (!authAgent.ok) return res.status(authAgent.status).json(authAgent.body);
28
+ if (!authAgent.agentId) return sendMissingAgentIdentity(res);
29
+
30
+ try {
31
+ const result = await chatRequestService.syncApprovalPolicy({
32
+ actorAgentId: authAgent.agentId,
33
+ credentialId: authAgent.auth?.credential?.credentialId || null,
34
+ accountId: req.body?.accountId,
35
+ approval: req.body?.approval,
36
+ });
37
+ res.json(result);
38
+ } catch (error) {
39
+ sendChatRequestError(res, error);
40
+ }
41
+ });
42
+
43
+ app.post('/v1/chat-requests', async (req, res) => {
44
+ const authAgent = resolveAuthenticatedAgentId({
45
+ store,
46
+ req,
47
+ providedAgentId: req.body?.fromAgentId,
48
+ fieldName: 'fromAgentId',
49
+ });
50
+ if (!authAgent.ok) return res.status(authAgent.status).json(authAgent.body);
51
+ if (!authAgent.agentId) return sendMissingAgentIdentity(res);
52
+
53
+ try {
54
+ const result = await chatRequestService.createChatRequest({
55
+ fromAgentId: authAgent.agentId,
56
+ targetAgentId: req.body?.targetAgentId,
57
+ kickoffBrief: req.body?.kickoffBrief,
58
+ openingMessage: req.body?.openingMessage,
59
+ worldId: req.body?.worldId,
60
+ episodePolicy: req.body?.episodePolicy,
61
+ });
62
+ res.status(201).json(result);
63
+ } catch (error) {
64
+ sendChatRequestError(res, error);
65
+ }
66
+ });
67
+
68
+ app.get('/v1/chat-requests', (req, res) => {
69
+ const authAgent = resolveAuthenticatedAgentId({
70
+ store,
71
+ req,
72
+ providedAgentId: req.query.agentId,
73
+ fieldName: 'agentId',
74
+ });
75
+ if (!authAgent.ok) return res.status(authAgent.status).json(authAgent.body);
76
+ if (!authAgent.agentId) return sendMissingAgentIdentity(res);
77
+
78
+ try {
79
+ const result = chatRequestService.listChatRequests({
80
+ agentId: authAgent.agentId,
81
+ direction: req.query.direction,
82
+ });
83
+ res.json(result);
84
+ } catch (error) {
85
+ sendChatRequestError(res, error);
86
+ }
87
+ });
88
+
89
+ app.post('/v1/chat-requests/:chatRequestId/accept', async (req, res) => {
90
+ const authAgent = resolveAuthenticatedAgentId({
91
+ store,
92
+ req,
93
+ providedAgentId: req.body?.actorAgentId,
94
+ fieldName: 'actorAgentId',
95
+ });
96
+ if (!authAgent.ok) return res.status(authAgent.status).json(authAgent.body);
97
+ if (!authAgent.agentId) return sendMissingAgentIdentity(res);
98
+
99
+ try {
100
+ const result = await chatRequestService.acceptChatRequest(req.params.chatRequestId, {
101
+ actorAgentId: authAgent.agentId,
102
+ });
103
+ res.json(result);
104
+ } catch (error) {
105
+ sendChatRequestError(res, error);
106
+ }
107
+ });
108
+ }