@copilotkit/aimock 1.7.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/.claude-plugin/marketplace.json +17 -0
- package/.claude-plugin/plugin.json +12 -0
- package/LICENSE +21 -0
- package/README.md +82 -0
- package/dist/_virtual/_rolldown/runtime.cjs +29 -0
- package/dist/a2a-handler.cjs +203 -0
- package/dist/a2a-handler.cjs.map +1 -0
- package/dist/a2a-handler.js +199 -0
- package/dist/a2a-handler.js.map +1 -0
- package/dist/a2a-mock.cjs +292 -0
- package/dist/a2a-mock.cjs.map +1 -0
- package/dist/a2a-mock.d.cts +41 -0
- package/dist/a2a-mock.d.cts.map +1 -0
- package/dist/a2a-mock.d.ts +41 -0
- package/dist/a2a-mock.d.ts.map +1 -0
- package/dist/a2a-mock.js +290 -0
- package/dist/a2a-mock.js.map +1 -0
- package/dist/a2a-stub.cjs +4 -0
- package/dist/a2a-stub.d.cts +3 -0
- package/dist/a2a-stub.d.ts +3 -0
- package/dist/a2a-stub.js +3 -0
- package/dist/a2a-types.d.cts +68 -0
- package/dist/a2a-types.d.cts.map +1 -0
- package/dist/a2a-types.d.ts +68 -0
- package/dist/a2a-types.d.ts.map +1 -0
- package/dist/aimock-cli.cjs +112 -0
- package/dist/aimock-cli.cjs.map +1 -0
- package/dist/aimock-cli.d.cts +19 -0
- package/dist/aimock-cli.d.cts.map +1 -0
- package/dist/aimock-cli.d.ts +19 -0
- package/dist/aimock-cli.d.ts.map +1 -0
- package/dist/aimock-cli.js +110 -0
- package/dist/aimock-cli.js.map +1 -0
- package/dist/aws-event-stream.cjs +117 -0
- package/dist/aws-event-stream.cjs.map +1 -0
- package/dist/aws-event-stream.d.cts +38 -0
- package/dist/aws-event-stream.d.cts.map +1 -0
- package/dist/aws-event-stream.d.ts +38 -0
- package/dist/aws-event-stream.d.ts.map +1 -0
- package/dist/aws-event-stream.js +114 -0
- package/dist/aws-event-stream.js.map +1 -0
- package/dist/bedrock-converse.cjs +445 -0
- package/dist/bedrock-converse.cjs.map +1 -0
- package/dist/bedrock-converse.d.cts +50 -0
- package/dist/bedrock-converse.d.cts.map +1 -0
- package/dist/bedrock-converse.d.ts +50 -0
- package/dist/bedrock-converse.d.ts.map +1 -0
- package/dist/bedrock-converse.js +443 -0
- package/dist/bedrock-converse.js.map +1 -0
- package/dist/bedrock.cjs +557 -0
- package/dist/bedrock.cjs.map +1 -0
- package/dist/bedrock.d.cts +41 -0
- package/dist/bedrock.d.cts.map +1 -0
- package/dist/bedrock.d.ts +41 -0
- package/dist/bedrock.d.ts.map +1 -0
- package/dist/bedrock.js +553 -0
- package/dist/bedrock.js.map +1 -0
- package/dist/chaos.cjs +114 -0
- package/dist/chaos.cjs.map +1 -0
- package/dist/chaos.d.cts +27 -0
- package/dist/chaos.d.cts.map +1 -0
- package/dist/chaos.d.ts +27 -0
- package/dist/chaos.d.ts.map +1 -0
- package/dist/chaos.js +113 -0
- package/dist/chaos.js.map +1 -0
- package/dist/cli.cjs +268 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +268 -0
- package/dist/cli.js.map +1 -0
- package/dist/cohere.cjs +434 -0
- package/dist/cohere.cjs.map +1 -0
- package/dist/cohere.d.cts +34 -0
- package/dist/cohere.d.cts.map +1 -0
- package/dist/cohere.d.ts +34 -0
- package/dist/cohere.d.ts.map +1 -0
- package/dist/cohere.js +433 -0
- package/dist/cohere.js.map +1 -0
- package/dist/config-loader.cjs +111 -0
- package/dist/config-loader.cjs.map +1 -0
- package/dist/config-loader.d.cts +100 -0
- package/dist/config-loader.d.cts.map +1 -0
- package/dist/config-loader.d.ts +100 -0
- package/dist/config-loader.d.ts.map +1 -0
- package/dist/config-loader.js +107 -0
- package/dist/config-loader.js.map +1 -0
- package/dist/embeddings.cjs +150 -0
- package/dist/embeddings.cjs.map +1 -0
- package/dist/embeddings.d.cts +12 -0
- package/dist/embeddings.d.cts.map +1 -0
- package/dist/embeddings.d.ts +12 -0
- package/dist/embeddings.d.ts.map +1 -0
- package/dist/embeddings.js +150 -0
- package/dist/embeddings.js.map +1 -0
- package/dist/fixture-loader.cjs +269 -0
- package/dist/fixture-loader.cjs.map +1 -0
- package/dist/fixture-loader.d.cts +17 -0
- package/dist/fixture-loader.d.cts.map +1 -0
- package/dist/fixture-loader.d.ts +17 -0
- package/dist/fixture-loader.d.ts.map +1 -0
- package/dist/fixture-loader.js +265 -0
- package/dist/fixture-loader.js.map +1 -0
- package/dist/gemini.cjs +403 -0
- package/dist/gemini.cjs.map +1 -0
- package/dist/gemini.d.cts +10 -0
- package/dist/gemini.d.cts.map +1 -0
- package/dist/gemini.d.ts +10 -0
- package/dist/gemini.d.ts.map +1 -0
- package/dist/gemini.js +403 -0
- package/dist/gemini.js.map +1 -0
- package/dist/helpers.cjs +276 -0
- package/dist/helpers.cjs.map +1 -0
- package/dist/helpers.d.cts +39 -0
- package/dist/helpers.d.cts.map +1 -0
- package/dist/helpers.d.ts +39 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +259 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.cjs +113 -0
- package/dist/index.d.cts +42 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +39 -0
- package/dist/interruption.cjs +40 -0
- package/dist/interruption.cjs.map +1 -0
- package/dist/interruption.d.cts +15 -0
- package/dist/interruption.d.cts.map +1 -0
- package/dist/interruption.d.ts +15 -0
- package/dist/interruption.d.ts.map +1 -0
- package/dist/interruption.js +39 -0
- package/dist/interruption.js.map +1 -0
- package/dist/journal.cjs +65 -0
- package/dist/journal.cjs.map +1 -0
- package/dist/journal.d.cts +23 -0
- package/dist/journal.d.cts.map +1 -0
- package/dist/journal.d.ts +23 -0
- package/dist/journal.d.ts.map +1 -0
- package/dist/journal.js +65 -0
- package/dist/journal.js.map +1 -0
- package/dist/jsonrpc.cjs +91 -0
- package/dist/jsonrpc.cjs.map +1 -0
- package/dist/jsonrpc.d.cts +24 -0
- package/dist/jsonrpc.d.cts.map +1 -0
- package/dist/jsonrpc.d.ts +24 -0
- package/dist/jsonrpc.d.ts.map +1 -0
- package/dist/jsonrpc.js +90 -0
- package/dist/jsonrpc.js.map +1 -0
- package/dist/llmock.cjs +223 -0
- package/dist/llmock.cjs.map +1 -0
- package/dist/llmock.d.cts +70 -0
- package/dist/llmock.d.cts.map +1 -0
- package/dist/llmock.d.ts +70 -0
- package/dist/llmock.d.ts.map +1 -0
- package/dist/llmock.js +223 -0
- package/dist/llmock.js.map +1 -0
- package/dist/logger.cjs +29 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.cts +14 -0
- package/dist/logger.d.cts.map +1 -0
- package/dist/logger.d.ts +14 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +28 -0
- package/dist/logger.js.map +1 -0
- package/dist/mcp-handler.cjs +189 -0
- package/dist/mcp-handler.cjs.map +1 -0
- package/dist/mcp-handler.js +188 -0
- package/dist/mcp-handler.js.map +1 -0
- package/dist/mcp-mock.cjs +169 -0
- package/dist/mcp-mock.cjs.map +1 -0
- package/dist/mcp-mock.d.cts +40 -0
- package/dist/mcp-mock.d.cts.map +1 -0
- package/dist/mcp-mock.d.ts +40 -0
- package/dist/mcp-mock.d.ts.map +1 -0
- package/dist/mcp-mock.js +167 -0
- package/dist/mcp-mock.js.map +1 -0
- package/dist/mcp-stub.cjs +4 -0
- package/dist/mcp-stub.d.cts +3 -0
- package/dist/mcp-stub.d.ts +3 -0
- package/dist/mcp-stub.js +3 -0
- package/dist/mcp-types.d.cts +65 -0
- package/dist/mcp-types.d.cts.map +1 -0
- package/dist/mcp-types.d.ts +65 -0
- package/dist/mcp-types.d.ts.map +1 -0
- package/dist/messages.cjs +489 -0
- package/dist/messages.cjs.map +1 -0
- package/dist/messages.d.cts +10 -0
- package/dist/messages.d.cts.map +1 -0
- package/dist/messages.d.ts +10 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +489 -0
- package/dist/messages.js.map +1 -0
- package/dist/metrics.cjs +160 -0
- package/dist/metrics.cjs.map +1 -0
- package/dist/metrics.d.cts +24 -0
- package/dist/metrics.d.cts.map +1 -0
- package/dist/metrics.d.ts +24 -0
- package/dist/metrics.d.ts.map +1 -0
- package/dist/metrics.js +158 -0
- package/dist/metrics.js.map +1 -0
- package/dist/moderation.cjs +91 -0
- package/dist/moderation.cjs.map +1 -0
- package/dist/moderation.d.cts +23 -0
- package/dist/moderation.d.cts.map +1 -0
- package/dist/moderation.d.ts +23 -0
- package/dist/moderation.d.ts.map +1 -0
- package/dist/moderation.js +91 -0
- package/dist/moderation.js.map +1 -0
- package/dist/ndjson-writer.cjs +31 -0
- package/dist/ndjson-writer.cjs.map +1 -0
- package/dist/ndjson-writer.d.cts +17 -0
- package/dist/ndjson-writer.d.cts.map +1 -0
- package/dist/ndjson-writer.d.ts +17 -0
- package/dist/ndjson-writer.d.ts.map +1 -0
- package/dist/ndjson-writer.js +31 -0
- package/dist/ndjson-writer.js.map +1 -0
- package/dist/ollama.cjs +519 -0
- package/dist/ollama.cjs.map +1 -0
- package/dist/ollama.d.cts +34 -0
- package/dist/ollama.d.cts.map +1 -0
- package/dist/ollama.d.ts +34 -0
- package/dist/ollama.d.ts.map +1 -0
- package/dist/ollama.js +517 -0
- package/dist/ollama.js.map +1 -0
- package/dist/recorder.cjs +311 -0
- package/dist/recorder.cjs.map +1 -0
- package/dist/recorder.d.cts +23 -0
- package/dist/recorder.d.cts.map +1 -0
- package/dist/recorder.d.ts +23 -0
- package/dist/recorder.d.ts.map +1 -0
- package/dist/recorder.js +305 -0
- package/dist/recorder.js.map +1 -0
- package/dist/rerank.cjs +71 -0
- package/dist/rerank.cjs.map +1 -0
- package/dist/rerank.d.cts +22 -0
- package/dist/rerank.d.cts.map +1 -0
- package/dist/rerank.d.ts +22 -0
- package/dist/rerank.d.ts.map +1 -0
- package/dist/rerank.js +71 -0
- package/dist/rerank.js.map +1 -0
- package/dist/responses.cjs +637 -0
- package/dist/responses.cjs.map +1 -0
- package/dist/responses.d.cts +16 -0
- package/dist/responses.d.cts.map +1 -0
- package/dist/responses.d.ts +16 -0
- package/dist/responses.d.ts.map +1 -0
- package/dist/responses.js +634 -0
- package/dist/responses.js.map +1 -0
- package/dist/router.cjs +68 -0
- package/dist/router.cjs.map +1 -0
- package/dist/router.d.cts +16 -0
- package/dist/router.d.cts.map +1 -0
- package/dist/router.d.ts +16 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +65 -0
- package/dist/router.js.map +1 -0
- package/dist/search.cjs +59 -0
- package/dist/search.cjs.map +1 -0
- package/dist/search.d.cts +23 -0
- package/dist/search.d.cts.map +1 -0
- package/dist/search.d.ts +23 -0
- package/dist/search.d.ts.map +1 -0
- package/dist/search.js +59 -0
- package/dist/search.js.map +1 -0
- package/dist/server.cjs +935 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.d.cts +28 -0
- package/dist/server.d.cts.map +1 -0
- package/dist/server.d.ts +28 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +933 -0
- package/dist/server.js.map +1 -0
- package/dist/sse-writer.cjs +59 -0
- package/dist/sse-writer.cjs.map +1 -0
- package/dist/sse-writer.d.cts +19 -0
- package/dist/sse-writer.d.cts.map +1 -0
- package/dist/sse-writer.d.ts +19 -0
- package/dist/sse-writer.d.ts.map +1 -0
- package/dist/sse-writer.js +55 -0
- package/dist/sse-writer.js.map +1 -0
- package/dist/stream-collapse.cjs +496 -0
- package/dist/stream-collapse.cjs.map +1 -0
- package/dist/stream-collapse.d.cts +70 -0
- package/dist/stream-collapse.d.cts.map +1 -0
- package/dist/stream-collapse.d.ts +70 -0
- package/dist/stream-collapse.d.ts.map +1 -0
- package/dist/stream-collapse.js +489 -0
- package/dist/stream-collapse.js.map +1 -0
- package/dist/suite.cjs +46 -0
- package/dist/suite.cjs.map +1 -0
- package/dist/suite.d.cts +31 -0
- package/dist/suite.d.cts.map +1 -0
- package/dist/suite.d.ts +31 -0
- package/dist/suite.d.ts.map +1 -0
- package/dist/suite.js +46 -0
- package/dist/suite.js.map +1 -0
- package/dist/types.d.cts +243 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +243 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/url.cjs +21 -0
- package/dist/url.cjs.map +1 -0
- package/dist/url.d.cts +16 -0
- package/dist/url.d.cts.map +1 -0
- package/dist/url.d.ts +16 -0
- package/dist/url.d.ts.map +1 -0
- package/dist/url.js +20 -0
- package/dist/url.js.map +1 -0
- package/dist/vector-handler.cjs +239 -0
- package/dist/vector-handler.cjs.map +1 -0
- package/dist/vector-handler.js +238 -0
- package/dist/vector-handler.js.map +1 -0
- package/dist/vector-mock.cjs +229 -0
- package/dist/vector-mock.cjs.map +1 -0
- package/dist/vector-mock.d.cts +39 -0
- package/dist/vector-mock.d.cts.map +1 -0
- package/dist/vector-mock.d.ts +39 -0
- package/dist/vector-mock.d.ts.map +1 -0
- package/dist/vector-mock.js +227 -0
- package/dist/vector-mock.js.map +1 -0
- package/dist/vector-stub.cjs +4 -0
- package/dist/vector-stub.d.cts +3 -0
- package/dist/vector-stub.d.ts +3 -0
- package/dist/vector-stub.js +3 -0
- package/dist/vector-types.d.cts +32 -0
- package/dist/vector-types.d.cts.map +1 -0
- package/dist/vector-types.d.ts +32 -0
- package/dist/vector-types.d.ts.map +1 -0
- package/dist/watcher.cjs +59 -0
- package/dist/watcher.cjs.map +1 -0
- package/dist/watcher.js +58 -0
- package/dist/watcher.js.map +1 -0
- package/dist/ws-framing.cjs +187 -0
- package/dist/ws-framing.cjs.map +1 -0
- package/dist/ws-framing.d.cts +26 -0
- package/dist/ws-framing.d.cts.map +1 -0
- package/dist/ws-framing.d.ts +26 -0
- package/dist/ws-framing.d.ts.map +1 -0
- package/dist/ws-framing.js +184 -0
- package/dist/ws-framing.js.map +1 -0
- package/dist/ws-gemini-live.cjs +364 -0
- package/dist/ws-gemini-live.cjs.map +1 -0
- package/dist/ws-gemini-live.d.cts +18 -0
- package/dist/ws-gemini-live.d.cts.map +1 -0
- package/dist/ws-gemini-live.d.ts +18 -0
- package/dist/ws-gemini-live.d.ts.map +1 -0
- package/dist/ws-gemini-live.js +364 -0
- package/dist/ws-gemini-live.js.map +1 -0
- package/dist/ws-realtime.cjs +435 -0
- package/dist/ws-realtime.cjs.map +1 -0
- package/dist/ws-realtime.d.cts +17 -0
- package/dist/ws-realtime.d.cts.map +1 -0
- package/dist/ws-realtime.d.ts +17 -0
- package/dist/ws-realtime.d.ts.map +1 -0
- package/dist/ws-realtime.js +435 -0
- package/dist/ws-realtime.js.map +1 -0
- package/dist/ws-responses.cjs +164 -0
- package/dist/ws-responses.cjs.map +1 -0
- package/dist/ws-responses.d.cts +18 -0
- package/dist/ws-responses.d.cts.map +1 -0
- package/dist/ws-responses.d.ts +18 -0
- package/dist/ws-responses.d.ts.map +1 -0
- package/dist/ws-responses.js +164 -0
- package/dist/ws-responses.js.map +1 -0
- package/fixtures/example-greeting.json +12 -0
- package/fixtures/example-multi-turn.json +14 -0
- package/fixtures/example-tool-call.json +15 -0
- package/package.json +118 -0
- package/skills/write-fixtures/SKILL.md +625 -0
package/dist/bedrock.cjs
ADDED
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
const require_helpers = require('./helpers.cjs');
|
|
2
|
+
const require_router = require('./router.cjs');
|
|
3
|
+
const require_sse_writer = require('./sse-writer.cjs');
|
|
4
|
+
const require_interruption = require('./interruption.cjs');
|
|
5
|
+
const require_chaos = require('./chaos.cjs');
|
|
6
|
+
const require_recorder = require('./recorder.cjs');
|
|
7
|
+
const require_aws_event_stream = require('./aws-event-stream.cjs');
|
|
8
|
+
|
|
9
|
+
//#region src/bedrock.ts
|
|
10
|
+
function extractTextContent(content) {
|
|
11
|
+
if (typeof content === "string") return content;
|
|
12
|
+
return content.filter((b) => b.type === "text").map((b) => b.text ?? "").join("");
|
|
13
|
+
}
|
|
14
|
+
function bedrockToCompletionRequest(req, modelId) {
|
|
15
|
+
const messages = [];
|
|
16
|
+
if (req.system) {
|
|
17
|
+
const systemText = typeof req.system === "string" ? req.system : req.system.filter((b) => b.type === "text").map((b) => b.text ?? "").join("");
|
|
18
|
+
if (systemText) messages.push({
|
|
19
|
+
role: "system",
|
|
20
|
+
content: systemText
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
for (const msg of req.messages) if (msg.role === "user") {
|
|
24
|
+
if (typeof msg.content !== "string" && Array.isArray(msg.content)) {
|
|
25
|
+
const toolResults = msg.content.filter((b) => b.type === "tool_result");
|
|
26
|
+
const textBlocks = msg.content.filter((b) => b.type === "text");
|
|
27
|
+
if (toolResults.length > 0) {
|
|
28
|
+
for (const tr of toolResults) {
|
|
29
|
+
const resultContent = typeof tr.content === "string" ? tr.content : Array.isArray(tr.content) ? tr.content.filter((b) => b.type === "text").map((b) => b.text ?? "").join("") : "";
|
|
30
|
+
messages.push({
|
|
31
|
+
role: "tool",
|
|
32
|
+
content: resultContent,
|
|
33
|
+
tool_call_id: tr.tool_use_id
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
if (textBlocks.length > 0) messages.push({
|
|
37
|
+
role: "user",
|
|
38
|
+
content: textBlocks.map((b) => b.text ?? "").join("")
|
|
39
|
+
});
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
messages.push({
|
|
44
|
+
role: "user",
|
|
45
|
+
content: extractTextContent(msg.content)
|
|
46
|
+
});
|
|
47
|
+
} else if (msg.role === "assistant") if (typeof msg.content === "string") messages.push({
|
|
48
|
+
role: "assistant",
|
|
49
|
+
content: msg.content
|
|
50
|
+
});
|
|
51
|
+
else if (Array.isArray(msg.content)) {
|
|
52
|
+
const toolUseBlocks = msg.content.filter((b) => b.type === "tool_use");
|
|
53
|
+
const textContent = extractTextContent(msg.content);
|
|
54
|
+
if (toolUseBlocks.length > 0) messages.push({
|
|
55
|
+
role: "assistant",
|
|
56
|
+
content: textContent || null,
|
|
57
|
+
tool_calls: toolUseBlocks.map((b) => ({
|
|
58
|
+
id: b.id ?? require_helpers.generateToolUseId(),
|
|
59
|
+
type: "function",
|
|
60
|
+
function: {
|
|
61
|
+
name: b.name ?? "",
|
|
62
|
+
arguments: typeof b.input === "string" ? b.input : JSON.stringify(b.input ?? {})
|
|
63
|
+
}
|
|
64
|
+
}))
|
|
65
|
+
});
|
|
66
|
+
else messages.push({
|
|
67
|
+
role: "assistant",
|
|
68
|
+
content: textContent || null
|
|
69
|
+
});
|
|
70
|
+
} else messages.push({
|
|
71
|
+
role: "assistant",
|
|
72
|
+
content: null
|
|
73
|
+
});
|
|
74
|
+
let tools;
|
|
75
|
+
if (req.tools && req.tools.length > 0) tools = req.tools.map((t) => ({
|
|
76
|
+
type: "function",
|
|
77
|
+
function: {
|
|
78
|
+
name: t.name,
|
|
79
|
+
description: t.description,
|
|
80
|
+
parameters: t.input_schema
|
|
81
|
+
}
|
|
82
|
+
}));
|
|
83
|
+
return {
|
|
84
|
+
model: modelId,
|
|
85
|
+
messages,
|
|
86
|
+
stream: false,
|
|
87
|
+
temperature: req.temperature,
|
|
88
|
+
tools
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function buildBedrockTextResponse(content, model) {
|
|
92
|
+
return {
|
|
93
|
+
id: require_helpers.generateMessageId(),
|
|
94
|
+
type: "message",
|
|
95
|
+
role: "assistant",
|
|
96
|
+
content: [{
|
|
97
|
+
type: "text",
|
|
98
|
+
text: content
|
|
99
|
+
}],
|
|
100
|
+
model,
|
|
101
|
+
stop_reason: "end_turn",
|
|
102
|
+
stop_sequence: null,
|
|
103
|
+
usage: {
|
|
104
|
+
input_tokens: 0,
|
|
105
|
+
output_tokens: 0
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function buildBedrockToolCallResponse(toolCalls, model, logger) {
|
|
110
|
+
return {
|
|
111
|
+
id: require_helpers.generateMessageId(),
|
|
112
|
+
type: "message",
|
|
113
|
+
role: "assistant",
|
|
114
|
+
content: toolCalls.map((tc) => {
|
|
115
|
+
let argsObj;
|
|
116
|
+
try {
|
|
117
|
+
argsObj = JSON.parse(tc.arguments || "{}");
|
|
118
|
+
} catch {
|
|
119
|
+
logger.warn(`Malformed JSON in fixture tool call arguments for "${tc.name}": ${tc.arguments}`);
|
|
120
|
+
argsObj = {};
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
type: "tool_use",
|
|
124
|
+
id: tc.id || require_helpers.generateToolUseId(),
|
|
125
|
+
name: tc.name,
|
|
126
|
+
input: argsObj
|
|
127
|
+
};
|
|
128
|
+
}),
|
|
129
|
+
model,
|
|
130
|
+
stop_reason: "tool_use",
|
|
131
|
+
stop_sequence: null,
|
|
132
|
+
usage: {
|
|
133
|
+
input_tokens: 0,
|
|
134
|
+
output_tokens: 0
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
async function handleBedrock(req, res, raw, modelId, fixtures, journal, defaults, setCorsHeaders) {
|
|
139
|
+
const { logger } = defaults;
|
|
140
|
+
setCorsHeaders(res);
|
|
141
|
+
const urlPath = req.url ?? `/model/${modelId}/invoke`;
|
|
142
|
+
let bedrockReq;
|
|
143
|
+
try {
|
|
144
|
+
bedrockReq = JSON.parse(raw);
|
|
145
|
+
} catch {
|
|
146
|
+
journal.add({
|
|
147
|
+
method: req.method ?? "POST",
|
|
148
|
+
path: urlPath,
|
|
149
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
150
|
+
body: null,
|
|
151
|
+
response: {
|
|
152
|
+
status: 400,
|
|
153
|
+
fixture: null
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
require_sse_writer.writeErrorResponse(res, 400, JSON.stringify({ error: {
|
|
157
|
+
message: "Malformed JSON",
|
|
158
|
+
type: "invalid_request_error"
|
|
159
|
+
} }));
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (!bedrockReq.messages || !Array.isArray(bedrockReq.messages)) {
|
|
163
|
+
journal.add({
|
|
164
|
+
method: req.method ?? "POST",
|
|
165
|
+
path: urlPath,
|
|
166
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
167
|
+
body: null,
|
|
168
|
+
response: {
|
|
169
|
+
status: 400,
|
|
170
|
+
fixture: null
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
require_sse_writer.writeErrorResponse(res, 400, JSON.stringify({ error: {
|
|
174
|
+
message: "Invalid request: messages array is required",
|
|
175
|
+
type: "invalid_request_error"
|
|
176
|
+
} }));
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
const completionReq = bedrockToCompletionRequest(bedrockReq, modelId);
|
|
180
|
+
const fixture = require_router.matchFixture(fixtures, completionReq, journal.fixtureMatchCounts);
|
|
181
|
+
if (fixture) journal.incrementFixtureMatchCount(fixture, fixtures);
|
|
182
|
+
if (require_chaos.applyChaos(res, fixture, defaults.chaos, req.headers, journal, {
|
|
183
|
+
method: req.method ?? "POST",
|
|
184
|
+
path: urlPath,
|
|
185
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
186
|
+
body: completionReq
|
|
187
|
+
}, defaults.registry, defaults.logger)) return;
|
|
188
|
+
if (!fixture) {
|
|
189
|
+
if (defaults.record) {
|
|
190
|
+
if (await require_recorder.proxyAndRecord(req, res, completionReq, "bedrock", urlPath, fixtures, defaults, raw)) {
|
|
191
|
+
journal.add({
|
|
192
|
+
method: req.method ?? "POST",
|
|
193
|
+
path: urlPath,
|
|
194
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
195
|
+
body: completionReq,
|
|
196
|
+
response: {
|
|
197
|
+
status: res.statusCode ?? 200,
|
|
198
|
+
fixture: null
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const strictStatus = defaults.strict ? 503 : 404;
|
|
205
|
+
const strictMessage = defaults.strict ? "Strict mode: no fixture matched" : "No fixture matched";
|
|
206
|
+
if (defaults.strict) logger.error(`STRICT: No fixture matched for ${req.method ?? "POST"} ${urlPath}`);
|
|
207
|
+
journal.add({
|
|
208
|
+
method: req.method ?? "POST",
|
|
209
|
+
path: urlPath,
|
|
210
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
211
|
+
body: completionReq,
|
|
212
|
+
response: {
|
|
213
|
+
status: strictStatus,
|
|
214
|
+
fixture: null
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
require_sse_writer.writeErrorResponse(res, strictStatus, JSON.stringify({ error: {
|
|
218
|
+
message: strictMessage,
|
|
219
|
+
type: "invalid_request_error"
|
|
220
|
+
} }));
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const response = fixture.response;
|
|
224
|
+
if (require_helpers.isErrorResponse(response)) {
|
|
225
|
+
const status = response.status ?? 500;
|
|
226
|
+
journal.add({
|
|
227
|
+
method: req.method ?? "POST",
|
|
228
|
+
path: urlPath,
|
|
229
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
230
|
+
body: completionReq,
|
|
231
|
+
response: {
|
|
232
|
+
status,
|
|
233
|
+
fixture
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
const anthropicError = {
|
|
237
|
+
type: "error",
|
|
238
|
+
error: {
|
|
239
|
+
type: response.error.type ?? "api_error",
|
|
240
|
+
message: response.error.message
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
require_sse_writer.writeErrorResponse(res, status, JSON.stringify(anthropicError));
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
if (require_helpers.isTextResponse(response)) {
|
|
247
|
+
journal.add({
|
|
248
|
+
method: req.method ?? "POST",
|
|
249
|
+
path: urlPath,
|
|
250
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
251
|
+
body: completionReq,
|
|
252
|
+
response: {
|
|
253
|
+
status: 200,
|
|
254
|
+
fixture
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
const body = buildBedrockTextResponse(response.content, completionReq.model);
|
|
258
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
259
|
+
res.end(JSON.stringify(body));
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (require_helpers.isToolCallResponse(response)) {
|
|
263
|
+
journal.add({
|
|
264
|
+
method: req.method ?? "POST",
|
|
265
|
+
path: urlPath,
|
|
266
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
267
|
+
body: completionReq,
|
|
268
|
+
response: {
|
|
269
|
+
status: 200,
|
|
270
|
+
fixture
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
const body = buildBedrockToolCallResponse(response.toolCalls, completionReq.model, logger);
|
|
274
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
275
|
+
res.end(JSON.stringify(body));
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
journal.add({
|
|
279
|
+
method: req.method ?? "POST",
|
|
280
|
+
path: urlPath,
|
|
281
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
282
|
+
body: completionReq,
|
|
283
|
+
response: {
|
|
284
|
+
status: 500,
|
|
285
|
+
fixture
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
|
|
289
|
+
message: "Fixture response did not match any known type",
|
|
290
|
+
type: "server_error"
|
|
291
|
+
} }));
|
|
292
|
+
}
|
|
293
|
+
function buildBedrockStreamTextEvents(content, chunkSize) {
|
|
294
|
+
const events = [];
|
|
295
|
+
events.push({
|
|
296
|
+
eventType: "messageStart",
|
|
297
|
+
payload: { role: "assistant" }
|
|
298
|
+
});
|
|
299
|
+
events.push({
|
|
300
|
+
eventType: "contentBlockStart",
|
|
301
|
+
payload: {
|
|
302
|
+
contentBlockIndex: 0,
|
|
303
|
+
start: {}
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
for (let i = 0; i < content.length; i += chunkSize) {
|
|
307
|
+
const slice = content.slice(i, i + chunkSize);
|
|
308
|
+
events.push({
|
|
309
|
+
eventType: "contentBlockDelta",
|
|
310
|
+
payload: {
|
|
311
|
+
contentBlockIndex: 0,
|
|
312
|
+
delta: {
|
|
313
|
+
type: "text_delta",
|
|
314
|
+
text: slice
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
events.push({
|
|
320
|
+
eventType: "contentBlockStop",
|
|
321
|
+
payload: { contentBlockIndex: 0 }
|
|
322
|
+
});
|
|
323
|
+
events.push({
|
|
324
|
+
eventType: "messageStop",
|
|
325
|
+
payload: { stopReason: "end_turn" }
|
|
326
|
+
});
|
|
327
|
+
return events;
|
|
328
|
+
}
|
|
329
|
+
function buildBedrockStreamToolCallEvents(toolCalls, chunkSize, logger) {
|
|
330
|
+
const events = [];
|
|
331
|
+
events.push({
|
|
332
|
+
eventType: "messageStart",
|
|
333
|
+
payload: { role: "assistant" }
|
|
334
|
+
});
|
|
335
|
+
for (let tcIdx = 0; tcIdx < toolCalls.length; tcIdx++) {
|
|
336
|
+
const tc = toolCalls[tcIdx];
|
|
337
|
+
const toolUseId = tc.id || require_helpers.generateToolUseId();
|
|
338
|
+
events.push({
|
|
339
|
+
eventType: "contentBlockStart",
|
|
340
|
+
payload: {
|
|
341
|
+
contentBlockIndex: tcIdx,
|
|
342
|
+
start: { toolUse: {
|
|
343
|
+
toolUseId,
|
|
344
|
+
name: tc.name
|
|
345
|
+
} }
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
let argsStr;
|
|
349
|
+
try {
|
|
350
|
+
const parsed = JSON.parse(tc.arguments || "{}");
|
|
351
|
+
argsStr = JSON.stringify(parsed);
|
|
352
|
+
} catch {
|
|
353
|
+
logger.warn(`Malformed JSON in fixture tool call arguments for "${tc.name}": ${tc.arguments}`);
|
|
354
|
+
argsStr = "{}";
|
|
355
|
+
}
|
|
356
|
+
for (let i = 0; i < argsStr.length; i += chunkSize) {
|
|
357
|
+
const slice = argsStr.slice(i, i + chunkSize);
|
|
358
|
+
events.push({
|
|
359
|
+
eventType: "contentBlockDelta",
|
|
360
|
+
payload: {
|
|
361
|
+
contentBlockIndex: tcIdx,
|
|
362
|
+
delta: {
|
|
363
|
+
type: "input_json_delta",
|
|
364
|
+
inputJSON: slice
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
events.push({
|
|
370
|
+
eventType: "contentBlockStop",
|
|
371
|
+
payload: { contentBlockIndex: tcIdx }
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
events.push({
|
|
375
|
+
eventType: "messageStop",
|
|
376
|
+
payload: { stopReason: "tool_use" }
|
|
377
|
+
});
|
|
378
|
+
return events;
|
|
379
|
+
}
|
|
380
|
+
async function handleBedrockStream(req, res, raw, modelId, fixtures, journal, defaults, setCorsHeaders) {
|
|
381
|
+
const { logger } = defaults;
|
|
382
|
+
setCorsHeaders(res);
|
|
383
|
+
const urlPath = req.url ?? `/model/${modelId}/invoke-with-response-stream`;
|
|
384
|
+
let bedrockReq;
|
|
385
|
+
try {
|
|
386
|
+
bedrockReq = JSON.parse(raw);
|
|
387
|
+
} catch {
|
|
388
|
+
journal.add({
|
|
389
|
+
method: req.method ?? "POST",
|
|
390
|
+
path: urlPath,
|
|
391
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
392
|
+
body: null,
|
|
393
|
+
response: {
|
|
394
|
+
status: 400,
|
|
395
|
+
fixture: null
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
require_sse_writer.writeErrorResponse(res, 400, JSON.stringify({ error: {
|
|
399
|
+
message: "Malformed JSON",
|
|
400
|
+
type: "invalid_request_error"
|
|
401
|
+
} }));
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
if (!bedrockReq.messages || !Array.isArray(bedrockReq.messages)) {
|
|
405
|
+
journal.add({
|
|
406
|
+
method: req.method ?? "POST",
|
|
407
|
+
path: urlPath,
|
|
408
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
409
|
+
body: null,
|
|
410
|
+
response: {
|
|
411
|
+
status: 400,
|
|
412
|
+
fixture: null
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
require_sse_writer.writeErrorResponse(res, 400, JSON.stringify({ error: {
|
|
416
|
+
message: "Invalid request: messages array is required",
|
|
417
|
+
type: "invalid_request_error"
|
|
418
|
+
} }));
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
const completionReq = bedrockToCompletionRequest(bedrockReq, modelId);
|
|
422
|
+
const fixture = require_router.matchFixture(fixtures, completionReq, journal.fixtureMatchCounts);
|
|
423
|
+
if (fixture) journal.incrementFixtureMatchCount(fixture, fixtures);
|
|
424
|
+
if (require_chaos.applyChaos(res, fixture, defaults.chaos, req.headers, journal, {
|
|
425
|
+
method: req.method ?? "POST",
|
|
426
|
+
path: urlPath,
|
|
427
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
428
|
+
body: completionReq
|
|
429
|
+
}, defaults.registry, defaults.logger)) return;
|
|
430
|
+
if (!fixture) {
|
|
431
|
+
if (defaults.record) {
|
|
432
|
+
if (await require_recorder.proxyAndRecord(req, res, completionReq, "bedrock", urlPath, fixtures, defaults, raw)) {
|
|
433
|
+
journal.add({
|
|
434
|
+
method: req.method ?? "POST",
|
|
435
|
+
path: urlPath,
|
|
436
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
437
|
+
body: completionReq,
|
|
438
|
+
response: {
|
|
439
|
+
status: res.statusCode ?? 200,
|
|
440
|
+
fixture: null
|
|
441
|
+
}
|
|
442
|
+
});
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
const strictStatus = defaults.strict ? 503 : 404;
|
|
447
|
+
const strictMessage = defaults.strict ? "Strict mode: no fixture matched" : "No fixture matched";
|
|
448
|
+
if (defaults.strict) logger.error(`STRICT: No fixture matched for ${req.method ?? "POST"} ${urlPath}`);
|
|
449
|
+
journal.add({
|
|
450
|
+
method: req.method ?? "POST",
|
|
451
|
+
path: urlPath,
|
|
452
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
453
|
+
body: completionReq,
|
|
454
|
+
response: {
|
|
455
|
+
status: strictStatus,
|
|
456
|
+
fixture: null
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
require_sse_writer.writeErrorResponse(res, strictStatus, JSON.stringify({ error: {
|
|
460
|
+
message: strictMessage,
|
|
461
|
+
type: "invalid_request_error"
|
|
462
|
+
} }));
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
const response = fixture.response;
|
|
466
|
+
const latency = fixture.latency ?? defaults.latency;
|
|
467
|
+
const chunkSize = Math.max(1, fixture.chunkSize ?? defaults.chunkSize);
|
|
468
|
+
if (require_helpers.isErrorResponse(response)) {
|
|
469
|
+
const status = response.status ?? 500;
|
|
470
|
+
journal.add({
|
|
471
|
+
method: req.method ?? "POST",
|
|
472
|
+
path: urlPath,
|
|
473
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
474
|
+
body: completionReq,
|
|
475
|
+
response: {
|
|
476
|
+
status,
|
|
477
|
+
fixture
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
require_sse_writer.writeErrorResponse(res, status, JSON.stringify(response));
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
if (require_helpers.isTextResponse(response)) {
|
|
484
|
+
const journalEntry = journal.add({
|
|
485
|
+
method: req.method ?? "POST",
|
|
486
|
+
path: urlPath,
|
|
487
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
488
|
+
body: completionReq,
|
|
489
|
+
response: {
|
|
490
|
+
status: 200,
|
|
491
|
+
fixture
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
const events = buildBedrockStreamTextEvents(response.content, chunkSize);
|
|
495
|
+
const interruption = require_interruption.createInterruptionSignal(fixture);
|
|
496
|
+
if (!await require_aws_event_stream.writeEventStream(res, events, {
|
|
497
|
+
latency,
|
|
498
|
+
streamingProfile: fixture.streamingProfile,
|
|
499
|
+
signal: interruption?.signal,
|
|
500
|
+
onChunkSent: interruption?.tick
|
|
501
|
+
})) {
|
|
502
|
+
if (!res.writableEnded) res.destroy();
|
|
503
|
+
journalEntry.response.interrupted = true;
|
|
504
|
+
journalEntry.response.interruptReason = interruption?.reason();
|
|
505
|
+
}
|
|
506
|
+
interruption?.cleanup();
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
if (require_helpers.isToolCallResponse(response)) {
|
|
510
|
+
const journalEntry = journal.add({
|
|
511
|
+
method: req.method ?? "POST",
|
|
512
|
+
path: urlPath,
|
|
513
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
514
|
+
body: completionReq,
|
|
515
|
+
response: {
|
|
516
|
+
status: 200,
|
|
517
|
+
fixture
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
const events = buildBedrockStreamToolCallEvents(response.toolCalls, chunkSize, logger);
|
|
521
|
+
const interruption = require_interruption.createInterruptionSignal(fixture);
|
|
522
|
+
if (!await require_aws_event_stream.writeEventStream(res, events, {
|
|
523
|
+
latency,
|
|
524
|
+
streamingProfile: fixture.streamingProfile,
|
|
525
|
+
signal: interruption?.signal,
|
|
526
|
+
onChunkSent: interruption?.tick
|
|
527
|
+
})) {
|
|
528
|
+
if (!res.writableEnded) res.destroy();
|
|
529
|
+
journalEntry.response.interrupted = true;
|
|
530
|
+
journalEntry.response.interruptReason = interruption?.reason();
|
|
531
|
+
}
|
|
532
|
+
interruption?.cleanup();
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
journal.add({
|
|
536
|
+
method: req.method ?? "POST",
|
|
537
|
+
path: urlPath,
|
|
538
|
+
headers: require_helpers.flattenHeaders(req.headers),
|
|
539
|
+
body: completionReq,
|
|
540
|
+
response: {
|
|
541
|
+
status: 500,
|
|
542
|
+
fixture
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
require_sse_writer.writeErrorResponse(res, 500, JSON.stringify({ error: {
|
|
546
|
+
message: "Fixture response did not match any known type",
|
|
547
|
+
type: "server_error"
|
|
548
|
+
} }));
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
//#endregion
|
|
552
|
+
exports.bedrockToCompletionRequest = bedrockToCompletionRequest;
|
|
553
|
+
exports.buildBedrockStreamTextEvents = buildBedrockStreamTextEvents;
|
|
554
|
+
exports.buildBedrockStreamToolCallEvents = buildBedrockStreamToolCallEvents;
|
|
555
|
+
exports.handleBedrock = handleBedrock;
|
|
556
|
+
exports.handleBedrockStream = handleBedrockStream;
|
|
557
|
+
//# sourceMappingURL=bedrock.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bedrock.cjs","names":["generateToolUseId","generateMessageId","flattenHeaders","matchFixture","applyChaos","proxyAndRecord","isErrorResponse","isTextResponse","isToolCallResponse","createInterruptionSignal","writeEventStream"],"sources":["../src/bedrock.ts"],"sourcesContent":["/**\n * AWS Bedrock Claude endpoint support — invoke and invoke-with-response-stream.\n *\n * Handles four Bedrock endpoint families (split across two modules):\n *\n * This file (bedrock.ts):\n * - POST /model/{modelId}/invoke — non-streaming invoke\n * - POST /model/{modelId}/invoke-with-response-stream — binary EventStream streaming\n *\n * bedrock-converse.ts:\n * - POST /model/{modelId}/converse — Converse API (non-streaming)\n * - POST /model/{modelId}/converse-stream — Converse API (EventStream streaming)\n *\n * Translates incoming Bedrock Claude format into the ChatCompletionRequest\n * format used by the fixture router, and converts fixture responses back into\n * the appropriate Bedrock response format (JSON for invoke, AWS Event Stream\n * binary encoding for streaming).\n */\n\nimport type * as http from \"node:http\";\nimport type {\n ChatCompletionRequest,\n ChatMessage,\n Fixture,\n HandlerDefaults,\n ToolCall,\n ToolDefinition,\n} from \"./types.js\";\nimport {\n generateMessageId,\n generateToolUseId,\n isTextResponse,\n isToolCallResponse,\n isErrorResponse,\n flattenHeaders,\n} from \"./helpers.js\";\nimport { matchFixture } from \"./router.js\";\nimport { writeErrorResponse } from \"./sse-writer.js\";\nimport { writeEventStream } from \"./aws-event-stream.js\";\nimport { createInterruptionSignal } from \"./interruption.js\";\nimport type { Journal } from \"./journal.js\";\nimport type { Logger } from \"./logger.js\";\nimport { applyChaos } from \"./chaos.js\";\nimport { proxyAndRecord } from \"./recorder.js\";\n\n// ─── Bedrock Claude request types ────────────────────────────────────────────\n\ninterface BedrockContentBlock {\n type: \"text\" | \"tool_use\" | \"tool_result\" | \"image\" | \"document\";\n text?: string;\n id?: string;\n name?: string;\n input?: unknown;\n tool_use_id?: string;\n content?: string | BedrockContentBlock[];\n is_error?: boolean;\n}\n\ninterface BedrockMessage {\n role: \"user\" | \"assistant\";\n content: string | BedrockContentBlock[];\n}\n\ninterface BedrockToolDef {\n name: string;\n description?: string;\n input_schema?: object;\n}\n\ninterface BedrockRequest {\n anthropic_version?: string;\n messages: BedrockMessage[];\n system?: string | BedrockContentBlock[];\n tools?: BedrockToolDef[];\n tool_choice?: unknown;\n max_tokens: number;\n temperature?: number;\n [key: string]: unknown;\n}\n\n// ─── Input conversion: Bedrock → ChatCompletionRequest ──────────────────────\n\nfunction extractTextContent(content: string | BedrockContentBlock[]): string {\n if (typeof content === \"string\") return content;\n return content\n .filter((b) => b.type === \"text\")\n .map((b) => b.text ?? \"\")\n .join(\"\");\n}\n\nexport function bedrockToCompletionRequest(\n req: BedrockRequest,\n modelId: string,\n): ChatCompletionRequest {\n const messages: ChatMessage[] = [];\n\n // system field → system message\n if (req.system) {\n const systemText =\n typeof req.system === \"string\"\n ? req.system\n : req.system\n .filter((b) => b.type === \"text\")\n .map((b) => b.text ?? \"\")\n .join(\"\");\n if (systemText) {\n messages.push({ role: \"system\", content: systemText });\n }\n }\n\n for (const msg of req.messages) {\n if (msg.role === \"user\") {\n // Check for tool_result blocks\n if (typeof msg.content !== \"string\" && Array.isArray(msg.content)) {\n const toolResults = msg.content.filter((b) => b.type === \"tool_result\");\n const textBlocks = msg.content.filter((b) => b.type === \"text\");\n\n if (toolResults.length > 0) {\n for (const tr of toolResults) {\n const resultContent =\n typeof tr.content === \"string\"\n ? tr.content\n : Array.isArray(tr.content)\n ? tr.content\n .filter((b) => b.type === \"text\")\n .map((b) => b.text ?? \"\")\n .join(\"\")\n : \"\";\n messages.push({\n role: \"tool\",\n content: resultContent,\n tool_call_id: tr.tool_use_id,\n });\n }\n if (textBlocks.length > 0) {\n messages.push({\n role: \"user\",\n content: textBlocks.map((b) => b.text ?? \"\").join(\"\"),\n });\n }\n continue;\n }\n }\n messages.push({\n role: \"user\",\n content: extractTextContent(msg.content),\n });\n } else if (msg.role === \"assistant\") {\n if (typeof msg.content === \"string\") {\n messages.push({ role: \"assistant\", content: msg.content });\n } else if (Array.isArray(msg.content)) {\n const toolUseBlocks = msg.content.filter((b) => b.type === \"tool_use\");\n const textContent = extractTextContent(msg.content);\n\n if (toolUseBlocks.length > 0) {\n messages.push({\n role: \"assistant\",\n content: textContent || null,\n tool_calls: toolUseBlocks.map((b) => ({\n id: b.id ?? generateToolUseId(),\n type: \"function\" as const,\n function: {\n name: b.name ?? \"\",\n arguments: typeof b.input === \"string\" ? b.input : JSON.stringify(b.input ?? {}),\n },\n })),\n });\n } else {\n messages.push({ role: \"assistant\", content: textContent || null });\n }\n } else {\n messages.push({ role: \"assistant\", content: null });\n }\n }\n }\n\n // Convert tools\n let tools: ToolDefinition[] | undefined;\n if (req.tools && req.tools.length > 0) {\n tools = req.tools.map((t) => ({\n type: \"function\" as const,\n function: {\n name: t.name,\n description: t.description,\n parameters: t.input_schema,\n },\n }));\n }\n\n return {\n model: modelId,\n messages,\n stream: false,\n temperature: req.temperature,\n tools,\n };\n}\n\n// ─── Response builders ──────────────────────────────────────────────────────\n\nfunction buildBedrockTextResponse(content: string, model: string): object {\n return {\n id: generateMessageId(),\n type: \"message\",\n role: \"assistant\",\n content: [{ type: \"text\", text: content }],\n model,\n stop_reason: \"end_turn\",\n stop_sequence: null,\n usage: { input_tokens: 0, output_tokens: 0 },\n };\n}\n\nfunction buildBedrockToolCallResponse(\n toolCalls: ToolCall[],\n model: string,\n logger: Logger,\n): object {\n return {\n id: generateMessageId(),\n type: \"message\",\n role: \"assistant\",\n content: toolCalls.map((tc) => {\n let argsObj: unknown;\n try {\n argsObj = JSON.parse(tc.arguments || \"{}\");\n } catch {\n logger.warn(\n `Malformed JSON in fixture tool call arguments for \"${tc.name}\": ${tc.arguments}`,\n );\n argsObj = {};\n }\n return {\n type: \"tool_use\",\n id: tc.id || generateToolUseId(),\n name: tc.name,\n input: argsObj,\n };\n }),\n model,\n stop_reason: \"tool_use\",\n stop_sequence: null,\n usage: { input_tokens: 0, output_tokens: 0 },\n };\n}\n\n// ─── Request handler ────────────────────────────────────────────────────────\n\nexport async function handleBedrock(\n req: http.IncomingMessage,\n res: http.ServerResponse,\n raw: string,\n modelId: string,\n fixtures: Fixture[],\n journal: Journal,\n defaults: HandlerDefaults,\n setCorsHeaders: (res: http.ServerResponse) => void,\n): Promise<void> {\n const { logger } = defaults;\n setCorsHeaders(res);\n\n const urlPath = req.url ?? `/model/${modelId}/invoke`;\n\n let bedrockReq: BedrockRequest;\n try {\n bedrockReq = JSON.parse(raw) as BedrockRequest;\n } catch {\n journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: null,\n response: { status: 400, fixture: null },\n });\n writeErrorResponse(\n res,\n 400,\n JSON.stringify({\n error: {\n message: \"Malformed JSON\",\n type: \"invalid_request_error\",\n },\n }),\n );\n return;\n }\n\n if (!bedrockReq.messages || !Array.isArray(bedrockReq.messages)) {\n journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: null,\n response: { status: 400, fixture: null },\n });\n writeErrorResponse(\n res,\n 400,\n JSON.stringify({\n error: {\n message: \"Invalid request: messages array is required\",\n type: \"invalid_request_error\",\n },\n }),\n );\n return;\n }\n\n // Convert to ChatCompletionRequest for fixture matching\n const completionReq = bedrockToCompletionRequest(bedrockReq, modelId);\n\n const fixture = matchFixture(fixtures, completionReq, journal.fixtureMatchCounts);\n\n if (fixture) {\n journal.incrementFixtureMatchCount(fixture, fixtures);\n }\n\n if (\n applyChaos(\n res,\n fixture,\n defaults.chaos,\n req.headers,\n journal,\n {\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: completionReq,\n },\n defaults.registry,\n defaults.logger,\n )\n )\n return;\n\n if (!fixture) {\n if (defaults.record) {\n const proxied = await proxyAndRecord(\n req,\n res,\n completionReq,\n \"bedrock\",\n urlPath,\n fixtures,\n defaults,\n raw,\n );\n if (proxied) {\n journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: completionReq,\n response: { status: res.statusCode ?? 200, fixture: null },\n });\n return;\n }\n }\n const strictStatus = defaults.strict ? 503 : 404;\n const strictMessage = defaults.strict\n ? \"Strict mode: no fixture matched\"\n : \"No fixture matched\";\n if (defaults.strict) {\n logger.error(`STRICT: No fixture matched for ${req.method ?? \"POST\"} ${urlPath}`);\n }\n journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: completionReq,\n response: { status: strictStatus, fixture: null },\n });\n writeErrorResponse(\n res,\n strictStatus,\n JSON.stringify({\n error: {\n message: strictMessage,\n type: \"invalid_request_error\",\n },\n }),\n );\n return;\n }\n\n const response = fixture.response;\n\n // Error response\n if (isErrorResponse(response)) {\n const status = response.status ?? 500;\n journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: completionReq,\n response: { status, fixture },\n });\n // Anthropic-style error format (Bedrock uses Claude): { type: \"error\", error: { type, message } }\n const anthropicError = {\n type: \"error\",\n error: {\n type: response.error.type ?? \"api_error\",\n message: response.error.message,\n },\n };\n writeErrorResponse(res, status, JSON.stringify(anthropicError));\n return;\n }\n\n // Text response\n if (isTextResponse(response)) {\n journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: completionReq,\n response: { status: 200, fixture },\n });\n const body = buildBedrockTextResponse(response.content, completionReq.model);\n res.writeHead(200, { \"Content-Type\": \"application/json\" });\n res.end(JSON.stringify(body));\n return;\n }\n\n // Tool call response\n if (isToolCallResponse(response)) {\n journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: completionReq,\n response: { status: 200, fixture },\n });\n const body = buildBedrockToolCallResponse(response.toolCalls, completionReq.model, logger);\n res.writeHead(200, { \"Content-Type\": \"application/json\" });\n res.end(JSON.stringify(body));\n return;\n }\n\n // Unknown response type\n journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: completionReq,\n response: { status: 500, fixture },\n });\n writeErrorResponse(\n res,\n 500,\n JSON.stringify({\n error: {\n message: \"Fixture response did not match any known type\",\n type: \"server_error\",\n },\n }),\n );\n}\n\n// ─── Streaming event builders ───────────────────────────────────────────────\n\nexport function buildBedrockStreamTextEvents(\n content: string,\n chunkSize: number,\n): Array<{ eventType: string; payload: object }> {\n const events: Array<{ eventType: string; payload: object }> = [];\n\n events.push({\n eventType: \"messageStart\",\n payload: { role: \"assistant\" },\n });\n\n events.push({\n eventType: \"contentBlockStart\",\n payload: { contentBlockIndex: 0, start: {} },\n });\n\n for (let i = 0; i < content.length; i += chunkSize) {\n const slice = content.slice(i, i + chunkSize);\n events.push({\n eventType: \"contentBlockDelta\",\n payload: {\n contentBlockIndex: 0,\n delta: { type: \"text_delta\", text: slice },\n },\n });\n }\n\n events.push({\n eventType: \"contentBlockStop\",\n payload: { contentBlockIndex: 0 },\n });\n\n events.push({\n eventType: \"messageStop\",\n payload: { stopReason: \"end_turn\" },\n });\n\n return events;\n}\n\nexport function buildBedrockStreamToolCallEvents(\n toolCalls: ToolCall[],\n chunkSize: number,\n logger: Logger,\n): Array<{ eventType: string; payload: object }> {\n const events: Array<{ eventType: string; payload: object }> = [];\n\n events.push({\n eventType: \"messageStart\",\n payload: { role: \"assistant\" },\n });\n\n for (let tcIdx = 0; tcIdx < toolCalls.length; tcIdx++) {\n const tc = toolCalls[tcIdx];\n const toolUseId = tc.id || generateToolUseId();\n\n events.push({\n eventType: \"contentBlockStart\",\n payload: {\n contentBlockIndex: tcIdx,\n start: {\n toolUse: { toolUseId, name: tc.name },\n },\n },\n });\n\n let argsStr: string;\n try {\n const parsed = JSON.parse(tc.arguments || \"{}\");\n argsStr = JSON.stringify(parsed);\n } catch {\n logger.warn(\n `Malformed JSON in fixture tool call arguments for \"${tc.name}\": ${tc.arguments}`,\n );\n argsStr = \"{}\";\n }\n\n for (let i = 0; i < argsStr.length; i += chunkSize) {\n const slice = argsStr.slice(i, i + chunkSize);\n events.push({\n eventType: \"contentBlockDelta\",\n payload: {\n contentBlockIndex: tcIdx,\n delta: { type: \"input_json_delta\", inputJSON: slice },\n },\n });\n }\n\n events.push({\n eventType: \"contentBlockStop\",\n payload: { contentBlockIndex: tcIdx },\n });\n }\n\n events.push({\n eventType: \"messageStop\",\n payload: { stopReason: \"tool_use\" },\n });\n\n return events;\n}\n\n// ─── Streaming request handler ──────────────────────────────────────────────\n\nexport async function handleBedrockStream(\n req: http.IncomingMessage,\n res: http.ServerResponse,\n raw: string,\n modelId: string,\n fixtures: Fixture[],\n journal: Journal,\n defaults: HandlerDefaults,\n setCorsHeaders: (res: http.ServerResponse) => void,\n): Promise<void> {\n const { logger } = defaults;\n setCorsHeaders(res);\n\n const urlPath = req.url ?? `/model/${modelId}/invoke-with-response-stream`;\n\n let bedrockReq: BedrockRequest;\n try {\n bedrockReq = JSON.parse(raw) as BedrockRequest;\n } catch {\n journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: null,\n response: { status: 400, fixture: null },\n });\n writeErrorResponse(\n res,\n 400,\n JSON.stringify({\n error: {\n message: \"Malformed JSON\",\n type: \"invalid_request_error\",\n },\n }),\n );\n return;\n }\n\n if (!bedrockReq.messages || !Array.isArray(bedrockReq.messages)) {\n journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: null,\n response: { status: 400, fixture: null },\n });\n writeErrorResponse(\n res,\n 400,\n JSON.stringify({\n error: {\n message: \"Invalid request: messages array is required\",\n type: \"invalid_request_error\",\n },\n }),\n );\n return;\n }\n\n const completionReq = bedrockToCompletionRequest(bedrockReq, modelId);\n\n const fixture = matchFixture(fixtures, completionReq, journal.fixtureMatchCounts);\n\n if (fixture) {\n journal.incrementFixtureMatchCount(fixture, fixtures);\n }\n\n if (\n applyChaos(\n res,\n fixture,\n defaults.chaos,\n req.headers,\n journal,\n {\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: completionReq,\n },\n defaults.registry,\n defaults.logger,\n )\n )\n return;\n\n if (!fixture) {\n if (defaults.record) {\n const proxied = await proxyAndRecord(\n req,\n res,\n completionReq,\n \"bedrock\",\n urlPath,\n fixtures,\n defaults,\n raw,\n );\n if (proxied) {\n journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: completionReq,\n response: { status: res.statusCode ?? 200, fixture: null },\n });\n return;\n }\n }\n const strictStatus = defaults.strict ? 503 : 404;\n const strictMessage = defaults.strict\n ? \"Strict mode: no fixture matched\"\n : \"No fixture matched\";\n if (defaults.strict) {\n logger.error(`STRICT: No fixture matched for ${req.method ?? \"POST\"} ${urlPath}`);\n }\n journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: completionReq,\n response: { status: strictStatus, fixture: null },\n });\n writeErrorResponse(\n res,\n strictStatus,\n JSON.stringify({\n error: {\n message: strictMessage,\n type: \"invalid_request_error\",\n },\n }),\n );\n return;\n }\n\n const response = fixture.response;\n const latency = fixture.latency ?? defaults.latency;\n const chunkSize = Math.max(1, fixture.chunkSize ?? defaults.chunkSize);\n\n // Error response\n if (isErrorResponse(response)) {\n const status = response.status ?? 500;\n journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: completionReq,\n response: { status, fixture },\n });\n writeErrorResponse(res, status, JSON.stringify(response));\n return;\n }\n\n // Text response — stream as Event Stream\n if (isTextResponse(response)) {\n const journalEntry = journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: completionReq,\n response: { status: 200, fixture },\n });\n const events = buildBedrockStreamTextEvents(response.content, chunkSize);\n const interruption = createInterruptionSignal(fixture);\n const completed = await writeEventStream(res, events, {\n latency,\n streamingProfile: fixture.streamingProfile,\n signal: interruption?.signal,\n onChunkSent: interruption?.tick,\n });\n if (!completed) {\n if (!res.writableEnded) res.destroy();\n journalEntry.response.interrupted = true;\n journalEntry.response.interruptReason = interruption?.reason();\n }\n interruption?.cleanup();\n return;\n }\n\n // Tool call response — stream as Event Stream\n if (isToolCallResponse(response)) {\n const journalEntry = journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: completionReq,\n response: { status: 200, fixture },\n });\n const events = buildBedrockStreamToolCallEvents(response.toolCalls, chunkSize, logger);\n const interruption = createInterruptionSignal(fixture);\n const completed = await writeEventStream(res, events, {\n latency,\n streamingProfile: fixture.streamingProfile,\n signal: interruption?.signal,\n onChunkSent: interruption?.tick,\n });\n if (!completed) {\n if (!res.writableEnded) res.destroy();\n journalEntry.response.interrupted = true;\n journalEntry.response.interruptReason = interruption?.reason();\n }\n interruption?.cleanup();\n return;\n }\n\n // Unknown response type\n journal.add({\n method: req.method ?? \"POST\",\n path: urlPath,\n headers: flattenHeaders(req.headers),\n body: completionReq,\n response: { status: 500, fixture },\n });\n writeErrorResponse(\n res,\n 500,\n JSON.stringify({\n error: {\n message: \"Fixture response did not match any known type\",\n type: \"server_error\",\n },\n }),\n );\n}\n"],"mappings":";;;;;;;;;AAkFA,SAAS,mBAAmB,SAAiD;AAC3E,KAAI,OAAO,YAAY,SAAU,QAA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,KAAK,QAAQ;GACpD;GACA,kBAAkB,QAAQ;GAC1B,QAAQ,cAAc;GACtB,aAAa,cAAc;GAC5B,CAAC,EACc;AACd,OAAI,CAAC,IAAI,cAAe,KAAI,SAAS;AACrC,gBAAa,SAAS,cAAc;AACpC,gBAAa,SAAS,kBAAkB,cAAc,QAAQ;;AAEhE,gBAAc,SAAS;AACvB;;AAIF,KAAIF,mCAAmB,SAAS,EAAE;EAChC,MAAM,eAAe,QAAQ,IAAI;GAC/B,QAAQ,IAAI,UAAU;GACtB,MAAM;GACN,SAASN,+BAAe,IAAI,QAAQ;GACpC,MAAM;GACN,UAAU;IAAE,QAAQ;IAAK;IAAS;GACnC,CAAC;EACF,MAAM,SAAS,iCAAiC,SAAS,WAAW,WAAW,OAAO;EACtF,MAAM,eAAeO,8CAAyB,QAAQ;AAOtD,MAAI,CANc,MAAMC,0CAAiB,KAAK,QAAQ;GACpD;GACA,kBAAkB,QAAQ;GAC1B,QAAQ,cAAc;GACtB,aAAa,cAAc;GAC5B,CAAC,EACc;AACd,OAAI,CAAC,IAAI,cAAe,KAAI,SAAS;AACrC,gBAAa,SAAS,cAAc;AACpC,gBAAa,SAAS,kBAAkB,cAAc,QAAQ;;AAEhE,gBAAc,SAAS;AACvB;;AAIF,SAAQ,IAAI;EACV,QAAQ,IAAI,UAAU;EACtB,MAAM;EACN,SAASR,+BAAe,IAAI,QAAQ;EACpC,MAAM;EACN,UAAU;GAAE,QAAQ;GAAK;GAAS;EACnC,CAAC;AACF,uCACE,KACA,KACA,KAAK,UAAU,EACb,OAAO;EACL,SAAS;EACT,MAAM;EACP,EACF,CAAC,CACH"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Journal } from "./journal.cjs";
|
|
2
|
+
import { ChatCompletionRequest, Fixture, HandlerDefaults } from "./types.cjs";
|
|
3
|
+
import * as http from "node:http";
|
|
4
|
+
|
|
5
|
+
//#region src/bedrock.d.ts
|
|
6
|
+
|
|
7
|
+
interface BedrockContentBlock {
|
|
8
|
+
type: "text" | "tool_use" | "tool_result" | "image" | "document";
|
|
9
|
+
text?: string;
|
|
10
|
+
id?: string;
|
|
11
|
+
name?: string;
|
|
12
|
+
input?: unknown;
|
|
13
|
+
tool_use_id?: string;
|
|
14
|
+
content?: string | BedrockContentBlock[];
|
|
15
|
+
is_error?: boolean;
|
|
16
|
+
}
|
|
17
|
+
interface BedrockMessage {
|
|
18
|
+
role: "user" | "assistant";
|
|
19
|
+
content: string | BedrockContentBlock[];
|
|
20
|
+
}
|
|
21
|
+
interface BedrockToolDef {
|
|
22
|
+
name: string;
|
|
23
|
+
description?: string;
|
|
24
|
+
input_schema?: object;
|
|
25
|
+
}
|
|
26
|
+
interface BedrockRequest {
|
|
27
|
+
anthropic_version?: string;
|
|
28
|
+
messages: BedrockMessage[];
|
|
29
|
+
system?: string | BedrockContentBlock[];
|
|
30
|
+
tools?: BedrockToolDef[];
|
|
31
|
+
tool_choice?: unknown;
|
|
32
|
+
max_tokens: number;
|
|
33
|
+
temperature?: number;
|
|
34
|
+
[key: string]: unknown;
|
|
35
|
+
}
|
|
36
|
+
declare function bedrockToCompletionRequest(req: BedrockRequest, modelId: string): ChatCompletionRequest;
|
|
37
|
+
declare function handleBedrock(req: http.IncomingMessage, res: http.ServerResponse, raw: string, modelId: string, fixtures: Fixture[], journal: Journal, defaults: HandlerDefaults, setCorsHeaders: (res: http.ServerResponse) => void): Promise<void>;
|
|
38
|
+
declare function handleBedrockStream(req: http.IncomingMessage, res: http.ServerResponse, raw: string, modelId: string, fixtures: Fixture[], journal: Journal, defaults: HandlerDefaults, setCorsHeaders: (res: http.ServerResponse) => void): Promise<void>;
|
|
39
|
+
//#endregion
|
|
40
|
+
export { bedrockToCompletionRequest, handleBedrock, handleBedrockStream };
|
|
41
|
+
//# sourceMappingURL=bedrock.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bedrock.d.cts","names":[],"sources":["../src/bedrock.ts"],"sourcesContent":[],"mappings":";;;;;;UA+CU,mBAAA,CA2ME;MAGA,EAAA,MAAA,GAAA,UAAA,GAAA,aAAA,GAAA,OAAA,GAAA,UAAA;MACD,CAAA,EAAA,MAAA;KACC,EAAA,MAAA;MACY,CAAA,EAAA,MAAK;OAC1B,CAAA,EAAA,OAAA;EAAO,WAAA,CAAA,EAAA,MAAA;EAqTY,OAAA,CAAA,EAAA,MAAA,GAhgBD,mBAggBoB,EAAA;EAAA,QAAA,CAAA,EAAA,OAAA;;UA5f/B,cAAA,CA8fE;MAGA,EAAA,MAAA,GAAA,WAAA;SACD,EAAA,MAAA,GAhgBS,mBAggBT,EAAA;;UA7fD,cAAA,CA+fmB;MAC1B,EAAA,MAAA;EAAO,WAAA,CAAA,EAAA,MAAA;;;UA1fA,cAAA;;YAEE;oBACQ;UACV;;;;;;iBAiBM,0BAAA,MACT,kCAEJ;iBA2JmB,aAAA,MACf,IAAA,CAAK,sBACL,IAAA,CAAK,wDAGA,oBACD,mBACC,uCACY,IAAA,CAAK,0BAC1B;iBAqTmB,mBAAA,MACf,IAAA,CAAK,sBACL,IAAA,CAAK,wDAGA,oBACD,mBACC,uCACY,IAAA,CAAK,0BAC1B"}
|