@xynogen/pix-find 0.1.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/package.json +42 -0
- package/src/extension.ts +39 -0
- package/src/find.test.ts +30 -0
- package/src/find.ts +142 -0
- package/src/index.ts +1 -0
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xynogen/pix-find",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pi tool — glob file search",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "bun test"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"pi": {
|
|
16
|
+
"extensions": [
|
|
17
|
+
"src/extension.ts"
|
|
18
|
+
]
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"pi",
|
|
22
|
+
"pi-package",
|
|
23
|
+
"pi-extension",
|
|
24
|
+
"find"
|
|
25
|
+
],
|
|
26
|
+
"author": "xynogen",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/xynogen/pix-mono.git",
|
|
31
|
+
"directory": "packages/pix-find"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@xynogen/pix-pretty": "*"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/extension.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { CursorStore, fffState } from "@xynogen/pix-pretty/fff";
|
|
2
|
+
import type {
|
|
3
|
+
PiPrettyApi,
|
|
4
|
+
PiPrettySdk,
|
|
5
|
+
TextComponentCtor,
|
|
6
|
+
} from "@xynogen/pix-pretty/types";
|
|
7
|
+
import { shortPath } from "@xynogen/pix-pretty/utils";
|
|
8
|
+
|
|
9
|
+
import { registerFindTool } from "./find.js";
|
|
10
|
+
|
|
11
|
+
export default function pixFindExtension(pi: PiPrettyApi): void {
|
|
12
|
+
let sdk: PiPrettySdk;
|
|
13
|
+
try {
|
|
14
|
+
sdk = require("@earendil-works/pi-coding-agent");
|
|
15
|
+
} catch {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const createFindTool = sdk.createFindToolDefinition ?? sdk.createFindTool;
|
|
20
|
+
if (!createFindTool) return;
|
|
21
|
+
|
|
22
|
+
let TextComponent: TextComponentCtor;
|
|
23
|
+
try {
|
|
24
|
+
TextComponent = require("@earendil-works/pi-tui").Text;
|
|
25
|
+
} catch {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const cwd = process.cwd();
|
|
30
|
+
const home = process.env.HOME ?? "";
|
|
31
|
+
|
|
32
|
+
registerFindTool(pi, createFindTool, {
|
|
33
|
+
cwd,
|
|
34
|
+
sp: (p: string) => shortPath(cwd, home, p),
|
|
35
|
+
TextComponent,
|
|
36
|
+
fffState,
|
|
37
|
+
cursorStore: new CursorStore(),
|
|
38
|
+
});
|
|
39
|
+
}
|
package/src/find.test.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, expect, it } from "bun:test";
|
|
2
|
+
import { registerFindTool } from "./find";
|
|
3
|
+
|
|
4
|
+
class MockTextComponent {
|
|
5
|
+
private text = "";
|
|
6
|
+
setText(v: string) {
|
|
7
|
+
this.text = v;
|
|
8
|
+
}
|
|
9
|
+
getText() {
|
|
10
|
+
return this.text;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
describe("registerFindTool", () => {
|
|
15
|
+
it("registers a tool named 'find'", () => {
|
|
16
|
+
const tools: string[] = [];
|
|
17
|
+
registerFindTool(
|
|
18
|
+
{ registerTool: (t: any) => tools.push(t.name) } as any,
|
|
19
|
+
() => ({ execute: async () => ({ content: [] }) }) as any,
|
|
20
|
+
{
|
|
21
|
+
cwd: process.cwd(),
|
|
22
|
+
sp: (p: string) => p,
|
|
23
|
+
TextComponent: MockTextComponent as any,
|
|
24
|
+
fffState: {} as any,
|
|
25
|
+
cursorStore: {} as any,
|
|
26
|
+
},
|
|
27
|
+
);
|
|
28
|
+
expect(tools).toEqual(["find"]);
|
|
29
|
+
});
|
|
30
|
+
});
|
package/src/find.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtensionContext,
|
|
3
|
+
FindToolInput,
|
|
4
|
+
ToolRenderResultOptions,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import type { ToolContext } from "@xynogen/pix-pretty/context";
|
|
7
|
+
import type {
|
|
8
|
+
FindParams,
|
|
9
|
+
FindResultDetails,
|
|
10
|
+
PiPrettyApi,
|
|
11
|
+
RenderContextLike,
|
|
12
|
+
ThemeLike,
|
|
13
|
+
ToolFactory,
|
|
14
|
+
ToolResultLike,
|
|
15
|
+
} from "@xynogen/pix-pretty/types";
|
|
16
|
+
import {
|
|
17
|
+
appendNotices,
|
|
18
|
+
fillToolBackground,
|
|
19
|
+
getTextContent,
|
|
20
|
+
makeTextResult,
|
|
21
|
+
renderDimPreview,
|
|
22
|
+
renderToolError,
|
|
23
|
+
setResultDetails,
|
|
24
|
+
} from "@xynogen/pix-pretty/utils";
|
|
25
|
+
|
|
26
|
+
export function registerFindTool(
|
|
27
|
+
pi: PiPrettyApi,
|
|
28
|
+
createFindTool: ToolFactory<FindToolInput>,
|
|
29
|
+
ctx: ToolContext,
|
|
30
|
+
): void {
|
|
31
|
+
const { cwd, sp, TextComponent, fffState } = ctx;
|
|
32
|
+
const origFind = createFindTool(cwd);
|
|
33
|
+
|
|
34
|
+
pi.registerTool({
|
|
35
|
+
...origFind,
|
|
36
|
+
name: "find",
|
|
37
|
+
renderShell: "self",
|
|
38
|
+
|
|
39
|
+
async execute(
|
|
40
|
+
tid: string,
|
|
41
|
+
params: FindParams,
|
|
42
|
+
sig: AbortSignal | undefined,
|
|
43
|
+
upd: unknown,
|
|
44
|
+
toolCtx: ExtensionContext,
|
|
45
|
+
) {
|
|
46
|
+
// Try FFF first (frecency-ranked, SIMD-accelerated)
|
|
47
|
+
if (fffState.finder && !fffState.finder.isDestroyed) {
|
|
48
|
+
try {
|
|
49
|
+
const effectiveLimit = Math.max(1, params.limit ?? 200);
|
|
50
|
+
let query = params.pattern;
|
|
51
|
+
if (params.path) query = `${params.path} ${query}`;
|
|
52
|
+
|
|
53
|
+
const searchResult = fffState.finder.fileSearch(query, {
|
|
54
|
+
pageSize: effectiveLimit,
|
|
55
|
+
});
|
|
56
|
+
if (searchResult.ok) {
|
|
57
|
+
const { items, totalMatched } = searchResult.value;
|
|
58
|
+
const trimmed = items.slice(0, effectiveLimit);
|
|
59
|
+
const notices: string[] = [];
|
|
60
|
+
if (fffState.partialIndex)
|
|
61
|
+
notices.push("Warning: partial file index");
|
|
62
|
+
if (trimmed.length >= effectiveLimit)
|
|
63
|
+
notices.push(`${effectiveLimit} limit reached`);
|
|
64
|
+
if (totalMatched > trimmed.length)
|
|
65
|
+
notices.push(`${totalMatched} total matches`);
|
|
66
|
+
|
|
67
|
+
const textContent = appendNotices(
|
|
68
|
+
trimmed.map((item) => item.relativePath).join("\n"),
|
|
69
|
+
notices,
|
|
70
|
+
);
|
|
71
|
+
return makeTextResult<FindResultDetails>(textContent, {
|
|
72
|
+
_type: "findResult",
|
|
73
|
+
text: textContent,
|
|
74
|
+
pattern: params.pattern,
|
|
75
|
+
matchCount: trimmed.length,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
} catch {
|
|
79
|
+
/* fall through to SDK */
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// SDK fallback
|
|
84
|
+
const result = await origFind.execute(
|
|
85
|
+
tid,
|
|
86
|
+
params,
|
|
87
|
+
sig,
|
|
88
|
+
upd as never,
|
|
89
|
+
toolCtx,
|
|
90
|
+
);
|
|
91
|
+
const textContent = getTextContent(result);
|
|
92
|
+
const matchCount = textContent
|
|
93
|
+
? textContent.trim().split("\n").filter(Boolean).length
|
|
94
|
+
: 0;
|
|
95
|
+
|
|
96
|
+
setResultDetails<FindResultDetails>(result, {
|
|
97
|
+
_type: "findResult",
|
|
98
|
+
text: textContent,
|
|
99
|
+
pattern: params.pattern,
|
|
100
|
+
matchCount,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return result;
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
renderCall(
|
|
107
|
+
args: FindParams,
|
|
108
|
+
theme: ThemeLike,
|
|
109
|
+
renderCtx: RenderContextLike,
|
|
110
|
+
) {
|
|
111
|
+
const pattern = args.pattern ?? "";
|
|
112
|
+
const path = args.path
|
|
113
|
+
? ` ${theme.fg("muted", `in ${sp(args.path)}`)}`
|
|
114
|
+
: "";
|
|
115
|
+
const text = renderCtx.lastComponent ?? new TextComponent("", 0, 0);
|
|
116
|
+
text.setText(
|
|
117
|
+
fillToolBackground(
|
|
118
|
+
`${theme.fg("toolTitle", theme.bold("find"))} ${theme.fg("accent", pattern)}${path}`,
|
|
119
|
+
),
|
|
120
|
+
);
|
|
121
|
+
return text;
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
renderResult(
|
|
125
|
+
result: ToolResultLike<FindResultDetails>,
|
|
126
|
+
_opt: ToolRenderResultOptions,
|
|
127
|
+
theme: ThemeLike,
|
|
128
|
+
renderCtx: RenderContextLike,
|
|
129
|
+
) {
|
|
130
|
+
const text = renderCtx.lastComponent ?? new TextComponent("", 0, 0);
|
|
131
|
+
|
|
132
|
+
if (renderCtx.isError) {
|
|
133
|
+
text.setText(renderToolError(getTextContent(result) || "Error", theme));
|
|
134
|
+
return text;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const output = getTextContent(result) || "found";
|
|
138
|
+
text.setText(renderDimPreview(output, theme));
|
|
139
|
+
return text;
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { registerFindTool } from "./find.js";
|