@pairz/team 0.0.1 → 1.0.0-rc.82

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 (96) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +51 -1
  3. package/WORDS.md +370 -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 +61 -3
  9. package/team/actions/agent.d.ts +14 -0
  10. package/team/actions/agent.js +401 -0
  11. package/team/actions/define.d.ts +1 -0
  12. package/team/actions/define.js +45 -0
  13. package/team/collections/agent.d.ts +170 -0
  14. package/team/collections/agent.js +126 -0
  15. package/team/contract.d.ts +11 -0
  16. package/team/contract.js +1 -0
  17. package/team/mod.d.ts +28 -0
  18. package/team/mod.js +64 -0
  19. package/team/resources/propose.d.ts +1 -0
  20. package/team/resources/propose.js +102 -0
  21. package/team/shared/definition.d.ts +3 -0
  22. package/team/shared/definition.js +11 -0
  23. package/team/shared/everywhere.d.ts +11 -0
  24. package/team/shared/everywhere.js +29 -0
  25. package/team/shared/limits.d.ts +17 -0
  26. package/team/shared/limits.js +17 -0
  27. package/team/views/web/agent-add.d.ts +14 -0
  28. package/team/views/web/agent-add.js +58 -0
  29. package/team/views/web/agent-describe.d.ts +38 -0
  30. package/team/views/web/agent-describe.js +44 -0
  31. package/team/views/web/agent-fields.d.ts +35 -0
  32. package/team/views/web/agent-fields.js +88 -0
  33. package/team/views/web/agent-known.d.ts +22 -0
  34. package/team/views/web/agent-known.js +62 -0
  35. package/team/views/web/agent-new.d.ts +27 -0
  36. package/team/views/web/agent-new.js +150 -0
  37. package/team/views/web/agent-org.d.ts +12 -0
  38. package/team/views/web/agent-org.js +89 -0
  39. package/team/views/web/agent-palette.d.ts +19 -0
  40. package/team/views/web/agent-palette.js +203 -0
  41. package/team/views/web/agent-steps.d.ts +23 -0
  42. package/team/views/web/agent-steps.js +56 -0
  43. package/team/views/web/agent.d.ts +20 -0
  44. package/team/views/web/agent.js +310 -0
  45. package/team/views/web/agents.d.ts +48 -0
  46. package/team/views/web/agents.js +59 -0
  47. package/team/views/web/model-pick.d.ts +36 -0
  48. package/team/views/web/model-pick.js +44 -0
  49. package/team/views/web/model-picker.d.ts +83 -0
  50. package/team/views/web/model-picker.js +262 -0
  51. package/team/views/web/places.d.ts +24 -0
  52. package/team/views/web/places.js +10 -0
  53. package/team/views/web/style.d.ts +9 -0
  54. package/team/views/web/style.js +251 -0
  55. package/team/views/web/words.d.ts +5 -0
  56. package/team/views/web/words.js +246 -0
  57. package/team/web.d.ts +22 -0
  58. package/team/web.js +43 -0
  59. package/tiers/actions/choose.d.ts +7 -0
  60. package/tiers/actions/choose.js +336 -0
  61. package/tiers/actions/model.d.ts +1 -0
  62. package/tiers/actions/model.js +61 -0
  63. package/tiers/collections/choice.d.ts +54 -0
  64. package/tiers/collections/choice.js +81 -0
  65. package/tiers/collections/template.d.ts +76 -0
  66. package/tiers/collections/template.js +42 -0
  67. package/tiers/contract.d.ts +23 -0
  68. package/tiers/contract.js +1 -0
  69. package/tiers/mod.d.ts +75 -0
  70. package/tiers/mod.js +114 -0
  71. package/tiers/shared/plans.d.ts +61 -0
  72. package/tiers/shared/plans.js +74 -0
  73. package/tiers/shared/shown.d.ts +189 -0
  74. package/tiers/shared/shown.js +284 -0
  75. package/tiers/shared/tiers.d.ts +157 -0
  76. package/tiers/shared/tiers.js +179 -0
  77. package/tiers/views/web/composer.d.ts +15 -0
  78. package/tiers/views/web/composer.js +87 -0
  79. package/tiers/views/web/org.d.ts +69 -0
  80. package/tiers/views/web/org.js +334 -0
  81. package/tiers/views/web/say.d.ts +146 -0
  82. package/tiers/views/web/say.js +150 -0
  83. package/tiers/views/web/setup.d.ts +23 -0
  84. package/tiers/views/web/setup.js +80 -0
  85. package/tiers/views/web/space.d.ts +32 -0
  86. package/tiers/views/web/space.js +78 -0
  87. package/tiers/views/web/style.d.ts +7 -0
  88. package/tiers/views/web/style.js +193 -0
  89. package/tiers/views/web/team.d.ts +38 -0
  90. package/tiers/views/web/team.js +118 -0
  91. package/tiers/views/web/words.d.ts +48 -0
  92. package/tiers/views/web/words.js +122 -0
  93. package/tiers/web.d.ts +31 -0
  94. package/tiers/web.js +72 -0
  95. package/web.d.ts +14 -0
  96. package/web.js +14 -0
@@ -0,0 +1,336 @@
1
+ /**
2
+ * Choosing tiers, and the org's own: all in person.
3
+ *
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
+ * where the org uses only its default (`fixed`), nobody chooses below it.
9
+ * - `tiers.cap` `{ tier?, fixed? }`: what the org's members may use, by its
10
+ * owners and admins: every tier (`tier: null`), tiers up to one (`tier`),
11
+ * or only the org's default (`fixed: true`).
12
+ * - `tiers.order` `{ tiers }`: the org's tiers in the order it wants them,
13
+ * lowest first, each once: where a cap stops.
14
+ * - `tiers.save` `{ tier, title?, description?, kinds?, from? }`: an org's
15
+ * tier, by its owners and admins: its version of one of the app's, or a
16
+ * new one (`title`, its chains copied from `from`, else the tier in force).
17
+ * `kinds`: chains by kind of work (a list of models, tried in order; null:
18
+ * back to the app's there), each checked as an agent's model is, and each
19
+ * given the `maxTokens` and `effort` of the chain it replaces, where it
20
+ * takes it.
21
+ * - `tiers.remove` `{ tier }`: takes one of the app's away, or deletes one
22
+ * the org made; not the last. Wherever it was chosen, what's above applies.
23
+ * - `tiers.restore` `{ tier }`: one of the app's back as the app has it.
24
+ * - `tiers.kind` `{ kind?, title, base }`: a kind of work of the org's own,
25
+ * starting from one of the app's (its chain in every tier, until changed).
26
+ *
27
+ * A change applies from each conversation's next reply.
28
+ */
29
+ import { defineAction, failure, inPerson, invalid } from "pairz";
30
+ import { must, ORG } from "pairz/plugins/access/contract";
31
+ import { orgTiersOf } from "../shared/shown.js";
32
+ import { aboveCap, appKindsOf, appTiersOf, entryOf, ORG_CHOICE, personChoice, settled, spaceChoice, threadChoice, } from "../shared/tiers.js";
33
+ /** Throws unless the caller runs the org. */
34
+ const runsOrg = (ctx, what) => must(ctx, "manage", ORG, `Only the org's owners and admins ${what}.`);
35
+ /** Whether the caller runs the org, without throwing. */
36
+ const running = (ctx) => runsOrg(ctx, "do that").then(() => true, () => false);
37
+ const choices = (ctx) => ctx.as().db.collection("modelChoice");
38
+ const templates = (ctx) => ctx.as().db.collection("tierTemplate");
39
+ /** Keeps a level's choice, or deletes it once it holds nothing (no tier, nor what members use). */
40
+ async function keep(ctx, had, next) {
41
+ if (!next.tier && !next.cap && !next.fixed) {
42
+ if (had)
43
+ await choices(ctx).deleteOne({ _id: next._id });
44
+ return;
45
+ }
46
+ await choices(ctx).replaceOne({ _id: next._id }, next, { upsert: true });
47
+ }
48
+ /** A level's choice as it's kept now: a person's old order of their plans goes. */
49
+ async function choiceOf(ctx, id) {
50
+ const found = await choices(ctx).findOne({ _id: id });
51
+ if (!found)
52
+ return null;
53
+ const { harnesses: _, ...kept } = found;
54
+ return kept;
55
+ }
56
+ /** Sets or clears one level's tier, keeping what else it holds (the org's cap). */
57
+ async function setTier(ctx, id, tier, more) {
58
+ const before = await choiceOf(ctx, id);
59
+ const next = { ...before, ...more, _id: id, at: new Date().toISOString() };
60
+ if (tier)
61
+ next.tier = tier;
62
+ else
63
+ delete next.tier;
64
+ await keep(ctx, before, next);
65
+ }
66
+ export const chooseAction = defineAction({
67
+ description: "Chooses the tier for the org, a space, a conversation or your new chats",
68
+ input: {
69
+ type: "object",
70
+ properties: {
71
+ tier: { type: ["string", "null"] },
72
+ space: { type: "string" },
73
+ thread: { type: "string" },
74
+ mine: { type: "boolean" },
75
+ },
76
+ required: ["tier"],
77
+ additionalProperties: false,
78
+ },
79
+ allow: inPerson,
80
+ run: async (ctx, input) => {
81
+ const { tiers } = await orgTiersOf(ctx.as());
82
+ if (input.tier !== null && !tiers[input.tier]) {
83
+ throw invalid(`There's no tier "${input.tier}".`);
84
+ }
85
+ const where = [input.space, input.thread, input.mine].filter(Boolean).length;
86
+ if (where > 1)
87
+ throw invalid("Choose for one place at a time.");
88
+ if (input.tier) {
89
+ const org = await choices(ctx).findOne({ _id: ORG_CHOICE });
90
+ const cap = org?.cap;
91
+ // Only the default: nobody chooses another below the org.
92
+ if (org?.fixed && where) {
93
+ throw failure("forbidden", "Every chat uses your org's default tier: its owners and admins chose so.", { public: true });
94
+ }
95
+ // Over the cap: only who runs the org.
96
+ if (aboveCap(tiers, input.tier, cap) && !await running(ctx)) {
97
+ throw failure("forbidden", `Your org's owners and admins let members use tiers up to ${tiers[cap].title}.`, { public: true });
98
+ }
99
+ }
100
+ if (input.thread) {
101
+ if (!await ctx.db.collection("thread").findOne({ _id: input.thread })) {
102
+ throw failure("forbidden", "You can't change that conversation.", { public: true });
103
+ }
104
+ await setTier(ctx, threadChoice(input.thread), input.tier, { thread: input.thread });
105
+ }
106
+ else if (input.mine) {
107
+ const person = ctx.actor.id;
108
+ await setTier(ctx, personChoice(person), input.tier, { person });
109
+ }
110
+ else if (input.space) {
111
+ await must(ctx, "manage", input.space, "Only this space's managers choose its tier.");
112
+ await setTier(ctx, spaceChoice(input.space), input.tier, { space: input.space });
113
+ }
114
+ else {
115
+ await runsOrg(ctx, "choose its default tier");
116
+ await setTier(ctx, ORG_CHOICE, input.tier, {});
117
+ }
118
+ return { tier: input.tier };
119
+ },
120
+ });
121
+ export const capAction = defineAction({
122
+ description: "Sets what the org's members may use: every tier, tiers up to one, or only the default",
123
+ input: {
124
+ type: "object",
125
+ properties: {
126
+ /** The highest tier they may choose; null: any. */
127
+ tier: { type: ["string", "null"] },
128
+ /** Only the org's default: no space, conversation or person chooses another. */
129
+ fixed: { type: "boolean" },
130
+ },
131
+ additionalProperties: false,
132
+ },
133
+ allow: inPerson,
134
+ run: async (ctx, input) => {
135
+ await runsOrg(ctx, "set what members may use");
136
+ const { tiers } = await orgTiersOf(ctx.as());
137
+ const cap = input.fixed ? null : input.tier ?? null;
138
+ if (cap !== null && !tiers[cap])
139
+ throw invalid(`There's no tier "${cap}".`);
140
+ const before = await choiceOf(ctx, ORG_CHOICE);
141
+ const next = { ...before, _id: ORG_CHOICE, at: new Date().toISOString() };
142
+ delete next.cap;
143
+ delete next.fixed;
144
+ if (input.fixed)
145
+ next.fixed = true;
146
+ else if (cap)
147
+ next.cap = cap;
148
+ await keep(ctx, before, next);
149
+ return { cap, fixed: !!input.fixed };
150
+ },
151
+ });
152
+ export const orderAction = defineAction({
153
+ description: "Puts the org's tiers in order, lowest first",
154
+ transaction: true,
155
+ input: {
156
+ type: "object",
157
+ properties: { tiers: { type: "array", items: { type: "string" }, maxItems: 64 } },
158
+ required: ["tiers"],
159
+ additionalProperties: false,
160
+ },
161
+ allow: inPerson,
162
+ run: async (ctx, input) => {
163
+ await runsOrg(ctx, "change its tiers");
164
+ const { tiers } = await orgTiersOf(ctx.as());
165
+ const names = Object.keys(tiers);
166
+ if (input.tiers.length !== names.length || names.some((name) => !input.tiers.includes(name)))
167
+ throw invalid("Put each of the org's tiers in the order, once.");
168
+ const at = new Date().toISOString();
169
+ for (const [i, name] of input.tiers.entries()) {
170
+ await templates(ctx).updateOne({ _id: name }, { $set: { order: i + 1, at } }, { upsert: true });
171
+ }
172
+ return { tiers: input.tiers };
173
+ },
174
+ });
175
+ /** A name for a new tier from its title: `balanced`, `balanced-2`. */
176
+ function nameFor(title, taken) {
177
+ const base = title.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase()
178
+ .replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 32) || "tier";
179
+ for (let n = 1;; n++) {
180
+ const name = n === 1 ? base : `${base}-${n}`;
181
+ if (!taken(name))
182
+ return name;
183
+ }
184
+ }
185
+ export const saveAction = defineAction({
186
+ description: "Saves one of the org's tiers: its version of the app's, or a new one",
187
+ input: {
188
+ type: "object",
189
+ properties: {
190
+ tier: { type: "string" },
191
+ title: { type: "string", minLength: 1, maxLength: 40 },
192
+ description: { type: "string", maxLength: 200 },
193
+ /** Chains by kind of work: models in order; null, back to the app's. */
194
+ kinds: { type: "object", additionalProperties: { type: ["array", "null"], maxItems: 8 } },
195
+ /** A new tier's chains, copied from this one. */
196
+ from: { type: "string" },
197
+ },
198
+ additionalProperties: false,
199
+ },
200
+ allow: inPerson,
201
+ run: async (ctx, input) => {
202
+ await runsOrg(ctx, "change its tiers");
203
+ const { tiers, kinds, templates: kept } = await orgTiersOf(ctx.as());
204
+ const app = appTiersOf(ctx);
205
+ const known = (name) => !!tiers[name] || !!app[name] || kept.some((t) => t._id === name);
206
+ // A new one: named from its title, its chains from another.
207
+ const making = !input.tier || (!tiers[input.tier] && !app[input.tier]);
208
+ if (making && !input.title)
209
+ throw invalid("Give the new tier a name.");
210
+ const name = input.tier && !known(input.tier)
211
+ ? input.tier
212
+ : making
213
+ ? nameFor(input.title, known)
214
+ : input.tier;
215
+ const had = kept.find((t) => t._id === name);
216
+ const source = input.from ? tiers[input.from] : undefined;
217
+ if (input.from && !source)
218
+ throw invalid(`There's no tier "${input.from}".`);
219
+ const base = tiers[name] ?? source ?? Object.values(tiers)[0];
220
+ const chains = making ? { ...base?.agents } : { ...had?.agents };
221
+ for (const [kind, chain] of Object.entries(input.kinds ?? {})) {
222
+ if (!kinds[kind])
223
+ throw invalid(`There's no kind of work "${kind}".`);
224
+ if (chain === null) {
225
+ delete chains[kind];
226
+ continue;
227
+ }
228
+ const list = chain;
229
+ if (!list.length)
230
+ throw invalid("A kind of work needs at least one model.");
231
+ for (const model of list)
232
+ await ctx.call("models.check", { model, port: "llm" });
233
+ chains[kind] = await settled(ctx, list, entryOf(base, kind, kinds) ?? undefined);
234
+ }
235
+ const order = making
236
+ ? Math.max(0, ...Object.values(tiers).map((t) => t.order ?? 0)) + 1
237
+ : had?.order;
238
+ await templates(ctx).replaceOne({ _id: name }, {
239
+ _id: name,
240
+ ...(input.title ?? had?.title ? { title: input.title ?? had?.title } : {}),
241
+ ...(input.description ?? had?.description
242
+ ? { description: input.description ?? had?.description }
243
+ : {}),
244
+ ...(order !== undefined ? { order } : {}),
245
+ ...(Object.keys(chains).length ? { agents: chains } : {}),
246
+ ...(making || had?.made ? { made: true } : {}),
247
+ at: new Date().toISOString(),
248
+ }, { upsert: true });
249
+ return { tier: name };
250
+ },
251
+ });
252
+ export const removeAction = defineAction({
253
+ description: "Takes one of the app's tiers away from the org, or deletes one it made",
254
+ input: {
255
+ type: "object",
256
+ properties: { tier: { type: "string" } },
257
+ required: ["tier"],
258
+ additionalProperties: false,
259
+ },
260
+ allow: inPerson,
261
+ run: async (ctx, input) => {
262
+ await runsOrg(ctx, "change its tiers");
263
+ const { tiers } = await orgTiersOf(ctx.as());
264
+ if (!tiers[input.tier])
265
+ throw invalid(`There's no tier "${input.tier}".`);
266
+ if (Object.keys(tiers).length === 1)
267
+ throw invalid("Keep at least one tier.");
268
+ if (tiers[input.tier].made)
269
+ await templates(ctx).deleteOne({ _id: input.tier });
270
+ else {
271
+ await templates(ctx).updateOne({ _id: input.tier }, { $set: { off: true, at: new Date().toISOString() } }, { upsert: true });
272
+ }
273
+ return { removed: input.tier };
274
+ },
275
+ });
276
+ export const restoreAction = defineAction({
277
+ description: "Puts one of the app's tiers back as the app has it",
278
+ input: {
279
+ type: "object",
280
+ properties: { tier: { type: "string" } },
281
+ required: ["tier"],
282
+ additionalProperties: false,
283
+ },
284
+ allow: inPerson,
285
+ run: async (ctx, input) => {
286
+ await runsOrg(ctx, "change its tiers");
287
+ if (!appTiersOf(ctx)[input.tier]) {
288
+ throw invalid(`"${input.tier}" isn't one of the app's tiers.`);
289
+ }
290
+ // As the app has it, in the place the org gave it.
291
+ const order = (await templates(ctx).findOne({ _id: input.tier }))?.order;
292
+ if (order === undefined)
293
+ await templates(ctx).deleteOne({ _id: input.tier });
294
+ else {
295
+ await templates(ctx).replaceOne({ _id: input.tier }, {
296
+ _id: input.tier,
297
+ order,
298
+ at: new Date().toISOString(),
299
+ });
300
+ }
301
+ return { tier: input.tier };
302
+ },
303
+ });
304
+ export const kindAction = defineAction({
305
+ description: "Adds or renames a kind of work of the org's own",
306
+ input: {
307
+ type: "object",
308
+ properties: {
309
+ kind: { type: "string" },
310
+ title: { type: "string", minLength: 1, maxLength: 40 },
311
+ description: { type: "string", maxLength: 200 },
312
+ base: { type: "string" },
313
+ },
314
+ required: ["title", "base"],
315
+ additionalProperties: false,
316
+ },
317
+ allow: inPerson,
318
+ run: async (ctx, input) => {
319
+ await runsOrg(ctx, "change its kinds of work");
320
+ const app = appKindsOf(ctx);
321
+ if (!app[input.base])
322
+ throw invalid(`"${input.base}" isn't one of the app's kinds of work.`);
323
+ const { kinds } = await orgTiersOf(ctx.as());
324
+ const kind = input.kind && !app[input.kind]
325
+ ? input.kind
326
+ : nameFor(input.title, (name) => !!kinds[name]);
327
+ await ctx.as().db.collection("tierKind").replaceOne({ _id: kind }, {
328
+ _id: kind,
329
+ title: input.title,
330
+ ...(input.description ? { description: input.description } : {}),
331
+ base: input.base,
332
+ at: new Date().toISOString(),
333
+ }, { upsert: true });
334
+ return { kind };
335
+ },
336
+ });
@@ -0,0 +1 @@
1
+ export declare const modelAction: import("pairz").ActionDef;
@@ -0,0 +1,61 @@
1
+ /**
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.
11
+ *
12
+ * Where the app has plans (providers with `harness`), the chain goes with
13
+ * the plans its person has connected now, in the plans' order, and the
14
+ * models those that said so run: `{ model: chain, harnesses, served? }`
15
+ * (none for a turn that's nobody's), and the models plugin runs each model
16
+ * on them first (see shared/plans.ts). Asking which they have is one read
17
+ * per plugin that keeps plans, beside the rest.
18
+ */
19
+ import { defineAction, personOf } from "pairz";
20
+ import { connectedOf } from "pairz/plugins/models/contract";
21
+ import { harnessesOf, plansOf } from "../shared/plans.js";
22
+ import { levelsOf, orgTiersOf } from "../shared/shown.js";
23
+ import { entryOf, kindOf, tierIn } from "../shared/tiers.js";
24
+ export const modelAction = defineAction({
25
+ description: "The model chain an agent thinks with in a conversation",
26
+ record: false,
27
+ input: {
28
+ type: "object",
29
+ properties: {
30
+ agent: { type: "string" },
31
+ space: { type: "string" },
32
+ thread: { type: "string" },
33
+ },
34
+ required: ["agent"],
35
+ additionalProperties: false,
36
+ },
37
+ run: async (ctx, input) => {
38
+ if (!Object.keys(ctx.resources.tiers ?? {}).length)
39
+ return null;
40
+ const person = personOf(ctx.actor);
41
+ const system = ctx.as();
42
+ const plans = Object.keys(harnessesOf(ctx)).length > 0;
43
+ const [{ tiers, kinds }, levels, connected] = await Promise.all([
44
+ orgTiersOf(system),
45
+ levelsOf(system, {
46
+ ...(input.space ? { space: input.space } : {}),
47
+ ...(input.thread ? { thread: input.thread } : {}),
48
+ ...(person ? { person } : {}),
49
+ }),
50
+ plans && person ? connectedOf(ctx) : new Map(),
51
+ ]);
52
+ const inForce = tierIn(tiers, levels);
53
+ const kind = kindOf(ctx, input.agent, undefined, kinds);
54
+ const chain = entryOf(inForce ? tiers[inForce.tier] : undefined, kind, kinds);
55
+ if (chain === undefined)
56
+ return null;
57
+ if (!plans)
58
+ return chain;
59
+ return { model: chain, ...plansOf(ctx, connected) };
60
+ },
61
+ });
@@ -0,0 +1,54 @@
1
+ export type ModelChoice = {
2
+ _id: string;
3
+ space?: string;
4
+ thread?: string;
5
+ person?: string;
6
+ tier?: string;
7
+ cap?: string;
8
+ fixed?: boolean;
9
+ at: string;
10
+ };
11
+ export declare const modelChoice: Omit<import("pairz").CollectionDef, "schema" | "queries"> & {
12
+ queries?: Record<string, import("pairz").QueryDef<import("pairz").WithId<{
13
+ [x: string]: unknown;
14
+ fixed?: boolean | undefined;
15
+ space?: string | undefined;
16
+ thread?: string | undefined;
17
+ person?: string | undefined;
18
+ tier?: string | undefined;
19
+ cap?: string | undefined;
20
+ at: string;
21
+ }>>> | undefined;
22
+ } & {
23
+ schema: {
24
+ readonly type: "object";
25
+ readonly properties: {
26
+ /** The space or conversation it's for, or the person whose default it is; none: the org's. */
27
+ readonly space: {
28
+ readonly type: "string";
29
+ };
30
+ readonly thread: {
31
+ readonly type: "string";
32
+ };
33
+ readonly person: {
34
+ readonly type: "string";
35
+ };
36
+ /** A tier by its name; none: the one above. */
37
+ readonly tier: {
38
+ readonly type: "string";
39
+ };
40
+ /** The org's: the highest tier its members may choose; none: any. */
41
+ readonly cap: {
42
+ readonly type: "string";
43
+ };
44
+ /** The org's: its members use only its default; no space, conversation or person chooses. */
45
+ readonly fixed: {
46
+ readonly type: "boolean";
47
+ };
48
+ readonly at: {
49
+ readonly type: "string";
50
+ };
51
+ };
52
+ readonly required: readonly ["at"];
53
+ };
54
+ };
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Which tier applies where (`modelChoice`): the org's (`_id: "org"`, with
3
+ * what its members may use: `cap`, the highest tier they may choose, or
4
+ * `fixed`, only its default), a space's (`space:<id>`), a conversation's
5
+ * (`thread:<id>`) and a person's own default for new chats (`person:<id>`).
6
+ * Each a tier by its name, never a copy: changing a tier moves everyone on
7
+ * it. Changed only through `tiers.choose` and `tiers.cap`, which say who
8
+ * may; read by `_id`, so a turn's asking costs what the owner has cached. A
9
+ * person's choice saved when people ordered their plans may still hold
10
+ * `harnesses`: nothing reads it, and their next choice drops it.
11
+ *
12
+ * Queries, for front ends, as the person looking:
13
+ * - `offered` `{ space?, thread? }`: the org's tiers with each kind of
14
+ * work's chain and a typical message's price at this org's prices, the
15
+ * kinds and the app's agents of each, what each level chose, and what
16
+ * applies there. Anyone in the org reads it; a space's and a
17
+ * conversation's, those who reach them.
18
+ * - `agent` `{ agent, space?, thread? }`: one agent there: its kind, the tier
19
+ * in force and its chain in it.
20
+ * - `harnesses` `{}`: the app's plans that pay for the person looking (those
21
+ * a tier's model runs on, and those they've connected), in the plans'
22
+ * order, each with whether they have it and the first model, in tier
23
+ * order, it runs (what a test of it tries).
24
+ */
25
+ import { defineCollection, forbidden, people } from "pairz";
26
+ import { harnessesShown } from "../shared/plans.js";
27
+ import { agentAt, shownAt } from "../shared/shown.js";
28
+ const choiceSchema = {
29
+ type: "object",
30
+ properties: {
31
+ /** The space or conversation it's for, or the person whose default it is; none: the org's. */
32
+ space: { type: "string" },
33
+ thread: { type: "string" },
34
+ person: { type: "string" },
35
+ /** A tier by its name; none: the one above. */
36
+ tier: { type: "string" },
37
+ /** The org's: the highest tier its members may choose; none: any. */
38
+ cap: { type: "string" },
39
+ /** The org's: its members use only its default; no space, conversation or person chooses. */
40
+ fixed: { type: "boolean" },
41
+ at: { type: "string" },
42
+ },
43
+ required: ["at"],
44
+ };
45
+ /** Only someone who reaches the space or the conversation reads what it chose: the rules say. */
46
+ async function reached(ctx, where) {
47
+ if (where.space && !await ctx.db.collection("space").findOne({ _id: where.space })) {
48
+ throw forbidden("You can't read that space.");
49
+ }
50
+ if (where.thread && !await ctx.db.collection("thread").findOne({ _id: where.thread })) {
51
+ throw forbidden("You can't read that conversation.");
52
+ }
53
+ }
54
+ export const modelChoice = defineCollection({
55
+ schema: choiceSchema,
56
+ queries: {
57
+ offered: {
58
+ allow: people,
59
+ run: async (_, params, ctx) => {
60
+ const where = {
61
+ ...(params.space ? { space: params.space } : {}),
62
+ ...(params.thread ? { thread: params.thread } : {}),
63
+ };
64
+ await reached(ctx, where);
65
+ return await shownAt(ctx, where);
66
+ },
67
+ },
68
+ agent: {
69
+ allow: people,
70
+ run: async (_, params, ctx) => {
71
+ const where = {
72
+ ...(params.space ? { space: params.space } : {}),
73
+ ...(params.thread ? { thread: params.thread } : {}),
74
+ };
75
+ await reached(ctx, where);
76
+ return await agentAt(ctx, params.agent, where);
77
+ },
78
+ },
79
+ harnesses: { allow: people, run: (_, __, ctx) => harnessesShown(ctx) },
80
+ },
81
+ });
@@ -0,0 +1,76 @@
1
+ export declare const tierTemplate: Omit<import("pairz").CollectionDef, "schema" | "queries"> & {
2
+ queries?: Record<string, import("pairz").QueryDef<import("pairz").WithId<{
3
+ [x: string]: unknown;
4
+ title?: string | undefined;
5
+ description?: string | undefined;
6
+ off?: boolean | undefined;
7
+ agents?: {
8
+ [x: string]: unknown;
9
+ } | undefined;
10
+ order?: number | undefined;
11
+ made?: boolean | undefined;
12
+ at: string;
13
+ }>>> | undefined;
14
+ } & {
15
+ schema: {
16
+ readonly type: "object";
17
+ readonly properties: {
18
+ readonly title: {
19
+ readonly type: "string";
20
+ };
21
+ readonly description: {
22
+ readonly type: "string";
23
+ };
24
+ readonly order: {
25
+ readonly type: "number";
26
+ };
27
+ /** Kinds of work's chains, by kind. */
28
+ readonly agents: {
29
+ readonly type: "object";
30
+ };
31
+ readonly off: {
32
+ readonly type: "boolean";
33
+ };
34
+ readonly made: {
35
+ readonly type: "boolean";
36
+ };
37
+ readonly at: {
38
+ readonly type: "string";
39
+ };
40
+ };
41
+ readonly required: readonly ["at"];
42
+ };
43
+ };
44
+ /**
45
+ * Kinds of work an org added (`tierKind`, by name): what it's called, and
46
+ * the app's kind it starts from, whose chain it takes in any tier that
47
+ * doesn't name it. Through `tiers.kind`, by owners and admins.
48
+ */
49
+ export declare const tierKind: Omit<import("pairz").CollectionDef, "schema" | "queries"> & {
50
+ queries?: Record<string, import("pairz").QueryDef<import("pairz").WithId<{
51
+ [x: string]: unknown;
52
+ description?: string | undefined;
53
+ at: string;
54
+ title: string;
55
+ base: string;
56
+ }>>> | undefined;
57
+ } & {
58
+ schema: {
59
+ readonly type: "object";
60
+ readonly properties: {
61
+ readonly title: {
62
+ readonly type: "string";
63
+ };
64
+ readonly description: {
65
+ readonly type: "string";
66
+ };
67
+ readonly base: {
68
+ readonly type: "string";
69
+ };
70
+ readonly at: {
71
+ readonly type: "string";
72
+ };
73
+ };
74
+ readonly required: readonly ["title", "base", "at"];
75
+ };
76
+ };
@@ -0,0 +1,42 @@
1
+ /**
2
+ * The org's tiers, as it keeps them (`tierTemplate`, one per tier, by its
3
+ * name): its version of one of the app's (kinds' chains changed, a title, its
4
+ * place among them, or `off`: taken away), or one it made (`made`, whole).
5
+ * Changed only through `tiers.save`, `tiers.order`, `tiers.remove` and
6
+ * `tiers.restore`, by the org's owners and admins; everyone in the org reads
7
+ * them, through `modelChoice.offered`.
8
+ */
9
+ import { defineCollection } from "pairz";
10
+ export const tierTemplate = defineCollection({
11
+ schema: {
12
+ type: "object",
13
+ properties: {
14
+ title: { type: "string" },
15
+ description: { type: "string" },
16
+ order: { type: "number" },
17
+ /** Kinds of work's chains, by kind. */
18
+ agents: { type: "object" },
19
+ off: { type: "boolean" },
20
+ made: { type: "boolean" },
21
+ at: { type: "string" },
22
+ },
23
+ required: ["at"],
24
+ },
25
+ });
26
+ /**
27
+ * Kinds of work an org added (`tierKind`, by name): what it's called, and
28
+ * the app's kind it starts from, whose chain it takes in any tier that
29
+ * doesn't name it. Through `tiers.kind`, by owners and admins.
30
+ */
31
+ export const tierKind = defineCollection({
32
+ schema: {
33
+ type: "object",
34
+ properties: {
35
+ title: { type: "string" },
36
+ description: { type: "string" },
37
+ base: { type: "string" },
38
+ at: { type: "string" },
39
+ },
40
+ required: ["title", "base", "at"],
41
+ },
42
+ });
@@ -0,0 +1,23 @@
1
+ /**
2
+ * The tiers plugin's contract: the shapes of a tier (its `tiers` resource), a
3
+ * kind of work (`tierKinds`), what levels chose, and what its queries answer,
4
+ * and the resource kinds it reads (`planTier`, `planCredits`,
5
+ * `typicalMessage`, and the models plugin's `connectedPlans`). The rest is
6
+ * reached by name: its collections (`modelChoice`, `tierTemplate`,
7
+ * `tierKind`), its queries (`modelChoice.offered`, `modelChoice.agent`,
8
+ * `modelChoice.harnesses`) and its Actions (`tiers.model`, `tiers.choose`,
9
+ * `tiers.cap`, `tiers.save`, `tiers.order`, `tiers.remove`, `tiers.restore`,
10
+ * `tiers.kind`).
11
+ */
12
+ export type { By, Choice, Kind, Tier } from "./shared/tiers.js";
13
+ export type { HarnessShown } from "./shared/plans.js";
14
+ export type { ModelChoice } from "./collections/choice.js";
15
+ export type { AgentNow, CreditsShown, KindShown, ModelShown, PlanCredits, Shown, TierShown, TypicalMessage, } from "./shared/shown.js";
16
+ /**
17
+ * A `planTier` resource: the Action saying which tier the org's plan gives its
18
+ * agents while the org hasn't chosen (credits' `billing.tier`), `{}` in, a
19
+ * tier's name or null out.
20
+ */
21
+ export type PlanTier = {
22
+ action: string;
23
+ };
@@ -0,0 +1 @@
1
+ export {};