@toren-run/providers 0.1.3 → 0.1.5

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.
@@ -0,0 +1,15 @@
1
+ import type { BedrockRuntimeClient, ConverseCommandInput, ConverseCommandOutput } from "@aws-sdk/client-bedrock-runtime";
2
+ import type { ModelProvider, ModelRequest, ModelResponse } from "@toren-run/core";
3
+ export declare function toConverseParams(req: ModelRequest): ConverseCommandInput;
4
+ export declare function fromConverseResponse(res: ConverseCommandOutput): ModelResponse;
5
+ /**
6
+ * Bedrock adapter over toren's normalized model interface, via the Converse
7
+ * API (one wire shape for every Bedrock model that supports tools).
8
+ * Credentials resolve from the AWS chain; the SDK's own retry logic handles
9
+ * throttling.
10
+ */
11
+ export declare class BedrockProvider implements ModelProvider {
12
+ private client;
13
+ constructor(client?: BedrockRuntimeClient);
14
+ complete(req: ModelRequest): Promise<ModelResponse>;
15
+ }
@@ -0,0 +1,73 @@
1
+ import { createRequire } from "node:module";
2
+ // Lazy SDK load, same pattern as the other providers: a deployment that never
3
+ // routes a bedrock/ model never parses the AWS SDK. Auth is the standard AWS
4
+ // credential chain (env, profile, IAM role) — no API key exists; that IS the
5
+ // enterprise story. Region: AWS_REGION.
6
+ const requireCjs = createRequire(import.meta.url);
7
+ let sdk;
8
+ function loadSdk() {
9
+ sdk ??= requireCjs("@aws-sdk/client-bedrock-runtime");
10
+ return sdk;
11
+ }
12
+ function toConverseContent(m) {
13
+ const out = [];
14
+ for (const b of m.content) {
15
+ if (b.type === "text")
16
+ out.push({ text: b.text });
17
+ else if (b.type === "toolUse")
18
+ out.push({ toolUse: { toolUseId: b.id, name: b.name, input: b.input } });
19
+ else if (b.type === "toolResult")
20
+ out.push({ toolResult: { toolUseId: b.toolUseId, content: [{ text: b.content }], ...(b.isError ? { status: "error" } : {}) } });
21
+ }
22
+ return out;
23
+ }
24
+ export function toConverseParams(req) {
25
+ return {
26
+ modelId: req.model.replace(/^bedrock\//, ""),
27
+ ...(req.system ? { system: [{ text: req.system }] } : {}),
28
+ inferenceConfig: { maxTokens: req.maxTokens },
29
+ messages: req.messages.map((m) => ({ role: m.role, content: toConverseContent(m) })),
30
+ ...(req.tools.length
31
+ ? { toolConfig: { tools: req.tools.map((t) => ({ toolSpec: { name: t.name, description: t.description, inputSchema: { json: t.inputSchema } } })) } }
32
+ : {}),
33
+ };
34
+ }
35
+ const STOP_MAP = {
36
+ end_turn: "endTurn",
37
+ stop_sequence: "endTurn",
38
+ tool_use: "toolUse",
39
+ max_tokens: "maxTokens",
40
+ content_filtered: "refusal",
41
+ guardrail_intervened: "refusal",
42
+ };
43
+ export function fromConverseResponse(res) {
44
+ const content = [];
45
+ for (const b of res.output?.message?.content ?? []) {
46
+ if ("text" in b && b.text != null)
47
+ content.push({ type: "text", text: b.text });
48
+ else if ("toolUse" in b && b.toolUse)
49
+ content.push({ type: "toolUse", id: b.toolUse.toolUseId ?? "", name: b.toolUse.name ?? "", input: b.toolUse.input ?? {} });
50
+ }
51
+ return {
52
+ content,
53
+ stopReason: STOP_MAP[res.stopReason ?? "end_turn"] ?? "endTurn",
54
+ usage: { inputTokens: res.usage?.inputTokens ?? 0, outputTokens: res.usage?.outputTokens ?? 0 },
55
+ };
56
+ }
57
+ /**
58
+ * Bedrock adapter over toren's normalized model interface, via the Converse
59
+ * API (one wire shape for every Bedrock model that supports tools).
60
+ * Credentials resolve from the AWS chain; the SDK's own retry logic handles
61
+ * throttling.
62
+ */
63
+ export class BedrockProvider {
64
+ client;
65
+ constructor(client) {
66
+ this.client = client ?? new (loadSdk().BedrockRuntimeClient)();
67
+ }
68
+ async complete(req) {
69
+ const { ConverseCommand } = loadSdk();
70
+ const response = (await this.client.send(new ConverseCommand(toConverseParams(req))));
71
+ return fromConverseResponse(response);
72
+ }
73
+ }
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./anthropic.js";
2
2
  export * from "./openai.js";
3
+ export * from "./bedrock.js";
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./anthropic.js";
2
2
  export * from "./openai.js";
3
+ export * from "./bedrock.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toren-run/providers",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -10,8 +10,9 @@
10
10
  },
11
11
  "dependencies": {
12
12
  "@anthropic-ai/sdk": "^0.110.0",
13
+ "@aws-sdk/client-bedrock-runtime": "^3.1116.0",
13
14
  "openai": "^4.68.0",
14
- "@toren-run/core": "^0.1.3"
15
+ "@toren-run/core": "^0.1.5"
15
16
  },
16
17
  "description": "Model provider adapters for Toren (Anthropic, OpenAI) — each SDK loads lazily on first use.",
17
18
  "license": "Apache-2.0",