Something went wrong. Try again.
ATProto-based AI Assistant Chat interface.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import { ScopePermissions, SpacePermission } from '@atproto/oauth-scopes'import { nsid } from '@scn-chat/lexicons'
export type ScopeMode = 'permission-set' | 'raw'
const recordActions = ['read', 'create', 'update', 'delete'].map((action) => `action=${action}`)const manageOps = ['create', 'update', 'delete'].map((op) => `manage=${op}`)
function spaceScope(type: string, params: string[]): string { return `space:${type}?${params.join('&')}`}
/** The OAuth scope the app requests, built from the lexicon module. */export function buildScope(mode: ScopeMode): string { if (mode === 'permission-set') return `atproto include:${nsid.permissions}` return [ 'atproto', spaceScope(nsid.conversation, [ `collection=${nsid.info}`, `collection=${nsid.message}`, ...recordActions, ...manageOps, ]), spaceScope(nsid.conversation, ['authority=*', 'action=read']), spaceScope(nsid.settings, [ 'skey=self', `collection=${nsid.preferences}`, `collection=${nsid.conversationRef}`, ...recordActions, ...manageOps, ]), 'blob:*/*', ].join(' ')}
/** Does a granted scope let the user create their own conversation spaces? */export function scopeAllowsSpaces(scope: string, did: string): boolean { const resolved = scope .split(' ') .filter(Boolean) .map((part) => { if (!part.startsWith('space:')) return part const permission = SpacePermission.fromString(part) if (!permission) throw new Error(`The PDS granted a space permission this app cannot read: ${part}`) return permission.withResolvedAuthority(did as `did:${string}:${string}`).toString() }) return new ScopePermissions(resolved).allowsSpace({ type: nsid.conversation, authority: did, skey: 'self', manage: 'create', })}