@fungi.computer/oyster 0.21.8 → 0.22.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/agents.d.ts +1 -0
- package/dist/agents.js +4 -9
- package/dist/channels/discord-binding.d.ts +132 -0
- package/dist/channels/discord-binding.js +234 -0
- package/dist/channels/discord-interactions.d.ts +190 -0
- package/dist/channels/discord-interactions.js +225 -0
- package/dist/standalone/agent-model-choice.d.ts +2 -14
- package/dist/standalone/agent-model-choice.js +65 -35
- package/dist/standalone/agents.d.ts +36 -4
- package/dist/standalone/agents.js +176 -5
- package/dist/standalone/computer.js +3 -0
- package/dist/standalone/discord-channels.d.ts +67 -0
- package/dist/standalone/discord-channels.js +212 -0
- package/dist/standalone/model-key-providers.d.ts +13 -0
- package/dist/standalone/model-key-providers.js +11 -0
- package/dist/standalone/model-key-vault.d.ts +9 -5
- package/dist/standalone/model-key-vault.js +9 -9
- package/dist/standalone/owner-routes.js +4 -1
- package/dist/standalone/worker-routes.d.ts +2 -0
- package/dist/standalone/worker-routes.js +1 -0
- package/dist/standalone/worker.js +4 -0
- package/docs/api/agents.md +36 -10
- package/docs/api/workers-git.md +7 -7
- package/package.json +3 -3
package/dist/agents.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { AppSetup } from "@fungi.computer/agar";
|
|
|
2
2
|
import type { TenantPrincipal } from "./tenant.js";
|
|
3
3
|
import type { ModelKeyRecord, ModelKeyRefusal, ModelKeySealInput } from "./standalone/model-key-vault.js";
|
|
4
4
|
export type { ModelKeyRecord, ModelKeyRefusal, ModelKeySealInput, } from "./standalone/model-key-vault.js";
|
|
5
|
+
export type { StandaloneModelKeyProvider } from "./standalone/model-key-providers.js";
|
|
5
6
|
import { type Routes } from "./authorization.js";
|
|
6
7
|
import type { AppToolPolicy } from "./apps/tools.js";
|
|
7
8
|
import * as Schema from "effect/Schema";
|
package/dist/agents.js
CHANGED
|
@@ -4,6 +4,7 @@ import { z } from "zod";
|
|
|
4
4
|
import {} from "./host.js";
|
|
5
5
|
import { agentAvatarSchema } from "./agent-avatars.js";
|
|
6
6
|
import { computerTargetParam } from "./computer-target.js";
|
|
7
|
+
import { modelKeyProviderChoices } from "./standalone/model-key-providers.js";
|
|
7
8
|
import { supportedAgentModels } from "./standalone/agent-model-choice.js";
|
|
8
9
|
import { agentAvatar, agentList, agentManage, agentRename, agentSessionRead, agentSessionWrite, agentToolPolicy, } from "@fungi.computer/agar/agents";
|
|
9
10
|
/** At most this many connections per Agent. */
|
|
@@ -374,12 +375,11 @@ export function agentApprovalRoutes(agents) {
|
|
|
374
375
|
})));
|
|
375
376
|
const keys = await agents.modelKeys();
|
|
376
377
|
const providers = [...new Set(keys.map((key) => key.provider))];
|
|
377
|
-
const catalogs = Object.fromEntries(await Promise.all(providers
|
|
378
|
-
.filter((provider) => provider !== "openai-compatible")
|
|
379
|
-
.map(async (provider) => [provider, await supportedAgentModels(provider)])));
|
|
378
|
+
const catalogs = Object.fromEntries(await Promise.all(providers.map(async (provider) => [provider, await supportedAgentModels(provider)])));
|
|
380
379
|
return Response.json({
|
|
381
380
|
agents: rows,
|
|
382
381
|
catalogs,
|
|
382
|
+
providers: modelKeyProviderChoices,
|
|
383
383
|
models: (await agents.setup()).models,
|
|
384
384
|
});
|
|
385
385
|
})
|
|
@@ -422,12 +422,7 @@ export function agentApprovalRoutes(agents) {
|
|
|
422
422
|
}
|
|
423
423
|
const keyIdPattern = /^cred_[A-Za-z0-9_-]{1,64}$/u;
|
|
424
424
|
const sealRequestSchema = Schema.Struct({
|
|
425
|
-
provider: Schema.Literals(
|
|
426
|
-
"anthropic",
|
|
427
|
-
"openai",
|
|
428
|
-
"openrouter",
|
|
429
|
-
"cloudflare-workers-ai",
|
|
430
|
-
]),
|
|
425
|
+
provider: Schema.Literals(modelKeyProviderChoices.map((choice) => choice.id)),
|
|
431
426
|
accountId: Schema.optional(Schema.String.check(Schema.isPattern(/^[a-f0-9]{32}$/u))),
|
|
432
427
|
label: Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(64)),
|
|
433
428
|
apiKey: Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(16_384)),
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { VisitorSql } from "./visitors.js";
|
|
3
|
+
/** The exact selector of a Discord binding; secrets are never part of it. */
|
|
4
|
+
declare const discordSelectorSchema: z.ZodObject<{
|
|
5
|
+
provider: z.ZodLiteral<"discord">;
|
|
6
|
+
applicationId: z.ZodString;
|
|
7
|
+
publicKey: z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>;
|
|
8
|
+
guildIds: z.ZodArray<z.ZodString>;
|
|
9
|
+
channelIds: z.ZodArray<z.ZodString>;
|
|
10
|
+
}, z.core.$strict>;
|
|
11
|
+
/** One parsed Discord selector. */
|
|
12
|
+
export type DiscordSelector = z.infer<typeof discordSelectorSchema>;
|
|
13
|
+
/** A Discord application's Agent: one owner Agent behind one application. */
|
|
14
|
+
declare const discordBindingSchema: z.ZodObject<{
|
|
15
|
+
bindingId: z.ZodString;
|
|
16
|
+
agentId: z.ZodString;
|
|
17
|
+
selector: z.ZodObject<{
|
|
18
|
+
provider: z.ZodLiteral<"discord">;
|
|
19
|
+
applicationId: z.ZodString;
|
|
20
|
+
publicKey: z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>;
|
|
21
|
+
guildIds: z.ZodArray<z.ZodString>;
|
|
22
|
+
channelIds: z.ZodArray<z.ZodString>;
|
|
23
|
+
}, z.core.$strict>;
|
|
24
|
+
lifecycle: z.ZodEnum<{
|
|
25
|
+
active: "active";
|
|
26
|
+
suspended: "suspended";
|
|
27
|
+
}>;
|
|
28
|
+
authorityVersion: z.ZodNumber;
|
|
29
|
+
persona: z.ZodString;
|
|
30
|
+
}, z.core.$strict>;
|
|
31
|
+
/** One Discord binding. */
|
|
32
|
+
export type DiscordBinding = z.infer<typeof discordBindingSchema>;
|
|
33
|
+
/** Owner input for a new Discord binding, the bot token included. */
|
|
34
|
+
export declare const createDiscordInputSchema: z.ZodObject<{
|
|
35
|
+
agentId: z.ZodString;
|
|
36
|
+
applicationId: z.ZodString;
|
|
37
|
+
publicKey: z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>;
|
|
38
|
+
guildIds: z.ZodArray<z.ZodString>;
|
|
39
|
+
channelIds: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
40
|
+
persona: z.ZodDefault<z.ZodString>;
|
|
41
|
+
botToken: z.ZodString;
|
|
42
|
+
}, z.core.$strict>;
|
|
43
|
+
/** Owner input for a new Discord binding. */
|
|
44
|
+
export type CreateDiscordInput = z.input<typeof createDiscordInputSchema>;
|
|
45
|
+
declare const discordFactsSchema: z.ZodObject<{
|
|
46
|
+
agentId: z.ZodString;
|
|
47
|
+
publicKey: z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>;
|
|
48
|
+
applicationId: z.ZodString;
|
|
49
|
+
guildIds: z.ZodArray<z.ZodString>;
|
|
50
|
+
channelIds: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
51
|
+
persona: z.ZodDefault<z.ZodString>;
|
|
52
|
+
}, z.core.$strict>;
|
|
53
|
+
/** The same input without the secret, which is sealed before the binding exists. */
|
|
54
|
+
type DiscordBindingFacts = z.input<typeof discordFactsSchema>;
|
|
55
|
+
/** Owner edit of one Discord binding, fenced by the version the owner last read. */
|
|
56
|
+
export declare const updateDiscordInputSchema: z.ZodObject<{
|
|
57
|
+
expectedVersion: z.ZodNumber;
|
|
58
|
+
publicKey: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>>;
|
|
59
|
+
guildIds: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
60
|
+
channelIds: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
61
|
+
persona: z.ZodOptional<z.ZodString>;
|
|
62
|
+
lifecycle: z.ZodOptional<z.ZodEnum<{
|
|
63
|
+
active: "active";
|
|
64
|
+
suspended: "suspended";
|
|
65
|
+
}>>;
|
|
66
|
+
botToken: z.ZodOptional<z.ZodString>;
|
|
67
|
+
}, z.core.$strict>;
|
|
68
|
+
/** Owner edit of one Discord binding. */
|
|
69
|
+
export type UpdateDiscordInput = z.input<typeof updateDiscordInputSchema>;
|
|
70
|
+
declare const discordEditSchema: z.ZodObject<{
|
|
71
|
+
lifecycle: z.ZodOptional<z.ZodEnum<{
|
|
72
|
+
active: "active";
|
|
73
|
+
suspended: "suspended";
|
|
74
|
+
}>>;
|
|
75
|
+
expectedVersion: z.ZodNumber;
|
|
76
|
+
publicKey: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>>;
|
|
77
|
+
guildIds: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
78
|
+
channelIds: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
79
|
+
persona: z.ZodOptional<z.ZodString>;
|
|
80
|
+
}, z.core.$strict>;
|
|
81
|
+
type DiscordEdit = z.input<typeof discordEditSchema>;
|
|
82
|
+
/** Closed owner failures; routes map each to one status. */
|
|
83
|
+
export declare class DiscordBindingError extends Error {
|
|
84
|
+
readonly code: "not_found" | "version_conflict" | "request_conflict" | "application_taken" | "invalid_input";
|
|
85
|
+
constructor(code: "not_found" | "version_conflict" | "request_conflict" | "application_taken" | "invalid_input");
|
|
86
|
+
}
|
|
87
|
+
/** Is this standalone channel binding a Discord application's? */
|
|
88
|
+
export declare function isDiscordBinding(binding: Readonly<{
|
|
89
|
+
selector: Readonly<{
|
|
90
|
+
provider: string;
|
|
91
|
+
}>;
|
|
92
|
+
}>): binding is DiscordBinding;
|
|
93
|
+
/**
|
|
94
|
+
* Does this guild and channel belong to the binding? An interaction outside
|
|
95
|
+
* the owner's allowlist is not this Agent's to answer.
|
|
96
|
+
*/
|
|
97
|
+
export declare function discordPlaceAllowed(selector: DiscordSelector, place: Readonly<{
|
|
98
|
+
guildId: string;
|
|
99
|
+
channelId: string;
|
|
100
|
+
}>): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* One standalone owner's Discord bindings, behind a SQL port. The owner
|
|
103
|
+
* authorizes the caller; this module fences versions and keeps one binding
|
|
104
|
+
* per Discord application.
|
|
105
|
+
*/
|
|
106
|
+
export declare function createDiscordBindings(options: Readonly<{
|
|
107
|
+
sql: VisitorSql;
|
|
108
|
+
transactionSync: <T>(body: () => T) => T;
|
|
109
|
+
/** Whether the Agent is in this owner's directory. */
|
|
110
|
+
agentExists: (agentId: string) => boolean;
|
|
111
|
+
}>): {
|
|
112
|
+
read: (bindingId: string) => DiscordBinding | null;
|
|
113
|
+
list: () => readonly DiscordBinding[];
|
|
114
|
+
/** Host-only: the sealed credential that holds this binding's bot token. */
|
|
115
|
+
tokenCredentialId: (bindingId: string) => string | null;
|
|
116
|
+
/**
|
|
117
|
+
* The binding this request already created, or null for a new request. A
|
|
118
|
+
* replay must not seal the token again; changed facts conflict.
|
|
119
|
+
*/
|
|
120
|
+
replay: (rawFacts: DiscordBindingFacts, requestId: string) => DiscordBinding | null;
|
|
121
|
+
/**
|
|
122
|
+
* Create once per request identity; a replay returns the first result. The
|
|
123
|
+
* secret is already sealed: `facts` carry none and `tokenCredentialId`
|
|
124
|
+
* names it.
|
|
125
|
+
*/
|
|
126
|
+
create: (rawFacts: DiscordBindingFacts, tokenCredentialId: string, requestId: string) => DiscordBinding;
|
|
127
|
+
/** Apply one edit; a replaced token's new credential id is already sealed. */
|
|
128
|
+
update: (bindingId: string, rawEdit: DiscordEdit, replacementCredentialId: string | null) => DiscordBinding;
|
|
129
|
+
};
|
|
130
|
+
/** The standalone Discord binding owner. */
|
|
131
|
+
export type DiscordBindings = ReturnType<typeof createDiscordBindings>;
|
|
132
|
+
export {};
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { discordSnowflakeSchema } from "./binding.js";
|
|
3
|
+
import { channelIds } from "./ids.js";
|
|
4
|
+
/**
|
|
5
|
+
* The Discord channel's neutral binding rules. The owner brings their own
|
|
6
|
+
* Discord application: its id and public key are facts of the binding, and its
|
|
7
|
+
* bot token is sealed elsewhere and only named here by an owner-only column.
|
|
8
|
+
* Fungi holds no Discord credential of its own.
|
|
9
|
+
*/
|
|
10
|
+
/** Ed25519 public key of a Discord application: 32 bytes, as 64 hex digits. */
|
|
11
|
+
const publicKeySchema = z
|
|
12
|
+
.string()
|
|
13
|
+
.regex(/^[0-9a-f]{64}$/iu, "Public key must be 64 hex digits")
|
|
14
|
+
.transform((value) => value.toLowerCase());
|
|
15
|
+
const guildIdsSchema = z.array(discordSnowflakeSchema).min(1).max(16);
|
|
16
|
+
/** Empty means every channel of the bound guilds. */
|
|
17
|
+
const channelIdsSchema = z.array(discordSnowflakeSchema).max(64);
|
|
18
|
+
/** The bot token as Discord issues it: dot-separated base64url segments. */
|
|
19
|
+
const discordBotTokenSchema = z
|
|
20
|
+
.string()
|
|
21
|
+
.regex(/^[\w-]{10,64}\.[\w-]{4,16}\.[\w-]{20,128}$/u, "Invalid bot token");
|
|
22
|
+
/** The exact selector of a Discord binding; secrets are never part of it. */
|
|
23
|
+
const discordSelectorSchema = z.strictObject({
|
|
24
|
+
provider: z.literal("discord"),
|
|
25
|
+
applicationId: discordSnowflakeSchema,
|
|
26
|
+
publicKey: publicKeySchema,
|
|
27
|
+
guildIds: guildIdsSchema,
|
|
28
|
+
channelIds: channelIdsSchema,
|
|
29
|
+
});
|
|
30
|
+
/** A Discord application's Agent: one owner Agent behind one application. */
|
|
31
|
+
const discordBindingSchema = z.strictObject({
|
|
32
|
+
bindingId: z
|
|
33
|
+
.string()
|
|
34
|
+
.refine((value) => channelIds.binding.is(value), "Invalid binding"),
|
|
35
|
+
agentId: z.string().min(1).max(256),
|
|
36
|
+
selector: discordSelectorSchema,
|
|
37
|
+
lifecycle: z.enum(["active", "suspended"]),
|
|
38
|
+
/** Increments on every owner edit of this binding. */
|
|
39
|
+
authorityVersion: z.number().int().positive(),
|
|
40
|
+
/** Owner persona text, placed after the fixed channel preamble. */
|
|
41
|
+
persona: z.string().max(4_000),
|
|
42
|
+
});
|
|
43
|
+
/** Owner input for a new Discord binding, the bot token included. */
|
|
44
|
+
export const createDiscordInputSchema = z.strictObject({
|
|
45
|
+
agentId: z.string().min(1).max(256),
|
|
46
|
+
applicationId: discordSnowflakeSchema,
|
|
47
|
+
publicKey: publicKeySchema,
|
|
48
|
+
guildIds: guildIdsSchema,
|
|
49
|
+
channelIds: channelIdsSchema.default([]),
|
|
50
|
+
persona: z.string().max(4_000).default(""),
|
|
51
|
+
botToken: discordBotTokenSchema,
|
|
52
|
+
});
|
|
53
|
+
const discordFactsSchema = createDiscordInputSchema.omit({ botToken: true });
|
|
54
|
+
/** Owner edit of one Discord binding, fenced by the version the owner last read. */
|
|
55
|
+
export const updateDiscordInputSchema = z.strictObject({
|
|
56
|
+
expectedVersion: z.number().int().positive(),
|
|
57
|
+
publicKey: publicKeySchema.optional(),
|
|
58
|
+
guildIds: guildIdsSchema.optional(),
|
|
59
|
+
channelIds: channelIdsSchema.optional(),
|
|
60
|
+
persona: z.string().max(4_000).optional(),
|
|
61
|
+
lifecycle: z.enum(["active", "suspended"]).optional(),
|
|
62
|
+
/** Replace the sealed bot token. */
|
|
63
|
+
botToken: discordBotTokenSchema.optional(),
|
|
64
|
+
});
|
|
65
|
+
const discordEditSchema = updateDiscordInputSchema.omit({ botToken: true });
|
|
66
|
+
/** Closed owner failures; routes map each to one status. */
|
|
67
|
+
export class DiscordBindingError extends Error {
|
|
68
|
+
code;
|
|
69
|
+
constructor(code) {
|
|
70
|
+
super(code);
|
|
71
|
+
this.code = code;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** Is this standalone channel binding a Discord application's? */
|
|
75
|
+
export function isDiscordBinding(binding) {
|
|
76
|
+
return binding.selector.provider === "discord";
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Does this guild and channel belong to the binding? An interaction outside
|
|
80
|
+
* the owner's allowlist is not this Agent's to answer.
|
|
81
|
+
*/
|
|
82
|
+
export function discordPlaceAllowed(selector, place) {
|
|
83
|
+
return (selector.guildIds.includes(place.guildId) &&
|
|
84
|
+
(selector.channelIds.length === 0 ||
|
|
85
|
+
selector.channelIds.includes(place.channelId)));
|
|
86
|
+
}
|
|
87
|
+
const payloadRow = z.strictObject({ payload: z.string() });
|
|
88
|
+
const receiptRow = payloadRow.extend({ input_json: z.string().nullable() });
|
|
89
|
+
const tokenRow = z.strictObject({ token_credential_id: z.string() });
|
|
90
|
+
const requestIdSchema = z.string().min(1).max(128);
|
|
91
|
+
/**
|
|
92
|
+
* One standalone owner's Discord bindings, behind a SQL port. The owner
|
|
93
|
+
* authorizes the caller; this module fences versions and keeps one binding
|
|
94
|
+
* per Discord application.
|
|
95
|
+
*/
|
|
96
|
+
export function createDiscordBindings(options) {
|
|
97
|
+
const { sql } = options;
|
|
98
|
+
const ensure = () => {
|
|
99
|
+
sql.exec(`CREATE TABLE IF NOT EXISTS oyster_discord_bindings (
|
|
100
|
+
binding_id TEXT PRIMARY KEY,
|
|
101
|
+
agent_id TEXT NOT NULL,
|
|
102
|
+
application_id TEXT NOT NULL UNIQUE,
|
|
103
|
+
token_credential_id TEXT NOT NULL,
|
|
104
|
+
request_id TEXT UNIQUE,
|
|
105
|
+
input_json TEXT,
|
|
106
|
+
payload TEXT NOT NULL
|
|
107
|
+
) STRICT`);
|
|
108
|
+
};
|
|
109
|
+
const decode = (raw) => discordBindingSchema.parse(JSON.parse(raw.payload));
|
|
110
|
+
const read = (bindingId) => {
|
|
111
|
+
ensure();
|
|
112
|
+
const row = sql
|
|
113
|
+
.exec("SELECT payload FROM oyster_discord_bindings WHERE binding_id=?", bindingId)
|
|
114
|
+
.toArray()[0];
|
|
115
|
+
return row === undefined ? null : decode(payloadRow.parse(row));
|
|
116
|
+
};
|
|
117
|
+
const receipt = (requestId) => {
|
|
118
|
+
ensure();
|
|
119
|
+
const row = sql
|
|
120
|
+
.exec("SELECT input_json,payload FROM oyster_discord_bindings WHERE request_id=?", requestId)
|
|
121
|
+
.toArray()[0];
|
|
122
|
+
return row === undefined ? null : receiptRow.parse(row);
|
|
123
|
+
};
|
|
124
|
+
return {
|
|
125
|
+
read,
|
|
126
|
+
list: () => {
|
|
127
|
+
ensure();
|
|
128
|
+
return sql
|
|
129
|
+
.exec("SELECT payload FROM oyster_discord_bindings ORDER BY binding_id")
|
|
130
|
+
.toArray()
|
|
131
|
+
.map((row) => decode(payloadRow.parse(row)));
|
|
132
|
+
},
|
|
133
|
+
/** Host-only: the sealed credential that holds this binding's bot token. */
|
|
134
|
+
tokenCredentialId: (bindingId) => {
|
|
135
|
+
ensure();
|
|
136
|
+
const row = sql
|
|
137
|
+
.exec("SELECT token_credential_id FROM oyster_discord_bindings WHERE binding_id=?", bindingId)
|
|
138
|
+
.toArray()[0];
|
|
139
|
+
return row === undefined ? null : tokenRow.parse(row).token_credential_id;
|
|
140
|
+
},
|
|
141
|
+
/**
|
|
142
|
+
* The binding this request already created, or null for a new request. A
|
|
143
|
+
* replay must not seal the token again; changed facts conflict.
|
|
144
|
+
*/
|
|
145
|
+
replay: (rawFacts, requestId) => {
|
|
146
|
+
const parsed = discordFactsSchema.safeParse(rawFacts);
|
|
147
|
+
const identity = requestIdSchema.safeParse(requestId);
|
|
148
|
+
if (!parsed.success || !identity.success)
|
|
149
|
+
throw new DiscordBindingError("invalid_input");
|
|
150
|
+
const prior = receipt(identity.data);
|
|
151
|
+
if (prior === null)
|
|
152
|
+
return null;
|
|
153
|
+
if (prior.input_json !== JSON.stringify(parsed.data))
|
|
154
|
+
throw new DiscordBindingError("request_conflict");
|
|
155
|
+
return decode(prior);
|
|
156
|
+
},
|
|
157
|
+
/**
|
|
158
|
+
* Create once per request identity; a replay returns the first result. The
|
|
159
|
+
* secret is already sealed: `facts` carry none and `tokenCredentialId`
|
|
160
|
+
* names it.
|
|
161
|
+
*/
|
|
162
|
+
create: (rawFacts, tokenCredentialId, requestId) => {
|
|
163
|
+
const parsed = discordFactsSchema.safeParse(rawFacts);
|
|
164
|
+
const identity = requestIdSchema.safeParse(requestId);
|
|
165
|
+
if (!parsed.success || !identity.success)
|
|
166
|
+
throw new DiscordBindingError("invalid_input");
|
|
167
|
+
const input = parsed.data;
|
|
168
|
+
const inputJson = JSON.stringify(input);
|
|
169
|
+
return options.transactionSync(() => {
|
|
170
|
+
ensure();
|
|
171
|
+
const prior = receipt(identity.data);
|
|
172
|
+
if (prior !== null) {
|
|
173
|
+
if (prior.input_json !== inputJson)
|
|
174
|
+
throw new DiscordBindingError("request_conflict");
|
|
175
|
+
return decode(prior);
|
|
176
|
+
}
|
|
177
|
+
if (!options.agentExists(input.agentId))
|
|
178
|
+
throw new DiscordBindingError("not_found");
|
|
179
|
+
const taken = sql
|
|
180
|
+
.exec("SELECT 1 FROM oyster_discord_bindings WHERE application_id=?", input.applicationId)
|
|
181
|
+
.toArray();
|
|
182
|
+
if (taken.length > 0)
|
|
183
|
+
throw new DiscordBindingError("application_taken");
|
|
184
|
+
const binding = discordBindingSchema.parse({
|
|
185
|
+
bindingId: channelIds.binding.generate(),
|
|
186
|
+
agentId: input.agentId,
|
|
187
|
+
selector: {
|
|
188
|
+
provider: "discord",
|
|
189
|
+
applicationId: input.applicationId,
|
|
190
|
+
publicKey: input.publicKey,
|
|
191
|
+
guildIds: input.guildIds,
|
|
192
|
+
channelIds: input.channelIds,
|
|
193
|
+
},
|
|
194
|
+
lifecycle: "active",
|
|
195
|
+
authorityVersion: 1,
|
|
196
|
+
persona: input.persona,
|
|
197
|
+
});
|
|
198
|
+
sql.exec("INSERT INTO oyster_discord_bindings (binding_id,agent_id,application_id,token_credential_id,request_id,input_json,payload) VALUES (?,?,?,?,?,?,?)", binding.bindingId, binding.agentId, input.applicationId, tokenCredentialId, identity.data, inputJson, JSON.stringify(binding));
|
|
199
|
+
return binding;
|
|
200
|
+
});
|
|
201
|
+
},
|
|
202
|
+
/** Apply one edit; a replaced token's new credential id is already sealed. */
|
|
203
|
+
update: (bindingId, rawEdit, replacementCredentialId) => {
|
|
204
|
+
const parsed = discordEditSchema.safeParse(rawEdit);
|
|
205
|
+
if (!parsed.success)
|
|
206
|
+
throw new DiscordBindingError("invalid_input");
|
|
207
|
+
const edit = parsed.data;
|
|
208
|
+
return options.transactionSync(() => {
|
|
209
|
+
const current = read(bindingId);
|
|
210
|
+
if (current === null)
|
|
211
|
+
throw new DiscordBindingError("not_found");
|
|
212
|
+
if (current.authorityVersion !== edit.expectedVersion)
|
|
213
|
+
throw new DiscordBindingError("version_conflict");
|
|
214
|
+
const next = discordBindingSchema.parse({
|
|
215
|
+
...current,
|
|
216
|
+
selector: {
|
|
217
|
+
...current.selector,
|
|
218
|
+
publicKey: edit.publicKey ?? current.selector.publicKey,
|
|
219
|
+
guildIds: edit.guildIds ?? current.selector.guildIds,
|
|
220
|
+
channelIds: edit.channelIds ?? current.selector.channelIds,
|
|
221
|
+
},
|
|
222
|
+
lifecycle: edit.lifecycle ?? current.lifecycle,
|
|
223
|
+
persona: edit.persona ?? current.persona,
|
|
224
|
+
authorityVersion: current.authorityVersion + 1,
|
|
225
|
+
});
|
|
226
|
+
if (replacementCredentialId === null)
|
|
227
|
+
sql.exec("UPDATE oyster_discord_bindings SET payload=? WHERE binding_id=?", JSON.stringify(next), bindingId);
|
|
228
|
+
else
|
|
229
|
+
sql.exec("UPDATE oyster_discord_bindings SET payload=?, token_credential_id=? WHERE binding_id=?", JSON.stringify(next), replacementCredentialId, bindingId);
|
|
230
|
+
return next;
|
|
231
|
+
});
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* Discord's HTTP interactions, as one Agent channel needs them: verify the
|
|
4
|
+
* Ed25519 signature of a request, decode only the fields we use, and build
|
|
5
|
+
* the few responses and follow-ups we send. No Gateway and no Message Content
|
|
6
|
+
* intent: members reach the Agent through the `/ask` command and a Reply
|
|
7
|
+
* button that opens a modal.
|
|
8
|
+
*/
|
|
9
|
+
/** The one command; an owner registers it in each bound guild. */
|
|
10
|
+
export declare const ASK_COMMAND = "ask";
|
|
11
|
+
/** The one option of `/ask`. */
|
|
12
|
+
export declare const ASK_OPTION = "question";
|
|
13
|
+
/** The Reply button and its modal share one custom id. */
|
|
14
|
+
export declare const REPLY_ID = "reply";
|
|
15
|
+
/** The modal's single text input. */
|
|
16
|
+
export declare const REPLY_TEXT_ID = "text";
|
|
17
|
+
/** The longest question or reply Discord lets a member type here. */
|
|
18
|
+
export declare const DISCORD_ASK_LIMIT = 1000;
|
|
19
|
+
/** How long an Agent has to answer before the interaction token lapses. */
|
|
20
|
+
export declare const DISCORD_ANSWER_WINDOW_MS: number;
|
|
21
|
+
/** One settlement read waits at most this long (Shiitake allows 30 s). */
|
|
22
|
+
export declare const DISCORD_SETTLEMENT_POLL_MS = 25000;
|
|
23
|
+
/** One verified question, as the public edge hands it to the Agent. */
|
|
24
|
+
export type DiscordAsk = Readonly<{
|
|
25
|
+
interactionId: string;
|
|
26
|
+
applicationId: string;
|
|
27
|
+
/** Credential for the follow-up webhook; never stored. */
|
|
28
|
+
interactionToken: string;
|
|
29
|
+
guildId: string;
|
|
30
|
+
channelId: string;
|
|
31
|
+
userId: string;
|
|
32
|
+
text: string;
|
|
33
|
+
/** The member's own words, quoted above the answer of a modal reply. */
|
|
34
|
+
echo?: string | undefined;
|
|
35
|
+
}>;
|
|
36
|
+
/** What a member is told when intake says no. */
|
|
37
|
+
export declare const discordDenial: {
|
|
38
|
+
readonly unmapped: "This Agent is not available right now.";
|
|
39
|
+
readonly suspended: "This Agent is paused.";
|
|
40
|
+
readonly banned: "You cannot ask this Agent anything.";
|
|
41
|
+
readonly too_long: "That message is too long.";
|
|
42
|
+
readonly party_daily_limit: "You have reached today's limit. Try again tomorrow.";
|
|
43
|
+
readonly new_party_limit: "This Agent is busy right now. Try again soon.";
|
|
44
|
+
readonly rate_limited: "This Agent is busy right now. Try again soon.";
|
|
45
|
+
readonly event_conflict: "That message could not be accepted.";
|
|
46
|
+
};
|
|
47
|
+
/** The interaction types we answer; any other type fails to decode. */
|
|
48
|
+
export declare const interactionSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
49
|
+
type: z.ZodLiteral<1>;
|
|
50
|
+
id: z.ZodString;
|
|
51
|
+
application_id: z.ZodString;
|
|
52
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
53
|
+
type: z.ZodLiteral<2>;
|
|
54
|
+
data: z.ZodObject<{
|
|
55
|
+
name: z.ZodString;
|
|
56
|
+
options: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
57
|
+
name: z.ZodString;
|
|
58
|
+
value: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean]>>;
|
|
59
|
+
}, z.core.$strip>>>;
|
|
60
|
+
}, z.core.$strip>;
|
|
61
|
+
id: z.ZodString;
|
|
62
|
+
application_id: z.ZodString;
|
|
63
|
+
token: z.ZodString;
|
|
64
|
+
guild_id: z.ZodOptional<z.ZodString>;
|
|
65
|
+
channel_id: z.ZodOptional<z.ZodString>;
|
|
66
|
+
member: z.ZodOptional<z.ZodObject<{
|
|
67
|
+
user: z.ZodObject<{
|
|
68
|
+
id: z.ZodString;
|
|
69
|
+
}, z.core.$strip>;
|
|
70
|
+
}, z.core.$strip>>;
|
|
71
|
+
user: z.ZodOptional<z.ZodObject<{
|
|
72
|
+
id: z.ZodString;
|
|
73
|
+
}, z.core.$strip>>;
|
|
74
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
75
|
+
type: z.ZodLiteral<3>;
|
|
76
|
+
data: z.ZodObject<{
|
|
77
|
+
custom_id: z.ZodString;
|
|
78
|
+
}, z.core.$strip>;
|
|
79
|
+
id: z.ZodString;
|
|
80
|
+
application_id: z.ZodString;
|
|
81
|
+
token: z.ZodString;
|
|
82
|
+
guild_id: z.ZodOptional<z.ZodString>;
|
|
83
|
+
channel_id: z.ZodOptional<z.ZodString>;
|
|
84
|
+
member: z.ZodOptional<z.ZodObject<{
|
|
85
|
+
user: z.ZodObject<{
|
|
86
|
+
id: z.ZodString;
|
|
87
|
+
}, z.core.$strip>;
|
|
88
|
+
}, z.core.$strip>>;
|
|
89
|
+
user: z.ZodOptional<z.ZodObject<{
|
|
90
|
+
id: z.ZodString;
|
|
91
|
+
}, z.core.$strip>>;
|
|
92
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
93
|
+
type: z.ZodLiteral<5>;
|
|
94
|
+
data: z.ZodObject<{
|
|
95
|
+
custom_id: z.ZodString;
|
|
96
|
+
components: z.ZodArray<z.ZodObject<{
|
|
97
|
+
components: z.ZodArray<z.ZodObject<{
|
|
98
|
+
custom_id: z.ZodString;
|
|
99
|
+
value: z.ZodString;
|
|
100
|
+
}, z.core.$strip>>;
|
|
101
|
+
}, z.core.$strip>>;
|
|
102
|
+
}, z.core.$strip>;
|
|
103
|
+
id: z.ZodString;
|
|
104
|
+
application_id: z.ZodString;
|
|
105
|
+
token: z.ZodString;
|
|
106
|
+
guild_id: z.ZodOptional<z.ZodString>;
|
|
107
|
+
channel_id: z.ZodOptional<z.ZodString>;
|
|
108
|
+
member: z.ZodOptional<z.ZodObject<{
|
|
109
|
+
user: z.ZodObject<{
|
|
110
|
+
id: z.ZodString;
|
|
111
|
+
}, z.core.$strip>;
|
|
112
|
+
}, z.core.$strip>>;
|
|
113
|
+
user: z.ZodOptional<z.ZodObject<{
|
|
114
|
+
id: z.ZodString;
|
|
115
|
+
}, z.core.$strip>>;
|
|
116
|
+
}, z.core.$strip>], "type">;
|
|
117
|
+
/** Why a request's signature was refused. */
|
|
118
|
+
export type SignatureRefusal = "missing" | "malformed" | "stale" | "invalid";
|
|
119
|
+
/**
|
|
120
|
+
* Verify Discord's Ed25519 signature over `timestamp + body` with WebCrypto.
|
|
121
|
+
* The timestamp is also bounded, so a captured request cannot be replayed
|
|
122
|
+
* later.
|
|
123
|
+
*/
|
|
124
|
+
export declare function verifyDiscordRequest(input: Readonly<{
|
|
125
|
+
publicKey: string;
|
|
126
|
+
signature: string | undefined;
|
|
127
|
+
timestamp: string | undefined;
|
|
128
|
+
body: string;
|
|
129
|
+
nowSeconds: number;
|
|
130
|
+
}>): Promise<"verified" | SignatureRefusal>;
|
|
131
|
+
/** The answer to Discord's endpoint check. */
|
|
132
|
+
export declare const pongResponse: {
|
|
133
|
+
readonly type: 1;
|
|
134
|
+
};
|
|
135
|
+
/** Tell the member the Agent is working; the answer follows by webhook. */
|
|
136
|
+
export declare const deferredResponse: {
|
|
137
|
+
readonly type: 5;
|
|
138
|
+
};
|
|
139
|
+
/** A one-off notice only the member sees, such as a refusal. */
|
|
140
|
+
export declare function ephemeralResponse(content: string): {
|
|
141
|
+
readonly type: 4;
|
|
142
|
+
readonly data: {
|
|
143
|
+
readonly content: string;
|
|
144
|
+
readonly flags: 64;
|
|
145
|
+
readonly allowed_mentions: {
|
|
146
|
+
readonly parse: readonly [];
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
/** The modal a Reply button opens: one paragraph input. */
|
|
151
|
+
export declare const replyModalResponse: {
|
|
152
|
+
readonly type: 9;
|
|
153
|
+
readonly data: {
|
|
154
|
+
readonly custom_id: "reply";
|
|
155
|
+
readonly title: "Reply";
|
|
156
|
+
readonly components: readonly [{
|
|
157
|
+
readonly type: 1;
|
|
158
|
+
readonly components: readonly [{
|
|
159
|
+
readonly type: 4;
|
|
160
|
+
readonly custom_id: "text";
|
|
161
|
+
readonly label: "Your reply";
|
|
162
|
+
readonly style: 2;
|
|
163
|
+
readonly min_length: 1;
|
|
164
|
+
readonly max_length: 1000;
|
|
165
|
+
readonly required: true;
|
|
166
|
+
}];
|
|
167
|
+
}];
|
|
168
|
+
};
|
|
169
|
+
};
|
|
170
|
+
/** Quote a member's modal reply so the thread reads in order. */
|
|
171
|
+
export declare function quoted(text: string): string;
|
|
172
|
+
/**
|
|
173
|
+
* Fill the deferred message with the Agent's answer. The interaction token is
|
|
174
|
+
* the credential: no bot token is involved.
|
|
175
|
+
*/
|
|
176
|
+
export declare function patchOriginal(send: typeof fetch, input: Readonly<{
|
|
177
|
+
applicationId: string;
|
|
178
|
+
interactionToken: string;
|
|
179
|
+
content: string;
|
|
180
|
+
withReply: boolean;
|
|
181
|
+
}>): Promise<boolean>;
|
|
182
|
+
/**
|
|
183
|
+
* Register `/ask` in one guild with the owner's bot token. Creating by name
|
|
184
|
+
* updates an existing `/ask` and leaves the owner's other commands alone.
|
|
185
|
+
*/
|
|
186
|
+
export declare function registerAskCommand(send: typeof fetch, input: Readonly<{
|
|
187
|
+
applicationId: string;
|
|
188
|
+
guildId: string;
|
|
189
|
+
botToken: string;
|
|
190
|
+
}>): Promise<boolean>;
|