ai-sdk-anthropic 0.0.1
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/index.d.ts +7 -0
- package/dist/index.js +83 -0
- package/package.json +31 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AnthropicProvider, AnthropicProviderSettings } from '@ai-sdk/anthropic';
|
|
2
|
+
export { VERSION } from '@ai-sdk/anthropic';
|
|
3
|
+
|
|
4
|
+
declare function createAnthropic(options?: AnthropicProviderSettings): AnthropicProvider;
|
|
5
|
+
declare const anthropic: AnthropicProvider;
|
|
6
|
+
|
|
7
|
+
export { anthropic, createAnthropic };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import {
|
|
3
|
+
createAnthropic as createAnthropicBase,
|
|
4
|
+
VERSION
|
|
5
|
+
} from "@ai-sdk/anthropic";
|
|
6
|
+
function isFinishPart(part) {
|
|
7
|
+
return part.type === "finish";
|
|
8
|
+
}
|
|
9
|
+
function getRawUsage(part) {
|
|
10
|
+
const usage = part.providerMetadata?.anthropic?.usage;
|
|
11
|
+
if (!usage || typeof usage !== "object") return void 0;
|
|
12
|
+
return usage;
|
|
13
|
+
}
|
|
14
|
+
function patchUsage(part) {
|
|
15
|
+
if (!isFinishPart(part)) return part;
|
|
16
|
+
const rawUsage = getRawUsage(part);
|
|
17
|
+
if (!rawUsage) return part;
|
|
18
|
+
const usage = { ...part.usage };
|
|
19
|
+
const inputTokens = rawUsage["input_tokens"];
|
|
20
|
+
const cacheReadTokens = rawUsage["cache_read_input_tokens"];
|
|
21
|
+
if ((usage.inputTokens == null || usage.inputTokens === 0) && typeof inputTokens === "number") {
|
|
22
|
+
usage.inputTokens = inputTokens;
|
|
23
|
+
}
|
|
24
|
+
if ((usage.cachedInputTokens == null || usage.cachedInputTokens === 0) && typeof cacheReadTokens === "number") {
|
|
25
|
+
usage.cachedInputTokens = cacheReadTokens;
|
|
26
|
+
}
|
|
27
|
+
if (typeof usage.inputTokens === "number" && typeof usage.outputTokens === "number") {
|
|
28
|
+
usage.totalTokens = usage.inputTokens + usage.outputTokens;
|
|
29
|
+
}
|
|
30
|
+
return { ...part, usage };
|
|
31
|
+
}
|
|
32
|
+
var WrappedLanguageModelV2 = class {
|
|
33
|
+
constructor(inner) {
|
|
34
|
+
this.inner = inner;
|
|
35
|
+
}
|
|
36
|
+
get specificationVersion() {
|
|
37
|
+
return this.inner.specificationVersion;
|
|
38
|
+
}
|
|
39
|
+
get provider() {
|
|
40
|
+
return this.inner.provider;
|
|
41
|
+
}
|
|
42
|
+
get modelId() {
|
|
43
|
+
return this.inner.modelId;
|
|
44
|
+
}
|
|
45
|
+
get supportedUrls() {
|
|
46
|
+
return this.inner.supportedUrls;
|
|
47
|
+
}
|
|
48
|
+
doGenerate(options) {
|
|
49
|
+
return this.inner.doGenerate(options);
|
|
50
|
+
}
|
|
51
|
+
async doStream(options) {
|
|
52
|
+
const result = await this.inner.doStream(options);
|
|
53
|
+
const patchedStream = result.stream.pipeThrough(
|
|
54
|
+
new TransformStream({
|
|
55
|
+
transform(part, controller) {
|
|
56
|
+
controller.enqueue(patchUsage(part));
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
);
|
|
60
|
+
return {
|
|
61
|
+
...result,
|
|
62
|
+
stream: patchedStream
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
function wrapModel(model) {
|
|
67
|
+
return new WrappedLanguageModelV2(model);
|
|
68
|
+
}
|
|
69
|
+
function createAnthropic(options = {}) {
|
|
70
|
+
const base = createAnthropicBase(options);
|
|
71
|
+
const provider = ((modelId) => wrapModel(base(modelId)));
|
|
72
|
+
Object.assign(provider, base);
|
|
73
|
+
provider.languageModel = (modelId) => wrapModel(base.languageModel(modelId));
|
|
74
|
+
provider.chat = (modelId) => wrapModel(base.chat(modelId));
|
|
75
|
+
provider.messages = (modelId) => wrapModel(base.messages(modelId));
|
|
76
|
+
return provider;
|
|
77
|
+
}
|
|
78
|
+
var anthropic = createAnthropic();
|
|
79
|
+
export {
|
|
80
|
+
VERSION,
|
|
81
|
+
anthropic,
|
|
82
|
+
createAnthropic
|
|
83
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ai-sdk-anthropic",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist/**/*",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsup",
|
|
19
|
+
"dev": "tsup --watch"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@ai-sdk/anthropic": "^2.0.57"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"tsup": "^8.0.2",
|
|
26
|
+
"typescript": "^5.8.3"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"zod": "^3.25.76 || ^4.1.8"
|
|
30
|
+
}
|
|
31
|
+
}
|