@copilotz/team 1.0.0-rc.54 → 1.0.0-rc.56

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 (66) hide show
  1. package/WORDS.md +156 -0
  2. package/package.json +2 -2
  3. package/team/actions/agent.d.ts +10 -0
  4. package/team/actions/agent.js +226 -7
  5. package/team/actions/define.js +7 -3
  6. package/team/collections/agent.d.ts +52 -7
  7. package/team/collections/agent.js +56 -5
  8. package/team/mod.d.ts +7 -0
  9. package/team/mod.js +14 -1
  10. package/team/resources/propose.d.ts +1 -0
  11. package/team/resources/propose.js +97 -0
  12. package/team/shared/definition.d.ts +1 -1
  13. package/team/shared/definition.js +5 -4
  14. package/team/shared/everywhere.d.ts +11 -0
  15. package/team/shared/everywhere.js +29 -0
  16. package/team/views/web/agent-add.js +14 -4
  17. package/team/views/web/agent-describe.d.ts +38 -0
  18. package/team/views/web/agent-describe.js +44 -0
  19. package/team/views/web/agent-fields.d.ts +3 -1
  20. package/team/views/web/agent-fields.js +2 -2
  21. package/team/views/web/agent-known.js +2 -1
  22. package/team/views/web/agent-new.d.ts +4 -1
  23. package/team/views/web/agent-new.js +26 -5
  24. package/team/views/web/agent-org.d.ts +12 -0
  25. package/team/views/web/agent-org.js +89 -0
  26. package/team/views/web/agent.d.ts +7 -4
  27. package/team/views/web/agent.js +81 -28
  28. package/team/views/web/model-pick.d.ts +3 -1
  29. package/team/views/web/model-pick.js +4 -1
  30. package/team/views/web/style.d.ts +1 -1
  31. package/team/views/web/style.js +5 -0
  32. package/team/views/web/words.d.ts +1 -1
  33. package/team/views/web/words.js +28 -0
  34. package/team/web.js +2 -0
  35. package/tiers/actions/choose.d.ts +5 -1
  36. package/tiers/actions/choose.js +247 -66
  37. package/tiers/actions/model.js +31 -22
  38. package/tiers/collections/choice.d.ts +17 -10
  39. package/tiers/collections/choice.js +39 -27
  40. package/tiers/collections/template.d.ts +90 -0
  41. package/tiers/collections/template.js +41 -0
  42. package/tiers/contract.d.ts +10 -8
  43. package/tiers/mod.d.ts +30 -21
  44. package/tiers/mod.js +45 -36
  45. package/tiers/shared/shown.d.ts +131 -49
  46. package/tiers/shared/shown.js +192 -98
  47. package/tiers/shared/tiers.d.ts +105 -62
  48. package/tiers/shared/tiers.js +127 -86
  49. package/tiers/views/web/composer.d.ts +12 -0
  50. package/tiers/views/web/composer.js +79 -0
  51. package/tiers/views/web/org.d.ts +44 -21
  52. package/tiers/views/web/org.js +231 -84
  53. package/tiers/views/web/say.d.ts +138 -0
  54. package/tiers/views/web/say.js +142 -0
  55. package/tiers/views/web/setup.d.ts +23 -0
  56. package/tiers/views/web/setup.js +80 -0
  57. package/tiers/views/web/space.d.ts +8 -20
  58. package/tiers/views/web/space.js +30 -59
  59. package/tiers/views/web/style.d.ts +1 -1
  60. package/tiers/views/web/style.js +98 -4
  61. package/tiers/views/web/team.d.ts +38 -0
  62. package/tiers/views/web/team.js +118 -0
  63. package/tiers/views/web/words.d.ts +36 -16
  64. package/tiers/views/web/words.js +102 -35
  65. package/tiers/web.d.ts +18 -9
  66. package/tiers/web.js +36 -10
@@ -205,6 +205,34 @@ const team = {
205
205
  mediaAudio: "Audio",
206
206
  mediaVideo: "Video",
207
207
  reasoning: "Reasoning",
208
+ // Making an agent: what it should do first, then drafted
209
+ describeHint: "A sentence or two is enough: its name, instructions and the rest are drafted for you to check.",
210
+ describePlaceholder: "Keeps the numbers straight for our clients: prices, costs and margins, with sources.",
211
+ fillMyself: "Fill it in myself",
212
+ draftIt: "Draft it",
213
+ drafting: "Drafting…",
214
+ draftingWait: "Writing its instructions, a few seconds…",
215
+ kindLabel: "Kind of work",
216
+ kindHint: "Its model comes from the tier each chat runs on, for this kind of work.",
217
+ kindLike: "{kind} · like {agents}",
218
+ // The org's team
219
+ orgForm: "A new agent for the org",
220
+ orgJoins: "It joins every space in the org, and every new one.",
221
+ back: "Back",
222
+ addToTeamNamed: "Add {name} to the team",
223
+ addToTeam: "Add to the team",
224
+ removeFromTeamMenu: "Remove from the team…",
225
+ changedForOrg: "Changed for your org",
226
+ orgsTeam: "Your org's team",
227
+ tierModel: "Its model comes from the tier each chat runs on. Change the tiers' models under {place}.",
228
+ tierModelPlace: "Organization settings › Agents",
229
+ removeTeamTitle: "Remove {name} from the team?",
230
+ removeButton: "Remove",
231
+ teamLeaves: "{bold}, and won't join new ones.",
232
+ teamLeavesBold: "It leaves every space in the org",
233
+ teamSaidStays: "What it already said stays in its chats.",
234
+ teamAddBack: "You can add it back under Organization settings › Agents.",
235
+ teamDeleted: "It's deleted: its instructions are gone.",
208
236
  };
209
237
  export const say = defineWords("team", team);
210
238
  /** Every word of team's front end, by area (WORDS.md lists them). */
package/team/web.js CHANGED
@@ -21,6 +21,7 @@
21
21
  import { definePlugin } from "copilotz";
22
22
  import { agentView } from "./views/web/agent.js";
23
23
  import { agentAddView } from "./views/web/agent-add.js";
24
+ import { agentOrgView } from "./views/web/agent-org.js";
24
25
  import { agentPaletteView } from "./views/web/agent-palette.js";
25
26
  import { modelPickView } from "./views/web/model-pick.js";
26
27
  export const teamWeb = definePlugin({
@@ -29,6 +30,7 @@ export const teamWeb = definePlugin({
29
30
  views: {
30
31
  agent: agentView,
31
32
  "agent.add": agentAddView,
33
+ "agent.org": agentOrgView,
32
34
  "agent.palette": agentPaletteView,
33
35
  "model.pick": modelPickView,
34
36
  },
@@ -1,2 +1,6 @@
1
1
  export declare const chooseAction: import("copilotz").ActionDef;
2
- export declare const pickAction: import("copilotz").ActionDef;
2
+ export declare const capAction: import("copilotz").ActionDef;
3
+ export declare const saveAction: import("copilotz").ActionDef;
4
+ export declare const removeAction: import("copilotz").ActionDef;
5
+ export declare const restoreAction: import("copilotz").ActionDef;
6
+ export declare const kindAction: import("copilotz").ActionDef;
@@ -1,98 +1,279 @@
1
1
  /**
2
- * Choosing the app's agents' models, for the org or for one space, in person.
3
- * The org's are changed by its owners and admins, a space's by its managers;
4
- * which tier a space takes isn't limited: the org's budget bounds what it
5
- * spends. A change applies from each conversation's next reply.
2
+ * Choosing tiers, and the org's own: all in person.
6
3
  *
7
- * - `tiers.choose` `{ space?, tier, picks? }`: the tier by its name (null:
8
- * the one above, the org's or the plan's), and `picks: null` to drop every
9
- * agent's own pick there too (back to the tier as it is).
10
- * - `tiers.pick` `{ space?, agent, model }`: a model for one of the app's
11
- * agents there, as an agent's `model` takes one (null: back to the tier's).
12
- * It's checked as an agent's would be, and keeps the tier's list behind it;
13
- * as it runs, it gets the `maxTokens` of the entry it replaces, and its
14
- * `effort` when the picked model takes it.
4
+ * - `tiers.choose` `{ tier, space?, thread?, mine? }`: the tier for the org
5
+ * (its owners and admins), a space (its managers), a conversation (anyone
6
+ * in it) or the person's own default for new chats (`mine`); null: the one
7
+ * above. Those who don't run the org can't choose a tier over its `cap`.
8
+ * - `tiers.cap` `{ tier }`: the highest tier the org's members may choose
9
+ * (null: any), by its owners and admins.
10
+ * - `tiers.save` `{ tier, title?, description?, kinds?, from? }`: an org's
11
+ * tier, by its owners and admins: its version of one of the app's, or a
12
+ * new one (`title`, its chains copied from `from`, else the tier in force).
13
+ * `kinds`: chains by kind of work (a list of models, tried in order; null:
14
+ * back to the app's there), each checked as an agent's model is, and each
15
+ * given the `maxTokens` and `effort` of the chain it replaces, where it
16
+ * takes it.
17
+ * - `tiers.remove` `{ tier }`: takes one of the app's away, or deletes one
18
+ * the org made; not the last. Wherever it was chosen, what's above applies.
19
+ * - `tiers.restore` `{ tier }`: one of the app's back as the app has it.
20
+ * - `tiers.kind` `{ kind?, title, base }`: a kind of work of the org's own,
21
+ * starting from one of the app's (its chain in every tier, until changed).
22
+ *
23
+ * A change applies from each conversation's next reply.
15
24
  */
16
- import { defineAction, inPerson, invalid } from "copilotz";
25
+ import { defineAction, failure, inPerson, invalid } from "copilotz";
17
26
  import { must, ORG } from "copilotz/plugins/access/contract";
18
- import { agentsOf } from "copilotz/plugins/agents/contract";
19
- import { ORG_CHOICE, spaceChoice, tiersOf } from "../shared/tiers.js";
20
- /** Where a choice is kept, once the caller may change it. */
21
- async function choiceFor(ctx, space) {
22
- if (!space) {
23
- await must(ctx, "manage", ORG, "Only the org's owners and admins change its agents' models.");
24
- return ORG_CHOICE;
25
- }
26
- await must(ctx, "manage", space, "Only this space's managers change its agents' models.");
27
- return spaceChoice(space);
28
- }
29
- /** Changes a choice; one left with nothing in it is gone, so what's above applies. */
30
- async function change(ctx, space, to) {
31
- const id = await choiceFor(ctx, space);
32
- const docs = ctx.as().db.collection("modelChoice");
33
- const had = await docs.findOne({ _id: id });
34
- const next = to({ ...(had?.tier ? { tier: had.tier } : {}), custom: { ...had?.custom } });
35
- const custom = next.custom && Object.keys(next.custom).length ? next.custom : undefined;
36
- if (!next.tier && !custom) {
27
+ import { orgTiersOf } from "../shared/shown.js";
28
+ import { aboveCap, appKindsOf, appTiersOf, entryOf, ORG_CHOICE, personChoice, settled, spaceChoice, threadChoice, } from "../shared/tiers.js";
29
+ /** Throws unless the caller runs the org. */
30
+ const runsOrg = (ctx, what) => must(ctx, "manage", ORG, `Only the org's owners and admins ${what}.`);
31
+ /** Whether the caller runs the org, without throwing. */
32
+ const running = (ctx) => runsOrg(ctx, "do that").then(() => true, () => false);
33
+ const choices = (ctx) => ctx.as().db.collection("modelChoice");
34
+ const templates = (ctx) => ctx.as().db.collection("tierTemplate");
35
+ /** Sets or clears one level's tier, keeping what else it holds (the org's cap). */
36
+ async function setTier(ctx, id, tier, more) {
37
+ const had = await choices(ctx).findOne({ _id: id });
38
+ const next = { ...had, ...more, _id: id, at: new Date().toISOString() };
39
+ if (tier)
40
+ next.tier = tier;
41
+ else
42
+ delete next.tier;
43
+ if (!next.tier && !next.cap) {
37
44
  if (had)
38
- await docs.deleteOne({ _id: id });
39
- return { tier: null, custom: {} };
45
+ await choices(ctx).deleteOne({ _id: id });
46
+ return;
40
47
  }
41
- await docs.replaceOne({ _id: id }, {
42
- _id: id,
43
- ...(space ? { space } : {}),
44
- ...(next.tier ? { tier: next.tier } : {}),
45
- ...(custom ? { custom } : {}),
46
- at: new Date().toISOString(),
47
- }, { upsert: true });
48
- return { tier: next.tier ?? null, custom: custom ?? {} };
48
+ await choices(ctx).replaceOne({ _id: id }, next, { upsert: true });
49
49
  }
50
50
  export const chooseAction = defineAction({
51
- description: "Chooses the tier of models the app's agents use, in the org or a space",
51
+ description: "Chooses the tier for the org, a space, a conversation or your new chats",
52
52
  input: {
53
53
  type: "object",
54
54
  properties: {
55
- space: { type: "string" },
56
55
  tier: { type: ["string", "null"] },
57
- picks: { type: "null" },
56
+ space: { type: "string" },
57
+ thread: { type: "string" },
58
+ mine: { type: "boolean" },
58
59
  },
59
60
  required: ["tier"],
60
61
  additionalProperties: false,
61
62
  },
62
63
  allow: inPerson,
63
- run: (ctx, input) => {
64
- if (input.tier !== null && !Object.hasOwn(tiersOf(ctx), input.tier)) {
64
+ run: async (ctx, input) => {
65
+ const { tiers } = await orgTiersOf(ctx.as());
66
+ if (input.tier !== null && !tiers[input.tier]) {
67
+ throw invalid(`There's no tier "${input.tier}".`);
68
+ }
69
+ const where = [input.space, input.thread, input.mine].filter(Boolean).length;
70
+ if (where > 1)
71
+ throw invalid("Choose for one place at a time.");
72
+ // Over the cap: only who runs the org.
73
+ if (input.tier) {
74
+ const cap = (await choices(ctx).findOne({ _id: ORG_CHOICE }))?.cap;
75
+ if (aboveCap(tiers, input.tier, cap) && !await running(ctx)) {
76
+ throw failure("forbidden", `Your org's owners and admins let members use tiers up to ${tiers[cap].title}.`, { public: true });
77
+ }
78
+ }
79
+ if (input.thread) {
80
+ if (!await ctx.db.collection("thread").findOne({ _id: input.thread })) {
81
+ throw failure("forbidden", "You can't change that conversation.", { public: true });
82
+ }
83
+ await setTier(ctx, threadChoice(input.thread), input.tier, { thread: input.thread });
84
+ }
85
+ else if (input.mine) {
86
+ const person = ctx.actor.id;
87
+ await setTier(ctx, personChoice(person), input.tier, { person });
88
+ }
89
+ else if (input.space) {
90
+ await must(ctx, "manage", input.space, "Only this space's managers choose its tier.");
91
+ await setTier(ctx, spaceChoice(input.space), input.tier, { space: input.space });
92
+ }
93
+ else {
94
+ await runsOrg(ctx, "choose its default tier");
95
+ await setTier(ctx, ORG_CHOICE, input.tier, {});
96
+ }
97
+ return { tier: input.tier };
98
+ },
99
+ });
100
+ export const capAction = defineAction({
101
+ description: "Sets the highest tier the org's members may choose",
102
+ input: {
103
+ type: "object",
104
+ properties: { tier: { type: ["string", "null"] } },
105
+ required: ["tier"],
106
+ additionalProperties: false,
107
+ },
108
+ allow: inPerson,
109
+ run: async (ctx, input) => {
110
+ await runsOrg(ctx, "set what members may use");
111
+ const { tiers } = await orgTiersOf(ctx.as());
112
+ if (input.tier !== null && !tiers[input.tier]) {
65
113
  throw invalid(`There's no tier "${input.tier}".`);
66
114
  }
67
- return change(ctx, input.space, (had) => ({
68
- ...(input.tier ? { tier: input.tier } : {}),
69
- ...(input.picks === null ? {} : { custom: had.custom }),
70
- }));
115
+ const had = await choices(ctx).findOne({ _id: ORG_CHOICE });
116
+ const next = { ...had, _id: ORG_CHOICE, at: new Date().toISOString() };
117
+ if (input.tier)
118
+ next.cap = input.tier;
119
+ else
120
+ delete next.cap;
121
+ if (!next.tier && !next.cap)
122
+ await choices(ctx).deleteOne({ _id: ORG_CHOICE });
123
+ else
124
+ await choices(ctx).replaceOne({ _id: ORG_CHOICE }, next, { upsert: true });
125
+ return { cap: input.tier };
71
126
  },
72
127
  });
73
- export const pickAction = defineAction({
74
- description: "Picks the model one of the app's agents uses, in the org or a space",
128
+ /** A name for a new tier from its title: `balanced`, `balanced-2`. */
129
+ function nameFor(title, taken) {
130
+ const base = title.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase()
131
+ .replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 32) || "tier";
132
+ for (let n = 1;; n++) {
133
+ const name = n === 1 ? base : `${base}-${n}`;
134
+ if (!taken(name))
135
+ return name;
136
+ }
137
+ }
138
+ export const saveAction = defineAction({
139
+ description: "Saves one of the org's tiers: its version of the app's, or a new one",
75
140
  input: {
76
141
  type: "object",
77
142
  properties: {
78
- space: { type: "string" },
79
- agent: { type: "string", minLength: 1 },
80
- model: {},
143
+ tier: { type: "string" },
144
+ title: { type: "string", minLength: 1, maxLength: 40 },
145
+ description: { type: "string", maxLength: 200 },
146
+ /** Chains by kind of work: models in order; null, back to the app's. */
147
+ kinds: { type: "object", additionalProperties: { type: ["array", "null"], maxItems: 8 } },
148
+ /** A new tier's chains, copied from this one. */
149
+ from: { type: "string" },
81
150
  },
82
- required: ["agent", "model"],
83
151
  additionalProperties: false,
84
152
  },
85
153
  allow: inPerson,
86
154
  run: async (ctx, input) => {
87
- if (!agentsOf(ctx)[input.agent]) {
88
- throw invalid(`"${input.agent}" isn't one of the app's agents: it keeps its own model.`);
155
+ await runsOrg(ctx, "change its tiers");
156
+ const { tiers, kinds, templates: kept } = await orgTiersOf(ctx.as());
157
+ const app = appTiersOf(ctx);
158
+ const known = (name) => !!tiers[name] || !!app[name] || kept.some((t) => t._id === name);
159
+ // A new one: named from its title, its chains from another.
160
+ const making = !input.tier || (!tiers[input.tier] && !app[input.tier]);
161
+ if (making && !input.title)
162
+ throw invalid("Give the new tier a name.");
163
+ const name = input.tier && !known(input.tier)
164
+ ? input.tier
165
+ : making
166
+ ? nameFor(input.title, known)
167
+ : input.tier;
168
+ const had = kept.find((t) => t._id === name);
169
+ const source = input.from ? tiers[input.from] : undefined;
170
+ if (input.from && !source)
171
+ throw invalid(`There's no tier "${input.from}".`);
172
+ const base = tiers[name] ?? source ?? Object.values(tiers)[0];
173
+ const chains = making ? { ...base?.agents } : { ...had?.agents };
174
+ for (const [kind, chain] of Object.entries(input.kinds ?? {})) {
175
+ if (!kinds[kind])
176
+ throw invalid(`There's no kind of work "${kind}".`);
177
+ if (chain === null) {
178
+ delete chains[kind];
179
+ continue;
180
+ }
181
+ const list = chain;
182
+ if (!list.length)
183
+ throw invalid("A kind of work needs at least one model.");
184
+ for (const model of list)
185
+ await ctx.call("models.check", { model, port: "llm" });
186
+ chains[kind] = await settled(ctx, list, entryOf(base, kind, kinds) ?? undefined);
187
+ }
188
+ const order = making
189
+ ? Math.max(0, ...Object.values(tiers).map((t) => t.order ?? 0)) + 1
190
+ : had?.order;
191
+ await templates(ctx).replaceOne({ _id: name }, {
192
+ _id: name,
193
+ ...(input.title ?? had?.title ? { title: input.title ?? had?.title } : {}),
194
+ ...(input.description ?? had?.description
195
+ ? { description: input.description ?? had?.description }
196
+ : {}),
197
+ ...(order !== undefined ? { order } : {}),
198
+ ...(Object.keys(chains).length ? { agents: chains } : {}),
199
+ ...(making || had?.made ? { made: true } : {}),
200
+ at: new Date().toISOString(),
201
+ }, { upsert: true });
202
+ return { tier: name };
203
+ },
204
+ });
205
+ export const removeAction = defineAction({
206
+ description: "Takes one of the app's tiers away from the org, or deletes one it made",
207
+ input: {
208
+ type: "object",
209
+ properties: { tier: { type: "string" } },
210
+ required: ["tier"],
211
+ additionalProperties: false,
212
+ },
213
+ allow: inPerson,
214
+ run: async (ctx, input) => {
215
+ await runsOrg(ctx, "change its tiers");
216
+ const { tiers } = await orgTiersOf(ctx.as());
217
+ if (!tiers[input.tier])
218
+ throw invalid(`There's no tier "${input.tier}".`);
219
+ if (Object.keys(tiers).length === 1)
220
+ throw invalid("Keep at least one tier.");
221
+ if (tiers[input.tier].made)
222
+ await templates(ctx).deleteOne({ _id: input.tier });
223
+ else {
224
+ await templates(ctx).updateOne({ _id: input.tier }, { $set: { off: true, at: new Date().toISOString() } }, { upsert: true });
225
+ }
226
+ return { removed: input.tier };
227
+ },
228
+ });
229
+ export const restoreAction = defineAction({
230
+ description: "Puts one of the app's tiers back as the app has it",
231
+ input: {
232
+ type: "object",
233
+ properties: { tier: { type: "string" } },
234
+ required: ["tier"],
235
+ additionalProperties: false,
236
+ },
237
+ allow: inPerson,
238
+ run: async (ctx, input) => {
239
+ await runsOrg(ctx, "change its tiers");
240
+ if (!appTiersOf(ctx)[input.tier]) {
241
+ throw invalid(`"${input.tier}" isn't one of the app's tiers.`);
89
242
  }
90
- const model = input.model;
91
- if (model !== null)
92
- await ctx.call("models.check", { model, port: "llm" });
93
- return change(ctx, input.space, (had) => {
94
- const { [input.agent]: _, ...rest } = had.custom ?? {};
95
- return { ...had, custom: model === null ? rest : { ...rest, [input.agent]: model } };
96
- });
243
+ await templates(ctx).deleteOne({ _id: input.tier });
244
+ return { tier: input.tier };
245
+ },
246
+ });
247
+ export const kindAction = defineAction({
248
+ description: "Adds or renames a kind of work of the org's own",
249
+ input: {
250
+ type: "object",
251
+ properties: {
252
+ kind: { type: "string" },
253
+ title: { type: "string", minLength: 1, maxLength: 40 },
254
+ description: { type: "string", maxLength: 200 },
255
+ base: { type: "string" },
256
+ },
257
+ required: ["title", "base"],
258
+ additionalProperties: false,
259
+ },
260
+ allow: inPerson,
261
+ run: async (ctx, input) => {
262
+ await runsOrg(ctx, "change its kinds of work");
263
+ const app = appKindsOf(ctx);
264
+ if (!app[input.base])
265
+ throw invalid(`"${input.base}" isn't one of the app's kinds of work.`);
266
+ const { kinds } = await orgTiersOf(ctx.as());
267
+ const kind = input.kind && !app[input.kind]
268
+ ? input.kind
269
+ : nameFor(input.title, (name) => !!kinds[name]);
270
+ await ctx.as().db.collection("tierKind").replaceOne({ _id: kind }, {
271
+ _id: kind,
272
+ title: input.title,
273
+ ...(input.description ? { description: input.description } : {}),
274
+ base: input.base,
275
+ at: new Date().toISOString(),
276
+ }, { upsert: true });
277
+ return { kind };
97
278
  },
98
279
  });
@@ -1,36 +1,45 @@
1
1
  /**
2
- * `tiers.model` `{ agent, space? }`: the model one of the app's agents thinks
3
- * with in a space (chat's `agentModel` resource names it): what's picked for it
4
- * there, the space's tier, the org's pick, the org's tier, the plan's tier
5
- * (see shared/tiers.ts), or null, its own. Asked on every run of a turn, so it
6
- * only reads by `_id`, which the owner keeps cached: the space's choice and
7
- * the org's, and the plan's tier (credits' `billing.tier`, a read by `_id` too).
8
- * A pick gets the `maxTokens` of the entry it replaces, and its `effort` when
9
- * the picked model takes it (`models.efforts`, the catalog's: nothing read).
2
+ * `tiers.model` `{ agent, space?, thread? }`: the model chain an agent thinks
3
+ * with in a conversation (chat's `agentModel` resource names it, with the
4
+ * kind the agent works `like`, else its name): the tier in force there (the
5
+ * conversation's, the person's own default, the space's, the org's, the
6
+ * plan's), as the org keeps it, and that kind's chain in it; null where no
7
+ * tier applies (an app without tiers: the agent's own). An agent of no kind
8
+ * the tiers know works as the app's first. Asked on every run of a turn, so
9
+ * it only reads by `_id` and the org's few templates, which the owner keeps
10
+ * cached.
10
11
  */
11
- import { defineAction } from "copilotz";
12
- import { carried, ORG_CHOICE, resolve, spaceChoice } from "../shared/tiers.js";
13
- import { agentsOf } from "copilotz/plugins/agents/contract";
12
+ import { defineAction, personOf } from "copilotz";
13
+ import { levelsOf, orgTiersOf } from "../shared/shown.js";
14
+ import { entryOf, kindOf, tierIn } from "../shared/tiers.js";
14
15
  export const modelAction = defineAction({
15
- description: "The model one of the app's agents thinks with in a space",
16
+ description: "The model chain an agent thinks with in a conversation",
16
17
  record: false,
17
18
  input: {
18
19
  type: "object",
19
- properties: { agent: { type: "string" }, space: { type: "string" } },
20
+ properties: {
21
+ agent: { type: "string" },
22
+ space: { type: "string" },
23
+ thread: { type: "string" },
24
+ },
20
25
  required: ["agent"],
21
26
  additionalProperties: false,
22
27
  },
23
28
  run: async (ctx, input) => {
24
- // Agents people make keep their own model, and cost nothing to ask about.
25
- if (!agentsOf(ctx)[input.agent])
29
+ if (!Object.keys(ctx.resources.tiers ?? {}).length)
26
30
  return null;
27
- const choices = ctx.as().db.collection("modelChoice");
28
- const source = ctx.resources.planTier;
29
- const [space, org, plan] = await Promise.all([
30
- input.space ? choices.findOne({ _id: spaceChoice(input.space) }) : null,
31
- choices.findOne({ _id: ORG_CHOICE }),
32
- source?.action ? ctx.as().call(source.action, {}) : null,
31
+ const person = personOf(ctx.actor);
32
+ const system = ctx.as();
33
+ const [{ tiers, kinds }, levels] = await Promise.all([
34
+ orgTiersOf(system),
35
+ levelsOf(system, {
36
+ ...(input.space ? { space: input.space } : {}),
37
+ ...(input.thread ? { thread: input.thread } : {}),
38
+ ...(person ? { person } : {}),
39
+ }),
33
40
  ]);
34
- return await carried(ctx, resolve(ctx, input.agent, { space, org, plan }));
41
+ const inForce = tierIn(tiers, levels);
42
+ const kind = kindOf(ctx, input.agent, undefined, kinds);
43
+ return entryOf(inForce ? tiers[inForce.tier] : undefined, kind, kinds) ?? null;
35
44
  },
36
45
  });
@@ -1,26 +1,33 @@
1
- import type { ModelRef } from "copilotz/plugins/models/contract";
2
1
  export type ModelChoice = {
3
2
  _id: string;
4
3
  space?: string;
4
+ thread?: string;
5
+ person?: string;
5
6
  tier?: string;
6
- custom?: Record<string, ModelRef>;
7
+ cap?: string;
7
8
  at: string;
8
9
  };
9
10
  export declare const modelChoice: {
10
11
  schema: {
11
12
  readonly type: "object";
12
13
  readonly properties: {
13
- /** The space it's for; none: the org's. */
14
+ /** The space or conversation it's for, or the person whose default it is; none: the org's. */
14
15
  readonly space: {
15
16
  readonly type: "string";
16
17
  };
17
- /** A tier by its name; none: the one above (the org's, then the plan's). */
18
+ readonly thread: {
19
+ readonly type: "string";
20
+ };
21
+ readonly person: {
22
+ readonly type: "string";
23
+ };
24
+ /** A tier by its name; none: the one above. */
18
25
  readonly tier: {
19
26
  readonly type: "string";
20
27
  };
21
- /** Models picked for some of the app's agents, by name, as an agent's `model` takes one. */
22
- readonly custom: {
23
- readonly type: "object";
28
+ /** The org's: the highest tier its members may choose; none: any. */
29
+ readonly cap: {
30
+ readonly type: "string";
24
31
  };
25
32
  readonly at: {
26
33
  readonly type: "string";
@@ -36,10 +43,10 @@ export declare const modelChoice: {
36
43
  queries?: Record<string, import("copilotz").QueryDef<{
37
44
  [x: string]: unknown;
38
45
  space?: string | undefined;
46
+ thread?: string | undefined;
47
+ person?: string | undefined;
39
48
  tier?: string | undefined;
40
- custom?: {
41
- [x: string]: unknown;
42
- } | undefined;
49
+ cap?: string | undefined;
43
50
  at: string;
44
51
  } & {
45
52
  _id: string;
@@ -1,41 +1,46 @@
1
1
  /**
2
- * What an org and its spaces chose for the app's agents' models: the org's
3
- * (`_id: "org"`) and each space's (`space:<id>`), each a tier by its name and
4
- * models picked for some agents. Never a copy of a tier's lists: retuning a
5
- * tier moves everyone on it. Changed only through `tiers.choose` and
6
- * `tiers.pick`, which say who may; read by `_id`, so a turn's asking costs
7
- * what the owner has cached.
2
+ * Which tier applies where (`modelChoice`): the org's (`_id: "org"`, with
3
+ * `cap`, the highest tier its members may choose), a space's (`space:<id>`),
4
+ * a conversation's (`thread:<id>`) and a person's own default for new chats
5
+ * (`person:<id>`). Each a tier by its name, never a copy: changing a tier
6
+ * moves everyone on it. Changed only through `tiers.choose` and `tiers.cap`,
7
+ * which say who may; read by `_id`, so a turn's asking costs what the owner
8
+ * has cached.
8
9
  *
9
10
  * Queries, for front ends, as the person looking:
10
- * - `offered` `{ space? }`: the tiers on offer with each agent's model and a
11
- * typical reply's price at this org's prices, what the org chose and the
12
- * plan gives, and with `space`, what the space chose; then what applies
13
- * there, each agent's model and where it comes from. Anyone in the org reads
14
- * the org's; a space's, those who reach it.
15
- * - `agent` `{ agent, space }`: one of the app's agents in a space, its model
16
- * and where it comes from, and when it's picked for the space, where taking
17
- * the pick back leaves it (`without`); null for an agent that isn't the app's.
11
+ * - `offered` `{ space?, thread? }`: the org's tiers with each kind of
12
+ * work's chain and a typical message's price at this org's prices, the
13
+ * kinds and the app's agents of each, what each level chose, and what
14
+ * applies there. Anyone in the org reads it; a space's and a
15
+ * conversation's, those who reach them.
16
+ * - `agent` `{ agent, space?, thread? }`: one agent there: its kind, the tier
17
+ * in force and its chain in it.
18
18
  */
19
19
  import { defineCollection, forbidden, people } from "copilotz";
20
20
  import { agentAt, shownAt } from "../shared/shown.js";
21
21
  const choiceSchema = {
22
22
  type: "object",
23
23
  properties: {
24
- /** The space it's for; none: the org's. */
24
+ /** The space or conversation it's for, or the person whose default it is; none: the org's. */
25
25
  space: { type: "string" },
26
- /** A tier by its name; none: the one above (the org's, then the plan's). */
26
+ thread: { type: "string" },
27
+ person: { type: "string" },
28
+ /** A tier by its name; none: the one above. */
27
29
  tier: { type: "string" },
28
- /** Models picked for some of the app's agents, by name, as an agent's `model` takes one. */
29
- custom: { type: "object" },
30
+ /** The org's: the highest tier its members may choose; none: any. */
31
+ cap: { type: "string" },
30
32
  at: { type: "string" },
31
33
  },
32
34
  required: ["at"],
33
35
  };
34
- /** Only someone who reaches the space reads what it chose: the rules say whether they do. */
35
- async function reached(ctx, space) {
36
- const found = await ctx.db.collection("space").findOne({ _id: space });
37
- if (!found)
36
+ /** Only someone who reaches the space or the conversation reads what it chose: the rules say. */
37
+ async function reached(ctx, where) {
38
+ if (where.space && !await ctx.db.collection("space").findOne({ _id: where.space })) {
38
39
  throw forbidden("You can't read that space.");
40
+ }
41
+ if (where.thread && !await ctx.db.collection("thread").findOne({ _id: where.thread })) {
42
+ throw forbidden("You can't read that conversation.");
43
+ }
39
44
  }
40
45
  export const modelChoice = defineCollection({
41
46
  schema: choiceSchema,
@@ -43,16 +48,23 @@ export const modelChoice = defineCollection({
43
48
  offered: {
44
49
  allow: people,
45
50
  run: async (_, params, ctx) => {
46
- if (params.space)
47
- await reached(ctx, params.space);
48
- return await shownAt(ctx, params.space);
51
+ const where = {
52
+ ...(params.space ? { space: params.space } : {}),
53
+ ...(params.thread ? { thread: params.thread } : {}),
54
+ };
55
+ await reached(ctx, where);
56
+ return await shownAt(ctx, where);
49
57
  },
50
58
  },
51
59
  agent: {
52
60
  allow: people,
53
61
  run: async (_, params, ctx) => {
54
- await reached(ctx, params.space);
55
- return await agentAt(ctx, params.agent, params.space);
62
+ const where = {
63
+ ...(params.space ? { space: params.space } : {}),
64
+ ...(params.thread ? { thread: params.thread } : {}),
65
+ };
66
+ await reached(ctx, where);
67
+ return await agentAt(ctx, params.agent, where);
56
68
  },
57
69
  },
58
70
  },