@veryfront/ext-llm-anthropic 0.1.1049 → 0.1.1051
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic-provider.d.ts","sourceRoot":"","sources":["../src/anthropic-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAE/E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EACL,kBAAkB,EAIlB,aAAa,EACb,UAAU,EACV,iBAAiB,EACjB,aAAa,EACb,uBAAuB,EACvB,kBAAkB,EAClB,sBAAsB,EACtB,oBAAoB,EAKpB,+BAA+B,EAC/B,8BAA8B,EAC/B,MAAM,2BAA2B,CAAC;AAWnC,OAAO,EACL,kBAAkB,EAClB,aAAa,EACb,UAAU,EACV,iBAAiB,EACjB,aAAa,EACb,uBAAuB,EACvB,kBAAkB,EAClB,sBAAsB,EACtB,oBAAoB,EACpB,+BAA+B,EAC/B,8BAA8B,GAC/B,CAAC;AAEF,MAAM,WAAW,sBAAsB;IACrC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;CACjC;AA4KD,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,sBAAsB,EAC9B,OAAO,EAAE,MAAM,GACd,YAAY,
|
|
1
|
+
{"version":3,"file":"anthropic-provider.d.ts","sourceRoot":"","sources":["../src/anthropic-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAE/E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EACL,kBAAkB,EAIlB,aAAa,EACb,UAAU,EACV,iBAAiB,EACjB,aAAa,EACb,uBAAuB,EACvB,kBAAkB,EAClB,sBAAsB,EACtB,oBAAoB,EAKpB,+BAA+B,EAC/B,8BAA8B,EAC/B,MAAM,2BAA2B,CAAC;AAWnC,OAAO,EACL,kBAAkB,EAClB,aAAa,EACb,UAAU,EACV,iBAAiB,EACjB,aAAa,EACb,uBAAuB,EACvB,kBAAkB,EAClB,sBAAsB,EACtB,oBAAoB,EACpB,+BAA+B,EAC/B,8BAA8B,GAC/B,CAAC;AAEF,MAAM,WAAW,sBAAsB;IACrC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;CACjC;AA4KD,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,sBAAsB,EAC9B,OAAO,EAAE,MAAM,GACd,YAAY,CAgFd;AAED,qBAAa,iBAAkB,YAAW,WAAW;IACnD,QAAQ,CAAC,EAAE,eAAe;IAE1B,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,iBAAiB,GAAG,YAAY;CAYtE"}
|
|
@@ -134,8 +134,12 @@ function buildAnthropicGenerateResult(payload) {
|
|
|
134
134
|
}
|
|
135
135
|
export function createAnthropicModelRuntime(config, modelId) {
|
|
136
136
|
const fetchImpl = config.fetch ?? globalThis.fetch;
|
|
137
|
+
const providerName = config.name ?? "anthropic";
|
|
138
|
+
const streamOptions = providerName === "veryfront-cloud"
|
|
139
|
+
? { clientToolUseTrailingUsageTimeoutMode: "drain" }
|
|
140
|
+
: undefined;
|
|
137
141
|
return {
|
|
138
|
-
provider:
|
|
142
|
+
provider: providerName,
|
|
139
143
|
modelId,
|
|
140
144
|
specificationVersion: "v3",
|
|
141
145
|
supportedUrls: {},
|
|
@@ -185,7 +189,7 @@ export function createAnthropicModelRuntime(config, modelId) {
|
|
|
185
189
|
}).then((responseStream) => {
|
|
186
190
|
const drained = warnings.drain();
|
|
187
191
|
return {
|
|
188
|
-
stream: ReadableStream.from(withToolInputStatusTransitions(streamAnthropicCompatibleParts(responseStream))),
|
|
192
|
+
stream: ReadableStream.from(withToolInputStatusTransitions(streamAnthropicCompatibleParts(responseStream, streamOptions))),
|
|
189
193
|
...(drained.length > 0 ? { warnings: drained } : {}),
|
|
190
194
|
};
|
|
191
195
|
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { RuntimeUsage } from "veryfront/provider/shared";
|
|
2
2
|
type AnthropicStreamOptions = {
|
|
3
3
|
clientToolUseTrailingUsageGraceMs?: number;
|
|
4
|
+
clientToolUseTrailingUsageTimeoutMode?: "cancel" | "drain";
|
|
5
|
+
clientToolUseTrailingUsageDrainTimeoutMs?: number;
|
|
4
6
|
};
|
|
5
7
|
export declare function normalizeAnthropicFinishReason(raw: unknown): string | {
|
|
6
8
|
unified: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"anthropic-stream.d.ts","sourceRoot":"","sources":["../src/anthropic-stream.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAgB9D,KAAK,sBAAsB,GAAG;IAC5B,iCAAiC,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"anthropic-stream.d.ts","sourceRoot":"","sources":["../src/anthropic-stream.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAgB9D,KAAK,sBAAsB,GAAG;IAC5B,iCAAiC,CAAC,EAAE,MAAM,CAAC;IAC3C,qCAAqC,CAAC,EAAE,QAAQ,GAAG,OAAO,CAAC;IAC3D,wCAAwC,CAAC,EAAE,MAAM,CAAC;CACnD,CAAC;AAoBF,wBAAgB,8BAA8B,CAC5C,GAAG,EAAE,OAAO,GACX,MAAM,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAgBlD;AAED,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,OAAO,GAAG,YAAY,GAAG,SAAS,CA+DhF;AA+FD,wBAAuB,8BAA8B,CACnD,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,EAClC,OAAO,GAAE,sBAA2B,GACnC,aAAa,CAAC,OAAO,CAAC,CAwUxB"}
|
package/esm/anthropic-stream.js
CHANGED
|
@@ -2,6 +2,7 @@ import * as dntShim from "./_dnt.shims.js";
|
|
|
2
2
|
import { mergeUsage, parseSseChunk, readGatewayBillingMode, readRecord, stringifyJsonValue, } from "veryfront/provider/shared";
|
|
3
3
|
const CLIENT_TOOL_USE_FINISH_REASON = { unified: "tool-calls", raw: "tool_use" };
|
|
4
4
|
const DEFAULT_CLIENT_TOOL_USE_TRAILING_USAGE_GRACE_MS = 100;
|
|
5
|
+
const DEFAULT_CLIENT_TOOL_USE_TRAILING_USAGE_DRAIN_TIMEOUT_MS = 15_000;
|
|
5
6
|
function isEmptyRecord(value) {
|
|
6
7
|
return Boolean(value &&
|
|
7
8
|
typeof value === "object" &&
|
|
@@ -90,7 +91,7 @@ function isToolCallsFinishReason(finishReason) {
|
|
|
90
91
|
return finishReason === "tool-calls" ||
|
|
91
92
|
(typeof finishReason === "object" && finishReason?.unified === "tool-calls");
|
|
92
93
|
}
|
|
93
|
-
async function readStreamChunk(reader, timeoutMs) {
|
|
94
|
+
async function readStreamChunk(reader, timeoutMs, timeoutMode = "cancel", drainTimeoutMs = DEFAULT_CLIENT_TOOL_USE_TRAILING_USAGE_DRAIN_TIMEOUT_MS) {
|
|
94
95
|
if (timeoutMs === undefined) {
|
|
95
96
|
const read = await reader.read();
|
|
96
97
|
return read.done ? { kind: "done" } : { kind: "chunk", chunk: read.value };
|
|
@@ -98,12 +99,17 @@ async function readStreamChunk(reader, timeoutMs) {
|
|
|
98
99
|
let timeoutId;
|
|
99
100
|
const readPromise = reader.read().then((read) => read.done ? { kind: "done" } : { kind: "chunk", chunk: read.value });
|
|
100
101
|
const timeoutPromise = new Promise((resolve) => {
|
|
101
|
-
timeoutId = dntShim.setTimeout(() => resolve({ kind: "timeout" }), Math.max(1, timeoutMs));
|
|
102
|
+
timeoutId = dntShim.setTimeout(() => resolve({ kind: "timeout", readerMode: timeoutMode }), Math.max(1, timeoutMs));
|
|
102
103
|
});
|
|
103
104
|
try {
|
|
104
105
|
const result = await Promise.race([readPromise, timeoutPromise]);
|
|
105
106
|
if (result.kind === "timeout") {
|
|
106
|
-
|
|
107
|
+
if (timeoutMode === "drain") {
|
|
108
|
+
void drainStreamReaderAfterTimeout(reader, readPromise, drainTimeoutMs);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
await cancelStreamReader(reader, "Timed out waiting for trailing Anthropic tool-use usage metadata");
|
|
112
|
+
}
|
|
107
113
|
}
|
|
108
114
|
return result;
|
|
109
115
|
}
|
|
@@ -121,11 +127,40 @@ async function cancelStreamReader(reader, reason) {
|
|
|
121
127
|
// The upstream body may already be closed or canceled by the runtime.
|
|
122
128
|
}
|
|
123
129
|
}
|
|
130
|
+
async function drainStreamReaderAfterTimeout(reader, pendingRead, timeoutMs) {
|
|
131
|
+
const timeoutId = dntShim.setTimeout(() => {
|
|
132
|
+
void cancelStreamReader(reader, "Timed out draining trailing Anthropic tool-use usage metadata");
|
|
133
|
+
}, Math.max(1, timeoutMs));
|
|
134
|
+
try {
|
|
135
|
+
const firstRead = await pendingRead;
|
|
136
|
+
if (firstRead.kind === "done") {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
while (true) {
|
|
140
|
+
const read = await reader.read();
|
|
141
|
+
if (read.done) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
// The caller has already emitted a finish event; late drain failures should
|
|
148
|
+
// not fail the completed tool turn.
|
|
149
|
+
}
|
|
150
|
+
finally {
|
|
151
|
+
clearTimeout(timeoutId);
|
|
152
|
+
reader.releaseLock();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
124
155
|
export async function* streamAnthropicCompatibleParts(stream, options = {}) {
|
|
125
156
|
const decoder = new TextDecoder();
|
|
126
157
|
const reader = stream.getReader();
|
|
127
158
|
const trailingUsageGraceMs = options.clientToolUseTrailingUsageGraceMs ??
|
|
128
159
|
DEFAULT_CLIENT_TOOL_USE_TRAILING_USAGE_GRACE_MS;
|
|
160
|
+
const trailingUsageTimeoutMode = options.clientToolUseTrailingUsageTimeoutMode ?? "cancel";
|
|
161
|
+
const trailingUsageDrainTimeoutMs = options.clientToolUseTrailingUsageDrainTimeoutMs ??
|
|
162
|
+
DEFAULT_CLIENT_TOOL_USE_TRAILING_USAGE_DRAIN_TIMEOUT_MS;
|
|
163
|
+
let readerReleased = false;
|
|
129
164
|
let buffer = "";
|
|
130
165
|
const toolCalls = new Map();
|
|
131
166
|
const reasoningBlocks = new Map();
|
|
@@ -165,8 +200,9 @@ export async function* streamAnthropicCompatibleParts(stream, options = {}) {
|
|
|
165
200
|
});
|
|
166
201
|
try {
|
|
167
202
|
while (true) {
|
|
168
|
-
const read = await readStreamChunk(reader, getClientToolUseReadTimeoutMs());
|
|
203
|
+
const read = await readStreamChunk(reader, getClientToolUseReadTimeoutMs(), trailingUsageTimeoutMode, trailingUsageDrainTimeoutMs);
|
|
169
204
|
if (read.kind === "timeout") {
|
|
205
|
+
readerReleased = read.readerMode === "drain";
|
|
170
206
|
mergeTrailingBufferUsage();
|
|
171
207
|
finishReason ??= CLIENT_TOOL_USE_FINISH_REASON;
|
|
172
208
|
yield buildFinishPart();
|
|
@@ -386,7 +422,9 @@ export async function* streamAnthropicCompatibleParts(stream, options = {}) {
|
|
|
386
422
|
}
|
|
387
423
|
}
|
|
388
424
|
finally {
|
|
389
|
-
|
|
425
|
+
if (!readerReleased) {
|
|
426
|
+
reader.releaseLock();
|
|
427
|
+
}
|
|
390
428
|
}
|
|
391
429
|
mergeTrailingBufferUsage();
|
|
392
430
|
yield buildFinishPart();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veryfront/ext-llm-anthropic",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1051",
|
|
4
4
|
"description": "Veryfront first-party extension package for ext-llm-anthropic",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"veryfront",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"@deno/shim-timers": "~0.1.0"
|
|
51
51
|
},
|
|
52
52
|
"peerDependencies": {
|
|
53
|
-
"veryfront": "^0.1.
|
|
53
|
+
"veryfront": "^0.1.1051"
|
|
54
54
|
},
|
|
55
55
|
"type": "module",
|
|
56
56
|
"types": "./esm/index.d.ts",
|