@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.
- package/README.md +58 -299
- package/dist/api/definition.js +1 -1
- package/dist/api/definition.js.map +1 -1
- package/dist/api/index.d.ts +0 -2
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +0 -2
- package/dist/api/index.js.map +1 -1
- package/dist/errors.d.ts +0 -44
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +0 -32
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -9
- package/dist/index.js.map +1 -1
- package/dist/integrations/anthropic/types.d.ts +3 -22
- package/dist/integrations/anthropic/types.d.ts.map +1 -1
- package/dist/integrations/base/index.d.ts +1 -1
- package/dist/integrations/base/index.d.ts.map +1 -1
- package/dist/integrations/base/rest-api-integration-client.d.ts +4 -48
- package/dist/integrations/base/rest-api-integration-client.d.ts.map +1 -1
- package/dist/integrations/base/rest-api-integration-client.js +2 -110
- package/dist/integrations/base/rest-api-integration-client.js.map +1 -1
- package/dist/integrations/base/types.d.ts +1 -67
- package/dist/integrations/base/types.d.ts.map +1 -1
- package/dist/integrations/index.d.ts +1 -1
- package/dist/integrations/index.d.ts.map +1 -1
- package/dist/integrations/index.js.map +1 -1
- package/dist/integrations/openai_v2/types.d.ts +4 -22
- package/dist/integrations/openai_v2/types.d.ts.map +1 -1
- package/dist/integrations/registry.d.ts +0 -16
- package/dist/integrations/registry.d.ts.map +1 -1
- package/dist/integrations/registry.js +38 -38
- package/dist/integrations/registry.js.map +1 -1
- package/dist/integrations/registry.test.js +1 -3
- package/dist/integrations/registry.test.js.map +1 -1
- package/dist/integrations/restapiintegration/types.d.ts +2 -2
- package/dist/integrations/restapiintegration/types.d.ts.map +1 -1
- package/dist/integrations/snowflakecortex/client.d.ts +1 -2
- package/dist/integrations/snowflakecortex/client.d.ts.map +1 -1
- package/dist/integrations/snowflakecortex/client.js +1 -2
- package/dist/integrations/snowflakecortex/client.js.map +1 -1
- package/dist/integrations/snowflakecortex/types.d.ts +3 -4
- package/dist/integrations/snowflakecortex/types.d.ts.map +1 -1
- package/dist/runtime/index.d.ts +0 -3
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +1 -6
- package/dist/runtime/index.js.map +1 -1
- package/package.json +1 -1
- package/src/api/definition.ts +1 -1
- package/src/api/index.ts +0 -4
- package/src/errors.ts +0 -48
- package/src/index.ts +2 -28
- package/src/integrations/anthropic/README.md +5 -101
- package/src/integrations/anthropic/types.ts +3 -29
- package/src/integrations/base/index.ts +0 -2
- package/src/integrations/base/rest-api-integration-client.ts +4 -153
- package/src/integrations/base/types.ts +3 -74
- package/src/integrations/cohere/README.md +11 -11
- package/src/integrations/fireworks/README.md +43 -17
- package/src/integrations/googleanalytics/README.md +2 -2
- package/src/integrations/googledrive/README.md +2 -2
- package/src/integrations/groq/README.md +41 -34
- package/src/integrations/gsheets/README.md +2 -2
- package/src/integrations/index.ts +0 -1
- package/src/integrations/launchdarkly/README.md +2 -2
- package/src/integrations/mistral/README.md +39 -13
- package/src/integrations/openai_v2/README.md +44 -116
- package/src/integrations/openai_v2/types.ts +4 -29
- package/src/integrations/pagerduty/README.md +2 -2
- package/src/integrations/perplexity/README.md +2 -1
- package/src/integrations/registry.test.ts +1 -3
- package/src/integrations/registry.ts +38 -127
- package/src/integrations/restapiintegration/README.md +4 -43
- package/src/integrations/restapiintegration/types.ts +2 -8
- package/src/integrations/sendgrid/README.md +2 -2
- package/src/integrations/snowflakecortex/README.md +4 -53
- package/src/integrations/snowflakecortex/client.ts +2 -3
- package/src/integrations/snowflakecortex/types.ts +3 -11
- package/src/integrations/stabilityai/README.md +2 -2
- package/src/runtime/index.ts +1 -23
- package/dist/api/streaming.d.ts +0 -229
- package/dist/api/streaming.d.ts.map +0 -1
- package/dist/api/streaming.js +0 -107
- package/dist/api/streaming.js.map +0 -1
- package/dist/api/streaming.test.d.ts +0 -5
- package/dist/api/streaming.test.d.ts.map +0 -1
- package/dist/api/streaming.test.js +0 -364
- package/dist/api/streaming.test.js.map +0 -1
- package/dist/runtime/execute.d.ts +0 -128
- package/dist/runtime/execute.d.ts.map +0 -1
- package/dist/runtime/execute.js +0 -84
- package/dist/runtime/execute.js.map +0 -1
- package/dist/runtime/streaming-context.d.ts +0 -49
- package/dist/runtime/streaming-context.d.ts.map +0 -1
- package/dist/runtime/streaming-context.js +0 -71
- package/dist/runtime/streaming-context.js.map +0 -1
- package/dist/runtime/streaming-executor.d.ts +0 -159
- package/dist/runtime/streaming-executor.d.ts.map +0 -1
- package/dist/runtime/streaming-executor.js +0 -229
- package/dist/runtime/streaming-executor.js.map +0 -1
- package/src/api/streaming.test.ts +0 -433
- package/src/api/streaming.ts +0 -303
- package/src/runtime/execute.ts +0 -221
- package/src/runtime/streaming-context.ts +0 -164
- package/src/runtime/streaming-executor.ts +0 -367
|
@@ -10,7 +10,9 @@ Interact with Groq's ultra-fast inference API for LLM chat completions with low
|
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
|
-
###
|
|
13
|
+
### Step 1: List Available Models
|
|
14
|
+
|
|
15
|
+
Model availability varies by account and changes over time. Before making requests, validate which models are available:
|
|
14
16
|
|
|
15
17
|
```typescript
|
|
16
18
|
import { api, z, groq } from "@superblocksteam/sdk-api";
|
|
@@ -18,6 +20,30 @@ import { api, z, groq } from "@superblocksteam/sdk-api";
|
|
|
18
20
|
// Integration ID from the integrations panel
|
|
19
21
|
const PROD_GROQ = "a1b2c3d4-5678-90ab-cdef-groq00000001";
|
|
20
22
|
|
|
23
|
+
const ModelsResponseSchema = z.object({
|
|
24
|
+
data: z.array(
|
|
25
|
+
z.object({
|
|
26
|
+
id: z.string(),
|
|
27
|
+
object: z.string(),
|
|
28
|
+
}),
|
|
29
|
+
),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const models = await ctx.integrations.groq.apiRequest(
|
|
33
|
+
{
|
|
34
|
+
method: "GET",
|
|
35
|
+
path: "/v1/models",
|
|
36
|
+
},
|
|
37
|
+
{ response: ModelsResponseSchema },
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
// Use a model ID from the response in subsequent requests
|
|
41
|
+
const availableModelId = models.data[0]?.id;
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Chat Completion
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
21
47
|
const ChatCompletionResponseSchema = z.object({
|
|
22
48
|
id: z.string(),
|
|
23
49
|
object: z.literal("chat.completion"),
|
|
@@ -59,7 +85,7 @@ export default api({
|
|
|
59
85
|
const result = await ctx.integrations.groq.apiRequest(
|
|
60
86
|
{
|
|
61
87
|
method: "POST",
|
|
62
|
-
path: "/
|
|
88
|
+
path: "/v1/chat/completions",
|
|
63
89
|
body: {
|
|
64
90
|
model: "llama-3.1-70b-versatile",
|
|
65
91
|
messages: [
|
|
@@ -83,7 +109,7 @@ export default api({
|
|
|
83
109
|
const llama70b = await ctx.integrations.groq.apiRequest(
|
|
84
110
|
{
|
|
85
111
|
method: "POST",
|
|
86
|
-
path: "/
|
|
112
|
+
path: "/v1/chat/completions",
|
|
87
113
|
body: {
|
|
88
114
|
model: "llama-3.1-70b-versatile",
|
|
89
115
|
messages: [{ role: "user", content: "Hello" }],
|
|
@@ -96,7 +122,7 @@ const llama70b = await ctx.integrations.groq.apiRequest(
|
|
|
96
122
|
const llama8b = await ctx.integrations.groq.apiRequest(
|
|
97
123
|
{
|
|
98
124
|
method: "POST",
|
|
99
|
-
path: "/
|
|
125
|
+
path: "/v1/chat/completions",
|
|
100
126
|
body: {
|
|
101
127
|
model: "llama-3.1-8b-instant",
|
|
102
128
|
messages: [{ role: "user", content: "Hello" }],
|
|
@@ -109,7 +135,7 @@ const llama8b = await ctx.integrations.groq.apiRequest(
|
|
|
109
135
|
const mixtral = await ctx.integrations.groq.apiRequest(
|
|
110
136
|
{
|
|
111
137
|
method: "POST",
|
|
112
|
-
path: "/
|
|
138
|
+
path: "/v1/chat/completions",
|
|
113
139
|
body: {
|
|
114
140
|
model: "mixtral-8x7b-32768",
|
|
115
141
|
messages: [{ role: "user", content: "Hello" }],
|
|
@@ -150,7 +176,7 @@ const ToolCallResponseSchema = z.object({
|
|
|
150
176
|
const result = await ctx.integrations.groq.apiRequest(
|
|
151
177
|
{
|
|
152
178
|
method: "POST",
|
|
153
|
-
path: "/
|
|
179
|
+
path: "/v1/chat/completions",
|
|
154
180
|
body: {
|
|
155
181
|
model: "llama-3.1-70b-versatile",
|
|
156
182
|
messages: [{ role: "user", content: "What's the weather in Tokyo?" }],
|
|
@@ -200,7 +226,7 @@ const JSONResponseSchema = z.object({
|
|
|
200
226
|
const result = await ctx.integrations.groq.apiRequest(
|
|
201
227
|
{
|
|
202
228
|
method: "POST",
|
|
203
|
-
path: "/
|
|
229
|
+
path: "/v1/chat/completions",
|
|
204
230
|
body: {
|
|
205
231
|
model: "llama-3.1-70b-versatile",
|
|
206
232
|
messages: [
|
|
@@ -234,55 +260,36 @@ await groq.createChatCompletion({ ... });
|
|
|
234
260
|
|
|
235
261
|
// CORRECT - Use apiRequest
|
|
236
262
|
await ctx.integrations.groq.apiRequest(
|
|
237
|
-
{ method: "POST", path: "/
|
|
263
|
+
{ method: "POST", path: "/v1/chat/completions", body: { ... } },
|
|
238
264
|
{ response: ChatCompletionResponseSchema }
|
|
239
265
|
);
|
|
240
266
|
```
|
|
241
267
|
|
|
242
268
|
### OpenAI-Compatible Path
|
|
243
269
|
|
|
244
|
-
|
|
270
|
+
The integration's base URL already includes `https://api.groq.com/openai`, so paths should start with `/v1/`:
|
|
245
271
|
|
|
246
272
|
```typescript
|
|
247
|
-
// WRONG -
|
|
273
|
+
// WRONG - Duplicates the /openai prefix (base URL already includes it)
|
|
248
274
|
await ctx.integrations.groq.apiRequest(
|
|
249
|
-
{ method: "POST", path: "/v1/chat/completions", body: { ... } },
|
|
275
|
+
{ method: "POST", path: "/openai/v1/chat/completions", body: { ... } },
|
|
250
276
|
{ response: schema }
|
|
251
277
|
);
|
|
252
278
|
|
|
253
|
-
// CORRECT -
|
|
279
|
+
// CORRECT - Start with /v1/ (the integration prepends /openai automatically)
|
|
254
280
|
await ctx.integrations.groq.apiRequest(
|
|
255
|
-
{ method: "POST", path: "/
|
|
281
|
+
{ method: "POST", path: "/v1/chat/completions", body: { ... } },
|
|
256
282
|
{ response: schema }
|
|
257
283
|
);
|
|
258
284
|
```
|
|
259
285
|
|
|
260
|
-
### Model Availability
|
|
261
|
-
|
|
262
|
-
Groq has specific models available. Check current availability:
|
|
263
|
-
|
|
264
|
-
| Model | Context Window | Best For |
|
|
265
|
-
| ----------------------- | -------------- | ------------------ |
|
|
266
|
-
| llama-3.1-70b-versatile | 128K | General use |
|
|
267
|
-
| llama-3.1-8b-instant | 128K | Fast, simple tasks |
|
|
268
|
-
| mixtral-8x7b-32768 | 32K | Code, reasoning |
|
|
269
|
-
| gemma2-9b-it | 8K | Lightweight tasks |
|
|
270
|
-
|
|
271
|
-
```typescript
|
|
272
|
-
// Ensure model is available
|
|
273
|
-
const body = {
|
|
274
|
-
model: "llama-3.1-70b-versatile", // Verify model exists
|
|
275
|
-
messages: [...],
|
|
276
|
-
};
|
|
277
|
-
```
|
|
278
|
-
|
|
279
286
|
### Rate Limits
|
|
280
287
|
|
|
281
288
|
Groq has rate limits based on tokens per minute. Monitor usage:
|
|
282
289
|
|
|
283
290
|
```typescript
|
|
284
291
|
const result = await ctx.integrations.groq.apiRequest(
|
|
285
|
-
{ method: "POST", path: "/
|
|
292
|
+
{ method: "POST", path: "/v1/chat/completions", body: { ... } },
|
|
286
293
|
{ response: ChatCompletionResponseSchema }
|
|
287
294
|
);
|
|
288
295
|
|
|
@@ -297,7 +304,7 @@ import { RestApiValidationError } from "@superblocksteam/sdk-api";
|
|
|
297
304
|
|
|
298
305
|
try {
|
|
299
306
|
const result = await ctx.integrations.groq.apiRequest(
|
|
300
|
-
{ method: "POST", path: "/
|
|
307
|
+
{ method: "POST", path: "/v1/chat/completions", body: { ... } },
|
|
301
308
|
{ response: ChatCompletionResponseSchema }
|
|
302
309
|
);
|
|
303
310
|
} catch (error) {
|
|
@@ -24,7 +24,7 @@ Interact with Google Sheets with full type safety and runtime validation.
|
|
|
24
24
|
### Read Spreadsheet Data
|
|
25
25
|
|
|
26
26
|
```typescript
|
|
27
|
-
import { api, z,
|
|
27
|
+
import { api, z, googleSheets } from "@superblocksteam/sdk-api";
|
|
28
28
|
|
|
29
29
|
// Integration ID from the integrations panel
|
|
30
30
|
const PROD_GSHEETS = "a1b2c3d4-5678-90ab-cdef-gsheets00001";
|
|
@@ -39,7 +39,7 @@ const RowSchema = z.array(
|
|
|
39
39
|
|
|
40
40
|
export default api({
|
|
41
41
|
integrations: {
|
|
42
|
-
sheets:
|
|
42
|
+
sheets: googleSheets(PROD_GSHEETS),
|
|
43
43
|
},
|
|
44
44
|
name: "GoogleSheetsExample",
|
|
45
45
|
input: z.object({
|
|
@@ -13,7 +13,7 @@ Manage feature flags, get flag evaluations, and interact with LaunchDarkly's fea
|
|
|
13
13
|
### Get Feature Flag
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
|
-
import { api, z,
|
|
16
|
+
import { api, z, launchDarkly } from "@superblocksteam/sdk-api";
|
|
17
17
|
|
|
18
18
|
// Integration ID from the integrations panel
|
|
19
19
|
const PROD_LAUNCHDARKLY = "a1b2c3d4-5678-90ab-cdef-launchda0001";
|
|
@@ -44,7 +44,7 @@ const FlagSchema = z.object({
|
|
|
44
44
|
|
|
45
45
|
export default api({
|
|
46
46
|
integrations: {
|
|
47
|
-
launchdarkly:
|
|
47
|
+
launchdarkly: launchDarkly(PROD_LAUNCHDARKLY),
|
|
48
48
|
},
|
|
49
49
|
name: "LaunchDarklyExample",
|
|
50
50
|
input: z.object({
|
|
@@ -10,7 +10,9 @@ Interact with Mistral AI's APIs for chat completions, embeddings, and code gener
|
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
|
-
###
|
|
13
|
+
### Step 1: List Available Models
|
|
14
|
+
|
|
15
|
+
Model availability varies by account and API plan. Before making requests, validate which models you can access:
|
|
14
16
|
|
|
15
17
|
```typescript
|
|
16
18
|
import { api, z, mistral } from "@superblocksteam/sdk-api";
|
|
@@ -18,6 +20,30 @@ import { api, z, mistral } from "@superblocksteam/sdk-api";
|
|
|
18
20
|
// Integration ID from the integrations panel
|
|
19
21
|
const PROD_MISTRAL = "a1b2c3d4-5678-90ab-cdef-mistral00001";
|
|
20
22
|
|
|
23
|
+
const ModelsResponseSchema = z.object({
|
|
24
|
+
data: z.array(
|
|
25
|
+
z.object({
|
|
26
|
+
id: z.string(),
|
|
27
|
+
object: z.string(),
|
|
28
|
+
}),
|
|
29
|
+
),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const models = await ctx.integrations.mistral.apiRequest(
|
|
33
|
+
{
|
|
34
|
+
method: "GET",
|
|
35
|
+
path: "/models",
|
|
36
|
+
},
|
|
37
|
+
{ response: ModelsResponseSchema },
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
// Use a model ID from the response in subsequent requests
|
|
41
|
+
const availableModelId = models.data[0]?.id;
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Chat Completion
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
21
47
|
const ChatCompletionResponseSchema = z.object({
|
|
22
48
|
id: z.string(),
|
|
23
49
|
object: z.string(),
|
|
@@ -55,7 +81,7 @@ export default api({
|
|
|
55
81
|
const result = await ctx.integrations.mistral.apiRequest(
|
|
56
82
|
{
|
|
57
83
|
method: "POST",
|
|
58
|
-
path: "/
|
|
84
|
+
path: "/chat/completions",
|
|
59
85
|
body: {
|
|
60
86
|
model: "mistral-large-latest",
|
|
61
87
|
messages: [{ role: "user", content: prompt }],
|
|
@@ -76,7 +102,7 @@ export default api({
|
|
|
76
102
|
const large = await ctx.integrations.mistral.apiRequest(
|
|
77
103
|
{
|
|
78
104
|
method: "POST",
|
|
79
|
-
path: "/
|
|
105
|
+
path: "/chat/completions",
|
|
80
106
|
body: {
|
|
81
107
|
model: "mistral-large-latest",
|
|
82
108
|
messages: [{ role: "user", content: "Complex analysis..." }],
|
|
@@ -89,7 +115,7 @@ const large = await ctx.integrations.mistral.apiRequest(
|
|
|
89
115
|
const medium = await ctx.integrations.mistral.apiRequest(
|
|
90
116
|
{
|
|
91
117
|
method: "POST",
|
|
92
|
-
path: "/
|
|
118
|
+
path: "/chat/completions",
|
|
93
119
|
body: {
|
|
94
120
|
model: "mistral-medium-latest",
|
|
95
121
|
messages: [{ role: "user", content: "General task..." }],
|
|
@@ -102,7 +128,7 @@ const medium = await ctx.integrations.mistral.apiRequest(
|
|
|
102
128
|
const small = await ctx.integrations.mistral.apiRequest(
|
|
103
129
|
{
|
|
104
130
|
method: "POST",
|
|
105
|
-
path: "/
|
|
131
|
+
path: "/chat/completions",
|
|
106
132
|
body: {
|
|
107
133
|
model: "mistral-small-latest",
|
|
108
134
|
messages: [{ role: "user", content: "Simple task..." }],
|
|
@@ -115,7 +141,7 @@ const small = await ctx.integrations.mistral.apiRequest(
|
|
|
115
141
|
const codestral = await ctx.integrations.mistral.apiRequest(
|
|
116
142
|
{
|
|
117
143
|
method: "POST",
|
|
118
|
-
path: "/
|
|
144
|
+
path: "/chat/completions",
|
|
119
145
|
body: {
|
|
120
146
|
model: "codestral-latest",
|
|
121
147
|
messages: [{ role: "user", content: "Write a function to..." }],
|
|
@@ -148,7 +174,7 @@ const EmbeddingsResponseSchema = z.object({
|
|
|
148
174
|
const result = await ctx.integrations.mistral.apiRequest(
|
|
149
175
|
{
|
|
150
176
|
method: "POST",
|
|
151
|
-
path: "/
|
|
177
|
+
path: "/embeddings",
|
|
152
178
|
body: {
|
|
153
179
|
model: "mistral-embed",
|
|
154
180
|
input: ["Hello world", "Bonjour le monde"],
|
|
@@ -193,7 +219,7 @@ const FunctionCallResponseSchema = z.object({
|
|
|
193
219
|
const result = await ctx.integrations.mistral.apiRequest(
|
|
194
220
|
{
|
|
195
221
|
method: "POST",
|
|
196
|
-
path: "/
|
|
222
|
+
path: "/chat/completions",
|
|
197
223
|
body: {
|
|
198
224
|
model: "mistral-large-latest",
|
|
199
225
|
messages: [{ role: "user", content: "What's the weather in London?" }],
|
|
@@ -227,7 +253,7 @@ const result = await ctx.integrations.mistral.apiRequest(
|
|
|
227
253
|
const result = await ctx.integrations.mistral.apiRequest(
|
|
228
254
|
{
|
|
229
255
|
method: "POST",
|
|
230
|
-
path: "/
|
|
256
|
+
path: "/chat/completions",
|
|
231
257
|
body: {
|
|
232
258
|
model: "mistral-large-latest",
|
|
233
259
|
messages: [
|
|
@@ -260,7 +286,7 @@ await mistral.embed({ ... });
|
|
|
260
286
|
|
|
261
287
|
// CORRECT - Use apiRequest
|
|
262
288
|
await ctx.integrations.mistral.apiRequest(
|
|
263
|
-
{ method: "POST", path: "/
|
|
289
|
+
{ method: "POST", path: "/chat/completions", body: { ... } },
|
|
264
290
|
{ response: ChatCompletionResponseSchema }
|
|
265
291
|
);
|
|
266
292
|
```
|
|
@@ -288,7 +314,7 @@ Mistral has a safe mode for content moderation:
|
|
|
288
314
|
const result = await ctx.integrations.mistral.apiRequest(
|
|
289
315
|
{
|
|
290
316
|
method: "POST",
|
|
291
|
-
path: "/
|
|
317
|
+
path: "/chat/completions",
|
|
292
318
|
body: {
|
|
293
319
|
model: "mistral-large-latest",
|
|
294
320
|
messages: [{ role: "user", content: "..." }],
|
|
@@ -307,7 +333,7 @@ Mistral supports system messages:
|
|
|
307
333
|
const result = await ctx.integrations.mistral.apiRequest(
|
|
308
334
|
{
|
|
309
335
|
method: "POST",
|
|
310
|
-
path: "/
|
|
336
|
+
path: "/chat/completions",
|
|
311
337
|
body: {
|
|
312
338
|
model: "mistral-large-latest",
|
|
313
339
|
messages: [
|
|
@@ -327,7 +353,7 @@ import { RestApiValidationError } from "@superblocksteam/sdk-api";
|
|
|
327
353
|
|
|
328
354
|
try {
|
|
329
355
|
const result = await ctx.integrations.mistral.apiRequest(
|
|
330
|
-
{ method: "POST", path: "/
|
|
356
|
+
{ method: "POST", path: "/chat/completions", body: { ... } },
|
|
331
357
|
{ response: ChatCompletionResponseSchema }
|
|
332
358
|
);
|
|
333
359
|
} catch (error) {
|
|
@@ -4,14 +4,15 @@ Interact with OpenAI's APIs for chat completions, embeddings, image generation,
|
|
|
4
4
|
|
|
5
5
|
## Methods
|
|
6
6
|
|
|
7
|
-
| Method
|
|
8
|
-
|
|
|
9
|
-
| `apiRequest(options, schema, metadata?)`
|
|
10
|
-
| `streamApiRequest(options, schema, metadata?)` | Stream responses with real-time chunk validation |
|
|
7
|
+
| Method | Description |
|
|
8
|
+
| ---------------------------------------- | -------------------------------------------------- |
|
|
9
|
+
| `apiRequest(options, schema, metadata?)` | Make any OpenAI API request with schema validation |
|
|
11
10
|
|
|
12
11
|
## Usage
|
|
13
12
|
|
|
14
|
-
###
|
|
13
|
+
### Step 1: List Available Models
|
|
14
|
+
|
|
15
|
+
Model availability depends on your OpenAI account and API tier. Before making requests, validate which models you can access:
|
|
15
16
|
|
|
16
17
|
```typescript
|
|
17
18
|
import { api, z, openai } from "@superblocksteam/sdk-api";
|
|
@@ -19,6 +20,30 @@ import { api, z, openai } from "@superblocksteam/sdk-api";
|
|
|
19
20
|
// Integration ID from the integrations panel
|
|
20
21
|
const PROD_OPENAI = "a1b2c3d4-5678-90ab-cdef-333333333333";
|
|
21
22
|
|
|
23
|
+
const ModelsResponseSchema = z.object({
|
|
24
|
+
data: z.array(
|
|
25
|
+
z.object({
|
|
26
|
+
id: z.string(),
|
|
27
|
+
object: z.string(),
|
|
28
|
+
}),
|
|
29
|
+
),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const models = await ctx.integrations.ai.apiRequest(
|
|
33
|
+
{
|
|
34
|
+
method: "GET",
|
|
35
|
+
path: "/models",
|
|
36
|
+
},
|
|
37
|
+
{ response: ModelsResponseSchema },
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
// Use a model ID from the response in subsequent requests
|
|
41
|
+
const availableModelId = models.data[0]?.id;
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Chat Completion
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
22
47
|
const ChatCompletionResponseSchema = z.object({
|
|
23
48
|
id: z.string(),
|
|
24
49
|
object: z.literal("chat.completion"),
|
|
@@ -56,7 +81,7 @@ export default api({
|
|
|
56
81
|
const result = await ctx.integrations.ai.apiRequest(
|
|
57
82
|
{
|
|
58
83
|
method: "POST",
|
|
59
|
-
path: "/
|
|
84
|
+
path: "/chat/completions",
|
|
60
85
|
body: {
|
|
61
86
|
model: "gpt-4",
|
|
62
87
|
messages: [
|
|
@@ -105,7 +130,7 @@ const FunctionCallResponseSchema = z.object({
|
|
|
105
130
|
const result = await ctx.integrations.ai.apiRequest(
|
|
106
131
|
{
|
|
107
132
|
method: "POST",
|
|
108
|
-
path: "/
|
|
133
|
+
path: "/chat/completions",
|
|
109
134
|
body: {
|
|
110
135
|
model: "gpt-4",
|
|
111
136
|
messages: [
|
|
@@ -161,7 +186,7 @@ const EmbeddingsResponseSchema = z.object({
|
|
|
161
186
|
const embeddings = await ctx.integrations.ai.apiRequest(
|
|
162
187
|
{
|
|
163
188
|
method: "POST",
|
|
164
|
-
path: "/
|
|
189
|
+
path: "/embeddings",
|
|
165
190
|
body: {
|
|
166
191
|
model: "text-embedding-3-small",
|
|
167
192
|
input: ["Hello world", "How are you?"],
|
|
@@ -192,7 +217,7 @@ const ImageResponseSchema = z.object({
|
|
|
192
217
|
const images = await ctx.integrations.ai.apiRequest(
|
|
193
218
|
{
|
|
194
219
|
method: "POST",
|
|
195
|
-
path: "/
|
|
220
|
+
path: "/images/generations",
|
|
196
221
|
body: {
|
|
197
222
|
model: "dall-e-3",
|
|
198
223
|
prompt: "A serene mountain landscape at sunset",
|
|
@@ -215,7 +240,7 @@ const imageUrl = images.data[0]?.url;
|
|
|
215
240
|
const ttsResult = await ctx.integrations.ai.apiRequest(
|
|
216
241
|
{
|
|
217
242
|
method: "POST",
|
|
218
|
-
path: "/
|
|
243
|
+
path: "/audio/speech",
|
|
219
244
|
body: {
|
|
220
245
|
model: "tts-1",
|
|
221
246
|
input: "Hello, how are you today?",
|
|
@@ -226,91 +251,6 @@ const ttsResult = await ctx.integrations.ai.apiRequest(
|
|
|
226
251
|
);
|
|
227
252
|
```
|
|
228
253
|
|
|
229
|
-
### Streaming Chat Completions
|
|
230
|
-
|
|
231
|
-
For real-time token streaming, use `streamApiRequest()` with `stream: true`:
|
|
232
|
-
|
|
233
|
-
```typescript
|
|
234
|
-
import { streamingApi, z, openai } from "@superblocksteam/sdk-api";
|
|
235
|
-
|
|
236
|
-
// Integration ID from the integrations panel
|
|
237
|
-
const PROD_OPENAI = "a1b2c3d4-5678-90ab-cdef-333333333333";
|
|
238
|
-
|
|
239
|
-
// Schema for OpenAI's streaming SSE chunks
|
|
240
|
-
const OpenAIStreamChunkSchema = z.object({
|
|
241
|
-
id: z.string(),
|
|
242
|
-
object: z.literal("chat.completion.chunk"),
|
|
243
|
-
created: z.number(),
|
|
244
|
-
model: z.string(),
|
|
245
|
-
choices: z.array(
|
|
246
|
-
z.object({
|
|
247
|
-
index: z.number(),
|
|
248
|
-
delta: z.object({
|
|
249
|
-
role: z.string().optional(),
|
|
250
|
-
content: z.string().optional(),
|
|
251
|
-
}),
|
|
252
|
-
finish_reason: z.string().nullable(),
|
|
253
|
-
}),
|
|
254
|
-
),
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
export default streamingApi({
|
|
258
|
-
integrations: {
|
|
259
|
-
ai: openai(PROD_OPENAI),
|
|
260
|
-
},
|
|
261
|
-
|
|
262
|
-
input: z.object({ prompt: z.string() }),
|
|
263
|
-
chunk: z.object({ type: z.literal("text"), text: z.string() }),
|
|
264
|
-
|
|
265
|
-
async *run(ctx, { prompt }) {
|
|
266
|
-
const stream = ctx.integrations.ai.streamApiRequest(
|
|
267
|
-
{
|
|
268
|
-
method: "POST",
|
|
269
|
-
path: "/v1/chat/completions",
|
|
270
|
-
body: {
|
|
271
|
-
model: "gpt-4",
|
|
272
|
-
stream: true, // Enable streaming
|
|
273
|
-
messages: [
|
|
274
|
-
{ role: "system", content: "You are a helpful assistant." },
|
|
275
|
-
{ role: "user", content: prompt },
|
|
276
|
-
],
|
|
277
|
-
},
|
|
278
|
-
},
|
|
279
|
-
{ chunk: OpenAIStreamChunkSchema },
|
|
280
|
-
);
|
|
281
|
-
|
|
282
|
-
// Yield text chunks as they arrive
|
|
283
|
-
for await (const chunk of stream) {
|
|
284
|
-
const content = chunk.choices[0]?.delta?.content;
|
|
285
|
-
if (content) {
|
|
286
|
-
yield { type: "text" as const, text: content };
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
},
|
|
290
|
-
});
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
#### Streaming Chunk Structure
|
|
294
|
-
|
|
295
|
-
OpenAI's streaming chunks have this structure:
|
|
296
|
-
|
|
297
|
-
```typescript
|
|
298
|
-
{
|
|
299
|
-
id: "chatcmpl-...",
|
|
300
|
-
object: "chat.completion.chunk",
|
|
301
|
-
created: 1234567890,
|
|
302
|
-
model: "gpt-4",
|
|
303
|
-
choices: [{
|
|
304
|
-
index: 0,
|
|
305
|
-
delta: {
|
|
306
|
-
role: "assistant", // Only in first chunk
|
|
307
|
-
content: "Hello" // Incremental text
|
|
308
|
-
},
|
|
309
|
-
finish_reason: null // "stop" in final chunk
|
|
310
|
-
}]
|
|
311
|
-
}
|
|
312
|
-
```
|
|
313
|
-
|
|
314
254
|
## Trace Metadata
|
|
315
255
|
|
|
316
256
|
All methods accept an optional `metadata` parameter as the last argument for diagnostics labeling. See the [root SDK README](../../../README.md#trace-metadata) for details.
|
|
@@ -319,7 +259,7 @@ All methods accept an optional `metadata` parameter as the last argument for dia
|
|
|
319
259
|
|
|
320
260
|
### No Specialized Methods
|
|
321
261
|
|
|
322
|
-
The OpenAI client only provides `apiRequest()`. There are no
|
|
262
|
+
The OpenAI client only provides `apiRequest()`. There are no other specialized methods:
|
|
323
263
|
|
|
324
264
|
```typescript
|
|
325
265
|
// WRONG - These methods do not exist
|
|
@@ -328,7 +268,7 @@ await openai.createEmbedding({ ... });
|
|
|
328
268
|
|
|
329
269
|
// CORRECT - Use apiRequest for all operations
|
|
330
270
|
await ctx.integrations.ai.apiRequest(
|
|
331
|
-
{ method: "POST", path: "/
|
|
271
|
+
{ method: "POST", path: "/chat/completions", body: { ... } },
|
|
332
272
|
{ response: ResponseSchema }
|
|
333
273
|
);
|
|
334
274
|
```
|
|
@@ -341,13 +281,13 @@ You must always provide a response schema:
|
|
|
341
281
|
// WRONG - Missing response schema
|
|
342
282
|
const result = await ctx.integrations.ai.apiRequest({
|
|
343
283
|
method: "POST",
|
|
344
|
-
path: "/
|
|
284
|
+
path: "/chat/completions",
|
|
345
285
|
body: { ... },
|
|
346
286
|
});
|
|
347
287
|
|
|
348
288
|
// CORRECT - Include response schema
|
|
349
289
|
const result = await ctx.integrations.ai.apiRequest(
|
|
350
|
-
{ method: "POST", path: "/
|
|
290
|
+
{ method: "POST", path: "/chat/completions", body: { ... } },
|
|
351
291
|
{ response: ChatCompletionResponseSchema }
|
|
352
292
|
);
|
|
353
293
|
```
|
|
@@ -389,7 +329,7 @@ Some features require beta headers:
|
|
|
389
329
|
const result = await ctx.integrations.ai.apiRequest(
|
|
390
330
|
{
|
|
391
331
|
method: "POST",
|
|
392
|
-
path: "/
|
|
332
|
+
path: "/assistants",
|
|
393
333
|
body: { ... },
|
|
394
334
|
headers: {
|
|
395
335
|
"OpenAI-Beta": "assistants=v2",
|
|
@@ -419,7 +359,7 @@ async function withRetry<T>(fn: () => Promise<T>, maxRetries = 3): Promise<T> {
|
|
|
419
359
|
|
|
420
360
|
const result = await withRetry(() =>
|
|
421
361
|
ctx.integrations.ai.apiRequest(
|
|
422
|
-
{ method: "POST", path: "/
|
|
362
|
+
{ method: "POST", path: "/chat/completions", body: { ... } },
|
|
423
363
|
{ response: ChatCompletionResponseSchema }
|
|
424
364
|
)
|
|
425
365
|
);
|
|
@@ -441,7 +381,7 @@ Be aware of model token limits:
|
|
|
441
381
|
const result = await ctx.integrations.ai.apiRequest(
|
|
442
382
|
{
|
|
443
383
|
method: "POST",
|
|
444
|
-
path: "/
|
|
384
|
+
path: "/chat/completions",
|
|
445
385
|
body: {
|
|
446
386
|
model: "gpt-4",
|
|
447
387
|
messages: [...],
|
|
@@ -455,11 +395,11 @@ const result = await ctx.integrations.ai.apiRequest(
|
|
|
455
395
|
## Error Handling
|
|
456
396
|
|
|
457
397
|
```typescript
|
|
458
|
-
import { RestApiValidationError
|
|
398
|
+
import { RestApiValidationError } from "@superblocksteam/sdk-api";
|
|
459
399
|
|
|
460
400
|
try {
|
|
461
401
|
const result = await ctx.integrations.ai.apiRequest(
|
|
462
|
-
{ method: "POST", path: "/
|
|
402
|
+
{ method: "POST", path: "/chat/completions", body: { ... } },
|
|
463
403
|
{ response: ChatCompletionResponseSchema }
|
|
464
404
|
);
|
|
465
405
|
} catch (error) {
|
|
@@ -468,18 +408,6 @@ try {
|
|
|
468
408
|
console.error("Actual response:", error.details.data);
|
|
469
409
|
}
|
|
470
410
|
}
|
|
471
|
-
|
|
472
|
-
// For streaming requests
|
|
473
|
-
try {
|
|
474
|
-
for await (const chunk of stream) {
|
|
475
|
-
// Process chunk
|
|
476
|
-
}
|
|
477
|
-
} catch (error) {
|
|
478
|
-
if (error instanceof StreamChunkValidationError) {
|
|
479
|
-
console.error("Chunk validation failed at index:", error.details.chunkIndex);
|
|
480
|
-
console.error("Raw chunk:", error.details.chunk);
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
411
|
```
|
|
484
412
|
|
|
485
413
|
## API Reference
|
|
@@ -3,16 +3,12 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import type { BaseIntegrationClient } from "../../types.js";
|
|
6
|
-
import type {
|
|
7
|
-
SupportsApiRequest,
|
|
8
|
-
SupportsStreamingApiRequest,
|
|
9
|
-
} from "../base/index.js";
|
|
6
|
+
import type { SupportsApiRequest } from "../base/index.js";
|
|
10
7
|
|
|
11
8
|
/**
|
|
12
9
|
* OpenAI client for AI model interactions.
|
|
13
10
|
*
|
|
14
|
-
* Provides the generic apiRequest() method for any OpenAI API endpoint
|
|
15
|
-
* and streamApiRequest() for streaming responses.
|
|
11
|
+
* Provides the generic apiRequest() method for any OpenAI API endpoint.
|
|
16
12
|
* Use this to interact with OpenAI's chat completions, embeddings, and other endpoints.
|
|
17
13
|
*
|
|
18
14
|
* @example
|
|
@@ -45,7 +41,7 @@ import type {
|
|
|
45
41
|
* const completion = await ctx.integrations.ai.apiRequest(
|
|
46
42
|
* {
|
|
47
43
|
* method: 'POST',
|
|
48
|
-
* path: '/
|
|
44
|
+
* path: '/chat/completions',
|
|
49
45
|
* body: {
|
|
50
46
|
* model: 'gpt-4',
|
|
51
47
|
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
@@ -57,30 +53,9 @@ import type {
|
|
|
57
53
|
* }
|
|
58
54
|
* );
|
|
59
55
|
*
|
|
60
|
-
* // Or use streaming for real-time responses
|
|
61
|
-
* const stream = openai.streamApiRequest(
|
|
62
|
-
* {
|
|
63
|
-
* method: 'POST',
|
|
64
|
-
* path: '/v1/chat/completions',
|
|
65
|
-
* body: {
|
|
66
|
-
* model: 'gpt-4',
|
|
67
|
-
* stream: true,
|
|
68
|
-
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
69
|
-
* },
|
|
70
|
-
* },
|
|
71
|
-
* { chunk: StreamChunkSchema }
|
|
72
|
-
* );
|
|
73
|
-
*
|
|
74
|
-
* for await (const chunk of stream) {
|
|
75
|
-
* // Handle streaming chunks
|
|
76
|
-
* }
|
|
77
56
|
* ```
|
|
78
57
|
*/
|
|
79
58
|
export interface OpenAIClient
|
|
80
|
-
extends
|
|
81
|
-
BaseIntegrationClient,
|
|
82
|
-
SupportsApiRequest,
|
|
83
|
-
SupportsStreamingApiRequest {
|
|
59
|
+
extends BaseIntegrationClient, SupportsApiRequest {
|
|
84
60
|
// apiRequest() method inherited from SupportsApiRequest
|
|
85
|
-
// streamApiRequest() method inherited from SupportsStreamingApiRequest
|
|
86
61
|
}
|