@celljs/ta-channels 4.8.0 → 4.9.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 +3 -2
- package/README.md +3 -2
- package/extension/index.mjs +109 -8
- package/host/index.mjs +281 -5
- package/package.json +13 -4
- package/web/index.js +992 -205
- package/web-src/agent-menu.tsx +0 -236
- package/web-src/channel-menu.tsx +0 -98
- package/web-src/channels-model.ts +0 -156
- package/web-src/channels-util.spec.ts +0 -312
- package/web-src/channels-util.ts +0 -556
- package/web-src/channels.ts +0 -235
- package/web-src/create.tsx +0 -273
- package/web-src/extension.ts +0 -91
- package/web-src/faces.tsx +0 -247
- package/web-src/host.ts +0 -12
- package/web-src/i18n.ts +0 -221
- package/web-src/index.tsx +0 -82
- package/web-src/members.tsx +0 -258
- package/web-src/more-menu.tsx +0 -113
- package/web-src/react.ts +0 -11
- package/web-src/room.tsx +0 -212
- package/web-src/sidebar.tsx +0 -295
- package/web-src/style.ts +0 -249
- package/web-src/team-tools.ts +0 -103
- package/web-src/team.spec.ts +0 -370
- package/web-src/team.ts +0 -560
- package/web-src/views.spec.ts +0 -89
- package/web-src/views.tsx +0 -239
package/web-src/channels.ts
DELETED
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
import type { WebStorage } from '@celljs/ta-web/lib/browser/extension/web-extension-api';
|
|
2
|
-
import {
|
|
3
|
-
type Agent,
|
|
4
|
-
type Channel,
|
|
5
|
-
type ChannelMessage,
|
|
6
|
-
ChannelsUtil,
|
|
7
|
-
type Focus,
|
|
8
|
-
Keys,
|
|
9
|
-
type MemberChange,
|
|
10
|
-
type Team,
|
|
11
|
-
type TeamRequest,
|
|
12
|
-
type TeamResult,
|
|
13
|
-
} from './channels-util';
|
|
14
|
-
|
|
15
|
-
export interface ChannelsState {
|
|
16
|
-
team: Team;
|
|
17
|
-
/** By channel id, once read. */
|
|
18
|
-
messages: Record<string, ChannelMessage[]>;
|
|
19
|
-
/** By channel id: the agents answering there now. */
|
|
20
|
-
working: Record<string, string[]>;
|
|
21
|
-
loaded: boolean;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface ChannelsClientOptions {
|
|
25
|
-
/** How long a request waits for ta serve. */
|
|
26
|
-
timeoutMs: number;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
interface Pending {
|
|
30
|
-
resolve(value: unknown): void;
|
|
31
|
-
reject(error: Error): void;
|
|
32
|
-
timer: ReturnType<typeof setTimeout>;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* The team as a page sees it. ta serve keeps and runs it (TeamService); the page reads the data of the
|
|
37
|
-
* package, hears what changes, and asks for changes with `requests/<id>`, so that closing the page stops
|
|
38
|
-
* nothing.
|
|
39
|
-
*/
|
|
40
|
-
export class ChannelsClient {
|
|
41
|
-
protected current: ChannelsState = { team: ChannelsUtil.emptyTeam(), messages: {}, working: {}, loaded: false };
|
|
42
|
-
protected readonly listeners = new Set<(state: ChannelsState) => void>();
|
|
43
|
-
protected readonly focusListeners = new Set<(channelId: string) => void>();
|
|
44
|
-
protected readonly pending = new Map<string, Pending>();
|
|
45
|
-
protected loading?: Promise<void>;
|
|
46
|
-
protected readonly options: ChannelsClientOptions;
|
|
47
|
-
protected readonly unlisten: () => void;
|
|
48
|
-
|
|
49
|
-
constructor(
|
|
50
|
-
protected readonly storage: WebStorage,
|
|
51
|
-
options: Partial<ChannelsClientOptions> = {},
|
|
52
|
-
) {
|
|
53
|
-
this.options = { timeoutMs: 30000, ...options };
|
|
54
|
-
this.unlisten = storage.onChange(keys => void this.changed(keys));
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
get state(): ChannelsState {
|
|
58
|
-
return this.current;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
subscribe(listener: (state: ChannelsState) => void): () => void {
|
|
62
|
-
this.listeners.add(listener);
|
|
63
|
-
return () => this.listeners.delete(listener);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** ta showed a channel with its tools. */
|
|
67
|
-
onFocus(listener: (channelId: string) => void): () => void {
|
|
68
|
-
this.focusListeners.add(listener);
|
|
69
|
-
return () => this.focusListeners.delete(listener);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
dispose(): void {
|
|
73
|
-
this.unlisten();
|
|
74
|
-
for (const [id, pending] of this.pending) {
|
|
75
|
-
clearTimeout(pending.timer);
|
|
76
|
-
this.pending.delete(id);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** Reads the team once; `again` reads it anew. */
|
|
81
|
-
load(again = false): Promise<void> {
|
|
82
|
-
if (!this.loading || again) {
|
|
83
|
-
this.loading = Promise.all([this.storage.get<Team>(Keys.TEAM), this.storage.get<Record<string, string[]>>(Keys.WORKING)]).then(([team, working]) =>
|
|
84
|
-
this.update({ team: team ?? ChannelsUtil.emptyTeam(), working: working ?? {}, loaded: true }),
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
return this.loading;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
async messages(channelId: string, again = false): Promise<ChannelMessage[]> {
|
|
91
|
-
const known = this.current.messages[channelId];
|
|
92
|
-
if (known && !again) {
|
|
93
|
-
return known;
|
|
94
|
-
}
|
|
95
|
-
const messages = (await this.storage.get<ChannelMessage[]>(Keys.messages(channelId))) ?? [];
|
|
96
|
-
this.update({ messages: { ...this.current.messages, [channelId]: messages } });
|
|
97
|
-
return messages;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
async createAgent(input: { name: unknown; role: unknown; cwd?: unknown; face?: unknown }): Promise<Agent> {
|
|
101
|
-
const agent = (await this.request({ kind: 'createAgent', ...input })) as Agent;
|
|
102
|
-
await this.load(true);
|
|
103
|
-
return agent;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
async createChannel(input: { name: unknown; members: unknown; topic?: unknown }): Promise<Channel> {
|
|
107
|
-
const channel = (await this.request({ kind: 'createChannel', ...input })) as Channel;
|
|
108
|
-
await this.load(true);
|
|
109
|
-
return channel;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
async addMembers(channelId: string, members: string[]): Promise<Channel> {
|
|
113
|
-
const channel = (await this.request({ kind: 'addMembers', channel: channelId, members })) as Channel;
|
|
114
|
-
await this.load(true);
|
|
115
|
-
return channel;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async changeMember(channelId: string, agentId: string, change: MemberChange): Promise<Channel> {
|
|
119
|
-
const channel = (await this.request({ kind: 'changeMember', channel: channelId, agent: agentId, change })) as Channel;
|
|
120
|
-
await this.load(true);
|
|
121
|
-
return channel;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
async updateChannel(channelId: string, change: { name?: string; topic?: string; pinned?: boolean }): Promise<Channel> {
|
|
125
|
-
const channel = (await this.request({ kind: 'updateChannel', channel: channelId, ...change })) as Channel;
|
|
126
|
-
await this.load(true);
|
|
127
|
-
return channel;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
async deleteChannel(channelId: string): Promise<void> {
|
|
131
|
-
await this.request({ kind: 'deleteChannel', channel: channelId });
|
|
132
|
-
const { [channelId]: _gone, ...messages } = this.current.messages;
|
|
133
|
-
this.update({ messages });
|
|
134
|
-
await this.load(true);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/** The agent answers next in a new session. */
|
|
138
|
-
async resetAgent(agentId: string): Promise<Agent> {
|
|
139
|
-
const agent = (await this.request({ kind: 'resetAgent', agent: agentId })) as Agent;
|
|
140
|
-
await this.load(true);
|
|
141
|
-
return agent;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
async updateAgent(agentId: string, change: { name?: string; role?: string; face?: number; cwd?: string }): Promise<Agent> {
|
|
145
|
-
const agent = (await this.request({ kind: 'updateAgent', agent: agentId, ...change })) as Agent;
|
|
146
|
-
await this.load(true);
|
|
147
|
-
return agent;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
async deleteAgent(agentId: string): Promise<void> {
|
|
151
|
-
await this.request({ kind: 'deleteAgent', agent: agentId });
|
|
152
|
-
await this.load(true);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** The session of an agent, opened on the server if it is not, for the page to show. */
|
|
156
|
-
async openAgent(agentId: string): Promise<string> {
|
|
157
|
-
const { sessionId } = (await this.request({ kind: 'openAgent', agent: agentId })) as { sessionId: string };
|
|
158
|
-
return sessionId;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/** Posts as the user; the answers come as the data changes. */
|
|
162
|
-
async post(channelId: string, text: string): Promise<void> {
|
|
163
|
-
await this.request({ kind: 'post', channel: channelId, text });
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
protected request(request: TeamRequest): Promise<unknown> {
|
|
167
|
-
const id = ChannelsUtil.newId('request');
|
|
168
|
-
const result = new Promise<unknown>((resolve, reject) => {
|
|
169
|
-
const timer = setTimeout(() => {
|
|
170
|
-
this.pending.delete(id);
|
|
171
|
-
reject(new Error('The Ta service did not answer: the extension may be off, or the service still starting.'));
|
|
172
|
-
}, this.options.timeoutMs);
|
|
173
|
-
this.pending.set(id, { resolve, reject, timer });
|
|
174
|
-
});
|
|
175
|
-
return this.storage.set(Keys.request(id), request).then(() => {
|
|
176
|
-
// The result may have come before the notice.
|
|
177
|
-
void this.take(id);
|
|
178
|
-
return result;
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
protected async take(id: string): Promise<void> {
|
|
183
|
-
if (!this.pending.has(id)) {
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
const result = await this.storage.get<TeamResult>(Keys.result(id));
|
|
187
|
-
const pending = this.pending.get(id);
|
|
188
|
-
if (!result || !pending) {
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
this.pending.delete(id);
|
|
192
|
-
clearTimeout(pending.timer);
|
|
193
|
-
void this.storage.set(Keys.result(id), null).catch(() => undefined);
|
|
194
|
-
if (result.ok) {
|
|
195
|
-
pending.resolve(result.value);
|
|
196
|
-
} else {
|
|
197
|
-
pending.reject(new Error(result.error));
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
protected async changed(keys: string[]): Promise<void> {
|
|
202
|
-
const reads: Promise<unknown>[] = [];
|
|
203
|
-
for (const key of keys) {
|
|
204
|
-
if (key === Keys.TEAM || key === Keys.WORKING) {
|
|
205
|
-
reads.push(this.load(true));
|
|
206
|
-
} else if (key.startsWith(Keys.MESSAGES)) {
|
|
207
|
-
const channelId = key.slice(Keys.MESSAGES.length);
|
|
208
|
-
if (this.current.messages[channelId]) {
|
|
209
|
-
reads.push(this.messages(channelId, true));
|
|
210
|
-
}
|
|
211
|
-
} else if (key.startsWith(Keys.RESULTS)) {
|
|
212
|
-
reads.push(this.take(key.slice(Keys.RESULTS.length)));
|
|
213
|
-
} else if (key === Keys.FOCUS) {
|
|
214
|
-
reads.push(
|
|
215
|
-
this.storage.get<Focus>(Keys.FOCUS).then(async focus => {
|
|
216
|
-
if (focus) {
|
|
217
|
-
await this.load(true);
|
|
218
|
-
for (const listener of this.focusListeners) {
|
|
219
|
-
listener(focus.channelId);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
}),
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
await Promise.all(reads).catch(error => console.error(error));
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
protected update(change: Partial<ChannelsState>): void {
|
|
230
|
-
this.current = { ...this.current, ...change };
|
|
231
|
-
for (const listener of this.listeners) {
|
|
232
|
-
listener(this.current);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}
|
package/web-src/create.tsx
DELETED
|
@@ -1,273 +0,0 @@
|
|
|
1
|
-
import { type Agent, ChannelsUtil, DEFAULT_ROLE, FACES, MAX_MEMBERS, NAME } from './channels-util';
|
|
2
|
-
import { Face } from './faces';
|
|
3
|
-
import { ChannelWords } from './i18n';
|
|
4
|
-
import { React } from './react';
|
|
5
|
-
import { AgentAvatar, type ChannelsContext, ChannelViews, SidebarUtil, useChannels, useFaces, useUi } from './views';
|
|
6
|
-
|
|
7
|
-
/** The page of an agent being made: shown from + in the side bar, made by its first message. */
|
|
8
|
-
export const AGENT_PAGE = `${NAME}/agent`;
|
|
9
|
-
|
|
10
|
-
/** Starts an agent: a name and a face no other agent has, on its page. */
|
|
11
|
-
export function newAgent(context: ChannelsContext): void {
|
|
12
|
-
const { pi, service, ui } = context;
|
|
13
|
-
if (!ui.state.draft) {
|
|
14
|
-
const team = service.state.team;
|
|
15
|
-
const name = ChannelsUtil.freeName(team, ChannelViews.words(pi).newAgentName);
|
|
16
|
-
ui.set({ draft: { name, face: ChannelsUtil.freeFaceFor(team.agents, ChannelsUtil.newId('draft')) } });
|
|
17
|
-
}
|
|
18
|
-
pi.ui.showView(AGENT_PAGE);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** The name and the face of the agent being made, in a card under what opened it. */
|
|
22
|
-
function Identity({ context, onClose }: { context: ChannelsContext; onClose(): void }) {
|
|
23
|
-
const { pi, ui } = context;
|
|
24
|
-
const { draft } = useUi(ui);
|
|
25
|
-
const w = ChannelViews.words(pi);
|
|
26
|
-
const ref = React.useRef<HTMLDivElement>(null);
|
|
27
|
-
const name = React.useRef<HTMLInputElement>(null);
|
|
28
|
-
React.useEffect(() => name.current?.select(), []);
|
|
29
|
-
React.useEffect(() => {
|
|
30
|
-
const close = (event: Event) => {
|
|
31
|
-
const target = event.target as Element;
|
|
32
|
-
if (!ref.current?.contains(target) && !target.closest?.('.tac-identity-open')) {
|
|
33
|
-
onClose();
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
const onKey = (event: KeyboardEvent) => event.key === 'Escape' && onClose();
|
|
37
|
-
document.addEventListener('pointerdown', close);
|
|
38
|
-
document.addEventListener('keydown', onKey);
|
|
39
|
-
return () => {
|
|
40
|
-
document.removeEventListener('pointerdown', close);
|
|
41
|
-
document.removeEventListener('keydown', onKey);
|
|
42
|
-
};
|
|
43
|
-
}, [onClose]);
|
|
44
|
-
if (!draft) {
|
|
45
|
-
return null;
|
|
46
|
-
}
|
|
47
|
-
return (
|
|
48
|
-
<div className="tac-identity" ref={ref} role="dialog" aria-label={w.editAgent}>
|
|
49
|
-
<label className="tac-identity-label">
|
|
50
|
-
{w.name}
|
|
51
|
-
<input
|
|
52
|
-
className="tac-input"
|
|
53
|
-
value={draft.name}
|
|
54
|
-
maxLength={40}
|
|
55
|
-
ref={name}
|
|
56
|
-
onChange={event => ui.set({ draft: { ...draft, name: event.target.value } })}
|
|
57
|
-
onKeyDown={event => event.key === 'Enter' && onClose()}
|
|
58
|
-
/>
|
|
59
|
-
</label>
|
|
60
|
-
<div className="tac-identity-label">{w.avatar}</div>
|
|
61
|
-
<div className="tac-faces">
|
|
62
|
-
{Array.from({ length: FACES }, (_, face) => (
|
|
63
|
-
<button
|
|
64
|
-
key={face}
|
|
65
|
-
type="button"
|
|
66
|
-
aria-pressed={draft.face === face}
|
|
67
|
-
className={`tac-face-pick${draft.face === face ? ' tac-on' : ''}`}
|
|
68
|
-
onClick={() => ui.set({ draft: { ...draft, face } })}
|
|
69
|
-
>
|
|
70
|
-
<Face face={face} size={30} />
|
|
71
|
-
</button>
|
|
72
|
-
))}
|
|
73
|
-
</div>
|
|
74
|
-
</div>
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** The middle of the bar above the agent being made: its face and name, which open the card to change them; Cancel. */
|
|
79
|
-
export function NewAgentHead({ context }: { context: ChannelsContext }) {
|
|
80
|
-
const { pi, ui } = context;
|
|
81
|
-
const { draft, editing } = useUi(ui);
|
|
82
|
-
const w = ChannelViews.words(pi);
|
|
83
|
-
const Down = ChannelViews.icon(pi, 'ChevronDown');
|
|
84
|
-
if (!draft) {
|
|
85
|
-
return null;
|
|
86
|
-
}
|
|
87
|
-
return (
|
|
88
|
-
<div className="tac-new-head">
|
|
89
|
-
<div className="tac-new-title">
|
|
90
|
-
<button
|
|
91
|
-
type="button"
|
|
92
|
-
className="tac-new-trigger tac-identity-open"
|
|
93
|
-
aria-expanded={editing}
|
|
94
|
-
title={w.editAgent}
|
|
95
|
-
onClick={() => ui.set({ editing: !editing })}
|
|
96
|
-
>
|
|
97
|
-
<Face face={draft.face} size={20} />
|
|
98
|
-
<span>{draft.name || w.newAgentName}</span>
|
|
99
|
-
<Down size={14} />
|
|
100
|
-
</button>
|
|
101
|
-
{editing && <Identity context={context} onClose={() => ui.set({ editing: false })} />}
|
|
102
|
-
</div>
|
|
103
|
-
<button
|
|
104
|
-
type="button"
|
|
105
|
-
className="tac-button tac-quiet"
|
|
106
|
-
onClick={() => {
|
|
107
|
-
ui.set({ draft: undefined, editing: false });
|
|
108
|
-
pi.ui.showView('');
|
|
109
|
-
}}
|
|
110
|
-
>
|
|
111
|
-
{w.cancel}
|
|
112
|
-
</button>
|
|
113
|
-
</div>
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
/** The agent being made: its face and name, and a box whose first message makes it and starts its session, in the directory beside its +. */
|
|
118
|
-
export function NewAgentPage({ context }: { context: ChannelsContext }) {
|
|
119
|
-
const { pi, service, ui } = context;
|
|
120
|
-
const { draft } = useUi(ui);
|
|
121
|
-
const w = ChannelViews.words(pi);
|
|
122
|
-
const Pencil = ChannelViews.icon(pi, 'Pencil');
|
|
123
|
-
const Composer = pi.ui.Composer;
|
|
124
|
-
// Shown again after a reload, when what was being made is gone: a new one.
|
|
125
|
-
React.useEffect(() => {
|
|
126
|
-
if (!draft) {
|
|
127
|
-
newAgent(context);
|
|
128
|
-
}
|
|
129
|
-
}, [draft, context]);
|
|
130
|
-
if (!draft) {
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
133
|
-
const send = async (text: string) => {
|
|
134
|
-
const agent = await service.createAgent({ name: draft.name, role: DEFAULT_ROLE, face: draft.face, cwd: draft.cwd });
|
|
135
|
-
ui.set({ draft: undefined, editing: false });
|
|
136
|
-
try {
|
|
137
|
-
const sessionId = await service.openAgent(agent.id);
|
|
138
|
-
context.sessions.set(agent.id, sessionId);
|
|
139
|
-
await pi.switchSession(sessionId);
|
|
140
|
-
await pi.sendUserMessage(text);
|
|
141
|
-
} catch (error) {
|
|
142
|
-
pi.ui.notify(ChannelViews.message(error), 'error');
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
return (
|
|
146
|
-
<div className="tac-new-page">
|
|
147
|
-
<div className="tac-new-col">
|
|
148
|
-
<Face face={draft.face} size={88} />
|
|
149
|
-
<h2 className="tac-new-name">
|
|
150
|
-
<span>{draft.name || w.newAgentName}</span>
|
|
151
|
-
<button
|
|
152
|
-
type="button"
|
|
153
|
-
className="tac-action tac-identity-open"
|
|
154
|
-
title={w.editAgent}
|
|
155
|
-
aria-label={w.editAgent}
|
|
156
|
-
onClick={() => ui.set({ editing: !ui.state.editing })}
|
|
157
|
-
>
|
|
158
|
-
<Pencil size={14} />
|
|
159
|
-
</button>
|
|
160
|
-
</h2>
|
|
161
|
-
<p className="tac-new-hint">{w.newAgentHint}</p>
|
|
162
|
-
<div className="tac-new-box">
|
|
163
|
-
<Composer
|
|
164
|
-
placeholder={ChannelWords.format(w.agentPlaceholder, { name: draft.name || w.newAgentName })}
|
|
165
|
-
onSend={send}
|
|
166
|
-
cwd={draft.cwd}
|
|
167
|
-
onPickCwd={cwd => ui.set({ draft: { ...draft, cwd } })}
|
|
168
|
-
/>
|
|
169
|
-
</div>
|
|
170
|
-
</div>
|
|
171
|
-
</div>
|
|
172
|
-
);
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/** A dialog to make a channel: its name and the agents in it, the first picked leading. */
|
|
176
|
-
export function NewChannelDialog({ context, onClose }: { context: ChannelsContext; onClose(): void }) {
|
|
177
|
-
const { pi, service } = context;
|
|
178
|
-
const { team } = useChannels(service);
|
|
179
|
-
const faces = useFaces(team);
|
|
180
|
-
const w = ChannelViews.words(pi);
|
|
181
|
-
const Search = ChannelViews.icon(pi, 'Search');
|
|
182
|
-
const Check = ChannelViews.icon(pi, 'Check');
|
|
183
|
-
const [name, setName] = React.useState('');
|
|
184
|
-
const [query, setQuery] = React.useState('');
|
|
185
|
-
const [picked, setPicked] = React.useState<string[]>([]);
|
|
186
|
-
const [busy, setBusy] = React.useState(false);
|
|
187
|
-
const [error, setError] = React.useState<string>();
|
|
188
|
-
const Dialog = pi.ui.Dialog;
|
|
189
|
-
const shown = team.agents.filter(agent => SidebarUtil.matches(query, agent.name, agent.role));
|
|
190
|
-
const full = picked.length >= MAX_MEMBERS;
|
|
191
|
-
const toggle = (agent: Agent) => setPicked(picked.includes(agent.id) ? picked.filter(id => id !== agent.id) : full ? picked : [...picked, agent.id]);
|
|
192
|
-
const create = (event: React.FormEvent) => {
|
|
193
|
-
event.preventDefault();
|
|
194
|
-
setBusy(true);
|
|
195
|
-
setError(undefined);
|
|
196
|
-
service.createChannel({ name, members: picked }).then(
|
|
197
|
-
channel => {
|
|
198
|
-
onClose();
|
|
199
|
-
context.showChannel(channel.id);
|
|
200
|
-
},
|
|
201
|
-
failure => {
|
|
202
|
-
setBusy(false);
|
|
203
|
-
setError(ChannelViews.message(failure));
|
|
204
|
-
},
|
|
205
|
-
);
|
|
206
|
-
};
|
|
207
|
-
return (
|
|
208
|
-
<Dialog title={w.newChannel} onClose={onClose}>
|
|
209
|
-
<form className="tac-dialog" onSubmit={create}>
|
|
210
|
-
<label className="tac-dialog-label">
|
|
211
|
-
{w.name}
|
|
212
|
-
<input
|
|
213
|
-
className="tac-input tac-dialog-input"
|
|
214
|
-
value={name}
|
|
215
|
-
placeholder={w.channelNamePlaceholder}
|
|
216
|
-
maxLength={40}
|
|
217
|
-
autoFocus
|
|
218
|
-
onChange={event => setName(event.target.value)}
|
|
219
|
-
/>
|
|
220
|
-
</label>
|
|
221
|
-
<div className="tac-dialog-label">
|
|
222
|
-
<span>{w.addAgents}</span>
|
|
223
|
-
<span className="tac-dialog-count">
|
|
224
|
-
{picked.length}/{MAX_MEMBERS}
|
|
225
|
-
</span>
|
|
226
|
-
</div>
|
|
227
|
-
<label className="tac-dialog-search">
|
|
228
|
-
<Search size={15} />
|
|
229
|
-
<input
|
|
230
|
-
value={query}
|
|
231
|
-
placeholder={ChannelWords.format(w.searchAgents, { max: String(MAX_MEMBERS) })}
|
|
232
|
-
aria-label={w.search}
|
|
233
|
-
onChange={event => setQuery(event.target.value)}
|
|
234
|
-
/>
|
|
235
|
-
</label>
|
|
236
|
-
<div className="tac-pick-list">
|
|
237
|
-
{team.agents.length === 0 && <div className="tac-empty">{w.noAgents}</div>}
|
|
238
|
-
{team.agents.length > 0 && shown.length === 0 && <div className="tac-empty">{w.noMatch}</div>}
|
|
239
|
-
{shown.map(agent => {
|
|
240
|
-
const on = picked.includes(agent.id);
|
|
241
|
-
return (
|
|
242
|
-
<button
|
|
243
|
-
key={agent.id}
|
|
244
|
-
type="button"
|
|
245
|
-
aria-pressed={on}
|
|
246
|
-
disabled={!on && full}
|
|
247
|
-
className={`tac-pick${on ? ' tac-on' : ''}`}
|
|
248
|
-
onClick={() => toggle(agent)}
|
|
249
|
-
>
|
|
250
|
-
<span className="tac-check">{on && <Check size={13} strokeWidth={3} />}</span>
|
|
251
|
-
<AgentAvatar agent={agent} faces={faces} size={30} />
|
|
252
|
-
<span className="tac-pick-text">
|
|
253
|
-
<span className="tac-pick-name">
|
|
254
|
-
{agent.name}
|
|
255
|
-
{picked[0] === agent.id && <span className="tac-msg-lead">{w.lead}</span>}
|
|
256
|
-
</span>
|
|
257
|
-
<span className="tac-pick-role">{agent.role.split('\n')[0]}</span>
|
|
258
|
-
</span>
|
|
259
|
-
</button>
|
|
260
|
-
);
|
|
261
|
-
})}
|
|
262
|
-
</div>
|
|
263
|
-
{picked.length > 0 && <div className="tac-dialog-note">{w.membersHint}</div>}
|
|
264
|
-
{error && <div className="tac-empty tac-error">{error}</div>}
|
|
265
|
-
<div className="tac-dialog-actions">
|
|
266
|
-
<button type="submit" className="tac-button tac-primary" disabled={busy || !name.trim() || picked.length === 0}>
|
|
267
|
-
{w.create}
|
|
268
|
-
</button>
|
|
269
|
-
</div>
|
|
270
|
-
</form>
|
|
271
|
-
</Dialog>
|
|
272
|
-
);
|
|
273
|
-
}
|
package/web-src/extension.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { AGENT_MARK, NAME } from './channels-util';
|
|
2
|
-
import type { TeamService } from './team';
|
|
3
|
-
import { TeamTools } from './team-tools';
|
|
4
|
-
|
|
5
|
-
/** TA_HOST_EVENT of @celljs/ta-protocol, which this bundle does not import. */
|
|
6
|
-
const TA_HOST_EVENT = 'ta:host';
|
|
7
|
-
/**
|
|
8
|
-
* Under ta serve the host answers at once; plain `ta` has nobody to answer. The timer keeps the process
|
|
9
|
-
* alive meanwhile: a tool waiting for it in `ta -p` would otherwise end the process.
|
|
10
|
-
*/
|
|
11
|
-
const HOST_WAIT_MS = 1000;
|
|
12
|
-
|
|
13
|
-
export const NO_SERVER =
|
|
14
|
-
'The team tools need the Ta service, which runs the agents: start it with `ta serve` and talk to Ta in its web page. In Ta in a terminal there are no agents or channels.';
|
|
15
|
-
export const NO_TEAM = 'The team did not start in the Ta service; its log says why.';
|
|
16
|
-
export const NOT_FOR_AGENTS = 'Agents do not build teams or post to channels: answer in your reply, and name a member with @ to hand on.';
|
|
17
|
-
|
|
18
|
-
/** The little of pi's extension API this uses. */
|
|
19
|
-
interface Context {
|
|
20
|
-
getSystemPrompt(): string;
|
|
21
|
-
}
|
|
22
|
-
interface Pi {
|
|
23
|
-
events: { emit(event: string, data: unknown): void };
|
|
24
|
-
on(event: string, handler: (event: never, ctx: never) => unknown): unknown;
|
|
25
|
-
registerTool(tool: never): unknown;
|
|
26
|
-
getActiveTools(): string[];
|
|
27
|
-
setActiveTools(names: string[]): void;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The pi extension: ta's tools to build a team and talk to it, in every session but those of agents. They
|
|
32
|
-
* work with the team of ta serve (host.ts); without it they say so.
|
|
33
|
-
*/
|
|
34
|
-
export default function activate(pi: Pi): void {
|
|
35
|
-
let reached: Promise<{ answered: boolean; team?: TeamService }> | undefined;
|
|
36
|
-
const reach = () =>
|
|
37
|
-
(reached ??= new Promise(resolve => {
|
|
38
|
-
const timer = setTimeout(() => resolve({ answered: false }), HOST_WAIT_MS);
|
|
39
|
-
pi.events.emit(TA_HOST_EVENT, {
|
|
40
|
-
name: NAME,
|
|
41
|
-
resolve: (answer: { service?: unknown }) => {
|
|
42
|
-
clearTimeout(timer);
|
|
43
|
-
resolve({ answered: true, team: answer.service as TeamService | undefined });
|
|
44
|
-
},
|
|
45
|
-
});
|
|
46
|
-
}));
|
|
47
|
-
const isAgent = (ctx: Context) => ctx.getSystemPrompt().includes(AGENT_MARK);
|
|
48
|
-
pi.on('session_start', () => {
|
|
49
|
-
void reach();
|
|
50
|
-
});
|
|
51
|
-
pi.on('before_agent_start', (_event: unknown, ctx: Context) => {
|
|
52
|
-
if (isAgent(ctx)) {
|
|
53
|
-
pi.setActiveTools(pi.getActiveTools().filter(name => !TeamTools.NAMES.includes(name)));
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
for (const tool of TeamTools.ALL) {
|
|
57
|
-
pi.registerTool(<never>{
|
|
58
|
-
name: tool.name,
|
|
59
|
-
label: tool.label,
|
|
60
|
-
description: tool.description,
|
|
61
|
-
parameters: tool.parameters,
|
|
62
|
-
execute: async (_id: string, params: Record<string, unknown>, signal: AbortSignal | undefined, _update: unknown, ctx: Context) => {
|
|
63
|
-
if (isAgent(ctx)) {
|
|
64
|
-
throw new Error(NOT_FOR_AGENTS);
|
|
65
|
-
}
|
|
66
|
-
const { answered, team } = await reach();
|
|
67
|
-
if (!team) {
|
|
68
|
-
throw new Error(answered ? NO_TEAM : NO_SERVER);
|
|
69
|
-
}
|
|
70
|
-
const text = await stoppable(tool.run(team, params ?? {}), signal);
|
|
71
|
-
return { content: [{ type: 'text', text }], details: undefined };
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** Stopping ta stops its waiting; the agents go on in the channel. */
|
|
78
|
-
function stoppable(work: Promise<string>, signal: AbortSignal | undefined): Promise<string> {
|
|
79
|
-
if (!signal) {
|
|
80
|
-
return work;
|
|
81
|
-
}
|
|
82
|
-
return new Promise((resolve, reject) => {
|
|
83
|
-
const stop = () => resolve('Stopped waiting; the agents go on in the channel, where their answers appear.');
|
|
84
|
-
if (signal.aborted) {
|
|
85
|
-
stop();
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
signal.addEventListener('abort', stop, { once: true });
|
|
89
|
-
work.then(resolve, reject).finally(() => signal.removeEventListener('abort', stop));
|
|
90
|
-
});
|
|
91
|
-
}
|