@celljs/ta-org 0.0.0-stage → 4.53.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.
- package/README.en.md +17 -0
- package/README.md +16 -2
- package/extension/index.mjs +465 -0
- package/host/index.mjs +1403 -0
- package/package.json +60 -3
- package/web/index.js +1895 -0
package/host/index.mjs
ADDED
|
@@ -0,0 +1,1403 @@
|
|
|
1
|
+
// ../ta-protocol/esm/common/language.js
|
|
2
|
+
var TaLanguageUtil;
|
|
3
|
+
(function(TaLanguageUtil2) {
|
|
4
|
+
TaLanguageUtil2.ALL = ["zh-CN", "zh-TW", "en-US", "ja-JP"];
|
|
5
|
+
TaLanguageUtil2.NAMES = { "zh-CN": "简体中文", "zh-TW": "繁體中文", "en-US": "English", "ja-JP": "日本語" };
|
|
6
|
+
function of(tag) {
|
|
7
|
+
const said = (tag !== null && tag !== undefined ? tag : "").toLowerCase().replace(/_/g, "-");
|
|
8
|
+
if (/^zh(\b|-)/.test(said)) {
|
|
9
|
+
return /-hant\b/.test(said) || !/-hans\b/.test(said) && /-(tw|hk|mo)\b/.test(said) ? "zh-TW" : "zh-CN";
|
|
10
|
+
}
|
|
11
|
+
return /^ja(\b|-)/.test(said) ? "ja-JP" : "en-US";
|
|
12
|
+
}
|
|
13
|
+
TaLanguageUtil2.of = of;
|
|
14
|
+
function server(env = typeof process === "undefined" ? {} : process.env) {
|
|
15
|
+
return of(env.TA_LANGUAGE || env.LC_ALL || env.LC_MESSAGES || env.LANG);
|
|
16
|
+
}
|
|
17
|
+
TaLanguageUtil2.server = server;
|
|
18
|
+
function pick(language, words) {
|
|
19
|
+
return words[of(language)];
|
|
20
|
+
}
|
|
21
|
+
TaLanguageUtil2.pick = pick;
|
|
22
|
+
function say(words) {
|
|
23
|
+
return words[server()];
|
|
24
|
+
}
|
|
25
|
+
TaLanguageUtil2.say = say;
|
|
26
|
+
function chinese(language) {
|
|
27
|
+
return of(language).startsWith("zh");
|
|
28
|
+
}
|
|
29
|
+
TaLanguageUtil2.chinese = chinese;
|
|
30
|
+
function cjk(language) {
|
|
31
|
+
return of(language) !== "en-US";
|
|
32
|
+
}
|
|
33
|
+
TaLanguageUtil2.cjk = cjk;
|
|
34
|
+
function list(language, items) {
|
|
35
|
+
return items.join(cjk(language) ? "、" : ", ");
|
|
36
|
+
}
|
|
37
|
+
TaLanguageUtil2.list = list;
|
|
38
|
+
})(TaLanguageUtil || (TaLanguageUtil = {}));
|
|
39
|
+
|
|
40
|
+
// web-src/org-protocol.ts
|
|
41
|
+
var NAME = "@celljs/ta-org";
|
|
42
|
+
var VIEW = "@celljs/ta-org/org";
|
|
43
|
+
var SECTION = "ta-org";
|
|
44
|
+
var ORG_KIND = "ta-org";
|
|
45
|
+
var ROOT_DEPT = "all";
|
|
46
|
+
var ORG_MAX_DEPTS = 60;
|
|
47
|
+
var DEPT_ID = /^[a-z0-9][a-z0-9-]{0,23}$/;
|
|
48
|
+
var ORG_KEY = "org:";
|
|
49
|
+
var PROFILE_KEY = "profile:";
|
|
50
|
+
var AGENT_KEY = "agent:";
|
|
51
|
+
var PULL_KEY = "pull:";
|
|
52
|
+
var PULL_ANSWER_KEY = "pull-ok:";
|
|
53
|
+
var WRAP_KEY = "wrap:";
|
|
54
|
+
var LINK_KEY = "link:";
|
|
55
|
+
var LISTING_KEY = "listings";
|
|
56
|
+
var OrgUtil;
|
|
57
|
+
((OrgUtil) => {
|
|
58
|
+
function deptId(value) {
|
|
59
|
+
const id = typeof value === "string" ? value.trim().toLowerCase() : "";
|
|
60
|
+
return DEPT_ID.test(id) && id !== ROOT_DEPT ? id : undefined;
|
|
61
|
+
}
|
|
62
|
+
OrgUtil.deptId = deptId;
|
|
63
|
+
function doc(value, now) {
|
|
64
|
+
const doc2 = value;
|
|
65
|
+
if (doc2?.v !== 1 || !Array.isArray(doc2.depts) || !Array.isArray(doc2.places) || typeof doc2.at !== "number") {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const ids = new Set([ROOT_DEPT]);
|
|
69
|
+
const depts = [{ id: ROOT_DEPT, name: "", parent: "", order: 0, leads: [] }];
|
|
70
|
+
for (const each of doc2.depts.slice(0, ORG_MAX_DEPTS)) {
|
|
71
|
+
const id = each?.id === ROOT_DEPT ? undefined : deptId(each?.id);
|
|
72
|
+
const name = typeof each?.name === "string" ? each.name.trim().slice(0, 40) : "";
|
|
73
|
+
if (!id || !name || ids.has(id)) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
ids.add(id);
|
|
77
|
+
depts.push({
|
|
78
|
+
id,
|
|
79
|
+
name,
|
|
80
|
+
parent: typeof each.parent === "string" && ids.has(each.parent) ? each.parent : ROOT_DEPT,
|
|
81
|
+
order: Number.isFinite(each.order) ? Number(each.order) : depts.length,
|
|
82
|
+
leads: Array.isArray(each.leads) ? [...new Set(each.leads.filter((lead) => typeof lead === "string"))].slice(0, 20) : []
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
const places = [];
|
|
86
|
+
const seen = new Set;
|
|
87
|
+
for (const each of doc2.places.slice(0, 500)) {
|
|
88
|
+
const member = typeof each?.member === "string" ? each.member : "";
|
|
89
|
+
if (!member || seen.has(member)) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
seen.add(member);
|
|
93
|
+
const deptsIn = Array.isArray(each.depts) ? [...new Set(each.depts.filter((dept) => ids.has(dept)))].slice(0, 8) : [];
|
|
94
|
+
const manager = each.manager && typeof each.manager.name === "string" ? { ...typeof each.manager.id === "string" ? { id: each.manager.id } : {}, name: each.manager.name.slice(0, 40) } : undefined;
|
|
95
|
+
places.push({
|
|
96
|
+
member,
|
|
97
|
+
depts: deptsIn.length ? deptsIn : [ROOT_DEPT],
|
|
98
|
+
...manager ? { manager } : {},
|
|
99
|
+
at: typeof each.at === "number" ? each.at : now
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
v: 1,
|
|
104
|
+
...typeof doc2.name === "string" && doc2.name.trim() ? { name: doc2.name.trim().slice(0, 60) } : {},
|
|
105
|
+
...typeof doc2.about === "string" && doc2.about.trim() ? { about: doc2.about.trim().slice(0, 300) } : {},
|
|
106
|
+
depts,
|
|
107
|
+
places,
|
|
108
|
+
rules: OrgUtil.rules(doc2.rules),
|
|
109
|
+
at: doc2.at
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
OrgUtil.doc = doc;
|
|
113
|
+
function rules(value) {
|
|
114
|
+
const rules2 = value ?? {};
|
|
115
|
+
return {
|
|
116
|
+
...rules2.ask === false ? {} : { ask: true },
|
|
117
|
+
...rules2.review === false ? {} : { review: true },
|
|
118
|
+
...rules2.see === "dept" || rules2.see === "self" ? { see: rules2.see } : {},
|
|
119
|
+
...rules2.pin === true ? { pin: true } : {}
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
OrgUtil.rules = rules;
|
|
123
|
+
function latest(docs, admin, members) {
|
|
124
|
+
const order = new Map(members.map((each, index) => [each.member, index]));
|
|
125
|
+
return docs.filter((each) => admin(members.find((member) => member.member === each.by) ?? { member: "" })).sort((a, b) => b.at - a.at || (order.get(b.by) ?? -1) - (order.get(a.by) ?? -1))[0];
|
|
126
|
+
}
|
|
127
|
+
OrgUtil.latest = latest;
|
|
128
|
+
function fold(docs, members) {
|
|
129
|
+
if (!docs.length) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const order = new Map(members.map((each, index) => [each.member, index]));
|
|
133
|
+
const sorted = [...docs].sort((a, b) => a.at - b.at || (order.get(a.by) ?? -1) - (order.get(b.by) ?? -1));
|
|
134
|
+
let out = sorted[0].body;
|
|
135
|
+
for (const { body } of sorted.slice(1)) {
|
|
136
|
+
const depts = new Map(out.depts.map((dept) => [dept.id, { dept, at: out.at }]));
|
|
137
|
+
for (const dept of body.depts) {
|
|
138
|
+
depts.set(dept.id, { dept, at: body.at });
|
|
139
|
+
}
|
|
140
|
+
const places = new Map(out.places.map((place) => [place.member, place]));
|
|
141
|
+
for (const place of body.places) {
|
|
142
|
+
const known = places.get(place.member);
|
|
143
|
+
if (!known || place.at >= known.at) {
|
|
144
|
+
places.set(place.member, place);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
out = {
|
|
148
|
+
v: 1,
|
|
149
|
+
...body.name !== undefined ? { name: body.name } : out.name !== undefined ? { name: out.name } : {},
|
|
150
|
+
...body.about !== undefined ? { about: body.about } : out.about !== undefined ? { about: out.about } : {},
|
|
151
|
+
depts: [...depts.values()].map(({ dept }) => dept),
|
|
152
|
+
places: [...places.values()],
|
|
153
|
+
rules: body.rules,
|
|
154
|
+
at: Math.max(out.at, body.at)
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
OrgUtil.fold = fold;
|
|
160
|
+
function nest(depts, id, above = false) {
|
|
161
|
+
const under = new Set([id]);
|
|
162
|
+
let grew = true;
|
|
163
|
+
while (grew) {
|
|
164
|
+
grew = false;
|
|
165
|
+
for (const dept of depts) {
|
|
166
|
+
if (!under.has(dept.id) && under.has(dept.parent)) {
|
|
167
|
+
under.add(dept.id);
|
|
168
|
+
grew = true;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (!above) {
|
|
173
|
+
return [...under];
|
|
174
|
+
}
|
|
175
|
+
const all = new Set(under);
|
|
176
|
+
let parent = depts.find((dept) => dept.id === id)?.parent;
|
|
177
|
+
while (parent && !all.has(parent)) {
|
|
178
|
+
all.add(parent);
|
|
179
|
+
parent = depts.find((dept) => dept.id === parent)?.parent;
|
|
180
|
+
}
|
|
181
|
+
return [...all];
|
|
182
|
+
}
|
|
183
|
+
OrgUtil.nest = nest;
|
|
184
|
+
function sees(doc2, member) {
|
|
185
|
+
const see = doc2.rules.see ?? "all";
|
|
186
|
+
if (see === "all") {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
const place = doc2.places.find((each) => each.member === member);
|
|
190
|
+
const mine = place?.depts ?? [ROOT_DEPT];
|
|
191
|
+
return new Set(see === "self" ? mine : mine.flatMap((dept) => [...OrgUtil.nest(doc2.depts, dept), ...OrgUtil.nest(doc2.depts, dept, true)]));
|
|
192
|
+
}
|
|
193
|
+
OrgUtil.sees = sees;
|
|
194
|
+
function leads(doc2, member) {
|
|
195
|
+
const found = new Set;
|
|
196
|
+
for (const dept of doc2.places.find((each) => each.member === member)?.depts ?? [ROOT_DEPT]) {
|
|
197
|
+
for (const id of OrgUtil.nest(doc2.depts, dept, true)) {
|
|
198
|
+
for (const lead of doc2.depts.find((each) => each.id === id)?.leads ?? []) {
|
|
199
|
+
if (lead !== member) {
|
|
200
|
+
found.add(lead);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return [...found];
|
|
206
|
+
}
|
|
207
|
+
OrgUtil.leads = leads;
|
|
208
|
+
function opens(scope2, me, depts) {
|
|
209
|
+
if (scope2.kind === "all") {
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
if (scope2.kind === "people") {
|
|
213
|
+
return scope2.people.includes(me);
|
|
214
|
+
}
|
|
215
|
+
return depts.some((dept) => scope2.depts.includes(dept));
|
|
216
|
+
}
|
|
217
|
+
OrgUtil.opens = opens;
|
|
218
|
+
function scope(scope2) {
|
|
219
|
+
const value = scope2;
|
|
220
|
+
if (value?.kind === "depts" && Array.isArray(value.depts)) {
|
|
221
|
+
return { kind: "depts", depts: value.depts.filter((dept) => typeof dept === "string").slice(0, ORG_MAX_DEPTS) };
|
|
222
|
+
}
|
|
223
|
+
if (value?.kind === "people" && Array.isArray(value.people)) {
|
|
224
|
+
return { kind: "people", people: value.people.filter((person) => typeof person === "string").slice(0, 200) };
|
|
225
|
+
}
|
|
226
|
+
return { kind: "all" };
|
|
227
|
+
}
|
|
228
|
+
OrgUtil.scope = scope;
|
|
229
|
+
function card(value, now) {
|
|
230
|
+
const card2 = value;
|
|
231
|
+
const id = typeof card2?.id === "string" ? card2.id.trim() : "";
|
|
232
|
+
const name = typeof card2?.name === "string" ? card2.name.trim().slice(0, 60) : "";
|
|
233
|
+
if (!card2 || !id || !name) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
id,
|
|
238
|
+
name,
|
|
239
|
+
...typeof card2.about === "string" && card2.about.trim() ? { about: card2.about.trim().slice(0, 300) } : {},
|
|
240
|
+
scope: OrgUtil.scope(card2.scope),
|
|
241
|
+
at: typeof card2.at === "number" ? card2.at : now
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
OrgUtil.card = card;
|
|
245
|
+
function roleOf(room) {
|
|
246
|
+
if (room?.status !== "member") {
|
|
247
|
+
return "none";
|
|
248
|
+
}
|
|
249
|
+
return room.owner ? "owner" : room.admin ? "admin" : "member";
|
|
250
|
+
}
|
|
251
|
+
OrgUtil.roleOf = roleOf;
|
|
252
|
+
function manages(room) {
|
|
253
|
+
return !!room && (room.owner || room.admin === true);
|
|
254
|
+
}
|
|
255
|
+
OrgUtil.manages = manages;
|
|
256
|
+
})(OrgUtil ||= {});
|
|
257
|
+
|
|
258
|
+
// web-src/org-crypto.ts
|
|
259
|
+
var CURVE = { name: "ECDH", namedCurve: "P-256" };
|
|
260
|
+
function base64(bytes) {
|
|
261
|
+
let text = "";
|
|
262
|
+
for (const byte of new Uint8Array(bytes)) {
|
|
263
|
+
text += String.fromCharCode(byte);
|
|
264
|
+
}
|
|
265
|
+
return btoa(text);
|
|
266
|
+
}
|
|
267
|
+
function bytes(text) {
|
|
268
|
+
const binary = atob(text);
|
|
269
|
+
const out = new Uint8Array(binary.length);
|
|
270
|
+
for (let i = 0;i < binary.length; i++) {
|
|
271
|
+
out[i] = binary.charCodeAt(i);
|
|
272
|
+
}
|
|
273
|
+
return out;
|
|
274
|
+
}
|
|
275
|
+
async function secret(privateKey, publicKey) {
|
|
276
|
+
const other = await crypto.subtle.importKey("jwk", publicKey, CURVE, false, []);
|
|
277
|
+
return crypto.subtle.deriveKey({ name: "ECDH", public: other }, privateKey, { name: "AES-GCM", length: 256 }, false, ["encrypt", "decrypt"]);
|
|
278
|
+
}
|
|
279
|
+
var OrgCrypto;
|
|
280
|
+
((OrgCrypto) => {
|
|
281
|
+
async function pair() {
|
|
282
|
+
const made = await crypto.subtle.generateKey(CURVE, true, ["deriveKey"]);
|
|
283
|
+
return { publicKey: await crypto.subtle.exportKey("jwk", made.publicKey), privateKey: await crypto.subtle.exportKey("jwk", made.privateKey) };
|
|
284
|
+
}
|
|
285
|
+
OrgCrypto.pair = pair;
|
|
286
|
+
function isPair(value) {
|
|
287
|
+
const pair2 = value;
|
|
288
|
+
return !!pair2 && typeof pair2.publicKey?.x === "string" && typeof pair2.privateKey?.d === "string";
|
|
289
|
+
}
|
|
290
|
+
OrgCrypto.isPair = isPair;
|
|
291
|
+
function isPublic(value) {
|
|
292
|
+
const key = value;
|
|
293
|
+
return !!key && key.kty === "EC" && typeof key.x === "string" && typeof key.y === "string" && !("d" in key);
|
|
294
|
+
}
|
|
295
|
+
OrgCrypto.isPublic = isPublic;
|
|
296
|
+
async function seal(value, publicKey) {
|
|
297
|
+
const made = await crypto.subtle.generateKey(CURVE, true, ["deriveKey"]);
|
|
298
|
+
const key = await secret(made.privateKey, publicKey);
|
|
299
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
300
|
+
const data = await crypto.subtle.encrypt({ name: "AES-GCM", iv }, key, new TextEncoder().encode(JSON.stringify(value)));
|
|
301
|
+
const e = await crypto.subtle.exportKey("jwk", made.publicKey);
|
|
302
|
+
return { e: { kty: String(e.kty), crv: String(e.crv), x: e.x, y: e.y }, iv: base64(iv), d: base64(data) };
|
|
303
|
+
}
|
|
304
|
+
OrgCrypto.seal = seal;
|
|
305
|
+
async function open(sealed, pair2) {
|
|
306
|
+
const value = sealed;
|
|
307
|
+
if (!value?.e || !isPublic(value.e) || typeof value.iv !== "string" || typeof value.d !== "string") {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
try {
|
|
311
|
+
const mine = await crypto.subtle.importKey("jwk", pair2.privateKey, CURVE, false, ["deriveKey"]);
|
|
312
|
+
const key = await secret(mine, value.e);
|
|
313
|
+
const data = await crypto.subtle.decrypt({ name: "AES-GCM", iv: bytes(value.iv) }, key, bytes(value.d));
|
|
314
|
+
return JSON.parse(new TextDecoder().decode(data));
|
|
315
|
+
} catch {
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
OrgCrypto.open = open;
|
|
320
|
+
})(OrgCrypto ||= {});
|
|
321
|
+
|
|
322
|
+
// web-src/org-rooms.ts
|
|
323
|
+
var OrgKeys;
|
|
324
|
+
((OrgKeys) => {
|
|
325
|
+
function of(prefix, member) {
|
|
326
|
+
return `${prefix}${member}`;
|
|
327
|
+
}
|
|
328
|
+
OrgKeys.of = of;
|
|
329
|
+
function named(key, prefix) {
|
|
330
|
+
return key.startsWith(prefix) && key.length > prefix.length ? key.slice(prefix.length) : undefined;
|
|
331
|
+
}
|
|
332
|
+
OrgKeys.named = named;
|
|
333
|
+
})(OrgKeys ||= {});
|
|
334
|
+
|
|
335
|
+
class OrgSpace {
|
|
336
|
+
side;
|
|
337
|
+
store;
|
|
338
|
+
constructor(side, store) {
|
|
339
|
+
this.side = side;
|
|
340
|
+
this.store = store;
|
|
341
|
+
}
|
|
342
|
+
get rooms() {
|
|
343
|
+
return this.side.rooms;
|
|
344
|
+
}
|
|
345
|
+
async data() {
|
|
346
|
+
return this.store.read();
|
|
347
|
+
}
|
|
348
|
+
async live() {
|
|
349
|
+
return (await this.rooms.listRooms().catch(() => [])).filter((room) => room.kind === ORG_KIND && room.status === "member");
|
|
350
|
+
}
|
|
351
|
+
async room(roomId) {
|
|
352
|
+
const found = (await this.rooms.listRooms().catch(() => [])).find((room) => room.id === roomId && room.kind === ORG_KIND);
|
|
353
|
+
return found && found.status === "member" ? found : undefined;
|
|
354
|
+
}
|
|
355
|
+
static self(room) {
|
|
356
|
+
return room.members.find((member) => member.self)?.member;
|
|
357
|
+
}
|
|
358
|
+
async manifest(room) {
|
|
359
|
+
const data = await this.data();
|
|
360
|
+
const known = data.orgs[room.id];
|
|
361
|
+
if (known?.at) {
|
|
362
|
+
return known;
|
|
363
|
+
}
|
|
364
|
+
const made = { room: room.id, kind: room.owner ? "org" : "member", at: Date.now() };
|
|
365
|
+
await this.store.write({ ...data, orgs: { ...data.orgs, [room.id]: made } });
|
|
366
|
+
return made;
|
|
367
|
+
}
|
|
368
|
+
async pair(room) {
|
|
369
|
+
const self = OrgSpace.self(room);
|
|
370
|
+
if (!self) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
const manifest = await this.manifest(room);
|
|
374
|
+
const made = manifest.wrap && OrgCrypto.isPair(manifest.wrap) ? manifest.wrap : await OrgCrypto.pair();
|
|
375
|
+
if (!manifest.wrap) {
|
|
376
|
+
const data = await this.data();
|
|
377
|
+
await this.store.write({ ...data, orgs: { ...data.orgs, [room.id]: { ...manifest, wrap: made, at: Date.now() } } });
|
|
378
|
+
}
|
|
379
|
+
const state = await this.rooms.getState(room.id, OrgKeys.of(WRAP_KEY, self)).catch(() => ({}));
|
|
380
|
+
const left = state[OrgKeys.of(WRAP_KEY, self)];
|
|
381
|
+
if (!left || JSON.stringify(left.value) !== JSON.stringify(made.publicKey)) {
|
|
382
|
+
await this.rooms.setState(room.id, OrgKeys.of(WRAP_KEY, self), made.publicKey).catch(() => {
|
|
383
|
+
return;
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
return { pair: made, self };
|
|
387
|
+
}
|
|
388
|
+
async held(room, prefix) {
|
|
389
|
+
return this.rooms.getState(room, prefix).catch(() => ({}));
|
|
390
|
+
}
|
|
391
|
+
async put(room, key, next, tries = 4) {
|
|
392
|
+
for (let i = 0;i < tries; i++) {
|
|
393
|
+
const before = (await this.rooms.getState(room, key).catch(() => ({})))[key];
|
|
394
|
+
const value = next(before?.value, before?.version ?? 0);
|
|
395
|
+
const set = await this.rooms.setState(room, key, value, before?.version ?? 0).catch(() => ({ ok: false, version: 0 }));
|
|
396
|
+
if (set.ok) {
|
|
397
|
+
return value;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
throw new Error("The directory is being written by someone else right now: a moment and again.");
|
|
401
|
+
}
|
|
402
|
+
async doc(room) {
|
|
403
|
+
const state = await this.held(room.id, ORG_KEY);
|
|
404
|
+
const docs = Object.entries(state).filter(([key]) => !!OrgKeys.named(key, ORG_KEY)).map(([, value]) => ({ body: OrgUtil.doc(value.value, value.version), by: value.by, at: value.value?.at ?? 0 })).filter((each) => !!each.body).filter((each) => OrgUtil.manages(room.members.find((member) => member.member === each.by)));
|
|
405
|
+
return OrgUtil.fold(docs, room.members);
|
|
406
|
+
}
|
|
407
|
+
async profiles(room) {
|
|
408
|
+
const state = await this.held(room, PROFILE_KEY);
|
|
409
|
+
const found = {};
|
|
410
|
+
for (const value of Object.values(state)) {
|
|
411
|
+
const profile = value.value;
|
|
412
|
+
if (profile && typeof profile.at === "number" && typeof value.by === "string") {
|
|
413
|
+
found[value.by] = profile;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return found;
|
|
417
|
+
}
|
|
418
|
+
async cards(room) {
|
|
419
|
+
const state = await this.held(room, AGENT_KEY);
|
|
420
|
+
const found = [];
|
|
421
|
+
for (const value of Object.values(state)) {
|
|
422
|
+
for (const each of Array.isArray(value.value) ? value.value : []) {
|
|
423
|
+
const card = OrgUtil.card(each, value.version);
|
|
424
|
+
if (card && typeof value.by === "string") {
|
|
425
|
+
found.push({ member: value.by, card });
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return found;
|
|
430
|
+
}
|
|
431
|
+
async listings(room) {
|
|
432
|
+
const value = (await this.held(room.id, LISTING_KEY))[LISTING_KEY];
|
|
433
|
+
const all = value?.value ?? {};
|
|
434
|
+
const found = {};
|
|
435
|
+
for (const [address, listing] of Object.entries(all)) {
|
|
436
|
+
if (!listing || typeof listing.at !== "number") {
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
if (!OrgUtil.manages(room.members.find((each) => each.member === value?.by))) {
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
const known = found[address];
|
|
443
|
+
if (!known || listing.at >= known.at) {
|
|
444
|
+
found[address] = listing;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return found;
|
|
448
|
+
}
|
|
449
|
+
async asks(room) {
|
|
450
|
+
const state = await this.held(room, PULL_KEY);
|
|
451
|
+
return Object.entries(state).flatMap(([key, value]) => {
|
|
452
|
+
const by = OrgKeys.named(key, PULL_KEY);
|
|
453
|
+
return by && value.by === by && Array.isArray(value.value) ? value.value.filter((each) => !!each && typeof each.member === "string" && each.by === by) : [];
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
async answers(room) {
|
|
457
|
+
const state = await this.held(room, PULL_ANSWER_KEY);
|
|
458
|
+
return Object.entries(state).flatMap(([key, value]) => {
|
|
459
|
+
const owner = OrgKeys.named(key, PULL_ANSWER_KEY);
|
|
460
|
+
return owner && value.by === owner && Array.isArray(value.value) ? value.value.filter((each) => !!each && typeof each.by === "string" && typeof each.agent === "string").map((each) => ({ ...each, owner })) : [];
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
async wraps(room) {
|
|
464
|
+
const state = await this.held(room, WRAP_KEY);
|
|
465
|
+
const found = {};
|
|
466
|
+
for (const [key, value] of Object.entries(state)) {
|
|
467
|
+
const member = OrgKeys.named(key, WRAP_KEY);
|
|
468
|
+
if (member && OrgCrypto.isPublic(value.value)) {
|
|
469
|
+
found[member] = value.value;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return found;
|
|
473
|
+
}
|
|
474
|
+
async wrapped(room, self) {
|
|
475
|
+
const state = await this.held(room, LINK_KEY);
|
|
476
|
+
return Object.entries(state).flatMap(([key, value]) => {
|
|
477
|
+
const from = OrgKeys.named(key, LINK_KEY);
|
|
478
|
+
return from && value.by === from && Array.isArray(value.value) ? value.value.filter((each) => each?.to === self && each.from === from && !!each.d) : [];
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
async links(room, self) {
|
|
482
|
+
const key = OrgKeys.of(LINK_KEY, self);
|
|
483
|
+
const list = (await this.held(room, key))[key]?.value;
|
|
484
|
+
return Array.isArray(list) ? list : [];
|
|
485
|
+
}
|
|
486
|
+
async hosted() {
|
|
487
|
+
return (await this.data()).hosted;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// web-src/org-directory.ts
|
|
492
|
+
var OrgDirectory;
|
|
493
|
+
((OrgDirectory) => {
|
|
494
|
+
function listingKey(member, agent) {
|
|
495
|
+
return `${member}/${agent}`;
|
|
496
|
+
}
|
|
497
|
+
OrgDirectory.listingKey = listingKey;
|
|
498
|
+
function listingOf(rules, listing) {
|
|
499
|
+
if (!listing) {
|
|
500
|
+
return rules.review ? "wait" : "ok";
|
|
501
|
+
}
|
|
502
|
+
if (listing.no && (!listing.ok || listing.no.at > listing.ok.at)) {
|
|
503
|
+
return "no";
|
|
504
|
+
}
|
|
505
|
+
return listing.ok ? "ok" : "wait";
|
|
506
|
+
}
|
|
507
|
+
OrgDirectory.listingOf = listingOf;
|
|
508
|
+
async function view(space, room, now = Date.now()) {
|
|
509
|
+
const self = OrgSpace.self(room);
|
|
510
|
+
const role = OrgUtil.roleOf(room);
|
|
511
|
+
const admin = OrgUtil.manages(room);
|
|
512
|
+
const [doc, profiles, cards, listings, asks, answers, hosted, wrapped] = await Promise.all([
|
|
513
|
+
space.doc(room),
|
|
514
|
+
space.profiles(room.id),
|
|
515
|
+
space.cards(room.id),
|
|
516
|
+
space.listings(room),
|
|
517
|
+
space.asks(room.id),
|
|
518
|
+
space.answers(room.id),
|
|
519
|
+
space.hosted(),
|
|
520
|
+
self ? space.wrapped(room.id, self) : Promise.resolve([])
|
|
521
|
+
]);
|
|
522
|
+
const rules = doc?.rules ?? OrgUtil.rules(undefined);
|
|
523
|
+
const depts = doc?.depts ?? [{ id: "all", name: "", parent: "", order: 0, leads: [] }];
|
|
524
|
+
const seen = doc && self && !admin ? OrgUtil.sees(doc, self) : undefined;
|
|
525
|
+
const placeOf = (member) => doc?.places.find((each) => each.member === member);
|
|
526
|
+
const agents = [];
|
|
527
|
+
const review = [];
|
|
528
|
+
const mineWaiting = [];
|
|
529
|
+
for (const { member, card } of cards) {
|
|
530
|
+
const state = listingOf(rules, listings[listingKey(member, card.id)]);
|
|
531
|
+
if (state === "no") {
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
const mine = member === self;
|
|
535
|
+
const theirDepts = placeOf(member)?.depts ?? ["all"];
|
|
536
|
+
const inSight = !seen || mine || theirDepts.some((dept) => seen.has(dept));
|
|
537
|
+
const show = OrgUtil.opens(card.scope, self ?? "", theirDepts);
|
|
538
|
+
if (mine) {
|
|
539
|
+
if (state === "wait") {
|
|
540
|
+
mineWaiting.push(card);
|
|
541
|
+
}
|
|
542
|
+
agents.push({ member, memberName: room.members.find((each2) => each2.member === member)?.name ?? "", card, listing: state, mine, pullable: false });
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
if (!admin && (state === "wait" || !inSight || !show)) {
|
|
546
|
+
continue;
|
|
547
|
+
}
|
|
548
|
+
const each = {
|
|
549
|
+
member,
|
|
550
|
+
memberName: room.members.find((one) => one.member === member)?.name ?? "",
|
|
551
|
+
card,
|
|
552
|
+
listing: state,
|
|
553
|
+
mine: false,
|
|
554
|
+
pullable: state === "ok" && show
|
|
555
|
+
};
|
|
556
|
+
agents.push(each);
|
|
557
|
+
if (state === "wait") {
|
|
558
|
+
review.push(each);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
const people = [];
|
|
562
|
+
for (const member of room.members) {
|
|
563
|
+
const place = placeOf(member.member);
|
|
564
|
+
if (seen && member.member !== self && !(place?.depts ?? ["all"]).some((dept) => seen.has(dept))) {
|
|
565
|
+
continue;
|
|
566
|
+
}
|
|
567
|
+
people.push({
|
|
568
|
+
member: member.member,
|
|
569
|
+
id: member.id,
|
|
570
|
+
name: member.name,
|
|
571
|
+
...member.look ? { look: member.look } : {},
|
|
572
|
+
online: member.online,
|
|
573
|
+
role: member.owner ? "owner" : member.admin ? "admin" : "member",
|
|
574
|
+
depts: depts.filter((dept) => (place?.depts ?? ["all"]).includes(dept.id)),
|
|
575
|
+
...place?.manager ? { manager: place.manager } : {},
|
|
576
|
+
...profiles[member.member] ? { profile: profiles[member.member] } : {},
|
|
577
|
+
agents: agents.filter((each) => each.member === member.member).map((each) => each.card),
|
|
578
|
+
in: true
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
for (const invited of room.invited ?? []) {
|
|
582
|
+
people.push({
|
|
583
|
+
member: invited,
|
|
584
|
+
id: invited,
|
|
585
|
+
name: invited,
|
|
586
|
+
online: false,
|
|
587
|
+
role: "member",
|
|
588
|
+
depts: depts.filter((dept) => dept.id === "all"),
|
|
589
|
+
agents: [],
|
|
590
|
+
in: false
|
|
591
|
+
});
|
|
592
|
+
}
|
|
593
|
+
return {
|
|
594
|
+
at: now,
|
|
595
|
+
room: room.id,
|
|
596
|
+
title: doc?.name || room.title,
|
|
597
|
+
role,
|
|
598
|
+
...self ? { self } : {},
|
|
599
|
+
...doc ? { doc } : {},
|
|
600
|
+
rules,
|
|
601
|
+
depts,
|
|
602
|
+
people,
|
|
603
|
+
agents,
|
|
604
|
+
waiting: admin ? await waiting(space, room) : [],
|
|
605
|
+
mineWaiting,
|
|
606
|
+
review,
|
|
607
|
+
pulls: admin ? asks.filter((each) => each.member === self) : asks.filter((each) => each.member === self && each.by !== self),
|
|
608
|
+
pulled: asks.filter((each) => each.by === self && !answers.some((answer) => answer.owner === each.member && answer.agent === each.agent && answer.room === each.room)).map((each) => ({ ...each, memberName: room.members.find((member) => member.member === each.member)?.name ?? "" })),
|
|
609
|
+
answers: answers.filter((each) => each.by === self).sort((a, b) => a.at - b.at).map((each) => {
|
|
610
|
+
const ask = asks.find((one) => one.by === self && one.member === each.owner && one.agent === each.agent && one.room === each.room);
|
|
611
|
+
return {
|
|
612
|
+
...each,
|
|
613
|
+
memberName: room.members.find((member) => member.member === each.owner)?.name ?? each.owner.slice(0, 6),
|
|
614
|
+
...ask ? { agentName: ask.agentName, title: ask.title } : {}
|
|
615
|
+
};
|
|
616
|
+
}),
|
|
617
|
+
hosted: hosted.filter((each) => each.org === room.id),
|
|
618
|
+
unwrapped: wrapped.length
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
OrgDirectory.view = view;
|
|
622
|
+
async function summary(space, room) {
|
|
623
|
+
const [doc, cards, listings, requests, data] = await Promise.all([
|
|
624
|
+
space.doc(room),
|
|
625
|
+
space.cards(room.id),
|
|
626
|
+
space.listings(room),
|
|
627
|
+
OrgUtil.manages(room) ? space.rooms.listRequests().catch(() => []) : Promise.resolve([]),
|
|
628
|
+
space.data()
|
|
629
|
+
]);
|
|
630
|
+
const rules = doc?.rules ?? OrgUtil.rules(undefined);
|
|
631
|
+
const reviews = cards.filter(({ member, card }) => listingOf(rules, listings[listingKey(member, card.id)]) === "wait").length;
|
|
632
|
+
return {
|
|
633
|
+
id: room.id,
|
|
634
|
+
title: doc?.name || room.title,
|
|
635
|
+
role: OrgUtil.roleOf(room),
|
|
636
|
+
members: room.members.length,
|
|
637
|
+
waiting: requests.filter((each) => each.room === room.id).length + reviews,
|
|
638
|
+
agents: cards.length,
|
|
639
|
+
...rules.pin && !data.orgs[room.id]?.pinned ? { pin: true } : {}
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
OrgDirectory.summary = summary;
|
|
643
|
+
})(OrgDirectory ||= {});
|
|
644
|
+
async function waiting(space, room) {
|
|
645
|
+
const requests = await space.rooms.listRequests().catch(() => []);
|
|
646
|
+
return requests.filter((each) => each.room === room.id).map((each) => ({ ask: each.ask, id: each.id, name: each.name, at: each.at })).sort((a, b) => a.at - b.at);
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
// web-src/org-org.ts
|
|
650
|
+
var REFUSED = {
|
|
651
|
+
"zh-CN": "只有管理员可以改通讯录。",
|
|
652
|
+
"zh-TW": "只有管理員可以改通訊錄。",
|
|
653
|
+
"en-US": "Only an admin changes the directory.",
|
|
654
|
+
"ja-JP": "名簿を変えられるのは管理者だけです。"
|
|
655
|
+
};
|
|
656
|
+
|
|
657
|
+
class OrgOffice {
|
|
658
|
+
space;
|
|
659
|
+
options;
|
|
660
|
+
constructor(space, options = {}) {
|
|
661
|
+
this.space = space;
|
|
662
|
+
this.options = options;
|
|
663
|
+
}
|
|
664
|
+
get now() {
|
|
665
|
+
return this.options.now?.() ?? Date.now();
|
|
666
|
+
}
|
|
667
|
+
async own(roomId) {
|
|
668
|
+
const room = await this.space.room(roomId);
|
|
669
|
+
const self = room ? OrgSpace.self(room) : undefined;
|
|
670
|
+
if (!room || !self) {
|
|
671
|
+
throw new Error("You are not in this organization any more.");
|
|
672
|
+
}
|
|
673
|
+
return { room, self, admin: OrgUtil.manages(room), owner: room.owner };
|
|
674
|
+
}
|
|
675
|
+
async managed(roomId) {
|
|
676
|
+
const { room, self, admin } = await this.own(roomId);
|
|
677
|
+
if (!admin) {
|
|
678
|
+
throw new Error(refused());
|
|
679
|
+
}
|
|
680
|
+
return { room, self, doc: await this.space.doc(room) };
|
|
681
|
+
}
|
|
682
|
+
async create(request) {
|
|
683
|
+
const title = (request.title ?? "").trim().slice(0, 60);
|
|
684
|
+
if (!title) {
|
|
685
|
+
throw new Error("Name the company.");
|
|
686
|
+
}
|
|
687
|
+
const made = await this.space.rooms.createRoom({ kind: ORG_KIND, title, view: VIEW, unlisted: true });
|
|
688
|
+
await this.space.rooms.configureRoom(made.id, { members: 100 }).catch(() => {
|
|
689
|
+
return;
|
|
690
|
+
});
|
|
691
|
+
await this.space.manifest(made);
|
|
692
|
+
await this.writeDoc({ room: made, self: OrgSpace.self(made) ?? "", doc: undefined, change: { name: request.name?.trim() || title } });
|
|
693
|
+
const hello = TaLanguageUtil.pick(TaLanguageUtil.server(), {
|
|
694
|
+
"zh-CN": `「${title}」建好了:把同事请进来,给他们分部门。`,
|
|
695
|
+
"zh-TW": `「${title}」建好了:把同事請進來,給他們分部門。`,
|
|
696
|
+
"en-US": `“${title}” is here: invite your colleagues in, and put them in departments.`,
|
|
697
|
+
"ja-JP": `「${title}」を作りました。同僚を招いて、部署に分けてください。`
|
|
698
|
+
});
|
|
699
|
+
await this.space.rooms.say(made.id, hello).then(() => {
|
|
700
|
+
return;
|
|
701
|
+
}, () => {
|
|
702
|
+
return;
|
|
703
|
+
});
|
|
704
|
+
return made;
|
|
705
|
+
}
|
|
706
|
+
async save(request) {
|
|
707
|
+
const { room, self } = await this.managed(request.room);
|
|
708
|
+
return this.writeDoc({
|
|
709
|
+
room,
|
|
710
|
+
self,
|
|
711
|
+
change: {
|
|
712
|
+
...request.name !== undefined ? { name: request.name.trim().slice(0, 60) } : {},
|
|
713
|
+
...request.about !== undefined ? { about: request.about.trim().slice(0, 300) } : {},
|
|
714
|
+
...request.rules ? { rules: OrgUtil.rules({ ...(await this.space.doc(room))?.rules, ...request.rules }) } : {}
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
async dept(request) {
|
|
719
|
+
const { room, self, doc } = await this.managed(request.room);
|
|
720
|
+
const id = OrgUtil.deptId(request.id);
|
|
721
|
+
const name = (request.name ?? "").trim().slice(0, 40);
|
|
722
|
+
if (!id || !name) {
|
|
723
|
+
throw new Error("A department needs an id of lowercase letters and dashes, and a name.");
|
|
724
|
+
}
|
|
725
|
+
const known = doc?.depts ?? [];
|
|
726
|
+
if (!known.some((each) => each.id === id) && known.length >= 60) {
|
|
727
|
+
throw new Error("Too many departments.");
|
|
728
|
+
}
|
|
729
|
+
const parent = request.parent && known.some((each) => each.id === request.parent) ? request.parent : "all";
|
|
730
|
+
const dept = { ...known.find((each) => each.id === id) ?? { id, order: known.length, leads: [] }, id, name, parent };
|
|
731
|
+
return this.writeDoc({ room, self, change: { depts: [...known.filter((each) => each.id !== id), dept] } });
|
|
732
|
+
}
|
|
733
|
+
async place(request) {
|
|
734
|
+
const { room, self, doc } = await this.managed(request.room);
|
|
735
|
+
if (!room.members.some((each) => each.member === request.member)) {
|
|
736
|
+
throw new Error("No such member.");
|
|
737
|
+
}
|
|
738
|
+
const depts = (request.depts ?? []).filter((dept) => doc?.depts.some((each) => each.id === dept));
|
|
739
|
+
const manager = request.manager ? room.members.find((each) => each.member === request.manager || !!request.manager && each.name === request.manager) : undefined;
|
|
740
|
+
const place = {
|
|
741
|
+
member: request.member,
|
|
742
|
+
depts: depts.length ? depts : ["all"],
|
|
743
|
+
...manager ? { manager: { id: manager.member, name: manager.name } } : {},
|
|
744
|
+
at: this.now
|
|
745
|
+
};
|
|
746
|
+
return this.writeDoc({ room, self, change: { places: [...(doc?.places ?? []).filter((each) => each.member !== request.member), place] } });
|
|
747
|
+
}
|
|
748
|
+
async lead(request) {
|
|
749
|
+
const { room, self, doc } = await this.managed(request.room);
|
|
750
|
+
const dept = doc?.depts.find((each) => each.id === request.dept);
|
|
751
|
+
if (!dept) {
|
|
752
|
+
throw new Error("No such department.");
|
|
753
|
+
}
|
|
754
|
+
const leads = request.lead ? [...new Set([...dept.leads, request.member])] : dept.leads.filter((each) => each !== request.member);
|
|
755
|
+
return this.writeDoc({ room, self, change: { depts: [...(doc?.depts ?? []).filter((each) => each.id !== dept.id), { ...dept, leads }] } });
|
|
756
|
+
}
|
|
757
|
+
async join(request) {
|
|
758
|
+
const room = await this.space.rooms.join(request.link.trim());
|
|
759
|
+
const made = (await this.space.live()).find((each) => each.id === room.id);
|
|
760
|
+
if (made && made.status === "member") {
|
|
761
|
+
await this.space.manifest(made);
|
|
762
|
+
}
|
|
763
|
+
return room;
|
|
764
|
+
}
|
|
765
|
+
async invite(request) {
|
|
766
|
+
const { room } = await this.managed(request.room);
|
|
767
|
+
return this.space.rooms.inviteFriends(room.id, request.friends.slice(0, 50));
|
|
768
|
+
}
|
|
769
|
+
async answer(request) {
|
|
770
|
+
const { room } = await this.managed(request.room);
|
|
771
|
+
await this.space.rooms.answer(room.id, request.ask, request.accept);
|
|
772
|
+
}
|
|
773
|
+
async out(request) {
|
|
774
|
+
const { room, self } = await this.managed(request.room);
|
|
775
|
+
if (request.member === self) {
|
|
776
|
+
throw new Error("Leave it yourself, from your own page.");
|
|
777
|
+
}
|
|
778
|
+
const theirs = room.members.find((each) => each.member === request.member);
|
|
779
|
+
if (theirs?.owner) {
|
|
780
|
+
throw new Error("The owner of the organization cannot be taken out.");
|
|
781
|
+
}
|
|
782
|
+
await this.space.rooms.removeMember(room.id, request.member);
|
|
783
|
+
await this.space.rooms.setState(room.id, OrgKeys.of(PROFILE_KEY, request.member), null).catch(() => {
|
|
784
|
+
return;
|
|
785
|
+
});
|
|
786
|
+
await this.space.rooms.setState(room.id, OrgKeys.of(AGENT_KEY, request.member), null).catch(() => {
|
|
787
|
+
return;
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
async leave(request) {
|
|
791
|
+
const { room, self } = await this.own(request.room);
|
|
792
|
+
if (room.owner) {
|
|
793
|
+
throw new Error("The owner closes the organization for all, in Ta → Discover → Directory.");
|
|
794
|
+
}
|
|
795
|
+
await this.space.rooms.setState(room.id, OrgKeys.of(PROFILE_KEY, self), null).catch(() => {
|
|
796
|
+
return;
|
|
797
|
+
});
|
|
798
|
+
await this.space.rooms.setState(room.id, OrgKeys.of(AGENT_KEY, self), null).catch(() => {
|
|
799
|
+
return;
|
|
800
|
+
});
|
|
801
|
+
const data = await this.space.data();
|
|
802
|
+
const hosted = data.hosted.filter((each) => each.org === room.id);
|
|
803
|
+
if (hosted.length) {
|
|
804
|
+
await this.options.recall?.(hosted);
|
|
805
|
+
}
|
|
806
|
+
await this.space.rooms.leave(room.id);
|
|
807
|
+
await this.forget(room.id);
|
|
808
|
+
}
|
|
809
|
+
async forget(room) {
|
|
810
|
+
const data = await this.space.data();
|
|
811
|
+
const { [room]: _gone, ...orgs } = data.orgs;
|
|
812
|
+
await this.space.store.write({ ...data, orgs, hosted: data.hosted.filter((each) => each.org !== room) });
|
|
813
|
+
}
|
|
814
|
+
async profile(request) {
|
|
815
|
+
const { room, self } = await this.own(request.room);
|
|
816
|
+
const clean = (value, max) => typeof value === "string" && value.trim() ? value.trim().slice(0, max) : undefined;
|
|
817
|
+
const profile = {
|
|
818
|
+
...clean(request.profile.title, 40) ? { title: clean(request.profile.title, 40) } : {},
|
|
819
|
+
...clean(request.profile.about, 300) ? { about: clean(request.profile.about, 300) } : {},
|
|
820
|
+
...clean(request.profile.staff, 40) ? { staff: clean(request.profile.staff, 40) } : {},
|
|
821
|
+
...clean(request.profile.mail, 100) ? { mail: clean(request.profile.mail, 100) } : {},
|
|
822
|
+
...clean(request.profile.phone, 40) ? { phone: clean(request.profile.phone, 40) } : {},
|
|
823
|
+
at: this.now
|
|
824
|
+
};
|
|
825
|
+
await this.space.put(room.id, OrgKeys.of(PROFILE_KEY, self), () => profile);
|
|
826
|
+
return profile;
|
|
827
|
+
}
|
|
828
|
+
async publish(request) {
|
|
829
|
+
const { room, self } = await this.own(request.room);
|
|
830
|
+
const card = OrgUtil.card({ ...request.card, at: this.now }, this.now);
|
|
831
|
+
if (!card) {
|
|
832
|
+
throw new Error("Name the agent.");
|
|
833
|
+
}
|
|
834
|
+
await this.space.put(room.id, OrgKeys.of(AGENT_KEY, self), (before) => {
|
|
835
|
+
const known = (Array.isArray(before) ? before : []).filter((each) => each?.id !== card.id);
|
|
836
|
+
return [...known, card].slice(0, 10);
|
|
837
|
+
});
|
|
838
|
+
return card;
|
|
839
|
+
}
|
|
840
|
+
async unpublish(request) {
|
|
841
|
+
const { room, self } = await this.own(request.room);
|
|
842
|
+
await this.space.put(room.id, OrgKeys.of(AGENT_KEY, self), (before) => (Array.isArray(before) ? before : []).filter((each) => each?.id !== request.agent));
|
|
843
|
+
const data = await this.space.data();
|
|
844
|
+
const hosted = data.hosted.filter((each) => each.org === room.id && each.agent === request.agent);
|
|
845
|
+
if (hosted.length) {
|
|
846
|
+
await this.options.recall?.(hosted);
|
|
847
|
+
await this.space.store.write({ ...data, hosted: data.hosted.filter((each) => !hosted.includes(each)) });
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
async listing(request) {
|
|
851
|
+
const { room, self } = await this.managed(request.room);
|
|
852
|
+
const cards = await this.space.cards(room.id);
|
|
853
|
+
const found = cards.find((each) => each.member === request.member && each.card.id === request.agent);
|
|
854
|
+
if (!found) {
|
|
855
|
+
throw new Error("That agent is no longer published.");
|
|
856
|
+
}
|
|
857
|
+
const key = `${request.member}/${request.agent}`;
|
|
858
|
+
await this.space.put(room.id, LISTING_KEY, (before) => {
|
|
859
|
+
const known = before ?? {};
|
|
860
|
+
return {
|
|
861
|
+
...known,
|
|
862
|
+
[key]: {
|
|
863
|
+
member: request.member,
|
|
864
|
+
agent: request.agent,
|
|
865
|
+
card: found.card,
|
|
866
|
+
ok: request.ok ? { by: self, at: this.now } : undefined,
|
|
867
|
+
no: request.ok ? undefined : { by: self, at: this.now },
|
|
868
|
+
at: this.now
|
|
869
|
+
}
|
|
870
|
+
};
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
async writeDoc(request) {
|
|
874
|
+
const before = request.doc ?? await this.space.doc(request.room);
|
|
875
|
+
const base = before ?? {
|
|
876
|
+
v: 1,
|
|
877
|
+
depts: [{ id: "all", name: "", parent: "", order: 0, leads: [] }],
|
|
878
|
+
places: [],
|
|
879
|
+
rules: OrgUtil.rules(undefined),
|
|
880
|
+
at: this.now
|
|
881
|
+
};
|
|
882
|
+
const doc = { ...base, ...request.change, v: 1, at: this.now };
|
|
883
|
+
await this.space.put(request.room.id, OrgKeys.of(ORG_KEY, request.self), () => doc);
|
|
884
|
+
return doc;
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
function refused() {
|
|
888
|
+
return TaLanguageUtil.pick(TaLanguageUtil.server(), REFUSED);
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
// web-src/org-puller.ts
|
|
892
|
+
var LANG = {
|
|
893
|
+
noRoom: {
|
|
894
|
+
"zh-CN": "不在这个群聊里:进群之后再拉。",
|
|
895
|
+
"zh-TW": "不在這個群聊裡:進群之後再拉。",
|
|
896
|
+
"en-US": "You are not in that group chat: join it first.",
|
|
897
|
+
"ja-JP": "そのグループチャットにいません:先に参加してください。"
|
|
898
|
+
},
|
|
899
|
+
notOwner: {
|
|
900
|
+
"zh-CN": "只有群主和管理员可以把公司里的智能体拉进群。",
|
|
901
|
+
"zh-TW": "只有群組主和管理員可以把公司的智慧體拉進群。",
|
|
902
|
+
"en-US": "Only the owner or an admin pulls an agent of the company into a group chat.",
|
|
903
|
+
"ja-JP": "会社のエージェントをグループチャットに入れられるのはオーナーと管理者だけです。"
|
|
904
|
+
},
|
|
905
|
+
noAgent: {
|
|
906
|
+
"zh-CN": "通讯录里没有这个智能体,或者它还没被同意出现。",
|
|
907
|
+
"zh-TW": "通訊錄裡沒有這個智慧體,或者它還沒被同意出現。",
|
|
908
|
+
"en-US": "That agent is not in the directory, or an admin has not agreed to it yet.",
|
|
909
|
+
"ja-JP": "そのエージェントは名簿になく、まだ管理者の承認もありません。"
|
|
910
|
+
},
|
|
911
|
+
full: {
|
|
912
|
+
"zh-CN": "群聊里最多 8 个智能体。",
|
|
913
|
+
"zh-TW": "群聊裡最多 8 個智慧體。",
|
|
914
|
+
"en-US": "At most 8 agents in a group chat.",
|
|
915
|
+
"ja-JP": "1 つのグループチャットに置けるエージェントは 8 個までです。"
|
|
916
|
+
}
|
|
917
|
+
};
|
|
918
|
+
var AGENTS_MAX = 8;
|
|
919
|
+
|
|
920
|
+
class OrgPuller {
|
|
921
|
+
space;
|
|
922
|
+
options;
|
|
923
|
+
constructor(space, options = {}) {
|
|
924
|
+
this.space = space;
|
|
925
|
+
this.options = options;
|
|
926
|
+
}
|
|
927
|
+
get now() {
|
|
928
|
+
return this.options.now?.() ?? Date.now();
|
|
929
|
+
}
|
|
930
|
+
async pull(request) {
|
|
931
|
+
const org = await this.space.room(request.org);
|
|
932
|
+
const self = org ? OrgSpace.self(org) : undefined;
|
|
933
|
+
if (!org || !self) {
|
|
934
|
+
throw new Error(pick(LANG.noRoom));
|
|
935
|
+
}
|
|
936
|
+
if (!OrgUtil.manages(org)) {
|
|
937
|
+
throw new Error(pick(LANG.notOwner));
|
|
938
|
+
}
|
|
939
|
+
const group = (await this.space.rooms.listRooms().catch(() => [])).find((each) => each.id === request.group && each.status === "member");
|
|
940
|
+
const me = group?.members.find((each) => each.self);
|
|
941
|
+
if (!group || !me) {
|
|
942
|
+
throw new Error(pick(LANG.noRoom));
|
|
943
|
+
}
|
|
944
|
+
if (!(me.owner || me.admin)) {
|
|
945
|
+
throw new Error(pick(LANG.notOwner));
|
|
946
|
+
}
|
|
947
|
+
if (group.kind === "ta-direct") {
|
|
948
|
+
throw new Error(pick(LANG.noRoom));
|
|
949
|
+
}
|
|
950
|
+
const cards = await this.space.cards(org.id);
|
|
951
|
+
const card = cards.find((each) => each.member === request.member && each.card.id === request.agent)?.card;
|
|
952
|
+
if (!card || request.member === self) {
|
|
953
|
+
throw new Error(pick(LANG.noAgent));
|
|
954
|
+
}
|
|
955
|
+
const mine = await this.inGroup(group.id, me.member);
|
|
956
|
+
if (mine.length >= AGENTS_MAX) {
|
|
957
|
+
throw new Error(pick(LANG.full));
|
|
958
|
+
}
|
|
959
|
+
const ask = {
|
|
960
|
+
member: request.member,
|
|
961
|
+
agent: card.id,
|
|
962
|
+
agentName: card.name,
|
|
963
|
+
room: group.id,
|
|
964
|
+
title: group.title,
|
|
965
|
+
by: self,
|
|
966
|
+
byName: me.name,
|
|
967
|
+
at: this.now
|
|
968
|
+
};
|
|
969
|
+
await this.space.put(org.id, OrgKeys.of(PULL_KEY, self), (before) => [
|
|
970
|
+
...(Array.isArray(before) ? before : []).filter((each) => !(each.member === ask.member && each.agent === ask.agent && each.room === ask.room)),
|
|
971
|
+
ask
|
|
972
|
+
].slice(-50));
|
|
973
|
+
await this.space.rooms.send(org.id, { ta: "ta-org", kind: "pull", ask }, request.member).catch(() => {
|
|
974
|
+
return;
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
async answer(request) {
|
|
978
|
+
const org = await this.space.room(request.org);
|
|
979
|
+
const self = org ? OrgSpace.self(org) : undefined;
|
|
980
|
+
if (!org || !self) {
|
|
981
|
+
throw new Error(pick(LANG.noRoom));
|
|
982
|
+
}
|
|
983
|
+
if (request.ask.member !== self) {
|
|
984
|
+
throw new Error(pick(LANG.noAgent));
|
|
985
|
+
}
|
|
986
|
+
await this.space.pair(org).catch(() => {
|
|
987
|
+
return;
|
|
988
|
+
});
|
|
989
|
+
const answer = {
|
|
990
|
+
by: request.ask.by,
|
|
991
|
+
agent: request.ask.agent,
|
|
992
|
+
room: request.ask.room,
|
|
993
|
+
ok: request.ok,
|
|
994
|
+
at: this.now,
|
|
995
|
+
...request.why ? { why: request.why } : {}
|
|
996
|
+
};
|
|
997
|
+
await this.writeAnswer(org.id, self, (before) => [
|
|
998
|
+
...(Array.isArray(before) ? before : []).filter((each) => !(each.by === answer.by && each.agent === answer.agent && each.room === answer.room)),
|
|
999
|
+
answer
|
|
1000
|
+
].slice(-50));
|
|
1001
|
+
await this.space.rooms.send(org.id, { ta: "ta-org", kind: "answer", answer }).catch(() => {
|
|
1002
|
+
return;
|
|
1003
|
+
});
|
|
1004
|
+
return answer;
|
|
1005
|
+
}
|
|
1006
|
+
async deliver(request) {
|
|
1007
|
+
const org = await this.space.room(request.org);
|
|
1008
|
+
const self = org ? OrgSpace.self(org) : undefined;
|
|
1009
|
+
if (!org || !self) {
|
|
1010
|
+
return 0;
|
|
1011
|
+
}
|
|
1012
|
+
const [asks, answers, wraps] = await Promise.all([this.space.asks(org.id), this.space.answers(org.id), this.space.wraps(org.id)]);
|
|
1013
|
+
const links = await this.space.links(org.id, self);
|
|
1014
|
+
let sent = 0;
|
|
1015
|
+
for (const ask of asks.filter((each) => each.by === self)) {
|
|
1016
|
+
const answer = answers.find((each) => each.by === self && each.owner === ask.member && each.agent === ask.agent && each.room === ask.room);
|
|
1017
|
+
if (!answer?.ok || answer.taken) {
|
|
1018
|
+
continue;
|
|
1019
|
+
}
|
|
1020
|
+
if (links.some((each) => each.to === ask.member && each.agent === ask.agent && each.room === ask.room && each.at >= answer.at)) {
|
|
1021
|
+
continue;
|
|
1022
|
+
}
|
|
1023
|
+
const theirKey = wraps[ask.member];
|
|
1024
|
+
if (!theirKey) {
|
|
1025
|
+
continue;
|
|
1026
|
+
}
|
|
1027
|
+
const sealed = {
|
|
1028
|
+
to: ask.member,
|
|
1029
|
+
from: self,
|
|
1030
|
+
room: ask.room,
|
|
1031
|
+
agent: ask.agent,
|
|
1032
|
+
d: await OrgCrypto.seal({ link: await this.link(ask.room), title: ask.title }, theirKey),
|
|
1033
|
+
at: this.now
|
|
1034
|
+
};
|
|
1035
|
+
const after = await this.space.put(org.id, OrgKeys.of(LINK_KEY, self), (before) => [
|
|
1036
|
+
...(Array.isArray(before) ? before : []).filter((each) => !(each.to === sealed.to && each.agent === sealed.agent && each.room === sealed.room)),
|
|
1037
|
+
sealed
|
|
1038
|
+
].slice(-20));
|
|
1039
|
+
links.splice(0, links.length, ...after);
|
|
1040
|
+
sent += 1;
|
|
1041
|
+
await this.space.rooms.send(org.id, { ta: "ta-org", kind: "delivered", agent: sealed.agent, room: sealed.room, to: sealed.to }).catch(() => {
|
|
1042
|
+
return;
|
|
1043
|
+
});
|
|
1044
|
+
}
|
|
1045
|
+
return sent;
|
|
1046
|
+
}
|
|
1047
|
+
async take(request) {
|
|
1048
|
+
const org = await this.space.room(request.org);
|
|
1049
|
+
const self = org ? OrgSpace.self(org) : undefined;
|
|
1050
|
+
const manifest = org ? await this.space.manifest(org) : undefined;
|
|
1051
|
+
if (!org || !self || !manifest?.wrap) {
|
|
1052
|
+
return 0;
|
|
1053
|
+
}
|
|
1054
|
+
const left = await this.space.wrapped(org.id, self);
|
|
1055
|
+
let taken = 0;
|
|
1056
|
+
for (const each of left) {
|
|
1057
|
+
const opened = await OrgCrypto.open(each.d, manifest.wrap);
|
|
1058
|
+
if (!opened?.link) {
|
|
1059
|
+
continue;
|
|
1060
|
+
}
|
|
1061
|
+
const joined = await this.space.rooms.join(opened.link).catch(() => {
|
|
1062
|
+
return;
|
|
1063
|
+
});
|
|
1064
|
+
if (joined?.status !== "member") {
|
|
1065
|
+
continue;
|
|
1066
|
+
}
|
|
1067
|
+
try {
|
|
1068
|
+
await this.options.invite?.(joined.id, each.agent);
|
|
1069
|
+
} catch {
|
|
1070
|
+
await this.markTaken(org.id, self, each, "invite");
|
|
1071
|
+
continue;
|
|
1072
|
+
}
|
|
1073
|
+
const data = await this.space.data();
|
|
1074
|
+
const record = { agent: each.agent, org: org.id, room: joined.id, title: opened.title || joined.title, by: each.from, at: each.at };
|
|
1075
|
+
await this.space.store.write({
|
|
1076
|
+
...data,
|
|
1077
|
+
hosted: [...data.hosted.filter((one) => !(one.agent === record.agent && one.room === record.room)), record].slice(-100)
|
|
1078
|
+
});
|
|
1079
|
+
await this.markTaken(org.id, self, each, undefined);
|
|
1080
|
+
taken += 1;
|
|
1081
|
+
await this.space.rooms.send(org.id, { ta: "ta-org", kind: "taken", agent: each.agent, room: joined.id, to: each.from }).catch(() => {
|
|
1082
|
+
return;
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1085
|
+
return taken;
|
|
1086
|
+
}
|
|
1087
|
+
async consent(request) {
|
|
1088
|
+
if (!request || typeof request.room !== "string" || typeof request.id !== "string") {
|
|
1089
|
+
return false;
|
|
1090
|
+
}
|
|
1091
|
+
for (const org of await this.space.live()) {
|
|
1092
|
+
const self = OrgSpace.self(org);
|
|
1093
|
+
if (!self) {
|
|
1094
|
+
continue;
|
|
1095
|
+
}
|
|
1096
|
+
const asks = (await this.space.asks(org.id)).filter((each) => each.by === self && each.room === request.room);
|
|
1097
|
+
if (!asks.length) {
|
|
1098
|
+
continue;
|
|
1099
|
+
}
|
|
1100
|
+
const answers = await this.space.answers(org.id);
|
|
1101
|
+
const owners = asks.map((each) => each.member).filter((member) => answers.some((each) => each.owner === member && each.by === self && each.room === request.room && each.ok));
|
|
1102
|
+
const sure = owners.some((member) => org.members.find((each) => each.member === member)?.id === request.id);
|
|
1103
|
+
if (sure) {
|
|
1104
|
+
await this.space.rooms.answer(request.room, request.ask, true).catch(() => {
|
|
1105
|
+
return;
|
|
1106
|
+
});
|
|
1107
|
+
return true;
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
return false;
|
|
1111
|
+
}
|
|
1112
|
+
async recall(hosted) {
|
|
1113
|
+
for (const each of hosted) {
|
|
1114
|
+
await this.options.recall?.(each.room, each.agent).catch(() => {
|
|
1115
|
+
return;
|
|
1116
|
+
});
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
async markTaken(room, self, each, why) {
|
|
1120
|
+
await this.writeAnswer(room, self, (before) => (Array.isArray(before) ? before : []).map((one) => one.by === each.from && one.agent === each.agent && one.room === each.room ? { ...one, taken: this.now, ...why ? { why } : {} } : one));
|
|
1121
|
+
}
|
|
1122
|
+
async writeAnswer(room, self, next) {
|
|
1123
|
+
await this.space.put(room, OrgKeys.of(PULL_ANSWER_KEY, self), next);
|
|
1124
|
+
}
|
|
1125
|
+
async link(room) {
|
|
1126
|
+
return (await this.space.rooms.invite(room, { days: 7 })).link;
|
|
1127
|
+
}
|
|
1128
|
+
async inGroup(room, self) {
|
|
1129
|
+
const key = `agents:${self}`;
|
|
1130
|
+
const value = (await this.space.held(room, key).catch(() => ({})))[key]?.value;
|
|
1131
|
+
return (Array.isArray(value) ? value : []).filter((each) => each && typeof each.id === "string").map((each) => String(each.id));
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
function pick(words) {
|
|
1135
|
+
return TaLanguageUtil.pick(TaLanguageUtil.server(), words);
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
// web-src/org-store.ts
|
|
1139
|
+
var EMPTY = { orgs: {}, hosted: [] };
|
|
1140
|
+
|
|
1141
|
+
class OrgStore {
|
|
1142
|
+
host;
|
|
1143
|
+
data = EMPTY;
|
|
1144
|
+
loading;
|
|
1145
|
+
constructor(host) {
|
|
1146
|
+
this.host = host;
|
|
1147
|
+
}
|
|
1148
|
+
async read() {
|
|
1149
|
+
this.loading ??= this.host.getExtensionData(NAME, "orgs").then((value) => this.data = OrgStore.clean(value)).catch(() => EMPTY);
|
|
1150
|
+
return this.loading;
|
|
1151
|
+
}
|
|
1152
|
+
async write(data) {
|
|
1153
|
+
this.data = data;
|
|
1154
|
+
this.loading = Promise.resolve(data);
|
|
1155
|
+
await this.host.setExtensionData(NAME, "orgs", data);
|
|
1156
|
+
}
|
|
1157
|
+
static clean(value) {
|
|
1158
|
+
const data = value;
|
|
1159
|
+
if (!data || typeof data !== "object") {
|
|
1160
|
+
return EMPTY;
|
|
1161
|
+
}
|
|
1162
|
+
const orgs = {};
|
|
1163
|
+
for (const [room, each] of Object.entries(data.orgs ?? {})) {
|
|
1164
|
+
if (each && typeof each.room === "string" && (each.kind === "org" || each.kind === "member")) {
|
|
1165
|
+
orgs[room] = {
|
|
1166
|
+
room: each.room,
|
|
1167
|
+
kind: each.kind,
|
|
1168
|
+
...each.wrap ? { wrap: each.wrap } : {},
|
|
1169
|
+
...typeof each.pinned === "number" ? { pinned: each.pinned } : {},
|
|
1170
|
+
at: Number(each.at) || 0
|
|
1171
|
+
};
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
const hosted = (Array.isArray(data.hosted) ? data.hosted : []).filter((each) => !!each && typeof each.agent === "string" && typeof each.room === "string" && typeof each.org === "string").slice(0, 200);
|
|
1175
|
+
return { orgs, hosted };
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
// web-src/org-host.ts
|
|
1180
|
+
class OrgHost {
|
|
1181
|
+
host;
|
|
1182
|
+
space;
|
|
1183
|
+
office;
|
|
1184
|
+
puller;
|
|
1185
|
+
stop;
|
|
1186
|
+
started = false;
|
|
1187
|
+
constructor(host) {
|
|
1188
|
+
this.host = host;
|
|
1189
|
+
const side = { rooms: host.rooms };
|
|
1190
|
+
this.space = new OrgSpace(side, new OrgStore(host));
|
|
1191
|
+
this.puller = new OrgPuller(this.space, {
|
|
1192
|
+
invite: (room, agent) => this.invite(room, agent),
|
|
1193
|
+
recall: (room, agent) => this.recall([{ agent, room, org: "", by: "", at: 0 }])
|
|
1194
|
+
});
|
|
1195
|
+
this.office = new OrgOffice(this.space, {
|
|
1196
|
+
recall: (hosted) => this.puller.recall(hosted)
|
|
1197
|
+
});
|
|
1198
|
+
}
|
|
1199
|
+
start() {
|
|
1200
|
+
if (this.started) {
|
|
1201
|
+
return;
|
|
1202
|
+
}
|
|
1203
|
+
this.started = true;
|
|
1204
|
+
this.stop = this.host.rooms.onEvent((event) => {
|
|
1205
|
+
if (event.type === "state" && event.room) {
|
|
1206
|
+
this.roomChanged(event.room, event.key);
|
|
1207
|
+
} else if (event.type === "request" && event.request) {
|
|
1208
|
+
this.puller.consent(event.request).catch(() => {
|
|
1209
|
+
return;
|
|
1210
|
+
});
|
|
1211
|
+
}
|
|
1212
|
+
});
|
|
1213
|
+
this.takeAll().catch(() => {
|
|
1214
|
+
return;
|
|
1215
|
+
});
|
|
1216
|
+
}
|
|
1217
|
+
dispose() {
|
|
1218
|
+
this.stop?.();
|
|
1219
|
+
this.started = false;
|
|
1220
|
+
}
|
|
1221
|
+
async view(request = {}) {
|
|
1222
|
+
const live = await this.space.live();
|
|
1223
|
+
const orgs = await Promise.all(live.map((room) => OrgDirectory.summary(this.space, room)));
|
|
1224
|
+
const chosen = request.room ? live.find((room) => room.id === request.room) : live[0];
|
|
1225
|
+
const org = chosen ? await OrgDirectory.view(this.space, chosen) : undefined;
|
|
1226
|
+
if (chosen) {
|
|
1227
|
+
await this.space.pair(chosen).catch(() => {
|
|
1228
|
+
return;
|
|
1229
|
+
});
|
|
1230
|
+
}
|
|
1231
|
+
return { orgs, ...org ? { org } : {} };
|
|
1232
|
+
}
|
|
1233
|
+
async agents() {
|
|
1234
|
+
const list = await this.host.callPackageHost("@celljs/ta-rooms", "doubles", [{ kind: "agents" }]).catch(() => {
|
|
1235
|
+
return;
|
|
1236
|
+
});
|
|
1237
|
+
return Array.isArray(list) ? list.filter((each) => each && typeof each.id === "string" && typeof each.name === "string") : [];
|
|
1238
|
+
}
|
|
1239
|
+
async groups() {
|
|
1240
|
+
const rooms = await this.host.rooms.listRooms().catch(() => []);
|
|
1241
|
+
return rooms.filter((room) => room.status === "member" && room.kind !== ORG_KIND && !room.unlisted && room.kind !== "ta-direct").map((room) => {
|
|
1242
|
+
const me = room.members.find((member) => member.self);
|
|
1243
|
+
return { id: room.id, title: room.title, mine: !!(me?.owner || me?.admin), agents: 0 };
|
|
1244
|
+
});
|
|
1245
|
+
}
|
|
1246
|
+
async request(request) {
|
|
1247
|
+
if (!request || typeof request !== "object") {
|
|
1248
|
+
throw new Error("Nothing asked.");
|
|
1249
|
+
}
|
|
1250
|
+
switch (request.kind) {
|
|
1251
|
+
case "create":
|
|
1252
|
+
return this.office.create(request);
|
|
1253
|
+
case "save":
|
|
1254
|
+
return this.office.save(request);
|
|
1255
|
+
case "dept":
|
|
1256
|
+
return this.office.dept(request);
|
|
1257
|
+
case "place":
|
|
1258
|
+
return this.office.place(request);
|
|
1259
|
+
case "lead":
|
|
1260
|
+
return this.office.lead(request);
|
|
1261
|
+
case "join":
|
|
1262
|
+
return this.office.join(request);
|
|
1263
|
+
case "invite":
|
|
1264
|
+
return this.office.invite(request);
|
|
1265
|
+
case "answer":
|
|
1266
|
+
return this.office.answer(request);
|
|
1267
|
+
case "out":
|
|
1268
|
+
return this.office.out(request);
|
|
1269
|
+
case "leave":
|
|
1270
|
+
return this.office.leave(request);
|
|
1271
|
+
case "profile":
|
|
1272
|
+
return this.office.profile(request);
|
|
1273
|
+
case "publish":
|
|
1274
|
+
return this.office.publish(request);
|
|
1275
|
+
case "unpublish":
|
|
1276
|
+
return this.office.unpublish(request);
|
|
1277
|
+
case "listing":
|
|
1278
|
+
return this.office.listing(request);
|
|
1279
|
+
case "pull":
|
|
1280
|
+
return this.puller.pull({ org: request.room, member: request.member, agent: request.agent, group: request.group });
|
|
1281
|
+
case "unpull":
|
|
1282
|
+
return this.unpull(request.room, request.group, request.agent);
|
|
1283
|
+
case "pullAnswer": {
|
|
1284
|
+
const ask = request.ask;
|
|
1285
|
+
return this.puller.answer({ org: request.org, ask, ok: request.ok });
|
|
1286
|
+
}
|
|
1287
|
+
case "pinned":
|
|
1288
|
+
return this.pinned(request.room);
|
|
1289
|
+
default:
|
|
1290
|
+
throw new Error("Nothing asked.");
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
async pinned(room) {
|
|
1294
|
+
const found = await this.space.room(room);
|
|
1295
|
+
if (!found) {
|
|
1296
|
+
return;
|
|
1297
|
+
}
|
|
1298
|
+
const manifest = await this.space.manifest(found);
|
|
1299
|
+
const data = await this.space.data();
|
|
1300
|
+
await this.space.store.write({ ...data, orgs: { ...data.orgs, [room]: { ...manifest, pinned: Date.now() } } });
|
|
1301
|
+
}
|
|
1302
|
+
async unpull(org, group, agent) {
|
|
1303
|
+
const data = await this.space.data();
|
|
1304
|
+
const hosted = data.hosted.filter((each) => each.org === org && each.room === group && each.agent === agent);
|
|
1305
|
+
await this.recall(hosted);
|
|
1306
|
+
await this.space.store.write({ ...data, hosted: data.hosted.filter((each) => !hosted.includes(each)) });
|
|
1307
|
+
}
|
|
1308
|
+
async invite(room, agent) {
|
|
1309
|
+
await this.host.callPackageHost("@celljs/ta-rooms", "doubles", [{ kind: "invite", room, agent }]);
|
|
1310
|
+
}
|
|
1311
|
+
async recall(hosted) {
|
|
1312
|
+
for (const each of hosted) {
|
|
1313
|
+
await this.host.callPackageHost("@celljs/ta-rooms", "doubles", [{ kind: "dismiss", room: each.room, agent: each.agent }]).catch(() => {
|
|
1314
|
+
return;
|
|
1315
|
+
});
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
takeAll() {
|
|
1319
|
+
return this.space.live().then(async (rooms) => {
|
|
1320
|
+
let taken = 0;
|
|
1321
|
+
for (const room of rooms) {
|
|
1322
|
+
taken += await this.puller.deliver({ org: room.id }).catch(() => 0);
|
|
1323
|
+
taken += await this.puller.take({ org: room.id }).catch(() => 0);
|
|
1324
|
+
}
|
|
1325
|
+
return taken;
|
|
1326
|
+
});
|
|
1327
|
+
}
|
|
1328
|
+
async roomChanged(room, key) {
|
|
1329
|
+
const live = await this.space.live();
|
|
1330
|
+
const found = live.find((each) => each.id === room);
|
|
1331
|
+
if (!found) {
|
|
1332
|
+
return;
|
|
1333
|
+
}
|
|
1334
|
+
const self = OrgSpace.self(found);
|
|
1335
|
+
if (!self) {
|
|
1336
|
+
return;
|
|
1337
|
+
}
|
|
1338
|
+
if (key.startsWith(PULL_ANSWER_KEY) || key.startsWith(LINK_KEY)) {
|
|
1339
|
+
await this.puller.deliver({ org: room }).catch(() => {
|
|
1340
|
+
return;
|
|
1341
|
+
});
|
|
1342
|
+
await this.puller.take({ org: room }).catch(() => {
|
|
1343
|
+
return;
|
|
1344
|
+
});
|
|
1345
|
+
}
|
|
1346
|
+
if (key.startsWith("agent:") || key.startsWith("profile:")) {
|
|
1347
|
+
this.host.changed?.("org");
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
async section() {
|
|
1351
|
+
const live = this.space.live();
|
|
1352
|
+
const rooms = await live;
|
|
1353
|
+
if (!rooms.length) {
|
|
1354
|
+
return;
|
|
1355
|
+
}
|
|
1356
|
+
const first = await OrgDirectory.view(this.space, rooms[0]);
|
|
1357
|
+
const name = first.title;
|
|
1358
|
+
const count = first.people.filter((each) => each.in).length;
|
|
1359
|
+
const agents = first.agents.filter((each) => each.pullable).map((each) => `${each.memberName}/${each.card.name}`);
|
|
1360
|
+
const depts = first.depts.filter((each) => each.id !== "all").length;
|
|
1361
|
+
const words = TaLanguageUtil.pick(TaLanguageUtil.server(), {
|
|
1362
|
+
"zh-CN": `${name}:${count} 人,${depts} 个部门。可以把同事的智能体拉进群聊一起干活:${agents.slice(0, 8).join("、") || "还没有"}。`,
|
|
1363
|
+
"zh-TW": `${name}:${count} 人,${depts} 個部門。可以把同事的智慧體拉進群聊一起做事:${agents.slice(0, 8).join("、") || "還沒有"}。`,
|
|
1364
|
+
"en-US": `${name}: ${count} people, ${depts} departments. Agents of theirs to pull into a group chat: ${agents.slice(0, 8).join(", ") || "none yet"}.`,
|
|
1365
|
+
"ja-JP": `${name}:${count} 人、${depts} 部署。同僚のエージェントをグループチャットに招けます:${agents.slice(0, 8).join("、") || "まだなし"}。`
|
|
1366
|
+
});
|
|
1367
|
+
return `<${SECTION}>
|
|
1368
|
+
${words}
|
|
1369
|
+
</${SECTION}>`;
|
|
1370
|
+
}
|
|
1371
|
+
async published() {
|
|
1372
|
+
const live = await this.space.live();
|
|
1373
|
+
const found = [];
|
|
1374
|
+
for (const room of live) {
|
|
1375
|
+
const self = OrgSpace.self(room);
|
|
1376
|
+
if (!self) {
|
|
1377
|
+
continue;
|
|
1378
|
+
}
|
|
1379
|
+
const cards = (await this.space.cards(room.id)).filter((each) => each.member === self).map((each) => each.card);
|
|
1380
|
+
found.push({ room: room.id, title: room.title, cards });
|
|
1381
|
+
}
|
|
1382
|
+
return found;
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
|
|
1386
|
+
// web-src/host.ts
|
|
1387
|
+
async function activate(host) {
|
|
1388
|
+
const org = new OrgHost(host);
|
|
1389
|
+
org.start();
|
|
1390
|
+
return {
|
|
1391
|
+
dispose: () => org.dispose(),
|
|
1392
|
+
org: (request) => org.request(request),
|
|
1393
|
+
view: (request) => org.view(request ?? {}),
|
|
1394
|
+
agents: () => org.agents(),
|
|
1395
|
+
groups: () => org.groups(),
|
|
1396
|
+
published: () => org.published(),
|
|
1397
|
+
section: () => org.section(),
|
|
1398
|
+
service: org
|
|
1399
|
+
};
|
|
1400
|
+
}
|
|
1401
|
+
export {
|
|
1402
|
+
activate as default
|
|
1403
|
+
};
|