@sanity/client 7.2.1-agent-actions.1 → 7.2.1-agent-actions.3

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.
package/README.md CHANGED
@@ -119,6 +119,8 @@ export async function updateDocumentTitle(_id, title) {
119
119
  - [Example: Field-based transformation](#example-field-based-transformation)
120
120
  - [Translating Documents](#translating-documents)
121
121
  - [Example: Storing language in a field](#example-storing-language-in-a-field)
122
+ - [Prompt the LLM](#prompt-the-llm)
123
+ - [Patch with a schema-aware API](#patch-with-a-schema-aware-api)
122
124
  - [Version actions](#version-actions)
123
125
  - [Create Version Action](#create-version)
124
126
  - [Discard Version Action](#discard-version)
@@ -1974,10 +1976,12 @@ Agent Actions allow you to:
1974
1976
  - **Generate** new content for a document or specific fields using LLM instructions.
1975
1977
  - **Transform** a document based on instructions, optionally copying from a source document.
1976
1978
  - **Translate** a document or fields from one language to another, with support for style guides and protected phrases.
1979
+ - **Prompt** the LLM using the same instruction template format as the other actions, but returns text or json instead of acting on a document.
1980
+ - **Patch** documents using a schema-aware API; validates that provided paths and values are schema compliant and handles `setIfMissing` semantics for deep value operations
1977
1981
 
1978
1982
  All methods are available in both Promise and Observable forms:
1979
1983
 
1980
- - `client.agent.action.generate`, `client.agent.action.transform`, `client.agent.action.translate` (Promise-based)
1984
+ - `client.agent.action.generate`, `client.agent.action.transform`, `client.agent.action.translate`, `client.agent.action.prompt`, `client.agent.action.patch` (Promise-based)
1981
1985
  - `client.observable.agent.action.generate`, etc. (Observable-based, for streaming or RxJS use)
1982
1986
 
1983
1987
  ---
@@ -2001,6 +2005,10 @@ const result = await client.agent.action.generate({
2001
2005
  - **instruction**: A string template describing what to generate. Use `$variable` for dynamic values.
2002
2006
  - **instructionParams**: Values for variables in the instruction. Supports constants, fields, documents, or GROQ queries.
2003
2007
  - **target**: (Optional) Specifies which fields or paths to generate content for.
2008
+ - **temperature**: (Optional) Controls variance, 0-1 – defaults to 0.3
2009
+ - **async**: (Optional) when true, the request will respond with the document id; the LLM request and mutations will continue on the server.
2010
+ - **noWrite**: (Optional) when true, the document will not be changed. The response will contain the document value with the changes.
2011
+ - **conditionalPaths**: (Optional) control how conditionally readOnly and hidden fields and types will be treated
2004
2012
 
2005
2013
  ##### Example: Using GROQ in instructionParams
2006
2014
 
@@ -2057,6 +2065,10 @@ const result = await client.agent.action.transform({
2057
2065
  - **instruction**: A string template describing the transformation.
2058
2066
  - **targetDocument**: (Optional) Specify a different document to write the result to, or create a new one.
2059
2067
  - **target**: (Optional) Specifies which fields or paths to transform.
2068
+ - **temperature**: (Optional) Controls variance, 0-1 – defaults to 0
2069
+ - **async**: (Optional) when true, the request will respond with the document id; the LLM request and mutations will continue on the server.
2070
+ - **noWrite**: (Optional) when true, the document will not be changed. The response will contain the document value with the changes.
2071
+ - **conditionalPaths**: (Optional) control how conditionally readOnly and hidden fields and types will be treated
2060
2072
 
2061
2073
  ##### Example: Field-based transformation
2062
2074
 
@@ -2080,6 +2092,7 @@ await client.agent.action.transform({
2080
2092
  const result = await client.agent.action.translate({
2081
2093
  schemaId: 'your-schema-id',
2082
2094
  documentId: 'source-document-id',
2095
+ targetDocument: {operation: 'create'},
2083
2096
  fromLanguage: {id: 'en', title: 'English'},
2084
2097
  toLanguage: {id: 'es', title: 'Spanish'},
2085
2098
  styleGuide: 'Use a friendly tone.',
@@ -2090,11 +2103,16 @@ const result = await client.agent.action.translate({
2090
2103
 
2091
2104
  - **schemaId**: The schema identifier for the document type.
2092
2105
  - **documentId**: The source document ID.
2106
+ - **targetDocument**: (Optional) Specify a different document to write the result to, or create a new one.
2093
2107
  - **fromLanguage**: (Optional) The source language code and title.
2094
2108
  - **toLanguage**: The target language code and title.
2095
2109
  - **styleGuide**: (Optional) Instructions for translation style.
2096
2110
  - **protectedPhrases**: (Optional) Array of phrases to leave untranslated.
2097
2111
  - **target**: (Optional) Specifies which fields or paths to translate.
2112
+ - **temperature**: (Optional) Controls variance, 0-1 – defaults to 0
2113
+ - **async**: (Optional) when true, the request will respond with the document id; the LLM request and mutations will continue on the server.
2114
+ - **noWrite**: (Optional) when true, the document will not be changed. The response will contain the document value with the changes.
2115
+ - **conditionalPaths**: (Optional) control how conditionally readOnly and hidden fields and types will be treated
2098
2116
 
2099
2117
  ##### Example: Storing language in a field
2100
2118
 
@@ -2108,6 +2126,68 @@ await client.agent.action.translate({
2108
2126
  })
2109
2127
  ```
2110
2128
 
2129
+ #### Prompt the LLM
2130
+
2131
+ ```ts
2132
+ const result = await client.agent.action.prompt({
2133
+ instruction: 'Say: Oh, hi $name!',
2134
+ instructionParams: {
2135
+ name: 'Mark',
2136
+ },
2137
+ temperature: 0.5,
2138
+ format: 'text'
2139
+ })
2140
+ ```
2141
+
2142
+ - **instruction**: A string template describing what the LLM should do. Use `$variable` for dynamic values.
2143
+ - **instructionParams**: Values for variables in the instruction. Supports constants, fields, documents, or GROQ queries.
2144
+ - **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.
2145
+ - **temperature**: (Optional) Controls variance, 0-1 – defaults to 0
2146
+
2147
+ #### Patch with a schema-aware API
2148
+
2149
+ 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.
2150
+
2151
+ To ensure schema-compliant operation, `client.agent.action.patch` is available. It will ensure that provided paths and values adhere to the document schema,
2152
+ ensure no duplicate keys are inserted, and merge object values so `set` operations dont accidentally remove existing values.
2153
+
2154
+ ```ts
2155
+ const result = await client.agent.action.prompt({
2156
+ schemaId,
2157
+ documentId,
2158
+ target: [
2159
+ {path: 'title', operation: 'set', value: 'New title'},
2160
+ {path: ['wrapper', 'array'], operation: 'append', value: [{_type: 'item', title: 'Item title'}]},
2161
+ ]
2162
+ })
2163
+ ```
2164
+
2165
+ - **schemaId**: The schema identifier for the document type.
2166
+ - **documentId**: The source document ID OR use `targetDocument`
2167
+ - **targetDocument**: (Optional) Specify a different document to write the result to, or create a new one. Incompatible with `documentId`
2168
+ - **target**: Specify patch operations and values for paths in the document.
2169
+ - **async**: (Optional) when true, the request will respond with the document id; the LLM request and mutations will continue on the server.
2170
+ - **noWrite**: (Optional) when true, the document will not be changed. The response will contain the document value with the changes.
2171
+ - **conditionalPaths**: (Optional) control how conditionally readOnly and hidden fields and types will be treated
2172
+
2173
+ #### Appending into array after a key
2174
+
2175
+ When appending to arrays, providing a `_key` is optional.
2176
+ When a path targets a key in an array, the values provided will be appended after that key'ed item in the array.
2177
+ Note that when appending to arrays, `value` must be an array itself, even when only a single item should be appended.
2178
+
2179
+ ```ts
2180
+ const result = await client.agent.action.prompt({
2181
+ schemaId,
2182
+ documentId,
2183
+ target: {
2184
+ path: ['array', {_key: 'existingKey'}],
2185
+ operation: 'append',
2186
+ value: [{_type: 'item', title: 'Item title', _key: 'isOptionalAndWillBeGeneratedIfMissing'}]
2187
+ },
2188
+ })
2189
+ ```
2190
+
2111
2191
  ## License
2112
2192
 
2113
2193
  MIT © [Sanity.io](https://www.sanity.io/)
@@ -1058,6 +1058,14 @@ function _generate(client, httpRequest, request) {
1058
1058
  body: request
1059
1059
  });
1060
1060
  }
1061
+ function _patch(client, httpRequest, request) {
1062
+ const dataset2 = hasDataset(client.config());
1063
+ return _request(client, httpRequest, {
1064
+ method: "POST",
1065
+ uri: `/agent/action/patch/${dataset2}`,
1066
+ body: request
1067
+ });
1068
+ }
1061
1069
  function _prompt(client, httpRequest, request) {
1062
1070
  const dataset2 = hasDataset(client.config());
1063
1071
  return _request(client, httpRequest, {
@@ -1144,6 +1152,14 @@ class AgentActionsClient {
1144
1152
  prompt(request) {
1145
1153
  return rxjs.lastValueFrom(_prompt(this.#client, this.#httpRequest, request));
1146
1154
  }
1155
+ /**
1156
+ * Patch a document using a schema aware API.
1157
+ * Does not use an LLM, but uses the schema to ensure paths and values matches the schema.
1158
+ * @param request - instruction request
1159
+ */
1160
+ patch(request) {
1161
+ return rxjs.lastValueFrom(_patch(this.#client, this.#httpRequest, request));
1162
+ }
1147
1163
  }
1148
1164
  class ObservableAssetsClient {
1149
1165
  #client;