@superblocksteam/sdk-api 2.0.103 → 2.0.104-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/README.md +58 -299
  2. package/dist/api/definition.js +1 -1
  3. package/dist/api/definition.js.map +1 -1
  4. package/dist/api/index.d.ts +0 -2
  5. package/dist/api/index.d.ts.map +1 -1
  6. package/dist/api/index.js +0 -2
  7. package/dist/api/index.js.map +1 -1
  8. package/dist/errors.d.ts +0 -44
  9. package/dist/errors.d.ts.map +1 -1
  10. package/dist/errors.js +0 -32
  11. package/dist/errors.js.map +1 -1
  12. package/dist/index.d.ts +5 -5
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +5 -9
  15. package/dist/index.js.map +1 -1
  16. package/dist/integrations/anthropic/types.d.ts +3 -22
  17. package/dist/integrations/anthropic/types.d.ts.map +1 -1
  18. package/dist/integrations/base/index.d.ts +1 -1
  19. package/dist/integrations/base/index.d.ts.map +1 -1
  20. package/dist/integrations/base/rest-api-integration-client.d.ts +4 -48
  21. package/dist/integrations/base/rest-api-integration-client.d.ts.map +1 -1
  22. package/dist/integrations/base/rest-api-integration-client.js +2 -110
  23. package/dist/integrations/base/rest-api-integration-client.js.map +1 -1
  24. package/dist/integrations/base/types.d.ts +1 -67
  25. package/dist/integrations/base/types.d.ts.map +1 -1
  26. package/dist/integrations/index.d.ts +1 -1
  27. package/dist/integrations/index.d.ts.map +1 -1
  28. package/dist/integrations/index.js.map +1 -1
  29. package/dist/integrations/openai_v2/types.d.ts +4 -22
  30. package/dist/integrations/openai_v2/types.d.ts.map +1 -1
  31. package/dist/integrations/registry.d.ts +0 -16
  32. package/dist/integrations/registry.d.ts.map +1 -1
  33. package/dist/integrations/registry.js +38 -38
  34. package/dist/integrations/registry.js.map +1 -1
  35. package/dist/integrations/registry.test.js +1 -3
  36. package/dist/integrations/registry.test.js.map +1 -1
  37. package/dist/integrations/restapiintegration/types.d.ts +2 -2
  38. package/dist/integrations/restapiintegration/types.d.ts.map +1 -1
  39. package/dist/integrations/snowflakecortex/client.d.ts +1 -2
  40. package/dist/integrations/snowflakecortex/client.d.ts.map +1 -1
  41. package/dist/integrations/snowflakecortex/client.js +1 -2
  42. package/dist/integrations/snowflakecortex/client.js.map +1 -1
  43. package/dist/integrations/snowflakecortex/types.d.ts +3 -4
  44. package/dist/integrations/snowflakecortex/types.d.ts.map +1 -1
  45. package/dist/runtime/index.d.ts +0 -3
  46. package/dist/runtime/index.d.ts.map +1 -1
  47. package/dist/runtime/index.js +1 -6
  48. package/dist/runtime/index.js.map +1 -1
  49. package/package.json +1 -1
  50. package/src/api/definition.ts +1 -1
  51. package/src/api/index.ts +0 -4
  52. package/src/errors.ts +0 -48
  53. package/src/index.ts +2 -28
  54. package/src/integrations/anthropic/README.md +5 -101
  55. package/src/integrations/anthropic/types.ts +3 -29
  56. package/src/integrations/base/index.ts +0 -2
  57. package/src/integrations/base/rest-api-integration-client.ts +4 -153
  58. package/src/integrations/base/types.ts +3 -74
  59. package/src/integrations/cohere/README.md +11 -11
  60. package/src/integrations/fireworks/README.md +43 -17
  61. package/src/integrations/googleanalytics/README.md +2 -2
  62. package/src/integrations/googledrive/README.md +2 -2
  63. package/src/integrations/groq/README.md +41 -34
  64. package/src/integrations/gsheets/README.md +2 -2
  65. package/src/integrations/index.ts +0 -1
  66. package/src/integrations/launchdarkly/README.md +2 -2
  67. package/src/integrations/mistral/README.md +39 -13
  68. package/src/integrations/openai_v2/README.md +44 -116
  69. package/src/integrations/openai_v2/types.ts +4 -29
  70. package/src/integrations/pagerduty/README.md +2 -2
  71. package/src/integrations/perplexity/README.md +2 -1
  72. package/src/integrations/registry.test.ts +1 -3
  73. package/src/integrations/registry.ts +38 -127
  74. package/src/integrations/restapiintegration/README.md +4 -43
  75. package/src/integrations/restapiintegration/types.ts +2 -8
  76. package/src/integrations/sendgrid/README.md +2 -2
  77. package/src/integrations/snowflakecortex/README.md +4 -53
  78. package/src/integrations/snowflakecortex/client.ts +2 -3
  79. package/src/integrations/snowflakecortex/types.ts +3 -11
  80. package/src/integrations/stabilityai/README.md +2 -2
  81. package/src/runtime/index.ts +1 -23
  82. package/dist/api/streaming.d.ts +0 -229
  83. package/dist/api/streaming.d.ts.map +0 -1
  84. package/dist/api/streaming.js +0 -107
  85. package/dist/api/streaming.js.map +0 -1
  86. package/dist/api/streaming.test.d.ts +0 -5
  87. package/dist/api/streaming.test.d.ts.map +0 -1
  88. package/dist/api/streaming.test.js +0 -364
  89. package/dist/api/streaming.test.js.map +0 -1
  90. package/dist/runtime/execute.d.ts +0 -128
  91. package/dist/runtime/execute.d.ts.map +0 -1
  92. package/dist/runtime/execute.js +0 -84
  93. package/dist/runtime/execute.js.map +0 -1
  94. package/dist/runtime/streaming-context.d.ts +0 -49
  95. package/dist/runtime/streaming-context.d.ts.map +0 -1
  96. package/dist/runtime/streaming-context.js +0 -71
  97. package/dist/runtime/streaming-context.js.map +0 -1
  98. package/dist/runtime/streaming-executor.d.ts +0 -159
  99. package/dist/runtime/streaming-executor.d.ts.map +0 -1
  100. package/dist/runtime/streaming-executor.js +0 -229
  101. package/dist/runtime/streaming-executor.js.map +0 -1
  102. package/src/api/streaming.test.ts +0 -433
  103. package/src/api/streaming.ts +0 -303
  104. package/src/runtime/execute.ts +0 -221
  105. package/src/runtime/streaming-context.ts +0 -164
  106. package/src/runtime/streaming-executor.ts +0 -367
@@ -1,367 +0,0 @@
1
- /**
2
- * Streaming API execution engine.
3
- *
4
- * Provides the entry point for executing compiled streaming APIs,
5
- * handling input validation, chunk validation, and error management.
6
- */
7
-
8
- import type { CompiledStreamingApi } from "../api/streaming.js";
9
- import { getIntegrationDeclarations } from "../integrations/declarations.js";
10
- import type { TraceMetadata } from "../integrations/registry.js";
11
- import type { IntegrationConfig } from "../integrations/types.js";
12
- import type { ApiUser } from "../types.js";
13
- import {
14
- SdkError,
15
- InputValidationError,
16
- ExecutionError,
17
- ErrorCode,
18
- type ErrorCodeType,
19
- } from "./errors.js";
20
- import {
21
- createStreamingApiContext,
22
- type CreateStreamingContextOptions,
23
- } from "./streaming-context.js";
24
-
25
- /**
26
- * Request to execute a streaming API.
27
- */
28
- export interface ExecuteStreamingApiRequest {
29
- /** Raw input data to be validated */
30
- input: unknown;
31
-
32
- /** Available integration configurations */
33
- integrations: IntegrationConfig[];
34
-
35
- /** Unique execution ID for tracing */
36
- executionId: string;
37
-
38
- /** Environment variables available to the API */
39
- env: Record<string, string>;
40
-
41
- /** User information extracted from the Superblocks JWT */
42
- user: ApiUser;
43
-
44
- /**
45
- * Function to execute integration operations (non-streaming).
46
- *
47
- * @param integrationId - The integration ID
48
- * @param request - Plugin-specific request object
49
- * @param bindings - Optional bindings for language plugins
50
- * @returns Promise resolving to the operation result
51
- */
52
- executeQuery: (
53
- integrationId: string,
54
- request: Record<string, unknown>,
55
- bindings?: Record<string, unknown>,
56
- metadata?: TraceMetadata,
57
- ) => Promise<unknown>;
58
-
59
- /**
60
- * Function to execute streaming integration operations.
61
- *
62
- * @param integrationId - The integration ID
63
- * @param request - Plugin-specific request object
64
- * @param metadata - Optional trace metadata for diagnostics
65
- * @returns AsyncIterable yielding raw chunks
66
- */
67
- executeStreamingQuery?: (
68
- integrationId: string,
69
- request: Record<string, unknown>,
70
- metadata?: TraceMetadata,
71
- ) => AsyncIterable<unknown>;
72
- }
73
-
74
- /**
75
- * Callback handlers for streaming API execution.
76
- */
77
- export interface StreamingApiCallbacks<TChunk = unknown> {
78
- /**
79
- * Called for each validated chunk.
80
- *
81
- * @param chunk - The validated chunk
82
- * @param index - Zero-based index of this chunk
83
- */
84
- onChunk: (chunk: TChunk, index: number) => void;
85
-
86
- /**
87
- * Called when the stream completes successfully.
88
- *
89
- * @param totalChunks - Total number of chunks yielded
90
- */
91
- onComplete?: (totalChunks: number) => void;
92
-
93
- /**
94
- * Called when an error occurs.
95
- *
96
- * @param error - The error that occurred
97
- */
98
- onError?: (error: StreamingApiError) => void;
99
- }
100
-
101
- /**
102
- * Error details for streaming API execution.
103
- */
104
- export interface StreamingApiError {
105
- code: ErrorCodeType;
106
- message: string;
107
- details?: unknown;
108
- /** Number of chunks successfully yielded before the error */
109
- chunksYielded?: number;
110
- }
111
-
112
- /**
113
- * Result of streaming API execution.
114
- *
115
- * Uses a discriminated union to prevent impossible states
116
- * like `{ success: true, error: {...} }`.
117
- */
118
- export type ExecuteStreamingApiResult =
119
- | { success: true; totalChunks: number }
120
- | { success: false; totalChunks: number; error: StreamingApiError };
121
-
122
- /**
123
- * Executes a compiled streaming API with the given request.
124
- *
125
- * This function handles:
126
- * 1. Input validation against the API's Zod schema
127
- * 2. Context creation with integration clients
128
- * 3. Iterating over the async generator
129
- * 4. Chunk validation against the API's chunk schema
130
- * 5. Calling callbacks for chunks, completion, and errors
131
- *
132
- * @param api - The compiled streaming API definition
133
- * @param request - The execution request
134
- * @param callbacks - Callbacks for chunks, completion, and errors
135
- * @returns Promise resolving to execution result
136
- *
137
- * @example
138
- * ```typescript
139
- * import { streamingApi, z, executeStreamingApi } from '@superblocksteam/sdk-api';
140
- *
141
- * const myApi = streamingApi({
142
- * input: z.object({ prompt: z.string() }),
143
- * chunk: z.object({ text: z.string() }),
144
- * async *run(ctx) {
145
- * yield { text: 'Hello' };
146
- * yield { text: ' World' };
147
- * },
148
- * });
149
- *
150
- * const result = await executeStreamingApi(
151
- * myApi,
152
- * {
153
- * input: { prompt: 'Hi' },
154
- * integrations: [],
155
- * executionId: 'exec_123',
156
- * env: {},
157
- * executeQuery: async () => ({}),
158
- * },
159
- * {
160
- * onChunk: (chunk, index) => console.log(`Chunk ${index}:`, chunk),
161
- * onComplete: (total) => console.log(`Stream completed with ${total} chunks`),
162
- * onError: (error) => console.error('Stream error:', error),
163
- * }
164
- * );
165
- * ```
166
- */
167
- export async function executeStreamingApi<TInput = unknown, TChunk = unknown>(
168
- api: CompiledStreamingApi<TInput, TChunk>,
169
- request: ExecuteStreamingApiRequest,
170
- callbacks: StreamingApiCallbacks<TChunk>,
171
- ): Promise<ExecuteStreamingApiResult> {
172
- let totalChunks = 0;
173
-
174
- try {
175
- // 1. Validate input
176
- const inputResult = api.inputSchema.safeParse(request.input);
177
- if (!inputResult.success) {
178
- const error: StreamingApiError = {
179
- code: ErrorCode.INPUT_VALIDATION,
180
- message: "Input validation failed",
181
- details: { issues: inputResult.error.issues },
182
- chunksYielded: 0,
183
- };
184
- callbacks.onError?.(error);
185
- return { success: false, totalChunks: 0, error };
186
- }
187
-
188
- // 2. Build integration map (keyed by id)
189
- const integrations = new Map<string, IntegrationConfig>();
190
- for (const config of request.integrations) {
191
- integrations.set(config.id, config);
192
- }
193
-
194
- // 3. Create execution context with streaming support
195
- const contextOptions: CreateStreamingContextOptions = {
196
- integrations,
197
- integrationDeclarations: getIntegrationDeclarations(api),
198
- executeQuery: request.executeQuery,
199
- executeStreamingQuery: request.executeStreamingQuery,
200
- executionId: request.executionId,
201
- env: request.env,
202
- user: request.user,
203
- };
204
-
205
- const ctx = createStreamingApiContext(contextOptions);
206
-
207
- // 4. Execute the streaming API
208
- let iterator: AsyncIterable<TChunk>;
209
- try {
210
- iterator = api.run(ctx, inputResult.data as TInput);
211
- } catch (err) {
212
- if (err instanceof SdkError) {
213
- const error: StreamingApiError = {
214
- code: err.code,
215
- message: err.message,
216
- details: err.details,
217
- chunksYielded: 0,
218
- };
219
- callbacks.onError?.(error);
220
- return { success: false, totalChunks: 0, error };
221
- }
222
-
223
- const message = err instanceof Error ? err.message : String(err);
224
- const error: StreamingApiError = {
225
- code: ErrorCode.EXECUTION_ERROR,
226
- message,
227
- details: { cause: err },
228
- chunksYielded: 0,
229
- };
230
- callbacks.onError?.(error);
231
- return { success: false, totalChunks: 0, error };
232
- }
233
-
234
- // 5. Iterate over chunks, validate, and call callbacks
235
- for await (const rawChunk of iterator) {
236
- // Validate chunk against schema
237
- const chunkResult = api.chunkSchema.safeParse(rawChunk);
238
- if (!chunkResult.success) {
239
- const error: StreamingApiError = {
240
- code: ErrorCode.OUTPUT_VALIDATION,
241
- message: `Chunk validation failed at index ${totalChunks}`,
242
- details: {
243
- issues: chunkResult.error.issues,
244
- chunkIndex: totalChunks,
245
- rawChunk,
246
- },
247
- chunksYielded: totalChunks,
248
- };
249
- callbacks.onError?.(error);
250
- return { success: false, totalChunks, error };
251
- }
252
-
253
- // Call chunk callback
254
- callbacks.onChunk(chunkResult.data as TChunk, totalChunks);
255
- totalChunks++;
256
- }
257
-
258
- // 6. Stream completed successfully
259
- callbacks.onComplete?.(totalChunks);
260
- return { success: true, totalChunks };
261
- } catch (err) {
262
- // Handle unexpected errors
263
- const message = err instanceof Error ? err.message : String(err);
264
-
265
- let error: StreamingApiError;
266
- if (err instanceof SdkError) {
267
- error = {
268
- code: err.code,
269
- message: err.message,
270
- details: err.details,
271
- chunksYielded: totalChunks,
272
- };
273
- } else {
274
- error = {
275
- code: ErrorCode.INTERNAL_ERROR,
276
- message: `Unexpected error: ${message}`,
277
- details: err instanceof Error ? { stack: err.stack } : undefined,
278
- chunksYielded: totalChunks,
279
- };
280
- }
281
-
282
- callbacks.onError?.(error);
283
- return { success: false, totalChunks, error };
284
- }
285
- }
286
-
287
- /**
288
- * Creates an async generator that wraps streaming API execution.
289
- *
290
- * This is an alternative interface for consuming streaming APIs that returns
291
- * an AsyncGenerator directly, rather than using callbacks.
292
- *
293
- * @param api - The compiled streaming API definition
294
- * @param request - The execution request
295
- * @returns AsyncGenerator yielding validated chunks
296
- *
297
- * @example
298
- * ```typescript
299
- * const generator = createStreamingApiGenerator(myApi, request);
300
- *
301
- * for await (const chunk of generator) {
302
- * console.log('Received chunk:', chunk);
303
- * }
304
- * ```
305
- */
306
- export async function* createStreamingApiGenerator<
307
- TInput = unknown,
308
- TChunk = unknown,
309
- >(
310
- api: CompiledStreamingApi<TInput, TChunk>,
311
- request: ExecuteStreamingApiRequest,
312
- ): AsyncGenerator<TChunk, void, undefined> {
313
- // Validate input
314
- const inputResult = api.inputSchema.safeParse(request.input);
315
- if (!inputResult.success) {
316
- throw new InputValidationError("Input validation failed", {
317
- issues: inputResult.error.issues,
318
- });
319
- }
320
-
321
- // Build integration map
322
- const integrations = new Map<string, IntegrationConfig>();
323
- for (const config of request.integrations) {
324
- integrations.set(config.id, config);
325
- }
326
-
327
- // Create execution context
328
- const ctx = createStreamingApiContext({
329
- integrations,
330
- integrationDeclarations: getIntegrationDeclarations(api),
331
- executeQuery: request.executeQuery,
332
- executeStreamingQuery: request.executeStreamingQuery,
333
- executionId: request.executionId,
334
- env: request.env,
335
- user: request.user,
336
- });
337
-
338
- // Execute and yield validated chunks
339
- let chunkIndex = 0;
340
- try {
341
- const iterator = api.run(ctx, inputResult.data as TInput);
342
- for await (const rawChunk of iterator) {
343
- const chunkResult = api.chunkSchema.safeParse(rawChunk);
344
- if (!chunkResult.success) {
345
- throw new SdkError(
346
- ErrorCode.OUTPUT_VALIDATION,
347
- `Chunk validation failed at index ${chunkIndex}`,
348
- {
349
- issues: chunkResult.error.issues,
350
- chunkIndex,
351
- rawChunk,
352
- },
353
- );
354
- }
355
-
356
- yield chunkResult.data as TChunk;
357
- chunkIndex++;
358
- }
359
- } catch (err) {
360
- if (err instanceof SdkError) {
361
- throw err;
362
- }
363
-
364
- const message = err instanceof Error ? err.message : String(err);
365
- throw new ExecutionError(message, err);
366
- }
367
- }