@sanity/client 7.1.0-views.0 → 7.1.0-views.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +668 -40
  2. package/dist/_chunks-cjs/config.cjs +14 -0
  3. package/dist/_chunks-cjs/config.cjs.map +1 -1
  4. package/dist/_chunks-cjs/dataMethods.cjs +197 -32
  5. package/dist/_chunks-cjs/dataMethods.cjs.map +1 -1
  6. package/dist/_chunks-cjs/isRecord.cjs +6 -0
  7. package/dist/_chunks-cjs/isRecord.cjs.map +1 -0
  8. package/dist/_chunks-cjs/resolveEditInfo.cjs +3 -5
  9. package/dist/_chunks-cjs/resolveEditInfo.cjs.map +1 -1
  10. package/dist/_chunks-cjs/stegaClean.cjs +4 -0
  11. package/dist/_chunks-cjs/stegaClean.cjs.map +1 -1
  12. package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs +2 -5
  13. package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs.map +1 -1
  14. package/dist/_chunks-es/config.js +15 -1
  15. package/dist/_chunks-es/config.js.map +1 -1
  16. package/dist/_chunks-es/dataMethods.js +200 -33
  17. package/dist/_chunks-es/dataMethods.js.map +1 -1
  18. package/dist/_chunks-es/isRecord.js +7 -0
  19. package/dist/_chunks-es/isRecord.js.map +1 -0
  20. package/dist/_chunks-es/resolveEditInfo.js +1 -3
  21. package/dist/_chunks-es/resolveEditInfo.js.map +1 -1
  22. package/dist/_chunks-es/stegaClean.js +4 -0
  23. package/dist/_chunks-es/stegaClean.js.map +1 -1
  24. package/dist/_chunks-es/stegaEncodeSourceMap.js +1 -4
  25. package/dist/_chunks-es/stegaEncodeSourceMap.js.map +1 -1
  26. package/dist/index.browser.cjs +1019 -59
  27. package/dist/index.browser.cjs.map +1 -1
  28. package/dist/index.browser.d.cts +1948 -149
  29. package/dist/index.browser.d.ts +1948 -149
  30. package/dist/index.browser.js +1021 -60
  31. package/dist/index.browser.js.map +1 -1
  32. package/dist/index.cjs +825 -29
  33. package/dist/index.cjs.map +1 -1
  34. package/dist/index.d.cts +1948 -149
  35. package/dist/index.d.ts +1948 -149
  36. package/dist/index.js +826 -31
  37. package/dist/index.js.map +1 -1
  38. package/dist/stega.browser.d.cts +1948 -149
  39. package/dist/stega.browser.d.ts +1948 -149
  40. package/dist/stega.d.cts +1948 -149
  41. package/dist/stega.d.ts +1948 -149
  42. package/dist/views.cjs +13 -5
  43. package/dist/views.cjs.map +1 -1
  44. package/dist/views.d.cts +51 -36
  45. package/dist/views.d.ts +51 -36
  46. package/dist/views.js +14 -5
  47. package/dist/views.js.map +1 -1
  48. package/package.json +2 -1
  49. package/src/SanityClient.ts +652 -12
  50. package/src/agent/actions/AgentActionsClient.ts +29 -2
  51. package/src/agent/actions/commonTypes.ts +57 -17
  52. package/src/agent/actions/generate.ts +36 -2
  53. package/src/agent/actions/patch.ts +136 -0
  54. package/src/agent/actions/prompt.ts +145 -0
  55. package/src/agent/actions/transform.ts +105 -7
  56. package/src/agent/actions/translate.ts +5 -2
  57. package/src/config.ts +3 -1
  58. package/src/csm/walkMap.ts +1 -1
  59. package/src/data/dataMethods.ts +170 -12
  60. package/src/data/encodeQueryString.ts +1 -1
  61. package/src/data/eventsource.ts +16 -7
  62. package/src/data/listen.ts +10 -4
  63. package/src/data/live.ts +13 -5
  64. package/src/datasets/DatasetsClient.ts +4 -1
  65. package/src/defineCreateClient.ts +7 -1
  66. package/src/http/errors.ts +92 -27
  67. package/src/http/request.ts +3 -3
  68. package/src/http/requestOptions.ts +4 -0
  69. package/src/projects/ProjectsClient.ts +6 -2
  70. package/src/releases/ReleasesClient.ts +693 -0
  71. package/src/releases/createRelease.ts +53 -0
  72. package/src/types.ts +291 -10
  73. package/src/users/UsersClient.ts +7 -3
  74. package/src/util/codeFrame.ts +174 -0
  75. package/src/util/createVersionId.ts +79 -0
  76. package/src/{csm → util}/isRecord.ts +1 -1
  77. package/src/validators.ts +23 -1
  78. package/src/views/index.ts +51 -15
  79. package/umd/sanityClient.js +1067 -61
  80. package/umd/sanityClient.min.js +2 -2
package/README.md CHANGED
@@ -27,6 +27,10 @@ export const client = createClient({
27
27
  projectId: 'your-project-id',
28
28
  dataset: 'your-dataset-name',
29
29
  useCdn: true, // set to `false` to bypass the edge cache
30
+ // Set default headers to be included with all requests
31
+ headers: {
32
+ 'X-Custom-Header': 'custom-value'
33
+ },
30
34
  apiVersion: '2025-02-06', // use current date (YYYY-MM-DD) to target the latest API version. Note: this should always be hard coded. Setting API version based on a dynamic value (e.g. new Date()) may break your application at a random point in the future.
31
35
  // token: process.env.SANITY_SECRET_TOKEN // Needed for certain operations like updating content, accessing drafts or using draft perspectives
32
36
  })
@@ -91,6 +95,7 @@ export async function updateDocumentTitle(_id, title) {
91
95
  - [Delete documents](#delete-documents)
92
96
  - [Multiple mutations in a transaction](#multiple-mutations-in-a-transaction)
93
97
  - [Clientless patches \& transactions](#clientless-patches--transactions)
98
+ - [Release and version operations](#release-and-version-operations)
94
99
  - [Uploading assets](#uploading-assets)
95
100
  - [Examples: Uploading assets from Node.js](#examples-uploading-assets-from-nodejs)
96
101
  - [Examples: Uploading assets from the Browser](#examples-uploading-assets-from-the-browser)
@@ -106,11 +111,37 @@ export async function updateDocumentTitle(_id, title) {
106
111
  - [Action options](#action-options)
107
112
  - [Create Action](#create-action)
108
113
  - [Delete Action](#delete-action)
109
- - [Discard Action](#discard-action)
110
114
  - [Edit Action](#edit-action)
111
115
  - [Publish Action](#publish-action)
112
- - [ReplaceDraft Action](#replacedraft-action)
113
116
  - [Unpublish Action](#unpublish-action)
117
+ - [Agent Actions API](#agent-actions-api)
118
+ - [Overview](#overview)
119
+ - [Generating Content](#generating-content)
120
+ - [Generating images](#generating-images)
121
+ - [Example: Using GROQ in instructionParams](#example-using-groq-in-instructionparams)
122
+ - [Example: Using the async flag](#example-using-the-async-flag)
123
+ - [Transforming Documents](#transforming-documents)
124
+ - [Transforming images](#transforming-images)
125
+ - [Image descriptions](#image-descriptions)
126
+ - [Example: Field-based transformation](#example-field-based-transformation)
127
+ - [Translating Documents](#translating-documents)
128
+ - [Example: Storing language in a field](#example-storing-language-in-a-field)
129
+ - [Prompt the LLM](#prompt-the-llm)
130
+ - [Patch with a schema-aware API](#patch-with-a-schema-aware-api)
131
+ - [Version actions](#version-actions)
132
+ - [Create Version Action](#create-version)
133
+ - [Discard Version Action](#discard-version)
134
+ - [Replace Version Action](#replace-version)
135
+ - [Unpublish Version Action](#unpublish-action)
136
+ - [Release Actions](#release-actions)
137
+ - [Create Release Action](#create-release)
138
+ - [Edit Release Action](#edit-release)
139
+ - [Published Release Action](#publish-release)
140
+ - [Schedule Release Action](#schedule-release)
141
+ - [Unschedule Release Action](#unarchive-release)
142
+ - [Archive Release Action](#archive-release)
143
+ - [Unarchive Release Action](#unarchive-release)
144
+ - [Delete Release Action](#delete-release)
114
145
  - [License](#license)
115
146
  - [From `v5`](#from-v5)
116
147
  - [The default `useCdn` is changed to `true`](#the-default-usecdn-is-changed-to-true)
@@ -1284,6 +1315,113 @@ client.mutate(transaction)
1284
1315
 
1285
1316
  An important note on this approach is that you cannot call `commit()` on transactions or patches instantiated this way, instead you have to pass them to `client.mutate()`
1286
1317
 
1318
+ ### Release and version operations
1319
+
1320
+ Release and version actions can be taken directly using the client's [actions API](#version-actions). Additionally, helper methods are provided which abstract some esoteric nomenclature with the release and version processing.
1321
+
1322
+ 0. Setup the client
1323
+
1324
+ ```js
1325
+ import {createClient} from '@sanity/client'
1326
+
1327
+ const client = createClient({
1328
+ projectId: 'your-project-id',
1329
+ dataset: 'bikeshop',
1330
+ })
1331
+ ```
1332
+
1333
+ 1. Create a new release
1334
+
1335
+ ```js
1336
+ const {releaseId} = await client.release.create({
1337
+ metadata: {
1338
+ title: 'New bike drop'
1339
+ releaseType: 'scheduled'
1340
+ }
1341
+ })
1342
+ ```
1343
+
1344
+ 2. Create a new document into the release
1345
+
1346
+ ```js
1347
+ client.createVersion({
1348
+ document: {
1349
+ _type: 'bike',
1350
+ name: 'Upgraded black bike',
1351
+ },
1352
+ releaseId,
1353
+ publishedId: 'bike-123',
1354
+ })
1355
+ ```
1356
+
1357
+ 3. Mark a document to be unpublished when the release is run
1358
+
1359
+ ```js
1360
+ client.unpublishVersion({
1361
+ publishedId: 'old-red-bike',
1362
+ releaseId,
1363
+ })
1364
+ ```
1365
+
1366
+ 4. List the release and all the documents within the release
1367
+
1368
+ ```js
1369
+ const newBikesRelease = await client.releases.get({releaseId})
1370
+ /**
1371
+ * {
1372
+ * _type: 'system.release',
1373
+ * _id: '_.releases.releaseId',
1374
+ * name: 'releaseId',
1375
+ * state: 'active',
1376
+ * metadata: {
1377
+ * name: 'New bike drop',
1378
+ * releaseType: 'scheduled'
1379
+ * }
1380
+ * }
1381
+ */
1382
+
1383
+ const releaseDocuments = await client.releases.getDocuments({
1384
+ releaseId,
1385
+ })
1386
+
1387
+ /**
1388
+ * Returns a list of documents eg.
1389
+ *
1390
+ * [{
1391
+ * _type: 'bike',
1392
+ * _id: 'versions.releaseId.bike-123',
1393
+ * name: 'Upgraded black bike',
1394
+ * ...
1395
+ * },
1396
+ * {
1397
+ * _type: 'bike',
1398
+ * _id: 'versions.releaseId.old-red-bike',
1399
+ * _system: {
1400
+ * delete: true
1401
+ * }
1402
+ * }]
1403
+ */
1404
+ ```
1405
+
1406
+ 5. Schedule the release (to run in 1 hours time)
1407
+
1408
+ ```js
1409
+ client.release.schedule({
1410
+ releaseId,
1411
+ publishAt: new Date(Date.now() + 3600000).toISOString(),
1412
+ })
1413
+ ```
1414
+
1415
+ 6. After the release has run, check and delete the release
1416
+
1417
+ ```js
1418
+ const runRelease = await client.releases.get({releaseId})
1419
+
1420
+ if (runRelease.state === 'published' && !runRelease.error) {
1421
+ client.releases.delete({releaseId})
1422
+ }
1423
+ ```
1424
+
1287
1425
  ### Actions
1288
1426
 
1289
1427
  The Actions API provides a new interface for creating, updating and publishing documents. It is a wrapper around the [Actions API](https://www.sanity.io/docs/http-actions).
@@ -1343,27 +1481,6 @@ client
1343
1481
  })
1344
1482
  ```
1345
1483
 
1346
- #### Discard Action
1347
-
1348
- A draft document can be deleted by specifying a discard action type:
1349
-
1350
- ```js
1351
- client
1352
- .action(
1353
- {
1354
- actionType: 'sanity.action.document.discard',
1355
- draftId: 'draft.bike-123',
1356
- },
1357
- actionOptions,
1358
- )
1359
- .then(() => {
1360
- console.log('Bike draft deleted')
1361
- })
1362
- .catch((err) => {
1363
- console.error('Discard failed: ', err.message)
1364
- })
1365
- ```
1366
-
1367
1484
  #### Edit Action
1368
1485
 
1369
1486
  A patch can be applied to an existing document draft or create a new one by specifying an edit action type:
@@ -1410,47 +1527,268 @@ client
1410
1527
  })
1411
1528
  ```
1412
1529
 
1413
- #### ReplaceDraft Action
1530
+ #### Unpublish Action
1414
1531
 
1415
- An existing document draft can be deleted and replaced by a new one by specifying a replaceDraft action type:
1532
+ A published document can be retracted by specifying an unpublish action type:
1416
1533
 
1417
1534
  ```js
1418
1535
  client
1419
1536
  .action(
1420
1537
  {
1421
- actionType: 'sanity.action.document.replaceDraft',
1538
+ actionType: 'sanity.action.document.unpublish',
1539
+ draftId: 'draft.bike-123',
1422
1540
  publishedId: 'bike-123',
1423
- attributes: {name: 'Sanity Tandem Extraordinaire', _type: 'bike', seats: 1},
1424
1541
  },
1425
1542
  actionOptions,
1426
1543
  )
1427
1544
  .then(() => {
1428
- console.log('Bike draft replaced')
1545
+ console.log('Bike draft unpublished')
1429
1546
  })
1430
1547
  .catch((err) => {
1431
- console.error('Replace draft failed: ', err.message)
1548
+ console.error('Unpublish draft failed: ', err.message)
1432
1549
  })
1433
1550
  ```
1434
1551
 
1435
- #### Unpublish Action
1552
+ ## Version actions
1436
1553
 
1437
- A published document can be retracted by specifying an unpublish action type:
1554
+ ### Create version
1555
+
1556
+ Create a draft or release version of a published document.
1557
+
1558
+ ```js
1559
+ client.action(
1560
+ {
1561
+ actionType: 'sanity.action.document.version.create',
1562
+ publishedId: 'bike-123',
1563
+ document: {
1564
+ _id: 'versions.new-bike-release.bike-123'
1565
+ _type: 'bike'
1566
+ }
1567
+ }
1568
+ ).then(() => {
1569
+ console.log('Copy of published `bike-123` created in release `new-bike-release`')
1570
+ }).catch((err) => {
1571
+ console.error('Create version failed: ', err.message)
1572
+ })
1573
+ ```
1574
+
1575
+ > [!NOTE]
1576
+ > Replacing `versions.<releaseId>` with `drafts` will create a new draft version from the published document.
1577
+
1578
+ ### Discard version
1579
+
1580
+ Discard a draft or release version.
1438
1581
 
1439
1582
  ```js
1440
1583
  client
1441
- .action(
1442
- {
1443
- actionType: 'sanity.action.document.unpublish',
1444
- draftId: 'draft.bike-123',
1445
- publishedId: 'bike-123',
1584
+ .action({
1585
+ actionType: 'sanity.action.document.version.discard',
1586
+ versionId: 'versions.new-bike-release.bike-123',
1587
+ })
1588
+ .then(() => {
1589
+ console.log('Discarded the version of `bike-123` within the `new-bike-release` release')
1590
+ })
1591
+ .catch((err) => {
1592
+ console.error('Discard version failed: ', err.message)
1593
+ })
1594
+ ```
1595
+
1596
+ ### Replace version
1597
+
1598
+ Replaces the contents of an existing draft or release version document.
1599
+
1600
+ ```js
1601
+ client
1602
+ .action({
1603
+ actionType: 'sanity.action.document.version.replace',
1604
+ document: {
1605
+ _id: 'versions.new-bike-release.bike-123',
1606
+ color: 'red',
1607
+ _type: 'bike',
1446
1608
  },
1447
- actionOptions,
1448
- )
1609
+ })
1449
1610
  .then(() => {
1450
- console.log('Bike draft unpublished')
1611
+ console.log('Replaced the existing `bike-123` document within the `new-bike-release` release')
1451
1612
  })
1452
1613
  .catch((err) => {
1453
- console.error('Unpublish draft failed: ', err.message)
1614
+ console.error('Replace version failed: ', err.message)
1615
+ })
1616
+ ```
1617
+
1618
+ ### Unpublish version
1619
+
1620
+ Marks a document to be unpublished when the release it is part of is run.
1621
+
1622
+ ```js
1623
+ client
1624
+ .action({
1625
+ actionType: 'sanity.action.document.version.unpublish',
1626
+ publishedId: 'bike-123',
1627
+ versionId: 'versions.new-bike-release.bike-123',
1628
+ })
1629
+ .then(() => {
1630
+ console.log('`bike-123` will be unpublished when `new-bike-release` release is run')
1631
+ })
1632
+ .catch((err) => {
1633
+ console.error('Unpublish version failed: ', err.message)
1634
+ })
1635
+ ```
1636
+
1637
+ ## Release Actions
1638
+
1639
+ ### Create release
1640
+
1641
+ Create a new release.
1642
+
1643
+ ```js
1644
+ client
1645
+ .action({
1646
+ actionType: 'sanity.action.release.create',
1647
+ releaseId: 'new-bikes-release',
1648
+ metadata: {
1649
+ title: 'New bikes',
1650
+ releaseType: 'undecided',
1651
+ },
1652
+ })
1653
+ .then(() => {
1654
+ console.log('`new-bikes-release` created')
1655
+ })
1656
+ .catch((err) => {
1657
+ console.error('Create release failed: ', err.message)
1658
+ })
1659
+ ```
1660
+
1661
+ ### Edit release
1662
+
1663
+ Edit the metadata on an existing release.
1664
+
1665
+ ```js
1666
+ client
1667
+ .action({
1668
+ actionType: 'sanity.action.release.edit',
1669
+ releaseId: 'new-bikes-release',
1670
+ patch: {
1671
+ set: {
1672
+ metadata: {
1673
+ releaseType: 'asap',
1674
+ },
1675
+ },
1676
+ },
1677
+ })
1678
+ .then(() => {
1679
+ console.log('`new-bikes-release` changed to `asap` release type')
1680
+ })
1681
+ .catch((err) => {
1682
+ console.error('Edit release failed: ', err.message)
1683
+ })
1684
+ ```
1685
+
1686
+ ### Publish release
1687
+
1688
+ Publish all document versions within a release.
1689
+
1690
+ ```js
1691
+ client
1692
+ .action({
1693
+ actionType: 'sanity.action.release.publish',
1694
+ releaseId: 'new-bikes-release',
1695
+ })
1696
+ .then(() => {
1697
+ console.log('`new-bikes-release` published')
1698
+ })
1699
+ .catch((err) => {
1700
+ console.error('Publish release failed: ', err.message)
1701
+ })
1702
+ ```
1703
+
1704
+ ### Schedule release
1705
+
1706
+ Schedule a release to be run now or in the future
1707
+
1708
+ ```js
1709
+ client
1710
+ .action({
1711
+ actionType: 'sanity.action.release.schedule',
1712
+ releaseId: 'new-bikes-release',
1713
+ publishAt: '2025-01-01T00:00:00.000Z',
1714
+ })
1715
+ .then(() => {
1716
+ console.log('`new-bikes-release` scheduled')
1717
+ })
1718
+ .catch((err) => {
1719
+ console.error('Schedule release failed: ', err.message)
1720
+ })
1721
+ ```
1722
+
1723
+ ### Unschedule release
1724
+
1725
+ Unschedule a currently scheduled release, to stop the release being run.
1726
+
1727
+ ```js
1728
+ client
1729
+ .action({
1730
+ actionType: 'sanity.action.release.unschedule',
1731
+ releaseId: 'new-bikes-release',
1732
+ })
1733
+ .then(() => {
1734
+ console.log('`new-bikes-release` unscheduled')
1735
+ })
1736
+ .catch((err) => {
1737
+ console.error('Unschedule release failed: ', err.message)
1738
+ })
1739
+ ```
1740
+
1741
+ ### Archive release
1742
+
1743
+ Mark an active (not published) release as archived.
1744
+
1745
+ ```js
1746
+ client
1747
+ .action({
1748
+ actionType: 'sanity.action.release.archive',
1749
+ releaseId: 'new-bikes-release',
1750
+ })
1751
+ .then(() => {
1752
+ console.log('`new-bikes-release` archived')
1753
+ })
1754
+ .catch((err) => {
1755
+ console.error('Archive release failed: ', err.message)
1756
+ })
1757
+ ```
1758
+
1759
+ ### Unarchive release
1760
+
1761
+ Once a release has been archived, the archive process may be undone by unarchiving the release.
1762
+
1763
+ ```js
1764
+ client
1765
+ .action({
1766
+ actionType: 'sanity.action.release.unarchive',
1767
+ releaseId: 'new-bikes-release',
1768
+ })
1769
+ .then(() => {
1770
+ console.log('`new-bikes-release` unarchived')
1771
+ })
1772
+ .catch((err) => {
1773
+ console.error('Unarchive release failed: ', err.message)
1774
+ })
1775
+ ```
1776
+
1777
+ ### Delete release
1778
+
1779
+ An archived release can be deleted, which will remove the system release document permanently from the dataset.
1780
+
1781
+ ```js
1782
+ client
1783
+ .action({
1784
+ actionType: 'sanity.action.release.delete',
1785
+ releaseId: 'new-bikes-release',
1786
+ })
1787
+ .then(() => {
1788
+ console.log('`new-bikes-release` deleted')
1789
+ })
1790
+ .catch((err) => {
1791
+ console.error('Delete release failed: ', err.message)
1454
1792
  })
1455
1793
  ```
1456
1794
 
@@ -1632,6 +1970,296 @@ client.config({dataset: 'newDataset'})
1632
1970
 
1633
1971
  Set client configuration. Required options are `projectId` and `dataset`.
1634
1972
 
1973
+ ### Agent Actions API
1974
+
1975
+ The Agent Actions API provides programmatic access to AI-powered content generation, transformation, and translation for your Sanity documents. These APIs are available on the `client.agent.action` namespace.
1976
+
1977
+ > **Note:** These APIs are currently in beta and may change in future releases.
1978
+
1979
+ #### Overview
1980
+
1981
+ Agent Actions allow you to:
1982
+
1983
+ - **Generate** new content for a document or specific fields using LLM instructions.
1984
+ - **Transform** a document based on instructions, optionally copying from a source document.
1985
+ - **Translate** a document or fields from one language to another, with support for style guides and protected phrases.
1986
+ - **Prompt** the LLM using the same instruction template format as the other actions, but returns text or json instead of acting on a document.
1987
+ - **Patch** documents using a schema-aware API; validates that provided paths and values are schema compliant and handles `setIfMissing` semantics for deep value operations
1988
+
1989
+ All methods are available in both Promise and Observable forms:
1990
+
1991
+ - `client.agent.action.generate`, `client.agent.action.transform`, `client.agent.action.translate`, `client.agent.action.prompt`, `client.agent.action.patch` (Promise-based)
1992
+ - `client.observable.agent.action.generate`, etc. (Observable-based, for streaming or RxJS use)
1993
+
1994
+ ---
1995
+
1996
+ #### Generating Content
1997
+
1998
+ ```ts
1999
+ const result = await client.agent.action.generate({
2000
+ schemaId: 'your-schema-id',
2001
+ documentId: 'your-document-id',
2002
+ instruction: 'Write a summary for the following topic: $topic',
2003
+ instructionParams: {
2004
+ topic: 'Grapefruit',
2005
+ },
2006
+ target: {path: ['body']},
2007
+ })
2008
+ ```
2009
+
2010
+ - **schemaId**: The schema identifier for the document type.
2011
+ - **documentId**: The ID of the document to generate content for.
2012
+ - **instruction**: A string template describing what to generate. Use `$variable` for dynamic values.
2013
+ - **instructionParams**: Values for variables in the instruction. Supports constants, fields, documents, or GROQ queries.
2014
+ - **target**: (Optional) Specifies which fields or paths to generate content for.
2015
+ - **temperature**: (Optional) Controls variance, 0-1 – defaults to 0.3
2016
+ - **async**: (Optional) when true, the request will respond with the document id; the LLM request and mutations will continue on the server.
2017
+ - **noWrite**: (Optional) when true, the document will not be changed. The response will contain the document value with the changes.
2018
+ - **conditionalPaths**: (Optional) control how conditionally readOnly and hidden fields and types will be treated
2019
+
2020
+ ##### Generating images
2021
+
2022
+ Generate will generate images the same was as AI Assist, for images that have been configured using
2023
+ [AI Assist schema options](https://github.com/sanity-io/assist/tree/main/plugin#image-generation).
2024
+
2025
+ To generate images _without_ changing the schema, directly target an image asset path.
2026
+
2027
+ For example, all the following will generate an image into the provided asset:
2028
+
2029
+ - `target: {path: ['image', 'asset'] }`
2030
+ - `target: {path: 'image', include: ['asset'] }`
2031
+
2032
+ Image generation can be combined with regular content targets:
2033
+
2034
+ - `target: [{path: ['image', 'asset'] }, {include: ['title', 'description']}]`
2035
+
2036
+ Since Generate happens in a single LLM pass, the image will be contextually related to other generated content.
2037
+
2038
+ ##### Example: Using GROQ in instructionParams
2039
+
2040
+ ```ts
2041
+ await client.agent.action.generate({
2042
+ schemaId,
2043
+ documentId,
2044
+ instruction: 'Generate a title based on these: $list',
2045
+ instructionParams: {
2046
+ list: {
2047
+ type: 'groq',
2048
+ query: '*[_type==$type].title',
2049
+ params: {type: 'article'},
2050
+ },
2051
+ },
2052
+ target: {path: ['body']},
2053
+ })
2054
+ ```
2055
+
2056
+ #### Example: Using the async flag
2057
+
2058
+ The `async` parameter allows you to fire and forget and will not wait for a response from the LLMj, this works also in the Transform and Translate APIs.
2059
+
2060
+ ```ts
2061
+ const result = await client.agent.action.generate({
2062
+ schemaId: 'article',
2063
+ documentId: 'article-123',
2064
+ instruction: 'Write a comprehensive article about $topic',
2065
+ instructionParams: {
2066
+ topic: 'Climate Change',
2067
+ },
2068
+ target: {path: ['body']},
2069
+ async: true, // Enable async mode for long-running tasks or where you don't want to wait for the result
2070
+ })
2071
+
2072
+ // result will return back the document id
2073
+ console.log('Generation task started:', result._id)
2074
+ ```
2075
+
2076
+ #### Transforming Documents
2077
+
2078
+ ```ts
2079
+ const result = await client.agent.action.transform({
2080
+ schemaId: 'your-schema-id',
2081
+ documentId: 'source-document-id',
2082
+ instruction: 'Transform the content to a more formal tone.',
2083
+ targetDocument: {operation: 'edit', _id: 'target-document-id'},
2084
+ target: {path: ['body']},
2085
+ })
2086
+ ```
2087
+
2088
+ - **schemaId**: The schema identifier for the document type.
2089
+ - **documentId**: The source document ID.
2090
+ - **instruction**: A string template describing the transformation.
2091
+ - **targetDocument**: (Optional) Specify a different document to write the result to, or create a new one.
2092
+ - **target**: (Optional) Specifies which fields or paths to transform.
2093
+ - **temperature**: (Optional) Controls variance, 0-1 – defaults to 0
2094
+ - **async**: (Optional) when true, the request will respond with the document id; the LLM request and mutations will continue on the server.
2095
+ - **noWrite**: (Optional) when true, the document will not be changed. The response will contain the document value with the changes.
2096
+ - **conditionalPaths**: (Optional) control how conditionally readOnly and hidden fields and types will be treated
2097
+
2098
+ ##### Transforming images
2099
+
2100
+ To transform an existing image, directly target an image asset path.
2101
+
2102
+ For example, all the following will transform the image into the provided asset:
2103
+
2104
+ - `target: {path: ['image', 'asset'] }`
2105
+ - `target: {path: 'image', include: ['asset'] }`
2106
+
2107
+ Image transform can be combined with regular content targets:
2108
+
2109
+ - `target: [{path: ['image', 'asset'] }, {include: ['title', 'description']}]`
2110
+
2111
+ Image transform can have per-path instructions, just like any other target paths:
2112
+
2113
+ - `target: [{path: ['image', 'asset'], instruction: 'Make the sky blue' }`
2114
+
2115
+ ##### Image descriptions
2116
+
2117
+ ## Image description
2118
+
2119
+ Images can be transformed to a textual description by targeting a `string`, `text` or Portable Text field (`array` with `block`)
2120
+ with `operation: {type: 'image-description'}`.
2121
+
2122
+ Custom instructions for image description targets will be used to generate the description.
2123
+
2124
+ ###### Targeting image fields
2125
+ If a target is a descendant field of an image object, no `sourcePath` is required in the operation:
2126
+
2127
+ For example:
2128
+ - `target: {path: ['image', 'description'], operation: {type: 'image-description'} }`
2129
+ - `target: {path: ['array', {_key: 'abc'}, 'alt'], operation: {type: 'image-description'} } //assuming the item in the array on the key-ed path is an image`
2130
+ - `target: {path: ['image'], include: ['portableTextField'], operation: {type: 'image-description'}, instruction: 'Use formatting and headings to describe the image in great detail' }`
2131
+
2132
+ ###### Targeting non-image fields
2133
+ If the target image description lives outside an image object, use the `sourcePath` option to specify the path to the image field.
2134
+ `sourcePath` must be an image or image asset field.
2135
+
2136
+ For example:
2137
+ - `target: {path: ['description'], operation: {type: 'image-description', sourcePath: ['image', 'asset'] }`
2138
+ - `target: {path: ['wrapper', 'title'], operation: {type: 'image-description', sourcePath: ['array', {_key: 'abc'}, 'image'] }`
2139
+ - `target: {path: ['wrapper'], include: ['portableTextField'], operation: {type: 'image-description', sourcePath: ['image', 'asset'] }, instruction: 'Use formatting and headings to describe the image in great detail' }`
2140
+
2141
+ ##### Example: Field-based transformation
2142
+
2143
+ ```ts
2144
+ await client.agent.action.transform({
2145
+ schemaId,
2146
+ documentId,
2147
+ instruction: 'Summarize the following field: $content',
2148
+ instructionParams: {
2149
+ content: {type: 'field', path: ['body']},
2150
+ },
2151
+ target: {path: ['body']},
2152
+ })
2153
+ ```
2154
+
2155
+ ---
2156
+
2157
+ #### Translating Documents
2158
+
2159
+ ```ts
2160
+ const result = await client.agent.action.translate({
2161
+ schemaId: 'your-schema-id',
2162
+ documentId: 'source-document-id',
2163
+ targetDocument: {operation: 'create'},
2164
+ fromLanguage: {id: 'en', title: 'English'},
2165
+ toLanguage: {id: 'es', title: 'Spanish'},
2166
+ styleGuide: 'Use a friendly tone.',
2167
+ protectedPhrases: ['Sanity', 'Grapefruit'],
2168
+ target: {path: ['body']},
2169
+ })
2170
+ ```
2171
+
2172
+ - **schemaId**: The schema identifier for the document type.
2173
+ - **documentId**: The source document ID.
2174
+ - **targetDocument**: (Optional) Specify a different document to write the result to, or create a new one.
2175
+ - **fromLanguage**: (Optional) The source language code and title.
2176
+ - **toLanguage**: The target language code and title.
2177
+ - **styleGuide**: (Optional) Instructions for translation style.
2178
+ - **protectedPhrases**: (Optional) Array of phrases to leave untranslated.
2179
+ - **target**: (Optional) Specifies which fields or paths to translate.
2180
+ - **temperature**: (Optional) Controls variance, 0-1 – defaults to 0
2181
+ - **async**: (Optional) when true, the request will respond with the document id; the LLM request and mutations will continue on the server.
2182
+ - **noWrite**: (Optional) when true, the document will not be changed. The response will contain the document value with the changes.
2183
+ - **conditionalPaths**: (Optional) control how conditionally readOnly and hidden fields and types will be treated
2184
+
2185
+ ##### Example: Storing language in a field
2186
+
2187
+ ```ts
2188
+ await client.agent.action.translate({
2189
+ schemaId,
2190
+ documentId,
2191
+ toLanguage: {id: 'fr', title: 'French'},
2192
+ languageFieldPath: ['language'],
2193
+ target: {path: ['body']},
2194
+ })
2195
+ ```
2196
+
2197
+ #### Prompt the LLM
2198
+
2199
+ ```ts
2200
+ const result = await client.agent.action.prompt({
2201
+ instruction: 'Say: Oh, hi $name!',
2202
+ instructionParams: {
2203
+ name: 'Mark',
2204
+ },
2205
+ temperature: 0.5,
2206
+ format: 'text',
2207
+ })
2208
+ ```
2209
+
2210
+ - **instruction**: A string template describing what the LLM should do. Use `$variable` for dynamic values.
2211
+ - **instructionParams**: Values for variables in the instruction. Supports constants, fields, documents, or GROQ queries.
2212
+ - **format**: (Optional) 'text' or 'json'. Defaults to 'text'. Note that when specifying 'json', the instruction MUST include the word "json" (ignoring case) in some form.
2213
+ - **temperature**: (Optional) Controls variance, 0-1 – defaults to 0
2214
+
2215
+ #### Patch with a schema-aware API
2216
+
2217
+ The `client.patch` and `client.transaction` API are not schema aware. This allows patching documents any way you want, but the operations will not fail if they deviate from the document schema.
2218
+
2219
+ To ensure schema-compliant operation, `client.agent.action.patch` is available. It will ensure that provided paths and values adhere to the document schema,
2220
+ ensure no duplicate keys are inserted, and merge object values so `set` operations dont accidentally remove existing values.
2221
+
2222
+ ```ts
2223
+ const result = await client.agent.action.patch({
2224
+ schemaId,
2225
+ documentId,
2226
+ target: [
2227
+ {path: 'title', operation: 'set', value: 'New title'},
2228
+ {
2229
+ path: ['wrapper', 'array'],
2230
+ operation: 'append',
2231
+ value: [{_type: 'item', title: 'Item title'}],
2232
+ },
2233
+ ],
2234
+ })
2235
+ ```
2236
+
2237
+ - **schemaId**: The schema identifier for the document type.
2238
+ - **documentId**: The source document ID OR use `targetDocument`
2239
+ - **targetDocument**: (Optional) Specify a different document to write the result to, or create a new one. Incompatible with `documentId`
2240
+ - **target**: Specify patch operations and values for paths in the document.
2241
+ - **async**: (Optional) when true, the request will respond with the document id; the LLM request and mutations will continue on the server.
2242
+ - **noWrite**: (Optional) when true, the document will not be changed. The response will contain the document value with the changes.
2243
+ - **conditionalPaths**: (Optional) control how conditionally readOnly and hidden fields and types will be treated
2244
+
2245
+ #### Appending into array after a key
2246
+
2247
+ When appending to arrays, providing a `_key` is optional.
2248
+ When a path targets a key in an array, the values provided will be appended after that key'ed item in the array.
2249
+ Note that when appending to arrays, `value` must be an array itself, even when only a single item should be appended.
2250
+
2251
+ ```ts
2252
+ const result = await client.agent.action.patch({
2253
+ schemaId,
2254
+ documentId,
2255
+ target: {
2256
+ path: ['array', {_key: 'existingKey'}],
2257
+ operation: 'append',
2258
+ value: [{_type: 'item', title: 'Item title', _key: 'isOptionalAndWillBeGeneratedIfMissing'}],
2259
+ },
2260
+ })
2261
+ ```
2262
+
1635
2263
  ## License
1636
2264
 
1637
2265
  MIT © [Sanity.io](https://www.sanity.io/)