copilotz 1.0.0-rc.12 → 1.0.0-rc.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +2 -2
  3. package/package.json +1 -1
  4. package/plugins/access/accounts/actions/model-key.d.ts +5 -0
  5. package/plugins/access/accounts/actions/model-key.js +102 -0
  6. package/plugins/access/accounts/collections/model-key.d.ts +58 -0
  7. package/plugins/access/accounts/collections/model-key.js +17 -0
  8. package/plugins/access/accounts/mod.d.ts +5 -0
  9. package/plugins/access/accounts/mod.js +14 -1
  10. package/plugins/access/accounts/resources/models.js +2 -0
  11. package/plugins/access/accounts/shared/rules.js +1 -1
  12. package/plugins/access/identity/collections/credential.d.ts +1 -1
  13. package/plugins/agents/chat/contract.d.ts +1 -0
  14. package/plugins/agents/chat/contract.js +1 -0
  15. package/plugins/agents/team/views/web/agent-fields.js +3 -4
  16. package/plugins/agents/team/views/web/model-picker.d.ts +19 -0
  17. package/plugins/agents/team/views/web/model-picker.js +195 -0
  18. package/plugins/agents/team/views/web/style.d.ts +2 -2
  19. package/plugins/agents/team/views/web/style.js +48 -1
  20. package/plugins/billing/usage/collections/rollup.d.ts +2 -2
  21. package/plugins/billing/usage/collections/usage.d.ts +2 -2
  22. package/plugins/compute/apps/actions/configure.js +2 -0
  23. package/plugins/compute/cloud/collections/pool-vm.d.ts +1 -1
  24. package/plugins/compute/cloud/mod.d.ts +2 -0
  25. package/plugins/compute/cloud/mod.js +5 -1
  26. package/plugins/compute/cloud/resources/tools.d.ts +3 -0
  27. package/plugins/compute/cloud/resources/tools.js +237 -0
  28. package/plugins/compute/computers/actions/allowed.js +4 -2
  29. package/plugins/compute/computers/actions/approve.js +3 -2
  30. package/plugins/compute/computers/actions/env.js +2 -0
  31. package/plugins/compute/computers/program/version.d.ts +1 -1
  32. package/plugins/compute/computers/program/version.js +1 -1
  33. package/plugins/compute/computers/resources/commands.js +3 -2
  34. package/plugins/compute/computers/resources/downloads.js +1 -1
  35. package/plugins/compute/computers/shared/ask.js +9 -5
  36. package/plugins/compute/computers/shared/settings.d.ts +4 -2
  37. package/plugins/compute/computers/shared/settings.js +2 -0
  38. package/plugins/compute/computers/shared/wake.d.ts +2 -0
  39. package/plugins/compute/computers/shared/where.d.ts +5 -1
  40. package/plugins/compute/computers/shared/where.js +4 -2
  41. package/plugins/compute/computers/views/context.js +5 -1
  42. package/plugins/compute/computers/views/web/asked.js +2 -0
  43. package/plugins/compute/computers/views/web/asks.js +3 -2
  44. package/plugins/interfaces/api/collections/key.d.ts +1 -1
  45. package/plugins/models/actions/offered.d.ts +32 -0
  46. package/plugins/models/actions/offered.js +140 -0
  47. package/plugins/models/contract.d.ts +5 -1
  48. package/plugins/models/mod.d.ts +6 -1
  49. package/plugins/models/mod.js +9 -1
  50. package/plugins/models/shared/catalog.json +9208 -8909
  51. package/plugins/models/shared/choice.d.ts +31 -0
  52. package/plugins/models/shared/choice.js +18 -2
  53. package/plugins/models/shared/generate.d.ts +1 -0
  54. package/plugins/models/shared/generate.js +36 -3
  55. package/runtime/client.js +28 -5
  56. package/runtime/executor.js +2 -2
package/CHANGELOG.md CHANGED
@@ -1,3 +1,28 @@
1
+ ## 1.0.0-rc.13 — 2026-11-01
2
+
3
+ - **Choose an agent's model from the catalog, with your own key.** The agent editor searches every
4
+ provider and model in the models.dev catalog an agent can work with (`models.offered`): those with
5
+ a protocol we speak and an address of their own. An org's owners and admins add a key for a
6
+ provider right there (`modelKey.set`): it's sealed, shows only its end, and its agents' calls to
7
+ that provider use it, over the app's. Those uses are tagged `paid: "own-key"`. The catalog now
8
+ keeps names, release dates, and which models can't call tools or answer in text.
9
+ - **Secrets stay out of the log.** `env.set` and `apps.configure` recorded their input, so a space's
10
+ or an app's secret was kept in plain text in its `action.started` event, sealed field or not. Both
11
+ are unrecorded now. Secrets set before this remain in events already written: rotate them.
12
+ - **Agents manage cloud computers.** `list_computers`, `create_computer` and `manage_computer` (add,
13
+ remove, rename, default, destroy) work in the conversation's space, as the person the agent works
14
+ for, through the same Actions and rules a person has. Nothing asks first except destroying, which
15
+ loses the disk: like publishing, it's asked every time and only "once" allows it. A person's own
16
+ machine isn't an agent's to manage. An agent in a space with no computer is told it can make one.
17
+ - **A query an Action runs knows who asks.** Named queries called from an Action or a transaction
18
+ lost the actor for what they call themselves, so a query asking another Action could fail, or
19
+ answer as though nobody asked.
20
+ - **A read's answer no longer overwrites something newer.** The owner answers a batch of reads once
21
+ its slowest is ready, so a change could be pushed before the answer that didn't include it. The
22
+ client then showed the older answer until the read next changed: on a page's first load (the push
23
+ was dropped) and after a reconnect (the push was overwritten). What's pushed while a read is asked
24
+ for now wins over its answer, and a connection that's been replaced is no longer listened to.
25
+
1
26
  ## 1.0.0-rc.12 — 2026-11-01
2
27
 
3
28
  - **An agent can hand work to itself in another space.** "Itself" now means the same agent in the
package/README.md CHANGED
@@ -22,7 +22,7 @@ bun add copilotz # Bun
22
22
  deno add jsr:@copilotz/copilotz # Deno (or `deno add npm:copilotz`)
23
23
  ```
24
24
 
25
- On Deno, name it `copilotz` in `deno.json` (`"copilotz": "jsr:@copilotz/copilotz@^1.0.0-rc.12"`),
25
+ On Deno, name it `copilotz` in `deno.json` (`"copilotz": "jsr:@copilotz/copilotz@^1.0.0-rc.13"`),
26
26
  and the imports below are the same everywhere. Some pieces need a package of their own, installed
27
27
  beside it: `esbuild` to bundle the web host on Node, `@electric-sql/pglite` for PostgreSQL in the
28
28
  process, and `@types/react` for the front ends' types.
@@ -204,7 +204,7 @@ and an app serves it from there by default:
204
204
  import { computerDownloads } from "copilotz/plugins/compute/downloads";
205
205
 
206
206
  plugins: [...plugins, await computerDownloads()], // this Copilotz's release
207
- plugins: [...plugins, await computerDownloads({ dir: "dist/computer/1.0.0-rc.12" })], // its own: /computer/download/
207
+ plugins: [...plugins, await computerDownloads({ dir: "dist/computer/1.0.0-rc.13" })], // its own: /computer/download/
208
208
  plugins: [...plugins, await computerDownloads({ url: "https://downloads.example.com/computer/" })], // or elsewhere
209
209
  ```
210
210
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "copilotz",
3
- "version": "1.0.0-rc.12",
3
+ "version": "1.0.0-rc.13",
4
4
  "description": "Copilotz: one runtime for Node, Deno, Bun, Cloudflare Workers and the browser.",
5
5
  "keywords": [
6
6
  "agents",
@@ -0,0 +1,5 @@
1
+ export declare const modelKeySet: import("../../../../runtime/define.js").ActionDef;
2
+ export declare const modelKeyRemove: import("../../../../runtime/define.js").ActionDef;
3
+ export declare const modelKeyMay: import("../../../../runtime/define.js").ActionDef;
4
+ export declare const modelKeyList: import("../../../../runtime/define.js").ActionDef;
5
+ export declare const modelKeyOpen: import("../../../../runtime/define.js").ActionDef;
@@ -0,0 +1,102 @@
1
+ /**
2
+ * An org's own keys for model providers (see collections/model-key.ts), as the
3
+ * models plugin's `modelKeys` resource names them. Its owners and admins change
4
+ * them, in person: `modelKey.set` `{ provider, key }` and `modelKey.remove`
5
+ * `{ provider }`; `modelKey.may` says whether the person asking can. For
6
+ * trusted code only: `modelKey.list` (nothing secret) and `modelKey.open`.
7
+ *
8
+ * All unrecorded: a key is in no event, as its input or its answer.
9
+ */
10
+ import { defineAction, forbidden, inPerson, invalid, people, } from "../../../../runtime/mod.js";
11
+ import { runsOrg } from "../../spaces/contract.js";
12
+ import { seal, unseal } from "../shared/seal.js";
13
+ import { settingsOf } from "../shared/settings.js";
14
+ const keysOf = (ctx) => ctx.as().db.collection("modelKey");
15
+ /** What a sealed key is bound to, so it can't be moved to another provider or org. */
16
+ const boundTo = (ctx, provider) => `modelKey:${ctx.resources.org?.id ?? ""}:${provider}`;
17
+ /** Whether whoever asks runs the org: only they change its keys. */
18
+ const runs = (ctx) => ctx.call("org.me", {})
19
+ .then((me) => !!me.staff || runsOrg(me.role ?? null), () => false);
20
+ const SHOW = 4;
21
+ /** A long key's last characters, to tell it apart; a short one shows nothing of itself. */
22
+ const hintOf = (key) => key.length >= 16 ? `••••${key.slice(-SHOW)}` : "••••";
23
+ export const modelKeySet = defineAction({
24
+ description: "Adds or replaces the org's own key for a model provider",
25
+ record: false,
26
+ input: {
27
+ type: "object",
28
+ properties: {
29
+ provider: { type: "string" },
30
+ key: { type: "string", minLength: 1, maxLength: 4096 },
31
+ },
32
+ required: ["provider", "key"],
33
+ additionalProperties: false,
34
+ },
35
+ allow: inPerson,
36
+ run: async (ctx, input) => {
37
+ if (!await runs(ctx))
38
+ throw forbidden("Only the org's owners and admins change its keys.");
39
+ if (!await ctx.call("models.keyable", { provider: input.provider })) {
40
+ throw invalid(`A key can't be added for "${input.provider}".`);
41
+ }
42
+ const key = input.key.trim();
43
+ if (!key || /\s/.test(key))
44
+ throw invalid("That doesn't look like a key.");
45
+ const sealed = await seal(settingsOf(ctx).key, boundTo(ctx, input.provider), {
46
+ access: key,
47
+ account: input.provider,
48
+ });
49
+ const hint = hintOf(key);
50
+ await keysOf(ctx).updateOne({ _id: input.provider }, { $set: { sealed, hint, by: ctx.actor.id, at: new Date().toISOString() } }, { upsert: true });
51
+ return { provider: input.provider, hint };
52
+ },
53
+ });
54
+ export const modelKeyRemove = defineAction({
55
+ description: "Removes the org's own key for a model provider",
56
+ record: false,
57
+ input: {
58
+ type: "object",
59
+ properties: { provider: { type: "string" } },
60
+ required: ["provider"],
61
+ additionalProperties: false,
62
+ },
63
+ allow: inPerson,
64
+ run: async (ctx, input) => {
65
+ if (!await runs(ctx))
66
+ throw forbidden("Only the org's owners and admins change its keys.");
67
+ await keysOf(ctx).deleteOne({ _id: input.provider });
68
+ return { removed: true };
69
+ },
70
+ });
71
+ export const modelKeyMay = defineAction({
72
+ description: "Whether you may change the org's keys for model providers",
73
+ record: false,
74
+ input: { type: "object", additionalProperties: false },
75
+ allow: people,
76
+ run: async (ctx) => ({ set: await runs(ctx) }),
77
+ });
78
+ // No `allow`: only the system (a model call, the models plugin) asks these.
79
+ export const modelKeyList = defineAction({
80
+ record: false,
81
+ input: { type: "object", additionalProperties: false },
82
+ run: async (ctx) => (await keysOf(ctx).find({})).map((k) => ({
83
+ provider: k._id,
84
+ ...(k.hint ? { hint: k.hint } : {}),
85
+ })),
86
+ });
87
+ export const modelKeyOpen = defineAction({
88
+ record: false,
89
+ input: {
90
+ type: "object",
91
+ properties: { provider: { type: "string" } },
92
+ required: ["provider"],
93
+ additionalProperties: false,
94
+ },
95
+ run: async (ctx, input) => {
96
+ const found = await keysOf(ctx).findOne({ _id: input.provider });
97
+ if (!found)
98
+ return null;
99
+ const { access } = await unseal(settingsOf(ctx).key, boundTo(ctx, input.provider), found.sealed);
100
+ return access;
101
+ },
102
+ });
@@ -0,0 +1,58 @@
1
+ /**
2
+ * An org's own key for a model provider (`_id`, the provider's id): sealed, in
3
+ * a private field, as an account's tokens are, with its last characters to tell
4
+ * it apart. Nobody reads the collection: `modelKey.list` says what's kept, and
5
+ * `modelKey.open` opens one for a model call.
6
+ */
7
+ import { type Infer } from "../../../../runtime/mod.js";
8
+ declare const modelKeySchema: {
9
+ readonly type: "object";
10
+ readonly properties: {
11
+ readonly hint: {
12
+ readonly type: "string";
13
+ };
14
+ readonly by: {
15
+ readonly type: "string";
16
+ };
17
+ readonly at: {
18
+ readonly type: "string";
19
+ };
20
+ };
21
+ readonly required: readonly ["by", "at"];
22
+ };
23
+ export declare const modelKey: {
24
+ schema: {
25
+ readonly type: "object";
26
+ readonly properties: {
27
+ readonly hint: {
28
+ readonly type: "string";
29
+ };
30
+ readonly by: {
31
+ readonly type: "string";
32
+ };
33
+ readonly at: {
34
+ readonly type: "string";
35
+ };
36
+ };
37
+ readonly required: readonly ["by", "at"];
38
+ };
39
+ events?: boolean;
40
+ records?: boolean;
41
+ durable?: false;
42
+ indexes?: string[][];
43
+ private?: string[];
44
+ queries?: Record<string, import("../../../../runtime/define.js").QueryDef<{
45
+ [x: string]: unknown;
46
+ hint?: string | undefined;
47
+ at: string;
48
+ by: string;
49
+ } & {
50
+ _id: string;
51
+ }>> | undefined;
52
+ vectors?: string[];
53
+ };
54
+ export type ModelKeyDoc = Infer<typeof modelKeySchema> & {
55
+ _id: string;
56
+ sealed: unknown;
57
+ };
58
+ export {};
@@ -0,0 +1,17 @@
1
+ /**
2
+ * An org's own key for a model provider (`_id`, the provider's id): sealed, in
3
+ * a private field, as an account's tokens are, with its last characters to tell
4
+ * it apart. Nobody reads the collection: `modelKey.list` says what's kept, and
5
+ * `modelKey.open` opens one for a model call.
6
+ */
7
+ import { defineCollection } from "../../../../runtime/mod.js";
8
+ const modelKeySchema = {
9
+ type: "object",
10
+ properties: {
11
+ hint: { type: "string" },
12
+ by: { type: "string" },
13
+ at: { type: "string" },
14
+ },
15
+ required: ["by", "at"],
16
+ };
17
+ export const modelKey = defineCollection({ schema: modelKeySchema, private: ["sealed"] });
@@ -31,6 +31,11 @@
31
31
  * (resources/models.ts); without a connection the call fails
32
32
  * `account_missing`, and a list of models moves on. Its uses are metered
33
33
  * as the person's plan's (`paid: "subscription"`), at no cost to the app.
34
+ * - **An org's own keys for model providers** (`modelKey`, the models
35
+ * plugin's `modelKeys` resource): its owners and admins add one (`modelKey.set`)
36
+ * for any provider that can take one; each is sealed as tokens are, kept per
37
+ * org and shows only its last characters; its agents' calls to that provider
38
+ * use it (`modelKey.open`, trusted code only), over the app's.
34
39
  * - `account.changed` says when an account is connected, lost or
35
40
  * disconnected: the audit trail, with nothing secret in it.
36
41
  *
@@ -31,6 +31,11 @@
31
31
  * (resources/models.ts); without a connection the call fails
32
32
  * `account_missing`, and a list of models moves on. Its uses are metered
33
33
  * as the person's plan's (`paid: "subscription"`), at no cost to the app.
34
+ * - **An org's own keys for model providers** (`modelKey`, the models
35
+ * plugin's `modelKeys` resource): its owners and admins add one (`modelKey.set`)
36
+ * for any provider that can take one; each is sealed as tokens are, kept per
37
+ * org and shows only its last characters; its agents' calls to that provider
38
+ * use it (`modelKey.open`, trusted code only), over the app's.
34
39
  * - `account.changed` says when an account is connected, lost or
35
40
  * disconnected: the audit trail, with nothing secret in it.
36
41
  *
@@ -53,6 +58,8 @@ import { refreshAction } from "./actions/refresh.js";
53
58
  import { tokenAction } from "./actions/token.js";
54
59
  import { account } from "./collections/account.js";
55
60
  import { accountFlow } from "./collections/flow.js";
61
+ import { modelKey } from "./collections/model-key.js";
62
+ import { modelKeyList, modelKeyMay, modelKeyOpen, modelKeyRemove, modelKeySet, } from "./actions/model-key.js";
56
63
  import { changed } from "./events/changed.js";
57
64
  import { chatgpt } from "./resources/chatgpt.js";
58
65
  import { chatgptCost, chatgptModels } from "./resources/models.js";
@@ -73,7 +80,7 @@ const sendable = (address) => {
73
80
  };
74
81
  export const accounts = definePlugin({
75
82
  name: "accounts",
76
- collections: { account, accountFlow },
83
+ collections: { account, accountFlow, modelKey },
77
84
  actions: {
78
85
  "account.connect": connectAction,
79
86
  "account.complete": completeAction,
@@ -81,6 +88,11 @@ export const accounts = definePlugin({
81
88
  "account.disconnect": disconnectAction,
82
89
  "account.refresh": refreshAction,
83
90
  "account.token": tokenAction,
91
+ "modelKey.set": modelKeySet,
92
+ "modelKey.remove": modelKeyRemove,
93
+ "modelKey.may": modelKeyMay,
94
+ "modelKey.list": modelKeyList,
95
+ "modelKey.open": modelKeyOpen,
84
96
  },
85
97
  events: { "account.changed": changed },
86
98
  guard: rules,
@@ -88,6 +100,7 @@ export const accounts = definePlugin({
88
100
  accounts: {},
89
101
  accountProviders: { chatgpt },
90
102
  provider: { chatgpt: chatgptModels },
103
+ modelKeys: { open: "modelKey.open", list: "modelKey.list", may: "modelKey.may" },
91
104
  cost: { "chatgpt/*": chatgptCost },
92
105
  routes: { accounts: { path: CALLBACK, action: "account.callback", methods: ["GET"] } },
93
106
  },
@@ -6,6 +6,8 @@ export const accountHeaders = (provider) => async ({ call }) => {
6
6
  return (await call("account.token", { provider })).headers;
7
7
  };
8
8
  export const chatgptModels = {
9
+ title: "ChatGPT plan",
10
+ note: "Runs on the plan of whoever asks: each person connects their own.",
9
11
  adapter: "openai",
10
12
  catalog: "openai",
11
13
  baseURL: "https://chatgpt.com/backend-api/codex",
@@ -1,6 +1,6 @@
1
1
  const NOBODY = { _id: { $in: [] } };
2
2
  export const rules = (actor, { collection }) => collection === "account"
3
3
  ? actor.kind === "person" ? { person: actor.id } : NOBODY
4
- : collection === "accountFlow"
4
+ : collection === "accountFlow" || collection === "modelKey"
5
5
  ? NOBODY
6
6
  : undefined;
@@ -78,8 +78,8 @@ export declare const credential: {
78
78
  queries?: Record<string, import("../../../../runtime/define.js").QueryDef<{
79
79
  [x: string]: unknown;
80
80
  name?: string | undefined;
81
- staff?: boolean | undefined;
82
81
  hint?: string | undefined;
82
+ staff?: boolean | undefined;
83
83
  expires?: string | undefined;
84
84
  at: string;
85
85
  kind: "key" | "session";
@@ -18,6 +18,7 @@ export type { Thread } from "./collections/thread.js";
18
18
  export type { Wait } from "./collections/wait.js";
19
19
  export type { Agent, ChatSettings, ContextEntry, ConversationView, HomeView, Offer, Prompt, RefusalView, ShownCall, Step, Tool, ToolContext, ToolViews, Turn, } from "./shared/types.js";
20
20
  export { tool } from "./shared/types.js";
21
+ export { principalOf } from "./shared/send.js";
21
22
  export { type Origin, originKey, originOf } from "./shared/origin.js";
22
23
  export type { AgentSource, Roster, RosterSource, Team } from "./shared/agents.js";
23
24
  /**
@@ -1,4 +1,5 @@
1
1
  export { tool } from "./shared/types.js";
2
+ export { principalOf } from "./shared/send.js";
2
3
  export { originKey, originOf } from "./shared/origin.js";
3
4
  /**
4
5
  * The app's agents (`agentsOf`), who of them answer new conversations
@@ -9,8 +9,9 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
9
9
  * what's typed goes up as it changes (`onChange`, a patch).
10
10
  */
11
11
  import { useId, useState } from "react";
12
- import { Avatar, Button, Checkbox, Field, Icon, Input, message, Select, Skeleton, Spinner, Textarea, } from "../../../../../hosts/web/mod.js";
12
+ import { Avatar, Button, Checkbox, Field, Icon, Input, message, Skeleton, Spinner, Textarea, } from "../../../../../hosts/web/mod.js";
13
13
  import { ICONS } from "./agents.js";
14
+ import { ModelPicker } from "./model-picker.js";
14
15
  /** How many tools the line shows before it counts the rest. */
15
16
  const LINE = 6;
16
17
  export function AgentFields({ value, onChange, options, handle, call, autoFocus, disabled }) {
@@ -20,9 +21,7 @@ export function AgentFields({ value, onChange, options, handle, call, autoFocus,
20
21
  ? _jsx(Avatar, { name: value.name.trim() || "?", kind: "agent", icon: value.icon, size: 56 })
21
22
  : (_jsx("span", { className: "agent-face-empty", "aria-hidden": "true", children: _jsx(Icon, { name: "bot", size: 24 }) })), _jsx(Field, { label: "Name", className: "agent-form-name", hint: handle
22
23
  ? (_jsxs(_Fragment, { children: ["Called ", _jsxs("b", { className: "agent-handle", children: ["@", handle] }), " in chats"] }))
23
- : "What people call it: Maya is @maya in chats.", children: (control) => (_jsx(Input, { ...control, value: value.name, maxLength: 60, placeholder: "Maya", autoComplete: "off", autoFocus: autoFocus, disabled: disabled, onChange: (event) => onChange({ name: event.target.value }) })) })] }), _jsxs("div", { className: "agent-form-field", role: "radiogroup", "aria-labelledby": `${id}-icon`, children: [_jsx("span", { id: `${id}-icon`, className: "agent-form-label", children: "Emoji" }), _jsxs("div", { className: "agent-icons", children: [_jsx("button", { type: "button", role: "radio", "aria-checked": !value.icon, "aria-label": "None: its initial", title: "None: its initial", className: "agent-icon agent-icon-none", disabled: disabled, onClick: () => onChange({ icon: "" }), children: (value.name.trim()[0] ?? "A").toUpperCase() }), ICONS.map((icon) => (_jsx("button", { type: "button", role: "radio", "aria-checked": value.icon === icon, className: "agent-icon", disabled: disabled, onClick: () => onChange({ icon }), children: icon }, icon))), _jsx(Input, { size: "sm", className: "agent-icon-own", "aria-label": "Another emoji", placeholder: "Other", value: own ? value.icon : "", maxLength: 16, autoComplete: "off", disabled: disabled, "data-chosen": own || undefined, onChange: (event) => onChange({ icon: event.target.value.trim() }) })] })] }), _jsx(Field, { label: "What should it do?", hint: "A line or two, for people, and for other agents deciding whom to ask.", children: (control) => (_jsx(Textarea, { ...control, rows: 2, autoGrow: true, maxHeight: 160, maxLength: 500, value: value.description, placeholder: "Writes launch copy in our voice, and checks facts with the researcher.", disabled: disabled, onChange: (event) => onChange({ description: event.target.value }) })) }), _jsx(Instructions, { value: value, onChange: onChange, call: call, disabled: disabled }), _jsx(Field, { label: "Model", hint: "What it thinks with.", children: (control) => (_jsx(Select, { ...control, value: value.model, disabled: disabled, onChange: (event) => onChange({ model: event.target.value }), children: (options.models.includes(value.model) || !value.model
24
- ? options.models
25
- : [value.model, ...options.models]).map((m) => _jsx("option", { value: m, children: m }, m)) })) }), _jsx(Tools, { value: value.tools, onChange: (tools) => onChange({ tools }), options: options })] }));
24
+ : "What people call it: Maya is @maya in chats.", children: (control) => (_jsx(Input, { ...control, value: value.name, maxLength: 60, placeholder: "Maya", autoComplete: "off", autoFocus: autoFocus, disabled: disabled, onChange: (event) => onChange({ name: event.target.value }) })) })] }), _jsxs("div", { className: "agent-form-field", role: "radiogroup", "aria-labelledby": `${id}-icon`, children: [_jsx("span", { id: `${id}-icon`, className: "agent-form-label", children: "Emoji" }), _jsxs("div", { className: "agent-icons", children: [_jsx("button", { type: "button", role: "radio", "aria-checked": !value.icon, "aria-label": "None: its initial", title: "None: its initial", className: "agent-icon agent-icon-none", disabled: disabled, onClick: () => onChange({ icon: "" }), children: (value.name.trim()[0] ?? "A").toUpperCase() }), ICONS.map((icon) => (_jsx("button", { type: "button", role: "radio", "aria-checked": value.icon === icon, className: "agent-icon", disabled: disabled, onClick: () => onChange({ icon }), children: icon }, icon))), _jsx(Input, { size: "sm", className: "agent-icon-own", "aria-label": "Another emoji", placeholder: "Other", value: own ? value.icon : "", maxLength: 16, autoComplete: "off", disabled: disabled, "data-chosen": own || undefined, onChange: (event) => onChange({ icon: event.target.value.trim() }) })] })] }), _jsx(Field, { label: "What should it do?", hint: "A line or two, for people, and for other agents deciding whom to ask.", children: (control) => (_jsx(Textarea, { ...control, rows: 2, autoGrow: true, maxHeight: 160, maxLength: 500, value: value.description, placeholder: "Writes launch copy in our voice, and checks facts with the researcher.", disabled: disabled, onChange: (event) => onChange({ description: event.target.value }) })) }), _jsx(Instructions, { value: value, onChange: onChange, call: call, disabled: disabled }), _jsx(Field, { label: "Model", hint: "What it thinks with.", children: (control) => (_jsx(ModelPicker, { id: control.id, value: value.model, call: call, disabled: disabled, onChange: (model) => onChange({ model }) })) }), _jsx(Tools, { value: value.tools, onChange: (tools) => onChange({ tools }), options: options })] }));
26
25
  }
27
26
  /**
28
27
  * Its instructions: typed, or written for the person from its name and
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Choosing the model an agent thinks with, from every provider the app can
3
+ * reach (`models.offered`, from the models.dev catalog). Closed, it shows
4
+ * what's chosen, and says so when it can't run yet. Open, a dialog: search
5
+ * across providers and models, or browse the providers, those ready first, and
6
+ * then one's models, newest first, with what each costs and can do. A provider
7
+ * without a key asks for one: the org's owners and admins add it here
8
+ * (`modelKey.set`), and it's never shown again; anyone else is told whom to ask.
9
+ */
10
+ import { type ReactNode } from "react";
11
+ type Call = <R = unknown>(action: string, input?: unknown) => Promise<R>;
12
+ export declare function ModelPicker({ value, onChange, call, disabled, id }: {
13
+ value: string;
14
+ onChange(model: string): void;
15
+ call: Call;
16
+ disabled?: boolean;
17
+ id?: string;
18
+ }): ReactNode;
19
+ export {};
@@ -0,0 +1,195 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ /**
3
+ * Choosing the model an agent thinks with, from every provider the app can
4
+ * reach (`models.offered`, from the models.dev catalog). Closed, it shows
5
+ * what's chosen, and says so when it can't run yet. Open, a dialog: search
6
+ * across providers and models, or browse the providers, those ready first, and
7
+ * then one's models, newest first, with what each costs and can do. A provider
8
+ * without a key asks for one: the org's owners and admins add it here
9
+ * (`modelKey.set`), and it's never shown again; anyone else is told whom to ask.
10
+ */
11
+ import { useCallback, useEffect, useRef, useState } from "react";
12
+ import { createPortal } from "react-dom";
13
+ import { Badge, Button, Dialog, Icon, IconButton, Input, message, Notice, Skeleton, } from "../../../../../hosts/web/mod.js";
14
+ /** How long after typing it searches. */
15
+ const TYPING_MS = 180;
16
+ /** Tokens, shortly: `200K`, `1M`. */
17
+ const tokens = (count) => count >= 1_000_000
18
+ ? `${+(count / 1_000_000).toFixed(1)}M`
19
+ : count >= 1_000
20
+ ? `${Math.round(count / 1_000)}K`
21
+ : String(count);
22
+ const usd = (amount) => `$${+amount.toFixed(amount < 1 ? 3 : 2)}`;
23
+ /** What a model costs, per million tokens in and out; free when it says so. */
24
+ function priced({ price }) {
25
+ if (price?.input === undefined || price.output === undefined)
26
+ return undefined;
27
+ if (!price.input && !price.output)
28
+ return "Free";
29
+ return `${usd(price.input)} in · ${usd(price.output)} out per 1M`;
30
+ }
31
+ const MEDIA = {
32
+ image: "Images",
33
+ pdf: "PDFs",
34
+ audio: "Audio",
35
+ video: "Video",
36
+ };
37
+ /** What a model can do, in a line. */
38
+ function abilities(model) {
39
+ return [
40
+ ...(model.context ? [`${tokens(model.context)} context`] : []),
41
+ ...(model.input ?? []).map((kind) => MEDIA[kind] ?? kind),
42
+ ...(model.reasons ? ["Reasoning"] : []),
43
+ ];
44
+ }
45
+ export function ModelPicker({ value, onChange, call, disabled, id }) {
46
+ const [open, setOpen] = useState(false);
47
+ const [chosen, setChosen] = useState();
48
+ const load = useCallback(() => {
49
+ if (!value)
50
+ return setChosen(undefined);
51
+ call("models.offered", {
52
+ model: value,
53
+ }).then(setChosen, () => setChosen(undefined));
54
+ }, [value, call]);
55
+ useEffect(load, [load]);
56
+ // Known and not ready: nothing can reach it until its key is added.
57
+ const stuck = chosen?.provider && !chosen.provider.ready;
58
+ return (_jsxs(_Fragment, { children: [_jsxs("button", { type: "button", id: id, className: "model-chosen", disabled: disabled, "aria-haspopup": "dialog", onClick: () => setOpen(true), children: [_jsxs("span", { className: "model-chosen-text", children: [_jsx("b", { children: chosen?.model?.name ?? (value || "Choose a model") }), _jsxs("span", { children: [chosen?.provider?.name, chosen?.model && priced(chosen.model) ? ` · ${priced(chosen.model)}` : ""] })] }), stuck ? _jsx(Badge, { tone: "warning", children: "Needs a key" }) : null, _jsx(Icon, { name: "selector", size: 16 })] }), open
59
+ ? createPortal(_jsx(Dialog, { open: true, onClose: () => {
60
+ setOpen(false);
61
+ load();
62
+ }, title: "Choose a model", className: "model-dialog", children: _jsx(Choosing, { value: value, call: call, onChoose: (model) => {
63
+ onChange(model);
64
+ setOpen(false);
65
+ } }) }), document.body)
66
+ : null] }));
67
+ }
68
+ /** What's in the dialog: the search, then what it found, the providers, or one provider. */
69
+ function Choosing({ value, call, onChoose }) {
70
+ const [listing, setListing] = useState();
71
+ const [typed, setTyped] = useState("");
72
+ const [found, setFound] = useState();
73
+ const [at, setAt] = useState();
74
+ const [error, setError] = useState();
75
+ const list = useCallback(() => {
76
+ call("models.offered", {}).then(setListing, (e) => setError(message(e)));
77
+ }, [call]);
78
+ useEffect(list, [list]);
79
+ // The last search asked is the one that shows.
80
+ const asked = useRef(0);
81
+ useEffect(() => {
82
+ const search = typed.trim();
83
+ const mine = ++asked.current;
84
+ if (!search)
85
+ return setFound(undefined);
86
+ const timer = setTimeout(() => {
87
+ call("models.offered", { search }).then((answer) => mine === asked.current && setFound(answer), (e) => mine === asked.current && setError(message(e)));
88
+ }, TYPING_MS);
89
+ return () => clearTimeout(timer);
90
+ }, [typed, call]);
91
+ if (at) {
92
+ return (_jsx(ProviderModels, { id: at, value: value, call: call, may: !!listing?.set, onBack: () => {
93
+ setAt(undefined);
94
+ list();
95
+ }, onChoose: onChoose }));
96
+ }
97
+ return (_jsxs("div", { className: "model-choose", children: [_jsx(Input, { icon: "search", placeholder: "Search providers and models", "aria-label": "Search providers and models", value: typed, autoFocus: true, onChange: (event) => setTyped(event.target.value) }), error ? _jsx(Notice, { tone: "destructive", role: "alert", children: error }) : null, typed.trim()
98
+ ? _jsx(Results, { found: found, value: value, onProvider: setAt, onChoose: onChoose })
99
+ : _jsx(Providers, { listing: listing, onProvider: setAt })] }));
100
+ }
101
+ const Loading = () => (_jsx("div", { className: "model-list", "aria-busy": "true", children: [0, 1, 2, 3].map((n) => _jsx(Skeleton, { height: 46 }, n)) }));
102
+ function ProviderRow({ provider, onOpen }) {
103
+ return (_jsx("li", { children: _jsxs("button", { type: "button", className: "model-row", onClick: onOpen, children: [_jsxs("span", { className: "model-row-text", children: [_jsx("b", { children: provider.name }), _jsxs("span", { children: [provider.models, " model", provider.models === 1 ? "" : "s", provider.note ? ` · ${provider.note}` : ""] })] }), provider.hint ? _jsx(Badge, { tone: "primary", children: "Your key" }) : null, _jsx(Icon, { name: "chevron", size: 15 })] }) }));
104
+ }
105
+ /** The providers: those that can be used now, then those that take a key. */
106
+ function Providers({ listing, onProvider }) {
107
+ if (!listing)
108
+ return _jsx(Loading, {});
109
+ const ready = listing.providers.filter((p) => p.ready);
110
+ const others = listing.providers.filter((p) => !p.ready);
111
+ return (_jsxs("div", { className: "model-groups", children: [ready.length
112
+ ? (_jsxs("section", { children: [_jsx("h3", { className: "model-group", children: "Ready to use" }), _jsx("ul", { className: "model-list", children: ready.map((p) => (_jsx(ProviderRow, { provider: p, onOpen: () => onProvider(p.id) }, p.id))) })] }))
113
+ : null, others.length
114
+ ? (_jsxs("section", { children: [_jsx("h3", { className: "model-group", children: "With your own key" }), _jsx("ul", { className: "model-list", children: others.map((p) => (_jsx(ProviderRow, { provider: p, onOpen: () => onProvider(p.id) }, p.id))) })] }))
115
+ : null] }));
116
+ }
117
+ function ModelRow({ model, value, withProvider, onChoose }) {
118
+ const cost = priced(model);
119
+ return (_jsx("li", { children: _jsxs("button", { type: "button", className: "model-row model-row-model", disabled: !model.ready, "aria-current": model.id === value || undefined, onClick: onChoose, children: [_jsxs("span", { className: "model-row-text", children: [_jsx("b", { children: model.name }), _jsx("span", { children: [withProvider, ...abilities(model)].filter(Boolean).join(" · ") }), cost ? _jsx("span", { children: cost }) : null] }), model.id === value ? _jsx(Icon, { name: "check", size: 16 }) : null] }) }));
120
+ }
121
+ function Results({ found, value, onProvider, onChoose }) {
122
+ if (!found)
123
+ return _jsx(Loading, {});
124
+ if (!found.providers.length && !found.models.length) {
125
+ return _jsx("p", { className: "model-none", children: "Nothing matches that." });
126
+ }
127
+ return (_jsxs("div", { className: "model-groups", children: [found.providers.length
128
+ ? (_jsxs("section", { children: [_jsx("h3", { className: "model-group", children: "Providers" }), _jsx("ul", { className: "model-list", children: found.providers.map((p) => (_jsx(ProviderRow, { provider: p, onOpen: () => onProvider(p.id) }, p.id))) })] }))
129
+ : null, found.models.length
130
+ ? (_jsxs("section", { children: [_jsx("h3", { className: "model-group", children: "Models" }), _jsx("ul", { className: "model-list", children: found.models.map((m) => m.ready
131
+ ? (_jsx(ModelRow, { model: m, value: value, withProvider: m.from, onChoose: () => onChoose(m.id) }, m.id))
132
+ : (_jsx("li", { children: _jsxs("button", { type: "button", className: "model-row", onClick: () => onProvider(m.provider), children: [_jsxs("span", { className: "model-row-text", children: [_jsx("b", { children: m.name }), _jsx("span", { children: m.from })] }), _jsx(Badge, { children: "Needs a key" })] }) }, m.id))) })] }))
133
+ : null] }));
134
+ }
135
+ /** One provider: its key, if it takes one, and its models. */
136
+ function ProviderModels({ id, value, call, may, onBack, onChoose }) {
137
+ const [shown, setShown] = useState();
138
+ const [error, setError] = useState();
139
+ const load = useCallback(() => {
140
+ call("models.offered", {
141
+ provider: id,
142
+ }).then(setShown, (e) => setError(message(e)));
143
+ }, [id, call]);
144
+ useEffect(load, [load]);
145
+ const provider = shown?.provider;
146
+ return (_jsxs("div", { className: "model-choose", children: [_jsxs("div", { className: "model-provider-head", children: [_jsx(IconButton, { icon: "chevron-left", label: "All providers", size: "sm", onClick: onBack }), _jsx("h3", { children: provider?.name ?? id })] }), provider?.note ? _jsx("p", { className: "model-provider-note", children: provider.note }) : null, error ? _jsx(Notice, { tone: "destructive", role: "alert", children: error }) : null, provider?.keyed
147
+ ? _jsx(ProviderKey, { provider: provider, may: may, call: call, onChange: load })
148
+ : null, shown
149
+ ? (_jsx("ul", { className: "model-list", children: shown.models.map((m) => (_jsx(ModelRow, { model: m, value: value, onChoose: () => onChoose(m.id) }, m.id))) }))
150
+ : _jsx(Loading, {})] }));
151
+ }
152
+ /** The org's key for a provider: added, replaced or removed by those who run it. */
153
+ function ProviderKey({ provider, may, call, onChange }) {
154
+ const [editing, setEditing] = useState(false);
155
+ const [key, setKey] = useState("");
156
+ const [busy, setBusy] = useState(false);
157
+ const [error, setError] = useState();
158
+ const act = async (work) => {
159
+ setBusy(true);
160
+ setError(undefined);
161
+ try {
162
+ await work();
163
+ setKey("");
164
+ setEditing(false);
165
+ onChange();
166
+ }
167
+ catch (e) {
168
+ setError(message(e));
169
+ }
170
+ finally {
171
+ setBusy(false);
172
+ }
173
+ };
174
+ if (!may) {
175
+ return provider.ready ? null : (_jsxs(Notice, { tone: "neutral", icon: "info", children: ["This org has no key for ", provider.name, ". Its owners and admins can add one."] }));
176
+ }
177
+ // Asked for at once where nothing else can reach this provider.
178
+ if (editing || (!provider.ready && !provider.hint)) {
179
+ return (_jsxs("form", { className: "model-key", onSubmit: (event) => {
180
+ event.preventDefault();
181
+ // React hands it on to the form this picker was opened from.
182
+ event.stopPropagation();
183
+ if (key.trim()) {
184
+ void act(() => call("modelKey.set", { provider: provider.id, key: key.trim() }));
185
+ }
186
+ }, children: [_jsxs("label", { htmlFor: `key-${provider.id}`, children: ["Your ", provider.name, " API key"] }), _jsxs("div", { className: "model-key-entry", children: [_jsx(Input, { id: `key-${provider.id}`, type: "password", autoComplete: "off", spellCheck: false, placeholder: "Paste it here", value: key, invalid: !!error, disabled: busy, onChange: (event) => setKey(event.target.value) }), _jsx(Button, { type: "submit", variant: "primary", disabled: busy || !key.trim(), children: "Save" }), provider.ready || provider.hint
187
+ ? (_jsx(Button, { type: "button", variant: "ghost", disabled: busy, onClick: () => setEditing(false), children: "Cancel" }))
188
+ : null] }), _jsxs("p", { children: ["Encrypted, and never shown again. All of this org's agents use it for ", provider.name, ", and", " ", provider.name, " charges your account."] }), error ? _jsx("p", { className: "model-key-error", role: "alert", children: error }) : null] }));
189
+ }
190
+ return (_jsxs("div", { className: "model-key-kept", children: [_jsx("span", { children: provider.hint
191
+ ? (_jsxs(_Fragment, { children: ["This org's key ", _jsx("code", { children: provider.hint })] }))
192
+ : "Uses the app's key" }), _jsxs("span", { className: "model-key-actions", children: [_jsx(Button, { type: "button", variant: "ghost", size: "sm", disabled: busy, onClick: () => setEditing(true), children: provider.hint ? "Replace" : "Use your own key" }), provider.hint
193
+ ? (_jsx(Button, { type: "button", variant: "ghost", size: "sm", disabled: busy, onClick: () => void act(() => call("modelKey.remove", { provider: provider.id })), children: "Remove" }))
194
+ : null] }), error ? _jsx("p", { className: "model-key-error", role: "alert", children: error }) : null] }));
195
+ }