@anvia/core 0.12.2 → 0.12.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -2
- package/dist/agent/index.js +3 -3
- package/dist/{chunk-4NCRFQHS.js → chunk-C7JLBS3F.js} +169 -83
- package/dist/chunk-C7JLBS3F.js.map +1 -0
- package/dist/{chunk-XUO3KR5T.js → chunk-OODGSEUU.js} +2 -2
- package/dist/{chunk-XUO3KR5T.js.map → chunk-OODGSEUU.js.map} +1 -1
- package/dist/{chunk-TUDZBJGF.js → chunk-OQXRM5DO.js} +7 -7
- package/dist/chunk-OQXRM5DO.js.map +1 -0
- package/dist/{chunk-QNVLGAPV.js → chunk-ZHIZZWBJ.js} +3 -3
- package/dist/evals/index.js +11 -5
- package/dist/evals/index.js.map +1 -1
- package/dist/extractor/index.js +4 -4
- package/dist/index.js +3 -3
- package/dist/internal/agent.js +2 -2
- package/dist/mcp/index.js +13 -1
- package/dist/mcp/index.js.map +1 -1
- package/dist/request/index.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-4NCRFQHS.js.map +0 -1
- package/dist/chunk-TUDZBJGF.js.map +0 -1
- /package/dist/{chunk-QNVLGAPV.js.map → chunk-ZHIZZWBJ.js.map} +0 -0
package/README.md
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
# @anvia/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Small, explicit, embeddable runtime contracts for Anvia agents, tools, structured extraction, pipelines, streaming, RAG, MCP, skills, and observability.
|
|
4
4
|
|
|
5
|
-
This package is provider-neutral. Pair it with a provider adapter such as `@anvia/openai`, `@anvia/anthropic`, or `@anvia/gemini` to create runnable
|
|
5
|
+
This package is provider-neutral. Pair it with a provider adapter such as `@anvia/openai`, `@anvia/anthropic`, or `@anvia/gemini` to create runnable model objects, then pass those objects into agents, extractors, pipelines, or direct completion helpers.
|
|
6
|
+
|
|
7
|
+
## Design Philosophy
|
|
8
|
+
|
|
9
|
+
`@anvia/core` owns the model/tool loop and the runtime contracts around it. Your application owns provider client construction, credentials, product data access, permissions, storage, deployment, observability backends, and response shape.
|
|
10
|
+
|
|
11
|
+
The package is dependency-injection oriented: create provider models, typed tools, memory stores, vector indexes, observers, and services in application code, then pass the relevant objects into agents, prompt requests, runners, or adapters. Core receives those objects and coordinates the run without taking over product architecture.
|
|
6
12
|
|
|
7
13
|
## Installation
|
|
8
14
|
|
package/dist/agent/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AgentBuilder
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
5
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-OODGSEUU.js";
|
|
4
|
+
import "../chunk-OQXRM5DO.js";
|
|
5
|
+
import "../chunk-C7JLBS3F.js";
|
|
6
6
|
import "../chunk-YK4WAAS4.js";
|
|
7
7
|
import "../chunk-XUUY2L2D.js";
|
|
8
8
|
import "../chunk-YVQBC3SJ.js";
|
|
@@ -289,48 +289,34 @@ function formatMetadata(metadata) {
|
|
|
289
289
|
}
|
|
290
290
|
|
|
291
291
|
// src/internal/prompt-runtime/stream-accumulator.ts
|
|
292
|
-
var CompletionStreamAccumulator = class
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
292
|
+
var CompletionStreamAccumulator = class {
|
|
293
|
+
orderedParts = [];
|
|
294
|
+
textParts = /* @__PURE__ */ new Map();
|
|
295
|
+
reasoningByKey = /* @__PURE__ */ new Map();
|
|
296
|
+
reasoningKeyById = /* @__PURE__ */ new Map();
|
|
297
297
|
toolCalls = /* @__PURE__ */ new Map();
|
|
298
|
-
toolCallOrder = [];
|
|
299
298
|
finalResponse;
|
|
300
299
|
messageId;
|
|
300
|
+
nextTextKey = 0;
|
|
301
|
+
nextReasoningKey = 0;
|
|
301
302
|
accept(event) {
|
|
302
303
|
if (event.type === "text_delta") {
|
|
303
|
-
this.
|
|
304
|
+
this.appendText(event.delta);
|
|
304
305
|
return { type: "text_delta", delta: event.delta };
|
|
305
306
|
}
|
|
306
307
|
if (event.type === "reasoning_delta") {
|
|
307
|
-
const
|
|
308
|
-
const existing = this.reasoningById.get(key);
|
|
309
|
-
const reasoning = existing ?? { text: "" };
|
|
310
|
-
if (!existing) {
|
|
311
|
-
this.reasoningOrder.push(key);
|
|
312
|
-
}
|
|
308
|
+
const reasoning = this.reasoningStateForEvent(event);
|
|
313
309
|
this.appendReasoning(reasoning, event);
|
|
314
|
-
this.reasoningById.set(key, reasoning);
|
|
315
310
|
return reasoningDeltaEvent(event);
|
|
316
311
|
}
|
|
317
312
|
if (event.type === "tool_call_delta") {
|
|
318
|
-
const
|
|
319
|
-
const toolCall = existing ?? {
|
|
320
|
-
id: event.id,
|
|
321
|
-
name: "",
|
|
322
|
-
argumentsText: ""
|
|
323
|
-
};
|
|
324
|
-
if (!existing) {
|
|
325
|
-
this.toolCallOrder.push(event.id);
|
|
326
|
-
}
|
|
313
|
+
const toolCall = this.toolCallStateForId(event.id);
|
|
327
314
|
if (event.callId !== void 0) toolCall.callId = event.callId;
|
|
328
315
|
if (event.name !== void 0) toolCall.name = event.name;
|
|
329
316
|
if (event.signature !== void 0) toolCall.signature = event.signature;
|
|
330
317
|
if (event.argumentsDelta !== void 0) {
|
|
331
318
|
toolCall.argumentsText += event.argumentsDelta;
|
|
332
319
|
}
|
|
333
|
-
this.toolCalls.set(event.id, toolCall);
|
|
334
320
|
return void 0;
|
|
335
321
|
}
|
|
336
322
|
if (event.type === "tool_call") {
|
|
@@ -350,16 +336,8 @@ var CompletionStreamAccumulator = class _CompletionStreamAccumulator {
|
|
|
350
336
|
response() {
|
|
351
337
|
const accumulatedResponse = this.buildAccumulatedResponse();
|
|
352
338
|
if (this.finalResponse !== void 0) {
|
|
353
|
-
if (
|
|
354
|
-
|
|
355
|
-
...accumulatedResponse,
|
|
356
|
-
usage: this.finalResponse.usage,
|
|
357
|
-
rawResponse: this.finalResponse.rawResponse
|
|
358
|
-
};
|
|
359
|
-
if (this.finalResponse.messageId !== void 0) {
|
|
360
|
-
response.messageId = this.finalResponse.messageId;
|
|
361
|
-
}
|
|
362
|
-
return response;
|
|
339
|
+
if (accumulatedResponse.choice.length === 0) {
|
|
340
|
+
return this.withMessageIdFallback(this.finalResponse, accumulatedResponse);
|
|
363
341
|
}
|
|
364
342
|
return this.mergeFinalResponse(accumulatedResponse, this.finalResponse);
|
|
365
343
|
}
|
|
@@ -367,33 +345,24 @@ var CompletionStreamAccumulator = class _CompletionStreamAccumulator {
|
|
|
367
345
|
}
|
|
368
346
|
buildAccumulatedResponse() {
|
|
369
347
|
const choice = [];
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
const id = key === _CompletionStreamAccumulator.defaultReasoningKey ? void 0 : key;
|
|
376
|
-
const content = reasoning.content === void 0 ? { type: "reasoning", text: reasoning.text } : { type: "reasoning", text: reasoning.text, content: reasoning.content };
|
|
377
|
-
choice.push(id === void 0 ? content : { ...content, id });
|
|
378
|
-
}
|
|
379
|
-
for (const id of this.toolCallOrder) {
|
|
380
|
-
const toolCall = this.toolCalls.get(id);
|
|
381
|
-
if (toolCall !== void 0) {
|
|
382
|
-
const content = {
|
|
383
|
-
type: "tool_call",
|
|
384
|
-
id: toolCall.id,
|
|
385
|
-
function: {
|
|
386
|
-
name: toolCall.name,
|
|
387
|
-
arguments: parseJsonValue(toolCall.argumentsText)
|
|
388
|
-
}
|
|
389
|
-
};
|
|
390
|
-
if (toolCall.callId !== void 0) {
|
|
391
|
-
content.callId = toolCall.callId;
|
|
348
|
+
for (const part of this.orderedParts) {
|
|
349
|
+
if (part.type === "text") {
|
|
350
|
+
const text = this.textParts.get(part.key) ?? "";
|
|
351
|
+
if (text.length > 0) {
|
|
352
|
+
choice.push({ type: "text", text });
|
|
392
353
|
}
|
|
393
|
-
|
|
394
|
-
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
if (part.type === "reasoning") {
|
|
357
|
+
const reasoning = this.reasoningByKey.get(part.key);
|
|
358
|
+
if (reasoning !== void 0) {
|
|
359
|
+
choice.push(reasoningContent(reasoning));
|
|
395
360
|
}
|
|
396
|
-
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
const toolCall = this.toolCalls.get(part.key);
|
|
364
|
+
if (toolCall !== void 0) {
|
|
365
|
+
choice.push(toolCallContent(toolCall));
|
|
397
366
|
}
|
|
398
367
|
}
|
|
399
368
|
const response = {
|
|
@@ -408,7 +377,7 @@ var CompletionStreamAccumulator = class _CompletionStreamAccumulator {
|
|
|
408
377
|
}
|
|
409
378
|
upsertToolCall(toolCall) {
|
|
410
379
|
if (!this.toolCalls.has(toolCall.id)) {
|
|
411
|
-
this.
|
|
380
|
+
this.orderedParts.push({ type: "tool_call", key: toolCall.id });
|
|
412
381
|
}
|
|
413
382
|
const partial = {
|
|
414
383
|
id: toolCall.id,
|
|
@@ -421,39 +390,120 @@ var CompletionStreamAccumulator = class _CompletionStreamAccumulator {
|
|
|
421
390
|
if (toolCall.signature !== void 0) {
|
|
422
391
|
partial.signature = toolCall.signature;
|
|
423
392
|
}
|
|
393
|
+
if (toolCall.additionalParams !== void 0) {
|
|
394
|
+
partial.additionalParams = toolCall.additionalParams;
|
|
395
|
+
}
|
|
424
396
|
this.toolCalls.set(toolCall.id, partial);
|
|
425
397
|
}
|
|
426
398
|
mergeFinalResponse(accumulatedResponse, finalResponse) {
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
399
|
+
if (finalResponse.choice.length === 0) {
|
|
400
|
+
return this.withMessageIdFallback(
|
|
401
|
+
{
|
|
402
|
+
...accumulatedResponse,
|
|
403
|
+
usage: finalResponse.usage,
|
|
404
|
+
rawResponse: finalResponse.rawResponse,
|
|
405
|
+
...finalResponse.messageId === void 0 ? {} : { messageId: finalResponse.messageId }
|
|
406
|
+
},
|
|
407
|
+
accumulatedResponse
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
const finalById = /* @__PURE__ */ new Map();
|
|
411
|
+
const finalByCallId = /* @__PURE__ */ new Map();
|
|
412
|
+
for (const content of finalResponse.choice) {
|
|
430
413
|
if (content.type !== "tool_call") {
|
|
431
414
|
continue;
|
|
432
415
|
}
|
|
433
|
-
|
|
416
|
+
finalById.set(content.id, content);
|
|
434
417
|
if (content.callId !== void 0) {
|
|
435
|
-
|
|
418
|
+
finalByCallId.set(content.callId, content);
|
|
436
419
|
}
|
|
437
420
|
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
421
|
+
const matchedFinalToolCalls = /* @__PURE__ */ new Set();
|
|
422
|
+
const choice = accumulatedResponse.choice.map((content) => {
|
|
423
|
+
if (content.type !== "tool_call") {
|
|
424
|
+
return content;
|
|
425
|
+
}
|
|
426
|
+
const finalToolCall = finalById.get(content.id) ?? (content.callId === void 0 ? void 0 : finalByCallId.get(content.callId));
|
|
427
|
+
if (finalToolCall === void 0) {
|
|
428
|
+
return content;
|
|
429
|
+
}
|
|
430
|
+
matchedFinalToolCalls.add(finalToolCall);
|
|
431
|
+
return mergeFinalToolCall(content, finalToolCall);
|
|
432
|
+
});
|
|
433
|
+
for (const content of finalResponse.choice) {
|
|
434
|
+
if (content.type !== "tool_call") {
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
if (!matchedFinalToolCalls.has(content)) {
|
|
438
|
+
choice.push(content);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
return this.withMessageIdFallback({ ...finalResponse, choice }, accumulatedResponse);
|
|
442
|
+
}
|
|
443
|
+
appendText(delta) {
|
|
444
|
+
const lastPart = this.orderedParts.at(-1);
|
|
445
|
+
const key = lastPart?.type === "text" ? lastPart.key : this.createTextKey();
|
|
446
|
+
if (lastPart?.type !== "text") {
|
|
447
|
+
this.orderedParts.push({ type: "text", key });
|
|
448
|
+
}
|
|
449
|
+
this.textParts.set(key, `${this.textParts.get(key) ?? ""}${delta}`);
|
|
450
|
+
}
|
|
451
|
+
reasoningStateForEvent(event) {
|
|
452
|
+
if (event.id !== void 0) {
|
|
453
|
+
const existingKey = this.reasoningKeyById.get(event.id);
|
|
454
|
+
if (existingKey !== void 0) {
|
|
455
|
+
const existing = this.reasoningByKey.get(existingKey);
|
|
456
|
+
if (existing !== void 0) {
|
|
457
|
+
return existing;
|
|
447
458
|
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
459
|
+
}
|
|
460
|
+
const key2 = this.createReasoningKey();
|
|
461
|
+
const reasoning2 = { id: event.id, text: "" };
|
|
462
|
+
this.reasoningKeyById.set(event.id, key2);
|
|
463
|
+
this.reasoningByKey.set(key2, reasoning2);
|
|
464
|
+
this.orderedParts.push({ type: "reasoning", key: key2 });
|
|
465
|
+
return reasoning2;
|
|
466
|
+
}
|
|
467
|
+
const lastPart = this.orderedParts.at(-1);
|
|
468
|
+
if (lastPart?.type === "reasoning") {
|
|
469
|
+
const lastReasoning = this.reasoningByKey.get(lastPart.key);
|
|
470
|
+
if (lastReasoning !== void 0 && lastReasoning.id === void 0) {
|
|
471
|
+
return lastReasoning;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
const key = this.createReasoningKey();
|
|
475
|
+
const reasoning = { text: "" };
|
|
476
|
+
this.reasoningByKey.set(key, reasoning);
|
|
477
|
+
this.orderedParts.push({ type: "reasoning", key });
|
|
478
|
+
return reasoning;
|
|
479
|
+
}
|
|
480
|
+
toolCallStateForId(id) {
|
|
481
|
+
const existing = this.toolCalls.get(id);
|
|
482
|
+
if (existing !== void 0) {
|
|
483
|
+
return existing;
|
|
484
|
+
}
|
|
485
|
+
const toolCall = {
|
|
486
|
+
id,
|
|
487
|
+
name: "",
|
|
488
|
+
argumentsText: ""
|
|
456
489
|
};
|
|
490
|
+
this.toolCalls.set(id, toolCall);
|
|
491
|
+
this.orderedParts.push({ type: "tool_call", key: id });
|
|
492
|
+
return toolCall;
|
|
493
|
+
}
|
|
494
|
+
withMessageIdFallback(response, accumulatedResponse) {
|
|
495
|
+
if (response.messageId !== void 0 || accumulatedResponse.messageId === void 0) {
|
|
496
|
+
return response;
|
|
497
|
+
}
|
|
498
|
+
return { ...response, messageId: accumulatedResponse.messageId };
|
|
499
|
+
}
|
|
500
|
+
createTextKey() {
|
|
501
|
+
this.nextTextKey += 1;
|
|
502
|
+
return `text_${this.nextTextKey.toString()}`;
|
|
503
|
+
}
|
|
504
|
+
createReasoningKey() {
|
|
505
|
+
this.nextReasoningKey += 1;
|
|
506
|
+
return `reasoning_${this.nextReasoningKey.toString()}`;
|
|
457
507
|
}
|
|
458
508
|
appendReasoning(reasoning, event) {
|
|
459
509
|
const contentType = event.contentType ?? "text";
|
|
@@ -493,6 +543,42 @@ var CompletionStreamAccumulator = class _CompletionStreamAccumulator {
|
|
|
493
543
|
reasoning.content.push({ type: "redacted", data: event.delta });
|
|
494
544
|
}
|
|
495
545
|
};
|
|
546
|
+
function reasoningContent(reasoning) {
|
|
547
|
+
const content = reasoning.content === void 0 ? { type: "reasoning", text: reasoning.text } : { type: "reasoning", text: reasoning.text, content: reasoning.content };
|
|
548
|
+
return reasoning.id === void 0 ? content : { ...content, id: reasoning.id };
|
|
549
|
+
}
|
|
550
|
+
function toolCallContent(toolCall) {
|
|
551
|
+
const content = {
|
|
552
|
+
type: "tool_call",
|
|
553
|
+
id: toolCall.id,
|
|
554
|
+
function: {
|
|
555
|
+
name: toolCall.name,
|
|
556
|
+
arguments: parseJsonValue(toolCall.argumentsText)
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
if (toolCall.callId !== void 0) {
|
|
560
|
+
content.callId = toolCall.callId;
|
|
561
|
+
}
|
|
562
|
+
if (toolCall.signature !== void 0) {
|
|
563
|
+
content.signature = toolCall.signature;
|
|
564
|
+
}
|
|
565
|
+
if (toolCall.additionalParams !== void 0) {
|
|
566
|
+
content.additionalParams = toolCall.additionalParams;
|
|
567
|
+
}
|
|
568
|
+
return content;
|
|
569
|
+
}
|
|
570
|
+
function mergeFinalToolCall(accumulated, finalToolCall) {
|
|
571
|
+
const argumentsValue = isEmptyToolArguments(finalToolCall.function.arguments) ? accumulated.function.arguments : finalToolCall.function.arguments;
|
|
572
|
+
return {
|
|
573
|
+
...accumulated,
|
|
574
|
+
...finalToolCall,
|
|
575
|
+
function: {
|
|
576
|
+
...accumulated.function,
|
|
577
|
+
...finalToolCall.function,
|
|
578
|
+
arguments: argumentsValue
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
}
|
|
496
582
|
function reasoningDeltaEvent(event) {
|
|
497
583
|
const mapped = { type: "reasoning_delta", delta: event.delta };
|
|
498
584
|
if (event.id !== void 0) mapped.id = event.id;
|
|
@@ -2014,4 +2100,4 @@ export {
|
|
|
2014
2100
|
extractRagText,
|
|
2015
2101
|
PromptRequest
|
|
2016
2102
|
};
|
|
2017
|
-
//# sourceMappingURL=chunk-
|
|
2103
|
+
//# sourceMappingURL=chunk-C7JLBS3F.js.map
|