@fungi.computer/tourtois 0.6.0 → 0.8.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/Connections.d.ts +94 -0
- package/dist/Connections.js +303 -0
- package/dist/ModelKeys.d.ts +68 -0
- package/dist/ModelKeys.js +200 -0
- package/dist/SystemSettings.js +4 -5
- package/dist/WebsiteChannels.d.ts +131 -0
- package/dist/WebsiteChannels.js +372 -0
- package/dist/oyster-platform-controls.js +97 -14
- package/dist/platform-controls.d.ts +1 -1
- package/dist/platform-controls.js +1 -1
- package/dist/queries.d.ts +3 -0
- package/dist/queries.js +6 -0
- package/dist/settings-navigation.css +7 -0
- package/dist/settings-sections.d.ts +6 -8
- package/dist/settings-sections.js +2 -2
- package/package.json +15 -11
- package/dist/AgentMcpServers.d.ts +0 -34
- package/dist/AgentMcpServers.js +0 -127
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { type ComponentType } from "react";
|
|
2
|
+
import type { ConnectorsTarget, Navigate } from "./settings-sections.js";
|
|
3
|
+
import * as z from "zod";
|
|
4
|
+
/** What an Agent may do with one connection: everything, only reads, or named tools. */
|
|
5
|
+
declare const connectionAccessSchema: z.ZodUnion<readonly [z.ZodLiteral<"all">, z.ZodLiteral<"read">, z.ZodArray<z.ZodString>]>;
|
|
6
|
+
export type ConnectionAccess = z.output<typeof connectionAccessSchema>;
|
|
7
|
+
/** The connections an owner holds, with who may use each. No secret ever appears. */
|
|
8
|
+
export declare const connectionsViewSchema: z.ZodObject<{
|
|
9
|
+
callbackUrl: z.ZodNullable<z.ZodString>;
|
|
10
|
+
agents: z.ZodArray<z.ZodObject<{
|
|
11
|
+
agentId: z.ZodString;
|
|
12
|
+
name: z.ZodString;
|
|
13
|
+
}, z.core.$strict>>;
|
|
14
|
+
connections: z.ZodArray<z.ZodObject<{
|
|
15
|
+
connectionId: z.ZodString;
|
|
16
|
+
name: z.ZodString;
|
|
17
|
+
label: z.ZodString;
|
|
18
|
+
kind: z.ZodEnum<{
|
|
19
|
+
mcp: "mcp";
|
|
20
|
+
openapi: "openapi";
|
|
21
|
+
}>;
|
|
22
|
+
url: z.ZodString;
|
|
23
|
+
auth: z.ZodEnum<{
|
|
24
|
+
none: "none";
|
|
25
|
+
token: "token";
|
|
26
|
+
oauth: "oauth";
|
|
27
|
+
}>;
|
|
28
|
+
state: z.ZodEnum<{
|
|
29
|
+
pending: "pending";
|
|
30
|
+
connected: "connected";
|
|
31
|
+
reconnect_required: "reconnect_required";
|
|
32
|
+
}>;
|
|
33
|
+
grants: z.ZodArray<z.ZodObject<{
|
|
34
|
+
agentId: z.ZodString;
|
|
35
|
+
access: z.ZodUnion<readonly [z.ZodLiteral<"all">, z.ZodLiteral<"read">, z.ZodArray<z.ZodString>]>;
|
|
36
|
+
}, z.core.$strict>>;
|
|
37
|
+
}, z.core.$strict>>;
|
|
38
|
+
}, z.core.$strict>;
|
|
39
|
+
export type ConnectionsView = z.output<typeof connectionsViewSchema>;
|
|
40
|
+
export type ConnectionRecord = ConnectionsView["connections"][number];
|
|
41
|
+
/** What the owner submits to connect one MCP server. */
|
|
42
|
+
export type ConnectionDraft = Readonly<{
|
|
43
|
+
name: string;
|
|
44
|
+
url: string;
|
|
45
|
+
bearer: string | null;
|
|
46
|
+
/** Sign in at the server instead of pasting a token; the client is optional. */
|
|
47
|
+
oauth?: Readonly<{
|
|
48
|
+
clientId?: string | undefined;
|
|
49
|
+
clientSecret?: string | undefined;
|
|
50
|
+
}>;
|
|
51
|
+
}>;
|
|
52
|
+
/** Where to send the owner to finish signing in, when the server asked for it. */
|
|
53
|
+
export declare const connectionAddedSchema: z.ZodObject<{
|
|
54
|
+
authorizeUrl: z.ZodNullable<z.ZodString>;
|
|
55
|
+
}, z.core.$strip>;
|
|
56
|
+
export declare const connectionToolsSchema: z.ZodObject<{
|
|
57
|
+
tools: z.ZodArray<z.ZodString>;
|
|
58
|
+
}, z.core.$strict>;
|
|
59
|
+
/**
|
|
60
|
+
* The one Settings port for connections, on every host: who holds what, who
|
|
61
|
+
* may use it, and the owner's controls. A host with kinds of its own mounts
|
|
62
|
+
* them beside the shared list through `hostSections`.
|
|
63
|
+
*/
|
|
64
|
+
export type ConnectionsPort = Readonly<{
|
|
65
|
+
list(): Promise<ConnectionsView>;
|
|
66
|
+
add(draft: ConnectionDraft): Promise<z.output<typeof connectionAddedSchema>>;
|
|
67
|
+
/** Give one Agent access, or change what it has. */
|
|
68
|
+
grant(input: {
|
|
69
|
+
connectionId: string;
|
|
70
|
+
agentId: string;
|
|
71
|
+
access: ConnectionAccess;
|
|
72
|
+
}): Promise<void>;
|
|
73
|
+
ungrant(connectionId: string, agentId: string): Promise<void>;
|
|
74
|
+
remove(connectionId: string): Promise<void>;
|
|
75
|
+
/** The tool names an MCP server offers, for choosing a narrower grant. */
|
|
76
|
+
tools(connectionId: string): Promise<readonly string[]>;
|
|
77
|
+
/** Where the host edits a connection it has its own editor for, or null. */
|
|
78
|
+
editor?(connection: ConnectionRecord): ConnectorsTarget | null;
|
|
79
|
+
/** The host's own connector editors (Apps, Git, importing a spec), shown beside the list. */
|
|
80
|
+
hostSections?: ComponentType<{
|
|
81
|
+
target: ConnectorsTarget;
|
|
82
|
+
onNavigate: Navigate;
|
|
83
|
+
}>;
|
|
84
|
+
}>;
|
|
85
|
+
/**
|
|
86
|
+
* Connections settings: what this instance reaches outside, which Agents may
|
|
87
|
+
* use it, and how much. Secrets stay with the owner, never with an Agent.
|
|
88
|
+
*/
|
|
89
|
+
export declare function Connections({ port, target, onNavigate, }: {
|
|
90
|
+
port: ConnectionsPort;
|
|
91
|
+
target: ConnectorsTarget;
|
|
92
|
+
onNavigate: Navigate;
|
|
93
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
94
|
+
export {};
|
|
@@ -0,0 +1,303 @@
|
|
|
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 { FieldSet, FormFooter } from "@fungi.computer/caps/components/field";
|
|
6
|
+
import { CheckKey, CheckKeys, } from "@fungi.computer/caps/components/icon-checkbox";
|
|
7
|
+
import { KeycapButton } from "@fungi.computer/caps/components/keycap";
|
|
8
|
+
import { SchemaForm, field, useSchemaForm } from "@fungi.computer/caps/form";
|
|
9
|
+
import { Sigil } from "@fungi.computer/sigils";
|
|
10
|
+
import { queryOptions, useMutation, useQuery, useQueryClient, } from "@tanstack/react-query";
|
|
11
|
+
import { useState } from "react";
|
|
12
|
+
import * as z from "zod";
|
|
13
|
+
import { SettingsItem } from "./settings-item.js";
|
|
14
|
+
/** What an Agent may do with one connection: everything, only reads, or named tools. */
|
|
15
|
+
const connectionAccessSchema = z.union([
|
|
16
|
+
z.literal("all"),
|
|
17
|
+
z.literal("read"),
|
|
18
|
+
z.array(z.string().min(1).max(256)).max(256),
|
|
19
|
+
]);
|
|
20
|
+
/** The connections an owner holds, with who may use each. No secret ever appears. */
|
|
21
|
+
export const connectionsViewSchema = z.strictObject({
|
|
22
|
+
/** The redirect URL to register when bringing your own OAuth app; null when the host has no sign-in. */
|
|
23
|
+
callbackUrl: z.string().min(1).nullable(),
|
|
24
|
+
agents: z.array(z.strictObject({ agentId: z.string().min(1), name: z.string() })),
|
|
25
|
+
connections: z.array(z.strictObject({
|
|
26
|
+
connectionId: z.string().min(1),
|
|
27
|
+
name: z.string().min(1),
|
|
28
|
+
label: z.string().min(1),
|
|
29
|
+
kind: z.enum(["mcp", "openapi"]),
|
|
30
|
+
url: z.string().min(1),
|
|
31
|
+
auth: z.enum(["none", "token", "oauth"]),
|
|
32
|
+
state: z.enum(["connected", "pending", "reconnect_required"]),
|
|
33
|
+
grants: z.array(z.strictObject({
|
|
34
|
+
agentId: z.string().min(1),
|
|
35
|
+
access: connectionAccessSchema,
|
|
36
|
+
})),
|
|
37
|
+
})),
|
|
38
|
+
});
|
|
39
|
+
/** Where to send the owner to finish signing in, when the server asked for it. */
|
|
40
|
+
export const connectionAddedSchema = z.object({
|
|
41
|
+
authorizeUrl: z.string().nullable(),
|
|
42
|
+
});
|
|
43
|
+
export const connectionToolsSchema = z.strictObject({
|
|
44
|
+
tools: z.array(z.string().min(1)).max(1_024),
|
|
45
|
+
});
|
|
46
|
+
const queryKey = ["tourtois", "connections"];
|
|
47
|
+
const toolsQuery = (port, connectionId) => queryOptions({
|
|
48
|
+
queryKey: [...queryKey, "tools", connectionId],
|
|
49
|
+
queryFn: () => port.tools(connectionId),
|
|
50
|
+
});
|
|
51
|
+
const authText = {
|
|
52
|
+
none: "No token",
|
|
53
|
+
token: "Token sealed",
|
|
54
|
+
oauth: "Signed in",
|
|
55
|
+
};
|
|
56
|
+
const stateText = {
|
|
57
|
+
connected: "",
|
|
58
|
+
pending: " · Waiting for sign-in",
|
|
59
|
+
reconnect_required: " · Sign in again",
|
|
60
|
+
};
|
|
61
|
+
const accessText = (access) => access === "all"
|
|
62
|
+
? "All tools"
|
|
63
|
+
: access === "read"
|
|
64
|
+
? "Read only"
|
|
65
|
+
: `${String(access.length)} selected`;
|
|
66
|
+
/**
|
|
67
|
+
* Connections settings: what this instance reaches outside, which Agents may
|
|
68
|
+
* use it, and how much. Secrets stay with the owner, never with an Agent.
|
|
69
|
+
*/
|
|
70
|
+
export function Connections({ port, target, onNavigate, }) {
|
|
71
|
+
const client = useQueryClient();
|
|
72
|
+
const view = useQuery({ queryKey, queryFn: () => port.list() });
|
|
73
|
+
const refresh = () => client.invalidateQueries({ queryKey });
|
|
74
|
+
const remove = useMutation({
|
|
75
|
+
mutationFn: (connectionId) => port.remove(connectionId),
|
|
76
|
+
onSuccess: refresh,
|
|
77
|
+
});
|
|
78
|
+
const agents = view.data?.agents ?? [];
|
|
79
|
+
const connections = view.data?.connections ?? [];
|
|
80
|
+
// A fresh form after each connection, so the token field empties.
|
|
81
|
+
const [formKey, setFormKey] = useState(0);
|
|
82
|
+
const HostSections = port.hostSections;
|
|
83
|
+
return (_jsxs("section", { className: "tourtois-settings-content tourtois-connectors", "aria-label": "Connections", children: [_jsx(SectionHeading, { children: "Connections" }), _jsx("p", { className: "tourtois-settings-lede", children: "Connect a remote MCP server, then choose which Agents may use it. A token is sealed by this host, never given to an Agent, and never shown again." }), view.data?.callbackUrl ? (_jsx(DataList, { density: "compact", "aria-label": "Redirect URL", children: _jsx(FactRow, { icon: _jsx(Sigil, { name: "internet" }), label: "Redirect URL", children: _jsx("code", { className: "text-[length:inherit]", children: view.data.callbackUrl }) }) })) : null, view.isPending ? _jsx("p", { children: "Loading connections\u2026" }) : null, view.isError ? (_jsxs("div", { role: "status", className: "tourtois-settings-notice", children: [_jsx("p", { children: "Connections could not be loaded." }), _jsx(Button, { size: "sm", variant: "outline", onClick: () => void view.refetch(), children: "Retry" })] })) : null, view.isSuccess ? (_jsxs(_Fragment, { children: [connections.length === 0 ? (_jsx("p", { children: "No connections yet." })) : (_jsx(DataList, { "aria-label": "Connections", children: connections.map((connection) => (_jsx(DataRow, { children: _jsx(SettingsItem, { icon: _jsx(Sigil, { name: "connected" }), title: connection.label, detail: _jsxs(_Fragment, { children: [_jsxs("p", { children: [connection.url, " \u00B7 ", authText[connection.auth], stateText[connection.state]] }), _jsx(AgentAccess, { connection: connection, agents: agents, port: port, onChanged: refresh }), _jsxs("div", { className: "tourtois-settings-item-keys mt-2", children: [port.editor?.(connection) ? (_jsx(KeycapButton, { density: "compact", label: "Edit", "aria-label": `Edit ${connection.label}`, onClick: () => {
|
|
84
|
+
const target = port.editor?.(connection);
|
|
85
|
+
if (target)
|
|
86
|
+
onNavigate({
|
|
87
|
+
category: "connectors",
|
|
88
|
+
target,
|
|
89
|
+
});
|
|
90
|
+
} })) : null, _jsx(KeycapButton, { density: "compact", tone: "destructive", label: "Remove", "aria-label": `Remove ${connection.label}`, disabled: remove.isPending, onClick: () => {
|
|
91
|
+
remove.mutate(connection.connectionId);
|
|
92
|
+
} })] })] }) }) }, connection.connectionId))) })), remove.isError ? (_jsx("p", { role: "alert", children: "The connection could not be removed." })) : null, _jsx(AddConnection, { signIn: view.data.callbackUrl !== null, onAdd: async (draft) => {
|
|
93
|
+
const added = await port.add(draft);
|
|
94
|
+
// OAuth servers finish at the authorization server, then return here.
|
|
95
|
+
if (added.authorizeUrl !== null) {
|
|
96
|
+
window.location.assign(added.authorizeUrl);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
await refresh();
|
|
100
|
+
setFormKey((current) => current + 1);
|
|
101
|
+
} }, formKey)] })) : null, HostSections ? (_jsx(HostSections, { target: target, onNavigate: onNavigate })) : null] }));
|
|
102
|
+
}
|
|
103
|
+
/** Who may use one connection, and how much; each change takes effect on the Agent's next call. */
|
|
104
|
+
function AgentAccess({ connection, agents, port, onChanged, }) {
|
|
105
|
+
const change = useMutation({
|
|
106
|
+
mutationFn: async (next) => {
|
|
107
|
+
if (next.access === null)
|
|
108
|
+
await port.ungrant(connection.connectionId, next.agentId);
|
|
109
|
+
else
|
|
110
|
+
await port.grant({
|
|
111
|
+
connectionId: connection.connectionId,
|
|
112
|
+
agentId: next.agentId,
|
|
113
|
+
access: next.access,
|
|
114
|
+
});
|
|
115
|
+
},
|
|
116
|
+
onSuccess: onChanged,
|
|
117
|
+
});
|
|
118
|
+
if (agents.length === 0)
|
|
119
|
+
return _jsx("p", { children: "Create an Agent to share this." });
|
|
120
|
+
const grantFor = (agentId) => connection.grants.find((grant) => grant.agentId === agentId);
|
|
121
|
+
return (_jsxs(FieldSet, { legend: `Agents using ${connection.label}`, icon: _jsx(Sigil, { name: "agent" }), disabled: change.isPending || connection.state !== "connected", children: [_jsx(CheckKeys, { children: agents.map((agent) => (_jsx(CheckKey, { name: `${connection.connectionId}:${agent.agentId}`, label: agent.name, icon: _jsx(Sigil, { name: "agent" }), hint: grantFor(agent.agentId) === undefined
|
|
122
|
+
? "No access"
|
|
123
|
+
: accessText(grantFor(agent.agentId)?.access ?? "all"), checked: grantFor(agent.agentId) !== undefined, onChange: (event) => {
|
|
124
|
+
change.mutate({
|
|
125
|
+
agentId: agent.agentId,
|
|
126
|
+
access: event.currentTarget.checked ? "all" : null,
|
|
127
|
+
});
|
|
128
|
+
} }, agent.agentId))) }), agents.flatMap((agent) => {
|
|
129
|
+
const grant = grantFor(agent.agentId);
|
|
130
|
+
return grant === undefined
|
|
131
|
+
? []
|
|
132
|
+
: [
|
|
133
|
+
_jsx(ChooseAccess, { connection: connection, agentName: agent.name, access: grant.access, port: port, onChoose: (access) => {
|
|
134
|
+
change.mutate({ agentId: agent.agentId, access });
|
|
135
|
+
} }, agent.agentId),
|
|
136
|
+
];
|
|
137
|
+
}), change.isError ? (_jsx("p", { role: "alert", children: "The access could not be changed." })) : null] }));
|
|
138
|
+
}
|
|
139
|
+
/** Narrow one Agent's access: everything, reads only, or the tools you pick. */
|
|
140
|
+
function ChooseAccess({ connection, agentName, access, port, onChoose, }) {
|
|
141
|
+
const selecting = Array.isArray(access);
|
|
142
|
+
const tools = useQuery({
|
|
143
|
+
...toolsQuery(port, connection.connectionId),
|
|
144
|
+
enabled: selecting && connection.kind === "mcp",
|
|
145
|
+
});
|
|
146
|
+
const modes = connection.kind === "mcp"
|
|
147
|
+
? [
|
|
148
|
+
["all", "All tools"],
|
|
149
|
+
["pick", "Selected tools"],
|
|
150
|
+
]
|
|
151
|
+
: [
|
|
152
|
+
["all", "All operations"],
|
|
153
|
+
["read", "Read only"],
|
|
154
|
+
];
|
|
155
|
+
const mode = selecting ? "pick" : access;
|
|
156
|
+
return (_jsxs("div", { className: "tourtois-connection-access", children: [_jsx(FactRow, { icon: _jsx(Sigil, { name: "agent" }), label: agentName, children: _jsxs("label", { children: [_jsx("span", { className: "sr-only", children: `Access for ${agentName}` }), _jsx("select", { "aria-label": `Access for ${agentName}`, value: mode, onChange: (event) => {
|
|
157
|
+
const next = event.currentTarget.value;
|
|
158
|
+
onChoose(next === "pick" ? [] : next === "read" ? "read" : "all");
|
|
159
|
+
}, children: modes.map(([value, text]) => (_jsx("option", { value: value, children: text }, value))) })] }) }), selecting ? (tools.isPending ? (_jsx("p", { children: "Loading tools\u2026" })) : tools.isError ? (_jsx("p", { role: "alert", children: "The tools could not be listed." })) : (_jsx(CheckKeys, { children: tools.data.map((tool) => (_jsx(CheckKey, { name: `${connection.connectionId}:${agentName}:${tool}`, label: tool, icon: _jsx(Sigil, { name: "write" }), checked: access.includes(tool), onChange: (event) => {
|
|
160
|
+
onChoose(event.currentTarget.checked
|
|
161
|
+
? [...access, tool]
|
|
162
|
+
: access.filter((name) => name !== tool));
|
|
163
|
+
} }, tool))) }))) : null] }));
|
|
164
|
+
}
|
|
165
|
+
const nameField = z
|
|
166
|
+
.string()
|
|
167
|
+
.regex(/^[a-z][a-z0-9_]{0,31}$/u, "Lowercase letters, digits and _, starting with a letter")
|
|
168
|
+
.meta(field({
|
|
169
|
+
label: "Name",
|
|
170
|
+
icon: _jsx(Sigil, { name: "write" }),
|
|
171
|
+
help: "The Agent calls its tools as env.mcp.<name>.",
|
|
172
|
+
maxLength: 32,
|
|
173
|
+
placeholder: "linear",
|
|
174
|
+
}));
|
|
175
|
+
const urlField = z
|
|
176
|
+
.string()
|
|
177
|
+
.max(2_048)
|
|
178
|
+
.refine((value) => URL.canParse(value) && new URL(value).protocol === "https:", "Use the server's https:// address")
|
|
179
|
+
.meta(field({
|
|
180
|
+
label: "Server URL",
|
|
181
|
+
icon: _jsx(Sigil, { name: "internet" }),
|
|
182
|
+
kind: "url",
|
|
183
|
+
placeholder: "https://mcp.example.com/mcp",
|
|
184
|
+
}));
|
|
185
|
+
const bearerField = z
|
|
186
|
+
.string()
|
|
187
|
+
.max(4_096)
|
|
188
|
+
.meta(field({
|
|
189
|
+
label: "Bearer token",
|
|
190
|
+
icon: _jsx(Sigil, { name: "restricted" }),
|
|
191
|
+
help: "Optional: only if the server asks for one.",
|
|
192
|
+
kind: "secret",
|
|
193
|
+
}));
|
|
194
|
+
/** Add a connection; a host with no sign-in page offers a token or none. */
|
|
195
|
+
function AddConnection({ onAdd, signIn, }) {
|
|
196
|
+
return signIn ? (_jsx(AddWithSignIn, { onAdd: onAdd })) : (_jsx(AddWithToken, { onAdd: onAdd }));
|
|
197
|
+
}
|
|
198
|
+
function AddWithToken({ onAdd, }) {
|
|
199
|
+
const schema = z.object({
|
|
200
|
+
name: nameField,
|
|
201
|
+
url: urlField,
|
|
202
|
+
bearer: bearerField,
|
|
203
|
+
});
|
|
204
|
+
const add = useMutation({
|
|
205
|
+
mutationFn: (draft) => onAdd(draft),
|
|
206
|
+
});
|
|
207
|
+
const model = useSchemaForm({
|
|
208
|
+
schema,
|
|
209
|
+
defaultValues: { name: "", url: "", bearer: "" },
|
|
210
|
+
onSubmit: async (value) => {
|
|
211
|
+
try {
|
|
212
|
+
await add.mutateAsync({
|
|
213
|
+
name: value.name,
|
|
214
|
+
url: value.url,
|
|
215
|
+
bearer: value.bearer.length === 0 ? null : value.bearer,
|
|
216
|
+
});
|
|
217
|
+
return { kind: "saved" };
|
|
218
|
+
}
|
|
219
|
+
catch (cause) {
|
|
220
|
+
return {
|
|
221
|
+
kind: "refused",
|
|
222
|
+
error: cause instanceof Error
|
|
223
|
+
? cause.message
|
|
224
|
+
: "The connection could not be made.",
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
return (_jsx(SchemaForm, { model: model, layout: "columns", "aria-label": "Add connection", disabled: add.isPending, footer: _jsx(FormFooter, { divider: false, error: model.error, children: _jsx(Button, { type: "submit", children: add.isPending ? "Connecting…" : "Add connection" }) }) }));
|
|
230
|
+
}
|
|
231
|
+
function AddWithSignIn({ onAdd, }) {
|
|
232
|
+
const schema = z.object({
|
|
233
|
+
name: nameField,
|
|
234
|
+
url: urlField,
|
|
235
|
+
bearer: bearerField,
|
|
236
|
+
signIn: z.string().meta(field({
|
|
237
|
+
label: "Authorization",
|
|
238
|
+
icon: _jsx(Sigil, { name: "restricted" }),
|
|
239
|
+
kind: "select",
|
|
240
|
+
options: {
|
|
241
|
+
token: "Bearer token, or none",
|
|
242
|
+
oauth: "Sign in with OAuth",
|
|
243
|
+
},
|
|
244
|
+
})),
|
|
245
|
+
clientId: z
|
|
246
|
+
.string()
|
|
247
|
+
.max(512)
|
|
248
|
+
.meta(field({
|
|
249
|
+
label: "Client ID",
|
|
250
|
+
icon: _jsx(Sigil, { name: "write" }),
|
|
251
|
+
help: "Optional: your own OAuth app. Otherwise the server registers one.",
|
|
252
|
+
})),
|
|
253
|
+
clientSecret: z
|
|
254
|
+
.string()
|
|
255
|
+
.max(4_096)
|
|
256
|
+
.meta(field({
|
|
257
|
+
label: "Client secret",
|
|
258
|
+
icon: _jsx(Sigil, { name: "restricted" }),
|
|
259
|
+
help: "Optional: sealed by this host.",
|
|
260
|
+
kind: "secret",
|
|
261
|
+
})),
|
|
262
|
+
});
|
|
263
|
+
const add = useMutation({
|
|
264
|
+
mutationFn: (draft) => onAdd(draft),
|
|
265
|
+
});
|
|
266
|
+
const model = useSchemaForm({
|
|
267
|
+
schema,
|
|
268
|
+
defaultValues: {
|
|
269
|
+
name: "",
|
|
270
|
+
url: "",
|
|
271
|
+
bearer: "",
|
|
272
|
+
signIn: "token",
|
|
273
|
+
clientId: "",
|
|
274
|
+
clientSecret: "",
|
|
275
|
+
},
|
|
276
|
+
onSubmit: async (value) => {
|
|
277
|
+
try {
|
|
278
|
+
const oauth = {
|
|
279
|
+
clientId: value.clientId.length > 0 ? value.clientId : undefined,
|
|
280
|
+
clientSecret: value.clientSecret.length > 0 ? value.clientSecret : undefined,
|
|
281
|
+
};
|
|
282
|
+
const draft = value.signIn === "oauth"
|
|
283
|
+
? { name: value.name, url: value.url, bearer: null, oauth }
|
|
284
|
+
: {
|
|
285
|
+
name: value.name,
|
|
286
|
+
url: value.url,
|
|
287
|
+
bearer: value.bearer.length === 0 ? null : value.bearer,
|
|
288
|
+
};
|
|
289
|
+
await add.mutateAsync(draft);
|
|
290
|
+
return { kind: "saved" };
|
|
291
|
+
}
|
|
292
|
+
catch (cause) {
|
|
293
|
+
return {
|
|
294
|
+
kind: "refused",
|
|
295
|
+
error: cause instanceof Error
|
|
296
|
+
? cause.message
|
|
297
|
+
: "The connection could not be made.",
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
});
|
|
302
|
+
return (_jsx(SchemaForm, { model: model, layout: "columns", "aria-label": "Add connection", disabled: add.isPending, footer: _jsx(FormFooter, { divider: false, error: model.error, children: _jsx(Button, { type: "submit", children: add.isPending ? "Connecting…" : "Add connection" }) }) }));
|
|
303
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
declare const providers: {
|
|
3
|
+
readonly anthropic: "Anthropic";
|
|
4
|
+
readonly openai: "OpenAI";
|
|
5
|
+
readonly openrouter: "OpenRouter";
|
|
6
|
+
};
|
|
7
|
+
export type Provider = keyof typeof providers;
|
|
8
|
+
/** Every stored key and every Agent with its persisted model choice. */
|
|
9
|
+
export declare const modelKeysViewSchema: z.ZodObject<{
|
|
10
|
+
keys: z.ZodArray<z.ZodObject<{
|
|
11
|
+
keyId: z.ZodString;
|
|
12
|
+
provider: z.ZodEnum<{
|
|
13
|
+
anthropic: "anthropic";
|
|
14
|
+
openai: "openai";
|
|
15
|
+
openrouter: "openrouter";
|
|
16
|
+
}>;
|
|
17
|
+
label: z.ZodString;
|
|
18
|
+
createdAt: z.ZodNumber;
|
|
19
|
+
validatedAt: z.ZodNumber;
|
|
20
|
+
}, z.core.$strict>>;
|
|
21
|
+
agents: z.ZodArray<z.ZodObject<{
|
|
22
|
+
agentId: z.ZodString;
|
|
23
|
+
name: z.ZodString;
|
|
24
|
+
choice: z.ZodNullable<z.ZodObject<{
|
|
25
|
+
keySource: z.ZodEnum<{
|
|
26
|
+
"workers-ai": "workers-ai";
|
|
27
|
+
"ai-gateway": "ai-gateway";
|
|
28
|
+
byok: "byok";
|
|
29
|
+
}>;
|
|
30
|
+
keyId: z.ZodOptional<z.ZodString>;
|
|
31
|
+
model: z.ZodObject<{
|
|
32
|
+
providerId: z.ZodString;
|
|
33
|
+
modelId: z.ZodString;
|
|
34
|
+
reasoning: z.ZodOptional<z.ZodString>;
|
|
35
|
+
}, z.core.$strict>;
|
|
36
|
+
}, z.core.$strict>>;
|
|
37
|
+
}, z.core.$strict>>;
|
|
38
|
+
}, z.core.$strict>;
|
|
39
|
+
export type ModelKeysView = z.output<typeof modelKeysViewSchema>;
|
|
40
|
+
/** What the owner submits to store one key. */
|
|
41
|
+
export type ModelKeyDraft = Readonly<{
|
|
42
|
+
provider: Provider;
|
|
43
|
+
label: string;
|
|
44
|
+
apiKey: string;
|
|
45
|
+
}>;
|
|
46
|
+
/** The Agent's next model: the instance default, or a stored key's provider. */
|
|
47
|
+
export type AgentModelChoiceDraft = Readonly<{
|
|
48
|
+
keyId: null;
|
|
49
|
+
}> | Readonly<{
|
|
50
|
+
keyId: string;
|
|
51
|
+
provider: Provider;
|
|
52
|
+
modelId: string;
|
|
53
|
+
}>;
|
|
54
|
+
/** The host's owner-direct model-key custody and per-Agent model choice. */
|
|
55
|
+
export type ModelKeysPort = Readonly<{
|
|
56
|
+
read(): Promise<ModelKeysView>;
|
|
57
|
+
addKey(draft: ModelKeyDraft): Promise<void>;
|
|
58
|
+
removeKey(keyId: string): Promise<void>;
|
|
59
|
+
chooseModel(agentId: string, draft: AgentModelChoiceDraft): Promise<void>;
|
|
60
|
+
}>;
|
|
61
|
+
/**
|
|
62
|
+
* Model keys on a standalone host: store a provider API key once, then pick
|
|
63
|
+
* which model and key each Agent runs on. A key is never shown again.
|
|
64
|
+
*/
|
|
65
|
+
export declare function ModelKeys({ port }: {
|
|
66
|
+
port: ModelKeysPort;
|
|
67
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
68
|
+
export {};
|
|
@@ -0,0 +1,200 @@
|
|
|
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, DataListHeading, DataRow, FactRow, } from "@fungi.computer/caps/components/data-list";
|
|
5
|
+
import { FormFooter } from "@fungi.computer/caps/components/field";
|
|
6
|
+
import { KeycapButton } from "@fungi.computer/caps/components/keycap";
|
|
7
|
+
import { SchemaForm, field, useSchemaForm } from "@fungi.computer/caps/form";
|
|
8
|
+
import { Sigil } from "@fungi.computer/sigils";
|
|
9
|
+
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
10
|
+
import { useMemo, useState } from "react";
|
|
11
|
+
import * as z from "zod";
|
|
12
|
+
import { SettingsItem } from "./settings-item.js";
|
|
13
|
+
const providers = {
|
|
14
|
+
anthropic: "Anthropic",
|
|
15
|
+
openai: "OpenAI",
|
|
16
|
+
openrouter: "OpenRouter",
|
|
17
|
+
};
|
|
18
|
+
const providerSchema = z.enum(["anthropic", "openai", "openrouter"]);
|
|
19
|
+
/** One stored key as the host lists it; the secret never comes back. */
|
|
20
|
+
const modelKeySchema = z.strictObject({
|
|
21
|
+
keyId: z.string().min(1),
|
|
22
|
+
provider: providerSchema,
|
|
23
|
+
label: z.string().min(1),
|
|
24
|
+
createdAt: z.number(),
|
|
25
|
+
validatedAt: z.number(),
|
|
26
|
+
});
|
|
27
|
+
const agentChoiceSchema = z.strictObject({
|
|
28
|
+
keySource: z.enum(["workers-ai", "ai-gateway", "byok"]),
|
|
29
|
+
keyId: z.string().min(1).optional(),
|
|
30
|
+
model: z.strictObject({
|
|
31
|
+
providerId: z.string().min(1),
|
|
32
|
+
modelId: z.string().min(1),
|
|
33
|
+
reasoning: z.string().optional(),
|
|
34
|
+
}),
|
|
35
|
+
});
|
|
36
|
+
/** Every stored key and every Agent with its persisted model choice. */
|
|
37
|
+
export const modelKeysViewSchema = z.strictObject({
|
|
38
|
+
keys: z.array(modelKeySchema),
|
|
39
|
+
agents: z.array(z.strictObject({
|
|
40
|
+
agentId: z.string().min(1),
|
|
41
|
+
name: z.string(),
|
|
42
|
+
choice: agentChoiceSchema.nullable(),
|
|
43
|
+
})),
|
|
44
|
+
});
|
|
45
|
+
const queryKey = ["tourtois", "model-keys"];
|
|
46
|
+
const defaultSource = "default";
|
|
47
|
+
/**
|
|
48
|
+
* Model keys on a standalone host: store a provider API key once, then pick
|
|
49
|
+
* which model and key each Agent runs on. A key is never shown again.
|
|
50
|
+
*/
|
|
51
|
+
export function ModelKeys({ port }) {
|
|
52
|
+
const client = useQueryClient();
|
|
53
|
+
const view = useQuery({ queryKey, queryFn: () => port.read() });
|
|
54
|
+
const refresh = () => client.invalidateQueries({ queryKey });
|
|
55
|
+
const remove = useMutation({
|
|
56
|
+
mutationFn: (keyId) => port.removeKey(keyId),
|
|
57
|
+
onSuccess: refresh,
|
|
58
|
+
});
|
|
59
|
+
const keys = view.data?.keys ?? [];
|
|
60
|
+
const agents = view.data?.agents ?? [];
|
|
61
|
+
// A fresh form after each stored key, so the key field empties.
|
|
62
|
+
const [formKey, setFormKey] = useState(0);
|
|
63
|
+
return (_jsxs("section", { className: "tourtois-settings-content tourtois-model-keys", "aria-label": "Model keys", children: [_jsx(SectionHeading, { children: "Model keys" }), _jsx("p", { className: "tourtois-settings-lede", children: "Bring your own provider API key. It is checked with the provider, sealed on this instance and never shown again. Each Agent can run on any key." }), view.isPending ? _jsx("p", { children: "Loading model keys\u2026" }) : null, view.isError ? (_jsxs("div", { role: "status", className: "tourtois-settings-notice", children: [_jsx("p", { children: "Model keys could not be loaded." }), _jsx(Button, { size: "sm", variant: "outline", onClick: () => void view.refetch(), children: "Retry" })] })) : null, view.isSuccess ? (_jsxs(_Fragment, { children: [_jsxs(DataList, { "aria-label": "Model keys", children: [keys.length === 0 ? (_jsx(DataRow, { className: "tourtois-settings-empty", children: "No model keys yet." })) : null, keys.map((key) => (_jsx(DataRow, { children: _jsx(SettingsItem, { icon: _jsx(Sigil, { name: "restricted" }), title: key.label, detail: _jsx("p", { children: providers[key.provider] }), actions: _jsx(KeycapButton, { density: "compact", tone: "destructive", label: "Remove", "aria-label": `Remove ${key.label}`, disabled: remove.isPending, onClick: () => {
|
|
64
|
+
remove.mutate(key.keyId);
|
|
65
|
+
} }) }) }, key.keyId)))] }), remove.isError ? (_jsx("p", { role: "alert", children: "The model key could not be removed." })) : null, _jsx(AddModelKey, { onAdd: async (draft) => {
|
|
66
|
+
await port.addKey(draft);
|
|
67
|
+
await refresh();
|
|
68
|
+
setFormKey((current) => current + 1);
|
|
69
|
+
} }, formKey), _jsx(SectionHeading, { children: "Agent models" }), _jsxs(DataList, { density: "compact", "aria-label": "Agent models", children: [_jsx(DataListHeading, { children: "Current choice" }), agents.length === 0 ? (_jsx(DataRow, { className: "tourtois-settings-empty", children: "Create an Agent first." })) : null, agents.map((agent) => {
|
|
70
|
+
const keyLabel = agent.choice?.keyId === undefined
|
|
71
|
+
? null
|
|
72
|
+
: (keys.find((key) => key.keyId === agent.choice?.keyId)
|
|
73
|
+
?.label ?? "Removed key");
|
|
74
|
+
return (_jsxs(FactRow, { icon: _jsx(Sigil, { name: "agent" }), label: agent.name, children: [agent.choice?.model.modelId ?? "Unavailable", keyLabel === null ? "" : ` · ${keyLabel}`] }, agent.agentId));
|
|
75
|
+
})] }), agents.length > 0 ? (_jsx(ChooseAgentModel, { agents: agents, keys: keys, onChoose: async (agentId, draft) => {
|
|
76
|
+
await port.chooseModel(agentId, draft);
|
|
77
|
+
await refresh();
|
|
78
|
+
} })) : null] })) : null] }));
|
|
79
|
+
}
|
|
80
|
+
function AddModelKey({ onAdd, }) {
|
|
81
|
+
const schema = useMemo(() => z.object({
|
|
82
|
+
provider: providerSchema.meta(field({
|
|
83
|
+
label: "Provider",
|
|
84
|
+
icon: _jsx(Sigil, { name: "internet" }),
|
|
85
|
+
kind: "select",
|
|
86
|
+
options: providers,
|
|
87
|
+
})),
|
|
88
|
+
label: z
|
|
89
|
+
.string()
|
|
90
|
+
.min(1, "Name this key")
|
|
91
|
+
.max(64)
|
|
92
|
+
.meta(field({
|
|
93
|
+
label: "Name",
|
|
94
|
+
icon: _jsx(Sigil, { name: "write" }),
|
|
95
|
+
maxLength: 64,
|
|
96
|
+
placeholder: "Work key",
|
|
97
|
+
})),
|
|
98
|
+
apiKey: z
|
|
99
|
+
.string()
|
|
100
|
+
.min(1, "Paste the key")
|
|
101
|
+
.max(16_384)
|
|
102
|
+
.meta(field({
|
|
103
|
+
label: "API key",
|
|
104
|
+
icon: _jsx(Sigil, { name: "restricted" }),
|
|
105
|
+
help: "Sealed on this instance and never shown again.",
|
|
106
|
+
kind: "secret",
|
|
107
|
+
})),
|
|
108
|
+
}), []);
|
|
109
|
+
const add = useMutation({ mutationFn: onAdd });
|
|
110
|
+
const model = useSchemaForm({
|
|
111
|
+
schema,
|
|
112
|
+
defaultValues: { provider: "anthropic", label: "", apiKey: "" },
|
|
113
|
+
onSubmit: async (value) => {
|
|
114
|
+
try {
|
|
115
|
+
await add.mutateAsync(value);
|
|
116
|
+
return { kind: "saved" };
|
|
117
|
+
}
|
|
118
|
+
catch (cause) {
|
|
119
|
+
return {
|
|
120
|
+
kind: "refused",
|
|
121
|
+
error: cause instanceof Error
|
|
122
|
+
? cause.message
|
|
123
|
+
: "The model key could not be stored.",
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
return (_jsx(SchemaForm, { model: model, layout: "columns", "aria-label": "Add model key", disabled: add.isPending, footer: _jsx(FormFooter, { divider: false, error: model.error, children: _jsx(Button, { type: "submit", children: add.isPending ? "Checking…" : "Add model key" }) }) }));
|
|
129
|
+
}
|
|
130
|
+
function ChooseAgentModel({ agents, keys, onChoose, }) {
|
|
131
|
+
const schema = useMemo(() => z.object({
|
|
132
|
+
agentId: z.string().meta(field({
|
|
133
|
+
label: "Agent",
|
|
134
|
+
icon: _jsx(Sigil, { name: "agent" }),
|
|
135
|
+
kind: "select",
|
|
136
|
+
options: Object.fromEntries(agents.map((agent) => [agent.agentId, agent.name])),
|
|
137
|
+
})),
|
|
138
|
+
source: z.string().meta(field({
|
|
139
|
+
label: "Runs on",
|
|
140
|
+
icon: _jsx(Sigil, { name: "restricted" }),
|
|
141
|
+
kind: "select",
|
|
142
|
+
options: {
|
|
143
|
+
[defaultSource]: "Instance default model",
|
|
144
|
+
...Object.fromEntries(keys.map((key) => [
|
|
145
|
+
key.keyId,
|
|
146
|
+
`${key.label} (${providers[key.provider]})`,
|
|
147
|
+
])),
|
|
148
|
+
},
|
|
149
|
+
})),
|
|
150
|
+
modelId: z
|
|
151
|
+
.string()
|
|
152
|
+
.max(256)
|
|
153
|
+
.meta(field({
|
|
154
|
+
label: "Model",
|
|
155
|
+
icon: _jsx(Sigil, { name: "write" }),
|
|
156
|
+
help: "For a key: the provider's model id, such as gpt-6-luna.",
|
|
157
|
+
maxLength: 256,
|
|
158
|
+
})),
|
|
159
|
+
}), [agents, keys]);
|
|
160
|
+
const choose = useMutation({
|
|
161
|
+
mutationFn: (input) => onChoose(input.agentId, input.draft),
|
|
162
|
+
});
|
|
163
|
+
const model = useSchemaForm({
|
|
164
|
+
schema,
|
|
165
|
+
defaultValues: {
|
|
166
|
+
agentId: agents[0]?.agentId ?? "",
|
|
167
|
+
source: defaultSource,
|
|
168
|
+
modelId: "",
|
|
169
|
+
},
|
|
170
|
+
onSubmit: async (value) => {
|
|
171
|
+
const key = keys.find((item) => item.keyId === value.source);
|
|
172
|
+
if (value.source !== defaultSource && key === undefined)
|
|
173
|
+
return { kind: "refused", error: "Choose a stored key." };
|
|
174
|
+
if (key !== undefined && value.modelId.length === 0)
|
|
175
|
+
return { kind: "refused", error: "Name the model this key runs." };
|
|
176
|
+
try {
|
|
177
|
+
await choose.mutateAsync({
|
|
178
|
+
agentId: value.agentId,
|
|
179
|
+
draft: key === undefined
|
|
180
|
+
? { keyId: null }
|
|
181
|
+
: {
|
|
182
|
+
keyId: key.keyId,
|
|
183
|
+
provider: key.provider,
|
|
184
|
+
modelId: value.modelId,
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
return { kind: "saved" };
|
|
188
|
+
}
|
|
189
|
+
catch (cause) {
|
|
190
|
+
return {
|
|
191
|
+
kind: "refused",
|
|
192
|
+
error: cause instanceof Error
|
|
193
|
+
? cause.message
|
|
194
|
+
: "The model could not be saved.",
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
return (_jsx(SchemaForm, { model: model, layout: "columns", "aria-label": "Choose Agent model", disabled: choose.isPending, footer: _jsx(FormFooter, { divider: false, error: model.error, children: _jsx(Button, { type: "submit", children: choose.isPending ? "Saving…" : "Save model" }) }) }));
|
|
200
|
+
}
|