@workglow/anthropic 0.2.34 → 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.js
CHANGED
|
@@ -8,7 +8,6 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
8
8
|
|
|
9
9
|
// src/ai/common/Anthropic_Constants.ts
|
|
10
10
|
var ANTHROPIC = "ANTHROPIC";
|
|
11
|
-
|
|
12
11
|
// src/ai/common/Anthropic_ModelSchema.ts
|
|
13
12
|
import { ModelConfigSchema, ModelRecordSchema } from "@workglow/ai/worker";
|
|
14
13
|
var AnthropicModelSchema = {
|
|
@@ -68,7 +67,6 @@ var AnthropicModelConfigSchema = {
|
|
|
68
67
|
required: [...ModelConfigSchema.required, ...AnthropicModelSchema.required],
|
|
69
68
|
additionalProperties: false
|
|
70
69
|
};
|
|
71
|
-
|
|
72
70
|
// src/ai/common/Anthropic_ModelSearch.ts
|
|
73
71
|
import { filterLabeledModelsByQuery } from "@workglow/ai/provider-utils";
|
|
74
72
|
|
|
@@ -142,14 +140,14 @@ function mapModelList(models) {
|
|
|
142
140
|
provider: ANTHROPIC,
|
|
143
141
|
title: m.value,
|
|
144
142
|
description: "",
|
|
145
|
-
|
|
143
|
+
capabilities: [],
|
|
146
144
|
provider_config: { model_name: m.value },
|
|
147
145
|
metadata: {}
|
|
148
146
|
},
|
|
149
147
|
raw: m
|
|
150
148
|
}));
|
|
151
149
|
}
|
|
152
|
-
var
|
|
150
|
+
var Anthropic_ModelSearch_Stream = async (input, _model, _signal, emit) => {
|
|
153
151
|
let models;
|
|
154
152
|
if (!input.credential_key) {
|
|
155
153
|
models = ANTHROPIC_FALLBACK;
|
|
@@ -157,44 +155,482 @@ var Anthropic_ModelSearch = async (input) => {
|
|
|
157
155
|
models = await listAnthropicModels(input.credential_key);
|
|
158
156
|
}
|
|
159
157
|
models = filterLabeledModelsByQuery(models, input.query);
|
|
160
|
-
|
|
158
|
+
emit({ type: "finish", data: { results: mapModelList(models) } });
|
|
161
159
|
};
|
|
162
|
-
|
|
163
160
|
// src/ai/registerAnthropic.ts
|
|
164
161
|
import { registerProviderWithWorker } from "@workglow/ai/provider-utils";
|
|
165
162
|
|
|
166
163
|
// src/ai/AnthropicQueuedProvider.ts
|
|
167
164
|
import { AiProvider } from "@workglow/ai";
|
|
168
165
|
import { createCloudProviderClass } from "@workglow/ai/provider-utils";
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
166
|
+
|
|
167
|
+
// src/ai/common/Anthropic_CapabilitySets.ts
|
|
168
|
+
var ANTHROPIC_TEXT_GENERATION = ["text.generation"];
|
|
169
|
+
var ANTHROPIC_TOOL_USE = ["text.generation", "tool-use"];
|
|
170
|
+
var ANTHROPIC_JSON_MODE = ["text.generation", "json-mode"];
|
|
171
|
+
var ANTHROPIC_TEXT_REWRITER = ["text.rewriter"];
|
|
172
|
+
var ANTHROPIC_TEXT_SUMMARY = ["text.summary"];
|
|
173
|
+
var ANTHROPIC_COUNT_TOKENS = ["model.count-tokens"];
|
|
174
|
+
var ANTHROPIC_MODEL_SEARCH = ["model.search"];
|
|
175
|
+
var ANTHROPIC_MODEL_INFO = ["model.info"];
|
|
176
|
+
var ANTHROPIC_CAPABILITY_SETS = [
|
|
177
|
+
ANTHROPIC_TEXT_GENERATION,
|
|
178
|
+
ANTHROPIC_TOOL_USE,
|
|
179
|
+
ANTHROPIC_JSON_MODE,
|
|
180
|
+
ANTHROPIC_TEXT_REWRITER,
|
|
181
|
+
ANTHROPIC_TEXT_SUMMARY,
|
|
182
|
+
ANTHROPIC_COUNT_TOKENS,
|
|
183
|
+
ANTHROPIC_MODEL_SEARCH,
|
|
184
|
+
ANTHROPIC_MODEL_INFO
|
|
178
185
|
];
|
|
179
186
|
|
|
187
|
+
// src/ai/common/Anthropic_Capabilities.ts
|
|
188
|
+
var ANTHROPIC_RUN_FN_SPECS = ANTHROPIC_CAPABILITY_SETS.map((serves) => ({ serves }));
|
|
189
|
+
function anthropicWorkerRunFnSpecs() {
|
|
190
|
+
return ANTHROPIC_RUN_FN_SPECS;
|
|
191
|
+
}
|
|
192
|
+
function inferAnthropicCapabilities(model) {
|
|
193
|
+
const id = String(model.model_id ?? model.provider_config?.model_name ?? "");
|
|
194
|
+
if (/^claude-3[.-][57]-/i.test(id)) {
|
|
195
|
+
return [
|
|
196
|
+
"text.generation",
|
|
197
|
+
"text.rewriter",
|
|
198
|
+
"text.summary",
|
|
199
|
+
"tool-use",
|
|
200
|
+
"json-mode",
|
|
201
|
+
"vision-input",
|
|
202
|
+
"model.count-tokens",
|
|
203
|
+
"model.info",
|
|
204
|
+
"model.search"
|
|
205
|
+
];
|
|
206
|
+
}
|
|
207
|
+
if (/^claude-(sonnet|opus|haiku)-4/i.test(id)) {
|
|
208
|
+
return [
|
|
209
|
+
"text.generation",
|
|
210
|
+
"text.rewriter",
|
|
211
|
+
"text.summary",
|
|
212
|
+
"tool-use",
|
|
213
|
+
"json-mode",
|
|
214
|
+
"vision-input",
|
|
215
|
+
"model.count-tokens",
|
|
216
|
+
"model.info",
|
|
217
|
+
"model.search"
|
|
218
|
+
];
|
|
219
|
+
}
|
|
220
|
+
if (/^claude-3[.-](haiku|opus|sonnet)/i.test(id)) {
|
|
221
|
+
return [
|
|
222
|
+
"text.generation",
|
|
223
|
+
"text.rewriter",
|
|
224
|
+
"text.summary",
|
|
225
|
+
"tool-use",
|
|
226
|
+
"json-mode",
|
|
227
|
+
"vision-input",
|
|
228
|
+
"model.count-tokens",
|
|
229
|
+
"model.info",
|
|
230
|
+
"model.search"
|
|
231
|
+
];
|
|
232
|
+
}
|
|
233
|
+
if (/^claude-2/i.test(id)) {
|
|
234
|
+
return [
|
|
235
|
+
"text.generation",
|
|
236
|
+
"text.rewriter",
|
|
237
|
+
"text.summary",
|
|
238
|
+
"tool-use",
|
|
239
|
+
"json-mode",
|
|
240
|
+
"model.count-tokens",
|
|
241
|
+
"model.info",
|
|
242
|
+
"model.search"
|
|
243
|
+
];
|
|
244
|
+
}
|
|
245
|
+
const declared = model.capabilities ?? [];
|
|
246
|
+
if (declared.length > 0)
|
|
247
|
+
return declared;
|
|
248
|
+
return ["model.search", "model.info"];
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// src/ai/AnthropicQueuedProvider.ts
|
|
180
252
|
class AnthropicQueuedProvider extends createCloudProviderClass(AiProvider, {
|
|
181
253
|
name: ANTHROPIC,
|
|
182
|
-
displayName: "Anthropic"
|
|
183
|
-
taskTypes: ANTHROPIC_TASK_TYPES
|
|
254
|
+
displayName: "Anthropic"
|
|
184
255
|
}) {
|
|
256
|
+
inferCapabilities(model) {
|
|
257
|
+
return inferAnthropicCapabilities(model);
|
|
258
|
+
}
|
|
259
|
+
workerRunFnSpecs() {
|
|
260
|
+
return anthropicWorkerRunFnSpecs();
|
|
261
|
+
}
|
|
185
262
|
}
|
|
186
263
|
|
|
187
264
|
// src/ai/registerAnthropic.ts
|
|
188
265
|
async function registerAnthropic(options) {
|
|
189
266
|
await registerProviderWithWorker(new AnthropicQueuedProvider, "Anthropic", options);
|
|
190
267
|
}
|
|
268
|
+
// src/ai/common/Anthropic_CountTokens.ts
|
|
269
|
+
var Anthropic_CountTokens_Stream = async (input, model, signal, emit) => {
|
|
270
|
+
const client = await getClient(model);
|
|
271
|
+
const result = await client.messages.countTokens({
|
|
272
|
+
model: getModelName(model),
|
|
273
|
+
messages: [{ role: "user", content: input.text }]
|
|
274
|
+
});
|
|
275
|
+
emit({ type: "finish", data: { count: result.input_tokens } });
|
|
276
|
+
};
|
|
277
|
+
var Anthropic_CountTokens_Preview = async (input, _model) => {
|
|
278
|
+
return { count: Math.ceil(input.text.length / 4) };
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
// src/ai/common/Anthropic_ModelInfo.ts
|
|
282
|
+
var Anthropic_ModelInfo_Stream = async (input, _model, _signal, emit) => {
|
|
283
|
+
const result = {
|
|
284
|
+
model: input.model,
|
|
285
|
+
is_local: false,
|
|
286
|
+
is_remote: true,
|
|
287
|
+
supports_browser: true,
|
|
288
|
+
supports_node: true,
|
|
289
|
+
is_cached: false,
|
|
290
|
+
is_loaded: false,
|
|
291
|
+
file_sizes: null
|
|
292
|
+
};
|
|
293
|
+
emit({ type: "finish", data: result });
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
// src/ai/common/Anthropic_StructuredGeneration.ts
|
|
297
|
+
import { parsePartialJson } from "@workglow/util/worker";
|
|
298
|
+
var Anthropic_StructuredGeneration_Stream = async (input, model, signal, emit, outputSchema) => {
|
|
299
|
+
const client = await getClient(model);
|
|
300
|
+
const modelName = getModelName(model);
|
|
301
|
+
const schema = input.outputSchema ?? outputSchema;
|
|
302
|
+
const stream = client.messages.stream({
|
|
303
|
+
model: modelName,
|
|
304
|
+
messages: [{ role: "user", content: input.prompt }],
|
|
305
|
+
tools: [
|
|
306
|
+
{
|
|
307
|
+
name: "structured_output",
|
|
308
|
+
description: "Output structured data conforming to the schema",
|
|
309
|
+
input_schema: schema
|
|
310
|
+
}
|
|
311
|
+
],
|
|
312
|
+
tool_choice: { type: "tool", name: "structured_output" },
|
|
313
|
+
max_tokens: getMaxTokens(input, model)
|
|
314
|
+
}, { signal });
|
|
315
|
+
let accumulatedJson = "";
|
|
316
|
+
for await (const event of stream) {
|
|
317
|
+
if (event.type === "content_block_delta" && event.delta.type === "input_json_delta") {
|
|
318
|
+
accumulatedJson += event.delta.partial_json;
|
|
319
|
+
const partial = parsePartialJson(accumulatedJson);
|
|
320
|
+
if (partial !== undefined) {
|
|
321
|
+
emit({ type: "object-delta", port: "object", objectDelta: partial });
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
let finalObject;
|
|
326
|
+
try {
|
|
327
|
+
finalObject = JSON.parse(accumulatedJson);
|
|
328
|
+
} catch {
|
|
329
|
+
finalObject = parsePartialJson(accumulatedJson) ?? {};
|
|
330
|
+
}
|
|
331
|
+
emit({ type: "finish", data: { object: finalObject } });
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
// src/ai/common/Anthropic_TextGeneration.ts
|
|
335
|
+
import { getLogger } from "@workglow/util/worker";
|
|
336
|
+
|
|
337
|
+
// src/ai/common/Anthropic_ToolCalling.ts
|
|
338
|
+
import { buildToolDescription, filterValidToolCalls } from "@workglow/ai/worker";
|
|
339
|
+
import { parsePartialJson as parsePartialJson2 } from "@workglow/util/worker";
|
|
340
|
+
function buildAnthropicMessages(messages, prompt) {
|
|
341
|
+
if (!messages || messages.length === 0) {
|
|
342
|
+
return [{ role: "user", content: prompt }];
|
|
343
|
+
}
|
|
344
|
+
const out = [];
|
|
345
|
+
for (const msg of messages) {
|
|
346
|
+
if (msg.role === "user") {
|
|
347
|
+
const blocks = msg.content.map((b) => {
|
|
348
|
+
if (b.type === "text")
|
|
349
|
+
return { type: "text", text: b.text };
|
|
350
|
+
if (b.type === "image") {
|
|
351
|
+
return {
|
|
352
|
+
type: "image",
|
|
353
|
+
source: { type: "base64", media_type: b.mimeType, data: b.data }
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
return b;
|
|
357
|
+
});
|
|
358
|
+
out.push({ role: "user", content: blocks });
|
|
359
|
+
} else if (msg.role === "assistant") {
|
|
360
|
+
const blocks = msg.content.map((b) => {
|
|
361
|
+
if (b.type === "text")
|
|
362
|
+
return { type: "text", text: b.text };
|
|
363
|
+
if (b.type === "tool_use") {
|
|
364
|
+
return { type: "tool_use", id: b.id, name: b.name, input: b.input };
|
|
365
|
+
}
|
|
366
|
+
return b;
|
|
367
|
+
});
|
|
368
|
+
out.push({ role: "assistant", content: blocks });
|
|
369
|
+
} else if (msg.role === "tool") {
|
|
370
|
+
const blocks = msg.content.filter((b) => b.type === "tool_result").map((b) => {
|
|
371
|
+
const content = b.content.map((inner) => {
|
|
372
|
+
if (inner.type === "text")
|
|
373
|
+
return { type: "text", text: inner.text };
|
|
374
|
+
if (inner.type === "image") {
|
|
375
|
+
return {
|
|
376
|
+
type: "image",
|
|
377
|
+
source: { type: "base64", media_type: inner.mimeType, data: inner.data }
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
return inner;
|
|
381
|
+
});
|
|
382
|
+
return {
|
|
383
|
+
type: "tool_result",
|
|
384
|
+
tool_use_id: b.tool_use_id,
|
|
385
|
+
content,
|
|
386
|
+
...b.is_error ? { is_error: true } : {}
|
|
387
|
+
};
|
|
388
|
+
});
|
|
389
|
+
out.push({ role: "user", content: blocks });
|
|
390
|
+
} else if (msg.role === "system") {
|
|
391
|
+
continue;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return out;
|
|
395
|
+
}
|
|
396
|
+
function mapAnthropicToolChoice(toolChoice) {
|
|
397
|
+
if (!toolChoice || toolChoice === "auto")
|
|
398
|
+
return { type: "auto" };
|
|
399
|
+
if (toolChoice === "none")
|
|
400
|
+
return;
|
|
401
|
+
if (toolChoice === "required")
|
|
402
|
+
return { type: "any" };
|
|
403
|
+
return { type: "tool", name: toolChoice };
|
|
404
|
+
}
|
|
405
|
+
var Anthropic_ToolCalling_Stream = async (input, model, signal, emit, _outputSchema, sessionId) => {
|
|
406
|
+
const client = await getClient(model);
|
|
407
|
+
const modelName = getModelName(model);
|
|
408
|
+
const tools = input.tools.map((t) => ({
|
|
409
|
+
name: t.name,
|
|
410
|
+
description: buildToolDescription(t),
|
|
411
|
+
input_schema: t.inputSchema
|
|
412
|
+
}));
|
|
413
|
+
const toolChoice = mapAnthropicToolChoice(input.toolChoice);
|
|
414
|
+
const messages = buildAnthropicMessages(input.messages, input.prompt);
|
|
415
|
+
const params = {
|
|
416
|
+
model: modelName,
|
|
417
|
+
messages,
|
|
418
|
+
max_tokens: getMaxTokens(input, model),
|
|
419
|
+
temperature: input.temperature
|
|
420
|
+
};
|
|
421
|
+
if (input.systemPrompt) {
|
|
422
|
+
params.system = input.systemPrompt;
|
|
423
|
+
}
|
|
424
|
+
if (toolChoice !== undefined) {
|
|
425
|
+
params.tools = tools;
|
|
426
|
+
params.tool_choice = toolChoice;
|
|
427
|
+
}
|
|
428
|
+
if (sessionId) {
|
|
429
|
+
if (params.system) {
|
|
430
|
+
params.system = [
|
|
431
|
+
{
|
|
432
|
+
type: "text",
|
|
433
|
+
text: params.system,
|
|
434
|
+
cache_control: { type: "ephemeral" }
|
|
435
|
+
}
|
|
436
|
+
];
|
|
437
|
+
}
|
|
438
|
+
if (params.tools && params.tools.length > 0) {
|
|
439
|
+
const lastIdx = params.tools.length - 1;
|
|
440
|
+
params.tools[lastIdx] = {
|
|
441
|
+
...params.tools[lastIdx],
|
|
442
|
+
cache_control: { type: "ephemeral" }
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
const stream = client.messages.stream(params, { signal });
|
|
447
|
+
const blockMeta = new Map;
|
|
448
|
+
const toolCallsByBlockIndex = new Map;
|
|
449
|
+
const toolCallsInStreamOrder = () => [...toolCallsByBlockIndex.entries()].sort((a, b) => a[0] - b[0]).map(([, tc]) => tc);
|
|
450
|
+
const validatedToolCallsInStreamOrder = () => filterValidToolCalls(toolCallsInStreamOrder(), input.tools);
|
|
451
|
+
for await (const event of stream) {
|
|
452
|
+
if (event.type === "content_block_start") {
|
|
453
|
+
const block = event.content_block;
|
|
454
|
+
const index = event.index;
|
|
455
|
+
if (block.type === "tool_use") {
|
|
456
|
+
blockMeta.set(index, {
|
|
457
|
+
type: "tool_use",
|
|
458
|
+
id: block.id,
|
|
459
|
+
name: block.name,
|
|
460
|
+
json: ""
|
|
461
|
+
});
|
|
462
|
+
} else if (block.type === "text") {
|
|
463
|
+
blockMeta.set(index, { type: "text", json: "" });
|
|
464
|
+
}
|
|
465
|
+
} else if (event.type === "content_block_delta") {
|
|
466
|
+
const index = event.index;
|
|
467
|
+
const delta = event.delta;
|
|
468
|
+
if (delta.type === "text_delta") {
|
|
469
|
+
emit({ type: "text-delta", port: "text", textDelta: delta.text });
|
|
470
|
+
} else if (delta.type === "input_json_delta") {
|
|
471
|
+
const meta = blockMeta.get(index);
|
|
472
|
+
if (meta) {
|
|
473
|
+
meta.json += delta.partial_json;
|
|
474
|
+
let parsedInput;
|
|
475
|
+
try {
|
|
476
|
+
parsedInput = JSON.parse(meta.json);
|
|
477
|
+
} catch {
|
|
478
|
+
const partial = parsePartialJson2(meta.json);
|
|
479
|
+
parsedInput = partial ?? {};
|
|
480
|
+
}
|
|
481
|
+
toolCallsByBlockIndex.set(index, {
|
|
482
|
+
id: meta.id ?? "",
|
|
483
|
+
name: meta.name ?? "",
|
|
484
|
+
input: parsedInput
|
|
485
|
+
});
|
|
486
|
+
emit({
|
|
487
|
+
type: "object-delta",
|
|
488
|
+
port: "toolCalls",
|
|
489
|
+
objectDelta: validatedToolCallsInStreamOrder()
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
} else if (event.type === "content_block_stop") {
|
|
494
|
+
const index = event.index;
|
|
495
|
+
const meta = blockMeta.get(index);
|
|
496
|
+
if (meta?.type === "tool_use") {
|
|
497
|
+
let finalInput;
|
|
498
|
+
try {
|
|
499
|
+
finalInput = JSON.parse(meta.json);
|
|
500
|
+
} catch {
|
|
501
|
+
finalInput = parsePartialJson2(meta.json) ?? {};
|
|
502
|
+
}
|
|
503
|
+
const id = meta.id ?? "";
|
|
504
|
+
toolCallsByBlockIndex.set(index, { id, name: meta.name ?? "", input: finalInput });
|
|
505
|
+
emit({
|
|
506
|
+
type: "object-delta",
|
|
507
|
+
port: "toolCalls",
|
|
508
|
+
objectDelta: validatedToolCallsInStreamOrder()
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
blockMeta.delete(index);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
emit({ type: "finish", data: { text: "", toolCalls: [] } });
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
// src/ai/common/Anthropic_TextGeneration.ts
|
|
518
|
+
var Anthropic_TextGeneration_Stream = async (input, model, signal, emit, _outputSchema, sessionId) => {
|
|
519
|
+
const logger = getLogger();
|
|
520
|
+
const timerLabel = `anthropic:TextGeneration:${getModelName(model)}`;
|
|
521
|
+
logger.time(timerLabel, { model: getModelName(model) });
|
|
522
|
+
try {
|
|
523
|
+
const client = await getClient(model);
|
|
524
|
+
const modelName = getModelName(model);
|
|
525
|
+
const unified = input;
|
|
526
|
+
const hasMessages = Array.isArray(unified.messages) && unified.messages.length > 0;
|
|
527
|
+
const messages = hasMessages ? buildAnthropicMessages(unified.messages, unified.prompt ?? "") : [{ role: "user", content: input.prompt }];
|
|
528
|
+
const params = {
|
|
529
|
+
model: modelName,
|
|
530
|
+
messages,
|
|
531
|
+
max_tokens: getMaxTokens(input, model)
|
|
532
|
+
};
|
|
533
|
+
if (input.temperature !== undefined)
|
|
534
|
+
params.temperature = input.temperature;
|
|
535
|
+
if (input.topP !== undefined)
|
|
536
|
+
params.top_p = input.topP;
|
|
537
|
+
if (unified.systemPrompt) {
|
|
538
|
+
params.system = sessionId ? [
|
|
539
|
+
{
|
|
540
|
+
type: "text",
|
|
541
|
+
text: unified.systemPrompt,
|
|
542
|
+
cache_control: { type: "ephemeral" }
|
|
543
|
+
}
|
|
544
|
+
] : unified.systemPrompt;
|
|
545
|
+
}
|
|
546
|
+
if (sessionId && hasMessages && Array.isArray(messages) && messages.length > 0) {
|
|
547
|
+
const last = messages[messages.length - 1];
|
|
548
|
+
if (Array.isArray(last.content) && last.content.length > 0) {
|
|
549
|
+
const blocks = last.content;
|
|
550
|
+
blocks[blocks.length - 1] = {
|
|
551
|
+
...blocks[blocks.length - 1],
|
|
552
|
+
cache_control: { type: "ephemeral" }
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
const stream = client.messages.stream(params, { signal });
|
|
557
|
+
for await (const event of stream) {
|
|
558
|
+
const e = event;
|
|
559
|
+
if (e.type === "content_block_delta" && e.delta?.type === "text_delta") {
|
|
560
|
+
emit({ type: "text-delta", port: "text", textDelta: e.delta.text ?? "" });
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
emit({ type: "finish", data: {} });
|
|
564
|
+
} finally {
|
|
565
|
+
logger.timeEnd(timerLabel, { model: getModelName(model) });
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
// src/ai/common/Anthropic_TextRewriter.ts
|
|
570
|
+
var Anthropic_TextRewriter_Stream = async (input, model, signal, emit) => {
|
|
571
|
+
const client = await getClient(model);
|
|
572
|
+
const modelName = getModelName(model);
|
|
573
|
+
const stream = client.messages.stream({
|
|
574
|
+
model: modelName,
|
|
575
|
+
system: input.prompt,
|
|
576
|
+
messages: [{ role: "user", content: input.text }],
|
|
577
|
+
max_tokens: getMaxTokens({}, model)
|
|
578
|
+
}, { signal });
|
|
579
|
+
for await (const event of stream) {
|
|
580
|
+
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
|
|
581
|
+
emit({ type: "text-delta", port: "text", textDelta: event.delta.text });
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
emit({ type: "finish", data: {} });
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
// src/ai/common/Anthropic_TextSummary.ts
|
|
588
|
+
var Anthropic_TextSummary_Stream = async (input, model, signal, emit) => {
|
|
589
|
+
const client = await getClient(model);
|
|
590
|
+
const modelName = getModelName(model);
|
|
591
|
+
const stream = client.messages.stream({
|
|
592
|
+
model: modelName,
|
|
593
|
+
system: "Summarize the following text concisely.",
|
|
594
|
+
messages: [{ role: "user", content: input.text }],
|
|
595
|
+
max_tokens: getMaxTokens({}, model)
|
|
596
|
+
}, { signal });
|
|
597
|
+
for await (const event of stream) {
|
|
598
|
+
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
|
|
599
|
+
emit({ type: "text-delta", port: "text", textDelta: event.delta.text });
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
emit({ type: "finish", data: {} });
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
// src/ai/common/Anthropic_JobRunFns.ts
|
|
606
|
+
var ANTHROPIC_RUN_FNS = [
|
|
607
|
+
{ serves: ANTHROPIC_TEXT_GENERATION, runFn: Anthropic_TextGeneration_Stream },
|
|
608
|
+
{ serves: ANTHROPIC_TOOL_USE, runFn: Anthropic_ToolCalling_Stream },
|
|
609
|
+
{ serves: ANTHROPIC_JSON_MODE, runFn: Anthropic_StructuredGeneration_Stream },
|
|
610
|
+
{ serves: ANTHROPIC_TEXT_REWRITER, runFn: Anthropic_TextRewriter_Stream },
|
|
611
|
+
{ serves: ANTHROPIC_TEXT_SUMMARY, runFn: Anthropic_TextSummary_Stream },
|
|
612
|
+
{ serves: ANTHROPIC_COUNT_TOKENS, runFn: Anthropic_CountTokens_Stream },
|
|
613
|
+
{ serves: ANTHROPIC_MODEL_SEARCH, runFn: Anthropic_ModelSearch_Stream },
|
|
614
|
+
{ serves: ANTHROPIC_MODEL_INFO, runFn: Anthropic_ModelInfo_Stream }
|
|
615
|
+
];
|
|
616
|
+
var ANTHROPIC_PREVIEW_TASKS = {
|
|
617
|
+
CountTokensTask: Anthropic_CountTokens_Preview
|
|
618
|
+
};
|
|
619
|
+
|
|
620
|
+
// src/ai/index.ts
|
|
621
|
+
var _testOnly = {
|
|
622
|
+
AnthropicQueuedProvider,
|
|
623
|
+
ANTHROPIC_RUN_FN_SPECS,
|
|
624
|
+
ANTHROPIC_RUN_FNS
|
|
625
|
+
};
|
|
191
626
|
export {
|
|
192
627
|
registerAnthropic,
|
|
193
|
-
|
|
628
|
+
_testOnly,
|
|
629
|
+
Anthropic_ModelSearch_Stream,
|
|
194
630
|
AnthropicModelSchema,
|
|
195
631
|
AnthropicModelRecordSchema,
|
|
196
632
|
AnthropicModelConfigSchema,
|
|
197
633
|
ANTHROPIC
|
|
198
634
|
};
|
|
199
635
|
|
|
200
|
-
//# debugId=
|
|
636
|
+
//# debugId=4152D03071B4556864756E2164756E21
|