@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
120 lines • 4.59 kB
JavaScript
import { buildNoticeBox } from "../notice/index.js";
const PR_PROMPT_PATTERN = /^\s*You are given one or more GitHub PR URLs:\s*(\S+)/im;
const ISSUE_PROMPT_PATTERN = /^\s*Analyze GitHub issue\(s\):\s*(\S+)/im;
function extractPromptMatch(prompt) {
const prMatch = prompt.match(PR_PROMPT_PATTERN);
if (prMatch?.[1]) {
return { kind: "pr", url: prMatch[1].trim() };
}
const issueMatch = prompt.match(ISSUE_PROMPT_PATTERN);
if (issueMatch?.[1]) {
return { kind: "issue", url: issueMatch[1].trim() };
}
return undefined;
}
async function fetchGhMetadata(pi, kind, url) {
const args = kind === "pr" ? ["pr", "view", url, "--json", "title,author"] : ["issue", "view", url, "--json", "title,author"];
try {
const result = await pi.exec("gh", args);
if (result.code !== 0 || !result.stdout)
return undefined;
return JSON.parse(result.stdout);
}
catch {
return undefined;
}
}
function formatAuthor(author) {
if (!author)
return undefined;
const name = author.name?.trim();
const login = author.login?.trim();
if (name && login)
return `${name} (@${login})`;
if (login)
return `@${login}`;
if (name)
return name;
return undefined;
}
export default function promptUrlWidgetExtension(pi) {
const setWidget = (ctx, match, title, authorText) => {
ctx.ui.setWidget("prompt-url", (_tui, thm) => buildNoticeBox({
title: title ?? match.url,
tone: "accent",
why: authorText ?? "Prompt URL detected.",
extra: [{ text: match.url, tone: "dim" }],
}, { expanded: false }, thm));
};
const applySessionName = (_ctx, match, title) => {
const label = match.kind === "pr" ? "PR" : "Issue";
const trimmedTitle = title?.trim();
const fallbackName = `${label}: ${match.url}`;
const desiredName = trimmedTitle ? `${label}: ${trimmedTitle} (${match.url})` : fallbackName;
const currentName = pi.getSessionName()?.trim();
if (!currentName) {
pi.setSessionName(desiredName);
return;
}
if (currentName === match.url || currentName === fallbackName) {
pi.setSessionName(desiredName);
}
};
const onBeforeAgentStart = async (event, ctx) => {
if (event.preview === true || !ctx.hasUI)
return;
const match = extractPromptMatch(event.prompt);
if (!match) {
return;
}
setWidget(ctx, match);
applySessionName(ctx, match);
void fetchGhMetadata(pi, match.kind, match.url).then((meta) => {
const title = meta?.title?.trim();
const authorText = formatAuthor(meta?.author);
setWidget(ctx, match, title, authorText);
applySessionName(ctx, match, title);
});
};
pi.on("before_agent_start", onBeforeAgentStart, { previewSafe: true });
const getUserText = (content) => {
if (!content)
return "";
if (typeof content === "string")
return content;
return (content
.filter((block) => block.type === "text")
.map((block) => block.text)
.join("\n") ?? "");
};
const rebuildFromSession = (ctx) => {
if (!ctx.hasUI)
return;
const entries = ctx.sessionManager.getEntries();
const lastMatch = [...entries].reverse().find((entry) => {
if (entry.type !== "message" || entry.message.role !== "user")
return false;
const text = getUserText(entry.message.content);
return !!extractPromptMatch(text);
});
const content = lastMatch?.type === "message" && lastMatch.message.role === "user" ? lastMatch.message.content : undefined;
const text = getUserText(content);
const match = text ? extractPromptMatch(text) : undefined;
if (!match) {
ctx.ui.setWidget("prompt-url", undefined);
return;
}
setWidget(ctx, match);
applySessionName(ctx, match);
void fetchGhMetadata(pi, match.kind, match.url).then((meta) => {
const title = meta?.title?.trim();
const authorText = formatAuthor(meta?.author);
setWidget(ctx, match, title, authorText);
applySessionName(ctx, match, title);
});
};
pi.on("session_start", async (_event, ctx) => {
rebuildFromSession(ctx);
});
}
//# sourceMappingURL=prompt-url-widget.js.map