@fungi.computer/oyster 0.49.1 → 0.50.1

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/files.js CHANGED
@@ -6,10 +6,15 @@
6
6
  * the Oyster's workspace unchanged. Other sources (a git repository) implement
7
7
  * the same `FileSource`.
8
8
  */
9
+ import { bodyAtMost } from "./body-limit.js";
9
10
  import { computer, routes } from "./authorization.js";
10
11
  import * as Schema from "effect/Schema";
11
12
  import { computerList, computerReadFile, computerWriteFile, } from "@fungi.computer/agar/computer";
12
13
  const MAX_FILE_BYTES = 25 * 1024 * 1024;
14
+ /** A replace carries the expected and the new text: each at most a file. */
15
+ const MAX_REPLACE_REQUEST_BYTES = 2 * MAX_FILE_BYTES + 64 * 1024;
16
+ /** A mkdir carries one path of at most 4,096 characters. */
17
+ const MAX_PATH_REQUEST_BYTES = 16 * 1024;
13
18
  const MAX_LIST = 1000;
14
19
  /** Normalize an absolute path; `null` when it is not one or escapes the root. */
15
20
  export function normalizeFilePath(raw) {
@@ -96,15 +101,9 @@ function createFileRoutes(source) {
96
101
  const path = pathOf(context.req.query("path"));
97
102
  if (path === null || path === "/")
98
103
  return refusal(400, "Invalid path");
99
- const length = Number(context.req.header("content-length") ?? "0");
100
- if (length > MAX_FILE_BYTES)
101
- return refusal(413, "File too large");
102
- const bytes = new Uint8Array(await context.req.arrayBuffer());
103
- if (bytes.length > MAX_FILE_BYTES)
104
- return refusal(413, "File too large");
105
- await source.write(path, bytes);
104
+ await source.write(path, new Uint8Array(await context.req.arrayBuffer()));
106
105
  return new Response(null, { status: 204 });
107
- });
106
+ }, bodyAtMost(MAX_FILE_BYTES, "File too large"));
108
107
  table.post("/mkdir", computer(), async (context) => {
109
108
  const body = Schema.decodeUnknownOption(mkdirSchema)(await context.req.json());
110
109
  const path = body._tag === "Some" ? normalizeFilePath(body.value.path) : null;
@@ -112,13 +111,13 @@ function createFileRoutes(source) {
112
111
  return refusal(400, "Invalid path");
113
112
  await source.mkdir(path);
114
113
  return new Response(null, { status: 204 });
115
- });
114
+ }, bodyAtMost(MAX_PATH_REQUEST_BYTES));
116
115
  table.post("/replace", computer(), async (context) => {
117
116
  const body = Schema.decodeUnknownOption(replaceSchema)(await context.req.json());
118
117
  const path = body._tag === "Some" ? normalizeFilePath(body.value.path) : null;
119
118
  if (body._tag === "None" || path === null)
120
119
  return refusal(400, "Invalid request");
121
120
  return Response.json(await source.replace(path, body.value.expectedContent, body.value.content));
122
- });
121
+ }, bodyAtMost(MAX_REPLACE_REQUEST_BYTES));
123
122
  return table;
124
123
  }
@@ -0,0 +1,6 @@
1
+ import { type Routes } from "../authorization.js";
2
+ import type { OysterEnv } from "../host.js";
3
+ import type { StandaloneRegistryRpc } from "./registry.js";
4
+ import type { ComputerDO } from "./computer.js";
5
+ /** Only the live tenant owner and an inventoried Computer reach the native port. */
6
+ export declare function standaloneComputerApiRoutes(registry: () => Pick<StandaloneRegistryRpc, "computerFleet">, computer: (id: string) => Pick<ComputerDO, "apiOperation">): Routes<OysterEnv>;
@@ -0,0 +1,122 @@
1
+ import * as Schema from "effect/Schema";
2
+ import { bodyLimit } from "hono/body-limit";
3
+ import { computerApiBodyBytes, computerApiDotSegment, computerApiId, computerApiOperations, computerApiRequestSchema, } from "../computer-api.js";
4
+ import { owner, routes } from "../authorization.js";
5
+ import { sameOriginReadRequest, sameOriginRequest } from "../request-origin.js";
6
+ /** `application/json`, with or without parameters; any other media type is not a JSON body. */
7
+ function isJsonBody(contentType) {
8
+ return (contentType?.split(";")[0]?.trim().toLowerCase() === "application/json");
9
+ }
10
+ /** The only fields a request may name a machine path in; other fields are decoded by the operation. */
11
+ const pathFields = Schema.Struct({
12
+ path: Schema.optionalKey(Schema.String),
13
+ cwd: Schema.optionalKey(Schema.String),
14
+ });
15
+ /** Only the live tenant owner and an inventoried Computer reach the native port. */
16
+ export function standaloneComputerApiRoutes(registry, computer) {
17
+ const table = routes();
18
+ const strict = { onExcessProperty: "error" };
19
+ const decode = Schema.decodeUnknownSync(computerApiRequestSchema, strict);
20
+ const query = Schema.Record(Schema.String, Schema.Array(Schema.String).check(Schema.isBetweenLength(1, 1)));
21
+ const methods = {
22
+ get: "GET",
23
+ post: "POST",
24
+ put: "PUT",
25
+ delete: "DELETE",
26
+ };
27
+ for (const operation of computerApiOperations) {
28
+ table.on([methods[operation.method]], `/api/computers/:computerId${operation.path}`, owner("direct-or-app"), async (context, { principal }) => {
29
+ context.header("Cache-Control", "no-store");
30
+ // The owner cookie is the only credential, so the request must prove
31
+ // where it came from and say it is JSON before anything is admitted: a
32
+ // page on a sibling site can otherwise post a text/plain form whose
33
+ // body is valid JSON. A reading route needs only no foreign provenance.
34
+ const mutates = operation.method !== "get";
35
+ if (!(mutates
36
+ ? sameOriginRequest(context.req.raw)
37
+ : sameOriginReadRequest(context.req.raw)))
38
+ return context.json({ error: "cross_origin" }, 403);
39
+ if ((operation.method === "post" || operation.method === "put") &&
40
+ !isJsonBody(context.req.header("content-type")))
41
+ return context.json({ error: "unsupported_media_type" }, 415);
42
+ let id;
43
+ try {
44
+ id = Schema.decodeUnknownSync(computerApiId)(context.req.param("computerId"));
45
+ }
46
+ catch {
47
+ return context.json({ error: "invalid" }, 400);
48
+ }
49
+ const admit = async () => {
50
+ const result = await registry().computerFleet(principal.actor.memberId, { operation: "resolve", computerId: id });
51
+ return result !== null && !("error" in result);
52
+ };
53
+ try {
54
+ if (!(await admit()))
55
+ return context.json({ error: "denied" }, 403);
56
+ let request;
57
+ try {
58
+ let input;
59
+ if (operation.method === "get" || operation.method === "delete") {
60
+ if (context.req.url.length > 8192)
61
+ return context.json({ error: "too_large" }, 413);
62
+ const parameters = Schema.decodeUnknownSync(query)(context.req.queries());
63
+ input = Object.fromEntries(Object.entries(parameters).map(([key, values]) => [
64
+ key,
65
+ ["lines", "limit", "offset"].includes(key)
66
+ ? Schema.decodeUnknownSync(Schema.NumberFromString)(values[0])
67
+ : values[0],
68
+ ]));
69
+ }
70
+ else
71
+ input = await context.req.json();
72
+ const { path, cwd } = Schema.decodeUnknownSync(pathFields)(input);
73
+ if ([path, cwd].some((value) => value !== undefined && computerApiDotSegment.test(value)))
74
+ return context.json({ error: "invalid_path" }, 400);
75
+ request = decode({ operation: operation.operation, input });
76
+ }
77
+ catch {
78
+ return context.json({ error: "invalid" }, 400);
79
+ }
80
+ // Body consumption may await a client; re-admit before any native mutation.
81
+ if (!(await admit()))
82
+ return context.json({ error: "denied" }, 403);
83
+ const action = operation.action;
84
+ const result = await computer(id).apiOperation({
85
+ computerId: id,
86
+ actions: new Set([action]),
87
+ expiresAt: Date.now() + 60_000,
88
+ provenance: { actor: `owner:${principal.actor.memberId}` },
89
+ }, request);
90
+ if ("error" in result) {
91
+ switch (result.error) {
92
+ case "too_large":
93
+ return context.json({ error: "too_large" }, 413);
94
+ case "denied":
95
+ return context.json({ error: "denied" }, 403);
96
+ case "not_found":
97
+ return context.json({ error: "not_found" }, 404);
98
+ case "conflict":
99
+ return context.json({ error: "conflict" }, 409);
100
+ case "invalid":
101
+ return context.json({ error: "invalid" }, 400);
102
+ default:
103
+ return context.json({ error: "unavailable" }, 503);
104
+ }
105
+ }
106
+ return context.json(result);
107
+ }
108
+ catch {
109
+ // Native errors may contain commands, file content or provider credentials.
110
+ return context.json({ error: "unavailable" }, 503);
111
+ }
112
+ }, {
113
+ middleware: [
114
+ bodyLimit({
115
+ maxSize: computerApiBodyBytes,
116
+ onError: (context) => context.json({ error: "too_large" }, 413),
117
+ }),
118
+ ],
119
+ });
120
+ }
121
+ return table;
122
+ }
@@ -1,3 +1,4 @@
1
+ import { type ComputerApiRequest } from "../computer-api.js";
1
2
  import { type ComputerSize } from "@fungi.computer/computer/backing";
2
3
  import { type AppMethodRefusedReply } from "@fungi.computer/agar/backend";
3
4
  import type { WatchdogSqliteOwner } from "@fungi.computer/watchdog";
@@ -214,6 +215,10 @@ export declare class ComputerDO extends NativeComputerDO implements ComputerFetc
214
215
  protected attachedBackends(): readonly AttachedBackend[];
215
216
  /** One advertised Computer tool, accepted against the host-issued grant. */
216
217
  tool(authority: ComputerGrant, capabilityId: string, input: ComputerValue): Promise<StandaloneComputerResult>;
218
+ /** Public REST reuses native receipts, process retention and files; it owns no store. */
219
+ apiOperation(authority: ComputerGrant, request: ComputerApiRequest): Promise<StandaloneComputerResult | Readonly<{
220
+ error: "too_large" | "unavailable" | "denied";
221
+ }>>;
217
222
  /** JSON operations keep the Computer's maintained parsers and execution engine. */
218
223
  value(authority: ComputerGrant, action: ComputerAction, input: ComputerValue, executionContext?: ComputerValue): Promise<StandaloneComputerResult>;
219
224
  fileList(authority: ComputerGrant, path: string, limit: number, offset: number): Promise<{
@@ -1,5 +1,7 @@
1
1
  import { agentWorkspacePath } from "@fungi.computer/computer/files";
2
2
  import { PortAccessError } from "@fungi.computer/computer/port-access";
3
+ import { ComputerFileTooLargeError, ComputerProcessNotFoundError, ComputerPortReservedError, maximumFileBytes, genericComputerExecutionBudget, } from "@fungi.computer/computer-contract";
4
+ import { computerApiRequestSchema, } from "../computer-api.js";
3
5
  import { attachedBackendId, computerSizeSchema, defaultComputerSize, hostedBackingSchema, } from "@fungi.computer/computer/backing";
4
6
  import { appMethodRefusalStatus, appMethodRefusedReplySchema, } from "@fungi.computer/agar/backend";
5
7
  import * as Schema from "effect/Schema";
@@ -578,6 +580,61 @@ export class ComputerDO extends NativeComputerDO {
578
580
  : undefined;
579
581
  return output.parse(await this.callAfterAcceptance(tool, input, accept, vault));
580
582
  }
583
+ /** Public REST reuses native receipts, process retention and files; it owns no store. */
584
+ async apiOperation(authority, request) {
585
+ try {
586
+ const parsed = Schema.decodeUnknownSync(computerApiRequestSchema, {
587
+ onExcessProperty: "error",
588
+ })(request);
589
+ switch (parsed.operation) {
590
+ case "exec":
591
+ return await this.executeOperationAfterAcceptance({ ...parsed.input, timeoutMs: parsed.input.timeoutMs ?? 10_000 }, () => {
592
+ this.#accept(authority, "exec");
593
+ }, "exec", genericComputerExecutionBudget);
594
+ case "start":
595
+ return await this.value(authority, "processStart", {
596
+ ...parsed.input,
597
+ requestScope: `rest:${authority.provenance.actor}`,
598
+ });
599
+ case "logs":
600
+ return await this.value(authority, "processLogs", parsed.input);
601
+ case "kill":
602
+ return await this.value(authority, "processKill", parsed.input);
603
+ case "read":
604
+ return readOutputSchema.parse(await this.callAfterAcceptance("read", parsed.input, () => {
605
+ this.#accept(authority, "files.read");
606
+ }));
607
+ case "write":
608
+ if (new TextEncoder().encode(parsed.input.content).byteLength >
609
+ maximumFileBytes)
610
+ return { error: "too_large" };
611
+ return await this.value(authority, "files.write", parsed.input);
612
+ case "list":
613
+ return await this.value(authority, "files.list", parsed.input);
614
+ case "remove":
615
+ return await this.value(authority, "files.remove", parsed.input);
616
+ case "expose":
617
+ return await this.value(authority, "exposePort", parsed.input);
618
+ }
619
+ }
620
+ catch (cause) {
621
+ if (cause instanceof ComputerFileTooLargeError)
622
+ return { error: "too_large" };
623
+ if (cause instanceof ComputerProcessNotFoundError ||
624
+ missingFile.safeParse(cause).success)
625
+ return { error: "not_found" };
626
+ if (cause instanceof z.ZodError ||
627
+ Schema.isSchemaError(cause) ||
628
+ cause instanceof ComputerPortReservedError)
629
+ return { error: "invalid" };
630
+ if (cause instanceof ComputerOperationConflictError ||
631
+ cause instanceof ComputerPortConflictError)
632
+ return { error: "conflict" };
633
+ if (cause instanceof FleetGrantRefused)
634
+ return { error: "denied" };
635
+ return { error: "unavailable" };
636
+ }
637
+ }
581
638
  /** JSON operations keep the Computer's maintained parsers and execution engine. */
582
639
  async value(authority, action, input, executionContext) {
583
640
  // Only an exact public grant rides along, and only on a native command.
@@ -12,6 +12,6 @@ import { type AppSourceBindingEnv } from "./app-source.js";
12
12
  import type { StandaloneComputerEnv } from "./computer.js";
13
13
  import type { StandaloneWorkflowEnv } from "./workflows.js";
14
14
  /** The bindings the standalone owner routes reach, resolved per request. */
15
- export type StandaloneOwnerRoutesEnv = AppSourceBindingEnv & Pick<StandaloneAgentsEnv, "AGENTS" | "OYSTER_ORIGIN"> & Pick<StandaloneComputerEnv, "REGISTRY"> & Pick<StandaloneAgentsEnv, "AGENT_DIRECTORY"> & Pick<StandaloneWorkflowEnv, "WORKFLOW_RUNS">;
15
+ export type StandaloneOwnerRoutesEnv = AppSourceBindingEnv & Pick<StandaloneAgentsEnv, "AGENTS" | "OYSTER_ORIGIN"> & Pick<StandaloneComputerEnv, "REGISTRY" | "COMPUTER"> & Pick<StandaloneAgentsEnv, "AGENT_DIRECTORY"> & Pick<StandaloneWorkflowEnv, "WORKFLOW_RUNS">;
16
16
  /** Every owner route the standalone root adds to its tenant host. */
17
17
  export declare function standaloneOwnerRoutes(env: StandaloneOwnerRoutesEnv, scope: TenantScope, hostFor: () => Promise<Pick<OysterHost, "installations">>): Routes<OysterEnv>;
@@ -1,3 +1,4 @@
1
+ import { standaloneComputerApiRoutes } from "./computer-api.js";
1
2
  import { defaultTenant } from "../tenant.js";
2
3
  import { agentDirectoryForScope } from "./agent-directory.js";
3
4
  import { standaloneAgentComputerRoutes } from "./agent-computers.js";
@@ -16,6 +17,7 @@ export function standaloneOwnerRoutes(env, scope, hostFor) {
16
17
  const owned = appSourceOwnerRoutes(env, scope)
17
18
  .include(standaloneBuildRoutes(registry))
18
19
  .include(trialStarterRoutes(registry))
20
+ .include(standaloneComputerApiRoutes(registry, (id) => env.COMPUTER.getByName(id)))
19
21
  .include(standaloneComputerFleetRoutes(registry))
20
22
  .include(standaloneAgentComputerRoutes({
21
23
  attach: (principal, command) => directory().attachComputer(command, principal, scope.tenantId),