Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@opencomputer/cli",
"version": "0.7.23",
"version": "0.7.24",
"description": "Build, test, deploy, and share OpenComputer agents as code.",
"type": "module",
"bin": {
Expand Down
20 changes: 20 additions & 0 deletions cli/src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,3 +594,23 @@ test("session list sends exact filters, the page size and the cursor as query pa
assert.equal(query.get("limit"), "2");
assert.equal(query.get("cursor"), "eyJjIjoxfQ");
});

test("connection fetch posts to the provider's fetch route", async (context) => {
const requests: Request[] = [];
context.mock.method(
globalThis,
"fetch",
async (input: string | URL | Request, init?: RequestInit) => {
requests.push(new Request(input, init));
return Response.json({ status: 200, body: "{}" });
},
);
const client = new OpenComputerClient({ apiUrl: "https://app.opencomputer.dev", apiKey: "test" }, "setup");
await client.connectionFetch({ service: "linear", path: "/graphql", method: "POST", body: "{}" });
await client.connectionFetch({ service: "gmail", path: "/gmail/v1/users/me/profile" });
assert.deepEqual(
requests.map((r) => new URL(r.url).pathname),
["/api/managed-agents/connections/linear/fetch", "/api/managed-agents/connections/google/fetch"],
);
assert.deepEqual(await requests[0]!.json(), { service: "linear", path: "/graphql", method: "POST", body: "{}" });
});
61 changes: 61 additions & 0 deletions cli/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ export interface ProjectServiceAttachment {
updatedAt: string;
}

/** A connection granted to the organization's computer (work 080). */
export interface ComputerServiceAttachment {
service: string;
provider: string;
label: string;
connectionId: string;
connection: ServiceConnection;
/** `session` (one Slack thread), `user` (one Slack user's threads) or `everyone`. */
scope: "session" | "user" | "everyone";
subject: string;
grantedBy: string;
createdAt: string;
updatedAt: string;
}

export interface ModelAccessConnection {
id: string;
organizationId: string;
Expand Down Expand Up @@ -969,6 +984,52 @@ export class OpenComputerClient {
);
}

async computerServiceConnections(): Promise<ComputerServiceAttachment[]> {
const result = await this.request<{
attachments: ComputerServiceAttachment[];
}>("/api/managed-agents/computer/service-connections");
return result.attachments ?? [];
}

/** Grants one of your connections to everyone on the computer (admins only). */
grantComputerServiceConnection(connectionId: string) {
return this.request<ComputerServiceAttachment>(
"/api/managed-agents/computer/service-connections",
{ method: "POST", body: JSON.stringify({ connectionId }) },
);
}

/**
* Calls a service through a connection, as the connections route proxies
* it: the answer is the provider's response as JSON. On the computer, only
* granted connections answer.
*/
connectionFetch(input: {
service: string;
path: string;
method?: string;
headers?: Record<string, string>;
body?: string;
label?: string;
}) {
const provider =
input.service === "linear" || input.service === "github"
? input.service
: "google";
return this.request<unknown>(
`/api/managed-agents/connections/${provider}/fetch`,
{ method: "POST", body: JSON.stringify(input) },
);
}

/** Revokes every grant of a connection to the computer. */
revokeComputerServiceConnection(connectionId: string) {
return this.request<void>(
"/api/managed-agents/computer/service-connections",
{ method: "DELETE", body: JSON.stringify({ connectionId }) },
);
}

/**
* Live status for one connected account.
*
Expand Down
100 changes: 99 additions & 1 deletion cli/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2194,6 +2194,104 @@ export async function runCommand(
return;
}

// The organization's computer (work 080): Slack threads use only the
// connections people grant it. From here, an admin grants their own
// connection to everyone; thread and personal grants are made in Slack.
if (action === "grants") {
if (args.length) throw new Error(`Unexpected argument: ${args[0]}`);
const attachments = await client.computerServiceConnections();
if (globals.json) {
printJSON(attachments);
return;
}
if (!attachments.length) {
process.stdout.write("No connections are granted to the computer.\n");
return;
}
for (const attachment of attachments) {
const audience =
attachment.scope === "everyone"
? "everyone"
: `${attachment.scope} ${attachment.subject}`;
process.stdout.write(
`${attachment.label.padEnd(18)} ${attachment.service.padEnd(8)} ` +
`${audience.padEnd(26)} ${attachment.connectionId}\n`,
);
}
return;
}

if (action === "grant" || action === "revoke") {
const target = args.shift();
if (!target) {
throw new Error(
`Use \`opencomputer connection ${action} <alias|connection-id>\``,
);
}
const service = option(args, "--service");
if (args.length) throw new Error(`Unexpected argument: ${args[0]}`);
const connection = await findConnection(target, service);
const resolved = service ?? serviceOfConnection(connection);
if (action === "grant") {
// The computer reaches GitHub through its own app, never a person's.
if (resolved === "github") {
throw new Error("GitHub connections cannot be granted to the computer");
}
const attachment = await client.grantComputerServiceConnection(
connection.id,
);
if (globals.json) printJSON(attachment);
else
process.stdout.write(
`Granted ${connection.label} (${resolved}) to everyone on the computer.\n`,
);
} else {
await client.revokeComputerServiceConnection(connection.id);
if (globals.json) printJSON({ revoked: connection.id });
else
process.stdout.write(
`Revoked ${connection.label} (${resolved}) from the computer.\n`,
);
}
return;
}

// A call through a connection, for agents on the computer and scripts:
// `connection fetch linear /graphql --data '{"query":"{ viewer { id } }"}'`.
if (action === "fetch") {
const service = args.shift();
const path = args.shift();
if (!service || !path) {
throw new Error(
"Use `opencomputer connection fetch <service> <path> [--method <m>] [--data <body>] [--header 'Name: value'] [--alias <name>]`",
);
}
const data = option(args, "--data");
const headers: Record<string, string> = {};
for (const header of options(args, "--header")) {
const colon = header.indexOf(":");
if (colon < 1) throw new Error(`Invalid header ${JSON.stringify(header)}: use 'Name: value'`);
headers[header.slice(0, colon).trim()] = header.slice(colon + 1).trim();
}
const method = option(args, "--method") ?? (data === undefined ? "GET" : "POST");
const label = option(args, "--alias");
if (args.length) throw new Error(`Unexpected argument: ${args[0]}`);
if (data !== undefined && !Object.keys(headers).some((h) => h.toLowerCase() === "content-type")) {
headers["Content-Type"] = "application/json";
}
printJSON(
await client.connectionFetch({
service,
path,
method: method.toUpperCase(),
...(Object.keys(headers).length ? { headers } : {}),
...(data === undefined ? {} : { body: data }),
...(label ? { label } : {}),
}),
);
return;
}

if (action === "remove" || action === "disconnect") {
const target = args.shift();
if (!target) {
Expand All @@ -2216,7 +2314,7 @@ export async function runCommand(
}

throw new Error(
"Use `opencomputer connection connect|reconnect|list|status|attach|detach|remove`.",
"Use `opencomputer connection connect|reconnect|list|status|attach|detach|grant|revoke|grants|fetch|remove`.",
);
}

Expand Down
4 changes: 4 additions & 0 deletions cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ Usage:
opencomputer connection status <alias|connection-id> [--service <name>]
opencomputer connection attach <alias|connection-id> [--service <name>] [--project <id|slug|current>]
opencomputer connection detach <alias|connection-id> [--service <name>] [--project <id|slug|current>]
opencomputer connection grant <alias|connection-id> [--service <name>]
opencomputer connection revoke <alias|connection-id> [--service <name>]
opencomputer connection grants
opencomputer connection fetch <service> <path> [--method <m>] [--data <body>] [--header 'Name: value'] [--alias <name>]
opencomputer connection remove <alias|connection-id> [--service <name>]
opencomputer github status [--project <id|slug>]
opencomputer github connect [--new | --connection <id|account>] [--environment development|production] [--project <id|slug>] [--no-wait]
Expand Down
4 changes: 4 additions & 0 deletions cloudflare-workers/api-edge/src/computer_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ describe("computer API allowlist", () => {
["PUT", "/computer/sessions/cs_01h/secrets/API_KEY"],
["DELETE", "/computer/me/secrets/API_KEY"],
["PUT", "/computer/organization/secrets/API_KEY"],
["GET", "/computer/service-connections"],
["POST", "/computer/service-connections"],
["DELETE", "/computer/service-connections"],
["GET", "/computer/work/repository"],
["PUT", "/computer/work/repository"],
["GET", "/computer/work/repositories"],
Expand Down Expand Up @@ -277,6 +280,7 @@ describe("computer API allowlist", () => {
["GET", "/computer/sessions/a%2Fb"],
["GET", "/computer/sessions/cs_01h/unknown"],
["POST", "/computer/slack/install"],
["PUT", "/computer/service-connections"],
["PUT", "/computer/settings"],
["GET", "/computer/settings/apiAccessDefault"],
// Close and Reopen come with build step 5.
Expand Down
6 changes: 6 additions & 0 deletions cloudflare-workers/api-edge/src/computer_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ const COMPUTER_API_ROUTES: ReadonlyArray<{
methods: new Set(["PUT", "DELETE"]),
pattern: route(`/(?:sessions/${SEG}|me|organization)/secrets/${SEG}`),
},
// Connections granted to the computer (serverless-agents-ws work 080):
// people list, grant and revoke; computer keys only list (isGrantRoute).
{
methods: new Set(["GET", "POST", "DELETE"]),
pattern: route("/service-connections"),
},
// The Work API (work tracking tech design 3.10).
{ methods: new Set(["GET", "PUT"]), pattern: route("/work/repository") },
{ methods: new Set(["GET"]), pattern: route("/work/repositories") },
Expand Down
3 changes: 3 additions & 0 deletions cloudflare-workers/api-edge/src/computer_principal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ describe("computer principal", () => {
["POST", "/connections/channel-identity/link"],
["POST", "/projects/p1/service-connections"],
["DELETE", "/projects/p1/service-connections"],
["POST", "/computer/service-connections"],
["DELETE", "/computer/service-connections"],
["POST", "/model-access/connections"],
["POST", "/model-access/connections/m1/complete"],
["PUT", "/projects/p1/model-access/bindings/openai/production"],
Expand All @@ -58,6 +60,7 @@ describe("computer principal", () => {
["POST", "/connections/linear/fetch"],
["POST", "/connections/google/fetch"],
["GET", "/projects/p1/service-connections"],
["GET", "/computer/service-connections"],
["GET", "/projects/p1/secrets"],
["PUT", "/projects/p1/secrets/API_KEY"],
["POST", "/projects"],
Expand Down
1 change: 1 addition & 0 deletions cloudflare-workers/api-edge/src/managed_agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2753,6 +2753,7 @@ export function isGrantRoute(method: string, suffix: string): boolean {
}
return (
/^\/projects\/[^/]+\/service-connections$/.test(suffix) ||
suffix === "/computer/service-connections" ||
/^\/(?:projects\/[^/]+\/)?model-access(?:\/|$)/.test(suffix) ||
isLinearRouteSuffix(suffix) ||
/^\/channels(?:\/|$)/.test(suffix) ||
Expand Down
4 changes: 2 additions & 2 deletions create-start/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@opencomputer/create-start",
"version": "0.7.23",
"version": "0.7.24",
"description": "Create a hello-world OpenComputer agent application.",
"type": "module",
"bin": {
Expand All @@ -18,7 +18,7 @@
"node": ">=22.0.0"
},
"dependencies": {
"@opencomputer/cli": "0.7.23"
"@opencomputer/cli": "0.7.24"
},
"publishConfig": {
"access": "public"
Expand Down
Loading