@copilotz/team 0.0.0-stage → 1.0.0-rc.35
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 +49 -2
- package/WORDS.md +200 -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 +63 -4
- package/team/actions/agent.d.ts +4 -0
- package/team/actions/agent.js +179 -0
- package/team/actions/define.d.ts +1 -0
- package/team/actions/define.js +27 -0
- package/team/collections/agent.d.ts +132 -0
- package/team/collections/agent.js +75 -0
- package/team/contract.d.ts +10 -0
- package/team/contract.js +1 -0
- package/team/mod.d.ts +21 -0
- package/team/mod.js +49 -0
- package/team/shared/definition.d.ts +3 -0
- package/team/shared/definition.js +10 -0
- package/team/views/web/agent-add.d.ts +14 -0
- package/team/views/web/agent-add.js +48 -0
- package/team/views/web/agent-fields.d.ts +33 -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 +61 -0
- package/team/views/web/agent-new.d.ts +24 -0
- package/team/views/web/agent-new.js +129 -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 +17 -0
- package/team/views/web/agent.js +257 -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 +25 -0
- package/team/views/web/model-pick.js +39 -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 +246 -0
- package/team/views/web/words.d.ts +5 -0
- package/team/views/web/words.js +211 -0
- package/team/web.d.ts +22 -0
- package/team/web.js +41 -0
- package/tiers/actions/choose.d.ts +2 -0
- package/tiers/actions/choose.js +98 -0
- package/tiers/actions/model.d.ts +1 -0
- package/tiers/actions/model.js +36 -0
- package/tiers/collections/choice.d.ts +48 -0
- package/tiers/collections/choice.js +59 -0
- package/tiers/contract.d.ts +19 -0
- package/tiers/contract.js +1 -0
- package/tiers/mod.d.ts +54 -0
- package/tiers/mod.js +92 -0
- package/tiers/shared/shown.d.ts +87 -0
- package/tiers/shared/shown.js +155 -0
- package/tiers/shared/tiers.d.ts +103 -0
- package/tiers/shared/tiers.js +129 -0
- package/tiers/views/web/org.d.ts +38 -0
- package/tiers/views/web/org.js +142 -0
- package/tiers/views/web/space.d.ts +43 -0
- package/tiers/views/web/space.js +103 -0
- package/tiers/views/web/style.d.ts +7 -0
- package/tiers/views/web/style.js +87 -0
- package/tiers/views/web/words.d.ts +21 -0
- package/tiers/views/web/words.js +49 -0
- package/tiers/web.d.ts +18 -0
- package/tiers/web.js +40 -0
- package/web.d.ts +14 -0
- package/web.js +14 -0
package/tiers/mod.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tiers: which models the app's agents think with, chosen by what the work
|
|
3
|
+
* needs rather than model by model. The app declares its tiers (its `tiers`
|
|
4
|
+
* resource): each a title, what it's good for, and a model for each of its
|
|
5
|
+
* agents, as an agent's `model` takes one (one, settings around one, or a list
|
|
6
|
+
* tried in order). An org chooses one, and a space can choose another:
|
|
7
|
+
*
|
|
8
|
+
* - **Who chooses, first that applies:** a model picked for the agent in the
|
|
9
|
+
* space, the space's tier, a model picked for it by the org, the org's tier,
|
|
10
|
+
* then the tier the org's plan gives (the `planTier` resource, which credits
|
|
11
|
+
* fills); else the agent's own model. A tier that doesn't name an agent
|
|
12
|
+
* leaves it on its own. A pick keeps the tier's list behind it, so a model
|
|
13
|
+
* that fails falls back as before, and the `maxTokens` of the entry it
|
|
14
|
+
* replaces, with its `effort` when the picked model takes it.
|
|
15
|
+
* - **Names, never copies:** an org or a space keeps a tier's name
|
|
16
|
+
* (`modelChoice`), so retuning a tier moves everyone on it.
|
|
17
|
+
* - **Who may:** the org's owners and admins choose for the org, a space's
|
|
18
|
+
* managers for the space (`tiers.choose`, `tiers.pick`); everyone who
|
|
19
|
+
* reaches them sees what was chosen (`modelChoice.offered`,
|
|
20
|
+
* `modelChoice.agent`). Which tier a space takes isn't limited: the org's
|
|
21
|
+
* budget bounds what it spends.
|
|
22
|
+
* - **Every turn:** chat asks `tiers.model` through its `agentModel` resource,
|
|
23
|
+
* for the app's agents only; agents people make keep their own model. A
|
|
24
|
+
* change applies from each conversation's next reply.
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* plugins: [models, chat, ...access, tiers, definePlugin({
|
|
28
|
+
* name: "app",
|
|
29
|
+
* resources: {
|
|
30
|
+
* tiers: {
|
|
31
|
+
* everyday: {
|
|
32
|
+
* title: "Everyday",
|
|
33
|
+
* description: "Quick answers, routine work, volume",
|
|
34
|
+
* order: 1,
|
|
35
|
+
* agents: { north: "anthropic/claude-haiku-5-5", east: "openai/gpt-6-luna" },
|
|
36
|
+
* },
|
|
37
|
+
* expert: {
|
|
38
|
+
* title: "Expert",
|
|
39
|
+
* description: "Hard problems, decisions that matter",
|
|
40
|
+
* order: 3,
|
|
41
|
+
* agents: {
|
|
42
|
+
* north: [{ model: "anthropic/claude-opus-5-5", effort: "medium" }, "openai/gpt-6.1-sol"],
|
|
43
|
+
* east: { model: "anthropic/claude-sonnet-5-5", effort: "high" },
|
|
44
|
+
* },
|
|
45
|
+
* },
|
|
46
|
+
* },
|
|
47
|
+
* },
|
|
48
|
+
* })]
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
import { definePlugin } from "copilotz";
|
|
52
|
+
import { agentsOf } from "copilotz/plugins/agents/contract";
|
|
53
|
+
import { chooseAction, pickAction } from "./actions/choose.js";
|
|
54
|
+
import { modelAction } from "./actions/model.js";
|
|
55
|
+
import { modelChoice } from "./collections/choice.js";
|
|
56
|
+
import { tiersOf } from "./shared/tiers.js";
|
|
57
|
+
export * from "./contract.js";
|
|
58
|
+
export const tiers = definePlugin({
|
|
59
|
+
name: "tiers",
|
|
60
|
+
// Its choices are an org's and its spaces', changed by who runs them.
|
|
61
|
+
requires: ["chat", "spaces"],
|
|
62
|
+
collections: { modelChoice },
|
|
63
|
+
actions: {
|
|
64
|
+
"tiers.model": modelAction,
|
|
65
|
+
"tiers.choose": chooseAction,
|
|
66
|
+
"tiers.pick": pickAction,
|
|
67
|
+
},
|
|
68
|
+
resources: {
|
|
69
|
+
// Chat asks it which model each of the app's agents thinks with.
|
|
70
|
+
agentModel: { tiers: { action: "tiers.model" } },
|
|
71
|
+
},
|
|
72
|
+
// Every tier names only the app's agents, with models that can be called as configured.
|
|
73
|
+
async check(merged) {
|
|
74
|
+
const agents = agentsOf(merged);
|
|
75
|
+
for (const [name, tier] of Object.entries(tiersOf(merged))) {
|
|
76
|
+
if (!tier.title)
|
|
77
|
+
throw new Error(`Tier "${name}" has no title`);
|
|
78
|
+
for (const [agent, model] of Object.entries(tier.agents ?? {})) {
|
|
79
|
+
if (!agents[agent]) {
|
|
80
|
+
throw new Error(`Tier "${name}" names "${agent}", which isn't one of the app's agents`);
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
await merged.call("models.check", { model, port: "llm" });
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
throw new Error(`Tier "${name}", ${agent}: ${error.message}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
export default tiers;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a page shows of tiers, read as the person looking: the tiers on offer,
|
|
3
|
+
* each agent's model in them by name, and what a typical reply comes to at
|
|
4
|
+
* this org's prices; what the org and a space chose, and what applies, with
|
|
5
|
+
* where it comes from.
|
|
6
|
+
*
|
|
7
|
+
* A typical reply, for now: 20,000 tokens in, 16,000 of them read from the
|
|
8
|
+
* cache (a conversation's history mostly is), and 1,000 out. One plain profile
|
|
9
|
+
* for every agent, to compare tiers by; it's to be calibrated from the ledger
|
|
10
|
+
* (what replies really take, by agent and effort) once there's enough of it.
|
|
11
|
+
* A tier's price is its agents' average.
|
|
12
|
+
*/
|
|
13
|
+
import type { ReadContext } from "copilotz";
|
|
14
|
+
import { type From, type Levels } from "./tiers.js";
|
|
15
|
+
/** A typical reply's tokens (see above). */
|
|
16
|
+
export declare const REPLY: {
|
|
17
|
+
input: number;
|
|
18
|
+
cached: number;
|
|
19
|
+
output: number;
|
|
20
|
+
};
|
|
21
|
+
/** A model as a page shows it: its name, the effort it's asked for, and a typical reply's price. */
|
|
22
|
+
export type ModelShown = {
|
|
23
|
+
id: string;
|
|
24
|
+
name: string;
|
|
25
|
+
effort?: string;
|
|
26
|
+
/** USD, at this org's prices; null when nothing prices it. */
|
|
27
|
+
price: number | null;
|
|
28
|
+
/** It runs on the org's own key: its provider charges for it, not the app. */
|
|
29
|
+
ownKey?: true;
|
|
30
|
+
};
|
|
31
|
+
export type TierShown = {
|
|
32
|
+
name: string;
|
|
33
|
+
title: string;
|
|
34
|
+
description?: string;
|
|
35
|
+
/** A typical reply, its agents' average (USD); null when none is priced. */
|
|
36
|
+
price: number | null;
|
|
37
|
+
agents: {
|
|
38
|
+
agent: string;
|
|
39
|
+
model: ModelShown;
|
|
40
|
+
}[];
|
|
41
|
+
};
|
|
42
|
+
/** A choice as a page shows it: the tier by name, and each agent's pick. */
|
|
43
|
+
export type ChoiceShown = {
|
|
44
|
+
tier: string | null;
|
|
45
|
+
custom: Record<string, ModelShown>;
|
|
46
|
+
};
|
|
47
|
+
/** One agent's model where it works, and where it comes from (a tier with its title). */
|
|
48
|
+
export type AgentNow = {
|
|
49
|
+
model: ModelShown;
|
|
50
|
+
from: From & {
|
|
51
|
+
title?: string;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
export type Shown = {
|
|
55
|
+
tiers: TierShown[];
|
|
56
|
+
/** The app's agents, as they're declared. */
|
|
57
|
+
agents: {
|
|
58
|
+
agent: string;
|
|
59
|
+
name: string;
|
|
60
|
+
icon?: string;
|
|
61
|
+
}[];
|
|
62
|
+
/** The tier the org's plan gives, when the app has plans that say. */
|
|
63
|
+
plan: string | null;
|
|
64
|
+
org: ChoiceShown;
|
|
65
|
+
space?: ChoiceShown;
|
|
66
|
+
/** What applies here: the tier in force and who chose it, and each agent's model. */
|
|
67
|
+
now: {
|
|
68
|
+
tier: {
|
|
69
|
+
name: string;
|
|
70
|
+
by: "space" | "org" | "plan";
|
|
71
|
+
} | null;
|
|
72
|
+
agents: Record<string, AgentNow>;
|
|
73
|
+
};
|
|
74
|
+
reply: typeof REPLY;
|
|
75
|
+
};
|
|
76
|
+
/** What applies at a level: the org's and the plan's, and the space's when there is one. */
|
|
77
|
+
export declare function levelsOf(ctx: ReadContext, space?: string): Promise<Levels>;
|
|
78
|
+
/** Everything a page shows, at the org's level, or a space's. */
|
|
79
|
+
export declare function shownAt(ctx: ReadContext, space?: string): Promise<Shown>;
|
|
80
|
+
/**
|
|
81
|
+
* One of the app's agents where it works in a space: its model, where that
|
|
82
|
+
* comes from, and, when it's picked for the space, what it would be without
|
|
83
|
+
* (`without`: where taking the pick back leaves it).
|
|
84
|
+
*/
|
|
85
|
+
export declare function agentAt(ctx: ReadContext, agent: string, space: string): Promise<AgentNow & {
|
|
86
|
+
without?: AgentNow;
|
|
87
|
+
} | null>;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { agentsOf, modelOf } from "copilotz/plugins/agents/contract";
|
|
2
|
+
import { carried, ORG_CHOICE, primaryOf, resolve, spaceChoice, tierIn, tiersOf, } from "./tiers.js";
|
|
3
|
+
/** A typical reply's tokens (see above). */
|
|
4
|
+
export const REPLY = { input: 20_000, cached: 16_000, output: 1_000 };
|
|
5
|
+
const choices = (ctx) => ctx.db.collection("modelChoice");
|
|
6
|
+
/** The org's plan's tier, through the `planTier` resource when something declares it. */
|
|
7
|
+
async function planTierOf(ctx) {
|
|
8
|
+
const source = ctx.resources.planTier;
|
|
9
|
+
return source?.action ? await ctx.call(source.action, {}) : null;
|
|
10
|
+
}
|
|
11
|
+
/** What applies at a level: the org's and the plan's, and the space's when there is one. */
|
|
12
|
+
export async function levelsOf(ctx, space) {
|
|
13
|
+
const [org, here, plan] = await Promise.all([
|
|
14
|
+
choices(ctx).findOne({ _id: ORG_CHOICE }),
|
|
15
|
+
space ? choices(ctx).findOne({ _id: spaceChoice(space) }) : null,
|
|
16
|
+
planTierOf(ctx),
|
|
17
|
+
]);
|
|
18
|
+
return { org, ...(space ? { space: here } : {}), plan };
|
|
19
|
+
}
|
|
20
|
+
/** Names and prices for models, by `"provider/model"` and effort, each asked once. */
|
|
21
|
+
async function modelsShown(ctx, refs) {
|
|
22
|
+
const primaries = refs.map((ref) => primaryOf(ctx, ref));
|
|
23
|
+
const ids = [...new Set(primaries.flatMap((p) => p ? [p.id] : []))];
|
|
24
|
+
const offered = await Promise.all(ids.map((model) => ctx.call("models.offered", { model })
|
|
25
|
+
.then((answer) => answer.model, () => null)));
|
|
26
|
+
const known = new Map(ids.map((id, i) => [id, offered[i]]));
|
|
27
|
+
const declared = (ctx.resources.provider ?? {});
|
|
28
|
+
// What a typical reply is charged, for those the app charges for, when there's a ledger.
|
|
29
|
+
const charged = ctx.resources.usage ? ids.filter((id) => !known.get(id)?.ownKey) : [];
|
|
30
|
+
const quoted = charged.length
|
|
31
|
+
? await ctx.call("usage.quotes", {
|
|
32
|
+
uses: charged.map((id) => {
|
|
33
|
+
const slash = id.indexOf("/");
|
|
34
|
+
const from = declared[id.slice(0, slash)]?.catalog;
|
|
35
|
+
return {
|
|
36
|
+
meter: "llm",
|
|
37
|
+
subject: id,
|
|
38
|
+
catalog: `${typeof from === "string" ? from : id.slice(0, slash)}${id.slice(slash)}`,
|
|
39
|
+
quantities: REPLY,
|
|
40
|
+
};
|
|
41
|
+
}),
|
|
42
|
+
}).catch(() => charged.map(() => null))
|
|
43
|
+
: [];
|
|
44
|
+
const prices = new Map(charged.map((id, i) => [id, quoted[i] ?? null]));
|
|
45
|
+
return primaries.map((p) => {
|
|
46
|
+
if (!p)
|
|
47
|
+
return undefined;
|
|
48
|
+
const info = known.get(p.id);
|
|
49
|
+
return {
|
|
50
|
+
id: p.id,
|
|
51
|
+
name: info?.name ?? p.id.slice(p.id.indexOf("/") + 1),
|
|
52
|
+
...(p.effort ? { effort: p.effort } : {}),
|
|
53
|
+
price: prices.get(p.id) ?? null,
|
|
54
|
+
...(info?.ownKey ? { ownKey: true } : {}),
|
|
55
|
+
};
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/** The average of what's priced; null when nothing is. */
|
|
59
|
+
const average = (prices) => {
|
|
60
|
+
const priced = prices.filter((p) => p !== null);
|
|
61
|
+
return priced.length ? priced.reduce((a, b) => a + b, 0) / priced.length : null;
|
|
62
|
+
};
|
|
63
|
+
/** Everything a page shows, at the org's level, or a space's. */
|
|
64
|
+
export async function shownAt(ctx, space) {
|
|
65
|
+
const tiers = Object.entries(tiersOf(ctx))
|
|
66
|
+
.sort(([a, x], [b, y]) => (x.order ?? 0) - (y.order ?? 0) || a.localeCompare(b));
|
|
67
|
+
const agents = agentsOf(ctx);
|
|
68
|
+
const levels = await levelsOf(ctx, space);
|
|
69
|
+
// What each agent uses now, a pick with what it carries over.
|
|
70
|
+
const now = await Promise.all(Object.keys(agents).map(async (agent) => {
|
|
71
|
+
const found = resolve(ctx, agent, levels);
|
|
72
|
+
return { agent, from: found.from, model: await carried(ctx, found) };
|
|
73
|
+
}));
|
|
74
|
+
const custom = (choice) => Object.entries(choice?.custom ?? {}).filter(([agent]) => agents[agent]);
|
|
75
|
+
// Every model shown, asked for at once, and taken back in the same order.
|
|
76
|
+
const refs = [
|
|
77
|
+
...tiers.flatMap(([, tier]) => Object.values(tier.agents)),
|
|
78
|
+
...custom(levels.org).map(([, ref]) => ref),
|
|
79
|
+
...custom(levels.space).map(([, ref]) => ref),
|
|
80
|
+
...now.map((n) => n.model ?? modelOf(agents[n.agent])),
|
|
81
|
+
];
|
|
82
|
+
const shown = await modelsShown(ctx, refs);
|
|
83
|
+
let cursor = 0;
|
|
84
|
+
const next = () => shown[cursor++];
|
|
85
|
+
const tiersShown = tiers.map(([name, tier]) => {
|
|
86
|
+
const listed = Object.keys(tier.agents).map((agent) => ({ agent, model: next() }))
|
|
87
|
+
.filter((a) => !!a.model && !!agents[a.agent]);
|
|
88
|
+
return {
|
|
89
|
+
name,
|
|
90
|
+
title: tier.title,
|
|
91
|
+
...(tier.description ? { description: tier.description } : {}),
|
|
92
|
+
price: average(listed.map((a) => a.model.price)),
|
|
93
|
+
agents: listed,
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
const choiceShown = (choice) => ({
|
|
97
|
+
tier: choice?.tier && Object.hasOwn(tiersOf(ctx), choice.tier) ? choice.tier : null,
|
|
98
|
+
custom: Object.fromEntries(custom(choice).flatMap(([agent]) => {
|
|
99
|
+
const model = next();
|
|
100
|
+
return model ? [[agent, model]] : [];
|
|
101
|
+
})),
|
|
102
|
+
});
|
|
103
|
+
const org = choiceShown(levels.org);
|
|
104
|
+
const here = space ? choiceShown(levels.space) : undefined;
|
|
105
|
+
const inForce = tierIn(tiersOf(ctx), levels);
|
|
106
|
+
return {
|
|
107
|
+
tiers: tiersShown,
|
|
108
|
+
agents: Object.entries(agents).map(([agent, def]) => ({
|
|
109
|
+
agent,
|
|
110
|
+
name: def.name ?? agent[0].toUpperCase() + agent.slice(1),
|
|
111
|
+
...(def.icon ? { icon: def.icon } : {}),
|
|
112
|
+
})),
|
|
113
|
+
plan: levels.plan && Object.hasOwn(tiersOf(ctx), levels.plan) ? levels.plan : null,
|
|
114
|
+
org,
|
|
115
|
+
...(here ? { space: here } : {}),
|
|
116
|
+
now: {
|
|
117
|
+
tier: inForce ? { name: inForce.tier, by: inForce.by } : null,
|
|
118
|
+
agents: Object.fromEntries(now.flatMap((n) => {
|
|
119
|
+
const model = next();
|
|
120
|
+
return model ? [[n.agent, { model, from: titled(ctx, n.from) }]] : [];
|
|
121
|
+
})),
|
|
122
|
+
},
|
|
123
|
+
reply: REPLY,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/** Where a model comes from, with its tier's title when it's a tier's. */
|
|
127
|
+
const titled = (ctx, from) => "tier" in from && from.tier ? { ...from, title: tiersOf(ctx)[from.tier]?.title } : from;
|
|
128
|
+
/**
|
|
129
|
+
* One of the app's agents where it works in a space: its model, where that
|
|
130
|
+
* comes from, and, when it's picked for the space, what it would be without
|
|
131
|
+
* (`without`: where taking the pick back leaves it).
|
|
132
|
+
*/
|
|
133
|
+
export async function agentAt(ctx, agent, space) {
|
|
134
|
+
const own = agentsOf(ctx)[agent];
|
|
135
|
+
if (!own)
|
|
136
|
+
return null;
|
|
137
|
+
const levels = await levelsOf(ctx, space);
|
|
138
|
+
const now = resolve(ctx, agent, levels);
|
|
139
|
+
const picked = levels.space?.custom?.[agent] !== undefined;
|
|
140
|
+
const { [agent]: _, ...others } = levels.space?.custom ?? {};
|
|
141
|
+
const before = picked
|
|
142
|
+
? resolve(ctx, agent, { ...levels, space: { ...levels.space, custom: others } })
|
|
143
|
+
: undefined;
|
|
144
|
+
const [shown, without] = await modelsShown(ctx, [
|
|
145
|
+
await carried(ctx, now) ?? modelOf(own),
|
|
146
|
+
...(before ? [await carried(ctx, before) ?? modelOf(own)] : []),
|
|
147
|
+
]);
|
|
148
|
+
if (!shown)
|
|
149
|
+
return null;
|
|
150
|
+
return {
|
|
151
|
+
model: shown,
|
|
152
|
+
from: titled(ctx, now.from),
|
|
153
|
+
...(before && without ? { without: { model: without, from: titled(ctx, before.from) } } : {}),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tiers, and which model each of the app's agents thinks with: the app's
|
|
3
|
+
* tiers (its `tiers` resource), what an org and a space chose (`modelChoice`),
|
|
4
|
+
* and what the org's plan gives, worked out the same way for a turn and for a
|
|
5
|
+
* page. Only plain data in and out: no reads here.
|
|
6
|
+
*/
|
|
7
|
+
import type { Resources } from "copilotz";
|
|
8
|
+
import type { ModelRef } from "copilotz/plugins/models/contract";
|
|
9
|
+
/** A tier: a model for each of the app's agents, named by what it's good for. */
|
|
10
|
+
export type Tier = {
|
|
11
|
+
title: string;
|
|
12
|
+
/** What it's good for, in a line: "Quick answers, routine work, volume". */
|
|
13
|
+
description?: string;
|
|
14
|
+
/** Among the tiers, lowest first. */
|
|
15
|
+
order?: number;
|
|
16
|
+
/** Each agent's model, as an agent's `model` takes it: one, settings around one, or a list. */
|
|
17
|
+
agents: Record<string, ModelRef>;
|
|
18
|
+
};
|
|
19
|
+
/** What an org or a space chose: a tier by its name, and models picked for some agents. Unset: theirs above. */
|
|
20
|
+
export type Choice = {
|
|
21
|
+
tier?: string;
|
|
22
|
+
custom?: Record<string, ModelRef>;
|
|
23
|
+
};
|
|
24
|
+
/** Where a choice is kept: the org's, or a space's. */
|
|
25
|
+
export declare const ORG_CHOICE = "org";
|
|
26
|
+
export declare const spaceChoice: (space: string) => string;
|
|
27
|
+
/** The app's tiers. */
|
|
28
|
+
export declare const tiersOf: (ctx: {
|
|
29
|
+
resources: Resources;
|
|
30
|
+
}) => Record<string, Tier>;
|
|
31
|
+
/** Where a model comes from: picked here, a tier chosen here, the plan's tier, or the agent's own. */
|
|
32
|
+
export type From = {
|
|
33
|
+
by: "space" | "org";
|
|
34
|
+
custom: true;
|
|
35
|
+
tier?: string;
|
|
36
|
+
} | {
|
|
37
|
+
by: "space" | "org" | "plan";
|
|
38
|
+
tier: string;
|
|
39
|
+
} | {
|
|
40
|
+
by: "app";
|
|
41
|
+
};
|
|
42
|
+
/** What applies, at one level and those above it: each a choice, or none. */
|
|
43
|
+
export type Levels = {
|
|
44
|
+
space?: Choice | null;
|
|
45
|
+
org?: Choice | null;
|
|
46
|
+
plan?: string | null;
|
|
47
|
+
};
|
|
48
|
+
/** The tier in force: the space's, else the org's, else the plan's; and who chose it. */
|
|
49
|
+
export declare function tierIn(tiers: Record<string, Tier>, levels: Levels): {
|
|
50
|
+
tier: string;
|
|
51
|
+
by: "space" | "org" | "plan";
|
|
52
|
+
} | undefined;
|
|
53
|
+
/** What `resolve` says: the model, where it comes from, and for a pick, what it stands in front of. */
|
|
54
|
+
export type Resolved = {
|
|
55
|
+
model: ModelRef | null;
|
|
56
|
+
from: From;
|
|
57
|
+
/** A pick: itself, the entry it replaces (whose settings carry over), and the list behind it. */
|
|
58
|
+
pick?: {
|
|
59
|
+
ref: ModelRef;
|
|
60
|
+
replaces: ModelRef;
|
|
61
|
+
behind: ModelRef[];
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Which model one of the app's agents thinks with, and where it comes from,
|
|
66
|
+
* the first that applies: the space's pick for it, the space's tier, the
|
|
67
|
+
* org's pick, the org's tier, the plan's tier. A tier that doesn't name the
|
|
68
|
+
* agent leaves it on its own model; so does nothing chosen (`model` null).
|
|
69
|
+
* A pick keeps the list it stands in front of behind it (the tier in force
|
|
70
|
+
* below it, or the agent's own), so a model that fails falls back as before;
|
|
71
|
+
* `carried` gives it that entry's settings. An agent that isn't the app's is
|
|
72
|
+
* none of this: null.
|
|
73
|
+
*/
|
|
74
|
+
export declare function resolve(ctx: {
|
|
75
|
+
resources: Resources;
|
|
76
|
+
}, agent: string, levels: Levels): Resolved;
|
|
77
|
+
/** What a model's first choice is set to think and write with. */
|
|
78
|
+
type Settings = {
|
|
79
|
+
effort?: string;
|
|
80
|
+
maxTokens?: number;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* The model a ref tries first, with the effort and most tokens it's asked
|
|
84
|
+
* for, following aliases (`resources.model`): what a page shows of it, and
|
|
85
|
+
* what a pick carries over.
|
|
86
|
+
*/
|
|
87
|
+
export declare function primaryOf(ctx: {
|
|
88
|
+
resources: Resources;
|
|
89
|
+
}, ref: ModelRef, outer?: Settings, seen?: string[]): ({
|
|
90
|
+
id: string;
|
|
91
|
+
} & Settings) | undefined;
|
|
92
|
+
/**
|
|
93
|
+
* The model `resolve` found, a pick given what the entry it replaces sets:
|
|
94
|
+
* its `maxTokens`, and its `effort` when the picked model takes that level
|
|
95
|
+
* (the catalog's, `models.efforts`), else none, so a pick isn't cut short by
|
|
96
|
+
* an adapter's default nor refused for an effort it doesn't have. What the
|
|
97
|
+
* pick sets itself wins.
|
|
98
|
+
*/
|
|
99
|
+
export declare function carried(ctx: {
|
|
100
|
+
resources: Resources;
|
|
101
|
+
call<R>(action: string, input?: unknown): Promise<R>;
|
|
102
|
+
}, resolved: Resolved): Promise<ModelRef | null>;
|
|
103
|
+
export {};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { agentsOf, modelOf } from "copilotz/plugins/agents/contract";
|
|
2
|
+
/** Where a choice is kept: the org's, or a space's. */
|
|
3
|
+
export const ORG_CHOICE = "org";
|
|
4
|
+
export const spaceChoice = (space) => `space:${space}`;
|
|
5
|
+
/** The app's tiers. */
|
|
6
|
+
export const tiersOf = (ctx) => (ctx.resources.tiers ?? {});
|
|
7
|
+
/** A tier that's still offered: one that's gone counts as none chosen. */
|
|
8
|
+
const offered = (tiers, name) => name && Object.hasOwn(tiers, name) ? name : undefined;
|
|
9
|
+
/** The tier in force: the space's, else the org's, else the plan's; and who chose it. */
|
|
10
|
+
export function tierIn(tiers, levels) {
|
|
11
|
+
const space = offered(tiers, levels.space?.tier);
|
|
12
|
+
if (space)
|
|
13
|
+
return { tier: space, by: "space" };
|
|
14
|
+
const org = offered(tiers, levels.org?.tier);
|
|
15
|
+
if (org)
|
|
16
|
+
return { tier: org, by: "org" };
|
|
17
|
+
const plan = offered(tiers, levels.plan);
|
|
18
|
+
return plan ? { tier: plan, by: "plan" } : undefined;
|
|
19
|
+
}
|
|
20
|
+
/** A model, as a list: what's tried in order. */
|
|
21
|
+
const listOf = (ref) => Array.isArray(ref) ? ref : [ref];
|
|
22
|
+
/** The model a ref tries first, as `"provider/model"` when it says so plainly. */
|
|
23
|
+
const firstId = (ref) => {
|
|
24
|
+
const first = listOf(ref)[0];
|
|
25
|
+
if (typeof first === "string")
|
|
26
|
+
return first;
|
|
27
|
+
if (first && typeof first === "object" && !Array.isArray(first)) {
|
|
28
|
+
return typeof first.model === "string" ? first.model : firstId(first.model);
|
|
29
|
+
}
|
|
30
|
+
return undefined;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Which model one of the app's agents thinks with, and where it comes from,
|
|
34
|
+
* the first that applies: the space's pick for it, the space's tier, the
|
|
35
|
+
* org's pick, the org's tier, the plan's tier. A tier that doesn't name the
|
|
36
|
+
* agent leaves it on its own model; so does nothing chosen (`model` null).
|
|
37
|
+
* A pick keeps the list it stands in front of behind it (the tier in force
|
|
38
|
+
* below it, or the agent's own), so a model that fails falls back as before;
|
|
39
|
+
* `carried` gives it that entry's settings. An agent that isn't the app's is
|
|
40
|
+
* none of this: null.
|
|
41
|
+
*/
|
|
42
|
+
export function resolve(ctx, agent, levels) {
|
|
43
|
+
const tiers = tiersOf(ctx);
|
|
44
|
+
const own = agentsOf(ctx)[agent];
|
|
45
|
+
if (!own)
|
|
46
|
+
return { model: null, from: { by: "app" } };
|
|
47
|
+
const below = (from) => {
|
|
48
|
+
const rest = from === "space" ? levels : { ...levels, space: null };
|
|
49
|
+
const tier = tierIn(tiers, rest);
|
|
50
|
+
return tier && Object.hasOwn(tiers[tier.tier].agents, agent)
|
|
51
|
+
? { list: tiers[tier.tier].agents[agent], tier: tier.tier }
|
|
52
|
+
: { list: modelOf(own) };
|
|
53
|
+
};
|
|
54
|
+
for (const by of ["space", "org"]) {
|
|
55
|
+
const choice = levels[by];
|
|
56
|
+
const pick = choice?.custom?.[agent];
|
|
57
|
+
if (pick !== undefined) {
|
|
58
|
+
const { list, tier } = below(by);
|
|
59
|
+
const picked = firstId(pick);
|
|
60
|
+
const behind = listOf(list).filter((ref) => !picked || firstId(ref) !== picked);
|
|
61
|
+
return {
|
|
62
|
+
model: [...listOf(pick), ...behind],
|
|
63
|
+
from: { by, custom: true, ...(tier ? { tier } : {}) },
|
|
64
|
+
pick: { ref: pick, replaces: listOf(list)[0], behind },
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
const tier = offered(tiers, choice?.tier);
|
|
68
|
+
if (tier) {
|
|
69
|
+
return Object.hasOwn(tiers[tier].agents, agent)
|
|
70
|
+
? { model: tiers[tier].agents[agent], from: { by, tier } }
|
|
71
|
+
: { model: null, from: { by: "app" } };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const plan = offered(tiers, levels.plan);
|
|
75
|
+
if (plan && Object.hasOwn(tiers[plan].agents, agent)) {
|
|
76
|
+
return { model: tiers[plan].agents[agent], from: { by: "plan", tier: plan } };
|
|
77
|
+
}
|
|
78
|
+
return { model: null, from: { by: "app" } };
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The model a ref tries first, with the effort and most tokens it's asked
|
|
82
|
+
* for, following aliases (`resources.model`): what a page shows of it, and
|
|
83
|
+
* what a pick carries over.
|
|
84
|
+
*/
|
|
85
|
+
export function primaryOf(ctx, ref, outer = {}, seen = []) {
|
|
86
|
+
if (Array.isArray(ref))
|
|
87
|
+
return ref.length ? primaryOf(ctx, ref[0], outer, seen) : undefined;
|
|
88
|
+
if (typeof ref === "string") {
|
|
89
|
+
if (ref.includes("/"))
|
|
90
|
+
return { id: ref, ...outer };
|
|
91
|
+
const alias = (ctx.resources.model ?? {})[ref];
|
|
92
|
+
if (alias === undefined || seen.includes(ref))
|
|
93
|
+
return undefined;
|
|
94
|
+
// An alias holds defaults: what's set where it's used wins.
|
|
95
|
+
const found = primaryOf(ctx, alias, {}, [...seen, ref]);
|
|
96
|
+
return found && { ...found, ...outer };
|
|
97
|
+
}
|
|
98
|
+
// Settings next to a model are over those around it.
|
|
99
|
+
return primaryOf(ctx, ref.model, {
|
|
100
|
+
...outer,
|
|
101
|
+
...(typeof ref.effort === "string" ? { effort: ref.effort } : {}),
|
|
102
|
+
...(typeof ref.maxTokens === "number" ? { maxTokens: ref.maxTokens } : {}),
|
|
103
|
+
}, seen);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The model `resolve` found, a pick given what the entry it replaces sets:
|
|
107
|
+
* its `maxTokens`, and its `effort` when the picked model takes that level
|
|
108
|
+
* (the catalog's, `models.efforts`), else none, so a pick isn't cut short by
|
|
109
|
+
* an adapter's default nor refused for an effort it doesn't have. What the
|
|
110
|
+
* pick sets itself wins.
|
|
111
|
+
*/
|
|
112
|
+
export async function carried(ctx, resolved) {
|
|
113
|
+
if (!resolved.pick)
|
|
114
|
+
return resolved.model;
|
|
115
|
+
const { ref, replaces, behind } = resolved.pick;
|
|
116
|
+
const from = primaryOf(ctx, replaces);
|
|
117
|
+
const picked = primaryOf(ctx, ref);
|
|
118
|
+
const levels = from?.effort && picked
|
|
119
|
+
? await ctx.call("models.efforts", { model: picked.id }).catch(() => null)
|
|
120
|
+
: null;
|
|
121
|
+
const settings = {
|
|
122
|
+
...(from?.maxTokens !== undefined ? { maxTokens: from.maxTokens } : {}),
|
|
123
|
+
...(from?.effort && levels?.includes(from.effort) ? { effort: from.effort } : {}),
|
|
124
|
+
};
|
|
125
|
+
if (!Object.keys(settings).length)
|
|
126
|
+
return resolved.model;
|
|
127
|
+
const [first, ...rest] = listOf(ref);
|
|
128
|
+
return [{ model: first, ...settings }, ...rest, ...behind];
|
|
129
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The org's agents' models (kind `tiers.org`, a section of the org's page,
|
|
3
|
+
* the web shell's `orgSettings`, at `/org?tab=models`). It opens with what's
|
|
4
|
+
* in use and who chose it, then the tiers as cards, side by side: each its
|
|
5
|
+
* name, what it's good for, what a typical reply comes to at this org's
|
|
6
|
+
* prices, and each agent's model; the one in use outlined and marked with
|
|
7
|
+
* where it comes from ("Your plan's default", "Chosen by your org"). Owners
|
|
8
|
+
* and admins move to another in one click, with Undo right after; anyone else
|
|
9
|
+
* reads it, and is told who changes it. Under the cards, each agent's model
|
|
10
|
+
* now, and Customize: a model per agent, with the picker an agent's model is
|
|
11
|
+
* chosen with (team's `model.pick`), the tier's list kept behind it, and the
|
|
12
|
+
* way back to the tier, for one agent or all. What's shown is a watched read
|
|
13
|
+
* (`modelChoice.offered`), so a change shows at once, wherever it was made.
|
|
14
|
+
*/
|
|
15
|
+
import { type ReactNode } from "react";
|
|
16
|
+
import type { ViewContext } from "copilotz";
|
|
17
|
+
import type { Shown, TierShown } from "../../contract.js";
|
|
18
|
+
type Call = <R = unknown>(action: string, input?: unknown) => Promise<R>;
|
|
19
|
+
export declare const tiersOrgView: (_: unknown, ctx: ViewContext<ReactNode>) => ReactNode;
|
|
20
|
+
export declare function OrgModels({ shown, may, org, call }: {
|
|
21
|
+
shown: Shown;
|
|
22
|
+
may: boolean;
|
|
23
|
+
org?: string;
|
|
24
|
+
call: Call;
|
|
25
|
+
}): ReactNode;
|
|
26
|
+
/** A tier: what it's for, what a reply comes to, each agent's model, and choosing it. */
|
|
27
|
+
export declare function TierCard({ tier, current, plan, may, busy, agents, onChoose }: {
|
|
28
|
+
tier: TierShown;
|
|
29
|
+
/** Who chose it, when it's the one in use. */
|
|
30
|
+
current?: "space" | "org" | "plan";
|
|
31
|
+
/** It's the plan's default. */
|
|
32
|
+
plan: boolean;
|
|
33
|
+
may: boolean;
|
|
34
|
+
busy?: string;
|
|
35
|
+
agents: Shown["agents"];
|
|
36
|
+
onChoose(): void;
|
|
37
|
+
}): ReactNode;
|
|
38
|
+
export {};
|