@fungi.computer/oyster 0.21.9 → 0.23.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/binding.d.ts +1 -0
- package/dist/channels/binding.js +3 -1
- package/dist/channels/conversations.d.ts +1 -0
- package/dist/channels/conversations.js +15 -1
- package/dist/channels/discord-binding.d.ts +132 -0
- package/dist/channels/discord-binding.js +234 -0
- package/dist/channels/discord-interactions.d.ts +253 -0
- package/dist/channels/discord-interactions.js +325 -0
- package/dist/channels/discord-replies.d.ts +65 -0
- package/dist/channels/discord-replies.js +182 -0
- package/dist/embed-metafiles.js +1 -1
- package/dist/embed-scripts.js +1 -1
- package/dist/page-script.js +1 -1
- 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 +60 -9
- package/dist/standalone/agents.js +244 -11
- package/dist/standalone/app-builds-binding.js +1 -1
- package/dist/standalone/discord-channels.d.ts +89 -0
- package/dist/standalone/discord-channels.js +260 -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/registry.d.ts +5 -2
- package/dist/standalone/registry.js +6 -4
- package/dist/standalone/website-channels.js +2 -2
- 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 +12 -12
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* The Discord replies an Agent still owes. An interaction token is valid for
|
|
4
|
+
* about 15 minutes; until the answer is delivered or the token lapses, one row
|
|
5
|
+
* per interaction keeps the debt in the Agent's own store, so an evicted run
|
|
6
|
+
* still answers after the Agent wakes. A row names no secret once it is
|
|
7
|
+
* closed, and a closed row says honestly why: `expired` (the token lapsed or
|
|
8
|
+
* Discord no longer knew it) or `failed` (delivery kept failing).
|
|
9
|
+
*/
|
|
10
|
+
/** The token window we honour, a little under Discord's own 15 minutes. */
|
|
11
|
+
const DISCORD_REPLY_WINDOW_MS = 14 * 60_000;
|
|
12
|
+
/** How long a closed row stays readable before it is pruned. */
|
|
13
|
+
const CLOSED_RETENTION_MS = 7 * 86_400_000;
|
|
14
|
+
/** Delivery attempts after which a reply is given up as failed. */
|
|
15
|
+
const MAX_DELIVERY_ATTEMPTS = 5;
|
|
16
|
+
/** Rows one pass works on, so a pass stays bounded. */
|
|
17
|
+
const PASS_LIMIT = 4;
|
|
18
|
+
/** How soon an unsettled run is looked at again. */
|
|
19
|
+
const POLL_MS = 1_000;
|
|
20
|
+
const pendingRow = z.strictObject({
|
|
21
|
+
interaction_id: z.string(),
|
|
22
|
+
binding_id: z.string(),
|
|
23
|
+
application_id: z.string(),
|
|
24
|
+
token: z.string(),
|
|
25
|
+
guild_id: z.string(),
|
|
26
|
+
channel_id: z.string(),
|
|
27
|
+
user_id: z.string(),
|
|
28
|
+
text: z.string(),
|
|
29
|
+
echo: z.string().nullable(),
|
|
30
|
+
display_name: z.string().nullable(),
|
|
31
|
+
session_id: z.string().nullable(),
|
|
32
|
+
command_id: z.string().nullable(),
|
|
33
|
+
attempts: z.number().int().nonnegative(),
|
|
34
|
+
delivered_parts: z.number().int().nonnegative(),
|
|
35
|
+
expires_at: z.number().int(),
|
|
36
|
+
});
|
|
37
|
+
const closedRow = z.strictObject({
|
|
38
|
+
interaction_id: z.string(),
|
|
39
|
+
display_name: z.string().nullable(),
|
|
40
|
+
state: z.enum(["expired", "failed"]),
|
|
41
|
+
closed_at: z.number().int(),
|
|
42
|
+
});
|
|
43
|
+
const dueRow = z.strictObject({ due: z.number().int().nullable() });
|
|
44
|
+
/** The pending-reply store and the host work that pays it off. */
|
|
45
|
+
export function createDiscordReplies(options) {
|
|
46
|
+
const { sql } = options;
|
|
47
|
+
const ensure = () => {
|
|
48
|
+
sql.exec(`CREATE TABLE IF NOT EXISTS oyster_discord_replies (
|
|
49
|
+
interaction_id TEXT PRIMARY KEY,
|
|
50
|
+
binding_id TEXT NOT NULL,
|
|
51
|
+
application_id TEXT NOT NULL,
|
|
52
|
+
token TEXT NOT NULL,
|
|
53
|
+
guild_id TEXT NOT NULL,
|
|
54
|
+
channel_id TEXT NOT NULL,
|
|
55
|
+
user_id TEXT NOT NULL,
|
|
56
|
+
text TEXT NOT NULL,
|
|
57
|
+
echo TEXT,
|
|
58
|
+
display_name TEXT,
|
|
59
|
+
delivered_parts INTEGER NOT NULL DEFAULT 0,
|
|
60
|
+
session_id TEXT,
|
|
61
|
+
command_id TEXT,
|
|
62
|
+
state TEXT NOT NULL CHECK (state IN ('waiting','expired','failed')),
|
|
63
|
+
attempts INTEGER NOT NULL,
|
|
64
|
+
created_at INTEGER NOT NULL,
|
|
65
|
+
expires_at INTEGER NOT NULL,
|
|
66
|
+
next_at INTEGER NOT NULL,
|
|
67
|
+
closed_at INTEGER
|
|
68
|
+
) STRICT`);
|
|
69
|
+
};
|
|
70
|
+
const close = (interactionId, state) => {
|
|
71
|
+
sql.exec("UPDATE oyster_discord_replies SET state=?, token='', text='', echo=NULL, closed_at=? WHERE interaction_id=?", state, options.now(), interactionId);
|
|
72
|
+
};
|
|
73
|
+
const reschedule = (interactionId, at, attempts) => {
|
|
74
|
+
sql.exec("UPDATE oyster_discord_replies SET next_at=?, attempts=? WHERE interaction_id=?", at, attempts, interactionId);
|
|
75
|
+
};
|
|
76
|
+
let active;
|
|
77
|
+
const pass = async () => {
|
|
78
|
+
ensure();
|
|
79
|
+
const now = options.now();
|
|
80
|
+
sql.exec("DELETE FROM oyster_discord_replies WHERE state!='waiting' AND closed_at<?", now - CLOSED_RETENTION_MS);
|
|
81
|
+
const rows = sql
|
|
82
|
+
.exec("SELECT interaction_id,binding_id,application_id,token,guild_id,channel_id,user_id,text,echo,display_name,session_id,command_id,attempts,delivered_parts,expires_at FROM oyster_discord_replies WHERE state='waiting' AND next_at<=? ORDER BY next_at LIMIT ?", now, PASS_LIMIT)
|
|
83
|
+
.toArray()
|
|
84
|
+
.map((raw) => pendingRow.parse(raw));
|
|
85
|
+
for (const row of rows) {
|
|
86
|
+
if (options.now() >= row.expires_at) {
|
|
87
|
+
close(row.interaction_id, "expired");
|
|
88
|
+
options.onExpired(row.interaction_id);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const delivery = await options.deliver({
|
|
92
|
+
interactionId: row.interaction_id,
|
|
93
|
+
bindingId: row.binding_id,
|
|
94
|
+
ask: {
|
|
95
|
+
interactionId: row.interaction_id,
|
|
96
|
+
applicationId: row.application_id,
|
|
97
|
+
interactionToken: row.token,
|
|
98
|
+
guildId: row.guild_id,
|
|
99
|
+
channelId: row.channel_id,
|
|
100
|
+
userId: row.user_id,
|
|
101
|
+
text: row.text,
|
|
102
|
+
echo: row.echo ?? undefined,
|
|
103
|
+
displayName: row.display_name ?? undefined,
|
|
104
|
+
},
|
|
105
|
+
accepted: row.session_id !== null && row.command_id !== null
|
|
106
|
+
? { sessionId: row.session_id, commandId: row.command_id }
|
|
107
|
+
: null,
|
|
108
|
+
attempts: row.attempts,
|
|
109
|
+
deliveredParts: row.delivered_parts,
|
|
110
|
+
});
|
|
111
|
+
switch (delivery) {
|
|
112
|
+
case "delivered":
|
|
113
|
+
sql.exec("DELETE FROM oyster_discord_replies WHERE interaction_id=?", row.interaction_id);
|
|
114
|
+
break;
|
|
115
|
+
case "waiting":
|
|
116
|
+
reschedule(row.interaction_id, options.now() + POLL_MS, row.attempts);
|
|
117
|
+
break;
|
|
118
|
+
case "gone":
|
|
119
|
+
close(row.interaction_id, "expired");
|
|
120
|
+
options.onExpired(row.interaction_id);
|
|
121
|
+
break;
|
|
122
|
+
case "retry":
|
|
123
|
+
if (row.attempts + 1 >= MAX_DELIVERY_ATTEMPTS)
|
|
124
|
+
close(row.interaction_id, "failed");
|
|
125
|
+
else
|
|
126
|
+
reschedule(row.interaction_id, options.now() + 1_000 * 2 ** (row.attempts + 1), row.attempts + 1);
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
return {
|
|
132
|
+
/** Owe one reply. A second record of the same interaction is the first. */
|
|
133
|
+
record: (bindingId, ask) => {
|
|
134
|
+
ensure();
|
|
135
|
+
const now = options.now();
|
|
136
|
+
sql.exec("INSERT OR IGNORE INTO oyster_discord_replies (interaction_id,binding_id,application_id,token,guild_id,channel_id,user_id,text,echo,display_name,state,attempts,created_at,expires_at,next_at) VALUES (?,?,?,?,?,?,?,?,?,?,'waiting',0,?,?,?)", ask.interactionId, bindingId, ask.applicationId, ask.interactionToken, ask.guildId, ask.channelId, ask.userId, ask.text, ask.echo ?? null, ask.displayName ?? null, now, now + DISCORD_REPLY_WINDOW_MS, now);
|
|
137
|
+
},
|
|
138
|
+
/** Remember how many messages of the answer Discord has. */
|
|
139
|
+
progress: (interactionId, parts) => {
|
|
140
|
+
sql.exec("UPDATE oyster_discord_replies SET delivered_parts=? WHERE interaction_id=?", parts, interactionId);
|
|
141
|
+
},
|
|
142
|
+
/** Replies of one binding that never reached Discord, newest first. */
|
|
143
|
+
closed: (bindingId) => {
|
|
144
|
+
ensure();
|
|
145
|
+
return sql
|
|
146
|
+
.exec("SELECT interaction_id,display_name,state,closed_at FROM oyster_discord_replies WHERE binding_id=? AND state!='waiting' ORDER BY closed_at DESC LIMIT 50", bindingId)
|
|
147
|
+
.toArray()
|
|
148
|
+
.map((raw) => {
|
|
149
|
+
const row = closedRow.parse(raw);
|
|
150
|
+
return {
|
|
151
|
+
interactionId: row.interaction_id,
|
|
152
|
+
label: row.display_name ?? "Discord member",
|
|
153
|
+
state: row.state,
|
|
154
|
+
closedAt: row.closed_at,
|
|
155
|
+
};
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
/** Remember which command answers this interaction. */
|
|
159
|
+
attach: (interactionId, accepted) => {
|
|
160
|
+
sql.exec("UPDATE oyster_discord_replies SET session_id=?, command_id=? WHERE interaction_id=?", accepted.sessionId, accepted.commandId, interactionId);
|
|
161
|
+
},
|
|
162
|
+
/** Host work on the Agent's one durable wake. */
|
|
163
|
+
work: {
|
|
164
|
+
pending: () => {
|
|
165
|
+
ensure();
|
|
166
|
+
const row = dueRow.parse(sql
|
|
167
|
+
.exec("SELECT MIN(next_at) AS due FROM oyster_discord_replies WHERE state='waiting'")
|
|
168
|
+
.toArray()[0]);
|
|
169
|
+
return {
|
|
170
|
+
due: row.due !== null && row.due <= options.now(),
|
|
171
|
+
nextWakeAt: row.due,
|
|
172
|
+
};
|
|
173
|
+
},
|
|
174
|
+
run: () => {
|
|
175
|
+
// Passes never overlap, so one reply is never delivered twice at once.
|
|
176
|
+
const next = (active ?? Promise.resolve()).then(pass, pass);
|
|
177
|
+
active = next;
|
|
178
|
+
return next;
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|