content-agent 0.5.2 → 0.6.0
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 +86 -45
- package/dist/index.d.ts +198 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +171 -7
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -23,12 +23,10 @@ const contentAgent = createContentAgent({
|
|
|
23
23
|
|
|
24
24
|
const model = contentAgent.agent("my-thread");
|
|
25
25
|
|
|
26
|
-
const
|
|
26
|
+
const { text } = await generateText({
|
|
27
27
|
model,
|
|
28
28
|
prompt: "What blog posts do I have?",
|
|
29
29
|
});
|
|
30
|
-
|
|
31
|
-
console.log(result.text);
|
|
32
30
|
```
|
|
33
31
|
|
|
34
32
|
## Streaming
|
|
@@ -50,13 +48,12 @@ for await (const text of textStream) {
|
|
|
50
48
|
|
|
51
49
|
## Applications
|
|
52
50
|
|
|
53
|
-
Each application key uniquely identifies a deployed Sanity Studio workspace. Since multiple studios can share the same project ID and dataset, the application key
|
|
51
|
+
Each application key uniquely identifies a deployed Sanity Studio workspace. Since multiple studios can share the same project ID and dataset, the application key disambiguates which one to target.
|
|
54
52
|
|
|
55
53
|
List available applications for your organization, then target one by its key:
|
|
56
54
|
|
|
57
55
|
```typescript
|
|
58
56
|
const apps = await contentAgent.applications();
|
|
59
|
-
|
|
60
57
|
const app = apps.find((app) => app.title === "My Studio");
|
|
61
58
|
|
|
62
59
|
const model = contentAgent.agent("my-thread", {
|
|
@@ -64,51 +61,56 @@ const model = contentAgent.agent("my-thread", {
|
|
|
64
61
|
});
|
|
65
62
|
```
|
|
66
63
|
|
|
64
|
+
You can also set a default application on the provider so every `.agent()` and `.prompt()` call inherits it:
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
const contentAgent = createContentAgent({
|
|
68
|
+
organizationId: "your-org-id",
|
|
69
|
+
token: "your-token",
|
|
70
|
+
application: resolvedApp,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// No need to pass application per-call
|
|
74
|
+
const model = contentAgent.agent("my-thread");
|
|
75
|
+
```
|
|
76
|
+
|
|
67
77
|
## Tools
|
|
68
78
|
|
|
69
79
|
Pass AI SDK tools to `streamText` or `generateText` — tool schemas are forwarded to the agent, and execution happens locally on the client:
|
|
70
80
|
|
|
71
81
|
```typescript
|
|
72
|
-
import { streamText, tool
|
|
82
|
+
import { streamText, tool } from "ai";
|
|
73
83
|
import { z } from "zod";
|
|
74
84
|
|
|
75
85
|
const result = streamText({
|
|
76
|
-
model: contentAgent.agent("my-thread"
|
|
77
|
-
prompt:
|
|
86
|
+
model: contentAgent.agent("my-thread"),
|
|
87
|
+
prompt: "Find all draft blog posts and submit them for review.",
|
|
78
88
|
tools: {
|
|
79
89
|
submit_for_review: tool({
|
|
80
|
-
description:
|
|
90
|
+
description: "Submit a document for editorial review.",
|
|
81
91
|
inputSchema: z.object({
|
|
82
92
|
documentId: z.string().describe("The Sanity document _id"),
|
|
83
|
-
email: z.string().email().describe("
|
|
93
|
+
email: z.string().email().describe("Reviewer email"),
|
|
84
94
|
note: z.string().optional().describe("Note for the reviewer"),
|
|
85
95
|
}),
|
|
86
96
|
execute: async ({ documentId, email, note }) => {
|
|
87
|
-
const res = await fetch(
|
|
88
|
-
"
|
|
89
|
-
{
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
},
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
const { ticketUrl } = await res.json();
|
|
96
|
-
return { success: true, documentId, ticketUrl };
|
|
97
|
+
const res = await fetch("https://workflows.example.com/api/review", {
|
|
98
|
+
method: "POST",
|
|
99
|
+
body: JSON.stringify({ documentId, email, note }),
|
|
100
|
+
});
|
|
101
|
+
return res.json();
|
|
97
102
|
},
|
|
98
103
|
}),
|
|
99
104
|
},
|
|
100
105
|
});
|
|
101
106
|
```
|
|
102
107
|
|
|
103
|
-
The model decides when to call your tools. `stopWhen` controls how many round-trips are allowed between the model and tool execution.
|
|
104
|
-
|
|
105
108
|
## Configuration
|
|
106
109
|
|
|
107
110
|
Pass a `config` object to `.agent()` to control behavior:
|
|
108
111
|
|
|
109
112
|
```typescript
|
|
110
113
|
const model = contentAgent.agent("my-thread", {
|
|
111
|
-
application: { key: "your-application-key" },
|
|
112
114
|
config: {
|
|
113
115
|
instruction: "You are a helpful content assistant for our marketing team.",
|
|
114
116
|
userMessageContext: {
|
|
@@ -137,41 +139,80 @@ const model = contentAgent.agent("my-thread", {
|
|
|
137
139
|
| `filter` | `object` | GROQ boolean expressions for document access control |
|
|
138
140
|
| `perspectives` | `object` | Perspective locking for read and write operations |
|
|
139
141
|
|
|
140
|
-
###
|
|
142
|
+
### Capabilities
|
|
141
143
|
|
|
142
|
-
Each
|
|
144
|
+
Each capability can be `true`, `false`, or a preset object:
|
|
143
145
|
|
|
144
146
|
```typescript
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
147
|
+
// Read-only
|
|
148
|
+
config: { capabilities: { read: true, write: false } }
|
|
149
|
+
|
|
150
|
+
// Minimal write access
|
|
151
|
+
config: { capabilities: { read: true, write: { preset: "minimal" } } }
|
|
151
152
|
```
|
|
152
153
|
|
|
153
|
-
|
|
154
|
+
## Studio usage
|
|
154
155
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
156
|
+
`createStudioAgent` makes the provider work inside Sanity Studio with no token or manual configuration. It derives the API host, organization ID, and application from the Studio context:
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
import { useMemo } from "react";
|
|
160
|
+
import { createStudioAgent } from "content-agent";
|
|
161
|
+
import { useClient, useWorkspace } from "sanity";
|
|
162
|
+
|
|
163
|
+
function MyComponent() {
|
|
164
|
+
const client = useClient({ apiVersion: "2024-01-01" });
|
|
165
|
+
const { name: workspace } = useWorkspace();
|
|
166
|
+
|
|
167
|
+
const contentAgent = useMemo(
|
|
168
|
+
() => createStudioAgent(client, workspace),
|
|
169
|
+
[client, workspace],
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
// Use contentAgent.agent(threadId) with useChat, generateText, etc.
|
|
173
|
+
}
|
|
161
174
|
```
|
|
162
175
|
|
|
163
|
-
|
|
176
|
+
The factory call is synchronous — async setup (org ID fetch, application resolution) runs lazily on the first API call and is cached at the module level. Wrap in `useMemo` so the provider keeps a stable identity across renders.
|
|
164
177
|
|
|
165
|
-
|
|
178
|
+
Since there is no token in a Studio context, `credentials: 'include'` is set on all fetch calls automatically so the browser sends session cookies. The base URL is derived from the client's `apiHost` config.
|
|
166
179
|
|
|
167
|
-
|
|
168
|
-
// Read-only
|
|
169
|
-
config: { capabilities: { read: true, write: false } }
|
|
180
|
+
### Using with `useChat`
|
|
170
181
|
|
|
171
|
-
|
|
172
|
-
|
|
182
|
+
For a streaming chat UI, pair with `DirectChatTransport` from the AI SDK:
|
|
183
|
+
|
|
184
|
+
```tsx
|
|
185
|
+
import { useMemo, useState } from "react";
|
|
186
|
+
import { useClient, useWorkspace } from "sanity";
|
|
187
|
+
import { useChat } from "@ai-sdk/react";
|
|
188
|
+
import { DirectChatTransport, ToolLoopAgent } from "ai";
|
|
189
|
+
import { createStudioAgent } from "content-agent";
|
|
190
|
+
|
|
191
|
+
function ChatTool() {
|
|
192
|
+
const client = useClient({ apiVersion: "2024-01-01" });
|
|
193
|
+
const { name: workspace } = useWorkspace();
|
|
194
|
+
const [threadId] = useState(() => `thread-${crypto.randomUUID()}`);
|
|
195
|
+
|
|
196
|
+
const transport = useMemo(() => {
|
|
197
|
+
const contentAgent = createStudioAgent(client, workspace);
|
|
198
|
+
return new DirectChatTransport({
|
|
199
|
+
agent: new ToolLoopAgent({ model: contentAgent.agent(threadId) }),
|
|
200
|
+
});
|
|
201
|
+
}, [client, workspace, threadId]);
|
|
202
|
+
|
|
203
|
+
const { messages, sendMessage, status } = useChat({ transport });
|
|
204
|
+
|
|
205
|
+
// Render messages, input form, etc.
|
|
206
|
+
}
|
|
173
207
|
```
|
|
174
208
|
|
|
209
|
+
### Lower-level helpers
|
|
210
|
+
|
|
211
|
+
If you already know the organization ID and need more control:
|
|
212
|
+
|
|
213
|
+
- `fromClient(client, workspace)` — derives `baseURL` and `application` from a Sanity client
|
|
214
|
+
- `contentAgent.resolveApplication(client, workspace)` — fetches applications and matches by resource ID + workspace name
|
|
215
|
+
|
|
175
216
|
## License
|
|
176
217
|
|
|
177
218
|
MIT
|
package/dist/index.d.ts
CHANGED
|
@@ -492,8 +492,8 @@ type ApplicationResource = NonNullable<Application['resource']>;
|
|
|
492
492
|
* ```
|
|
493
493
|
*/
|
|
494
494
|
type Config = components['schemas']['Config'] & {
|
|
495
|
-
/**
|
|
496
|
-
|
|
495
|
+
/** Controls how the agent delivers responses. 'tool' enables the final_answer tool with structured message + suggestions. Omit or use 'stream' for normal streaming. */responseMode?: 'stream' | 'tool'; /** When true and responseMode is 'tool', the final_answer tool includes a suggestions field for follow-up reply buttons. */
|
|
496
|
+
enableReplySuggestions?: boolean;
|
|
497
497
|
};
|
|
498
498
|
/**
|
|
499
499
|
* Controls what operations the agent can perform.
|
|
@@ -598,8 +598,20 @@ type UserMessageContext = NonNullable<Config['userMessageContext']>;
|
|
|
598
598
|
interface ContentAgentProviderSettings {
|
|
599
599
|
/**
|
|
600
600
|
* Your Sanity organization ID.
|
|
601
|
+
*
|
|
602
|
+
* When using {@link createStudioAgent}, this is fetched automatically
|
|
603
|
+
* from the project metadata.
|
|
601
604
|
*/
|
|
602
605
|
organizationId: string;
|
|
606
|
+
/**
|
|
607
|
+
* Default application for all agent and prompt calls.
|
|
608
|
+
*
|
|
609
|
+
* When set, this application is used unless overridden per-call via
|
|
610
|
+
* {@link AgentSettings.application} or {@link GenerateSettings.application}.
|
|
611
|
+
*
|
|
612
|
+
* Typically set automatically by {@link createStudioAgent}.
|
|
613
|
+
*/
|
|
614
|
+
application?: Application;
|
|
603
615
|
/**
|
|
604
616
|
* Base URL for the Sanity API.
|
|
605
617
|
* @default 'https://api.sanity.io'
|
|
@@ -776,6 +788,86 @@ interface GenerateResult {
|
|
|
776
788
|
*/
|
|
777
789
|
text: string;
|
|
778
790
|
}
|
|
791
|
+
/**
|
|
792
|
+
* Minimal interface for a Sanity client. Avoids a hard dependency on `@sanity/client` —
|
|
793
|
+
* any object whose `config()` returns `projectId`, `dataset`, and `apiHost` will work.
|
|
794
|
+
*
|
|
795
|
+
* @category Studio Integration
|
|
796
|
+
*/
|
|
797
|
+
type SanityClientLike = {
|
|
798
|
+
config(): {
|
|
799
|
+
projectId?: string;
|
|
800
|
+
dataset?: string;
|
|
801
|
+
apiHost?: string;
|
|
802
|
+
};
|
|
803
|
+
};
|
|
804
|
+
/**
|
|
805
|
+
* Configuration derived from a Sanity client for use with {@link createContentAgent}.
|
|
806
|
+
*
|
|
807
|
+
* @category Studio Integration
|
|
808
|
+
*/
|
|
809
|
+
type ClientDerivedConfig = {
|
|
810
|
+
/**
|
|
811
|
+
* Project-scoped API base URL derived from the client's `apiHost`.
|
|
812
|
+
*
|
|
813
|
+
* Using a project-scoped host avoids CORS preflight issues when making
|
|
814
|
+
* cookie-authenticated requests from a Studio context.
|
|
815
|
+
*/
|
|
816
|
+
baseURL: string;
|
|
817
|
+
/**
|
|
818
|
+
* Application targeting the client's project, dataset, and workspace.
|
|
819
|
+
*
|
|
820
|
+
* Identified by `resource.id` (`projectId.dataset`) and `name` (workspace name),
|
|
821
|
+
* which matches the format the Content Agent API uses internally.
|
|
822
|
+
*/
|
|
823
|
+
application: Application;
|
|
824
|
+
};
|
|
825
|
+
/**
|
|
826
|
+
* Derives a project-scoped base URL from the client's configured API host.
|
|
827
|
+
* Inserts `projectId` as a subdomain so cookie-authenticated requests
|
|
828
|
+
* from Studio pass CORS checks.
|
|
829
|
+
*
|
|
830
|
+
* `https://api.sanity.io` → `https://{projectId}.api.sanity.io`
|
|
831
|
+
* `https://api.sanity.work` → `https://{projectId}.api.sanity.work`
|
|
832
|
+
*
|
|
833
|
+
* @category Studio Integration
|
|
834
|
+
*/
|
|
835
|
+
declare function deriveBaseUrl(apiHost: string | undefined, projectId: string): string;
|
|
836
|
+
/**
|
|
837
|
+
* Derives Content Agent configuration from a Sanity client and workspace name.
|
|
838
|
+
*
|
|
839
|
+
* Extracts `projectId`, `dataset`, and `apiHost` to build a CORS-compatible
|
|
840
|
+
* `baseURL` and an `application` that targets the correct workspace — no need
|
|
841
|
+
* to know the Studio deployment ID or application key.
|
|
842
|
+
*
|
|
843
|
+
* Designed for Studio / browser contexts where authentication is handled
|
|
844
|
+
* via cookies. Omit the `token` when creating the provider and credentials
|
|
845
|
+
* will be included automatically.
|
|
846
|
+
*
|
|
847
|
+
* @category Studio Integration
|
|
848
|
+
*
|
|
849
|
+
* @example Studio AI Assist action
|
|
850
|
+
* ```ts
|
|
851
|
+
* import { createContentAgent, fromClient } from 'content-agent'
|
|
852
|
+
* import { useClient, useWorkspace } from 'sanity'
|
|
853
|
+
*
|
|
854
|
+
* const client = useClient({ apiVersion: '2024-01-01' })
|
|
855
|
+
* const workspace = useWorkspace()
|
|
856
|
+
* const { baseURL } = fromClient(client, workspace.name)
|
|
857
|
+
*
|
|
858
|
+
* const contentAgent = createContentAgent({
|
|
859
|
+
* organizationId: 'your-org-id',
|
|
860
|
+
* baseURL,
|
|
861
|
+
* })
|
|
862
|
+
*
|
|
863
|
+
* const application = await contentAgent.resolveApplication(client, workspace.name)
|
|
864
|
+
* const model = contentAgent.agent('thread-id', { application })
|
|
865
|
+
* ```
|
|
866
|
+
*
|
|
867
|
+
* @param client - Any object with a `config()` method returning `projectId`, `dataset`, and optionally `apiHost`.
|
|
868
|
+
* @param workspace - The active workspace name (e.g. `'default'`). Available via `useWorkspace().name` in Studio.
|
|
869
|
+
*/
|
|
870
|
+
declare function fromClient(client: SanityClientLike, workspace: string): ClientDerivedConfig;
|
|
779
871
|
/**
|
|
780
872
|
* @category Model
|
|
781
873
|
*/
|
|
@@ -785,6 +877,7 @@ interface ContentAgentModelConfig {
|
|
|
785
877
|
apiVersion: string;
|
|
786
878
|
organizationId: string;
|
|
787
879
|
headers: () => Record<string, string | undefined>;
|
|
880
|
+
credentials?: RequestCredentials;
|
|
788
881
|
fetch?: typeof globalThis.fetch;
|
|
789
882
|
}
|
|
790
883
|
/**
|
|
@@ -915,6 +1008,39 @@ interface ContentAgentProvider {
|
|
|
915
1008
|
* ```
|
|
916
1009
|
*/
|
|
917
1010
|
applications(): Promise<Application[]>;
|
|
1011
|
+
/**
|
|
1012
|
+
* Resolves the full application for a Sanity client by matching its `projectId`
|
|
1013
|
+
* and `dataset` against the organization's application list.
|
|
1014
|
+
*
|
|
1015
|
+
* This is the easiest way to get a valid application in Studio contexts —
|
|
1016
|
+
* pass the client you already have and get back an application with the
|
|
1017
|
+
* correct `key` for the Content Agent API.
|
|
1018
|
+
*
|
|
1019
|
+
* Throws if no matching application is found (e.g. wrong org, or the Studio
|
|
1020
|
+
* hasn't been deployed yet).
|
|
1021
|
+
*
|
|
1022
|
+
* @example
|
|
1023
|
+
* ```ts
|
|
1024
|
+
* import { createContentAgent, fromClient } from 'content-agent'
|
|
1025
|
+
* import { useClient, useWorkspace } from 'sanity'
|
|
1026
|
+
*
|
|
1027
|
+
* const client = useClient({ apiVersion: '2024-01-01' })
|
|
1028
|
+
* const workspace = useWorkspace()
|
|
1029
|
+
* const { baseURL } = fromClient(client, workspace.name)
|
|
1030
|
+
*
|
|
1031
|
+
* const contentAgent = createContentAgent({
|
|
1032
|
+
* organizationId: 'your-org-id',
|
|
1033
|
+
* baseURL,
|
|
1034
|
+
* })
|
|
1035
|
+
*
|
|
1036
|
+
* const application = await contentAgent.resolveApplication(client, workspace.name)
|
|
1037
|
+
* const model = contentAgent.agent('thread-id', { application })
|
|
1038
|
+
* ```
|
|
1039
|
+
*
|
|
1040
|
+
* @param client - Any object with a `config()` method returning `projectId` and `dataset`.
|
|
1041
|
+
* @param workspace - The active workspace name (e.g. `'default'`). Available via `useWorkspace().name` in Studio.
|
|
1042
|
+
*/
|
|
1043
|
+
resolveApplication(client: SanityClientLike, workspace: string): Promise<Application>;
|
|
918
1044
|
/**
|
|
919
1045
|
* Sends a one-shot prompt to the Content Agent (stateless).
|
|
920
1046
|
*
|
|
@@ -999,6 +1125,74 @@ interface ContentAgentProvider {
|
|
|
999
1125
|
* @param options - Provider configuration including organization ID, token, and optional overrides.
|
|
1000
1126
|
*/
|
|
1001
1127
|
declare function createContentAgent(options: ContentAgentProviderSettings): ContentAgentProvider;
|
|
1128
|
+
/**
|
|
1129
|
+
* Options for {@link createStudioAgent}.
|
|
1130
|
+
*
|
|
1131
|
+
* @category Studio Integration
|
|
1132
|
+
*/
|
|
1133
|
+
type StudioAgentOptions = {
|
|
1134
|
+
/**
|
|
1135
|
+
* Custom fetch implementation. Useful for testing.
|
|
1136
|
+
*/
|
|
1137
|
+
fetch?: typeof globalThis.fetch;
|
|
1138
|
+
/**
|
|
1139
|
+
* Additional provider settings forwarded to {@link createContentAgent}.
|
|
1140
|
+
* `organizationId`, `baseURL`, and `application` are derived automatically
|
|
1141
|
+
* and cannot be overridden here.
|
|
1142
|
+
*/
|
|
1143
|
+
providerOptions?: Omit<ContentAgentProviderSettings, 'organizationId' | 'baseURL' | 'application'>;
|
|
1144
|
+
};
|
|
1145
|
+
/**
|
|
1146
|
+
* Creates a Content Agent provider fully configured for a Studio context.
|
|
1147
|
+
*
|
|
1148
|
+
* This is the simplest way to use Content Agent from inside Sanity Studio.
|
|
1149
|
+
* The call is **synchronous** — safe to use in React components and hooks.
|
|
1150
|
+
* Async setup (fetching the organization ID and resolving the application)
|
|
1151
|
+
* is deferred to the first API call, which is already async.
|
|
1152
|
+
*
|
|
1153
|
+
* Internally handles:
|
|
1154
|
+
* 1. Deriving the project-scoped API URL (for CORS)
|
|
1155
|
+
* 2. Fetching the organization ID from project metadata
|
|
1156
|
+
* 3. Resolving the Studio application by workspace name
|
|
1157
|
+
* 4. Cookie-based auth (no token needed)
|
|
1158
|
+
*
|
|
1159
|
+
* The returned provider works exactly like one from {@link createContentAgent},
|
|
1160
|
+
* but `.agent()` and `.prompt()` automatically target the resolved application
|
|
1161
|
+
* unless explicitly overridden per-call.
|
|
1162
|
+
*
|
|
1163
|
+
* @category Studio Integration
|
|
1164
|
+
*
|
|
1165
|
+
* @example Studio component with memoized provider
|
|
1166
|
+
* ```ts
|
|
1167
|
+
* import { useMemo } from 'react'
|
|
1168
|
+
* import { createStudioAgent } from 'content-agent'
|
|
1169
|
+
* import { useClient, useWorkspace } from 'sanity'
|
|
1170
|
+
* import { generateText } from 'ai'
|
|
1171
|
+
*
|
|
1172
|
+
* function MyComponent() {
|
|
1173
|
+
* const client = useClient({ apiVersion: '2024-01-01' })
|
|
1174
|
+
* const { name: workspace } = useWorkspace()
|
|
1175
|
+
*
|
|
1176
|
+
* const contentAgent = useMemo(
|
|
1177
|
+
* () => createStudioAgent(client, workspace),
|
|
1178
|
+
* [client, workspace],
|
|
1179
|
+
* )
|
|
1180
|
+
*
|
|
1181
|
+
* async function handleSummarize() {
|
|
1182
|
+
* const { text } = await generateText({
|
|
1183
|
+
* model: contentAgent.agent('my-thread'),
|
|
1184
|
+
* prompt: 'Summarize my content',
|
|
1185
|
+
* })
|
|
1186
|
+
* console.log(text)
|
|
1187
|
+
* }
|
|
1188
|
+
* }
|
|
1189
|
+
* ```
|
|
1190
|
+
*
|
|
1191
|
+
* @param client - Any object with a `config()` returning `projectId` and `dataset`.
|
|
1192
|
+
* @param workspace - The active workspace name. Available via `useWorkspace().name` in Studio.
|
|
1193
|
+
* @param options - Optional overrides (custom fetch, additional provider settings).
|
|
1194
|
+
*/
|
|
1195
|
+
declare function createStudioAgent(client: SanityClientLike, workspace: string, options?: StudioAgentOptions): ContentAgentProvider;
|
|
1002
1196
|
/**
|
|
1003
1197
|
* Builds headers for API requests, filtering out undefined values.
|
|
1004
1198
|
*
|
|
@@ -1052,6 +1246,7 @@ interface FetchOptions {
|
|
|
1052
1246
|
url: string;
|
|
1053
1247
|
body: unknown;
|
|
1054
1248
|
headers: Record<string, string>;
|
|
1249
|
+
credentials?: RequestCredentials;
|
|
1055
1250
|
signal?: AbortSignal;
|
|
1056
1251
|
fetch?: typeof globalThis.fetch;
|
|
1057
1252
|
}
|
|
@@ -1130,5 +1325,5 @@ declare function extractTextFromStream(responseBody: ReadableStream<Uint8Array>)
|
|
|
1130
1325
|
* @category Utilities
|
|
1131
1326
|
*/
|
|
1132
1327
|
declare function createSSEStream(responseBody: ReadableStream<Uint8Array>, warnings: SharedV3Warning[], onData?: (type: string, data: unknown) => void): ReadableStream<LanguageModelV3StreamPart>;
|
|
1133
|
-
export { type AgentErrorType, type AgentSettings, type Application, type ApplicationResource, type Capabilities, type Capability, type CapabilityPreset, type CapabilityPresetValue, type ChatRequest, type ChatResponse, type ClientToolDefinition, type Config, ContentAgentModel, type ContentAgentModelConfig, type ContentAgentProvider, type ContentAgentProviderSettings, type ErrorResponse, type Filter, GENERIC_ERROR_MESSAGE, type GenerateOptions, type GenerateRequest, type GenerateResult, type GenerateSettings, type Message, type MessageRole, type OutputFormat, type ParsedStreamLine, SanityAgentError, type UserMessageContext, type ValidationIssue, buildApiUrl, buildHeaders, classifyAgentError, type components, createContentAgent, createSSEStream, extractTextFromParts, extractTextFromStream, fetchApi, getAgentErrorMessage, mapFinishReason, type operations, parseStreamLine, type paths };
|
|
1328
|
+
export { type AgentErrorType, type AgentSettings, type Application, type ApplicationResource, type Capabilities, type Capability, type CapabilityPreset, type CapabilityPresetValue, type ChatRequest, type ChatResponse, type ClientDerivedConfig, type ClientToolDefinition, type Config, ContentAgentModel, type ContentAgentModelConfig, type ContentAgentProvider, type ContentAgentProviderSettings, type ErrorResponse, type Filter, GENERIC_ERROR_MESSAGE, type GenerateOptions, type GenerateRequest, type GenerateResult, type GenerateSettings, type Message, type MessageRole, type OutputFormat, type ParsedStreamLine, SanityAgentError, type SanityClientLike, type StudioAgentOptions, type UserMessageContext, type ValidationIssue, buildApiUrl, buildHeaders, classifyAgentError, type components, createContentAgent, createSSEStream, createStudioAgent, deriveBaseUrl, extractTextFromParts, extractTextFromStream, fetchApi, fromClient, getAgentErrorMessage, mapFinishReason, type operations, parseStreamLine, type paths };
|
|
1134
1329
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/generated/openapi.ts","../src/types.ts","../src/sanity-agent-language-model.ts","../src/sanity-agent-provider.ts","../src/utils.ts"],"mappings":";;;;;AAKA;KAAY,KAAA;EACV,sCAAA;IACE,UAAA;MACE,KAAA;MACA,MAAA;MACA,IAAA;MACA,MAAA;IAAA;IAEF,GAAA;IACA,GAAA;IAAA;;;;;IAMA,IAAA,EAAM,UAAA;IACN,MAAA;IACA,OAAA;IACA,IAAA;IACA,KAAA;IACA,KAAA;EAAA;EAEF,SAAA;IACE,UAAA;MACE,KAAA;MACA,MAAA;MACA,IAAA;MACA,MAAA;IAAA;IAEF,GAAA;IACA,GAAA;IAUA;;;;AAKJ;IATI,IAAA,EAAM,UAAA;IACN,MAAA;IACA,OAAA;IACA,IAAA;IACA,KAAA;IACA,KAAA;EAAA;AAAA;AAAA,KAIQ,UAAA;EACV,OAAA;IACE,WAAA;MAiJsB,4CA/IpB,QAAA,EAAU,UAAA;MACV,WAAA,GAAc,UAAA;MACd,MAAA,GAAS,UAAA;MA6Ra;;;;MAxRtB,MAAA;MAPA;;;;;;;MAeA,MAAA;IAAA;IAEF,aAAA;MAUE;;;;MALA,cAAA;MAqBF;;;;MAhBE,OAAA;MACA,MAAA,GAAS,UAAA;MAkCT;;;;MA7BA,YAAA;MAiDA;;;;;;;MAzCA,MAAA;IAAA;IAEF,OAAA;MAmEE;;;;MA9DA,IAAA,mCA0EA;MAxEA,OAAA,UAyEA;MAvEA,EAAA,WAwEA;MAtEA,IAAA;IAAA,GAiFA;IA9EF,WAAA;MAsFsB;;;;MAjFp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,EAAoB,UAAA;MAAA;IAAA,GC7GiB;IDiHzC,YAAA;MACE,OAAA;QAAA,CACG,IAAA;MAAA;MAEH,OAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/generated/openapi.ts","../src/types.ts","../src/from-client.ts","../src/sanity-agent-language-model.ts","../src/sanity-agent-provider.ts","../src/studio.ts","../src/utils.ts"],"mappings":";;;;;AAKA;KAAY,KAAA;EACV,sCAAA;IACE,UAAA;MACE,KAAA;MACA,MAAA;MACA,IAAA;MACA,MAAA;IAAA;IAEF,GAAA;IACA,GAAA;IAAA;;;;;IAMA,IAAA,EAAM,UAAA;IACN,MAAA;IACA,OAAA;IACA,IAAA;IACA,KAAA;IACA,KAAA;EAAA;EAEF,SAAA;IACE,UAAA;MACE,KAAA;MACA,MAAA;MACA,IAAA;MACA,MAAA;IAAA;IAEF,GAAA;IACA,GAAA;IAUA;;;;AAKJ;IATI,IAAA,EAAM,UAAA;IACN,MAAA;IACA,OAAA;IACA,IAAA;IACA,KAAA;IACA,KAAA;EAAA;AAAA;AAAA,KAIQ,UAAA;EACV,OAAA;IACE,WAAA;MAiJsB,4CA/IpB,QAAA,EAAU,UAAA;MACV,WAAA,GAAc,UAAA;MACd,MAAA,GAAS,UAAA;MA6Ra;;;;MAxRtB,MAAA;MAPA;;;;;;;MAeA,MAAA;IAAA;IAEF,aAAA;MAUE;;;;MALA,cAAA;MAqBF;;;;MAhBE,OAAA;MACA,MAAA,GAAS,UAAA;MAkCT;;;;MA7BA,YAAA;MAiDA;;;;;;;MAzCA,MAAA;IAAA;IAEF,OAAA;MAmEE;;;;MA9DA,IAAA,mCA0EA;MAxEA,OAAA,UAyEA;MAvEA,EAAA,WAwEA;MAtEA,IAAA;IAAA,GAiFA;IA9EF,WAAA;MAsFsB;;;;MAjFpB,GAAA;MACA,QAAA;QAkHF;;;;QA7GI,EAAA;QAmIF;;;;QA9HE,IAAA;MAAA;MA8IJ;;;;;MAvIE,aAAA,WA8JA;MA5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EAAM,OAAA,CAAQ,qBAAA;cAEX,MAAA,UAAgB,OAAA;EAAA,eAQb,SAAA;AAAA;AAAA,cASJ,qBAAA;AAAA,KAED,cAAA;EACN,IAAA;AAAA;EACA,IAAA;AAAA;EACA,IAAA;AAAA;;;;ANrBN;;;;iBM8BgB,kBAAA,CAAmB,KAAA,YAAiB,cAAA;;;;;;;iBAqBpC,oBAAA,CAAqB,KAAA;AAAA,UAOpB,YAAA;EACf,GAAA;EACA,IAAA;EACA,OAAA,EAAS,MAAA;EACT,WAAA,GAAc,kBAAA;EACd,MAAA,GAAS,WAAA;EACT,KAAA,UAAe,UAAA,CAAW,KAAA;AAAA;;;;;;;iBASN,QAAA,CAAS,OAAA,EAAS,YAAA,GAAe,OAAA,CAAQ,QAAA;;;;;;iBAwB/C,oBAAA,CACd,OAAA,WAAkB,KAAA;EAAQ,IAAA;EAAc,IAAA;AAAA;;;;;;iBAyC1B,eAAA,CAAgB,MAAA,uBAA6B,2BAAA;AAAA,KAiBjD,gBAAA;EACN,IAAA;EAAc,IAAA,EAAM,yBAAA;AAAA;EACpB,IAAA;EAAgB,YAAA,EAAc,2BAAA;AAAA;EAC9B,IAAA;EAAc,IAAA;EAAc,IAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;iBA0BlB,eAAA,CAAgB,IAAA,UAAc,aAAA,WAAwB,gBAAA;;;;;;;iBAuFhD,qBAAA,CACpB,YAAA,EAAc,cAAA,CAAe,UAAA,IAC5B,OAAA;;;;;;;;;;iBA+Ba,eAAA,CACd,YAAA,EAAc,cAAA,CAAe,UAAA,GAC7B,QAAA,EAAU,eAAA,IACV,MAAA,IAAU,IAAA,UAAc,IAAA,qBACvB,cAAA,CAAe,yBAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
import { pickBy, isString, groupBy } from "es-toolkit";
|
|
2
2
|
import { ERROR_CODES } from "@sanity/agent-types";
|
|
3
|
+
const DEFAULT_API_HOST = "https://api.sanity.io";
|
|
4
|
+
function deriveBaseUrl(apiHost, projectId) {
|
|
5
|
+
const url = new URL(apiHost || DEFAULT_API_HOST);
|
|
6
|
+
return url.hostname = `${projectId}.${url.hostname}`, url.origin;
|
|
7
|
+
}
|
|
8
|
+
function fromClient(client, workspace) {
|
|
9
|
+
const { projectId, dataset, apiHost } = client.config();
|
|
10
|
+
if (!projectId)
|
|
11
|
+
throw new Error("fromClient: client is missing `projectId` in its config");
|
|
12
|
+
if (!dataset)
|
|
13
|
+
throw new Error("fromClient: client is missing `dataset` in its config");
|
|
14
|
+
return {
|
|
15
|
+
baseURL: deriveBaseUrl(apiHost, projectId),
|
|
16
|
+
application: {
|
|
17
|
+
name: workspace,
|
|
18
|
+
resource: {
|
|
19
|
+
id: `${projectId}.${dataset}`,
|
|
20
|
+
type: "dataset"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
3
25
|
function buildHeaders(configHeaders, additionalHeaders) {
|
|
4
26
|
return pickBy(
|
|
5
27
|
{
|
|
@@ -49,7 +71,8 @@ async function fetchApi(options) {
|
|
|
49
71
|
method: "POST",
|
|
50
72
|
headers: options.headers,
|
|
51
73
|
body: JSON.stringify(options.body),
|
|
52
|
-
signal: options.signal
|
|
74
|
+
signal: options.signal,
|
|
75
|
+
...options.credentials && { credentials: options.credentials }
|
|
53
76
|
});
|
|
54
77
|
if (!response.ok) {
|
|
55
78
|
const errorText = await response.text();
|
|
@@ -320,6 +343,7 @@ class ContentAgentModel {
|
|
|
320
343
|
),
|
|
321
344
|
body,
|
|
322
345
|
headers: buildHeaders(this.config.headers()),
|
|
346
|
+
credentials: this.config.credentials,
|
|
323
347
|
signal: options.abortSignal,
|
|
324
348
|
fetch: this.config.fetch
|
|
325
349
|
});
|
|
@@ -377,6 +401,7 @@ class ContentAgentModel {
|
|
|
377
401
|
),
|
|
378
402
|
body,
|
|
379
403
|
headers: buildHeaders(this.config.headers()),
|
|
404
|
+
credentials: this.config.credentials,
|
|
380
405
|
signal: options.abortSignal,
|
|
381
406
|
fetch: this.config.fetch
|
|
382
407
|
});
|
|
@@ -394,27 +419,58 @@ function createContentAgent(options) {
|
|
|
394
419
|
const baseURL = options.baseURL?.replace(/\/$/, "") ?? DEFAULT_BASE_URL, apiVersion = options.apiVersion ?? DEFAULT_API_VERSION, { organizationId } = options, getHeaders = () => ({
|
|
395
420
|
...options.token && { Authorization: `Bearer ${options.token}` },
|
|
396
421
|
...options.headers
|
|
397
|
-
}), config = {
|
|
422
|
+
}), credentials = options.token ? void 0 : "include", config = {
|
|
398
423
|
provider: "content-agent",
|
|
399
424
|
baseURL,
|
|
400
425
|
apiVersion,
|
|
401
426
|
organizationId,
|
|
402
427
|
headers: getHeaders,
|
|
428
|
+
credentials,
|
|
403
429
|
fetch: options.fetch
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
|
|
430
|
+
}, agent = (threadId, settings) => new ContentAgentModel(
|
|
431
|
+
threadId,
|
|
432
|
+
{
|
|
433
|
+
...settings,
|
|
434
|
+
application: settings?.application ?? options.application
|
|
435
|
+
},
|
|
436
|
+
config
|
|
437
|
+
);
|
|
438
|
+
let applicationsPromise = null;
|
|
439
|
+
const applications = () => (applicationsPromise || (applicationsPromise = (async () => {
|
|
440
|
+
const fetchFn = options.fetch ?? globalThis.fetch, url = buildApiUrl(baseURL, apiVersion, `/${organizationId}/applications`), headers = buildHeaders(getHeaders()), response = await fetchFn(url, {
|
|
441
|
+
method: "GET",
|
|
442
|
+
headers,
|
|
443
|
+
...credentials && { credentials }
|
|
444
|
+
});
|
|
407
445
|
if (!response.ok) {
|
|
446
|
+
applicationsPromise = null;
|
|
408
447
|
const errorText = await response.text();
|
|
409
448
|
throw new SanityAgentError(response.status, errorText);
|
|
410
449
|
}
|
|
411
450
|
return (await response.json()).applications ?? [];
|
|
451
|
+
})()), applicationsPromise);
|
|
452
|
+
return { agent, applications, resolveApplication: async (client, workspace) => {
|
|
453
|
+
const { projectId, dataset } = client.config();
|
|
454
|
+
if (!projectId || !dataset)
|
|
455
|
+
throw new Error(
|
|
456
|
+
"resolveApplication: client is missing `projectId` or `dataset` in its config"
|
|
457
|
+
);
|
|
458
|
+
const resourceId = `${projectId}.${dataset}`, match = (await applications()).find((app) => app.resource?.id === resourceId && app.name === workspace);
|
|
459
|
+
if (!match)
|
|
460
|
+
throw new SanityAgentError(
|
|
461
|
+
400,
|
|
462
|
+
JSON.stringify({
|
|
463
|
+
code: "APPLICATION_NOT_FOUND",
|
|
464
|
+
message: `No application found for resource "${resourceId}" with workspace "${workspace}". Make sure the Studio has been deployed and belongs to this organization.`
|
|
465
|
+
})
|
|
466
|
+
);
|
|
467
|
+
return match;
|
|
412
468
|
}, prompt: async (settings, generateOptions) => {
|
|
413
|
-
const body = {
|
|
469
|
+
const effectiveApplication = settings.application ?? options.application, body = {
|
|
414
470
|
organizationId,
|
|
415
471
|
message: generateOptions.message,
|
|
416
472
|
stream: generateOptions.stream ?? !1,
|
|
417
|
-
...
|
|
473
|
+
...effectiveApplication && { application: effectiveApplication },
|
|
418
474
|
...settings.config && { config: settings.config },
|
|
419
475
|
...settings.instructions && { instructions: settings.instructions },
|
|
420
476
|
...settings.format && { format: settings.format }
|
|
@@ -422,6 +478,7 @@ function createContentAgent(options) {
|
|
|
422
478
|
url: buildApiUrl(baseURL, apiVersion, "/prompt"),
|
|
423
479
|
body,
|
|
424
480
|
headers: buildHeaders(getHeaders()),
|
|
481
|
+
credentials,
|
|
425
482
|
signal: generateOptions.abortSignal,
|
|
426
483
|
fetch: options.fetch
|
|
427
484
|
});
|
|
@@ -433,6 +490,110 @@ function createContentAgent(options) {
|
|
|
433
490
|
return { text: (await response.json()).text ?? "" };
|
|
434
491
|
} };
|
|
435
492
|
}
|
|
493
|
+
const PROJECTS_API_VERSION = "v2024-01-01", providerCache = /* @__PURE__ */ new Map();
|
|
494
|
+
async function fetchOrganizationId(apiHost, projectId, fetchFn) {
|
|
495
|
+
const url = `${apiHost}/${PROJECTS_API_VERSION}/projects/${projectId}`, response = await fetchFn(url, {
|
|
496
|
+
method: "GET",
|
|
497
|
+
headers: buildHeaders({}),
|
|
498
|
+
credentials: "include"
|
|
499
|
+
});
|
|
500
|
+
if (!response.ok) {
|
|
501
|
+
const errorText = await response.text();
|
|
502
|
+
throw new SanityAgentError(
|
|
503
|
+
response.status,
|
|
504
|
+
JSON.stringify({
|
|
505
|
+
code: "PROJECT_FETCH_FAILED",
|
|
506
|
+
message: `Failed to fetch project "${projectId}": ${errorText}`
|
|
507
|
+
})
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
const project = await response.json();
|
|
511
|
+
if (!project.organizationId)
|
|
512
|
+
throw new SanityAgentError(
|
|
513
|
+
400,
|
|
514
|
+
JSON.stringify({
|
|
515
|
+
code: "NO_ORGANIZATION",
|
|
516
|
+
message: `Project "${projectId}" is not assigned to an organization. Content Agent requires an organization-level project.`
|
|
517
|
+
})
|
|
518
|
+
);
|
|
519
|
+
return project.organizationId;
|
|
520
|
+
}
|
|
521
|
+
function initProvider(projectId, dataset, workspace, apiHost, baseURL, fetchFn, providerOptions) {
|
|
522
|
+
return fetchOrganizationId(apiHost, projectId, fetchFn).then(async (organizationId) => {
|
|
523
|
+
const tempProvider = createContentAgent({
|
|
524
|
+
organizationId,
|
|
525
|
+
baseURL,
|
|
526
|
+
fetch: fetchFn
|
|
527
|
+
}), client = { config: () => ({ projectId, dataset }) }, application = await tempProvider.resolveApplication(client, workspace);
|
|
528
|
+
return createContentAgent({
|
|
529
|
+
...providerOptions,
|
|
530
|
+
organizationId,
|
|
531
|
+
baseURL,
|
|
532
|
+
application
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
class LazyContentAgentModel {
|
|
537
|
+
specificationVersion = "v3";
|
|
538
|
+
provider = "content-agent";
|
|
539
|
+
modelId;
|
|
540
|
+
supportedUrls = {};
|
|
541
|
+
providerPromise;
|
|
542
|
+
threadId;
|
|
543
|
+
settings;
|
|
544
|
+
resolvedModel = null;
|
|
545
|
+
constructor(threadId, settings, providerPromise) {
|
|
546
|
+
this.threadId = threadId, this.settings = settings, this.modelId = `content-agent:${threadId}`, this.providerPromise = providerPromise;
|
|
547
|
+
}
|
|
548
|
+
async getModel() {
|
|
549
|
+
if (!this.resolvedModel) {
|
|
550
|
+
const provider = await this.providerPromise;
|
|
551
|
+
this.resolvedModel = provider.agent(this.threadId, this.settings);
|
|
552
|
+
}
|
|
553
|
+
return this.resolvedModel;
|
|
554
|
+
}
|
|
555
|
+
async doGenerate(options) {
|
|
556
|
+
return (await this.getModel()).doGenerate(options);
|
|
557
|
+
}
|
|
558
|
+
async doStream(options) {
|
|
559
|
+
return (await this.getModel()).doStream(options);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
function createStudioAgent(client, workspace, options) {
|
|
563
|
+
const { projectId, dataset } = client.config();
|
|
564
|
+
if (!projectId)
|
|
565
|
+
throw new Error("createStudioAgent: client is missing `projectId` in its config");
|
|
566
|
+
if (!dataset)
|
|
567
|
+
throw new Error("createStudioAgent: client is missing `dataset` in its config");
|
|
568
|
+
const { apiHost } = client.config(), fetchFn = options?.fetch ?? globalThis.fetch, baseURL = deriveBaseUrl(apiHost, projectId), cacheKey = `${projectId}.${dataset}.${workspace}`, useCache = !options?.fetch && !options?.providerOptions;
|
|
569
|
+
let providerPromise;
|
|
570
|
+
useCache && providerCache.has(cacheKey) ? providerPromise = providerCache.get(cacheKey) : (providerPromise = initProvider(
|
|
571
|
+
projectId,
|
|
572
|
+
dataset,
|
|
573
|
+
workspace,
|
|
574
|
+
apiHost || "https://api.sanity.io",
|
|
575
|
+
baseURL,
|
|
576
|
+
fetchFn,
|
|
577
|
+
options?.providerOptions
|
|
578
|
+
), useCache && (providerCache.set(cacheKey, providerPromise), providerPromise.catch(() => {
|
|
579
|
+
providerCache.delete(cacheKey);
|
|
580
|
+
})));
|
|
581
|
+
const awaitProvider = async (fn) => fn(await providerPromise);
|
|
582
|
+
return {
|
|
583
|
+
agent(threadId, settings) {
|
|
584
|
+
return new LazyContentAgentModel(threadId, settings, providerPromise);
|
|
585
|
+
},
|
|
586
|
+
applications() {
|
|
587
|
+
return awaitProvider((p) => p.applications());
|
|
588
|
+
},
|
|
589
|
+
resolveApplication(resolveClient, resolveWorkspace) {
|
|
590
|
+
return awaitProvider((p) => p.resolveApplication(resolveClient, resolveWorkspace));
|
|
591
|
+
},
|
|
592
|
+
prompt(settings, generateOptions) {
|
|
593
|
+
return awaitProvider((p) => p.prompt(settings, generateOptions));
|
|
594
|
+
}
|
|
595
|
+
};
|
|
596
|
+
}
|
|
436
597
|
export {
|
|
437
598
|
ContentAgentModel,
|
|
438
599
|
GENERIC_ERROR_MESSAGE,
|
|
@@ -442,9 +603,12 @@ export {
|
|
|
442
603
|
classifyAgentError,
|
|
443
604
|
createContentAgent,
|
|
444
605
|
createSSEStream,
|
|
606
|
+
createStudioAgent,
|
|
607
|
+
deriveBaseUrl,
|
|
445
608
|
extractTextFromParts,
|
|
446
609
|
extractTextFromStream,
|
|
447
610
|
fetchApi,
|
|
611
|
+
fromClient,
|
|
448
612
|
getAgentErrorMessage,
|
|
449
613
|
mapFinishReason,
|
|
450
614
|
parseStreamLine
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/utils.ts","../src/sanity-agent-language-model.ts","../src/sanity-agent-provider.ts"],"sourcesContent":["import type {\n LanguageModelV3FinishReason,\n LanguageModelV3StreamPart,\n LanguageModelV3Usage,\n SharedV3Warning,\n} from '@ai-sdk/provider'\nimport { ERROR_CODES, type HeadlessErrorResponse } from '@sanity/agent-types'\nimport { isString, pickBy } from 'es-toolkit'\n\n/**\n * Builds headers for API requests, filtering out undefined values.\n *\n * @category Utilities\n */\nexport function buildHeaders(\n configHeaders: Record<string, string | undefined>,\n additionalHeaders?: Record<string, string>,\n): Record<string, string> {\n return pickBy(\n {\n 'Content-Type': 'application/json',\n ...additionalHeaders,\n ...configHeaders,\n },\n isString,\n )\n}\n\n/**\n * Constructs the full API URL for an endpoint.\n *\n * @category Utilities\n */\nexport function buildApiUrl(baseURL: string, apiVersion: string, endpoint: string): string {\n return `${baseURL}/${apiVersion}/agent${endpoint}`\n}\n\n/**\n * Custom error class for Sanity Agent API errors.\n * The {@link body} is parsed from the JSON response and follows the\n * {@link HeadlessErrorResponse} shape defined in `@sanity/agent-types`.\n *\n * @category Utilities\n */\nexport class SanityAgentError extends Error {\n readonly status: number\n readonly isRetryable: boolean\n readonly body: Partial<HeadlessErrorResponse>\n\n constructor(status: number, message: string) {\n super(`Sanity Agent API error: ${status} ${message}`)\n this.name = 'SanityAgentError'\n this.status = status\n this.isRetryable = status >= 500\n this.body = SanityAgentError.parseBody(message)\n }\n\n private static parseBody(message: string): Partial<HeadlessErrorResponse> {\n try {\n return JSON.parse(message)\n } catch {\n return {}\n }\n }\n}\n\nexport const GENERIC_ERROR_MESSAGE = 'Sorry, something went wrong. Please try again.'\n\nexport type AgentErrorType =\n | { type: 'no_applications' }\n | { type: 'application_not_found' }\n | { type: 'unknown' }\n\n/**\n * Classifies a caught error from the Content Agent API into a known error type.\n * Useful for clients that need to take different actions per error (e.g. show\n * resource-selection buttons). For the user-facing message, use {@link getAgentErrorMessage}.\n *\n * @category Utilities\n */\nexport function classifyAgentError(error: unknown): AgentErrorType {\n if (!(error instanceof SanityAgentError) || error.status !== 400) {\n return { type: 'unknown' }\n }\n\n switch (error.body.code) {\n case ERROR_CODES.NO_COMPATIBLE_APPLICATIONS:\n return { type: 'no_applications' }\n case ERROR_CODES.APPLICATION_NOT_FOUND:\n return { type: 'application_not_found' }\n default:\n return { type: 'unknown' }\n }\n}\n\n/**\n * Extracts a user-facing error message from an agent API error.\n * Returns the message sent by the API when available, otherwise a generic fallback.\n *\n * @category Utilities\n */\nexport function getAgentErrorMessage(error: unknown): string {\n if (error instanceof SanityAgentError && error.body.message) {\n return error.body.message\n }\n return GENERIC_ERROR_MESSAGE\n}\n\nexport interface FetchOptions {\n url: string\n body: unknown\n headers: Record<string, string>\n signal?: AbortSignal\n fetch?: typeof globalThis.fetch\n}\n\n/**\n * Makes a POST request and returns the response.\n * Throws SanityAgentError if the response is not ok.\n *\n * @category Utilities\n */\nexport async function fetchApi(options: FetchOptions): Promise<Response> {\n const fetchFn = options.fetch ?? globalThis.fetch\n\n const response = await fetchFn(options.url, {\n method: 'POST',\n headers: options.headers,\n body: JSON.stringify(options.body),\n signal: options.signal,\n })\n\n if (!response.ok) {\n const errorText = await response.text()\n throw new SanityAgentError(response.status, errorText)\n }\n\n return response\n}\n\n/**\n * Extracts text content from message parts (user or assistant).\n *\n * @category Utilities\n */\nexport function extractTextFromParts(\n content: string | Array<{ type: string; text?: string }>,\n): string {\n if (typeof content === 'string') {\n return content\n }\n\n return content\n .filter(\n (part): part is { type: 'text'; text: string } =>\n part.type === 'text' && typeof part.text === 'string',\n )\n .map((part) => part.text)\n .join('\\n')\n}\n\n/**\n * Creates an empty V3 usage object. Our API does not return token counts.\n *\n * @category Utilities\n */\nexport function createEmptyUsage(): LanguageModelV3Usage {\n return {\n inputTokens: {\n total: 0,\n noCache: undefined,\n cacheRead: undefined,\n cacheWrite: undefined,\n },\n outputTokens: {\n total: 0,\n text: undefined,\n reasoning: undefined,\n },\n }\n}\n\n/**\n * Maps API finish reasons to LanguageModelV3FinishReason.\n *\n * @category Utilities\n */\nexport function mapFinishReason(reason: string | undefined): LanguageModelV3FinishReason {\n const unified = (() => {\n switch (reason) {\n case 'stop':\n case 'length':\n case 'content-filter':\n case 'tool-calls':\n case 'error':\n return reason\n default:\n return 'other' as const\n }\n })()\n\n return { unified, raw: reason }\n}\n\nexport type ParsedStreamLine =\n | { kind: 'part'; part: LanguageModelV3StreamPart }\n | { kind: 'finish'; finishReason: LanguageModelV3FinishReason }\n | { kind: 'data'; type: string; data: unknown }\n\n/**\n * Parses a single SSE line from the AI SDK v6 UI Message Stream protocol.\n * Returns null if the line cannot be parsed or should be ignored.\n *\n * Format: `data: <json>\\n`\n *\n * Supported types:\n * text-start – Text block start (forwarded as-is)\n * text-delta – Text chunk\n * text-end – Text block end (forwarded as-is)\n * tool-input-available – Tool call (client tools only, without providerExecuted)\n * tool-output-available – Tool result (informational, skipped)\n * finish – Finish event with reason\n * error – Error event\n * start – Stream start (skipped)\n * [DONE] – Stream termination (skipped)\n *\n * @see https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol\n *\n * @param line A single line from the SSE byte stream.\n * @param textContentId Fallback ID for text parts that lack an explicit id.\n *\n * @category Utilities\n */\nexport function parseStreamLine(line: string, textContentId: string): ParsedStreamLine | null {\n const trimmed = line.trim()\n if (!trimmed.startsWith('data: ')) return null\n\n const payload = trimmed.slice(6)\n if (payload === '[DONE]') return null\n\n let part: Record<string, unknown>\n try {\n part = JSON.parse(payload) as Record<string, unknown>\n } catch {\n return null\n }\n\n const type = part.type as string | undefined\n if (!type) return null\n\n switch (type) {\n case 'text-start': {\n const id = (part.id as string) || textContentId\n return { kind: 'part', part: { type: 'text-start', id } }\n }\n\n case 'text-delta': {\n const id = (part.id as string) || textContentId\n const delta = part.delta as string\n if (typeof delta !== 'string') return null\n return { kind: 'part', part: { type: 'text-delta', id, delta } }\n }\n\n case 'text-end': {\n const id = (part.id as string) || textContentId\n return { kind: 'part', part: { type: 'text-end', id } }\n }\n\n case 'tool-input-available': {\n // Skip server-executed tools — only forward client tools for local execution.\n if (part.providerExecuted) return null\n\n const input = part.input\n return {\n kind: 'part',\n part: {\n type: 'tool-call',\n toolCallId: part.toolCallId as string,\n toolName: part.toolName as string,\n input: typeof input === 'string' ? input : JSON.stringify(input),\n },\n }\n }\n\n case 'error': {\n return {\n kind: 'part',\n part: {\n type: 'error',\n error: new Error((part.errorText as string) ?? 'Unknown error'),\n },\n }\n }\n\n case 'abort': {\n return { kind: 'data', type: 'abort', data: { reason: part.reason } }\n }\n\n case 'finish': {\n return {\n kind: 'finish',\n finishReason: mapFinishReason(part.finishReason as string | undefined),\n }\n }\n\n default: {\n if (type.startsWith('data-')) {\n return { kind: 'data', type: type.slice(5), data: part.data }\n }\n return null\n }\n }\n}\n\n/**\n * Extracts accumulated text from a streaming response body.\n * Used for simple streaming cases where only the final text is needed.\n *\n * @category Utilities\n */\nexport async function extractTextFromStream(\n responseBody: ReadableStream<Uint8Array>,\n): Promise<string> {\n const reader = responseBody.getReader()\n const decoder = new TextDecoder()\n const textContentId = 'extract'\n let text = ''\n\n while (true) {\n const { done, value } = await reader.read()\n if (done) break\n\n const chunk = decoder.decode(value, { stream: true })\n for (const line of chunk.split('\\n')) {\n const parsed = parseStreamLine(line, textContentId)\n if (parsed?.kind === 'part' && parsed.part.type === 'text-delta') {\n text += parsed.part.delta\n }\n }\n }\n\n return text\n}\n\n/**\n * Creates a ReadableStream that parses AI SDK v6 UI Message Stream SSE data\n * from a response body and emits LanguageModelV3 stream parts.\n *\n * Text lifecycle events (text-start / text-end) are forwarded from the server.\n * Tool calls are tracked to ensure the finish reason is set correctly.\n *\n * @category Utilities\n */\nexport function createSSEStream(\n responseBody: ReadableStream<Uint8Array>,\n warnings: SharedV3Warning[],\n onData?: (type: string, data: unknown) => void,\n): ReadableStream<LanguageModelV3StreamPart> {\n let finishReason: LanguageModelV3FinishReason = { unified: 'other', raw: undefined }\n const fallbackTextId = crypto.randomUUID()\n let hasToolCalls = false\n\n return new ReadableStream<LanguageModelV3StreamPart>({\n async start(controller) {\n const reader = responseBody.getReader()\n const decoder = new TextDecoder()\n let buffer = ''\n\n controller.enqueue({ type: 'stream-start', warnings })\n\n try {\n while (true) {\n const { done, value } = await reader.read()\n if (done) break\n\n buffer += decoder.decode(value, { stream: true })\n const lines = buffer.split('\\n')\n buffer = lines.pop() ?? ''\n\n for (const line of lines) {\n const parsed = parseStreamLine(line, fallbackTextId)\n if (parsed?.kind === 'part') {\n if (parsed.part.type === 'tool-call') {\n hasToolCalls = true\n }\n controller.enqueue(parsed.part)\n } else if (parsed?.kind === 'finish') {\n finishReason = parsed.finishReason\n } else if (parsed?.kind === 'data') {\n onData?.(parsed.type, parsed.data)\n }\n }\n }\n\n if (buffer.trim()) {\n const parsed = parseStreamLine(buffer, fallbackTextId)\n if (parsed?.kind === 'part') {\n if (parsed.part.type === 'tool-call') {\n hasToolCalls = true\n }\n controller.enqueue(parsed.part)\n } else if (parsed?.kind === 'finish') {\n finishReason = parsed.finishReason\n } else if (parsed?.kind === 'data') {\n onData?.(parsed.type, parsed.data)\n }\n }\n\n // If tool calls were emitted, override finish reason so streamText\n // knows to execute tools and continue with the next step.\n if (hasToolCalls && finishReason.unified !== 'tool-calls') {\n finishReason = { unified: 'tool-calls', raw: finishReason.raw ?? 'tool-calls' }\n }\n\n controller.enqueue({\n type: 'finish',\n finishReason,\n usage: createEmptyUsage(),\n })\n } catch (error) {\n controller.enqueue({\n type: 'error',\n error: error instanceof Error ? error : new Error(String(error)),\n })\n } finally {\n controller.close()\n }\n },\n })\n}\n","import type {\n LanguageModelV3,\n LanguageModelV3CallOptions,\n LanguageModelV3Content,\n LanguageModelV3GenerateResult,\n LanguageModelV3Prompt,\n LanguageModelV3StreamResult,\n SharedV3Warning,\n} from '@ai-sdk/provider'\nimport type { ClientToolDefinition } from '@sanity/agent-types'\nimport { groupBy } from 'es-toolkit'\nimport type { AgentSettings, Message } from './types'\nimport {\n buildApiUrl,\n buildHeaders,\n createEmptyUsage,\n createSSEStream,\n extractTextFromParts,\n fetchApi,\n parseStreamLine,\n} from './utils'\n\n/**\n * @category Model\n */\nexport interface ContentAgentModelConfig {\n provider: string\n baseURL: string\n apiVersion: string\n organizationId: string\n headers: () => Record<string, string | undefined>\n fetch?: typeof globalThis.fetch\n}\n\n/**\n * Extended message type for the wire format that supports tool calls and tool results\n * in addition to plain text messages.\n */\ntype WireMessage =\n | Message\n | {\n role: 'assistant'\n content: Array<\n | { type: 'text'; text: string }\n | { type: 'tool-call'; toolCallId: string; toolName: string; input: string }\n >\n }\n | {\n role: 'tool'\n content: Array<{\n type: 'tool-result'\n toolCallId: string\n toolName: string\n output: unknown\n }>\n }\n\nconst UNSUPPORTED_SETTINGS = [\n 'maxOutputTokens',\n 'temperature',\n 'topP',\n 'topK',\n 'frequencyPenalty',\n 'presencePenalty',\n 'seed',\n] as const\n\n/**\n * Vercel AI SDK `LanguageModelV1` implementation that communicates with the\n * Sanity Content Agent API via a conversation thread.\n *\n * Typically you don't construct this directly — use\n * {@link createContentAgent | createContentAgent().agent(threadId)} instead.\n *\n * @category Model\n *\n * @example\n * ```ts\n * import { createContentAgent } from 'content-agent'\n * import { streamText } from 'ai'\n *\n * const contentAgent = createContentAgent({\n * organizationId: 'your-org-id',\n * token: 'your-sanity-token',\n * })\n *\n * const model = contentAgent.agent('my-thread-id', {\n * application: { key: 'projectId.datasetName' },\n * config: {\n * capabilities: { read: true, write: false },\n * },\n * })\n *\n * const { textStream } = await streamText({ model, prompt: 'Summarize my content' })\n * for await (const chunk of textStream) {\n * process.stdout.write(chunk)\n * }\n * ```\n */\nexport class ContentAgentModel implements LanguageModelV3 {\n readonly specificationVersion = 'v3' as const\n readonly provider: string\n readonly modelId: string\n readonly supportedUrls: Record<string, RegExp[]> = {}\n\n private readonly config: ContentAgentModelConfig\n private readonly threadId: string\n private readonly settings: AgentSettings\n\n constructor(\n threadId: string,\n settings: AgentSettings | undefined,\n config: ContentAgentModelConfig,\n ) {\n this.threadId = threadId\n this.modelId = `content-agent:${threadId}`\n this.provider = config.provider\n this.settings = settings ?? {}\n this.config = config\n }\n\n /**\n * Converts a LanguageModelV3Prompt into wire messages for the agent API.\n *\n * Handles all message roles:\n * - system: plain text system messages\n * - user: extracts text from content parts\n * - assistant: extracts text and/or tool-call parts\n * - tool: converts tool-result parts for the API\n */\n private convertPromptToMessages(prompt: LanguageModelV3Prompt): WireMessage[] {\n const messages: WireMessage[] = []\n\n for (const message of prompt) {\n if (message.role === 'system') {\n if (message.content) {\n messages.push({ role: 'system', content: message.content })\n }\n continue\n }\n\n if (message.role === 'tool') {\n const toolResults = message.content\n .filter((part) => part.type === 'tool-result')\n .map((part) => {\n const p = part as {\n toolCallId: string\n toolName: string\n output: { type: string; value: unknown } | unknown\n }\n return {\n type: 'tool-result' as const,\n toolCallId: p.toolCallId,\n toolName: p.toolName,\n output:\n p.output && typeof p.output === 'object' && 'value' in p.output\n ? (p.output as { value: unknown }).value\n : p.output,\n }\n })\n\n if (toolResults.length > 0) {\n messages.push({ role: 'tool', content: toolResults })\n }\n continue\n }\n\n if (message.role === 'assistant') {\n const content = message.content as Array<{\n type: string\n text?: string\n toolCallId?: string\n toolName?: string\n input?: string\n }>\n\n const grouped = groupBy(content, (p) => p.type)\n const toolCalls = grouped['tool-call'] ?? []\n const textParts = grouped.text ?? []\n\n if (toolCalls.length > 0) {\n const parts = [\n ...textParts\n .filter((p) => p.text)\n .map((p) => ({ type: 'text' as const, text: p.text! })),\n ...toolCalls.map((tc) => ({\n type: 'tool-call' as const,\n toolCallId: tc.toolCallId!,\n toolName: tc.toolName!,\n input: tc.input!,\n })),\n ]\n\n if (parts.length > 0) {\n messages.push({ role: 'assistant', content: parts })\n }\n } else {\n const text = extractTextFromParts(content as Array<{ type: string; text?: string }>)\n if (text) {\n messages.push({ role: 'assistant', content: text })\n }\n }\n continue\n }\n\n // user messages\n const text = extractTextFromParts(message.content as Array<{ type: string; text?: string }>)\n if (text) {\n messages.push({ role: message.role, content: text })\n }\n }\n\n return messages\n }\n\n /**\n * Extracts client tool definitions from the model call options.\n * Only the schemas are sent — execute functions stay on the client.\n */\n private extractClientTools(\n options: LanguageModelV3CallOptions,\n ): ClientToolDefinition[] | undefined {\n if (!options.tools?.length) return undefined\n\n const clientTools = options.tools\n .filter((t) => t.type === 'function')\n .map((t) => ({\n type: 'function' as const,\n name: t.name,\n description: t.description,\n inputSchema: t.inputSchema as Record<string, unknown>,\n }))\n\n return clientTools.length > 0 ? clientTools : undefined\n }\n\n private collectWarnings(options: LanguageModelV3CallOptions): SharedV3Warning[] {\n const warnings: SharedV3Warning[] = []\n\n for (const setting of UNSUPPORTED_SETTINGS) {\n if (options[setting] !== undefined) {\n warnings.push({ type: 'unsupported', feature: setting })\n }\n }\n\n if (options.stopSequences?.length) {\n warnings.push({ type: 'unsupported', feature: 'stopSequences' })\n }\n\n if (options.responseFormat && options.responseFormat.type !== 'text') {\n warnings.push({ type: 'unsupported', feature: 'responseFormat' })\n }\n\n return warnings\n }\n\n private buildRequestBody(options: LanguageModelV3CallOptions, stream: boolean) {\n const clientTools = this.extractClientTools(options)\n const toolChoice = options.toolChoice\n\n return {\n messages: this.convertPromptToMessages(options.prompt),\n stream,\n ...(clientTools && { clientTools }),\n ...(toolChoice && { toolChoice }),\n ...(this.settings.application && { application: this.settings.application }),\n ...(this.settings.config && { config: this.settings.config }),\n ...(this.settings.format && { format: this.settings.format }),\n }\n }\n\n private getResponseHeaders(response: Response): Record<string, string> {\n return Object.fromEntries(response.headers.entries())\n }\n\n async doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult> {\n // Always request a stream from the backend and buffer the result.\n // This keeps the HTTP connection alive with incremental data, avoiding\n // 504 gateway timeouts on long-running agent requests.\n const body = this.buildRequestBody(options, true)\n const warnings = this.collectWarnings(options)\n\n const response = await fetchApi({\n url: buildApiUrl(\n this.config.baseURL,\n this.config.apiVersion,\n `/${this.config.organizationId}/threads/${this.threadId}`,\n ),\n body,\n headers: buildHeaders(this.config.headers()),\n signal: options.abortSignal,\n fetch: this.config.fetch,\n })\n\n if (!response.body) {\n throw new Error('No response body for generate request')\n }\n\n const { content, finishReason } = await this.extractContentFromStream(response.body)\n\n return {\n content,\n finishReason,\n usage: createEmptyUsage(),\n request: { body },\n response: { headers: this.getResponseHeaders(response) },\n warnings,\n }\n }\n\n /**\n * Parses a streamed response body into content parts (text and tool calls)\n * and a finish reason. Used by doGenerate to buffer the full response.\n */\n private async extractContentFromStream(responseBody: ReadableStream<Uint8Array>): Promise<{\n content: LanguageModelV3Content[]\n finishReason: LanguageModelV3GenerateResult['finishReason']\n }> {\n const reader = responseBody.getReader()\n const decoder = new TextDecoder()\n const textContentId = 'generate'\n let text = ''\n let hasToolCalls = false\n const content: LanguageModelV3Content[] = []\n\n while (true) {\n const { done, value } = await reader.read()\n if (done) break\n\n const chunk = decoder.decode(value, { stream: true })\n for (const line of chunk.split('\\n')) {\n const parsed = parseStreamLine(line, textContentId)\n if (parsed?.kind === 'part') {\n if (parsed.part.type === 'text-delta') {\n text += parsed.part.delta\n } else if (parsed.part.type === 'tool-call') {\n hasToolCalls = true\n const tc = parsed.part as { toolCallId: string; toolName: string; input: string }\n content.push({\n type: 'tool-call',\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n input: tc.input,\n })\n }\n }\n }\n }\n\n if (text) {\n content.unshift({ type: 'text', text })\n }\n\n const finishReason = hasToolCalls\n ? { unified: 'tool-calls' as const, raw: 'tool-calls' }\n : { unified: 'stop' as const, raw: undefined }\n\n return { content, finishReason }\n }\n\n async doStream(options: LanguageModelV3CallOptions): Promise<LanguageModelV3StreamResult> {\n const body = this.buildRequestBody(options, true)\n const warnings = this.collectWarnings(options)\n\n const response = await fetchApi({\n url: buildApiUrl(\n this.config.baseURL,\n this.config.apiVersion,\n `/${this.config.organizationId}/threads/${this.threadId}`,\n ),\n body,\n headers: buildHeaders(this.config.headers()),\n signal: options.abortSignal,\n fetch: this.config.fetch,\n })\n\n if (!response.body) {\n throw new Error('No response body for streaming request')\n }\n\n return {\n stream: createSSEStream(response.body, warnings, this.settings.onStreamData),\n request: { body },\n response: { headers: this.getResponseHeaders(response) },\n }\n }\n}\n","import type { LanguageModelV3 } from '@ai-sdk/provider'\nimport { ContentAgentModel, type ContentAgentModelConfig } from './sanity-agent-language-model'\nimport type {\n AgentSettings,\n Application,\n ContentAgentProviderSettings,\n GenerateOptions,\n GenerateResult,\n GenerateSettings,\n} from './types'\nimport {\n buildApiUrl,\n buildHeaders,\n extractTextFromStream,\n fetchApi,\n SanityAgentError,\n} from './utils'\n\nconst DEFAULT_BASE_URL = 'https://api.sanity.io'\nconst DEFAULT_API_VERSION = 'v2026-01-01'\n\n/**\n * A Content Agent provider instance returned by {@link createContentAgent}.\n *\n * Provides two modes of interaction:\n * - **Conversation threads** via {@link ContentAgentProvider.agent | .agent()} — stateful, multi-turn conversations with server-side message history.\n * - **One-shot prompts** via {@link ContentAgentProvider.prompt | .prompt()} — stateless, single-turn interactions.\n *\n * @category Provider\n */\nexport interface ContentAgentProvider {\n /**\n * Creates a Vercel AI SDK {@link https://sdk.vercel.ai/docs/ai-sdk-core | LanguageModelV1} for a conversation thread.\n *\n * The thread ID identifies the conversation and maintains message history server-side.\n * If a thread with that ID already exists, the conversation continues. If not, a new\n * thread is automatically created.\n *\n * Use the returned model with `generateText` or `streamText` from the `ai` package.\n *\n * @example\n * ```ts\n * import { generateText, streamText } from 'ai'\n *\n * const model = contentAgent.agent('my-thread-id', {\n * application: { key: 'projectId.datasetName' },\n * config: {\n * capabilities: { read: true, write: false },\n * },\n * })\n *\n * // Non-streaming\n * const { text } = await generateText({ model, prompt: 'What blog posts do I have?' })\n *\n * // Streaming\n * const { textStream } = await streamText({ model, prompt: 'Summarize my content' })\n * for await (const chunk of textStream) {\n * process.stdout.write(chunk)\n * }\n * ```\n *\n * @param threadId - Unique identifier for the conversation thread.\n * @param settings - Optional agent settings (application, capabilities, filters, etc.).\n */\n agent(threadId: string, settings?: AgentSettings): LanguageModelV3\n\n /**\n * Lists available applications for the organization.\n *\n * Returns all Sanity Studio applications the authenticated user has access to.\n * Use the `key` from a returned application to target it when creating an agent.\n *\n * @example\n * ```ts\n * const apps = await contentAgent.applications()\n * const app = apps.find((app) => app.title === \"My Studio\")\n *\n * const model = contentAgent.agent(\"my-thread\", {\n * application: { key: app.key },\n * })\n * ```\n */\n applications(): Promise<Application[]>\n\n /**\n * Sends a one-shot prompt to the Content Agent (stateless).\n *\n * Unlike {@link ContentAgentProvider.agent | .agent()}, this does not create or use a conversation thread.\n * Each call is independent with no message history or persistence.\n * Ideal for simple, single-turn interactions.\n *\n * @example\n * ```ts\n * const { text } = await contentAgent.prompt(\n * {\n * application: { key: 'projectId.datasetName' },\n * config: { capabilities: { read: true, write: false } },\n * instructions: 'Be concise',\n * },\n * { message: 'List my 5 most recent posts' },\n * )\n * ```\n *\n * @param settings - Generation settings (application, capabilities, instructions, etc.).\n * @param options - The prompt message and request options.\n */\n prompt(settings: GenerateSettings, options: GenerateOptions): Promise<GenerateResult>\n}\n\n/**\n * Creates a Content Agent provider instance.\n *\n * The provider is the main entry point for interacting with the Sanity Content Agent API.\n * It returns an object with methods for conversation threads and one-shot prompts.\n *\n * @category Provider\n *\n * @example Basic setup\n * ```ts\n * import { createContentAgent } from 'content-agent'\n *\n * const contentAgent = createContentAgent({\n * organizationId: 'your-org-id',\n * token: 'your-sanity-token',\n * })\n * ```\n *\n * @example Using with Vercel AI SDK for conversation threads\n * ```ts\n * import { createContentAgent } from 'content-agent'\n * import { generateText, streamText } from 'ai'\n *\n * const contentAgent = createContentAgent({\n * organizationId: 'your-org-id',\n * token: 'your-sanity-token',\n * })\n *\n * const model = contentAgent.agent('my-thread-id', {\n * application: { key: 'projectId.datasetName' },\n * config: {\n * capabilities: { read: true, write: false },\n * },\n * })\n *\n * // Non-streaming\n * const { text } = await generateText({ model, prompt: 'What blog posts do I have?' })\n *\n * // Streaming\n * const { textStream } = await streamText({ model, prompt: 'Summarize my content' })\n * for await (const chunk of textStream) {\n * process.stdout.write(chunk)\n * }\n * ```\n *\n * @example One-shot generation (stateless, no thread)\n * ```ts\n * const { text } = await contentAgent.prompt(\n * {\n * application: { key: 'projectId.datasetName' },\n * config: { capabilities: { read: true, write: false } },\n * instructions: 'Be concise',\n * },\n * { message: 'List my 5 most recent posts' },\n * )\n * ```\n *\n * @param options - Provider configuration including organization ID, token, and optional overrides.\n */\nexport function createContentAgent(options: ContentAgentProviderSettings): ContentAgentProvider {\n const baseURL = options.baseURL?.replace(/\\/$/, '') ?? DEFAULT_BASE_URL\n const apiVersion = options.apiVersion ?? DEFAULT_API_VERSION\n const { organizationId } = options\n\n const getHeaders = (): Record<string, string | undefined> => ({\n ...(options.token && { Authorization: `Bearer ${options.token}` }),\n ...options.headers,\n })\n\n const config: ContentAgentModelConfig = {\n provider: 'content-agent',\n baseURL,\n apiVersion,\n organizationId,\n headers: getHeaders,\n fetch: options.fetch,\n }\n\n const agent = (threadId: string, settings?: AgentSettings): LanguageModelV3 => {\n return new ContentAgentModel(threadId, settings, config)\n }\n\n const applications = async (): Promise<Application[]> => {\n const fetchFn = options.fetch ?? globalThis.fetch\n const url = buildApiUrl(baseURL, apiVersion, `/${organizationId}/applications`)\n const headers = buildHeaders(getHeaders())\n\n const response = await fetchFn(url, { method: 'GET', headers })\n\n if (!response.ok) {\n const errorText = await response.text()\n throw new SanityAgentError(response.status, errorText)\n }\n\n const json = (await response.json()) as { applications?: Application[] }\n return json.applications ?? []\n }\n\n const prompt = async (\n settings: GenerateSettings,\n generateOptions: GenerateOptions,\n ): Promise<GenerateResult> => {\n const body = {\n organizationId,\n message: generateOptions.message,\n stream: generateOptions.stream ?? false,\n ...(settings.application && { application: settings.application }),\n ...(settings.config && { config: settings.config }),\n ...(settings.instructions && { instructions: settings.instructions }),\n ...(settings.format && { format: settings.format }),\n }\n\n const response = await fetchApi({\n url: buildApiUrl(baseURL, apiVersion, '/prompt'),\n body,\n headers: buildHeaders(getHeaders()),\n signal: generateOptions.abortSignal,\n fetch: options.fetch,\n })\n\n if (body.stream) {\n if (!response.body) {\n throw new SanityAgentError(500, 'No response body')\n }\n return { text: await extractTextFromStream(response.body) }\n }\n\n const json = (await response.json()) as { text?: string }\n return { text: json.text ?? '' }\n }\n\n return { agent, applications, prompt }\n}\n"],"names":["text"],"mappings":";;AAcO,SAAS,aACd,eACA,mBACwB;AACxB,SAAO;AAAA,IACL;AAAA,MACE,gBAAgB;AAAA,MAChB,GAAG;AAAA,MACH,GAAG;AAAA,IAAA;AAAA,IAEL;AAAA,EAAA;AAEJ;AAOO,SAAS,YAAY,SAAiB,YAAoB,UAA0B;AACzF,SAAO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I,EAAA;AAAA,IAC1D;AAGA,WAAO,EAAE,OADK,MAAM,SAAS,KAAA,GACT,QAAQ,GAAA;AAAA,EAC9B,EAAA;AAGF;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/from-client.ts","../src/utils.ts","../src/sanity-agent-language-model.ts","../src/sanity-agent-provider.ts","../src/studio.ts"],"sourcesContent":["import type { Application } from './types'\n\nconst DEFAULT_API_HOST = 'https://api.sanity.io'\n\n/**\n * Minimal interface for a Sanity client. Avoids a hard dependency on `@sanity/client` —\n * any object whose `config()` returns `projectId`, `dataset`, and `apiHost` will work.\n *\n * @category Studio Integration\n */\nexport type SanityClientLike = {\n config(): { projectId?: string; dataset?: string; apiHost?: string }\n}\n\n/**\n * Configuration derived from a Sanity client for use with {@link createContentAgent}.\n *\n * @category Studio Integration\n */\nexport type ClientDerivedConfig = {\n /**\n * Project-scoped API base URL derived from the client's `apiHost`.\n *\n * Using a project-scoped host avoids CORS preflight issues when making\n * cookie-authenticated requests from a Studio context.\n */\n baseURL: string\n\n /**\n * Application targeting the client's project, dataset, and workspace.\n *\n * Identified by `resource.id` (`projectId.dataset`) and `name` (workspace name),\n * which matches the format the Content Agent API uses internally.\n */\n application: Application\n}\n\n/**\n * Derives a project-scoped base URL from the client's configured API host.\n * Inserts `projectId` as a subdomain so cookie-authenticated requests\n * from Studio pass CORS checks.\n *\n * `https://api.sanity.io` → `https://{projectId}.api.sanity.io`\n * `https://api.sanity.work` → `https://{projectId}.api.sanity.work`\n *\n * @category Studio Integration\n */\nexport function deriveBaseUrl(apiHost: string | undefined, projectId: string): string {\n const url = new URL(apiHost || DEFAULT_API_HOST)\n url.hostname = `${projectId}.${url.hostname}`\n return url.origin\n}\n\n/**\n * Derives Content Agent configuration from a Sanity client and workspace name.\n *\n * Extracts `projectId`, `dataset`, and `apiHost` to build a CORS-compatible\n * `baseURL` and an `application` that targets the correct workspace — no need\n * to know the Studio deployment ID or application key.\n *\n * Designed for Studio / browser contexts where authentication is handled\n * via cookies. Omit the `token` when creating the provider and credentials\n * will be included automatically.\n *\n * @category Studio Integration\n *\n * @example Studio AI Assist action\n * ```ts\n * import { createContentAgent, fromClient } from 'content-agent'\n * import { useClient, useWorkspace } from 'sanity'\n *\n * const client = useClient({ apiVersion: '2024-01-01' })\n * const workspace = useWorkspace()\n * const { baseURL } = fromClient(client, workspace.name)\n *\n * const contentAgent = createContentAgent({\n * organizationId: 'your-org-id',\n * baseURL,\n * })\n *\n * const application = await contentAgent.resolveApplication(client, workspace.name)\n * const model = contentAgent.agent('thread-id', { application })\n * ```\n *\n * @param client - Any object with a `config()` method returning `projectId`, `dataset`, and optionally `apiHost`.\n * @param workspace - The active workspace name (e.g. `'default'`). Available via `useWorkspace().name` in Studio.\n */\nexport function fromClient(client: SanityClientLike, workspace: string): ClientDerivedConfig {\n const { projectId, dataset, apiHost } = client.config()\n\n if (!projectId) {\n throw new Error('fromClient: client is missing `projectId` in its config')\n }\n if (!dataset) {\n throw new Error('fromClient: client is missing `dataset` in its config')\n }\n\n return {\n baseURL: deriveBaseUrl(apiHost, projectId),\n application: {\n name: workspace,\n resource: {\n id: `${projectId}.${dataset}`,\n type: 'dataset',\n },\n },\n }\n}\n","import type {\n LanguageModelV3FinishReason,\n LanguageModelV3StreamPart,\n LanguageModelV3Usage,\n SharedV3Warning,\n} from '@ai-sdk/provider'\nimport { ERROR_CODES, type HeadlessErrorResponse } from '@sanity/agent-types'\nimport { isString, pickBy } from 'es-toolkit'\n\n/**\n * Builds headers for API requests, filtering out undefined values.\n *\n * @category Utilities\n */\nexport function buildHeaders(\n configHeaders: Record<string, string | undefined>,\n additionalHeaders?: Record<string, string>,\n): Record<string, string> {\n return pickBy(\n {\n 'Content-Type': 'application/json',\n ...additionalHeaders,\n ...configHeaders,\n },\n isString,\n )\n}\n\n/**\n * Constructs the full API URL for an endpoint.\n *\n * @category Utilities\n */\nexport function buildApiUrl(baseURL: string, apiVersion: string, endpoint: string): string {\n return `${baseURL}/${apiVersion}/agent${endpoint}`\n}\n\n/**\n * Custom error class for Sanity Agent API errors.\n * The {@link body} is parsed from the JSON response and follows the\n * {@link HeadlessErrorResponse} shape defined in `@sanity/agent-types`.\n *\n * @category Utilities\n */\nexport class SanityAgentError extends Error {\n readonly status: number\n readonly isRetryable: boolean\n readonly body: Partial<HeadlessErrorResponse>\n\n constructor(status: number, message: string) {\n super(`Sanity Agent API error: ${status} ${message}`)\n this.name = 'SanityAgentError'\n this.status = status\n this.isRetryable = status >= 500\n this.body = SanityAgentError.parseBody(message)\n }\n\n private static parseBody(message: string): Partial<HeadlessErrorResponse> {\n try {\n return JSON.parse(message)\n } catch {\n return {}\n }\n }\n}\n\nexport const GENERIC_ERROR_MESSAGE = 'Sorry, something went wrong. Please try again.'\n\nexport type AgentErrorType =\n | { type: 'no_applications' }\n | { type: 'application_not_found' }\n | { type: 'unknown' }\n\n/**\n * Classifies a caught error from the Content Agent API into a known error type.\n * Useful for clients that need to take different actions per error (e.g. show\n * resource-selection buttons). For the user-facing message, use {@link getAgentErrorMessage}.\n *\n * @category Utilities\n */\nexport function classifyAgentError(error: unknown): AgentErrorType {\n if (!(error instanceof SanityAgentError) || error.status !== 400) {\n return { type: 'unknown' }\n }\n\n switch (error.body.code) {\n case ERROR_CODES.NO_COMPATIBLE_APPLICATIONS:\n return { type: 'no_applications' }\n case ERROR_CODES.APPLICATION_NOT_FOUND:\n return { type: 'application_not_found' }\n default:\n return { type: 'unknown' }\n }\n}\n\n/**\n * Extracts a user-facing error message from an agent API error.\n * Returns the message sent by the API when available, otherwise a generic fallback.\n *\n * @category Utilities\n */\nexport function getAgentErrorMessage(error: unknown): string {\n if (error instanceof SanityAgentError && error.body.message) {\n return error.body.message\n }\n return GENERIC_ERROR_MESSAGE\n}\n\nexport interface FetchOptions {\n url: string\n body: unknown\n headers: Record<string, string>\n credentials?: RequestCredentials\n signal?: AbortSignal\n fetch?: typeof globalThis.fetch\n}\n\n/**\n * Makes a POST request and returns the response.\n * Throws SanityAgentError if the response is not ok.\n *\n * @category Utilities\n */\nexport async function fetchApi(options: FetchOptions): Promise<Response> {\n const fetchFn = options.fetch ?? globalThis.fetch\n\n const response = await fetchFn(options.url, {\n method: 'POST',\n headers: options.headers,\n body: JSON.stringify(options.body),\n signal: options.signal,\n ...(options.credentials && { credentials: options.credentials }),\n })\n\n if (!response.ok) {\n const errorText = await response.text()\n throw new SanityAgentError(response.status, errorText)\n }\n\n return response\n}\n\n/**\n * Extracts text content from message parts (user or assistant).\n *\n * @category Utilities\n */\nexport function extractTextFromParts(\n content: string | Array<{ type: string; text?: string }>,\n): string {\n if (typeof content === 'string') {\n return content\n }\n\n return content\n .filter(\n (part): part is { type: 'text'; text: string } =>\n part.type === 'text' && typeof part.text === 'string',\n )\n .map((part) => part.text)\n .join('\\n')\n}\n\n/**\n * Creates an empty V3 usage object. Our API does not return token counts.\n *\n * @category Utilities\n */\nexport function createEmptyUsage(): LanguageModelV3Usage {\n return {\n inputTokens: {\n total: 0,\n noCache: undefined,\n cacheRead: undefined,\n cacheWrite: undefined,\n },\n outputTokens: {\n total: 0,\n text: undefined,\n reasoning: undefined,\n },\n }\n}\n\n/**\n * Maps API finish reasons to LanguageModelV3FinishReason.\n *\n * @category Utilities\n */\nexport function mapFinishReason(reason: string | undefined): LanguageModelV3FinishReason {\n const unified = (() => {\n switch (reason) {\n case 'stop':\n case 'length':\n case 'content-filter':\n case 'tool-calls':\n case 'error':\n return reason\n default:\n return 'other' as const\n }\n })()\n\n return { unified, raw: reason }\n}\n\nexport type ParsedStreamLine =\n | { kind: 'part'; part: LanguageModelV3StreamPart }\n | { kind: 'finish'; finishReason: LanguageModelV3FinishReason }\n | { kind: 'data'; type: string; data: unknown }\n\n/**\n * Parses a single SSE line from the AI SDK v6 UI Message Stream protocol.\n * Returns null if the line cannot be parsed or should be ignored.\n *\n * Format: `data: <json>\\n`\n *\n * Supported types:\n * text-start – Text block start (forwarded as-is)\n * text-delta – Text chunk\n * text-end – Text block end (forwarded as-is)\n * tool-input-available – Tool call (client tools only, without providerExecuted)\n * tool-output-available – Tool result (informational, skipped)\n * finish – Finish event with reason\n * error – Error event\n * start – Stream start (skipped)\n * [DONE] – Stream termination (skipped)\n *\n * @see https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol\n *\n * @param line A single line from the SSE byte stream.\n * @param textContentId Fallback ID for text parts that lack an explicit id.\n *\n * @category Utilities\n */\nexport function parseStreamLine(line: string, textContentId: string): ParsedStreamLine | null {\n const trimmed = line.trim()\n if (!trimmed.startsWith('data: ')) return null\n\n const payload = trimmed.slice(6)\n if (payload === '[DONE]') return null\n\n let part: Record<string, unknown>\n try {\n part = JSON.parse(payload) as Record<string, unknown>\n } catch {\n return null\n }\n\n const type = part.type as string | undefined\n if (!type) return null\n\n switch (type) {\n case 'text-start': {\n const id = (part.id as string) || textContentId\n return { kind: 'part', part: { type: 'text-start', id } }\n }\n\n case 'text-delta': {\n const id = (part.id as string) || textContentId\n const delta = part.delta as string\n if (typeof delta !== 'string') return null\n return { kind: 'part', part: { type: 'text-delta', id, delta } }\n }\n\n case 'text-end': {\n const id = (part.id as string) || textContentId\n return { kind: 'part', part: { type: 'text-end', id } }\n }\n\n case 'tool-input-available': {\n // Skip server-executed tools — only forward client tools for local execution.\n if (part.providerExecuted) return null\n\n const input = part.input\n return {\n kind: 'part',\n part: {\n type: 'tool-call',\n toolCallId: part.toolCallId as string,\n toolName: part.toolName as string,\n input: typeof input === 'string' ? input : JSON.stringify(input),\n },\n }\n }\n\n case 'error': {\n return {\n kind: 'part',\n part: {\n type: 'error',\n error: new Error((part.errorText as string) ?? 'Unknown error'),\n },\n }\n }\n\n case 'abort': {\n return { kind: 'data', type: 'abort', data: { reason: part.reason } }\n }\n\n case 'finish': {\n return {\n kind: 'finish',\n finishReason: mapFinishReason(part.finishReason as string | undefined),\n }\n }\n\n default: {\n if (type.startsWith('data-')) {\n return { kind: 'data', type: type.slice(5), data: part.data }\n }\n return null\n }\n }\n}\n\n/**\n * Extracts accumulated text from a streaming response body.\n * Used for simple streaming cases where only the final text is needed.\n *\n * @category Utilities\n */\nexport async function extractTextFromStream(\n responseBody: ReadableStream<Uint8Array>,\n): Promise<string> {\n const reader = responseBody.getReader()\n const decoder = new TextDecoder()\n const textContentId = 'extract'\n let text = ''\n\n while (true) {\n const { done, value } = await reader.read()\n if (done) break\n\n const chunk = decoder.decode(value, { stream: true })\n for (const line of chunk.split('\\n')) {\n const parsed = parseStreamLine(line, textContentId)\n if (parsed?.kind === 'part' && parsed.part.type === 'text-delta') {\n text += parsed.part.delta\n }\n }\n }\n\n return text\n}\n\n/**\n * Creates a ReadableStream that parses AI SDK v6 UI Message Stream SSE data\n * from a response body and emits LanguageModelV3 stream parts.\n *\n * Text lifecycle events (text-start / text-end) are forwarded from the server.\n * Tool calls are tracked to ensure the finish reason is set correctly.\n *\n * @category Utilities\n */\nexport function createSSEStream(\n responseBody: ReadableStream<Uint8Array>,\n warnings: SharedV3Warning[],\n onData?: (type: string, data: unknown) => void,\n): ReadableStream<LanguageModelV3StreamPart> {\n let finishReason: LanguageModelV3FinishReason = { unified: 'other', raw: undefined }\n const fallbackTextId = crypto.randomUUID()\n let hasToolCalls = false\n\n return new ReadableStream<LanguageModelV3StreamPart>({\n async start(controller) {\n const reader = responseBody.getReader()\n const decoder = new TextDecoder()\n let buffer = ''\n\n controller.enqueue({ type: 'stream-start', warnings })\n\n try {\n while (true) {\n const { done, value } = await reader.read()\n if (done) break\n\n buffer += decoder.decode(value, { stream: true })\n const lines = buffer.split('\\n')\n buffer = lines.pop() ?? ''\n\n for (const line of lines) {\n const parsed = parseStreamLine(line, fallbackTextId)\n if (parsed?.kind === 'part') {\n if (parsed.part.type === 'tool-call') {\n hasToolCalls = true\n }\n controller.enqueue(parsed.part)\n } else if (parsed?.kind === 'finish') {\n finishReason = parsed.finishReason\n } else if (parsed?.kind === 'data') {\n onData?.(parsed.type, parsed.data)\n }\n }\n }\n\n if (buffer.trim()) {\n const parsed = parseStreamLine(buffer, fallbackTextId)\n if (parsed?.kind === 'part') {\n if (parsed.part.type === 'tool-call') {\n hasToolCalls = true\n }\n controller.enqueue(parsed.part)\n } else if (parsed?.kind === 'finish') {\n finishReason = parsed.finishReason\n } else if (parsed?.kind === 'data') {\n onData?.(parsed.type, parsed.data)\n }\n }\n\n // If tool calls were emitted, override finish reason so streamText\n // knows to execute tools and continue with the next step.\n if (hasToolCalls && finishReason.unified !== 'tool-calls') {\n finishReason = { unified: 'tool-calls', raw: finishReason.raw ?? 'tool-calls' }\n }\n\n controller.enqueue({\n type: 'finish',\n finishReason,\n usage: createEmptyUsage(),\n })\n } catch (error) {\n controller.enqueue({\n type: 'error',\n error: error instanceof Error ? error : new Error(String(error)),\n })\n } finally {\n controller.close()\n }\n },\n })\n}\n","import type {\n LanguageModelV3,\n LanguageModelV3CallOptions,\n LanguageModelV3Content,\n LanguageModelV3GenerateResult,\n LanguageModelV3Prompt,\n LanguageModelV3StreamResult,\n SharedV3Warning,\n} from '@ai-sdk/provider'\nimport type { ClientToolDefinition } from '@sanity/agent-types'\nimport { groupBy } from 'es-toolkit'\nimport type { AgentSettings, Message } from './types'\nimport {\n buildApiUrl,\n buildHeaders,\n createEmptyUsage,\n createSSEStream,\n extractTextFromParts,\n fetchApi,\n parseStreamLine,\n} from './utils'\n\n/**\n * @category Model\n */\nexport interface ContentAgentModelConfig {\n provider: string\n baseURL: string\n apiVersion: string\n organizationId: string\n headers: () => Record<string, string | undefined>\n credentials?: RequestCredentials\n fetch?: typeof globalThis.fetch\n}\n\n/**\n * Extended message type for the wire format that supports tool calls and tool results\n * in addition to plain text messages.\n */\ntype WireMessage =\n | Message\n | {\n role: 'assistant'\n content: Array<\n | { type: 'text'; text: string }\n | { type: 'tool-call'; toolCallId: string; toolName: string; input: string }\n >\n }\n | {\n role: 'tool'\n content: Array<{\n type: 'tool-result'\n toolCallId: string\n toolName: string\n output: unknown\n }>\n }\n\nconst UNSUPPORTED_SETTINGS = [\n 'maxOutputTokens',\n 'temperature',\n 'topP',\n 'topK',\n 'frequencyPenalty',\n 'presencePenalty',\n 'seed',\n] as const\n\n/**\n * Vercel AI SDK `LanguageModelV1` implementation that communicates with the\n * Sanity Content Agent API via a conversation thread.\n *\n * Typically you don't construct this directly — use\n * {@link createContentAgent | createContentAgent().agent(threadId)} instead.\n *\n * @category Model\n *\n * @example\n * ```ts\n * import { createContentAgent } from 'content-agent'\n * import { streamText } from 'ai'\n *\n * const contentAgent = createContentAgent({\n * organizationId: 'your-org-id',\n * token: 'your-sanity-token',\n * })\n *\n * const model = contentAgent.agent('my-thread-id', {\n * application: { key: 'projectId.datasetName' },\n * config: {\n * capabilities: { read: true, write: false },\n * },\n * })\n *\n * const { textStream } = await streamText({ model, prompt: 'Summarize my content' })\n * for await (const chunk of textStream) {\n * process.stdout.write(chunk)\n * }\n * ```\n */\nexport class ContentAgentModel implements LanguageModelV3 {\n readonly specificationVersion = 'v3' as const\n readonly provider: string\n readonly modelId: string\n readonly supportedUrls: Record<string, RegExp[]> = {}\n\n private readonly config: ContentAgentModelConfig\n private readonly threadId: string\n private readonly settings: AgentSettings\n\n constructor(\n threadId: string,\n settings: AgentSettings | undefined,\n config: ContentAgentModelConfig,\n ) {\n this.threadId = threadId\n this.modelId = `content-agent:${threadId}`\n this.provider = config.provider\n this.settings = settings ?? {}\n this.config = config\n }\n\n /**\n * Converts a LanguageModelV3Prompt into wire messages for the agent API.\n *\n * Handles all message roles:\n * - system: plain text system messages\n * - user: extracts text from content parts\n * - assistant: extracts text and/or tool-call parts\n * - tool: converts tool-result parts for the API\n */\n private convertPromptToMessages(prompt: LanguageModelV3Prompt): WireMessage[] {\n const messages: WireMessage[] = []\n\n for (const message of prompt) {\n if (message.role === 'system') {\n if (message.content) {\n messages.push({ role: 'system', content: message.content })\n }\n continue\n }\n\n if (message.role === 'tool') {\n const toolResults = message.content\n .filter((part) => part.type === 'tool-result')\n .map((part) => {\n const p = part as {\n toolCallId: string\n toolName: string\n output: { type: string; value: unknown } | unknown\n }\n return {\n type: 'tool-result' as const,\n toolCallId: p.toolCallId,\n toolName: p.toolName,\n output:\n p.output && typeof p.output === 'object' && 'value' in p.output\n ? (p.output as { value: unknown }).value\n : p.output,\n }\n })\n\n if (toolResults.length > 0) {\n messages.push({ role: 'tool', content: toolResults })\n }\n continue\n }\n\n if (message.role === 'assistant') {\n const content = message.content as Array<{\n type: string\n text?: string\n toolCallId?: string\n toolName?: string\n input?: string\n }>\n\n const grouped = groupBy(content, (p) => p.type)\n const toolCalls = grouped['tool-call'] ?? []\n const textParts = grouped.text ?? []\n\n if (toolCalls.length > 0) {\n const parts = [\n ...textParts\n .filter((p) => p.text)\n .map((p) => ({ type: 'text' as const, text: p.text! })),\n ...toolCalls.map((tc) => ({\n type: 'tool-call' as const,\n toolCallId: tc.toolCallId!,\n toolName: tc.toolName!,\n input: tc.input!,\n })),\n ]\n\n if (parts.length > 0) {\n messages.push({ role: 'assistant', content: parts })\n }\n } else {\n const text = extractTextFromParts(content as Array<{ type: string; text?: string }>)\n if (text) {\n messages.push({ role: 'assistant', content: text })\n }\n }\n continue\n }\n\n // user messages\n const text = extractTextFromParts(message.content as Array<{ type: string; text?: string }>)\n if (text) {\n messages.push({ role: message.role, content: text })\n }\n }\n\n return messages\n }\n\n /**\n * Extracts client tool definitions from the model call options.\n * Only the schemas are sent — execute functions stay on the client.\n */\n private extractClientTools(\n options: LanguageModelV3CallOptions,\n ): ClientToolDefinition[] | undefined {\n if (!options.tools?.length) return undefined\n\n const clientTools = options.tools\n .filter((t) => t.type === 'function')\n .map((t) => ({\n type: 'function' as const,\n name: t.name,\n description: t.description,\n inputSchema: t.inputSchema as Record<string, unknown>,\n }))\n\n return clientTools.length > 0 ? clientTools : undefined\n }\n\n private collectWarnings(options: LanguageModelV3CallOptions): SharedV3Warning[] {\n const warnings: SharedV3Warning[] = []\n\n for (const setting of UNSUPPORTED_SETTINGS) {\n if (options[setting] !== undefined) {\n warnings.push({ type: 'unsupported', feature: setting })\n }\n }\n\n if (options.stopSequences?.length) {\n warnings.push({ type: 'unsupported', feature: 'stopSequences' })\n }\n\n if (options.responseFormat && options.responseFormat.type !== 'text') {\n warnings.push({ type: 'unsupported', feature: 'responseFormat' })\n }\n\n return warnings\n }\n\n private buildRequestBody(options: LanguageModelV3CallOptions, stream: boolean) {\n const clientTools = this.extractClientTools(options)\n const toolChoice = options.toolChoice\n\n return {\n messages: this.convertPromptToMessages(options.prompt),\n stream,\n ...(clientTools && { clientTools }),\n ...(toolChoice && { toolChoice }),\n ...(this.settings.application && { application: this.settings.application }),\n ...(this.settings.config && { config: this.settings.config }),\n ...(this.settings.format && { format: this.settings.format }),\n }\n }\n\n private getResponseHeaders(response: Response): Record<string, string> {\n return Object.fromEntries(response.headers.entries())\n }\n\n async doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult> {\n // Always request a stream from the backend and buffer the result.\n // This keeps the HTTP connection alive with incremental data, avoiding\n // 504 gateway timeouts on long-running agent requests.\n const body = this.buildRequestBody(options, true)\n const warnings = this.collectWarnings(options)\n\n const response = await fetchApi({\n url: buildApiUrl(\n this.config.baseURL,\n this.config.apiVersion,\n `/${this.config.organizationId}/threads/${this.threadId}`,\n ),\n body,\n headers: buildHeaders(this.config.headers()),\n credentials: this.config.credentials,\n signal: options.abortSignal,\n fetch: this.config.fetch,\n })\n\n if (!response.body) {\n throw new Error('No response body for generate request')\n }\n\n const { content, finishReason } = await this.extractContentFromStream(response.body)\n\n return {\n content,\n finishReason,\n usage: createEmptyUsage(),\n request: { body },\n response: { headers: this.getResponseHeaders(response) },\n warnings,\n }\n }\n\n /**\n * Parses a streamed response body into content parts (text and tool calls)\n * and a finish reason. Used by doGenerate to buffer the full response.\n */\n private async extractContentFromStream(responseBody: ReadableStream<Uint8Array>): Promise<{\n content: LanguageModelV3Content[]\n finishReason: LanguageModelV3GenerateResult['finishReason']\n }> {\n const reader = responseBody.getReader()\n const decoder = new TextDecoder()\n const textContentId = 'generate'\n let text = ''\n let hasToolCalls = false\n const content: LanguageModelV3Content[] = []\n\n while (true) {\n const { done, value } = await reader.read()\n if (done) break\n\n const chunk = decoder.decode(value, { stream: true })\n for (const line of chunk.split('\\n')) {\n const parsed = parseStreamLine(line, textContentId)\n if (parsed?.kind === 'part') {\n if (parsed.part.type === 'text-delta') {\n text += parsed.part.delta\n } else if (parsed.part.type === 'tool-call') {\n hasToolCalls = true\n const tc = parsed.part as { toolCallId: string; toolName: string; input: string }\n content.push({\n type: 'tool-call',\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n input: tc.input,\n })\n }\n }\n }\n }\n\n if (text) {\n content.unshift({ type: 'text', text })\n }\n\n const finishReason = hasToolCalls\n ? { unified: 'tool-calls' as const, raw: 'tool-calls' }\n : { unified: 'stop' as const, raw: undefined }\n\n return { content, finishReason }\n }\n\n async doStream(options: LanguageModelV3CallOptions): Promise<LanguageModelV3StreamResult> {\n const body = this.buildRequestBody(options, true)\n const warnings = this.collectWarnings(options)\n\n const response = await fetchApi({\n url: buildApiUrl(\n this.config.baseURL,\n this.config.apiVersion,\n `/${this.config.organizationId}/threads/${this.threadId}`,\n ),\n body,\n headers: buildHeaders(this.config.headers()),\n credentials: this.config.credentials,\n signal: options.abortSignal,\n fetch: this.config.fetch,\n })\n\n if (!response.body) {\n throw new Error('No response body for streaming request')\n }\n\n return {\n stream: createSSEStream(response.body, warnings, this.settings.onStreamData),\n request: { body },\n response: { headers: this.getResponseHeaders(response) },\n }\n }\n}\n","import type { LanguageModelV3 } from '@ai-sdk/provider'\nimport type { SanityClientLike } from './from-client'\nimport { ContentAgentModel, type ContentAgentModelConfig } from './sanity-agent-language-model'\nimport type {\n AgentSettings,\n Application,\n ContentAgentProviderSettings,\n GenerateOptions,\n GenerateResult,\n GenerateSettings,\n} from './types'\nimport {\n buildApiUrl,\n buildHeaders,\n extractTextFromStream,\n fetchApi,\n SanityAgentError,\n} from './utils'\n\nconst DEFAULT_BASE_URL = 'https://api.sanity.io'\nconst DEFAULT_API_VERSION = 'v2026-01-01'\n\n/**\n * A Content Agent provider instance returned by {@link createContentAgent}.\n *\n * Provides two modes of interaction:\n * - **Conversation threads** via {@link ContentAgentProvider.agent | .agent()} — stateful, multi-turn conversations with server-side message history.\n * - **One-shot prompts** via {@link ContentAgentProvider.prompt | .prompt()} — stateless, single-turn interactions.\n *\n * @category Provider\n */\nexport interface ContentAgentProvider {\n /**\n * Creates a Vercel AI SDK {@link https://sdk.vercel.ai/docs/ai-sdk-core | LanguageModelV1} for a conversation thread.\n *\n * The thread ID identifies the conversation and maintains message history server-side.\n * If a thread with that ID already exists, the conversation continues. If not, a new\n * thread is automatically created.\n *\n * Use the returned model with `generateText` or `streamText` from the `ai` package.\n *\n * @example\n * ```ts\n * import { generateText, streamText } from 'ai'\n *\n * const model = contentAgent.agent('my-thread-id', {\n * application: { key: 'projectId.datasetName' },\n * config: {\n * capabilities: { read: true, write: false },\n * },\n * })\n *\n * // Non-streaming\n * const { text } = await generateText({ model, prompt: 'What blog posts do I have?' })\n *\n * // Streaming\n * const { textStream } = await streamText({ model, prompt: 'Summarize my content' })\n * for await (const chunk of textStream) {\n * process.stdout.write(chunk)\n * }\n * ```\n *\n * @param threadId - Unique identifier for the conversation thread.\n * @param settings - Optional agent settings (application, capabilities, filters, etc.).\n */\n agent(threadId: string, settings?: AgentSettings): LanguageModelV3\n\n /**\n * Lists available applications for the organization.\n *\n * Returns all Sanity Studio applications the authenticated user has access to.\n * Use the `key` from a returned application to target it when creating an agent.\n *\n * @example\n * ```ts\n * const apps = await contentAgent.applications()\n * const app = apps.find((app) => app.title === \"My Studio\")\n *\n * const model = contentAgent.agent(\"my-thread\", {\n * application: { key: app.key },\n * })\n * ```\n */\n applications(): Promise<Application[]>\n\n /**\n * Resolves the full application for a Sanity client by matching its `projectId`\n * and `dataset` against the organization's application list.\n *\n * This is the easiest way to get a valid application in Studio contexts —\n * pass the client you already have and get back an application with the\n * correct `key` for the Content Agent API.\n *\n * Throws if no matching application is found (e.g. wrong org, or the Studio\n * hasn't been deployed yet).\n *\n * @example\n * ```ts\n * import { createContentAgent, fromClient } from 'content-agent'\n * import { useClient, useWorkspace } from 'sanity'\n *\n * const client = useClient({ apiVersion: '2024-01-01' })\n * const workspace = useWorkspace()\n * const { baseURL } = fromClient(client, workspace.name)\n *\n * const contentAgent = createContentAgent({\n * organizationId: 'your-org-id',\n * baseURL,\n * })\n *\n * const application = await contentAgent.resolveApplication(client, workspace.name)\n * const model = contentAgent.agent('thread-id', { application })\n * ```\n *\n * @param client - Any object with a `config()` method returning `projectId` and `dataset`.\n * @param workspace - The active workspace name (e.g. `'default'`). Available via `useWorkspace().name` in Studio.\n */\n resolveApplication(client: SanityClientLike, workspace: string): Promise<Application>\n\n /**\n * Sends a one-shot prompt to the Content Agent (stateless).\n *\n * Unlike {@link ContentAgentProvider.agent | .agent()}, this does not create or use a conversation thread.\n * Each call is independent with no message history or persistence.\n * Ideal for simple, single-turn interactions.\n *\n * @example\n * ```ts\n * const { text } = await contentAgent.prompt(\n * {\n * application: { key: 'projectId.datasetName' },\n * config: { capabilities: { read: true, write: false } },\n * instructions: 'Be concise',\n * },\n * { message: 'List my 5 most recent posts' },\n * )\n * ```\n *\n * @param settings - Generation settings (application, capabilities, instructions, etc.).\n * @param options - The prompt message and request options.\n */\n prompt(settings: GenerateSettings, options: GenerateOptions): Promise<GenerateResult>\n}\n\n/**\n * Creates a Content Agent provider instance.\n *\n * The provider is the main entry point for interacting with the Sanity Content Agent API.\n * It returns an object with methods for conversation threads and one-shot prompts.\n *\n * @category Provider\n *\n * @example Basic setup\n * ```ts\n * import { createContentAgent } from 'content-agent'\n *\n * const contentAgent = createContentAgent({\n * organizationId: 'your-org-id',\n * token: 'your-sanity-token',\n * })\n * ```\n *\n * @example Using with Vercel AI SDK for conversation threads\n * ```ts\n * import { createContentAgent } from 'content-agent'\n * import { generateText, streamText } from 'ai'\n *\n * const contentAgent = createContentAgent({\n * organizationId: 'your-org-id',\n * token: 'your-sanity-token',\n * })\n *\n * const model = contentAgent.agent('my-thread-id', {\n * application: { key: 'projectId.datasetName' },\n * config: {\n * capabilities: { read: true, write: false },\n * },\n * })\n *\n * // Non-streaming\n * const { text } = await generateText({ model, prompt: 'What blog posts do I have?' })\n *\n * // Streaming\n * const { textStream } = await streamText({ model, prompt: 'Summarize my content' })\n * for await (const chunk of textStream) {\n * process.stdout.write(chunk)\n * }\n * ```\n *\n * @example One-shot generation (stateless, no thread)\n * ```ts\n * const { text } = await contentAgent.prompt(\n * {\n * application: { key: 'projectId.datasetName' },\n * config: { capabilities: { read: true, write: false } },\n * instructions: 'Be concise',\n * },\n * { message: 'List my 5 most recent posts' },\n * )\n * ```\n *\n * @param options - Provider configuration including organization ID, token, and optional overrides.\n */\nexport function createContentAgent(options: ContentAgentProviderSettings): ContentAgentProvider {\n const baseURL = options.baseURL?.replace(/\\/$/, '') ?? DEFAULT_BASE_URL\n const apiVersion = options.apiVersion ?? DEFAULT_API_VERSION\n const { organizationId } = options\n\n const getHeaders = (): Record<string, string | undefined> => ({\n ...(options.token && { Authorization: `Bearer ${options.token}` }),\n ...options.headers,\n })\n\n // When no token is provided, include credentials so the browser sends\n // cookies automatically — this is the Studio / browser auth path.\n const credentials: RequestCredentials | undefined = options.token ? undefined : 'include'\n\n const config: ContentAgentModelConfig = {\n provider: 'content-agent',\n baseURL,\n apiVersion,\n organizationId,\n headers: getHeaders,\n credentials,\n fetch: options.fetch,\n }\n\n const agent = (threadId: string, settings?: AgentSettings): LanguageModelV3 => {\n return new ContentAgentModel(\n threadId,\n {\n ...settings,\n application: settings?.application ?? options.application,\n },\n config,\n )\n }\n\n let applicationsPromise: Promise<Application[]> | null = null\n\n const applications = (): Promise<Application[]> => {\n if (!applicationsPromise) {\n applicationsPromise = (async () => {\n const fetchFn = options.fetch ?? globalThis.fetch\n const url = buildApiUrl(baseURL, apiVersion, `/${organizationId}/applications`)\n const headers = buildHeaders(getHeaders())\n\n const response = await fetchFn(url, {\n method: 'GET',\n headers,\n ...(credentials && { credentials }),\n })\n\n if (!response.ok) {\n applicationsPromise = null\n const errorText = await response.text()\n throw new SanityAgentError(response.status, errorText)\n }\n\n const json = (await response.json()) as { applications?: Application[] }\n return json.applications ?? []\n })()\n }\n\n return applicationsPromise\n }\n\n const prompt = async (\n settings: GenerateSettings,\n generateOptions: GenerateOptions,\n ): Promise<GenerateResult> => {\n const effectiveApplication = settings.application ?? options.application\n\n const body = {\n organizationId,\n message: generateOptions.message,\n stream: generateOptions.stream ?? false,\n ...(effectiveApplication && { application: effectiveApplication }),\n ...(settings.config && { config: settings.config }),\n ...(settings.instructions && { instructions: settings.instructions }),\n ...(settings.format && { format: settings.format }),\n }\n\n const response = await fetchApi({\n url: buildApiUrl(baseURL, apiVersion, '/prompt'),\n body,\n headers: buildHeaders(getHeaders()),\n credentials,\n signal: generateOptions.abortSignal,\n fetch: options.fetch,\n })\n\n if (body.stream) {\n if (!response.body) {\n throw new SanityAgentError(500, 'No response body')\n }\n return { text: await extractTextFromStream(response.body) }\n }\n\n const json = (await response.json()) as { text?: string }\n return { text: json.text ?? '' }\n }\n\n const resolveApplication = async (\n client: SanityClientLike,\n workspace: string,\n ): Promise<Application> => {\n const { projectId, dataset } = client.config()\n if (!projectId || !dataset) {\n throw new Error(\n 'resolveApplication: client is missing `projectId` or `dataset` in its config',\n )\n }\n\n const resourceId = `${projectId}.${dataset}`\n const apps = await applications()\n const match = apps.find((app) => app.resource?.id === resourceId && app.name === workspace)\n\n if (!match) {\n throw new SanityAgentError(\n 400,\n JSON.stringify({\n code: 'APPLICATION_NOT_FOUND',\n message: `No application found for resource \"${resourceId}\" with workspace \"${workspace}\". Make sure the Studio has been deployed and belongs to this organization.`,\n }),\n )\n }\n\n return match\n }\n\n return { agent, applications, resolveApplication, prompt }\n}\n","import type { LanguageModelV3, LanguageModelV3CallOptions } from '@ai-sdk/provider'\nimport { deriveBaseUrl, type SanityClientLike } from './from-client'\nimport type { ContentAgentProvider } from './sanity-agent-provider'\nimport { createContentAgent } from './sanity-agent-provider'\nimport type { AgentSettings, ContentAgentProviderSettings } from './types'\nimport { buildHeaders, SanityAgentError } from './utils'\n\nconst PROJECTS_API_VERSION = 'v2024-01-01'\n\n/**\n * Module-level cache for setup promises. Keyed by `projectId.dataset.workspace`,\n * so repeated calls to `createStudioAgent` with the same params (e.g. React strict\n * mode double-render, component remounts) reuse the same in-flight or resolved setup.\n */\nconst providerCache = new Map<string, Promise<ContentAgentProvider>>()\n\ntype ProjectResponse = {\n id: string\n organizationId?: string | null\n}\n\n/**\n * Options for {@link createStudioAgent}.\n *\n * @category Studio Integration\n */\nexport type StudioAgentOptions = {\n /**\n * Custom fetch implementation. Useful for testing.\n */\n fetch?: typeof globalThis.fetch\n\n /**\n * Additional provider settings forwarded to {@link createContentAgent}.\n * `organizationId`, `baseURL`, and `application` are derived automatically\n * and cannot be overridden here.\n */\n providerOptions?: Omit<ContentAgentProviderSettings, 'organizationId' | 'baseURL' | 'application'>\n}\n\nasync function fetchOrganizationId(\n apiHost: string,\n projectId: string,\n fetchFn: typeof globalThis.fetch,\n): Promise<string> {\n const url = `${apiHost}/${PROJECTS_API_VERSION}/projects/${projectId}`\n\n const response = await fetchFn(url, {\n method: 'GET',\n headers: buildHeaders({}),\n credentials: 'include',\n })\n\n if (!response.ok) {\n const errorText = await response.text()\n throw new SanityAgentError(\n response.status,\n JSON.stringify({\n code: 'PROJECT_FETCH_FAILED',\n message: `Failed to fetch project \"${projectId}\": ${errorText}`,\n }),\n )\n }\n\n const project = (await response.json()) as ProjectResponse\n\n if (!project.organizationId) {\n throw new SanityAgentError(\n 400,\n JSON.stringify({\n code: 'NO_ORGANIZATION',\n message: `Project \"${projectId}\" is not assigned to an organization. Content Agent requires an organization-level project.`,\n }),\n )\n }\n\n return project.organizationId\n}\n\n/**\n * Resolves the full provider by fetching org ID and application. The returned\n * promise is cached so the setup only runs once regardless of how many calls\n * go through the lazy wrapper.\n */\nfunction initProvider(\n projectId: string,\n dataset: string,\n workspace: string,\n apiHost: string,\n baseURL: string,\n fetchFn: typeof globalThis.fetch,\n providerOptions?: StudioAgentOptions['providerOptions'],\n): Promise<ContentAgentProvider> {\n return fetchOrganizationId(apiHost, projectId, fetchFn).then(async (organizationId) => {\n const tempProvider = createContentAgent({\n organizationId,\n baseURL,\n fetch: fetchFn,\n })\n\n // Reuse the provider's own resolveApplication logic — no duplicated matching\n const client: SanityClientLike = { config: () => ({ projectId, dataset }) }\n const application = await tempProvider.resolveApplication(client, workspace)\n\n return createContentAgent({\n ...providerOptions,\n organizationId,\n baseURL,\n application,\n })\n })\n}\n\n/**\n * A {@link LanguageModelV3} that defers to a lazily-resolved provider.\n *\n * The first call to `doGenerate` or `doStream` awaits the setup promise,\n * then delegates to the real model. The resolved model is cached for\n * subsequent calls.\n */\nclass LazyContentAgentModel implements LanguageModelV3 {\n readonly specificationVersion = 'v3' as const\n readonly provider = 'content-agent'\n readonly modelId: string\n readonly supportedUrls: Record<string, RegExp[]> = {}\n\n private readonly providerPromise: Promise<ContentAgentProvider>\n private readonly threadId: string\n private readonly settings: AgentSettings | undefined\n private resolvedModel: LanguageModelV3 | null = null\n\n constructor(\n threadId: string,\n settings: AgentSettings | undefined,\n providerPromise: Promise<ContentAgentProvider>,\n ) {\n this.threadId = threadId\n this.settings = settings\n this.modelId = `content-agent:${threadId}`\n this.providerPromise = providerPromise\n }\n\n private async getModel(): Promise<LanguageModelV3> {\n if (!this.resolvedModel) {\n const provider = await this.providerPromise\n this.resolvedModel = provider.agent(this.threadId, this.settings)\n }\n return this.resolvedModel\n }\n\n async doGenerate(options: LanguageModelV3CallOptions) {\n const model = await this.getModel()\n return model.doGenerate(options)\n }\n\n async doStream(options: LanguageModelV3CallOptions) {\n const model = await this.getModel()\n return model.doStream(options)\n }\n}\n\n/**\n * Creates a Content Agent provider fully configured for a Studio context.\n *\n * This is the simplest way to use Content Agent from inside Sanity Studio.\n * The call is **synchronous** — safe to use in React components and hooks.\n * Async setup (fetching the organization ID and resolving the application)\n * is deferred to the first API call, which is already async.\n *\n * Internally handles:\n * 1. Deriving the project-scoped API URL (for CORS)\n * 2. Fetching the organization ID from project metadata\n * 3. Resolving the Studio application by workspace name\n * 4. Cookie-based auth (no token needed)\n *\n * The returned provider works exactly like one from {@link createContentAgent},\n * but `.agent()` and `.prompt()` automatically target the resolved application\n * unless explicitly overridden per-call.\n *\n * @category Studio Integration\n *\n * @example Studio component with memoized provider\n * ```ts\n * import { useMemo } from 'react'\n * import { createStudioAgent } from 'content-agent'\n * import { useClient, useWorkspace } from 'sanity'\n * import { generateText } from 'ai'\n *\n * function MyComponent() {\n * const client = useClient({ apiVersion: '2024-01-01' })\n * const { name: workspace } = useWorkspace()\n *\n * const contentAgent = useMemo(\n * () => createStudioAgent(client, workspace),\n * [client, workspace],\n * )\n *\n * async function handleSummarize() {\n * const { text } = await generateText({\n * model: contentAgent.agent('my-thread'),\n * prompt: 'Summarize my content',\n * })\n * console.log(text)\n * }\n * }\n * ```\n *\n * @param client - Any object with a `config()` returning `projectId` and `dataset`.\n * @param workspace - The active workspace name. Available via `useWorkspace().name` in Studio.\n * @param options - Optional overrides (custom fetch, additional provider settings).\n */\nexport function createStudioAgent(\n client: SanityClientLike,\n workspace: string,\n options?: StudioAgentOptions,\n): ContentAgentProvider {\n const { projectId, dataset } = client.config()\n\n if (!projectId) {\n throw new Error('createStudioAgent: client is missing `projectId` in its config')\n }\n if (!dataset) {\n throw new Error('createStudioAgent: client is missing `dataset` in its config')\n }\n\n const { apiHost } = client.config()\n const fetchFn = options?.fetch ?? globalThis.fetch\n const baseURL = deriveBaseUrl(apiHost, projectId)\n const cacheKey = `${projectId}.${dataset}.${workspace}`\n\n // Reuse an existing setup promise for the same project/dataset/workspace,\n // or start a new one. Custom fetch or providerOptions bypass the cache\n // since they indicate a non-default configuration (e.g. tests).\n const useCache = !options?.fetch && !options?.providerOptions\n\n let providerPromise: Promise<ContentAgentProvider>\n\n if (useCache && providerCache.has(cacheKey)) {\n providerPromise = providerCache.get(cacheKey)!\n } else {\n providerPromise = initProvider(\n projectId,\n dataset,\n workspace,\n apiHost || 'https://api.sanity.io',\n baseURL,\n fetchFn,\n options?.providerOptions,\n )\n\n if (useCache) {\n providerCache.set(cacheKey, providerPromise)\n\n // Evict on failure so a transient error doesn't stick forever\n providerPromise.catch(() => {\n providerCache.delete(cacheKey)\n })\n }\n }\n\n const awaitProvider = async <T>(fn: (provider: ContentAgentProvider) => Promise<T>): Promise<T> =>\n fn(await providerPromise)\n\n return {\n agent(threadId: string, settings?: AgentSettings): LanguageModelV3 {\n return new LazyContentAgentModel(threadId, settings, providerPromise)\n },\n\n applications() {\n return awaitProvider((p) => p.applications())\n },\n\n resolveApplication(resolveClient: SanityClientLike, resolveWorkspace: string) {\n return awaitProvider((p) => p.resolveApplication(resolveClient, resolveWorkspace))\n },\n\n prompt(settings, generateOptions) {\n return awaitProvider((p) => p.prompt(settings, generateOptions))\n },\n }\n}\n"],"names":["text"],"mappings":";;AAEA,MAAM,mBAAmB;AA6ClB,SAAS,cAAc,SAA6B,WAA2B;AACpF,QAAM,MAAM,IAAI,IAAI,WAAW,gBAAgB;AAC/C,SAAA,IAAI,WAAW,GAAG,SAAS,IAAI,IAAI,QAAQ,IACpC,IAAI;AACb;AAoCO,SAAS,WAAW,QAA0B,WAAwC;AAC3F,QAAM,EAAE,WAAW,SAAS,QAAA,IAAY,OAAO,OAAA;AAE/C,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,yDAAyD;AAE3E,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,uDAAuD;AAGzE,SAAO;AAAA,IACL,SAAS,cAAc,SAAS,SAAS;AAAA,IACzC,aAAa;AAAA,MACX,MAAM;AAAA,MACN,UAAU;AAAA,QACR,IAAI,GAAG,SAAS,IAAI,OAAO;AAAA,QAC3B,MAAM;AAAA,MAAA;AAAA,IACR;AAAA,EACF;AAEJ;AC7FO,SAAS,aACd,eACA,mBACwB;AACxB,SAAO;AAAA,IACL;AAAA,MACE,gBAAgB;AAAA,MAChB,GAAG;AAAA,MACH,GAA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e;AACb,aAAO,cAAc,CAAC,MAAM,EAAE,cAAc;AAAA,IAC9C;AAAA,IAEA,mBAAmB,eAAiC,kBAA0B;AAC5E,aAAO,cAAc,CAAC,MAAM,EAAE,mBAAmB,eAAe,gBAAgB,CAAC;AAAA,IACnF;AAAA,IAEA,OAAO,UAAU,iBAAiB;AAChC,aAAO,cAAc,CAAC,MAAM,EAAE,OAAO,UAAU,eAAe,CAAC;AAAA,IACjE;AAAA,EAAA;AAEJ;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "content-agent",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Vercel AI SDK provider for Sanity Content Agent API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"@ai-sdk/provider": "^3.0.0",
|
|
31
31
|
"@ai-sdk/provider-utils": "^2.2.8",
|
|
32
32
|
"es-toolkit": "^1.44.0",
|
|
33
|
-
"@sanity/agent-types": "0.0.
|
|
33
|
+
"@sanity/agent-types": "0.0.95"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@sanity/pkg-utils": "^10.4.1",
|