@narumitw/pi-firecrawl 0.35.0 → 0.39.0

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 CHANGED
@@ -16,7 +16,7 @@ Use it to give your AI coding agent reliable web research capabilities for docum
16
16
  - Supports Firecrawl API endpoint overrides.
17
17
  - Shows statusline activity only while Firecrawl tools are running.
18
18
  - Provides a `/firecrawl` menu with configuration help and tool controls.
19
- - Provides a Plan-mode-style selector for choosing individual Firecrawl tools.
19
+ - Uses `@narumitw/pi-tui-kit` for width-safe menus and individual tool selection.
20
20
  - Persists the selected Firecrawl tools across Pi restarts.
21
21
  - Bounds tool output to Pi's 50 KB or 2,000-line limit while preserving oversized responses in private temporary files.
22
22
  - Never logs, displays, or stores your Firecrawl API key.
@@ -99,7 +99,7 @@ Direct subcommands are also available:
99
99
  - `quickstart` is an alias for `config`.
100
100
  - `status` shows runtime tool state, persisted selection, settings file path, API-key presence,
101
101
  API URL, and active non-Firecrawl tool count.
102
- - `tools` opens a Plan-mode-style selector for choosing individual `firecrawl_*` tools.
102
+ - `tools` opens a width-safe selector for choosing individual `firecrawl_*` tools.
103
103
  - `toggle` is an alias for `tools`.
104
104
  - `enable` enables all `firecrawl_*` tools for future turns.
105
105
  - `disable` disables all `firecrawl_*` tools for future turns. The slash command remains
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narumitw/pi-firecrawl",
3
- "version": "0.35.0",
3
+ "version": "0.39.0",
4
4
  "description": "Pi extension that exposes Firecrawl web scraping and crawling tools.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -29,11 +29,12 @@
29
29
  "typecheck": "tsc --noEmit"
30
30
  },
31
31
  "dependencies": {
32
+ "@narumitw/pi-tui-kit": "<1",
32
33
  "typebox": "^1.3.8"
33
34
  },
34
35
  "devDependencies": {
35
- "@biomejs/biome": "2.5.5",
36
- "@earendil-works/pi-coding-agent": "0.82.1",
36
+ "@biomejs/biome": "2.5.6",
37
+ "@earendil-works/pi-coding-agent": "0.83.0",
37
38
  "typescript": "7.0.2"
38
39
  },
39
40
  "repository": {
package/src/firecrawl.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
2
+ import { defineMenu, runMenu } from "@narumitw/pi-tui-kit";
2
3
  import { hasApiKey } from "./client.js";
3
4
  import { cleanupResponseArtifacts, openResponseArtifacts } from "./response-format.js";
4
5
  import { loadSettings } from "./settings.js";
@@ -11,6 +12,7 @@ import {
11
12
  buildStatusMessage,
12
13
  clearSettingsNotice,
13
14
  currentFirecrawlSessionGeneration,
15
+ currentFirecrawlSessionSignal,
14
16
  isCurrentFirecrawlSession,
15
17
  recordSettingsNotice,
16
18
  showToolSelector,
@@ -137,31 +139,57 @@ async function showMenu(pi: ExtensionAPI, ctx: CommandContext, generation: numbe
137
139
  return;
138
140
  }
139
141
 
140
- const choice = await ctx.ui.select("Firecrawl", Object.values(MENU_OPTIONS));
141
- if (!isCurrentFirecrawlSession(generation)) return;
142
- switch (choice) {
143
- case MENU_OPTIONS.config:
144
- ctx.ui.notify(buildConfigMessage(), hasApiKey() ? "info" : "warning");
145
- return;
146
- case MENU_OPTIONS.help:
147
- ctx.ui.notify(buildCommandGuide(), "info");
148
- return;
149
- case MENU_OPTIONS.status: {
150
- const status = await buildStatusMessage(pi);
151
- if (!isCurrentFirecrawlSession(generation)) return;
152
- ctx.ui.notify(status, hasApiKey() ? "info" : "warning");
153
- return;
154
- }
155
- case MENU_OPTIONS.tools:
156
- await showToolSelector(pi, ctx);
157
- return;
158
- case MENU_OPTIONS.enable:
159
- await updateFirecrawlTools(pi, ctx, allFirecrawlTools(), "enabled all");
160
- return;
161
- case MENU_OPTIONS.disable:
162
- await updateFirecrawlTools(pi, ctx, [], "disabled all");
163
- return;
164
- }
142
+ type Screen = "main";
143
+ type Action = keyof typeof MENU_OPTIONS;
144
+ const menu = defineMenu<undefined, Screen, Action>({
145
+ start: "main",
146
+ screens: {
147
+ main: () => ({
148
+ kind: "actions",
149
+ title: "Firecrawl",
150
+ items: Object.entries(MENU_OPTIONS).map(([id, label]) => ({
151
+ id,
152
+ label,
153
+ action: id as Action,
154
+ })),
155
+ hint: "close",
156
+ }),
157
+ },
158
+ actions: {
159
+ config: async () => {
160
+ ctx.ui.notify(buildConfigMessage(), hasApiKey() ? "info" : "warning");
161
+ return { kind: "close" };
162
+ },
163
+ help: async () => {
164
+ ctx.ui.notify(buildCommandGuide(), "info");
165
+ return { kind: "close" };
166
+ },
167
+ status: async () => {
168
+ const status = await buildStatusMessage(pi);
169
+ if (isCurrentFirecrawlSession(generation)) {
170
+ ctx.ui.notify(status, hasApiKey() ? "info" : "warning");
171
+ }
172
+ return { kind: "close" };
173
+ },
174
+ tools: async () => {
175
+ await showToolSelector(pi, ctx);
176
+ return { kind: "close" };
177
+ },
178
+ enable: async () => {
179
+ await updateFirecrawlTools(pi, ctx, allFirecrawlTools(), "enabled all");
180
+ return { kind: "close" };
181
+ },
182
+ disable: async () => {
183
+ await updateFirecrawlTools(pi, ctx, [], "disabled all");
184
+ return { kind: "close" };
185
+ },
186
+ },
187
+ });
188
+ await runMenu(ctx, menu, {
189
+ getState: () => undefined,
190
+ signal: currentFirecrawlSessionSignal(),
191
+ isCurrent: () => isCurrentFirecrawlSession(generation),
192
+ });
165
193
  }
166
194
 
167
195
  export function parseCommand(args: string): CommandAction | "unknown" {
@@ -1,4 +1,5 @@
1
1
  import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
2
+ import { defineMenu, runMenu } from "@narumitw/pi-tui-kit";
2
3
  import { configuredApiUrl, hasApiKey } from "./client.js";
3
4
  import {
4
5
  loadSettings,
@@ -9,15 +10,9 @@ import {
9
10
  import { FIRECRAWL_TOOL_NAMES, type FirecrawlToolName } from "./tools.js";
10
11
 
11
12
  type CommandContext = ExtensionCommandContext;
12
-
13
- const TOOL_SELECTOR_DONE = "Done";
14
- const TOOL_SELECTOR_ENABLE_ALL = "Enable all Firecrawl tools";
15
- const TOOL_SELECTOR_DISABLE_ALL = "Disable all Firecrawl tools";
16
13
  type ToolRuntimeStatus = "enabled" | "disabled" | "partial";
17
- type ToolSelectorAction = "enableAll" | "disableAll" | "done";
18
- type ToolSelectorRow =
19
- | { kind: "tool"; toolName: FirecrawlToolName }
20
- | { kind: "action"; action: ToolSelectorAction; label: string };
14
+ type ToolSelectorScreen = "tools";
15
+ type ToolSelectorAction = "toggle" | "enableAll" | "disableAll";
21
16
  interface ToolStatusSummary {
22
17
  runtimeStatus: ToolRuntimeStatus;
23
18
  activeFirecrawlToolCount: number;
@@ -26,8 +21,11 @@ interface ToolStatusSummary {
26
21
 
27
22
  let settingsNotice: string | undefined;
28
23
  let sessionGeneration = 0;
24
+ let sessionController = new AbortController();
29
25
 
30
26
  export function advanceFirecrawlSessionGeneration(): number {
27
+ sessionController.abort(new DOMException("Firecrawl session replaced", "AbortError"));
28
+ sessionController = new AbortController();
31
29
  return ++sessionGeneration;
32
30
  }
33
31
 
@@ -39,6 +37,10 @@ export function isCurrentFirecrawlSession(generation: number): boolean {
39
37
  return generation === sessionGeneration;
40
38
  }
41
39
 
40
+ export function currentFirecrawlSessionSignal(): AbortSignal {
41
+ return sessionController.signal;
42
+ }
43
+
42
44
  export function clearSettingsNotice() {
43
45
  settingsNotice = undefined;
44
46
  }
@@ -50,156 +52,68 @@ export function recordSettingsNotice(settings: SettingsLoadResult) {
50
52
  export async function showToolSelector(pi: ExtensionAPI, ctx: CommandContext) {
51
53
  const generation = sessionGeneration;
52
54
  if (!ctx.hasUI) return;
53
- if (ctx.mode !== "tui") {
54
- await showDialogToolSelector(pi, ctx, generation);
55
- return;
56
- }
57
-
58
- let selectedTools = new Set<FirecrawlToolName>(getActiveFirecrawlTools(pi));
59
- let persistQueue = Promise.resolve();
60
- let requestedRevision = 0;
61
- let requestRender: () => void = () => undefined;
62
- const commitSelectedTools = () => {
63
- const nextSelectedTools = orderedFirecrawlTools(selectedTools);
64
- const revision = ++requestedRevision;
65
- persistQueue = persistQueue.then(async () => {
66
- const saved = await transactSelectedTools(pi, ctx, nextSelectedTools, generation);
67
- if (!saved && isCurrentFirecrawlSession(generation) && revision === requestedRevision) {
68
- selectedTools = new Set(getActiveFirecrawlTools(pi));
69
- requestRender();
70
- }
71
- });
72
- };
73
-
74
- const customResult = await ctx.ui.custom<"closed" | undefined>(
75
- (tui, theme, keybindings, done) => {
76
- requestRender = () => tui.requestRender();
77
- const rows = firecrawlToolSelectorRows();
78
- let selectedIndex = 0;
79
- const moveSelection = (delta: number) => {
80
- selectedIndex = (selectedIndex + delta + rows.length) % rows.length;
81
- };
82
- const activateSelectedRow = () => {
83
- const row = rows[selectedIndex];
84
- if (!row) return;
85
-
86
- if (row.kind === "tool") {
87
- if (selectedTools.has(row.toolName)) selectedTools.delete(row.toolName);
88
- else selectedTools.add(row.toolName);
89
- commitSelectedTools();
90
- return;
91
- }
92
-
93
- if (row.action === "enableAll") {
94
- selectedTools = new Set(allFirecrawlTools());
95
- commitSelectedTools();
96
- return;
97
- }
98
- if (row.action === "disableAll") {
99
- selectedTools = new Set();
100
- commitSelectedTools();
101
- return;
102
- }
103
-
104
- done("closed");
105
- };
106
-
107
- return {
108
- invalidate() {},
109
- render() {
110
- return [
111
- theme.fg("accent", theme.bold(toolSelectorTitle(selectedTools))),
112
- "",
113
- ...rows.map((row, index) => {
114
- const label = formatToolSelectorRow(row, selectedTools);
115
- if (index === selectedIndex) {
116
- return `${theme.fg("accent", "›")} ${theme.fg("accent", label)}`;
117
- }
118
- return ` ${label}`;
119
- }),
120
- "",
121
- theme.fg("dim", "↑↓ navigate • Enter/Space toggle • Esc close"),
122
- ];
123
- },
124
- handleInput(data: string) {
125
- if (keybindings.matches(data, "tui.select.up")) {
126
- moveSelection(-1);
127
- tui.requestRender();
128
- return;
129
- }
130
- if (keybindings.matches(data, "tui.select.down")) {
131
- moveSelection(1);
132
- tui.requestRender();
133
- return;
134
- }
135
- if (keybindings.matches(data, "tui.select.pageUp")) {
136
- selectedIndex = 0;
137
- tui.requestRender();
138
- return;
139
- }
140
- if (keybindings.matches(data, "tui.select.pageDown")) {
141
- selectedIndex = rows.length - 1;
142
- tui.requestRender();
143
- return;
144
- }
145
- if (keybindings.matches(data, "tui.select.confirm") || data === " ") {
146
- activateSelectedRow();
147
- tui.requestRender();
148
- return;
149
- }
150
- if (keybindings.matches(data, "tui.select.cancel")) {
151
- done("closed");
152
- }
153
- },
154
- };
55
+ const menu = defineMenu<undefined, ToolSelectorScreen, ToolSelectorAction>({
56
+ start: "tools",
57
+ screens: {
58
+ tools: () => {
59
+ const selectedTools = new Set(getActiveFirecrawlTools(pi));
60
+ return {
61
+ kind: "multiSelect",
62
+ title: toolSelectorTitle(selectedTools),
63
+ items: FIRECRAWL_TOOL_NAMES.map((toolName) => ({
64
+ id: toolName,
65
+ label: toolName,
66
+ selected: selectedTools.has(toolName),
67
+ })),
68
+ action: "toggle",
69
+ actions: [
70
+ {
71
+ id: "enable-all",
72
+ label: "Enable all Firecrawl tools",
73
+ action: "enableAll",
74
+ },
75
+ {
76
+ id: "disable-all",
77
+ label: "Disable all Firecrawl tools",
78
+ action: "disableAll",
79
+ },
80
+ { id: "done", label: "Done", close: true },
81
+ ],
82
+ hint: "close",
83
+ doneLabel: "Done",
84
+ };
85
+ },
155
86
  },
156
- );
157
-
158
- if (!isCurrentFirecrawlSession(generation)) return;
159
- if (customResult !== "closed") {
160
- await showDialogToolSelector(pi, ctx, generation);
161
- return;
162
- }
163
-
164
- await persistQueue;
165
- if (!isCurrentFirecrawlSession(generation)) return;
166
- const status = await buildStatusMessage(pi);
167
- if (!isCurrentFirecrawlSession(generation)) return;
168
- ctx.ui.notify(status, hasApiKey() ? "info" : "warning");
169
- }
170
-
171
- async function showDialogToolSelector(pi: ExtensionAPI, ctx: CommandContext, generation: number) {
172
- let selectedTools = new Set<FirecrawlToolName>(getActiveFirecrawlTools(pi));
173
- while (true) {
174
- const rows = firecrawlToolSelectorRows();
175
- const choices = rows.map((row) => formatToolSelectorRow(row, selectedTools));
176
- const choice = await ctx.ui.select(toolSelectorTitle(selectedTools), choices);
177
- if (!isCurrentFirecrawlSession(generation) || !choice) break;
178
-
179
- const row = rows[choices.indexOf(choice)];
180
- if (!row) continue;
181
- if (row.kind === "action" && row.action === "done") break;
182
-
183
- if (row.kind === "tool") {
184
- if (selectedTools.has(row.toolName)) selectedTools.delete(row.toolName);
185
- else selectedTools.add(row.toolName);
186
- } else if (row.action === "enableAll") {
187
- selectedTools = new Set(allFirecrawlTools());
188
- } else if (row.action === "disableAll") {
189
- selectedTools = new Set();
190
- }
191
-
192
- const saved = await transactSelectedTools(
193
- pi,
194
- ctx,
195
- orderedFirecrawlTools(selectedTools),
196
- generation,
197
- );
198
- if (!isCurrentFirecrawlSession(generation)) return;
199
- if (!saved) selectedTools = new Set(getActiveFirecrawlTools(pi));
200
- }
201
-
202
- if (!isCurrentFirecrawlSession(generation)) return;
87
+ actions: {
88
+ toggle: async ({ itemId, selected }) => {
89
+ if (!isFirecrawlToolName(itemId)) return { kind: "rejected" };
90
+ const selectedTools = new Set(getActiveFirecrawlTools(pi));
91
+ if (selected) selectedTools.add(itemId);
92
+ else selectedTools.delete(itemId);
93
+ const saved = await transactSelectedTools(
94
+ pi,
95
+ ctx,
96
+ orderedFirecrawlTools(selectedTools),
97
+ generation,
98
+ );
99
+ return saved ? { kind: "stay" } : { kind: "rejected" };
100
+ },
101
+ enableAll: async () => {
102
+ const saved = await transactSelectedTools(pi, ctx, allFirecrawlTools(), generation);
103
+ return saved ? { kind: "stay" } : { kind: "rejected" };
104
+ },
105
+ disableAll: async () => {
106
+ const saved = await transactSelectedTools(pi, ctx, [], generation);
107
+ return saved ? { kind: "stay" } : { kind: "rejected" };
108
+ },
109
+ },
110
+ });
111
+ const result = await runMenu(ctx, menu, {
112
+ getState: () => undefined,
113
+ signal: sessionController.signal,
114
+ isCurrent: () => isCurrentFirecrawlSession(generation),
115
+ });
116
+ if (result.kind !== "closed" || !isCurrentFirecrawlSession(generation)) return;
203
117
  const status = await buildStatusMessage(pi);
204
118
  if (!isCurrentFirecrawlSession(generation)) return;
205
119
  ctx.ui.notify(status, hasApiKey() ? "info" : "warning");
@@ -353,21 +267,8 @@ function toolSelectorTitle(selectedTools: ReadonlySet<FirecrawlToolName>) {
353
267
  return `Firecrawl tools (${selectedTools.size}/${FIRECRAWL_TOOL_NAMES.length}). Non-built-in tools run at user risk.`;
354
268
  }
355
269
 
356
- function firecrawlToolSelectorRows(): ToolSelectorRow[] {
357
- return [
358
- ...FIRECRAWL_TOOL_NAMES.map((toolName) => ({ kind: "tool" as const, toolName })),
359
- { kind: "action", action: "enableAll", label: TOOL_SELECTOR_ENABLE_ALL },
360
- { kind: "action", action: "disableAll", label: TOOL_SELECTOR_DISABLE_ALL },
361
- { kind: "action", action: "done", label: TOOL_SELECTOR_DONE },
362
- ];
363
- }
364
-
365
- function formatToolSelectorRow(
366
- row: ToolSelectorRow,
367
- selectedTools: ReadonlySet<FirecrawlToolName>,
368
- ) {
369
- if (row.kind === "action") return row.label;
370
- return `${selectedTools.has(row.toolName) ? "[x]" : "[ ]"} ${row.toolName}`;
270
+ function isFirecrawlToolName(value: string): value is FirecrawlToolName {
271
+ return FIRECRAWL_TOOL_NAMES.includes(value as FirecrawlToolName);
371
272
  }
372
273
 
373
274
  function getActiveFirecrawlTools(pi: ExtensionAPI) {