mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-15 09:01:14 +00:00
Rebuild prompt widget on resume
This commit is contained in:
parent
35690f6d1a
commit
b4b9cc2a80
1 changed files with 29 additions and 14 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
import { DynamicBorder, type ExtensionAPI, type ExtensionContext } from "@mariozechner/pi-coding-agent";
|
import { DynamicBorder, type ExtensionAPI, type ExtensionContext } from "@mariozechner/pi-coding-agent";
|
||||||
import { Container, Text } from "@mariozechner/pi-tui";
|
import { Container, Text } from "@mariozechner/pi-tui";
|
||||||
|
|
||||||
const PR_PROMPT_PATTERN = /^You are given one or more GitHub PR URLs:\s*(\S+)/im;
|
const PR_PROMPT_PATTERN = /^\s*You are given one or more GitHub PR URLs:\s*(\S+)/im;
|
||||||
const ISSUE_PROMPT_PATTERN = /^Analyze GitHub issue\(s\):\s*(\S+)/im;
|
const ISSUE_PROMPT_PATTERN = /^\s*Analyze GitHub issue\(s\):\s*(\S+)/im;
|
||||||
|
|
||||||
type PromptMatch = {
|
type PromptMatch = {
|
||||||
kind: "pr" | "issue";
|
kind: "pr" | "issue";
|
||||||
|
|
@ -92,23 +92,34 @@ export default function promptUrlWidgetExtension(pi: ExtensionAPI) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
pi.on("session_start", async (_event, ctx) => {
|
pi.on("session_switch", async (_event, ctx) => {
|
||||||
|
rebuildFromSession(ctx);
|
||||||
|
});
|
||||||
|
|
||||||
|
const getUserText = (content: string | { type: string; text?: string }[] | undefined): string => {
|
||||||
|
if (!content) return "";
|
||||||
|
if (typeof content === "string") return content;
|
||||||
|
return (
|
||||||
|
content
|
||||||
|
.filter((block): block is { type: "text"; text: string } => block.type === "text")
|
||||||
|
.map((block) => block.text)
|
||||||
|
.join("\n") ?? ""
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const rebuildFromSession = (ctx: ExtensionContext) => {
|
||||||
if (!ctx.hasUI) return;
|
if (!ctx.hasUI) return;
|
||||||
|
|
||||||
const entries = ctx.sessionManager.getEntries();
|
const entries = ctx.sessionManager.getEntries();
|
||||||
const lastUser = [...entries].reverse().find((entry) => {
|
const lastMatch = [...entries].reverse().find((entry) => {
|
||||||
return entry.type === "message" && entry.message.role === "user";
|
if (entry.type !== "message" || entry.message.role !== "user") return false;
|
||||||
|
const text = getUserText(entry.message.content);
|
||||||
|
return !!extractPromptMatch(text);
|
||||||
});
|
});
|
||||||
|
|
||||||
const content = lastUser?.type === "message" && lastUser?.message.role === "user" ? lastUser.message.content : undefined;
|
const content =
|
||||||
const text =
|
lastMatch?.type === "message" && lastMatch.message.role === "user" ? lastMatch.message.content : undefined;
|
||||||
typeof content === "string"
|
const text = getUserText(content);
|
||||||
? content
|
|
||||||
: content
|
|
||||||
?.filter((block): block is { type: "text"; text: string } => block.type === "text")
|
|
||||||
.map((block) => block.text)
|
|
||||||
.join("\n") ?? "";
|
|
||||||
|
|
||||||
const match = text ? extractPromptMatch(text) : undefined;
|
const match = text ? extractPromptMatch(text) : undefined;
|
||||||
if (!match) {
|
if (!match) {
|
||||||
ctx.ui.setWidget("prompt-url", undefined);
|
ctx.ui.setWidget("prompt-url", undefined);
|
||||||
|
|
@ -121,5 +132,9 @@ export default function promptUrlWidgetExtension(pi: ExtensionAPI) {
|
||||||
const authorText = formatAuthor(meta?.author);
|
const authorText = formatAuthor(meta?.author);
|
||||||
setWidget(ctx, match, title, authorText);
|
setWidget(ctx, match, title, authorText);
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
pi.on("session_start", async (_event, ctx) => {
|
||||||
|
rebuildFromSession(ctx);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue