@mappies/miniwat-provider-bedrock 0.0.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/LICENSE ADDED
@@ -0,0 +1,131 @@
1
+ # PolyForm Noncommercial License 1.0.0
2
+
3
+ <https://polyformproject.org/licenses/noncommercial/1.0.0>
4
+
5
+ ## Acceptance
6
+
7
+ In order to get any license under these terms, you must agree
8
+ to them as both strict obligations and conditions to all
9
+ your licenses.
10
+
11
+ ## Copyright License
12
+
13
+ The licensor grants you a copyright license for the
14
+ software to do everything you might do with the software
15
+ that would otherwise infringe the licensor's copyright
16
+ in it for any permitted purpose. However, you may
17
+ only distribute the software according to [Distribution
18
+ License](#distribution-license) and make changes or new works
19
+ based on the software according to [Changes and New Works
20
+ License](#changes-and-new-works-license).
21
+
22
+ ## Distribution License
23
+
24
+ The licensor grants you an additional copyright license
25
+ to distribute copies of the software. Your license
26
+ to distribute covers distributing the software with
27
+ changes and new works permitted by [Changes and New Works
28
+ License](#changes-and-new-works-license).
29
+
30
+ ## Notices
31
+
32
+ You must ensure that anyone who gets a copy of any part of
33
+ the software from you also gets a copy of these terms or the
34
+ URL for them above, as well as copies of any plain-text lines
35
+ beginning with `Required Notice:` that the licensor provided
36
+ with the software. For example:
37
+
38
+ > Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
39
+
40
+ ## Changes and New Works License
41
+
42
+ The licensor grants you an additional copyright license to
43
+ make changes and new works based on the software for any
44
+ permitted purpose.
45
+
46
+ ## Patent License
47
+
48
+ The licensor grants you a patent license for the software that
49
+ covers patent claims the licensor can license, or becomes able
50
+ to license, that you would infringe by using the software.
51
+
52
+ ## Noncommercial Purposes
53
+
54
+ Any noncommercial purpose is a permitted purpose.
55
+
56
+ ## Personal Uses
57
+
58
+ Personal use for research, experiment, and testing for
59
+ the benefit of public knowledge, personal study, private
60
+ entertainment, hobby projects, amateur pursuits, or religious
61
+ observance, without any anticipated commercial application,
62
+ is use for a permitted purpose.
63
+
64
+ ## Noncommercial Organizations
65
+
66
+ Use by any charitable organization, educational institution,
67
+ public research organization, public safety or health
68
+ organization, environmental protection organization,
69
+ or government institution is use for a permitted purpose
70
+ regardless of the source of funding or obligations resulting
71
+ from the funding.
72
+
73
+ ## Fair Use
74
+
75
+ You may have "fair use" rights for the software under the
76
+ law. These terms do not limit them.
77
+
78
+ ## No Other Rights
79
+
80
+ These terms do not allow you to sublicense or transfer any of
81
+ your licenses to anyone else, or prevent the licensor from
82
+ granting licenses to anyone else. These terms do not imply
83
+ any other licenses.
84
+
85
+ ## Patent Defense
86
+
87
+ If you make any written claim that the software infringes or
88
+ contributes to infringement of any patent, your patent license
89
+ for the software granted under these terms ends immediately. If
90
+ your company makes such a claim, your patent license ends
91
+ immediately for work on behalf of your company.
92
+
93
+ ## Violations
94
+
95
+ The first time you are notified in writing that you have
96
+ violated any of these terms, or done anything with the software
97
+ not covered by your licenses, your licenses can nonetheless
98
+ continue if you come into full compliance with these terms,
99
+ and take practical steps to correct past violations, within
100
+ 32 days of receiving notice. Otherwise, all your licenses
101
+ end immediately.
102
+
103
+ ## No Liability
104
+
105
+ ***As far as the law allows, the software comes as is, without
106
+ any warranty or condition, and the licensor will not be liable
107
+ to you for any damages arising out of these terms or the use
108
+ or nature of the software, under any kind of legal claim.***
109
+
110
+ ## Definitions
111
+
112
+ The **licensor** is the individual or entity offering these
113
+ terms, and the **software** is the software the licensor makes
114
+ available under these terms.
115
+
116
+ **You** refers to the individual or entity agreeing to these
117
+ terms.
118
+
119
+ **Your company** is any legal entity, sole proprietorship,
120
+ or other kind of organization that you work for, plus all
121
+ organizations that have control over, are under the control of,
122
+ or are under common control with that organization. **Control**
123
+ means ownership of substantially all the assets of an entity,
124
+ or the power to direct its management and policies by vote,
125
+ contract, or otherwise. Control can be direct or indirect.
126
+
127
+ **Your licenses** are all the licenses granted to you for the
128
+ software under these terms.
129
+
130
+ **Use** means anything you do with the software requiring one
131
+ of your licenses.
package/dist/index.cjs ADDED
@@ -0,0 +1,343 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ BedrockLlmProvider: () => BedrockLlmProvider
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+
27
+ // src/bedrockLlmProvider.ts
28
+ var import_stream = require("stream");
29
+ var import_miniwat_api_core3 = require("@mappies/miniwat-api-core");
30
+ var import_miniwat_mcp_core = require("@mappies/miniwat-mcp-core");
31
+
32
+ // src/bedrockMessage.ts
33
+ var import_miniwat_api_core = require("@mappies/miniwat-api-core");
34
+ var BedrockMessage = class extends import_miniwat_api_core.Message {
35
+ constructor(data) {
36
+ super(data);
37
+ }
38
+ static toAssistantMessage(text) {
39
+ return new import_miniwat_api_core.Message({
40
+ type: import_miniwat_api_core.MessageType.Agent,
41
+ content: text,
42
+ createdTimestamp: (/* @__PURE__ */ new Date()).toISOString()
43
+ });
44
+ }
45
+ static toToolUseMessage(text, toolUses) {
46
+ return new import_miniwat_api_core.Message({
47
+ type: import_miniwat_api_core.MessageType.Agent,
48
+ content: text,
49
+ createdTimestamp: (/* @__PURE__ */ new Date()).toISOString(),
50
+ toolUse: toolUses.map(({ toolUseId, name, input }) => ({ toolUseId, name, input }))
51
+ });
52
+ }
53
+ static toToolResultMessage(toolResult) {
54
+ return new import_miniwat_api_core.Message({
55
+ type: import_miniwat_api_core.MessageType.ToolResult,
56
+ toolResult
57
+ });
58
+ }
59
+ };
60
+
61
+ // src/bedrockLlmProvider.ts
62
+ var import_client_bedrock_runtime = require("@aws-sdk/client-bedrock-runtime");
63
+
64
+ // src/bedrockAdapter.ts
65
+ var import_miniwat_api_core2 = require("@mappies/miniwat-api-core");
66
+ var BedrockAdapter = class {
67
+ static toBedrockTool(tool) {
68
+ return {
69
+ toolSpec: {
70
+ name: tool.name,
71
+ description: tool.description,
72
+ inputSchema: { json: tool.schema }
73
+ }
74
+ };
75
+ }
76
+ static toBedrockMessages(history) {
77
+ return history.filter(
78
+ (m) => (m.content ?? "").trim().length > 0 || (m.toolUse?.length ?? 0) > 0 || (m.toolResult?.length ?? 0) > 0
79
+ ).map((m) => {
80
+ const role = m.type === import_miniwat_api_core2.MessageType.User || m.type === import_miniwat_api_core2.MessageType.ToolResult ? "user" : "assistant";
81
+ const content = [];
82
+ if ((m.content ?? "").trim().length > 0) {
83
+ content.push({ text: m.content });
84
+ }
85
+ if (m.toolUse?.length) {
86
+ content.push(...m.toolUse.map((t) => ({ toolUse: t })));
87
+ }
88
+ if (m.toolResult?.length) {
89
+ content.push(...m.toolResult.map((r) => ({ toolResult: { ...r, "status": r.status ? "success" : "error" } })));
90
+ }
91
+ return { role, content };
92
+ });
93
+ }
94
+ static toToolResultContent(result) {
95
+ try {
96
+ if (result instanceof Error) {
97
+ return [{ text: result.message }];
98
+ }
99
+ if (result === null || result === void 0 || result === "") {
100
+ return [{ text: "" }];
101
+ }
102
+ if (typeof result === "object") {
103
+ let json = JSON.parse(JSON.stringify(result));
104
+ if (Array.isArray(json)) {
105
+ json = { result: json };
106
+ }
107
+ return [{ json }];
108
+ }
109
+ return [{ text: String(result) }];
110
+ } catch (e) {
111
+ const message = e instanceof Error ? e.message : String(e);
112
+ return [{
113
+ json: {
114
+ error: "Failed to serialize tool result",
115
+ message
116
+ }
117
+ }];
118
+ }
119
+ }
120
+ static getResponseFromStopReasonWhenResponseIsEmpty(stopReason) {
121
+ switch (stopReason) {
122
+ case "max_tokens":
123
+ return "My response was cut off due to length limits. Would you like me to continue or summarize the remaining information?";
124
+ case "stop_sequence":
125
+ return "I've reached a natural stopping point in my response. Please let me know if you need additional information.";
126
+ case "guardrail_intervened":
127
+ return "I'm unable to provide a response to this request as it doesn't align with my usage guidelines. Please rephrase your question or ask something else.";
128
+ case "content_filtered":
129
+ return "I cannot generate a response for this request due to content policy restrictions. Please try rephrasing your question.";
130
+ case "model_context_window_exceeded":
131
+ return "The conversation or request is too long for me to process. Please try with a shorter input or start a new conversation.";
132
+ case "end_turn":
133
+ return "What can I help with?";
134
+ case "tool_use":
135
+ return "";
136
+ default:
137
+ return "I couldn't complete the response. Please try again.";
138
+ }
139
+ }
140
+ };
141
+
142
+ // src/bedrockLlmProvider.ts
143
+ var BedrockLlmProvider = class {
144
+ constructor(options) {
145
+ this.toolDispatcher = new import_miniwat_mcp_core.ToolDispatcher();
146
+ this.modelId = options.modelId;
147
+ this.systemPrompt = options?.systemPrompt;
148
+ this.client = new import_client_bedrock_runtime.BedrockRuntimeClient({
149
+ region: options?.region ?? process.env.DEFAULT_AWS_REGION ?? process.env.AWS_REGION,
150
+ ...options?.clientConfig
151
+ });
152
+ }
153
+ async streamBufferedText(chunks, write) {
154
+ for (const chunk of chunks) {
155
+ write(chunk);
156
+ await new Promise((r) => setTimeout(r, 8 + Math.random() * 12));
157
+ }
158
+ }
159
+ extractAssistantText(res, messages) {
160
+ const content = res?.output?.message?.content ?? [];
161
+ let text = content.map((c) => c.text ?? "").join("");
162
+ if (!text) {
163
+ text = BedrockAdapter.getResponseFromStopReasonWhenResponseIsEmpty(res.stopReason);
164
+ }
165
+ return text;
166
+ }
167
+ async executeTools(toolUses, opts) {
168
+ const results = [];
169
+ for (const toolUse of toolUses) {
170
+ try {
171
+ const resultPromise = this.toolDispatcher.execute(toolUse.name, toolUse.input);
172
+ if (opts?.onThinking && toolUse.name) {
173
+ const inputText = await this.toolDispatcher.getInputText(toolUse.name, toolUse.input);
174
+ if (inputText) opts.onThinking(inputText);
175
+ }
176
+ const result = await resultPromise;
177
+ results.push({
178
+ toolUseId: toolUse.toolUseId,
179
+ content: BedrockAdapter.toToolResultContent(result),
180
+ status: true
181
+ });
182
+ } catch (err) {
183
+ results.push({
184
+ toolUseId: toolUse.toolUseId,
185
+ content: BedrockAdapter.toToolResultContent(err),
186
+ status: false
187
+ });
188
+ }
189
+ }
190
+ return results;
191
+ }
192
+ async runConversationLoop(messages, inference, options) {
193
+ let currentMessages = [...messages];
194
+ let iterations = 0;
195
+ while (true) {
196
+ if (++iterations > 20) throw new Error("Too many tool iterations");
197
+ const brMessages = BedrockAdapter.toBedrockMessages(currentMessages);
198
+ const converseCommandInput = {
199
+ modelId: this.modelId,
200
+ messages: brMessages,
201
+ system: this.systemPrompt ? [{ text: this.systemPrompt }] : void 0,
202
+ inferenceConfig: {
203
+ temperature: inference?.temperature ?? 0.2,
204
+ maxTokens: inference?.maxTokens,
205
+ topP: inference?.topP
206
+ },
207
+ toolConfig: {
208
+ tools: (await this.toolDispatcher.list()).map(BedrockAdapter.toBedrockTool)
209
+ }
210
+ };
211
+ if (options?.streaming) {
212
+ const res = await this.client.send(new import_client_bedrock_runtime.ConverseStreamCommand(converseCommandInput));
213
+ let chunks = [];
214
+ let streamingStarted = false;
215
+ let toolUses = [];
216
+ let currentToolUse;
217
+ let assistantText = "";
218
+ for await (const event of res.stream ?? []) {
219
+ if (event.modelStreamErrorException) {
220
+ throw new Error(event.modelStreamErrorException?.message ?? "Model stream error");
221
+ }
222
+ if (event.contentBlockDelta?.delta?.text) {
223
+ const chunk = event.contentBlockDelta.delta.text;
224
+ assistantText += chunk;
225
+ if (!streamingStarted) {
226
+ chunks.push(chunk);
227
+ if (chunks.length > 20) {
228
+ streamingStarted = true;
229
+ await this.streamBufferedText(chunks, options.onChunk);
230
+ chunks = [];
231
+ }
232
+ } else {
233
+ options.onChunk?.(chunk);
234
+ }
235
+ }
236
+ if (event.contentBlockStart?.start?.toolUse) {
237
+ currentToolUse = {
238
+ toolUseId: event.contentBlockStart.start.toolUse.toolUseId,
239
+ name: event.contentBlockStart.start.toolUse.name,
240
+ inputBuffer: ""
241
+ };
242
+ toolUses.push(currentToolUse);
243
+ }
244
+ if (currentToolUse && event.contentBlockDelta?.delta?.toolUse?.input) {
245
+ currentToolUse.inputBuffer += event.contentBlockDelta.delta.toolUse.input;
246
+ }
247
+ if (event.contentBlockStop && currentToolUse?.inputBuffer) {
248
+ currentToolUse.input = JSON.parse(currentToolUse.inputBuffer);
249
+ delete currentToolUse.inputBuffer;
250
+ currentToolUse = void 0;
251
+ }
252
+ if (event.messageStop) {
253
+ const reason = event.messageStop.stopReason;
254
+ if (reason === "tool_use") {
255
+ if (!streamingStarted && chunks.length > 0) {
256
+ options.onThinking?.(chunks.join(""));
257
+ chunks = [];
258
+ }
259
+ currentMessages.push(BedrockMessage.toToolUseMessage(assistantText, toolUses));
260
+ const toolResults = await this.executeTools(toolUses, options);
261
+ currentMessages.push(BedrockMessage.toToolResultMessage(toolResults));
262
+ break;
263
+ }
264
+ if (typeof reason === "string") {
265
+ if (/content|filter|guardrail/i.test(reason)) {
266
+ throw new Error("Output blocked by content filtering policy: " + reason);
267
+ } else if (reason === "max_tokens") {
268
+ options.onChunk?.(BedrockAdapter.getResponseFromStopReasonWhenResponseIsEmpty(reason));
269
+ } else if (!streamingStarted) {
270
+ if (chunks.length > 0) {
271
+ await this.streamBufferedText(chunks, options.onChunk);
272
+ } else {
273
+ options.onChunk?.(BedrockAdapter.getResponseFromStopReasonWhenResponseIsEmpty(reason));
274
+ }
275
+ }
276
+ }
277
+ return {
278
+ message: BedrockMessage.toAssistantMessage(assistantText),
279
+ stopReason: reason
280
+ };
281
+ }
282
+ }
283
+ } else {
284
+ const res = await this.client.send(new import_client_bedrock_runtime.ConverseCommand(converseCommandInput));
285
+ const text = this.extractAssistantText(res, brMessages);
286
+ const content = res?.output?.message?.content ?? [];
287
+ if (res.stopReason === "tool_use") {
288
+ const toolUses = content.filter((c) => c.toolUse).map((c) => ({
289
+ toolUseId: c.toolUse.toolUseId,
290
+ name: c.toolUse.name,
291
+ input: c.toolUse.input
292
+ }));
293
+ currentMessages.push(BedrockMessage.toToolUseMessage(text, toolUses));
294
+ const toolResults = await this.executeTools(toolUses, options);
295
+ currentMessages.push(BedrockMessage.toToolResultMessage(toolResults));
296
+ continue;
297
+ }
298
+ const msg = BedrockMessage.toAssistantMessage(text);
299
+ return {
300
+ message: msg,
301
+ stopReason: res.stopReason,
302
+ usage: res.usage
303
+ };
304
+ }
305
+ }
306
+ }
307
+ async invoke(messages, inference, callback) {
308
+ const response = await this.runConversationLoop(messages, inference, {
309
+ streaming: false,
310
+ onThinking: callback?.onThinking
311
+ });
312
+ messages.push(response.message);
313
+ return new import_miniwat_api_core3.ResponseMessage({
314
+ stopReason: response.stopReason,
315
+ tokenUsage: response.usage,
316
+ message: response.message
317
+ });
318
+ }
319
+ async stream(messages, inference, callback) {
320
+ const out = new import_stream.PassThrough({ encoding: "utf8" });
321
+ (async () => {
322
+ try {
323
+ await this.runConversationLoop(messages, inference, {
324
+ streaming: true,
325
+ onChunk: (c) => out.write(c),
326
+ onThinking: callback?.onThinking
327
+ });
328
+ out.write("");
329
+ out.end();
330
+ } catch (e) {
331
+ out.emit("throw", e);
332
+ out.write("");
333
+ out.end();
334
+ }
335
+ })();
336
+ return out;
337
+ }
338
+ };
339
+ // Annotate the CommonJS export names for ESM import in node:
340
+ 0 && (module.exports = {
341
+ BedrockLlmProvider
342
+ });
343
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/bedrockLlmProvider.ts","../src/bedrockMessage.ts","../src/bedrockAdapter.ts"],"sourcesContent":["export { BedrockLlmProvider } from './bedrockLlmProvider';","import { PassThrough, Readable } from \"stream\";\nimport { ResponseMessage, Message, LlmProvider, InferenceOptions, ToolUse, ToolResult } from '@mappies/miniwat-api-core'\nimport { ToolDispatcher } from '@mappies/miniwat-mcp-core'\nimport BedrockMessage from \"./bedrockMessage\";\nimport { BedrockRuntimeClient, ConverseCommand, ConverseStreamCommand, Message as BrMessage, StopReason, BedrockRuntimeClientConfig } from \"@aws-sdk/client-bedrock-runtime\";\nimport { BedrockAdapter } from \"./bedrockAdapter\";\n\nexport class BedrockLlmProvider implements LlmProvider\n{\n private modelId: string;\n private systemPrompt?: string;\n private toolDispatcher: ToolDispatcher;\n private client: BedrockRuntimeClient;\n\n constructor(options: { region?: string; modelId: string; systemPrompt?: string; clientConfig?: BedrockRuntimeClientConfig; })\n {\n this.toolDispatcher = new ToolDispatcher();\n this.modelId = options.modelId;\n this.systemPrompt = options?.systemPrompt;\n\n this.client = new BedrockRuntimeClient({\n region: options?.region ?? process.env.DEFAULT_AWS_REGION ?? process.env.AWS_REGION,\n ...options?.clientConfig\n });\n }\n\n private async streamBufferedText(chunks: string[], write: (chunk: string) => void)\n {\n for(const chunk of chunks)\n {\n write(chunk);\n await new Promise(r => setTimeout(r, 8 + Math.random() * 12));\n }\n }\n\n private extractAssistantText(res: any, messages: BrMessage[]): string\n {\n const content = res?.output?.message?.content ?? [];\n let text = content.map((c: any) => c.text ?? \"\").join(\"\");\n\n if(!text)\n {\n text = BedrockAdapter.getResponseFromStopReasonWhenResponseIsEmpty(res.stopReason);\n }\n\n return text;\n }\n\n private async executeTools(toolUses: ToolUse[], opts?: any): Promise<ToolResult[]>\n {\n const results: ToolResult[] = [];\n\n for(const toolUse of toolUses)\n {\n try\n {\n const resultPromise = this.toolDispatcher.execute(toolUse.name, toolUse.input);\n\n // Show a thinking message for the tool\n if (opts?.onThinking && toolUse.name)\n {\n const inputText = await this.toolDispatcher.getInputText(toolUse.name, toolUse.input);\n if(inputText) opts.onThinking(inputText);\n }\n\n const result = await resultPromise;\n\n results.push({\n toolUseId: toolUse.toolUseId,\n content: BedrockAdapter.toToolResultContent(result),\n status: true\n });\n }\n catch(err: any)\n {\n results.push({\n toolUseId: toolUse.toolUseId,\n content: BedrockAdapter.toToolResultContent(err),\n status: false\n });\n }\n }\n\n return results;\n }\n\n private async runConversationLoop(messages: Message[], inference?: InferenceOptions, options?: { streaming?: boolean; onChunk?: (chunk: string) => void; onThinking?: (msg: string) => Promise<void>;}): Promise<{ message: Message; stopReason?: StopReason; usage?: any }>\n {\n let currentMessages = [...messages];\n let iterations = 0;\n\n while(true)\n {\n if (++iterations > 20) throw new Error(\"Too many tool iterations\");\n\n const brMessages = BedrockAdapter.toBedrockMessages(currentMessages);\n\n const converseCommandInput = {\n modelId: this.modelId,\n messages: brMessages,\n system: this.systemPrompt ? [{ text: this.systemPrompt }] : undefined,\n inferenceConfig: {\n temperature: inference?.temperature ?? 0.2,\n maxTokens: inference?.maxTokens,\n topP: inference?.topP\n },\n toolConfig: {\n tools: (await this.toolDispatcher.list()).map(BedrockAdapter.toBedrockTool)\n }\n }\n\n if (options?.streaming)\n {\n const res = await this.client.send(new ConverseStreamCommand(converseCommandInput));\n\n let chunks: string[] = [];\n let streamingStarted = false;\n let toolUses: ToolUse[] = [];\n let currentToolUse: ToolUse | undefined;\n let assistantText = '';\n\n for await (const event of res.stream ?? [])\n {\n\n if (event.modelStreamErrorException)\n {\n throw new Error(event.modelStreamErrorException?.message ?? \"Model stream error\");\n }\n\n if (event.contentBlockDelta?.delta?.text)\n {\n const chunk = event.contentBlockDelta.delta.text;\n assistantText += chunk;\n\n if (!streamingStarted)\n {\n chunks.push(chunk);\n\n if (chunks.length > 20)\n {\n streamingStarted = true;\n await this.streamBufferedText(chunks, options.onChunk!);\n chunks = [];\n }\n }\n else\n {\n options.onChunk?.(chunk);\n }\n }\n\n if (event.contentBlockStart?.start?.toolUse)\n {\n currentToolUse = {\n toolUseId: event.contentBlockStart.start.toolUse.toolUseId as string,\n name: event.contentBlockStart.start.toolUse.name as string,\n inputBuffer: \"\"\n };\n toolUses.push(currentToolUse);\n }\n\n if (currentToolUse && event.contentBlockDelta?.delta?.toolUse?.input)\n {\n currentToolUse.inputBuffer += event.contentBlockDelta.delta.toolUse.input;\n }\n\n if (event.contentBlockStop && currentToolUse?.inputBuffer)\n {\n currentToolUse.input = JSON.parse(currentToolUse.inputBuffer);\n delete currentToolUse.inputBuffer;\n currentToolUse = undefined;\n }\n\n if (event.messageStop)\n {\n const reason = event.messageStop.stopReason;\n\n if (reason === \"tool_use\")\n {\n\n if (!streamingStarted && chunks.length > 0)\n {\n options.onThinking?.(chunks.join(\"\"));\n chunks = [];\n }\n\n currentMessages.push(BedrockMessage.toToolUseMessage(assistantText, toolUses));\n\n const toolResults = await this.executeTools(toolUses, options);\n\n currentMessages.push(BedrockMessage.toToolResultMessage(toolResults));\n\n break;\n }\n\n if (typeof reason === \"string\")\n {\n if (/content|filter|guardrail/i.test(reason))\n {\n throw new Error(\"Output blocked by content filtering policy: \" + reason);\n }\n else if (reason === \"max_tokens\")\n {\n options.onChunk?.(BedrockAdapter.getResponseFromStopReasonWhenResponseIsEmpty(reason));\n }\n else if (!streamingStarted)\n {\n if (chunks.length > 0)\n {\n await this.streamBufferedText(chunks, options.onChunk!);\n } else\n {\n options.onChunk?.(BedrockAdapter.getResponseFromStopReasonWhenResponseIsEmpty(reason));\n }\n }\n }\n\n return {\n message: BedrockMessage.toAssistantMessage(assistantText),\n stopReason: reason\n };\n }\n }\n }\n else\n {\n const res = await this.client.send(new ConverseCommand(converseCommandInput));\n const text = this.extractAssistantText(res, brMessages);\n\n const content = res?.output?.message?.content ?? [];\n\n if (res.stopReason === \"tool_use\")\n {\n const toolUses = content.filter((c: any) => c.toolUse)\n .map((c: any) => ({\n toolUseId: c.toolUse.toolUseId,\n name: c.toolUse.name,\n input: c.toolUse.input\n }));\n\n currentMessages.push(BedrockMessage.toToolUseMessage(text, toolUses));\n\n const toolResults = await this.executeTools(toolUses, options);\n\n currentMessages.push(BedrockMessage.toToolResultMessage(toolResults));\n\n continue;\n }\n\n const msg = BedrockMessage.toAssistantMessage(text);\n\n return {\n message: msg,\n stopReason: res.stopReason,\n usage: res.usage\n };\n }\n }\n }\n\n async invoke(messages: Message[], inference?: InferenceOptions, callback?: { onThinking: (s: string) => Promise<void>; }): Promise<ResponseMessage>\n {\n const response = await this.runConversationLoop(messages, inference, {\n streaming: false,\n onThinking: callback?.onThinking\n });\n\n messages.push(response.message);\n\n return new ResponseMessage({\n stopReason: response.stopReason as StopReason,\n tokenUsage: response.usage,\n message: response.message\n });\n }\n\n async stream(messages: Message[], inference?: InferenceOptions, callback?: { onThinking: (s: string) => Promise<void>; }): Promise<Readable>\n {\n const out = new PassThrough({ encoding: \"utf8\" });\n\n (async () =>\n {\n try\n {\n await this.runConversationLoop(messages, inference, {\n streaming: true,\n onChunk: c => out.write(c),\n onThinking: callback?.onThinking\n });\n\n out.write('');\n out.end();\n }\n catch (e)\n {\n out.emit(\"throw\", e);\n out.write('');\n out.end();\n }\n })();\n\n return out;\n }\n}","import { MessageType, Message, ToolUse, ToolResult } from '@mappies/miniwat-api-core'\n\nexport default class BedrockMessage extends Message\n{\n constructor(data:Partial<Message>)\n {\n super(data)\n }\n \n public static toAssistantMessage(text: string): Message\n {\n return new Message({\n type: MessageType.Agent,\n content: text,\n createdTimestamp: new Date().toISOString(),\n });\n }\n \n public static toToolUseMessage(text:string, toolUses: ToolUse[]): Message\n {\n return new Message({\n type: MessageType.Agent,\n content: text,\n createdTimestamp: new Date().toISOString(),\n toolUse: toolUses.map(({ toolUseId, name, input }) => (<ToolUse>{ toolUseId, name, input })),\n });\n }\n \n public static toToolResultMessage(toolResult: ToolResult[]): Message\n {\n return new Message({\n type: MessageType.ToolResult,\n toolResult\n });\n }\n}","import { MessageType, Message, Tool } from '@mappies/miniwat-api-core'\nimport { Message as BrMessage, StopReason, Tool as BedrockTool } from \"@aws-sdk/client-bedrock-runtime\";\n\nexport class BedrockAdapter\n{\n static toBedrockTool(tool: Tool): BedrockTool\n {\n return {\n toolSpec: {\n name: tool.name,\n description: tool.description,\n inputSchema: { json: tool.schema } as any\n }\n };\n }\n \n static toBedrockMessages(history: Message[]): BrMessage[]\n {\n return history\n .filter(m =>\n (m.content ?? \"\").trim().length > 0 ||\n (m.toolUse?.length ?? 0) > 0 ||\n (m.toolResult?.length ?? 0) > 0\n )\n .map(m =>\n {\n const role = m.type === MessageType.User || m.type === MessageType.ToolResult\n ? \"user\"\n : \"assistant\";\n\n const content: any[] = [];\n\n if ((m.content ?? \"\").trim().length > 0)\n {\n content.push({ text: m.content });\n }\n\n if (m.toolUse?.length)\n {\n content.push(...m.toolUse.map(t => ({ toolUse: t })));\n }\n\n if (m.toolResult?.length)\n {\n content.push(...m.toolResult.map(r => ({ toolResult: {...r, \"status\": r.status ? \"success\" : \"error\"} })));\n }\n\n return { role, content };\n });\n }\n\n static toToolResultContent(result: unknown): { text?: string; json?: object }[] \n {\n try \n {\n if(result instanceof Error)\n {\n return [{ text: result.message }];\n }\n\n if(result === null || result === undefined || result === '')\n {\n return [{ text: '' }];\n }\n\n if(typeof result === \"object\") \n {\n let json = JSON.parse(JSON.stringify(result));\n\n if(Array.isArray(json))\n {\n json = { result: json };\n }\n\n return [{ json }];\n }\n\n return [{ text: String(result) }];\n }\n catch (e: unknown) \n {\n const message = e instanceof Error ? e.message : String(e);\n\n return [{\n json: {\n error: \"Failed to serialize tool result\",\n message\n }\n }];\n }\n }\n\n static getResponseFromStopReasonWhenResponseIsEmpty(stopReason: StopReason): string\n {\n switch (stopReason)\n {\n case \"max_tokens\":\n return \"My response was cut off due to length limits. Would you like me to continue or summarize the remaining information?\";\n case \"stop_sequence\":\n return \"I've reached a natural stopping point in my response. Please let me know if you need additional information.\";\n case \"guardrail_intervened\":\n return \"I'm unable to provide a response to this request as it doesn't align with my usage guidelines. Please rephrase your question or ask something else.\";\n case \"content_filtered\":\n return \"I cannot generate a response for this request due to content policy restrictions. Please try rephrasing your question.\";\n case \"model_context_window_exceeded\":\n return \"The conversation or request is too long for me to process. Please try with a shorter input or start a new conversation.\";\n case \"end_turn\":\n return \"What can I help with?\";\n case \"tool_use\":\n return \"\";\n default:\n return \"I couldn't complete the response. Please try again.\";\n }\n }\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAsC;AACtC,IAAAA,2BAA6F;AAC7F,8BAA+B;;;ACF/B,8BAA0D;AAE1D,IAAqB,iBAArB,cAA4C,gCAC5C;AAAA,EACI,YAAY,MACZ;AACI,UAAM,IAAI;AAAA,EACd;AAAA,EAEA,OAAc,mBAAmB,MACjC;AACI,WAAO,IAAI,gCAAQ;AAAA,MACf,MAAM,oCAAY;AAAA,MAClB,SAAS;AAAA,MACT,mBAAkB,oBAAI,KAAK,GAAE,YAAY;AAAA,IAC7C,CAAC;AAAA,EACL;AAAA,EAEA,OAAc,iBAAiB,MAAa,UAC5C;AACI,WAAO,IAAI,gCAAQ;AAAA,MACX,MAAM,oCAAY;AAAA,MAClB,SAAS;AAAA,MACT,mBAAkB,oBAAI,KAAK,GAAE,YAAY;AAAA,MACzC,SAAS,SAAS,IAAI,CAAC,EAAE,WAAW,MAAM,MAAM,OAAgB,EAAE,WAAW,MAAM,MAAM,EAAE;AAAA,IACnG,CAAC;AAAA,EACL;AAAA,EAEA,OAAc,oBAAoB,YAClC;AACI,WAAO,IAAI,gCAAQ;AAAA,MACf,MAAM,oCAAY;AAAA,MAClB;AAAA,IACJ,CAAC;AAAA,EACL;AACJ;;;AD/BA,oCAA2I;;;AEJ3I,IAAAC,2BAA2C;AAGpC,IAAM,iBAAN,MACP;AAAA,EACI,OAAO,cAAc,MACrB;AACI,WAAO;AAAA,MACH,UAAU;AAAA,QACN,MAAM,KAAK;AAAA,QACX,aAAa,KAAK;AAAA,QAClB,aAAa,EAAE,MAAM,KAAK,OAAO;AAAA,MACrC;AAAA,IACJ;AAAA,EACJ;AAAA,EAEA,OAAO,kBAAkB,SACzB;AACI,WAA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,0BAAgB,KAAK,eAAe,oBAAoB,WAAW,CAAC;AAEpE;AAAA,QACJ;AAEA,cAAM,MAAM,eAAe,mBAAmB,IAAI;AAElD,eAAO;AAAA,UACH,SAAS;AAAA,UACT,YAAY,IAAI;AAAA,UAChB,OAAO,IAAI;AAAA,QACf;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AAAA,EAEA,MAAM,OAAO,UAAqB,WAA8B,UAChE;AACI,UAAM,WAAW,MAAM,KAAK,oBAAoB,UAAU,WAAW;AAAA,MACjE,WAAW;AAAA,MACX,YAAY,UAAU;AAAA,IAC1B,CAAC;AAED,aAAS,KAAK,SAAS,OAAO;AAE9B,WAAO,IAAI,yCAAgB;AAAA,MACvB,YAAY,SAAS;AAAA,MACrB,YAAY,SAAS;AAAA,MACrB,SAAS,SAAS;AAAA,IACtB,CAAC;AAAA,EACL;AAAA,EAEA,MAAM,OAAO,UAAqB,WAA8B,UAChE;AACI,UAAM,MAAM,IAAI,0BAAY,EAAE,UAAU,OAAO,CAAC;AAEhD,KAAC,YACD;AACI,UACA;AACI,cAAM,KAAK,oBAAoB,UAAU,WAAW;AAAA,UAChD,WAAW;AAAA,UACX,SAAS,OAAK,IAAI,MAAM,CAAC;AAAA,UACzB,YAAY,UAAU;AAAA,QAC1B,CAAC;AAED,YAAI,MAAM,EAAE;AACZ,YAAI,IAAI;AAAA,MACZ,SACO,GACP;AACI,YAAI,KAAK,SAAS,CAAC;AACnB,YAAI,MAAM,EAAE;AACZ,YAAI,IAAI;AAAA,MACZ;AAAA,IACJ,GAAG;AAEH,WAAO;AAAA,EACX;AACJ;","names":["import_miniwat_api_core","import_miniwat_api_core"]}
@@ -0,0 +1,28 @@
1
+ import { Readable } from 'stream';
2
+ import { LlmProvider, Message, InferenceOptions, ResponseMessage } from '@mappies/miniwat-api-core';
3
+ import { BedrockRuntimeClientConfig } from '@aws-sdk/client-bedrock-runtime';
4
+
5
+ declare class BedrockLlmProvider implements LlmProvider {
6
+ private modelId;
7
+ private systemPrompt?;
8
+ private toolDispatcher;
9
+ private client;
10
+ constructor(options: {
11
+ region?: string;
12
+ modelId: string;
13
+ systemPrompt?: string;
14
+ clientConfig?: BedrockRuntimeClientConfig;
15
+ });
16
+ private streamBufferedText;
17
+ private extractAssistantText;
18
+ private executeTools;
19
+ private runConversationLoop;
20
+ invoke(messages: Message[], inference?: InferenceOptions, callback?: {
21
+ onThinking: (s: string) => Promise<void>;
22
+ }): Promise<ResponseMessage>;
23
+ stream(messages: Message[], inference?: InferenceOptions, callback?: {
24
+ onThinking: (s: string) => Promise<void>;
25
+ }): Promise<Readable>;
26
+ }
27
+
28
+ export { BedrockLlmProvider };
@@ -0,0 +1,28 @@
1
+ import { Readable } from 'stream';
2
+ import { LlmProvider, Message, InferenceOptions, ResponseMessage } from '@mappies/miniwat-api-core';
3
+ import { BedrockRuntimeClientConfig } from '@aws-sdk/client-bedrock-runtime';
4
+
5
+ declare class BedrockLlmProvider implements LlmProvider {
6
+ private modelId;
7
+ private systemPrompt?;
8
+ private toolDispatcher;
9
+ private client;
10
+ constructor(options: {
11
+ region?: string;
12
+ modelId: string;
13
+ systemPrompt?: string;
14
+ clientConfig?: BedrockRuntimeClientConfig;
15
+ });
16
+ private streamBufferedText;
17
+ private extractAssistantText;
18
+ private executeTools;
19
+ private runConversationLoop;
20
+ invoke(messages: Message[], inference?: InferenceOptions, callback?: {
21
+ onThinking: (s: string) => Promise<void>;
22
+ }): Promise<ResponseMessage>;
23
+ stream(messages: Message[], inference?: InferenceOptions, callback?: {
24
+ onThinking: (s: string) => Promise<void>;
25
+ }): Promise<Readable>;
26
+ }
27
+
28
+ export { BedrockLlmProvider };
package/dist/index.js ADDED
@@ -0,0 +1,316 @@
1
+ // src/bedrockLlmProvider.ts
2
+ import { PassThrough } from "stream";
3
+ import { ResponseMessage } from "@mappies/miniwat-api-core";
4
+ import { ToolDispatcher } from "@mappies/miniwat-mcp-core";
5
+
6
+ // src/bedrockMessage.ts
7
+ import { MessageType, Message } from "@mappies/miniwat-api-core";
8
+ var BedrockMessage = class extends Message {
9
+ constructor(data) {
10
+ super(data);
11
+ }
12
+ static toAssistantMessage(text) {
13
+ return new Message({
14
+ type: MessageType.Agent,
15
+ content: text,
16
+ createdTimestamp: (/* @__PURE__ */ new Date()).toISOString()
17
+ });
18
+ }
19
+ static toToolUseMessage(text, toolUses) {
20
+ return new Message({
21
+ type: MessageType.Agent,
22
+ content: text,
23
+ createdTimestamp: (/* @__PURE__ */ new Date()).toISOString(),
24
+ toolUse: toolUses.map(({ toolUseId, name, input }) => ({ toolUseId, name, input }))
25
+ });
26
+ }
27
+ static toToolResultMessage(toolResult) {
28
+ return new Message({
29
+ type: MessageType.ToolResult,
30
+ toolResult
31
+ });
32
+ }
33
+ };
34
+
35
+ // src/bedrockLlmProvider.ts
36
+ import { BedrockRuntimeClient, ConverseCommand, ConverseStreamCommand } from "@aws-sdk/client-bedrock-runtime";
37
+
38
+ // src/bedrockAdapter.ts
39
+ import { MessageType as MessageType2 } from "@mappies/miniwat-api-core";
40
+ var BedrockAdapter = class {
41
+ static toBedrockTool(tool) {
42
+ return {
43
+ toolSpec: {
44
+ name: tool.name,
45
+ description: tool.description,
46
+ inputSchema: { json: tool.schema }
47
+ }
48
+ };
49
+ }
50
+ static toBedrockMessages(history) {
51
+ return history.filter(
52
+ (m) => (m.content ?? "").trim().length > 0 || (m.toolUse?.length ?? 0) > 0 || (m.toolResult?.length ?? 0) > 0
53
+ ).map((m) => {
54
+ const role = m.type === MessageType2.User || m.type === MessageType2.ToolResult ? "user" : "assistant";
55
+ const content = [];
56
+ if ((m.content ?? "").trim().length > 0) {
57
+ content.push({ text: m.content });
58
+ }
59
+ if (m.toolUse?.length) {
60
+ content.push(...m.toolUse.map((t) => ({ toolUse: t })));
61
+ }
62
+ if (m.toolResult?.length) {
63
+ content.push(...m.toolResult.map((r) => ({ toolResult: { ...r, "status": r.status ? "success" : "error" } })));
64
+ }
65
+ return { role, content };
66
+ });
67
+ }
68
+ static toToolResultContent(result) {
69
+ try {
70
+ if (result instanceof Error) {
71
+ return [{ text: result.message }];
72
+ }
73
+ if (result === null || result === void 0 || result === "") {
74
+ return [{ text: "" }];
75
+ }
76
+ if (typeof result === "object") {
77
+ let json = JSON.parse(JSON.stringify(result));
78
+ if (Array.isArray(json)) {
79
+ json = { result: json };
80
+ }
81
+ return [{ json }];
82
+ }
83
+ return [{ text: String(result) }];
84
+ } catch (e) {
85
+ const message = e instanceof Error ? e.message : String(e);
86
+ return [{
87
+ json: {
88
+ error: "Failed to serialize tool result",
89
+ message
90
+ }
91
+ }];
92
+ }
93
+ }
94
+ static getResponseFromStopReasonWhenResponseIsEmpty(stopReason) {
95
+ switch (stopReason) {
96
+ case "max_tokens":
97
+ return "My response was cut off due to length limits. Would you like me to continue or summarize the remaining information?";
98
+ case "stop_sequence":
99
+ return "I've reached a natural stopping point in my response. Please let me know if you need additional information.";
100
+ case "guardrail_intervened":
101
+ return "I'm unable to provide a response to this request as it doesn't align with my usage guidelines. Please rephrase your question or ask something else.";
102
+ case "content_filtered":
103
+ return "I cannot generate a response for this request due to content policy restrictions. Please try rephrasing your question.";
104
+ case "model_context_window_exceeded":
105
+ return "The conversation or request is too long for me to process. Please try with a shorter input or start a new conversation.";
106
+ case "end_turn":
107
+ return "What can I help with?";
108
+ case "tool_use":
109
+ return "";
110
+ default:
111
+ return "I couldn't complete the response. Please try again.";
112
+ }
113
+ }
114
+ };
115
+
116
+ // src/bedrockLlmProvider.ts
117
+ var BedrockLlmProvider = class {
118
+ constructor(options) {
119
+ this.toolDispatcher = new ToolDispatcher();
120
+ this.modelId = options.modelId;
121
+ this.systemPrompt = options?.systemPrompt;
122
+ this.client = new BedrockRuntimeClient({
123
+ region: options?.region ?? process.env.DEFAULT_AWS_REGION ?? process.env.AWS_REGION,
124
+ ...options?.clientConfig
125
+ });
126
+ }
127
+ async streamBufferedText(chunks, write) {
128
+ for (const chunk of chunks) {
129
+ write(chunk);
130
+ await new Promise((r) => setTimeout(r, 8 + Math.random() * 12));
131
+ }
132
+ }
133
+ extractAssistantText(res, messages) {
134
+ const content = res?.output?.message?.content ?? [];
135
+ let text = content.map((c) => c.text ?? "").join("");
136
+ if (!text) {
137
+ text = BedrockAdapter.getResponseFromStopReasonWhenResponseIsEmpty(res.stopReason);
138
+ }
139
+ return text;
140
+ }
141
+ async executeTools(toolUses, opts) {
142
+ const results = [];
143
+ for (const toolUse of toolUses) {
144
+ try {
145
+ const resultPromise = this.toolDispatcher.execute(toolUse.name, toolUse.input);
146
+ if (opts?.onThinking && toolUse.name) {
147
+ const inputText = await this.toolDispatcher.getInputText(toolUse.name, toolUse.input);
148
+ if (inputText) opts.onThinking(inputText);
149
+ }
150
+ const result = await resultPromise;
151
+ results.push({
152
+ toolUseId: toolUse.toolUseId,
153
+ content: BedrockAdapter.toToolResultContent(result),
154
+ status: true
155
+ });
156
+ } catch (err) {
157
+ results.push({
158
+ toolUseId: toolUse.toolUseId,
159
+ content: BedrockAdapter.toToolResultContent(err),
160
+ status: false
161
+ });
162
+ }
163
+ }
164
+ return results;
165
+ }
166
+ async runConversationLoop(messages, inference, options) {
167
+ let currentMessages = [...messages];
168
+ let iterations = 0;
169
+ while (true) {
170
+ if (++iterations > 20) throw new Error("Too many tool iterations");
171
+ const brMessages = BedrockAdapter.toBedrockMessages(currentMessages);
172
+ const converseCommandInput = {
173
+ modelId: this.modelId,
174
+ messages: brMessages,
175
+ system: this.systemPrompt ? [{ text: this.systemPrompt }] : void 0,
176
+ inferenceConfig: {
177
+ temperature: inference?.temperature ?? 0.2,
178
+ maxTokens: inference?.maxTokens,
179
+ topP: inference?.topP
180
+ },
181
+ toolConfig: {
182
+ tools: (await this.toolDispatcher.list()).map(BedrockAdapter.toBedrockTool)
183
+ }
184
+ };
185
+ if (options?.streaming) {
186
+ const res = await this.client.send(new ConverseStreamCommand(converseCommandInput));
187
+ let chunks = [];
188
+ let streamingStarted = false;
189
+ let toolUses = [];
190
+ let currentToolUse;
191
+ let assistantText = "";
192
+ for await (const event of res.stream ?? []) {
193
+ if (event.modelStreamErrorException) {
194
+ throw new Error(event.modelStreamErrorException?.message ?? "Model stream error");
195
+ }
196
+ if (event.contentBlockDelta?.delta?.text) {
197
+ const chunk = event.contentBlockDelta.delta.text;
198
+ assistantText += chunk;
199
+ if (!streamingStarted) {
200
+ chunks.push(chunk);
201
+ if (chunks.length > 20) {
202
+ streamingStarted = true;
203
+ await this.streamBufferedText(chunks, options.onChunk);
204
+ chunks = [];
205
+ }
206
+ } else {
207
+ options.onChunk?.(chunk);
208
+ }
209
+ }
210
+ if (event.contentBlockStart?.start?.toolUse) {
211
+ currentToolUse = {
212
+ toolUseId: event.contentBlockStart.start.toolUse.toolUseId,
213
+ name: event.contentBlockStart.start.toolUse.name,
214
+ inputBuffer: ""
215
+ };
216
+ toolUses.push(currentToolUse);
217
+ }
218
+ if (currentToolUse && event.contentBlockDelta?.delta?.toolUse?.input) {
219
+ currentToolUse.inputBuffer += event.contentBlockDelta.delta.toolUse.input;
220
+ }
221
+ if (event.contentBlockStop && currentToolUse?.inputBuffer) {
222
+ currentToolUse.input = JSON.parse(currentToolUse.inputBuffer);
223
+ delete currentToolUse.inputBuffer;
224
+ currentToolUse = void 0;
225
+ }
226
+ if (event.messageStop) {
227
+ const reason = event.messageStop.stopReason;
228
+ if (reason === "tool_use") {
229
+ if (!streamingStarted && chunks.length > 0) {
230
+ options.onThinking?.(chunks.join(""));
231
+ chunks = [];
232
+ }
233
+ currentMessages.push(BedrockMessage.toToolUseMessage(assistantText, toolUses));
234
+ const toolResults = await this.executeTools(toolUses, options);
235
+ currentMessages.push(BedrockMessage.toToolResultMessage(toolResults));
236
+ break;
237
+ }
238
+ if (typeof reason === "string") {
239
+ if (/content|filter|guardrail/i.test(reason)) {
240
+ throw new Error("Output blocked by content filtering policy: " + reason);
241
+ } else if (reason === "max_tokens") {
242
+ options.onChunk?.(BedrockAdapter.getResponseFromStopReasonWhenResponseIsEmpty(reason));
243
+ } else if (!streamingStarted) {
244
+ if (chunks.length > 0) {
245
+ await this.streamBufferedText(chunks, options.onChunk);
246
+ } else {
247
+ options.onChunk?.(BedrockAdapter.getResponseFromStopReasonWhenResponseIsEmpty(reason));
248
+ }
249
+ }
250
+ }
251
+ return {
252
+ message: BedrockMessage.toAssistantMessage(assistantText),
253
+ stopReason: reason
254
+ };
255
+ }
256
+ }
257
+ } else {
258
+ const res = await this.client.send(new ConverseCommand(converseCommandInput));
259
+ const text = this.extractAssistantText(res, brMessages);
260
+ const content = res?.output?.message?.content ?? [];
261
+ if (res.stopReason === "tool_use") {
262
+ const toolUses = content.filter((c) => c.toolUse).map((c) => ({
263
+ toolUseId: c.toolUse.toolUseId,
264
+ name: c.toolUse.name,
265
+ input: c.toolUse.input
266
+ }));
267
+ currentMessages.push(BedrockMessage.toToolUseMessage(text, toolUses));
268
+ const toolResults = await this.executeTools(toolUses, options);
269
+ currentMessages.push(BedrockMessage.toToolResultMessage(toolResults));
270
+ continue;
271
+ }
272
+ const msg = BedrockMessage.toAssistantMessage(text);
273
+ return {
274
+ message: msg,
275
+ stopReason: res.stopReason,
276
+ usage: res.usage
277
+ };
278
+ }
279
+ }
280
+ }
281
+ async invoke(messages, inference, callback) {
282
+ const response = await this.runConversationLoop(messages, inference, {
283
+ streaming: false,
284
+ onThinking: callback?.onThinking
285
+ });
286
+ messages.push(response.message);
287
+ return new ResponseMessage({
288
+ stopReason: response.stopReason,
289
+ tokenUsage: response.usage,
290
+ message: response.message
291
+ });
292
+ }
293
+ async stream(messages, inference, callback) {
294
+ const out = new PassThrough({ encoding: "utf8" });
295
+ (async () => {
296
+ try {
297
+ await this.runConversationLoop(messages, inference, {
298
+ streaming: true,
299
+ onChunk: (c) => out.write(c),
300
+ onThinking: callback?.onThinking
301
+ });
302
+ out.write("");
303
+ out.end();
304
+ } catch (e) {
305
+ out.emit("throw", e);
306
+ out.write("");
307
+ out.end();
308
+ }
309
+ })();
310
+ return out;
311
+ }
312
+ };
313
+ export {
314
+ BedrockLlmProvider
315
+ };
316
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/bedrockLlmProvider.ts","../src/bedrockMessage.ts","../src/bedrockAdapter.ts"],"sourcesContent":["import { PassThrough, Readable } from \"stream\";\nimport { ResponseMessage, Message, LlmProvider, InferenceOptions, ToolUse, ToolResult } from '@mappies/miniwat-api-core'\nimport { ToolDispatcher } from '@mappies/miniwat-mcp-core'\nimport BedrockMessage from \"./bedrockMessage\";\nimport { BedrockRuntimeClient, ConverseCommand, ConverseStreamCommand, Message as BrMessage, StopReason, BedrockRuntimeClientConfig } from \"@aws-sdk/client-bedrock-runtime\";\nimport { BedrockAdapter } from \"./bedrockAdapter\";\n\nexport class BedrockLlmProvider implements LlmProvider\n{\n private modelId: string;\n private systemPrompt?: string;\n private toolDispatcher: ToolDispatcher;\n private client: BedrockRuntimeClient;\n\n constructor(options: { region?: string; modelId: string; systemPrompt?: string; clientConfig?: BedrockRuntimeClientConfig; })\n {\n this.toolDispatcher = new ToolDispatcher();\n this.modelId = options.modelId;\n this.systemPrompt = options?.systemPrompt;\n\n this.client = new BedrockRuntimeClient({\n region: options?.region ?? process.env.DEFAULT_AWS_REGION ?? process.env.AWS_REGION,\n ...options?.clientConfig\n });\n }\n\n private async streamBufferedText(chunks: string[], write: (chunk: string) => void)\n {\n for(const chunk of chunks)\n {\n write(chunk);\n await new Promise(r => setTimeout(r, 8 + Math.random() * 12));\n }\n }\n\n private extractAssistantText(res: any, messages: BrMessage[]): string\n {\n const content = res?.output?.message?.content ?? [];\n let text = content.map((c: any) => c.text ?? \"\").join(\"\");\n\n if(!text)\n {\n text = BedrockAdapter.getResponseFromStopReasonWhenResponseIsEmpty(res.stopReason);\n }\n\n return text;\n }\n\n private async executeTools(toolUses: ToolUse[], opts?: any): Promise<ToolResult[]>\n {\n const results: ToolResult[] = [];\n\n for(const toolUse of toolUses)\n {\n try\n {\n const resultPromise = this.toolDispatcher.execute(toolUse.name, toolUse.input);\n\n // Show a thinking message for the tool\n if (opts?.onThinking && toolUse.name)\n {\n const inputText = await this.toolDispatcher.getInputText(toolUse.name, toolUse.input);\n if(inputText) opts.onThinking(inputText);\n }\n\n const result = await resultPromise;\n\n results.push({\n toolUseId: toolUse.toolUseId,\n content: BedrockAdapter.toToolResultContent(result),\n status: true\n });\n }\n catch(err: any)\n {\n results.push({\n toolUseId: toolUse.toolUseId,\n content: BedrockAdapter.toToolResultContent(err),\n status: false\n });\n }\n }\n\n return results;\n }\n\n private async runConversationLoop(messages: Message[], inference?: InferenceOptions, options?: { streaming?: boolean; onChunk?: (chunk: string) => void; onThinking?: (msg: string) => Promise<void>;}): Promise<{ message: Message; stopReason?: StopReason; usage?: any }>\n {\n let currentMessages = [...messages];\n let iterations = 0;\n\n while(true)\n {\n if (++iterations > 20) throw new Error(\"Too many tool iterations\");\n\n const brMessages = BedrockAdapter.toBedrockMessages(currentMessages);\n\n const converseCommandInput = {\n modelId: this.modelId,\n messages: brMessages,\n system: this.systemPrompt ? [{ text: this.systemPrompt }] : undefined,\n inferenceConfig: {\n temperature: inference?.temperature ?? 0.2,\n maxTokens: inference?.maxTokens,\n topP: inference?.topP\n },\n toolConfig: {\n tools: (await this.toolDispatcher.list()).map(BedrockAdapter.toBedrockTool)\n }\n }\n\n if (options?.streaming)\n {\n const res = await this.client.send(new ConverseStreamCommand(converseCommandInput));\n\n let chunks: string[] = [];\n let streamingStarted = false;\n let toolUses: ToolUse[] = [];\n let currentToolUse: ToolUse | undefined;\n let assistantText = '';\n\n for await (const event of res.stream ?? [])\n {\n\n if (event.modelStreamErrorException)\n {\n throw new Error(event.modelStreamErrorException?.message ?? \"Model stream error\");\n }\n\n if (event.contentBlockDelta?.delta?.text)\n {\n const chunk = event.contentBlockDelta.delta.text;\n assistantText += chunk;\n\n if (!streamingStarted)\n {\n chunks.push(chunk);\n\n if (chunks.length > 20)\n {\n streamingStarted = true;\n await this.streamBufferedText(chunks, options.onChunk!);\n chunks = [];\n }\n }\n else\n {\n options.onChunk?.(chunk);\n }\n }\n\n if (event.contentBlockStart?.start?.toolUse)\n {\n currentToolUse = {\n toolUseId: event.contentBlockStart.start.toolUse.toolUseId as string,\n name: event.contentBlockStart.start.toolUse.name as string,\n inputBuffer: \"\"\n };\n toolUses.push(currentToolUse);\n }\n\n if (currentToolUse && event.contentBlockDelta?.delta?.toolUse?.input)\n {\n currentToolUse.inputBuffer += event.contentBlockDelta.delta.toolUse.input;\n }\n\n if (event.contentBlockStop && currentToolUse?.inputBuffer)\n {\n currentToolUse.input = JSON.parse(currentToolUse.inputBuffer);\n delete currentToolUse.inputBuffer;\n currentToolUse = undefined;\n }\n\n if (event.messageStop)\n {\n const reason = event.messageStop.stopReason;\n\n if (reason === \"tool_use\")\n {\n\n if (!streamingStarted && chunks.length > 0)\n {\n options.onThinking?.(chunks.join(\"\"));\n chunks = [];\n }\n\n currentMessages.push(BedrockMessage.toToolUseMessage(assistantText, toolUses));\n\n const toolResults = await this.executeTools(toolUses, options);\n\n currentMessages.push(BedrockMessage.toToolResultMessage(toolResults));\n\n break;\n }\n\n if (typeof reason === \"string\")\n {\n if (/content|filter|guardrail/i.test(reason))\n {\n throw new Error(\"Output blocked by content filtering policy: \" + reason);\n }\n else if (reason === \"max_tokens\")\n {\n options.onChunk?.(BedrockAdapter.getResponseFromStopReasonWhenResponseIsEmpty(reason));\n }\n else if (!streamingStarted)\n {\n if (chunks.length > 0)\n {\n await this.streamBufferedText(chunks, options.onChunk!);\n } else\n {\n options.onChunk?.(BedrockAdapter.getResponseFromStopReasonWhenResponseIsEmpty(reason));\n }\n }\n }\n\n return {\n message: BedrockMessage.toAssistantMessage(assistantText),\n stopReason: reason\n };\n }\n }\n }\n else\n {\n const res = await this.client.send(new ConverseCommand(converseCommandInput));\n const text = this.extractAssistantText(res, brMessages);\n\n const content = res?.output?.message?.content ?? [];\n\n if (res.stopReason === \"tool_use\")\n {\n const toolUses = content.filter((c: any) => c.toolUse)\n .map((c: any) => ({\n toolUseId: c.toolUse.toolUseId,\n name: c.toolUse.name,\n input: c.toolUse.input\n }));\n\n currentMessages.push(BedrockMessage.toToolUseMessage(text, toolUses));\n\n const toolResults = await this.executeTools(toolUses, options);\n\n currentMessages.push(BedrockMessage.toToolResultMessage(toolResults));\n\n continue;\n }\n\n const msg = BedrockMessage.toAssistantMessage(text);\n\n return {\n message: msg,\n stopReason: res.stopReason,\n usage: res.usage\n };\n }\n }\n }\n\n async invoke(messages: Message[], inference?: InferenceOptions, callback?: { onThinking: (s: string) => Promise<void>; }): Promise<ResponseMessage>\n {\n const response = await this.runConversationLoop(messages, inference, {\n streaming: false,\n onThinking: callback?.onThinking\n });\n\n messages.push(response.message);\n\n return new ResponseMessage({\n stopReason: response.stopReason as StopReason,\n tokenUsage: response.usage,\n message: response.message\n });\n }\n\n async stream(messages: Message[], inference?: InferenceOptions, callback?: { onThinking: (s: string) => Promise<void>; }): Promise<Readable>\n {\n const out = new PassThrough({ encoding: \"utf8\" });\n\n (async () =>\n {\n try\n {\n await this.runConversationLoop(messages, inference, {\n streaming: true,\n onChunk: c => out.write(c),\n onThinking: callback?.onThinking\n });\n\n out.write('');\n out.end();\n }\n catch (e)\n {\n out.emit(\"throw\", e);\n out.write('');\n out.end();\n }\n })();\n\n return out;\n }\n}","import { MessageType, Message, ToolUse, ToolResult } from '@mappies/miniwat-api-core'\n\nexport default class BedrockMessage extends Message\n{\n constructor(data:Partial<Message>)\n {\n super(data)\n }\n \n public static toAssistantMessage(text: string): Message\n {\n return new Message({\n type: MessageType.Agent,\n content: text,\n createdTimestamp: new Date().toISOString(),\n });\n }\n \n public static toToolUseMessage(text:string, toolUses: ToolUse[]): Message\n {\n return new Message({\n type: MessageType.Agent,\n content: text,\n createdTimestamp: new Date().toISOString(),\n toolUse: toolUses.map(({ toolUseId, name, input }) => (<ToolUse>{ toolUseId, name, input })),\n });\n }\n \n public static toToolResultMessage(toolResult: ToolResult[]): Message\n {\n return new Message({\n type: MessageType.ToolResult,\n toolResult\n });\n }\n}","import { MessageType, Message, Tool } from '@mappies/miniwat-api-core'\nimport { Message as BrMessage, StopReason, Tool as BedrockTool } from \"@aws-sdk/client-bedrock-runtime\";\n\nexport class BedrockAdapter\n{\n static toBedrockTool(tool: Tool): BedrockTool\n {\n return {\n toolSpec: {\n name: tool.name,\n description: tool.description,\n inputSchema: { json: tool.schema } as any\n }\n };\n }\n \n static toBedrockMessages(history: Message[]): BrMessage[]\n {\n return history\n .filter(m =>\n (m.content ?? \"\").trim().length > 0 ||\n (m.toolUse?.length ?? 0) > 0 ||\n (m.toolResult?.length ?? 0) > 0\n )\n .map(m =>\n {\n const role = m.type === MessageType.User || m.type === MessageType.ToolResult\n ? \"user\"\n : \"assistant\";\n\n const content: any[] = [];\n\n if ((m.content ?? \"\").trim().length > 0)\n {\n content.push({ text: m.content });\n }\n\n if (m.toolUse?.length)\n {\n content.push(...m.toolUse.map(t => ({ toolUse: t })));\n }\n\n if (m.toolResult?.length)\n {\n content.push(...m.toolResult.map(r => ({ toolResult: {...r, \"status\": r.status ? \"success\" : \"error\"} })));\n }\n\n return { role, content };\n });\n }\n\n static toToolResultContent(result: unknown): { text?: string; json?: object }[] \n {\n try \n {\n if(result instanceof Error)\n {\n return [{ text: result.message }];\n }\n\n if(result === null || result === undefined || result === '')\n {\n return [{ text: '' }];\n }\n\n if(typeof result === \"object\") \n {\n let json = JSON.parse(JSON.stringify(result));\n\n if(Array.isArray(json))\n {\n json = { result: json };\n }\n\n return [{ json }];\n }\n\n return [{ text: String(result) }];\n }\n catch (e: unknown) \n {\n const message = e instanceof Error ? e.message : String(e);\n\n return [{\n json: {\n error: \"Failed to serialize tool result\",\n message\n }\n }];\n }\n }\n\n static getResponseFromStopReasonWhenResponseIsEmpty(stopReason: StopReason): string\n {\n switch (stopReason)\n {\n case \"max_tokens\":\n return \"My response was cut off due to length limits. Would you like me to continue or summarize the remaining information?\";\n case \"stop_sequence\":\n return \"I've reached a natural stopping point in my response. Please let me know if you need additional information.\";\n case \"guardrail_intervened\":\n return \"I'm unable to provide a response to this request as it doesn't align with my usage guidelines. Please rephrase your question or ask something else.\";\n case \"content_filtered\":\n return \"I cannot generate a response for this request due to content policy restrictions. Please try rephrasing your question.\";\n case \"model_context_window_exceeded\":\n return \"The conversation or request is too long for me to process. Please try with a shorter input or start a new conversation.\";\n case \"end_turn\":\n return \"What can I help with?\";\n case \"tool_use\":\n return \"\";\n default:\n return \"I couldn't complete the response. Please try again.\";\n }\n }\n}"],"mappings":";AAAA,SAAS,mBAA6B;AACtC,SAAS,uBAAoF;AAC7F,SAAS,sBAAsB;;;ACF/B,SAAS,aAAa,eAAoC;AAE1D,IAAqB,iBAArB,cAA4C,QAC5C;AAAA,EACI,YAAY,MACZ;AACI,UAAM,IAAI;AAAA,EACd;AAAA,EAEA,OAAc,mBAAmB,MACjC;AACI,WAAO,IAAI,QAAQ;AAAA,MACf,MAAM,YAAY;AAAA,MAClB,SAAS;AAAA,MACT,mBAAkB,oBAAI,KAAK,GAAE,YAAY;AAAA,IAC7C,CAAC;AAAA,EACL;AAAA,EAEA,OAAc,iBAAiB,MAAa,UAC5C;AACI,WAAO,IAAI,QAAQ;AAAA,MACX,MAAM,YAAY;AAAA,MAClB,SAAS;AAAA,MACT,mBAAkB,oBAAI,KAAK,GAAE,YAAY;AAAA,MACzC,SAAS,SAAS,IAAI,CAAC,EAAE,WAAW,MAAM,MAAM,OAAgB,EAAE,WAAW,MAAM,MAAM,EAAE;AAAA,IACnG,CAAC;AAAA,EACL;AAAA,EAEA,OAAc,oBAAoB,YAClC;AACI,WAAO,IAAI,QAAQ;AAAA,MACf,MAAM,YAAY;AAAA,MAClB;AAAA,IACJ,CAAC;AAAA,EACL;AACJ;;;AD/BA,SAAS,sBAAsB,iBAAiB,6BAA2F;;;AEJ3I,SAAS,eAAAA,oBAAkC;AAGpC,IAAM,iBAAN,MACP;AAAA,EACI,OAAO,cAAc,MACrB;AACI,WAAO;AAAA,MACH,UAAU;AAAA,QACN,MAAM,KAAK;AAAA,QACX,aAA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UACrB,EAAE;AAE1B,0BAAgB,KAAK,eAAe,iBAAiB,MAAM,QAAQ,CAAC;AAEpE,gBAAM,cAAc,MAAM,KAAK,aAAa,UAAU,OAAO;AAE7D,0BAAgB,KAAK,eAAe,oBAAoB,WAAW,CAAC;AAEpE;AAAA,QACJ;AAEA,cAAM,MAAM,eAAe,mBAAmB,IAAI;AAElD,eAAO;AAAA,UACH,SAAS;AAAA,UACT,YAAY,IAAI;AAAA,UAChB,OAAO,IAAI;AAAA,QACf;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AAAA,EAEA,MAAM,OAAO,UAAqB,WAA8B,UAChE;AACI,UAAM,WAAW,MAAM,KAAK,oBAAoB,UAAU,WAAW;AAAA,MACjE,WAAW;AAAA,MACX,YAAY,UAAU;AAAA,IAC1B,CAAC;AAED,aAAS,KAAK,SAAS,OAAO;AAE9B,WAAO,IAAI,gBAAgB;AAAA,MACvB,YAAY,SAAS;AAAA,MACrB,YAAY,SAAS;AAAA,MACrB,SAAS,SAAS;AAAA,IACtB,CAAC;AAAA,EACL;AAAA,EAEA,MAAM,OAAO,UAAqB,WAA8B,UAChE;AACI,UAAM,MAAM,IAAI,YAAY,EAAE,UAAU,OAAO,CAAC;AAEhD,KAAC,YACD;AACI,UACA;AACI,cAAM,KAAK,oBAAoB,UAAU,WAAW;AAAA,UAChD,WAAW;AAAA,UACX,SAAS,OAAK,IAAI,MAAM,CAAC;AAAA,UACzB,YAAY,UAAU;AAAA,QAC1B,CAAC;AAED,YAAI,MAAM,EAAE;AACZ,YAAI,IAAI;AAAA,MACZ,SACO,GACP;AACI,YAAI,KAAK,SAAS,CAAC;AACnB,YAAI,MAAM,EAAE;AACZ,YAAI,IAAI;AAAA,MACZ;AAAA,IACJ,GAAG;AAEH,WAAO;AAAA,EACX;AACJ;","names":["MessageType"]}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@mappies/miniwat-provider-bedrock",
3
+ "version": "0.0.0",
4
+ "description": "Bedrock provider for Miniwat",
5
+ "homepage": "https://github.com/mappies/miniwat-provider-bedrock#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/mappies/miniwat-provider-bedrock/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/mappies/miniwat-provider-bedrock.git"
12
+ },
13
+ "license": "PolyForm-Noncommercial-1.0.0",
14
+ "author": "Nithiwat Kampanya",
15
+ "scripts": {
16
+ "test": "vitest run",
17
+ "test-watch": "vitest",
18
+ "build": "tsup"
19
+ },
20
+ "type": "module",
21
+ "main": "./dist/index.cjs",
22
+ "module": "./dist/index.js",
23
+ "types": "./dist/index.d.ts",
24
+ "exports": {
25
+ ".": {
26
+ "import": {
27
+ "types": "./dist/index.d.ts",
28
+ "default": "./dist/index.js"
29
+ },
30
+ "require": {
31
+ "types": "./dist/index.d.cts",
32
+ "default": "./dist/index.cjs"
33
+ }
34
+ }
35
+ },
36
+ "files": [
37
+ "dist"
38
+ ],
39
+ "devDependencies": {
40
+ "@types/node": "^25.6.0",
41
+ "tsup": "^8.5.1",
42
+ "typescript": "^5.9.3",
43
+ "vitest": "^4.1.0"
44
+ },
45
+ "dependencies": {
46
+ "@aws-sdk/client-bedrock-runtime": "^3.908.0",
47
+ "@mappies/miniwat-api-core": "^0.3.0",
48
+ "@mappies/miniwat-mcp-core": "^0.2.0"
49
+ }
50
+ }