@ragable/sdk 0.7.7 → 0.7.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +183 -2
- package/dist/index.d.ts +183 -2
- package/dist/index.js +353 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +348 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1062,6 +1062,8 @@ declare function mapAgentEvent(event: AgentStreamLikeEvent): StreamPart | null;
|
|
|
1062
1062
|
* against the website's organization.
|
|
1063
1063
|
*/
|
|
1064
1064
|
|
|
1065
|
+
/** JSON Schema for a structured response. Plain object — no Zod dep. */
|
|
1066
|
+
type JsonSchema = Record<string, unknown>;
|
|
1065
1067
|
interface StreamTextParams {
|
|
1066
1068
|
model: string;
|
|
1067
1069
|
messages: Message[];
|
|
@@ -1100,15 +1102,87 @@ interface GenerateTextResult {
|
|
|
1100
1102
|
finishReason: FinishReason;
|
|
1101
1103
|
toolCalls: ToolCallRecord[];
|
|
1102
1104
|
}
|
|
1105
|
+
interface StreamObjectParams {
|
|
1106
|
+
model: string;
|
|
1107
|
+
/**
|
|
1108
|
+
* JSON Schema describing the expected output. Constrains the model via the
|
|
1109
|
+
* Fireworks `response_format: { type: "json_object", schema }` mode.
|
|
1110
|
+
*/
|
|
1111
|
+
schema: JsonSchema;
|
|
1112
|
+
/** Optional name for the schema (passed through to providers that support it). */
|
|
1113
|
+
schemaName?: string;
|
|
1114
|
+
/** Optional natural-language description of the schema. */
|
|
1115
|
+
schemaDescription?: string;
|
|
1116
|
+
messages: Message[];
|
|
1117
|
+
system?: string;
|
|
1118
|
+
temperature?: number;
|
|
1119
|
+
maxTokens?: number;
|
|
1120
|
+
topP?: number;
|
|
1121
|
+
signal?: AbortSignal;
|
|
1122
|
+
}
|
|
1123
|
+
interface StreamObjectResult<T = unknown> {
|
|
1124
|
+
/** Raw text deltas — the JSON string as it streams in. */
|
|
1125
|
+
textStream: AsyncIterable<string>;
|
|
1126
|
+
/**
|
|
1127
|
+
* Async iterable of best-effort parsed partial objects. Emits whenever the
|
|
1128
|
+
* accumulated JSON parses cleanly under the partial-JSON repair rules.
|
|
1129
|
+
* Subsequent values are *replacements*, not patches — each is a snapshot.
|
|
1130
|
+
*/
|
|
1131
|
+
partialObjectStream: AsyncIterable<T>;
|
|
1132
|
+
/** Resolves to the final parsed object once the stream ends. Rejects if the final JSON is invalid. */
|
|
1133
|
+
object: Promise<T>;
|
|
1134
|
+
/** Resolves to the raw JSON string the model produced. */
|
|
1135
|
+
text: Promise<string>;
|
|
1136
|
+
usage: Promise<TokenUsage>;
|
|
1137
|
+
finishReason: Promise<FinishReason>;
|
|
1138
|
+
/**
|
|
1139
|
+
* Resolves to tool calls the model made during this turn.
|
|
1140
|
+
* Empty for `client.ai.streamObject` (no tools exposed on raw inference);
|
|
1141
|
+
* populated for `client.agents.runObject` when the agent has tools.
|
|
1142
|
+
*/
|
|
1143
|
+
toolCalls: Promise<ToolCallRecord[]>;
|
|
1144
|
+
}
|
|
1145
|
+
interface GenerateObjectResult<T = unknown> {
|
|
1146
|
+
object: T;
|
|
1147
|
+
usage: TokenUsage;
|
|
1148
|
+
finishReason: FinishReason;
|
|
1149
|
+
toolCalls: ToolCallRecord[];
|
|
1150
|
+
}
|
|
1103
1151
|
/**
|
|
1104
1152
|
* Build the JSON body for the inference proxy. Public so tests can assert it.
|
|
1153
|
+
* Accepts an optional `responseFormat` so the same builder serves both
|
|
1154
|
+
* `streamText` and `streamObject`.
|
|
1105
1155
|
*/
|
|
1106
|
-
declare function buildInferenceRequestBody(params: StreamTextParams): Record<string, unknown>;
|
|
1156
|
+
declare function buildInferenceRequestBody(params: StreamTextParams, responseFormat?: Record<string, unknown>): Record<string, unknown>;
|
|
1157
|
+
/**
|
|
1158
|
+
* Build the `response_format` payload for a JSON-Schema-constrained call.
|
|
1159
|
+
* Emits Fireworks' canonical shape (also accepted by OpenAI):
|
|
1160
|
+
*
|
|
1161
|
+
* { type: "json_schema",
|
|
1162
|
+
* json_schema: { name, schema, description?, strict? } }
|
|
1163
|
+
*
|
|
1164
|
+
* @see https://docs.fireworks.ai/structured-responses/structured-response-formatting
|
|
1165
|
+
*/
|
|
1166
|
+
declare function buildResponseFormat(params: {
|
|
1167
|
+
schema: JsonSchema;
|
|
1168
|
+
name?: string;
|
|
1169
|
+
description?: string;
|
|
1170
|
+
/** Default `true` — providers ignore unknown fields safely. */
|
|
1171
|
+
strict?: boolean;
|
|
1172
|
+
}): Record<string, unknown>;
|
|
1107
1173
|
/**
|
|
1108
1174
|
* Build a `StreamTextResult` from a pre-existing async iterable of StreamParts.
|
|
1109
1175
|
* Used by `client.agents.run()` to share the same DX as `client.ai.streamText`.
|
|
1110
1176
|
*/
|
|
1111
1177
|
declare function createStreamResultFromParts(source: AsyncIterable<StreamPart>): StreamTextResult;
|
|
1178
|
+
interface InferenceRequestContext {
|
|
1179
|
+
/** Resolved URL of the inference endpoint. */
|
|
1180
|
+
url: string;
|
|
1181
|
+
/** Pre-built request headers (`Content-Type` already set). */
|
|
1182
|
+
headers: Headers;
|
|
1183
|
+
/** Bound fetch (already wraps user `fetch` option). */
|
|
1184
|
+
fetch: typeof fetch;
|
|
1185
|
+
}
|
|
1112
1186
|
interface AiClientOptions {
|
|
1113
1187
|
organizationId: string;
|
|
1114
1188
|
websiteId?: string;
|
|
@@ -1125,7 +1199,52 @@ declare class RagableBrowserAiClient {
|
|
|
1125
1199
|
private buildContext;
|
|
1126
1200
|
streamText(params: StreamTextParams): StreamTextResult;
|
|
1127
1201
|
generateText(params: StreamTextParams): Promise<GenerateTextResult>;
|
|
1202
|
+
/**
|
|
1203
|
+
* Stream a JSON-Schema-constrained response. Matches Vercel AI SDK's
|
|
1204
|
+
* `streamObject` shape — returns a synchronous result with `partialObjectStream`
|
|
1205
|
+
* (best-effort incremental parses) and `object` (the final parsed JSON).
|
|
1206
|
+
*
|
|
1207
|
+
* ```ts
|
|
1208
|
+
* const { partialObjectStream, object } = client.ai.streamObject({
|
|
1209
|
+
* model: "accounts/fireworks/models/kimi-k2p5",
|
|
1210
|
+
* schema: {
|
|
1211
|
+
* type: "object",
|
|
1212
|
+
* properties: {
|
|
1213
|
+
* title: { type: "string" },
|
|
1214
|
+
* tags: { type: "array", items: { type: "string" } },
|
|
1215
|
+
* },
|
|
1216
|
+
* required: ["title", "tags"],
|
|
1217
|
+
* },
|
|
1218
|
+
* messages: [{ role: "user", content: "Give me a blog post idea about AI." }],
|
|
1219
|
+
* });
|
|
1220
|
+
* for await (const partial of partialObjectStream) renderPreview(partial);
|
|
1221
|
+
* const final = await object;
|
|
1222
|
+
* ```
|
|
1223
|
+
*/
|
|
1224
|
+
streamObject<T = unknown>(params: StreamObjectParams): StreamObjectResult<T>;
|
|
1225
|
+
/**
|
|
1226
|
+
* Non-streaming variant of {@link streamObject}. Resolves once the model
|
|
1227
|
+
* finishes; rejects if the final text isn't valid JSON for the schema.
|
|
1228
|
+
*/
|
|
1229
|
+
generateObject<T = unknown>(params: StreamObjectParams): Promise<GenerateObjectResult<T>>;
|
|
1128
1230
|
}
|
|
1231
|
+
/**
|
|
1232
|
+
* Open a JSON-mode inference stream against the proxy and return a
|
|
1233
|
+
* `StreamObjectResult`. Exposed for advanced callers; most code should use
|
|
1234
|
+
* `RagableBrowserAiClient.streamObject`.
|
|
1235
|
+
*/
|
|
1236
|
+
declare function streamObjectFromContext<T = unknown>(ctx: InferenceRequestContext, params: StreamObjectParams): StreamObjectResult<T>;
|
|
1237
|
+
/**
|
|
1238
|
+
* Wrap a `StreamTextResult` (text-only stream) into a `StreamObjectResult`
|
|
1239
|
+
* by parsing the accumulated text as JSON. The text path stays the source of
|
|
1240
|
+
* truth for `text`/`usage`/`finishReason`/`toolCalls`; this helper only adds
|
|
1241
|
+
* `partialObjectStream` and `object` on top.
|
|
1242
|
+
*
|
|
1243
|
+
* Public so `client.agents.runObject` can reuse the parsing logic — agents
|
|
1244
|
+
* stream text through their own SSE protocol, but the structured-output
|
|
1245
|
+
* conversion is identical.
|
|
1246
|
+
*/
|
|
1247
|
+
declare function wrapStreamTextAsObject<T = unknown>(inner: StreamTextResult): StreamObjectResult<T>;
|
|
1129
1248
|
|
|
1130
1249
|
/** Canonical browser/server API base (`…/api`, no trailing slash). */
|
|
1131
1250
|
declare function normalizeBrowserApiBase(): string;
|
|
@@ -1720,6 +1839,52 @@ declare class RagableBrowserAgentsClient {
|
|
|
1720
1839
|
messages: Message[];
|
|
1721
1840
|
signal?: AbortSignal;
|
|
1722
1841
|
}): StreamTextResult;
|
|
1842
|
+
/**
|
|
1843
|
+
* Same agent, same tools/instructions/RAG — but constrain the final output
|
|
1844
|
+
* to a JSON Schema. Matches `client.ai.streamObject` exactly so callers can
|
|
1845
|
+
* swap between raw inference and an agent without changing call shape.
|
|
1846
|
+
*
|
|
1847
|
+
* Tool calling and structured output are **compatible**: the model may call
|
|
1848
|
+
* the agent's tools as usual; the final assistant message is the schema-
|
|
1849
|
+
* conformant JSON. The agent's `agents/<name>.json` is unchanged — whether
|
|
1850
|
+
* the run is conversational or structured is decided by the call site.
|
|
1851
|
+
*
|
|
1852
|
+
* ```ts
|
|
1853
|
+
* const { partialObjectStream, object } = client.agents.runObject<Plan>(
|
|
1854
|
+
* "planner",
|
|
1855
|
+
* {
|
|
1856
|
+
* messages: [{ role: "user", content: "Plan a 3-day trip to Kyoto." }],
|
|
1857
|
+
* schema: {
|
|
1858
|
+
* type: "object",
|
|
1859
|
+
* properties: {
|
|
1860
|
+
* days: {
|
|
1861
|
+
* type: "array",
|
|
1862
|
+
* items: { type: "object", properties: { date: { type: "string" }, activities: { type: "array", items: { type: "string" } } } },
|
|
1863
|
+
* },
|
|
1864
|
+
* },
|
|
1865
|
+
* required: ["days"],
|
|
1866
|
+
* },
|
|
1867
|
+
* },
|
|
1868
|
+
* );
|
|
1869
|
+
* for await (const p of partialObjectStream) renderPreview(p);
|
|
1870
|
+
* console.log(await object);
|
|
1871
|
+
* ```
|
|
1872
|
+
*/
|
|
1873
|
+
runObject<T = unknown>(agentName: string, params: {
|
|
1874
|
+
messages: Message[];
|
|
1875
|
+
schema: JsonSchema;
|
|
1876
|
+
schemaName?: string;
|
|
1877
|
+
schemaDescription?: string;
|
|
1878
|
+
signal?: AbortSignal;
|
|
1879
|
+
}): StreamObjectResult<T>;
|
|
1880
|
+
/** Non-streaming variant of {@link runObject}. */
|
|
1881
|
+
generateObject<T = unknown>(agentName: string, params: {
|
|
1882
|
+
messages: Message[];
|
|
1883
|
+
schema: JsonSchema;
|
|
1884
|
+
schemaName?: string;
|
|
1885
|
+
schemaDescription?: string;
|
|
1886
|
+
signal?: AbortSignal;
|
|
1887
|
+
}): Promise<GenerateObjectResult<T>>;
|
|
1723
1888
|
private runStreamParts;
|
|
1724
1889
|
/**
|
|
1725
1890
|
* @deprecated Use {@link run} for new code. This method is kept for
|
|
@@ -1790,6 +1955,22 @@ declare function parseSseDataLine(line: string): SseJsonEvent | null;
|
|
|
1790
1955
|
*/
|
|
1791
1956
|
declare function readSseStream(body: ReadableStream<Uint8Array>): AsyncGenerator<SseJsonEvent, void, undefined>;
|
|
1792
1957
|
|
|
1958
|
+
/**
|
|
1959
|
+
* Best-effort parser for incomplete JSON streamed token-by-token from an LLM.
|
|
1960
|
+
*
|
|
1961
|
+
* Used by `streamObject` to emit partial objects as the model writes them.
|
|
1962
|
+
* Strategy:
|
|
1963
|
+
* 1. Try `JSON.parse(text)` — if it works, the JSON is already complete.
|
|
1964
|
+
* 2. Otherwise scan the text and close any open `{`, `[`, or string literal
|
|
1965
|
+
* so the result is parseable.
|
|
1966
|
+
* 3. If a trailing comma is the problem, strip it and retry.
|
|
1967
|
+
*
|
|
1968
|
+
* Returns `undefined` when no parseable prefix can be recovered.
|
|
1969
|
+
*
|
|
1970
|
+
* Designed to be allocation-light and dep-free.
|
|
1971
|
+
*/
|
|
1972
|
+
declare function tryParsePartialJson(text: string): unknown | undefined;
|
|
1973
|
+
|
|
1793
1974
|
declare function createClient<Database extends RagableDatabase = DefaultRagableDatabase, AuthUser extends object = DefaultAuthUser>(options: RagableBrowserClientOptions): RagableBrowser<Database, AuthUser>;
|
|
1794
1975
|
|
|
1795
|
-
export { type AgentChatMessage, type AgentChatParams, type AgentChatStreamDonePayload, type AgentChatStreamHandlers, type AgentChatStreamResult, type AgentChatStreamUiHandlers, type AgentChatUiAssistantMessage, type AgentChatUiSegment, type AgentChatUiStreamResult, type AgentConversation, type AgentConversationMessage, type AgentConversationSubscription, type AgentPublicChatParams, type AgentStreamAgentInfoEvent, type AgentStreamEvent, AuthBroadcastChannel, type AuthBroadcastMessage, type AuthChangeEvent, type AuthOptions, type AuthSession, type AuthSignUpCredentials, type AuthUpdateUserAttributes, type AuthUserMetadata, type BrowserAuthSession, type BrowserAuthTokens, BrowserCollectionApi, type BrowserCollectionDefinition, type BrowserCollectionFactory, type BrowserCollectionFindParams, type BrowserCollectionRecord, type BrowserCollections, type BrowserDataAuthMode, type BrowserRealtimeNotification, type BrowserRealtimeStatus, type BrowserRealtimeSubscribeParams, type BrowserRealtimeSubscription, type BrowserSqlExecParams, type BrowserSqlExecResult, type BrowserSqlQueryParams, type BrowserSqlQueryResult, BrowserStorageBucketClient, type BrowserStorageBulkDeleteResult, type BrowserStorageDownloadResult, type BrowserStorageItem, type BrowserStorageListResult, type BrowserStorageSignedUrlResult, type BrowserStorageUploadResult, type CollectionReturnMode, type CollectionRowData, type CollectionRowWithMeta, type CollectionWhere, type ColumnName, type ColumnValue, CookieStorageAdapter, DEFAULT_RAGABLE_API_BASE, type DefaultAuthUser, type DefaultRagableDatabase, type FinishReason, type GenerateTextResult, type HttpMethod, type Json, LocalStorageAdapter, MemoryStorageAdapter, type Message, type PostgRESTFetch, type PostgRESTFetchParams, PostgrestDeleteReturningBuilder, PostgrestDeleteRootBuilder, PostgrestInsertReturningBuilder, PostgrestInsertRootBuilder, PostgrestInsertSdkErrorReturning, PostgrestInsertSdkErrorRoot, type PostgrestResult, PostgrestSelectBuilder, PostgrestTableApi, PostgrestUpdateReturningBuilder, PostgrestUpdateRootBuilder, type PostgrestUpsertOptions, PostgrestUpsertReturningBuilder, PostgrestUpsertRootBuilder, RagableAbortError, RagableAuth, type RagableAuthConfig, RagableBrowser, RagableBrowserAgentsClient, RagableBrowserAiClient, RagableBrowserAuthClient, type RagableBrowserClientOptions, RagableBrowserDatabaseClient, RagableBrowserStorageClient, type RagableDatabase, RagableError, RagableNetworkError, type RagableResult, RagableSdkError, type RagableTableDefinition, type RagableTableNames, RagableTimeoutError, type RequestOptions, type RetryOptions, type RunAgentChatStreamOptions, type RunQuery, type SessionStorage, SessionStorageAdapter, type SseJsonEvent, type StreamPart, type StreamTextParams, type StreamTextResult, type SupabaseCompatSession, type TableInsertRow, type TableRow, type TableUpdatePatch, type Tables, type TablesInsert, type TablesUpdate, type TokenUsage, type ToolCallRecord, Transport, type TransportOptions, type TransportRequest, type WhereInput, type WhereOperatorObject, asPostgrestResponse, assertPostgrestSuccess, bindFetch, buildInferenceRequestBody, collectAssistantTextFromUiSegments, collectionRecordToRowWithMeta, collectionRecordsToRowWithMeta, createBrowserClient, createClient, createRagableBrowserClient, createStreamResultFromParts, detectStorage, effectiveDataAuth, extractErrorMessage, finalizeAgentChatUiTurn, foldAgentStreamIntoUiSegments, formatPostgrestError, formatSdkError, generateIdempotencyKey, isIncompleteAgentStreamError, mapAgentEvent, mapFireworksChunk, normalizeBrowserApiBase, parseAgentStreamAgentInfo, parseAgentStreamDone, parseSseDataLine, parseTransportResponse, readSseStream, runAgentChatStream, runAgentChatStreamForUi, runAgentChatStreamLenient, toRagableResult, unwrapPostgrest };
|
|
1976
|
+
export { type AgentChatMessage, type AgentChatParams, type AgentChatStreamDonePayload, type AgentChatStreamHandlers, type AgentChatStreamResult, type AgentChatStreamUiHandlers, type AgentChatUiAssistantMessage, type AgentChatUiSegment, type AgentChatUiStreamResult, type AgentConversation, type AgentConversationMessage, type AgentConversationSubscription, type AgentPublicChatParams, type AgentStreamAgentInfoEvent, type AgentStreamEvent, AuthBroadcastChannel, type AuthBroadcastMessage, type AuthChangeEvent, type AuthOptions, type AuthSession, type AuthSignUpCredentials, type AuthUpdateUserAttributes, type AuthUserMetadata, type BrowserAuthSession, type BrowserAuthTokens, BrowserCollectionApi, type BrowserCollectionDefinition, type BrowserCollectionFactory, type BrowserCollectionFindParams, type BrowserCollectionRecord, type BrowserCollections, type BrowserDataAuthMode, type BrowserRealtimeNotification, type BrowserRealtimeStatus, type BrowserRealtimeSubscribeParams, type BrowserRealtimeSubscription, type BrowserSqlExecParams, type BrowserSqlExecResult, type BrowserSqlQueryParams, type BrowserSqlQueryResult, BrowserStorageBucketClient, type BrowserStorageBulkDeleteResult, type BrowserStorageDownloadResult, type BrowserStorageItem, type BrowserStorageListResult, type BrowserStorageSignedUrlResult, type BrowserStorageUploadResult, type CollectionReturnMode, type CollectionRowData, type CollectionRowWithMeta, type CollectionWhere, type ColumnName, type ColumnValue, CookieStorageAdapter, DEFAULT_RAGABLE_API_BASE, type DefaultAuthUser, type DefaultRagableDatabase, type FinishReason, type GenerateObjectResult, type GenerateTextResult, type HttpMethod, type Json, type JsonSchema, LocalStorageAdapter, MemoryStorageAdapter, type Message, type PostgRESTFetch, type PostgRESTFetchParams, PostgrestDeleteReturningBuilder, PostgrestDeleteRootBuilder, PostgrestInsertReturningBuilder, PostgrestInsertRootBuilder, PostgrestInsertSdkErrorReturning, PostgrestInsertSdkErrorRoot, type PostgrestResult, PostgrestSelectBuilder, PostgrestTableApi, PostgrestUpdateReturningBuilder, PostgrestUpdateRootBuilder, type PostgrestUpsertOptions, PostgrestUpsertReturningBuilder, PostgrestUpsertRootBuilder, RagableAbortError, RagableAuth, type RagableAuthConfig, RagableBrowser, RagableBrowserAgentsClient, RagableBrowserAiClient, RagableBrowserAuthClient, type RagableBrowserClientOptions, RagableBrowserDatabaseClient, RagableBrowserStorageClient, type RagableDatabase, RagableError, RagableNetworkError, type RagableResult, RagableSdkError, type RagableTableDefinition, type RagableTableNames, RagableTimeoutError, type RequestOptions, type RetryOptions, type RunAgentChatStreamOptions, type RunQuery, type SessionStorage, SessionStorageAdapter, type SseJsonEvent, type StreamObjectParams, type StreamObjectResult, type StreamPart, type StreamTextParams, type StreamTextResult, type SupabaseCompatSession, type TableInsertRow, type TableRow, type TableUpdatePatch, type Tables, type TablesInsert, type TablesUpdate, type TokenUsage, type ToolCallRecord, Transport, type TransportOptions, type TransportRequest, type WhereInput, type WhereOperatorObject, asPostgrestResponse, assertPostgrestSuccess, bindFetch, buildInferenceRequestBody, buildResponseFormat, collectAssistantTextFromUiSegments, collectionRecordToRowWithMeta, collectionRecordsToRowWithMeta, createBrowserClient, createClient, createRagableBrowserClient, createStreamResultFromParts, detectStorage, effectiveDataAuth, extractErrorMessage, finalizeAgentChatUiTurn, foldAgentStreamIntoUiSegments, formatPostgrestError, formatSdkError, generateIdempotencyKey, isIncompleteAgentStreamError, mapAgentEvent, mapFireworksChunk, normalizeBrowserApiBase, parseAgentStreamAgentInfo, parseAgentStreamDone, parseSseDataLine, parseTransportResponse, readSseStream, runAgentChatStream, runAgentChatStreamForUi, runAgentChatStreamLenient, streamObjectFromContext, toRagableResult, tryParsePartialJson, unwrapPostgrest, wrapStreamTextAsObject };
|
package/dist/index.d.ts
CHANGED
|
@@ -1062,6 +1062,8 @@ declare function mapAgentEvent(event: AgentStreamLikeEvent): StreamPart | null;
|
|
|
1062
1062
|
* against the website's organization.
|
|
1063
1063
|
*/
|
|
1064
1064
|
|
|
1065
|
+
/** JSON Schema for a structured response. Plain object — no Zod dep. */
|
|
1066
|
+
type JsonSchema = Record<string, unknown>;
|
|
1065
1067
|
interface StreamTextParams {
|
|
1066
1068
|
model: string;
|
|
1067
1069
|
messages: Message[];
|
|
@@ -1100,15 +1102,87 @@ interface GenerateTextResult {
|
|
|
1100
1102
|
finishReason: FinishReason;
|
|
1101
1103
|
toolCalls: ToolCallRecord[];
|
|
1102
1104
|
}
|
|
1105
|
+
interface StreamObjectParams {
|
|
1106
|
+
model: string;
|
|
1107
|
+
/**
|
|
1108
|
+
* JSON Schema describing the expected output. Constrains the model via the
|
|
1109
|
+
* Fireworks `response_format: { type: "json_object", schema }` mode.
|
|
1110
|
+
*/
|
|
1111
|
+
schema: JsonSchema;
|
|
1112
|
+
/** Optional name for the schema (passed through to providers that support it). */
|
|
1113
|
+
schemaName?: string;
|
|
1114
|
+
/** Optional natural-language description of the schema. */
|
|
1115
|
+
schemaDescription?: string;
|
|
1116
|
+
messages: Message[];
|
|
1117
|
+
system?: string;
|
|
1118
|
+
temperature?: number;
|
|
1119
|
+
maxTokens?: number;
|
|
1120
|
+
topP?: number;
|
|
1121
|
+
signal?: AbortSignal;
|
|
1122
|
+
}
|
|
1123
|
+
interface StreamObjectResult<T = unknown> {
|
|
1124
|
+
/** Raw text deltas — the JSON string as it streams in. */
|
|
1125
|
+
textStream: AsyncIterable<string>;
|
|
1126
|
+
/**
|
|
1127
|
+
* Async iterable of best-effort parsed partial objects. Emits whenever the
|
|
1128
|
+
* accumulated JSON parses cleanly under the partial-JSON repair rules.
|
|
1129
|
+
* Subsequent values are *replacements*, not patches — each is a snapshot.
|
|
1130
|
+
*/
|
|
1131
|
+
partialObjectStream: AsyncIterable<T>;
|
|
1132
|
+
/** Resolves to the final parsed object once the stream ends. Rejects if the final JSON is invalid. */
|
|
1133
|
+
object: Promise<T>;
|
|
1134
|
+
/** Resolves to the raw JSON string the model produced. */
|
|
1135
|
+
text: Promise<string>;
|
|
1136
|
+
usage: Promise<TokenUsage>;
|
|
1137
|
+
finishReason: Promise<FinishReason>;
|
|
1138
|
+
/**
|
|
1139
|
+
* Resolves to tool calls the model made during this turn.
|
|
1140
|
+
* Empty for `client.ai.streamObject` (no tools exposed on raw inference);
|
|
1141
|
+
* populated for `client.agents.runObject` when the agent has tools.
|
|
1142
|
+
*/
|
|
1143
|
+
toolCalls: Promise<ToolCallRecord[]>;
|
|
1144
|
+
}
|
|
1145
|
+
interface GenerateObjectResult<T = unknown> {
|
|
1146
|
+
object: T;
|
|
1147
|
+
usage: TokenUsage;
|
|
1148
|
+
finishReason: FinishReason;
|
|
1149
|
+
toolCalls: ToolCallRecord[];
|
|
1150
|
+
}
|
|
1103
1151
|
/**
|
|
1104
1152
|
* Build the JSON body for the inference proxy. Public so tests can assert it.
|
|
1153
|
+
* Accepts an optional `responseFormat` so the same builder serves both
|
|
1154
|
+
* `streamText` and `streamObject`.
|
|
1105
1155
|
*/
|
|
1106
|
-
declare function buildInferenceRequestBody(params: StreamTextParams): Record<string, unknown>;
|
|
1156
|
+
declare function buildInferenceRequestBody(params: StreamTextParams, responseFormat?: Record<string, unknown>): Record<string, unknown>;
|
|
1157
|
+
/**
|
|
1158
|
+
* Build the `response_format` payload for a JSON-Schema-constrained call.
|
|
1159
|
+
* Emits Fireworks' canonical shape (also accepted by OpenAI):
|
|
1160
|
+
*
|
|
1161
|
+
* { type: "json_schema",
|
|
1162
|
+
* json_schema: { name, schema, description?, strict? } }
|
|
1163
|
+
*
|
|
1164
|
+
* @see https://docs.fireworks.ai/structured-responses/structured-response-formatting
|
|
1165
|
+
*/
|
|
1166
|
+
declare function buildResponseFormat(params: {
|
|
1167
|
+
schema: JsonSchema;
|
|
1168
|
+
name?: string;
|
|
1169
|
+
description?: string;
|
|
1170
|
+
/** Default `true` — providers ignore unknown fields safely. */
|
|
1171
|
+
strict?: boolean;
|
|
1172
|
+
}): Record<string, unknown>;
|
|
1107
1173
|
/**
|
|
1108
1174
|
* Build a `StreamTextResult` from a pre-existing async iterable of StreamParts.
|
|
1109
1175
|
* Used by `client.agents.run()` to share the same DX as `client.ai.streamText`.
|
|
1110
1176
|
*/
|
|
1111
1177
|
declare function createStreamResultFromParts(source: AsyncIterable<StreamPart>): StreamTextResult;
|
|
1178
|
+
interface InferenceRequestContext {
|
|
1179
|
+
/** Resolved URL of the inference endpoint. */
|
|
1180
|
+
url: string;
|
|
1181
|
+
/** Pre-built request headers (`Content-Type` already set). */
|
|
1182
|
+
headers: Headers;
|
|
1183
|
+
/** Bound fetch (already wraps user `fetch` option). */
|
|
1184
|
+
fetch: typeof fetch;
|
|
1185
|
+
}
|
|
1112
1186
|
interface AiClientOptions {
|
|
1113
1187
|
organizationId: string;
|
|
1114
1188
|
websiteId?: string;
|
|
@@ -1125,7 +1199,52 @@ declare class RagableBrowserAiClient {
|
|
|
1125
1199
|
private buildContext;
|
|
1126
1200
|
streamText(params: StreamTextParams): StreamTextResult;
|
|
1127
1201
|
generateText(params: StreamTextParams): Promise<GenerateTextResult>;
|
|
1202
|
+
/**
|
|
1203
|
+
* Stream a JSON-Schema-constrained response. Matches Vercel AI SDK's
|
|
1204
|
+
* `streamObject` shape — returns a synchronous result with `partialObjectStream`
|
|
1205
|
+
* (best-effort incremental parses) and `object` (the final parsed JSON).
|
|
1206
|
+
*
|
|
1207
|
+
* ```ts
|
|
1208
|
+
* const { partialObjectStream, object } = client.ai.streamObject({
|
|
1209
|
+
* model: "accounts/fireworks/models/kimi-k2p5",
|
|
1210
|
+
* schema: {
|
|
1211
|
+
* type: "object",
|
|
1212
|
+
* properties: {
|
|
1213
|
+
* title: { type: "string" },
|
|
1214
|
+
* tags: { type: "array", items: { type: "string" } },
|
|
1215
|
+
* },
|
|
1216
|
+
* required: ["title", "tags"],
|
|
1217
|
+
* },
|
|
1218
|
+
* messages: [{ role: "user", content: "Give me a blog post idea about AI." }],
|
|
1219
|
+
* });
|
|
1220
|
+
* for await (const partial of partialObjectStream) renderPreview(partial);
|
|
1221
|
+
* const final = await object;
|
|
1222
|
+
* ```
|
|
1223
|
+
*/
|
|
1224
|
+
streamObject<T = unknown>(params: StreamObjectParams): StreamObjectResult<T>;
|
|
1225
|
+
/**
|
|
1226
|
+
* Non-streaming variant of {@link streamObject}. Resolves once the model
|
|
1227
|
+
* finishes; rejects if the final text isn't valid JSON for the schema.
|
|
1228
|
+
*/
|
|
1229
|
+
generateObject<T = unknown>(params: StreamObjectParams): Promise<GenerateObjectResult<T>>;
|
|
1128
1230
|
}
|
|
1231
|
+
/**
|
|
1232
|
+
* Open a JSON-mode inference stream against the proxy and return a
|
|
1233
|
+
* `StreamObjectResult`. Exposed for advanced callers; most code should use
|
|
1234
|
+
* `RagableBrowserAiClient.streamObject`.
|
|
1235
|
+
*/
|
|
1236
|
+
declare function streamObjectFromContext<T = unknown>(ctx: InferenceRequestContext, params: StreamObjectParams): StreamObjectResult<T>;
|
|
1237
|
+
/**
|
|
1238
|
+
* Wrap a `StreamTextResult` (text-only stream) into a `StreamObjectResult`
|
|
1239
|
+
* by parsing the accumulated text as JSON. The text path stays the source of
|
|
1240
|
+
* truth for `text`/`usage`/`finishReason`/`toolCalls`; this helper only adds
|
|
1241
|
+
* `partialObjectStream` and `object` on top.
|
|
1242
|
+
*
|
|
1243
|
+
* Public so `client.agents.runObject` can reuse the parsing logic — agents
|
|
1244
|
+
* stream text through their own SSE protocol, but the structured-output
|
|
1245
|
+
* conversion is identical.
|
|
1246
|
+
*/
|
|
1247
|
+
declare function wrapStreamTextAsObject<T = unknown>(inner: StreamTextResult): StreamObjectResult<T>;
|
|
1129
1248
|
|
|
1130
1249
|
/** Canonical browser/server API base (`…/api`, no trailing slash). */
|
|
1131
1250
|
declare function normalizeBrowserApiBase(): string;
|
|
@@ -1720,6 +1839,52 @@ declare class RagableBrowserAgentsClient {
|
|
|
1720
1839
|
messages: Message[];
|
|
1721
1840
|
signal?: AbortSignal;
|
|
1722
1841
|
}): StreamTextResult;
|
|
1842
|
+
/**
|
|
1843
|
+
* Same agent, same tools/instructions/RAG — but constrain the final output
|
|
1844
|
+
* to a JSON Schema. Matches `client.ai.streamObject` exactly so callers can
|
|
1845
|
+
* swap between raw inference and an agent without changing call shape.
|
|
1846
|
+
*
|
|
1847
|
+
* Tool calling and structured output are **compatible**: the model may call
|
|
1848
|
+
* the agent's tools as usual; the final assistant message is the schema-
|
|
1849
|
+
* conformant JSON. The agent's `agents/<name>.json` is unchanged — whether
|
|
1850
|
+
* the run is conversational or structured is decided by the call site.
|
|
1851
|
+
*
|
|
1852
|
+
* ```ts
|
|
1853
|
+
* const { partialObjectStream, object } = client.agents.runObject<Plan>(
|
|
1854
|
+
* "planner",
|
|
1855
|
+
* {
|
|
1856
|
+
* messages: [{ role: "user", content: "Plan a 3-day trip to Kyoto." }],
|
|
1857
|
+
* schema: {
|
|
1858
|
+
* type: "object",
|
|
1859
|
+
* properties: {
|
|
1860
|
+
* days: {
|
|
1861
|
+
* type: "array",
|
|
1862
|
+
* items: { type: "object", properties: { date: { type: "string" }, activities: { type: "array", items: { type: "string" } } } },
|
|
1863
|
+
* },
|
|
1864
|
+
* },
|
|
1865
|
+
* required: ["days"],
|
|
1866
|
+
* },
|
|
1867
|
+
* },
|
|
1868
|
+
* );
|
|
1869
|
+
* for await (const p of partialObjectStream) renderPreview(p);
|
|
1870
|
+
* console.log(await object);
|
|
1871
|
+
* ```
|
|
1872
|
+
*/
|
|
1873
|
+
runObject<T = unknown>(agentName: string, params: {
|
|
1874
|
+
messages: Message[];
|
|
1875
|
+
schema: JsonSchema;
|
|
1876
|
+
schemaName?: string;
|
|
1877
|
+
schemaDescription?: string;
|
|
1878
|
+
signal?: AbortSignal;
|
|
1879
|
+
}): StreamObjectResult<T>;
|
|
1880
|
+
/** Non-streaming variant of {@link runObject}. */
|
|
1881
|
+
generateObject<T = unknown>(agentName: string, params: {
|
|
1882
|
+
messages: Message[];
|
|
1883
|
+
schema: JsonSchema;
|
|
1884
|
+
schemaName?: string;
|
|
1885
|
+
schemaDescription?: string;
|
|
1886
|
+
signal?: AbortSignal;
|
|
1887
|
+
}): Promise<GenerateObjectResult<T>>;
|
|
1723
1888
|
private runStreamParts;
|
|
1724
1889
|
/**
|
|
1725
1890
|
* @deprecated Use {@link run} for new code. This method is kept for
|
|
@@ -1790,6 +1955,22 @@ declare function parseSseDataLine(line: string): SseJsonEvent | null;
|
|
|
1790
1955
|
*/
|
|
1791
1956
|
declare function readSseStream(body: ReadableStream<Uint8Array>): AsyncGenerator<SseJsonEvent, void, undefined>;
|
|
1792
1957
|
|
|
1958
|
+
/**
|
|
1959
|
+
* Best-effort parser for incomplete JSON streamed token-by-token from an LLM.
|
|
1960
|
+
*
|
|
1961
|
+
* Used by `streamObject` to emit partial objects as the model writes them.
|
|
1962
|
+
* Strategy:
|
|
1963
|
+
* 1. Try `JSON.parse(text)` — if it works, the JSON is already complete.
|
|
1964
|
+
* 2. Otherwise scan the text and close any open `{`, `[`, or string literal
|
|
1965
|
+
* so the result is parseable.
|
|
1966
|
+
* 3. If a trailing comma is the problem, strip it and retry.
|
|
1967
|
+
*
|
|
1968
|
+
* Returns `undefined` when no parseable prefix can be recovered.
|
|
1969
|
+
*
|
|
1970
|
+
* Designed to be allocation-light and dep-free.
|
|
1971
|
+
*/
|
|
1972
|
+
declare function tryParsePartialJson(text: string): unknown | undefined;
|
|
1973
|
+
|
|
1793
1974
|
declare function createClient<Database extends RagableDatabase = DefaultRagableDatabase, AuthUser extends object = DefaultAuthUser>(options: RagableBrowserClientOptions): RagableBrowser<Database, AuthUser>;
|
|
1794
1975
|
|
|
1795
|
-
export { type AgentChatMessage, type AgentChatParams, type AgentChatStreamDonePayload, type AgentChatStreamHandlers, type AgentChatStreamResult, type AgentChatStreamUiHandlers, type AgentChatUiAssistantMessage, type AgentChatUiSegment, type AgentChatUiStreamResult, type AgentConversation, type AgentConversationMessage, type AgentConversationSubscription, type AgentPublicChatParams, type AgentStreamAgentInfoEvent, type AgentStreamEvent, AuthBroadcastChannel, type AuthBroadcastMessage, type AuthChangeEvent, type AuthOptions, type AuthSession, type AuthSignUpCredentials, type AuthUpdateUserAttributes, type AuthUserMetadata, type BrowserAuthSession, type BrowserAuthTokens, BrowserCollectionApi, type BrowserCollectionDefinition, type BrowserCollectionFactory, type BrowserCollectionFindParams, type BrowserCollectionRecord, type BrowserCollections, type BrowserDataAuthMode, type BrowserRealtimeNotification, type BrowserRealtimeStatus, type BrowserRealtimeSubscribeParams, type BrowserRealtimeSubscription, type BrowserSqlExecParams, type BrowserSqlExecResult, type BrowserSqlQueryParams, type BrowserSqlQueryResult, BrowserStorageBucketClient, type BrowserStorageBulkDeleteResult, type BrowserStorageDownloadResult, type BrowserStorageItem, type BrowserStorageListResult, type BrowserStorageSignedUrlResult, type BrowserStorageUploadResult, type CollectionReturnMode, type CollectionRowData, type CollectionRowWithMeta, type CollectionWhere, type ColumnName, type ColumnValue, CookieStorageAdapter, DEFAULT_RAGABLE_API_BASE, type DefaultAuthUser, type DefaultRagableDatabase, type FinishReason, type GenerateTextResult, type HttpMethod, type Json, LocalStorageAdapter, MemoryStorageAdapter, type Message, type PostgRESTFetch, type PostgRESTFetchParams, PostgrestDeleteReturningBuilder, PostgrestDeleteRootBuilder, PostgrestInsertReturningBuilder, PostgrestInsertRootBuilder, PostgrestInsertSdkErrorReturning, PostgrestInsertSdkErrorRoot, type PostgrestResult, PostgrestSelectBuilder, PostgrestTableApi, PostgrestUpdateReturningBuilder, PostgrestUpdateRootBuilder, type PostgrestUpsertOptions, PostgrestUpsertReturningBuilder, PostgrestUpsertRootBuilder, RagableAbortError, RagableAuth, type RagableAuthConfig, RagableBrowser, RagableBrowserAgentsClient, RagableBrowserAiClient, RagableBrowserAuthClient, type RagableBrowserClientOptions, RagableBrowserDatabaseClient, RagableBrowserStorageClient, type RagableDatabase, RagableError, RagableNetworkError, type RagableResult, RagableSdkError, type RagableTableDefinition, type RagableTableNames, RagableTimeoutError, type RequestOptions, type RetryOptions, type RunAgentChatStreamOptions, type RunQuery, type SessionStorage, SessionStorageAdapter, type SseJsonEvent, type StreamPart, type StreamTextParams, type StreamTextResult, type SupabaseCompatSession, type TableInsertRow, type TableRow, type TableUpdatePatch, type Tables, type TablesInsert, type TablesUpdate, type TokenUsage, type ToolCallRecord, Transport, type TransportOptions, type TransportRequest, type WhereInput, type WhereOperatorObject, asPostgrestResponse, assertPostgrestSuccess, bindFetch, buildInferenceRequestBody, collectAssistantTextFromUiSegments, collectionRecordToRowWithMeta, collectionRecordsToRowWithMeta, createBrowserClient, createClient, createRagableBrowserClient, createStreamResultFromParts, detectStorage, effectiveDataAuth, extractErrorMessage, finalizeAgentChatUiTurn, foldAgentStreamIntoUiSegments, formatPostgrestError, formatSdkError, generateIdempotencyKey, isIncompleteAgentStreamError, mapAgentEvent, mapFireworksChunk, normalizeBrowserApiBase, parseAgentStreamAgentInfo, parseAgentStreamDone, parseSseDataLine, parseTransportResponse, readSseStream, runAgentChatStream, runAgentChatStreamForUi, runAgentChatStreamLenient, toRagableResult, unwrapPostgrest };
|
|
1976
|
+
export { type AgentChatMessage, type AgentChatParams, type AgentChatStreamDonePayload, type AgentChatStreamHandlers, type AgentChatStreamResult, type AgentChatStreamUiHandlers, type AgentChatUiAssistantMessage, type AgentChatUiSegment, type AgentChatUiStreamResult, type AgentConversation, type AgentConversationMessage, type AgentConversationSubscription, type AgentPublicChatParams, type AgentStreamAgentInfoEvent, type AgentStreamEvent, AuthBroadcastChannel, type AuthBroadcastMessage, type AuthChangeEvent, type AuthOptions, type AuthSession, type AuthSignUpCredentials, type AuthUpdateUserAttributes, type AuthUserMetadata, type BrowserAuthSession, type BrowserAuthTokens, BrowserCollectionApi, type BrowserCollectionDefinition, type BrowserCollectionFactory, type BrowserCollectionFindParams, type BrowserCollectionRecord, type BrowserCollections, type BrowserDataAuthMode, type BrowserRealtimeNotification, type BrowserRealtimeStatus, type BrowserRealtimeSubscribeParams, type BrowserRealtimeSubscription, type BrowserSqlExecParams, type BrowserSqlExecResult, type BrowserSqlQueryParams, type BrowserSqlQueryResult, BrowserStorageBucketClient, type BrowserStorageBulkDeleteResult, type BrowserStorageDownloadResult, type BrowserStorageItem, type BrowserStorageListResult, type BrowserStorageSignedUrlResult, type BrowserStorageUploadResult, type CollectionReturnMode, type CollectionRowData, type CollectionRowWithMeta, type CollectionWhere, type ColumnName, type ColumnValue, CookieStorageAdapter, DEFAULT_RAGABLE_API_BASE, type DefaultAuthUser, type DefaultRagableDatabase, type FinishReason, type GenerateObjectResult, type GenerateTextResult, type HttpMethod, type Json, type JsonSchema, LocalStorageAdapter, MemoryStorageAdapter, type Message, type PostgRESTFetch, type PostgRESTFetchParams, PostgrestDeleteReturningBuilder, PostgrestDeleteRootBuilder, PostgrestInsertReturningBuilder, PostgrestInsertRootBuilder, PostgrestInsertSdkErrorReturning, PostgrestInsertSdkErrorRoot, type PostgrestResult, PostgrestSelectBuilder, PostgrestTableApi, PostgrestUpdateReturningBuilder, PostgrestUpdateRootBuilder, type PostgrestUpsertOptions, PostgrestUpsertReturningBuilder, PostgrestUpsertRootBuilder, RagableAbortError, RagableAuth, type RagableAuthConfig, RagableBrowser, RagableBrowserAgentsClient, RagableBrowserAiClient, RagableBrowserAuthClient, type RagableBrowserClientOptions, RagableBrowserDatabaseClient, RagableBrowserStorageClient, type RagableDatabase, RagableError, RagableNetworkError, type RagableResult, RagableSdkError, type RagableTableDefinition, type RagableTableNames, RagableTimeoutError, type RequestOptions, type RetryOptions, type RunAgentChatStreamOptions, type RunQuery, type SessionStorage, SessionStorageAdapter, type SseJsonEvent, type StreamObjectParams, type StreamObjectResult, type StreamPart, type StreamTextParams, type StreamTextResult, type SupabaseCompatSession, type TableInsertRow, type TableRow, type TableUpdatePatch, type Tables, type TablesInsert, type TablesUpdate, type TokenUsage, type ToolCallRecord, Transport, type TransportOptions, type TransportRequest, type WhereInput, type WhereOperatorObject, asPostgrestResponse, assertPostgrestSuccess, bindFetch, buildInferenceRequestBody, buildResponseFormat, collectAssistantTextFromUiSegments, collectionRecordToRowWithMeta, collectionRecordsToRowWithMeta, createBrowserClient, createClient, createRagableBrowserClient, createStreamResultFromParts, detectStorage, effectiveDataAuth, extractErrorMessage, finalizeAgentChatUiTurn, foldAgentStreamIntoUiSegments, formatPostgrestError, formatSdkError, generateIdempotencyKey, isIncompleteAgentStreamError, mapAgentEvent, mapFireworksChunk, normalizeBrowserApiBase, parseAgentStreamAgentInfo, parseAgentStreamDone, parseSseDataLine, parseTransportResponse, readSseStream, runAgentChatStream, runAgentChatStreamForUi, runAgentChatStreamLenient, streamObjectFromContext, toRagableResult, tryParsePartialJson, unwrapPostgrest, wrapStreamTextAsObject };
|