@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
@@ -1,155 +1,249 @@
1
- import { agentsOf, modelOf } from "copilotz/plugins/agents/contract";
2
- import { carried, ORG_CHOICE, primaryOf, resolve, spaceChoice, tierIn, tiersOf, } from "./tiers.js";
3
- /** A typical reply's tokens (see above). */
1
+ import { agentsOf } from "copilotz/plugins/agents/contract";
2
+ import { personOf } from "copilotz";
3
+ import { entryOf, kindOf, kindsIn, ORG_CHOICE, personChoice, primaryOf, spaceChoice, threadChoice, tierIn, tiersIn, } from "./tiers.js";
4
+ /** A typical reply's tokens, where the app measured no message (see above). */
4
5
  export const REPLY = { input: 20_000, cached: 16_000, output: 1_000 };
6
+ /** The org's tiers and kinds, as it keeps them. */
7
+ export async function orgTiersOf(ctx) {
8
+ const [templates, kinds] = await Promise.all([
9
+ ctx.db.collection("tierTemplate").find({}),
10
+ ctx.db.collection("tierKind").find({}),
11
+ ]);
12
+ return { tiers: tiersIn(ctx, templates), kinds: kindsIn(ctx, kinds), templates };
13
+ }
5
14
  const choices = (ctx) => ctx.db.collection("modelChoice");
6
15
  /** The org's plan's tier, through the `planTier` resource when something declares it. */
7
16
  async function planTierOf(ctx) {
8
17
  const source = ctx.resources.planTier;
9
18
  return source?.action ? await ctx.call(source.action, {}) : null;
10
19
  }
11
- /** What applies at a level: the org's and the plan's, and the space's when there is one. */
12
- export async function levelsOf(ctx, space) {
13
- const [org, here, plan] = await Promise.all([
14
- choices(ctx).findOne({ _id: ORG_CHOICE }),
15
- space ? choices(ctx).findOne({ _id: spaceChoice(space) }) : null,
20
+ /** What each level chose, where asked: the conversation, the person, the space, the org, the plan. */
21
+ export async function levelsOf(ctx, where) {
22
+ const one = (id) => id ? choices(ctx).findOne({ _id: id }) : Promise.resolve(null);
23
+ const [thread, person, space, org, plan] = await Promise.all([
24
+ one(where.thread && threadChoice(where.thread)),
25
+ one(where.person && personChoice(where.person)),
26
+ one(where.space && spaceChoice(where.space)),
27
+ one(ORG_CHOICE),
16
28
  planTierOf(ctx),
17
29
  ]);
18
- return { org, ...(space ? { space: here } : {}), plan };
30
+ return { thread, person, space, org, plan };
31
+ }
32
+ /** The app's measured message, when it has one. */
33
+ const typicalOf = (ctx) => {
34
+ const typical = ctx.resources.typicalMessage;
35
+ return typical && Object.keys(typical).length ? typical : undefined;
36
+ };
37
+ /** What the org's plan gives in credits, through the `planCredits` resource when something declares it. */
38
+ async function creditsOf(ctx) {
39
+ const source = ctx.resources.planCredits;
40
+ if (!source?.action)
41
+ return null;
42
+ return await ctx.call(source.action, {}).catch(() => null);
19
43
  }
20
- /** Names and prices for models, by `"provider/model"` and effort, each asked once. */
21
- async function modelsShown(ctx, refs) {
22
- const primaries = refs.map((ref) => primaryOf(ctx, ref));
23
- const ids = [...new Set(primaries.flatMap((p) => p ? [p.id] : []))];
44
+ /** Each model a ref tries, in order, each as `primaryOf` reads one. */
45
+ const entriesOf = (ctx, ref) => (Array.isArray(ref) ? ref : [ref]).flatMap((entry) => {
46
+ const found = primaryOf(ctx, entry);
47
+ return found ? [found] : [];
48
+ });
49
+ /**
50
+ * Names and prices for models, by `"provider/model"` and effort, each asked
51
+ * once: a typical message's part for the agent (its calls, each call's
52
+ * tokens), else a plain reply. A model is named by the first it tries, and
53
+ * priced at the first the app charges something for: one on the org's own key
54
+ * or free here (a person's ChatGPT plan) is passed over for the next, as a
55
+ * turn is when the person hasn't connected that plan; a list all on the org's
56
+ * own keys isn't priced.
57
+ */
58
+ async function modelsShown(ctx, uses) {
59
+ const typical = typicalOf(ctx);
60
+ const lists = uses.map((use) => entriesOf(ctx, use.ref));
61
+ const ids = [...new Set(lists.flat().map((entry) => entry.id))];
24
62
  const offered = await Promise.all(ids.map((model) => ctx.call("models.offered", { model })
25
63
  .then((answer) => answer.model, () => null)));
26
64
  const known = new Map(ids.map((id, i) => [id, offered[i]]));
27
65
  const declared = (ctx.resources.provider ?? {});
28
- // What a typical reply is charged, for those the app charges for, when there's a ledger.
29
- const charged = ctx.resources.usage ? ids.filter((id) => !known.get(id)?.ownKey) : [];
30
- const quoted = charged.length
66
+ // What one call is charged: each agent's (a plain reply's, without a measured message).
67
+ const callOf = (agent) => {
68
+ const measured = typical?.[agent];
69
+ if (!typical)
70
+ return REPLY;
71
+ if (!measured)
72
+ return undefined;
73
+ const { calls: _, ...tokens } = measured;
74
+ return tokens;
75
+ };
76
+ const keyOf = (id, agent) => typical ? `${id} ${agent}` : id;
77
+ // Every model each tries that's on the app's keys, when there's a ledger, each call asked once.
78
+ const charged = lists.map((list) => ctx.resources.usage ? list.filter((entry) => !known.get(entry.id)?.ownKey) : []);
79
+ const asked = new Map();
80
+ charged.forEach((list, i) => {
81
+ if (!callOf(uses[i].agent))
82
+ return;
83
+ for (const entry of list) {
84
+ asked.set(keyOf(entry.id, uses[i].agent), { id: entry.id, agent: uses[i].agent });
85
+ }
86
+ });
87
+ const quotes = [...asked.values()];
88
+ const quoted = quotes.length
31
89
  ? await ctx.call("usage.quotes", {
32
- uses: charged.map((id) => {
90
+ uses: quotes.map(({ id, agent }) => {
33
91
  const slash = id.indexOf("/");
34
92
  const from = declared[id.slice(0, slash)]?.catalog;
35
93
  return {
36
94
  meter: "llm",
37
95
  subject: id,
38
96
  catalog: `${typeof from === "string" ? from : id.slice(0, slash)}${id.slice(slash)}`,
39
- quantities: REPLY,
97
+ quantities: callOf(agent),
40
98
  };
41
99
  }),
42
- }).catch(() => charged.map(() => null))
100
+ }).catch(() => quotes.map(() => null))
43
101
  : [];
44
- const prices = new Map(charged.map((id, i) => [id, quoted[i] ?? null]));
45
- return primaries.map((p) => {
102
+ const prices = new Map([...asked.keys()].map((key, i) => [key, quoted[i] ?? null]));
103
+ return lists.map((list, i) => {
104
+ const p = list[0];
46
105
  if (!p)
47
106
  return undefined;
48
107
  const info = known.get(p.id);
108
+ const agent = uses[i].agent;
109
+ // The first that costs something: one that's free here is a plan the person brings.
110
+ const each = charged[i].map((entry) => prices.get(keyOf(entry.id, agent)) ?? null);
111
+ const call = each.find((price) => price !== null && price > 0) ??
112
+ each.find((price) => price !== null) ?? null;
113
+ const calls = typical ? typical[agent]?.calls ?? 0 : 1;
114
+ const named = (id) => known.get(id)?.name ?? id.slice(id.indexOf("/") + 1);
49
115
  return {
50
116
  id: p.id,
51
- name: info?.name ?? p.id.slice(p.id.indexOf("/") + 1),
117
+ name: named(p.id),
52
118
  ...(p.effort ? { effort: p.effort } : {}),
53
- price: prices.get(p.id) ?? null,
54
- ...(info?.ownKey ? { ownKey: true } : {}),
119
+ price: call === null ? null : call * calls,
120
+ ...(info?.ownKey && !charged[i].length ? { ownKey: true } : {}),
121
+ ...(list.length > 1
122
+ ? {
123
+ fallbacks: list.slice(1).map((entry) => ({
124
+ id: entry.id,
125
+ name: named(entry.id),
126
+ ...(entry.effort ? { effort: entry.effort } : {}),
127
+ })),
128
+ }
129
+ : {}),
55
130
  };
56
131
  });
57
132
  }
58
- /** The average of what's priced; null when nothing is. */
59
- const average = (prices) => {
133
+ /** What's priced, summed (a message) or averaged (a reply); null when nothing is. */
134
+ const total = (prices, per) => {
60
135
  const priced = prices.filter((p) => p !== null);
61
- return priced.length ? priced.reduce((a, b) => a + b, 0) / priced.length : null;
136
+ if (!priced.length)
137
+ return null;
138
+ const sum = priced.reduce((a, b) => a + b, 0);
139
+ return per === "message" ? sum : sum / priced.length;
62
140
  };
63
- /** Everything a page shows, at the org's level, or a space's. */
64
- export async function shownAt(ctx, space) {
65
- const tiers = Object.entries(tiersOf(ctx))
66
- .sort(([a, x], [b, y]) => (x.order ?? 0) - (y.order ?? 0) || a.localeCompare(b));
141
+ /** Everything a page shows, at the org's level, or where asked (a space, a conversation). */
142
+ export async function shownAt(ctx, where = {}) {
143
+ const person = personOf(ctx.actor);
144
+ const [{ tiers, kinds, templates }, levels, credits] = await Promise.all([
145
+ orgTiersOf(ctx),
146
+ levelsOf(ctx, { ...where, ...(person ? { person } : {}) }),
147
+ creditsOf(ctx),
148
+ ]);
149
+ const per = typicalOf(ctx) ? "message" : "reply";
67
150
  const agents = agentsOf(ctx);
68
- const levels = await levelsOf(ctx, space);
69
- // What each agent uses now, a pick with what it carries over.
70
- const now = await Promise.all(Object.keys(agents).map(async (agent) => {
71
- const found = resolve(ctx, agent, levels);
72
- return { agent, from: found.from, model: await carried(ctx, found) };
151
+ const kindNames = Object.keys(kinds);
152
+ const list = Object.entries(tiers);
153
+ // Every tier's chain for every kind, asked for at once, and taken back in the same order.
154
+ const uses = list.flatMap(([, tier]) => kindNames.flatMap((kind) => {
155
+ const ref = entryOf(tier, kind, kinds);
156
+ return ref === undefined ? [] : [{ ref, agent: kinds[kind].base ?? kind }];
73
157
  }));
74
- const custom = (choice) => Object.entries(choice?.custom ?? {}).filter(([agent]) => agents[agent]);
75
- // Every model shown, asked for at once, and taken back in the same order.
76
- const refs = [
77
- ...tiers.flatMap(([, tier]) => Object.values(tier.agents)),
78
- ...custom(levels.org).map(([, ref]) => ref),
79
- ...custom(levels.space).map(([, ref]) => ref),
80
- ...now.map((n) => n.model ?? modelOf(agents[n.agent])),
81
- ];
82
- const shown = await modelsShown(ctx, refs);
158
+ const shown = await modelsShown(ctx, uses);
83
159
  let cursor = 0;
84
- const next = () => shown[cursor++];
85
- const tiersShown = tiers.map(([name, tier]) => {
86
- const listed = Object.keys(tier.agents).map((agent) => ({ agent, model: next() }))
87
- .filter((a) => !!a.model && !!agents[a.agent]);
160
+ const tiersShown = list.map(([name, tier]) => {
161
+ const own = kindNames.flatMap((kind) => entryOf(tier, kind, kinds) === undefined ? [] : [{ kind, model: shown[cursor++] }]).filter((k) => !!k.model);
88
162
  return {
89
163
  name,
90
164
  title: tier.title,
91
165
  ...(tier.description ? { description: tier.description } : {}),
92
- price: average(listed.map((a) => a.model.price)),
93
- agents: listed,
166
+ // Only the app's kinds count toward a typical message: an org's own is extra.
167
+ price: total(own.filter((k) => !kinds[k.kind].base).map((k) => k.model.price), per),
168
+ ...(tier.app ? { app: true } : {}),
169
+ ...(tier.changed ? { changed: true } : {}),
170
+ ...(tier.made ? { made: true } : {}),
171
+ kinds: own,
94
172
  };
95
173
  });
96
- const choiceShown = (choice) => ({
97
- tier: choice?.tier && Object.hasOwn(tiersOf(ctx), choice.tier) ? choice.tier : null,
98
- custom: Object.fromEntries(custom(choice).flatMap(([agent]) => {
99
- const model = next();
100
- return model ? [[agent, model]] : [];
101
- })),
102
- });
103
- const org = choiceShown(levels.org);
104
- const here = space ? choiceShown(levels.space) : undefined;
105
- const inForce = tierIn(tiersOf(ctx), levels);
174
+ const offered = (name) => name && tiers[name] ? name : null;
175
+ const app = (ctx.resources.tiers ?? {});
176
+ const inForce = tierIn(tiers, levels);
106
177
  return {
107
178
  tiers: tiersShown,
108
- agents: Object.entries(agents).map(([agent, def]) => ({
109
- agent,
110
- name: def.name ?? agent[0].toUpperCase() + agent.slice(1),
111
- ...(def.icon ? { icon: def.icon } : {}),
112
- })),
113
- plan: levels.plan && Object.hasOwn(tiersOf(ctx), levels.plan) ? levels.plan : null,
114
- org,
115
- ...(here ? { space: here } : {}),
116
- now: {
117
- tier: inForce ? { name: inForce.tier, by: inForce.by } : null,
118
- agents: Object.fromEntries(now.flatMap((n) => {
119
- const model = next();
120
- return model ? [[n.agent, { model, from: titled(ctx, n.from) }]] : [];
179
+ kinds: kindNames.map((kind) => ({
180
+ kind,
181
+ title: kinds[kind].title,
182
+ ...(kinds[kind].description ? { description: kinds[kind].description } : {}),
183
+ ...(kinds[kind].base ? { base: kinds[kind].base } : {}),
184
+ agents: Object.entries(agents)
185
+ .filter(([name, def]) => kindOf(ctx, name, def.like, kinds) === kind && agents[name].default)
186
+ .map(([name, def]) => ({
187
+ agent: name,
188
+ name: def.name ?? name[0].toUpperCase() + name.slice(1),
189
+ ...(def.icon ? { icon: def.icon } : {}),
121
190
  })),
122
- },
191
+ })),
192
+ removed: templates.filter((t) => t.off && app[t._id])
193
+ .map((t) => ({ name: t._id, title: app[t._id].title })),
194
+ plan: offered(levels.plan),
195
+ org: { tier: offered(levels.org?.tier), cap: offered(levels.org?.cap) },
196
+ ...(where.space ? { space: { tier: offered(levels.space?.tier) } } : {}),
197
+ ...(where.thread ? { thread: { tier: offered(levels.thread?.tier) } } : {}),
198
+ person: { tier: offered(levels.person?.tier) },
199
+ now: inForce ?? null,
200
+ per,
123
201
  reply: REPLY,
202
+ credits,
124
203
  };
125
204
  }
126
- /** Where a model comes from, with its tier's title when it's a tier's. */
127
- const titled = (ctx, from) => "tier" in from && from.tier ? { ...from, title: tiersOf(ctx)[from.tier]?.title } : from;
205
+ /** Which of the app's kinds one people made works as, as chat's `agentSources` say. */
206
+ async function likeOf(ctx, agent) {
207
+ const sources = Object.values((ctx.resources.agentSources ?? {}));
208
+ for (const source of sources) {
209
+ const told = await ctx.call(source.action, {
210
+ names: [agent],
211
+ })
212
+ .catch(() => ({}));
213
+ if (told[agent])
214
+ return told[agent].like;
215
+ }
216
+ return undefined;
217
+ }
128
218
  /**
129
- * One of the app's agents where it works in a space: its model, where that
130
- * comes from, and, when it's picked for the space, what it would be without
131
- * (`without`: where taking the pick back leaves it).
219
+ * One agent where it works (a space, maybe a conversation): its kind of work,
220
+ * the tier in force there and who chose it, and its chain in that tier; null
221
+ * when no tier applies.
132
222
  */
133
- export async function agentAt(ctx, agent, space) {
134
- const own = agentsOf(ctx)[agent];
135
- if (!own)
136
- return null;
137
- const levels = await levelsOf(ctx, space);
138
- const now = resolve(ctx, agent, levels);
139
- const picked = levels.space?.custom?.[agent] !== undefined;
140
- const { [agent]: _, ...others } = levels.space?.custom ?? {};
141
- const before = picked
142
- ? resolve(ctx, agent, { ...levels, space: { ...levels.space, custom: others } })
143
- : undefined;
144
- const [shown, without] = await modelsShown(ctx, [
145
- await carried(ctx, now) ?? modelOf(own),
146
- ...(before ? [await carried(ctx, before) ?? modelOf(own)] : []),
223
+ export async function agentAt(ctx, agent, where) {
224
+ const person = personOf(ctx.actor);
225
+ const [{ tiers, kinds }, levels] = await Promise.all([
226
+ orgTiersOf(ctx),
227
+ levelsOf(ctx, { ...where, ...(person ? { person } : {}) }),
147
228
  ]);
148
- if (!shown)
229
+ const declared = agentsOf(ctx)[agent];
230
+ const like = declared ? declared.like : await likeOf(ctx, agent);
231
+ const kind = kindOf(ctx, agent, like, kinds);
232
+ const inForce = tierIn(tiers, levels);
233
+ if (!kind || !inForce)
234
+ return null;
235
+ const ref = entryOf(tiers[inForce.tier], kind, kinds);
236
+ if (ref === undefined)
237
+ return null;
238
+ const [model] = await modelsShown(ctx, [{ ref, agent: kinds[kind].base ?? kind }]);
239
+ if (!model)
149
240
  return null;
150
241
  return {
151
- model: shown,
152
- from: titled(ctx, now.from),
153
- ...(before && without ? { without: { model: without, from: titled(ctx, before.from) } } : {}),
242
+ kind,
243
+ kindTitle: kinds[kind].title,
244
+ tier: inForce.tier,
245
+ tierTitle: tiers[inForce.tier].title,
246
+ by: inForce.by,
247
+ model,
154
248
  };
155
249
  }
@@ -1,88 +1,133 @@
1
1
  /**
2
- * Tiers, and which model each of the app's agents thinks with: the app's
3
- * tiers (its `tiers` resource), what an org and a space chose (`modelChoice`),
4
- * and what the org's plan gives, worked out the same way for a turn and for a
5
- * page. Only plain data in and out: no reads here.
2
+ * Tiers as templates, worked out the same way for a turn and for a page. Only
3
+ * plain data in and out: no reads here.
4
+ *
5
+ * - **A kind of work** (`north`, as the app calls it, titled "Decides") is
6
+ * what an agent does, as far as its model goes. The app's kinds are its
7
+ * tiers' keys, titled by its `tierKinds` resource; an org adds its own,
8
+ * each based on one of the app's (`tierKind`). Every agent has one: one of
9
+ * the app's agents is its own kind when the tiers name it; any other says
10
+ * which (`like`), else it's the app's first.
11
+ * - **A tier** is a template: for each kind of work, a model chain (as an
12
+ * agent's `model` takes one: a model, settings around one, or a list tried
13
+ * in order). The app declares its tiers (`tiers`); an org keeps its own
14
+ * version of one (`tierTemplate`: kinds' chains changed, or `off`, taken
15
+ * away) and makes new ones (`made`). A kind a tier doesn't name takes its
16
+ * base kind's chain there.
17
+ * - **Which tier a turn uses**, the first that applies: the conversation's,
18
+ * the person's own default for new chats, the space's, the org's, the
19
+ * plan's (`modelChoice`).
6
20
  */
7
21
  import type { Resources } from "copilotz";
8
22
  import type { ModelRef } from "copilotz/plugins/models/contract";
9
- /** A tier: a model for each of the app's agents, named by what it's good for. */
23
+ /** A tier: a model chain for each kind of work, named by what it's good for. */
10
24
  export type Tier = {
11
25
  title: string;
12
26
  /** What it's good for, in a line: "Quick answers, routine work, volume". */
13
27
  description?: string;
14
28
  /** Among the tiers, lowest first. */
15
29
  order?: number;
16
- /** Each agent's model, as an agent's `model` takes it: one, settings around one, or a list. */
30
+ /** Each kind of work's chain, by kind, as an agent's `model` takes it. */
17
31
  agents: Record<string, ModelRef>;
18
32
  };
19
- /** What an org or a space chose: a tier by its name, and models picked for some agents. Unset: theirs above. */
33
+ /** A tier as the org has it: the app's (as it is, or `changed`), or one the org `made`. */
34
+ export type OrgTier = Tier & {
35
+ app?: true;
36
+ changed?: true;
37
+ made?: true;
38
+ };
39
+ /** What an org keeps of a tier: its version of the app's (or that it took it away), or one it made. */
40
+ export type Template = {
41
+ _id: string;
42
+ title?: string;
43
+ description?: string;
44
+ order?: number;
45
+ agents?: Record<string, ModelRef>;
46
+ /** One of the app's the org took away. */
47
+ off?: boolean;
48
+ /** One the org made. */
49
+ made?: boolean;
50
+ at: string;
51
+ };
52
+ /** A kind of work: what it's called, and for one an org added, the app's kind it starts from. */
53
+ export type Kind = {
54
+ title: string;
55
+ description?: string;
56
+ base?: string;
57
+ };
58
+ /** One an org added (`tierKind`), by its name. */
59
+ export type OrgKind = {
60
+ _id: string;
61
+ title: string;
62
+ description?: string;
63
+ base: string;
64
+ at: string;
65
+ };
66
+ /** What a level chose: a tier by its name; the org's also the highest tier its members may use. */
20
67
  export type Choice = {
21
68
  tier?: string;
22
- custom?: Record<string, ModelRef>;
69
+ cap?: string;
23
70
  };
24
- /** Where a choice is kept: the org's, or a space's. */
71
+ /** Where a choice is kept. */
25
72
  export declare const ORG_CHOICE = "org";
26
73
  export declare const spaceChoice: (space: string) => string;
27
- /** The app's tiers. */
28
- export declare const tiersOf: (ctx: {
29
- resources: Resources;
30
- }) => Record<string, Tier>;
31
- /** Where a model comes from: picked here, a tier chosen here, the plan's tier, or the agent's own. */
32
- export type From = {
33
- by: "space" | "org";
34
- custom: true;
35
- tier?: string;
36
- } | {
37
- by: "space" | "org" | "plan";
38
- tier: string;
39
- } | {
40
- by: "app";
41
- };
42
- /** What applies, at one level and those above it: each a choice, or none. */
74
+ export declare const threadChoice: (thread: string) => string;
75
+ export declare const personChoice: (person: string) => string;
76
+ /** Who chose the tier in force. */
77
+ export type By = "thread" | "person" | "space" | "org" | "plan";
78
+ /** What applies, at each level: each a choice, or none. */
43
79
  export type Levels = {
80
+ thread?: Choice | null;
81
+ person?: Choice | null;
44
82
  space?: Choice | null;
45
83
  org?: Choice | null;
46
84
  plan?: string | null;
47
85
  };
48
- /** The tier in force: the space's, else the org's, else the plan's; and who chose it. */
49
- export declare function tierIn(tiers: Record<string, Tier>, levels: Levels): {
50
- tier: string;
51
- by: "space" | "org" | "plan";
52
- } | undefined;
53
- /** What `resolve` says: the model, where it comes from, and for a pick, what it stands in front of. */
54
- export type Resolved = {
55
- model: ModelRef | null;
56
- from: From;
57
- /** A pick: itself, the entry it replaces (whose settings carry over), and the list behind it. */
58
- pick?: {
59
- ref: ModelRef;
60
- replaces: ModelRef;
61
- behind: ModelRef[];
62
- };
63
- };
86
+ /** The app's tiers, as it declares them. */
87
+ export declare const appTiersOf: (ctx: {
88
+ resources: Resources;
89
+ }) => Record<string, Tier>;
90
+ /** The app's kinds of work: its tiers' keys, in the order its agents are declared, titled. */
91
+ export declare function appKindsOf(ctx: {
92
+ resources: Resources;
93
+ }): Record<string, Kind>;
94
+ /** Every kind of work: the app's, then those the org added (on an app's kind still there). */
95
+ export declare function kindsIn(ctx: {
96
+ resources: Resources;
97
+ }, own: OrgKind[]): Record<string, Kind>;
98
+ /** The kind an agent works as: what it says (`like`); its own, where tiers name it; else the app's first. */
99
+ export declare function kindOf(ctx: {
100
+ resources: Resources;
101
+ }, name: string, like: string | undefined, kinds: Record<string, Kind>): string | undefined;
64
102
  /**
65
- * Which model one of the app's agents thinks with, and where it comes from,
66
- * the first that applies: the space's pick for it, the space's tier, the
67
- * org's pick, the org's tier, the plan's tier. A tier that doesn't name the
68
- * agent leaves it on its own model; so does nothing chosen (`model` null).
69
- * A pick keeps the list it stands in front of behind it (the tier in force
70
- * below it, or the agent's own), so a model that fails falls back as before;
71
- * `carried` gives it that entry's settings. An agent that isn't the app's is
72
- * none of this: null.
103
+ * The org's tiers: the app's as the org keeps them (its version over them,
104
+ * those it took away left out), then those it made; each by its name, in
105
+ * order.
73
106
  */
74
- export declare function resolve(ctx: {
107
+ export declare function tiersIn(ctx: {
75
108
  resources: Resources;
76
- }, agent: string, levels: Levels): Resolved;
109
+ }, templates: Template[]): Record<string, OrgTier>;
110
+ /** A kind's chain in a tier: its own entry there, else its base kind's. */
111
+ export declare function entryOf(tier: Tier | undefined, kind: string | undefined, kinds: Record<string, Kind>): ModelRef | undefined;
112
+ /** The tier in force, and who chose it: the conversation, the person, the space, the org, the plan. */
113
+ export declare function tierIn(tiers: Record<string, unknown>, levels: Levels): {
114
+ tier: string;
115
+ by: By;
116
+ } | undefined;
117
+ /** Whether a tier is above the highest the org lets its members use (`cap`). */
118
+ export declare function aboveCap(tiers: Record<string, unknown>, tier: string, cap?: string): boolean;
119
+ /** A model, as a list: what's tried in order. */
120
+ export declare const listOf: (ref: ModelRef) => ModelRef[];
121
+ /** The model a ref tries first, as `"provider/model"` when it says so plainly. */
122
+ export declare const firstId: (ref: ModelRef) => string | undefined;
77
123
  /** What a model's first choice is set to think and write with. */
78
- type Settings = {
124
+ export type Settings = {
79
125
  effort?: string;
80
126
  maxTokens?: number;
81
127
  };
82
128
  /**
83
129
  * The model a ref tries first, with the effort and most tokens it's asked
84
- * for, following aliases (`resources.model`): what a page shows of it, and
85
- * what a pick carries over.
130
+ * for, following aliases (`resources.model`): what a page shows of it.
86
131
  */
87
132
  export declare function primaryOf(ctx: {
88
133
  resources: Resources;
@@ -90,14 +135,12 @@ export declare function primaryOf(ctx: {
90
135
  id: string;
91
136
  } & Settings) | undefined;
92
137
  /**
93
- * The model `resolve` found, a pick given what the entry it replaces sets:
94
- * its `maxTokens`, and its `effort` when the picked model takes that level
95
- * (the catalog's, `models.efforts`), else none, so a pick isn't cut short by
96
- * an adapter's default nor refused for an effort it doesn't have. What the
97
- * pick sets itself wins.
138
+ * A chain someone set on a page (each entry a model), with what the chain it
139
+ * replaces sets on its first entry: its `maxTokens`, and its `effort` where
140
+ * each model takes it and none was said (the catalog's, `models.efforts`).
141
+ * So a chain set by hand isn't cut short by an adapter's default.
98
142
  */
99
- export declare function carried(ctx: {
143
+ export declare function settled(ctx: {
100
144
  resources: Resources;
101
145
  call<R>(action: string, input?: unknown): Promise<R>;
102
- }, resolved: Resolved): Promise<ModelRef | null>;
103
- export {};
146
+ }, chain: ModelRef[], like: ModelRef | undefined): Promise<ModelRef[]>;