@fungi.computer/tourtois 0.15.3 → 0.17.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/ChannelConversations.d.ts +34 -0
- package/dist/ChannelConversations.js +43 -0
- package/dist/DiscordChannels.d.ts +84 -0
- package/dist/DiscordChannels.js +386 -0
- package/dist/ModelKeys.d.ts +8 -20
- package/dist/ModelKeys.js +23 -24
- package/dist/WebsiteChannels.js +5 -29
- package/dist/oyster-platform-controls.js +3 -1
- package/dist/queries.d.ts +4 -0
- package/dist/queries.js +11 -0
- package/dist/settings-navigation.css +33 -0
- package/package.json +6 -6
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { WebsiteConversation, WebsiteTranscript } from "@fungi.computer/nacre/website-channels";
|
|
2
|
+
/** One reply that never reached its member, as the owner is told. */
|
|
3
|
+
export type ChannelProblem = Readonly<{
|
|
4
|
+
id: string;
|
|
5
|
+
/** Who was waiting. */
|
|
6
|
+
label: string;
|
|
7
|
+
at: number;
|
|
8
|
+
/** Why it did not arrive, in a sentence. */
|
|
9
|
+
reason: string;
|
|
10
|
+
}>;
|
|
11
|
+
/**
|
|
12
|
+
* What one channel's owner reads: the conversations its Agent had, and any
|
|
13
|
+
* one transcript on request. Website and Discord share it; each host port
|
|
14
|
+
* brings its own cache keys, reads and words for the other party.
|
|
15
|
+
*/
|
|
16
|
+
export declare function ChannelConversations({ keys, load, read, speaker, empty, label, problems, }: {
|
|
17
|
+
keys: Readonly<{
|
|
18
|
+
list: readonly unknown[];
|
|
19
|
+
transcript: (sessionId: string) => readonly unknown[];
|
|
20
|
+
}>;
|
|
21
|
+
load: () => Promise<readonly WebsiteConversation[]>;
|
|
22
|
+
read: (sessionId: string) => Promise<WebsiteTranscript>;
|
|
23
|
+
/** What the party on the other side is called in a transcript. */
|
|
24
|
+
speaker: string;
|
|
25
|
+
/** What to say when nobody has written yet. */
|
|
26
|
+
empty: string;
|
|
27
|
+
/** Names the list for assistive technology. */
|
|
28
|
+
label: string;
|
|
29
|
+
/** Replies the channel owed and could not deliver; absent where there are none to report. */
|
|
30
|
+
problems?: Readonly<{
|
|
31
|
+
key: readonly unknown[];
|
|
32
|
+
load: () => Promise<readonly ChannelProblem[]>;
|
|
33
|
+
}>;
|
|
34
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { DataList, DataListHeading, DataRow, } from "@fungi.computer/caps/components/data-list";
|
|
3
|
+
import { KeycapButton } from "@fungi.computer/caps/components/keycap";
|
|
4
|
+
import { useQuery } from "@tanstack/react-query";
|
|
5
|
+
import { useState } from "react";
|
|
6
|
+
/**
|
|
7
|
+
* What one channel's owner reads: the conversations its Agent had, and any
|
|
8
|
+
* one transcript on request. Website and Discord share it; each host port
|
|
9
|
+
* brings its own cache keys, reads and words for the other party.
|
|
10
|
+
*/
|
|
11
|
+
export function ChannelConversations({ keys, load, read, speaker, empty, label, problems, }) {
|
|
12
|
+
const [shown, setShown] = useState(false);
|
|
13
|
+
const [reading, setReading] = useState(null);
|
|
14
|
+
const list = useQuery({
|
|
15
|
+
queryKey: keys.list,
|
|
16
|
+
enabled: shown,
|
|
17
|
+
retry: false,
|
|
18
|
+
queryFn: load,
|
|
19
|
+
});
|
|
20
|
+
const undelivered = useQuery({
|
|
21
|
+
queryKey: problems?.key ?? [],
|
|
22
|
+
enabled: shown && problems !== undefined,
|
|
23
|
+
retry: false,
|
|
24
|
+
queryFn: () => problems?.load() ?? Promise.resolve([]),
|
|
25
|
+
});
|
|
26
|
+
const transcript = useQuery({
|
|
27
|
+
queryKey: keys.transcript(reading ?? ""),
|
|
28
|
+
enabled: reading !== null,
|
|
29
|
+
retry: false,
|
|
30
|
+
queryFn: () => reading === null
|
|
31
|
+
? Promise.reject(new Error("No conversation is open"))
|
|
32
|
+
: read(reading),
|
|
33
|
+
});
|
|
34
|
+
return (_jsxs("section", { className: "grid gap-sm", "aria-label": "Conversations", children: [_jsx(DataListHeading, { level: 3, children: "Conversations" }), shown ? null : (_jsx(KeycapButton, { density: "compact", label: "Show conversations", onClick: () => {
|
|
35
|
+
setShown(true);
|
|
36
|
+
} })), list.isPending && shown ? _jsx("p", { role: "status", children: "Loading\u2026" }) : null, list.isError ? (_jsx("p", { role: "alert", children: "Conversations could not be loaded." })) : null, list.data?.length === 0 ? _jsx("p", { className: "text-sm", children: empty }) : null, list.data !== undefined && list.data.length > 0 ? (_jsx(DataList, { density: "compact", "aria-label": label, children: list.data.map((conversation) => (_jsx(DataRow, { children: _jsxs("div", { className: "tourtois-conversation", children: [_jsxs("div", { className: "tourtois-conversation-row", children: [_jsx("span", { className: "tourtois-conversation-name text-sm", children: conversation.label }), _jsxs("span", { className: "flex flex-wrap items-center gap-sm", children: [_jsx("span", { className: "text-sm", children: new Date(conversation.lastEventAt).toLocaleString() }), _jsx(KeycapButton, { density: "compact", label: reading === conversation.sessionId
|
|
37
|
+
? "Close transcript"
|
|
38
|
+
: "Read transcript", "aria-label": `${reading === conversation.sessionId ? "Close" : "Read"} transcript of ${conversation.label}`, onClick: () => {
|
|
39
|
+
setReading(reading === conversation.sessionId
|
|
40
|
+
? null
|
|
41
|
+
: conversation.sessionId);
|
|
42
|
+
} })] })] }), reading === conversation.sessionId ? (_jsxs("div", { className: "tourtois-conversation-transcript", children: [transcript.isPending ? (_jsx("p", { role: "status", children: "Loading\u2026" })) : null, transcript.isError ? (_jsx("p", { role: "alert", children: "This conversation could not be read." })) : null, transcript.data ? (_jsx("ol", { "aria-label": "Transcript", className: "grid gap-xs", children: transcript.data.lines.map((line, index) => (_jsxs("li", { className: "text-sm", children: [_jsxs("strong", { children: [line.role === "visitor" ? speaker : "Agent", ":"] }), " ", line.text] }, `${String(index)}-${line.role}`))) })) : null] })) : null] }) }, conversation.sessionId))) })) : null, undelivered.data !== undefined && undelivered.data.length > 0 ? (_jsxs("section", { className: "grid gap-xs", "aria-label": "Replies not delivered", children: [_jsx(DataListHeading, { level: 3, children: "Not delivered" }), _jsx(DataList, { density: "compact", "aria-label": "Replies not delivered", children: undelivered.data.map((problem) => (_jsx(DataRow, { children: _jsxs("div", { className: "tourtois-conversation", children: [_jsxs("div", { className: "tourtois-conversation-row", children: [_jsx("span", { className: "tourtois-conversation-name text-sm", children: problem.label }), _jsx("span", { className: "text-sm", children: new Date(problem.at).toLocaleString() })] }), _jsx("p", { className: "text-sm", children: problem.reason })] }) }, problem.id))) })] })) : null] }));
|
|
43
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { type WebsiteConversation, type WebsiteTranscript } from "@fungi.computer/nacre/website-channels";
|
|
3
|
+
/** An Agent a Discord application can be bound to. */
|
|
4
|
+
export type DiscordAgent = Readonly<{
|
|
5
|
+
agentId: string;
|
|
6
|
+
name: string;
|
|
7
|
+
}>;
|
|
8
|
+
declare const discordBindingWire: z.ZodObject<{
|
|
9
|
+
bindingId: z.ZodString;
|
|
10
|
+
agentId: z.ZodString;
|
|
11
|
+
selector: z.ZodObject<{
|
|
12
|
+
provider: z.ZodLiteral<"discord">;
|
|
13
|
+
applicationId: z.ZodString;
|
|
14
|
+
publicKey: z.ZodString;
|
|
15
|
+
guildIds: z.ZodArray<z.ZodString>;
|
|
16
|
+
channelIds: z.ZodArray<z.ZodString>;
|
|
17
|
+
}, z.core.$strip>;
|
|
18
|
+
lifecycle: z.ZodEnum<{
|
|
19
|
+
active: "active";
|
|
20
|
+
suspended: "suspended";
|
|
21
|
+
}>;
|
|
22
|
+
authorityVersion: z.ZodNumber;
|
|
23
|
+
persona: z.ZodString;
|
|
24
|
+
}, z.core.$strip>;
|
|
25
|
+
/** One Discord application bound to an Agent; the bot token never comes back. */
|
|
26
|
+
export type DiscordBinding = z.infer<typeof discordBindingWire>;
|
|
27
|
+
/** Everything one host knows about its Discord applications. */
|
|
28
|
+
export type DiscordDirectory = Readonly<{
|
|
29
|
+
/** The host's own address, where Discord posts its interactions. */
|
|
30
|
+
interactionsOrigin: string;
|
|
31
|
+
agents: readonly DiscordAgent[];
|
|
32
|
+
bindings: readonly DiscordBinding[];
|
|
33
|
+
}>;
|
|
34
|
+
/** A new Discord application: its Agent, identity, token, places and brief. */
|
|
35
|
+
export type NewDiscord = Readonly<{
|
|
36
|
+
agentId: string;
|
|
37
|
+
applicationId: string;
|
|
38
|
+
publicKey: string;
|
|
39
|
+
botToken: string;
|
|
40
|
+
guildIds: readonly string[];
|
|
41
|
+
channelIds: readonly string[];
|
|
42
|
+
persona: string;
|
|
43
|
+
}>;
|
|
44
|
+
/** What an owner may change about one Discord application. */
|
|
45
|
+
export type DiscordChange = Readonly<{
|
|
46
|
+
lifecycle?: "active" | "suspended";
|
|
47
|
+
botToken?: string;
|
|
48
|
+
}>;
|
|
49
|
+
/** Which guilds took `/ask` and which Discord refused. */
|
|
50
|
+
export type DiscordCommandResult = Readonly<{
|
|
51
|
+
registered: readonly string[];
|
|
52
|
+
failed: readonly string[];
|
|
53
|
+
}>;
|
|
54
|
+
/** Host-supplied owner controls over its Discord applications. */
|
|
55
|
+
export type DiscordChannelsPort = Readonly<{
|
|
56
|
+
/** Names this host's data; two hosts never share a cache entry. */
|
|
57
|
+
scope: string;
|
|
58
|
+
read(): Promise<DiscordDirectory>;
|
|
59
|
+
create(input: NewDiscord, requestId: string): Promise<DiscordBinding>;
|
|
60
|
+
update(bindingId: string, expectedVersion: number, change: DiscordChange): Promise<DiscordBinding>;
|
|
61
|
+
registerCommands(bindingId: string): Promise<DiscordCommandResult>;
|
|
62
|
+
conversations(binding: DiscordBinding): Promise<readonly WebsiteConversation[]>;
|
|
63
|
+
transcript(binding: DiscordBinding, sessionId: string): Promise<WebsiteTranscript>;
|
|
64
|
+
/** Replies the Agent owed this application's members and could not deliver. */
|
|
65
|
+
undelivered(binding: DiscordBinding): Promise<readonly DiscordUndelivered[]>;
|
|
66
|
+
}>;
|
|
67
|
+
/** One reply that never reached Discord; the member is named, never numbered. */
|
|
68
|
+
export type DiscordUndelivered = Readonly<{
|
|
69
|
+
interactionId: string;
|
|
70
|
+
label: string;
|
|
71
|
+
state: "expired" | "failed";
|
|
72
|
+
closedAt: number;
|
|
73
|
+
}>;
|
|
74
|
+
/** Bind owner Discord controls to this trusted shell's own host. */
|
|
75
|
+
export declare function createOysterDiscordChannelsPort(origin: URL): DiscordChannelsPort;
|
|
76
|
+
/**
|
|
77
|
+
* Owner Settings for Discord: bring your own Discord application, choose the
|
|
78
|
+
* Agent members talk to, add `/ask` to your servers and pause it. Sits under
|
|
79
|
+
* the Website section of Channels, in the same shape.
|
|
80
|
+
*/
|
|
81
|
+
export declare function DiscordChannels({ port }: {
|
|
82
|
+
port: DiscordChannelsPort;
|
|
83
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
84
|
+
export {};
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { AppError } from "@fungi.computer/agar";
|
|
3
|
+
import { DataList, DataListHeading, DataRow, FactRow, } from "@fungi.computer/caps/components/data-list";
|
|
4
|
+
import { Button } from "@fungi.computer/caps/components/button";
|
|
5
|
+
import { CopyField } from "@fungi.computer/caps/components/copy-field";
|
|
6
|
+
import { FormFooter } from "@fungi.computer/caps/components/field";
|
|
7
|
+
import { InlineCode } from "@fungi.computer/caps/components/inline-code";
|
|
8
|
+
import { KeycapButton } from "@fungi.computer/caps/components/keycap";
|
|
9
|
+
import { Prose } from "@fungi.computer/caps/components/prose";
|
|
10
|
+
import { Status } from "@fungi.computer/caps/components/status";
|
|
11
|
+
import { SchemaForm, field, sections, useSchemaForm, } from "@fungi.computer/caps/form";
|
|
12
|
+
import { Sigil } from "@fungi.computer/sigils";
|
|
13
|
+
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
14
|
+
import { useId, useMemo, useState } from "react";
|
|
15
|
+
import * as Option from "effect/Option";
|
|
16
|
+
import * as Schema from "effect/Schema";
|
|
17
|
+
import * as z from "zod";
|
|
18
|
+
import { websiteConversationSchema, websiteTranscriptSchema, } from "@fungi.computer/nacre/website-channels";
|
|
19
|
+
import { ChannelConversations } from "./ChannelConversations.js";
|
|
20
|
+
import { tourtoisQueryKeys } from "./queries.js";
|
|
21
|
+
import { requestIds } from "./team-directory.js";
|
|
22
|
+
const snowflake = z.string().regex(/^\d{17,20}$/u);
|
|
23
|
+
const discordBindingWire = z.object({
|
|
24
|
+
bindingId: z.string(),
|
|
25
|
+
agentId: z.string(),
|
|
26
|
+
selector: z.object({
|
|
27
|
+
provider: z.literal("discord"),
|
|
28
|
+
applicationId: snowflake,
|
|
29
|
+
publicKey: z.string(),
|
|
30
|
+
guildIds: z.array(snowflake),
|
|
31
|
+
channelIds: z.array(snowflake),
|
|
32
|
+
}),
|
|
33
|
+
lifecycle: z.enum(["active", "suspended"]),
|
|
34
|
+
authorityVersion: z.number().int().positive(),
|
|
35
|
+
persona: z.string(),
|
|
36
|
+
});
|
|
37
|
+
function parseIds(text, what, least) {
|
|
38
|
+
const entries = text.split(/[\s,]+/u).filter(Boolean);
|
|
39
|
+
if (entries.length < least)
|
|
40
|
+
return { kind: "invalid", message: `Enter at least one ${what} ID.` };
|
|
41
|
+
const ids = [];
|
|
42
|
+
for (const entry of entries) {
|
|
43
|
+
if (!snowflake.safeParse(entry).success)
|
|
44
|
+
return {
|
|
45
|
+
kind: "invalid",
|
|
46
|
+
message: `“${entry}” is not a Discord ID. It is 17 to 20 digits.`,
|
|
47
|
+
};
|
|
48
|
+
if (!ids.includes(entry))
|
|
49
|
+
ids.push(entry);
|
|
50
|
+
}
|
|
51
|
+
return { kind: "parsed", ids };
|
|
52
|
+
}
|
|
53
|
+
const directoryWire = z.object({
|
|
54
|
+
interactionsOrigin: z.url(),
|
|
55
|
+
agents: z.array(z.object({ agentId: z.string(), name: z.string() })),
|
|
56
|
+
discords: z.array(discordBindingWire),
|
|
57
|
+
});
|
|
58
|
+
const bindingChange = z.object({ binding: discordBindingWire });
|
|
59
|
+
const commandResult = z.object({
|
|
60
|
+
registered: z.array(snowflake),
|
|
61
|
+
failed: z.array(snowflake),
|
|
62
|
+
});
|
|
63
|
+
const decodeRefusal = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Struct({ error: Schema.String })));
|
|
64
|
+
const undeliveredList = z.object({
|
|
65
|
+
undelivered: z.array(z.object({
|
|
66
|
+
interactionId: z.string(),
|
|
67
|
+
label: z.string(),
|
|
68
|
+
state: z.enum(["expired", "failed"]),
|
|
69
|
+
closedAt: z.number(),
|
|
70
|
+
})),
|
|
71
|
+
});
|
|
72
|
+
const conversationList = z.object({
|
|
73
|
+
conversations: z.array(websiteConversationSchema),
|
|
74
|
+
});
|
|
75
|
+
const refusalText = new Map([
|
|
76
|
+
["application_taken", "That Discord application is already bound."],
|
|
77
|
+
["not_found", "That Agent or Discord application was not found."],
|
|
78
|
+
["version_conflict", "It changed elsewhere. Reload, then try again."],
|
|
79
|
+
["invalid_input", "Check the IDs, key and bot token, then try again."],
|
|
80
|
+
[
|
|
81
|
+
"custody_unavailable",
|
|
82
|
+
"This Oyster has no credential key, so it cannot store a bot token.",
|
|
83
|
+
],
|
|
84
|
+
["limit", "This Oyster holds as many sealed credentials as it can."],
|
|
85
|
+
]);
|
|
86
|
+
/** The owner-readable reason a refusal body names, if it names one we know. */
|
|
87
|
+
async function refusalOf(response) {
|
|
88
|
+
const decoded = decodeRefusal(await response.text());
|
|
89
|
+
return Option.isSome(decoded)
|
|
90
|
+
? refusalText.get(decoded.value.error)
|
|
91
|
+
: undefined;
|
|
92
|
+
}
|
|
93
|
+
async function request(url, init) {
|
|
94
|
+
const response = await fetch(url, {
|
|
95
|
+
...init,
|
|
96
|
+
headers: {
|
|
97
|
+
Accept: "application/json",
|
|
98
|
+
"Content-Type": "application/json",
|
|
99
|
+
...Object.fromEntries(new Headers(init?.headers)),
|
|
100
|
+
},
|
|
101
|
+
credentials: "same-origin",
|
|
102
|
+
cache: "no-store",
|
|
103
|
+
redirect: "error",
|
|
104
|
+
});
|
|
105
|
+
if (!response.ok) {
|
|
106
|
+
throw new AppError((await refusalOf(response)) ??
|
|
107
|
+
`Discord request refused (${String(response.status)}).`, {
|
|
108
|
+
code: response.status === 401 || response.status === 403
|
|
109
|
+
? "CAPABILITY_DENIED"
|
|
110
|
+
: "HOST_FAILURE",
|
|
111
|
+
retryable: response.status >= 500,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return response;
|
|
115
|
+
}
|
|
116
|
+
/** Bind owner Discord controls to this trusted shell's own host. */
|
|
117
|
+
export function createOysterDiscordChannelsPort(origin) {
|
|
118
|
+
const send = (path, init) => request(new URL(path, origin), init);
|
|
119
|
+
const binding = (bindingId) => `/api/discord/${encodeURIComponent(bindingId)}`;
|
|
120
|
+
return {
|
|
121
|
+
scope: origin.origin,
|
|
122
|
+
read: async () => {
|
|
123
|
+
const wire = directoryWire.parse(await (await send("/api/discord")).json());
|
|
124
|
+
return {
|
|
125
|
+
interactionsOrigin: wire.interactionsOrigin,
|
|
126
|
+
agents: wire.agents,
|
|
127
|
+
bindings: wire.discords,
|
|
128
|
+
};
|
|
129
|
+
},
|
|
130
|
+
create: async (input, requestId) => {
|
|
131
|
+
const response = await send("/api/discord", {
|
|
132
|
+
method: "POST",
|
|
133
|
+
headers: { "x-request-id": requestId },
|
|
134
|
+
body: JSON.stringify(input),
|
|
135
|
+
});
|
|
136
|
+
return bindingChange.parse(await response.json()).binding;
|
|
137
|
+
},
|
|
138
|
+
update: async (bindingId, expectedVersion, change) => {
|
|
139
|
+
const response = await send(binding(bindingId), {
|
|
140
|
+
method: "PUT",
|
|
141
|
+
body: JSON.stringify({ expectedVersion, ...change }),
|
|
142
|
+
});
|
|
143
|
+
return bindingChange.parse(await response.json()).binding;
|
|
144
|
+
},
|
|
145
|
+
registerCommands: async (bindingId) => commandResult.parse(await (await send(`${binding(bindingId)}/commands`, { method: "POST" })).json()),
|
|
146
|
+
conversations: async ({ bindingId }) => conversationList.parse(await (await send(`${binding(bindingId)}/conversations`)).json()).conversations,
|
|
147
|
+
undelivered: async ({ bindingId }) => undeliveredList.parse(await (await send(`${binding(bindingId)}/undelivered`)).json()).undelivered,
|
|
148
|
+
transcript: async ({ bindingId }, sessionId) => websiteTranscriptSchema.parse(await (await send(`${binding(bindingId)}/conversations/${encodeURIComponent(sessionId)}`)).json()),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
const portalUrl = "https://discord.com/developers/applications";
|
|
152
|
+
/**
|
|
153
|
+
* Owner Settings for Discord: bring your own Discord application, choose the
|
|
154
|
+
* Agent members talk to, add `/ask` to your servers and pause it. Sits under
|
|
155
|
+
* the Website section of Channels, in the same shape.
|
|
156
|
+
*/
|
|
157
|
+
export function DiscordChannels({ port }) {
|
|
158
|
+
const directory = useQuery({
|
|
159
|
+
queryKey: tourtoisQueryKeys.discordChannels(port.scope),
|
|
160
|
+
retry: false,
|
|
161
|
+
queryFn: () => port.read(),
|
|
162
|
+
});
|
|
163
|
+
const [adding, setAdding] = useState(false);
|
|
164
|
+
const [createdBinding, setCreatedBinding] = useState(null);
|
|
165
|
+
const [openBinding, setOpenBinding] = useState(null);
|
|
166
|
+
const agents = directory.data?.agents ?? [];
|
|
167
|
+
return (_jsxs("section", { className: "tourtois-websites grid gap-md", "aria-label": "Discord", children: [_jsxs("header", { className: "grid gap-xs", children: [_jsx(DataListHeading, { level: 3, children: "Discord" }), _jsxs("p", { className: "text-sm", children: ["Let people in your Discord server talk to one of your Agents. You bring your own Discord application; Fungi holds no Discord account. Members type ", _jsx(InlineCode, { children: "/ask" }), ", and the Agent answers in the channel."] })] }), directory.isPending ? _jsx("p", { role: "status", children: "Loading Discord\u2026" }) : null, directory.isError ? (_jsxs("div", { role: "alert", children: [_jsx("p", { children: "Discord could not be loaded. Your saved settings have not changed." }), _jsx(Button, { onClick: () => {
|
|
168
|
+
void directory.refetch();
|
|
169
|
+
}, children: "Retry Discord" })] })) : null, directory.data ? (_jsxs(_Fragment, { children: [directory.data.bindings.length > 0 ? (_jsx(DataList, { "aria-label": "Saved Discord applications", children: directory.data.bindings.map((binding) => (_jsx(DiscordRow, { port: port, binding: binding, interactionsOrigin: directory.data.interactionsOrigin, open: openBinding === binding.bindingId, justCreated: createdBinding === binding.bindingId, onOpenChange: (open) => {
|
|
170
|
+
setOpenBinding(open ? binding.bindingId : null);
|
|
171
|
+
}, agentName: agents.find((agent) => agent.agentId === binding.agentId)
|
|
172
|
+
?.name ?? "Unavailable Agent" }, binding.bindingId))) })) : adding ? null : (_jsx("p", { className: "text-sm", children: "No Discord applications yet." })), adding ? (_jsx(AddDiscord, { port: port, agents: agents, onDone: (bindingId) => {
|
|
173
|
+
setAdding(false);
|
|
174
|
+
if (bindingId) {
|
|
175
|
+
setOpenBinding(bindingId);
|
|
176
|
+
setCreatedBinding(bindingId);
|
|
177
|
+
}
|
|
178
|
+
} })) : agents.length === 0 ? (_jsx("p", { className: "text-sm", children: "Create an Agent first; Discord needs one to answer members." })) : (_jsx("div", { className: "flex justify-end", children: _jsx(Button, { onClick: () => {
|
|
179
|
+
setAdding(true);
|
|
180
|
+
}, children: "Add Discord" }) }))] })) : null] }));
|
|
181
|
+
}
|
|
182
|
+
function AddDiscord({ port, agents, onDone, }) {
|
|
183
|
+
const cache = useQueryClient();
|
|
184
|
+
const schema = useMemo(() => z
|
|
185
|
+
.object({
|
|
186
|
+
agentId: z.string().meta(field({
|
|
187
|
+
label: "Agent",
|
|
188
|
+
help: "Members talk to this Agent.",
|
|
189
|
+
kind: "select",
|
|
190
|
+
section: "discord",
|
|
191
|
+
options: Object.fromEntries(agents.map((agent) => [agent.agentId, agent.name])),
|
|
192
|
+
})),
|
|
193
|
+
applicationId: snowflake.meta(field({
|
|
194
|
+
label: "Application ID",
|
|
195
|
+
help: "From General Information in the Discord developer portal.",
|
|
196
|
+
placeholder: "1234567890123456789",
|
|
197
|
+
required: true,
|
|
198
|
+
section: "discord",
|
|
199
|
+
})),
|
|
200
|
+
publicKey: z
|
|
201
|
+
.string()
|
|
202
|
+
.regex(/^[0-9a-fA-F]{64}$/u, "Paste the 64-character public key")
|
|
203
|
+
.meta(field({
|
|
204
|
+
label: "Public key",
|
|
205
|
+
help: "Also on General Information. Discord signs every request with it.",
|
|
206
|
+
required: true,
|
|
207
|
+
section: "discord",
|
|
208
|
+
})),
|
|
209
|
+
botToken: z
|
|
210
|
+
.string()
|
|
211
|
+
.min(1, "Paste the bot token")
|
|
212
|
+
.max(256)
|
|
213
|
+
.meta(field({
|
|
214
|
+
label: "Bot token",
|
|
215
|
+
icon: _jsx(Sigil, { name: "restricted" }),
|
|
216
|
+
help: "From Bot in the developer portal. Sealed on this instance and never shown again.",
|
|
217
|
+
kind: "secret",
|
|
218
|
+
required: true,
|
|
219
|
+
section: "discord",
|
|
220
|
+
})),
|
|
221
|
+
guilds: z
|
|
222
|
+
.string()
|
|
223
|
+
.transform((text, context) => {
|
|
224
|
+
const parsed = parseIds(text, "server", 1);
|
|
225
|
+
if (parsed.kind === "parsed")
|
|
226
|
+
return parsed.ids;
|
|
227
|
+
context.addIssue({ code: "custom", message: parsed.message });
|
|
228
|
+
return z.NEVER;
|
|
229
|
+
})
|
|
230
|
+
.meta(field({
|
|
231
|
+
label: "Server IDs",
|
|
232
|
+
help: "Turn on Developer Mode in Discord, then right-click your server and Copy Server ID. Separate more than one with commas.",
|
|
233
|
+
placeholder: "2345678901234567890",
|
|
234
|
+
required: true,
|
|
235
|
+
section: "discord",
|
|
236
|
+
})),
|
|
237
|
+
channels: z
|
|
238
|
+
.string()
|
|
239
|
+
.transform((text, context) => {
|
|
240
|
+
const parsed = parseIds(text, "channel", 0);
|
|
241
|
+
if (parsed.kind === "parsed")
|
|
242
|
+
return parsed.ids;
|
|
243
|
+
context.addIssue({ code: "custom", message: parsed.message });
|
|
244
|
+
return z.NEVER;
|
|
245
|
+
})
|
|
246
|
+
.meta(field({
|
|
247
|
+
label: "Channel IDs",
|
|
248
|
+
help: "Leave empty to answer in every channel of those servers.",
|
|
249
|
+
optional: true,
|
|
250
|
+
section: "discord",
|
|
251
|
+
})),
|
|
252
|
+
persona: z
|
|
253
|
+
.string()
|
|
254
|
+
.max(4_000)
|
|
255
|
+
.meta(field({
|
|
256
|
+
label: "What should the Agent do here?",
|
|
257
|
+
help: "Its brief for Discord. Members don't see it.",
|
|
258
|
+
kind: "textarea",
|
|
259
|
+
optional: true,
|
|
260
|
+
rows: 3,
|
|
261
|
+
maxLength: 4_000,
|
|
262
|
+
placeholder: "Answer questions about our community.",
|
|
263
|
+
section: "discord",
|
|
264
|
+
})),
|
|
265
|
+
})
|
|
266
|
+
.meta(sections({
|
|
267
|
+
discord: {
|
|
268
|
+
title: "Add a Discord application",
|
|
269
|
+
description: "Create an application in the Discord developer portal, add its bot to your server, then paste its details here. You get the address to give Discord next.",
|
|
270
|
+
},
|
|
271
|
+
})), [agents]);
|
|
272
|
+
const create = useMutation({
|
|
273
|
+
mutationFn: (input) => port.create({
|
|
274
|
+
agentId: input.agentId,
|
|
275
|
+
applicationId: input.applicationId,
|
|
276
|
+
publicKey: input.publicKey,
|
|
277
|
+
botToken: input.botToken,
|
|
278
|
+
guildIds: input.guildIds,
|
|
279
|
+
channelIds: input.channelIds,
|
|
280
|
+
persona: input.persona,
|
|
281
|
+
}, input.requestId),
|
|
282
|
+
onSuccess: async (binding) => {
|
|
283
|
+
await cache.invalidateQueries({
|
|
284
|
+
queryKey: tourtoisQueryKeys.discordChannels(port.scope),
|
|
285
|
+
});
|
|
286
|
+
onDone(binding.bindingId);
|
|
287
|
+
},
|
|
288
|
+
});
|
|
289
|
+
const model = useSchemaForm({
|
|
290
|
+
schema,
|
|
291
|
+
defaultValues: {
|
|
292
|
+
agentId: agents[0]?.agentId ?? "",
|
|
293
|
+
applicationId: "",
|
|
294
|
+
publicKey: "",
|
|
295
|
+
botToken: "",
|
|
296
|
+
guilds: "",
|
|
297
|
+
channels: "",
|
|
298
|
+
persona: "",
|
|
299
|
+
},
|
|
300
|
+
onSubmit: async (value) => {
|
|
301
|
+
try {
|
|
302
|
+
await create.mutateAsync({
|
|
303
|
+
requestId: requestIds.request.generate(),
|
|
304
|
+
agentId: value.agentId,
|
|
305
|
+
applicationId: value.applicationId,
|
|
306
|
+
publicKey: value.publicKey,
|
|
307
|
+
botToken: value.botToken,
|
|
308
|
+
guildIds: value.guilds,
|
|
309
|
+
channelIds: value.channels,
|
|
310
|
+
persona: value.persona,
|
|
311
|
+
});
|
|
312
|
+
return { kind: "saved" };
|
|
313
|
+
}
|
|
314
|
+
catch (cause) {
|
|
315
|
+
return {
|
|
316
|
+
kind: "refused",
|
|
317
|
+
error: cause instanceof Error
|
|
318
|
+
? cause.message
|
|
319
|
+
: "Discord could not be added. Try the saved request again.",
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
},
|
|
323
|
+
});
|
|
324
|
+
return (_jsx(SchemaForm, { model: model, layout: "columns", "aria-label": "Add a Discord application", disabled: create.isPending, footer: _jsxs(FormFooter, { error: create.error?.message ?? model.error, children: [_jsx(Button, { type: "button", variant: "ghost", onClick: () => {
|
|
325
|
+
onDone(null);
|
|
326
|
+
}, children: "Cancel" }), create.isError ? (_jsx(Button, { type: "button", variant: "outline", onClick: () => {
|
|
327
|
+
create.mutate(create.variables);
|
|
328
|
+
}, children: "Retry saved request" })) : null, _jsx(Button, { type: "submit", children: create.isPending ? "Adding Discord…" : "Add Discord" })] }) }));
|
|
329
|
+
}
|
|
330
|
+
function DiscordRow({ port, binding, interactionsOrigin, agentName, open, justCreated, onOpenChange, }) {
|
|
331
|
+
const cache = useQueryClient();
|
|
332
|
+
const guideId = useId();
|
|
333
|
+
const key = tourtoisQueryKeys.discordChannels(port.scope);
|
|
334
|
+
const patch = useMutation({
|
|
335
|
+
// Edits queue per application; each sends the last version the host acknowledged.
|
|
336
|
+
scope: { id: JSON.stringify([port.scope, binding.bindingId]) },
|
|
337
|
+
mutationFn: (change) => {
|
|
338
|
+
const current = cache
|
|
339
|
+
.getQueryData(key)
|
|
340
|
+
?.bindings.find((item) => item.bindingId === binding.bindingId);
|
|
341
|
+
if (!current)
|
|
342
|
+
throw new Error("Discord unavailable. Reload before changing it.");
|
|
343
|
+
return port.update(binding.bindingId, current.authorityVersion, change);
|
|
344
|
+
},
|
|
345
|
+
onSettled: () => cache.invalidateQueries({ queryKey: key }),
|
|
346
|
+
});
|
|
347
|
+
const register = useMutation({
|
|
348
|
+
mutationFn: () => port.registerCommands(binding.bindingId),
|
|
349
|
+
});
|
|
350
|
+
const { applicationId, guildIds, channelIds } = binding.selector;
|
|
351
|
+
const endpoint = `${interactionsOrigin}/api/discord/interactions/${binding.bindingId}`;
|
|
352
|
+
const active = binding.lifecycle === "active";
|
|
353
|
+
const places = channelIds.length === 0
|
|
354
|
+
? `all channels in ${String(guildIds.length)} ${guildIds.length === 1 ? "server" : "servers"}`
|
|
355
|
+
: `${String(channelIds.length)} ${channelIds.length === 1 ? "channel" : "channels"}`;
|
|
356
|
+
return (_jsx(DataRow, { children: _jsxs("div", { className: "tourtois-connector-row grid gap-md", children: [_jsxs("div", { className: "tourtois-website-summary", children: [_jsx(Sigil, { name: "agent", size: 28 }), _jsxs("div", { className: "min-w-0 flex-1", children: [_jsx(DataListHeading, { level: 3, children: agentName }), _jsx("p", { className: "tourtois-website-hosts text-sm", children: places })] }), _jsx(Status, { appearance: "chip", label: active ? "Active" : "Paused", size: "compact", tone: active ? "success" : "neutral" }), _jsx(Button, { variant: "outline", size: "sm", "aria-controls": guideId, "aria-expanded": open, "aria-label": `${open ? "Close" : "Set up"} ${agentName} on Discord`, onClick: () => {
|
|
357
|
+
onOpenChange(!open);
|
|
358
|
+
}, children: open ? "Close" : "Set up" })] }), open ? (_jsxs("div", { className: "tourtois-connector-row-details tourtois-website-guide", id: guideId, children: [_jsxs(Prose, { className: "tourtois-website-doc", children: [_jsx("h4", { children: justCreated ? "Finish setting up Discord" : "Connect Discord" }), _jsxs("ol", { children: [_jsxs("li", { children: [_jsxs("p", { children: [_jsx("strong", { children: "Copy this address." }), " In the", " ", _jsx("a", { href: portalUrl, children: "Discord developer portal" }), ", open your application, paste it into Interactions Endpoint URL on General Information and save. Discord checks it at once."] }), _jsx(CopyField, { layout: "block", title: "URL", label: "Copy address", tokens: [{ kind: "string", text: endpoint }], "aria-label": `Interactions endpoint for ${agentName}` })] }), _jsx("li", { children: _jsxs("p", { children: [_jsx("strong", { children: "Add the bot to your server" }), " from OAuth2 in the portal, with the ", _jsx(InlineCode, { children: "bot" }), " and", " ", _jsx(InlineCode, { children: "applications.commands" }), " scopes. It needs no other permission."] }) }), _jsxs("li", { children: [_jsxs("p", { children: [_jsx("strong", { children: "Add the command." }), " This puts", " ", _jsx(InlineCode, { children: "/ask" }), " in your server."] }), _jsxs("span", { className: "tourtois-website-fact", children: [_jsx(KeycapButton, { density: "compact", label: register.isPending
|
|
359
|
+
? "Adding /ask…"
|
|
360
|
+
: "Add /ask to Discord", disabled: register.isPending, onClick: () => {
|
|
361
|
+
register.mutate();
|
|
362
|
+
} }), register.data ? (_jsx(Status, { glyph: "dot", tone: register.data.failed.length === 0
|
|
363
|
+
? "success"
|
|
364
|
+
: "warning", label: register.data.failed.length === 0
|
|
365
|
+
? `Added to ${String(register.data.registered.length)} ${register.data.registered.length === 1 ? "server" : "servers"}`
|
|
366
|
+
: `Discord refused ${String(register.data.failed.length)} of ${String(register.data.failed.length + register.data.registered.length)}. Check the bot is in that server.` })) : null] })] }), _jsx("li", { children: _jsxs("p", { children: [_jsx("strong", { children: "Try it." }), " Type ", _jsx(InlineCode, { children: "/ask" }), " ", "in a channel. ", agentName, " answers there, with a Reply button to carry on."] }) })] }), _jsx("h4", { children: "What members can trigger" }), _jsxs("p", { children: ["Members talk to ", agentName, " itself, with ", agentName, "\u2019s own tools and permissions. They cannot sign in to this Oyster or reach your account. The server and channel list limits where it answers, not who can use it there."] })] }), _jsxs(DataList, { density: "compact", "aria-label": `Discord status for ${agentName}`, children: [_jsx(FactRow, { icon: _jsx(Sigil, { name: "agent" }), label: "Agent", children: agentName }), _jsx(FactRow, { icon: _jsx(Sigil, { name: "internet" }), label: "Application ID", children: _jsx(InlineCode, { children: applicationId }) }), _jsx(FactRow, { icon: _jsx(Sigil, { name: "chat" }), label: active ? "Discord is live" : "Discord is paused", children: _jsxs("span", { className: "tourtois-website-fact", children: [_jsx("span", { className: "text-sm", children: active
|
|
367
|
+
? "Pause to stop answering without removing the setup."
|
|
368
|
+
: "Members get a notice until you resume it." }), _jsx(KeycapButton, { density: "compact", label: active ? "Pause Discord" : "Resume Discord", disabled: patch.isPending, onClick: () => {
|
|
369
|
+
patch.mutate({
|
|
370
|
+
lifecycle: active ? "suspended" : "active",
|
|
371
|
+
});
|
|
372
|
+
} })] }) })] }), patch.isError ? _jsx("p", { role: "alert", children: patch.error.message }) : null, register.isError ? (_jsx("p", { role: "alert", children: register.error.message })) : null, _jsx(ChannelConversations, { keys: {
|
|
373
|
+
list: tourtoisQueryKeys.discordConversations(port.scope, binding.bindingId),
|
|
374
|
+
transcript: (sessionId) => tourtoisQueryKeys.discordTranscript(port.scope, binding.bindingId, sessionId),
|
|
375
|
+
}, load: () => port.conversations(binding), read: (sessionId) => port.transcript(binding, sessionId), problems: {
|
|
376
|
+
key: tourtoisQueryKeys.discordUndelivered(port.scope, binding.bindingId),
|
|
377
|
+
load: async () => (await port.undelivered(binding)).map((reply) => ({
|
|
378
|
+
id: reply.interactionId,
|
|
379
|
+
label: reply.label,
|
|
380
|
+
at: reply.closedAt,
|
|
381
|
+
reason: reply.state === "failed"
|
|
382
|
+
? "Discord kept refusing the answer, so it never arrived."
|
|
383
|
+
: "The answer was not ready before Discord's reply window closed.",
|
|
384
|
+
})),
|
|
385
|
+
}, speaker: "Member", empty: "No one has asked yet.", label: "Discord conversations" })] })) : null] }) }));
|
|
386
|
+
}
|
package/dist/ModelKeys.d.ts
CHANGED
|
@@ -1,21 +1,10 @@
|
|
|
1
1
|
import * as z from "zod";
|
|
2
|
-
|
|
3
|
-
readonly "cloudflare-workers-ai": "Cloudflare Workers AI";
|
|
4
|
-
readonly anthropic: "Anthropic";
|
|
5
|
-
readonly openai: "OpenAI";
|
|
6
|
-
readonly openrouter: "OpenRouter";
|
|
7
|
-
};
|
|
8
|
-
export type Provider = keyof typeof providers;
|
|
2
|
+
export type Provider = string;
|
|
9
3
|
/** Every stored key and every Agent with its persisted model choice. */
|
|
10
4
|
export declare const modelKeysViewSchema: z.ZodObject<{
|
|
11
5
|
keys: z.ZodArray<z.ZodObject<{
|
|
12
6
|
keyId: z.ZodString;
|
|
13
|
-
provider: z.
|
|
14
|
-
"cloudflare-workers-ai": "cloudflare-workers-ai";
|
|
15
|
-
anthropic: "anthropic";
|
|
16
|
-
openai: "openai";
|
|
17
|
-
openrouter: "openrouter";
|
|
18
|
-
}>;
|
|
7
|
+
provider: z.ZodString;
|
|
19
8
|
label: z.ZodString;
|
|
20
9
|
createdAt: z.ZodNumber;
|
|
21
10
|
validatedAt: z.ZodNumber;
|
|
@@ -25,12 +14,12 @@ export declare const modelKeysViewSchema: z.ZodObject<{
|
|
|
25
14
|
unknown: "unknown";
|
|
26
15
|
missing: "missing";
|
|
27
16
|
}>;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
17
|
+
providers: z.ZodArray<z.ZodObject<{
|
|
18
|
+
id: z.ZodString;
|
|
19
|
+
label: z.ZodString;
|
|
20
|
+
accountId: z.ZodBoolean;
|
|
21
|
+
}, z.core.$strict>>;
|
|
22
|
+
catalogs: z.ZodRecord<z.ZodString & z.core.$partial, z.ZodArray<z.ZodObject<{
|
|
34
23
|
id: z.ZodString;
|
|
35
24
|
name: z.ZodString;
|
|
36
25
|
input: z.ZodNumber;
|
|
@@ -86,4 +75,3 @@ export declare function ModelKeys({ port, focus, }: {
|
|
|
86
75
|
port: ModelKeysPort;
|
|
87
76
|
focus?: boolean;
|
|
88
77
|
}): import("react/jsx-runtime").JSX.Element;
|
|
89
|
-
export {};
|
package/dist/ModelKeys.js
CHANGED
|
@@ -11,18 +11,13 @@ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
|
11
11
|
import { useCallback, useMemo, useState } from "react";
|
|
12
12
|
import * as z from "zod";
|
|
13
13
|
import { SettingsItem } from "./settings-item.js";
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
};
|
|
20
|
-
const providerSchema = z.
|
|
21
|
-
"cloudflare-workers-ai",
|
|
22
|
-
"anthropic",
|
|
23
|
-
"openai",
|
|
24
|
-
"openrouter",
|
|
25
|
-
]);
|
|
14
|
+
/** A provider the host offers; the host owns the list and the client only shows it. */
|
|
15
|
+
const providerChoiceSchema = z.strictObject({
|
|
16
|
+
id: z.string().min(1),
|
|
17
|
+
label: z.string().min(1),
|
|
18
|
+
accountId: z.boolean(),
|
|
19
|
+
});
|
|
20
|
+
const providerSchema = z.string().min(1);
|
|
26
21
|
/** One stored key as the host lists it; the secret never comes back. */
|
|
27
22
|
const modelKeySchema = z.strictObject({
|
|
28
23
|
keyId: z.string().min(1),
|
|
@@ -44,6 +39,7 @@ const agentChoiceSchema = z.strictObject({
|
|
|
44
39
|
export const modelKeysViewSchema = z.strictObject({
|
|
45
40
|
keys: z.array(modelKeySchema),
|
|
46
41
|
models: z.enum(["ready", "missing", "unknown"]),
|
|
42
|
+
providers: z.array(providerChoiceSchema).min(1),
|
|
47
43
|
catalogs: z.partialRecord(providerSchema, z.array(z.strictObject({
|
|
48
44
|
id: z.string().min(1),
|
|
49
45
|
name: z.string().min(1),
|
|
@@ -57,6 +53,8 @@ export const modelKeysViewSchema = z.strictObject({
|
|
|
57
53
|
choice: agentChoiceSchema.nullable(),
|
|
58
54
|
})),
|
|
59
55
|
});
|
|
56
|
+
/** Provider label from the host's list; an unlisted provider shows its id. */
|
|
57
|
+
const providerLabel = (providers, id) => providers.find((choice) => choice.id === id)?.label ?? id;
|
|
60
58
|
const queryKey = ["tourtois", "model-keys"];
|
|
61
59
|
const defaultSource = "default";
|
|
62
60
|
/**
|
|
@@ -83,9 +81,9 @@ export function ModelKeys({ port, focus = false, }) {
|
|
|
83
81
|
const agents = view.data?.agents ?? [];
|
|
84
82
|
// A fresh form after each stored key, so the key field empties.
|
|
85
83
|
const [formKey, setFormKey] = useState(0);
|
|
86
|
-
return (_jsxs("section", { ref: focusSetup, className: "tourtois-settings-stack 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
|
|
84
|
+
return (_jsxs("section", { ref: focusSetup, className: "tourtois-settings-stack 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: providerLabel(view.data.providers, key.provider) }), actions: _jsx(KeycapButton, { density: "compact", tone: "destructive", label: "Remove", "aria-label": `Remove ${key.label}`, disabled: remove.isPending, onClick: () => {
|
|
87
85
|
remove.mutate(key.keyId);
|
|
88
|
-
} }) }) }, key.keyId)))] }), remove.isError ? (_jsx("p", { role: "alert", children: "The model key could not be removed." })) : null, _jsx(AddModelKey, { onAdd: async (draft) => {
|
|
86
|
+
} }) }) }, key.keyId)))] }), remove.isError ? (_jsx("p", { role: "alert", children: "The model key could not be removed." })) : null, _jsx(AddModelKey, { providers: view.data.providers, onAdd: async (draft) => {
|
|
89
87
|
await port.addKey(draft);
|
|
90
88
|
await refresh();
|
|
91
89
|
setFormKey((current) => current + 1);
|
|
@@ -106,18 +104,19 @@ export function ModelKeys({ port, focus = false, }) {
|
|
|
106
104
|
: "Model unavailable for tools")), disconnected || selectedKey === undefined
|
|
107
105
|
? ""
|
|
108
106
|
: ` · ${selectedKey.label}`] }, agent.agentId));
|
|
109
|
-
})] }), agents.length > 0 ? (_jsx(ChooseAgentModel, { agents: agents, keys: keys, catalogs: view.data.catalogs, models: view.data.models, onChoose: async (agentId, draft) => {
|
|
107
|
+
})] }), agents.length > 0 ? (_jsx(ChooseAgentModel, { agents: agents, keys: keys, providers: view.data.providers, catalogs: view.data.catalogs, models: view.data.models, onChoose: async (agentId, draft) => {
|
|
110
108
|
await port.chooseModel(agentId, draft);
|
|
111
109
|
await refresh();
|
|
112
110
|
} }, keys.map((key) => key.keyId).join(":"))) : null] })) : null] }));
|
|
113
111
|
}
|
|
114
|
-
function AddModelKey({ onAdd, }) {
|
|
112
|
+
function AddModelKey({ providers, onAdd, }) {
|
|
113
|
+
const needsAccountId = (provider) => providers.find((choice) => choice.id === provider)?.accountId === true;
|
|
115
114
|
const schema = useMemo(() => z.object({
|
|
116
115
|
provider: providerSchema.meta(field({
|
|
117
116
|
label: "Provider",
|
|
118
117
|
icon: _jsx(Sigil, { name: "internet" }),
|
|
119
118
|
kind: "select",
|
|
120
|
-
options: providers,
|
|
119
|
+
options: Object.fromEntries(providers.map((choice) => [choice.id, choice.label])),
|
|
121
120
|
})),
|
|
122
121
|
label: z
|
|
123
122
|
.string()
|
|
@@ -148,19 +147,19 @@ function AddModelKey({ onAdd, }) {
|
|
|
148
147
|
help: "Sealed on this instance and never shown again.",
|
|
149
148
|
kind: "secret",
|
|
150
149
|
})),
|
|
151
|
-
}), []);
|
|
150
|
+
}), [providers]);
|
|
152
151
|
const add = useMutation({ mutationFn: onAdd });
|
|
153
152
|
const model = useSchemaForm({
|
|
154
153
|
schema,
|
|
155
154
|
defaultValues: {
|
|
156
|
-
provider: "
|
|
155
|
+
provider: providers[0]?.id ?? "",
|
|
157
156
|
label: "",
|
|
158
157
|
apiKey: "",
|
|
159
158
|
accountId: "",
|
|
160
159
|
},
|
|
161
160
|
onSubmit: async (value) => {
|
|
162
161
|
try {
|
|
163
|
-
if (value.provider
|
|
162
|
+
if (needsAccountId(value.provider) &&
|
|
164
163
|
!/^[a-f0-9]{32}$/u.test(value.accountId))
|
|
165
164
|
return {
|
|
166
165
|
kind: "refused",
|
|
@@ -173,7 +172,7 @@ function AddModelKey({ onAdd, }) {
|
|
|
173
172
|
label: value.label,
|
|
174
173
|
apiKey: value.apiKey,
|
|
175
174
|
};
|
|
176
|
-
await add.mutateAsync(value.provider
|
|
175
|
+
await add.mutateAsync(needsAccountId(value.provider)
|
|
177
176
|
? { ...draft, accountId: value.accountId }
|
|
178
177
|
: draft);
|
|
179
178
|
return { kind: "saved" };
|
|
@@ -188,11 +187,11 @@ function AddModelKey({ onAdd, }) {
|
|
|
188
187
|
}
|
|
189
188
|
},
|
|
190
189
|
});
|
|
191
|
-
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" }) }), children: _jsx(model.form.Subscribe, { selector: (state) => state.values.provider, children: (provider) => (_jsx(FormFields, { model: model, names: provider
|
|
190
|
+
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" }) }), children: _jsx(model.form.Subscribe, { selector: (state) => state.values.provider, children: (provider) => (_jsx(FormFields, { model: model, names: needsAccountId(provider)
|
|
192
191
|
? ["provider", "label", "accountId", "apiKey"]
|
|
193
192
|
: ["provider", "label", "apiKey"], layout: "columns" })) }) }));
|
|
194
193
|
}
|
|
195
|
-
function ChooseAgentModel({ agents, keys, catalogs, models, onChoose, }) {
|
|
194
|
+
function ChooseAgentModel({ agents, keys, providers, catalogs, models, onChoose, }) {
|
|
196
195
|
const schema = useMemo(() => z.object({
|
|
197
196
|
agentId: z.string().meta(field({
|
|
198
197
|
label: "Agent",
|
|
@@ -252,7 +251,7 @@ function ChooseAgentModel({ agents, keys, catalogs, models, onChoose, }) {
|
|
|
252
251
|
model.form.setFieldValue("modelId", "");
|
|
253
252
|
}, children: [_jsx("option", { value: defaultSource, children: models === "missing"
|
|
254
253
|
? "Instance default — not connected"
|
|
255
|
-
: "Instance default model" }), keys.map((key) => (_jsxs("option", { value: key.keyId, children: [key.label, " (", providers
|
|
254
|
+
: "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) => {
|
|
256
255
|
const key = keys.find((item) => item.keyId === source);
|
|
257
256
|
if (key === undefined)
|
|
258
257
|
return null;
|
package/dist/WebsiteChannels.js
CHANGED
|
@@ -16,6 +16,7 @@ import { Sigil } from "@fungi.computer/sigils";
|
|
|
16
16
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
17
17
|
import { useId, useMemo, useState } from "react";
|
|
18
18
|
import * as z from "zod";
|
|
19
|
+
import { ChannelConversations } from "./ChannelConversations.js";
|
|
19
20
|
import { tourtoisQueryKeys } from "./queries.js";
|
|
20
21
|
import { requestIds } from "./team-directory.js";
|
|
21
22
|
const exampleAddress = "https://example.com";
|
|
@@ -340,33 +341,8 @@ function WebsiteRow({ port, copy, site, notice, embedOrigin, agentName, open, ju
|
|
|
340
341
|
setConfirmingKey(false);
|
|
341
342
|
} })] })) : (_jsx(KeycapButton, { density: "compact", label: "New site key", disabled: patch.isPending, onClick: () => {
|
|
342
343
|
setConfirmingKey(true);
|
|
343
|
-
} }))] }) })] }), notice, patch.isError ? _jsx("p", { role: "alert", children: patch.error.message }) : null, _jsx(
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
const [shown, setShown] = useState(false);
|
|
348
|
-
const [reading, setReading] = useState(null);
|
|
349
|
-
const list = useQuery({
|
|
350
|
-
queryKey: tourtoisQueryKeys.websiteConversations(port.scope, bindingId),
|
|
351
|
-
enabled: shown,
|
|
352
|
-
retry: false,
|
|
353
|
-
queryFn: () => port.conversations(site),
|
|
354
|
-
});
|
|
355
|
-
const transcript = useQuery({
|
|
356
|
-
queryKey: tourtoisQueryKeys.websiteTranscript(port.scope, bindingId, reading ?? ""),
|
|
357
|
-
enabled: reading !== null,
|
|
358
|
-
retry: false,
|
|
359
|
-
queryFn: () => reading === null
|
|
360
|
-
? Promise.reject(new Error("No conversation is open"))
|
|
361
|
-
: port.transcript(site, reading),
|
|
362
|
-
});
|
|
363
|
-
return (_jsxs("section", { className: "grid gap-sm", "aria-label": "Conversations", children: [_jsx(DataListHeading, { level: 3, children: "Conversations" }), shown ? null : (_jsx(KeycapButton, { density: "compact", label: "Show conversations", onClick: () => {
|
|
364
|
-
setShown(true);
|
|
365
|
-
} })), list.isPending && shown ? _jsx("p", { role: "status", children: "Loading\u2026" }) : null, list.isError ? (_jsx("p", { role: "alert", children: "Conversations could not be loaded." })) : null, list.data?.length === 0 ? (_jsx("p", { className: "text-sm", children: "No visitor has written yet." })) : null, list.data !== undefined && list.data.length > 0 ? (_jsx(DataList, { density: "compact", "aria-label": "Website conversations", children: list.data.map((conversation) => (_jsx(FactRow, { label: conversation.label, children: _jsxs("span", { className: "flex flex-wrap items-center gap-sm", children: [_jsx("span", { className: "text-sm", children: new Date(conversation.lastEventAt).toLocaleString() }), _jsx(KeycapButton, { density: "compact", label: reading === conversation.sessionId
|
|
366
|
-
? "Close transcript"
|
|
367
|
-
: "Read transcript", "aria-label": `${reading === conversation.sessionId ? "Close" : "Read"} transcript of ${conversation.label}`, onClick: () => {
|
|
368
|
-
setReading(reading === conversation.sessionId
|
|
369
|
-
? null
|
|
370
|
-
: conversation.sessionId);
|
|
371
|
-
} })] }) }, conversation.sessionId))) })) : null, reading !== null && transcript.isPending ? (_jsx("p", { role: "status", children: "Loading\u2026" })) : null, transcript.isError ? (_jsx("p", { role: "alert", children: "This conversation could not be read." })) : null, reading !== null && transcript.data ? (_jsx("ol", { "aria-label": "Transcript", className: "grid gap-xs", children: transcript.data.lines.map((line, index) => (_jsxs("li", { className: "text-sm", children: [_jsxs("strong", { children: [line.role === "visitor" ? "Visitor" : "Agent", ":"] }), " ", line.text] }, `${String(index)}-${line.role}`))) })) : null] }));
|
|
344
|
+
} }))] }) })] }), notice, patch.isError ? _jsx("p", { role: "alert", children: patch.error.message }) : null, _jsx(ChannelConversations, { keys: {
|
|
345
|
+
list: tourtoisQueryKeys.websiteConversations(port.scope, site.bindingId),
|
|
346
|
+
transcript: (sessionId) => tourtoisQueryKeys.websiteTranscript(port.scope, site.bindingId, sessionId),
|
|
347
|
+
}, load: () => port.conversations(site), read: (sessionId) => port.transcript(site, sessionId), speaker: "Visitor", empty: "No visitor has written yet.", label: "Website conversations" })] })) : null] }) }));
|
|
372
348
|
}
|
|
@@ -18,6 +18,7 @@ import { createOysterSetupPort, oysterSetupSchema } from "./oyster-setup.js";
|
|
|
18
18
|
import { modelKeysViewSchema } from "./ModelKeys.js";
|
|
19
19
|
import { settingsLocationSchema } from "./settings-routes.js";
|
|
20
20
|
import { OysterComputers, createOysterComputersPort, } from "./OysterComputers.js";
|
|
21
|
+
import { DiscordChannels, createOysterDiscordChannelsPort, } from "./DiscordChannels.js";
|
|
21
22
|
import { WebsiteChannels, createOysterWebsiteChannelsPort, } from "./WebsiteChannels.js";
|
|
22
23
|
import { useSettingsLeaveGuard, } from "./settings-leave-guard.js";
|
|
23
24
|
async function json(path) {
|
|
@@ -287,7 +288,8 @@ export const mountPlatformControls = (root, input) => {
|
|
|
287
288
|
const computersPort = createOysterComputersPort(new URL(window.location.origin));
|
|
288
289
|
const computers = () => (_jsx(OysterComputers, { teamId: input.teamId, port: computersPort, onChanged: controls.changed }));
|
|
289
290
|
const websitesPort = createOysterWebsiteChannelsPort(new URL(window.location.origin));
|
|
290
|
-
const
|
|
291
|
+
const discordPort = createOysterDiscordChannelsPort(new URL(window.location.origin));
|
|
292
|
+
const channels = () => (_jsx(WebsiteChannels, { port: websitesPort, copy: oysterWebsiteCopy, children: _jsx(DiscordChannels, { port: discordPort }) }));
|
|
291
293
|
const setup = createOysterSetupPort({
|
|
292
294
|
readFacts: async () => oysterSetupSchema.parse(await json("/api/setup")),
|
|
293
295
|
openSettings: (location) => {
|
package/dist/queries.d.ts
CHANGED
|
@@ -6,6 +6,10 @@ export declare const tourtoisQueryKeys: {
|
|
|
6
6
|
oysterInternetAccess: (teamId: string, computerId: string) => readonly ["tourtois", "oyster-internet-access", string, string];
|
|
7
7
|
oysterKeepAwake: (teamId: string, computerId: string) => readonly ["tourtois", "oyster-keep-awake", string, string];
|
|
8
8
|
oysterEgressPolicy: (teamId: string) => readonly ["tourtois", "oyster-egress-policy", string];
|
|
9
|
+
discordChannels: (scope: string) => readonly ["tourtois", "discord-channels", string];
|
|
10
|
+
discordConversations: (scope: string, bindingId: string) => readonly ["tourtois", "discord-channels", string, string];
|
|
11
|
+
discordUndelivered: (scope: string, bindingId: string) => readonly ["tourtois", "discord-channels", string, "undelivered", string];
|
|
12
|
+
discordTranscript: (scope: string, bindingId: string, sessionId: string) => readonly ["tourtois", "discord-channels", string, string, string];
|
|
9
13
|
websiteChannels: (scope: string) => readonly ["tourtois", "website-channels", string];
|
|
10
14
|
websiteConversations: (scope: string, bindingId: string) => readonly ["tourtois", "website-channels", string, string];
|
|
11
15
|
websiteTranscript: (scope: string, bindingId: string, sessionId: string) => readonly ["tourtois", "website-channels", string, string, string];
|
package/dist/queries.js
CHANGED
|
@@ -6,6 +6,17 @@ export const tourtoisQueryKeys = {
|
|
|
6
6
|
oysterInternetAccess: (teamId, computerId) => ["tourtois", "oyster-internet-access", teamId, computerId],
|
|
7
7
|
oysterKeepAwake: (teamId, computerId) => ["tourtois", "oyster-keep-awake", teamId, computerId],
|
|
8
8
|
oysterEgressPolicy: (teamId) => ["tourtois", "oyster-egress-policy", teamId],
|
|
9
|
+
discordChannels: (scope) => ["tourtois", "discord-channels", scope],
|
|
10
|
+
discordConversations: (scope, bindingId) => [...tourtoisQueryKeys.discordChannels(scope), bindingId],
|
|
11
|
+
discordUndelivered: (scope, bindingId) => [
|
|
12
|
+
...tourtoisQueryKeys.discordChannels(scope),
|
|
13
|
+
"undelivered",
|
|
14
|
+
bindingId,
|
|
15
|
+
],
|
|
16
|
+
discordTranscript: (scope, bindingId, sessionId) => [
|
|
17
|
+
...tourtoisQueryKeys.discordConversations(scope, bindingId),
|
|
18
|
+
sessionId,
|
|
19
|
+
],
|
|
9
20
|
websiteChannels: (scope) => ["tourtois", "website-channels", scope],
|
|
10
21
|
websiteConversations: (scope, bindingId) => [...tourtoisQueryKeys.websiteChannels(scope), bindingId],
|
|
11
22
|
websiteTranscript: (scope, bindingId, sessionId) => [
|
|
@@ -129,6 +129,39 @@
|
|
|
129
129
|
min-width: 0;
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
+
/* A conversation is one column: its line, then its transcript when open. */
|
|
133
|
+
.tourtois-conversation {
|
|
134
|
+
display: grid;
|
|
135
|
+
flex: 1;
|
|
136
|
+
grid-column: 1 / -1;
|
|
137
|
+
width: 100%;
|
|
138
|
+
min-width: 0;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/* A conversation line: its name gets the room, its date and button wrap below
|
|
142
|
+
when the line is narrow. */
|
|
143
|
+
.tourtois-conversation-row {
|
|
144
|
+
display: flex;
|
|
145
|
+
flex-wrap: wrap;
|
|
146
|
+
align-items: center;
|
|
147
|
+
justify-content: space-between;
|
|
148
|
+
gap: var(--spacing-xs) var(--spacing-md);
|
|
149
|
+
min-width: 0;
|
|
150
|
+
}
|
|
151
|
+
/* An open transcript sits inside its own row, under the name it belongs to. */
|
|
152
|
+
.tourtois-conversation-transcript {
|
|
153
|
+
display: grid;
|
|
154
|
+
gap: var(--spacing-xs);
|
|
155
|
+
margin-top: var(--spacing-sm);
|
|
156
|
+
min-width: 0;
|
|
157
|
+
}
|
|
158
|
+
.tourtois-conversation-name {
|
|
159
|
+
flex: 1 1 12rem;
|
|
160
|
+
min-width: 0;
|
|
161
|
+
overflow-wrap: anywhere;
|
|
162
|
+
font-weight: 500;
|
|
163
|
+
}
|
|
164
|
+
|
|
132
165
|
/* The Settings panel owns scrolling; its GitHub contents retain their natural
|
|
133
166
|
height rather than shrinking the form and feedback inside the flex column. */
|
|
134
167
|
.tourtois-github-settings > * {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fungi.computer/tourtois",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"author": "Levi <levi@technomancy.dev>",
|
|
5
5
|
"description": "Tourtois: the trusted desktop shell for Oyster and Fungi, with host-supplied Settings sections",
|
|
6
6
|
"type": "module",
|
|
@@ -77,15 +77,15 @@
|
|
|
77
77
|
"LICENSE"
|
|
78
78
|
],
|
|
79
79
|
"dependencies": {
|
|
80
|
-
"@fungi.computer/agar": "0.10.
|
|
80
|
+
"@fungi.computer/agar": "0.10.1",
|
|
81
81
|
"@fungi.computer/cairn": "0.1.0",
|
|
82
82
|
"@fungi.computer/caps": "0.11.3",
|
|
83
83
|
"@fungi.computer/ear-tag": "0.1.0",
|
|
84
84
|
"@fungi.computer/gills": "0.3.0",
|
|
85
85
|
"@fungi.computer/maosaico": "0.1.2",
|
|
86
|
-
"@fungi.computer/parakeet": "0.11.
|
|
86
|
+
"@fungi.computer/parakeet": "0.11.22",
|
|
87
87
|
"@fungi.computer/puffball": "0.5.5",
|
|
88
|
-
"@fungi.computer/shiitake": "0.
|
|
88
|
+
"@fungi.computer/shiitake": "0.14.0",
|
|
89
89
|
"@fungi.computer/sigils": "0.3.1",
|
|
90
90
|
"@fungi.computer/sprite": "0.2.2",
|
|
91
91
|
"@fungi.computer/sprite-zdog": "0.3.2",
|
|
@@ -100,13 +100,13 @@
|
|
|
100
100
|
"lucide-react": "0.460.0",
|
|
101
101
|
"react-dom": "18.3.1",
|
|
102
102
|
"zod": "4.4.3",
|
|
103
|
-
"@fungi.computer/nacre": "0.5.
|
|
103
|
+
"@fungi.computer/nacre": "0.5.3"
|
|
104
104
|
},
|
|
105
105
|
"peerDependencies": {
|
|
106
106
|
"react": "^18.3.1 || ^19.2.0"
|
|
107
107
|
},
|
|
108
108
|
"devDependencies": {
|
|
109
|
-
"@fungi.computer/oyster": "0.
|
|
109
|
+
"@fungi.computer/oyster": "0.23.0",
|
|
110
110
|
"@cloudflare/workers-types": "5.20260828.1",
|
|
111
111
|
"@fungi.computer/forage": "0.3.5",
|
|
112
112
|
"@fungi.computer/mycelium": "0.3.8",
|