@fungi.computer/tourtois 0.27.1 → 0.28.0
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/dist/SettingsHome.js +2 -0
- package/dist/SettingsNavigation.js +7 -0
- package/dist/SystemSettings.js +5 -11
- package/dist/Vault.d.ts +341 -0
- package/dist/Vault.js +930 -0
- package/dist/oyster-platform-controls.d.ts +2 -0
- package/dist/oyster-platform-controls.js +210 -49
- package/dist/oyster-setup.js +6 -4
- package/dist/platform-controls.d.ts +1 -0
- package/dist/platform-controls.js +1 -0
- package/dist/settings-item.d.ts +1 -1
- package/dist/settings-item.js +1 -1
- package/dist/settings-routes.d.ts +13 -6
- package/dist/settings-routes.js +29 -14
- package/dist/settings-sections.d.ts +5 -3
- package/dist/settings-sections.js +2 -1
- package/dist/shell.css +155 -0
- package/package.json +10 -5
- package/dist/ModelKeys.d.ts +0 -77
- package/dist/ModelKeys.js +0 -275
package/dist/Vault.js
ADDED
|
@@ -0,0 +1,930 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { SectionHeading } from "@fungi.computer/caps/blocks/section-heading";
|
|
3
|
+
import { Button } from "@fungi.computer/caps/components/button";
|
|
4
|
+
import { DataList, DataRow, FactRow, } from "@fungi.computer/caps/components/data-list";
|
|
5
|
+
import { Field, FieldSet, FormFooter, } from "@fungi.computer/caps/components/field";
|
|
6
|
+
import { Checkbox } from "@fungi.computer/caps/components/checkbox";
|
|
7
|
+
import { Input } from "@fungi.computer/caps/components/input";
|
|
8
|
+
import { KeycapButton } from "@fungi.computer/caps/components/keycap";
|
|
9
|
+
import { Select } from "@fungi.computer/caps/components/select";
|
|
10
|
+
import { Status } from "@fungi.computer/caps/components/status";
|
|
11
|
+
import { toast } from "@fungi.computer/caps/components/toaster";
|
|
12
|
+
import { FormField, FormFields, SchemaForm, field, useSchemaForm, } from "@fungi.computer/caps/form";
|
|
13
|
+
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
14
|
+
import { useCallback, useMemo, useState, } from "react";
|
|
15
|
+
import * as z from "zod";
|
|
16
|
+
import { WORKBENCH_NOTICES } from "./app-toast.js";
|
|
17
|
+
import { SettingsItem } from "./settings-item.js";
|
|
18
|
+
const methods = [
|
|
19
|
+
"GET",
|
|
20
|
+
"HEAD",
|
|
21
|
+
"POST",
|
|
22
|
+
"PUT",
|
|
23
|
+
"PATCH",
|
|
24
|
+
"DELETE",
|
|
25
|
+
"OPTIONS",
|
|
26
|
+
];
|
|
27
|
+
const methodSchema = z.enum(methods);
|
|
28
|
+
/** One site a secret is bound to: an exact host, a path and its methods. */
|
|
29
|
+
const destinationSchema = z.strictObject({
|
|
30
|
+
host: z.string().min(1).max(253),
|
|
31
|
+
pathPrefix: z.string().max(2048).optional(),
|
|
32
|
+
methods: z.array(methodSchema).min(1).max(7),
|
|
33
|
+
});
|
|
34
|
+
/** One through eight rules per secret (adr-vault-v1). */
|
|
35
|
+
const destinationLimit = 8;
|
|
36
|
+
/** Where the value goes on the way out. The host places it; nobody reads it. */
|
|
37
|
+
const placementSchema = z.union([
|
|
38
|
+
z.literal("bearer"),
|
|
39
|
+
z.literal("basic"),
|
|
40
|
+
z.strictObject({ header: z.string().min(1).max(64) }),
|
|
41
|
+
z.strictObject({ query: z.string().min(1).max(64) }),
|
|
42
|
+
]);
|
|
43
|
+
/** Who may use a stored value: the broker's principal vocabulary. */
|
|
44
|
+
const subjectSchema = z.strictObject({
|
|
45
|
+
kind: z.enum(["agent", "computer", "app", "team-key"]),
|
|
46
|
+
id: z.string().min(1),
|
|
47
|
+
});
|
|
48
|
+
/** One stored credential as the owner lists it. No value, ever. */
|
|
49
|
+
const credentialSchema = z.strictObject({
|
|
50
|
+
connectionId: z.string().min(1),
|
|
51
|
+
name: z.string().min(1),
|
|
52
|
+
label: z.string().min(1),
|
|
53
|
+
/** A secret bound to destinations, or the Connection that holds it. */
|
|
54
|
+
kind: z.enum(["secret", "mcp", "openapi"]),
|
|
55
|
+
origin: z.string(),
|
|
56
|
+
destinations: z.array(destinationSchema).nullable(),
|
|
57
|
+
/** A pasted value, a sign-in's tokens, or nothing yet. */
|
|
58
|
+
credential: z.enum(["value", "sign-in", "none"]),
|
|
59
|
+
placement: placementSchema.nullable(),
|
|
60
|
+
state: z.enum(["pending", "active", "reconnect_required"]),
|
|
61
|
+
version: z.number().int().positive(),
|
|
62
|
+
subjects: z.array(subjectSchema),
|
|
63
|
+
});
|
|
64
|
+
/** One stored model key as the owner lists it. The key never comes back. */
|
|
65
|
+
const modelKeySchema = z.strictObject({
|
|
66
|
+
keyId: z.string().min(1),
|
|
67
|
+
provider: z.string().min(1),
|
|
68
|
+
label: z.string().min(1),
|
|
69
|
+
/** A Team key, or a key that serves only the member who added it. */
|
|
70
|
+
scope: z.enum(["user", "team"]),
|
|
71
|
+
createdAt: z.number().nonnegative(),
|
|
72
|
+
/** The server an OpenAI-compatible key calls; null for a named provider. */
|
|
73
|
+
baseUrl: z.string().nullable(),
|
|
74
|
+
});
|
|
75
|
+
/** A provider the host offers; the host owns the list. */
|
|
76
|
+
const providerSchema = z.strictObject({
|
|
77
|
+
id: z.string().min(1),
|
|
78
|
+
label: z.string().min(1),
|
|
79
|
+
/** Cloudflare Workers AI needs the account its token belongs to. */
|
|
80
|
+
accountId: z.boolean(),
|
|
81
|
+
/** An OpenAI-compatible server names where it runs. */
|
|
82
|
+
baseUrl: z.boolean(),
|
|
83
|
+
});
|
|
84
|
+
/**
|
|
85
|
+
* Everything one owner holds, as the page shows it: model keys and
|
|
86
|
+
* Connection credentials together, what the viewer may manage, and the
|
|
87
|
+
* Agents and Computers a secret can be scoped to. No value appears.
|
|
88
|
+
*/
|
|
89
|
+
export const vaultViewSchema = z.strictObject({
|
|
90
|
+
/** The host's Vault authority, for display; the server still decides. */
|
|
91
|
+
role: z.strictObject({
|
|
92
|
+
/** Team secrets, Connection credentials and Team model keys. */
|
|
93
|
+
manageTeam: z.boolean(),
|
|
94
|
+
/** The viewer's own model keys. */
|
|
95
|
+
managePersonal: z.boolean(),
|
|
96
|
+
}),
|
|
97
|
+
/** Stored values Sessions cannot scrub yet; rotating one protects it. */
|
|
98
|
+
coverage: z.discriminatedUnion("state", [
|
|
99
|
+
z.strictObject({ state: z.literal("complete") }),
|
|
100
|
+
z.strictObject({
|
|
101
|
+
state: z.literal("incomplete"),
|
|
102
|
+
missing: z.array(z.strictObject({
|
|
103
|
+
kind: z.enum(["model-key", "connection"]),
|
|
104
|
+
id: z.string().min(1),
|
|
105
|
+
})),
|
|
106
|
+
}),
|
|
107
|
+
]),
|
|
108
|
+
providers: z.array(providerSchema).min(1),
|
|
109
|
+
/** The model-key scopes this host offers. */
|
|
110
|
+
scopes: z.array(z.enum(["user", "team"])).min(1),
|
|
111
|
+
modelKeys: z.array(modelKeySchema),
|
|
112
|
+
credentials: z.array(credentialSchema),
|
|
113
|
+
subjects: z.strictObject({
|
|
114
|
+
agents: z.array(z.strictObject({ id: z.string().min(1), name: z.string() })),
|
|
115
|
+
computers: z.array(z.strictObject({ id: z.string().min(1), name: z.string() })),
|
|
116
|
+
}),
|
|
117
|
+
});
|
|
118
|
+
const agentChoiceSchema = z.strictObject({
|
|
119
|
+
keySource: z.enum(["workers-ai", "ai-gateway", "byok"]),
|
|
120
|
+
keyId: z.string().min(1).optional(),
|
|
121
|
+
model: z.strictObject({
|
|
122
|
+
providerId: z.string().min(1),
|
|
123
|
+
modelId: z.string().min(1),
|
|
124
|
+
reasoning: z.string().optional(),
|
|
125
|
+
}),
|
|
126
|
+
});
|
|
127
|
+
/** Every Agent with its persisted model choice, and the models each key offers. */
|
|
128
|
+
export const agentModelsViewSchema = z.strictObject({
|
|
129
|
+
models: z.enum(["ready", "missing", "unknown"]),
|
|
130
|
+
catalogs: z.partialRecord(z.string().min(1), z.array(z.strictObject({
|
|
131
|
+
id: z.string().min(1),
|
|
132
|
+
name: z.string().min(1),
|
|
133
|
+
input: z.number().nonnegative(),
|
|
134
|
+
output: z.number().nonnegative(),
|
|
135
|
+
tools: z.boolean(),
|
|
136
|
+
}))),
|
|
137
|
+
agents: z.array(z.strictObject({
|
|
138
|
+
agentId: z.string().min(1),
|
|
139
|
+
name: z.string(),
|
|
140
|
+
choice: agentChoiceSchema.nullable(),
|
|
141
|
+
})),
|
|
142
|
+
});
|
|
143
|
+
const vaultQueryKey = ["tourtois", "vault"];
|
|
144
|
+
/** What the Vault is, in the one sentence pair every host opens it with. */
|
|
145
|
+
export const vaultLede = "Every key and credential your Agents use, in one place. A value goes in once and is never shown again, to you or to an Agent.";
|
|
146
|
+
const defaultSource = "default";
|
|
147
|
+
const providerLabel = (providers, id) => providers.find((choice) => choice.id === id)?.label ?? id;
|
|
148
|
+
/**
|
|
149
|
+
* A host's refusal, already a sentence the owner can act on. Anything else a
|
|
150
|
+
* port throws is shown as the page's own fallback, never as host detail.
|
|
151
|
+
*/
|
|
152
|
+
export class VaultRefusal extends Error {
|
|
153
|
+
constructor(sentence, options) {
|
|
154
|
+
super(sentence, options);
|
|
155
|
+
this.name = "VaultRefusal";
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const failure = (cause, fallback) => cause instanceof VaultRefusal ? cause.message : fallback;
|
|
159
|
+
const dateText = (at) => new Date(at).toLocaleDateString(undefined, {
|
|
160
|
+
month: "short",
|
|
161
|
+
day: "numeric",
|
|
162
|
+
year: "numeric",
|
|
163
|
+
});
|
|
164
|
+
const timeText = (at) => new Date(at).toLocaleString(undefined, {
|
|
165
|
+
month: "short",
|
|
166
|
+
day: "numeric",
|
|
167
|
+
hour: "numeric",
|
|
168
|
+
minute: "2-digit",
|
|
169
|
+
});
|
|
170
|
+
const placementText = (placement) => placement === null
|
|
171
|
+
? null
|
|
172
|
+
: placement === "bearer"
|
|
173
|
+
? "Bearer token"
|
|
174
|
+
: placement === "basic"
|
|
175
|
+
? "Basic sign-in"
|
|
176
|
+
: "header" in placement
|
|
177
|
+
? `${placement.header} header`
|
|
178
|
+
: `?${placement.query}`;
|
|
179
|
+
const destinationText = (destination) => `${destination.host}${destination.pathPrefix ?? ""} · ${destination.methods.join(", ")}`;
|
|
180
|
+
function plural(count, one, many) {
|
|
181
|
+
return `${String(count)} ${count === 1 ? one : many}`;
|
|
182
|
+
}
|
|
183
|
+
function subjectsText(subjects, connection) {
|
|
184
|
+
const agents = subjects.filter((subject) => subject.kind === "agent").length;
|
|
185
|
+
const computers = subjects.filter((subject) => subject.kind === "computer").length;
|
|
186
|
+
const parts = [
|
|
187
|
+
agents > 0 ? plural(agents, "Agent", "Agents") : null,
|
|
188
|
+
computers > 0 ? plural(computers, "Computer", "Computers") : null,
|
|
189
|
+
].filter((part) => part !== null);
|
|
190
|
+
if (parts.length > 0)
|
|
191
|
+
return `Used by ${parts.join(", ")}`;
|
|
192
|
+
return connection ? "No Agent has it yet" : "Nobody can use it yet";
|
|
193
|
+
}
|
|
194
|
+
/** One choice in a short multi-select: a Caps checkbox and its name. */
|
|
195
|
+
function Choice({ label, checked, disabled, onChange, }) {
|
|
196
|
+
return (_jsxs("label", { className: "tourtois-vault-choice", children: [_jsx(Checkbox, { checked: checked, disabled: disabled, onChange: onChange }), _jsx("span", { children: label })] }));
|
|
197
|
+
}
|
|
198
|
+
/** A section's title at the shared left edge, its explanation under it. */
|
|
199
|
+
function SectionHead({ title, note }) {
|
|
200
|
+
return (_jsxs("header", { className: "tourtois-vault-section-head", children: [_jsx(SectionHeading, { className: "tourtois-vault-section-title", children: title }), _jsx("p", { className: "tourtois-vault-note", children: note })] }));
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* The add form under a list: closed behind its opener until asked for, and
|
|
204
|
+
* always open while the list is empty. Opening moves focus to the first field.
|
|
205
|
+
*/
|
|
206
|
+
function AddDisclosure({ label, empty, initiallyOpen = false, children, }) {
|
|
207
|
+
const [asked, setAsked] = useState(initiallyOpen);
|
|
208
|
+
const [focusFirst, setFocusFirst] = useState(false);
|
|
209
|
+
const into = useCallback((node) => {
|
|
210
|
+
if (node === null || !focusFirst)
|
|
211
|
+
return;
|
|
212
|
+
node
|
|
213
|
+
.querySelector("input:not([type=hidden]), select, textarea")
|
|
214
|
+
?.focus();
|
|
215
|
+
setFocusFirst(false);
|
|
216
|
+
}, [focusFirst]);
|
|
217
|
+
if (!asked && !empty)
|
|
218
|
+
return (_jsx("div", { className: "tourtois-vault-add", children: _jsx(KeycapButton, { density: "compact", label: label, "aria-expanded": false, onClick: () => {
|
|
219
|
+
setAsked(true);
|
|
220
|
+
setFocusFirst(true);
|
|
221
|
+
} }) }));
|
|
222
|
+
return (_jsx("div", { ref: into, className: "tourtois-vault-add-form", children: children(empty
|
|
223
|
+
? undefined
|
|
224
|
+
: () => {
|
|
225
|
+
setAsked(false);
|
|
226
|
+
}) }));
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* A value field with no way to show what was typed: a write-only password
|
|
230
|
+
* input. The Vault never offers a reveal, even of the draft.
|
|
231
|
+
*/
|
|
232
|
+
function writeOnly(props) {
|
|
233
|
+
return (_jsx(Input, { id: props.id, name: props.name, type: "password", autoComplete: "off", spellCheck: false, className: "caps-field-secret", "aria-describedby": props["aria-describedby"], "aria-invalid": props["aria-invalid"], disabled: props.disabled, value: props.value, onBlur: props.onBlur, onChange: (event) => {
|
|
234
|
+
props.onChange(event.currentTarget.value);
|
|
235
|
+
} }));
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Vault: every key and credential this owner holds, in one place. Add one,
|
|
239
|
+
* replace its value, choose where and by whom it may be used, revoke it and
|
|
240
|
+
* see each use. A value goes in once and is never shown again.
|
|
241
|
+
*/
|
|
242
|
+
export function Vault({ port, focus = false, toasterId = WORKBENCH_NOTICES, heading = true, }) {
|
|
243
|
+
const client = useQueryClient();
|
|
244
|
+
const view = useQuery({
|
|
245
|
+
queryKey: vaultQueryKey,
|
|
246
|
+
queryFn: () => port.read(),
|
|
247
|
+
});
|
|
248
|
+
const status = view.status;
|
|
249
|
+
const focusSetup = useCallback((section) => {
|
|
250
|
+
if (!focus || status === "pending")
|
|
251
|
+
return;
|
|
252
|
+
section
|
|
253
|
+
?.querySelector("[aria-label='Add model key'] select, [aria-label='Add model key'] input")
|
|
254
|
+
?.focus();
|
|
255
|
+
}, [focus, status]);
|
|
256
|
+
const refresh = () => client.invalidateQueries({ queryKey: vaultQueryKey });
|
|
257
|
+
const notice = useMemo(() => ({
|
|
258
|
+
error: (message) => {
|
|
259
|
+
toast.error(message, { toasterId });
|
|
260
|
+
},
|
|
261
|
+
done: (message) => {
|
|
262
|
+
toast.success(message, { toasterId });
|
|
263
|
+
},
|
|
264
|
+
}), [toasterId]);
|
|
265
|
+
const data = view.data;
|
|
266
|
+
return (_jsxs("section", { ref: focusSetup, className: "tourtois-settings-stack tourtois-vault", "aria-label": "Vault", children: [heading ? (_jsxs(_Fragment, { children: [_jsx(SectionHeading, { children: "Vault" }), _jsx("p", { className: "tourtois-settings-lede", children: vaultLede })] })) : null, view.isPending ? (_jsx("p", { role: "status", className: "tourtois-settings-empty", children: "Loading the Vault\u2026" })) : null, view.isError ? (_jsxs("div", { role: "alert", className: "tourtois-settings-notice", children: [_jsx(Status, { glyph: "dot", tone: "error", size: "compact" }), _jsx("p", { children: "The Vault could not be loaded." }), _jsx(Button, { size: "sm", variant: "outline", onClick: () => void view.refetch(), children: "Retry" })] })) : null, data !== undefined ? (_jsxs(_Fragment, { children: [data.coverage.state === "incomplete" &&
|
|
267
|
+
data.coverage.missing.length > 0 ? (_jsxs("div", { role: "status", className: "tourtois-vault-coverage", children: [_jsx(Status, { glyph: "dot", tone: "warning", size: "compact" }), _jsxs("p", { children: [plural(data.coverage.missing.length, "value was", "values were"), " ", "stored before transcripts were scrubbed. Replace", " ", data.coverage.missing.length === 1 ? "it" : "each one", " to protect it."] })] })) : null, _jsx(ModelKeys, { port: port, setup: focus, view: data, notice: notice, refresh: refresh }), port.agentModels !== undefined ? (_jsx(AgentModels, { port: port.agentModels, view: data, notice: notice })) : null, _jsx(Secrets, { port: port, view: data, notice: notice, refresh: refresh }), _jsx(ConnectionCredentials, { port: port, view: data, notice: notice, refresh: refresh })] })) : null] }));
|
|
268
|
+
}
|
|
269
|
+
const unprotected = (view, kind, id) => view.coverage.state === "incomplete" &&
|
|
270
|
+
view.coverage.missing.some((item) => item.kind === kind && item.id === id);
|
|
271
|
+
function UnprotectedMark() {
|
|
272
|
+
return (_jsx(Status, { appearance: "chip", glyph: "dot", tone: "warning", size: "compact", label: "Replace to protect" }));
|
|
273
|
+
}
|
|
274
|
+
function ModelKeys({ port, setup, view, notice, refresh, }) {
|
|
275
|
+
const [open, setOpen] = useState(null);
|
|
276
|
+
const [busy, setBusy] = useState(false);
|
|
277
|
+
const may = (scope) => scope === "team" ? view.role.manageTeam : view.role.managePersonal;
|
|
278
|
+
const scopes = view.scopes.filter(may);
|
|
279
|
+
// A fresh form after each stored key, so the key field empties.
|
|
280
|
+
const [formKey, setFormKey] = useState(0);
|
|
281
|
+
const toggle = (keyId, panel) => {
|
|
282
|
+
setOpen((current) => current?.keyId === keyId && current.panel === panel
|
|
283
|
+
? null
|
|
284
|
+
: { keyId, panel });
|
|
285
|
+
};
|
|
286
|
+
return (_jsxs("section", { className: "tourtois-settings-stack", "aria-label": "Model keys", children: [_jsx(SectionHead, { title: "Model keys", note: "The provider keys your Agents chat on. Each is checked with its provider when you add it." }), _jsxs(DataList, { density: "compact", "aria-label": "Model keys", children: [view.modelKeys.length === 0 ? (_jsx(DataRow, { className: "tourtois-settings-empty", children: "No model keys yet. Agents use the default model until you add one." })) : null, view.modelKeys.map((key) => {
|
|
287
|
+
const manage = may(key.scope);
|
|
288
|
+
const panel = open?.keyId === key.keyId ? open.panel : null;
|
|
289
|
+
return (_jsx(DataRow, { children: _jsxs("div", { className: "tourtois-vault-item", children: [_jsx(SettingsItem, { title: key.label, detail: _jsxs(_Fragment, { children: [_jsxs("p", { children: [providerLabel(view.providers, key.provider), view.scopes.length > 1
|
|
290
|
+
? key.scope === "team"
|
|
291
|
+
? " · Team"
|
|
292
|
+
: " · Just you"
|
|
293
|
+
: "", " · Added ", dateText(key.createdAt)] }), key.baseUrl !== null ? (_jsx("p", { className: "tourtois-vault-mono", children: key.baseUrl })) : null, unprotected(view, "model-key", key.keyId) ? (_jsx("p", { children: _jsx(UnprotectedMark, {}) })) : null] }), actions: _jsxs(_Fragment, { children: [manage && port.modelKeys.rotate !== undefined ? (_jsx(KeycapButton, { density: "compact", label: "Rotate", "aria-label": `Rotate ${key.label}`, "aria-expanded": panel === "rotate", disabled: busy, onClick: () => {
|
|
294
|
+
toggle(key.keyId, "rotate");
|
|
295
|
+
} })) : null, port.modelKeys.usage !== undefined ? (_jsx(KeycapButton, { density: "compact", label: "Usage", "aria-label": `Usage of ${key.label}`, "aria-expanded": panel === "usage", onClick: () => {
|
|
296
|
+
toggle(key.keyId, "usage");
|
|
297
|
+
} })) : null, manage ? (_jsx(KeycapButton, { density: "compact", tone: "destructive", label: "Remove", "aria-label": `Remove ${key.label}`, "aria-expanded": panel === "revoke", disabled: busy, onClick: () => {
|
|
298
|
+
toggle(key.keyId, "revoke");
|
|
299
|
+
} })) : null] }) }), panel === "rotate" && port.modelKeys.rotate !== undefined ? (_jsx(RotatePanel, { name: key.label, hint: "Agents that run on this key keep it. The old value stops working here at once.", onCancel: () => {
|
|
300
|
+
setOpen(null);
|
|
301
|
+
}, onRotate: async (value) => {
|
|
302
|
+
await port.modelKeys.rotate?.(key.keyId, value);
|
|
303
|
+
setOpen(null);
|
|
304
|
+
notice.done(`${key.label} now uses the new value.`);
|
|
305
|
+
await refresh();
|
|
306
|
+
}, notice: notice })) : null, panel === "usage" && port.modelKeys.usage !== undefined ? (_jsx(UsagePanel, { name: key.label, queryKey: [...vaultQueryKey, "usage", "model", key.keyId], read: () => port.modelKeys.usage?.(key.keyId) ?? Promise.resolve([]), view: view })) : null, panel === "revoke" ? (_jsx(RevokePanel, { name: key.label, action: "Remove", consequence: "Agents that run on this key stop replying until they get another.", disabled: busy, onCancel: () => {
|
|
307
|
+
setOpen(null);
|
|
308
|
+
}, onConfirm: async () => {
|
|
309
|
+
setBusy(true);
|
|
310
|
+
try {
|
|
311
|
+
await port.modelKeys.remove(key.keyId);
|
|
312
|
+
setOpen(null);
|
|
313
|
+
notice.done(`${key.label} was removed.`);
|
|
314
|
+
await refresh();
|
|
315
|
+
}
|
|
316
|
+
catch (cause) {
|
|
317
|
+
console.error("Model key removal failed", cause);
|
|
318
|
+
notice.error(failure(cause, "The model key could not be removed."));
|
|
319
|
+
}
|
|
320
|
+
finally {
|
|
321
|
+
setBusy(false);
|
|
322
|
+
}
|
|
323
|
+
} })) : null] }) }, key.keyId));
|
|
324
|
+
})] }), scopes.length > 0 ? (_jsx(AddDisclosure, { label: "Add model key", empty: view.modelKeys.length === 0, initiallyOpen: setup && formKey === 0, children: (close) => (_jsx(AddModelKey, { providers: view.providers, offered: view.scopes, scopes: scopes, notice: notice, onCancel: close, onAdd: async (draft) => {
|
|
325
|
+
await port.modelKeys.add(draft);
|
|
326
|
+
await refresh();
|
|
327
|
+
notice.done(`${draft.label} was checked and stored.`);
|
|
328
|
+
setFormKey((current) => current + 1);
|
|
329
|
+
} })) }, formKey)) : (_jsx("p", { className: "tourtois-settings-note", children: "Only Team owners and admins can add model keys here." }))] }));
|
|
330
|
+
}
|
|
331
|
+
function AddModelKey({ providers, offered, scopes, notice, onCancel, onAdd, }) {
|
|
332
|
+
const choice = (id) => providers.find((item) => item.id === id);
|
|
333
|
+
const schema = useMemo(() => z.object({
|
|
334
|
+
provider: z.string().meta(field({
|
|
335
|
+
label: "Provider",
|
|
336
|
+
kind: "select",
|
|
337
|
+
options: Object.fromEntries(providers.map((item) => [item.id, item.label])),
|
|
338
|
+
})),
|
|
339
|
+
scope: z.enum(["user", "team"]).meta(field({
|
|
340
|
+
label: "Who can use it",
|
|
341
|
+
kind: "radio",
|
|
342
|
+
options: {
|
|
343
|
+
user: {
|
|
344
|
+
label: "Just me",
|
|
345
|
+
description: "Only you and your own Agents.",
|
|
346
|
+
disabled: !scopes.includes("user"),
|
|
347
|
+
},
|
|
348
|
+
team: {
|
|
349
|
+
label: "The whole Team",
|
|
350
|
+
description: "Every Agent in this Team.",
|
|
351
|
+
disabled: !scopes.includes("team"),
|
|
352
|
+
},
|
|
353
|
+
},
|
|
354
|
+
})),
|
|
355
|
+
label: z
|
|
356
|
+
.string()
|
|
357
|
+
.trim()
|
|
358
|
+
.min(1, "Name this key")
|
|
359
|
+
.max(64)
|
|
360
|
+
.meta(field({
|
|
361
|
+
label: "Name",
|
|
362
|
+
maxLength: 64,
|
|
363
|
+
placeholder: "Work key",
|
|
364
|
+
})),
|
|
365
|
+
accountId: z
|
|
366
|
+
.string()
|
|
367
|
+
.max(32)
|
|
368
|
+
.meta(field({
|
|
369
|
+
label: "Account ID",
|
|
370
|
+
help: "Find this in your Cloudflare dashboard. The token needs Workers AI access for this account.",
|
|
371
|
+
maxLength: 32,
|
|
372
|
+
})),
|
|
373
|
+
baseUrl: z
|
|
374
|
+
.string()
|
|
375
|
+
.max(2048)
|
|
376
|
+
.meta(field({
|
|
377
|
+
label: "Base URL",
|
|
378
|
+
placeholder: "https://llm.example.com/v1",
|
|
379
|
+
})),
|
|
380
|
+
apiKey: z
|
|
381
|
+
.string()
|
|
382
|
+
.min(1, "Paste the key")
|
|
383
|
+
.max(16_384)
|
|
384
|
+
.meta(field({
|
|
385
|
+
label: "API key",
|
|
386
|
+
help: "Checked with the provider, then never shown again.",
|
|
387
|
+
render: writeOnly,
|
|
388
|
+
})),
|
|
389
|
+
}), [providers, scopes]);
|
|
390
|
+
const [pending, setPending] = useState(false);
|
|
391
|
+
const blank = {
|
|
392
|
+
provider: providers[0]?.id ?? "",
|
|
393
|
+
scope: scopes[0] ?? "user",
|
|
394
|
+
label: "",
|
|
395
|
+
accountId: "",
|
|
396
|
+
baseUrl: "",
|
|
397
|
+
apiKey: "",
|
|
398
|
+
};
|
|
399
|
+
const model = useSchemaForm({
|
|
400
|
+
schema,
|
|
401
|
+
defaultValues: blank,
|
|
402
|
+
onSubmit: async (value) => {
|
|
403
|
+
const provider = choice(value.provider);
|
|
404
|
+
if (provider?.accountId === true &&
|
|
405
|
+
!/^[a-f0-9]{32}$/u.test(value.accountId))
|
|
406
|
+
return {
|
|
407
|
+
kind: "refused",
|
|
408
|
+
fields: {
|
|
409
|
+
accountId: "Enter your 32-character Cloudflare account ID.",
|
|
410
|
+
},
|
|
411
|
+
};
|
|
412
|
+
if (provider?.baseUrl === true && !URL.canParse(value.baseUrl))
|
|
413
|
+
return {
|
|
414
|
+
kind: "refused",
|
|
415
|
+
fields: { baseUrl: "Enter the server's https:// address." },
|
|
416
|
+
};
|
|
417
|
+
setPending(true);
|
|
418
|
+
try {
|
|
419
|
+
await onAdd({
|
|
420
|
+
provider: value.provider,
|
|
421
|
+
scope: value.scope,
|
|
422
|
+
label: value.label,
|
|
423
|
+
apiKey: value.apiKey,
|
|
424
|
+
accountId: provider?.accountId === true ? value.accountId : undefined,
|
|
425
|
+
baseUrl: provider?.baseUrl === true ? value.baseUrl : undefined,
|
|
426
|
+
});
|
|
427
|
+
return { kind: "saved", values: blank };
|
|
428
|
+
}
|
|
429
|
+
catch (cause) {
|
|
430
|
+
notice.error(failure(cause, "The model key could not be stored."));
|
|
431
|
+
return { kind: "refused" };
|
|
432
|
+
}
|
|
433
|
+
finally {
|
|
434
|
+
setPending(false);
|
|
435
|
+
}
|
|
436
|
+
},
|
|
437
|
+
});
|
|
438
|
+
return (_jsx(SchemaForm, { model: model, layout: "columns", "aria-label": "Add model key", disabled: pending, footer: _jsxs(FormFooter, { divider: false, children: [onCancel === undefined ? null : (_jsx(Button, { type: "button", variant: "ghost", onClick: onCancel, children: "Cancel" })), _jsx(Button, { type: "submit", disabled: pending, children: pending ? "Checking…" : "Add model key" })] }), children: _jsx(model.form.Subscribe, { selector: (state) => state.values.provider, children: (provider) => (_jsx(FormFields, { model: model, names: [
|
|
439
|
+
"provider",
|
|
440
|
+
...(offered.length > 1 ? ["scope"] : []),
|
|
441
|
+
"label",
|
|
442
|
+
...(choice(provider)?.accountId === true
|
|
443
|
+
? ["accountId"]
|
|
444
|
+
: []),
|
|
445
|
+
...(choice(provider)?.baseUrl === true
|
|
446
|
+
? ["baseUrl"]
|
|
447
|
+
: []),
|
|
448
|
+
"apiKey",
|
|
449
|
+
], layout: "columns" })) }) }));
|
|
450
|
+
}
|
|
451
|
+
function AgentModels({ port, view, notice, }) {
|
|
452
|
+
const client = useQueryClient();
|
|
453
|
+
const queryKey = [...vaultQueryKey, "agent-models"];
|
|
454
|
+
const models = useQuery({ queryKey, queryFn: () => port.read() });
|
|
455
|
+
const keys = view.modelKeys;
|
|
456
|
+
const data = models.data;
|
|
457
|
+
return (_jsxs("section", { className: "tourtois-settings-stack", "aria-label": "Agent models", children: [_jsx(SectionHead, { title: "Agent models", note: "The model each Agent chats on, and the key it runs on." }), models.isError ? (_jsxs("div", { role: "alert", className: "tourtois-settings-notice", children: [_jsx(Status, { glyph: "dot", tone: "error", size: "compact" }), _jsx("p", { children: "Agent models could not be loaded." }), _jsx(Button, { size: "sm", variant: "outline", onClick: () => void models.refetch(), children: "Retry" })] })) : null, data?.models === "missing" ? (_jsx("p", { role: "status", className: "tourtois-settings-note", children: "No model is connected yet. The instance default cannot reply until you add a model key." })) : null, data !== undefined ? (_jsxs(_Fragment, { children: [_jsxs(DataList, { density: "compact", "aria-label": "Agent models", children: [data.agents.length === 0 ? (_jsx(DataRow, { className: "tourtois-settings-empty", children: "Create an Agent first." })) : null, data.agents.map((agent) => {
|
|
458
|
+
const selectedKey = keys.find((key) => key.keyId === agent.choice?.keyId);
|
|
459
|
+
const catalog = Object.entries(data.catalogs).find(([provider]) => provider === agent.choice?.model.providerId)?.[1];
|
|
460
|
+
const name = catalog?.find((item) => item.id === agent.choice?.model.modelId)?.name;
|
|
461
|
+
const disconnected = data.models === "missing" ||
|
|
462
|
+
(agent.choice?.keySource === "byok" &&
|
|
463
|
+
selectedKey === undefined);
|
|
464
|
+
return (_jsxs(FactRow, { label: agent.name, children: [disconnected
|
|
465
|
+
? "Not connected"
|
|
466
|
+
: agent.choice === null
|
|
467
|
+
? "Unavailable"
|
|
468
|
+
: (name ??
|
|
469
|
+
(catalog === undefined
|
|
470
|
+
? agent.choice.model.modelId
|
|
471
|
+
: "Model unavailable for tools")), disconnected || selectedKey === undefined
|
|
472
|
+
? ""
|
|
473
|
+
: ` · ${selectedKey.label}`] }, agent.agentId));
|
|
474
|
+
})] }), data.agents.length > 0 ? (_jsx(ChooseAgentModel, { agents: data.agents, keys: keys, providers: view.providers, catalogs: data.catalogs, models: data.models, onChoose: async (agentId, draft) => {
|
|
475
|
+
try {
|
|
476
|
+
await port.choose(agentId, draft);
|
|
477
|
+
await client.invalidateQueries({ queryKey });
|
|
478
|
+
notice.done("The Agent's model was saved.");
|
|
479
|
+
return true;
|
|
480
|
+
}
|
|
481
|
+
catch (cause) {
|
|
482
|
+
notice.error(failure(cause, "The model could not be saved."));
|
|
483
|
+
return false;
|
|
484
|
+
}
|
|
485
|
+
} }, keys.map((key) => key.keyId).join(":"))) : null] })) : null] }));
|
|
486
|
+
}
|
|
487
|
+
function ChooseAgentModel({ agents, keys, providers, catalogs, models, onChoose, }) {
|
|
488
|
+
const schema = useMemo(() => z.object({
|
|
489
|
+
agentId: z.string().meta(field({
|
|
490
|
+
label: "Agent",
|
|
491
|
+
kind: "select",
|
|
492
|
+
options: Object.fromEntries(agents.map((agent) => [agent.agentId, agent.name])),
|
|
493
|
+
})),
|
|
494
|
+
source: z.string(),
|
|
495
|
+
modelId: z.string(),
|
|
496
|
+
}), [agents]);
|
|
497
|
+
const [pending, setPending] = useState(false);
|
|
498
|
+
const model = useSchemaForm({
|
|
499
|
+
schema,
|
|
500
|
+
defaultValues: {
|
|
501
|
+
agentId: agents[0]?.agentId ?? "",
|
|
502
|
+
source: keys[0]?.keyId ?? defaultSource,
|
|
503
|
+
modelId: "",
|
|
504
|
+
},
|
|
505
|
+
onSubmit: async (value) => {
|
|
506
|
+
const key = keys.find((item) => item.keyId === value.source);
|
|
507
|
+
if (value.source !== defaultSource && key === undefined)
|
|
508
|
+
return { kind: "refused", fields: { source: "Choose a stored key." } };
|
|
509
|
+
const available = key === undefined ? [] : (catalogs[key.provider] ?? []);
|
|
510
|
+
const modelId = value.modelId || available.find((item) => item.tools)?.id;
|
|
511
|
+
if (key !== undefined && !available.some((item) => item.id === modelId))
|
|
512
|
+
return {
|
|
513
|
+
kind: "refused",
|
|
514
|
+
fields: { modelId: "Choose a supported model for this key." },
|
|
515
|
+
};
|
|
516
|
+
setPending(true);
|
|
517
|
+
try {
|
|
518
|
+
const saved = await onChoose(value.agentId, key === undefined
|
|
519
|
+
? { keyId: null }
|
|
520
|
+
: {
|
|
521
|
+
keyId: key.keyId,
|
|
522
|
+
provider: key.provider,
|
|
523
|
+
modelId: modelId ?? "",
|
|
524
|
+
});
|
|
525
|
+
return saved ? { kind: "saved" } : { kind: "refused" };
|
|
526
|
+
}
|
|
527
|
+
finally {
|
|
528
|
+
setPending(false);
|
|
529
|
+
}
|
|
530
|
+
},
|
|
531
|
+
});
|
|
532
|
+
return (_jsxs(SchemaForm, { model: model, layout: "columns", "aria-label": "Choose Agent model", disabled: pending, footer: _jsx(FormFooter, { divider: false, children: _jsx(Button, { type: "submit", disabled: pending, children: pending ? "Saving…" : "Save model" }) }), children: [_jsx(FormFields, { model: model, names: ["agentId"] }), _jsx(FormField, { form: model.form, name: "source", label: "Runs on", children: (source) => (_jsxs(Select, { name: "source", value: source.state.value, onChange: (event) => {
|
|
533
|
+
source.handleChange(event.target.value);
|
|
534
|
+
model.form.setFieldValue("modelId", "");
|
|
535
|
+
}, children: [_jsx("option", { value: defaultSource, children: models === "missing"
|
|
536
|
+
? "Instance default — not connected"
|
|
537
|
+
: "Instance default model" }), keys.map((key) => (_jsxs("option", { value: key.keyId, children: [key.label, " (", providerLabel(providers, key.provider), ")"] }, key.keyId)))] })) }), _jsx(model.form.Subscribe, { selector: (state) => state.values.source, children: (source) => {
|
|
538
|
+
const key = keys.find((item) => item.keyId === source);
|
|
539
|
+
if (key === undefined)
|
|
540
|
+
return null;
|
|
541
|
+
const available = catalogs[key.provider] ?? [];
|
|
542
|
+
return (_jsx(FormField, { form: model.form, name: "modelId", label: "Model", description: "Choose tools for work in Apps. Other models may not use tools. USD per million tokens: input / output; lowest rate first in each group.", children: (choice) => (_jsxs(Select, { name: "modelId", value: choice.state.value ||
|
|
543
|
+
(available.find((item) => item.tools)?.id ?? ""), onChange: (event) => {
|
|
544
|
+
choice.handleChange(event.target.value);
|
|
545
|
+
}, children: [available.length === 0 ? (_jsx("option", { value: "", children: "No supported models available" })) : null, available.length > 0 &&
|
|
546
|
+
!available.some((item) => item.tools) ? (_jsx("option", { value: "", children: "Choose a model" })) : null, [true, false].map((tools) => {
|
|
547
|
+
const group = available.filter((item) => item.tools === tools);
|
|
548
|
+
return group.length === 0 ? null : (_jsx("optgroup", { label: tools ? "Tools" : "Other models (may not use tools)", children: group.map((item) => (_jsxs("option", { value: item.id, children: [item.name, " \u00B7 ", tools ? "tools" : "chat only", " \u00B7 $", item.input, " / $", item.output] }, item.id))) }, String(tools)));
|
|
549
|
+
})] })) }));
|
|
550
|
+
} })] }));
|
|
551
|
+
}
|
|
552
|
+
function Secrets({ port, view, notice, refresh, }) {
|
|
553
|
+
const secrets = view.credentials.filter((item) => item.kind === "secret");
|
|
554
|
+
const [formKey, setFormKey] = useState(0);
|
|
555
|
+
return (_jsxs("section", { className: "tourtois-settings-stack", "aria-label": "Secrets", children: [_jsx(SectionHead, { title: "Secrets", note: "An API key or token bound to the exact sites that may receive it. It is added to a request on the way out, only for the Agents and Computers you choose." }), _jsxs(DataList, { density: "compact", "aria-label": "Secrets", children: [secrets.length === 0 ? (_jsx(DataRow, { className: "tourtois-settings-empty", children: "No secrets yet." })) : null, secrets.map((credential) => (_jsx(CredentialRow, { port: port, view: view, credential: credential, notice: notice, refresh: refresh }, credential.connectionId)))] }), view.role.manageTeam ? (_jsx(AddDisclosure, { label: "Add secret", empty: secrets.length === 0, children: (close) => (_jsx(AddSecret, { notice: notice, onCancel: close, onAdd: async (draft) => {
|
|
556
|
+
await port.addSecret(draft);
|
|
557
|
+
await refresh();
|
|
558
|
+
notice.done(`${draft.label ?? draft.name} was stored. Choose who may use it.`);
|
|
559
|
+
setFormKey((current) => current + 1);
|
|
560
|
+
} })) }, formKey)) : (_jsx("p", { className: "tourtois-settings-note", children: "Only Team owners and admins can add or change secrets." }))] }));
|
|
561
|
+
}
|
|
562
|
+
function ConnectionCredentials({ port, view, notice, refresh, }) {
|
|
563
|
+
const connections = view.credentials.filter((item) => item.kind !== "secret");
|
|
564
|
+
return (_jsxs("section", { className: "tourtois-settings-stack", "aria-label": "Connection credentials", children: [_jsx(SectionHead, { title: "Connection credentials", note: "Values your Connections signed in with or were given. Rotate or revoke them here." }), _jsxs(DataList, { density: "compact", "aria-label": "Connection credentials", children: [connections.length === 0 ? (_jsx(DataRow, { className: "tourtois-settings-empty", children: "No Connection holds a credential yet." })) : null, connections.map((credential) => (_jsx(CredentialRow, { port: port, view: view, credential: credential, notice: notice, refresh: refresh }, credential.connectionId)))] })] }));
|
|
565
|
+
}
|
|
566
|
+
const credentialText = {
|
|
567
|
+
value: "Pasted value",
|
|
568
|
+
"sign-in": "Signed in",
|
|
569
|
+
none: "No credential",
|
|
570
|
+
};
|
|
571
|
+
const stateText = {
|
|
572
|
+
active: "",
|
|
573
|
+
pending: " · Waiting for sign-in",
|
|
574
|
+
reconnect_required: " · Sign in again",
|
|
575
|
+
};
|
|
576
|
+
function CredentialRow({ port, view, credential, notice, refresh, }) {
|
|
577
|
+
const [panel, setPanel] = useState(null);
|
|
578
|
+
const [busy, setBusy] = useState(false);
|
|
579
|
+
const secret = credential.kind === "secret";
|
|
580
|
+
const manage = view.role.manageTeam;
|
|
581
|
+
const name = credential.label;
|
|
582
|
+
const toggle = (next) => {
|
|
583
|
+
setPanel((current) => (current === next ? null : next));
|
|
584
|
+
};
|
|
585
|
+
const placement = placementText(credential.placement);
|
|
586
|
+
return (_jsx(DataRow, { children: _jsxs("div", { className: "tourtois-vault-item", children: [_jsx(SettingsItem, { title: name, detail: _jsxs(_Fragment, { children: [secret ? (_jsxs(_Fragment, { children: [(credential.destinations ?? []).map((destination) => (_jsx("p", { className: "tourtois-vault-mono", children: destinationText(destination) }, `${destination.host}${destination.pathPrefix ?? ""}`))), _jsxs("p", { children: [placement === null ? "" : `${placement} · `, subjectsText(credential.subjects, false)] })] })) : (_jsxs(_Fragment, { children: [_jsx("p", { className: "tourtois-vault-mono", children: credential.origin }), _jsxs("p", { children: [credentialText[credential.credential], stateText[credential.state], " \u00B7", " ", subjectsText(credential.subjects, true)] })] })), unprotected(view, "connection", credential.connectionId) ? (_jsx("p", { children: _jsx(UnprotectedMark, {}) })) : null] }), actions: _jsxs(_Fragment, { children: [manage && credential.credential === "value" ? (_jsx(KeycapButton, { density: "compact", label: "Rotate", "aria-label": `Rotate ${name}`, "aria-expanded": panel === "rotate", disabled: busy, onClick: () => {
|
|
587
|
+
toggle("rotate");
|
|
588
|
+
} })) : null, secret ? (_jsx(KeycapButton, { density: "compact", label: "Scope", "aria-label": `Scope ${name}`, "aria-expanded": panel === "scope", disabled: busy, onClick: () => {
|
|
589
|
+
toggle("scope");
|
|
590
|
+
} })) : null, _jsx(KeycapButton, { density: "compact", label: "Usage", "aria-label": `Usage of ${name}`, "aria-expanded": panel === "usage", onClick: () => {
|
|
591
|
+
toggle("usage");
|
|
592
|
+
} }), manage && (secret || credential.credential !== "none") ? (_jsx(KeycapButton, { density: "compact", tone: "destructive", label: "Revoke", "aria-label": `Revoke ${name}`, "aria-expanded": panel === "revoke", disabled: busy, onClick: () => {
|
|
593
|
+
toggle("revoke");
|
|
594
|
+
} })) : null] }) }), panel === "rotate" ? (_jsx(RotatePanel, { name: name, hint: secret
|
|
595
|
+
? "Who may use it and where it is sent stay the same. The old value stops working here at once."
|
|
596
|
+
: "The Connection and its Agents stay the same. The old value stops working here at once.", notice: notice, onCancel: () => {
|
|
597
|
+
setPanel(null);
|
|
598
|
+
}, onRotate: async (value) => {
|
|
599
|
+
await port.updateCredential(credential.connectionId, {
|
|
600
|
+
expectedVersion: credential.version,
|
|
601
|
+
secret: value,
|
|
602
|
+
});
|
|
603
|
+
setPanel(null);
|
|
604
|
+
notice.done(`${name} now uses the new value.`);
|
|
605
|
+
await refresh();
|
|
606
|
+
} })) : null, panel === "scope" && secret ? (_jsx(ScopePanel, { port: port, view: view, credential: credential, manage: manage, notice: notice, refresh: refresh })) : null, panel === "usage" ? (_jsx(UsagePanel, { name: name, queryKey: [...vaultQueryKey, "usage", credential.connectionId], read: () => port.credentialUsage(credential.connectionId), view: view })) : null, panel === "revoke" ? (_jsx(RevokePanel, { name: name, action: "Revoke", consequence: secret
|
|
607
|
+
? "It is deleted. The next request that needs it is refused, with no restart."
|
|
608
|
+
: "The Connection stays, without its value, until you add a new one.", disabled: busy, onCancel: () => {
|
|
609
|
+
setPanel(null);
|
|
610
|
+
}, onConfirm: async () => {
|
|
611
|
+
setBusy(true);
|
|
612
|
+
try {
|
|
613
|
+
await port.revokeCredential(credential.connectionId, credential.version);
|
|
614
|
+
setPanel(null);
|
|
615
|
+
notice.done(`${name} was revoked.`);
|
|
616
|
+
await refresh();
|
|
617
|
+
}
|
|
618
|
+
catch (cause) {
|
|
619
|
+
console.error("Vault revoke failed", cause);
|
|
620
|
+
notice.error(failure(cause, `${name} could not be revoked.`));
|
|
621
|
+
}
|
|
622
|
+
finally {
|
|
623
|
+
setBusy(false);
|
|
624
|
+
}
|
|
625
|
+
} })) : null] }) }));
|
|
626
|
+
}
|
|
627
|
+
/** An inline panel under one row: its own heading and quiet frame. */
|
|
628
|
+
function RowPanel({ label, children }) {
|
|
629
|
+
return (_jsx("section", { className: "tourtois-vault-panel", "aria-label": label, children: children }));
|
|
630
|
+
}
|
|
631
|
+
function RotatePanel({ name, hint, notice, onCancel, onRotate, }) {
|
|
632
|
+
const schema = useMemo(() => z.object({
|
|
633
|
+
value: z
|
|
634
|
+
.string()
|
|
635
|
+
.min(1, "Paste the new value")
|
|
636
|
+
.max(16_384)
|
|
637
|
+
.meta(field({
|
|
638
|
+
label: "New value",
|
|
639
|
+
help: hint,
|
|
640
|
+
render: writeOnly,
|
|
641
|
+
})),
|
|
642
|
+
}), [hint]);
|
|
643
|
+
const [pending, setPending] = useState(false);
|
|
644
|
+
const model = useSchemaForm({
|
|
645
|
+
schema,
|
|
646
|
+
defaultValues: { value: "" },
|
|
647
|
+
onSubmit: async ({ value }) => {
|
|
648
|
+
setPending(true);
|
|
649
|
+
try {
|
|
650
|
+
await onRotate(value);
|
|
651
|
+
return { kind: "saved", values: { value: "" } };
|
|
652
|
+
}
|
|
653
|
+
catch (cause) {
|
|
654
|
+
notice.error(failure(cause, `${name} could not be replaced.`));
|
|
655
|
+
return { kind: "refused" };
|
|
656
|
+
}
|
|
657
|
+
finally {
|
|
658
|
+
setPending(false);
|
|
659
|
+
}
|
|
660
|
+
},
|
|
661
|
+
});
|
|
662
|
+
return (_jsx(RowPanel, { label: `Rotating ${name}`, children: _jsx(SchemaForm, { model: model, "aria-label": `New value for ${name}`, disabled: pending, footer: _jsxs(FormFooter, { divider: false, children: [_jsx(Button, { type: "button", variant: "ghost", size: "sm", onClick: onCancel, children: "Cancel" }), _jsx(Button, { type: "submit", size: "sm", disabled: pending, children: pending ? "Replacing…" : "Replace value" })] }) }) }));
|
|
663
|
+
}
|
|
664
|
+
function RevokePanel({ name, action, consequence, disabled, onCancel, onConfirm, }) {
|
|
665
|
+
return (_jsxs(RowPanel, { label: `${action === "Revoke" ? "Revoking" : "Removing"} ${name}`, children: [_jsxs("p", { className: "tourtois-vault-panel-copy", children: [_jsxs("strong", { children: [action, " ", name, "?"] }), " ", consequence] }), _jsxs(FormFooter, { divider: false, children: [_jsx(Button, { type: "button", variant: "ghost", size: "sm", onClick: onCancel, children: "Keep it" }), _jsx(Button, { type: "button", variant: "destructive", size: "sm", disabled: disabled, onClick: () => void onConfirm(), children: disabled
|
|
666
|
+
? `${action === "Revoke" ? "Revoking" : "Removing"}…`
|
|
667
|
+
: action })] })] }));
|
|
668
|
+
}
|
|
669
|
+
const outcomeText = {
|
|
670
|
+
placed: "Sent",
|
|
671
|
+
upstream_status: "Answered",
|
|
672
|
+
refused_destination: "Refused: wrong site",
|
|
673
|
+
refused_grant: "Refused: not allowed",
|
|
674
|
+
failed: "No answer",
|
|
675
|
+
};
|
|
676
|
+
const outcomeTone = {
|
|
677
|
+
placed: "success",
|
|
678
|
+
upstream_status: "success",
|
|
679
|
+
refused_destination: "error",
|
|
680
|
+
refused_grant: "error",
|
|
681
|
+
failed: "warning",
|
|
682
|
+
};
|
|
683
|
+
function UsagePanel({ name, queryKey, read, view, }) {
|
|
684
|
+
const usage = useQuery({ queryKey, queryFn: read, retry: false });
|
|
685
|
+
const subjectName = (subject) => {
|
|
686
|
+
if (subject === null)
|
|
687
|
+
return "Unknown caller";
|
|
688
|
+
const list = subject.kind === "agent"
|
|
689
|
+
? view.subjects.agents
|
|
690
|
+
: subject.kind === "computer"
|
|
691
|
+
? view.subjects.computers
|
|
692
|
+
: [];
|
|
693
|
+
const listed = list.find((item) => item.id === subject.id)?.name;
|
|
694
|
+
return (listed ??
|
|
695
|
+
(subject.kind === "team-key"
|
|
696
|
+
? "A Team key"
|
|
697
|
+
: subject.kind === "computer"
|
|
698
|
+
? "A Computer"
|
|
699
|
+
: "An Agent"));
|
|
700
|
+
};
|
|
701
|
+
return (_jsxs(RowPanel, { label: `History of ${name}`, children: [usage.isPending ? (_jsx("p", { role: "status", className: "tourtois-settings-empty", children: "Loading uses\u2026" })) : null, usage.isError ? (_jsxs("div", { role: "alert", className: "tourtois-settings-notice", children: [_jsx(Status, { glyph: "dot", tone: "error", size: "compact" }), _jsx("p", { children: "Uses could not be loaded." }), _jsx(Button, { size: "sm", variant: "outline", onClick: () => void usage.refetch(), children: "Retry" })] })) : null, usage.data?.length === 0 ? (_jsx("p", { className: "tourtois-settings-empty", children: "Not used yet." })) : null, usage.data !== undefined && usage.data.length > 0 ? (_jsx("ul", { className: "tourtois-vault-usage", "aria-label": `Uses of ${name}`, children: usage.data.map((use) => (_jsxs("li", { className: "tourtois-vault-use", children: [_jsx(Status, { glyph: "dot", size: "compact", tone: outcomeTone[use.outcome], label: outcomeText[use.outcome] +
|
|
702
|
+
(use.status === null ? "" : ` ${String(use.status)}`) }), _jsx("span", { className: "tourtois-vault-use-who", children: subjectName(use.subject) }), _jsx("span", { className: "tourtois-vault-use-where tourtois-vault-mono", children: [use.method, use.host].filter(Boolean).join(" ") }), _jsxs("span", { className: "tourtois-vault-use-when", children: [use.count > 1 ? `${String(use.count)}× · ` : "", timeText(use.lastAt)] })] }, `${String(use.lastAt)}:${use.subject?.kind ?? ""}:${use.subject?.id ?? ""}:${use.outcome}:${use.host ?? ""}:${use.method ?? ""}:${String(use.status)}`))) })) : null, _jsx("p", { className: "tourtois-vault-panel-copy", children: "Each use is kept for 90 days: who, when, the site and the outcome. Never the value, the full path or the reply." })] }));
|
|
703
|
+
}
|
|
704
|
+
function ScopePanel({ port, view, credential, manage, notice, refresh, }) {
|
|
705
|
+
const [pending, setPending] = useState(null);
|
|
706
|
+
const granted = (kind, id) => credential.subjects.some((subject) => subject.kind === kind && subject.id === id);
|
|
707
|
+
const change = async (kind, id, on) => {
|
|
708
|
+
const subject = { kind, id };
|
|
709
|
+
setPending(`${kind}:${id}`);
|
|
710
|
+
try {
|
|
711
|
+
await (on
|
|
712
|
+
? port.grant(credential.connectionId, subject)
|
|
713
|
+
: port.ungrant(credential.connectionId, subject));
|
|
714
|
+
await refresh();
|
|
715
|
+
}
|
|
716
|
+
catch (cause) {
|
|
717
|
+
console.error("Vault grant change failed", cause);
|
|
718
|
+
notice.error(failure(cause, "Who may use it could not be changed."));
|
|
719
|
+
}
|
|
720
|
+
finally {
|
|
721
|
+
setPending(null);
|
|
722
|
+
}
|
|
723
|
+
};
|
|
724
|
+
const group = (kind, legend, items) => (_jsx(FieldSet, { legend: legend, disabled: !manage, children: items.length === 0 ? (_jsx("p", { className: "tourtois-settings-empty", children: kind === "agent" ? "No Agents yet." : "No Computers yet." })) : (_jsx("div", { className: "tourtois-vault-choices", children: items.map((item) => (_jsx(Choice, { label: item.name, checked: granted(kind, item.id), disabled: !manage || pending !== null, onChange: (event) => {
|
|
725
|
+
void change(kind, item.id, event.currentTarget.checked);
|
|
726
|
+
} }, item.id))) })) }));
|
|
727
|
+
return (_jsx(RowPanel, { label: `Scope of ${credential.label}`, children: _jsxs("div", { className: "tourtois-vault-scope", children: [group("agent", "Agents", view.subjects.agents), group("computer", "Computers", view.subjects.computers), _jsx("p", { className: "tourtois-vault-panel-copy", children: "A grant to an Agent never reaches the visitors its channels talk to." }), _jsx(EditDestinations, { initial: credential.destinations ?? [], disabled: !manage, onSave: async (destinations) => {
|
|
728
|
+
try {
|
|
729
|
+
await port.updateCredential(credential.connectionId, {
|
|
730
|
+
expectedVersion: credential.version,
|
|
731
|
+
destinations,
|
|
732
|
+
});
|
|
733
|
+
notice.done(`${credential.label} now goes only to these sites.`);
|
|
734
|
+
await refresh();
|
|
735
|
+
}
|
|
736
|
+
catch (cause) {
|
|
737
|
+
console.error("Vault sites change failed", cause);
|
|
738
|
+
notice.error(failure(cause, "The sites could not be saved."));
|
|
739
|
+
}
|
|
740
|
+
}, notice: notice }, credential.version)] }) }));
|
|
741
|
+
}
|
|
742
|
+
let draftSerial = 0;
|
|
743
|
+
const draftKey = () => {
|
|
744
|
+
draftSerial += 1;
|
|
745
|
+
return `site-${String(draftSerial)}`;
|
|
746
|
+
};
|
|
747
|
+
const blankDestination = () => ({
|
|
748
|
+
key: draftKey(),
|
|
749
|
+
host: "",
|
|
750
|
+
pathPrefix: "",
|
|
751
|
+
methods: ["GET", "POST"],
|
|
752
|
+
});
|
|
753
|
+
const draftOf = (destination) => ({
|
|
754
|
+
key: draftKey(),
|
|
755
|
+
host: destination.host,
|
|
756
|
+
pathPrefix: destination.pathPrefix ?? "",
|
|
757
|
+
methods: [...destination.methods],
|
|
758
|
+
});
|
|
759
|
+
const refused = (reason) => ({ kind: "refused", reason });
|
|
760
|
+
/** Exact hosts only: the owner's sites, checked again by the server. */
|
|
761
|
+
function destinationsFrom(drafts) {
|
|
762
|
+
if (drafts.length === 0)
|
|
763
|
+
return refused("Bind it to at least one site.");
|
|
764
|
+
const out = [];
|
|
765
|
+
for (const draft of drafts) {
|
|
766
|
+
const host = draft.host.trim().toLowerCase();
|
|
767
|
+
if (host.length === 0)
|
|
768
|
+
return refused("Enter each site's host, like api.stripe.com.");
|
|
769
|
+
if (host.includes("*") || host.includes("/") || host.includes(":"))
|
|
770
|
+
return refused("Enter an exact host, like api.stripe.com, with no wildcard, scheme or port.");
|
|
771
|
+
if (draft.methods.length === 0)
|
|
772
|
+
return refused(`Choose at least one method for ${host}.`);
|
|
773
|
+
const pathPrefix = draft.pathPrefix.trim();
|
|
774
|
+
if (pathPrefix.length > 0 && !pathPrefix.startsWith("/"))
|
|
775
|
+
return refused(`Start the path for ${host} with /.`);
|
|
776
|
+
const destination = { host, methods: draft.methods };
|
|
777
|
+
if (pathPrefix.length > 0)
|
|
778
|
+
destination.pathPrefix = pathPrefix;
|
|
779
|
+
out.push(destination);
|
|
780
|
+
}
|
|
781
|
+
return { kind: "sites", destinations: out };
|
|
782
|
+
}
|
|
783
|
+
function DestinationsEditor({ value, onChange, disabled, }) {
|
|
784
|
+
const update = (index, patch) => {
|
|
785
|
+
onChange(value.map((draft, at) => (at === index ? { ...draft, ...patch } : draft)));
|
|
786
|
+
};
|
|
787
|
+
return (_jsxs("div", { className: "tourtois-vault-destinations", children: [value.map((draft, index) => {
|
|
788
|
+
const which = value.length > 1 ? ` ${String(index + 1)}` : "";
|
|
789
|
+
return (_jsxs(FieldSet, { legend: `Site${which}`, disabled: disabled, className: "tourtois-vault-destination", children: [_jsx(Field, { label: "Host", description: "Exact host, like api.stripe.com.", children: _jsx(Input, { value: draft.host, autoComplete: "off", spellCheck: false, placeholder: "api.example.com", onChange: (event) => {
|
|
790
|
+
update(index, { host: event.currentTarget.value });
|
|
791
|
+
} }) }), _jsx(Field, { label: "Path", optional: true, description: "Only paths that start with this.", children: _jsx(Input, { value: draft.pathPrefix, autoComplete: "off", spellCheck: false, placeholder: "/v1/", onChange: (event) => {
|
|
792
|
+
update(index, { pathPrefix: event.currentTarget.value });
|
|
793
|
+
} }) }), _jsx("div", { role: "group", "aria-label": `Methods for site${which}`, className: "tourtois-vault-choices tourtois-vault-methods", children: methods.map((method) => (_jsx(Choice, { label: method, checked: draft.methods.includes(method), onChange: (event) => {
|
|
794
|
+
const on = event.currentTarget.checked;
|
|
795
|
+
update(index, {
|
|
796
|
+
methods: methods.filter((item) => item === method ? on : draft.methods.includes(item)),
|
|
797
|
+
});
|
|
798
|
+
} }, method))) }), value.length > 1 ? (_jsx("div", { children: _jsxs(Button, { type: "button", size: "sm", variant: "ghost", onClick: () => {
|
|
799
|
+
onChange(value.filter((_, at) => at !== index));
|
|
800
|
+
}, children: ["Remove site", which] }) })) : null] }, draft.key));
|
|
801
|
+
}), value.length < destinationLimit ? (_jsx("div", { children: _jsx(Button, { type: "button", size: "sm", variant: "outline", disabled: disabled, onClick: () => {
|
|
802
|
+
onChange([...value, blankDestination()]);
|
|
803
|
+
}, children: "Add another site" }) })) : null] }));
|
|
804
|
+
}
|
|
805
|
+
function EditDestinations({ initial, disabled, notice, onSave, }) {
|
|
806
|
+
const [drafts, setDrafts] = useState(() => initial.length === 0 ? [blankDestination()] : initial.map(draftOf));
|
|
807
|
+
const [pending, setPending] = useState(false);
|
|
808
|
+
return (_jsxs("form", { className: "tourtois-vault-sites", "aria-label": "Sites it may be sent to", onSubmit: (event) => {
|
|
809
|
+
event.preventDefault();
|
|
810
|
+
const sites = destinationsFrom(drafts);
|
|
811
|
+
if (sites.kind === "refused") {
|
|
812
|
+
notice.error(sites.reason);
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
setPending(true);
|
|
816
|
+
void onSave(sites.destinations).finally(() => {
|
|
817
|
+
setPending(false);
|
|
818
|
+
});
|
|
819
|
+
}, children: [_jsx(DestinationsEditor, { value: drafts, onChange: setDrafts, disabled: disabled || pending }), disabled ? null : (_jsx(FormFooter, { divider: false, children: _jsx(Button, { type: "submit", size: "sm", disabled: pending, children: pending ? "Saving…" : "Save sites" }) }))] }));
|
|
820
|
+
}
|
|
821
|
+
const placementKinds = {
|
|
822
|
+
bearer: "Authorization: Bearer",
|
|
823
|
+
basic: "Basic sign-in (user:password)",
|
|
824
|
+
header: "A named header",
|
|
825
|
+
query: "A query parameter",
|
|
826
|
+
};
|
|
827
|
+
function AddSecret({ notice, onCancel, onAdd, }) {
|
|
828
|
+
const schema = useMemo(() => z.object({
|
|
829
|
+
label: z
|
|
830
|
+
.string()
|
|
831
|
+
.trim()
|
|
832
|
+
.min(1, "Name this secret")
|
|
833
|
+
.max(128)
|
|
834
|
+
.meta(field({
|
|
835
|
+
label: "Name",
|
|
836
|
+
placeholder: "Stripe",
|
|
837
|
+
maxLength: 128,
|
|
838
|
+
})),
|
|
839
|
+
name: z
|
|
840
|
+
.string()
|
|
841
|
+
.regex(/^[a-z][a-z0-9_]{0,31}$/u, "Lowercase letters, digits and _, starting with a letter")
|
|
842
|
+
.meta(field({
|
|
843
|
+
label: "Reference",
|
|
844
|
+
placeholder: "stripe",
|
|
845
|
+
maxLength: 32,
|
|
846
|
+
help: "How commands refer to it. Lowercase letters, digits and _.",
|
|
847
|
+
})),
|
|
848
|
+
placement: z.enum(["bearer", "basic", "header", "query"]).meta(field({
|
|
849
|
+
label: "Sent as",
|
|
850
|
+
kind: "select",
|
|
851
|
+
options: placementKinds,
|
|
852
|
+
})),
|
|
853
|
+
field: z
|
|
854
|
+
.string()
|
|
855
|
+
.max(64)
|
|
856
|
+
.meta(field({
|
|
857
|
+
label: "Header or parameter",
|
|
858
|
+
placeholder: "X-Api-Key",
|
|
859
|
+
maxLength: 64,
|
|
860
|
+
})),
|
|
861
|
+
secret: z
|
|
862
|
+
.string()
|
|
863
|
+
.min(1, "Paste the value")
|
|
864
|
+
.max(16_384)
|
|
865
|
+
.meta(field({
|
|
866
|
+
label: "Value",
|
|
867
|
+
help: "Stored sealed and never shown again.",
|
|
868
|
+
render: writeOnly,
|
|
869
|
+
})),
|
|
870
|
+
}), []);
|
|
871
|
+
const [drafts, setDrafts] = useState(() => [
|
|
872
|
+
blankDestination(),
|
|
873
|
+
]);
|
|
874
|
+
const [pending, setPending] = useState(false);
|
|
875
|
+
const blank = {
|
|
876
|
+
label: "",
|
|
877
|
+
name: "",
|
|
878
|
+
placement: "bearer",
|
|
879
|
+
field: "",
|
|
880
|
+
secret: "",
|
|
881
|
+
};
|
|
882
|
+
const model = useSchemaForm({
|
|
883
|
+
schema,
|
|
884
|
+
defaultValues: blank,
|
|
885
|
+
onSubmit: async (value) => {
|
|
886
|
+
const sites = destinationsFrom(drafts);
|
|
887
|
+
if (sites.kind === "refused") {
|
|
888
|
+
notice.error(sites.reason);
|
|
889
|
+
return { kind: "refused" };
|
|
890
|
+
}
|
|
891
|
+
const destinations = sites.destinations;
|
|
892
|
+
const named = value.placement === "header" || value.placement === "query";
|
|
893
|
+
if (named && !/^[A-Za-z0-9_.-]{1,64}$/u.test(value.field))
|
|
894
|
+
return {
|
|
895
|
+
kind: "refused",
|
|
896
|
+
fields: { field: "Name the header or parameter it goes in." },
|
|
897
|
+
};
|
|
898
|
+
const placement = value.placement === "header"
|
|
899
|
+
? { header: value.field }
|
|
900
|
+
: value.placement === "query"
|
|
901
|
+
? { query: value.field }
|
|
902
|
+
: value.placement;
|
|
903
|
+
setPending(true);
|
|
904
|
+
try {
|
|
905
|
+
await onAdd({
|
|
906
|
+
name: value.name,
|
|
907
|
+
label: value.label,
|
|
908
|
+
destinations,
|
|
909
|
+
placement,
|
|
910
|
+
secret: value.secret,
|
|
911
|
+
});
|
|
912
|
+
return { kind: "saved", values: blank };
|
|
913
|
+
}
|
|
914
|
+
catch (cause) {
|
|
915
|
+
notice.error(failure(cause, "The secret could not be stored."));
|
|
916
|
+
return { kind: "refused" };
|
|
917
|
+
}
|
|
918
|
+
finally {
|
|
919
|
+
setPending(false);
|
|
920
|
+
}
|
|
921
|
+
},
|
|
922
|
+
});
|
|
923
|
+
return (_jsxs(SchemaForm, { model: model, layout: "columns", "aria-label": "Add secret", disabled: pending, footer: _jsxs(FormFooter, { divider: false, children: [onCancel === undefined ? null : (_jsx(Button, { type: "button", variant: "ghost", onClick: onCancel, children: "Cancel" })), _jsx(Button, { type: "submit", disabled: pending, children: pending ? "Storing…" : "Add secret" })] }), children: [_jsx(FormFields, { model: model, names: ["label", "name"], layout: "columns" }), _jsx(DestinationsEditor, { value: drafts, onChange: setDrafts, disabled: pending }), _jsx(model.form.Subscribe, { selector: (state) => state.values.placement, children: (placement) => (_jsx(FormFields, { model: model, names: [
|
|
924
|
+
"placement",
|
|
925
|
+
...(placement === "header" || placement === "query"
|
|
926
|
+
? ["field"]
|
|
927
|
+
: []),
|
|
928
|
+
"secret",
|
|
929
|
+
], layout: "columns" })) })] }));
|
|
930
|
+
}
|