@workglow/anthropic 0.2.33 → 0.2.35
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/AnthropicProvider.d.ts +16 -22
- package/dist/ai/AnthropicProvider.d.ts.map +1 -1
- package/dist/ai/AnthropicQueuedProvider.d.ts +22 -22
- package/dist/ai/AnthropicQueuedProvider.d.ts.map +1 -1
- package/dist/ai/common/Anthropic_Capabilities.d.ts +38 -0
- package/dist/ai/common/Anthropic_Capabilities.d.ts.map +1 -0
- package/dist/ai/common/Anthropic_CapabilitySets.d.ts +32 -0
- package/dist/ai/common/Anthropic_CapabilitySets.d.ts.map +1 -0
- package/dist/ai/common/Anthropic_CountTokens.d.ts +1 -1
- package/dist/ai/common/Anthropic_CountTokens.d.ts.map +1 -1
- package/dist/ai/common/Anthropic_JobRunFns.d.ts +12 -3
- package/dist/ai/common/Anthropic_JobRunFns.d.ts.map +1 -1
- package/dist/ai/common/Anthropic_ModelInfo.d.ts +1 -1
- package/dist/ai/common/Anthropic_ModelInfo.d.ts.map +1 -1
- package/dist/ai/common/Anthropic_ModelSchema.d.ts +3 -3
- package/dist/ai/common/Anthropic_ModelSearch.d.ts +1 -1
- package/dist/ai/common/Anthropic_ModelSearch.d.ts.map +1 -1
- package/dist/ai/common/Anthropic_StructuredGeneration.d.ts +14 -3
- package/dist/ai/common/Anthropic_StructuredGeneration.d.ts.map +1 -1
- package/dist/ai/common/Anthropic_TextGeneration.d.ts +13 -3
- package/dist/ai/common/Anthropic_TextGeneration.d.ts.map +1 -1
- package/dist/ai/common/Anthropic_TextRewriter.d.ts +2 -3
- package/dist/ai/common/Anthropic_TextRewriter.d.ts.map +1 -1
- package/dist/ai/common/Anthropic_TextSummary.d.ts +2 -3
- package/dist/ai/common/Anthropic_TextSummary.d.ts.map +1 -1
- package/dist/ai/common/Anthropic_ToolCalling.d.ts +2 -3
- package/dist/ai/common/Anthropic_ToolCalling.d.ts.map +1 -1
- package/dist/ai/index.d.ts +26 -0
- package/dist/ai/index.d.ts.map +1 -1
- package/dist/ai/registerAnthropicInline.d.ts.map +1 -1
- package/dist/ai/registerAnthropicWorker.d.ts.map +1 -1
- package/dist/ai/runtime.d.ts.map +1 -1
- package/dist/ai-runtime.d.ts.map +1 -1
- package/dist/ai-runtime.js +245 -355
- package/dist/ai-runtime.js.map +18 -17
- package/dist/ai.d.ts.map +1 -1
- package/dist/ai.js +455 -19
- package/dist/ai.js.map +16 -5
- package/package.json +13 -14
- package/dist/ai/common/Anthropic_Chat.d.ts +0 -10
- package/dist/ai/common/Anthropic_Chat.d.ts.map +0 -1
package/dist/ai-runtime.js
CHANGED
|
@@ -55,25 +55,131 @@ import { createCloudProviderClass } from "@workglow/ai/provider-utils";
|
|
|
55
55
|
// src/ai/common/Anthropic_Constants.ts
|
|
56
56
|
var ANTHROPIC = "ANTHROPIC";
|
|
57
57
|
|
|
58
|
-
// src/ai/
|
|
59
|
-
var
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
58
|
+
// src/ai/common/Anthropic_CapabilitySets.ts
|
|
59
|
+
var ANTHROPIC_TEXT_GENERATION = ["text.generation"];
|
|
60
|
+
var ANTHROPIC_TOOL_USE = ["text.generation", "tool-use"];
|
|
61
|
+
var ANTHROPIC_JSON_MODE = ["text.generation", "json-mode"];
|
|
62
|
+
var ANTHROPIC_TEXT_REWRITER = ["text.rewriter"];
|
|
63
|
+
var ANTHROPIC_TEXT_SUMMARY = ["text.summary"];
|
|
64
|
+
var ANTHROPIC_COUNT_TOKENS = ["model.count-tokens"];
|
|
65
|
+
var ANTHROPIC_MODEL_SEARCH = ["model.search"];
|
|
66
|
+
var ANTHROPIC_MODEL_INFO = ["model.info"];
|
|
67
|
+
var ANTHROPIC_CAPABILITY_SETS = [
|
|
68
|
+
ANTHROPIC_TEXT_GENERATION,
|
|
69
|
+
ANTHROPIC_TOOL_USE,
|
|
70
|
+
ANTHROPIC_JSON_MODE,
|
|
71
|
+
ANTHROPIC_TEXT_REWRITER,
|
|
72
|
+
ANTHROPIC_TEXT_SUMMARY,
|
|
73
|
+
ANTHROPIC_COUNT_TOKENS,
|
|
74
|
+
ANTHROPIC_MODEL_SEARCH,
|
|
75
|
+
ANTHROPIC_MODEL_INFO
|
|
68
76
|
];
|
|
69
77
|
|
|
78
|
+
// src/ai/common/Anthropic_Capabilities.ts
|
|
79
|
+
var ANTHROPIC_RUN_FN_SPECS = ANTHROPIC_CAPABILITY_SETS.map((serves) => ({ serves }));
|
|
80
|
+
function anthropicWorkerRunFnSpecs() {
|
|
81
|
+
return ANTHROPIC_RUN_FN_SPECS;
|
|
82
|
+
}
|
|
83
|
+
function inferAnthropicCapabilities(model) {
|
|
84
|
+
const id = String(model.model_id ?? model.provider_config?.model_name ?? "");
|
|
85
|
+
if (/^claude-3[.-][57]-/i.test(id)) {
|
|
86
|
+
return [
|
|
87
|
+
"text.generation",
|
|
88
|
+
"text.rewriter",
|
|
89
|
+
"text.summary",
|
|
90
|
+
"tool-use",
|
|
91
|
+
"json-mode",
|
|
92
|
+
"vision-input",
|
|
93
|
+
"model.count-tokens",
|
|
94
|
+
"model.info",
|
|
95
|
+
"model.search"
|
|
96
|
+
];
|
|
97
|
+
}
|
|
98
|
+
if (/^claude-(sonnet|opus|haiku)-4/i.test(id)) {
|
|
99
|
+
return [
|
|
100
|
+
"text.generation",
|
|
101
|
+
"text.rewriter",
|
|
102
|
+
"text.summary",
|
|
103
|
+
"tool-use",
|
|
104
|
+
"json-mode",
|
|
105
|
+
"vision-input",
|
|
106
|
+
"model.count-tokens",
|
|
107
|
+
"model.info",
|
|
108
|
+
"model.search"
|
|
109
|
+
];
|
|
110
|
+
}
|
|
111
|
+
if (/^claude-3[.-](haiku|opus|sonnet)/i.test(id)) {
|
|
112
|
+
return [
|
|
113
|
+
"text.generation",
|
|
114
|
+
"text.rewriter",
|
|
115
|
+
"text.summary",
|
|
116
|
+
"tool-use",
|
|
117
|
+
"json-mode",
|
|
118
|
+
"vision-input",
|
|
119
|
+
"model.count-tokens",
|
|
120
|
+
"model.info",
|
|
121
|
+
"model.search"
|
|
122
|
+
];
|
|
123
|
+
}
|
|
124
|
+
if (/^claude-2/i.test(id)) {
|
|
125
|
+
return [
|
|
126
|
+
"text.generation",
|
|
127
|
+
"text.rewriter",
|
|
128
|
+
"text.summary",
|
|
129
|
+
"tool-use",
|
|
130
|
+
"json-mode",
|
|
131
|
+
"model.count-tokens",
|
|
132
|
+
"model.info",
|
|
133
|
+
"model.search"
|
|
134
|
+
];
|
|
135
|
+
}
|
|
136
|
+
const declared = model.capabilities ?? [];
|
|
137
|
+
if (declared.length > 0)
|
|
138
|
+
return declared;
|
|
139
|
+
return ["model.search", "model.info"];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/ai/AnthropicQueuedProvider.ts
|
|
70
143
|
class AnthropicQueuedProvider extends createCloudProviderClass(AiProvider, {
|
|
71
144
|
name: ANTHROPIC,
|
|
72
|
-
displayName: "Anthropic"
|
|
73
|
-
taskTypes: ANTHROPIC_TASK_TYPES
|
|
145
|
+
displayName: "Anthropic"
|
|
74
146
|
}) {
|
|
147
|
+
inferCapabilities(model) {
|
|
148
|
+
return inferAnthropicCapabilities(model);
|
|
149
|
+
}
|
|
150
|
+
workerRunFnSpecs() {
|
|
151
|
+
return anthropicWorkerRunFnSpecs();
|
|
152
|
+
}
|
|
75
153
|
}
|
|
76
154
|
|
|
155
|
+
// src/ai/common/Anthropic_CountTokens.ts
|
|
156
|
+
var Anthropic_CountTokens_Stream = async (input, model, signal, emit) => {
|
|
157
|
+
const client = await getClient(model);
|
|
158
|
+
const result = await client.messages.countTokens({
|
|
159
|
+
model: getModelName(model),
|
|
160
|
+
messages: [{ role: "user", content: input.text }]
|
|
161
|
+
});
|
|
162
|
+
emit({ type: "finish", data: { count: result.input_tokens } });
|
|
163
|
+
};
|
|
164
|
+
var Anthropic_CountTokens_Preview = async (input, _model) => {
|
|
165
|
+
return { count: Math.ceil(input.text.length / 4) };
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
// src/ai/common/Anthropic_ModelInfo.ts
|
|
169
|
+
var Anthropic_ModelInfo_Stream = async (input, _model, _signal, emit) => {
|
|
170
|
+
const result = {
|
|
171
|
+
model: input.model,
|
|
172
|
+
is_local: false,
|
|
173
|
+
is_remote: true,
|
|
174
|
+
supports_browser: true,
|
|
175
|
+
supports_node: true,
|
|
176
|
+
is_cached: false,
|
|
177
|
+
is_loaded: false,
|
|
178
|
+
file_sizes: null
|
|
179
|
+
};
|
|
180
|
+
emit({ type: "finish", data: result });
|
|
181
|
+
};
|
|
182
|
+
|
|
77
183
|
// src/ai/common/Anthropic_ModelSearch.ts
|
|
78
184
|
import { filterLabeledModelsByQuery } from "@workglow/ai/provider-utils";
|
|
79
185
|
var ANTHROPIC_FALLBACK = [
|
|
@@ -106,14 +212,14 @@ function mapModelList(models) {
|
|
|
106
212
|
provider: ANTHROPIC,
|
|
107
213
|
title: m.value,
|
|
108
214
|
description: "",
|
|
109
|
-
|
|
215
|
+
capabilities: [],
|
|
110
216
|
provider_config: { model_name: m.value },
|
|
111
217
|
metadata: {}
|
|
112
218
|
},
|
|
113
219
|
raw: m
|
|
114
220
|
}));
|
|
115
221
|
}
|
|
116
|
-
var
|
|
222
|
+
var Anthropic_ModelSearch_Stream = async (input, _model, _signal, emit) => {
|
|
117
223
|
let models;
|
|
118
224
|
if (!input.credential_key) {
|
|
119
225
|
models = ANTHROPIC_FALLBACK;
|
|
@@ -121,12 +227,53 @@ var Anthropic_ModelSearch = async (input) => {
|
|
|
121
227
|
models = await listAnthropicModels(input.credential_key);
|
|
122
228
|
}
|
|
123
229
|
models = filterLabeledModelsByQuery(models, input.query);
|
|
124
|
-
|
|
230
|
+
emit({ type: "finish", data: { results: mapModelList(models) } });
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
// src/ai/common/Anthropic_StructuredGeneration.ts
|
|
234
|
+
import { parsePartialJson } from "@workglow/util/worker";
|
|
235
|
+
var Anthropic_StructuredGeneration_Stream = async (input, model, signal, emit, outputSchema) => {
|
|
236
|
+
const client = await getClient(model);
|
|
237
|
+
const modelName = getModelName(model);
|
|
238
|
+
const schema = input.outputSchema ?? outputSchema;
|
|
239
|
+
const stream = client.messages.stream({
|
|
240
|
+
model: modelName,
|
|
241
|
+
messages: [{ role: "user", content: input.prompt }],
|
|
242
|
+
tools: [
|
|
243
|
+
{
|
|
244
|
+
name: "structured_output",
|
|
245
|
+
description: "Output structured data conforming to the schema",
|
|
246
|
+
input_schema: schema
|
|
247
|
+
}
|
|
248
|
+
],
|
|
249
|
+
tool_choice: { type: "tool", name: "structured_output" },
|
|
250
|
+
max_tokens: getMaxTokens(input, model)
|
|
251
|
+
}, { signal });
|
|
252
|
+
let accumulatedJson = "";
|
|
253
|
+
for await (const event of stream) {
|
|
254
|
+
if (event.type === "content_block_delta" && event.delta.type === "input_json_delta") {
|
|
255
|
+
accumulatedJson += event.delta.partial_json;
|
|
256
|
+
const partial = parsePartialJson(accumulatedJson);
|
|
257
|
+
if (partial !== undefined) {
|
|
258
|
+
emit({ type: "object-delta", port: "object", objectDelta: partial });
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
let finalObject;
|
|
263
|
+
try {
|
|
264
|
+
finalObject = JSON.parse(accumulatedJson);
|
|
265
|
+
} catch {
|
|
266
|
+
finalObject = parsePartialJson(accumulatedJson) ?? {};
|
|
267
|
+
}
|
|
268
|
+
emit({ type: "finish", data: { object: finalObject } });
|
|
125
269
|
};
|
|
126
270
|
|
|
271
|
+
// src/ai/common/Anthropic_TextGeneration.ts
|
|
272
|
+
import { getLogger } from "@workglow/util/worker";
|
|
273
|
+
|
|
127
274
|
// src/ai/common/Anthropic_ToolCalling.ts
|
|
128
275
|
import { buildToolDescription, filterValidToolCalls } from "@workglow/ai/worker";
|
|
129
|
-
import { parsePartialJson } from "@workglow/util/worker";
|
|
276
|
+
import { parsePartialJson as parsePartialJson2 } from "@workglow/util/worker";
|
|
130
277
|
function buildAnthropicMessages(messages, prompt) {
|
|
131
278
|
if (!messages || messages.length === 0) {
|
|
132
279
|
return [{ role: "user", content: prompt }];
|
|
@@ -192,62 +339,7 @@ function mapAnthropicToolChoice(toolChoice) {
|
|
|
192
339
|
return { type: "any" };
|
|
193
340
|
return { type: "tool", name: toolChoice };
|
|
194
341
|
}
|
|
195
|
-
var
|
|
196
|
-
update_progress(0, "Starting Anthropic tool calling");
|
|
197
|
-
const client = await getClient(model);
|
|
198
|
-
const modelName = getModelName(model);
|
|
199
|
-
const tools = input.tools.map((t) => ({
|
|
200
|
-
name: t.name,
|
|
201
|
-
description: buildToolDescription(t),
|
|
202
|
-
input_schema: t.inputSchema
|
|
203
|
-
}));
|
|
204
|
-
const toolChoice = mapAnthropicToolChoice(input.toolChoice);
|
|
205
|
-
const messages = buildAnthropicMessages(input.messages, input.prompt);
|
|
206
|
-
const params = {
|
|
207
|
-
model: modelName,
|
|
208
|
-
messages,
|
|
209
|
-
max_tokens: getMaxTokens(input, model),
|
|
210
|
-
temperature: input.temperature
|
|
211
|
-
};
|
|
212
|
-
if (input.systemPrompt) {
|
|
213
|
-
params.system = input.systemPrompt;
|
|
214
|
-
}
|
|
215
|
-
if (toolChoice !== undefined) {
|
|
216
|
-
params.tools = tools;
|
|
217
|
-
params.tool_choice = toolChoice;
|
|
218
|
-
}
|
|
219
|
-
if (sessionId) {
|
|
220
|
-
if (params.system) {
|
|
221
|
-
params.system = [
|
|
222
|
-
{
|
|
223
|
-
type: "text",
|
|
224
|
-
text: params.system,
|
|
225
|
-
cache_control: { type: "ephemeral" }
|
|
226
|
-
}
|
|
227
|
-
];
|
|
228
|
-
}
|
|
229
|
-
if (params.tools && params.tools.length > 0) {
|
|
230
|
-
const lastIdx = params.tools.length - 1;
|
|
231
|
-
params.tools[lastIdx] = {
|
|
232
|
-
...params.tools[lastIdx],
|
|
233
|
-
cache_control: { type: "ephemeral" }
|
|
234
|
-
};
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
const response = await client.messages.create(params, { signal });
|
|
238
|
-
const text = response.content.filter((b) => b.type === "text").map((b) => b.text).join("");
|
|
239
|
-
const toolCalls = [];
|
|
240
|
-
response.content.filter((b) => b.type === "tool_use").forEach((b) => {
|
|
241
|
-
toolCalls.push({
|
|
242
|
-
id: b.id,
|
|
243
|
-
name: b.name,
|
|
244
|
-
input: b.input ?? {}
|
|
245
|
-
});
|
|
246
|
-
});
|
|
247
|
-
update_progress(100, "Completed Anthropic tool calling");
|
|
248
|
-
return { text, toolCalls: filterValidToolCalls(toolCalls, input.tools) };
|
|
249
|
-
};
|
|
250
|
-
var Anthropic_ToolCalling_Stream = async function* (input, model, signal, _outputSchema, sessionId) {
|
|
342
|
+
var Anthropic_ToolCalling_Stream = async (input, model, signal, emit, _outputSchema, sessionId) => {
|
|
251
343
|
const client = await getClient(model);
|
|
252
344
|
const modelName = getModelName(model);
|
|
253
345
|
const tools = input.tools.map((t) => ({
|
|
@@ -292,6 +384,7 @@ var Anthropic_ToolCalling_Stream = async function* (input, model, signal, _outpu
|
|
|
292
384
|
const blockMeta = new Map;
|
|
293
385
|
const toolCallsByBlockIndex = new Map;
|
|
294
386
|
const toolCallsInStreamOrder = () => [...toolCallsByBlockIndex.entries()].sort((a, b) => a[0] - b[0]).map(([, tc]) => tc);
|
|
387
|
+
const validatedToolCallsInStreamOrder = () => filterValidToolCalls(toolCallsInStreamOrder(), input.tools);
|
|
295
388
|
for await (const event of stream) {
|
|
296
389
|
if (event.type === "content_block_start") {
|
|
297
390
|
const block = event.content_block;
|
|
@@ -310,7 +403,7 @@ var Anthropic_ToolCalling_Stream = async function* (input, model, signal, _outpu
|
|
|
310
403
|
const index = event.index;
|
|
311
404
|
const delta = event.delta;
|
|
312
405
|
if (delta.type === "text_delta") {
|
|
313
|
-
|
|
406
|
+
emit({ type: "text-delta", port: "text", textDelta: delta.text });
|
|
314
407
|
} else if (delta.type === "input_json_delta") {
|
|
315
408
|
const meta = blockMeta.get(index);
|
|
316
409
|
if (meta) {
|
|
@@ -319,7 +412,7 @@ var Anthropic_ToolCalling_Stream = async function* (input, model, signal, _outpu
|
|
|
319
412
|
try {
|
|
320
413
|
parsedInput = JSON.parse(meta.json);
|
|
321
414
|
} catch {
|
|
322
|
-
const partial =
|
|
415
|
+
const partial = parsePartialJson2(meta.json);
|
|
323
416
|
parsedInput = partial ?? {};
|
|
324
417
|
}
|
|
325
418
|
toolCallsByBlockIndex.set(index, {
|
|
@@ -327,11 +420,11 @@ var Anthropic_ToolCalling_Stream = async function* (input, model, signal, _outpu
|
|
|
327
420
|
name: meta.name ?? "",
|
|
328
421
|
input: parsedInput
|
|
329
422
|
});
|
|
330
|
-
|
|
423
|
+
emit({
|
|
331
424
|
type: "object-delta",
|
|
332
425
|
port: "toolCalls",
|
|
333
|
-
objectDelta:
|
|
334
|
-
};
|
|
426
|
+
objectDelta: validatedToolCallsInStreamOrder()
|
|
427
|
+
});
|
|
335
428
|
}
|
|
336
429
|
}
|
|
337
430
|
} else if (event.type === "content_block_stop") {
|
|
@@ -342,250 +435,76 @@ var Anthropic_ToolCalling_Stream = async function* (input, model, signal, _outpu
|
|
|
342
435
|
try {
|
|
343
436
|
finalInput = JSON.parse(meta.json);
|
|
344
437
|
} catch {
|
|
345
|
-
finalInput =
|
|
438
|
+
finalInput = parsePartialJson2(meta.json) ?? {};
|
|
346
439
|
}
|
|
347
440
|
const id = meta.id ?? "";
|
|
348
441
|
toolCallsByBlockIndex.set(index, { id, name: meta.name ?? "", input: finalInput });
|
|
349
|
-
|
|
442
|
+
emit({
|
|
350
443
|
type: "object-delta",
|
|
351
444
|
port: "toolCalls",
|
|
352
|
-
objectDelta:
|
|
353
|
-
};
|
|
445
|
+
objectDelta: validatedToolCallsInStreamOrder()
|
|
446
|
+
});
|
|
354
447
|
}
|
|
355
448
|
blockMeta.delete(index);
|
|
356
449
|
}
|
|
357
450
|
}
|
|
358
|
-
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
// src/ai/common/Anthropic_Chat.ts
|
|
362
|
-
function buildParams(input, model, sessionId) {
|
|
363
|
-
const messages = buildAnthropicMessages(input.messages, input.prompt);
|
|
364
|
-
const params = {
|
|
365
|
-
model: getModelName(model),
|
|
366
|
-
messages,
|
|
367
|
-
max_tokens: getMaxTokens({ maxTokens: input.maxTokens }, model)
|
|
368
|
-
};
|
|
369
|
-
if (input.temperature !== undefined)
|
|
370
|
-
params.temperature = input.temperature;
|
|
371
|
-
if (input.systemPrompt) {
|
|
372
|
-
params.system = sessionId ? [
|
|
373
|
-
{
|
|
374
|
-
type: "text",
|
|
375
|
-
text: input.systemPrompt,
|
|
376
|
-
cache_control: { type: "ephemeral" }
|
|
377
|
-
}
|
|
378
|
-
] : input.systemPrompt;
|
|
379
|
-
}
|
|
380
|
-
if (sessionId && messages.length > 0) {
|
|
381
|
-
const last = messages[messages.length - 1];
|
|
382
|
-
if (Array.isArray(last.content) && last.content.length > 0) {
|
|
383
|
-
const blocks = last.content;
|
|
384
|
-
blocks[blocks.length - 1] = {
|
|
385
|
-
...blocks[blocks.length - 1],
|
|
386
|
-
cache_control: { type: "ephemeral" }
|
|
387
|
-
};
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
return params;
|
|
391
|
-
}
|
|
392
|
-
var Anthropic_Chat = async (input, model, update_progress, signal, _outputSchema, sessionId) => {
|
|
393
|
-
update_progress(0, "Anthropic chat turn");
|
|
394
|
-
const client = await getClient(model);
|
|
395
|
-
const params = buildParams(input, model, sessionId);
|
|
396
|
-
const response = await client.messages.create(params, { signal });
|
|
397
|
-
const text = response.content.filter((b) => b.type === "text").map((b) => b.text).join("");
|
|
398
|
-
update_progress(100, "Turn complete");
|
|
399
|
-
return { text };
|
|
400
|
-
};
|
|
401
|
-
var Anthropic_Chat_Stream = async function* (input, model, signal, _outputSchema, sessionId) {
|
|
402
|
-
const client = await getClient(model);
|
|
403
|
-
const params = buildParams(input, model, sessionId);
|
|
404
|
-
const stream = client.messages.stream(params, { signal });
|
|
405
|
-
for await (const event of stream) {
|
|
406
|
-
const e = event;
|
|
407
|
-
if (e.type === "content_block_delta" && e.delta?.type === "text_delta") {
|
|
408
|
-
yield { type: "text-delta", port: "text", textDelta: e.delta.text ?? "" };
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
yield { type: "finish", data: {} };
|
|
412
|
-
};
|
|
413
|
-
|
|
414
|
-
// src/ai/common/Anthropic_CountTokens.ts
|
|
415
|
-
var Anthropic_CountTokens = async (input, model, onProgress, signal) => {
|
|
416
|
-
const client = await getClient(model);
|
|
417
|
-
const result = await client.messages.countTokens({
|
|
418
|
-
model: getModelName(model),
|
|
419
|
-
messages: [{ role: "user", content: input.text }]
|
|
420
|
-
});
|
|
421
|
-
return { count: result.input_tokens };
|
|
422
|
-
};
|
|
423
|
-
var Anthropic_CountTokens_Preview = async (input, _model) => {
|
|
424
|
-
return { count: Math.ceil(input.text.length / 4) };
|
|
425
|
-
};
|
|
426
|
-
|
|
427
|
-
// src/ai/common/Anthropic_ModelInfo.ts
|
|
428
|
-
var Anthropic_ModelInfo = async (input) => {
|
|
429
|
-
if (input.detail === "dimensions") {
|
|
430
|
-
return {
|
|
431
|
-
model: input.model,
|
|
432
|
-
is_local: false,
|
|
433
|
-
is_remote: true,
|
|
434
|
-
supports_browser: true,
|
|
435
|
-
supports_node: true,
|
|
436
|
-
is_cached: false,
|
|
437
|
-
is_loaded: false,
|
|
438
|
-
file_sizes: null
|
|
439
|
-
};
|
|
440
|
-
}
|
|
441
|
-
return {
|
|
442
|
-
model: input.model,
|
|
443
|
-
is_local: false,
|
|
444
|
-
is_remote: true,
|
|
445
|
-
supports_browser: true,
|
|
446
|
-
supports_node: true,
|
|
447
|
-
is_cached: false,
|
|
448
|
-
is_loaded: false,
|
|
449
|
-
file_sizes: null
|
|
450
|
-
};
|
|
451
|
-
};
|
|
452
|
-
|
|
453
|
-
// src/ai/common/Anthropic_StructuredGeneration.ts
|
|
454
|
-
import { parsePartialJson as parsePartialJson2 } from "@workglow/util/worker";
|
|
455
|
-
var Anthropic_StructuredGeneration = async (input, model, update_progress, signal, outputSchema) => {
|
|
456
|
-
update_progress(0, "Starting Anthropic structured generation");
|
|
457
|
-
const client = await getClient(model);
|
|
458
|
-
const modelName = getModelName(model);
|
|
459
|
-
const schema = input.outputSchema ?? outputSchema;
|
|
460
|
-
const response = await client.messages.create({
|
|
461
|
-
model: modelName,
|
|
462
|
-
messages: [{ role: "user", content: input.prompt }],
|
|
463
|
-
tools: [
|
|
464
|
-
{
|
|
465
|
-
name: "structured_output",
|
|
466
|
-
description: "Output structured data conforming to the schema",
|
|
467
|
-
input_schema: schema
|
|
468
|
-
}
|
|
469
|
-
],
|
|
470
|
-
tool_choice: { type: "tool", name: "structured_output" },
|
|
471
|
-
max_tokens: getMaxTokens(input, model)
|
|
472
|
-
}, { signal });
|
|
473
|
-
const toolBlock = response.content.find((b) => b.type === "tool_use");
|
|
474
|
-
const object = toolBlock?.input ?? {};
|
|
475
|
-
update_progress(100, "Completed Anthropic structured generation");
|
|
476
|
-
return { object };
|
|
477
|
-
};
|
|
478
|
-
var Anthropic_StructuredGeneration_Stream = async function* (input, model, signal, outputSchema) {
|
|
479
|
-
const client = await getClient(model);
|
|
480
|
-
const modelName = getModelName(model);
|
|
481
|
-
const schema = input.outputSchema ?? outputSchema;
|
|
482
|
-
const stream = client.messages.stream({
|
|
483
|
-
model: modelName,
|
|
484
|
-
messages: [{ role: "user", content: input.prompt }],
|
|
485
|
-
tools: [
|
|
486
|
-
{
|
|
487
|
-
name: "structured_output",
|
|
488
|
-
description: "Output structured data conforming to the schema",
|
|
489
|
-
input_schema: schema
|
|
490
|
-
}
|
|
491
|
-
],
|
|
492
|
-
tool_choice: { type: "tool", name: "structured_output" },
|
|
493
|
-
max_tokens: getMaxTokens(input, model)
|
|
494
|
-
}, { signal });
|
|
495
|
-
let accumulatedJson = "";
|
|
496
|
-
for await (const event of stream) {
|
|
497
|
-
if (event.type === "content_block_delta" && event.delta.type === "input_json_delta") {
|
|
498
|
-
accumulatedJson += event.delta.partial_json;
|
|
499
|
-
const partial = parsePartialJson2(accumulatedJson);
|
|
500
|
-
if (partial !== undefined) {
|
|
501
|
-
yield { type: "object-delta", port: "object", objectDelta: partial };
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
let finalObject;
|
|
506
|
-
try {
|
|
507
|
-
finalObject = JSON.parse(accumulatedJson);
|
|
508
|
-
} catch {
|
|
509
|
-
finalObject = parsePartialJson2(accumulatedJson) ?? {};
|
|
510
|
-
}
|
|
511
|
-
yield { type: "finish", data: { object: finalObject } };
|
|
451
|
+
emit({ type: "finish", data: { text: "", toolCalls: [] } });
|
|
512
452
|
};
|
|
513
453
|
|
|
514
454
|
// src/ai/common/Anthropic_TextGeneration.ts
|
|
515
|
-
|
|
516
|
-
var Anthropic_TextGeneration = async (input, model, update_progress, signal, _outputSchema, sessionId) => {
|
|
455
|
+
var Anthropic_TextGeneration_Stream = async (input, model, signal, emit, _outputSchema, sessionId) => {
|
|
517
456
|
const logger = getLogger();
|
|
518
|
-
const timerLabel = `anthropic:TextGeneration:${model
|
|
519
|
-
logger.time(timerLabel, { model: model
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
messages: [{ role: "user", content: input.prompt }]
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
457
|
+
const timerLabel = `anthropic:TextGeneration:${getModelName(model)}`;
|
|
458
|
+
logger.time(timerLabel, { model: getModelName(model) });
|
|
459
|
+
try {
|
|
460
|
+
const client = await getClient(model);
|
|
461
|
+
const modelName = getModelName(model);
|
|
462
|
+
const unified = input;
|
|
463
|
+
const hasMessages = Array.isArray(unified.messages) && unified.messages.length > 0;
|
|
464
|
+
const messages = hasMessages ? buildAnthropicMessages(unified.messages, unified.prompt ?? "") : [{ role: "user", content: input.prompt }];
|
|
465
|
+
const params = {
|
|
466
|
+
model: modelName,
|
|
467
|
+
messages,
|
|
468
|
+
max_tokens: getMaxTokens(input, model)
|
|
469
|
+
};
|
|
470
|
+
if (input.temperature !== undefined)
|
|
471
|
+
params.temperature = input.temperature;
|
|
472
|
+
if (input.topP !== undefined)
|
|
473
|
+
params.top_p = input.topP;
|
|
474
|
+
if (unified.systemPrompt) {
|
|
475
|
+
params.system = sessionId ? [
|
|
476
|
+
{
|
|
477
|
+
type: "text",
|
|
478
|
+
text: unified.systemPrompt,
|
|
479
|
+
cache_control: { type: "ephemeral" }
|
|
480
|
+
}
|
|
481
|
+
] : unified.systemPrompt;
|
|
482
|
+
}
|
|
483
|
+
if (sessionId && hasMessages && Array.isArray(messages) && messages.length > 0) {
|
|
484
|
+
const last = messages[messages.length - 1];
|
|
485
|
+
if (Array.isArray(last.content) && last.content.length > 0) {
|
|
486
|
+
const blocks = last.content;
|
|
487
|
+
blocks[blocks.length - 1] = {
|
|
488
|
+
...blocks[blocks.length - 1],
|
|
489
|
+
cache_control: { type: "ephemeral" }
|
|
490
|
+
};
|
|
536
491
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
return { text };
|
|
544
|
-
};
|
|
545
|
-
var Anthropic_TextGeneration_Stream = async function* (input, model, signal, _outputSchema, sessionId) {
|
|
546
|
-
const client = await getClient(model);
|
|
547
|
-
const modelName = getModelName(model);
|
|
548
|
-
const params = {
|
|
549
|
-
model: modelName,
|
|
550
|
-
messages: [{ role: "user", content: input.prompt }],
|
|
551
|
-
max_tokens: getMaxTokens(input, model),
|
|
552
|
-
temperature: input.temperature,
|
|
553
|
-
top_p: input.topP
|
|
554
|
-
};
|
|
555
|
-
if (sessionId && params.system) {
|
|
556
|
-
params.system = [
|
|
557
|
-
{
|
|
558
|
-
type: "text",
|
|
559
|
-
text: params.system,
|
|
560
|
-
cache_control: { type: "ephemeral" }
|
|
492
|
+
}
|
|
493
|
+
const stream = client.messages.stream(params, { signal });
|
|
494
|
+
for await (const event of stream) {
|
|
495
|
+
const e = event;
|
|
496
|
+
if (e.type === "content_block_delta" && e.delta?.type === "text_delta") {
|
|
497
|
+
emit({ type: "text-delta", port: "text", textDelta: e.delta.text ?? "" });
|
|
561
498
|
}
|
|
562
|
-
];
|
|
563
|
-
}
|
|
564
|
-
const stream = client.messages.stream(params, { signal });
|
|
565
|
-
for await (const event of stream) {
|
|
566
|
-
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
|
|
567
|
-
yield { type: "text-delta", port: "text", textDelta: event.delta.text };
|
|
568
499
|
}
|
|
500
|
+
emit({ type: "finish", data: {} });
|
|
501
|
+
} finally {
|
|
502
|
+
logger.timeEnd(timerLabel, { model: getModelName(model) });
|
|
569
503
|
}
|
|
570
|
-
yield { type: "finish", data: {} };
|
|
571
504
|
};
|
|
572
505
|
|
|
573
506
|
// src/ai/common/Anthropic_TextRewriter.ts
|
|
574
|
-
var
|
|
575
|
-
update_progress(0, "Starting Anthropic text rewriting");
|
|
576
|
-
const client = await getClient(model);
|
|
577
|
-
const modelName = getModelName(model);
|
|
578
|
-
const response = await client.messages.create({
|
|
579
|
-
model: modelName,
|
|
580
|
-
system: input.prompt,
|
|
581
|
-
messages: [{ role: "user", content: input.text }],
|
|
582
|
-
max_tokens: getMaxTokens({}, model)
|
|
583
|
-
}, { signal });
|
|
584
|
-
const text = response.content[0]?.type === "text" ? response.content[0].text : "";
|
|
585
|
-
update_progress(100, "Completed Anthropic text rewriting");
|
|
586
|
-
return { text };
|
|
587
|
-
};
|
|
588
|
-
var Anthropic_TextRewriter_Stream = async function* (input, model, signal) {
|
|
507
|
+
var Anthropic_TextRewriter_Stream = async (input, model, signal, emit) => {
|
|
589
508
|
const client = await getClient(model);
|
|
590
509
|
const modelName = getModelName(model);
|
|
591
510
|
const stream = client.messages.stream({
|
|
@@ -596,28 +515,14 @@ var Anthropic_TextRewriter_Stream = async function* (input, model, signal) {
|
|
|
596
515
|
}, { signal });
|
|
597
516
|
for await (const event of stream) {
|
|
598
517
|
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
|
|
599
|
-
|
|
518
|
+
emit({ type: "text-delta", port: "text", textDelta: event.delta.text });
|
|
600
519
|
}
|
|
601
520
|
}
|
|
602
|
-
|
|
521
|
+
emit({ type: "finish", data: {} });
|
|
603
522
|
};
|
|
604
523
|
|
|
605
524
|
// src/ai/common/Anthropic_TextSummary.ts
|
|
606
|
-
var
|
|
607
|
-
update_progress(0, "Starting Anthropic text summarization");
|
|
608
|
-
const client = await getClient(model);
|
|
609
|
-
const modelName = getModelName(model);
|
|
610
|
-
const response = await client.messages.create({
|
|
611
|
-
model: modelName,
|
|
612
|
-
system: "Summarize the following text concisely.",
|
|
613
|
-
messages: [{ role: "user", content: input.text }],
|
|
614
|
-
max_tokens: getMaxTokens({}, model)
|
|
615
|
-
}, { signal });
|
|
616
|
-
const text = response.content[0]?.type === "text" ? response.content[0].text : "";
|
|
617
|
-
update_progress(100, "Completed Anthropic text summarization");
|
|
618
|
-
return { text };
|
|
619
|
-
};
|
|
620
|
-
var Anthropic_TextSummary_Stream = async function* (input, model, signal) {
|
|
525
|
+
var Anthropic_TextSummary_Stream = async (input, model, signal, emit) => {
|
|
621
526
|
const client = await getClient(model);
|
|
622
527
|
const modelName = getModelName(model);
|
|
623
528
|
const stream = client.messages.stream({
|
|
@@ -628,39 +533,30 @@ var Anthropic_TextSummary_Stream = async function* (input, model, signal) {
|
|
|
628
533
|
}, { signal });
|
|
629
534
|
for await (const event of stream) {
|
|
630
535
|
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
|
|
631
|
-
|
|
536
|
+
emit({ type: "text-delta", port: "text", textDelta: event.delta.text });
|
|
632
537
|
}
|
|
633
538
|
}
|
|
634
|
-
|
|
539
|
+
emit({ type: "finish", data: {} });
|
|
635
540
|
};
|
|
636
541
|
|
|
637
542
|
// src/ai/common/Anthropic_JobRunFns.ts
|
|
638
|
-
var
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
};
|
|
649
|
-
var ANTHROPIC_STREAM_TASKS = {
|
|
650
|
-
AiChatTask: Anthropic_Chat_Stream,
|
|
651
|
-
TextGenerationTask: Anthropic_TextGeneration_Stream,
|
|
652
|
-
TextRewriterTask: Anthropic_TextRewriter_Stream,
|
|
653
|
-
TextSummaryTask: Anthropic_TextSummary_Stream,
|
|
654
|
-
StructuredGenerationTask: Anthropic_StructuredGeneration_Stream,
|
|
655
|
-
ToolCallingTask: Anthropic_ToolCalling_Stream
|
|
656
|
-
};
|
|
543
|
+
var ANTHROPIC_RUN_FNS = [
|
|
544
|
+
{ serves: ANTHROPIC_TEXT_GENERATION, runFn: Anthropic_TextGeneration_Stream },
|
|
545
|
+
{ serves: ANTHROPIC_TOOL_USE, runFn: Anthropic_ToolCalling_Stream },
|
|
546
|
+
{ serves: ANTHROPIC_JSON_MODE, runFn: Anthropic_StructuredGeneration_Stream },
|
|
547
|
+
{ serves: ANTHROPIC_TEXT_REWRITER, runFn: Anthropic_TextRewriter_Stream },
|
|
548
|
+
{ serves: ANTHROPIC_TEXT_SUMMARY, runFn: Anthropic_TextSummary_Stream },
|
|
549
|
+
{ serves: ANTHROPIC_COUNT_TOKENS, runFn: Anthropic_CountTokens_Stream },
|
|
550
|
+
{ serves: ANTHROPIC_MODEL_SEARCH, runFn: Anthropic_ModelSearch_Stream },
|
|
551
|
+
{ serves: ANTHROPIC_MODEL_INFO, runFn: Anthropic_ModelInfo_Stream }
|
|
552
|
+
];
|
|
657
553
|
var ANTHROPIC_PREVIEW_TASKS = {
|
|
658
554
|
CountTokensTask: Anthropic_CountTokens_Preview
|
|
659
555
|
};
|
|
660
556
|
|
|
661
557
|
// src/ai/registerAnthropicInline.ts
|
|
662
558
|
async function registerAnthropicInline(options) {
|
|
663
|
-
await registerProviderInline(new AnthropicQueuedProvider(
|
|
559
|
+
await registerProviderInline(new AnthropicQueuedProvider(ANTHROPIC_RUN_FNS, ANTHROPIC_PREVIEW_TASKS), "Anthropic", options);
|
|
664
560
|
}
|
|
665
561
|
|
|
666
562
|
// src/ai/registerAnthropicWorker.ts
|
|
@@ -669,27 +565,21 @@ import { registerProviderWorker } from "@workglow/ai/provider-utils";
|
|
|
669
565
|
// src/ai/AnthropicProvider.ts
|
|
670
566
|
import { AiProvider as AiProvider2 } from "@workglow/ai/worker";
|
|
671
567
|
import { createCloudProviderClass as createCloudProviderClass2 } from "@workglow/ai/provider-utils";
|
|
672
|
-
var ANTHROPIC_TASK_TYPES2 = [
|
|
673
|
-
"CountTokensTask",
|
|
674
|
-
"ModelInfoTask",
|
|
675
|
-
"TextGenerationTask",
|
|
676
|
-
"TextRewriterTask",
|
|
677
|
-
"TextSummaryTask",
|
|
678
|
-
"StructuredGenerationTask",
|
|
679
|
-
"ToolCallingTask",
|
|
680
|
-
"ModelSearchTask"
|
|
681
|
-
];
|
|
682
|
-
|
|
683
568
|
class AnthropicProvider extends createCloudProviderClass2(AiProvider2, {
|
|
684
569
|
name: ANTHROPIC,
|
|
685
|
-
displayName: "Anthropic"
|
|
686
|
-
taskTypes: ANTHROPIC_TASK_TYPES2
|
|
570
|
+
displayName: "Anthropic"
|
|
687
571
|
}) {
|
|
572
|
+
inferCapabilities(model) {
|
|
573
|
+
return inferAnthropicCapabilities(model);
|
|
574
|
+
}
|
|
575
|
+
workerRunFnSpecs() {
|
|
576
|
+
return anthropicWorkerRunFnSpecs();
|
|
577
|
+
}
|
|
688
578
|
}
|
|
689
579
|
|
|
690
580
|
// src/ai/registerAnthropicWorker.ts
|
|
691
581
|
async function registerAnthropicWorker() {
|
|
692
|
-
await registerProviderWorker((ws) => new AnthropicProvider(
|
|
582
|
+
await registerProviderWorker((ws) => new AnthropicProvider(ANTHROPIC_RUN_FNS, ANTHROPIC_PREVIEW_TASKS).registerOnWorkerServer(ws), "Anthropic");
|
|
693
583
|
}
|
|
694
584
|
export {
|
|
695
585
|
registerAnthropicWorker,
|
|
@@ -700,4 +590,4 @@ export {
|
|
|
700
590
|
getClient
|
|
701
591
|
};
|
|
702
592
|
|
|
703
|
-
//# debugId=
|
|
593
|
+
//# debugId=C12852ADBF7F344D64756E2164756E21
|