Repository navigation
Expand file tree
/
Copy pathruntime-profile.mjs
More file actions
106 lines (104 loc) · 3.94 KB
/
Copy pathruntime-profile.mjs
File metadata and controls
106 lines (104 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { environmentVariableName } from "../shared/bot-bindings.mjs";
import { credentialNames } from "./capability-kinds.mjs";
import { posix } from "node:path";
import { AppError, text } from "./store.mjs";
// Product-owned preparation. Core validates the public Environment request again.
export function runtimeProfile(body, previous) {
if (body.environment !== undefined && body.environment !== "openai_hosted")
throw new AppError(400, "Parsar 仅支持托管工作环境。");
const packages = {};
for (const manager of ["npm", "python"]) {
const entries = body.packages?.[manager] ?? [];
if (
!Array.isArray(entries) ||
entries.length > 100 ||
entries.some(
(v) =>
typeof v !== "string" ||
!v.trim() ||
v.startsWith("-") ||
/[\x00-\x20\x7f]/.test(v) ||
v.length > 512,
)
)
throw new AppError(400, "软件包格式无效,请每行填写一个包名和可选版本。");
packages[manager] = [...new Set(entries)];
}
if (
body.packages &&
Object.keys(body.packages).some((k) => !["npm", "python"].includes(k))
)
throw new AppError(400, "环境模板支持 npm 和 Python 软件包。");
// Core validates the domains; Parsar only maps the choice.
const access = body.network?.access;
let network;
if (access === "disabled") network = { access };
else if (access === "restricted") {
const domains = body.network.allowed_domains;
if (!Array.isArray(domains) || domains.some((d) => typeof d !== "string"))
throw new AppError(400, "请填写允许访问的域名,每行一个。");
network = {
access,
allowed_domains: domains.map((d) => d.trim()).filter(Boolean),
};
} else if (access !== undefined && access !== "enabled")
throw new AppError(400, "网络设置无效。");
const env = Object.create(null);
const input = body.env === undefined ? previous?.env || {} : body.env;
if (
!input ||
typeof input !== "object" ||
Array.isArray(input) ||
Object.keys(input).length > 100
)
throw new AppError(400, "环境变量格式无效。");
for (const [name, value] of Object.entries(input)) {
if (!environmentVariableName(name))
throw new AppError(400, "环境变量名称无效或被运行环境保留。");
const selected = value === null ? previous?.env?.[name] : value;
if (
typeof selected !== "string" ||
selected.includes("\0") ||
selected.length > 8192
)
throw new AppError(400, "请填写新环境变量的值;已有变量留空可保留。");
env[name] = selected;
}
const files = body.files ?? [];
if (!Array.isArray(files) || files.length > 20)
throw new AppError(400, "初始文件最多 20 个。");
const paths = new Set();
const normalized = files.map((file) => {
const path = text(file.path, "文件路径", 1024, true);
if (
!path.startsWith("/workspace/") ||
posix.normalize(path) !== path ||
path.endsWith("/") ||
/[\\\x00-\x1f\x7f]/.test(path) ||
paths.has(path)
)
throw new AppError(400, "文件路径必须在 /workspace/ 内且不能重复。");
paths.add(path);
return { path, content: text(file.content, "文件内容", 100000) };
});
if (Buffer.byteLength(JSON.stringify(normalized)) > 500000)
throw new AppError(400, "初始文件总大小不能超过 500 KB。");
return {
environment: "openai_hosted",
description: text(body.description || "", "说明", 1000),
packages,
setup_script: text(body.setup_script || "", "初始化脚本", 100000),
...(network && { network }),
env,
// Variables the Agent supplies: the bound bot's identity or chosen secrets.
credentials: credentialNames(body.credentials),
files: normalized,
prepare: body.prepare === true,
};
}
export function publicRuntime({ env, ...row }) {
return {
...row,
env: Object.fromEntries(Object.keys(env || {}).map((key) => [key, null])),
};
}