An open-source, faster Devin alternative
TypeScript
let useSecret declare a tenant or user scope
Brian Reardon0174800ca872
diff --git a/agent/src/index.ts b/agent/src/index.tsindex 8bb31d3b..3cca5a17 100644--- a/agent/src/index.ts+++ b/agent/src/index.ts@@ -94,8 +94,27 @@ export interface ConnectionReference extends ResourceReference {readonly kind: "connection";}+/**+ * Which store a secret is read from.+ *+ * `project` is one value for the whole deployment. `tenant` is one value per+ * channel installation: an agent answering many workspaces resolves the+ * credential belonging to whichever installation the message arrived through,+ * so one customer's agent cannot reach another customer's account. `user` is+ * narrower still — the credential of the particular person being acted for,+ * so the upstream applies its own permissions to them rather than to the+ * installation as a whole.+ *+ * Nothing falls back. A `tenant` secret never reaches for the project's, and a+ * `user` secret never reaches for the installation's, because a credential+ * substituted when the right one is missing is exactly how one customer ends+ * up acting with another's authority.+ */+export type SecretScope = "project" | "tenant" | "user";+export interface SecretReference extends ResourceReference {readonly kind: "secret";+ readonly scope: SecretScope;}export interface SecretHeaderReference {@@ -545,14 +564,21 @@ function identifier(value: string, kind: string): string {const OPENCOMPUTER_USER_AGENT ="OpenComputer-Agent/1 (+https://opencomputer.dev)";-export function useSecret(name: string): SecretReference {+export function useSecret(+ name: string,+ options: { scope?: SecretScope } = {},+): SecretReference {const id = identifier(name, "useSecret");if (!/^[A-Z][A-Z0-9_]{0,127}$/.test(id)) {throw new Error("Secret names must use uppercase letters, numbers, and underscores",);}- return Object.freeze({ kind: "secret", id });+ const scope = options.scope ?? "project";+ if (scope !== "project" && scope !== "tenant" && scope !== "user") {+ throw new Error('A secret scope must be "project", "tenant" or "user"');+ }+ return Object.freeze({ kind: "secret", id, scope });}export function secretHeader(diff --git a/cli/src/project.ts b/cli/src/project.tsindex 43beb5d0..45b8e479 100644--- a/cli/src/project.ts+++ b/cli/src/project.ts@@ -35,7 +35,18 @@ export interface HttpConnectionManifest {origin: string;headers: Record<string,- string | { kind: "secret"; name: string; prefix?: string; suffix?: string }+ | string+ | {+ kind: "secret";+ name: string;+ /**+ * Omitted means project-wide; "tenant" resolves per installation and+ * "user" per person acted for.+ */+ scope?: "tenant" | "user";+ prefix?: string;+ suffix?: string;+ }>;methods?: string[];pathPrefix?: string;@@ -1504,7 +1515,17 @@ export async function readProjectResources(};}-function secretNameFromExpression(expression: ts.Expression): string {+/**+ * A secret reference, as written in source.+ *+ * The scope is read here rather than resolved later because the manifest is+ * the record of what an agent can reach: a reviewer should be able to see that+ * a credential is per-installation without running anything.+ */+function secretFromExpression(expression: ts.Expression): {+ name: string;+ scope?: "tenant" | "user";+} {if (!ts.isCallExpression(expression) ||!ts.isIdentifier(expression.expression) ||@@ -1512,7 +1533,19 @@ function secretNameFromExpression(expression: ts.Expression): string {) {throw new Error("Connection secret headers must reference useSecret()");}- return literalStringValue(expression.arguments[0], "useSecret name");+ const name = literalStringValue(expression.arguments[0], "useSecret name");+ const options = expression.arguments[1];+ if (!options) return { name };+ if (!ts.isObjectLiteralExpression(options)) {+ throw new Error("useSecret options must be an object literal");+ }+ const scope = objectProperty(options, "scope");+ if (!scope) return { name };+ const value = literalStringValue(scope, "useSecret scope");+ if (value !== "project" && value !== "tenant" && value !== "user") {+ throw new Error(`useSecret scope must be "project", "tenant" or "user"`);+ }+ return value === "project" ? { name } : { name, scope: value };}function connectionHeaderValue(@@ -1533,7 +1566,7 @@ function connectionHeaderValue(if (!secret) throw new Error("bearer() requires useSecret()");return {kind: "secret",- name: secretNameFromExpression(secret),+ ...secretFromExpression(secret),prefix: "Bearer ",};}@@ -1543,9 +1576,10 @@ function connectionHeaderValue(const result: {kind: "secret";name: string;+ scope?: "tenant" | "user";prefix?: string;suffix?: string;- } = { kind: "secret", name: secretNameFromExpression(secret) };+ } = { kind: "secret", ...secretFromExpression(secret) };const options = expression.arguments[1];if (options) {if (!ts.isObjectLiteralExpression(options)) {@@ -1805,10 +1839,12 @@ function id(value, kind) {if (!normalized) throw new Error(kind + " requires a non-empty id");return normalized;}-export const useSecret = (value) => {+export const useSecret = (value, options = {}) => {const name = id(value, "useSecret");+ const scope = options.scope ?? "project";+ if (scope !== "project" && scope !== "tenant" && scope !== "user") throw new Error('A secret scope must be "project", "tenant" or "user"');if (!/^[A-Z][A-Z0-9_]{0,127}$/.test(name)) throw new Error("Invalid secret name " + JSON.stringify(name));- return Object.freeze({ kind: "secret", id: name });+ return Object.freeze({ kind: "secret", id: name, scope });};export const secretHeader = (secret, options = {}) => Object.freeze({ kind: "secret-header", secret, ...options });export const bearer = (secret) => secretHeader(secret, { prefix: "Bearer " });