@fungi.computer/oyster 0.44.0 → 0.45.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 +6 -0
- package/dist/agents.js +122 -0
- package/dist/standalone/agent-connections.d.ts +21 -1
- package/dist/standalone/agent-connections.js +85 -1
- package/dist/standalone/agents.d.ts +30 -3
- package/dist/standalone/agents.js +118 -28
- package/dist/standalone/model-key-usage.d.ts +28 -0
- package/dist/standalone/model-key-usage.js +70 -0
- package/dist/standalone/vault.d.ts +84 -0
- package/dist/standalone/vault.js +64 -0
- package/docs/api/agents.md +167 -3
- package/docs/api/workers-git.md +7 -7
- package/package.json +6 -6
package/dist/agents.d.ts
CHANGED
|
@@ -3,6 +3,10 @@ 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
5
|
export type { StandaloneModelKeyProvider } from "./standalone/model-key-providers.js";
|
|
6
|
+
import type { VaultCommand, VaultResult } from "./standalone/vault.js";
|
|
7
|
+
export type { VaultCommand, VaultCredential, VaultRefusal, VaultResult, VaultView, } from "./standalone/vault.js";
|
|
8
|
+
export type { ModelKeyUsage } from "./standalone/model-key-usage.js";
|
|
9
|
+
export type { CredentialRefusal } from "./standalone/agent-connections.js";
|
|
6
10
|
import { type Routes } from "./authorization.js";
|
|
7
11
|
import type { AppToolPolicy } from "./apps/tools.js";
|
|
8
12
|
import * as Schema from "effect/Schema";
|
|
@@ -204,6 +208,8 @@ export type OysterAgents = Readonly<{
|
|
|
204
208
|
}>>;
|
|
205
209
|
/** Remove one key; Agents that chose it then fail with `key_removed`. */
|
|
206
210
|
removeModelKey(keyId: string): Promise<"deleted" | "absent">;
|
|
211
|
+
/** The Vault page's one owner door: every stored credential, write-only. */
|
|
212
|
+
vault(command: VaultCommand): Promise<VaultResult>;
|
|
207
213
|
/** Every connection with the Agents that may use it. */
|
|
208
214
|
connections(): Promise<readonly OysterConnection[]>;
|
|
209
215
|
/** Connect one remote MCP server; no Agent has it until the owner grants it. */
|
package/dist/agents.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { destinationsSchema, subjectSchema, } from "@fungi.computer/veil/connections";
|
|
1
2
|
import { agent, agents as agentCollection, owner, routes, } from "./authorization.js";
|
|
2
3
|
import * as Schema from "effect/Schema";
|
|
3
4
|
import { agentNameSchema } from "@fungi.computer/agar/agents";
|
|
@@ -525,6 +526,127 @@ export function modelKeyRoutes(agents, computerSetup) {
|
|
|
525
526
|
return (await agents.removeModelKey(keyId)) === "deleted"
|
|
526
527
|
? new Response(null, { status: 204 })
|
|
527
528
|
: refusal(404, "No such key");
|
|
529
|
+
})
|
|
530
|
+
.include(vaultRoutes(agents));
|
|
531
|
+
}
|
|
532
|
+
/** Printable ASCII, as Veil seals it; never echoed back. */
|
|
533
|
+
const vaultValue = Schema.String.check(Schema.isPattern(/^[\x21-\x7e]{1,16384}$/u));
|
|
534
|
+
const vaultLabel = Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(128));
|
|
535
|
+
const expectedVersion = Schema.Number.check(Schema.isInt(), Schema.isGreaterThanOrEqualTo(1));
|
|
536
|
+
const addSecretSchema = Schema.Struct({
|
|
537
|
+
name: Schema.String.check(Schema.isPattern(/^[a-z][a-z0-9_]{0,31}$/u)),
|
|
538
|
+
label: Schema.optional(vaultLabel),
|
|
539
|
+
destinations: destinationsSchema,
|
|
540
|
+
placement: Schema.Union([
|
|
541
|
+
Schema.Literal("bearer"),
|
|
542
|
+
Schema.Literal("basic"),
|
|
543
|
+
Schema.Struct({
|
|
544
|
+
header: Schema.String.check(Schema.isPattern(/^[A-Za-z0-9-]{1,64}$/u)),
|
|
545
|
+
}),
|
|
546
|
+
Schema.Struct({
|
|
547
|
+
query: Schema.String.check(Schema.isPattern(/^[A-Za-z0-9_.-]{1,64}$/u)),
|
|
548
|
+
}),
|
|
549
|
+
]),
|
|
550
|
+
secret: vaultValue,
|
|
551
|
+
});
|
|
552
|
+
const updateCredentialSchema = Schema.Struct({
|
|
553
|
+
expectedVersion,
|
|
554
|
+
label: Schema.optional(vaultLabel),
|
|
555
|
+
destinations: Schema.optional(destinationsSchema),
|
|
556
|
+
secret: Schema.optional(vaultValue),
|
|
557
|
+
});
|
|
558
|
+
const revokeCredentialSchema = Schema.Struct({ expectedVersion });
|
|
559
|
+
/** Apps hold no Vault power: the subject kinds the page offers. */
|
|
560
|
+
const vaultSubjectKinds = new Set(["agent", "computer", "team-key"]);
|
|
561
|
+
const vaultRefusalStatus = {
|
|
562
|
+
not_found: 404,
|
|
563
|
+
conflict: 409,
|
|
564
|
+
denied: 403,
|
|
565
|
+
invalid_input: 400,
|
|
566
|
+
duplicate_name: 409,
|
|
567
|
+
limit: 409,
|
|
568
|
+
invalid_endpoint: 400,
|
|
569
|
+
unsupported_subject: 400,
|
|
570
|
+
};
|
|
571
|
+
/**
|
|
572
|
+
* The Vault page's owner routes. A value is posted straight here and nothing
|
|
573
|
+
* ever reads one back; listings and usage carry no value.
|
|
574
|
+
*/
|
|
575
|
+
function vaultRoutes(agents) {
|
|
576
|
+
const answer = async (command, status = 200) => {
|
|
577
|
+
const result = await agents.vault(command);
|
|
578
|
+
return "error" in result
|
|
579
|
+
? refusal(vaultRefusalStatus[result.error], result.error)
|
|
580
|
+
: Response.json(result, {
|
|
581
|
+
status,
|
|
582
|
+
headers: { "cache-control": "private, no-store" },
|
|
583
|
+
});
|
|
584
|
+
};
|
|
585
|
+
const body = async (request, schema) => {
|
|
586
|
+
if (Number(request.headers.get("content-length") ?? "0") > 64 * 1024)
|
|
587
|
+
return null;
|
|
588
|
+
const decoded = Schema.decodeUnknownOption(Schema.fromJsonString(schema), {
|
|
589
|
+
onExcessProperty: "error",
|
|
590
|
+
})(await request.text());
|
|
591
|
+
return decoded._tag === "None" ? null : decoded.value;
|
|
592
|
+
};
|
|
593
|
+
const subjectOf = (kind, id) => {
|
|
594
|
+
if (!vaultSubjectKinds.has(kind))
|
|
595
|
+
return null;
|
|
596
|
+
const subject = Schema.decodeUnknownOption(subjectSchema)({ kind, id });
|
|
597
|
+
return subject._tag === "None" ? null : subject.value;
|
|
598
|
+
};
|
|
599
|
+
return routes()
|
|
600
|
+
.get("/api/vault", owner("direct"), () => answer({ op: "read" }))
|
|
601
|
+
.post("/api/vault/secrets", owner("direct"), async (context) => {
|
|
602
|
+
const input = await body(context.req.raw, addSecretSchema);
|
|
603
|
+
return input === null
|
|
604
|
+
? refusal(400, "Name the secret, bind it to hosts and paste its value")
|
|
605
|
+
: answer({ op: "addSecret", ...input }, 201);
|
|
606
|
+
})
|
|
607
|
+
.put("/api/vault/credentials/:connectionId", owner("direct"), async (context) => {
|
|
608
|
+
const connectionId = context.req.param("connectionId");
|
|
609
|
+
const input = await body(context.req.raw, updateCredentialSchema);
|
|
610
|
+
if (!connectionIdPattern.test(connectionId))
|
|
611
|
+
return refusal(404, "No such credential");
|
|
612
|
+
return input === null
|
|
613
|
+
? refusal(400, "invalid_input")
|
|
614
|
+
: answer({ op: "updateCredential", connectionId, ...input });
|
|
615
|
+
})
|
|
616
|
+
.delete("/api/vault/credentials/:connectionId", owner("direct"), async (context) => {
|
|
617
|
+
const connectionId = context.req.param("connectionId");
|
|
618
|
+
const input = await body(context.req.raw, revokeCredentialSchema);
|
|
619
|
+
if (!connectionIdPattern.test(connectionId))
|
|
620
|
+
return refusal(404, "No such credential");
|
|
621
|
+
return input === null
|
|
622
|
+
? refusal(400, "invalid_input")
|
|
623
|
+
: answer({ op: "revokeCredential", connectionId, ...input });
|
|
624
|
+
})
|
|
625
|
+
.put("/api/vault/credentials/:connectionId/subjects/:kind/:subjectId", owner("direct"), (context) => {
|
|
626
|
+
const connectionId = context.req.param("connectionId");
|
|
627
|
+
const subject = subjectOf(context.req.param("kind"), context.req.param("subjectId"));
|
|
628
|
+
return !connectionIdPattern.test(connectionId) || subject === null
|
|
629
|
+
? refusal(400, "invalid_input")
|
|
630
|
+
: answer({ op: "grantSecret", connectionId, subject });
|
|
631
|
+
})
|
|
632
|
+
.delete("/api/vault/credentials/:connectionId/subjects/:kind/:subjectId", owner("direct"), (context) => {
|
|
633
|
+
const connectionId = context.req.param("connectionId");
|
|
634
|
+
const subject = subjectOf(context.req.param("kind"), context.req.param("subjectId"));
|
|
635
|
+
return !connectionIdPattern.test(connectionId) || subject === null
|
|
636
|
+
? refusal(400, "invalid_input")
|
|
637
|
+
: answer({ op: "ungrantSecret", connectionId, subject });
|
|
638
|
+
})
|
|
639
|
+
.get("/api/vault/credentials/:connectionId/usage", owner("direct"), (context) => {
|
|
640
|
+
const connectionId = context.req.param("connectionId");
|
|
641
|
+
return connectionIdPattern.test(connectionId)
|
|
642
|
+
? answer({ op: "usage", connectionId })
|
|
643
|
+
: refusal(404, "No such credential");
|
|
644
|
+
})
|
|
645
|
+
.get("/api/vault/model-keys/:keyId/usage", owner("direct"), (context) => {
|
|
646
|
+
const keyId = context.req.param("keyId");
|
|
647
|
+
return keyIdPattern.test(keyId)
|
|
648
|
+
? answer({ op: "modelKeyUsage", keyId })
|
|
649
|
+
: refusal(404, "No such key");
|
|
528
650
|
});
|
|
529
651
|
}
|
|
530
652
|
const connectionIdPattern = /^[A-Za-z0-9_.:-]{1,128}$/u;
|
|
@@ -5,6 +5,10 @@ import { type ConnectionAccess, type ConnectionAdded, type ConnectionInput, type
|
|
|
5
5
|
type SecretGrantRefusal = ConnectionRefusal | Readonly<{
|
|
6
6
|
error: "unsupported_subject";
|
|
7
7
|
}>;
|
|
8
|
+
/** Why the Vault could not change a stored credential; never its value. */
|
|
9
|
+
export type CredentialRefusal = Readonly<{
|
|
10
|
+
error: "not_found" | "conflict" | "denied" | "invalid_input";
|
|
11
|
+
}>;
|
|
8
12
|
/**
|
|
9
13
|
* This instance's Agent MCP connections over Veil's connections core. The
|
|
10
14
|
* owner holds every credential; an Agent receives a secret-free projection and
|
|
@@ -39,11 +43,27 @@ export declare function createAgentMcpConnections(options: {
|
|
|
39
43
|
connectionId: string;
|
|
40
44
|
}> | ConnectionRefusal>;
|
|
41
45
|
secrets: () => Promise<readonly ConnectionView[]>;
|
|
46
|
+
credentials: () => Promise<readonly ConnectionView[]>;
|
|
47
|
+
updateCredential: (input: Readonly<{
|
|
48
|
+
connectionId: string;
|
|
49
|
+
expectedVersion: number;
|
|
50
|
+
label?: string | undefined;
|
|
51
|
+
destinations?: HttpDestinations | undefined;
|
|
52
|
+
secret?: string | undefined;
|
|
53
|
+
}>) => Promise<Readonly<{
|
|
54
|
+
version: number;
|
|
55
|
+
}> | CredentialRefusal>;
|
|
56
|
+
clearCredential: (input: Readonly<{
|
|
57
|
+
connectionId: string;
|
|
58
|
+
expectedVersion: number;
|
|
59
|
+
}>) => Promise<Readonly<{
|
|
60
|
+
version: number;
|
|
61
|
+
}> | CredentialRefusal>;
|
|
42
62
|
grantSecret: (connectionId: string, subject: ConnectionSubject) => Promise<Readonly<{
|
|
43
63
|
ok: true;
|
|
44
64
|
}> | SecretGrantRefusal>;
|
|
45
65
|
ungrantSecret: (connectionId: string, subject: ConnectionSubject) => Promise<void>;
|
|
46
|
-
/** Every recorded use and refusal of one
|
|
66
|
+
/** Every recorded use and refusal of one credential, without its value. */
|
|
47
67
|
secretUsage: (connectionId: string) => Promise<{
|
|
48
68
|
readonly firstAt: number;
|
|
49
69
|
readonly lastAt: number;
|
|
@@ -9,6 +9,24 @@ const ids = defineIdFamily({
|
|
|
9
9
|
});
|
|
10
10
|
/** The Oyster owner is the only actor that manages connections. */
|
|
11
11
|
const owner = "owner";
|
|
12
|
+
const credentialRefusal = (cause) => {
|
|
13
|
+
if (cause instanceof ConnectionsError)
|
|
14
|
+
switch (cause.code) {
|
|
15
|
+
case "conflict":
|
|
16
|
+
return { error: "conflict" };
|
|
17
|
+
case "denied":
|
|
18
|
+
return { error: "denied" };
|
|
19
|
+
case "revoked":
|
|
20
|
+
case "pending":
|
|
21
|
+
return { error: "not_found" };
|
|
22
|
+
case "invalid_input":
|
|
23
|
+
case "unsupported":
|
|
24
|
+
return { error: "invalid_input" };
|
|
25
|
+
default:
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
throw cause;
|
|
29
|
+
};
|
|
12
30
|
/**
|
|
13
31
|
* This instance's Agent MCP connections over Veil's connections core. The
|
|
14
32
|
* owner holds every credential; an Agent receives a secret-free projection and
|
|
@@ -269,13 +287,79 @@ export function createAgentMcpConnections(options) {
|
|
|
269
287
|
if (subject.kind === "agent")
|
|
270
288
|
await options.notify(subject.id);
|
|
271
289
|
}
|
|
290
|
+
/** Every live credential this instance holds, of every kind; never a value. */
|
|
291
|
+
async function credentials() {
|
|
292
|
+
return (await run(connections.list(owner))).filter((row) => row.state !== "revoked");
|
|
293
|
+
}
|
|
294
|
+
const live = async (connectionId) => (await credentials()).find((row) => row.connectionId === connectionId);
|
|
295
|
+
/**
|
|
296
|
+
* The Vault's one write path for a stored credential, write-only: rotate its
|
|
297
|
+
* value, rename it, rescope an HTTP secret, or give a Connection without one
|
|
298
|
+
* a bearer value. Grants stay; a widened secret needs its value re-entered.
|
|
299
|
+
*/
|
|
300
|
+
async function updateCredential(input) {
|
|
301
|
+
const view = await live(input.connectionId);
|
|
302
|
+
if (view === undefined)
|
|
303
|
+
return { error: "not_found" };
|
|
304
|
+
// A signed-in Connection's tokens come from its sign-in, never a paste.
|
|
305
|
+
if (input.secret !== undefined && view.credential.kind === "oauth")
|
|
306
|
+
return { error: "invalid_input" };
|
|
307
|
+
try {
|
|
308
|
+
const done = await run(connections.update(owner, {
|
|
309
|
+
requestId: ids.connection.generate(),
|
|
310
|
+
connectionId: input.connectionId,
|
|
311
|
+
expectedVersion: input.expectedVersion,
|
|
312
|
+
label: input.label,
|
|
313
|
+
destinations: input.destinations,
|
|
314
|
+
// A rotation keeps the placement; a first value rides as a bearer.
|
|
315
|
+
credential: input.secret === undefined
|
|
316
|
+
? undefined
|
|
317
|
+
: view.credential.kind === "static"
|
|
318
|
+
? view.credential
|
|
319
|
+
: { kind: "static", placement: "bearer" },
|
|
320
|
+
secret: input.secret,
|
|
321
|
+
}));
|
|
322
|
+
await notifyHolders(view);
|
|
323
|
+
return { version: done.version };
|
|
324
|
+
}
|
|
325
|
+
catch (cause) {
|
|
326
|
+
return credentialRefusal(cause);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
/** Take a Connection's pasted value out of custody; an HTTP secret is revoked whole instead. */
|
|
330
|
+
async function clearCredential(input) {
|
|
331
|
+
const view = await live(input.connectionId);
|
|
332
|
+
if (view === undefined)
|
|
333
|
+
return { error: "not_found" };
|
|
334
|
+
if (view.surface.kind === "http" || view.credential.kind !== "static")
|
|
335
|
+
return { error: "invalid_input" };
|
|
336
|
+
try {
|
|
337
|
+
const done = await run(connections.update(owner, {
|
|
338
|
+
requestId: ids.connection.generate(),
|
|
339
|
+
connectionId: input.connectionId,
|
|
340
|
+
expectedVersion: input.expectedVersion,
|
|
341
|
+
credential: { kind: "none" },
|
|
342
|
+
}));
|
|
343
|
+
await notifyHolders(view);
|
|
344
|
+
return { version: done.version };
|
|
345
|
+
}
|
|
346
|
+
catch (cause) {
|
|
347
|
+
return credentialRefusal(cause);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
const notifyHolders = (view) => Promise.all(view.grants
|
|
351
|
+
.filter((grant) => grant.subject.kind === "agent")
|
|
352
|
+
.map((grant) => options.notify(grant.subject.id)));
|
|
272
353
|
return {
|
|
273
354
|
list,
|
|
274
355
|
addSecret,
|
|
275
356
|
secrets,
|
|
357
|
+
credentials,
|
|
358
|
+
updateCredential,
|
|
359
|
+
clearCredential,
|
|
276
360
|
grantSecret,
|
|
277
361
|
ungrantSecret,
|
|
278
|
-
/** Every recorded use and refusal of one
|
|
362
|
+
/** Every recorded use and refusal of one credential, without its value. */
|
|
279
363
|
secretUsage: (connectionId) => run(connections.audit(owner, connectionId)),
|
|
280
364
|
/** Complete one request with a secret on the owner side; the caller names its subject. */
|
|
281
365
|
fetchSecret: (subject, ref, data) => answerEgressRequest(connections.egress, subject, ref, data),
|
|
@@ -28,6 +28,7 @@ import { DurableObject } from "cloudflare:workers";
|
|
|
28
28
|
import { type StandaloneComputerEnv } from "./computer.js";
|
|
29
29
|
import { ShiitakeDO } from "@fungi.computer/shiitake/cloudflare";
|
|
30
30
|
import { type ModelKeyRecord, type ModelKeyRefusal, type ModelKeySealInput } from "./model-key-vault.js";
|
|
31
|
+
import type { VaultCommand, VaultResult } from "./vault.js";
|
|
31
32
|
import type { CloudflareShiitakeOptions } from "@fungi.computer/shiitake/cloudflare";
|
|
32
33
|
import { type AgentModelChoice, type AgentModelRefusal } from "./agent-model-choice.js";
|
|
33
34
|
import * as Schema from "effect/Schema";
|
|
@@ -67,6 +68,8 @@ export type StandaloneAgentsEnv = Readonly<{
|
|
|
67
68
|
OYSTER_WEBSITE_PROOF_KEY?: string;
|
|
68
69
|
/** Configured standalone-owner identity subject; never a stored member ID. */
|
|
69
70
|
OYSTER_OWNER_NAME?: string;
|
|
71
|
+
/** The host's own configured Computer; the starter's Home when it is there. */
|
|
72
|
+
OYSTER_COMPUTER_ID?: string;
|
|
70
73
|
/** Gateway custody remains in Veil; only the host receives these bindings. */
|
|
71
74
|
AI_GATEWAY_AUTH_TOKEN?: string;
|
|
72
75
|
AI_GATEWAY_ACCOUNT_ID?: string;
|
|
@@ -291,6 +294,24 @@ export declare abstract class StandaloneAgentBase<Env extends StandaloneAgentEnv
|
|
|
291
294
|
}>>;
|
|
292
295
|
/** Secret-free default of this Agent; stored separately from Session history. */
|
|
293
296
|
readModelChoice(): Promise<AgentModelChoice>;
|
|
297
|
+
/**
|
|
298
|
+
* Keep the starter's setup prompt on this Agent while it has no Home to hold
|
|
299
|
+
* an AGENTS.md. Written once; Agents that never needed it have no such key.
|
|
300
|
+
* `actor` is the owner whose grant later writes it into a Home.
|
|
301
|
+
*/
|
|
302
|
+
seedFirstConversation(content: string, actor: string): Promise<void>;
|
|
303
|
+
/** The instructions every run of this Agent starts from. */
|
|
304
|
+
protected agentInstructions(): Promise<{
|
|
305
|
+
revision: string;
|
|
306
|
+
text: string;
|
|
307
|
+
}>;
|
|
308
|
+
/**
|
|
309
|
+
* The kept setup prompt, until a Home exists. Then it moves into that Home's
|
|
310
|
+
* AGENTS.md when the Home has none (the starter seed's rule), so an
|
|
311
|
+
* interview that gains a Home midway keeps its instructions, and the kept
|
|
312
|
+
* copy is dropped. A Home that already has an AGENTS.md is left as it is.
|
|
313
|
+
*/
|
|
314
|
+
private firstConversation;
|
|
294
315
|
setModelChoice(input: AgentModelChoice): Promise<AgentModelChoice | Readonly<{
|
|
295
316
|
error: AgentModelRefusal;
|
|
296
317
|
}>>;
|
|
@@ -377,9 +398,15 @@ export declare class AgentDirectory extends DurableObject<StandaloneAgentEnv> {
|
|
|
377
398
|
method: string;
|
|
378
399
|
headers: readonly (readonly [string, string])[];
|
|
379
400
|
body: string | null;
|
|
380
|
-
}
|
|
401
|
+
}>,
|
|
402
|
+
/** The Agent the use is audited to; never its prompt or reply. */
|
|
403
|
+
agentId?: string | null): Promise<Response | Readonly<{
|
|
381
404
|
error: ModelKeyRefusal;
|
|
382
405
|
}>>;
|
|
406
|
+
private modelKeyUsageOwner;
|
|
407
|
+
private modelKeyUsage;
|
|
408
|
+
/** The Vault page's one owner door: every stored credential, write-only. */
|
|
409
|
+
vault(command: VaultCommand): Promise<VaultResult>;
|
|
383
410
|
private redactionSet;
|
|
384
411
|
/**
|
|
385
412
|
* Host-only: fingerprints of every credential this directory stores (model
|
|
@@ -409,10 +436,10 @@ export declare class AgentDirectory extends DurableObject<StandaloneAgentEnv> {
|
|
|
409
436
|
}>[];
|
|
410
437
|
}>[]>;
|
|
411
438
|
addConnection(input: ConnectionInput): Promise<Readonly<{
|
|
439
|
+
error: "not_found" | "duplicate_name" | "limit" | "invalid_endpoint";
|
|
440
|
+
}> | Readonly<{
|
|
412
441
|
connectionId: string;
|
|
413
442
|
authorizeUrl: string | null;
|
|
414
|
-
}> | Readonly<{
|
|
415
|
-
error: "not_found" | "duplicate_name" | "limit" | "invalid_endpoint";
|
|
416
443
|
}>>;
|
|
417
444
|
grantConnection(connectionId: string, agentId: string, access: ConnectionAccess): Promise<Readonly<{
|
|
418
445
|
error: "not_found" | "duplicate_name" | "limit" | "invalid_endpoint";
|
|
@@ -51,6 +51,7 @@ import { ShiitakeDO } from "@fungi.computer/shiitake/cloudflare";
|
|
|
51
51
|
import { agentStoreKeeping } from "./agent-store.js";
|
|
52
52
|
import { CredentialError, } from "@fungi.computer/veil/credential-store";
|
|
53
53
|
import { createInstanceCredentialStore, createModelKeyVault, instanceCredentialKeyring, instanceSecretCustody, ModelKeyRefused, } from "./model-key-vault.js";
|
|
54
|
+
import { createModelKeyUsage } from "./model-key-usage.js";
|
|
54
55
|
import { agentArchiveOwner } from "./agent-archive-owner.js";
|
|
55
56
|
import { agentArchiveProjection } from "../agent-archive-projection.js";
|
|
56
57
|
import { standaloneBrowserLiveView, standaloneBrowserSource, } from "./browser.js";
|
|
@@ -98,13 +99,15 @@ const logRedactedFailure = (label, cause) => {
|
|
|
98
99
|
console.error(label, text);
|
|
99
100
|
});
|
|
100
101
|
};
|
|
101
|
-
/** The starter
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
102
|
+
/** The starter's Home attachment: one fixed id, so a replayed create attaches once. */
|
|
103
|
+
const starterHomeAttachment = "starter-home:v1";
|
|
104
|
+
/** The Agent's own storage key for the setup prompt it carries while it has no Home. */
|
|
105
|
+
const firstConversationKey = "first-conversation";
|
|
106
|
+
/** The kept prompt, and the owner whose grant writes it to a later Home. */
|
|
107
|
+
const firstConversationSchema = z.object({
|
|
108
|
+
content: z.string().min(1),
|
|
109
|
+
actor: z.string().min(1),
|
|
110
|
+
});
|
|
108
111
|
const noHomeInstructions = {
|
|
109
112
|
home: null,
|
|
110
113
|
content: null,
|
|
@@ -1160,6 +1163,59 @@ export class StandaloneAgentBase extends ShiitakeDO {
|
|
|
1160
1163
|
async readModelChoice() {
|
|
1161
1164
|
return this.choice().read();
|
|
1162
1165
|
}
|
|
1166
|
+
/**
|
|
1167
|
+
* Keep the starter's setup prompt on this Agent while it has no Home to hold
|
|
1168
|
+
* an AGENTS.md. Written once; Agents that never needed it have no such key.
|
|
1169
|
+
* `actor` is the owner whose grant later writes it into a Home.
|
|
1170
|
+
*/
|
|
1171
|
+
async seedFirstConversation(content, actor) {
|
|
1172
|
+
if ((await this.ctx.storage.get(firstConversationKey)) === undefined)
|
|
1173
|
+
await this.ctx.storage.put(firstConversationKey, firstConversationSchema.parse({ content, actor }));
|
|
1174
|
+
}
|
|
1175
|
+
/** The instructions every run of this Agent starts from. */
|
|
1176
|
+
async agentInstructions() {
|
|
1177
|
+
const { appApprovalInstructions } = await import("../mcp.js");
|
|
1178
|
+
const first = await this.firstConversation();
|
|
1179
|
+
return {
|
|
1180
|
+
revision: first === null ? "oyster-7" : "oyster-7+first-conversation",
|
|
1181
|
+
text: instructions +
|
|
1182
|
+
"\n" +
|
|
1183
|
+
appApprovalInstructions +
|
|
1184
|
+
(first === null ? "" : "\n\n" + first),
|
|
1185
|
+
};
|
|
1186
|
+
}
|
|
1187
|
+
/**
|
|
1188
|
+
* The kept setup prompt, until a Home exists. Then it moves into that Home's
|
|
1189
|
+
* AGENTS.md when the Home has none (the starter seed's rule), so an
|
|
1190
|
+
* interview that gains a Home midway keeps its instructions, and the kept
|
|
1191
|
+
* copy is dropped. A Home that already has an AGENTS.md is left as it is.
|
|
1192
|
+
*/
|
|
1193
|
+
async firstConversation() {
|
|
1194
|
+
const stored = await this.ctx.storage.get(firstConversationKey);
|
|
1195
|
+
if (stored === undefined)
|
|
1196
|
+
return null;
|
|
1197
|
+
const { content, actor } = firstConversationSchema.parse(stored);
|
|
1198
|
+
const attachments = (await this.preparedComputerSnapshot())?.projection.attachments ?? [];
|
|
1199
|
+
if (attachments.length === 0)
|
|
1200
|
+
return content;
|
|
1201
|
+
// Until the move lands (the Home went away, or its Computer could not
|
|
1202
|
+
// write), the prompt keeps riding in the instructions; a later run retries.
|
|
1203
|
+
let moved;
|
|
1204
|
+
try {
|
|
1205
|
+
moved = await this.replaceInstructions(actor, null, content);
|
|
1206
|
+
}
|
|
1207
|
+
catch (cause) {
|
|
1208
|
+
logRedactedFailure("starter setup prompt move failed", cause);
|
|
1209
|
+
return content;
|
|
1210
|
+
}
|
|
1211
|
+
if ("error" in moved && moved.error !== "conflict") {
|
|
1212
|
+
if (moved.error === "too_large")
|
|
1213
|
+
logRedactedFailure("starter setup prompt move failed", new Error("The setup prompt exceeds the AGENTS.md limit"));
|
|
1214
|
+
return content;
|
|
1215
|
+
}
|
|
1216
|
+
await this.ctx.storage.delete(firstConversationKey);
|
|
1217
|
+
return null;
|
|
1218
|
+
}
|
|
1163
1219
|
async setModelChoice(input) {
|
|
1164
1220
|
try {
|
|
1165
1221
|
return await this.choice().set(input);
|
|
@@ -1215,7 +1271,7 @@ export class StandaloneAgentBase extends ShiitakeDO {
|
|
|
1215
1271
|
method: request.method,
|
|
1216
1272
|
headers: [...request.headers],
|
|
1217
1273
|
body: request.body === null ? null : await request.text(),
|
|
1218
|
-
});
|
|
1274
|
+
}, this.ctx.id.name ?? null);
|
|
1219
1275
|
if (!(sent instanceof Response))
|
|
1220
1276
|
throw new AgentModelRefused(sent.error === "key_removed"
|
|
1221
1277
|
? "key_removed"
|
|
@@ -1362,13 +1418,7 @@ export class StandaloneAgentBase extends ShiitakeDO {
|
|
|
1362
1418
|
meta: { schema: FungiMessageMetaSchema },
|
|
1363
1419
|
...this.mail().host,
|
|
1364
1420
|
instructions: this.channelInstructions({
|
|
1365
|
-
load:
|
|
1366
|
-
const { appApprovalInstructions } = await import("../mcp.js");
|
|
1367
|
-
return {
|
|
1368
|
-
revision: "oyster-7",
|
|
1369
|
-
text: instructions + "\n" + appApprovalInstructions,
|
|
1370
|
-
};
|
|
1371
|
-
},
|
|
1421
|
+
load: () => this.agentInstructions(),
|
|
1372
1422
|
}),
|
|
1373
1423
|
tools: {
|
|
1374
1424
|
// A run outlasts one ten-minute Computer command; waiting for a
|
|
@@ -1631,19 +1681,42 @@ export class AgentDirectory extends DurableObject {
|
|
|
1631
1681
|
return this.modelKeys().remove(keyId);
|
|
1632
1682
|
}
|
|
1633
1683
|
/** Host-only: the one place a stored key meets a provider request. */
|
|
1634
|
-
async sendModelKey(keyId, request
|
|
1684
|
+
async sendModelKey(keyId, request,
|
|
1685
|
+
/** The Agent the use is audited to; never its prompt or reply. */
|
|
1686
|
+
agentId = null) {
|
|
1687
|
+
let status = null;
|
|
1635
1688
|
try {
|
|
1636
|
-
|
|
1689
|
+
const response = await this.modelKeys().send(keyId, new Request(request.url, {
|
|
1637
1690
|
method: request.method,
|
|
1638
1691
|
headers: request.headers.map(([name, value]) => [name, value]),
|
|
1639
1692
|
body: request.body,
|
|
1640
1693
|
}));
|
|
1694
|
+
status = response.status;
|
|
1695
|
+
return response;
|
|
1641
1696
|
}
|
|
1642
1697
|
catch (error) {
|
|
1643
1698
|
if (error instanceof ModelKeyRefused)
|
|
1644
1699
|
return { error: error.reason };
|
|
1645
1700
|
throw error;
|
|
1646
1701
|
}
|
|
1702
|
+
finally {
|
|
1703
|
+
this.modelKeyUsage().record({ keyId, agentId, status });
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
modelKeyUsageOwner;
|
|
1707
|
+
modelKeyUsage() {
|
|
1708
|
+
return (this.modelKeyUsageOwner ??= createModelKeyUsage({
|
|
1709
|
+
sql: this.ctx.storage.sql,
|
|
1710
|
+
}));
|
|
1711
|
+
}
|
|
1712
|
+
/** The Vault page's one owner door: every stored credential, write-only. */
|
|
1713
|
+
async vault(command) {
|
|
1714
|
+
const { createInstanceVault } = await import("./vault.js");
|
|
1715
|
+
return createInstanceVault({
|
|
1716
|
+
connections: await this.mcpConnections(),
|
|
1717
|
+
modelKeys: () => this.listModelKeys(),
|
|
1718
|
+
modelKeyUsage: (keyId) => this.modelKeyUsage().list(keyId),
|
|
1719
|
+
})(command);
|
|
1647
1720
|
}
|
|
1648
1721
|
redactionSet;
|
|
1649
1722
|
/**
|
|
@@ -2678,6 +2751,11 @@ export function standaloneAgents(env, scope) {
|
|
|
2678
2751
|
create: created,
|
|
2679
2752
|
ensureStarter: (principal) => {
|
|
2680
2753
|
const owner = directory();
|
|
2754
|
+
const reportStarterFailure = (cause) => {
|
|
2755
|
+
void redactedFailure(cause).then((text) => {
|
|
2756
|
+
console.warn(JSON.stringify({ event: "starter_agent_failed", cause: text }));
|
|
2757
|
+
});
|
|
2758
|
+
};
|
|
2681
2759
|
return ensureStarterAgent({
|
|
2682
2760
|
ledger: {
|
|
2683
2761
|
read: () => owner.readStarter(),
|
|
@@ -2685,14 +2763,32 @@ export function standaloneAgents(env, scope) {
|
|
|
2685
2763
|
},
|
|
2686
2764
|
isOwner: () => oysterPrincipalIsOwner(principal),
|
|
2687
2765
|
agentCount: () => owner.agentTotal(),
|
|
2688
|
-
// The ordinary create
|
|
2766
|
+
// The ordinary create, then the ordinary owner attach of the host's own
|
|
2767
|
+
// configured Computer as its Home. A host with no such Computer, or one
|
|
2768
|
+
// the attach refuses, leaves the starter without a Home.
|
|
2689
2769
|
create: async (request) => {
|
|
2690
2770
|
const agent = await created(request.name, request.requestId, principal, "owner-direct");
|
|
2771
|
+
const computerId = env.OYSTER_COMPUTER_ID;
|
|
2772
|
+
// The Agent exists now; a failed Home attach must not fail the
|
|
2773
|
+
// create, or the starter ledger is never written. It is reported,
|
|
2774
|
+
// and the seed falls back to the Agent's own record.
|
|
2775
|
+
if (computerId !== undefined && computerId !== "")
|
|
2776
|
+
await directory()
|
|
2777
|
+
.attachComputer({
|
|
2778
|
+
agentId: agent.agentId,
|
|
2779
|
+
attachmentId: starterHomeAttachment,
|
|
2780
|
+
computerId,
|
|
2781
|
+
requestId: starterHomeAttachment,
|
|
2782
|
+
}, principal, scope?.tenantId ?? defaultTenant)
|
|
2783
|
+
.catch((cause) => {
|
|
2784
|
+
reportStarterFailure(cause);
|
|
2785
|
+
});
|
|
2691
2786
|
return { status: "created", agentId: agent.agentId };
|
|
2692
2787
|
},
|
|
2693
2788
|
// No money step on Oyster: the owner brings their own model, so the
|
|
2694
2789
|
// seed is only the first-run section, written to a Home that has no
|
|
2695
|
-
// AGENTS.md. A Home the owner already wrote to is left as it is.
|
|
2790
|
+
// AGENTS.md. A Home the owner already wrote to is left as it is. With
|
|
2791
|
+
// no Home, the same section stays on the Agent until it has one.
|
|
2696
2792
|
seedHome: async (agentId) => {
|
|
2697
2793
|
await seedStarterHome(async (path, content) => {
|
|
2698
2794
|
if (path !== "AGENTS.md")
|
|
@@ -2703,18 +2799,11 @@ export function standaloneAgents(env, scope) {
|
|
|
2703
2799
|
if (result.error === "conflict")
|
|
2704
2800
|
return;
|
|
2705
2801
|
if (result.error === "no_home")
|
|
2706
|
-
|
|
2802
|
+
return env.AGENTS.getByName(agentId).seedFirstConversation(content, JSON.stringify(principal));
|
|
2707
2803
|
throw new Error(`Starter Home seed refused: ${result.error}`);
|
|
2708
2804
|
});
|
|
2709
2805
|
},
|
|
2710
|
-
report:
|
|
2711
|
-
// Waiting for the owner to attach a Computer is not a failure.
|
|
2712
|
-
if (cause instanceof StarterHomePending)
|
|
2713
|
-
return;
|
|
2714
|
-
void redactedFailure(cause).then((text) => {
|
|
2715
|
-
console.warn(JSON.stringify({ event: "starter_agent_failed", cause: text }));
|
|
2716
|
-
});
|
|
2717
|
-
},
|
|
2806
|
+
report: reportStarterFailure,
|
|
2718
2807
|
});
|
|
2719
2808
|
},
|
|
2720
2809
|
rename: async (agentId, name, principal) => {
|
|
@@ -2773,6 +2862,7 @@ export function standaloneAgents(env, scope) {
|
|
|
2773
2862
|
modelKeys: () => directory().listModelKeys(),
|
|
2774
2863
|
sealModelKey: (input) => directory().sealModelKey(input),
|
|
2775
2864
|
removeModelKey: (keyId) => directory().removeModelKey(keyId),
|
|
2865
|
+
vault: (command) => directory().vault(command),
|
|
2776
2866
|
policyStatus: async (agentId) => directory().policyStatus(agentId),
|
|
2777
2867
|
appToolPolicy: (actor) => directory().appToolPolicy(actor, scope?.tenantId ?? defaultTenant),
|
|
2778
2868
|
policyRevision: (agentId) => directory().policyRevision(agentId),
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type ModelKeyUse = Readonly<{
|
|
2
|
+
keyId: string;
|
|
3
|
+
agentId: string | null;
|
|
4
|
+
/** The provider's status, or null when nothing came back. */
|
|
5
|
+
status: number | null;
|
|
6
|
+
}>;
|
|
7
|
+
/** One recorded use, as the Vault lists it. */
|
|
8
|
+
export type ModelKeyUsage = Readonly<{
|
|
9
|
+
keyId: string;
|
|
10
|
+
subject: Readonly<{
|
|
11
|
+
kind: "agent";
|
|
12
|
+
id: string;
|
|
13
|
+
}> | null;
|
|
14
|
+
outcome: "upstream_status" | "failed";
|
|
15
|
+
status: number | null;
|
|
16
|
+
firstAt: number;
|
|
17
|
+
lastAt: number;
|
|
18
|
+
count: number;
|
|
19
|
+
}>;
|
|
20
|
+
export declare function createModelKeyUsage(options: {
|
|
21
|
+
sql: SqlStorage;
|
|
22
|
+
now?: () => number;
|
|
23
|
+
}): {
|
|
24
|
+
record(use: ModelKeyUse): void;
|
|
25
|
+
/** The newest uses of one key, oldest first. */
|
|
26
|
+
list(keyId: string): readonly ModelKeyUsage[];
|
|
27
|
+
};
|
|
28
|
+
export {};
|