Skip to content

Commit

Permalink
fix(docs): added snipped about conditional user access
Browse files Browse the repository at this point in the history
  • Loading branch information
snorrees authored May 7, 2024
1 parent dea2a5d commit 62b1404
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [Add the plugin](#add-the-plugin)
- [Enabling the AI Assist API](#enabling-the-ai-assist-api)
- [Permissions](#permissions)
- [Conditional user access](#conditional-user-access)
- [Schema configuration](#schema-configuration)
- [Disable AI Assist for a schema type](#disable-ai-assist-for-a-schema-type)
- [Disable for a field](#disable-for-a-field)
Expand Down Expand Up @@ -113,6 +114,45 @@ To edit instructions, users will need read and write access to documents of `_ty
Note that instructions run using the permissions of the user invoking it, so only fields that the user
themselves can edit can be changed by the instruction instance.

### Conditional user access
To limit which users can see the AI Assist actions in the Studio, use a custom-plugin after `assist()`
that filters out the inspector and actions, based on user properties:

```ts
import {CurrentUser, defineConfig} from 'sanity'
import {assist} from '@sanity/assist'

export default defineConfig({
// ...

plugins: [
// ...
assist(),
{
name: 'disable-ai-assist',
document: {
inspectors: (prev, {currentUser}) =>
isAiAssistAllowed(currentUser)
? prev
: prev.filter((inspector) => inspector.name !== 'ai-assistance'),

unstable_fieldActions: (prev, {currentUser}) =>
isAiAssistAllowed(currentUser)
? prev
: prev.filter((fieldActions) => fieldActions.name !== 'sanity-assist-actions'),
},
},
],

})

const ALLOWED_ROLES = ['administrator']

function isAiAssistAllowed(user?: CurrentUser | null) {
return user && user.roles.some((role) => ALLOWED_ROLES.includes(role.name))
}
```

## Schema configuration

By default, most string, object, and array field types (including Portable Text!) have AI writing assistance enabled. Your assistant can write to all compatible fields that it detects.
Expand Down

0 comments on commit 62b1404

Please sign in to comment.