@daoninc/opencode-file-explorer 0.1.0 → 0.1.1

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
@@ -2,16 +2,20 @@
2
2
 
3
3
  ## Install
4
4
 
5
- Initial npm release is pending. After publication, add to the existing `plugins`
5
+ Add the fixed `0.1.1` npm release to the existing `plugins`
6
6
  array in `~/.config/opencode/cli.json`:
7
7
 
8
8
  ```json
9
- { "plugins": ["@daoninc/opencode-file-explorer@0.1.0"] }
9
+ { "plugins": ["@daoninc/opencode-file-explorer@0.1.1"] }
10
10
  ```
11
11
 
12
12
  Requires OpenCode V2. This is a TUI-only package; remove the equivalent local
13
13
  entry when switching to npm. MIT licensed; see `LICENSE`.
14
14
 
15
+ Version `0.1.1` ships Solid-compiled JavaScript and helper type declarations, using
16
+ the `daoninc.file-explorer` runtime/command/panel namespace. Do not use the raw-TSX
17
+ `0.1.0` build with OpenCode 2.3.1. `/files` is unchanged.
18
+
15
19
  ## Features
16
20
 
17
21
  Global OpenCode V2 TUI plugin. Run `/files`, or choose
@@ -0,0 +1,36 @@
1
+ export type Entry = {
2
+ path: string;
3
+ type: "file" | "directory";
4
+ };
5
+ export type Row = Entry & {
6
+ name: string;
7
+ depth: number;
8
+ parent: string;
9
+ };
10
+ export type TreeState = {
11
+ children: Record<string, Entry[]>;
12
+ expanded: Set<string>;
13
+ loading: Set<string>;
14
+ errors: Record<string, string>;
15
+ };
16
+ export declare function nameOf(path: string): string;
17
+ export declare function sorted(entries: Entry[]): Entry[];
18
+ export declare function searchRows(entries: Entry[], hidden: boolean): Row[];
19
+ export declare function visibleRows(state: TreeState, hidden: boolean): Row[];
20
+ export declare function previewText(bytes: Uint8Array, limit?: number): string;
21
+ type PreviewRead = (input: {
22
+ location: {
23
+ directory: string;
24
+ };
25
+ path: string;
26
+ }, options: {
27
+ signal: AbortSignal;
28
+ }) => Promise<Uint8Array>;
29
+ export declare function readPreview(read: PreviewRead, directory: string, path: string, signal: AbortSignal): Promise<string>;
30
+ export declare function createTree(list: (directory: string, path: string, signal: AbortSignal) => Promise<Entry[]>, notify: (state: TreeState) => void): {
31
+ setDirectory(next: string): Promise<void>;
32
+ toggle(path: string): Promise<void>;
33
+ refresh(): Promise<void>;
34
+ dispose(): void;
35
+ };
36
+ export {};
package/dist/model.js ADDED
@@ -0,0 +1,150 @@
1
+ export function nameOf(path) {
2
+ return path.replace(/\/$/, "").split("/").at(-1) ?? path;
3
+ }
4
+ export function sorted(entries) {
5
+ return [...entries].sort((a, b) => {
6
+ if (a.type !== b.type) return a.type === "directory" ? -1 : 1;
7
+ return nameOf(a.path).localeCompare(nameOf(b.path), undefined, {
8
+ numeric: true,
9
+ sensitivity: "base"
10
+ });
11
+ });
12
+ }
13
+ export function searchRows(entries, hidden) {
14
+ const seen = new Set();
15
+ return entries.filter(entry => {
16
+ if (entry.type !== "file" || seen.has(entry.path)) return false;
17
+ if (!hidden && entry.path.split("/").some(part => part.startsWith("."))) return false;
18
+ seen.add(entry.path);
19
+ return true;
20
+ }).map(entry => ({
21
+ ...entry,
22
+ name: entry.path,
23
+ depth: 0,
24
+ parent: ""
25
+ }));
26
+ }
27
+ export function visibleRows(state, hidden) {
28
+ const rows = [];
29
+ const visit = (parent, depth, ancestors) => {
30
+ for (const entry of state.children[parent] ?? []) {
31
+ const name = nameOf(entry.path);
32
+ if (!hidden && name.startsWith(".")) continue;
33
+ if (ancestors.has(entry.path)) continue;
34
+ rows.push({
35
+ ...entry,
36
+ name,
37
+ depth,
38
+ parent
39
+ });
40
+ if (entry.type === "directory" && state.expanded.has(entry.path)) {
41
+ visit(entry.path, depth + 1, new Set([...ancestors, entry.path]));
42
+ }
43
+ }
44
+ };
45
+ visit("", 0, new Set([""]));
46
+ return rows;
47
+ }
48
+ export function previewText(bytes, limit = 65_536) {
49
+ if (bytes.subarray(0, 8192).includes(0)) return "Binary file — text preview unavailable.";
50
+ const text = new TextDecoder().decode(bytes.subarray(0, limit)).replace(/\r\n/g, "\n").replace(/[\x00-\x08\x0b-\x1f\x7f-\x9f]/g, "");
51
+ return (text || "(empty file)") + (bytes.length > limit ? "\n\n… preview truncated at 64 KiB …" : "");
52
+ }
53
+ export async function readPreview(read, directory, path, signal) {
54
+ const bytes = await read({
55
+ location: {
56
+ directory
57
+ },
58
+ path
59
+ }, {
60
+ signal
61
+ });
62
+ return previewText(bytes);
63
+ }
64
+ export function createTree(list, notify) {
65
+ let state;
66
+ let directory = "";
67
+ let disposed = false;
68
+ let generation = 0;
69
+ const requests = new Map();
70
+ const publish = () => {
71
+ if (!disposed) notify({
72
+ ...state
73
+ });
74
+ };
75
+ const cancel = () => {
76
+ generation++;
77
+ for (const request of requests.values()) request.controller.abort();
78
+ requests.clear();
79
+ };
80
+ const reset = () => {
81
+ state = {
82
+ children: {},
83
+ expanded: new Set(),
84
+ loading: new Set(),
85
+ errors: {}
86
+ };
87
+ };
88
+ reset();
89
+ function load(path) {
90
+ if (disposed) return Promise.resolve();
91
+ const pending = requests.get(path);
92
+ if (pending) return pending.promise;
93
+ const epoch = generation;
94
+ const root = directory;
95
+ const controller = new AbortController();
96
+ state.loading.add(path);
97
+ delete state.errors[path];
98
+ publish();
99
+ const promise = Promise.resolve().then(() => list(root, path, controller.signal)).then(entries => {
100
+ if (disposed || generation !== epoch) return;
101
+ state.children[path] = sorted(entries);
102
+ }).catch(error => {
103
+ if (disposed || generation !== epoch || controller.signal.aborted) return;
104
+ state.errors[path] = error instanceof Error ? error.message : "Unable to read directory";
105
+ }).finally(() => {
106
+ if (disposed || generation !== epoch) return;
107
+ state.loading.delete(path);
108
+ requests.delete(path);
109
+ publish();
110
+ });
111
+ requests.set(path, {
112
+ controller,
113
+ promise
114
+ });
115
+ return promise;
116
+ }
117
+ return {
118
+ async setDirectory(next) {
119
+ if (disposed) return;
120
+ cancel();
121
+ directory = next;
122
+ reset();
123
+ await load("");
124
+ },
125
+ async toggle(path) {
126
+ if (disposed) return;
127
+ if (state.expanded.has(path)) {
128
+ state.expanded.delete(path);
129
+ publish();
130
+ return;
131
+ }
132
+ state.expanded.add(path);
133
+ publish();
134
+ if (!state.children[path] || state.errors[path]) await load(path);
135
+ },
136
+ async refresh() {
137
+ if (disposed) return;
138
+ const paths = ["", ...state.expanded];
139
+ const epoch = generation;
140
+ for (let i = 0; i < paths.length; i += 4) {
141
+ if (disposed || generation !== epoch) return;
142
+ await Promise.all(paths.slice(i, i + 4).map(load));
143
+ }
144
+ },
145
+ dispose() {
146
+ disposed = true;
147
+ cancel();
148
+ }
149
+ };
150
+ }
@@ -0,0 +1,10 @@
1
+ export function createPreviewRequest(path, sessionID, directory) {
2
+ if (!path || /[\x00-\x1f\x7f]/.test(path) || path.startsWith("/") || /^[A-Za-z]:[\\/]/.test(path) || path.split(/[\\/]/).includes("..")) {
3
+ throw new Error("Choose a file inside the current session directory.");
4
+ }
5
+ return {
6
+ path,
7
+ sessionID,
8
+ directory
9
+ };
10
+ }
@@ -0,0 +1,15 @@
1
+ type Editor = {
2
+ readonly isDestroyed: boolean;
3
+ focus(): void;
4
+ };
5
+ type Renderer = {
6
+ readonly currentFocusedEditor: Editor | null;
7
+ on(event: "focused_editor", handler: () => void): unknown;
8
+ off(event: "focused_editor", handler: () => void): unknown;
9
+ };
10
+ export declare function trackPromptFocus(renderer: Renderer, sessionID: () => string, activeSessionID: () => string | undefined, canRemember?: () => boolean): {
11
+ remember: () => void;
12
+ focus(): void;
13
+ dispose(): void;
14
+ };
15
+ export {};
@@ -0,0 +1,31 @@
1
+ export function trackPromptFocus(renderer, sessionID, activeSessionID, canRemember = () => true) {
2
+ let last;
3
+ let disposed = false;
4
+ const remember = () => {
5
+ const active = activeSessionID();
6
+ const editor = renderer.currentFocusedEditor;
7
+ if (disposed || !canRemember() || active !== sessionID() || !editor || editor.isDestroyed) return;
8
+ last = {
9
+ sessionID: active,
10
+ editor
11
+ };
12
+ };
13
+ renderer.on("focused_editor", remember);
14
+ remember();
15
+ return {
16
+ remember,
17
+ focus() {
18
+ remember();
19
+ if (disposed || activeSessionID() !== sessionID() || last?.sessionID !== sessionID() || last.editor.isDestroyed) {
20
+ throw new Error("Click the prompt field once, then select the file again.");
21
+ }
22
+ last.editor.focus();
23
+ if (renderer.currentFocusedEditor !== last.editor) throw new Error("The prompt field is not currently available.");
24
+ },
25
+ dispose() {
26
+ disposed = true;
27
+ renderer.off("focused_editor", remember);
28
+ last = undefined;
29
+ }
30
+ };
31
+ }
@@ -0,0 +1,9 @@
1
+ export declare function fileReference(path: string): string;
2
+ type PromptTarget = {
3
+ close(): void;
4
+ isCurrent(): boolean;
5
+ focus(): void;
6
+ paste(bytes: Uint8Array): void;
7
+ };
8
+ export declare function insertFileReference(path: string, target: PromptTarget): Promise<void>;
9
+ export {};
package/dist/prompt.js ADDED
@@ -0,0 +1,13 @@
1
+ export function fileReference(path) {
2
+ if (!path || /[\x00-\x1f\x7f]/.test(path)) throw new Error("This filename cannot be inserted safely into the prompt.");
3
+ return ` @${path} `;
4
+ }
5
+ export async function insertFileReference(path, target) {
6
+ const bytes = new TextEncoder().encode(fileReference(path));
7
+ if (!target.isCurrent()) return;
8
+ target.close();
9
+ await new Promise(resolve => queueMicrotask(resolve));
10
+ if (!target.isCurrent()) return;
11
+ target.focus();
12
+ target.paste(bytes);
13
+ }