content-agent 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.ts +58 -145
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +57 -26
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -175,7 +175,7 @@ function MyComponent() {
|
|
|
175
175
|
|
|
176
176
|
The factory call is synchronous — async setup (org ID fetch, application resolution) runs lazily on the first API call and is cached at the module level. Wrap in `useMemo` so the provider keeps a stable identity across renders.
|
|
177
177
|
|
|
178
|
-
Since there is no token in a Studio context, `credentials: 'include'` is set on all fetch calls automatically so the browser sends session cookies. The base URL is derived from the client's `
|
|
178
|
+
Since there is no token in a Studio context, `credentials: 'include'` is set on all fetch calls automatically so the browser sends session cookies. The base URL is derived from the client's project-scoped `getUrl` method.
|
|
179
179
|
|
|
180
180
|
### Using with `useChat`
|
|
181
181
|
|
package/dist/index.d.ts
CHANGED
|
@@ -789,12 +789,10 @@ interface GenerateResult {
|
|
|
789
789
|
text: string;
|
|
790
790
|
}
|
|
791
791
|
/**
|
|
792
|
-
*
|
|
793
|
-
*
|
|
794
|
-
*
|
|
795
|
-
* @category Studio Integration
|
|
792
|
+
* Narrow client interface — only needs `config()`.
|
|
793
|
+
* Used by {@link resolveApplication} which doesn't need URL generation.
|
|
796
794
|
*/
|
|
797
|
-
type
|
|
795
|
+
type SanityClientConfig = {
|
|
798
796
|
config(): {
|
|
799
797
|
projectId?: string;
|
|
800
798
|
dataset?: string;
|
|
@@ -802,70 +800,24 @@ type SanityClientLike = {
|
|
|
802
800
|
};
|
|
803
801
|
};
|
|
804
802
|
/**
|
|
805
|
-
*
|
|
806
|
-
*
|
|
807
|
-
* @category Studio Integration
|
|
803
|
+
* Client interface with URL generation support.
|
|
804
|
+
* Any `@sanity/client` instance satisfies this.
|
|
808
805
|
*/
|
|
806
|
+
type SanityClientLike = SanityClientConfig & {
|
|
807
|
+
getUrl(uri: string): string;
|
|
808
|
+
};
|
|
809
|
+
/** Extracts the project-scoped API origin from a Sanity client. */
|
|
810
|
+
declare function getBaseUrl(client: SanityClientLike): string;
|
|
809
811
|
type ClientDerivedConfig = {
|
|
810
|
-
/**
|
|
811
|
-
* Project-scoped API base URL derived from the client's `apiHost`.
|
|
812
|
-
*
|
|
813
|
-
* Using a project-scoped host avoids CORS preflight issues when making
|
|
814
|
-
* cookie-authenticated requests from a Studio context.
|
|
815
|
-
*/
|
|
816
812
|
baseURL: string;
|
|
817
|
-
/**
|
|
818
|
-
* Application targeting the client's project, dataset, and workspace.
|
|
819
|
-
*
|
|
820
|
-
* Identified by `resource.id` (`projectId.dataset`) and `name` (workspace name),
|
|
821
|
-
* which matches the format the Content Agent API uses internally.
|
|
822
|
-
*/
|
|
823
813
|
application: Application;
|
|
824
814
|
};
|
|
825
|
-
/**
|
|
826
|
-
* Derives a project-scoped base URL from the client's configured API host.
|
|
827
|
-
* Inserts `projectId` as a subdomain so cookie-authenticated requests
|
|
828
|
-
* from Studio pass CORS checks.
|
|
829
|
-
*
|
|
830
|
-
* `https://api.sanity.io` → `https://{projectId}.api.sanity.io`
|
|
831
|
-
* `https://api.sanity.work` → `https://{projectId}.api.sanity.work`
|
|
832
|
-
*
|
|
833
|
-
* @category Studio Integration
|
|
834
|
-
*/
|
|
835
|
-
declare function deriveBaseUrl(apiHost: string | undefined, projectId: string): string;
|
|
836
815
|
/**
|
|
837
816
|
* Derives Content Agent configuration from a Sanity client and workspace name.
|
|
817
|
+
* Builds a CORS-compatible `baseURL` and an `application` targeting the workspace.
|
|
838
818
|
*
|
|
839
|
-
*
|
|
840
|
-
*
|
|
841
|
-
* to know the Studio deployment ID or application key.
|
|
842
|
-
*
|
|
843
|
-
* Designed for Studio / browser contexts where authentication is handled
|
|
844
|
-
* via cookies. Omit the `token` when creating the provider and credentials
|
|
845
|
-
* will be included automatically.
|
|
846
|
-
*
|
|
847
|
-
* @category Studio Integration
|
|
848
|
-
*
|
|
849
|
-
* @example Studio AI Assist action
|
|
850
|
-
* ```ts
|
|
851
|
-
* import { createContentAgent, fromClient } from 'content-agent'
|
|
852
|
-
* import { useClient, useWorkspace } from 'sanity'
|
|
853
|
-
*
|
|
854
|
-
* const client = useClient({ apiVersion: '2024-01-01' })
|
|
855
|
-
* const workspace = useWorkspace()
|
|
856
|
-
* const { baseURL } = fromClient(client, workspace.name)
|
|
857
|
-
*
|
|
858
|
-
* const contentAgent = createContentAgent({
|
|
859
|
-
* organizationId: 'your-org-id',
|
|
860
|
-
* baseURL,
|
|
861
|
-
* })
|
|
862
|
-
*
|
|
863
|
-
* const application = await contentAgent.resolveApplication(client, workspace.name)
|
|
864
|
-
* const model = contentAgent.agent('thread-id', { application })
|
|
865
|
-
* ```
|
|
866
|
-
*
|
|
867
|
-
* @param client - Any object with a `config()` method returning `projectId`, `dataset`, and optionally `apiHost`.
|
|
868
|
-
* @param workspace - The active workspace name (e.g. `'default'`). Available via `useWorkspace().name` in Studio.
|
|
819
|
+
* @param client - A Sanity client instance. Any `@sanity/client` instance works.
|
|
820
|
+
* @param workspace - The active workspace name (e.g. `'default'`).
|
|
869
821
|
*/
|
|
870
822
|
declare function fromClient(client: SanityClientLike, workspace: string): ClientDerivedConfig;
|
|
871
823
|
/**
|
|
@@ -1009,38 +961,10 @@ interface ContentAgentProvider {
|
|
|
1009
961
|
*/
|
|
1010
962
|
applications(): Promise<Application[]>;
|
|
1011
963
|
/**
|
|
1012
|
-
*
|
|
1013
|
-
*
|
|
1014
|
-
*
|
|
1015
|
-
* This is the easiest way to get a valid application in Studio contexts —
|
|
1016
|
-
* pass the client you already have and get back an application with the
|
|
1017
|
-
* correct `key` for the Content Agent API.
|
|
1018
|
-
*
|
|
1019
|
-
* Throws if no matching application is found (e.g. wrong org, or the Studio
|
|
1020
|
-
* hasn't been deployed yet).
|
|
1021
|
-
*
|
|
1022
|
-
* @example
|
|
1023
|
-
* ```ts
|
|
1024
|
-
* import { createContentAgent, fromClient } from 'content-agent'
|
|
1025
|
-
* import { useClient, useWorkspace } from 'sanity'
|
|
1026
|
-
*
|
|
1027
|
-
* const client = useClient({ apiVersion: '2024-01-01' })
|
|
1028
|
-
* const workspace = useWorkspace()
|
|
1029
|
-
* const { baseURL } = fromClient(client, workspace.name)
|
|
1030
|
-
*
|
|
1031
|
-
* const contentAgent = createContentAgent({
|
|
1032
|
-
* organizationId: 'your-org-id',
|
|
1033
|
-
* baseURL,
|
|
1034
|
-
* })
|
|
1035
|
-
*
|
|
1036
|
-
* const application = await contentAgent.resolveApplication(client, workspace.name)
|
|
1037
|
-
* const model = contentAgent.agent('thread-id', { application })
|
|
1038
|
-
* ```
|
|
1039
|
-
*
|
|
1040
|
-
* @param client - Any object with a `config()` method returning `projectId` and `dataset`.
|
|
1041
|
-
* @param workspace - The active workspace name (e.g. `'default'`). Available via `useWorkspace().name` in Studio.
|
|
964
|
+
* Matches a Sanity client's `projectId` and `dataset` against the org's application list.
|
|
965
|
+
* Throws if no match is found. Prefer {@link createStudioAgent} which calls this automatically.
|
|
1042
966
|
*/
|
|
1043
|
-
resolveApplication(client:
|
|
967
|
+
resolveApplication(client: SanityClientConfig, workspace: string): Promise<Application>;
|
|
1044
968
|
/**
|
|
1045
969
|
* Sends a one-shot prompt to the Content Agent (stateless).
|
|
1046
970
|
*
|
|
@@ -1127,69 +1051,23 @@ interface ContentAgentProvider {
|
|
|
1127
1051
|
declare function createContentAgent(options: ContentAgentProviderSettings): ContentAgentProvider;
|
|
1128
1052
|
/**
|
|
1129
1053
|
* Options for {@link createStudioAgent}.
|
|
1130
|
-
*
|
|
1131
|
-
* @category Studio Integration
|
|
1132
1054
|
*/
|
|
1133
1055
|
type StudioAgentOptions = {
|
|
1134
|
-
/**
|
|
1135
|
-
* Custom fetch implementation. Useful for testing.
|
|
1136
|
-
*/
|
|
1137
|
-
fetch?: typeof globalThis.fetch;
|
|
1056
|
+
/** Custom fetch implementation. Useful for testing. */fetch?: typeof globalThis.fetch;
|
|
1138
1057
|
/**
|
|
1139
1058
|
* Additional provider settings forwarded to {@link createContentAgent}.
|
|
1140
|
-
* `organizationId`, `baseURL`, and `application` are derived automatically
|
|
1141
|
-
* and cannot be overridden here.
|
|
1059
|
+
* `organizationId`, `baseURL`, and `application` are derived automatically.
|
|
1142
1060
|
*/
|
|
1143
1061
|
providerOptions?: Omit<ContentAgentProviderSettings, 'organizationId' | 'baseURL' | 'application'>;
|
|
1144
1062
|
};
|
|
1145
1063
|
/**
|
|
1146
1064
|
* Creates a Content Agent provider fully configured for a Studio context.
|
|
1147
1065
|
*
|
|
1148
|
-
*
|
|
1149
|
-
*
|
|
1150
|
-
* Async setup (fetching the organization ID and resolving the application)
|
|
1151
|
-
* is deferred to the first API call, which is already async.
|
|
1152
|
-
*
|
|
1153
|
-
* Internally handles:
|
|
1154
|
-
* 1. Deriving the project-scoped API URL (for CORS)
|
|
1155
|
-
* 2. Fetching the organization ID from project metadata
|
|
1156
|
-
* 3. Resolving the Studio application by workspace name
|
|
1157
|
-
* 4. Cookie-based auth (no token needed)
|
|
1158
|
-
*
|
|
1159
|
-
* The returned provider works exactly like one from {@link createContentAgent},
|
|
1160
|
-
* but `.agent()` and `.prompt()` automatically target the resolved application
|
|
1161
|
-
* unless explicitly overridden per-call.
|
|
1162
|
-
*
|
|
1163
|
-
* @category Studio Integration
|
|
1164
|
-
*
|
|
1165
|
-
* @example Studio component with memoized provider
|
|
1166
|
-
* ```ts
|
|
1167
|
-
* import { useMemo } from 'react'
|
|
1168
|
-
* import { createStudioAgent } from 'content-agent'
|
|
1169
|
-
* import { useClient, useWorkspace } from 'sanity'
|
|
1170
|
-
* import { generateText } from 'ai'
|
|
1171
|
-
*
|
|
1172
|
-
* function MyComponent() {
|
|
1173
|
-
* const client = useClient({ apiVersion: '2024-01-01' })
|
|
1174
|
-
* const { name: workspace } = useWorkspace()
|
|
1175
|
-
*
|
|
1176
|
-
* const contentAgent = useMemo(
|
|
1177
|
-
* () => createStudioAgent(client, workspace),
|
|
1178
|
-
* [client, workspace],
|
|
1179
|
-
* )
|
|
1180
|
-
*
|
|
1181
|
-
* async function handleSummarize() {
|
|
1182
|
-
* const { text } = await generateText({
|
|
1183
|
-
* model: contentAgent.agent('my-thread'),
|
|
1184
|
-
* prompt: 'Summarize my content',
|
|
1185
|
-
* })
|
|
1186
|
-
* console.log(text)
|
|
1187
|
-
* }
|
|
1188
|
-
* }
|
|
1189
|
-
* ```
|
|
1066
|
+
* The call is **synchronous** — safe for React components and `useMemo`.
|
|
1067
|
+
* Async setup (org ID + application resolution) is deferred to the first API call.
|
|
1190
1068
|
*
|
|
1191
|
-
* @param client -
|
|
1192
|
-
* @param workspace - The active workspace name. Available via `useWorkspace().name
|
|
1069
|
+
* @param client - A Sanity client instance. Available via `useClient()` in Studio.
|
|
1070
|
+
* @param workspace - The active workspace name. Available via `useWorkspace().name`.
|
|
1193
1071
|
* @param options - Optional overrides (custom fetch, additional provider settings).
|
|
1194
1072
|
*/
|
|
1195
1073
|
declare function createStudioAgent(client: SanityClientLike, workspace: string, options?: StudioAgentOptions): ContentAgentProvider;
|
|
@@ -1219,11 +1097,33 @@ declare class SanityAgentError extends Error {
|
|
|
1219
1097
|
constructor(status: number, message: string);
|
|
1220
1098
|
private static parseBody;
|
|
1221
1099
|
}
|
|
1100
|
+
/**
|
|
1101
|
+
* Error class for errors received via SSE stream events.
|
|
1102
|
+
* Unlike {@link SanityAgentError} (which is thrown for HTTP-level errors before
|
|
1103
|
+
* streaming starts), this wraps errors received mid-stream as SSE `error` events.
|
|
1104
|
+
*
|
|
1105
|
+
* The {@link code} carries the backend's error classification (e.g. `UPSTREAM_OVERLOADED`)
|
|
1106
|
+
* so clients can take targeted action without parsing the message string.
|
|
1107
|
+
*
|
|
1108
|
+
* @category Utilities
|
|
1109
|
+
*/
|
|
1110
|
+
declare class SanityAgentStreamError extends Error {
|
|
1111
|
+
readonly code: string | undefined;
|
|
1112
|
+
constructor(message: string, code?: string);
|
|
1113
|
+
}
|
|
1222
1114
|
declare const GENERIC_ERROR_MESSAGE = "Sorry, something went wrong. Please try again.";
|
|
1223
1115
|
type AgentErrorType = {
|
|
1224
1116
|
type: 'no_applications';
|
|
1225
1117
|
} | {
|
|
1226
1118
|
type: 'application_not_found';
|
|
1119
|
+
} | {
|
|
1120
|
+
type: 'upstream_overloaded';
|
|
1121
|
+
} | {
|
|
1122
|
+
type: 'upstream_error';
|
|
1123
|
+
} | {
|
|
1124
|
+
type: 'usage_limit_exceeded';
|
|
1125
|
+
} | {
|
|
1126
|
+
type: 'streaming_error';
|
|
1227
1127
|
} | {
|
|
1228
1128
|
type: 'unknown';
|
|
1229
1129
|
};
|
|
@@ -1235,6 +1135,19 @@ type AgentErrorType = {
|
|
|
1235
1135
|
* @category Utilities
|
|
1236
1136
|
*/
|
|
1237
1137
|
declare function classifyAgentError(error: unknown): AgentErrorType;
|
|
1138
|
+
declare function isAppRelatedError(classified: AgentErrorType): boolean;
|
|
1139
|
+
type AgentErrorMessageContext = {
|
|
1140
|
+
orgName?: string | null;
|
|
1141
|
+
};
|
|
1142
|
+
/**
|
|
1143
|
+
* Resolves a user-facing error message for a classified agent error.
|
|
1144
|
+
* For errors that benefit from additional context (e.g. org name), the optional
|
|
1145
|
+
* {@link resolveContext} callback is invoked lazily — only when the error type
|
|
1146
|
+
* needs it. Non-contextual errors return the backend message as-is.
|
|
1147
|
+
*
|
|
1148
|
+
* @category Utilities
|
|
1149
|
+
*/
|
|
1150
|
+
declare function resolveAgentErrorMessage(classified: AgentErrorType, error: unknown, resolveContext?: () => Promise<AgentErrorMessageContext>): Promise<string>;
|
|
1238
1151
|
/**
|
|
1239
1152
|
* Extracts a user-facing error message from an agent API error.
|
|
1240
1153
|
* Returns the message sent by the API when available, otherwise a generic fallback.
|
|
@@ -1325,5 +1238,5 @@ declare function extractTextFromStream(responseBody: ReadableStream<Uint8Array>)
|
|
|
1325
1238
|
* @category Utilities
|
|
1326
1239
|
*/
|
|
1327
1240
|
declare function createSSEStream(responseBody: ReadableStream<Uint8Array>, warnings: SharedV3Warning[], onData?: (type: string, data: unknown) => void): ReadableStream<LanguageModelV3StreamPart>;
|
|
1328
|
-
export { type AgentErrorType, type AgentSettings, type Application, type ApplicationResource, type Capabilities, type Capability, type CapabilityPreset, type CapabilityPresetValue, type ChatRequest, type ChatResponse, type ClientDerivedConfig, type ClientToolDefinition, type Config, ContentAgentModel, type ContentAgentModelConfig, type ContentAgentProvider, type ContentAgentProviderSettings, type ErrorResponse, type Filter, GENERIC_ERROR_MESSAGE, type GenerateOptions, type GenerateRequest, type GenerateResult, type GenerateSettings, type Message, type MessageRole, type OutputFormat, type ParsedStreamLine, SanityAgentError, type SanityClientLike, type StudioAgentOptions, type UserMessageContext, type ValidationIssue, buildApiUrl, buildHeaders, classifyAgentError, type components, createContentAgent, createSSEStream, createStudioAgent,
|
|
1241
|
+
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 };
|
|
1329
1242
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/generated/openapi.ts","../src/types.ts","../src/from-client.ts","../src/sanity-agent-language-model.ts","../src/sanity-agent-provider.ts","../src/studio.ts","../src/utils.ts"],"mappings":";;;;;AAKA;KAAY,KAAA;EACV,sCAAA;IACE,UAAA;MACE,KAAA;MACA,MAAA;MACA,IAAA;MACA,MAAA;IAAA;IAEF,GAAA;IACA,GAAA;IAAA;;;;;IAMA,IAAA,EAAM,UAAA;IACN,MAAA;IACA,OAAA;IACA,IAAA;IACA,KAAA;IACA,KAAA;EAAA;EAEF,SAAA;IACE,UAAA;MACE,KAAA;MACA,MAAA;MACA,IAAA;MACA,MAAA;IAAA;IAEF,GAAA;IACA,GAAA;IAUA;;;;AAKJ;IATI,IAAA,EAAM,UAAA;IACN,MAAA;IACA,OAAA;IACA,IAAA;IACA,KAAA;IACA,KAAA;EAAA;AAAA;AAAA,KAIQ,UAAA;EACV,OAAA;IACE,WAAA;MAiJsB,4CA/IpB,QAAA,EAAU,UAAA;MACV,WAAA,GAAc,UAAA;MACd,MAAA,GAAS,UAAA;MA6Ra;;;;MAxRtB,MAAA;MAPA;;;;;;;MAeA,MAAA;IAAA;IAEF,aAAA;MAUE;;;;MALA,cAAA;MAqBF;;;;MAhBE,OAAA;MACA,MAAA,GAAS,UAAA;MAkCT;;;;MA7BA,YAAA;MAiDA;;;;;;;MAzCA,MAAA;IAAA;IAEF,OAAA;MAmEE;;;;MA9DA,IAAA,mCA0EA;MAxEA,OAAA,UAyEA;MAvEA,EAAA,WAwEA;MAtEA,IAAA;IAAA,GAiFA;IA9EF,WAAA;MAsFsB;;;;MAjFpB,GAAA;MACA,QAAA;QAkHF;;;;QA7GI,EAAA;QAmIF;;;;QA9HE,IAAA;MAAA;MA8IJ;;;;;MAvIE,aAAA,WA8JA;MA5JA,aAAA,WA+JA;MA7JA,IAAA,WA6KsB;MA3KtB,KAAA,WAgLA;MA9KA,kBAAA;IAAA,GAwLE;IArLJ,MAAA;MAyLA;;;;MApLE,W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CjC;;;;QD2BQ,kBAAA,EAAoB,UAAA;MAAA;IAAA;EAAA;EAI1B,UAAA;EACA,aAAA;EACA,OAAA;EACA,SAAA;AAAA;AAAA,UAGe,UAAA;EACf,IAAA;IACE,UAAA;MACE,KAAA;MACA,MAAA;MACA,IAAA;QCLiC;;;;QDU/B,cAAA;QCHqC;AAuB7C;;;QDfQ,QAAA;MAAA;MAEF,MAAA;IAAA;IAEF,WAAA;MACE,OAAA;QACE,kBAAA,EAAoB,UAAA;MAAA;IAAA;IAGxB,SAAA;MCyCuB,sCDvCrB,GAAA;QACE,OAAA;UACE,cAAA;UACA,yBAAA;UAAA,CACC,IAAA;QAAA;QAEH,OAAA;UACE,mBAAA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/generated/openapi.ts","../src/types.ts","../src/from-client.ts","../src/sanity-agent-language-model.ts","../src/sanity-agent-provider.ts","../src/studio.ts","../src/utils.ts"],"mappings":";;;;;AAKA;KAAY,KAAA;EACV,sCAAA;IACE,UAAA;MACE,KAAA;MACA,MAAA;MACA,IAAA;MACA,MAAA;IAAA;IAEF,GAAA;IACA,GAAA;IAAA;;;;;IAMA,IAAA,EAAM,UAAA;IACN,MAAA;IACA,OAAA;IACA,IAAA;IACA,KAAA;IACA,KAAA;EAAA;EAEF,SAAA;IACE,UAAA;MACE,KAAA;MACA,MAAA;MACA,IAAA;MACA,MAAA;IAAA;IAEF,GAAA;IACA,GAAA;IAUA;;;;AAKJ;IATI,IAAA,EAAM,UAAA;IACN,MAAA;IACA,OAAA;IACA,IAAA;IACA,KAAA;IACA,KAAA;EAAA;AAAA;AAAA,KAIQ,UAAA;EACV,OAAA;IACE,WAAA;MAiJsB,4CA/IpB,QAAA,EAAU,UAAA;MACV,WAAA,GAAc,UAAA;MACd,MAAA,GAAS,UAAA;MA6Ra;;;;MAxRtB,MAAA;MAPA;;;;;;;MAeA,MAAA;IAAA;IAEF,aAAA;MAUE;;;;MALA,cAAA;MAqBF;;;;MAhBE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AAAA;EACpB,IAAA;EAAgB,YAAA,EAAc,2BAAA;AAAA;EAC9B,IAAA;EAAc,IAAA;EAAc,IAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;iBA0BlB,eAAA,CAAgB,IAAA,UAAc,aAAA,WAAwB,gBAAA;;;;;;;iBAyFhD,qBAAA,CACpB,YAAA,EAAc,cAAA,CAAe,UAAA,IAC5B,OAAA;;;;;;;;;;iBA+Ba,eAAA,CACd,YAAA,EAAc,cAAA,CAAe,UAAA,GAC7B,QAAA,EAAU,eAAA,IACV,MAAA,IAAU,IAAA,UAAc,IAAA,qBACvB,cAAA,CAAe,yBAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,24 +1,19 @@
|
|
|
1
1
|
import { pickBy, isString, groupBy } from "es-toolkit";
|
|
2
2
|
import { ERROR_CODES } from "@sanity/agent-types";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const url = new URL(apiHost || DEFAULT_API_HOST);
|
|
6
|
-
return url.hostname = `${projectId}.${url.hostname}`, url.origin;
|
|
3
|
+
function getBaseUrl(client) {
|
|
4
|
+
return new URL(client.getUrl("/")).origin;
|
|
7
5
|
}
|
|
8
6
|
function fromClient(client, workspace) {
|
|
9
|
-
const { projectId, dataset
|
|
7
|
+
const { projectId, dataset } = client.config();
|
|
10
8
|
if (!projectId)
|
|
11
9
|
throw new Error("fromClient: client is missing `projectId` in its config");
|
|
12
10
|
if (!dataset)
|
|
13
11
|
throw new Error("fromClient: client is missing `dataset` in its config");
|
|
14
12
|
return {
|
|
15
|
-
baseURL:
|
|
13
|
+
baseURL: getBaseUrl(client),
|
|
16
14
|
application: {
|
|
17
15
|
name: workspace,
|
|
18
|
-
resource: {
|
|
19
|
-
id: `${projectId}.${dataset}`,
|
|
20
|
-
type: "dataset"
|
|
21
|
-
}
|
|
16
|
+
resource: { id: `${projectId}.${dataset}`, type: "dataset" }
|
|
22
17
|
}
|
|
23
18
|
};
|
|
24
19
|
}
|
|
@@ -50,21 +45,53 @@ class SanityAgentError extends Error {
|
|
|
50
45
|
}
|
|
51
46
|
}
|
|
52
47
|
}
|
|
48
|
+
class SanityAgentStreamError extends Error {
|
|
49
|
+
code;
|
|
50
|
+
constructor(message, code) {
|
|
51
|
+
super(message), this.name = "SanityAgentStreamError", this.code = code;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
53
54
|
const GENERIC_ERROR_MESSAGE = "Sorry, something went wrong. Please try again.";
|
|
54
|
-
function
|
|
55
|
-
|
|
56
|
-
return { type: "unknown" };
|
|
57
|
-
switch (error.body.code) {
|
|
55
|
+
function mapErrorCode(code) {
|
|
56
|
+
switch (code) {
|
|
58
57
|
case ERROR_CODES.NO_COMPATIBLE_APPLICATIONS:
|
|
59
58
|
return { type: "no_applications" };
|
|
60
59
|
case ERROR_CODES.APPLICATION_NOT_FOUND:
|
|
61
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.`;
|
|
62
89
|
default:
|
|
63
|
-
return
|
|
90
|
+
return fallback;
|
|
64
91
|
}
|
|
65
92
|
}
|
|
66
93
|
function getAgentErrorMessage(error) {
|
|
67
|
-
return error instanceof SanityAgentError && error.body.message ? error.body.message : GENERIC_ERROR_MESSAGE;
|
|
94
|
+
return error instanceof SanityAgentStreamError ? error.message : error instanceof SanityAgentError && error.body.message ? error.body.message : GENERIC_ERROR_MESSAGE;
|
|
68
95
|
}
|
|
69
96
|
async function fetchApi(options) {
|
|
70
97
|
const response = await (options.fetch ?? globalThis.fetch)(options.url, {
|
|
@@ -150,14 +177,16 @@ function parseStreamLine(line, textContentId) {
|
|
|
150
177
|
}
|
|
151
178
|
};
|
|
152
179
|
}
|
|
153
|
-
case "error":
|
|
180
|
+
case "error": {
|
|
181
|
+
const errorText = part.errorText ?? "Unknown error", errorCode = part.errorCode;
|
|
154
182
|
return {
|
|
155
183
|
kind: "part",
|
|
156
184
|
part: {
|
|
157
185
|
type: "error",
|
|
158
|
-
error: new
|
|
186
|
+
error: new SanityAgentStreamError(errorText, errorCode)
|
|
159
187
|
}
|
|
160
188
|
};
|
|
189
|
+
}
|
|
161
190
|
case "abort":
|
|
162
191
|
return { kind: "data", type: "abort", data: { reason: part.reason } };
|
|
163
192
|
case "finish":
|
|
@@ -491,8 +520,8 @@ function createContentAgent(options) {
|
|
|
491
520
|
} };
|
|
492
521
|
}
|
|
493
522
|
const PROJECTS_API_VERSION = "v2024-01-01", providerCache = /* @__PURE__ */ new Map();
|
|
494
|
-
async function fetchOrganizationId(
|
|
495
|
-
const url = `${
|
|
523
|
+
async function fetchOrganizationId(baseURL, projectId, fetchFn) {
|
|
524
|
+
const url = `${baseURL}/${PROJECTS_API_VERSION}/projects/${projectId}`, response = await fetchFn(url, {
|
|
496
525
|
method: "GET",
|
|
497
526
|
headers: buildHeaders({}),
|
|
498
527
|
credentials: "include"
|
|
@@ -518,8 +547,8 @@ async function fetchOrganizationId(apiHost, projectId, fetchFn) {
|
|
|
518
547
|
);
|
|
519
548
|
return project.organizationId;
|
|
520
549
|
}
|
|
521
|
-
function initProvider(projectId, dataset, workspace,
|
|
522
|
-
return fetchOrganizationId(
|
|
550
|
+
function initProvider(projectId, dataset, workspace, baseURL, fetchFn, providerOptions) {
|
|
551
|
+
return fetchOrganizationId(baseURL, projectId, fetchFn).then(async (organizationId) => {
|
|
523
552
|
const tempProvider = createContentAgent({
|
|
524
553
|
organizationId,
|
|
525
554
|
baseURL,
|
|
@@ -565,13 +594,12 @@ function createStudioAgent(client, workspace, options) {
|
|
|
565
594
|
throw new Error("createStudioAgent: client is missing `projectId` in its config");
|
|
566
595
|
if (!dataset)
|
|
567
596
|
throw new Error("createStudioAgent: client is missing `dataset` in its config");
|
|
568
|
-
const
|
|
597
|
+
const fetchFn = options?.fetch ?? globalThis.fetch, baseURL = getBaseUrl(client), cacheKey = `${projectId}.${dataset}.${workspace}`, useCache = !options?.fetch && !options?.providerOptions;
|
|
569
598
|
let providerPromise;
|
|
570
599
|
useCache && providerCache.has(cacheKey) ? providerPromise = providerCache.get(cacheKey) : (providerPromise = initProvider(
|
|
571
600
|
projectId,
|
|
572
601
|
dataset,
|
|
573
602
|
workspace,
|
|
574
|
-
apiHost || "https://api.sanity.io",
|
|
575
603
|
baseURL,
|
|
576
604
|
fetchFn,
|
|
577
605
|
options?.providerOptions
|
|
@@ -598,19 +626,22 @@ export {
|
|
|
598
626
|
ContentAgentModel,
|
|
599
627
|
GENERIC_ERROR_MESSAGE,
|
|
600
628
|
SanityAgentError,
|
|
629
|
+
SanityAgentStreamError,
|
|
601
630
|
buildApiUrl,
|
|
602
631
|
buildHeaders,
|
|
603
632
|
classifyAgentError,
|
|
604
633
|
createContentAgent,
|
|
605
634
|
createSSEStream,
|
|
606
635
|
createStudioAgent,
|
|
607
|
-
deriveBaseUrl,
|
|
608
636
|
extractTextFromParts,
|
|
609
637
|
extractTextFromStream,
|
|
610
638
|
fetchApi,
|
|
611
639
|
fromClient,
|
|
612
640
|
getAgentErrorMessage,
|
|
641
|
+
getBaseUrl,
|
|
642
|
+
isAppRelatedError,
|
|
613
643
|
mapFinishReason,
|
|
614
|
-
parseStreamLine
|
|
644
|
+
parseStreamLine,
|
|
645
|
+
resolveAgentErrorMessage
|
|
615
646
|
};
|
|
616
647
|
//# sourceMappingURL=index.js.map
|
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\nconst DEFAULT_API_HOST = 'https://api.sanity.io'\n\n/**\n * Minimal interface for a Sanity client. Avoids a hard dependency on `@sanity/client` —\n * any object whose `config()` returns `projectId`, `dataset`, and `apiHost` will work.\n *\n * @category Studio Integration\n */\nexport type SanityClientLike = {\n config(): { projectId?: string; dataset?: string; apiHost?: string }\n}\n\n/**\n * Configuration derived from a Sanity client for use with {@link createContentAgent}.\n *\n * @category Studio Integration\n */\nexport type ClientDerivedConfig = {\n /**\n * Project-scoped API base URL derived from the client's `apiHost`.\n *\n * Using a project-scoped host avoids CORS preflight issues when making\n * cookie-authenticated requests from a Studio context.\n */\n baseURL: string\n\n /**\n * Application targeting the client's project, dataset, and workspace.\n *\n * Identified by `resource.id` (`projectId.dataset`) and `name` (workspace name),\n * which matches the format the Content Agent API uses internally.\n */\n application: Application\n}\n\n/**\n * Derives a project-scoped base URL from the client's configured API host.\n * Inserts `projectId` as a subdomain so cookie-authenticated requests\n * from Studio pass CORS checks.\n *\n * `https://api.sanity.io` → `https://{projectId}.api.sanity.io`\n * `https://api.sanity.work` → `https://{projectId}.api.sanity.work`\n *\n * @category Studio Integration\n */\nexport function deriveBaseUrl(apiHost: string | undefined, projectId: string): string {\n const url = new URL(apiHost || DEFAULT_API_HOST)\n url.hostname = `${projectId}.${url.hostname}`\n return url.origin\n}\n\n/**\n * Derives Content Agent configuration from a Sanity client and workspace name.\n *\n * Extracts `projectId`, `dataset`, and `apiHost` to build a CORS-compatible\n * `baseURL` and an `application` that targets the correct workspace — no need\n * to know the Studio deployment ID or application key.\n *\n * Designed for Studio / browser contexts where authentication is handled\n * via cookies. Omit the `token` when creating the provider and credentials\n * will be included automatically.\n *\n * @category Studio Integration\n *\n * @example Studio AI Assist action\n * ```ts\n * import { createContentAgent, fromClient } from 'content-agent'\n * import { useClient, useWorkspace } from 'sanity'\n *\n * const client = useClient({ apiVersion: '2024-01-01' })\n * const workspace = useWorkspace()\n * const { baseURL } = fromClient(client, workspace.name)\n *\n * const contentAgent = createContentAgent({\n * organizationId: 'your-org-id',\n * baseURL,\n * })\n *\n * const application = await contentAgent.resolveApplication(client, workspace.name)\n * const model = contentAgent.agent('thread-id', { application })\n * ```\n *\n * @param client - Any object with a `config()` method returning `projectId`, `dataset`, and optionally `apiHost`.\n * @param workspace - The active workspace name (e.g. `'default'`). Available via `useWorkspace().name` in Studio.\n */\nexport function fromClient(client: SanityClientLike, workspace: string): ClientDerivedConfig {\n const { projectId, dataset, apiHost } = client.config()\n\n if (!projectId) {\n throw new Error('fromClient: client is missing `projectId` in its config')\n }\n if (!dataset) {\n throw new Error('fromClient: client is missing `dataset` in its config')\n }\n\n return {\n baseURL: deriveBaseUrl(apiHost, projectId),\n application: {\n name: workspace,\n resource: {\n id: `${projectId}.${dataset}`,\n type: 'dataset',\n },\n },\n }\n}\n","import type {\n LanguageModelV3FinishReason,\n LanguageModelV3StreamPart,\n LanguageModelV3Usage,\n SharedV3Warning,\n} from '@ai-sdk/provider'\nimport { ERROR_CODES, type HeadlessErrorResponse } from '@sanity/agent-types'\nimport { isString, pickBy } from 'es-toolkit'\n\n/**\n * Builds headers for API requests, filtering out undefined values.\n *\n * @category Utilities\n */\nexport function buildHeaders(\n configHeaders: Record<string, string | undefined>,\n additionalHeaders?: Record<string, string>,\n): Record<string, string> {\n return pickBy(\n {\n 'Content-Type': 'application/json',\n ...additionalHeaders,\n ...configHeaders,\n },\n isString,\n )\n}\n\n/**\n * Constructs the full API URL for an endpoint.\n *\n * @category Utilities\n */\nexport function buildApiUrl(baseURL: string, apiVersion: string, endpoint: string): string {\n return `${baseURL}/${apiVersion}/agent${endpoint}`\n}\n\n/**\n * Custom error class for Sanity Agent API errors.\n * The {@link body} is parsed from the JSON response and follows the\n * {@link HeadlessErrorResponse} shape defined in `@sanity/agent-types`.\n *\n * @category Utilities\n */\nexport class SanityAgentError extends Error {\n readonly status: number\n readonly isRetryable: boolean\n readonly body: Partial<HeadlessErrorResponse>\n\n constructor(status: number, message: string) {\n super(`Sanity Agent API error: ${status} ${message}`)\n this.name = 'SanityAgentError'\n this.status = status\n this.isRetryable = status >= 500\n this.body = SanityAgentError.parseBody(message)\n }\n\n private static parseBody(message: string): Partial<HeadlessErrorResponse> {\n try {\n return JSON.parse(message)\n } catch {\n return {}\n }\n }\n}\n\nexport const GENERIC_ERROR_MESSAGE = 'Sorry, something went wrong. Please try again.'\n\nexport type AgentErrorType =\n | { type: 'no_applications' }\n | { type: 'application_not_found' }\n | { type: 'unknown' }\n\n/**\n * Classifies a caught error from the Content Agent API into a known error type.\n * Useful for clients that need to take different actions per error (e.g. show\n * resource-selection buttons). For the user-facing message, use {@link getAgentErrorMessage}.\n *\n * @category Utilities\n */\nexport function classifyAgentError(error: unknown): AgentErrorType {\n if (!(error instanceof SanityAgentError) || error.status !== 400) {\n return { type: 'unknown' }\n }\n\n switch (error.body.code) {\n case ERROR_CODES.NO_COMPATIBLE_APPLICATIONS:\n return { type: 'no_applications' }\n case ERROR_CODES.APPLICATION_NOT_FOUND:\n return { type: 'application_not_found' }\n default:\n return { type: 'unknown' }\n }\n}\n\n/**\n * Extracts a user-facing error message from an agent API error.\n * Returns the message sent by the API when available, otherwise a generic fallback.\n *\n * @category Utilities\n */\nexport function getAgentErrorMessage(error: unknown): string {\n if (error instanceof SanityAgentError && error.body.message) {\n return error.body.message\n }\n return GENERIC_ERROR_MESSAGE\n}\n\nexport interface FetchOptions {\n url: string\n body: unknown\n headers: Record<string, string>\n credentials?: RequestCredentials\n signal?: AbortSignal\n fetch?: typeof globalThis.fetch\n}\n\n/**\n * Makes a POST request and returns the response.\n * Throws SanityAgentError if the response is not ok.\n *\n * @category Utilities\n */\nexport async function fetchApi(options: FetchOptions): Promise<Response> {\n const fetchFn = options.fetch ?? globalThis.fetch\n\n const response = await fetchFn(options.url, {\n method: 'POST',\n headers: options.headers,\n body: JSON.stringify(options.body),\n signal: options.signal,\n ...(options.credentials && { credentials: options.credentials }),\n })\n\n if (!response.ok) {\n const errorText = await response.text()\n throw new SanityAgentError(response.status, errorText)\n }\n\n return response\n}\n\n/**\n * Extracts text content from message parts (user or assistant).\n *\n * @category Utilities\n */\nexport function extractTextFromParts(\n content: string | Array<{ type: string; text?: string }>,\n): string {\n if (typeof content === 'string') {\n return content\n }\n\n return content\n .filter(\n (part): part is { type: 'text'; text: string } =>\n part.type === 'text' && typeof part.text === 'string',\n )\n .map((part) => part.text)\n .join('\\n')\n}\n\n/**\n * Creates an empty V3 usage object. Our API does not return token counts.\n *\n * @category Utilities\n */\nexport function createEmptyUsage(): LanguageModelV3Usage {\n return {\n inputTokens: {\n total: 0,\n noCache: undefined,\n cacheRead: undefined,\n cacheWrite: undefined,\n },\n outputTokens: {\n total: 0,\n text: undefined,\n reasoning: undefined,\n },\n }\n}\n\n/**\n * Maps API finish reasons to LanguageModelV3FinishReason.\n *\n * @category Utilities\n */\nexport function mapFinishReason(reason: string | undefined): LanguageModelV3FinishReason {\n const unified = (() => {\n switch (reason) {\n case 'stop':\n case 'length':\n case 'content-filter':\n case 'tool-calls':\n case 'error':\n return reason\n default:\n return 'other' as const\n }\n })()\n\n return { unified, raw: reason }\n}\n\nexport type ParsedStreamLine =\n | { kind: 'part'; part: LanguageModelV3StreamPart }\n | { kind: 'finish'; finishReason: LanguageModelV3FinishReason }\n | { kind: 'data'; type: string; data: unknown }\n\n/**\n * Parses a single SSE line from the AI SDK v6 UI Message Stream protocol.\n * Returns null if the line cannot be parsed or should be ignored.\n *\n * Format: `data: <json>\\n`\n *\n * Supported types:\n * text-start – Text block start (forwarded as-is)\n * text-delta – Text chunk\n * text-end – Text block end (forwarded as-is)\n * tool-input-available – Tool call (client tools only, without providerExecuted)\n * tool-output-available – Tool result (informational, skipped)\n * finish – Finish event with reason\n * error – Error event\n * start – Stream start (skipped)\n * [DONE] – Stream termination (skipped)\n *\n * @see https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol\n *\n * @param line A single line from the SSE byte stream.\n * @param textContentId Fallback ID for text parts that lack an explicit id.\n *\n * @category Utilities\n */\nexport function parseStreamLine(line: string, textContentId: string): ParsedStreamLine | null {\n const trimmed = line.trim()\n if (!trimmed.startsWith('data: ')) return null\n\n const payload = trimmed.slice(6)\n if (payload === '[DONE]') return null\n\n let part: Record<string, unknown>\n try {\n part = JSON.parse(payload) as Record<string, unknown>\n } catch {\n return null\n }\n\n const type = part.type as string | undefined\n if (!type) return null\n\n switch (type) {\n case 'text-start': {\n const id = (part.id as string) || textContentId\n return { kind: 'part', part: { type: 'text-start', id } }\n }\n\n case 'text-delta': {\n const id = (part.id as string) || textContentId\n const delta = part.delta as string\n if (typeof delta !== 'string') return null\n return { kind: 'part', part: { type: 'text-delta', id, delta } }\n }\n\n case 'text-end': {\n const id = (part.id as string) || textContentId\n return { kind: 'part', part: { type: 'text-end', id } }\n }\n\n case 'tool-input-available': {\n // Skip server-executed tools — only forward client tools for local execution.\n if (part.providerExecuted) return null\n\n const input = part.input\n return {\n kind: 'part',\n part: {\n type: 'tool-call',\n toolCallId: part.toolCallId as string,\n toolName: part.toolName as string,\n input: typeof input === 'string' ? input : JSON.stringify(input),\n },\n }\n }\n\n case 'error': {\n return {\n kind: 'part',\n part: {\n type: 'error',\n error: new Error((part.errorText as string) ?? 'Unknown error'),\n },\n }\n }\n\n case 'abort': {\n return { kind: 'data', type: 'abort', data: { reason: part.reason } }\n }\n\n case 'finish': {\n return {\n kind: 'finish',\n finishReason: mapFinishReason(part.finishReason as string | undefined),\n }\n }\n\n default: {\n if (type.startsWith('data-')) {\n return { kind: 'data', type: type.slice(5), data: part.data }\n }\n return null\n }\n }\n}\n\n/**\n * Extracts accumulated text from a streaming response body.\n * Used for simple streaming cases where only the final text is needed.\n *\n * @category Utilities\n */\nexport async function extractTextFromStream(\n responseBody: ReadableStream<Uint8Array>,\n): Promise<string> {\n const reader = responseBody.getReader()\n const decoder = new TextDecoder()\n const textContentId = 'extract'\n let text = ''\n\n while (true) {\n const { done, value } = await reader.read()\n if (done) break\n\n const chunk = decoder.decode(value, { stream: true })\n for (const line of chunk.split('\\n')) {\n const parsed = parseStreamLine(line, textContentId)\n if (parsed?.kind === 'part' && parsed.part.type === 'text-delta') {\n text += parsed.part.delta\n }\n }\n }\n\n return text\n}\n\n/**\n * Creates a ReadableStream that parses AI SDK v6 UI Message Stream SSE data\n * from a response body and emits LanguageModelV3 stream parts.\n *\n * Text lifecycle events (text-start / text-end) are forwarded from the server.\n * Tool calls are tracked to ensure the finish reason is set correctly.\n *\n * @category Utilities\n */\nexport function createSSEStream(\n responseBody: ReadableStream<Uint8Array>,\n warnings: SharedV3Warning[],\n onData?: (type: string, data: unknown) => void,\n): ReadableStream<LanguageModelV3StreamPart> {\n let finishReason: LanguageModelV3FinishReason = { unified: 'other', raw: undefined }\n const fallbackTextId = crypto.randomUUID()\n let hasToolCalls = false\n\n return new ReadableStream<LanguageModelV3StreamPart>({\n async start(controller) {\n const reader = responseBody.getReader()\n const decoder = new TextDecoder()\n let buffer = ''\n\n controller.enqueue({ type: 'stream-start', warnings })\n\n try {\n while (true) {\n const { done, value } = await reader.read()\n if (done) break\n\n buffer += decoder.decode(value, { stream: true })\n const lines = buffer.split('\\n')\n buffer = lines.pop() ?? ''\n\n for (const line of lines) {\n const parsed = parseStreamLine(line, fallbackTextId)\n if (parsed?.kind === 'part') {\n if (parsed.part.type === 'tool-call') {\n hasToolCalls = true\n }\n controller.enqueue(parsed.part)\n } else if (parsed?.kind === 'finish') {\n finishReason = parsed.finishReason\n } else if (parsed?.kind === 'data') {\n onData?.(parsed.type, parsed.data)\n }\n }\n }\n\n if (buffer.trim()) {\n const parsed = parseStreamLine(buffer, fallbackTextId)\n if (parsed?.kind === 'part') {\n if (parsed.part.type === 'tool-call') {\n hasToolCalls = true\n }\n controller.enqueue(parsed.part)\n } else if (parsed?.kind === 'finish') {\n finishReason = parsed.finishReason\n } else if (parsed?.kind === 'data') {\n onData?.(parsed.type, parsed.data)\n }\n }\n\n // If tool calls were emitted, override finish reason so streamText\n // knows to execute tools and continue with the next step.\n if (hasToolCalls && finishReason.unified !== 'tool-calls') {\n finishReason = { unified: 'tool-calls', raw: finishReason.raw ?? 'tool-calls' }\n }\n\n controller.enqueue({\n type: 'finish',\n finishReason,\n usage: createEmptyUsage(),\n })\n } catch (error) {\n controller.enqueue({\n type: 'error',\n error: error instanceof Error ? error : new Error(String(error)),\n })\n } finally {\n controller.close()\n }\n },\n })\n}\n","import type {\n LanguageModelV3,\n LanguageModelV3CallOptions,\n LanguageModelV3Content,\n LanguageModelV3GenerateResult,\n LanguageModelV3Prompt,\n LanguageModelV3StreamResult,\n SharedV3Warning,\n} from '@ai-sdk/provider'\nimport type { ClientToolDefinition } from '@sanity/agent-types'\nimport { groupBy } from 'es-toolkit'\nimport type { AgentSettings, Message } from './types'\nimport {\n buildApiUrl,\n buildHeaders,\n createEmptyUsage,\n createSSEStream,\n extractTextFromParts,\n fetchApi,\n parseStreamLine,\n} from './utils'\n\n/**\n * @category Model\n */\nexport interface ContentAgentModelConfig {\n provider: string\n baseURL: string\n apiVersion: string\n organizationId: string\n headers: () => Record<string, string | undefined>\n credentials?: RequestCredentials\n fetch?: typeof globalThis.fetch\n}\n\n/**\n * Extended message type for the wire format that supports tool calls and tool results\n * in addition to plain text messages.\n */\ntype WireMessage =\n | Message\n | {\n role: 'assistant'\n content: Array<\n | { type: 'text'; text: string }\n | { type: 'tool-call'; toolCallId: string; toolName: string; input: string }\n >\n }\n | {\n role: 'tool'\n content: Array<{\n type: 'tool-result'\n toolCallId: string\n toolName: string\n output: unknown\n }>\n }\n\nconst UNSUPPORTED_SETTINGS = [\n 'maxOutputTokens',\n 'temperature',\n 'topP',\n 'topK',\n 'frequencyPenalty',\n 'presencePenalty',\n 'seed',\n] as const\n\n/**\n * Vercel AI SDK `LanguageModelV1` implementation that communicates with the\n * Sanity Content Agent API via a conversation thread.\n *\n * Typically you don't construct this directly — use\n * {@link createContentAgent | createContentAgent().agent(threadId)} instead.\n *\n * @category Model\n *\n * @example\n * ```ts\n * import { createContentAgent } from 'content-agent'\n * import { streamText } from 'ai'\n *\n * const contentAgent = createContentAgent({\n * organizationId: 'your-org-id',\n * token: 'your-sanity-token',\n * })\n *\n * const model = contentAgent.agent('my-thread-id', {\n * application: { key: 'projectId.datasetName' },\n * config: {\n * capabilities: { read: true, write: false },\n * },\n * })\n *\n * const { textStream } = await streamText({ model, prompt: 'Summarize my content' })\n * for await (const chunk of textStream) {\n * process.stdout.write(chunk)\n * }\n * ```\n */\nexport class ContentAgentModel implements LanguageModelV3 {\n readonly specificationVersion = 'v3' as const\n readonly provider: string\n readonly modelId: string\n readonly supportedUrls: Record<string, RegExp[]> = {}\n\n private readonly config: ContentAgentModelConfig\n private readonly threadId: string\n private readonly settings: AgentSettings\n\n constructor(\n threadId: string,\n settings: AgentSettings | undefined,\n config: ContentAgentModelConfig,\n ) {\n this.threadId = threadId\n this.modelId = `content-agent:${threadId}`\n this.provider = config.provider\n this.settings = settings ?? {}\n this.config = config\n }\n\n /**\n * Converts a LanguageModelV3Prompt into wire messages for the agent API.\n *\n * Handles all message roles:\n * - system: plain text system messages\n * - user: extracts text from content parts\n * - assistant: extracts text and/or tool-call parts\n * - tool: converts tool-result parts for the API\n */\n private convertPromptToMessages(prompt: LanguageModelV3Prompt): WireMessage[] {\n const messages: WireMessage[] = []\n\n for (const message of prompt) {\n if (message.role === 'system') {\n if (message.content) {\n messages.push({ role: 'system', content: message.content })\n }\n continue\n }\n\n if (message.role === 'tool') {\n const toolResults = message.content\n .filter((part) => part.type === 'tool-result')\n .map((part) => {\n const p = part as {\n toolCallId: string\n toolName: string\n output: { type: string; value: unknown } | unknown\n }\n return {\n type: 'tool-result' as const,\n toolCallId: p.toolCallId,\n toolName: p.toolName,\n output:\n p.output && typeof p.output === 'object' && 'value' in p.output\n ? (p.output as { value: unknown }).value\n : p.output,\n }\n })\n\n if (toolResults.length > 0) {\n messages.push({ role: 'tool', content: toolResults })\n }\n continue\n }\n\n if (message.role === 'assistant') {\n const content = message.content as Array<{\n type: string\n text?: string\n toolCallId?: string\n toolName?: string\n input?: string\n }>\n\n const grouped = groupBy(content, (p) => p.type)\n const toolCalls = grouped['tool-call'] ?? []\n const textParts = grouped.text ?? []\n\n if (toolCalls.length > 0) {\n const parts = [\n ...textParts\n .filter((p) => p.text)\n .map((p) => ({ type: 'text' as const, text: p.text! })),\n ...toolCalls.map((tc) => ({\n type: 'tool-call' as const,\n toolCallId: tc.toolCallId!,\n toolName: tc.toolName!,\n input: tc.input!,\n })),\n ]\n\n if (parts.length > 0) {\n messages.push({ role: 'assistant', content: parts })\n }\n } else {\n const text = extractTextFromParts(content as Array<{ type: string; text?: string }>)\n if (text) {\n messages.push({ role: 'assistant', content: text })\n }\n }\n continue\n }\n\n // user messages\n const text = extractTextFromParts(message.content as Array<{ type: string; text?: string }>)\n if (text) {\n messages.push({ role: message.role, content: text })\n }\n }\n\n return messages\n }\n\n /**\n * Extracts client tool definitions from the model call options.\n * Only the schemas are sent — execute functions stay on the client.\n */\n private extractClientTools(\n options: LanguageModelV3CallOptions,\n ): ClientToolDefinition[] | undefined {\n if (!options.tools?.length) return undefined\n\n const clientTools = options.tools\n .filter((t) => t.type === 'function')\n .map((t) => ({\n type: 'function' as const,\n name: t.name,\n description: t.description,\n inputSchema: t.inputSchema as Record<string, unknown>,\n }))\n\n return clientTools.length > 0 ? clientTools : undefined\n }\n\n private collectWarnings(options: LanguageModelV3CallOptions): SharedV3Warning[] {\n const warnings: SharedV3Warning[] = []\n\n for (const setting of UNSUPPORTED_SETTINGS) {\n if (options[setting] !== undefined) {\n warnings.push({ type: 'unsupported', feature: setting })\n }\n }\n\n if (options.stopSequences?.length) {\n warnings.push({ type: 'unsupported', feature: 'stopSequences' })\n }\n\n if (options.responseFormat && options.responseFormat.type !== 'text') {\n warnings.push({ type: 'unsupported', feature: 'responseFormat' })\n }\n\n return warnings\n }\n\n private buildRequestBody(options: LanguageModelV3CallOptions, stream: boolean) {\n const clientTools = this.extractClientTools(options)\n const toolChoice = options.toolChoice\n\n return {\n messages: this.convertPromptToMessages(options.prompt),\n stream,\n ...(clientTools && { clientTools }),\n ...(toolChoice && { toolChoice }),\n ...(this.settings.application && { application: this.settings.application }),\n ...(this.settings.config && { config: this.settings.config }),\n ...(this.settings.format && { format: this.settings.format }),\n }\n }\n\n private getResponseHeaders(response: Response): Record<string, string> {\n return Object.fromEntries(response.headers.entries())\n }\n\n async doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult> {\n // Always request a stream from the backend and buffer the result.\n // This keeps the HTTP connection alive with incremental data, avoiding\n // 504 gateway timeouts on long-running agent requests.\n const body = this.buildRequestBody(options, true)\n const warnings = this.collectWarnings(options)\n\n const response = await fetchApi({\n url: buildApiUrl(\n this.config.baseURL,\n this.config.apiVersion,\n `/${this.config.organizationId}/threads/${this.threadId}`,\n ),\n body,\n headers: buildHeaders(this.config.headers()),\n credentials: this.config.credentials,\n signal: options.abortSignal,\n fetch: this.config.fetch,\n })\n\n if (!response.body) {\n throw new Error('No response body for generate request')\n }\n\n const { content, finishReason } = await this.extractContentFromStream(response.body)\n\n return {\n content,\n finishReason,\n usage: createEmptyUsage(),\n request: { body },\n response: { headers: this.getResponseHeaders(response) },\n warnings,\n }\n }\n\n /**\n * Parses a streamed response body into content parts (text and tool calls)\n * and a finish reason. Used by doGenerate to buffer the full response.\n */\n private async extractContentFromStream(responseBody: ReadableStream<Uint8Array>): Promise<{\n content: LanguageModelV3Content[]\n finishReason: LanguageModelV3GenerateResult['finishReason']\n }> {\n const reader = responseBody.getReader()\n const decoder = new TextDecoder()\n const textContentId = 'generate'\n let text = ''\n let hasToolCalls = false\n const content: LanguageModelV3Content[] = []\n\n while (true) {\n const { done, value } = await reader.read()\n if (done) break\n\n const chunk = decoder.decode(value, { stream: true })\n for (const line of chunk.split('\\n')) {\n const parsed = parseStreamLine(line, textContentId)\n if (parsed?.kind === 'part') {\n if (parsed.part.type === 'text-delta') {\n text += parsed.part.delta\n } else if (parsed.part.type === 'tool-call') {\n hasToolCalls = true\n const tc = parsed.part as { toolCallId: string; toolName: string; input: string }\n content.push({\n type: 'tool-call',\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n input: tc.input,\n })\n }\n }\n }\n }\n\n if (text) {\n content.unshift({ type: 'text', text })\n }\n\n const finishReason = hasToolCalls\n ? { unified: 'tool-calls' as const, raw: 'tool-calls' }\n : { unified: 'stop' as const, raw: undefined }\n\n return { content, finishReason }\n }\n\n async doStream(options: LanguageModelV3CallOptions): Promise<LanguageModelV3StreamResult> {\n const body = this.buildRequestBody(options, true)\n const warnings = this.collectWarnings(options)\n\n const response = await fetchApi({\n url: buildApiUrl(\n this.config.baseURL,\n this.config.apiVersion,\n `/${this.config.organizationId}/threads/${this.threadId}`,\n ),\n body,\n headers: buildHeaders(this.config.headers()),\n credentials: this.config.credentials,\n signal: options.abortSignal,\n fetch: this.config.fetch,\n })\n\n if (!response.body) {\n throw new Error('No response body for streaming request')\n }\n\n return {\n stream: createSSEStream(response.body, warnings, this.settings.onStreamData),\n request: { body },\n response: { headers: this.getResponseHeaders(response) },\n }\n }\n}\n","import type { LanguageModelV3 } from '@ai-sdk/provider'\nimport type { SanityClientLike } from './from-client'\nimport { ContentAgentModel, type ContentAgentModelConfig } from './sanity-agent-language-model'\nimport type {\n AgentSettings,\n Application,\n ContentAgentProviderSettings,\n GenerateOptions,\n GenerateResult,\n GenerateSettings,\n} from './types'\nimport {\n buildApiUrl,\n buildHeaders,\n extractTextFromStream,\n fetchApi,\n SanityAgentError,\n} from './utils'\n\nconst DEFAULT_BASE_URL = 'https://api.sanity.io'\nconst DEFAULT_API_VERSION = 'v2026-01-01'\n\n/**\n * A Content Agent provider instance returned by {@link createContentAgent}.\n *\n * Provides two modes of interaction:\n * - **Conversation threads** via {@link ContentAgentProvider.agent | .agent()} — stateful, multi-turn conversations with server-side message history.\n * - **One-shot prompts** via {@link ContentAgentProvider.prompt | .prompt()} — stateless, single-turn interactions.\n *\n * @category Provider\n */\nexport interface ContentAgentProvider {\n /**\n * Creates a Vercel AI SDK {@link https://sdk.vercel.ai/docs/ai-sdk-core | LanguageModelV1} for a conversation thread.\n *\n * The thread ID identifies the conversation and maintains message history server-side.\n * If a thread with that ID already exists, the conversation continues. If not, a new\n * thread is automatically created.\n *\n * Use the returned model with `generateText` or `streamText` from the `ai` package.\n *\n * @example\n * ```ts\n * import { generateText, streamText } from 'ai'\n *\n * const model = contentAgent.agent('my-thread-id', {\n * application: { key: 'projectId.datasetName' },\n * config: {\n * capabilities: { read: true, write: false },\n * },\n * })\n *\n * // Non-streaming\n * const { text } = await generateText({ model, prompt: 'What blog posts do I have?' })\n *\n * // Streaming\n * const { textStream } = await streamText({ model, prompt: 'Summarize my content' })\n * for await (const chunk of textStream) {\n * process.stdout.write(chunk)\n * }\n * ```\n *\n * @param threadId - Unique identifier for the conversation thread.\n * @param settings - Optional agent settings (application, capabilities, filters, etc.).\n */\n agent(threadId: string, settings?: AgentSettings): LanguageModelV3\n\n /**\n * Lists available applications for the organization.\n *\n * Returns all Sanity Studio applications the authenticated user has access to.\n * Use the `key` from a returned application to target it when creating an agent.\n *\n * @example\n * ```ts\n * const apps = await contentAgent.applications()\n * const app = apps.find((app) => app.title === \"My Studio\")\n *\n * const model = contentAgent.agent(\"my-thread\", {\n * application: { key: app.key },\n * })\n * ```\n */\n applications(): Promise<Application[]>\n\n /**\n * Resolves the full application for a Sanity client by matching its `projectId`\n * and `dataset` against the organization's application list.\n *\n * This is the easiest way to get a valid application in Studio contexts —\n * pass the client you already have and get back an application with the\n * correct `key` for the Content Agent API.\n *\n * Throws if no matching application is found (e.g. wrong org, or the Studio\n * hasn't been deployed yet).\n *\n * @example\n * ```ts\n * import { createContentAgent, fromClient } from 'content-agent'\n * import { useClient, useWorkspace } from 'sanity'\n *\n * const client = useClient({ apiVersion: '2024-01-01' })\n * const workspace = useWorkspace()\n * const { baseURL } = fromClient(client, workspace.name)\n *\n * const contentAgent = createContentAgent({\n * organizationId: 'your-org-id',\n * baseURL,\n * })\n *\n * const application = await contentAgent.resolveApplication(client, workspace.name)\n * const model = contentAgent.agent('thread-id', { application })\n * ```\n *\n * @param client - Any object with a `config()` method returning `projectId` and `dataset`.\n * @param workspace - The active workspace name (e.g. `'default'`). Available via `useWorkspace().name` in Studio.\n */\n resolveApplication(client: SanityClientLike, workspace: string): Promise<Application>\n\n /**\n * Sends a one-shot prompt to the Content Agent (stateless).\n *\n * Unlike {@link ContentAgentProvider.agent | .agent()}, this does not create or use a conversation thread.\n * Each call is independent with no message history or persistence.\n * Ideal for simple, single-turn interactions.\n *\n * @example\n * ```ts\n * const { text } = await contentAgent.prompt(\n * {\n * application: { key: 'projectId.datasetName' },\n * config: { capabilities: { read: true, write: false } },\n * instructions: 'Be concise',\n * },\n * { message: 'List my 5 most recent posts' },\n * )\n * ```\n *\n * @param settings - Generation settings (application, capabilities, instructions, etc.).\n * @param options - The prompt message and request options.\n */\n prompt(settings: GenerateSettings, options: GenerateOptions): Promise<GenerateResult>\n}\n\n/**\n * Creates a Content Agent provider instance.\n *\n * The provider is the main entry point for interacting with the Sanity Content Agent API.\n * It returns an object with methods for conversation threads and one-shot prompts.\n *\n * @category Provider\n *\n * @example Basic setup\n * ```ts\n * import { createContentAgent } from 'content-agent'\n *\n * const contentAgent = createContentAgent({\n * organizationId: 'your-org-id',\n * token: 'your-sanity-token',\n * })\n * ```\n *\n * @example Using with Vercel AI SDK for conversation threads\n * ```ts\n * import { createContentAgent } from 'content-agent'\n * import { generateText, streamText } from 'ai'\n *\n * const contentAgent = createContentAgent({\n * organizationId: 'your-org-id',\n * token: 'your-sanity-token',\n * })\n *\n * const model = contentAgent.agent('my-thread-id', {\n * application: { key: 'projectId.datasetName' },\n * config: {\n * capabilities: { read: true, write: false },\n * },\n * })\n *\n * // Non-streaming\n * const { text } = await generateText({ model, prompt: 'What blog posts do I have?' })\n *\n * // Streaming\n * const { textStream } = await streamText({ model, prompt: 'Summarize my content' })\n * for await (const chunk of textStream) {\n * process.stdout.write(chunk)\n * }\n * ```\n *\n * @example One-shot generation (stateless, no thread)\n * ```ts\n * const { text } = await contentAgent.prompt(\n * {\n * application: { key: 'projectId.datasetName' },\n * config: { capabilities: { read: true, write: false } },\n * instructions: 'Be concise',\n * },\n * { message: 'List my 5 most recent posts' },\n * )\n * ```\n *\n * @param options - Provider configuration including organization ID, token, and optional overrides.\n */\nexport function createContentAgent(options: ContentAgentProviderSettings): ContentAgentProvider {\n const baseURL = options.baseURL?.replace(/\\/$/, '') ?? DEFAULT_BASE_URL\n const apiVersion = options.apiVersion ?? DEFAULT_API_VERSION\n const { organizationId } = options\n\n const getHeaders = (): Record<string, string | undefined> => ({\n ...(options.token && { Authorization: `Bearer ${options.token}` }),\n ...options.headers,\n })\n\n // When no token is provided, include credentials so the browser sends\n // cookies automatically — this is the Studio / browser auth path.\n const credentials: RequestCredentials | undefined = options.token ? undefined : 'include'\n\n const config: ContentAgentModelConfig = {\n provider: 'content-agent',\n baseURL,\n apiVersion,\n organizationId,\n headers: getHeaders,\n credentials,\n fetch: options.fetch,\n }\n\n const agent = (threadId: string, settings?: AgentSettings): LanguageModelV3 => {\n return new ContentAgentModel(\n threadId,\n {\n ...settings,\n application: settings?.application ?? options.application,\n },\n config,\n )\n }\n\n let applicationsPromise: Promise<Application[]> | null = null\n\n const applications = (): Promise<Application[]> => {\n if (!applicationsPromise) {\n applicationsPromise = (async () => {\n const fetchFn = options.fetch ?? globalThis.fetch\n const url = buildApiUrl(baseURL, apiVersion, `/${organizationId}/applications`)\n const headers = buildHeaders(getHeaders())\n\n const response = await fetchFn(url, {\n method: 'GET',\n headers,\n ...(credentials && { credentials }),\n })\n\n if (!response.ok) {\n applicationsPromise = null\n const errorText = await response.text()\n throw new SanityAgentError(response.status, errorText)\n }\n\n const json = (await response.json()) as { applications?: Application[] }\n return json.applications ?? []\n })()\n }\n\n return applicationsPromise\n }\n\n const prompt = async (\n settings: GenerateSettings,\n generateOptions: GenerateOptions,\n ): Promise<GenerateResult> => {\n const effectiveApplication = settings.application ?? options.application\n\n const body = {\n organizationId,\n message: generateOptions.message,\n stream: generateOptions.stream ?? false,\n ...(effectiveApplication && { application: effectiveApplication }),\n ...(settings.config && { config: settings.config }),\n ...(settings.instructions && { instructions: settings.instructions }),\n ...(settings.format && { format: settings.format }),\n }\n\n const response = await fetchApi({\n url: buildApiUrl(baseURL, apiVersion, '/prompt'),\n body,\n headers: buildHeaders(getHeaders()),\n credentials,\n signal: generateOptions.abortSignal,\n fetch: options.fetch,\n })\n\n if (body.stream) {\n if (!response.body) {\n throw new SanityAgentError(500, 'No response body')\n }\n return { text: await extractTextFromStream(response.body) }\n }\n\n const json = (await response.json()) as { text?: string }\n return { text: json.text ?? '' }\n }\n\n const resolveApplication = async (\n client: SanityClientLike,\n workspace: string,\n ): Promise<Application> => {\n const { projectId, dataset } = client.config()\n if (!projectId || !dataset) {\n throw new Error(\n 'resolveApplication: client is missing `projectId` or `dataset` in its config',\n )\n }\n\n const resourceId = `${projectId}.${dataset}`\n const apps = await applications()\n const match = apps.find((app) => app.resource?.id === resourceId && app.name === workspace)\n\n if (!match) {\n throw new SanityAgentError(\n 400,\n JSON.stringify({\n code: 'APPLICATION_NOT_FOUND',\n message: `No application found for resource \"${resourceId}\" with workspace \"${workspace}\". Make sure the Studio has been deployed and belongs to this organization.`,\n }),\n )\n }\n\n return match\n }\n\n return { agent, applications, resolveApplication, prompt }\n}\n","import type { LanguageModelV3, LanguageModelV3CallOptions } from '@ai-sdk/provider'\nimport { deriveBaseUrl, type SanityClientLike } from './from-client'\nimport type { ContentAgentProvider } from './sanity-agent-provider'\nimport { createContentAgent } from './sanity-agent-provider'\nimport type { AgentSettings, ContentAgentProviderSettings } from './types'\nimport { buildHeaders, SanityAgentError } from './utils'\n\nconst PROJECTS_API_VERSION = 'v2024-01-01'\n\n/**\n * Module-level cache for setup promises. Keyed by `projectId.dataset.workspace`,\n * so repeated calls to `createStudioAgent` with the same params (e.g. React strict\n * mode double-render, component remounts) reuse the same in-flight or resolved setup.\n */\nconst providerCache = new Map<string, Promise<ContentAgentProvider>>()\n\ntype ProjectResponse = {\n id: string\n organizationId?: string | null\n}\n\n/**\n * Options for {@link createStudioAgent}.\n *\n * @category Studio Integration\n */\nexport type StudioAgentOptions = {\n /**\n * Custom fetch implementation. Useful for testing.\n */\n fetch?: typeof globalThis.fetch\n\n /**\n * Additional provider settings forwarded to {@link createContentAgent}.\n * `organizationId`, `baseURL`, and `application` are derived automatically\n * and cannot be overridden here.\n */\n providerOptions?: Omit<ContentAgentProviderSettings, 'organizationId' | 'baseURL' | 'application'>\n}\n\nasync function fetchOrganizationId(\n apiHost: string,\n projectId: string,\n fetchFn: typeof globalThis.fetch,\n): Promise<string> {\n const url = `${apiHost}/${PROJECTS_API_VERSION}/projects/${projectId}`\n\n const response = await fetchFn(url, {\n method: 'GET',\n headers: buildHeaders({}),\n credentials: 'include',\n })\n\n if (!response.ok) {\n const errorText = await response.text()\n throw new SanityAgentError(\n response.status,\n JSON.stringify({\n code: 'PROJECT_FETCH_FAILED',\n message: `Failed to fetch project \"${projectId}\": ${errorText}`,\n }),\n )\n }\n\n const project = (await response.json()) as ProjectResponse\n\n if (!project.organizationId) {\n throw new SanityAgentError(\n 400,\n JSON.stringify({\n code: 'NO_ORGANIZATION',\n message: `Project \"${projectId}\" is not assigned to an organization. Content Agent requires an organization-level project.`,\n }),\n )\n }\n\n return project.organizationId\n}\n\n/**\n * Resolves the full provider by fetching org ID and application. The returned\n * promise is cached so the setup only runs once regardless of how many calls\n * go through the lazy wrapper.\n */\nfunction initProvider(\n projectId: string,\n dataset: string,\n workspace: string,\n apiHost: string,\n baseURL: string,\n fetchFn: typeof globalThis.fetch,\n providerOptions?: StudioAgentOptions['providerOptions'],\n): Promise<ContentAgentProvider> {\n return fetchOrganizationId(apiHost, projectId, fetchFn).then(async (organizationId) => {\n const tempProvider = createContentAgent({\n organizationId,\n baseURL,\n fetch: fetchFn,\n })\n\n // Reuse the provider's own resolveApplication logic — no duplicated matching\n const client: SanityClientLike = { config: () => ({ projectId, dataset }) }\n const application = await tempProvider.resolveApplication(client, workspace)\n\n return createContentAgent({\n ...providerOptions,\n organizationId,\n baseURL,\n application,\n })\n })\n}\n\n/**\n * A {@link LanguageModelV3} that defers to a lazily-resolved provider.\n *\n * The first call to `doGenerate` or `doStream` awaits the setup promise,\n * then delegates to the real model. The resolved model is cached for\n * subsequent calls.\n */\nclass LazyContentAgentModel implements LanguageModelV3 {\n readonly specificationVersion = 'v3' as const\n readonly provider = 'content-agent'\n readonly modelId: string\n readonly supportedUrls: Record<string, RegExp[]> = {}\n\n private readonly providerPromise: Promise<ContentAgentProvider>\n private readonly threadId: string\n private readonly settings: AgentSettings | undefined\n private resolvedModel: LanguageModelV3 | null = null\n\n constructor(\n threadId: string,\n settings: AgentSettings | undefined,\n providerPromise: Promise<ContentAgentProvider>,\n ) {\n this.threadId = threadId\n this.settings = settings\n this.modelId = `content-agent:${threadId}`\n this.providerPromise = providerPromise\n }\n\n private async getModel(): Promise<LanguageModelV3> {\n if (!this.resolvedModel) {\n const provider = await this.providerPromise\n this.resolvedModel = provider.agent(this.threadId, this.settings)\n }\n return this.resolvedModel\n }\n\n async doGenerate(options: LanguageModelV3CallOptions) {\n const model = await this.getModel()\n return model.doGenerate(options)\n }\n\n async doStream(options: LanguageModelV3CallOptions) {\n const model = await this.getModel()\n return model.doStream(options)\n }\n}\n\n/**\n * Creates a Content Agent provider fully configured for a Studio context.\n *\n * This is the simplest way to use Content Agent from inside Sanity Studio.\n * The call is **synchronous** — safe to use in React components and hooks.\n * Async setup (fetching the organization ID and resolving the application)\n * is deferred to the first API call, which is already async.\n *\n * Internally handles:\n * 1. Deriving the project-scoped API URL (for CORS)\n * 2. Fetching the organization ID from project metadata\n * 3. Resolving the Studio application by workspace name\n * 4. Cookie-based auth (no token needed)\n *\n * The returned provider works exactly like one from {@link createContentAgent},\n * but `.agent()` and `.prompt()` automatically target the resolved application\n * unless explicitly overridden per-call.\n *\n * @category Studio Integration\n *\n * @example Studio component with memoized provider\n * ```ts\n * import { useMemo } from 'react'\n * import { createStudioAgent } from 'content-agent'\n * import { useClient, useWorkspace } from 'sanity'\n * import { generateText } from 'ai'\n *\n * function MyComponent() {\n * const client = useClient({ apiVersion: '2024-01-01' })\n * const { name: workspace } = useWorkspace()\n *\n * const contentAgent = useMemo(\n * () => createStudioAgent(client, workspace),\n * [client, workspace],\n * )\n *\n * async function handleSummarize() {\n * const { text } = await generateText({\n * model: contentAgent.agent('my-thread'),\n * prompt: 'Summarize my content',\n * })\n * console.log(text)\n * }\n * }\n * ```\n *\n * @param client - Any object with a `config()` returning `projectId` and `dataset`.\n * @param workspace - The active workspace name. Available via `useWorkspace().name` in Studio.\n * @param options - Optional overrides (custom fetch, additional provider settings).\n */\nexport function createStudioAgent(\n client: SanityClientLike,\n workspace: string,\n options?: StudioAgentOptions,\n): ContentAgentProvider {\n const { projectId, dataset } = client.config()\n\n if (!projectId) {\n throw new Error('createStudioAgent: client is missing `projectId` in its config')\n }\n if (!dataset) {\n throw new Error('createStudioAgent: client is missing `dataset` in its config')\n }\n\n const { apiHost } = client.config()\n const fetchFn = options?.fetch ?? globalThis.fetch\n const baseURL = deriveBaseUrl(apiHost, projectId)\n const cacheKey = `${projectId}.${dataset}.${workspace}`\n\n // Reuse an existing setup promise for the same project/dataset/workspace,\n // or start a new one. Custom fetch or providerOptions bypass the cache\n // since they indicate a non-default configuration (e.g. tests).\n const useCache = !options?.fetch && !options?.providerOptions\n\n let providerPromise: Promise<ContentAgentProvider>\n\n if (useCache && providerCache.has(cacheKey)) {\n providerPromise = providerCache.get(cacheKey)!\n } else {\n providerPromise = initProvider(\n projectId,\n dataset,\n workspace,\n apiHost || 'https://api.sanity.io',\n baseURL,\n fetchFn,\n options?.providerOptions,\n )\n\n if (useCache) {\n providerCache.set(cacheKey, providerPromise)\n\n // Evict on failure so a transient error doesn't stick forever\n providerPromise.catch(() => {\n providerCache.delete(cacheKey)\n })\n }\n }\n\n const awaitProvider = async <T>(fn: (provider: ContentAgentProvider) => Promise<T>): Promise<T> =>\n fn(await providerPromise)\n\n return {\n agent(threadId: string, settings?: AgentSettings): LanguageModelV3 {\n return new LazyContentAgentModel(threadId, settings, providerPromise)\n },\n\n applications() {\n return awaitProvider((p) => p.applications())\n },\n\n resolveApplication(resolveClient: SanityClientLike, resolveWorkspace: string) {\n return awaitProvider((p) => p.resolveApplication(resolveClient, resolveWorkspace))\n },\n\n prompt(settings, generateOptions) {\n return awaitProvider((p) => p.prompt(settings, generateOptions))\n },\n }\n}\n"],"names":["text"],"mappings":";;AAEA,MAAM,mBAAmB;AA6ClB,SAAS,cAAc,SAA6B,WAA2B;AACpF,QAAM,MAAM,IAAI,IAAI,WAAW,gBAAgB;AAC/C,SAAA,IAAI,WAAW,GAAG,SAAS,IAAI,IAAI,QAAQ,IAC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,WAAW,CAAC,SAAS,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,WAAW;AAAA,IACX;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,eAAiC,kBAA0B;AAC5E,aAAO,cAAc,CAAC,MAAM,EAAE,mBAAmB,eAAe,gBAAgB,CAAC;AAAA,IACnF;AAAA,IAEA,OAAO,UAAU,iBAAiB;AAChC,aAAO,cAAc,CAAC,MAAM,EAAE,OAAO,UAAU,eAAe,CAAC;AAAA,IACjE;AAAA,EAAA;AAEJ;"}
|
|
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 // Skip server-executed tools — only forward client tools for local execution.\n if (part.providerExecuted) return null\n\n const input = part.input\n return {\n kind: 'part',\n part: {\n type: 'tool-call',\n toolCallId: part.toolCallId as string,\n toolName: part.toolName as string,\n input: typeof input === 'string' ? input : JSON.stringify(input),\n },\n }\n }\n\n case 'error': {\n 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 }\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;AAE3E,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,uDAAuD;AAGzE,SAAO;AAAA,IACL,SAAS,WAAW,MAAM;AAAA,IAC1B,aAAa;AAAA,MACX,MAAM;AAAA,MACN,UAAU,EAAE,IAAI,GAAG,SAAS,IAAI,OAAO,IAAI,MAAM,UAAA;AAAA,IAAU;AAAA,EAC7D;AAEJ;ACtCO,SAAS,aACd,eACA,mBACwB;AACxB,SAAO;AAAA,IACL;AAAA,MACE,gBAAgB;AAAA,MAChB,GAAG;AAAA,MACH,GAAG;AAAA,IAAA;AAAA,IAEL;AAAA,EAAA;AAEJ;AAOO,SAAS,YAAY,SAAiB,YAAoB,UAA0B;AACzF,SAAO,GAAG,OAAO,I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