Skip to content
Merged
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ deno run -A scripts/setup.ts --configure-claude
`.env` is reused.

Flags: `--env-file <path>` (default `.env`), `--scope <name>` (default `default`), `--member <name>`,
`--email <address>` (a human member needs one; defaults to `git config user.email`), `--bot` (a bot member instead of a
`--email <address>` (optional; only links the member to a Supabase login), `--bot` (a bot member instead of a
human), `--new-token` (issue another token even if `.env` has a live one), `--skip-deploy`, `--configure-claude` and
`--name <server-name>` (default `shared-memory`).

Expand Down Expand Up @@ -89,7 +89,8 @@ password (for example `@` as `%40`, `/` as `%2F`, `#` as `%23`), or the URL will
export AGENT_MEMORY_DB_URL='postgresql://postgres.<project-ref>:<password>@<pooler-host>:5432/postgres'

deno run -A scripts/admin.ts create-scope my-team --description "Team memory"
deno run -A scripts/admin.ts create-member alice --human alice@example.com
deno run -A scripts/admin.ts create-member alice # email optional: --email alice@example.com
# links a Supabase login if one exists
deno run -A scripts/admin.ts create-member ci-bot --bot
deno run -A scripts/admin.ts grant alice my-team write
deno run -A scripts/admin.ts grant ci-bot my-team read
Expand Down
20 changes: 10 additions & 10 deletions scripts/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
const USAGE = `Usage: deno run -A scripts/admin.ts <command> ...
create-scope <name> [--description <text>]
list-scopes
create-member <name> --human <email> | --bot
create-member <name> [--email <address>] | --bot (email optional; links a Supabase login)
grant <member> <scope> read|write
revoke <member> <scope>
list-grants
Expand Down Expand Up @@ -50,7 +50,7 @@ const DELETED_COLS: (keyof DeletedRow & string)[] = ['id', 'scope', 'deletedAt',
/** Returns the process exit code. */
async function main(): Promise<number> {
const args = parseArgs(Deno.args, {
string: ['description', 'human', 'expires', 'scope', 'by', 'since'],
string: ['description', 'human', 'email', 'expires', 'scope', 'by', 'since'],
boolean: ['bot', 'yes'],
});
const [cmd, ...p] = args._.map(String);
Expand Down Expand Up @@ -80,16 +80,16 @@ async function main(): Promise<number> {
console.log(table(await listScopes(sql), ['name', 'description']));
break;
case 'create-member': {
need(1, 'create-member <name> --human <email> | --bot');
if (!!args.human === args.bot) throw new Error('Specify exactly one of --human <email> or --bot');
const m = await createMember(
sql,
p[0],
args.bot ? { kind: 'bot' } : { kind: 'human', email: args.human },
);
need(1, 'create-member <name> [--email <address>] | --bot');
// --human <email> is the older spelling of --email and still works.
const email = args.email || args.human || undefined;
if (args.bot && email) throw new Error('A bot member has no email: give --bot or --email, not both');
const m = await createMember(sql, p[0], args.bot ? { kind: 'bot' } : { kind: 'human', email });
console.log(
`Created ${m.kind} member '${m.name}'` +
(m.kind === 'human' ? (m.authUserId ? ' (linked to auth user)' : ' (no matching auth user)') : ''),
(m.kind === 'human'
? (m.authUserId ? ' (linked to auth user)' : email ? ' (no matching auth user)' : ' (no email)')
: ''),
);
break;
}
Expand Down
9 changes: 5 additions & 4 deletions scripts/lib/admin-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,19 @@ export async function createMember(
name: string,
opts: { kind: 'human' | 'bot'; email?: string },
): Promise<Member> {
if (opts.kind === 'human' && !opts.email) throw new Error('A human member requires an email (--human <email>)');
// Email is optional: it is only used to link the member to a Supabase login (auth.users).
const email = opts.email?.trim() || null;
const [dup] = await sql`select 1 from agent_memory.members where display_name = ${name}`;
if (dup) throw new Error(`Member '${name}' already exists`);
let authUserId: string | null = null;
if (opts.email) {
if (email) {
// Read-only lookup; nothing is ever written to auth.
const [u] = await sql`select id from auth.users where lower(email) = lower(${opts.email})`;
const [u] = await sql`select id from auth.users where lower(email) = lower(${email})`;
authUserId = u?.id ?? null;
}
const [r] = await sql`
insert into agent_memory.members (display_name, kind, email, auth_user_id)
values (${name}, ${opts.kind}, ${opts.email ?? null}, ${authUserId})
values (${name}, ${opts.kind}, ${email}, ${authUserId})
returning id, display_name as name, kind, auth_user_id as "authUserId"`;
return r as Member;
}
Expand Down
13 changes: 2 additions & 11 deletions scripts/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,8 @@ export async function setup(o: SetupOptions): Promise<number> {
log(`[5/7] Scope '${scope}' ${step}`);
const [existing] = await sql`select id from agent_memory.members where display_name = ${member}`;
if (!existing) {
let email = o.email;
if (kind === 'human' && !email) {
const g = await run('git', ['config', 'user.email']).catch(() => ({ code: 1, stdout: '', stderr: '' }));
email = g.code === 0 ? g.stdout.trim() || undefined : undefined;
}
if (kind === 'human' && !email) {
throw new Error(
`A human member needs an email: pass --email <address>, or --bot. Nothing was created for '${member}'.`,
);
}
await createMember(sql, member, { kind, email });
// Email is optional and only links a Supabase login, so it is stored only when given.
await createMember(sql, member, { kind, email: kind === 'human' ? o.email : undefined });
}
await grant(sql, member, scope, 'write');
log(`[6/7] Member '${member}' (${existing ? 'existing' : kind}) can write to '${scope}'`);
Expand Down
6 changes: 5 additions & 1 deletion tests/integration/admin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ Deno.test('create-member --human links auth.users by email, case-insensitively',
assertEquals(linked.authUserId, uid);
const unlinked = await createMember(sql, `h2-${s}`, { kind: 'human', email: `none-${s}@example.test` });
assertEquals(unlinked.authUserId, null);
await assertRejects(() => createMember(sql, `h3-${s}`, { kind: 'human' }), Error, 'requires an email');
const noEmail = await createMember(sql, `h3-${s}`, { kind: 'human' });
assertEquals(noEmail.kind, 'human');
assertEquals(noEmail.authUserId, null);
const [row] = await sql`select email from agent_memory.members where id = ${noEmail.id}`;
assertEquals(row.email, null);
} finally {
await sql`delete from auth.users where id = ${uid}`;
}
Expand Down
Loading