@arhen/pi-senja 1.4.0 → 1.5.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/package.json +1 -1
- package/src/tool-groups.ts +63 -0
package/package.json
CHANGED
package/src/tool-groups.ts
CHANGED
|
@@ -431,6 +431,64 @@ export function createTranscriptView(
|
|
|
431
431
|
const previews = new WeakSet<Component>();
|
|
432
432
|
type Thought = ReturnType<typeof thinkingRow>;
|
|
433
433
|
const originals = new WeakMap<Component, MouseRegion>();
|
|
434
|
+
const styledMarkdown = new WeakSet<Markdown>();
|
|
435
|
+
function styleFences(part: Markdown) {
|
|
436
|
+
if (styledMarkdown.has(part) || Object.hasOwn(part, "renderToken")) return;
|
|
437
|
+
const native = Reflect.get(part, "renderToken");
|
|
438
|
+
if (typeof native !== "function") return;
|
|
439
|
+
// ponytail: Pi has no block renderer hook; override this instance only while its native token method exists.
|
|
440
|
+
Reflect.set(
|
|
441
|
+
part,
|
|
442
|
+
"renderToken",
|
|
443
|
+
(token: { type: string; text: string; lang?: string; raw?: string }, width: number, ...rest: unknown[]) => {
|
|
444
|
+
if (token.type !== "code" || !/^ {0,3}(?:`{3,}|~{3,})/.test(token.raw ?? "") || width < 1)
|
|
445
|
+
return native.call(part, token, width, ...rest);
|
|
446
|
+
const source = stripTerminalSequences(token.text)
|
|
447
|
+
.replace(/[\x00-\x08\x0b-\x1f\x7f-\x9f]/g, "")
|
|
448
|
+
.replace(/\t/g, " ")
|
|
449
|
+
.split("\n");
|
|
450
|
+
const lang = summary(token.lang).split(" ")[0] || "code";
|
|
451
|
+
const framed = width >= 24;
|
|
452
|
+
const rim = framed ? 2 : 0;
|
|
453
|
+
const pad = width >= 8 ? 2 : 0;
|
|
454
|
+
const inner = Math.min(
|
|
455
|
+
width - 2 * (pad + rim),
|
|
456
|
+
source.reduce((n, s) => Math.max(n, visibleWidth(s)), Math.max(18, visibleWidth(lang) + 12)),
|
|
457
|
+
);
|
|
458
|
+
const matte = (length: number) => {
|
|
459
|
+
const space = " ".repeat(length);
|
|
460
|
+
return theme.colors
|
|
461
|
+
? theme.style(space, { bg: theme.colors.customMessageLabel })
|
|
462
|
+
: theme.bg("customMessageBg", space);
|
|
463
|
+
};
|
|
464
|
+
const edge = matte(rim);
|
|
465
|
+
const line = (text: string) =>
|
|
466
|
+
edge +
|
|
467
|
+
theme.bg(
|
|
468
|
+
"toolSuccessBg",
|
|
469
|
+
" ".repeat(pad) + text + " ".repeat(Math.max(0, inner + pad - visibleWidth(text))),
|
|
470
|
+
) +
|
|
471
|
+
edge;
|
|
472
|
+
const cap = matte(inner + 2 * (pad + rim));
|
|
473
|
+
const dots = framed
|
|
474
|
+
? [theme.fg("error", "●"), theme.fg("warning", "●"), theme.fg("success", "●")].join(" ")
|
|
475
|
+
: "";
|
|
476
|
+
const label = truncateToWidth(lang, inner - visibleWidth(dots), "…");
|
|
477
|
+
const gap = Math.max(0, Math.floor((inner - visibleWidth(label)) / 2) - visibleWidth(dots));
|
|
478
|
+
const lines = [line(dots + " ".repeat(gap) + theme.fg("muted", label)), line("")];
|
|
479
|
+
if (framed) lines.unshift(cap);
|
|
480
|
+
for (const logical of source) {
|
|
481
|
+
for (const row of wrapTextWithAnsi(logical, inner)) lines.push(line(theme.fg("text", row)));
|
|
482
|
+
}
|
|
483
|
+
lines.push(line(""));
|
|
484
|
+
if (framed) lines.push(cap);
|
|
485
|
+
if (rest[0] && rest[0] !== "space") lines.push("");
|
|
486
|
+
return lines;
|
|
487
|
+
},
|
|
488
|
+
);
|
|
489
|
+
styledMarkdown.add(part);
|
|
490
|
+
part.invalidate();
|
|
491
|
+
}
|
|
434
492
|
function styleThoughts(message: AssistantMessageComponent): Thought[] | undefined {
|
|
435
493
|
const [content] = message.children;
|
|
436
494
|
if (message.children.length !== 1 || !(content instanceof Container)) return;
|
|
@@ -438,6 +496,7 @@ export function createTranscriptView(
|
|
|
438
496
|
let onlyThoughts = true;
|
|
439
497
|
for (const [i, part] of content.children.entries()) {
|
|
440
498
|
if (part instanceof Spacer) continue;
|
|
499
|
+
if (part instanceof Markdown) styleFences(part);
|
|
441
500
|
const region = originals.get(part) ?? part;
|
|
442
501
|
const child = region instanceof MouseRegion ? Reflect.get(region, "child") : undefined;
|
|
443
502
|
if (!(region instanceof MouseRegion) || !(child instanceof Text || child instanceof Markdown)) {
|
|
@@ -700,6 +759,10 @@ export function createTranscriptView(
|
|
|
700
759
|
const [content] = message.children;
|
|
701
760
|
if (content instanceof Container) {
|
|
702
761
|
for (const [i, part] of content.children.entries()) {
|
|
762
|
+
if (part instanceof Markdown && styledMarkdown.has(part)) {
|
|
763
|
+
Reflect.deleteProperty(part, "renderToken");
|
|
764
|
+
part.invalidate();
|
|
765
|
+
}
|
|
703
766
|
const original = originals.get(part);
|
|
704
767
|
if (original) content.children[i] = original;
|
|
705
768
|
}
|