pi-bedrock-compat 0.0.0-stage → 0.1.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/CHANGELOG.md +8 -0
- package/LICENSE +21 -0
- package/README.md +65 -2
- package/package.json +55 -4
- package/src/index.ts +28 -0
- package/src/payload.ts +98 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
- Publish the package as `pi-bedrock-compat` so future Bedrock compatibility fixes can live in the same extension.
|
|
6
|
+
- Hoist images out of Bedrock Converse tool results for models other than Anthropic Claude and Amazon Nova.
|
|
7
|
+
- Preserve tool-result text, status, ordering, and image-only results.
|
|
8
|
+
- Repair outgoing payloads without modifying stored Pi session history.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jillian Kozyra
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,66 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pi-bedrock-compat
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A [Pi](https://pi.dev) extension that applies compatibility fixes for models accessed through Amazon Bedrock Converse.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Pi normally places images returned by tools inside `toolResult.content`. Amazon Bedrock supports that shape for Anthropic Claude and Amazon Nova, but other image-capable models can reject it with a validation error.
|
|
8
|
+
|
|
9
|
+
For affected models, this extension moves each image beside its tool result in the outgoing user message. The request remains valid for Bedrock, vision input is preserved, and Pi's stored session history is not modified.
|
|
10
|
+
|
|
11
|
+
The rewrite runs only when the selected provider is `amazon-bedrock`, the API is `bedrock-converse-stream`, and the model is not identified as Claude or Nova. It supports system and application inference profiles, and unknown or malformed payloads pass through unchanged.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
After the package is published, install it from npm:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pi install npm:pi-bedrock-compat
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Alternatively, install it directly from GitHub:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pi install git:github.com/jekozyra/pi-bedrock-compat
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Restart Pi after installation, or run `/reload` in an active session.
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
No configuration is required. Once installed, the extension automatically changes affected Bedrock Converse requests from this shape:
|
|
32
|
+
|
|
33
|
+
```text
|
|
34
|
+
user: [toolResult(content: [text, image])]
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
To this shape:
|
|
38
|
+
|
|
39
|
+
```text
|
|
40
|
+
user: [toolResult(content: [text]), image]
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
When a tool result contains only images, the extension inserts an `<empty>` text block so Bedrock still receives a non-empty tool result. The extension does not change encrypted reasoning returned by OpenAI models on Bedrock, which Pi displays as `[Reasoning redacted]`.
|
|
44
|
+
|
|
45
|
+
## Related docs
|
|
46
|
+
|
|
47
|
+
- [Pi issue #8643](https://github.com/earendil-works/pi/issues/8643)
|
|
48
|
+
- [AWS `ToolResultContentBlock.image` API reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ToolResultContentBlock.html)
|
|
49
|
+
|
|
50
|
+
## Contributing
|
|
51
|
+
|
|
52
|
+
To contribute a change, install Node.js 22.19 or newer, then run the project checks locally:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
npm install
|
|
56
|
+
npm run check
|
|
57
|
+
npm run smoke:package
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Maintainers
|
|
61
|
+
|
|
62
|
+
Maintained by [Jillian Kozyra](https://github.com/jekozyra).
|
|
63
|
+
|
|
64
|
+
## License
|
|
65
|
+
|
|
66
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-bedrock-compat",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Compatibility fixes for Pi models using Amazon Bedrock Converse",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"amazon-bedrock",
|
|
7
|
+
"bedrock-converse",
|
|
8
|
+
"compatibility",
|
|
9
|
+
"pi-extension",
|
|
10
|
+
"pi-package"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/jekozyra/pi-bedrock-compat.git"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"src",
|
|
19
|
+
"README.md",
|
|
20
|
+
"CHANGELOG.md",
|
|
21
|
+
"LICENSE"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"test": "tsx --test tests/*.test.ts",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"check": "npm run fmt:check && npm run lint && npm run typecheck && npm test",
|
|
28
|
+
"smoke:package": "npm pack --dry-run",
|
|
29
|
+
"fmt": "oxfmt --write .",
|
|
30
|
+
"fmt:check": "oxfmt --check .",
|
|
31
|
+
"lint": "oxlint --deny-warnings src tests"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@earendil-works/pi-coding-agent": "1.0.0",
|
|
35
|
+
"@types/node": "^22.0.0",
|
|
36
|
+
"oxfmt": "0.68.0",
|
|
37
|
+
"oxlint": "1.83.0",
|
|
38
|
+
"tsx": "^4.20.0",
|
|
39
|
+
"typescript": "^5.8.3"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
43
|
+
},
|
|
44
|
+
"peerDependenciesMeta": {
|
|
45
|
+
"@earendil-works/pi-coding-agent": {
|
|
46
|
+
"optional": true
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": ">=22.19.0"
|
|
51
|
+
},
|
|
52
|
+
"pi": {
|
|
53
|
+
"extensions": [
|
|
54
|
+
"./src/index.ts"
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { hoistToolResultImages, supportsNestedToolResultImages } from "./payload.ts";
|
|
3
|
+
|
|
4
|
+
export interface ProviderModel {
|
|
5
|
+
provider: string;
|
|
6
|
+
api: string;
|
|
7
|
+
id: string;
|
|
8
|
+
name?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function shouldRewrite(model: ProviderModel | undefined): boolean {
|
|
12
|
+
return (
|
|
13
|
+
model?.provider === "amazon-bedrock" &&
|
|
14
|
+
model.api === "bedrock-converse-stream" &&
|
|
15
|
+
!supportsNestedToolResultImages(model)
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default function bedrockCompat(pi: ExtensionAPI): void {
|
|
20
|
+
pi.on("before_provider_request", (event, ctx) => {
|
|
21
|
+
if (!shouldRewrite(ctx.model)) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const rewritten = hoistToolResultImages(event.payload);
|
|
26
|
+
return rewritten === event.payload ? undefined : rewritten;
|
|
27
|
+
});
|
|
28
|
+
}
|
package/src/payload.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const EMPTY_TEXT_PLACEHOLDER = "<empty>";
|
|
2
|
+
|
|
3
|
+
type UnknownRecord = Record<string, unknown>;
|
|
4
|
+
|
|
5
|
+
export interface BedrockModelReference {
|
|
6
|
+
id: string;
|
|
7
|
+
name?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function isRecord(value: unknown): value is UnknownRecord {
|
|
11
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function isImageBlock(value: unknown): value is UnknownRecord {
|
|
15
|
+
return isRecord(value) && isRecord(value.image);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function isSupportedNestedImageFamily(value: string): boolean {
|
|
19
|
+
const normalized = value.toLowerCase().replace(/[\s_/:]+/gu, ".");
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
normalized.includes("anthropic.claude") ||
|
|
23
|
+
/(?:^|\.)claude(?:\.|$)/u.test(normalized) ||
|
|
24
|
+
normalized.includes("amazon.nova") ||
|
|
25
|
+
/(?:^|\.)nova(?:\.|$)/u.test(normalized)
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Whether Bedrock documents this model family as supporting images inside tool results. */
|
|
30
|
+
export function supportsNestedToolResultImages(model: BedrockModelReference): boolean {
|
|
31
|
+
return [model.id, model.name].some(
|
|
32
|
+
(value) => value !== undefined && isSupportedNestedImageFamily(value),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function rewriteMessage(message: unknown): { message: unknown; changed: boolean } {
|
|
37
|
+
if (!isRecord(message) || !Array.isArray(message.content)) {
|
|
38
|
+
return { message, changed: false };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const hoistedImages: UnknownRecord[] = [];
|
|
42
|
+
let changed = false;
|
|
43
|
+
const content = message.content.map((block) => {
|
|
44
|
+
if (!isRecord(block) || !isRecord(block.toolResult)) {
|
|
45
|
+
return block;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const toolResult = block.toolResult;
|
|
49
|
+
if (!Array.isArray(toolResult.content)) {
|
|
50
|
+
return block;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const images = toolResult.content.filter(isImageBlock);
|
|
54
|
+
if (images.length === 0) {
|
|
55
|
+
return block;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const remaining = toolResult.content.filter((item) => !isImageBlock(item));
|
|
59
|
+
hoistedImages.push(...images);
|
|
60
|
+
changed = true;
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
...block,
|
|
64
|
+
toolResult: {
|
|
65
|
+
...toolResult,
|
|
66
|
+
content: remaining.length > 0 ? remaining : [{ text: EMPTY_TEXT_PLACEHOLDER }],
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
if (!changed) {
|
|
72
|
+
return { message, changed: false };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
message: { ...message, content: [...content, ...hoistedImages] },
|
|
77
|
+
changed: true,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Hoist images out of Bedrock Converse tool results without mutating the input payload.
|
|
83
|
+
* Malformed or unrelated payloads are returned unchanged.
|
|
84
|
+
*/
|
|
85
|
+
export function hoistToolResultImages(payload: unknown): unknown {
|
|
86
|
+
if (!isRecord(payload) || !Array.isArray(payload.messages)) {
|
|
87
|
+
return payload;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
let changed = false;
|
|
91
|
+
const messages = payload.messages.map((message) => {
|
|
92
|
+
const rewritten = rewriteMessage(message);
|
|
93
|
+
changed ||= rewritten.changed;
|
|
94
|
+
return rewritten.message;
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
return changed ? { ...payload, messages } : payload;
|
|
98
|
+
}
|