@morozinc/opencode-project-filesystem-plugin 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +49 -2
- package/dist/gui-experimental.cjs +10 -0
- package/docs/screenshots/commit.png +0 -0
- package/docs/screenshots/project.png +0 -0
- package/package.json +48 -4
- package/src/contract.ts +43 -0
- package/src/editors.tsx +229 -0
- package/src/gui.tsx +111 -0
- package/src/index.ts +8 -0
- package/src/main.ts +103 -0
- package/src/plugin-id.ts +1 -0
- package/src/project-tree.tsx +135 -0
- package/src/snapshot.ts +165 -0
- package/src/workspace.css +371 -0
- package/src/workspace.tsx +389 -0
package/src/main.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode/plugin/gui";
|
|
2
|
+
import { realpath } from "node:fs/promises";
|
|
3
|
+
import { Filesystem, type Change, type SessionScope } from "./contract";
|
|
4
|
+
import { scan, type Snapshot } from "./snapshot";
|
|
5
|
+
|
|
6
|
+
const retainedSessions = 8;
|
|
7
|
+
|
|
8
|
+
interface Tracking {
|
|
9
|
+
readonly root: string;
|
|
10
|
+
readonly since: number;
|
|
11
|
+
readonly baseline: Snapshot;
|
|
12
|
+
current: Snapshot;
|
|
13
|
+
refresh?: Promise<Snapshot>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const main: Plugin.MainSetup = (ctx) => {
|
|
17
|
+
const sessions = new Map<string, Promise<Tracking>>();
|
|
18
|
+
const lifetime = new AbortController();
|
|
19
|
+
|
|
20
|
+
function track(scope: SessionScope): Promise<Tracking> {
|
|
21
|
+
const key = JSON.stringify([scope.sessionId, scope.directory]);
|
|
22
|
+
const existing = sessions.get(key);
|
|
23
|
+
if (existing) {
|
|
24
|
+
sessions.delete(key);
|
|
25
|
+
sessions.set(key, existing);
|
|
26
|
+
return existing;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const pending = (async () => {
|
|
30
|
+
const session = await ctx.client.session.get({
|
|
31
|
+
sessionID: scope.sessionId,
|
|
32
|
+
});
|
|
33
|
+
if (session.location.directory !== scope.directory) {
|
|
34
|
+
throw new Error("The session's workspace has changed.");
|
|
35
|
+
}
|
|
36
|
+
const root = await realpath(session.location.directory);
|
|
37
|
+
const baseline = await scan(root, undefined, lifetime.signal);
|
|
38
|
+
return { root, since: Date.now(), baseline, current: baseline };
|
|
39
|
+
})();
|
|
40
|
+
sessions.set(key, pending);
|
|
41
|
+
if (sessions.size > retainedSessions) {
|
|
42
|
+
const oldest = sessions.keys().next().value;
|
|
43
|
+
if (oldest !== undefined) sessions.delete(oldest);
|
|
44
|
+
}
|
|
45
|
+
void pending.catch(() => {
|
|
46
|
+
if (sessions.get(key) === pending) sessions.delete(key);
|
|
47
|
+
});
|
|
48
|
+
return pending;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function refresh(tracking: Tracking): Promise<Snapshot> {
|
|
52
|
+
tracking.refresh ??= scan(
|
|
53
|
+
tracking.root,
|
|
54
|
+
tracking.current,
|
|
55
|
+
lifetime.signal,
|
|
56
|
+
(path, entry) => entry.digest !== tracking.baseline.get(path)?.digest,
|
|
57
|
+
)
|
|
58
|
+
.then((current) => (tracking.current = current))
|
|
59
|
+
.finally(() => {
|
|
60
|
+
tracking.refresh = undefined;
|
|
61
|
+
});
|
|
62
|
+
return tracking.refresh;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
ctx.provide(Filesystem, {
|
|
66
|
+
async track(scope) {
|
|
67
|
+
await track(scope);
|
|
68
|
+
},
|
|
69
|
+
async changes(scope) {
|
|
70
|
+
const tracking = await track(scope);
|
|
71
|
+
const current = await refresh(tracking);
|
|
72
|
+
const files: Change[] = [];
|
|
73
|
+
|
|
74
|
+
for (const [path, after] of current) {
|
|
75
|
+
const before = tracking.baseline.get(path);
|
|
76
|
+
if (before?.digest === after.digest) continue;
|
|
77
|
+
files.push({
|
|
78
|
+
path,
|
|
79
|
+
status: before ? "modified" : "added",
|
|
80
|
+
version: after.digest,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
for (const [path] of tracking.baseline) {
|
|
84
|
+
if (!current.has(path))
|
|
85
|
+
files.push({ path, status: "deleted", version: "deleted" });
|
|
86
|
+
}
|
|
87
|
+
files.sort((left, right) => left.path.localeCompare(right.path));
|
|
88
|
+
return { since: tracking.since, files };
|
|
89
|
+
},
|
|
90
|
+
async compare({ scope, path }) {
|
|
91
|
+
const tracking = await track(scope);
|
|
92
|
+
const before = tracking.baseline.get(path) ?? null;
|
|
93
|
+
const after =
|
|
94
|
+
(await (tracking.refresh ?? tracking.current)).get(path) ?? null;
|
|
95
|
+
return { before, after };
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
return () => {
|
|
100
|
+
lifetime.abort();
|
|
101
|
+
sessions.clear();
|
|
102
|
+
};
|
|
103
|
+
};
|
package/src/plugin-id.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const pluginId = "opencode-project-filesystem-plugin";
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { Files, Ui } from "@opencode/plugin/gui";
|
|
2
|
+
import { createMemo, For, Match, onMount, Show, Switch } from "solid-js";
|
|
3
|
+
|
|
4
|
+
interface TreeProps {
|
|
5
|
+
readonly files: Files;
|
|
6
|
+
readonly ui: Ui;
|
|
7
|
+
readonly active: string | undefined;
|
|
8
|
+
onFile(path: string): void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function filename(path: string): string {
|
|
12
|
+
return path.split("/").at(-1) || path;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function ProjectTree(props: TreeProps) {
|
|
16
|
+
onMount(() => {
|
|
17
|
+
if (props.files.tree.state("")?.loaded) {
|
|
18
|
+
void props.files.tree.sync("", { force: true });
|
|
19
|
+
} else {
|
|
20
|
+
props.files.tree.expand("");
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
return (
|
|
24
|
+
<ul class="opf-tree" aria-label="Project files">
|
|
25
|
+
<Folder
|
|
26
|
+
{...props}
|
|
27
|
+
path=""
|
|
28
|
+
name={filename(props.files.root.replaceAll("\\", "/"))}
|
|
29
|
+
depth={0}
|
|
30
|
+
/>
|
|
31
|
+
</ul>
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function Folder(
|
|
36
|
+
props: TreeProps & { path: string; name: string; depth: number },
|
|
37
|
+
) {
|
|
38
|
+
const state = () => props.files.tree.state(props.path);
|
|
39
|
+
const entries = createMemo(() =>
|
|
40
|
+
[...props.files.tree.list(props.path)].sort(
|
|
41
|
+
(left, right) =>
|
|
42
|
+
Number(right.type === "directory") -
|
|
43
|
+
Number(left.type === "directory") ||
|
|
44
|
+
left.name.localeCompare(right.name, undefined, { numeric: true }),
|
|
45
|
+
),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<li>
|
|
50
|
+
<button
|
|
51
|
+
type="button"
|
|
52
|
+
class="opf-row"
|
|
53
|
+
classList={{ "opf-root": props.depth === 0 }}
|
|
54
|
+
style={{ "--opf-depth": props.depth }}
|
|
55
|
+
aria-expanded={state()?.expanded ?? false}
|
|
56
|
+
title={props.path || props.files.root}
|
|
57
|
+
onClick={() => {
|
|
58
|
+
if (state()?.expanded) {
|
|
59
|
+
props.files.tree.collapse(props.path);
|
|
60
|
+
} else if (state()?.loaded) {
|
|
61
|
+
props.files.tree.expand(props.path, { list: false });
|
|
62
|
+
void props.files.tree.sync(props.path, { force: true });
|
|
63
|
+
} else {
|
|
64
|
+
props.files.tree.expand(props.path);
|
|
65
|
+
}
|
|
66
|
+
}}
|
|
67
|
+
>
|
|
68
|
+
<props.ui.Icon
|
|
69
|
+
name={state()?.expanded ? "chevron-down" : "chevron-right"}
|
|
70
|
+
size="small"
|
|
71
|
+
/>
|
|
72
|
+
<props.ui.Icon name="folder" size="small" />
|
|
73
|
+
<span class="opf-truncate">{props.name}</span>
|
|
74
|
+
</button>
|
|
75
|
+
<Show when={state()?.expanded}>
|
|
76
|
+
<Switch>
|
|
77
|
+
<Match when={state()?.error}>
|
|
78
|
+
<button
|
|
79
|
+
type="button"
|
|
80
|
+
class="opf-note"
|
|
81
|
+
onClick={() =>
|
|
82
|
+
void props.files.tree.sync(props.path, { force: true })
|
|
83
|
+
}
|
|
84
|
+
>
|
|
85
|
+
Couldn't load folder. Retry
|
|
86
|
+
</button>
|
|
87
|
+
</Match>
|
|
88
|
+
<Match when={!state()?.loaded}>
|
|
89
|
+
<div class="opf-note" role="status">
|
|
90
|
+
Loading…
|
|
91
|
+
</div>
|
|
92
|
+
</Match>
|
|
93
|
+
<Match when={state()?.loaded}>
|
|
94
|
+
<ul>
|
|
95
|
+
<For each={entries()}>
|
|
96
|
+
{(node) => (
|
|
97
|
+
<Show
|
|
98
|
+
when={node.type === "directory"}
|
|
99
|
+
fallback={
|
|
100
|
+
<li>
|
|
101
|
+
<button
|
|
102
|
+
type="button"
|
|
103
|
+
class="opf-row opf-tree-file"
|
|
104
|
+
style={{ "--opf-depth": props.depth + 1 }}
|
|
105
|
+
aria-current={
|
|
106
|
+
props.active === node.path ? "true" : undefined
|
|
107
|
+
}
|
|
108
|
+
title={node.path}
|
|
109
|
+
onClick={() => props.onFile(node.path)}
|
|
110
|
+
>
|
|
111
|
+
<props.ui.Icon name="file-document" size="small" />
|
|
112
|
+
<span class="opf-truncate">{node.name}</span>
|
|
113
|
+
</button>
|
|
114
|
+
</li>
|
|
115
|
+
}
|
|
116
|
+
>
|
|
117
|
+
<Folder
|
|
118
|
+
files={props.files}
|
|
119
|
+
ui={props.ui}
|
|
120
|
+
active={props.active}
|
|
121
|
+
onFile={props.onFile}
|
|
122
|
+
path={node.path}
|
|
123
|
+
name={node.name}
|
|
124
|
+
depth={props.depth + 1}
|
|
125
|
+
/>
|
|
126
|
+
</Show>
|
|
127
|
+
)}
|
|
128
|
+
</For>
|
|
129
|
+
</ul>
|
|
130
|
+
</Match>
|
|
131
|
+
</Switch>
|
|
132
|
+
</Show>
|
|
133
|
+
</li>
|
|
134
|
+
);
|
|
135
|
+
}
|
package/src/snapshot.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { constants } from "node:fs";
|
|
3
|
+
import { lstat, open, readdir, readlink, realpath } from "node:fs/promises";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
|
|
6
|
+
const textFileLimit = 512 * 1024;
|
|
7
|
+
const retainedTextLimit = 16 * 1024 * 1024;
|
|
8
|
+
const workspaceEntryLimit = 50_000;
|
|
9
|
+
const excludedDirectories = new Set(["node_modules"]);
|
|
10
|
+
const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
11
|
+
|
|
12
|
+
export interface Entry {
|
|
13
|
+
readonly stamp: string;
|
|
14
|
+
readonly digest: string;
|
|
15
|
+
readonly size: number;
|
|
16
|
+
readonly text: string | null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type Snapshot = ReadonlyMap<string, Entry>;
|
|
20
|
+
|
|
21
|
+
function decode(bytes: Buffer): string | null {
|
|
22
|
+
if (bytes.includes(0)) return null;
|
|
23
|
+
try {
|
|
24
|
+
return decoder.decode(bytes);
|
|
25
|
+
} catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function isMissing(error: unknown): boolean {
|
|
31
|
+
const code = (error as NodeJS.ErrnoException).code;
|
|
32
|
+
return code === "ENOENT" || code === "ENOTDIR" || code === "ELOOP";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function stamp(info: Awaited<ReturnType<typeof lstat>>): string {
|
|
36
|
+
return `${info.dev}:${info.ino}:${info.mode}:${info.size}:${info.mtimeMs}:${info.ctimeMs}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function readEntry(
|
|
40
|
+
absolute: string,
|
|
41
|
+
previous: Entry | undefined,
|
|
42
|
+
signal: AbortSignal,
|
|
43
|
+
): Promise<Entry | undefined> {
|
|
44
|
+
try {
|
|
45
|
+
signal.throwIfAborted();
|
|
46
|
+
const info = await lstat(absolute);
|
|
47
|
+
const version = stamp(info);
|
|
48
|
+
if (previous?.stamp === version) return previous;
|
|
49
|
+
if (info.isSymbolicLink()) {
|
|
50
|
+
const text = await readlink(absolute);
|
|
51
|
+
return {
|
|
52
|
+
stamp: version,
|
|
53
|
+
digest: `link:${text}`,
|
|
54
|
+
size: Buffer.byteLength(text),
|
|
55
|
+
text,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
if (!info.isFile()) return;
|
|
59
|
+
|
|
60
|
+
const file = await open(
|
|
61
|
+
absolute,
|
|
62
|
+
constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | constants.O_NONBLOCK,
|
|
63
|
+
);
|
|
64
|
+
try {
|
|
65
|
+
const opened = await file.stat();
|
|
66
|
+
if (!opened.isFile()) return;
|
|
67
|
+
const hash = createHash("sha256");
|
|
68
|
+
const preview: Buffer[] = [];
|
|
69
|
+
let size = 0;
|
|
70
|
+
for await (const chunk of file.createReadStream({
|
|
71
|
+
autoClose: false,
|
|
72
|
+
signal,
|
|
73
|
+
})) {
|
|
74
|
+
size += chunk.length;
|
|
75
|
+
hash.update(chunk);
|
|
76
|
+
if (size <= textFileLimit) preview.push(chunk);
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
stamp: stamp(opened),
|
|
80
|
+
digest: `${opened.mode & 0o111}:${hash.digest("hex")}`,
|
|
81
|
+
size,
|
|
82
|
+
text: size <= textFileLimit ? decode(Buffer.concat(preview)) : null,
|
|
83
|
+
};
|
|
84
|
+
} finally {
|
|
85
|
+
await file.close();
|
|
86
|
+
}
|
|
87
|
+
} catch (error) {
|
|
88
|
+
if (!isMissing(error)) throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Scan without following symbolic links or using Git's index or ignore rules. */
|
|
93
|
+
export async function scan(
|
|
94
|
+
root: string,
|
|
95
|
+
previous: Snapshot | undefined,
|
|
96
|
+
signal: AbortSignal,
|
|
97
|
+
keepText: (path: string, entry: Entry) => boolean = () => true,
|
|
98
|
+
): Promise<Snapshot> {
|
|
99
|
+
if ((await realpath(root)) !== root) {
|
|
100
|
+
throw new Error("The workspace directory has moved.");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const entries = new Map<string, Entry>();
|
|
104
|
+
const directories = [""];
|
|
105
|
+
let entryCount = 0;
|
|
106
|
+
let retainedBytes = 0;
|
|
107
|
+
|
|
108
|
+
for (const directory of directories) {
|
|
109
|
+
signal.throwIfAborted();
|
|
110
|
+
const absolute = join(root, directory);
|
|
111
|
+
if (
|
|
112
|
+
directory &&
|
|
113
|
+
(
|
|
114
|
+
await lstat(absolute).catch((error: unknown) => {
|
|
115
|
+
if (!isMissing(error)) throw error;
|
|
116
|
+
})
|
|
117
|
+
)?.isDirectory() !== true
|
|
118
|
+
)
|
|
119
|
+
continue;
|
|
120
|
+
|
|
121
|
+
const children = await readdir(absolute, { withFileTypes: true }).catch(
|
|
122
|
+
(error: unknown) => {
|
|
123
|
+
if (!directory || !isMissing(error)) throw error;
|
|
124
|
+
return [];
|
|
125
|
+
},
|
|
126
|
+
);
|
|
127
|
+
entryCount += children.length;
|
|
128
|
+
if (entryCount > workspaceEntryLimit) {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`This workspace exceeds the ${workspaceEntryLimit.toLocaleString()} entry tracking limit.`,
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
for (let offset = 0; offset < children.length; offset += 16) {
|
|
135
|
+
await Promise.all(
|
|
136
|
+
children.slice(offset, offset + 16).map(async (child) => {
|
|
137
|
+
if (child.name === ".git") return;
|
|
138
|
+
const path = directory ? `${directory}/${child.name}` : child.name;
|
|
139
|
+
if (child.isDirectory()) {
|
|
140
|
+
if (!excludedDirectories.has(child.name)) directories.push(path);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const entry = await readEntry(
|
|
145
|
+
join(root, path),
|
|
146
|
+
previous?.get(path),
|
|
147
|
+
signal,
|
|
148
|
+
);
|
|
149
|
+
if (!entry) return;
|
|
150
|
+
if (
|
|
151
|
+
entry.text !== null &&
|
|
152
|
+
keepText(path, entry) &&
|
|
153
|
+
retainedBytes + entry.size <= retainedTextLimit
|
|
154
|
+
) {
|
|
155
|
+
retainedBytes += entry.size;
|
|
156
|
+
entries.set(path, entry);
|
|
157
|
+
} else {
|
|
158
|
+
entries.set(path, { ...entry, text: null });
|
|
159
|
+
}
|
|
160
|
+
}),
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return entries;
|
|
165
|
+
}
|