content-agent 1.0.3 → 1.2.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/dist/_chunks-dts/sanity-agent-provider.d.ts +2 -793
- package/dist/_chunks-dts/sanity-agent-provider.d.ts.map +1 -1
- package/dist/_chunks-dts/types.d.ts +794 -0
- package/dist/_chunks-dts/types.d.ts.map +1 -0
- package/dist/_chunks-es/utils.js +310 -0
- package/dist/_chunks-es/utils.js.map +1 -0
- package/dist/chat-transport.d.ts +90 -0
- package/dist/chat-transport.d.ts.map +1 -0
- package/dist/chat-transport.js +91 -0
- package/dist/chat-transport.js.map +1 -0
- package/dist/content-agent.d.ts +2 -1
- package/dist/content-agent.d.ts.map +1 -1
- package/dist/index.d.ts +21 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -255
- package/dist/index.js.map +1 -1
- package/dist/messages.d.ts +106 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +79 -0
- package/dist/messages.js.map +1 -0
- package/dist/react.d.ts +110 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +84 -0
- package/dist/react.js.map +1 -0
- package/package.json +25 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { C as components, S as ValidationIssue, T as paths, _ as GenerateSettings, a as Capability, b as OutputFormat, c as ChatRequest, d as ContentAgentProviderSettings, f as ErrorResponse, g as GenerateResult, h as GenerateRequest, i as Capabilities, l as ChatResponse, m as GenerateOptions, n as Application, o as CapabilityPreset, p as Filter, r as ApplicationResource, s as CapabilityPresetValue, t as AgentSettings, u as Config, v as Message, w as operations, x as UserMessageContext, y as MessageRole } from "./_chunks-dts/types.js";
|
|
2
|
+
import { a as SanityClientLike, i as SanityClientConfig, n as createContentAgent, o as fromClient, r as ClientDerivedConfig, s as getBaseUrl, t as ContentAgentProvider } from "./_chunks-dts/sanity-agent-provider.js";
|
|
2
3
|
import { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3FinishReason, LanguageModelV3GenerateResult, LanguageModelV3StreamPart, LanguageModelV3StreamResult, SharedV3Warning } from "@ai-sdk/provider";
|
|
3
4
|
import { ClientToolDefinition, HeadlessErrorResponse } from "@sanity/agent-types";
|
|
4
5
|
/**
|
|
@@ -188,14 +189,15 @@ declare function resolveAgentErrorMessage(classified: AgentErrorType, error: unk
|
|
|
188
189
|
declare function getAgentErrorMessage(error: unknown): string;
|
|
189
190
|
interface FetchOptions {
|
|
190
191
|
url: string;
|
|
191
|
-
|
|
192
|
+
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD';
|
|
193
|
+
body?: unknown;
|
|
192
194
|
headers: Record<string, string>;
|
|
193
195
|
credentials?: RequestCredentials;
|
|
194
196
|
signal?: AbortSignal;
|
|
195
197
|
fetch?: typeof globalThis.fetch;
|
|
196
198
|
}
|
|
197
199
|
/**
|
|
198
|
-
* Makes
|
|
200
|
+
* Makes an API request and returns the response.
|
|
199
201
|
* Throws SanityAgentError if the response is not ok.
|
|
200
202
|
*
|
|
201
203
|
* @category Utilities
|
|
@@ -259,6 +261,21 @@ declare function parseStreamLine(line: string, textContentId: string): ParsedStr
|
|
|
259
261
|
* @category Utilities
|
|
260
262
|
*/
|
|
261
263
|
declare function extractTextFromStream(responseBody: ReadableStream<Uint8Array>): Promise<string>;
|
|
264
|
+
/** @category Utilities */
|
|
265
|
+
type CreateSSEStreamOptions = {
|
|
266
|
+
/**
|
|
267
|
+
* Hook to receive `data-*` chunks the AI SDK doesn't surface natively.
|
|
268
|
+
*/
|
|
269
|
+
onData?: (type: string, data: unknown) => void;
|
|
270
|
+
/**
|
|
271
|
+
* Optional reconnect callback. When set, retryable read errors trigger a
|
|
272
|
+
* call with the last seen `id:` sequence number, expecting a fresh
|
|
273
|
+
* `Response` whose body continues the stream from that frame. Return
|
|
274
|
+
* `null` to signal a clean end (HTTP 204 from the server).
|
|
275
|
+
*/
|
|
276
|
+
reconnect?: (afterSeq: number) => Promise<Response | null>;
|
|
277
|
+
maxRetries?: number;
|
|
278
|
+
};
|
|
262
279
|
/**
|
|
263
280
|
* Creates a ReadableStream that parses AI SDK v6 UI Message Stream SSE data
|
|
264
281
|
* from a response body and emits LanguageModelV3 stream parts.
|
|
@@ -268,6 +285,6 @@ declare function extractTextFromStream(responseBody: ReadableStream<Uint8Array>)
|
|
|
268
285
|
*
|
|
269
286
|
* @category Utilities
|
|
270
287
|
*/
|
|
271
|
-
declare function createSSEStream(responseBody: ReadableStream<Uint8Array>, warnings: SharedV3Warning[],
|
|
288
|
+
declare function createSSEStream(responseBody: ReadableStream<Uint8Array>, warnings: SharedV3Warning[], optionsOrOnData?: CreateSSEStreamOptions | ((type: string, data: unknown) => void)): ReadableStream<LanguageModelV3StreamPart>;
|
|
272
289
|
export { type AgentErrorMessageContext, 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, SanityAgentStreamError, type SanityClientConfig, type SanityClientLike, type StudioAgentOptions, type UserMessageContext, type ValidationIssue, buildApiUrl, buildHeaders, classifyAgentError, type components, createContentAgent, createSSEStream, createStudioAgent, extractTextFromParts, extractTextFromStream, fetchApi, fromClient, getAgentErrorMessage, getBaseUrl, isAppRelatedError, mapFinishReason, type operations, parseStreamLine, type paths, resolveAgentErrorMessage };
|
|
273
290
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/sanity-agent-language-model.ts","../src/studio.ts","../src/utils.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/sanity-agent-language-model.ts","../src/studio.ts","../src/utils.ts"],"mappings":";;;;;;;UAyBiB,uBAAA;EACf,QAAA;EACA,OAAA;EACA,UAAA;EACA,cAAA;EACA,OAAA,QAAe,MAAA;EACf,WAAA,GAAc,kBAAA;EACd,KAAA,UAAe,UAAA,CAAW,KAAA;AAAA;;;;;;;;;;;;;AAoE5B;;;;;;;;;;;;;;;;;;;;cAAa,iBAAA,YAA6B,eAAA;EAAA,SAC/B,oBAAA;EAAA,SACA,QAAA;EAAA,SACA,OAAA;EAAA,SACA,aAAA,EAAe,MAAA,SAAe,MAAA;EAAA,iBAEtB,MAAA;EAAA,iBACA,QAAA;EAAA,iBACA,QAAA;cAGf,QAAA,UACA,QAAA,EAAU,aAAA,cACV,MAAA,EAAQ,uBAAA;EADR;;;;;;;;;EAAA,QAmBM,uBAAA;EAkJS;;;;EAAA,QAzDT,kBAAA;EAAA,QAiBA,eAAA;EAAA,QAoBA,gBAAA;EAAA,QAgBA,kBAAA;EAIF,UAAA,CAAW,OAAA,EAAS,0BAAA,GAA6B,OAAA,CAAQ,6BAAA;EAsFyB;;;ACnV1F;EDmV0F,QA9C1E,wBAAA;EA8CR,QAAA,CAAS,OAAA,EAAS,0BAAA,GAA6B,OAAA,CAAQ,2BAAA;AAAA;;;AAlV/D;KCDY,kBAAA;yDAEV,KAAA,UAAe,UAAA,CAAW,KAAA;EDKZ;;;;ECCd,eAAA,GAAkB,IAAA,CAAK,4BAAA;AAAA;;;;;;;;;;;iBAiIT,iBAAA,CACd,MAAA,EAAQ,gBAAA,EACR,SAAA,UACA,OAAA,GAAU,kBAAA,GACT,oBAAA;;;;AD5IH;;iBEXgB,YAAA,CACd,aAAA,EAAe,MAAA,8BACf,iBAAA,GAAoB,MAAA,mBACnB,MAAA;;;;;;iBAgBa,WAAA,CAAY,OAAA,UAAiB,U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AA9PlC;;;;;;;;;;;;;;;;;;;;AAgCA;;;;AAhCA,iBAwRgB,eAAA,CAAgB,IAAA,UAAc,aAAA,WAAwB,gBAAA;;;;;;;iBAmIhD,qBAAA,CACpB,YAAA,EAAc,cAAA,CAAe,UAAA,IAC5B,OAAA;;KAuBS,sBAAA;EA1YsB;;AAElC;EA4YE,MAAA,IAAU,IAAA,UAAc,IAAA;;;;;;;EAOxB,SAAA,IAAa,QAAA,aAAqB,OAAA,CAAQ,QAAA;EAC1C,UAAA;AAAA;;;AAjXF;;;;;AAiBA;;iBAsYgB,eAAA,CACd,YAAA,EAAc,cAAA,CAAe,UAAA,GAC7B,QAAA,EAAU,eAAA,IACV,eAAA,GAAkB,sBAAA,KAA2B,IAAA,UAAc,IAAA,sBAC1D,cAAA,CAAe,yBAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { groupBy } from "es-toolkit";
|
|
2
|
+
import { extractTextFromParts, fetchApi, buildHeaders, createEmptyUsage, parseStreamLine, createSSEStream, buildApiUrl, SanityAgentError, extractTextFromStream } from "./_chunks-es/utils.js";
|
|
3
|
+
import { GENERIC_ERROR_MESSAGE, SanityAgentStreamError, classifyAgentError, getAgentErrorMessage, isAppRelatedError, mapFinishReason, resolveAgentErrorMessage } from "./_chunks-es/utils.js";
|
|
3
4
|
function getBaseUrl(client) {
|
|
4
5
|
return new URL(client.getUrl("/")).origin;
|
|
5
6
|
}
|
|
@@ -17,258 +18,6 @@ function fromClient(client, workspace) {
|
|
|
17
18
|
}
|
|
18
19
|
};
|
|
19
20
|
}
|
|
20
|
-
function buildHeaders(configHeaders, additionalHeaders) {
|
|
21
|
-
return pickBy(
|
|
22
|
-
{
|
|
23
|
-
"Content-Type": "application/json",
|
|
24
|
-
...additionalHeaders,
|
|
25
|
-
...configHeaders
|
|
26
|
-
},
|
|
27
|
-
isString
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
function buildApiUrl(baseURL, apiVersion, endpoint) {
|
|
31
|
-
return `${baseURL}/${apiVersion}/agent${endpoint}`;
|
|
32
|
-
}
|
|
33
|
-
class SanityAgentError extends Error {
|
|
34
|
-
status;
|
|
35
|
-
isRetryable;
|
|
36
|
-
body;
|
|
37
|
-
constructor(status, message) {
|
|
38
|
-
super(`Sanity Agent API error: ${status} ${message}`), this.name = "SanityAgentError", this.status = status, this.isRetryable = status >= 500, this.body = SanityAgentError.parseBody(message);
|
|
39
|
-
}
|
|
40
|
-
static parseBody(message) {
|
|
41
|
-
try {
|
|
42
|
-
return JSON.parse(message);
|
|
43
|
-
} catch {
|
|
44
|
-
return {};
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
class SanityAgentStreamError extends Error {
|
|
49
|
-
code;
|
|
50
|
-
constructor(message, code) {
|
|
51
|
-
super(message), this.name = "SanityAgentStreamError", this.code = code;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
const GENERIC_ERROR_MESSAGE = "Sorry, something went wrong. Please try again.";
|
|
55
|
-
function mapErrorCode(code) {
|
|
56
|
-
switch (code) {
|
|
57
|
-
case ERROR_CODES.NO_COMPATIBLE_APPLICATIONS:
|
|
58
|
-
return { type: "no_applications" };
|
|
59
|
-
case ERROR_CODES.APPLICATION_NOT_FOUND:
|
|
60
|
-
return { type: "application_not_found" };
|
|
61
|
-
case ERROR_CODES.UPSTREAM_OVERLOADED:
|
|
62
|
-
return { type: "upstream_overloaded" };
|
|
63
|
-
case ERROR_CODES.UPSTREAM_ERROR:
|
|
64
|
-
return { type: "upstream_error" };
|
|
65
|
-
case ERROR_CODES.AI_USAGE_LIMIT_EXCEEDED:
|
|
66
|
-
return { type: "usage_limit_exceeded" };
|
|
67
|
-
case ERROR_CODES.STREAMING_ERROR:
|
|
68
|
-
return { type: "streaming_error" };
|
|
69
|
-
default:
|
|
70
|
-
return null;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
function classifyAgentError(error) {
|
|
74
|
-
return error instanceof SanityAgentStreamError && error.code ? mapErrorCode(error.code) ?? { type: "unknown" } : error instanceof SanityAgentError ? error.status !== 400 ? { type: "unknown" } : mapErrorCode(error.body.code) ?? { type: "unknown" } : { type: "unknown" };
|
|
75
|
-
}
|
|
76
|
-
function isAppRelatedError(classified) {
|
|
77
|
-
return classified.type === "no_applications" || classified.type === "application_not_found";
|
|
78
|
-
}
|
|
79
|
-
async function resolveAgentErrorMessage(classified, error, resolveContext) {
|
|
80
|
-
const fallback = getAgentErrorMessage(error);
|
|
81
|
-
if (!resolveContext || !isAppRelatedError(classified)) return fallback;
|
|
82
|
-
const ctx = await resolveContext();
|
|
83
|
-
if (!ctx.orgName) return fallback;
|
|
84
|
-
switch (classified.type) {
|
|
85
|
-
case "no_applications":
|
|
86
|
-
return `Studio action required: To use the Content Agent in "${ctx.orgName}", you must have at least one Sanity Studio running version 5.1.0 or above. After upgrading, open your studio to connect it to the agent.`;
|
|
87
|
-
case "application_not_found":
|
|
88
|
-
return `The requested studio is not available in "${ctx.orgName}". You may not have access, or it may belong to a different organization.`;
|
|
89
|
-
default:
|
|
90
|
-
return fallback;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
function getAgentErrorMessage(error) {
|
|
94
|
-
return error instanceof SanityAgentStreamError ? error.message : error instanceof SanityAgentError && error.body.message ? error.body.message : GENERIC_ERROR_MESSAGE;
|
|
95
|
-
}
|
|
96
|
-
async function fetchApi(options) {
|
|
97
|
-
const response = await (options.fetch ?? globalThis.fetch)(options.url, {
|
|
98
|
-
method: "POST",
|
|
99
|
-
headers: options.headers,
|
|
100
|
-
body: JSON.stringify(options.body),
|
|
101
|
-
signal: options.signal,
|
|
102
|
-
...options.credentials && { credentials: options.credentials }
|
|
103
|
-
});
|
|
104
|
-
if (!response.ok) {
|
|
105
|
-
const errorText = await response.text();
|
|
106
|
-
throw new SanityAgentError(response.status, errorText);
|
|
107
|
-
}
|
|
108
|
-
return response;
|
|
109
|
-
}
|
|
110
|
-
function extractTextFromParts(content) {
|
|
111
|
-
return typeof content == "string" ? content : content.filter(
|
|
112
|
-
(part) => part.type === "text" && typeof part.text == "string"
|
|
113
|
-
).map((part) => part.text).join(`
|
|
114
|
-
`);
|
|
115
|
-
}
|
|
116
|
-
function createEmptyUsage() {
|
|
117
|
-
return {
|
|
118
|
-
inputTokens: {
|
|
119
|
-
total: 0,
|
|
120
|
-
noCache: void 0,
|
|
121
|
-
cacheRead: void 0,
|
|
122
|
-
cacheWrite: void 0
|
|
123
|
-
},
|
|
124
|
-
outputTokens: {
|
|
125
|
-
total: 0,
|
|
126
|
-
text: void 0,
|
|
127
|
-
reasoning: void 0
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
function mapFinishReason(reason) {
|
|
132
|
-
return { unified: (() => {
|
|
133
|
-
switch (reason) {
|
|
134
|
-
case "stop":
|
|
135
|
-
case "length":
|
|
136
|
-
case "content-filter":
|
|
137
|
-
case "tool-calls":
|
|
138
|
-
case "error":
|
|
139
|
-
return reason;
|
|
140
|
-
default:
|
|
141
|
-
return "other";
|
|
142
|
-
}
|
|
143
|
-
})(), raw: reason };
|
|
144
|
-
}
|
|
145
|
-
function parseStreamLine(line, textContentId) {
|
|
146
|
-
const trimmed = line.trim();
|
|
147
|
-
if (!trimmed.startsWith("data: ")) return null;
|
|
148
|
-
const payload = trimmed.slice(6);
|
|
149
|
-
if (payload === "[DONE]") return null;
|
|
150
|
-
let part;
|
|
151
|
-
try {
|
|
152
|
-
part = JSON.parse(payload);
|
|
153
|
-
} catch {
|
|
154
|
-
return null;
|
|
155
|
-
}
|
|
156
|
-
const type = part.type;
|
|
157
|
-
if (!type) return null;
|
|
158
|
-
switch (type) {
|
|
159
|
-
case "text-start":
|
|
160
|
-
return { kind: "part", part: { type: "text-start", id: part.id || textContentId } };
|
|
161
|
-
case "text-delta": {
|
|
162
|
-
const id = part.id || textContentId, delta = part.delta;
|
|
163
|
-
return typeof delta != "string" ? null : { kind: "part", part: { type: "text-delta", id, delta } };
|
|
164
|
-
}
|
|
165
|
-
case "text-end":
|
|
166
|
-
return { kind: "part", part: { type: "text-end", id: part.id || textContentId } };
|
|
167
|
-
case "tool-input-available": {
|
|
168
|
-
const input = part.input, isServerExecuted = part.providerExecuted === !0;
|
|
169
|
-
return {
|
|
170
|
-
kind: "part",
|
|
171
|
-
part: {
|
|
172
|
-
type: "tool-call",
|
|
173
|
-
toolCallId: part.toolCallId,
|
|
174
|
-
toolName: part.toolName,
|
|
175
|
-
input: typeof input == "string" ? input : JSON.stringify(input),
|
|
176
|
-
...isServerExecuted && { providerExecuted: !0, dynamic: !0 }
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
case "tool-output-available": {
|
|
181
|
-
if (part.providerExecuted !== !0) return null;
|
|
182
|
-
const result = part.output ?? {};
|
|
183
|
-
return {
|
|
184
|
-
kind: "part",
|
|
185
|
-
part: {
|
|
186
|
-
type: "tool-result",
|
|
187
|
-
toolCallId: part.toolCallId,
|
|
188
|
-
toolName: part.toolName,
|
|
189
|
-
result,
|
|
190
|
-
dynamic: !0
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
case "error": {
|
|
195
|
-
const errorText = part.errorText ?? "Unknown error", errorCode = part.errorCode;
|
|
196
|
-
return {
|
|
197
|
-
kind: "part",
|
|
198
|
-
part: {
|
|
199
|
-
type: "error",
|
|
200
|
-
error: new SanityAgentStreamError(errorText, errorCode)
|
|
201
|
-
}
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
case "abort":
|
|
205
|
-
return { kind: "data", type: "abort", data: { reason: part.reason } };
|
|
206
|
-
case "finish":
|
|
207
|
-
return {
|
|
208
|
-
kind: "finish",
|
|
209
|
-
finishReason: mapFinishReason(part.finishReason)
|
|
210
|
-
};
|
|
211
|
-
default:
|
|
212
|
-
return type.startsWith("data-") ? { kind: "data", type: type.slice(5), data: part.data } : null;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
async function extractTextFromStream(responseBody) {
|
|
216
|
-
const reader = responseBody.getReader(), decoder = new TextDecoder(), textContentId = "extract";
|
|
217
|
-
let text = "";
|
|
218
|
-
for (; ; ) {
|
|
219
|
-
const { done, value } = await reader.read();
|
|
220
|
-
if (done) break;
|
|
221
|
-
const chunk = decoder.decode(value, { stream: !0 });
|
|
222
|
-
for (const line of chunk.split(`
|
|
223
|
-
`)) {
|
|
224
|
-
const parsed = parseStreamLine(line, textContentId);
|
|
225
|
-
parsed?.kind === "part" && parsed.part.type === "text-delta" && (text += parsed.part.delta);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
return text;
|
|
229
|
-
}
|
|
230
|
-
function createSSEStream(responseBody, warnings, onData) {
|
|
231
|
-
let finishReason = { unified: "other", raw: void 0 };
|
|
232
|
-
const fallbackTextId = crypto.randomUUID();
|
|
233
|
-
let hasToolCalls = !1;
|
|
234
|
-
return new ReadableStream({
|
|
235
|
-
async start(controller) {
|
|
236
|
-
const reader = responseBody.getReader(), decoder = new TextDecoder();
|
|
237
|
-
let buffer = "";
|
|
238
|
-
controller.enqueue({ type: "stream-start", warnings });
|
|
239
|
-
try {
|
|
240
|
-
for (; ; ) {
|
|
241
|
-
const { done, value } = await reader.read();
|
|
242
|
-
if (done) break;
|
|
243
|
-
buffer += decoder.decode(value, { stream: !0 });
|
|
244
|
-
const lines = buffer.split(`
|
|
245
|
-
`);
|
|
246
|
-
buffer = lines.pop() ?? "";
|
|
247
|
-
for (const line of lines) {
|
|
248
|
-
const parsed = parseStreamLine(line, fallbackTextId);
|
|
249
|
-
parsed?.kind === "part" ? (parsed.part.type === "tool-call" && (hasToolCalls = !0), controller.enqueue(parsed.part)) : parsed?.kind === "finish" ? finishReason = parsed.finishReason : parsed?.kind === "data" && onData?.(parsed.type, parsed.data);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
if (buffer.trim()) {
|
|
253
|
-
const parsed = parseStreamLine(buffer, fallbackTextId);
|
|
254
|
-
parsed?.kind === "part" ? (parsed.part.type === "tool-call" && (hasToolCalls = !0), controller.enqueue(parsed.part)) : parsed?.kind === "finish" ? finishReason = parsed.finishReason : parsed?.kind === "data" && onData?.(parsed.type, parsed.data);
|
|
255
|
-
}
|
|
256
|
-
hasToolCalls && finishReason.unified !== "tool-calls" && (finishReason = { unified: "tool-calls", raw: finishReason.raw ?? "tool-calls" }), controller.enqueue({
|
|
257
|
-
type: "finish",
|
|
258
|
-
finishReason,
|
|
259
|
-
usage: createEmptyUsage()
|
|
260
|
-
});
|
|
261
|
-
} catch (error) {
|
|
262
|
-
controller.enqueue({
|
|
263
|
-
type: "error",
|
|
264
|
-
error: error instanceof Error ? error : new Error(String(error))
|
|
265
|
-
});
|
|
266
|
-
} finally {
|
|
267
|
-
controller.close();
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
21
|
const UNSUPPORTED_SETTINGS = [
|
|
273
22
|
"maxOutputTokens",
|
|
274
23
|
"temperature",
|
|
@@ -451,8 +200,26 @@ class ContentAgentModel {
|
|
|
451
200
|
});
|
|
452
201
|
if (!response.body)
|
|
453
202
|
throw new Error("No response body for streaming request");
|
|
203
|
+
const reconnect = async (afterSeq) => {
|
|
204
|
+
const url = buildApiUrl(
|
|
205
|
+
this.config.baseURL,
|
|
206
|
+
this.config.apiVersion,
|
|
207
|
+
`/${this.config.organizationId}/threads/${this.threadId}/stream`
|
|
208
|
+
), qs = new URLSearchParams({ afterSeq: String(afterSeq) }), res = await fetchApi({
|
|
209
|
+
url: `${url}?${qs}`,
|
|
210
|
+
method: "GET",
|
|
211
|
+
headers: buildHeaders(this.config.headers()),
|
|
212
|
+
credentials: this.config.credentials,
|
|
213
|
+
signal: options.abortSignal,
|
|
214
|
+
fetch: this.config.fetch
|
|
215
|
+
});
|
|
216
|
+
return res.status === 204 ? null : res;
|
|
217
|
+
};
|
|
454
218
|
return {
|
|
455
|
-
stream: createSSEStream(response.body, warnings,
|
|
219
|
+
stream: createSSEStream(response.body, warnings, {
|
|
220
|
+
onData: this.settings.onStreamData,
|
|
221
|
+
reconnect
|
|
222
|
+
}),
|
|
456
223
|
request: { body },
|
|
457
224
|
response: { headers: this.getResponseHeaders(response) }
|
|
458
225
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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\n/**\n * Narrow client interface — only needs `config()`.\n * Used by {@link resolveApplication} which doesn't need URL generation.\n */\nexport type SanityClientConfig = {\n config(): { projectId?: string; dataset?: string; apiHost?: string }\n}\n\n/**\n * Client interface with URL generation support.\n * Any `@sanity/client` instance satisfies this.\n */\nexport type SanityClientLike = SanityClientConfig & {\n getUrl(uri: string): string\n}\n\n/** Extracts the project-scoped API origin from a Sanity client. */\nexport function getBaseUrl(client: SanityClientLike): string {\n return new URL(client.getUrl('/')).origin\n}\n\nexport type ClientDerivedConfig = {\n baseURL: string\n application: Application\n}\n\n/**\n * Derives Content Agent configuration from a Sanity client and workspace name.\n * Builds a CORS-compatible `baseURL` and an `application` targeting the workspace.\n *\n * @param client - A Sanity client instance. Any `@sanity/client` instance works.\n * @param workspace - The active workspace name (e.g. `'default'`).\n */\nexport function fromClient(client: SanityClientLike, workspace: string): ClientDerivedConfig {\n const { projectId, dataset } = 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: getBaseUrl(client),\n application: {\n name: workspace,\n resource: { id: `${projectId}.${dataset}`, type: 'dataset' },\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\n/**\n * Error class for errors received via SSE stream events.\n * Unlike {@link SanityAgentError} (which is thrown for HTTP-level errors before\n * streaming starts), this wraps errors received mid-stream as SSE `error` events.\n *\n * The {@link code} carries the backend's error classification (e.g. `UPSTREAM_OVERLOADED`)\n * so clients can take targeted action without parsing the message string.\n *\n * @category Utilities\n */\nexport class SanityAgentStreamError extends Error {\n readonly code: string | undefined\n\n constructor(message: string, code?: string) {\n super(message)\n this.name = 'SanityAgentStreamError'\n this.code = code\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: 'upstream_overloaded' }\n | { type: 'upstream_error' }\n | { type: 'usage_limit_exceeded' }\n | { type: 'streaming_error' }\n | { type: 'unknown' }\n\nfunction mapErrorCode(code: string | undefined): AgentErrorType | null {\n switch (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 case ERROR_CODES.UPSTREAM_OVERLOADED:\n return { type: 'upstream_overloaded' }\n case ERROR_CODES.UPSTREAM_ERROR:\n return { type: 'upstream_error' }\n case ERROR_CODES.AI_USAGE_LIMIT_EXCEEDED:\n return { type: 'usage_limit_exceeded' }\n case ERROR_CODES.STREAMING_ERROR:\n return { type: 'streaming_error' }\n default:\n return null\n }\n}\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 // Handle SSE stream errors (from parseStreamLine)\n if (error instanceof SanityAgentStreamError && error.code) {\n return mapErrorCode(error.code) ?? { type: 'unknown' }\n }\n\n // Handle HTTP-level errors (from fetchApi)\n if (error instanceof SanityAgentError) {\n if (error.status !== 400) {\n return { type: 'unknown' }\n }\n return mapErrorCode(error.body.code) ?? { type: 'unknown' }\n }\n\n return { type: 'unknown' }\n}\n\nexport function isAppRelatedError(classified: AgentErrorType): boolean {\n return classified.type === 'no_applications' || classified.type === 'application_not_found'\n}\n\nexport type AgentErrorMessageContext = {\n orgName?: string | null\n}\n\n/**\n * Resolves a user-facing error message for a classified agent error.\n * For errors that benefit from additional context (e.g. org name), the optional\n * {@link resolveContext} callback is invoked lazily — only when the error type\n * needs it. Non-contextual errors return the backend message as-is.\n *\n * @category Utilities\n */\nexport async function resolveAgentErrorMessage(\n classified: AgentErrorType,\n error: unknown,\n resolveContext?: () => Promise<AgentErrorMessageContext>,\n): Promise<string> {\n const fallback = getAgentErrorMessage(error)\n\n if (!resolveContext || !isAppRelatedError(classified)) return fallback\n\n const ctx = await resolveContext()\n if (!ctx.orgName) return fallback\n\n switch (classified.type) {\n case 'no_applications':\n return `Studio action required: To use the Content Agent in \"${ctx.orgName}\", you must have at least one Sanity Studio running version 5.1.0 or above. After upgrading, open your studio to connect it to the agent.`\n case 'application_not_found':\n return `The requested studio is not available in \"${ctx.orgName}\". You may not have access, or it may belong to a different organization.`\n default:\n return fallback\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 SanityAgentStreamError) {\n return error.message\n }\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 // Forward both client- and server-executed tools to the consumer's\n // streamText loop. Stop conditions like `hasToolCall('final_answer')`\n // need to see the terminal `providerExecuted` tool call to fire; the\n // consumer's local-execute path skips tools without an `execute`\n // function (or that are not in the consumer's tool map), so a server\n // tool surfaced here cannot be double-executed. See SAGE-182.\n //\n // Server-executed tools (`providerExecuted: true`) are also marked\n // `dynamic: true`. The AI SDK's `parseToolCall` only accepts a\n // tool that is missing from the consumer's tool map when both\n // `providerExecuted` and `dynamic` are set — otherwise it throws\n // `NoSuchToolError` and emits a phantom `tool-error` chunk for a\n // tool that already executed server-side. The agent server's tools\n // are not exposed to the consumer at all (server-execute is the\n // whole point), so `dynamic` is the right flag to set here.\n const input = part.input\n const isServerExecuted = part.providerExecuted === true\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 ...(isServerExecuted && { providerExecuted: true, dynamic: true }),\n },\n }\n }\n\n case 'tool-output-available': {\n // Forward the result of a provider-executed tool (e.g. `final_answer`)\n // so the AI SDK loop can pair it with the matching `tool-call` and\n // include it in `currentModelResponse.content`. Without the result\n // chunk the loop will request another step to satisfy the dangling\n // tool call, defeating the natural termination Phase 1 enables.\n //\n // Only forward when `providerExecuted` is set. Client-tool results\n // are produced locally on the consumer side via `tool.execute`, not\n // streamed from the server, so any `tool-output-available` we see\n // for a non-providerExecuted tool would be unexpected — drop it.\n if (part.providerExecuted !== true) return null\n // The AI SDK's `tool-result` part requires a non-null JSON-serializable\n // result. Fall back to an empty object when the server omits one (e.g.\n // tools that signal completion via side effects).\n const result = (part.output ?? {}) as NonNullable<typeof part.output>\n return {\n kind: 'part',\n part: {\n type: 'tool-result',\n toolCallId: part.toolCallId as string,\n toolName: part.toolName as string,\n result,\n dynamic: true,\n },\n }\n }\n\n case 'error': {\n const errorText = (part.errorText as string) ?? 'Unknown error'\n const errorCode = part.errorCode as string | undefined\n return {\n kind: 'part',\n part: {\n type: 'error',\n error: new SanityAgentStreamError(errorText, errorCode),\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 ...(this.settings.clientId && { clientId: this.settings.clientId }),\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 { SanityClientConfig } 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 * Matches a Sanity client's `projectId` and `dataset` against the org's application list.\n * Throws if no match is found. Prefer {@link createStudioAgent} which calls this automatically.\n */\n resolveApplication(client: SanityClientConfig, 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: SanityClientConfig,\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 { getBaseUrl, type SanityClientConfig, 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 */\nexport type StudioAgentOptions = {\n /** Custom fetch implementation. Useful for testing. */\n fetch?: typeof globalThis.fetch\n\n /**\n * Additional provider settings forwarded to {@link createContentAgent}.\n * `organizationId`, `baseURL`, and `application` are derived automatically.\n */\n providerOptions?: Omit<ContentAgentProviderSettings, 'organizationId' | 'baseURL' | 'application'>\n}\n\nasync function fetchOrganizationId(\n baseURL: string,\n projectId: string,\n fetchFn: typeof globalThis.fetch,\n): Promise<string> {\n const url = `${baseURL}/${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.\n */\nfunction initProvider(\n projectId: string,\n dataset: string,\n workspace: string,\n baseURL: string,\n fetchFn: typeof globalThis.fetch,\n providerOptions?: StudioAgentOptions['providerOptions'],\n): Promise<ContentAgentProvider> {\n return fetchOrganizationId(baseURL, projectId, fetchFn).then(async (organizationId) => {\n const tempProvider = createContentAgent({\n organizationId,\n baseURL,\n fetch: fetchFn,\n })\n\n const client: SanityClientConfig = { 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 * The first call to `doGenerate` or `doStream` awaits the setup promise,\n * then delegates to the real model.\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 * The call is **synchronous** — safe for React components and `useMemo`.\n * Async setup (org ID + application resolution) is deferred to the first API call.\n *\n * @param client - A Sanity client instance. Available via `useClient()` in Studio.\n * @param workspace - The active workspace name. Available via `useWorkspace().name`.\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 fetchFn = options?.fetch ?? globalThis.fetch\n const baseURL = getBaseUrl(client)\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 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: SanityClientConfig, 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":";;AAmBO,SAAS,WAAW,QAAkC;AAC3D,SAAO,IAAI,IAAI,OAAO,OAAO,GAAG,CAAC,EAAE;AACrC;AAcO,SAAS,WAAW,QAA0B,WAAwC;AAC3F,QAAM,EAAE,WAAW,YAAY,OAAO,OAAA;AAEtC,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,yDAAyD;A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,SAAS,CAAC,SAAS;AAE9C,MAAI;AAEA,cAAY,cAAc,IAAI,QAAQ,IACxC,kBAAkB,cAAc,IAAI,QAAQ,KAE5C,kBAAkB;AAAA,IAChB;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,SAAS;AAAA,EAAA,GAGP,aACF,cAAc,IAAI,UAAU,eAAe,GAG3C,gBAAgB,MAAM,MAAM;AAC1B,kBAAc,OAAO,QAAQ;AAAA,EAC/B,CAAC;AAIL,QAAM,gBAAgB,OAAU,OAC9B,GAAG,MAAM,eAAe;AAE1B,SAAO;AAAA,IACL,MAAM,UAAkB,UAA2C;AACjE,aAAO,IAAI,sBAAsB,UAAU,UAAU,eAAe;AAAA,IACtE;AAAA,IAEA,eAAe;AACb,aAAO,cAAc,CAAC,MAAM,EAAE,cAAc;AAAA,IAC9C;AAAA,IAEA,mBAAmB,eAAmC,kBAA0B;AAC9E,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;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/from-client.ts","../src/sanity-agent-language-model.ts","../src/sanity-agent-provider.ts","../src/studio.ts"],"sourcesContent":["import type { Application } from './types'\n\n/**\n * Narrow client interface — only needs `config()`.\n * Used by {@link resolveApplication} which doesn't need URL generation.\n */\nexport type SanityClientConfig = {\n config(): { projectId?: string; dataset?: string; apiHost?: string }\n}\n\n/**\n * Client interface with URL generation support.\n * Any `@sanity/client` instance satisfies this.\n */\nexport type SanityClientLike = SanityClientConfig & {\n getUrl(uri: string): string\n}\n\n/** Extracts the project-scoped API origin from a Sanity client. */\nexport function getBaseUrl(client: SanityClientLike): string {\n return new URL(client.getUrl('/')).origin\n}\n\nexport type ClientDerivedConfig = {\n baseURL: string\n application: Application\n}\n\n/**\n * Derives Content Agent configuration from a Sanity client and workspace name.\n * Builds a CORS-compatible `baseURL` and an `application` targeting the workspace.\n *\n * @param client - A Sanity client instance. Any `@sanity/client` instance works.\n * @param workspace - The active workspace name (e.g. `'default'`).\n */\nexport function fromClient(client: SanityClientLike, workspace: string): ClientDerivedConfig {\n const { projectId, dataset } = 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: getBaseUrl(client),\n application: {\n name: workspace,\n resource: { id: `${projectId}.${dataset}`, type: 'dataset' },\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 ...(this.settings.clientId && { clientId: this.settings.clientId }),\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 // On retryable transport errors, reconnect via GET /stream?afterSeq=N\n // so the server replays only unseen frames (sidesteps vercel/ai#14688).\n const reconnect = async (afterSeq: number): Promise<Response | null> => {\n const url = buildApiUrl(\n this.config.baseURL,\n this.config.apiVersion,\n `/${this.config.organizationId}/threads/${this.threadId}/stream`,\n )\n const qs = new URLSearchParams({ afterSeq: String(afterSeq) })\n const res = await fetchApi({\n url: `${url}?${qs}`,\n method: 'GET',\n headers: buildHeaders(this.config.headers()),\n credentials: this.config.credentials,\n signal: options.abortSignal,\n fetch: this.config.fetch,\n })\n if (res.status === 204) return null\n return res\n }\n\n return {\n stream: createSSEStream(response.body, warnings, {\n onData: this.settings.onStreamData,\n reconnect,\n }),\n request: { body },\n response: { headers: this.getResponseHeaders(response) },\n }\n }\n}\n","import type { LanguageModelV3 } from '@ai-sdk/provider'\nimport type { SanityClientConfig } 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 * Matches a Sanity client's `projectId` and `dataset` against the org's application list.\n * Throws if no match is found. Prefer {@link createStudioAgent} which calls this automatically.\n */\n resolveApplication(client: SanityClientConfig, 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: SanityClientConfig,\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 { getBaseUrl, type SanityClientConfig, 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 */\nexport type StudioAgentOptions = {\n /** Custom fetch implementation. Useful for testing. */\n fetch?: typeof globalThis.fetch\n\n /**\n * Additional provider settings forwarded to {@link createContentAgent}.\n * `organizationId`, `baseURL`, and `application` are derived automatically.\n */\n providerOptions?: Omit<ContentAgentProviderSettings, 'organizationId' | 'baseURL' | 'application'>\n}\n\nasync function fetchOrganizationId(\n baseURL: string,\n projectId: string,\n fetchFn: typeof globalThis.fetch,\n): Promise<string> {\n const url = `${baseURL}/${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.\n */\nfunction initProvider(\n projectId: string,\n dataset: string,\n workspace: string,\n baseURL: string,\n fetchFn: typeof globalThis.fetch,\n providerOptions?: StudioAgentOptions['providerOptions'],\n): Promise<ContentAgentProvider> {\n return fetchOrganizationId(baseURL, projectId, fetchFn).then(async (organizationId) => {\n const tempProvider = createContentAgent({\n organizationId,\n baseURL,\n fetch: fetchFn,\n })\n\n const client: SanityClientConfig = { 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 * The first call to `doGenerate` or `doStream` awaits the setup promise,\n * then delegates to the real model.\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 * The call is **synchronous** — safe for React components and `useMemo`.\n * Async setup (org ID + application resolution) is deferred to the first API call.\n *\n * @param client - A Sanity client instance. Available via `useClient()` in Studio.\n * @param workspace - The active workspace name. Available via `useWorkspace().name`.\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 fetchFn = options?.fetch ?? globalThis.fetch\n const baseURL = getBaseUrl(client)\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 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: SanityClientConfig, 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":";;;AAmBO,SAAS,WAAW,QAAkC;AAC3D,SAAO,IAAI,IAAI,OAAO,OAAO,GAAG,CAAC,EAAE;AACrC;AAcO,SAAS,WAAW,QAA0B,WAAwC;AAC3F,QAAM,EAAE,WAAW,YAAY,OAAO,OAAA;AAEtC,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,yDAAyD;AAE3E,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,uDAAuD;AAGzE,SAAO;AAAA,IACL,SAAS,WAAW,MAAM;AAAA,IAC1B,aAAa;AA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