@narumitw/pi-chat 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 +21 -0
- package/README.md +293 -0
- package/package.json +55 -0
- package/src/chat-session.ts +436 -0
- package/src/chat-view.ts +256 -0
- package/src/external.d.ts +69 -0
- package/src/identity.ts +80 -0
- package/src/index.ts +1 -0
- package/src/menu.ts +490 -0
- package/src/network.ts +201 -0
- package/src/pi-chat.ts +647 -0
- package/src/protocol.ts +325 -0
- package/src/settings.ts +330 -0
- package/src/text.ts +73 -0
- package/src/widget.ts +87 -0
package/src/text.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
const REPLACEMENT = "�";
|
|
2
|
+
|
|
3
|
+
export function sanitizeChatText(value: string): string {
|
|
4
|
+
const normalized = value.replace(/\r\n?/gu, "\n");
|
|
5
|
+
let result = "";
|
|
6
|
+
for (let index = 0; index < normalized.length; index += 1) {
|
|
7
|
+
const code = normalized.charCodeAt(index);
|
|
8
|
+
if (code === 0x1b) {
|
|
9
|
+
index = escapeSequenceEnd(normalized, index);
|
|
10
|
+
result += REPLACEMENT;
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
const point = normalized.codePointAt(index) ?? code;
|
|
14
|
+
if (isBidiControl(point) || isUnsafeControl(point)) {
|
|
15
|
+
result += REPLACEMENT;
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
if (code === 0x09) {
|
|
19
|
+
result += " ";
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
result += String.fromCodePoint(point);
|
|
23
|
+
if (point > 0xffff) index += 1;
|
|
24
|
+
}
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function sanitizeSingleLine(value: string): string {
|
|
29
|
+
return sanitizeChatText(value).replace(/\n+/gu, " ");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function escapeSequenceEnd(value: string, start: number): number {
|
|
33
|
+
const introducer = value.charCodeAt(start + 1);
|
|
34
|
+
if (introducer === 0x5d) {
|
|
35
|
+
for (let index = start + 2; index < value.length; index += 1) {
|
|
36
|
+
const code = value.charCodeAt(index);
|
|
37
|
+
if (code === 0x07) return index;
|
|
38
|
+
if (code === 0x1b && value.charCodeAt(index + 1) === 0x5c) return index + 1;
|
|
39
|
+
}
|
|
40
|
+
return value.length - 1;
|
|
41
|
+
}
|
|
42
|
+
if (introducer === 0x50) {
|
|
43
|
+
for (let index = start + 2; index < value.length; index += 1) {
|
|
44
|
+
if (value.charCodeAt(index) === 0x1b && value.charCodeAt(index + 1) === 0x5c) {
|
|
45
|
+
return index + 1;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return value.length - 1;
|
|
49
|
+
}
|
|
50
|
+
if (introducer === 0x5b) {
|
|
51
|
+
for (let index = start + 2; index < value.length; index += 1) {
|
|
52
|
+
const code = value.charCodeAt(index);
|
|
53
|
+
if (code >= 0x40 && code <= 0x7e) return index;
|
|
54
|
+
}
|
|
55
|
+
return value.length - 1;
|
|
56
|
+
}
|
|
57
|
+
return Math.min(start + 1, value.length - 1);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function isUnsafeControl(point: number): boolean {
|
|
61
|
+
if (point === 0x0a || point === 0x09) return false;
|
|
62
|
+
return point <= 0x1f || (point >= 0x7f && point <= 0x9f);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function isBidiControl(point: number): boolean {
|
|
66
|
+
return (
|
|
67
|
+
point === 0x061c ||
|
|
68
|
+
point === 0x200e ||
|
|
69
|
+
point === 0x200f ||
|
|
70
|
+
(point >= 0x202a && point <= 0x202e) ||
|
|
71
|
+
(point >= 0x2066 && point <= 0x2069)
|
|
72
|
+
);
|
|
73
|
+
}
|
package/src/widget.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
3
|
+
import type { ChatSnapshot, TranscriptEntry } from "./chat-session.js";
|
|
4
|
+
import type { WidgetMode } from "./settings.js";
|
|
5
|
+
import { sanitizeChatText, sanitizeSingleLine } from "./text.js";
|
|
6
|
+
|
|
7
|
+
export function renderChatWidget(
|
|
8
|
+
snapshot: ChatSnapshot,
|
|
9
|
+
mode: WidgetMode,
|
|
10
|
+
width: number,
|
|
11
|
+
theme: Theme,
|
|
12
|
+
terminalRows = 24,
|
|
13
|
+
): string[] {
|
|
14
|
+
if (mode === "off" || snapshot.state === "disconnected") return [];
|
|
15
|
+
const safeWidth = Math.max(1, width);
|
|
16
|
+
if (mode === "dock") return renderRoomDock(snapshot, safeWidth, terminalRows, theme);
|
|
17
|
+
|
|
18
|
+
const peers = snapshot.peers.length;
|
|
19
|
+
const presence = peers === 0 ? "alone" : `${peers} direct peer${peers === 1 ? "" : "s"}`;
|
|
20
|
+
const unread = snapshot.unread > 0 ? `${snapshot.unread} unread · ` : "";
|
|
21
|
+
const attention = snapshot.state === "degraded" ? " · reconnecting" : "";
|
|
22
|
+
const header = truncateToWidth(
|
|
23
|
+
theme.fg(
|
|
24
|
+
snapshot.state === "degraded" ? "warning" : "accent",
|
|
25
|
+
`chat · ${unread}${presence}${attention}`,
|
|
26
|
+
),
|
|
27
|
+
safeWidth,
|
|
28
|
+
);
|
|
29
|
+
const roomHint = truncateToWidth(
|
|
30
|
+
theme.fg("dim", `${sanitizeSingleLine(snapshot.room.label)} · /chat to open`),
|
|
31
|
+
safeWidth,
|
|
32
|
+
);
|
|
33
|
+
if (mode !== "latest") return [header, roomHint];
|
|
34
|
+
const latest = snapshot.transcript.at(-1);
|
|
35
|
+
if (!latest) return [header, roomHint];
|
|
36
|
+
return [header, roomHint, renderMessagePreview(latest, safeWidth, theme, false)];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function renderRoomDock(
|
|
40
|
+
snapshot: ChatSnapshot,
|
|
41
|
+
width: number,
|
|
42
|
+
terminalRows: number,
|
|
43
|
+
theme: Theme,
|
|
44
|
+
): string[] {
|
|
45
|
+
const peers = snapshot.peers.length;
|
|
46
|
+
const peerText = `${peers} direct peer${peers === 1 ? "" : "s"}`;
|
|
47
|
+
const stateText =
|
|
48
|
+
snapshot.state === "connecting"
|
|
49
|
+
? "joining…"
|
|
50
|
+
: snapshot.state === "degraded"
|
|
51
|
+
? `reconnecting · ${peerText}`
|
|
52
|
+
: peers === 0
|
|
53
|
+
? "waiting for peers"
|
|
54
|
+
: peerText;
|
|
55
|
+
const unread = snapshot.unread > 0 ? ` · ${snapshot.unread} unread` : "";
|
|
56
|
+
const roomTitle = `ROOM ${sanitizeSingleLine(snapshot.room.label)}`;
|
|
57
|
+
const status = `${stateText}${unread}`;
|
|
58
|
+
const color = snapshot.state === "degraded" ? "warning" : "accent";
|
|
59
|
+
const combinedHeader = `${roomTitle} · ${status}`;
|
|
60
|
+
const header =
|
|
61
|
+
visibleWidth(combinedHeader) <= width
|
|
62
|
+
? [theme.fg(color, combinedHeader)]
|
|
63
|
+
: [
|
|
64
|
+
truncateToWidth(theme.fg(color, roomTitle), width),
|
|
65
|
+
truncateToWidth(theme.fg(color, status), width),
|
|
66
|
+
];
|
|
67
|
+
const previewCount = terminalRows < 10 ? 0 : terminalRows < 18 ? 1 : terminalRows < 26 ? 2 : 3;
|
|
68
|
+
const previews = (previewCount === 0 ? [] : snapshot.transcript.slice(-previewCount)).map(
|
|
69
|
+
(entry) => renderMessagePreview(entry, width, theme, true),
|
|
70
|
+
);
|
|
71
|
+
const target = snapshot.composerOpen
|
|
72
|
+
? `Input → CHAT ${sanitizeSingleLine(snapshot.room.label)} · Esc returns to Pi/LLM`
|
|
73
|
+
: "Input → Pi/LLM · /chat then Enter to reply";
|
|
74
|
+
return [...header, ...previews, truncateToWidth(theme.fg("dim", target), width)];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function renderMessagePreview(
|
|
78
|
+
entry: TranscriptEntry,
|
|
79
|
+
width: number,
|
|
80
|
+
theme: Theme,
|
|
81
|
+
includeIdentity = false,
|
|
82
|
+
): string {
|
|
83
|
+
const preview = sanitizeChatText(entry.text).replace(/\n+/gu, " ");
|
|
84
|
+
const label = sanitizeSingleLine(entry.label);
|
|
85
|
+
const author = includeIdentity ? label || "peer" : label.split("~", 1)[0] || "peer";
|
|
86
|
+
return truncateToWidth(theme.fg("muted", `${author}: ${preview}`), width);
|
|
87
|
+
}
|