char-slop/ai-dots

ai dotfiles

git clone https://git.t4t.associates/char-slop/ai-dots

Charlotte Somcompact-reasoning: change this message28ac03c

main
2.1 KiB62 linesraw
1import type { AssistantMessage } from "@earendil-works/pi-ai";
2import {
3  AssistantMessageComponent,
4  type ExtensionAPI,
5} from "@earendil-works/pi-coding-agent";
6import { Markdown, truncateToWidth, type Component } from "@earendil-works/pi-tui";
7
8const LINES = 8;
9type AssistantInternals = {
10  contentContainer: { children: Component[] };
11  lastMessage?: AssistantMessage;
12};
13type ThinkingMarkdown = {
14  defaultTextStyle?: { italic?: boolean };
15  paddingX: number;
16};
17type Prototype = typeof AssistantMessageComponent.prototype & {
18  setExpanded?: (expanded: boolean) => void;
19};
20
21export default function (pi: ExtensionAPI) {
22  const prototype = AssistantMessageComponent.prototype as Prototype;
23  const updateContent = prototype.updateContent;
24  const setExpanded = prototype.setExpanded;
25  const expanded = new WeakSet<AssistantMessageComponent>();
26
27  prototype.updateContent = function (message) {
28    updateContent.call(this, message);
29    if (expanded.has(this)) return;
30
31    const { children } = (this as unknown as AssistantInternals).contentContainer;
32    for (const child of children) {
33      if (!(child instanceof Markdown)) continue;
34      const markdown = child as unknown as ThinkingMarkdown;
35      if (!markdown.defaultTextStyle?.italic) continue;
36      const render = child.render.bind(child);
37      child.render = (width) => {
38        const lines = render(width);
39        if (lines.length <= LINES) return lines;
40        const indicator = `(${lines.length - LINES} earlier lines hidden - ctrl+o to expand)`;
41        const margin = " ".repeat(markdown.paddingX);
42        return [
43          ...lines.slice(-LINES),
44          truncateToWidth(`${margin}${indicator}${margin}`, width, "", true),
45        ];
46      };
47    }
48  };
49
50  prototype.setExpanded = function (value) {
51    value ? expanded.add(this) : expanded.delete(this);
52    setExpanded?.call(this, value);
53    const message = (this as unknown as AssistantInternals).lastMessage;
54    if (message) this.updateContent(message);
55  };
56
57  pi.on("session_shutdown", () => {
58    prototype.updateContent = updateContent;
59    if (setExpanded) prototype.setExpanded = setExpanded;
60    else delete prototype.setExpanded;
61  });
62}