@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
67 lines • 3.55 kB
JavaScript
import { Box, Markdown, Spacer, Text } from "@earendil-works/pi-tui";
import { isSessionControlDeliveryDetails, SESSION_CONTROL_DELIVERY_TYPE, sessionControlRemoteOriginOf, sessionControlSenderOf, } from "../../../core/extensions/types.js";
import { sharedMarkdownTheme } from "../theme/theme.js";
/**
* A message another session delivered renders as its own block - never as the user's own input, so
* a reader always sees which text came from outside this terminal. A delivery that names its sender
* shows one label line ("Sent by another agent · <name>") over the message as it was written; one
* that does not keeps the generic heading over the text the model read. A delivery an RPC connection admitted for a
* remote device leads with the device the engine stamped, before the sender that device's caller claimed.
*/
export const renderRemoteDelivery = (message, _options, theme) => {
const box = new Box(1, 1, (text) => theme.bg("customMessageBg", text));
const sender = sessionControlSenderOf(message.details);
const origin = sessionControlRemoteOriginOf(message.details);
const shown = sender === undefined && origin === undefined ? undefined : displayTextOf(message.details);
if (shown !== undefined) {
if (origin !== undefined)
box.addChild(new Text(theme.fg("dim", originLabel(origin)), 0, 0));
if (sender !== undefined)
box.addChild(new Text(theme.fg("dim", senderLabel(sender)), 0, 0));
box.addChild(new Spacer(1));
box.addChild(new Markdown(shown, 0, 0, sharedMarkdownTheme(), { color: (line) => theme.fg("customMessageText", line) }));
return box;
}
const label = theme.fg("customMessageLabel", theme.bold("remote message"));
box.addChild(new Text(`${label}${theme.fg("dim", ` · delivery ${deliveryIdOf(message.details)}`)}`, 0, 0));
box.addChild(new Spacer(1));
const text = typeof message.content === "string"
? message.content
: message.content
.filter((part) => part.type === "text")
.map((part) => part.text)
.join("\n");
box.addChild(new Markdown(text, 0, 0, sharedMarkdownTheme(), { color: (line) => theme.fg("customMessageText", line) }));
return box;
};
export function senderLabel(sender) {
switch (sender.kind) {
case "agent":
return sender.name === undefined || sender.name.trim() === ""
? "Sent by another agent"
: `Sent by another agent · ${sender.name}`;
case "command_line":
return "Sent from the command line";
case "external":
return sender.author === undefined || sender.author.trim() === ""
? `Sent from ${sender.platform}`
: `Sent from ${sender.platform} · ${sender.author}`;
}
}
function originLabel(origin) {
return `Relayed from device ${origin.deviceName ?? origin.deviceId}`;
}
export function builtInMessageRenderer(customType) {
return customType === SESSION_CONTROL_DELIVERY_TYPE ? renderRemoteDelivery : undefined;
}
function displayTextOf(details) {
return isSessionControlDeliveryDetails(details) && typeof details.display_text === "string"
? details.display_text
: undefined;
}
function deliveryIdOf(details) {
if (typeof details !== "object" || details === null || !("delivery_id" in details))
return "unknown";
return typeof details.delivery_id === "string" ? details.delivery_id : "unknown";
}
//# sourceMappingURL=remote-delivery-message.js.map