@pi-kaush/pi-tool-call-markers 0.3.25 → 0.3.26
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/README.md +2 -2
- package/package.json +1 -1
- package/src/index.ts +113 -81
- package/src/prompt-shell.ts +1 -1
- package/src/question-block.ts +48 -33
package/README.md
CHANGED
|
@@ -107,8 +107,8 @@ The block reuses `pi-content-layout`'s submitted-prompt geometry — the same ra
|
|
|
107
107
|
|
|
108
108
|
- **Shape-first recognition.** Any tool whose result carries an `answers` array of `{ question, answer | selected }` entries renders as a question block, so another author's question tool works without a change here. The tool name (`ask_user_question` and close variants) is the fallback for a call that failed before answering.
|
|
109
109
|
- **Answers, not options.** Each question shows its answer: an option label, typed custom text, or multi-select labels joined with commas. A question left blank reads `User: (no answer)`, a decline reads `User declined to answer questions`, and a validation failure keeps Pi's native failure row.
|
|
110
|
-
- **Quoted questions.** The
|
|
111
|
-
- **Prompt-shell tones.** The rail uses `borderAccent` and the body
|
|
110
|
+
- **Quoted questions.** The `>` marker and every line of the question use `muted` in italics. The `User:` label and reply use `userMessageText`. Only the actual reply is bold, not the label or the blank-answer placeholder.
|
|
111
|
+
- **Prompt-shell tones.** The rail uses `borderAccent` and the body uses `userMessageBg`. A live row's `awaiting your answer…` stays in `warning`.
|
|
112
112
|
- **One blank row per question.** Questions are separated by a painted blank row so a multi-question ask stays scannable. A lone question keeps its outcome on the following line.
|
|
113
113
|
- **No grouping.** Question blocks never join an adjacent tool group and carry no tool glyph. `Ctrl+O` still restores Pi's native expanded rendering.
|
|
114
114
|
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -50,6 +50,8 @@ const TOOL_CALL_GLYPHS: ReadonlyMap<string, string> = new Map([
|
|
|
50
50
|
["get_search_content", "↗"],
|
|
51
51
|
["codemode", "¢"],
|
|
52
52
|
]);
|
|
53
|
+
// Keep the historical slot so old, still-reachable delegates can be handed off.
|
|
54
|
+
const PRESENTATION_GENERATION = 4;
|
|
53
55
|
const PRESENTATION_PATCHED = Symbol.for("kg.pi.toolPresentation.v3");
|
|
54
56
|
const LEGACY_PRESENTATION_PATCHED = Symbol.for("kg.pi.toolPresentation.v2");
|
|
55
57
|
const GROUPING_PATCHED = Symbol.for("kg.pi.toolGrouping.v1");
|
|
@@ -99,10 +101,11 @@ type ToolExecutionRow = {
|
|
|
99
101
|
};
|
|
100
102
|
|
|
101
103
|
type PresentationPatchState = {
|
|
104
|
+
generation?: number;
|
|
102
105
|
owners: number;
|
|
103
106
|
// Set at the final owner's shutdown. A wrapper that another extension has
|
|
104
107
|
// buried cannot be uninstalled, so it delegates instead of outliving its
|
|
105
|
-
// owner; a later install re-enables it.
|
|
108
|
+
// owner; a later compatible install re-enables it.
|
|
106
109
|
disabled?: boolean;
|
|
107
110
|
theme?: ThemeLike;
|
|
108
111
|
cwd: string;
|
|
@@ -1086,15 +1089,30 @@ function isRecord(value: unknown): value is Record<string, unknown> {
|
|
|
1086
1089
|
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
1087
1090
|
}
|
|
1088
1091
|
|
|
1089
|
-
function
|
|
1090
|
-
|
|
1092
|
+
function subagentDisplayProfile(
|
|
1093
|
+
args: Record<string, unknown>,
|
|
1094
|
+
action: unknown,
|
|
1095
|
+
): string | undefined {
|
|
1096
|
+
const { profile, model } = args;
|
|
1097
|
+
if (typeof profile !== "string") return undefined;
|
|
1098
|
+
// Blank model strings do not override launch profiles. Keep the existing
|
|
1099
|
+
// rendering of controls, unknown actions, and malformed model values.
|
|
1100
|
+
const launch =
|
|
1101
|
+
action === undefined || action === "run" || action === "interactive";
|
|
1091
1102
|
if (
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
args.model === undefined
|
|
1103
|
+
model !== undefined &&
|
|
1104
|
+
(!launch || typeof model !== "string" || model.trim() !== "")
|
|
1095
1105
|
)
|
|
1096
|
-
return
|
|
1097
|
-
return
|
|
1106
|
+
return undefined;
|
|
1107
|
+
return profile.trim() || undefined;
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
function defaultSubagentEmoji(
|
|
1111
|
+
action: unknown,
|
|
1112
|
+
profile: string | undefined,
|
|
1113
|
+
): string {
|
|
1114
|
+
if (profile === "coder") return "🧑💻";
|
|
1115
|
+
return typeof action === "string" &&
|
|
1098
1116
|
[
|
|
1099
1117
|
"interactive",
|
|
1100
1118
|
"list",
|
|
@@ -1103,15 +1121,34 @@ function defaultSubagentEmoji(args: unknown): string {
|
|
|
1103
1121
|
"wait",
|
|
1104
1122
|
"stop",
|
|
1105
1123
|
"resume",
|
|
1106
|
-
].includes(
|
|
1124
|
+
].includes(action)
|
|
1107
1125
|
? "🤖"
|
|
1108
1126
|
: "👾";
|
|
1109
1127
|
}
|
|
1110
1128
|
|
|
1129
|
+
// Match a whole icon token, including joined emoji, flags, and keycaps.
|
|
1130
|
+
// ASCII digits, #, and * count only in keycap sequences.
|
|
1131
|
+
const SUBAGENT_EMOJI_TOKEN =
|
|
1132
|
+
/^(?:[#*0-9]\uFE0F?\u20E3|(?![#*0-9])\p{Emoji}[\uFE0E\uFE0F\u{E0020}-\u{E007F}]*\u200D?)+$/u;
|
|
1133
|
+
|
|
1111
1134
|
function subagentFallbackLabel(args: unknown, summary: string): string {
|
|
1112
|
-
const
|
|
1113
|
-
const
|
|
1114
|
-
|
|
1135
|
+
const input = isRecord(args) ? args : undefined;
|
|
1136
|
+
const match = /^(\S+)\s+(?=\S)/u.exec(summary);
|
|
1137
|
+
const icon = match?.[1] ?? "";
|
|
1138
|
+
const heading = match ? summary.slice(match[0].length) : "";
|
|
1139
|
+
const agent =
|
|
1140
|
+
typeof input?.agent === "string" ? sanitizeInline(input.agent).trim() : "";
|
|
1141
|
+
const startsWithAgent = (text: string) =>
|
|
1142
|
+
agent !== "" && (text === agent || text.startsWith(`${agent} `));
|
|
1143
|
+
// Subagents also accept configured Unicode symbols as icons. A token
|
|
1144
|
+
// directly before the known name can still be one of those assignments.
|
|
1145
|
+
const assigned = startsWithAgent(heading)
|
|
1146
|
+
? /[^\x00-\x7f]/u.test(icon)
|
|
1147
|
+
: !startsWithAgent(summary) && SUBAGENT_EMOJI_TOKEN.test(icon);
|
|
1148
|
+
const action = input?.action;
|
|
1149
|
+
const prefix = assigned
|
|
1150
|
+
? ""
|
|
1151
|
+
: `${defaultSubagentEmoji(action, input && subagentDisplayProfile(input, action))} `;
|
|
1115
1152
|
return `subagent ${prefix}${summary}`.trimEnd();
|
|
1116
1153
|
}
|
|
1117
1154
|
|
|
@@ -1127,8 +1164,9 @@ type SubagentPlan = {
|
|
|
1127
1164
|
steps: readonly [SubagentStep, ...SubagentStep[]];
|
|
1128
1165
|
};
|
|
1129
1166
|
|
|
1130
|
-
function parseSubagentArgs(
|
|
1131
|
-
|
|
1167
|
+
function parseSubagentArgs(
|
|
1168
|
+
args: Record<string, unknown>,
|
|
1169
|
+
): SubagentPlan | undefined {
|
|
1132
1170
|
const scope =
|
|
1133
1171
|
args.agentScope === "project" || args.agentScope === "both"
|
|
1134
1172
|
? args.agentScope
|
|
@@ -1148,14 +1186,7 @@ function parseSubagentArgs(args: unknown): SubagentPlan | undefined {
|
|
|
1148
1186
|
typeof item.agent === "string" && item.agent.trim()
|
|
1149
1187
|
? item.agent.trim()
|
|
1150
1188
|
: "...",
|
|
1151
|
-
|
|
1152
|
-
// override is present; mirror that here.
|
|
1153
|
-
profile:
|
|
1154
|
-
typeof item.profile === "string" &&
|
|
1155
|
-
item.profile.trim() &&
|
|
1156
|
-
item.model === undefined
|
|
1157
|
-
? item.profile.trim()
|
|
1158
|
-
: undefined,
|
|
1189
|
+
profile: subagentDisplayProfile(item, args.action),
|
|
1159
1190
|
task: item.task,
|
|
1160
1191
|
};
|
|
1161
1192
|
};
|
|
@@ -1187,40 +1218,21 @@ function parseSubagentArgs(args: unknown): SubagentPlan | undefined {
|
|
|
1187
1218
|
return { kind: "single", scope, steps: [single] };
|
|
1188
1219
|
}
|
|
1189
1220
|
|
|
1190
|
-
//
|
|
1191
|
-
function parseStepContent(
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
if (tokens.length === 0) return { displayName: "", bareName: "" };
|
|
1197
|
-
const first = tokens[0]!;
|
|
1221
|
+
// Extract the existing emoji-capable prefix and its bare lookup name.
|
|
1222
|
+
function parseStepContent(
|
|
1223
|
+
content: string,
|
|
1224
|
+
): { emoji: string; bareName: string } | undefined {
|
|
1225
|
+
const [first, bareName] = content.trim().split(/\s+/).filter(Boolean);
|
|
1226
|
+
if (!first || !bareName) return undefined;
|
|
1198
1227
|
const hasEmoji = [...first].some((char) => (char.codePointAt(0) ?? 0) > 0x7f);
|
|
1199
|
-
|
|
1200
|
-
const displayName = tokens.slice(0, nameTokens).join(" ");
|
|
1201
|
-
return {
|
|
1202
|
-
displayName,
|
|
1203
|
-
bareName: tokens[nameTokens - 1] ?? displayName,
|
|
1204
|
-
};
|
|
1228
|
+
return hasEmoji ? { emoji: first, bareName } : undefined;
|
|
1205
1229
|
}
|
|
1206
1230
|
|
|
1207
|
-
//
|
|
1208
|
-
//
|
|
1209
|
-
function
|
|
1210
|
-
displayName: string,
|
|
1211
|
-
bareName: string,
|
|
1212
|
-
): { emoji: string; name: string } {
|
|
1213
|
-
const tokens = displayName.trim().split(/\s+/).filter(Boolean);
|
|
1214
|
-
if (tokens.length > 1 && tokens[tokens.length - 1] === bareName) {
|
|
1215
|
-
return { emoji: tokens.slice(0, -1).join(" "), name: bareName };
|
|
1216
|
-
}
|
|
1217
|
-
return { emoji: "", name: bareName };
|
|
1218
|
-
}
|
|
1219
|
-
|
|
1220
|
-
// Agent display names (emoji + name) come from the subagent extension's own
|
|
1221
|
-
// call component when available; args only carry the bare agent name.
|
|
1222
|
-
function scrapeSubagentDisplayNames(
|
|
1231
|
+
// Assigned emojis come from the subagent extension's call component when
|
|
1232
|
+
// available; args only carry the bare agent name.
|
|
1233
|
+
function scrapeSubagentEmojis(
|
|
1223
1234
|
row: ToolExecutionRow,
|
|
1235
|
+
kind: SubagentPlan["kind"],
|
|
1224
1236
|
): Map<string, string> {
|
|
1225
1237
|
const names = new Map<string, string>();
|
|
1226
1238
|
let component = row.callRendererComponent;
|
|
@@ -1230,20 +1242,21 @@ function scrapeSubagentDisplayNames(
|
|
|
1230
1242
|
if (!component || typeof component.render !== "function") return names;
|
|
1231
1243
|
try {
|
|
1232
1244
|
for (const raw of component.render(120)) {
|
|
1233
|
-
const
|
|
1245
|
+
const text = sanitizeInline(stripAnsi(raw));
|
|
1246
|
+
// Native single-call headings can wrap. The first indented line starts
|
|
1247
|
+
// the task preview; neither it nor its continuations carry identity.
|
|
1248
|
+
if (kind === "single" && text.startsWith(" ")) break;
|
|
1249
|
+
const line = text.trim();
|
|
1234
1250
|
if (!line) continue;
|
|
1235
1251
|
// Single calls carry the agent name on their "subagent <name>" heading
|
|
1236
1252
|
// line; stripping the heading word (and step numbers) lets both shapes
|
|
1237
1253
|
// flow through the same parse. Heading remainders without an emoji
|
|
1238
|
-
// (e.g. "chain (2 steps)")
|
|
1239
|
-
// skipped by the guard below.
|
|
1254
|
+
// (e.g. "chain (2 steps)") are skipped.
|
|
1240
1255
|
const content = line
|
|
1241
1256
|
.replace(/^subagent\b\s*/, "")
|
|
1242
1257
|
.replace(/^\d+\.\s*/, "");
|
|
1243
|
-
const
|
|
1244
|
-
if (
|
|
1245
|
-
names.set(bareName, displayName);
|
|
1246
|
-
}
|
|
1258
|
+
const identity = parseStepContent(content);
|
|
1259
|
+
if (identity) names.set(identity.bareName, identity.emoji);
|
|
1247
1260
|
}
|
|
1248
1261
|
} catch {
|
|
1249
1262
|
// Display-name scraping is cosmetic; args still carry the bare name.
|
|
@@ -1313,6 +1326,8 @@ function renderSubagentPlan(
|
|
|
1313
1326
|
width: number,
|
|
1314
1327
|
theme: ThemeLike,
|
|
1315
1328
|
): string[] | undefined {
|
|
1329
|
+
if (!isRecord(row.args)) return undefined;
|
|
1330
|
+
const { action } = row.args;
|
|
1316
1331
|
const plan = parseSubagentArgs(row.args);
|
|
1317
1332
|
if (!plan || width < 12) return undefined;
|
|
1318
1333
|
|
|
@@ -1320,24 +1335,19 @@ function renderSubagentPlan(
|
|
|
1320
1335
|
const color = failed ? "error" : "muted";
|
|
1321
1336
|
const nameColor = failed ? "error" : "accent";
|
|
1322
1337
|
const suffix = fgCollapsed(theme, color, "…");
|
|
1323
|
-
const
|
|
1338
|
+
const displayEmojis = scrapeSubagentEmojis(row, plan.kind);
|
|
1324
1339
|
// Agent/profile/task values are model-supplied; sanitize them like every
|
|
1325
1340
|
// other collapsed-row text so control bytes cannot reach the terminal.
|
|
1326
1341
|
// Identity order: emoji, then the profile and agent-name badges.
|
|
1327
1342
|
const identityOf = (step: SubagentStep) => {
|
|
1328
|
-
const
|
|
1329
|
-
displayNames.get(step.agent) ?? sanitizeInline(step.agent);
|
|
1330
|
-
const { emoji, name } = splitEmojiName(displayName, step.agent);
|
|
1343
|
+
const emoji = displayEmojis.get(step.agent);
|
|
1331
1344
|
const accentPart = (text: string) => fgCollapsed(theme, nameColor, text);
|
|
1332
|
-
const fallbackEmoji = defaultSubagentEmoji(
|
|
1333
|
-
action: isRecord(row.args) ? row.args.action : undefined,
|
|
1334
|
-
profile: step.profile,
|
|
1335
|
-
});
|
|
1345
|
+
const fallbackEmoji = defaultSubagentEmoji(action, step.profile);
|
|
1336
1346
|
const emojiPart = `${accentPart(emoji || fallbackEmoji)} `;
|
|
1337
1347
|
const profilePart = step.profile
|
|
1338
1348
|
? accentPart(`[${sanitizeInline(step.profile)}]`)
|
|
1339
1349
|
: "";
|
|
1340
|
-
return `${emojiPart}${profilePart}${accentPart(`[${sanitizeInline(
|
|
1350
|
+
return `${emojiPart}${profilePart}${accentPart(`[${sanitizeInline(step.agent)}]`)}`;
|
|
1341
1351
|
};
|
|
1342
1352
|
const detailColor = failed ? "error" : "toolOutput";
|
|
1343
1353
|
const previewOf = (step: SubagentStep) =>
|
|
@@ -1732,12 +1742,16 @@ function installPresentationPatch(): PresentationPatchState | undefined {
|
|
|
1732
1742
|
if (!proto) return undefined;
|
|
1733
1743
|
|
|
1734
1744
|
const existing = proto[PRESENTATION_PATCHED];
|
|
1735
|
-
|
|
1745
|
+
const retired =
|
|
1746
|
+
existing?.disabled && existing.generation !== PRESENTATION_GENERATION
|
|
1747
|
+
? existing
|
|
1748
|
+
: undefined;
|
|
1749
|
+
if (existing && !retired) {
|
|
1736
1750
|
existing.owners++;
|
|
1737
1751
|
existing.disabled = false;
|
|
1738
1752
|
return existing;
|
|
1739
1753
|
}
|
|
1740
|
-
const legacy = proto[LEGACY_PRESENTATION_PATCHED];
|
|
1754
|
+
const legacy = !existing && proto[LEGACY_PRESENTATION_PATCHED];
|
|
1741
1755
|
if (legacy) {
|
|
1742
1756
|
proto.render = legacy.originalRender;
|
|
1743
1757
|
proto.updateDisplay = legacy.originalUpdateDisplay;
|
|
@@ -1748,17 +1762,25 @@ function installPresentationPatch(): PresentationPatchState | undefined {
|
|
|
1748
1762
|
)
|
|
1749
1763
|
return undefined;
|
|
1750
1764
|
|
|
1765
|
+
const { render, updateDisplay } = proto;
|
|
1766
|
+
const buriedRender = retired && render !== retired.patchedRender;
|
|
1767
|
+
const buriedUpdate =
|
|
1768
|
+
retired && updateDisplay !== retired.patchedUpdateDisplay;
|
|
1769
|
+
const retiredMouse = retired?.originalHandleMouse;
|
|
1770
|
+
|
|
1751
1771
|
const state: PresentationPatchState = {
|
|
1772
|
+
generation: PRESENTATION_GENERATION,
|
|
1752
1773
|
owners: 1,
|
|
1753
1774
|
cwd: process.cwd(),
|
|
1754
1775
|
promptPaths: new Set(),
|
|
1755
|
-
collapseParallel:
|
|
1756
|
-
|
|
1776
|
+
collapseParallel:
|
|
1777
|
+
retired?.collapseParallel ?? envEnabled(COLLAPSE_PARALLEL_ENV, true),
|
|
1778
|
+
expandedTools: retired?.expandedTools ?? envToolNames(EXPANDED_TOOLS_ENV),
|
|
1757
1779
|
groupCache: new WeakMap(),
|
|
1758
1780
|
collapsedCache: new WeakMap(),
|
|
1759
1781
|
rowVersions: new WeakMap(),
|
|
1760
|
-
originalRender:
|
|
1761
|
-
originalUpdateDisplay:
|
|
1782
|
+
originalRender: retired?.originalRender ?? render,
|
|
1783
|
+
originalUpdateDisplay: retired?.originalUpdateDisplay ?? updateDisplay,
|
|
1762
1784
|
};
|
|
1763
1785
|
const patchedUpdateDisplay = function updateDisplayWithToolPresentation(
|
|
1764
1786
|
this: ToolExecutionRow,
|
|
@@ -1792,8 +1814,9 @@ function installPresentationPatch(): PresentationPatchState | undefined {
|
|
|
1792
1814
|
let patchedHandleMouse:
|
|
1793
1815
|
| ((this: ToolExecutionRow, event: unknown) => unknown)
|
|
1794
1816
|
| undefined;
|
|
1795
|
-
|
|
1796
|
-
|
|
1817
|
+
const originalHandleMouse = retiredMouse ?? proto.handleMouse;
|
|
1818
|
+
if (typeof originalHandleMouse === "function") {
|
|
1819
|
+
state.originalHandleMouse = originalHandleMouse;
|
|
1797
1820
|
patchedHandleMouse = function handleMouseWithCollapsedToggle(
|
|
1798
1821
|
this: ToolExecutionRow,
|
|
1799
1822
|
event: unknown,
|
|
@@ -1815,7 +1838,7 @@ function installPresentationPatch(): PresentationPatchState | undefined {
|
|
|
1815
1838
|
}
|
|
1816
1839
|
return state.originalHandleMouse?.call(this, event);
|
|
1817
1840
|
};
|
|
1818
|
-
proto.handleMouse = patchedHandleMouse;
|
|
1841
|
+
if (!retiredMouse) proto.handleMouse = patchedHandleMouse;
|
|
1819
1842
|
}
|
|
1820
1843
|
const patchedRender = function renderWithToolPresentation(
|
|
1821
1844
|
this: ToolExecutionRow,
|
|
@@ -1895,15 +1918,24 @@ function installPresentationPatch(): PresentationPatchState | undefined {
|
|
|
1895
1918
|
try {
|
|
1896
1919
|
state.patchedUpdateDisplay = patchedUpdateDisplay;
|
|
1897
1920
|
state.patchedRender = patchedRender;
|
|
1898
|
-
proto.updateDisplay = patchedUpdateDisplay;
|
|
1899
|
-
proto.render = patchedRender;
|
|
1921
|
+
if (!buriedUpdate) proto.updateDisplay = patchedUpdateDisplay;
|
|
1922
|
+
if (!buriedRender) proto.render = patchedRender;
|
|
1900
1923
|
Object.defineProperty(proto, PRESENTATION_PATCHED, {
|
|
1901
1924
|
configurable: true,
|
|
1902
1925
|
value: state,
|
|
1903
1926
|
});
|
|
1927
|
+
if (retired) {
|
|
1928
|
+
// Keep foreign wrappers on the outside, including on cached renders.
|
|
1929
|
+
// The retired closures stay disabled and only forward to these delegates.
|
|
1930
|
+
if (buriedRender) retired.originalRender = patchedRender;
|
|
1931
|
+
if (buriedUpdate) retired.originalUpdateDisplay = patchedUpdateDisplay;
|
|
1932
|
+
if (retiredMouse && patchedHandleMouse) {
|
|
1933
|
+
retired.originalHandleMouse = patchedHandleMouse;
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1904
1936
|
} catch {
|
|
1905
|
-
proto.updateDisplay =
|
|
1906
|
-
proto.render =
|
|
1937
|
+
if (!buriedUpdate) proto.updateDisplay = updateDisplay;
|
|
1938
|
+
if (!buriedRender) proto.render = render;
|
|
1907
1939
|
return undefined;
|
|
1908
1940
|
}
|
|
1909
1941
|
return state;
|
package/src/prompt-shell.ts
CHANGED
|
@@ -158,7 +158,7 @@ export function renderRailedBlockLines(
|
|
|
158
158
|
const margin = " ".repeat(inset);
|
|
159
159
|
const leftPad = " ".repeat(padding.left);
|
|
160
160
|
const rightPad = " ".repeat(padding.right);
|
|
161
|
-
const contentWidth =
|
|
161
|
+
const contentWidth = promptBlockContentWidth(width, inset, padding);
|
|
162
162
|
const surfaceBg = promptSurfaceBg(theme);
|
|
163
163
|
return lines.map((line) => {
|
|
164
164
|
const { controls, content } = splitLeadingSemanticControls(line);
|
package/src/question-block.ts
CHANGED
|
@@ -16,7 +16,7 @@ import { sanitizeInline } from "./sanitize.ts";
|
|
|
16
16
|
// any particular question-tool implementation is needed.
|
|
17
17
|
|
|
18
18
|
type QuestionTheme = PromptShellTheme & {
|
|
19
|
-
|
|
19
|
+
bold?(text: string): string;
|
|
20
20
|
italic?(text: string): string;
|
|
21
21
|
};
|
|
22
22
|
|
|
@@ -40,7 +40,7 @@ type AnswerEntry = {
|
|
|
40
40
|
|
|
41
41
|
type QuestionLine =
|
|
42
42
|
| { kind: "question"; text: string }
|
|
43
|
-
| { kind: "answer"; text: string }
|
|
43
|
+
| { kind: "answer"; text: string | undefined }
|
|
44
44
|
| { kind: "status"; text: string; tone: "muted" | "warning" }
|
|
45
45
|
// A painted blank body row; the shell gives it the prompt surface.
|
|
46
46
|
| { kind: "spacer" };
|
|
@@ -196,7 +196,7 @@ function questionLines(
|
|
|
196
196
|
const groups: QuestionLine[][] = pairs.map((pair) => {
|
|
197
197
|
const group: QuestionLine[] = [{ kind: "question", text: pair.question }];
|
|
198
198
|
if (!pending && !declined) {
|
|
199
|
-
group.push({ kind: "answer", text: pair.answer
|
|
199
|
+
group.push({ kind: "answer", text: pair.answer });
|
|
200
200
|
}
|
|
201
201
|
return group;
|
|
202
202
|
});
|
|
@@ -228,18 +228,21 @@ function fg(theme: QuestionTheme, token: string, text: string): string {
|
|
|
228
228
|
}
|
|
229
229
|
}
|
|
230
230
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
231
|
+
function emphasize(
|
|
232
|
+
theme: QuestionTheme,
|
|
233
|
+
style: "bold" | "italic",
|
|
234
|
+
text: string,
|
|
235
|
+
): string {
|
|
236
|
+
if (text === "" || typeof theme[style] !== "function") return text;
|
|
236
237
|
try {
|
|
237
|
-
return theme
|
|
238
|
+
return theme[style](text);
|
|
238
239
|
} catch {
|
|
239
240
|
return text;
|
|
240
241
|
}
|
|
241
242
|
}
|
|
242
243
|
|
|
244
|
+
const graphemes = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
245
|
+
|
|
243
246
|
// Plain-text word wrap with a hanging indent, so a wrapped question stays
|
|
244
247
|
// visually attached to its `> ` prefix. Wrapping before styling keeps the
|
|
245
248
|
// width math exact and lets the shell's own fit pass stay a no-op.
|
|
@@ -261,15 +264,24 @@ function wrapPlain(text: string, width: number): string[] {
|
|
|
261
264
|
return;
|
|
262
265
|
}
|
|
263
266
|
if (current !== "") lines.push(current);
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
267
|
+
if (visibleWidth(word) <= max) {
|
|
268
|
+
current = word;
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
current = "";
|
|
273
|
+
let columns = 0;
|
|
274
|
+
for (const { segment } of graphemes.segment(word)) {
|
|
275
|
+
const segmentWidth = visibleWidth(segment);
|
|
276
|
+
// Let the shell clip a grapheme wider than the whole line.
|
|
277
|
+
if (current !== "" && columns + segmentWidth > max) {
|
|
278
|
+
lines.push(current);
|
|
279
|
+
current = "";
|
|
280
|
+
columns = 0;
|
|
281
|
+
}
|
|
282
|
+
current += segment;
|
|
283
|
+
columns += segmentWidth;
|
|
271
284
|
}
|
|
272
|
-
current = rest;
|
|
273
285
|
};
|
|
274
286
|
for (const word of words) push(word);
|
|
275
287
|
if (current !== "") lines.push(current);
|
|
@@ -288,22 +300,25 @@ function styleLine(
|
|
|
288
300
|
: line.kind === "answer"
|
|
289
301
|
? ANSWER_PREFIX
|
|
290
302
|
: "";
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
303
|
+
const text =
|
|
304
|
+
line.kind === "answer" ? (line.text ?? NO_ANSWER_TEXT) : line.text;
|
|
305
|
+
const prefixWidth = visibleWidth(prefix);
|
|
306
|
+
const hang = " ".repeat(prefixWidth);
|
|
307
|
+
const body = wrapPlain(text, Math.max(1, width - prefixWidth));
|
|
308
|
+
return body.map((piece, index) => {
|
|
309
|
+
const leading = index === 0 ? prefix : hang;
|
|
310
|
+
switch (line.kind) {
|
|
311
|
+
case "question":
|
|
312
|
+
return fg(theme, "muted", emphasize(theme, "italic", leading + piece));
|
|
313
|
+
case "answer": {
|
|
314
|
+
const reply =
|
|
315
|
+
line.text === undefined ? piece : emphasize(theme, "bold", piece);
|
|
316
|
+
return fg(theme, "userMessageText", leading + reply);
|
|
317
|
+
}
|
|
318
|
+
case "status":
|
|
319
|
+
return fg(theme, line.tone, piece);
|
|
320
|
+
}
|
|
321
|
+
});
|
|
307
322
|
}
|
|
308
323
|
|
|
309
324
|
/**
|