@gakr-gakr/diffs 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/dist/api.js +3 -0
- package/dist/index.js +2104 -0
- package/dist/runtime-api.js +2 -0
- package/package.json +18 -1
- package/api.ts +0 -10
- package/index.ts +0 -11
- package/runtime-api.ts +0 -1
- package/src/browser.ts +0 -564
- package/src/config.ts +0 -443
- package/src/http.ts +0 -324
- package/src/language-hints.ts +0 -117
- package/src/pierre-themes.ts +0 -59
- package/src/plugin.ts +0 -73
- package/src/prompt-guidance.ts +0 -7
- package/src/render.ts +0 -557
- package/src/store.ts +0 -387
- package/src/tool.ts +0 -547
- package/src/types.ts +0 -127
- package/src/url.ts +0 -60
- package/src/viewer-assets.ts +0 -103
- package/src/viewer-client.ts +0 -353
- package/src/viewer-payload.ts +0 -94
- package/tsconfig.json +0 -16
- /package/{assets → dist/assets}/viewer-runtime.js +0 -0
package/src/store.ts
DELETED
|
@@ -1,387 +0,0 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
|
-
import fs from "node:fs/promises";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { root as fsRoot } from "autobot/plugin-sdk/security-runtime";
|
|
5
|
-
import { normalizeOptionalString } from "autobot/plugin-sdk/string-coerce-runtime";
|
|
6
|
-
import type { PluginLogger } from "../api.js";
|
|
7
|
-
import type { DiffArtifactContext, DiffArtifactMeta, DiffOutputFormat } from "./types.js";
|
|
8
|
-
|
|
9
|
-
const DEFAULT_TTL_MS = 30 * 60 * 1000;
|
|
10
|
-
const MAX_TTL_MS = 6 * 60 * 60 * 1000;
|
|
11
|
-
const SWEEP_FALLBACK_AGE_MS = 24 * 60 * 60 * 1000;
|
|
12
|
-
const DEFAULT_CLEANUP_INTERVAL_MS = 5 * 60 * 1000;
|
|
13
|
-
const VIEWER_PREFIX = "/plugins/diffs/view";
|
|
14
|
-
|
|
15
|
-
type CreateArtifactParams = {
|
|
16
|
-
html: string;
|
|
17
|
-
title: string;
|
|
18
|
-
inputKind: DiffArtifactMeta["inputKind"];
|
|
19
|
-
fileCount: number;
|
|
20
|
-
ttlMs?: number;
|
|
21
|
-
context?: DiffArtifactContext;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
type CreateStandaloneFileArtifactParams = {
|
|
25
|
-
format?: DiffOutputFormat;
|
|
26
|
-
ttlMs?: number;
|
|
27
|
-
context?: DiffArtifactContext;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
type StandaloneFileMeta = {
|
|
31
|
-
kind: "standalone_file";
|
|
32
|
-
id: string;
|
|
33
|
-
createdAt: string;
|
|
34
|
-
expiresAt: string;
|
|
35
|
-
filePath: string;
|
|
36
|
-
context?: DiffArtifactContext;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
type ArtifactMetaFileName = "meta.json" | "file-meta.json";
|
|
40
|
-
type ArtifactRoot = Awaited<ReturnType<typeof fsRoot>>;
|
|
41
|
-
|
|
42
|
-
export class DiffArtifactStore {
|
|
43
|
-
private readonly rootDir: string;
|
|
44
|
-
private readonly logger?: PluginLogger;
|
|
45
|
-
private readonly cleanupIntervalMs: number;
|
|
46
|
-
private cleanupInFlight: Promise<void> | null = null;
|
|
47
|
-
private nextCleanupAt = 0;
|
|
48
|
-
|
|
49
|
-
constructor(params: { rootDir: string; logger?: PluginLogger; cleanupIntervalMs?: number }) {
|
|
50
|
-
this.rootDir = path.resolve(params.rootDir);
|
|
51
|
-
this.logger = params.logger;
|
|
52
|
-
this.cleanupIntervalMs =
|
|
53
|
-
params.cleanupIntervalMs === undefined
|
|
54
|
-
? DEFAULT_CLEANUP_INTERVAL_MS
|
|
55
|
-
: Math.max(0, Math.floor(params.cleanupIntervalMs));
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
async createArtifact(params: CreateArtifactParams): Promise<DiffArtifactMeta> {
|
|
59
|
-
await this.ensureRoot();
|
|
60
|
-
|
|
61
|
-
const id = crypto.randomBytes(10).toString("hex");
|
|
62
|
-
const token = crypto.randomBytes(24).toString("hex");
|
|
63
|
-
const artifactDir = this.artifactDir(id);
|
|
64
|
-
const htmlPath = path.join(artifactDir, "viewer.html");
|
|
65
|
-
const ttlMs = normalizeTtlMs(params.ttlMs);
|
|
66
|
-
const createdAt = new Date();
|
|
67
|
-
const expiresAt = new Date(createdAt.getTime() + ttlMs);
|
|
68
|
-
const meta: DiffArtifactMeta = {
|
|
69
|
-
id,
|
|
70
|
-
token,
|
|
71
|
-
title: params.title,
|
|
72
|
-
inputKind: params.inputKind,
|
|
73
|
-
fileCount: params.fileCount,
|
|
74
|
-
createdAt: createdAt.toISOString(),
|
|
75
|
-
expiresAt: expiresAt.toISOString(),
|
|
76
|
-
viewerPath: `${VIEWER_PREFIX}/${id}/${token}`,
|
|
77
|
-
htmlPath,
|
|
78
|
-
...(params.context ? { context: params.context } : {}),
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
const root = await this.artifactRoot();
|
|
82
|
-
await root.mkdir(id);
|
|
83
|
-
await root.write(path.posix.join(id, "viewer.html"), params.html);
|
|
84
|
-
await this.writeMeta(meta);
|
|
85
|
-
this.scheduleCleanup();
|
|
86
|
-
return meta;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
async getArtifact(id: string, token: string): Promise<DiffArtifactMeta | null> {
|
|
90
|
-
const meta = await this.readMeta(id);
|
|
91
|
-
if (!meta) {
|
|
92
|
-
return null;
|
|
93
|
-
}
|
|
94
|
-
if (meta.token !== token) {
|
|
95
|
-
return null;
|
|
96
|
-
}
|
|
97
|
-
if (isExpired(meta)) {
|
|
98
|
-
await this.deleteArtifact(id);
|
|
99
|
-
return null;
|
|
100
|
-
}
|
|
101
|
-
return meta;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
async readHtml(id: string): Promise<string> {
|
|
105
|
-
const meta = await this.readMeta(id);
|
|
106
|
-
if (!meta) {
|
|
107
|
-
throw new Error(`Diff artifact not found: ${id}`);
|
|
108
|
-
}
|
|
109
|
-
const htmlPath = this.normalizeStoredPath(meta.htmlPath, "htmlPath");
|
|
110
|
-
return await (await this.artifactRoot()).readText(this.relativeStoredPath(htmlPath));
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
async updateFilePath(id: string, filePath: string): Promise<DiffArtifactMeta> {
|
|
114
|
-
const meta = await this.readMeta(id);
|
|
115
|
-
if (!meta) {
|
|
116
|
-
throw new Error(`Diff artifact not found: ${id}`);
|
|
117
|
-
}
|
|
118
|
-
const normalizedFilePath = this.normalizeStoredPath(filePath, "filePath");
|
|
119
|
-
const next: DiffArtifactMeta = {
|
|
120
|
-
...meta,
|
|
121
|
-
filePath: normalizedFilePath,
|
|
122
|
-
imagePath: normalizedFilePath,
|
|
123
|
-
};
|
|
124
|
-
await this.writeMeta(next);
|
|
125
|
-
return next;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
async updateImagePath(id: string, imagePath: string): Promise<DiffArtifactMeta> {
|
|
129
|
-
return this.updateFilePath(id, imagePath);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
allocateFilePath(id: string, format: DiffOutputFormat = "png"): string {
|
|
133
|
-
return path.join(this.artifactDir(id), `preview.${format}`);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
async createStandaloneFileArtifact(
|
|
137
|
-
params: CreateStandaloneFileArtifactParams = {},
|
|
138
|
-
): Promise<{ id: string; filePath: string; expiresAt: string; context?: DiffArtifactContext }> {
|
|
139
|
-
await this.ensureRoot();
|
|
140
|
-
|
|
141
|
-
const id = crypto.randomBytes(10).toString("hex");
|
|
142
|
-
const artifactDir = this.artifactDir(id);
|
|
143
|
-
const format = params.format ?? "png";
|
|
144
|
-
const filePath = path.join(artifactDir, `preview.${format}`);
|
|
145
|
-
const ttlMs = normalizeTtlMs(params.ttlMs);
|
|
146
|
-
const createdAt = new Date();
|
|
147
|
-
const expiresAt = new Date(createdAt.getTime() + ttlMs).toISOString();
|
|
148
|
-
const meta: StandaloneFileMeta = {
|
|
149
|
-
kind: "standalone_file",
|
|
150
|
-
id,
|
|
151
|
-
createdAt: createdAt.toISOString(),
|
|
152
|
-
expiresAt,
|
|
153
|
-
filePath: this.normalizeStoredPath(filePath, "filePath"),
|
|
154
|
-
...(params.context ? { context: params.context } : {}),
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
await (await this.artifactRoot()).mkdir(id);
|
|
158
|
-
await this.writeStandaloneMeta(meta);
|
|
159
|
-
this.scheduleCleanup();
|
|
160
|
-
return {
|
|
161
|
-
id,
|
|
162
|
-
filePath: meta.filePath,
|
|
163
|
-
expiresAt: meta.expiresAt,
|
|
164
|
-
...(meta.context ? { context: meta.context } : {}),
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
allocateImagePath(id: string, format: DiffOutputFormat = "png"): string {
|
|
169
|
-
return this.allocateFilePath(id, format);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
scheduleCleanup(): void {
|
|
173
|
-
this.maybeCleanupExpired();
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
async cleanupExpired(): Promise<void> {
|
|
177
|
-
const root = await this.artifactRoot();
|
|
178
|
-
const entries = await root.list("", { withFileTypes: true }).catch(() => []);
|
|
179
|
-
const now = Date.now();
|
|
180
|
-
|
|
181
|
-
await Promise.all(
|
|
182
|
-
entries
|
|
183
|
-
.filter((entry) => entry.isDirectory)
|
|
184
|
-
.map(async (entry) => {
|
|
185
|
-
const id = entry.name;
|
|
186
|
-
const meta = await this.readMeta(id);
|
|
187
|
-
if (meta) {
|
|
188
|
-
if (isExpired(meta)) {
|
|
189
|
-
await this.deleteArtifact(id);
|
|
190
|
-
}
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const standaloneMeta = await this.readStandaloneMeta(id);
|
|
195
|
-
if (standaloneMeta) {
|
|
196
|
-
if (isExpired(standaloneMeta)) {
|
|
197
|
-
await this.deleteArtifact(id);
|
|
198
|
-
}
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
if (now - entry.mtimeMs > SWEEP_FALLBACK_AGE_MS) {
|
|
203
|
-
await this.deleteArtifact(id);
|
|
204
|
-
}
|
|
205
|
-
}),
|
|
206
|
-
);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
private async ensureRoot(): Promise<void> {
|
|
210
|
-
await fs.mkdir(this.rootDir, { recursive: true });
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
private async artifactRoot(): Promise<ArtifactRoot> {
|
|
214
|
-
await this.ensureRoot();
|
|
215
|
-
return await fsRoot(this.rootDir);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
private maybeCleanupExpired(): void {
|
|
219
|
-
const now = Date.now();
|
|
220
|
-
if (this.cleanupInFlight || now < this.nextCleanupAt) {
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
this.nextCleanupAt = now + this.cleanupIntervalMs;
|
|
225
|
-
const cleanupPromise = this.cleanupExpired()
|
|
226
|
-
.catch((error) => {
|
|
227
|
-
this.nextCleanupAt = 0;
|
|
228
|
-
this.logger?.warn(`Failed to clean expired diff artifacts: ${String(error)}`);
|
|
229
|
-
})
|
|
230
|
-
.finally(() => {
|
|
231
|
-
if (this.cleanupInFlight === cleanupPromise) {
|
|
232
|
-
this.cleanupInFlight = null;
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
this.cleanupInFlight = cleanupPromise;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
private artifactDir(id: string): string {
|
|
240
|
-
return this.resolveWithinRoot(id);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
private async writeMeta(meta: DiffArtifactMeta): Promise<void> {
|
|
244
|
-
await this.writeJsonMeta(meta.id, "meta.json", meta);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
private async readMeta(id: string): Promise<DiffArtifactMeta | null> {
|
|
248
|
-
const parsed = await this.readJsonMeta(id, "meta.json", "diff artifact");
|
|
249
|
-
if (!parsed) {
|
|
250
|
-
return null;
|
|
251
|
-
}
|
|
252
|
-
return parsed as DiffArtifactMeta;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
private async writeStandaloneMeta(meta: StandaloneFileMeta): Promise<void> {
|
|
256
|
-
await this.writeJsonMeta(meta.id, "file-meta.json", meta);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
private async readStandaloneMeta(id: string): Promise<StandaloneFileMeta | null> {
|
|
260
|
-
const parsed = await this.readJsonMeta(id, "file-meta.json", "standalone diff");
|
|
261
|
-
if (!parsed) {
|
|
262
|
-
return null;
|
|
263
|
-
}
|
|
264
|
-
try {
|
|
265
|
-
const value = parsed as Partial<StandaloneFileMeta>;
|
|
266
|
-
if (
|
|
267
|
-
value.kind !== "standalone_file" ||
|
|
268
|
-
typeof value.id !== "string" ||
|
|
269
|
-
typeof value.createdAt !== "string" ||
|
|
270
|
-
typeof value.expiresAt !== "string" ||
|
|
271
|
-
typeof value.filePath !== "string"
|
|
272
|
-
) {
|
|
273
|
-
return null;
|
|
274
|
-
}
|
|
275
|
-
return {
|
|
276
|
-
kind: value.kind,
|
|
277
|
-
id: value.id,
|
|
278
|
-
createdAt: value.createdAt,
|
|
279
|
-
expiresAt: value.expiresAt,
|
|
280
|
-
filePath: this.normalizeStoredPath(value.filePath, "filePath"),
|
|
281
|
-
...(value.context ? { context: normalizeArtifactContext(value.context) } : {}),
|
|
282
|
-
};
|
|
283
|
-
} catch (error) {
|
|
284
|
-
this.logger?.warn(`Failed to normalize standalone diff metadata for ${id}: ${String(error)}`);
|
|
285
|
-
return null;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
private async writeJsonMeta(
|
|
290
|
-
id: string,
|
|
291
|
-
fileName: ArtifactMetaFileName,
|
|
292
|
-
data: unknown,
|
|
293
|
-
): Promise<void> {
|
|
294
|
-
await (await this.artifactRoot()).writeJson(path.posix.join(id, fileName), data, { space: 2 });
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
private async readJsonMeta(
|
|
298
|
-
id: string,
|
|
299
|
-
fileName: ArtifactMetaFileName,
|
|
300
|
-
context: string,
|
|
301
|
-
): Promise<unknown> {
|
|
302
|
-
try {
|
|
303
|
-
const raw = await (await this.artifactRoot()).readText(path.posix.join(id, fileName));
|
|
304
|
-
return JSON.parse(raw) as unknown;
|
|
305
|
-
} catch (error) {
|
|
306
|
-
if (isFileNotFound(error)) {
|
|
307
|
-
return null;
|
|
308
|
-
}
|
|
309
|
-
this.logger?.warn(`Failed to read ${context} metadata for ${id}: ${String(error)}`);
|
|
310
|
-
return null;
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
private async deleteArtifact(id: string): Promise<void> {
|
|
315
|
-
await fs.rm(this.artifactDir(id), { recursive: true, force: true }).catch(() => {});
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
private resolveWithinRoot(...parts: string[]): string {
|
|
319
|
-
const candidate = path.resolve(this.rootDir, ...parts);
|
|
320
|
-
this.assertWithinRoot(candidate);
|
|
321
|
-
return candidate;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
private normalizeStoredPath(rawPath: string, label: string): string {
|
|
325
|
-
const candidate = path.isAbsolute(rawPath)
|
|
326
|
-
? path.resolve(rawPath)
|
|
327
|
-
: path.resolve(this.rootDir, rawPath);
|
|
328
|
-
this.assertWithinRoot(candidate, label);
|
|
329
|
-
return candidate;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
private relativeStoredPath(storedPath: string): string {
|
|
333
|
-
const relativePath = path.relative(this.rootDir, this.normalizeStoredPath(storedPath, "path"));
|
|
334
|
-
return relativePath.split(path.sep).join(path.posix.sep);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
private assertWithinRoot(candidate: string, label = "path"): void {
|
|
338
|
-
const relative = path.relative(this.rootDir, candidate);
|
|
339
|
-
if (
|
|
340
|
-
relative === "" ||
|
|
341
|
-
(!relative.startsWith(`..${path.sep}`) && relative !== ".." && !path.isAbsolute(relative))
|
|
342
|
-
) {
|
|
343
|
-
return;
|
|
344
|
-
}
|
|
345
|
-
throw new Error(`Diff artifact ${label} escapes store root: ${candidate}`);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
function normalizeTtlMs(value?: number): number {
|
|
350
|
-
if (!Number.isFinite(value) || value === undefined) {
|
|
351
|
-
return DEFAULT_TTL_MS;
|
|
352
|
-
}
|
|
353
|
-
const rounded = Math.floor(value);
|
|
354
|
-
if (rounded <= 0) {
|
|
355
|
-
return DEFAULT_TTL_MS;
|
|
356
|
-
}
|
|
357
|
-
return Math.min(rounded, MAX_TTL_MS);
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
function isExpired(meta: { expiresAt: string }): boolean {
|
|
361
|
-
const expiresAt = Date.parse(meta.expiresAt);
|
|
362
|
-
if (!Number.isFinite(expiresAt)) {
|
|
363
|
-
return true;
|
|
364
|
-
}
|
|
365
|
-
return Date.now() >= expiresAt;
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
function isFileNotFound(error: unknown): boolean {
|
|
369
|
-
const code = error instanceof Error && "code" in error ? error.code : undefined;
|
|
370
|
-
return code === "ENOENT" || code === "not-found";
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
function normalizeArtifactContext(value: unknown): DiffArtifactContext | undefined {
|
|
374
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
375
|
-
return undefined;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
const raw = value as Record<string, unknown>;
|
|
379
|
-
const context = {
|
|
380
|
-
agentId: normalizeOptionalString(raw.agentId),
|
|
381
|
-
sessionId: normalizeOptionalString(raw.sessionId),
|
|
382
|
-
messageChannel: normalizeOptionalString(raw.messageChannel),
|
|
383
|
-
agentAccountId: normalizeOptionalString(raw.agentAccountId),
|
|
384
|
-
};
|
|
385
|
-
|
|
386
|
-
return Object.values(context).some((entry) => entry !== undefined) ? context : undefined;
|
|
387
|
-
}
|