@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/README.en.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# @celljs/ta-org
|
|
2
|
+
|
|
3
|
+
English | [简体中文](./README.md)
|
|
4
|
+
|
|
5
|
+
An enterprise directory for Ta: it runs between the Ta of a company, end to end. The design is in `docs/design/ta-org.md`.
|
|
6
|
+
|
|
7
|
+
- **One room a company**: made in Discover → Directory, it is a group of up to 100 people, unlisted; the members, departments, cards and agents are the shared state of the room, which the relay carries and cannot read.
|
|
8
|
+
- **Coming in takes an admin**: by a link (30 days, renewable) or an invitation of a friend; an admin agrees in Admin, then puts them in departments and sets who they report to.
|
|
9
|
+
- **Departments and leads**: departments under departments, each with leads; a member may be in several. Admins write one document of the organization, folded, so two changing different things keep both.
|
|
10
|
+
- **A card of one's own**: title, what they work on, staff id, mail, phone: theirs to write; where they sit is an admin's.
|
|
11
|
+
- **Agents**: publish an agent of yours, who sees it (everyone, departments, people); an admin agrees first unless the owner says otherwise.
|
|
12
|
+
- **Pulling one into a group chat**: a colleague pulls it in the directory; the request reaches its owner through the organization; once they agree, their Ta joins by a link sealed for them, and brings the agent in through ta-rooms (the owner of that group chat still agrees). They may take it out, and it goes when they leave the company or unpublish.
|
|
13
|
+
- **What a member sees**: everyone and every department, theirs and those under, or themselves only.
|
|
14
|
+
|
|
15
|
+
The host's methods (`ta call @celljs/ta-org <method> [json...]`): `view`, `org` (what the page asks of it), `agents`, `groups`, `published`, `section`. It reaches ta-rooms only for `invite` and `dismiss` of its `doubles`. Turned off, its pages and tools stop; the rooms and the data stay.
|
|
16
|
+
|
|
17
|
+
**Limits**: 100 people a company, 60 departments, 10 agents each, 8 agents a group chat.
|
package/README.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @celljs/ta-org
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[English](./README.en.md) | 简体中文
|
|
4
|
+
|
|
5
|
+
Ta 的企业通讯录:跑在公司的 Ta 之间,端到端加密。设计见 `docs/design/ta-org.md`。
|
|
6
|
+
|
|
7
|
+
- **一家公司一个房间**:在 发现 → 通讯录 建一个,它是房间里的一群人(最多 100 人),不列在任何列表里;成员、部门、名片、智能体都在这个房间的共享状态里,中继只转发、看不到内容。
|
|
8
|
+
- **进来要走同意**:发链接(30 天有效,可随时换新)或用好友邀请;管理员在「管理」里同意或拒绝,同意后给他分部门、设汇报对象。
|
|
9
|
+
- **部门与负责人**:一层层加部门,每个部门可以有几个负责人;成员可以同时在几个部门。管理员改的是同一份组织文档,两个管理员同时改不同的东西不会互相覆盖。
|
|
10
|
+
- **名片**:职位、在做什么、工号、邮箱、电话,每人只写自己的;部门与汇报对象由管理员设。
|
|
11
|
+
- **智能体**:把自己的智能体发布到通讯录,选谁看得见(所有人 / 部门 / 指定的人);公司默认由管理员同意后才出现。
|
|
12
|
+
- **拉进群聊一起干活**:同事在通讯录里点一个智能体「拉进群聊」,请求经组织房间送到智能体主人的 Ta;主人同意后,他的 Ta 用一条只为对方加密的链接进群,再通过 ta-rooms 把自己的智能体拉进来(群主仍然要同意)。主人可以随时把自己的智能体从群聊里撤出,离开公司或撤下发布时自动撤出。
|
|
13
|
+
- **成员能看到什么**:主人可以选「所有人、所有部门」「自己所在的部门和下级」「只有自己」。
|
|
14
|
+
|
|
15
|
+
包宿主的方法(`ta call @celljs/ta-org <方法> [json...]`):`view`、`org`(页面对它的请求)、`agents`、`groups`、`published`、`section`。它只经 ta-rooms 的宿主方法拉智能体进来(`doubles` 的 `invite`/`dismiss`),关掉本包,通讯录的页面与工具都停,房间和数据留着。
|
|
16
|
+
|
|
17
|
+
**上限**:一家公司 100 人(中继房间的人数上限)、60 个部门、每人 10 个智能体、一个群聊 8 个智能体。
|
|
@@ -0,0 +1,465 @@
|
|
|
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
|
+
// ../ta-protocol/esm/common/host-cloud-protocol.js
|
|
41
|
+
var TA_CLOUD_OUTPUT_BYTES = 1024 * 1024;
|
|
42
|
+
// ../ta-protocol/esm/common/package-data-keys.js
|
|
43
|
+
var TA_DOUBLE_LOOK = {
|
|
44
|
+
name: "@celljs/ta-web",
|
|
45
|
+
key: "assistant.species",
|
|
46
|
+
given: ["trex", "triceratops", "stegosaurus", "brachiosaurus", "velociraptor", "ankylosaurus", "pteranodon", "parasaurolophus"]
|
|
47
|
+
};
|
|
48
|
+
var TaDoubleLookUtil;
|
|
49
|
+
(function(TaDoubleLookUtil2) {
|
|
50
|
+
function given(id) {
|
|
51
|
+
let hash = 2166136261;
|
|
52
|
+
for (let i = 0;i < id.length; i++) {
|
|
53
|
+
hash = Math.imul(hash ^ id.charCodeAt(i), 16777619);
|
|
54
|
+
}
|
|
55
|
+
return TA_DOUBLE_LOOK.given[(hash >>> 0) % TA_DOUBLE_LOOK.given.length];
|
|
56
|
+
}
|
|
57
|
+
TaDoubleLookUtil2.given = given;
|
|
58
|
+
})(TaDoubleLookUtil || (TaDoubleLookUtil = {}));
|
|
59
|
+
|
|
60
|
+
// ../ta-protocol/esm/common/ta-extension-host.js
|
|
61
|
+
var TA_HOST_EVENT = "ta:host";
|
|
62
|
+
var TaHostUtil;
|
|
63
|
+
(function(TaHostUtil2) {
|
|
64
|
+
TaHostUtil2.WAIT_MS = 1000;
|
|
65
|
+
function reach(events, name, waitMs = TaHostUtil2.WAIT_MS) {
|
|
66
|
+
return new Promise((resolve) => {
|
|
67
|
+
let heard = false;
|
|
68
|
+
let answered = false;
|
|
69
|
+
const timer = setTimeout(() => resolve(undefined), waitMs);
|
|
70
|
+
const request = {
|
|
71
|
+
name,
|
|
72
|
+
heard: () => {
|
|
73
|
+
heard = true;
|
|
74
|
+
},
|
|
75
|
+
resolve: (answer) => {
|
|
76
|
+
answered = true;
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
resolve(answer);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
events.emit(TA_HOST_EVENT, request);
|
|
82
|
+
if (!heard && !answered) {
|
|
83
|
+
clearTimeout(timer);
|
|
84
|
+
resolve(undefined);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
TaHostUtil2.reach = reach;
|
|
89
|
+
function marked(systemPrompt, mark) {
|
|
90
|
+
return role(systemPrompt).includes(mark);
|
|
91
|
+
}
|
|
92
|
+
TaHostUtil2.marked = marked;
|
|
93
|
+
function role(systemPrompt) {
|
|
94
|
+
if (!systemPrompt) {
|
|
95
|
+
return "";
|
|
96
|
+
}
|
|
97
|
+
const open = `<addendum>
|
|
98
|
+
`;
|
|
99
|
+
const start = systemPrompt.indexOf(open);
|
|
100
|
+
const context = systemPrompt.indexOf(`<project_context>
|
|
101
|
+
`);
|
|
102
|
+
if (start < 0 || context >= 0 && context < start) {
|
|
103
|
+
return "";
|
|
104
|
+
}
|
|
105
|
+
const end = systemPrompt.indexOf(`
|
|
106
|
+
</addendum>`, start);
|
|
107
|
+
return systemPrompt.slice(start + open.length, end < 0 ? undefined : end);
|
|
108
|
+
}
|
|
109
|
+
TaHostUtil2.role = role;
|
|
110
|
+
})(TaHostUtil || (TaHostUtil = {}));
|
|
111
|
+
var TA_WITH_OTHERS_MARK = "[ta:speaks-with-others]";
|
|
112
|
+
|
|
113
|
+
// web-src/org-protocol.ts
|
|
114
|
+
var NAME = "@celljs/ta-org";
|
|
115
|
+
var SECTION = "ta-org";
|
|
116
|
+
var ROOT_DEPT = "all";
|
|
117
|
+
var ORG_MAX_DEPTS = 60;
|
|
118
|
+
var DEPT_ID = /^[a-z0-9][a-z0-9-]{0,23}$/;
|
|
119
|
+
var OrgUtil;
|
|
120
|
+
((OrgUtil) => {
|
|
121
|
+
function deptId(value) {
|
|
122
|
+
const id = typeof value === "string" ? value.trim().toLowerCase() : "";
|
|
123
|
+
return DEPT_ID.test(id) && id !== ROOT_DEPT ? id : undefined;
|
|
124
|
+
}
|
|
125
|
+
OrgUtil.deptId = deptId;
|
|
126
|
+
function doc(value, now) {
|
|
127
|
+
const doc2 = value;
|
|
128
|
+
if (doc2?.v !== 1 || !Array.isArray(doc2.depts) || !Array.isArray(doc2.places) || typeof doc2.at !== "number") {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const ids = new Set([ROOT_DEPT]);
|
|
132
|
+
const depts = [{ id: ROOT_DEPT, name: "", parent: "", order: 0, leads: [] }];
|
|
133
|
+
for (const each of doc2.depts.slice(0, ORG_MAX_DEPTS)) {
|
|
134
|
+
const id = each?.id === ROOT_DEPT ? undefined : deptId(each?.id);
|
|
135
|
+
const name = typeof each?.name === "string" ? each.name.trim().slice(0, 40) : "";
|
|
136
|
+
if (!id || !name || ids.has(id)) {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
ids.add(id);
|
|
140
|
+
depts.push({
|
|
141
|
+
id,
|
|
142
|
+
name,
|
|
143
|
+
parent: typeof each.parent === "string" && ids.has(each.parent) ? each.parent : ROOT_DEPT,
|
|
144
|
+
order: Number.isFinite(each.order) ? Number(each.order) : depts.length,
|
|
145
|
+
leads: Array.isArray(each.leads) ? [...new Set(each.leads.filter((lead) => typeof lead === "string"))].slice(0, 20) : []
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
const places = [];
|
|
149
|
+
const seen = new Set;
|
|
150
|
+
for (const each of doc2.places.slice(0, 500)) {
|
|
151
|
+
const member = typeof each?.member === "string" ? each.member : "";
|
|
152
|
+
if (!member || seen.has(member)) {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
seen.add(member);
|
|
156
|
+
const deptsIn = Array.isArray(each.depts) ? [...new Set(each.depts.filter((dept) => ids.has(dept)))].slice(0, 8) : [];
|
|
157
|
+
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;
|
|
158
|
+
places.push({
|
|
159
|
+
member,
|
|
160
|
+
depts: deptsIn.length ? deptsIn : [ROOT_DEPT],
|
|
161
|
+
...manager ? { manager } : {},
|
|
162
|
+
at: typeof each.at === "number" ? each.at : now
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
v: 1,
|
|
167
|
+
...typeof doc2.name === "string" && doc2.name.trim() ? { name: doc2.name.trim().slice(0, 60) } : {},
|
|
168
|
+
...typeof doc2.about === "string" && doc2.about.trim() ? { about: doc2.about.trim().slice(0, 300) } : {},
|
|
169
|
+
depts,
|
|
170
|
+
places,
|
|
171
|
+
rules: OrgUtil.rules(doc2.rules),
|
|
172
|
+
at: doc2.at
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
OrgUtil.doc = doc;
|
|
176
|
+
function rules(value) {
|
|
177
|
+
const rules2 = value ?? {};
|
|
178
|
+
return {
|
|
179
|
+
...rules2.ask === false ? {} : { ask: true },
|
|
180
|
+
...rules2.review === false ? {} : { review: true },
|
|
181
|
+
...rules2.see === "dept" || rules2.see === "self" ? { see: rules2.see } : {},
|
|
182
|
+
...rules2.pin === true ? { pin: true } : {}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
OrgUtil.rules = rules;
|
|
186
|
+
function latest(docs, admin, members) {
|
|
187
|
+
const order = new Map(members.map((each, index) => [each.member, index]));
|
|
188
|
+
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];
|
|
189
|
+
}
|
|
190
|
+
OrgUtil.latest = latest;
|
|
191
|
+
function fold(docs, members) {
|
|
192
|
+
if (!docs.length) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const order = new Map(members.map((each, index) => [each.member, index]));
|
|
196
|
+
const sorted = [...docs].sort((a, b) => a.at - b.at || (order.get(a.by) ?? -1) - (order.get(b.by) ?? -1));
|
|
197
|
+
let out = sorted[0].body;
|
|
198
|
+
for (const { body } of sorted.slice(1)) {
|
|
199
|
+
const depts = new Map(out.depts.map((dept) => [dept.id, { dept, at: out.at }]));
|
|
200
|
+
for (const dept of body.depts) {
|
|
201
|
+
depts.set(dept.id, { dept, at: body.at });
|
|
202
|
+
}
|
|
203
|
+
const places = new Map(out.places.map((place) => [place.member, place]));
|
|
204
|
+
for (const place of body.places) {
|
|
205
|
+
const known = places.get(place.member);
|
|
206
|
+
if (!known || place.at >= known.at) {
|
|
207
|
+
places.set(place.member, place);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
out = {
|
|
211
|
+
v: 1,
|
|
212
|
+
...body.name !== undefined ? { name: body.name } : out.name !== undefined ? { name: out.name } : {},
|
|
213
|
+
...body.about !== undefined ? { about: body.about } : out.about !== undefined ? { about: out.about } : {},
|
|
214
|
+
depts: [...depts.values()].map(({ dept }) => dept),
|
|
215
|
+
places: [...places.values()],
|
|
216
|
+
rules: body.rules,
|
|
217
|
+
at: Math.max(out.at, body.at)
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
return out;
|
|
221
|
+
}
|
|
222
|
+
OrgUtil.fold = fold;
|
|
223
|
+
function nest(depts, id, above = false) {
|
|
224
|
+
const under = new Set([id]);
|
|
225
|
+
let grew = true;
|
|
226
|
+
while (grew) {
|
|
227
|
+
grew = false;
|
|
228
|
+
for (const dept of depts) {
|
|
229
|
+
if (!under.has(dept.id) && under.has(dept.parent)) {
|
|
230
|
+
under.add(dept.id);
|
|
231
|
+
grew = true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (!above) {
|
|
236
|
+
return [...under];
|
|
237
|
+
}
|
|
238
|
+
const all = new Set(under);
|
|
239
|
+
let parent = depts.find((dept) => dept.id === id)?.parent;
|
|
240
|
+
while (parent && !all.has(parent)) {
|
|
241
|
+
all.add(parent);
|
|
242
|
+
parent = depts.find((dept) => dept.id === parent)?.parent;
|
|
243
|
+
}
|
|
244
|
+
return [...all];
|
|
245
|
+
}
|
|
246
|
+
OrgUtil.nest = nest;
|
|
247
|
+
function sees(doc2, member) {
|
|
248
|
+
const see = doc2.rules.see ?? "all";
|
|
249
|
+
if (see === "all") {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
const place = doc2.places.find((each) => each.member === member);
|
|
253
|
+
const mine = place?.depts ?? [ROOT_DEPT];
|
|
254
|
+
return new Set(see === "self" ? mine : mine.flatMap((dept) => [...OrgUtil.nest(doc2.depts, dept), ...OrgUtil.nest(doc2.depts, dept, true)]));
|
|
255
|
+
}
|
|
256
|
+
OrgUtil.sees = sees;
|
|
257
|
+
function leads(doc2, member) {
|
|
258
|
+
const found = new Set;
|
|
259
|
+
for (const dept of doc2.places.find((each) => each.member === member)?.depts ?? [ROOT_DEPT]) {
|
|
260
|
+
for (const id of OrgUtil.nest(doc2.depts, dept, true)) {
|
|
261
|
+
for (const lead of doc2.depts.find((each) => each.id === id)?.leads ?? []) {
|
|
262
|
+
if (lead !== member) {
|
|
263
|
+
found.add(lead);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return [...found];
|
|
269
|
+
}
|
|
270
|
+
OrgUtil.leads = leads;
|
|
271
|
+
function opens(scope2, me, depts) {
|
|
272
|
+
if (scope2.kind === "all") {
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
275
|
+
if (scope2.kind === "people") {
|
|
276
|
+
return scope2.people.includes(me);
|
|
277
|
+
}
|
|
278
|
+
return depts.some((dept) => scope2.depts.includes(dept));
|
|
279
|
+
}
|
|
280
|
+
OrgUtil.opens = opens;
|
|
281
|
+
function scope(scope2) {
|
|
282
|
+
const value = scope2;
|
|
283
|
+
if (value?.kind === "depts" && Array.isArray(value.depts)) {
|
|
284
|
+
return { kind: "depts", depts: value.depts.filter((dept) => typeof dept === "string").slice(0, ORG_MAX_DEPTS) };
|
|
285
|
+
}
|
|
286
|
+
if (value?.kind === "people" && Array.isArray(value.people)) {
|
|
287
|
+
return { kind: "people", people: value.people.filter((person) => typeof person === "string").slice(0, 200) };
|
|
288
|
+
}
|
|
289
|
+
return { kind: "all" };
|
|
290
|
+
}
|
|
291
|
+
OrgUtil.scope = scope;
|
|
292
|
+
function card(value, now) {
|
|
293
|
+
const card2 = value;
|
|
294
|
+
const id = typeof card2?.id === "string" ? card2.id.trim() : "";
|
|
295
|
+
const name = typeof card2?.name === "string" ? card2.name.trim().slice(0, 60) : "";
|
|
296
|
+
if (!card2 || !id || !name) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
return {
|
|
300
|
+
id,
|
|
301
|
+
name,
|
|
302
|
+
...typeof card2.about === "string" && card2.about.trim() ? { about: card2.about.trim().slice(0, 300) } : {},
|
|
303
|
+
scope: OrgUtil.scope(card2.scope),
|
|
304
|
+
at: typeof card2.at === "number" ? card2.at : now
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
OrgUtil.card = card;
|
|
308
|
+
function roleOf(room) {
|
|
309
|
+
if (room?.status !== "member") {
|
|
310
|
+
return "none";
|
|
311
|
+
}
|
|
312
|
+
return room.owner ? "owner" : room.admin ? "admin" : "member";
|
|
313
|
+
}
|
|
314
|
+
OrgUtil.roleOf = roleOf;
|
|
315
|
+
function manages(room) {
|
|
316
|
+
return !!room && (room.owner || room.admin === true);
|
|
317
|
+
}
|
|
318
|
+
OrgUtil.manages = manages;
|
|
319
|
+
})(OrgUtil ||= {});
|
|
320
|
+
|
|
321
|
+
// web-src/extension.ts
|
|
322
|
+
var NO_SERVER = "The directory needs the Ta service: start it with `ta serve`.";
|
|
323
|
+
function text(value) {
|
|
324
|
+
return { content: [{ type: "text", text: value }] };
|
|
325
|
+
}
|
|
326
|
+
function activate(pi) {
|
|
327
|
+
let reached;
|
|
328
|
+
const reach = () => reached ??= TaHostUtil.reach(pi.events, NAME).then((answer) => ({
|
|
329
|
+
answered: !!answer,
|
|
330
|
+
service: answer?.service?.service
|
|
331
|
+
}));
|
|
332
|
+
const service = async () => {
|
|
333
|
+
const { answered, service: service2 } = await reach();
|
|
334
|
+
if (!service2) {
|
|
335
|
+
throw new Error(answered ? "The directory did not start in the Ta service; its log says why." : NO_SERVER);
|
|
336
|
+
}
|
|
337
|
+
return service2;
|
|
338
|
+
};
|
|
339
|
+
pi.on("before_agent_start", async (event, ctx) => {
|
|
340
|
+
if (TaHostUtil.marked(ctx.getSystemPrompt(), TA_WITH_OTHERS_MARK)) {
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
const section = await reach().then((found) => found.service?.section()).catch(() => {
|
|
344
|
+
return;
|
|
345
|
+
});
|
|
346
|
+
if (section) {
|
|
347
|
+
event.systemPromptOptions.sections = { ...event.systemPromptOptions.sections, [SECTION]: section };
|
|
348
|
+
}
|
|
349
|
+
return;
|
|
350
|
+
});
|
|
351
|
+
pi.registerTool({
|
|
352
|
+
name: "org_list",
|
|
353
|
+
label: "Directory",
|
|
354
|
+
description: "Lists the companies the user is in (an enterprise directory of Ta): each with its people, departments and the agents of theirs the user may pull into a group chat, as the directory says. Nothing of it is secret, but it is the company's: give it as it is.",
|
|
355
|
+
parameters: { type: "object", properties: {} },
|
|
356
|
+
execute: async () => {
|
|
357
|
+
const found = await service();
|
|
358
|
+
const section = await found.section();
|
|
359
|
+
return text(section ? section.replace(/<\/?ta-org>\n?/g, "").trim() : "The user is in no company yet. They make one, or come in by its link, in Ta → Directory.");
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
pi.registerTool({
|
|
363
|
+
name: "org_people",
|
|
364
|
+
label: "People of the company",
|
|
365
|
+
description: "Finds people in a company the user is in: by a name, a department, or a title they told. What they work on, who they report to, what they tell of themselves, and the agents of theirs in the directory.",
|
|
366
|
+
parameters: {
|
|
367
|
+
type: "object",
|
|
368
|
+
properties: {
|
|
369
|
+
company: { type: "string", description: "The company, as org_list names it." },
|
|
370
|
+
find: { type: "string", description: "A name, a department, or a word of what they do." }
|
|
371
|
+
}
|
|
372
|
+
},
|
|
373
|
+
execute: async (_id, params) => {
|
|
374
|
+
const found = await service();
|
|
375
|
+
const live = await found.space.live();
|
|
376
|
+
const company = typeof params.company === "string" ? params.company.trim() : "";
|
|
377
|
+
const find = typeof params.find === "string" ? params.find.trim().toLowerCase() : "";
|
|
378
|
+
const wanted = company ? live.filter((room) => room.title === company || room.id === company) : live;
|
|
379
|
+
if (!wanted.length) {
|
|
380
|
+
throw new Error(company ? `No company called ${company}. org_list lists them.` : "The user is in no company yet.");
|
|
381
|
+
}
|
|
382
|
+
const lines = [];
|
|
383
|
+
for (const room of wanted.slice(0, 3)) {
|
|
384
|
+
const view = await found.view({ room: room.id }).then((answer) => answer.org);
|
|
385
|
+
for (const person of view?.people ?? []) {
|
|
386
|
+
const what = [
|
|
387
|
+
person.profile?.title,
|
|
388
|
+
person.depts.map((dept) => dept.name).filter(Boolean).join("/"),
|
|
389
|
+
person.profile?.about
|
|
390
|
+
].filter(Boolean).join(" · ");
|
|
391
|
+
const line = `${person.name}${what ? ` — ${what}` : ""}${person.manager ? `(向 ${person.manager.name} 汇报)` : ""}${person.agents.length ? ` · 智能体:${person.agents.map((agent) => agent.name).join("、")}` : ""}`;
|
|
392
|
+
if (!find || line.toLowerCase().includes(find)) {
|
|
393
|
+
lines.push(line);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return text(lines.length ? lines.slice(0, 60).join(`
|
|
398
|
+
`) : "No one matches. Give the company exactly as org_list names it.");
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
pi.registerTool({
|
|
402
|
+
name: "org_pull",
|
|
403
|
+
label: "Pull an agent into a group chat",
|
|
404
|
+
description: "Asks the owner of an agent in the company's directory to bring it into a group chat that the user leads: the owner says yes in their own Ta, then their Ta joins the group chat and the agent comes in (the owner of the group chat still agrees, as for any agent). Ask the user before you ask them.",
|
|
405
|
+
parameters: {
|
|
406
|
+
type: "object",
|
|
407
|
+
properties: {
|
|
408
|
+
company: { type: "string" },
|
|
409
|
+
agent: { type: "string", description: "The agent, as /<their name>/<agent name> in org_list, or its name when only one has it." },
|
|
410
|
+
group: { type: "string", description: "The group chat, by its name." }
|
|
411
|
+
},
|
|
412
|
+
required: ["agent", "group"]
|
|
413
|
+
},
|
|
414
|
+
execute: async (_id, params, _signal, _update, ctx) => {
|
|
415
|
+
const found = await service();
|
|
416
|
+
const agent = typeof params.agent === "string" ? params.agent.trim() : "";
|
|
417
|
+
const groupName = typeof params.group === "string" ? params.group.trim() : "";
|
|
418
|
+
if (!agent || !groupName) {
|
|
419
|
+
throw new Error("Name the agent and the group chat.");
|
|
420
|
+
}
|
|
421
|
+
const live = await found.space.live();
|
|
422
|
+
const orgs = (params.company || live.length === 1) && typeof params.company === "string" ? live.filter((room) => room.title === params.company || room.id === params.company) : live;
|
|
423
|
+
const groups = await found.groups();
|
|
424
|
+
const group = groups.find((each) => each.title === groupName) ?? groups.find((each) => each.title.includes(groupName));
|
|
425
|
+
if (!group) {
|
|
426
|
+
throw new Error(`No group chat called ${groupName}. The user leads: ${groups.filter((each) => each.mine).map((each) => each.title).join(", ") || "none"}`);
|
|
427
|
+
}
|
|
428
|
+
if (!group.mine) {
|
|
429
|
+
throw new Error("Only a group chat the user leads takes an agent of the company: the owner or an admin of it pulls.");
|
|
430
|
+
}
|
|
431
|
+
for (const room of orgs.slice(0, 3)) {
|
|
432
|
+
const view = await found.view({ room: room.id }).then((answer) => answer.org);
|
|
433
|
+
const hit = view?.agents.find((each) => each.pullable && (`${each.memberName}/${each.card.name}` === agent || each.card.name === agent));
|
|
434
|
+
if (!view || !hit) {
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
if (ctx.hasUI && !await ctx.ui.confirm(...confirmPull(hit.card.name, group.title, hit.memberName, view.title)).catch(() => false)) {
|
|
438
|
+
throw new Error("The user did not want to ask.");
|
|
439
|
+
}
|
|
440
|
+
await found.space.rooms.listRooms();
|
|
441
|
+
await found.puller.pull({ org: room.id, member: hit.member, agent: hit.card.id, group: group.id });
|
|
442
|
+
return text(`Asked ${hit.memberName} to bring ${hit.card.name} into ${group.title}. Once they agree in their own Ta, they come into the group chat as a member and bring the agent with them.`);
|
|
443
|
+
}
|
|
444
|
+
throw new Error(`The directory shows no agent of the company called ${agent} that the user may pull. org_list says which.`);
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
function confirmPull(agent, group, who, company) {
|
|
449
|
+
return TaLanguageUtil.pick(TaLanguageUtil.server(), {
|
|
450
|
+
"zh-CN": ["把公司里的智能体拉进群", `问一下 ${who},把「${company}」通讯录里的 ${agent} 拉进群聊「${group}」。对方同意后,本人和 ${agent} 一起进群。`],
|
|
451
|
+
"zh-TW": ["把公司的智慧體拉進群", `問一下 ${who},把「${company}」通訊錄裡的 ${agent} 拉進群聊「${group}」。`],
|
|
452
|
+
"en-US": [
|
|
453
|
+
"Pull an agent of the company into a group chat",
|
|
454
|
+
`Ask ${who} to bring ${agent} of ${company} into “${group}”. Once they agree, they and the agent both come in.`
|
|
455
|
+
],
|
|
456
|
+
"ja-JP": [
|
|
457
|
+
"会社のエージェントをグループチャットへ",
|
|
458
|
+
`${who} さんに、${company} の ${agent} を「${group}」へ招いてよいか聞きます。同意すると本人もメンバーになります。`
|
|
459
|
+
]
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
export {
|
|
463
|
+
activate as default,
|
|
464
|
+
NO_SERVER
|
|
465
|
+
};
|