@redditech/provider-kit 0.2.0 → 0.3.0
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 +65 -30
- package/dist/src/adl.d.ts +2 -2
- package/dist/src/adl.js +4 -4
- package/dist/src/capacity.d.ts +61 -0
- package/dist/src/capacity.js +67 -0
- package/dist/src/cli.js +2 -2
- package/dist/src/config.d.ts +20 -0
- package/dist/src/config.js +49 -4
- package/dist/src/doctor.d.ts +3 -1
- package/dist/src/doctor.js +25 -2
- package/dist/src/handler.d.ts +21 -1
- package/dist/src/handler.js +184 -57
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.js +4 -0
- package/dist/src/keys.d.ts +10 -0
- package/dist/src/keys.js +29 -4
- package/dist/src/pending.d.ts +9 -3
- package/dist/src/postgres.d.ts +78 -0
- package/dist/src/postgres.js +186 -0
- package/dist/src/server.d.ts +3 -0
- package/dist/src/server.js +21 -7
- package/dist/src/serverless.d.ts +42 -0
- package/dist/src/serverless.js +115 -0
- package/dist/src/state.d.ts +48 -0
- package/dist/src/state.js +98 -0
- package/dist/src/upstream.d.ts +30 -2
- package/dist/src/upstream.js +71 -2
- package/package.json +7 -4
package/dist/src/upstream.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The model behind the provider: any server that speaks the OpenAI chat
|
|
3
|
-
* completions API (OpenRouter, a hosted API, a local Ollama or vLLM)
|
|
3
|
+
* completions API (OpenRouter, a hosted API, a local Ollama or vLLM), or
|
|
4
|
+
* Claude through the Anthropic API.
|
|
4
5
|
*/
|
|
6
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
5
7
|
export interface UpstreamMessage {
|
|
6
8
|
role: string;
|
|
7
9
|
content: string;
|
|
@@ -27,7 +29,9 @@ type FetchLike = (url: string, init: {
|
|
|
27
29
|
json(): Promise<unknown>;
|
|
28
30
|
}>;
|
|
29
31
|
export declare class UpstreamError extends Error {
|
|
30
|
-
|
|
32
|
+
/** The model's HTTP status, when it answered with one. 429 means it is rate limited. */
|
|
33
|
+
readonly status?: number;
|
|
34
|
+
constructor(message: string, status?: number);
|
|
31
35
|
}
|
|
32
36
|
export declare function createOpenAiCompatibleUpstream(options: {
|
|
33
37
|
baseUrl: string;
|
|
@@ -37,4 +41,28 @@ export declare function createOpenAiCompatibleUpstream(options: {
|
|
|
37
41
|
}): Upstream;
|
|
38
42
|
/** A canned answer with a usage report, for trying the payment flow without a model. */
|
|
39
43
|
export declare function createMockUpstream(): Upstream;
|
|
44
|
+
/** How much thinking a Claude model does before it answers. "off" maps to what the model accepts. */
|
|
45
|
+
export type ClaudeThinking = "adaptive" | "off";
|
|
46
|
+
export type ClaudeEffort = "low" | "medium" | "high" | "xhigh" | "max";
|
|
47
|
+
/** The part of the Anthropic client this upstream uses, so tests can stand in for it. */
|
|
48
|
+
export interface AnthropicMessagesLike {
|
|
49
|
+
messages: {
|
|
50
|
+
create(body: Anthropic.MessageCreateParamsNonStreaming, options?: {
|
|
51
|
+
timeout?: number;
|
|
52
|
+
}): Promise<Anthropic.Message>;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Claude through the Anthropic Messages API. The answer comes back in the
|
|
57
|
+
* chat completions shape the handler reads, with usage counting every input
|
|
58
|
+
* token (cache reads and writes included) and every output token (thinking
|
|
59
|
+
* included), so the buyer is charged for what the model was paid for.
|
|
60
|
+
*/
|
|
61
|
+
export declare function createAnthropicUpstream(options: {
|
|
62
|
+
apiKey?: string;
|
|
63
|
+
timeoutMs?: number;
|
|
64
|
+
effort?: ClaudeEffort;
|
|
65
|
+
thinking?: ClaudeThinking;
|
|
66
|
+
client?: AnthropicMessagesLike;
|
|
67
|
+
}): Upstream;
|
|
40
68
|
export {};
|
package/dist/src/upstream.js
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The model behind the provider: any server that speaks the OpenAI chat
|
|
3
|
+
* completions API (OpenRouter, a hosted API, a local Ollama or vLLM), or
|
|
4
|
+
* Claude through the Anthropic API.
|
|
5
|
+
*/
|
|
6
|
+
import Anthropic from "@anthropic-ai/sdk";
|
|
1
7
|
export class UpstreamError extends Error {
|
|
2
|
-
|
|
8
|
+
/** The model's HTTP status, when it answered with one. 429 means it is rate limited. */
|
|
9
|
+
status;
|
|
10
|
+
constructor(message, status) {
|
|
3
11
|
super(message);
|
|
4
12
|
this.name = "UpstreamError";
|
|
13
|
+
if (status !== undefined)
|
|
14
|
+
this.status = status;
|
|
5
15
|
}
|
|
6
16
|
}
|
|
7
17
|
export function createOpenAiCompatibleUpstream(options) {
|
|
@@ -22,7 +32,7 @@ export function createOpenAiCompatibleUpstream(options) {
|
|
|
22
32
|
throw new UpstreamError("the model could not be reached");
|
|
23
33
|
}
|
|
24
34
|
if (!response.ok)
|
|
25
|
-
throw new UpstreamError(`the model answered with status ${response.status}
|
|
35
|
+
throw new UpstreamError(`the model answered with status ${response.status}`, response.status);
|
|
26
36
|
let body;
|
|
27
37
|
try {
|
|
28
38
|
body = await response.json();
|
|
@@ -54,3 +64,62 @@ export function createMockUpstream() {
|
|
|
54
64
|
},
|
|
55
65
|
};
|
|
56
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* Claude through the Anthropic Messages API. The answer comes back in the
|
|
69
|
+
* chat completions shape the handler reads, with usage counting every input
|
|
70
|
+
* token (cache reads and writes included) and every output token (thinking
|
|
71
|
+
* included), so the buyer is charged for what the model was paid for.
|
|
72
|
+
*/
|
|
73
|
+
export function createAnthropicUpstream(options) {
|
|
74
|
+
const client = options.client ?? new Anthropic({ ...(options.apiKey ? { apiKey: options.apiKey } : {}), maxRetries: 1 });
|
|
75
|
+
return {
|
|
76
|
+
async createChatCompletion(request) {
|
|
77
|
+
const system = request.messages.filter((m) => m.role === "system").map((m) => m.content).join("\n\n");
|
|
78
|
+
const messages = request.messages
|
|
79
|
+
.filter((m) => m.role !== "system")
|
|
80
|
+
.map((m) => ({ role: m.role === "assistant" ? "assistant" : "user", content: m.content }));
|
|
81
|
+
const thinking = claudeThinking(request.model, options.thinking);
|
|
82
|
+
let message;
|
|
83
|
+
try {
|
|
84
|
+
message = await client.messages.create({
|
|
85
|
+
model: request.model,
|
|
86
|
+
max_tokens: request.max_tokens,
|
|
87
|
+
...(system ? { system } : {}),
|
|
88
|
+
messages,
|
|
89
|
+
...(thinking ? { thinking } : {}),
|
|
90
|
+
...(options.effort ? { output_config: { effort: options.effort } } : {}),
|
|
91
|
+
}, { timeout: options.timeoutMs ?? 120_000 });
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
// Only the status is passed on: the error can carry the request, key included.
|
|
95
|
+
if (error instanceof Anthropic.APIError && typeof error.status === "number")
|
|
96
|
+
throw new UpstreamError(`the model answered with status ${error.status}`, error.status);
|
|
97
|
+
throw new UpstreamError("the model could not be reached");
|
|
98
|
+
}
|
|
99
|
+
if (message.stop_reason === "refusal")
|
|
100
|
+
throw new UpstreamError("the model declined to answer");
|
|
101
|
+
const content = message.content.flatMap((block) => (block.type === "text" ? [block.text] : [])).join("");
|
|
102
|
+
if (!content)
|
|
103
|
+
throw new UpstreamError("the model returned no answer");
|
|
104
|
+
const usage = message.usage;
|
|
105
|
+
const promptTokens = usage.input_tokens + (usage.cache_creation_input_tokens ?? 0) + (usage.cache_read_input_tokens ?? 0);
|
|
106
|
+
return {
|
|
107
|
+
id: message.id,
|
|
108
|
+
object: "chat.completion",
|
|
109
|
+
model: message.model,
|
|
110
|
+
choices: [{ index: 0, message: { role: "assistant", content }, finish_reason: message.stop_reason === "max_tokens" ? "length" : "stop" }],
|
|
111
|
+
usage: { prompt_tokens: promptTokens, completion_tokens: usage.output_tokens, total_tokens: promptTokens + usage.output_tokens },
|
|
112
|
+
};
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
/** Thinking off is spelled differently per model, and Opus 5.5 cannot turn it off (lower the effort instead). */
|
|
117
|
+
function claudeThinking(model, thinking) {
|
|
118
|
+
if (thinking !== "off")
|
|
119
|
+
return undefined;
|
|
120
|
+
if (model.startsWith("claude-opus-5-5"))
|
|
121
|
+
return undefined;
|
|
122
|
+
if (model.startsWith("claude-sonnet-5-5"))
|
|
123
|
+
return { type: "between_tools" };
|
|
124
|
+
return { type: "disabled" };
|
|
125
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@redditech/provider-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Run your own paid, per-token priced provider on Reddi Agent Protocol: one config file, one command, any OpenAI-compatible model.",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Run your own paid, per-token priced provider on Reddi Agent Protocol: one config file, one command, any OpenAI-compatible model or Claude.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -41,12 +41,15 @@
|
|
|
41
41
|
"node": ">=20"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@
|
|
45
|
-
"@redditech/
|
|
44
|
+
"@anthropic-ai/sdk": "1.7.0",
|
|
45
|
+
"@redditech/agent-protocol": "^0.2.1",
|
|
46
|
+
"@redditech/x402-solana": "^0.1.2",
|
|
46
47
|
"@solana/web3.js": "^1.98.4"
|
|
47
48
|
},
|
|
48
49
|
"devDependencies": {
|
|
49
50
|
"@types/node": "^20",
|
|
51
|
+
"@types/pg": "8.23.1",
|
|
52
|
+
"pg": "8.23.1",
|
|
50
53
|
"typescript": "^5"
|
|
51
54
|
}
|
|
52
55
|
}
|