@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.
- package/LICENSE +21 -0
- package/README.md +51 -1
- package/WORDS.md +370 -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 +36 -0
- package/team/views/web/model-pick.js +44 -0
- package/team/views/web/model-picker.d.ts +83 -0
- package/team/views/web/model-picker.js +262 -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 +246 -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 +336 -0
- package/tiers/actions/model.d.ts +1 -0
- package/tiers/actions/model.js +61 -0
- package/tiers/collections/choice.d.ts +54 -0
- package/tiers/collections/choice.js +81 -0
- package/tiers/collections/template.d.ts +76 -0
- package/tiers/collections/template.js +42 -0
- package/tiers/contract.d.ts +23 -0
- package/tiers/contract.js +1 -0
- package/tiers/mod.d.ts +75 -0
- package/tiers/mod.js +114 -0
- package/tiers/shared/plans.d.ts +61 -0
- package/tiers/shared/plans.js +74 -0
- package/tiers/shared/shown.d.ts +189 -0
- package/tiers/shared/shown.js +284 -0
- package/tiers/shared/tiers.d.ts +157 -0
- package/tiers/shared/tiers.js +179 -0
- package/tiers/views/web/composer.d.ts +15 -0
- package/tiers/views/web/composer.js +87 -0
- package/tiers/views/web/org.d.ts +69 -0
- package/tiers/views/web/org.js +334 -0
- package/tiers/views/web/say.d.ts +146 -0
- package/tiers/views/web/say.js +150 -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 +32 -0
- package/tiers/views/web/space.js +78 -0
- package/tiers/views/web/style.d.ts +7 -0
- package/tiers/views/web/style.js +193 -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 +31 -0
- package/tiers/web.js +72 -0
- package/web.d.ts +14 -0
- package/web.js +14 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { agentsOf } from "pairz/plugins/agents/contract";
|
|
2
|
+
import { personOf } from "pairz";
|
|
3
|
+
import { plansFor } from "./plans.js";
|
|
4
|
+
import { entryOf, kindOf, kindsIn, ORG_CHOICE, personChoice, primaryOf, spaceChoice, threadChoice, tierIn, tiersIn, } from "./tiers.js";
|
|
5
|
+
/** A typical reply's tokens, where the app measured no message (see above). */
|
|
6
|
+
export const REPLY = { input: 20_000, cached: 16_000, output: 1_000 };
|
|
7
|
+
/** The org's tiers and kinds, as it keeps them. */
|
|
8
|
+
export async function orgTiersOf(ctx) {
|
|
9
|
+
const [templates, kinds] = await Promise.all([
|
|
10
|
+
ctx.db.collection("tierTemplate").find({}),
|
|
11
|
+
ctx.db.collection("tierKind").find({}),
|
|
12
|
+
]);
|
|
13
|
+
return { tiers: tiersIn(ctx, templates), kinds: kindsIn(ctx, kinds), templates };
|
|
14
|
+
}
|
|
15
|
+
const choices = (ctx) => ctx.db.collection("modelChoice");
|
|
16
|
+
/** The org's plan's tier, through the `planTier` resource when something declares it. */
|
|
17
|
+
async function planTierOf(ctx) {
|
|
18
|
+
const source = ctx.resources.planTier;
|
|
19
|
+
return source?.action ? await ctx.call(source.action, {}) : null;
|
|
20
|
+
}
|
|
21
|
+
/** What each level chose, where asked: the conversation, the person, the space, the org, the plan. */
|
|
22
|
+
export async function levelsOf(ctx, where) {
|
|
23
|
+
const one = (id) => id ? choices(ctx).findOne({ _id: id }) : Promise.resolve(null);
|
|
24
|
+
const [thread, person, space, org, plan] = await Promise.all([
|
|
25
|
+
one(where.thread && threadChoice(where.thread)),
|
|
26
|
+
one(where.person && personChoice(where.person)),
|
|
27
|
+
one(where.space && spaceChoice(where.space)),
|
|
28
|
+
one(ORG_CHOICE),
|
|
29
|
+
planTierOf(ctx),
|
|
30
|
+
]);
|
|
31
|
+
return { thread, person, space, org, plan };
|
|
32
|
+
}
|
|
33
|
+
/** The app's measured message, when it has one. */
|
|
34
|
+
const typicalOf = (ctx) => {
|
|
35
|
+
const typical = ctx.resources.typicalMessage;
|
|
36
|
+
return typical && Object.keys(typical).length ? typical : undefined;
|
|
37
|
+
};
|
|
38
|
+
/** What the org's plan gives in credits, through the `planCredits` resource when something declares it. */
|
|
39
|
+
async function creditsOf(ctx) {
|
|
40
|
+
const source = ctx.resources.planCredits;
|
|
41
|
+
if (!source?.action)
|
|
42
|
+
return null;
|
|
43
|
+
return await ctx.call(source.action, {}).catch(() => null);
|
|
44
|
+
}
|
|
45
|
+
/** Each model a ref tries, in order, each as `primaryOf` reads one. */
|
|
46
|
+
const entriesOf = (ctx, ref) => (Array.isArray(ref) ? ref : [ref]).flatMap((entry) => {
|
|
47
|
+
const found = primaryOf(ctx, entry);
|
|
48
|
+
return found ? [found] : [];
|
|
49
|
+
});
|
|
50
|
+
/**
|
|
51
|
+
* For each use, the plan of the person's (`theirs`) its chain would run on
|
|
52
|
+
* first, as their turns try it (`models.check`), when it's a plan; each
|
|
53
|
+
* chain asked once.
|
|
54
|
+
*/
|
|
55
|
+
async function planFirsts(ctx, uses, theirs) {
|
|
56
|
+
const asked = new Map();
|
|
57
|
+
const firstOf = (ref) => {
|
|
58
|
+
const key = JSON.stringify(ref);
|
|
59
|
+
let found = asked.get(key);
|
|
60
|
+
if (!found) {
|
|
61
|
+
found = ctx.call("models.check", { model: { model: ref, ...theirs }, port: "llm" })
|
|
62
|
+
.then((tried) => tried[0], () => undefined);
|
|
63
|
+
asked.set(key, found);
|
|
64
|
+
}
|
|
65
|
+
return found;
|
|
66
|
+
};
|
|
67
|
+
const firsts = await Promise.all(uses.map((use) => firstOf(use.ref)));
|
|
68
|
+
return firsts.map((id) => id && theirs.harnesses.includes(id.slice(0, id.indexOf("/"))) ? id : undefined);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Names and prices for models, by `"provider/model"` and effort, each asked
|
|
72
|
+
* once: a typical message's part for the agent (its calls, each call's
|
|
73
|
+
* tokens), else a plain reply. A model is named by the first it tries, and
|
|
74
|
+
* priced at the first the app charges something for: one on the org's own key
|
|
75
|
+
* or free here (a person's ChatGPT plan) is passed over for the next, as a
|
|
76
|
+
* turn is when the person hasn't connected that plan; a list all on the org's
|
|
77
|
+
* own keys isn't priced.
|
|
78
|
+
*/
|
|
79
|
+
async function modelsShown(ctx, uses, theirs = { harnesses: [] }) {
|
|
80
|
+
const typical = typicalOf(ctx);
|
|
81
|
+
const lists = uses.map((use) => entriesOf(ctx, use.ref));
|
|
82
|
+
const firsts = theirs.harnesses.length ? planFirsts(ctx, uses, theirs) : undefined;
|
|
83
|
+
const ids = [...new Set(lists.flat().map((entry) => entry.id))];
|
|
84
|
+
const offered = await Promise.all(ids.map((model) => ctx.call("models.offered", { model })
|
|
85
|
+
.then((answer) => answer.model, () => null)));
|
|
86
|
+
const known = new Map(ids.map((id, i) => [id, offered[i]]));
|
|
87
|
+
const declared = (ctx.resources.provider ?? {});
|
|
88
|
+
// What one call is charged: each agent's (a plain reply's, without a measured message).
|
|
89
|
+
const callOf = (agent) => {
|
|
90
|
+
const measured = typical?.[agent];
|
|
91
|
+
if (!typical)
|
|
92
|
+
return REPLY;
|
|
93
|
+
if (!measured)
|
|
94
|
+
return undefined;
|
|
95
|
+
const { calls: _, ...tokens } = measured;
|
|
96
|
+
return tokens;
|
|
97
|
+
};
|
|
98
|
+
const keyOf = (id, agent) => typical ? `${id} ${agent}` : id;
|
|
99
|
+
// Every model each tries that's on the app's keys, when there's a ledger, each call asked once.
|
|
100
|
+
const charged = lists.map((list) => ctx.resources.usage ? list.filter((entry) => !known.get(entry.id)?.ownKey) : []);
|
|
101
|
+
const asked = new Map();
|
|
102
|
+
charged.forEach((list, i) => {
|
|
103
|
+
if (!callOf(uses[i].agent))
|
|
104
|
+
return;
|
|
105
|
+
for (const entry of list) {
|
|
106
|
+
asked.set(keyOf(entry.id, uses[i].agent), { id: entry.id, agent: uses[i].agent });
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
const quotes = [...asked.values()];
|
|
110
|
+
const quoted = quotes.length
|
|
111
|
+
? await ctx.call("usage.quotes", {
|
|
112
|
+
uses: quotes.map(({ id, agent }) => {
|
|
113
|
+
const slash = id.indexOf("/");
|
|
114
|
+
const from = declared[id.slice(0, slash)]?.catalog;
|
|
115
|
+
return {
|
|
116
|
+
meter: "llm",
|
|
117
|
+
subject: id,
|
|
118
|
+
catalog: `${typeof from === "string" ? from : id.slice(0, slash)}${id.slice(slash)}`,
|
|
119
|
+
quantities: callOf(agent),
|
|
120
|
+
};
|
|
121
|
+
}),
|
|
122
|
+
}).catch(() => quotes.map(() => null))
|
|
123
|
+
: [];
|
|
124
|
+
const prices = new Map([...asked.keys()].map((key, i) => [key, quoted[i] ?? null]));
|
|
125
|
+
const plans = await firsts;
|
|
126
|
+
return lists.map((list, i) => {
|
|
127
|
+
const p = list[0];
|
|
128
|
+
if (!p)
|
|
129
|
+
return undefined;
|
|
130
|
+
const info = known.get(p.id);
|
|
131
|
+
const agent = uses[i].agent;
|
|
132
|
+
// The first that costs something: one that's free here is a plan the person brings.
|
|
133
|
+
const each = charged[i].map((entry) => prices.get(keyOf(entry.id, agent)) ?? null);
|
|
134
|
+
const call = each.find((price) => price !== null && price > 0) ??
|
|
135
|
+
each.find((price) => price !== null) ?? null;
|
|
136
|
+
const calls = typical ? typical[agent]?.calls ?? 0 : 1;
|
|
137
|
+
const named = (id) => known.get(id)?.name ?? id.slice(id.indexOf("/") + 1);
|
|
138
|
+
return {
|
|
139
|
+
id: p.id,
|
|
140
|
+
name: named(p.id),
|
|
141
|
+
...(p.effort ? { effort: p.effort } : {}),
|
|
142
|
+
price: call === null ? null : call * calls,
|
|
143
|
+
...(info?.ownKey && !charged[i].length ? { ownKey: true } : {}),
|
|
144
|
+
...(plans?.[i] ? { plan: plans[i] } : {}),
|
|
145
|
+
...(list.length > 1
|
|
146
|
+
? {
|
|
147
|
+
fallbacks: list.slice(1).map((entry) => ({
|
|
148
|
+
id: entry.id,
|
|
149
|
+
name: named(entry.id),
|
|
150
|
+
...(entry.effort ? { effort: entry.effort } : {}),
|
|
151
|
+
})),
|
|
152
|
+
}
|
|
153
|
+
: {}),
|
|
154
|
+
};
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
/** What's priced, summed (a message) or averaged (a reply); null when nothing is. */
|
|
158
|
+
const total = (prices, per) => {
|
|
159
|
+
const priced = prices.filter((p) => p !== null);
|
|
160
|
+
if (!priced.length)
|
|
161
|
+
return null;
|
|
162
|
+
const sum = priced.reduce((a, b) => a + b, 0);
|
|
163
|
+
return per === "message" ? sum : sum / priced.length;
|
|
164
|
+
};
|
|
165
|
+
/** Everything a page shows, at the org's level, or where asked (a space, a conversation). */
|
|
166
|
+
export async function shownAt(ctx, where = {}) {
|
|
167
|
+
const person = personOf(ctx.actor);
|
|
168
|
+
const [{ tiers, kinds, templates }, levels, credits] = await Promise.all([
|
|
169
|
+
orgTiersOf(ctx),
|
|
170
|
+
levelsOf(ctx, { ...where, ...(person ? { person } : {}) }),
|
|
171
|
+
creditsOf(ctx),
|
|
172
|
+
]);
|
|
173
|
+
const per = typicalOf(ctx) ? "message" : "reply";
|
|
174
|
+
const agents = agentsOf(ctx);
|
|
175
|
+
const kindNames = Object.keys(kinds);
|
|
176
|
+
const list = Object.entries(tiers);
|
|
177
|
+
// Every tier's chain for every kind, asked for at once, and taken back in the same order.
|
|
178
|
+
const uses = list.flatMap(([, tier]) => kindNames.flatMap((kind) => {
|
|
179
|
+
const ref = entryOf(tier, kind, kinds);
|
|
180
|
+
return ref === undefined ? [] : [{ ref, agent: kinds[kind].base ?? kind }];
|
|
181
|
+
}));
|
|
182
|
+
// The person's plans they can run now: what their turns go with.
|
|
183
|
+
const theirs = person ? await plansFor(ctx) : { harnesses: [] };
|
|
184
|
+
const shown = await modelsShown(ctx, uses, theirs);
|
|
185
|
+
let cursor = 0;
|
|
186
|
+
const tiersShown = list.map(([name, tier]) => {
|
|
187
|
+
const own = kindNames.flatMap((kind) => entryOf(tier, kind, kinds) === undefined ? [] : [{ kind, model: shown[cursor++] }]).filter((k) => !!k.model);
|
|
188
|
+
// Only the app's kinds count toward a typical message: an org's own is extra.
|
|
189
|
+
const counted = own.filter((k) => !kinds[k.kind].base);
|
|
190
|
+
return {
|
|
191
|
+
name,
|
|
192
|
+
title: tier.title,
|
|
193
|
+
...(tier.description ? { description: tier.description } : {}),
|
|
194
|
+
price: total(counted.map((k) => k.model.price), per),
|
|
195
|
+
// On a plan of theirs, a kind costs them nothing.
|
|
196
|
+
...(counted.some((k) => k.model.plan)
|
|
197
|
+
? { yours: total(counted.map((k) => k.model.plan ? 0 : k.model.price), per) }
|
|
198
|
+
: {}),
|
|
199
|
+
...(tier.app ? { app: true } : {}),
|
|
200
|
+
...(tier.changed ? { changed: true } : {}),
|
|
201
|
+
...(tier.made ? { made: true } : {}),
|
|
202
|
+
kinds: own,
|
|
203
|
+
};
|
|
204
|
+
});
|
|
205
|
+
const offered = (name) => name && tiers[name] ? name : null;
|
|
206
|
+
const app = (ctx.resources.tiers ?? {});
|
|
207
|
+
const inForce = tierIn(tiers, levels);
|
|
208
|
+
return {
|
|
209
|
+
tiers: tiersShown,
|
|
210
|
+
kinds: kindNames.map((kind) => ({
|
|
211
|
+
kind,
|
|
212
|
+
title: kinds[kind].title,
|
|
213
|
+
...(kinds[kind].description ? { description: kinds[kind].description } : {}),
|
|
214
|
+
...(kinds[kind].base ? { base: kinds[kind].base } : {}),
|
|
215
|
+
agents: Object.entries(agents)
|
|
216
|
+
.filter(([name, def]) => kindOf(ctx, name, def.like, kinds) === kind && agents[name].default)
|
|
217
|
+
.map(([name, def]) => ({
|
|
218
|
+
agent: name,
|
|
219
|
+
name: def.name ?? name[0].toUpperCase() + name.slice(1),
|
|
220
|
+
...(def.icon ? { icon: def.icon } : {}),
|
|
221
|
+
})),
|
|
222
|
+
})),
|
|
223
|
+
removed: templates.filter((t) => t.off && app[t._id])
|
|
224
|
+
.map((t) => ({ name: t._id, title: app[t._id].title })),
|
|
225
|
+
plan: offered(levels.plan),
|
|
226
|
+
org: {
|
|
227
|
+
tier: offered(levels.org?.tier),
|
|
228
|
+
cap: offered(levels.org?.cap),
|
|
229
|
+
fixed: levels.org?.fixed === true,
|
|
230
|
+
},
|
|
231
|
+
...(where.space ? { space: { tier: offered(levels.space?.tier) } } : {}),
|
|
232
|
+
...(where.thread ? { thread: { tier: offered(levels.thread?.tier) } } : {}),
|
|
233
|
+
person: { tier: offered(levels.person?.tier) },
|
|
234
|
+
now: inForce ?? null,
|
|
235
|
+
per,
|
|
236
|
+
reply: REPLY,
|
|
237
|
+
credits,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
/** Which of the app's kinds one people made works as, as chat's `agentSources` say. */
|
|
241
|
+
async function likeOf(ctx, agent) {
|
|
242
|
+
const sources = Object.values((ctx.resources.agentSources ?? {}));
|
|
243
|
+
for (const source of sources) {
|
|
244
|
+
const told = await ctx.call(source.action, {
|
|
245
|
+
names: [agent],
|
|
246
|
+
})
|
|
247
|
+
.catch(() => ({}));
|
|
248
|
+
if (told[agent])
|
|
249
|
+
return told[agent].like;
|
|
250
|
+
}
|
|
251
|
+
return undefined;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* One agent where it works (a space, maybe a conversation): its kind of work,
|
|
255
|
+
* the tier in force there and who chose it, and its chain in that tier; null
|
|
256
|
+
* when no tier applies.
|
|
257
|
+
*/
|
|
258
|
+
export async function agentAt(ctx, agent, where) {
|
|
259
|
+
const person = personOf(ctx.actor);
|
|
260
|
+
const [{ tiers, kinds }, levels] = await Promise.all([
|
|
261
|
+
orgTiersOf(ctx),
|
|
262
|
+
levelsOf(ctx, { ...where, ...(person ? { person } : {}) }),
|
|
263
|
+
]);
|
|
264
|
+
const declared = agentsOf(ctx)[agent];
|
|
265
|
+
const like = declared ? declared.like : await likeOf(ctx, agent);
|
|
266
|
+
const kind = kindOf(ctx, agent, like, kinds);
|
|
267
|
+
const inForce = tierIn(tiers, levels);
|
|
268
|
+
if (!kind || !inForce)
|
|
269
|
+
return null;
|
|
270
|
+
const ref = entryOf(tiers[inForce.tier], kind, kinds);
|
|
271
|
+
if (ref === undefined)
|
|
272
|
+
return null;
|
|
273
|
+
const [model] = await modelsShown(ctx, [{ ref, agent: kinds[kind].base ?? kind }]);
|
|
274
|
+
if (!model)
|
|
275
|
+
return null;
|
|
276
|
+
return {
|
|
277
|
+
kind,
|
|
278
|
+
kindTitle: kinds[kind].title,
|
|
279
|
+
tier: inForce.tier,
|
|
280
|
+
tierTitle: tiers[inForce.tier].title,
|
|
281
|
+
by: inForce.by,
|
|
282
|
+
model,
|
|
283
|
+
};
|
|
284
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
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`); only the org's and the plan's when the org uses
|
|
20
|
+
* its default alone (`fixed`).
|
|
21
|
+
* - **In order:** the app's tiers by their `order`, an org's own order over
|
|
22
|
+
* it (`tiers.order`); a cap is "up to" a tier in that order.
|
|
23
|
+
*/
|
|
24
|
+
import type { Resources } from "pairz";
|
|
25
|
+
import type { ModelRef } from "pairz/plugins/models/contract";
|
|
26
|
+
/** A tier: a model chain for each kind of work, named by what it's good for. */
|
|
27
|
+
export type Tier = {
|
|
28
|
+
title: string;
|
|
29
|
+
/** What it's good for, in a line: "Quick answers, routine work, volume". */
|
|
30
|
+
description?: string;
|
|
31
|
+
/** Among the tiers, lowest first. */
|
|
32
|
+
order?: number;
|
|
33
|
+
/** Each kind of work's chain, by kind, as an agent's `model` takes it. */
|
|
34
|
+
agents: Record<string, ModelRef>;
|
|
35
|
+
};
|
|
36
|
+
/** A tier as the org has it: the app's (as it is, or `changed`), or one the org `made`. */
|
|
37
|
+
export type OrgTier = Tier & {
|
|
38
|
+
app?: true;
|
|
39
|
+
changed?: true;
|
|
40
|
+
made?: true;
|
|
41
|
+
};
|
|
42
|
+
/** What an org keeps of a tier: its version of the app's (or that it took it away), or one it made. */
|
|
43
|
+
export type Template = {
|
|
44
|
+
_id: string;
|
|
45
|
+
title?: string;
|
|
46
|
+
description?: string;
|
|
47
|
+
order?: number;
|
|
48
|
+
agents?: Record<string, ModelRef>;
|
|
49
|
+
/** One of the app's the org took away. */
|
|
50
|
+
off?: boolean;
|
|
51
|
+
/** One the org made. */
|
|
52
|
+
made?: boolean;
|
|
53
|
+
at: string;
|
|
54
|
+
};
|
|
55
|
+
/** A kind of work: what it's called, and for one an org added, the app's kind it starts from. */
|
|
56
|
+
export type Kind = {
|
|
57
|
+
title: string;
|
|
58
|
+
description?: string;
|
|
59
|
+
base?: string;
|
|
60
|
+
};
|
|
61
|
+
/** One an org added (`tierKind`), by its name. */
|
|
62
|
+
export type OrgKind = {
|
|
63
|
+
_id: string;
|
|
64
|
+
title: string;
|
|
65
|
+
description?: string;
|
|
66
|
+
base: string;
|
|
67
|
+
at: string;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* What a level chose: a tier by its name; the org's also what its members
|
|
71
|
+
* may use: tiers up to one (`cap`), or only its default (`fixed`).
|
|
72
|
+
*/
|
|
73
|
+
export type Choice = {
|
|
74
|
+
tier?: string;
|
|
75
|
+
cap?: string;
|
|
76
|
+
fixed?: boolean;
|
|
77
|
+
};
|
|
78
|
+
/** Where a choice is kept. */
|
|
79
|
+
export declare const ORG_CHOICE = "org";
|
|
80
|
+
export declare const spaceChoice: (space: string) => string;
|
|
81
|
+
export declare const threadChoice: (thread: string) => string;
|
|
82
|
+
export declare const personChoice: (person: string) => string;
|
|
83
|
+
/** Who chose the tier in force. */
|
|
84
|
+
export type By = "thread" | "person" | "space" | "org" | "plan";
|
|
85
|
+
/** What applies, at each level: each a choice, or none. */
|
|
86
|
+
export type Levels = {
|
|
87
|
+
thread?: Choice | null;
|
|
88
|
+
person?: Choice | null;
|
|
89
|
+
space?: Choice | null;
|
|
90
|
+
org?: Choice | null;
|
|
91
|
+
plan?: string | null;
|
|
92
|
+
};
|
|
93
|
+
/** The app's tiers, as it declares them. */
|
|
94
|
+
export declare const appTiersOf: (ctx: {
|
|
95
|
+
resources: Resources;
|
|
96
|
+
}) => Record<string, Tier>;
|
|
97
|
+
/** The app's kinds of work: its tiers' keys, in the order its agents are declared, titled. */
|
|
98
|
+
export declare function appKindsOf(ctx: {
|
|
99
|
+
resources: Resources;
|
|
100
|
+
}): Record<string, Kind>;
|
|
101
|
+
/** Every kind of work: the app's, then those the org added (on an app's kind still there). */
|
|
102
|
+
export declare function kindsIn(ctx: {
|
|
103
|
+
resources: Resources;
|
|
104
|
+
}, own: OrgKind[]): Record<string, Kind>;
|
|
105
|
+
/** The kind an agent works as: what it says (`like`); its own, where tiers name it; else the app's first. */
|
|
106
|
+
export declare function kindOf(ctx: {
|
|
107
|
+
resources: Resources;
|
|
108
|
+
}, name: string, like: string | undefined, kinds: Record<string, Kind>): string | undefined;
|
|
109
|
+
/**
|
|
110
|
+
* The org's tiers: the app's as the org keeps them (its version over them,
|
|
111
|
+
* those it took away left out), then those it made; each by its name, in
|
|
112
|
+
* order.
|
|
113
|
+
*/
|
|
114
|
+
export declare function tiersIn(ctx: {
|
|
115
|
+
resources: Resources;
|
|
116
|
+
}, templates: Template[]): Record<string, OrgTier>;
|
|
117
|
+
/** A kind's chain in a tier: its own entry there, else its base kind's. */
|
|
118
|
+
export declare function entryOf(tier: Tier | undefined, kind: string | undefined, kinds: Record<string, Kind>): ModelRef | undefined;
|
|
119
|
+
/**
|
|
120
|
+
* The tier in force, and who chose it: the conversation, the person, the
|
|
121
|
+
* space, the org, the plan; only the org and the plan where the org uses its
|
|
122
|
+
* default alone (`fixed`).
|
|
123
|
+
*/
|
|
124
|
+
export declare function tierIn(tiers: Record<string, unknown>, levels: Levels): {
|
|
125
|
+
tier: string;
|
|
126
|
+
by: By;
|
|
127
|
+
} | undefined;
|
|
128
|
+
/** Whether a tier is above the highest the org lets its members use (`cap`). */
|
|
129
|
+
export declare function aboveCap(tiers: Record<string, unknown>, tier: string, cap?: string): boolean;
|
|
130
|
+
/** A model, as a list: what's tried in order. */
|
|
131
|
+
export declare const listOf: (ref: ModelRef) => ModelRef[];
|
|
132
|
+
/** The model a ref tries first, as `"provider/model"` when it says so plainly. */
|
|
133
|
+
export declare const firstId: (ref: ModelRef) => string | undefined;
|
|
134
|
+
/** What a model's first choice is set to think and write with. */
|
|
135
|
+
export type Settings = {
|
|
136
|
+
effort?: string;
|
|
137
|
+
maxTokens?: number;
|
|
138
|
+
};
|
|
139
|
+
/**
|
|
140
|
+
* The model a ref tries first, with the effort and most tokens it's asked
|
|
141
|
+
* for, following aliases (`resources.model`): what a page shows of it.
|
|
142
|
+
*/
|
|
143
|
+
export declare function primaryOf(ctx: {
|
|
144
|
+
resources: Resources;
|
|
145
|
+
}, ref: ModelRef, outer?: Settings, seen?: string[]): ({
|
|
146
|
+
id: string;
|
|
147
|
+
} & Settings) | undefined;
|
|
148
|
+
/**
|
|
149
|
+
* A chain someone set on a page (each entry a model), with what the chain it
|
|
150
|
+
* replaces sets on its first entry: its `maxTokens`, and its `effort` where
|
|
151
|
+
* each model takes it and none was said (the catalog's, `models.efforts`).
|
|
152
|
+
* So a chain set by hand isn't cut short by an adapter's default.
|
|
153
|
+
*/
|
|
154
|
+
export declare function settled(ctx: {
|
|
155
|
+
resources: Resources;
|
|
156
|
+
call<R>(action: string, input?: unknown): Promise<R>;
|
|
157
|
+
}, chain: ModelRef[], like: ModelRef | undefined): Promise<ModelRef[]>;
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { agentsOf, defaults } from "pairz/plugins/agents/contract";
|
|
2
|
+
/** Where a choice is kept. */
|
|
3
|
+
export const ORG_CHOICE = "org";
|
|
4
|
+
export const spaceChoice = (space) => `space:${space}`;
|
|
5
|
+
export const threadChoice = (thread) => `thread:${thread}`;
|
|
6
|
+
export const personChoice = (person) => `person:${person}`;
|
|
7
|
+
/** The app's tiers, as it declares them. */
|
|
8
|
+
export const appTiersOf = (ctx) => (ctx.resources.tiers ?? {});
|
|
9
|
+
/** The app's kinds of work: its tiers' keys, in the order its agents are declared, titled. */
|
|
10
|
+
export function appKindsOf(ctx) {
|
|
11
|
+
const named = (ctx.resources.tierKinds ?? {});
|
|
12
|
+
const agents = agentsOf(ctx);
|
|
13
|
+
const keys = new Set(Object.values(appTiersOf(ctx)).flatMap((t) => Object.keys(t.agents ?? {})));
|
|
14
|
+
const order = [
|
|
15
|
+
...Object.keys(agents).filter((a) => keys.has(a)),
|
|
16
|
+
...[...keys].filter((k) => !agents[k]),
|
|
17
|
+
];
|
|
18
|
+
return Object.fromEntries(order.map((key) => [key, {
|
|
19
|
+
title: named[key]?.title ?? agents[key]?.name ?? key[0].toUpperCase() + key.slice(1),
|
|
20
|
+
...(named[key]?.description ? { description: named[key].description } : {}),
|
|
21
|
+
}]));
|
|
22
|
+
}
|
|
23
|
+
/** Every kind of work: the app's, then those the org added (on an app's kind still there). */
|
|
24
|
+
export function kindsIn(ctx, own) {
|
|
25
|
+
const app = appKindsOf(ctx);
|
|
26
|
+
const added = own.filter((k) => !app[k._id] && app[k.base])
|
|
27
|
+
.sort((a, b) => a.at.localeCompare(b.at) || a._id.localeCompare(b._id));
|
|
28
|
+
return {
|
|
29
|
+
...app,
|
|
30
|
+
...Object.fromEntries(added.map((k) => [k._id, {
|
|
31
|
+
title: k.title,
|
|
32
|
+
...(k.description ? { description: k.description } : {}),
|
|
33
|
+
base: k.base,
|
|
34
|
+
}])),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** The kind an agent works as: what it says (`like`); its own, where tiers name it; else the app's first. */
|
|
38
|
+
export function kindOf(ctx, name, like, kinds) {
|
|
39
|
+
if (like && kinds[like])
|
|
40
|
+
return like;
|
|
41
|
+
if (kinds[name])
|
|
42
|
+
return name;
|
|
43
|
+
const agents = agentsOf(ctx);
|
|
44
|
+
return defaults(agents).find((a) => kinds[a]) ?? Object.keys(kinds)[0];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The org's tiers: the app's as the org keeps them (its version over them,
|
|
48
|
+
* those it took away left out), then those it made; each by its name, in
|
|
49
|
+
* order.
|
|
50
|
+
*/
|
|
51
|
+
export function tiersIn(ctx, templates) {
|
|
52
|
+
const app = appTiersOf(ctx);
|
|
53
|
+
const kept = new Map(templates.map((t) => [t._id, t]));
|
|
54
|
+
const out = [];
|
|
55
|
+
for (const [name, tier] of Object.entries(app)) {
|
|
56
|
+
const mine = kept.get(name);
|
|
57
|
+
if (mine?.off)
|
|
58
|
+
continue;
|
|
59
|
+
const changed = !!mine && (Object.keys(mine.agents ?? {}).length > 0 ||
|
|
60
|
+
mine.title !== undefined || mine.description !== undefined);
|
|
61
|
+
out.push([name, {
|
|
62
|
+
...tier,
|
|
63
|
+
// The org's place for it, over the app's.
|
|
64
|
+
...(mine?.order !== undefined ? { order: mine.order } : {}),
|
|
65
|
+
...(mine?.title ? { title: mine.title } : {}),
|
|
66
|
+
...(mine?.description !== undefined ? { description: mine.description } : {}),
|
|
67
|
+
agents: { ...tier.agents, ...mine?.agents },
|
|
68
|
+
app: true,
|
|
69
|
+
...(changed ? { changed: true } : {}),
|
|
70
|
+
}]);
|
|
71
|
+
}
|
|
72
|
+
for (const mine of templates) {
|
|
73
|
+
if (!mine.made || app[mine._id] || mine.off)
|
|
74
|
+
continue;
|
|
75
|
+
out.push([mine._id, {
|
|
76
|
+
title: mine.title ?? mine._id,
|
|
77
|
+
...(mine.description ? { description: mine.description } : {}),
|
|
78
|
+
order: mine.order ?? 1000,
|
|
79
|
+
agents: { ...mine.agents },
|
|
80
|
+
made: true,
|
|
81
|
+
}]);
|
|
82
|
+
}
|
|
83
|
+
return Object.fromEntries(out.sort(([a, x], [b, y]) => (x.order ?? 0) - (y.order ?? 0) || a.localeCompare(b)));
|
|
84
|
+
}
|
|
85
|
+
/** A kind's chain in a tier: its own entry there, else its base kind's. */
|
|
86
|
+
export function entryOf(tier, kind, kinds) {
|
|
87
|
+
if (!tier || !kind)
|
|
88
|
+
return undefined;
|
|
89
|
+
if (tier.agents[kind] !== undefined)
|
|
90
|
+
return tier.agents[kind];
|
|
91
|
+
const base = kinds[kind]?.base;
|
|
92
|
+
return base && tier.agents[base] !== undefined ? tier.agents[base] : undefined;
|
|
93
|
+
}
|
|
94
|
+
/** The levels that choose a tier, first first, above the plan's. */
|
|
95
|
+
const LEVELS = ["thread", "person", "space", "org"];
|
|
96
|
+
/** A tier that's still offered: one that's gone counts as none chosen. */
|
|
97
|
+
const offered = (tiers, name) => name && Object.hasOwn(tiers, name) ? name : undefined;
|
|
98
|
+
/**
|
|
99
|
+
* The tier in force, and who chose it: the conversation, the person, the
|
|
100
|
+
* space, the org, the plan; only the org and the plan where the org uses its
|
|
101
|
+
* default alone (`fixed`).
|
|
102
|
+
*/
|
|
103
|
+
export function tierIn(tiers, levels) {
|
|
104
|
+
const levelsNow = levels.org?.fixed ? ["org"] : LEVELS;
|
|
105
|
+
for (const by of levelsNow) {
|
|
106
|
+
const tier = offered(tiers, levels[by]?.tier);
|
|
107
|
+
if (tier)
|
|
108
|
+
return { tier, by };
|
|
109
|
+
}
|
|
110
|
+
const plan = offered(tiers, levels.plan) ?? Object.keys(tiers)[0];
|
|
111
|
+
return plan ? { tier: plan, by: "plan" } : undefined;
|
|
112
|
+
}
|
|
113
|
+
/** Whether a tier is above the highest the org lets its members use (`cap`). */
|
|
114
|
+
export function aboveCap(tiers, tier, cap) {
|
|
115
|
+
if (!cap || !Object.hasOwn(tiers, cap) || !Object.hasOwn(tiers, tier))
|
|
116
|
+
return false;
|
|
117
|
+
const names = Object.keys(tiers);
|
|
118
|
+
return names.indexOf(tier) > names.indexOf(cap);
|
|
119
|
+
}
|
|
120
|
+
/** A model, as a list: what's tried in order. */
|
|
121
|
+
export const listOf = (ref) => Array.isArray(ref) ? ref : [ref];
|
|
122
|
+
/** The model a ref tries first, as `"provider/model"` when it says so plainly. */
|
|
123
|
+
export const firstId = (ref) => {
|
|
124
|
+
const first = listOf(ref)[0];
|
|
125
|
+
if (typeof first === "string")
|
|
126
|
+
return first;
|
|
127
|
+
if (first && typeof first === "object" && !Array.isArray(first)) {
|
|
128
|
+
return typeof first.model === "string" ? first.model : firstId(first.model);
|
|
129
|
+
}
|
|
130
|
+
return undefined;
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* The model a ref tries first, with the effort and most tokens it's asked
|
|
134
|
+
* for, following aliases (`resources.model`): what a page shows of it.
|
|
135
|
+
*/
|
|
136
|
+
export function primaryOf(ctx, ref, outer = {}, seen = []) {
|
|
137
|
+
if (Array.isArray(ref))
|
|
138
|
+
return ref.length ? primaryOf(ctx, ref[0], outer, seen) : undefined;
|
|
139
|
+
if (typeof ref === "string") {
|
|
140
|
+
if (ref.includes("/"))
|
|
141
|
+
return { id: ref, ...outer };
|
|
142
|
+
const alias = (ctx.resources.model ?? {})[ref];
|
|
143
|
+
if (alias === undefined || seen.includes(ref))
|
|
144
|
+
return undefined;
|
|
145
|
+
// An alias holds defaults: what's set where it's used wins.
|
|
146
|
+
const found = primaryOf(ctx, alias, {}, [...seen, ref]);
|
|
147
|
+
return found && { ...found, ...outer };
|
|
148
|
+
}
|
|
149
|
+
// Settings next to a model are over those around it.
|
|
150
|
+
return primaryOf(ctx, ref.model, {
|
|
151
|
+
...outer,
|
|
152
|
+
...(typeof ref.effort === "string" ? { effort: ref.effort } : {}),
|
|
153
|
+
...(typeof ref.maxTokens === "number" ? { maxTokens: ref.maxTokens } : {}),
|
|
154
|
+
}, seen);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* A chain someone set on a page (each entry a model), with what the chain it
|
|
158
|
+
* replaces sets on its first entry: its `maxTokens`, and its `effort` where
|
|
159
|
+
* each model takes it and none was said (the catalog's, `models.efforts`).
|
|
160
|
+
* So a chain set by hand isn't cut short by an adapter's default.
|
|
161
|
+
*/
|
|
162
|
+
export async function settled(ctx, chain, like) {
|
|
163
|
+
const from = like ? primaryOf(ctx, like) : undefined;
|
|
164
|
+
return await Promise.all(chain.map(async (entry) => {
|
|
165
|
+
const picked = primaryOf(ctx, entry);
|
|
166
|
+
if (!picked || !from)
|
|
167
|
+
return entry;
|
|
168
|
+
const levels = from.effort && !picked.effort
|
|
169
|
+
? await ctx.call("models.efforts", { model: picked.id }).catch(() => null)
|
|
170
|
+
: null;
|
|
171
|
+
const settings = {
|
|
172
|
+
...(from.maxTokens !== undefined && picked.maxTokens === undefined
|
|
173
|
+
? { maxTokens: from.maxTokens }
|
|
174
|
+
: {}),
|
|
175
|
+
...(from.effort && levels?.includes(from.effort) ? { effort: from.effort } : {}),
|
|
176
|
+
};
|
|
177
|
+
return Object.keys(settings).length ? { model: entry, ...settings } : entry;
|
|
178
|
+
}));
|
|
179
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tier beside the send button (kind `tiers.composer`, chat's
|
|
3
|
+
* `composerControls`, hints `{ thread?, in? }`): what what's sent runs on,
|
|
4
|
+
* with what a typical message comes to for the person, their own plans
|
|
5
|
+
* costing nothing ("Everyday · on your plans · model $0"). In a
|
|
6
|
+
* conversation, choosing one sets that conversation's tier
|
|
7
|
+
* (`tiers.choose { thread }`), for everyone in it; on home, the person's own
|
|
8
|
+
* default for new chats (`{ mine }`). Each tier says its price; one over the
|
|
9
|
+
* org's cap is out of reach for those who don't run it, and says who sets
|
|
10
|
+
* it. Back to the default where one was chosen. Where the org uses only its
|
|
11
|
+
* default, it just says the tier: there's nothing to switch.
|
|
12
|
+
*/
|
|
13
|
+
import { type ReactNode } from "react";
|
|
14
|
+
import type { ViewContext } from "pairz";
|
|
15
|
+
export declare const tiersComposerView: (_: unknown, ctx: ViewContext<ReactNode>) => ReactNode;
|