mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 21:01:43 +03:00
refactor(browser): share proxy file helpers
This commit is contained in:
@@ -22,8 +22,8 @@ import {
|
|||||||
import { resolveBrowserConfig } from "../../browser/config.js";
|
import { resolveBrowserConfig } from "../../browser/config.js";
|
||||||
import { DEFAULT_AI_SNAPSHOT_MAX_CHARS } from "../../browser/constants.js";
|
import { DEFAULT_AI_SNAPSHOT_MAX_CHARS } from "../../browser/constants.js";
|
||||||
import { DEFAULT_UPLOAD_DIR, resolvePathsWithinRoot } from "../../browser/paths.js";
|
import { DEFAULT_UPLOAD_DIR, resolvePathsWithinRoot } from "../../browser/paths.js";
|
||||||
|
import { applyBrowserProxyPaths, persistBrowserProxyFiles } from "../../browser/proxy-files.js";
|
||||||
import { loadConfig } from "../../config/config.js";
|
import { loadConfig } from "../../config/config.js";
|
||||||
import { saveMediaBuffer } from "../../media/store.js";
|
|
||||||
import { wrapExternalContent } from "../../security/external-content.js";
|
import { wrapExternalContent } from "../../security/external-content.js";
|
||||||
import { BrowserToolSchema } from "./browser-tool.schema.js";
|
import { BrowserToolSchema } from "./browser-tool.schema.js";
|
||||||
import { type AnyAgentTool, imageResultFromFile, jsonResult, readStringParam } from "./common.js";
|
import { type AnyAgentTool, imageResultFromFile, jsonResult, readStringParam } from "./common.js";
|
||||||
@@ -181,36 +181,11 @@ async function callBrowserProxy(params: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function persistProxyFiles(files: BrowserProxyFile[] | undefined) {
|
async function persistProxyFiles(files: BrowserProxyFile[] | undefined) {
|
||||||
if (!files || files.length === 0) {
|
return await persistBrowserProxyFiles(files);
|
||||||
return new Map<string, string>();
|
|
||||||
}
|
|
||||||
const mapping = new Map<string, string>();
|
|
||||||
for (const file of files) {
|
|
||||||
const buffer = Buffer.from(file.base64, "base64");
|
|
||||||
const saved = await saveMediaBuffer(buffer, file.mimeType, "browser", buffer.byteLength);
|
|
||||||
mapping.set(file.path, saved.path);
|
|
||||||
}
|
|
||||||
return mapping;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyProxyPaths(result: unknown, mapping: Map<string, string>) {
|
function applyProxyPaths(result: unknown, mapping: Map<string, string>) {
|
||||||
if (!result || typeof result !== "object") {
|
applyBrowserProxyPaths(result, mapping);
|
||||||
return;
|
|
||||||
}
|
|
||||||
const obj = result as Record<string, unknown>;
|
|
||||||
if (typeof obj.path === "string" && mapping.has(obj.path)) {
|
|
||||||
obj.path = mapping.get(obj.path);
|
|
||||||
}
|
|
||||||
if (typeof obj.imagePath === "string" && mapping.has(obj.imagePath)) {
|
|
||||||
obj.imagePath = mapping.get(obj.imagePath);
|
|
||||||
}
|
|
||||||
const download = obj.download;
|
|
||||||
if (download && typeof download === "object") {
|
|
||||||
const d = download as Record<string, unknown>;
|
|
||||||
if (typeof d.path === "string" && mapping.has(d.path)) {
|
|
||||||
d.path = mapping.get(d.path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveBrowserBaseUrl(params: {
|
function resolveBrowserBaseUrl(params: {
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import { saveMediaBuffer } from "../media/store.js";
|
||||||
|
|
||||||
|
export type BrowserProxyFile = {
|
||||||
|
path: string;
|
||||||
|
base64: string;
|
||||||
|
mimeType?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function persistBrowserProxyFiles(files: BrowserProxyFile[] | undefined) {
|
||||||
|
if (!files || files.length === 0) {
|
||||||
|
return new Map<string, string>();
|
||||||
|
}
|
||||||
|
const mapping = new Map<string, string>();
|
||||||
|
for (const file of files) {
|
||||||
|
const buffer = Buffer.from(file.base64, "base64");
|
||||||
|
const saved = await saveMediaBuffer(buffer, file.mimeType, "browser", buffer.byteLength);
|
||||||
|
mapping.set(file.path, saved.path);
|
||||||
|
}
|
||||||
|
return mapping;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function applyBrowserProxyPaths(result: unknown, mapping: Map<string, string>) {
|
||||||
|
if (!result || typeof result !== "object") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const obj = result as Record<string, unknown>;
|
||||||
|
if (typeof obj.path === "string" && mapping.has(obj.path)) {
|
||||||
|
obj.path = mapping.get(obj.path);
|
||||||
|
}
|
||||||
|
if (typeof obj.imagePath === "string" && mapping.has(obj.imagePath)) {
|
||||||
|
obj.imagePath = mapping.get(obj.imagePath);
|
||||||
|
}
|
||||||
|
const download = obj.download;
|
||||||
|
if (download && typeof download === "object") {
|
||||||
|
const d = download as Record<string, unknown>;
|
||||||
|
if (typeof d.path === "string" && mapping.has(d.path)) {
|
||||||
|
d.path = mapping.get(d.path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,9 +5,9 @@ import {
|
|||||||
createBrowserControlContext,
|
createBrowserControlContext,
|
||||||
startBrowserControlServiceFromConfig,
|
startBrowserControlServiceFromConfig,
|
||||||
} from "../../browser/control-service.js";
|
} from "../../browser/control-service.js";
|
||||||
|
import { applyBrowserProxyPaths, persistBrowserProxyFiles } from "../../browser/proxy-files.js";
|
||||||
import { createBrowserRouteDispatcher } from "../../browser/routes/dispatcher.js";
|
import { createBrowserRouteDispatcher } from "../../browser/routes/dispatcher.js";
|
||||||
import { loadConfig } from "../../config/config.js";
|
import { loadConfig } from "../../config/config.js";
|
||||||
import { saveMediaBuffer } from "../../media/store.js";
|
|
||||||
import { isNodeCommandAllowed, resolveNodeCommandAllowlist } from "../node-command-policy.js";
|
import { isNodeCommandAllowed, resolveNodeCommandAllowlist } from "../node-command-policy.js";
|
||||||
import { ErrorCodes, errorShape } from "../protocol/index.js";
|
import { ErrorCodes, errorShape } from "../protocol/index.js";
|
||||||
import { safeParseJson } from "./nodes.helpers.js";
|
import { safeParseJson } from "./nodes.helpers.js";
|
||||||
@@ -113,36 +113,11 @@ function resolveBrowserNodeTarget(params: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function persistProxyFiles(files: BrowserProxyFile[] | undefined) {
|
async function persistProxyFiles(files: BrowserProxyFile[] | undefined) {
|
||||||
if (!files || files.length === 0) {
|
return await persistBrowserProxyFiles(files);
|
||||||
return new Map<string, string>();
|
|
||||||
}
|
|
||||||
const mapping = new Map<string, string>();
|
|
||||||
for (const file of files) {
|
|
||||||
const buffer = Buffer.from(file.base64, "base64");
|
|
||||||
const saved = await saveMediaBuffer(buffer, file.mimeType, "browser", buffer.byteLength);
|
|
||||||
mapping.set(file.path, saved.path);
|
|
||||||
}
|
|
||||||
return mapping;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyProxyPaths(result: unknown, mapping: Map<string, string>) {
|
function applyProxyPaths(result: unknown, mapping: Map<string, string>) {
|
||||||
if (!result || typeof result !== "object") {
|
applyBrowserProxyPaths(result, mapping);
|
||||||
return;
|
|
||||||
}
|
|
||||||
const obj = result as Record<string, unknown>;
|
|
||||||
if (typeof obj.path === "string" && mapping.has(obj.path)) {
|
|
||||||
obj.path = mapping.get(obj.path);
|
|
||||||
}
|
|
||||||
if (typeof obj.imagePath === "string" && mapping.has(obj.imagePath)) {
|
|
||||||
obj.imagePath = mapping.get(obj.imagePath);
|
|
||||||
}
|
|
||||||
const download = obj.download;
|
|
||||||
if (download && typeof download === "object") {
|
|
||||||
const d = download as Record<string, unknown>;
|
|
||||||
if (typeof d.path === "string" && mapping.has(d.path)) {
|
|
||||||
d.path = mapping.get(d.path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const browserHandlers: GatewayRequestHandlers = {
|
export const browserHandlers: GatewayRequestHandlers = {
|
||||||
|
|||||||
Reference in New Issue
Block a user