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