@copilotz/team 0.0.0-stage โ†’ 1.0.0-rc.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +49 -2
  3. package/WORDS.md +200 -0
  4. package/contract.d.ts +8 -0
  5. package/contract.js +8 -0
  6. package/mod.d.ts +25 -0
  7. package/mod.js +25 -0
  8. package/package.json +63 -4
  9. package/team/actions/agent.d.ts +4 -0
  10. package/team/actions/agent.js +179 -0
  11. package/team/actions/define.d.ts +1 -0
  12. package/team/actions/define.js +27 -0
  13. package/team/collections/agent.d.ts +132 -0
  14. package/team/collections/agent.js +75 -0
  15. package/team/contract.d.ts +10 -0
  16. package/team/contract.js +1 -0
  17. package/team/mod.d.ts +21 -0
  18. package/team/mod.js +49 -0
  19. package/team/shared/definition.d.ts +3 -0
  20. package/team/shared/definition.js +10 -0
  21. package/team/views/web/agent-add.d.ts +14 -0
  22. package/team/views/web/agent-add.js +48 -0
  23. package/team/views/web/agent-fields.d.ts +33 -0
  24. package/team/views/web/agent-fields.js +88 -0
  25. package/team/views/web/agent-known.d.ts +22 -0
  26. package/team/views/web/agent-known.js +61 -0
  27. package/team/views/web/agent-new.d.ts +24 -0
  28. package/team/views/web/agent-new.js +129 -0
  29. package/team/views/web/agent-palette.d.ts +19 -0
  30. package/team/views/web/agent-palette.js +203 -0
  31. package/team/views/web/agent-steps.d.ts +23 -0
  32. package/team/views/web/agent-steps.js +56 -0
  33. package/team/views/web/agent.d.ts +17 -0
  34. package/team/views/web/agent.js +257 -0
  35. package/team/views/web/agents.d.ts +48 -0
  36. package/team/views/web/agents.js +59 -0
  37. package/team/views/web/model-pick.d.ts +25 -0
  38. package/team/views/web/model-pick.js +39 -0
  39. package/team/views/web/model-picker.d.ts +57 -0
  40. package/team/views/web/model-picker.js +218 -0
  41. package/team/views/web/places.d.ts +24 -0
  42. package/team/views/web/places.js +10 -0
  43. package/team/views/web/style.d.ts +9 -0
  44. package/team/views/web/style.js +246 -0
  45. package/team/views/web/words.d.ts +5 -0
  46. package/team/views/web/words.js +211 -0
  47. package/team/web.d.ts +22 -0
  48. package/team/web.js +41 -0
  49. package/tiers/actions/choose.d.ts +2 -0
  50. package/tiers/actions/choose.js +98 -0
  51. package/tiers/actions/model.d.ts +1 -0
  52. package/tiers/actions/model.js +36 -0
  53. package/tiers/collections/choice.d.ts +48 -0
  54. package/tiers/collections/choice.js +59 -0
  55. package/tiers/contract.d.ts +19 -0
  56. package/tiers/contract.js +1 -0
  57. package/tiers/mod.d.ts +54 -0
  58. package/tiers/mod.js +92 -0
  59. package/tiers/shared/shown.d.ts +87 -0
  60. package/tiers/shared/shown.js +155 -0
  61. package/tiers/shared/tiers.d.ts +103 -0
  62. package/tiers/shared/tiers.js +129 -0
  63. package/tiers/views/web/org.d.ts +38 -0
  64. package/tiers/views/web/org.js +142 -0
  65. package/tiers/views/web/space.d.ts +43 -0
  66. package/tiers/views/web/space.js +103 -0
  67. package/tiers/views/web/style.d.ts +7 -0
  68. package/tiers/views/web/style.js +87 -0
  69. package/tiers/views/web/words.d.ts +21 -0
  70. package/tiers/views/web/words.js +49 -0
  71. package/tiers/web.d.ts +18 -0
  72. package/tiers/web.js +40 -0
  73. package/web.d.ts +14 -0
  74. package/web.js +14 -0
@@ -0,0 +1,257 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ /**
3
+ * An agent's page (kind `agent`, at `/agent/<id>`): who it is (its emoji,
4
+ * its name and what it's called by, what it does, where it belongs), where
5
+ * it works among the person's spaces (its role there, and where it answers
6
+ * new chats), and what it's told: its instructions, model and tools. "Ask"
7
+ * starts a chat where it works, `@name` ready. Those who manage where it
8
+ * belongs change it in place (`?edit=1` opens it so) and delete it, after
9
+ * a question that says it leaves every space it works in. The app's own
10
+ * agents are read only; a manager starts a new agent from one. Its menu
11
+ * adds it to another space they manage, and has it answer new chats where
12
+ * the person is.
13
+ */
14
+ import { useState } from "react";
15
+ import { Avatar, Badge, Button, Card, DetailList, Dialog, EmptyState, go, Icon, IconButton, LongText, Menu, message, Notice, openPalette, Page, Skeleton, Title, } from "copilotz/web";
16
+ import { AgentFields } from "./agent-fields.js";
17
+ import { spaceStep } from "./agent-steps.js";
18
+ import { agentPath, listed, shownAs } from "./agents.js";
19
+ import { managed, manages, placeName, placePath } from "./places.js";
20
+ import { STYLE } from "./style.js";
21
+ import { say } from "./words.js";
22
+ export const agentView = (item, ctx) => {
23
+ const id = item._id ?? "";
24
+ const here = typeof ctx.hints.in === "string" ? ctx.hints.in : undefined;
25
+ const roster = ctx.db.collection("thread").query("agents", {
26
+ ...(here ? { in: here } : {}),
27
+ names: [id],
28
+ }).value;
29
+ const places = ctx.db.collection("space").query("reached", {}).value;
30
+ const options = ctx.db.collection("agent").query("options", {}).value;
31
+ const seats = ctx.db.collection("member").find({ member: `agent:${id}` }).value;
32
+ const style = _jsx("style", { href: "team-web", precedence: "copilotz", children: STYLE });
33
+ if (!roster || !places || !options || !seats) {
34
+ return (_jsxs(Page, { title: _jsx(Skeleton, { width: 200, height: 30 }), leading: _jsx(Skeleton, { width: 52, height: 52 }), children: [style, _jsxs("div", { className: "agent-page", "aria-busy": "true", children: [_jsx(Skeleton, { height: 64, radius: "xl" }), _jsx(Skeleton, { height: 160, radius: "xl" })] })] }));
35
+ }
36
+ const agent = roster.agents[id];
37
+ if (!agent) {
38
+ return (_jsxs("div", { className: "agent-missing", children: [style, _jsx(Title, { text: say("noSuchAgentTitle") }), _jsx(EmptyState, { icon: "bot", heading: "h1", title: say("noSuchAgent"), action: _jsx(Button, { variant: "primary", icon: "compose", href: "/", children: say("goHome") }), children: say("noSuchAgentBody") })] }));
39
+ }
40
+ const query = (ctx.hints.query ?? {});
41
+ return (_jsxs(_Fragment, { children: [style, _jsx(AgentPage, { id: id, agent: agent, here: places.find((p) => p._id === here), answers: !!here && roster.here.includes(id) && roster.default.includes(id), places: places, seats: seats.filter((s) => !s.on), options: options, editing: query.edit === "1", call: ctx.call }, id)] }));
42
+ };
43
+ function AgentPage({ id, agent, here, answers, places, seats, options, editing: asked, call }) {
44
+ const { name, icon } = shownAs(id, agent);
45
+ const home = agent.in ? places.find((p) => p._id === agent.in) : undefined;
46
+ const edits = !!home && manages(home);
47
+ const [editing, setEditing] = useState(asked && edits);
48
+ const [deleting, setDeleting] = useState(false);
49
+ const [error, setError] = useState();
50
+ const app = !agent.in;
51
+ const placeOf = (space) => places.find((p) => p._id === space);
52
+ const working = seats.flatMap((s) => {
53
+ const place = placeOf(s.space);
54
+ return place ? [{ seat: s, place }] : [];
55
+ });
56
+ const mine = managed(places);
57
+ const elsewhere = mine.filter((p) => !seats.some((s) => s.space === p._id));
58
+ const seatHere = here && seats.find((s) => s.space === here._id);
59
+ // Back to the space the person is in, if it works there; else to where it belongs.
60
+ const back = seatHere ? here : home;
61
+ // Where asking it starts a chat: here if it answers chats here, else where it does.
62
+ const ask = seatHere?.role === "editor"
63
+ ? here
64
+ : working.find((w) => w.seat.role === "editor" && !w.place.archived)?.place;
65
+ const askAt = (place) => `/?in=${encodeURIComponent(place._id)}&to=${encodeURIComponent(id)}`;
66
+ const done = () => {
67
+ setEditing(false);
68
+ if (asked)
69
+ go(agentPath(id));
70
+ };
71
+ const addStep = () => openPalette(spaceStep({
72
+ title: say("addToTitle", { name }),
73
+ note: say("addToNote"),
74
+ places: elsewhere,
75
+ pick: (place) => call("member.set", {
76
+ space: place._id,
77
+ member: { kind: "agent", id },
78
+ role: "editor",
79
+ }),
80
+ }));
81
+ const fromStep = () => openPalette(spaceStep({
82
+ title: say("newLikeTitle", { name }),
83
+ note: say("newLikeNote"),
84
+ places: mine,
85
+ pick: (place, palette) => palette.dialog({
86
+ title: say("newAgentIn", { place: placeName(place) }),
87
+ description: say("startingFrom", { name }),
88
+ view: "agent.add",
89
+ item: { _id: place._id },
90
+ hints: { in: place._id, mode: "new", from: id },
91
+ }),
92
+ }));
93
+ const more = [
94
+ ...(here && seatHere?.role === "editor" && !answers && manages(here)
95
+ ? [{
96
+ label: say("makeAnswerIn", { place: placeName(here) }),
97
+ icon: "chat",
98
+ onSelect: () => void call("space.agent", { space: here._id, agent: id }).catch((e) => setError(message(e))),
99
+ }]
100
+ : []),
101
+ ...(elsewhere.length
102
+ ? [{ label: say("addToSpaceMenu"), icon: "plus", onSelect: addStep }]
103
+ : []),
104
+ ...(app && mine.length
105
+ ? [{ label: say("startFromIt"), icon: "sparkles", onSelect: fromStep }]
106
+ : []),
107
+ ...(edits
108
+ ? [
109
+ "separator",
110
+ {
111
+ label: say("deleteMenu", { name }),
112
+ icon: "trash",
113
+ tone: "destructive",
114
+ onSelect: () => setDeleting(true),
115
+ },
116
+ ]
117
+ : []),
118
+ ];
119
+ return (_jsxs(Page, { className: "agent-page-frame", leading: _jsx(Avatar, { name: name, kind: "agent", icon: icon, size: 52, className: "agent-mark" }), title: name, back: back ? { href: placePath(back._id, "members"), label: placeName(back) } : undefined, badges: _jsxs(_Fragment, { children: [_jsxs(Badge, { variant: "outline", className: "agent-page-handle", children: ["@", id] }), app ? _jsx(Badge, { children: say("partOfApp") }) : (_jsx(Badge, { tone: "primary", children: home ? say("madeIn", { place: placeName(home) }) : say("madeElsewhere") })), answers && here ? _jsx(Badge, { tone: "success", children: say("answersNewChatsHere") }) : null] }), description: agent.description, actions: editing ? null : (_jsxs(_Fragment, { children: [ask
120
+ ? (_jsxs(_Fragment, { children: [_jsx(Button, { variant: "primary", icon: "chat", href: askAt(ask), className: "agent-wide", children: say("askAgent", { name }) }), _jsx(IconButton, { icon: "chat", label: say("askAgent", { name }), variant: "primary", href: askAt(ask), className: "agent-narrow" })] }))
121
+ : null, edits
122
+ ? (_jsxs(_Fragment, { children: [_jsx(Button, { icon: "pencil", className: "agent-wide", onClick: () => setEditing(true), children: say("edit") }), _jsx(IconButton, { icon: "pencil", label: say("editAgent", { name }), variant: "outline", className: "agent-narrow", onClick: () => setEditing(true) })] }))
123
+ : null, more.length ? _jsx(Menu, { label: say("moreMenu", { name }), align: "end", items: more }) : null] })), children: [_jsx(Title, { text: name }), error
124
+ ? _jsx(Notice, { tone: "destructive", role: "alert", className: "agent-page-error", children: error })
125
+ : null, editing
126
+ ? _jsx(Edit, { id: id, agent: agent, options: options, call: call, done: done })
127
+ : (_jsxs("div", { className: "agent-page", children: [app
128
+ ? (_jsx(Notice, { tone: "neutral", icon: "lock", action: mine.length
129
+ ? (_jsx(Button, { size: "sm", icon: "sparkles", onClick: fromStep, children: say("startFromShort") }))
130
+ : null, children: say("appAgentNotice") }))
131
+ : !edits
132
+ ? (_jsxs("p", { className: "agent-page-note", children: [_jsx(Icon, { name: "lock", size: 14 }), home
133
+ ? say("managersChangeIt", { place: placeName(home) })
134
+ : say("managersChangeItElsewhere")] }))
135
+ : null, _jsxs("section", { className: "agent-section", "aria-labelledby": "agent-works", children: [_jsxs("div", { className: "agent-section-head", children: [_jsx("h2", { id: "agent-works", className: "agent-section-title", children: say.nodes("worksIn", {
136
+ count: _jsx("span", { className: "agent-count", children: working.length }),
137
+ }) }), elsewhere.length
138
+ ? (_jsx(Button, { size: "sm", variant: "ghost", icon: "plus", onClick: addStep, children: say("addToSpace") }))
139
+ : null] }), working.length
140
+ ? (_jsx("ul", { className: "agent-seats", children: working.map(({ seat, place }) => (_jsx("li", { children: _jsxs("a", { className: "agent-seat", href: placePath(place._id, "members"), children: [place.kind === "personal"
141
+ ? (_jsx("span", { className: "agent-seat-personal", children: _jsx(Icon, { name: "person", size: 15 }) }))
142
+ : _jsx(Avatar, { name: place.name, kind: "space", size: 28 }), _jsx("span", { className: "agent-seat-name", children: placeName(place) }), place.agent === id
143
+ ? _jsx(Badge, { tone: "primary", children: say("answersNewChats") })
144
+ : null, _jsx(Badge, { variant: "outline", children: seat.role === "viewer" ? say("roleViewer") : say("roleEditor") }), _jsx(Icon, { name: "chevron", size: 15 })] }) }, seat._id))) }))
145
+ : _jsx("p", { className: "agent-none", children: say("worksNowhere") })] }), _jsxs("section", { className: "agent-section", "aria-labelledby": "agent-told", children: [_jsx("h2", { id: "agent-told", className: "agent-section-title", children: say("instructions") }), _jsx(Card, { className: "agent-instructions-read", children: _jsx(LongText, { text: agent.instructions, lines: 12 }) })] }), _jsxs("section", { className: "agent-section", "aria-labelledby": "agent-uses", children: [_jsx("h2", { id: "agent-uses", className: "agent-section-title", children: say("modelAndTools") }), _jsx(DetailList, { item: {}, fields: [
146
+ {
147
+ field: "model",
148
+ label: say("model"),
149
+ value: _jsx("code", { children: modelName(agent.model) }),
150
+ },
151
+ ...(agent.effort
152
+ ? [{
153
+ field: "effort",
154
+ label: say("thinks"),
155
+ value: say("effort", { effort: agent.effort }),
156
+ }]
157
+ : []),
158
+ {
159
+ field: "tools",
160
+ label: say("tools"),
161
+ value: agent.tools?.length
162
+ ? _jsx(ToolList, { tools: agent.tools, options: options })
163
+ : say("noTools"),
164
+ },
165
+ ] })] })] })), edits
166
+ ? (_jsx(Delete, { open: deleting, onClose: () => setDeleting(false), id: id, name: name, spaces: working.map((w) => placeName(w.place)), after: home ? placePath(home._id, "members") : "/", call: call }))
167
+ : null] }));
168
+ }
169
+ /** How many tools show before "Show all". */
170
+ const TOOLS = 8;
171
+ /** The tools it uses, each with what it does; past a few, the rest on a press. */
172
+ function ToolList({ tools, options }) {
173
+ const [all, setAll] = useState(false);
174
+ const shown = all ? tools : tools.slice(0, TOOLS);
175
+ return (_jsxs("div", { className: "agent-tools-read", children: [_jsx("ul", { className: "agent-tool-list", children: shown.map((tool) => {
176
+ const about = options.tools.find((t) => t.name === tool)?.description;
177
+ return (_jsxs("li", { title: about, children: [_jsx("code", { children: tool }), _jsx("span", { children: about ?? "" })] }, tool));
178
+ }) }), tools.length > TOOLS
179
+ ? (_jsx(Button, { variant: "link", size: "sm", iconEnd: all ? undefined : "chevron-down", onClick: () => setAll(!all), children: all ? say("showFewer") : say("showAll", { count: tools.length }) }))
180
+ : null] }));
181
+ }
182
+ /** A model, as it's named: `"provider/model"`, an alias, or a list tried in order. */
183
+ const modelName = (model) => typeof model === "string"
184
+ ? model
185
+ : Array.isArray(model)
186
+ ? model.map(modelName).join(say("modelsThenJoin"))
187
+ : modelName(model.model);
188
+ /** Changing it, in place: its fields, then Save (what changed only) or Cancel. */
189
+ function Edit({ id, agent, options, call, done }) {
190
+ const was = {
191
+ name: shownAs(id, agent).name,
192
+ icon: agent.icon ?? "",
193
+ description: agent.description ?? "",
194
+ instructions: agent.instructions,
195
+ model: typeof agent.model === "string" ? agent.model : options.models[0] ?? "",
196
+ tools: agent.tools ?? [],
197
+ };
198
+ const [draft, setDraft] = useState(was);
199
+ const [busy, setBusy] = useState(false);
200
+ const [error, setError] = useState();
201
+ const changed = changes(was, draft);
202
+ const ready = !!draft.name.trim() && !!draft.model;
203
+ return (_jsxs("form", { className: "agent-edit", "aria-label": say("changeAgent", { name: was.name }), onSubmit: async (event) => {
204
+ event.preventDefault();
205
+ if (!ready || busy)
206
+ return;
207
+ if (!Object.keys(changed).length)
208
+ return done();
209
+ setBusy(true);
210
+ setError(undefined);
211
+ try {
212
+ await call("agent.update", { agent: id, ...changed });
213
+ done();
214
+ }
215
+ catch (e) {
216
+ setError(message(e));
217
+ setBusy(false);
218
+ }
219
+ }, children: [_jsx(Card, { className: "agent-edit-card", children: _jsx(AgentFields, { value: draft, onChange: (patch) => setDraft((now) => ({ ...now, ...patch })), options: options, handle: id, call: call, autoFocus: true, disabled: busy }) }), _jsxs("div", { className: "agent-edit-end", children: [_jsx("p", { className: "agent-form-note", "aria-live": "polite", children: error
220
+ ? _jsx("span", { className: "agent-error", role: "alert", children: error })
221
+ : say("keepsHandle", { handle: id }) }), _jsx(Button, { onClick: done, disabled: busy, children: say("cancel") }), _jsx(Button, { type: "submit", variant: "primary", icon: "check", loading: busy, disabled: !ready || !Object.keys(changed).length, children: say("saveChanges") })] })] }));
222
+ }
223
+ /** What changed between what an agent was and what's typed, as `agent.update` takes it. */
224
+ function changes(was, now) {
225
+ const out = {};
226
+ for (const field of ["name", "icon", "description", "instructions", "model"]) {
227
+ const value = now[field].trim();
228
+ if (value !== was[field].trim() && (value || field !== "name"))
229
+ out[field] = value;
230
+ }
231
+ if (now.tools.join() !== was.tools.join())
232
+ out.tools = now.tools;
233
+ return out;
234
+ }
235
+ /** Deleting it, once they've read what it means: it leaves every space it works in. */
236
+ function Delete({ open, onClose, id, name, spaces, after, call }) {
237
+ const [busy, setBusy] = useState(false);
238
+ const [error, setError] = useState();
239
+ return (_jsxs(Dialog, { open: open, onClose: onClose, size: "sm", title: say("deleteTitle", { name }), footer: _jsxs(_Fragment, { children: [_jsx(Button, { onClick: onClose, disabled: busy, children: say("cancel") }), _jsx(Button, { variant: "destructive", icon: "trash", loading: busy, onClick: async () => {
240
+ setBusy(true);
241
+ setError(undefined);
242
+ try {
243
+ await call("agent.delete", { agent: id });
244
+ onClose();
245
+ go(after);
246
+ }
247
+ catch (e) {
248
+ setError(message(e));
249
+ setBusy(false);
250
+ }
251
+ }, children: say("deleteButton", { name }) })] }), children: [_jsxs("ul", { className: "agent-delete-means", children: [_jsx("li", { children: spaces.length
252
+ ? say.nodes("deleteLeavesNamed", {
253
+ bold: _jsx("b", { children: say("deleteLeavesBold") }),
254
+ spaces: listed(spaces),
255
+ })
256
+ : say.nodes("deleteLeaves", { bold: _jsx("b", { children: say("deleteLeavesBold") }) }) }), _jsx("li", { children: say.nodes("deleteGone", { bold: _jsx("b", { children: say("deleteGoneBold") }) }) }), _jsx("li", { children: say.nodes("deleteChats", { bold: _jsx("b", { children: say("deleteChatsBold") }) }) })] }), error ? _jsx(Notice, { tone: "destructive", role: "alert", children: error }) : null] }));
257
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Agents as team's web front end makes them: each by what it's shown as
3
+ * (the name people gave it, "Maya"; one of the app's, by what it's called)
4
+ * with its emoji; what `agent.options` answers; the name a new one is
5
+ * called by (`Maya` is `@maya`, as `agent.create` makes it); finding one
6
+ * among those someone knows; what a new one starts with (the app's usual
7
+ * agent's tools, an emoji to pick); and names in a sentence.
8
+ */
9
+ import { type Agent } from "copilotz/plugins/agents/contract";
10
+ /** What `agent.options` answers: what an agent can be made with. */
11
+ export type Options = {
12
+ tools: {
13
+ name: string;
14
+ description: string;
15
+ }[];
16
+ models: string[];
17
+ };
18
+ /** How an agent shows: its name, and its emoji if it has one. */
19
+ export type Shown = {
20
+ name: string;
21
+ icon?: string;
22
+ };
23
+ /** How agent `id` shows: by its name, or its id when it has none (the app's). */
24
+ export declare const shownAs: (id: string, agent?: Pick<Agent, "name" | "icon">) => Shown;
25
+ /** An agent's page; `edit`, open to change it. */
26
+ export declare const agentPath: (id: string, edit?: boolean) => string;
27
+ /**
28
+ * What a new agent named `name` is called by, as `agent.create` makes it
29
+ * ("Maya Lima" is `maya-lima`), or the next one free when it's `taken`
30
+ * (`maya-2`).
31
+ */
32
+ export declare function handleOf(name: string, taken?: (id: string) => boolean): string;
33
+ /** The agents what's typed finds: every word in its name, what it's called by, or what it does. */
34
+ export declare function found<A extends {
35
+ id: string;
36
+ name: string;
37
+ description?: string;
38
+ }>(agents: A[], typed: string): A[];
39
+ /**
40
+ * The tools a new agent starts with: those the app's usual agent has (its
41
+ * default one, or its first) that an agent may be given; without any,
42
+ * messaging others.
43
+ */
44
+ export declare function usualTools(known: Record<string, Agent>, offered: string[]): string[];
45
+ /** Emoji a new agent may be shown with, a few to pick from. */
46
+ export declare const ICONS: string[];
47
+ /** Names as a sentence says them: "a", "a and b", "a, b and c" (the app's words say how). */
48
+ export declare const listed: (items: string[]) => string;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Agents as team's web front end makes them: each by what it's shown as
3
+ * (the name people gave it, "Maya"; one of the app's, by what it's called)
4
+ * with its emoji; what `agent.options` answers; the name a new one is
5
+ * called by (`Maya` is `@maya`, as `agent.create` makes it); finding one
6
+ * among those someone knows; what a new one starts with (the app's usual
7
+ * agent's tools, an emoji to pick); and names in a sentence.
8
+ */
9
+ import { SEND } from "copilotz/plugins/agents/contract";
10
+ import { say } from "./words.js";
11
+ /** How agent `id` shows: by its name, or its id when it has none (the app's). */
12
+ export const shownAs = (id, agent) => ({
13
+ name: agent?.name || id,
14
+ ...(agent?.icon ? { icon: agent.icon } : {}),
15
+ });
16
+ /** An agent's page; `edit`, open to change it. */
17
+ export const agentPath = (id, edit = false) => `/agent/${encodeURIComponent(id)}${edit ? "?edit=1" : ""}`;
18
+ /** Text as it's matched: any case, any accents. */
19
+ const fold = (text) => text.normalize("NFD").replace(/[ฬ€-อฏ]/g, "").toLowerCase();
20
+ /**
21
+ * What a new agent named `name` is called by, as `agent.create` makes it
22
+ * ("Maya Lima" is `maya-lima`), or the next one free when it's `taken`
23
+ * (`maya-2`).
24
+ */
25
+ export function handleOf(name, taken = () => false) {
26
+ const base = fold(name).replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 32) ||
27
+ "agent";
28
+ for (let n = 1;; n++) {
29
+ const handle = n === 1 ? base : `${base}-${n}`;
30
+ if (!taken(handle))
31
+ return handle;
32
+ }
33
+ }
34
+ /** The agents what's typed finds: every word in its name, what it's called by, or what it does. */
35
+ export function found(agents, typed) {
36
+ const words = fold(typed.replace(/@/g, " ")).split(/\s+/).filter(Boolean);
37
+ return agents.filter((a) => {
38
+ const text = fold(`${a.name} ${a.id} ${a.description ?? ""}`);
39
+ return words.every((w) => text.includes(w));
40
+ });
41
+ }
42
+ /**
43
+ * The tools a new agent starts with: those the app's usual agent has (its
44
+ * default one, or its first) that an agent may be given; without any,
45
+ * messaging others.
46
+ */
47
+ export function usualTools(known, offered) {
48
+ const app = Object.values(known).filter((agent) => !agent.in);
49
+ const usual = app.find((agent) => agent.default) ?? app[0];
50
+ const tools = (usual?.tools ?? []).filter((tool) => offered.includes(tool));
51
+ return tools.length ? tools : offered.filter((tool) => tool === SEND);
52
+ }
53
+ /** Emoji a new agent may be shown with, a few to pick from. */
54
+ export const ICONS = ["โœจ", "๐Ÿงญ", "๐Ÿ”Ž", "โœ๏ธ", "๐Ÿ“Š", "๐Ÿง ", "๐Ÿ› ๏ธ", "๐ŸŽจ", "๐Ÿ“…", "๐Ÿงช", "๐Ÿ“š", "๐Ÿฆ‰"];
55
+ /** Names as a sentence says them: "a", "a and b", "a, b and c" (the app's words say how). */
56
+ export const listed = (items) => items.length < 2 ? items.join("") : say("listEnd", {
57
+ rest: items.slice(0, -1).join(say("listJoin")),
58
+ last: items.at(-1),
59
+ });
@@ -0,0 +1,25 @@
1
+ /**
2
+ * A model to choose, for another front end's page (kind `model.pick`): the
3
+ * same picker an agent's model is chosen with. Item `{ value }`, the model
4
+ * chosen now (`"provider/model"`; none: nothing yet); hints `{ action, input,
5
+ * look?, label?, disabled? }`: choosing one calls `action` with `input` and
6
+ * the `model` chosen, and says so if that fails. What changes shows where it's
7
+ * read (a watched read), so it hands nothing back. `look`: the picker's field
8
+ * (the default), or a small button saying `label` ("Change").
9
+ *
10
+ * ```tsx
11
+ * <LazyView kind="model.pick" item={{ value }} hints={{ action: "tiers.pick", input: { agent } }} />
12
+ * ```
13
+ */
14
+ import { type ReactNode } from "react";
15
+ import type { ViewContext } from "copilotz";
16
+ export type ModelPick = {
17
+ action: string;
18
+ input?: Record<string, unknown>;
19
+ look?: "field" | "link";
20
+ label?: string;
21
+ disabled?: boolean;
22
+ };
23
+ export declare const modelPickView: (item: {
24
+ value?: string;
25
+ }, ctx: ViewContext<ReactNode>) => ReactNode;
@@ -0,0 +1,39 @@
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * A model to choose, for another front end's page (kind `model.pick`): the
4
+ * same picker an agent's model is chosen with. Item `{ value }`, the model
5
+ * chosen now (`"provider/model"`; none: nothing yet); hints `{ action, input,
6
+ * look?, label?, disabled? }`: choosing one calls `action` with `input` and
7
+ * the `model` chosen, and says so if that fails. What changes shows where it's
8
+ * read (a watched read), so it hands nothing back. `look`: the picker's field
9
+ * (the default), or a small button saying `label` ("Change").
10
+ *
11
+ * ```tsx
12
+ * <LazyView kind="model.pick" item={{ value }} hints={{ action: "tiers.pick", input: { agent } }} />
13
+ * ```
14
+ */
15
+ import { useState } from "react";
16
+ import { message } from "copilotz/web";
17
+ import { ModelPicker } from "./model-picker.js";
18
+ import { STYLE } from "./style.js";
19
+ export const modelPickView = (item, ctx) => (_jsxs(_Fragment, { children: [_jsx("style", { href: "team-web", precedence: "copilotz", children: STYLE }), _jsx(Pick, { value: item.value ?? "", pick: ctx.hints, call: ctx.call })] }));
20
+ function Pick({ value, pick, call }) {
21
+ const [busy, setBusy] = useState(false);
22
+ const [error, setError] = useState();
23
+ const choose = async (model) => {
24
+ if (model === value)
25
+ return;
26
+ setBusy(true);
27
+ setError(undefined);
28
+ try {
29
+ await call(pick.action, { ...pick.input, model });
30
+ }
31
+ catch (e) {
32
+ setError(message(e));
33
+ }
34
+ finally {
35
+ setBusy(false);
36
+ }
37
+ };
38
+ return (_jsxs("span", { className: "model-pick", "data-look": pick.look ?? "field", "aria-busy": busy || undefined, children: [_jsx(ModelPicker, { value: value, call: call, look: pick.look, label: pick.label, disabled: busy || pick.disabled, onChange: (model) => void choose(model) }), error ? _jsx("span", { className: "model-pick-error", role: "alert", children: error }) : null] }));
39
+ }
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Choosing the model an agent thinks with, from every provider the app can
3
+ * reach (`models.offered`, from the models.dev catalog). Closed, it shows
4
+ * what's chosen, and says so when it can't run yet; or, where a line already
5
+ * says what's chosen (`look: "link"`), it's just a small "Change". Open, a dialog: search
6
+ * across providers and models, or browse the providers, those ready first, and
7
+ * then one's models, newest first, with what each costs and can do. A provider
8
+ * without a key asks for one: the org's owners and admins add it here
9
+ * (`modelKey.set`), and it's never shown again; anyone else is told whom to ask.
10
+ */
11
+ import { type ReactNode } from "react";
12
+ import type { OfferedModel, OfferedProvider } from "copilotz/plugins/models/contract";
13
+ type Call = <R = unknown>(action: string, input?: unknown) => Promise<R>;
14
+ export declare function ModelPicker({ value, onChange, call, disabled, id, look, label }: {
15
+ value: string;
16
+ onChange(model: string): void;
17
+ call: Call;
18
+ disabled?: boolean;
19
+ id?: string;
20
+ /** A field showing what's chosen (the default), or a small button saying `label` ("Change"). */
21
+ look?: "field" | "link";
22
+ label?: string;
23
+ }): ReactNode;
24
+ type Listing = {
25
+ providers: OfferedProvider[];
26
+ set: boolean;
27
+ };
28
+ type Found = {
29
+ providers: OfferedProvider[];
30
+ models: OfferedModel[];
31
+ };
32
+ /** The providers: those that can be used now, then those that take a key. */
33
+ export declare function Providers({ listing, onProvider }: {
34
+ listing?: Listing;
35
+ onProvider(id: string): void;
36
+ }): ReactNode;
37
+ export declare function ModelRow({ model, value, withProvider, onChoose }: {
38
+ model: OfferedModel;
39
+ value: string;
40
+ /** Who it's from, where several providers' are listed. */
41
+ withProvider?: string;
42
+ onChoose(): void;
43
+ }): ReactNode;
44
+ export declare function Results({ found, value, onProvider, onChoose }: {
45
+ found?: Found;
46
+ value: string;
47
+ onProvider(id: string): void;
48
+ onChoose(model: string): void;
49
+ }): ReactNode;
50
+ /** The org's key for a provider: added, replaced or removed by those who run it. */
51
+ export declare function ProviderKey({ provider, may, call, onChange }: {
52
+ provider: OfferedProvider;
53
+ may: boolean;
54
+ call: Call;
55
+ onChange(): void;
56
+ }): ReactNode;
57
+ export {};