@workglow/openai 0.3.17 → 0.3.19
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/ai/OpenAiProvider.d.ts +79 -0
- package/dist/ai/OpenAiProvider.d.ts.map +1 -0
- package/dist/ai/OpenAiQueuedProvider.d.ts +76 -0
- package/dist/ai/OpenAiQueuedProvider.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_Capabilities.d.ts +36 -0
- package/dist/ai/common/OpenAI_Capabilities.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_CapabilitySets.d.ts +30 -0
- package/dist/ai/common/OpenAI_CapabilitySets.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_Client.d.ts +17 -0
- package/dist/ai/common/OpenAI_Client.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_Constants.d.ts +7 -0
- package/dist/ai/common/OpenAI_Constants.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_CountTokens.browser.d.ts +15 -0
- package/dist/ai/common/OpenAI_CountTokens.browser.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_CountTokens.d.ts +15 -0
- package/dist/ai/common/OpenAI_CountTokens.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_ImageEdit.d.ts +14 -0
- package/dist/ai/common/OpenAI_ImageEdit.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_ImageGenerate.d.ts +16 -0
- package/dist/ai/common/OpenAI_ImageGenerate.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_ImageValidation.d.ts +15 -0
- package/dist/ai/common/OpenAI_ImageValidation.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_JobRunFns.browser.d.ts +16 -0
- package/dist/ai/common/OpenAI_JobRunFns.browser.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_JobRunFns.d.ts +19 -0
- package/dist/ai/common/OpenAI_JobRunFns.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_ModelInfo.d.ts +14 -0
- package/dist/ai/common/OpenAI_ModelInfo.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_ModelSchema.d.ts +185 -0
- package/dist/ai/common/OpenAI_ModelSchema.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_ModelSearch.d.ts +13 -0
- package/dist/ai/common/OpenAI_ModelSearch.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_StructuredGeneration.d.ts +15 -0
- package/dist/ai/common/OpenAI_StructuredGeneration.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_TextEmbedding.d.ts +14 -0
- package/dist/ai/common/OpenAI_TextEmbedding.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_TextGeneration.d.ts +16 -0
- package/dist/ai/common/OpenAI_TextGeneration.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_TextRewriter.d.ts +13 -0
- package/dist/ai/common/OpenAI_TextRewriter.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_TextSummary.d.ts +13 -0
- package/dist/ai/common/OpenAI_TextSummary.d.ts.map +1 -0
- package/dist/ai/common/OpenAI_ToolCalling.d.ts +18 -0
- package/dist/ai/common/OpenAI_ToolCalling.d.ts.map +1 -0
- package/dist/ai/index.browser.d.ts +10 -0
- package/dist/ai/index.browser.d.ts.map +1 -0
- package/dist/ai/index.d.ts +39 -0
- package/dist/ai/index.d.ts.map +1 -0
- package/dist/ai/registerOpenAi.d.ts +10 -0
- package/dist/ai/registerOpenAi.d.ts.map +1 -0
- package/dist/ai/registerOpenAiInline.browser.d.ts +8 -0
- package/dist/ai/registerOpenAiInline.browser.d.ts.map +1 -0
- package/dist/ai/registerOpenAiInline.d.ts +8 -0
- package/dist/ai/registerOpenAiInline.d.ts.map +1 -0
- package/dist/ai/registerOpenAiWorker.browser.d.ts +7 -0
- package/dist/ai/registerOpenAiWorker.browser.d.ts.map +1 -0
- package/dist/ai/registerOpenAiWorker.d.ts +7 -0
- package/dist/ai/registerOpenAiWorker.d.ts.map +1 -0
- package/dist/ai/runtime.browser.d.ts +15 -0
- package/dist/ai/runtime.browser.d.ts.map +1 -0
- package/dist/ai/runtime.d.ts +16 -0
- package/dist/ai/runtime.d.ts.map +1 -0
- package/dist/ai-runtime.browser.d.ts +7 -0
- package/dist/ai-runtime.browser.d.ts.map +1 -0
- package/dist/ai-runtime.browser.js +716 -0
- package/dist/ai-runtime.browser.js.map +29 -0
- package/dist/ai-runtime.d.ts +7 -0
- package/dist/ai-runtime.d.ts.map +1 -0
- package/dist/ai-runtime.js +731 -0
- package/dist/ai-runtime.js.map +30 -0
- package/dist/ai.browser.d.ts +7 -0
- package/dist/ai.browser.d.ts.map +1 -0
- package/dist/ai.browser.js +231 -0
- package/dist/ai.browser.js.map +17 -0
- package/dist/ai.d.ts +7 -0
- package/dist/ai.d.ts.map +1 -0
- package/dist/ai.js +775 -0
- package/dist/ai.js.map +30 -0
- package/package.json +11 -11
|
@@ -0,0 +1,716 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined")
|
|
5
|
+
return require.apply(this, arguments);
|
|
6
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
// src/ai/common/OpenAI_Client.ts
|
|
10
|
+
import { isBrowserLike, resolveApiKey, validateProviderBaseUrl } from "@workglow/ai/provider-utils";
|
|
11
|
+
var OPENAI_ALLOWED_HOSTS = ["api.openai.com", ".openai.azure.com"];
|
|
12
|
+
var _loadPromise;
|
|
13
|
+
async function loadOpenAISDK() {
|
|
14
|
+
_loadPromise ??= import("openai").then((mod) => mod.default).catch(() => {
|
|
15
|
+
_loadPromise = undefined;
|
|
16
|
+
throw new Error("openai is required for OpenAI tasks. Install it with: bun add openai");
|
|
17
|
+
});
|
|
18
|
+
return _loadPromise;
|
|
19
|
+
}
|
|
20
|
+
async function getClient(model) {
|
|
21
|
+
const OpenAI = await loadOpenAISDK();
|
|
22
|
+
const config = model?.provider_config;
|
|
23
|
+
const apiKey = resolveApiKey({
|
|
24
|
+
config,
|
|
25
|
+
envVar: "OPENAI_API_KEY",
|
|
26
|
+
providerLabel: "OpenAI"
|
|
27
|
+
});
|
|
28
|
+
const baseURL = validateProviderBaseUrl(config?.base_url, {
|
|
29
|
+
vendor: "openai",
|
|
30
|
+
allowHosts: OPENAI_ALLOWED_HOSTS,
|
|
31
|
+
trustedBaseUrl: config?.trustedBaseUrl,
|
|
32
|
+
providerLabel: "OpenAI"
|
|
33
|
+
});
|
|
34
|
+
try {
|
|
35
|
+
return new OpenAI({
|
|
36
|
+
apiKey,
|
|
37
|
+
baseURL,
|
|
38
|
+
organization: config?.organization || undefined,
|
|
39
|
+
dangerouslyAllowBrowser: isBrowserLike()
|
|
40
|
+
});
|
|
41
|
+
} catch (err) {
|
|
42
|
+
throw new Error(`Failed to create OpenAI client: ${err instanceof Error ? err.message : "unknown error"}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function getModelName(model) {
|
|
46
|
+
const name = model?.provider_config?.model_name;
|
|
47
|
+
if (!name) {
|
|
48
|
+
throw new Error("Missing model name in provider_config.model_name.");
|
|
49
|
+
}
|
|
50
|
+
return name;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/ai/registerOpenAiInline.browser.ts
|
|
54
|
+
import { registerProviderInline } from "@workglow/ai/provider-utils";
|
|
55
|
+
|
|
56
|
+
// src/ai/common/OpenAI_CapabilitySets.ts
|
|
57
|
+
var OPENAI_TEXT_GENERATION = ["text.generation"];
|
|
58
|
+
var OPENAI_TOOL_USE = ["text.generation", "tool-use"];
|
|
59
|
+
var OPENAI_JSON_MODE = ["text.generation", "json-mode"];
|
|
60
|
+
var OPENAI_TEXT_REWRITER = ["text.rewriter"];
|
|
61
|
+
var OPENAI_TEXT_SUMMARY = ["text.summary"];
|
|
62
|
+
var OPENAI_TEXT_EMBEDDING = ["text.embedding"];
|
|
63
|
+
var OPENAI_IMAGE_GENERATION = ["image.generation"];
|
|
64
|
+
var OPENAI_IMAGE_EDITING = ["image.editing"];
|
|
65
|
+
var OPENAI_COUNT_TOKENS = ["model.count-tokens"];
|
|
66
|
+
var OPENAI_MODEL_SEARCH = ["model.search"];
|
|
67
|
+
var OPENAI_MODEL_INFO = ["model.info"];
|
|
68
|
+
var OPENAI_CAPABILITY_SETS = [
|
|
69
|
+
OPENAI_TEXT_GENERATION,
|
|
70
|
+
OPENAI_TOOL_USE,
|
|
71
|
+
OPENAI_JSON_MODE,
|
|
72
|
+
OPENAI_TEXT_REWRITER,
|
|
73
|
+
OPENAI_TEXT_SUMMARY,
|
|
74
|
+
OPENAI_TEXT_EMBEDDING,
|
|
75
|
+
OPENAI_IMAGE_GENERATION,
|
|
76
|
+
OPENAI_IMAGE_EDITING,
|
|
77
|
+
OPENAI_COUNT_TOKENS,
|
|
78
|
+
OPENAI_MODEL_SEARCH,
|
|
79
|
+
OPENAI_MODEL_INFO
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
// src/ai/common/OpenAI_CountTokens.browser.ts
|
|
83
|
+
var _tiktoken;
|
|
84
|
+
async function loadTiktoken() {
|
|
85
|
+
if (!_tiktoken) {
|
|
86
|
+
try {
|
|
87
|
+
_tiktoken = await import("js-tiktoken");
|
|
88
|
+
} catch {
|
|
89
|
+
throw new Error("js-tiktoken is required for OpenAI token counting in the browser. Install it with: bun add js-tiktoken");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return _tiktoken;
|
|
93
|
+
}
|
|
94
|
+
var _encoderCache = new Map;
|
|
95
|
+
async function getEncoder(modelName) {
|
|
96
|
+
const tiktoken = await loadTiktoken();
|
|
97
|
+
if (!_encoderCache.has(modelName)) {
|
|
98
|
+
try {
|
|
99
|
+
_encoderCache.set(modelName, tiktoken.encodingForModel(modelName));
|
|
100
|
+
} catch {
|
|
101
|
+
const fallback = "cl100k_base";
|
|
102
|
+
if (!_encoderCache.has(fallback)) {
|
|
103
|
+
_encoderCache.set(fallback, tiktoken.getEncoding(fallback));
|
|
104
|
+
}
|
|
105
|
+
_encoderCache.set(modelName, _encoderCache.get(fallback));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return _encoderCache.get(modelName);
|
|
109
|
+
}
|
|
110
|
+
async function countTokens(input, model) {
|
|
111
|
+
const enc = await getEncoder(getModelName(model));
|
|
112
|
+
const tokens = enc.encode(input.text);
|
|
113
|
+
return { count: tokens.length };
|
|
114
|
+
}
|
|
115
|
+
var OpenAI_CountTokens_Stream = async (input, model, _signal, emit) => {
|
|
116
|
+
const result = await countTokens(input, model);
|
|
117
|
+
emit({ type: "finish", data: result });
|
|
118
|
+
};
|
|
119
|
+
var OpenAI_CountTokens_Preview = async (input, model) => {
|
|
120
|
+
return countTokens(input, model);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// src/ai/common/OpenAI_ImageEdit.ts
|
|
124
|
+
import { ImageGenerationContentPolicyError, ImageGenerationProviderError } from "@workglow/ai";
|
|
125
|
+
import {
|
|
126
|
+
dataUriToImageValue,
|
|
127
|
+
imageValueToPngBytes,
|
|
128
|
+
modelIdForError
|
|
129
|
+
} from "@workglow/ai/provider-utils";
|
|
130
|
+
function aspectRatioToSize(aspectRatio) {
|
|
131
|
+
switch (aspectRatio) {
|
|
132
|
+
case "16:9":
|
|
133
|
+
case "4:3":
|
|
134
|
+
return "1536x1024";
|
|
135
|
+
case "9:16":
|
|
136
|
+
case "3:4":
|
|
137
|
+
return "1024x1536";
|
|
138
|
+
case "1:1":
|
|
139
|
+
default:
|
|
140
|
+
return "1024x1024";
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
async function gpuImageToOpenAiFile(image, name) {
|
|
144
|
+
const bytes = await imageValueToPngBytes(image);
|
|
145
|
+
const buffer = bytes.buffer instanceof ArrayBuffer ? bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) : new Uint8Array(bytes).buffer;
|
|
146
|
+
const sdk = await import("openai");
|
|
147
|
+
if (typeof sdk.toFile === "function") {
|
|
148
|
+
return sdk.toFile(new Blob([buffer], { type: "image/png" }), name, { type: "image/png" });
|
|
149
|
+
}
|
|
150
|
+
return new File([buffer], name, { type: "image/png" });
|
|
151
|
+
}
|
|
152
|
+
async function decodeB64Png(b64) {
|
|
153
|
+
return dataUriToImageValue(`data:image/png;base64,${b64}`);
|
|
154
|
+
}
|
|
155
|
+
async function buildEditPayload(input, model) {
|
|
156
|
+
const modelName = getModelName(model);
|
|
157
|
+
const primary = await gpuImageToOpenAiFile(input.image, "image.png");
|
|
158
|
+
const additionalFiles = input.additionalImages && input.additionalImages.length > 0 ? await Promise.all(input.additionalImages.map((g, i) => gpuImageToOpenAiFile(g, `image-${i + 1}.png`))) : [];
|
|
159
|
+
const imageField = additionalFiles.length === 0 ? primary : [primary, ...additionalFiles];
|
|
160
|
+
const payload = {
|
|
161
|
+
model: modelName,
|
|
162
|
+
prompt: input.prompt,
|
|
163
|
+
image: imageField,
|
|
164
|
+
size: aspectRatioToSize(input.aspectRatio),
|
|
165
|
+
quality: input.quality,
|
|
166
|
+
n: 1,
|
|
167
|
+
...input.providerOptions ?? {}
|
|
168
|
+
};
|
|
169
|
+
if (input.mask) {
|
|
170
|
+
payload.mask = await gpuImageToOpenAiFile(input.mask, "mask.png");
|
|
171
|
+
}
|
|
172
|
+
return payload;
|
|
173
|
+
}
|
|
174
|
+
var OpenAI_ImageEdit_Stream = async (input, model, signal, emit) => {
|
|
175
|
+
const client = await getClient(model);
|
|
176
|
+
try {
|
|
177
|
+
const payload = await buildEditPayload(input, model);
|
|
178
|
+
const stream = await client.images.edit({ ...payload, stream: true, partial_images: 3 }, { signal });
|
|
179
|
+
for await (const event of stream) {
|
|
180
|
+
if (signal.aborted)
|
|
181
|
+
return;
|
|
182
|
+
const b64 = event.b64_json;
|
|
183
|
+
if (!b64)
|
|
184
|
+
continue;
|
|
185
|
+
const image = await decodeB64Png(b64);
|
|
186
|
+
emit({ type: "snapshot", data: { image } });
|
|
187
|
+
}
|
|
188
|
+
emit({ type: "finish", data: {} });
|
|
189
|
+
} catch (err) {
|
|
190
|
+
if (err instanceof ImageGenerationProviderError || err instanceof ImageGenerationContentPolicyError) {
|
|
191
|
+
throw err;
|
|
192
|
+
}
|
|
193
|
+
const msg = err instanceof Error ? err.message : "unknown error";
|
|
194
|
+
if (/safety|policy|moderation/i.test(msg)) {
|
|
195
|
+
throw new ImageGenerationContentPolicyError(modelIdForError(model, "openai"), msg);
|
|
196
|
+
}
|
|
197
|
+
throw new ImageGenerationProviderError(modelIdForError(model, "openai"), msg, {
|
|
198
|
+
cause: err
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
// src/ai/common/OpenAI_ImageGenerate.ts
|
|
204
|
+
import { ImageGenerationContentPolicyError as ImageGenerationContentPolicyError2, ImageGenerationProviderError as ImageGenerationProviderError2 } from "@workglow/ai";
|
|
205
|
+
import { dataUriToImageValue as dataUriToImageValue2, modelIdForError as modelIdForError2 } from "@workglow/ai/provider-utils";
|
|
206
|
+
function aspectRatioToSize2(aspectRatio) {
|
|
207
|
+
switch (aspectRatio) {
|
|
208
|
+
case "16:9":
|
|
209
|
+
case "4:3":
|
|
210
|
+
return "1536x1024";
|
|
211
|
+
case "9:16":
|
|
212
|
+
case "3:4":
|
|
213
|
+
return "1024x1536";
|
|
214
|
+
case "1:1":
|
|
215
|
+
default:
|
|
216
|
+
return "1024x1024";
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
async function decodeB64Png2(b64) {
|
|
220
|
+
return dataUriToImageValue2(`data:image/png;base64,${b64}`);
|
|
221
|
+
}
|
|
222
|
+
var OpenAI_ImageGenerate_Stream = async (input, model, signal, emit) => {
|
|
223
|
+
const client = await getClient(model);
|
|
224
|
+
const modelName = getModelName(model);
|
|
225
|
+
const size = aspectRatioToSize2(input.aspectRatio);
|
|
226
|
+
if (modelName.startsWith("dall-e")) {
|
|
227
|
+
try {
|
|
228
|
+
const resp = await client.images.generate({
|
|
229
|
+
model: modelName,
|
|
230
|
+
prompt: input.prompt,
|
|
231
|
+
size,
|
|
232
|
+
quality: input.quality,
|
|
233
|
+
n: 1,
|
|
234
|
+
response_format: "b64_json",
|
|
235
|
+
...input.providerOptions ?? {}
|
|
236
|
+
}, { signal });
|
|
237
|
+
const b64 = resp.data?.[0]?.b64_json;
|
|
238
|
+
if (!b64) {
|
|
239
|
+
throw new ImageGenerationProviderError2(modelIdForError2(model, "openai"), "Empty response (no b64_json)");
|
|
240
|
+
}
|
|
241
|
+
const image = await decodeB64Png2(b64);
|
|
242
|
+
emit({ type: "snapshot", data: { image } });
|
|
243
|
+
emit({ type: "finish", data: {} });
|
|
244
|
+
return;
|
|
245
|
+
} catch (err) {
|
|
246
|
+
if (err instanceof ImageGenerationProviderError2 || err instanceof ImageGenerationContentPolicyError2) {
|
|
247
|
+
throw err;
|
|
248
|
+
}
|
|
249
|
+
const msg = err instanceof Error ? err.message : "unknown error";
|
|
250
|
+
if (/safety|policy|moderation/i.test(msg)) {
|
|
251
|
+
throw new ImageGenerationContentPolicyError2(modelIdForError2(model, "openai"), msg);
|
|
252
|
+
}
|
|
253
|
+
throw new ImageGenerationProviderError2(modelIdForError2(model, "openai"), msg, {
|
|
254
|
+
cause: err
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
try {
|
|
259
|
+
const stream = await client.images.generate({
|
|
260
|
+
model: modelName,
|
|
261
|
+
prompt: input.prompt,
|
|
262
|
+
size,
|
|
263
|
+
quality: input.quality,
|
|
264
|
+
n: 1,
|
|
265
|
+
stream: true,
|
|
266
|
+
partial_images: 3,
|
|
267
|
+
...input.providerOptions ?? {}
|
|
268
|
+
}, { signal });
|
|
269
|
+
for await (const event of stream) {
|
|
270
|
+
if (signal.aborted)
|
|
271
|
+
return;
|
|
272
|
+
const b64 = event.b64_json;
|
|
273
|
+
if (!b64)
|
|
274
|
+
continue;
|
|
275
|
+
const image = await decodeB64Png2(b64);
|
|
276
|
+
emit({ type: "snapshot", data: { image } });
|
|
277
|
+
}
|
|
278
|
+
emit({ type: "finish", data: {} });
|
|
279
|
+
} catch (err) {
|
|
280
|
+
if (err instanceof ImageGenerationProviderError2 || err instanceof ImageGenerationContentPolicyError2) {
|
|
281
|
+
throw err;
|
|
282
|
+
}
|
|
283
|
+
const msg = err instanceof Error ? err.message : "unknown error";
|
|
284
|
+
if (/safety|policy|moderation/i.test(msg)) {
|
|
285
|
+
throw new ImageGenerationContentPolicyError2(modelIdForError2(model, "openai"), msg);
|
|
286
|
+
}
|
|
287
|
+
throw new ImageGenerationProviderError2(modelIdForError2(model, "openai"), msg, {
|
|
288
|
+
cause: err
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
// src/ai/common/OpenAI_ModelInfo.ts
|
|
294
|
+
var OPENAI_EMBEDDING_DIMENSIONS = {
|
|
295
|
+
"text-embedding-3-small": { native_dimensions: 1536, mrl: true },
|
|
296
|
+
"text-embedding-3-large": { native_dimensions: 3072, mrl: true },
|
|
297
|
+
"text-embedding-ada-002": { native_dimensions: 1536, mrl: false }
|
|
298
|
+
};
|
|
299
|
+
var OpenAI_ModelInfo_Stream = async (input, model, _signal, emit) => {
|
|
300
|
+
if (input.detail === "dimensions") {
|
|
301
|
+
const pc = model?.provider_config;
|
|
302
|
+
let native_dimensions = typeof pc?.native_dimensions === "number" ? pc.native_dimensions : undefined;
|
|
303
|
+
let mrl = typeof pc?.mrl === "boolean" ? pc.mrl : undefined;
|
|
304
|
+
if (native_dimensions === undefined) {
|
|
305
|
+
const modelName = pc?.model_name ?? "";
|
|
306
|
+
const known = OPENAI_EMBEDDING_DIMENSIONS[modelName];
|
|
307
|
+
if (known) {
|
|
308
|
+
native_dimensions = known.native_dimensions;
|
|
309
|
+
mrl = mrl ?? known.mrl;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
emit({
|
|
313
|
+
type: "finish",
|
|
314
|
+
data: {
|
|
315
|
+
model: input.model,
|
|
316
|
+
is_local: false,
|
|
317
|
+
is_remote: true,
|
|
318
|
+
supports_browser: true,
|
|
319
|
+
supports_node: true,
|
|
320
|
+
is_cached: false,
|
|
321
|
+
is_loaded: false,
|
|
322
|
+
file_sizes: null,
|
|
323
|
+
...native_dimensions !== undefined ? { native_dimensions } : {},
|
|
324
|
+
...mrl !== undefined ? { mrl } : {}
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
emit({
|
|
330
|
+
type: "finish",
|
|
331
|
+
data: {
|
|
332
|
+
model: input.model,
|
|
333
|
+
is_local: false,
|
|
334
|
+
is_remote: true,
|
|
335
|
+
supports_browser: true,
|
|
336
|
+
supports_node: true,
|
|
337
|
+
is_cached: false,
|
|
338
|
+
is_loaded: false,
|
|
339
|
+
file_sizes: null
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
// src/ai/common/OpenAI_ModelSearch.ts
|
|
345
|
+
import { filterLabeledModelsByQuery } from "@workglow/ai/provider-utils";
|
|
346
|
+
|
|
347
|
+
// src/ai/common/OpenAI_Constants.ts
|
|
348
|
+
var OPENAI = "OPENAI";
|
|
349
|
+
|
|
350
|
+
// src/ai/common/OpenAI_ModelSearch.ts
|
|
351
|
+
var OPENAI_FALLBACK = [
|
|
352
|
+
{ label: "gpt-image-2", value: "gpt-image-2" },
|
|
353
|
+
{ label: "dall-e-3", value: "dall-e-3" },
|
|
354
|
+
{ label: "gpt-5.5", value: "gpt-5.5" },
|
|
355
|
+
{ label: "gpt-5.4-mini", value: "gpt-5.4-mini" },
|
|
356
|
+
{ label: "gpt-5.4-nano", value: "gpt-5.4-nano" },
|
|
357
|
+
{ label: "gpt-5", value: "gpt-5" },
|
|
358
|
+
{ label: "gpt-5-mini", value: "gpt-5-mini" },
|
|
359
|
+
{ label: "gpt-4o-mini", value: "gpt-4o-mini" },
|
|
360
|
+
{ label: "gpt-4-turbo", value: "gpt-4-turbo" },
|
|
361
|
+
{ label: "o3", value: "o3" },
|
|
362
|
+
{ label: "o3-mini", value: "o3-mini" },
|
|
363
|
+
{ label: "o1", value: "o1" },
|
|
364
|
+
{ label: "o1-mini", value: "o1-mini" }
|
|
365
|
+
];
|
|
366
|
+
var OPENAI_IMAGE_MODELS = [
|
|
367
|
+
{ value: "gpt-image-2", capabilities: ["image.generation", "image.editing"] },
|
|
368
|
+
{ value: "dall-e-3", capabilities: ["image.generation"] }
|
|
369
|
+
];
|
|
370
|
+
async function listOpenAiModels(credentialKey) {
|
|
371
|
+
const client = await getClient({
|
|
372
|
+
provider: OPENAI,
|
|
373
|
+
provider_config: { model_name: "", credential_key: credentialKey }
|
|
374
|
+
});
|
|
375
|
+
const models = [];
|
|
376
|
+
for await (const m of client.models.list()) {
|
|
377
|
+
models.push({ label: m.id, value: m.id, description: m.owned_by });
|
|
378
|
+
}
|
|
379
|
+
models.sort((a, b) => {
|
|
380
|
+
const aGpt = a.value.startsWith("gpt") || a.value.startsWith("o1") ? 0 : 1;
|
|
381
|
+
const bGpt = b.value.startsWith("gpt") || b.value.startsWith("o1") ? 0 : 1;
|
|
382
|
+
if (aGpt !== bGpt)
|
|
383
|
+
return aGpt - bGpt;
|
|
384
|
+
return a.value.localeCompare(b.value);
|
|
385
|
+
});
|
|
386
|
+
return models;
|
|
387
|
+
}
|
|
388
|
+
function mapModelList(models) {
|
|
389
|
+
return models.map((m) => {
|
|
390
|
+
const imageEntry = OPENAI_IMAGE_MODELS.find((i) => i.value === m.value);
|
|
391
|
+
return {
|
|
392
|
+
id: m.value,
|
|
393
|
+
label: m.label,
|
|
394
|
+
description: m.description ?? "",
|
|
395
|
+
record: {
|
|
396
|
+
model_id: m.value,
|
|
397
|
+
provider: OPENAI,
|
|
398
|
+
title: m.value,
|
|
399
|
+
description: "",
|
|
400
|
+
capabilities: imageEntry?.capabilities ?? [],
|
|
401
|
+
provider_config: { model_name: m.value },
|
|
402
|
+
metadata: {}
|
|
403
|
+
},
|
|
404
|
+
raw: m
|
|
405
|
+
};
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
var OpenAI_ModelSearch_Stream = async (input, _model, _signal, emit) => {
|
|
409
|
+
let models;
|
|
410
|
+
if (!input.credential_key) {
|
|
411
|
+
models = OPENAI_FALLBACK;
|
|
412
|
+
} else {
|
|
413
|
+
models = await listOpenAiModels(input.credential_key);
|
|
414
|
+
}
|
|
415
|
+
models = filterLabeledModelsByQuery(models, input.query);
|
|
416
|
+
emit({ type: "finish", data: { results: mapModelList(models) } });
|
|
417
|
+
};
|
|
418
|
+
|
|
419
|
+
// src/ai/common/OpenAI_StructuredGeneration.ts
|
|
420
|
+
import { parsePartialJson } from "@workglow/util/worker";
|
|
421
|
+
var OpenAI_StructuredGeneration_Stream = async (input, model, signal, emit, outputSchema) => {
|
|
422
|
+
const client = await getClient(model);
|
|
423
|
+
const modelName = getModelName(model);
|
|
424
|
+
const schema = input.outputSchema ?? outputSchema;
|
|
425
|
+
const stream = await client.chat.completions.create({
|
|
426
|
+
model: modelName,
|
|
427
|
+
messages: [{ role: "user", content: input.prompt }],
|
|
428
|
+
response_format: {
|
|
429
|
+
type: "json_schema",
|
|
430
|
+
json_schema: {
|
|
431
|
+
name: "structured_output",
|
|
432
|
+
schema,
|
|
433
|
+
strict: true
|
|
434
|
+
}
|
|
435
|
+
},
|
|
436
|
+
max_completion_tokens: input.maxTokens,
|
|
437
|
+
temperature: input.temperature,
|
|
438
|
+
stream: true
|
|
439
|
+
}, { signal });
|
|
440
|
+
let accumulatedJson = "";
|
|
441
|
+
for await (const chunk of stream) {
|
|
442
|
+
const delta = chunk.choices[0]?.delta?.content ?? "";
|
|
443
|
+
if (delta) {
|
|
444
|
+
accumulatedJson += delta;
|
|
445
|
+
const partial = parsePartialJson(accumulatedJson);
|
|
446
|
+
if (partial !== undefined) {
|
|
447
|
+
emit({ type: "object-delta", port: "object", objectDelta: partial });
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
let finalObject;
|
|
452
|
+
try {
|
|
453
|
+
finalObject = JSON.parse(accumulatedJson);
|
|
454
|
+
} catch {
|
|
455
|
+
finalObject = parsePartialJson(accumulatedJson) ?? {};
|
|
456
|
+
}
|
|
457
|
+
emit({ type: "finish", data: { object: finalObject } });
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
// src/ai/common/OpenAI_TextEmbedding.ts
|
|
461
|
+
import { getLogger } from "@workglow/util/worker";
|
|
462
|
+
var OpenAI_TextEmbedding_Stream = async (input, model, signal, emit) => {
|
|
463
|
+
const logger = getLogger();
|
|
464
|
+
const timerLabel = `openai:TextEmbedding:${getModelName(model)}`;
|
|
465
|
+
logger.time(timerLabel, { model: getModelName(model) });
|
|
466
|
+
try {
|
|
467
|
+
const client = await getClient(model);
|
|
468
|
+
const modelName = getModelName(model);
|
|
469
|
+
const response = await client.embeddings.create({
|
|
470
|
+
model: modelName,
|
|
471
|
+
input: input.text
|
|
472
|
+
}, { signal });
|
|
473
|
+
const result = Array.isArray(input.text) ? {
|
|
474
|
+
vector: response.data.map((item) => new Float32Array(item.embedding))
|
|
475
|
+
} : { vector: new Float32Array(response.data[0].embedding) };
|
|
476
|
+
emit({ type: "finish", data: result });
|
|
477
|
+
} finally {
|
|
478
|
+
logger.timeEnd(timerLabel, { model: getModelName(model) });
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
// src/ai/common/OpenAI_TextGeneration.ts
|
|
483
|
+
import { toOpenAIMessages } from "@workglow/ai/worker";
|
|
484
|
+
import { getLogger as getLogger2 } from "@workglow/util/worker";
|
|
485
|
+
function buildChatParams(input, model) {
|
|
486
|
+
const hasMessages = Array.isArray(input.messages) && input.messages.length > 0;
|
|
487
|
+
const messages = hasMessages ? toOpenAIMessages({
|
|
488
|
+
messages: input.messages,
|
|
489
|
+
systemPrompt: input.systemPrompt,
|
|
490
|
+
prompt: "",
|
|
491
|
+
tools: []
|
|
492
|
+
}) : [{ role: "user", content: input.prompt }];
|
|
493
|
+
const params = {
|
|
494
|
+
model: getModelName(model),
|
|
495
|
+
messages
|
|
496
|
+
};
|
|
497
|
+
if (input.maxTokens !== undefined)
|
|
498
|
+
params.max_completion_tokens = input.maxTokens;
|
|
499
|
+
if (input.temperature !== undefined)
|
|
500
|
+
params.temperature = input.temperature;
|
|
501
|
+
if (input.topP !== undefined)
|
|
502
|
+
params.top_p = input.topP;
|
|
503
|
+
if (input.frequencyPenalty !== undefined)
|
|
504
|
+
params.frequency_penalty = input.frequencyPenalty;
|
|
505
|
+
if (input.presencePenalty !== undefined)
|
|
506
|
+
params.presence_penalty = input.presencePenalty;
|
|
507
|
+
return params;
|
|
508
|
+
}
|
|
509
|
+
var OpenAI_TextGeneration_Stream = async (input, model, signal, emit) => {
|
|
510
|
+
const logger = getLogger2();
|
|
511
|
+
const timerLabel = `openai:TextGeneration:${getModelName(model)}`;
|
|
512
|
+
logger.time(timerLabel, { model: getModelName(model) });
|
|
513
|
+
try {
|
|
514
|
+
const client = await getClient(model);
|
|
515
|
+
const params = buildChatParams(input, model);
|
|
516
|
+
const stream = await client.chat.completions.create({ ...params, stream: true }, { signal });
|
|
517
|
+
for await (const chunk of stream) {
|
|
518
|
+
const delta = chunk.choices?.[0]?.delta?.content ?? "";
|
|
519
|
+
if (delta) {
|
|
520
|
+
emit({ type: "text-delta", port: "text", textDelta: delta });
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
emit({ type: "finish", data: {} });
|
|
524
|
+
} finally {
|
|
525
|
+
logger.timeEnd(timerLabel, { model: getModelName(model) });
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
|
|
529
|
+
// src/ai/common/OpenAI_TextRewriter.ts
|
|
530
|
+
var OpenAI_TextRewriter_Stream = async (input, model, signal, emit) => {
|
|
531
|
+
const client = await getClient(model);
|
|
532
|
+
const modelName = getModelName(model);
|
|
533
|
+
const stream = await client.chat.completions.create({
|
|
534
|
+
model: modelName,
|
|
535
|
+
messages: [
|
|
536
|
+
{ role: "system", content: input.prompt },
|
|
537
|
+
{ role: "user", content: input.text }
|
|
538
|
+
],
|
|
539
|
+
stream: true
|
|
540
|
+
}, { signal });
|
|
541
|
+
for await (const chunk of stream) {
|
|
542
|
+
const delta = chunk.choices[0]?.delta?.content ?? "";
|
|
543
|
+
if (delta) {
|
|
544
|
+
emit({ type: "text-delta", port: "text", textDelta: delta });
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
emit({ type: "finish", data: {} });
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
// src/ai/common/OpenAI_TextSummary.ts
|
|
551
|
+
var OpenAI_TextSummary_Stream = async (input, model, signal, emit) => {
|
|
552
|
+
const client = await getClient(model);
|
|
553
|
+
const modelName = getModelName(model);
|
|
554
|
+
const stream = await client.chat.completions.create({
|
|
555
|
+
model: modelName,
|
|
556
|
+
messages: [
|
|
557
|
+
{ role: "system", content: "Summarize the following text concisely." },
|
|
558
|
+
{ role: "user", content: input.text }
|
|
559
|
+
],
|
|
560
|
+
stream: true
|
|
561
|
+
}, { signal });
|
|
562
|
+
for await (const chunk of stream) {
|
|
563
|
+
const delta = chunk.choices[0]?.delta?.content ?? "";
|
|
564
|
+
if (delta) {
|
|
565
|
+
emit({ type: "text-delta", port: "text", textDelta: delta });
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
emit({ type: "finish", data: {} });
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
// src/ai/common/OpenAI_ToolCalling.ts
|
|
572
|
+
import {
|
|
573
|
+
accumulateOpenAIStream,
|
|
574
|
+
buildOpenAITools,
|
|
575
|
+
mapOpenAIToolChoice
|
|
576
|
+
} from "@workglow/ai/provider-utils";
|
|
577
|
+
import { filterValidToolCalls, toOpenAIMessages as toOpenAIMessages2 } from "@workglow/ai/worker";
|
|
578
|
+
var OpenAI_ToolCalling_Stream = async (input, model, signal, emit) => {
|
|
579
|
+
const client = await getClient(model);
|
|
580
|
+
const modelName = getModelName(model);
|
|
581
|
+
const tools = buildOpenAITools(input.tools);
|
|
582
|
+
const messages = toOpenAIMessages2(input);
|
|
583
|
+
const toolChoice = mapOpenAIToolChoice(input.toolChoice, true);
|
|
584
|
+
const stream = await client.chat.completions.create({
|
|
585
|
+
model: modelName,
|
|
586
|
+
messages,
|
|
587
|
+
max_completion_tokens: input.maxTokens,
|
|
588
|
+
temperature: input.temperature,
|
|
589
|
+
stream: true,
|
|
590
|
+
tools,
|
|
591
|
+
tool_choice: toolChoice
|
|
592
|
+
}, { signal });
|
|
593
|
+
await accumulateOpenAIStream(stream, (event) => {
|
|
594
|
+
if (event.type === "object-delta" && event.port === "toolCalls") {
|
|
595
|
+
const validated = filterValidToolCalls(event.objectDelta, input.tools);
|
|
596
|
+
if (validated.length > 0) {
|
|
597
|
+
emit({ type: "object-delta", port: "toolCalls", objectDelta: validated });
|
|
598
|
+
}
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
emit(event);
|
|
602
|
+
});
|
|
603
|
+
emit({ type: "finish", data: { text: "", toolCalls: [] } });
|
|
604
|
+
};
|
|
605
|
+
|
|
606
|
+
// src/ai/common/OpenAI_JobRunFns.browser.ts
|
|
607
|
+
var OPENAI_RUN_FNS = [
|
|
608
|
+
{ serves: OPENAI_TEXT_GENERATION, runFn: OpenAI_TextGeneration_Stream },
|
|
609
|
+
{ serves: OPENAI_TOOL_USE, runFn: OpenAI_ToolCalling_Stream },
|
|
610
|
+
{ serves: OPENAI_JSON_MODE, runFn: OpenAI_StructuredGeneration_Stream },
|
|
611
|
+
{ serves: OPENAI_TEXT_REWRITER, runFn: OpenAI_TextRewriter_Stream },
|
|
612
|
+
{ serves: OPENAI_TEXT_SUMMARY, runFn: OpenAI_TextSummary_Stream },
|
|
613
|
+
{ serves: OPENAI_TEXT_EMBEDDING, runFn: OpenAI_TextEmbedding_Stream },
|
|
614
|
+
{ serves: OPENAI_IMAGE_GENERATION, runFn: OpenAI_ImageGenerate_Stream },
|
|
615
|
+
{ serves: OPENAI_IMAGE_EDITING, runFn: OpenAI_ImageEdit_Stream },
|
|
616
|
+
{ serves: OPENAI_COUNT_TOKENS, runFn: OpenAI_CountTokens_Stream },
|
|
617
|
+
{ serves: OPENAI_MODEL_SEARCH, runFn: OpenAI_ModelSearch_Stream },
|
|
618
|
+
{ serves: OPENAI_MODEL_INFO, runFn: OpenAI_ModelInfo_Stream }
|
|
619
|
+
];
|
|
620
|
+
var OPENAI_PREVIEW_TASKS = {
|
|
621
|
+
CountTokensTask: OpenAI_CountTokens_Preview
|
|
622
|
+
};
|
|
623
|
+
|
|
624
|
+
// src/ai/OpenAiQueuedProvider.ts
|
|
625
|
+
import { AiProvider } from "@workglow/ai";
|
|
626
|
+
import { createCloudProviderClass } from "@workglow/ai/provider-utils";
|
|
627
|
+
|
|
628
|
+
// src/ai/common/OpenAI_Capabilities.ts
|
|
629
|
+
var OPENAI_RUN_FN_SPECS = OPENAI_CAPABILITY_SETS.map((serves) => ({ serves }));
|
|
630
|
+
function openAiWorkerRunFnSpecs() {
|
|
631
|
+
return OPENAI_RUN_FN_SPECS;
|
|
632
|
+
}
|
|
633
|
+
function inferOpenAiCapabilities(model) {
|
|
634
|
+
const id = String(model.model_id ?? model.provider_config?.model_name ?? "");
|
|
635
|
+
if (/^text-embedding/i.test(id)) {
|
|
636
|
+
return ["text.embedding", "model.count-tokens", "model.info", "model.search"];
|
|
637
|
+
}
|
|
638
|
+
if (/^dall-e/i.test(id)) {
|
|
639
|
+
return ["image.generation", "model.info", "model.search"];
|
|
640
|
+
}
|
|
641
|
+
if (/^gpt-image/i.test(id)) {
|
|
642
|
+
return ["image.generation", "image.editing", "model.info", "model.search"];
|
|
643
|
+
}
|
|
644
|
+
if (/^gpt-/i.test(id) || /^o\d/i.test(id)) {
|
|
645
|
+
const caps = [
|
|
646
|
+
"text.generation",
|
|
647
|
+
"text.rewriter",
|
|
648
|
+
"text.summary",
|
|
649
|
+
"tool-use",
|
|
650
|
+
"json-mode",
|
|
651
|
+
"model.count-tokens",
|
|
652
|
+
"model.info",
|
|
653
|
+
"model.search"
|
|
654
|
+
];
|
|
655
|
+
const supportsVision = /gpt-4o|gpt-4\.1|gpt-5|gpt-4-vision|gpt-4-turbo/i.test(id) || /^o\d/i.test(id);
|
|
656
|
+
if (supportsVision) {
|
|
657
|
+
caps.push("vision-input");
|
|
658
|
+
}
|
|
659
|
+
return caps;
|
|
660
|
+
}
|
|
661
|
+
const declared = model.capabilities ?? [];
|
|
662
|
+
if (declared.length > 0)
|
|
663
|
+
return declared;
|
|
664
|
+
return ["model.search", "model.info"];
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// src/ai/OpenAiQueuedProvider.ts
|
|
668
|
+
class OpenAiQueuedProvider extends createCloudProviderClass(AiProvider, {
|
|
669
|
+
name: OPENAI,
|
|
670
|
+
displayName: "OpenAI"
|
|
671
|
+
}) {
|
|
672
|
+
inferCapabilities(model) {
|
|
673
|
+
return inferOpenAiCapabilities(model);
|
|
674
|
+
}
|
|
675
|
+
workerRunFnSpecs() {
|
|
676
|
+
return openAiWorkerRunFnSpecs();
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
// src/ai/registerOpenAiInline.browser.ts
|
|
681
|
+
async function registerOpenAiInline(options) {
|
|
682
|
+
await registerProviderInline(new OpenAiQueuedProvider(OPENAI_RUN_FNS, OPENAI_PREVIEW_TASKS), "OpenAI", options);
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
// src/ai/registerOpenAiWorker.browser.ts
|
|
686
|
+
import { registerProviderWorker } from "@workglow/ai/provider-utils";
|
|
687
|
+
|
|
688
|
+
// src/ai/OpenAiProvider.ts
|
|
689
|
+
import { createCloudProviderClass as createCloudProviderClass2 } from "@workglow/ai/provider-utils";
|
|
690
|
+
import { AiProvider as AiProvider2 } from "@workglow/ai/worker";
|
|
691
|
+
class OpenAiProvider extends createCloudProviderClass2(AiProvider2, {
|
|
692
|
+
name: OPENAI,
|
|
693
|
+
displayName: "OpenAI"
|
|
694
|
+
}) {
|
|
695
|
+
inferCapabilities(model) {
|
|
696
|
+
return inferOpenAiCapabilities(model);
|
|
697
|
+
}
|
|
698
|
+
workerRunFnSpecs() {
|
|
699
|
+
return openAiWorkerRunFnSpecs();
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
// src/ai/registerOpenAiWorker.browser.ts
|
|
704
|
+
async function registerOpenAiWorker() {
|
|
705
|
+
await registerProviderWorker((ws) => new OpenAiProvider(OPENAI_RUN_FNS, OPENAI_PREVIEW_TASKS).registerOnWorkerServer(ws), "OpenAI");
|
|
706
|
+
}
|
|
707
|
+
export {
|
|
708
|
+
registerOpenAiWorker,
|
|
709
|
+
registerOpenAiInline,
|
|
710
|
+
loadOpenAISDK,
|
|
711
|
+
getModelName,
|
|
712
|
+
getClient,
|
|
713
|
+
OPENAI_ALLOWED_HOSTS
|
|
714
|
+
};
|
|
715
|
+
|
|
716
|
+
//# debugId=C59188D3A89EE51E64756E2164756E21
|