mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 16:10:43 +00:00
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import fs from "node:fs";
|
|
import * as path from "node:path";
|
|
import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/security-runtime";
|
|
import { asRecord, readStringValue } from "openclaw/plugin-sdk/text-runtime";
|
|
|
|
type CanvasSnapshotPayload = {
|
|
format: string;
|
|
base64: string;
|
|
};
|
|
|
|
export function parseCanvasSnapshotPayload(value: unknown): CanvasSnapshotPayload {
|
|
const obj = asRecord(value);
|
|
const format = readStringValue(obj.format);
|
|
const base64 = readStringValue(obj.base64);
|
|
if (!format || !base64) {
|
|
throw new Error("invalid canvas.snapshot payload");
|
|
}
|
|
return { format, base64 };
|
|
}
|
|
|
|
function resolveCliName(): string {
|
|
return "openclaw";
|
|
}
|
|
|
|
function resolveTempPathParts(opts: { ext: string; tmpDir?: string; id?: string }) {
|
|
const tmpDir = opts.tmpDir ?? resolvePreferredOpenClawTmpDir();
|
|
if (!opts.tmpDir) {
|
|
fs.mkdirSync(tmpDir, { recursive: true, mode: 0o700 });
|
|
}
|
|
return {
|
|
tmpDir,
|
|
id: opts.id ?? randomUUID(),
|
|
ext: opts.ext.startsWith(".") ? opts.ext : `.${opts.ext}`,
|
|
};
|
|
}
|
|
|
|
export function canvasSnapshotTempPath(opts: { ext: string; tmpDir?: string; id?: string }) {
|
|
const { tmpDir, id, ext } = resolveTempPathParts(opts);
|
|
const cliName = resolveCliName();
|
|
return path.join(tmpDir, `${cliName}-canvas-snapshot-${id}${ext}`);
|
|
}
|