Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
max-parallel: 1
matrix:
node: ["22.19.0", "24"]
pi: ["0.99.2", "1.0.0"]
pi: ["0.99.2", "1.0.0", "1.5.0"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand Down
34 changes: 15 additions & 19 deletions internal-patch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from "node:path";
import { pathToFileURL } from "node:url";
import type { AssistantMessage, ThinkingContent } from "@earendil-works/pi-ai";
import { AssistantMessageComponent as HostAssistantMessageComponent, getPackageDir, type MarkdownTransformer } from "@earendil-works/pi-coding-agent";
import { AssistantMessageComponent as HostAssistantMessageComponent, getPackageDir, VERSION, type MarkdownTransformer } from "@earendil-works/pi-coding-agent";
import { Markdown, Spacer, Text, type Component, type MarkdownOptions, type MarkdownTheme } from "@earendil-works/pi-tui";
import { decrementPatchRefCount, getPatchCleanup, getPatchInstallPromise, incrementPatchRefCount, resolveThinkingMessageScope, setPatchCleanup, setPatchInstallPromise } from "./state.js";
import { ThinkingStepsComponent } from "./render.js";
Expand Down Expand Up @@ -78,18 +78,6 @@ function hasPatchableContentContainer(value: AssistantMessageComponentPrototype)
);
}

function fallbackToOriginalUpdateContent(
instance: AssistantMessageComponentPrototype,
message: AssistantMessage,
originalUpdateContent: (message: AssistantMessage) => void,
): void {
try {
originalUpdateContent.call(instance, message);
} catch (error) {
throw new Error("Thinking Steps patch failed: Pi internals are incompatible and fallback rendering also failed.", { cause: error });
}
}

export function resolvePiCodingAgentInternalModuleUrl(relativePath: string): string {
try {
return pathToFileURL(join(getPackageDir(), relativePath)).href;
Expand All @@ -109,6 +97,17 @@ export async function importPiCodingAgentInternal<TModule>(relativePath: string)
}
}

export function needsIsStreamingThunk(version: string): boolean {
const [major = 0, minor = 0] = version.split(".").map(Number);
return !(major < 1 || (major === 1 && minor < 5));
}

const PI_TAKES_IS_STREAMING_THUNK = needsIsStreamingThunk(VERSION);

export function isStreamingArgument(component: { isStreaming: boolean }): boolean | (() => boolean) {
return PI_TAKES_IS_STREAMING_THUNK ? () => component.isStreaming : component.isStreaming;
}

function hasVisibleThinking(content: ThinkingContent): boolean {
return content.redacted === true || content.thinking.trim().length > 0;
}
Expand All @@ -131,17 +130,13 @@ function hasVisibleTextContent(message: AssistantMessage): boolean {
return message.content.some((content) => content.type === "text" && content.text.trim().length > 0);
}

function hasVisibleThinkingContent(message: AssistantMessage): boolean {
return message.content.some((content) => content.type === "thinking" && hasVisibleThinking(content));
}

async function installPatch(providedTheme?: ThinkingThemeLike): Promise<() => void> {
const [{ theme: rawTheme }, { createMarkdownTransform }] = await Promise.all([
providedTheme === undefined
? importPiCodingAgentInternal<{ theme: unknown }>(PI_CODING_AGENT_INTERNAL_MODULES.theme)
: Promise.resolve({ theme: providedTheme }),
importPiCodingAgentInternal<{
createMarkdownTransform(messageType: "assistant", isStreaming: boolean, transformers: MarkdownTransformer[]): NonNullable<MarkdownOptions["transform"]>;
createMarkdownTransform(messageType: "assistant", isStreaming: boolean | (() => boolean), transformers: MarkdownTransformer[]): NonNullable<MarkdownOptions["transform"]>;
}>(PI_CODING_AGENT_INTERNAL_MODULES.markdownTransform),
]);

Expand Down Expand Up @@ -298,10 +293,11 @@ async function installPatch(providedTheme?: ThinkingThemeLike): Promise<() => vo
return message.content.slice(firstThinkingIndex + 1).some((content) => content.type === "text" && content.text.trim().length > 0);
})();

const streamingArgument = isStreamingArgument(this);
for (const content of message.content) {
if (content.type === "text" && content.text.trim()) {
this.contentContainer.addChild(new Markdown(content.text.trim(), this.outputPad, 0, this.markdownTheme, undefined, {
transform: createMarkdownTransform("assistant", this.isStreaming, this.markdownTransformers),
transform: createMarkdownTransform("assistant", streamingArgument, this.markdownTransformers),
}));
continue;
}
Expand Down
5 changes: 5 additions & 0 deletions scripts/test-host.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ try {
assert.match(component.render(100).join("\n"), /Waiting for thinking content/);
component.updateContent({ ...message, content: [] }, false);
assert.match(component.render(100).join("\n"), /No thinking content supplied/);
// Text blocks go through the host's Markdown transform, which takes the streaming flag in a version-specific shape.
component.updateContent({ ...message, content: [{ type: "thinking", thinking: "Verify the host renderer." }, { type: "text", text: "HOST_TEXT_ANSWER" }] }, true);
assert.match(component.render(100).join("\n"), /HOST_TEXT_ANSWER/);
component.updateContent({ ...message, content: [{ type: "text", text: "HOST_TEXT_ANSWER" }] }, false);
assert.match(component.render(100).join("\n"), /HOST_TEXT_ANSWER/);
}
const shortcut = extension.shortcuts.get("alt+t");
assert.ok(shortcut);
Expand Down
28 changes: 28 additions & 0 deletions test/thinking-steps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
assertPatchableAssistantMessageComponent,
assertThinkingStepsTheme,
importPiCodingAgentInternal,
isStreamingArgument,
needsIsStreamingThunk,
PI_CODING_AGENT_INTERNAL_MODULES,
resolvePiCodingAgentInternalModuleUrl,
retainThinkingStepsPatch,
Expand Down Expand Up @@ -388,6 +390,32 @@ describe("patch guards", () => {
});
});

describe("isStreaming argument compatibility", () => {
it("builds a transform that renders without throwing and reports a boolean isStreaming", async () => {
const host = await importPiCodingAgentInternal<{
createMarkdownTransform(messageType: "assistant", isStreaming: boolean | (() => boolean), transformers: MarkdownTransformer[]): (markdown: string, availableWidth: number) => string;
}>(PI_CODING_AGENT_INTERNAL_MODULES.markdownTransform);
let seen: unknown;
const transform = host.createMarkdownTransform("assistant", isStreamingArgument({ isStreaming: true }), [
(markdown, context) => {
seen = context.isStreaming;
return markdown;
},
]);

assert.equal(transform("hello", 80), "hello");
assert.equal(seen, true);
});

it("only asks for the thunk on pi >=1.5", () => {
assert.equal(needsIsStreamingThunk("0.99.2"), false);
assert.equal(needsIsStreamingThunk("1.0.0"), false);
assert.equal(needsIsStreamingThunk("1.4.9"), false);
assert.equal(needsIsStreamingThunk("1.5.0"), true);
assert.equal(needsIsStreamingThunk("2.0.0"), true);
assert.equal(needsIsStreamingThunk("garbage"), true);
});
});

describe("iconForThinkingRole", () => {
it("uses a restrained icon set with distinct inspect and search glyphs", () => {
Expand Down