Skip to content
Draft
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
10 changes: 8 additions & 2 deletions slack-bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ import { createSlackRequestRuntime } from "./slack-request-runtime.js";
import { createPinetRegistrationGate } from "./pinet-registration-gate.js";
import { createBrokerRuntimeAccess } from "./broker-runtime-access.js";
import { createInboxDrainRuntime } from "./inbox-drain-runtime.js";
import {
registerPinetDeliveryMessageRenderer,
sendPinetDeliveryMessage,
} from "./pinet-delivery-card.js";
import { createAgentCompletionRuntime } from "./agent-completion-runtime.js";
import { sendBrokerMessage } from "./broker/message-send.js";
import {
Expand All @@ -84,6 +88,8 @@ import {
// Settings and helpers imported from ./helpers.js

export default function (pi: ExtensionAPI) {
registerPinetDeliveryMessageRenderer(pi);

let settings = loadSettingsFromFile();

let botToken = settings.botToken ?? process.env.SLACK_BOT_TOKEN;
Expand Down Expand Up @@ -213,7 +219,7 @@ export default function (pi: ExtensionAPI) {
}) => boolean = () => false;
const inboxDrainRuntime = createInboxDrainRuntime({
sendUserMessage: (body) => {
pi.sendUserMessage(body);
sendPinetDeliveryMessage(pi, body);
},
isIdle: () => sessionUiRuntime.getExtensionContext()?.isIdle?.() ?? true,
takeInboxMessages: (maxMessages) => inbox.splice(0, maxMessages ?? inbox.length),
Expand Down Expand Up @@ -503,7 +509,7 @@ export default function (pi: ExtensionAPI) {
getActiveBrokerSelfId,
isIdle: () => sessionUiRuntime.getExtensionContext()?.isIdle?.() ?? true,
sendUserMessage: (body) => {
pi.sendUserMessage(body);
sendPinetDeliveryMessage(pi, body);
},
});
const { sendBrokerMaintenanceMessage, trySendBrokerFollowUp } = pinetMaintenanceDelivery;
Expand Down
91 changes: 91 additions & 0 deletions slack-bridge/pinet-delivery-card.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { describe, expect, it, vi } from "vitest";
import {
buildPinetDeliveryCardDetails,
PINET_DELIVERY_CUSTOM_TYPE,
registerPinetDeliveryMessageRenderer,
renderPinetDeliveryMessage,
sendPinetDeliveryMessage,
type PinetDeliveryApi,
} from "./pinet-delivery-card.js";

const body = [
"New Pinet messages:",
"[thread a2a:broker:worker] [steering] Broker: inbox_id=42 pointer=pinet action=read args.thread_id=a2a:broker:worker args.unread_only=true",
"",
"Read pointer(s) before acting; reply via pinet action=send.",
].join("\n");

function renderText(component: { render(width: number): string[] }): string {
return component.render(300).join("\n");
}

describe("Pinet delivery cards", () => {
it("builds compact delivery card details from the full prompt body", () => {
expect(buildPinetDeliveryCardDetails(body)).toEqual({
title: "New Pinet messages:",
summary:
"[thread a2a:broker:worker] [steering] Broker: inbox_id=42 pointer=pinet action=read args.thread_id=a2a:broker:worker args.unread_only=true",
lineCount: 4,
characterCount: body.length,
});
});

it("renders collapsed cards by default with an expand hint and without the full body", () => {
const rendered = renderText(
renderPinetDeliveryMessage(
{ content: body, details: buildPinetDeliveryCardDetails(body) },
{ expanded: false },
),
);

expect(rendered).toContain("[Slack Bridge] New Pinet messages:");
expect(rendered).toContain("Ctrl+O to expand full delivery prompt");
expect(rendered).toContain("[thread a2a:broker:worker] [steering] Broker:");
expect(rendered).not.toContain("Read pointer(s) before acting");
});

it("renders the exact full prompt body when expanded", () => {
const rendered = renderText(renderPinetDeliveryMessage({ content: body }, { expanded: true }));

expect(rendered).toContain(body);
});

it("registers the custom renderer under the Slack Bridge Pinet delivery type", () => {
const registerMessageRenderer = vi.fn();

registerPinetDeliveryMessageRenderer({ registerMessageRenderer });

expect(registerMessageRenderer).toHaveBeenCalledWith(
PINET_DELIVERY_CUSTOM_TYPE,
renderPinetDeliveryMessage,
);
});

it("delivers full model-visible content through a displayed custom message", () => {
const sendMessage = vi.fn();
const sendUserMessage = vi.fn();
const pi: PinetDeliveryApi = { sendMessage, sendUserMessage };

sendPinetDeliveryMessage(pi, body);

expect(sendMessage).toHaveBeenCalledWith(
{
customType: PINET_DELIVERY_CUSTOM_TYPE,
content: body,
display: true,
details: buildPinetDeliveryCardDetails(body),
},
{ triggerTurn: true },
);
expect(sendUserMessage).not.toHaveBeenCalled();
});

it("falls back to user-message delivery when custom messages are unavailable", () => {
const sendUserMessage = vi.fn();
const pi: PinetDeliveryApi = { sendUserMessage };

sendPinetDeliveryMessage(pi, body);

expect(sendUserMessage).toHaveBeenCalledWith(body);
});
});
177 changes: 177 additions & 0 deletions slack-bridge/pinet-delivery-card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";

export const PINET_DELIVERY_CUSTOM_TYPE = "slack-bridge:pinet-delivery";

const DEFAULT_SUMMARY = "Slack Bridge delivery";
const COLLAPSED_PREVIEW_MAX_LENGTH = 180;

export interface PinetDeliveryCardDetails {
title: string;
summary: string;
lineCount: number;
characterCount: number;
}

interface PinetDeliveryMessageContentBlock {
type: string;
text?: string;
}

interface PinetDeliveryCustomMessage {
content: string | PinetDeliveryMessageContentBlock[];
details?: unknown;
}

interface PinetDeliveryRenderOptions {
expanded?: boolean;
}

interface PinetDeliveryComponent {
render(width: number): string[];
invalidate(): void;
}

export interface PinetDeliveryApi {
sendMessage?: (
message: {
customType: string;
content: string;
display: boolean;
details: PinetDeliveryCardDetails;
},
options?: { triggerTurn?: boolean; deliverAs?: "steer" | "followUp" | "nextTurn" },
) => void;
sendUserMessage: ExtensionAPI["sendUserMessage"];
}

function collapseWhitespace(value: string): string {
return value.replace(/\s+/g, " ").trim();
}

function truncateText(value: string, maxLength = COLLAPSED_PREVIEW_MAX_LENGTH): string {
const collapsed = collapseWhitespace(value);
if (collapsed.length <= maxLength) return collapsed;
return `${collapsed.slice(0, Math.max(0, maxLength - 1))}…`;
}

function getDeliveryTitle(body: string): string {
const firstLine = body
.split("\n")
.map((line) => line.trim())
.find((line) => line.length > 0);
return firstLine ?? DEFAULT_SUMMARY;
}

function getDeliverySummary(body: string): string {
const lines = body
.split("\n")
.map((line) => line.trim())
.filter(Boolean);
const firstMessageLine =
lines.find((line) => line.startsWith("[thread ")) ?? lines[1] ?? lines[0];
return truncateText(firstMessageLine ?? DEFAULT_SUMMARY);
}

export function buildPinetDeliveryCardDetails(body: string): PinetDeliveryCardDetails {
return {
title: getDeliveryTitle(body),
summary: getDeliverySummary(body),
lineCount: body.length === 0 ? 0 : body.split("\n").length,
characterCount: body.length,
};
}

function readTextContent(message: PinetDeliveryCustomMessage): string {
if (typeof message.content === "string") {
return message.content;
}

return message.content
.filter((block) => block.type === "text" && typeof block.text === "string")
.map((block) => block.text ?? "")
.join("\n");
}

function readDetails(message: PinetDeliveryCustomMessage): PinetDeliveryCardDetails | null {
if (typeof message.details !== "object" || message.details === null) return null;
const details = message.details;
if (!("title" in details) || !("summary" in details)) return null;
const title = details.title;
const summary = details.summary;
if (typeof title !== "string" || typeof summary !== "string") return null;
return {
title,
summary,
lineCount:
"lineCount" in details && typeof details.lineCount === "number" ? details.lineCount : 0,
characterCount:
"characterCount" in details && typeof details.characterCount === "number"
? details.characterCount
: 0,
};
}

function truncateLineToWidth(value: string, width: number): string {
if (width <= 0) return "";
if (value.length <= width) return value;
if (width === 1) return "…";
return `${value.slice(0, width - 1)}…`;
}

class PinetDeliveryCardComponent implements PinetDeliveryComponent {
constructor(private readonly text: string) {}

render(width: number): string[] {
return this.text.split("\n").map((line) => truncateLineToWidth(line, width));
}

invalidate(): void {
// Stateless component.
}
}

export function renderPinetDeliveryMessage(
message: PinetDeliveryCustomMessage,
options: PinetDeliveryRenderOptions,
): PinetDeliveryComponent {
const body = readTextContent(message);
const details = readDetails(message) ?? buildPinetDeliveryCardDetails(body);
const heading = `[Slack Bridge] ${details.title}`;

if (options.expanded) {
return new PinetDeliveryCardComponent(`${heading}\n\n${body}`);
}

const stats =
details.lineCount > 1 || details.characterCount > COLLAPSED_PREVIEW_MAX_LENGTH
? ` (${details.lineCount} lines, ${details.characterCount} chars)`
: "";
const summary = details.summary;
return new PinetDeliveryCardComponent(
`${heading}${stats}\n${summary}\nCtrl+O to expand full delivery prompt`,
);
}

export function registerPinetDeliveryMessageRenderer(
pi: Pick<ExtensionAPI, "registerMessageRenderer">,
): void {
if (typeof pi.registerMessageRenderer !== "function") return;
pi.registerMessageRenderer(PINET_DELIVERY_CUSTOM_TYPE, renderPinetDeliveryMessage);
}

export function sendPinetDeliveryMessage(pi: PinetDeliveryApi, body: string): void {
if (typeof pi.sendMessage === "function") {
pi.sendMessage(
{
customType: PINET_DELIVERY_CUSTOM_TYPE,
content: body,
display: true,
details: buildPinetDeliveryCardDetails(body),
},
{ triggerTurn: true },
);
return;
}

pi.sendUserMessage(body);
}
Loading