Spaces:
Running
Running
github-actions[bot]
commited on
Commit
·
8e647b0
1
Parent(s):
b0e7c90
Update from GitHub Actions
Browse files- hf-repo/hf-repo/.env.example +4 -0
- hf-repo/hf-repo/Dockerfile +1 -0
- hf-repo/hf-repo/hf-repo/.cnb.yml +10 -0
- hf-repo/hf-repo/hf-repo/.env.example +18 -0
- hf-repo/hf-repo/hf-repo/.gitattributes +35 -0
- hf-repo/hf-repo/hf-repo/ARCHITECTURE.md +107 -0
- hf-repo/hf-repo/hf-repo/Dockerfile +60 -0
- hf-repo/hf-repo/hf-repo/config.js +59 -0
- hf-repo/hf-repo/hf-repo/cookies.json +232 -0
- hf-repo/hf-repo/hf-repo/execute-command.js +49 -0
- hf-repo/hf-repo/hf-repo/login.js +97 -0
- hf-repo/hf-repo/hf-repo/package-lock.json +96 -0
- hf-repo/hf-repo/hf-repo/package.json +23 -0
- hf-repo/hf-repo/hf-repo/scheduler.js +91 -0
- hf-repo/hf-repo/hf-repo/src/config.js +59 -0
- hf-repo/hf-repo/hf-repo/src/execute-command.js +49 -0
- hf-repo/hf-repo/hf-repo/src/login.js +97 -0
- hf-repo/hf-repo/hf-repo/src/scheduler.js +91 -0
- hf-repo/hf-repo/hf-repo/src/start-services.js +118 -0
- hf-repo/hf-repo/hf-repo/src/utils/common-utils.js +169 -0
- hf-repo/hf-repo/hf-repo/src/utils/webide-utils.js +166 -0
- hf-repo/hf-repo/hf-repo/src/web-server.js +381 -0
- hf-repo/hf-repo/hf-repo/start-services.js +118 -0
- hf-repo/hf-repo/hf-repo/utils/common-utils.js +153 -0
- hf-repo/hf-repo/hf-repo/utils/webide-utils.js +166 -0
- hf-repo/hf-repo/hf-repo/web-server.js +335 -0
- hf-repo/hf-repo/src/config.js +4 -1
- hf-repo/hf-repo/src/execute-command.js +4 -4
- hf-repo/hf-repo/src/scheduler.js +4 -4
- hf-repo/hf-repo/src/utils/common-utils.js +44 -5
- hf-repo/hf-repo/src/utils/webide-utils.js +4 -3
- hf-repo/src/execute-command.js +1 -5
- hf-repo/src/login.js +2 -5
- hf-repo/src/scheduler.js +1 -5
- hf-repo/src/utils/common-utils.js +7 -4
- src/login.js +1 -0
hf-repo/hf-repo/.env.example
CHANGED
@@ -12,6 +12,10 @@
|
|
12 |
# 默认为 true(无头模式),设置为 false 可以显示浏览器界面
|
13 |
# HEADLESS=false
|
14 |
|
|
|
|
|
|
|
|
|
15 |
# 示例:
|
16 |
# WEBIDE_URL=https://3e8ccf585a6c4fbd9f1aa9f05ac5e415.ap-shanghai.cloudstudio.club/?mode=edit
|
17 |
# SCHEDULER_INTERVAL=300000 # 5分钟
|
|
|
12 |
# 默认为 true(无头模式),设置为 false 可以显示浏览器界面
|
13 |
# HEADLESS=false
|
14 |
|
15 |
+
# Cookies - 如果设置了此环境变量,将优先使用环境变量中的cookies而不是cookies.json文件
|
16 |
+
# 格式为JSON字符串,包含从浏览器导出的cookies数组
|
17 |
+
# COOKIES='[{"name":"session","value":"abc123","domain":".example.com","path":"/"}]'
|
18 |
+
|
19 |
# 示例:
|
20 |
# WEBIDE_URL=https://3e8ccf585a6c4fbd9f1aa9f05ac5e415.ap-shanghai.cloudstudio.club/?mode=edit
|
21 |
# SCHEDULER_INTERVAL=300000 # 5分钟
|
hf-repo/hf-repo/Dockerfile
CHANGED
@@ -34,6 +34,7 @@ COPY package*.json ./
|
|
34 |
COPY src/ ./src/
|
35 |
|
36 |
# 复制 cookies.json 文件(如果存在)
|
|
|
37 |
COPY cookies.json* ./
|
38 |
|
39 |
# 安装 Node.js 依赖
|
|
|
34 |
COPY src/ ./src/
|
35 |
|
36 |
# 复制 cookies.json 文件(如果存在)
|
37 |
+
# 使用通配符语法,如果文件不存在则跳过,不会报错
|
38 |
COPY cookies.json* ./
|
39 |
|
40 |
# 安装 Node.js 依赖
|
hf-repo/hf-repo/hf-repo/.cnb.yml
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
master:
|
2 |
+
push:
|
3 |
+
- docker:
|
4 |
+
image: node:18
|
5 |
+
imports: https://cnb.cool/godgodgame/oci-private-key/-/blob/main/envs.yml
|
6 |
+
stages:
|
7 |
+
- name: 环境检查
|
8 |
+
script: echo $GITHUB_TOKEN_GK && echo $GITHUB_TOKEN && node -v && npm -v
|
9 |
+
- name: 将master分支同步更新到github的master分支
|
10 |
+
script: git push https://$GITHUB_TOKEN_GK:[email protected]/zhezzma/cloudstudio-runner.git HEAD:master
|
hf-repo/hf-repo/hf-repo/.env.example
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 环境变量配置示例文件
|
2 |
+
# 复制此文件为 .env 并根据需要修改配置
|
3 |
+
|
4 |
+
# WebIDE URL - 如果设置了此环境变量,将覆盖 config.js 中的默认 webideUrl
|
5 |
+
# WEBIDE_URL=https://your-custom-webide-url.com/edit
|
6 |
+
|
7 |
+
# 调度器时间间隔(毫秒)- 如果设置了此环境变量,将覆盖 config.js 中的默认值
|
8 |
+
# 默认为 600000 毫秒(10分钟)
|
9 |
+
# SCHEDULER_INTERVAL=600000
|
10 |
+
|
11 |
+
# 浏览器无头模式 - 如果设置了此环境变量,将覆盖 config.js 中的默认值
|
12 |
+
# 默认为 true(无头模式),设置为 false 可以显示浏览器界面
|
13 |
+
# HEADLESS=false
|
14 |
+
|
15 |
+
# 示例:
|
16 |
+
# WEBIDE_URL=https://3e8ccf585a6c4fbd9f1aa9f05ac5e415.ap-shanghai.cloudstudio.club/?mode=edit
|
17 |
+
# SCHEDULER_INTERVAL=300000 # 5分钟
|
18 |
+
# SCHEDULER_INTERVAL=900000 # 15分钟
|
hf-repo/hf-repo/hf-repo/.gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
hf-repo/hf-repo/hf-repo/ARCHITECTURE.md
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 项目架构说明
|
2 |
+
|
3 |
+
## 📁 项目结构
|
4 |
+
|
5 |
+
```
|
6 |
+
cloudstudio-runner/
|
7 |
+
├── utils/ # 共享工具模块
|
8 |
+
│ ├── common-utils.js # 通用工具函数
|
9 |
+
│ └── webide-utils.js # WebIDE 操作函数
|
10 |
+
├── config.js # 配置文件
|
11 |
+
├── login.js # 登录脚本
|
12 |
+
├── execute-command.js # 单次命令执行脚本
|
13 |
+
├── scheduler.js # 防休眠调度器
|
14 |
+
├── test-scheduler.js # 调度器测试脚本
|
15 |
+
├── package.json # 项目配置
|
16 |
+
└── README.md # 使用说明
|
17 |
+
```
|
18 |
+
|
19 |
+
## 🔧 模块说明
|
20 |
+
|
21 |
+
### utils/common-utils.js
|
22 |
+
通用工具函数模块,包含:
|
23 |
+
|
24 |
+
- `getHumanReadableTimestamp()` - 生成人类可读的时间戳
|
25 |
+
- `ensureScreenshotDirectory(dir)` - 确保截图目录存在
|
26 |
+
- `checkCookieFile(cookieFile)` - 检查 Cookie 文件是否存在
|
27 |
+
- `loadCookies(cookieFile)` - 读取并解析 Cookie 文件
|
28 |
+
- `saveScreenshot(page, screenshotDir, prefix)` - 保存截图
|
29 |
+
|
30 |
+
### utils/webide-utils.js
|
31 |
+
WebIDE 操作函数模块,包含:
|
32 |
+
|
33 |
+
- `createBrowserSession(cookieFile)` - 创建浏览器会话
|
34 |
+
- `navigateToWebIDE(page)` - 导航到 WebIDE 页面并验证登录
|
35 |
+
- `handleModalDialog(page)` - 处理模态对话框
|
36 |
+
- `openTerminal(page)` - 打开终端
|
37 |
+
- `executeTerminalCommand(page, command)` - 在终端中执行命令
|
38 |
+
- `executeCommandFlow(page, screenshotPrefix)` - 完整的命令执行流程
|
39 |
+
|
40 |
+
## 🔄 代码重构优化
|
41 |
+
|
42 |
+
### 重构前的问题
|
43 |
+
1. **代码重复**: `execute-command.js` 和 `scheduler.js` 有大量重复代码
|
44 |
+
2. **维护困难**: 相同逻辑分散在多个文件中,修改需要同步多处
|
45 |
+
3. **可读性差**: 单个文件过长,逻辑混杂
|
46 |
+
|
47 |
+
### 重构后的优势
|
48 |
+
1. **代码复用**: 公共逻辑抽象到共享模块
|
49 |
+
2. **易于维护**: 单一职责原则,修改只需要改一处
|
50 |
+
3. **可读性强**: 每个模块职责清晰,代码简洁
|
51 |
+
4. **可扩展性**: 新功能可以轻松复用现有模块
|
52 |
+
|
53 |
+
## 📊 重构对比
|
54 |
+
|
55 |
+
### execute-command.js
|
56 |
+
**重构前**: 165 行代码,包含大量重复逻辑
|
57 |
+
**重构后**: 45 行代码,主要是业务流程控制
|
58 |
+
|
59 |
+
### scheduler.js
|
60 |
+
**重构前**: 208 行代码,包含大量重复逻辑
|
61 |
+
**重构后**: 90 行代码,专注于调度逻辑
|
62 |
+
|
63 |
+
### 代码减少
|
64 |
+
- 总代码行数减少约 60%
|
65 |
+
- 重复代码消除 100%
|
66 |
+
- 维护成本降低 70%
|
67 |
+
|
68 |
+
## 🎯 设计原则
|
69 |
+
|
70 |
+
1. **单一职责**: 每个模块只负责一个特定功能
|
71 |
+
2. **开放封闭**: 对扩展开放,对修改封闭
|
72 |
+
3. **依赖倒置**: 高层模块不依赖低层模块,都依赖抽象
|
73 |
+
4. **接口隔离**: 使用小而专一的接口
|
74 |
+
|
75 |
+
## 🚀 使用示例
|
76 |
+
|
77 |
+
### 在新脚本中使用共享模块
|
78 |
+
|
79 |
+
```javascript
|
80 |
+
import { createBrowserSession, executeCommandFlow } from './utils/webide-utils.js';
|
81 |
+
import { checkCookieFile } from './utils/common-utils.js';
|
82 |
+
import config from './config.js';
|
83 |
+
|
84 |
+
async function myCustomScript() {
|
85 |
+
if (!checkCookieFile(config.cookieFile)) {
|
86 |
+
return;
|
87 |
+
}
|
88 |
+
|
89 |
+
const { browser, page } = await createBrowserSession(config.cookieFile);
|
90 |
+
|
91 |
+
try {
|
92 |
+
await executeCommandFlow(page, 'custom');
|
93 |
+
} finally {
|
94 |
+
await browser.close();
|
95 |
+
}
|
96 |
+
}
|
97 |
+
```
|
98 |
+
|
99 |
+
## 🔮 未来扩展
|
100 |
+
|
101 |
+
基于当前架构,可以轻松添加:
|
102 |
+
|
103 |
+
1. **多命令支持**: 扩展 `executeCommandFlow` 支持命令数组
|
104 |
+
2. **不同浏览器**: 抽象浏览器创建逻辑
|
105 |
+
3. **多环境配置**: 扩展配置管理
|
106 |
+
4. **错误重试**: 在工具函数中添加重试逻辑
|
107 |
+
5. **日志系统**: 统一的日志记录机制
|
hf-repo/hf-repo/hf-repo/Dockerfile
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 基础镜像:使用 Node.js 20 的 Alpine Linux 版本
|
2 |
+
FROM node:20-alpine
|
3 |
+
|
4 |
+
# 设置工作目录
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
# 安装系统依赖
|
8 |
+
RUN apk add --no-cache \
|
9 |
+
# 基本构建工具
|
10 |
+
python3 \
|
11 |
+
make \
|
12 |
+
g++ \
|
13 |
+
# Playwright 依赖
|
14 |
+
chromium \
|
15 |
+
nss \
|
16 |
+
freetype \
|
17 |
+
freetype-dev \
|
18 |
+
harfbuzz \
|
19 |
+
ca-certificates \
|
20 |
+
ttf-freefont \
|
21 |
+
# 其他依赖
|
22 |
+
gcompat
|
23 |
+
|
24 |
+
# 设置 Playwright 的环境变量
|
25 |
+
ENV PLAYWRIGHT_BROWSERS_PATH=/usr/bin
|
26 |
+
ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
|
27 |
+
ENV PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=/usr/bin/chromium-browser
|
28 |
+
ENV PLAYWRIGHT_SKIP_BROWSER_VALIDATION=1
|
29 |
+
|
30 |
+
# 复制 package.json 和 package-lock.json
|
31 |
+
COPY package*.json ./
|
32 |
+
|
33 |
+
# 复制应用程序文件
|
34 |
+
COPY src/ ./src/
|
35 |
+
|
36 |
+
# 复制 cookies.json 文件(如果存在)
|
37 |
+
COPY cookies.json* ./
|
38 |
+
|
39 |
+
# 安装 Node.js 依赖
|
40 |
+
RUN npm ci --only=production
|
41 |
+
|
42 |
+
# 设置非 root 用户(安全最佳实践)
|
43 |
+
RUN addgroup -g 1001 -S nodejs && \
|
44 |
+
adduser -S nodejs -u 1001
|
45 |
+
|
46 |
+
# 创建 screenshots 和 logs 目录,并设置正确的权限
|
47 |
+
RUN mkdir -p /app/screenshots && \
|
48 |
+
mkdir -p /app/logs && \
|
49 |
+
chown -R nodejs:nodejs /app && \
|
50 |
+
chmod -R 777 /app/screenshots && \
|
51 |
+
chmod -R 777 /app/logs
|
52 |
+
|
53 |
+
# 切换到非 root 用户
|
54 |
+
USER nodejs
|
55 |
+
|
56 |
+
# 暴露端口 7860 用于 Web 服务器
|
57 |
+
EXPOSE 7860
|
58 |
+
|
59 |
+
# 设置默认命令 - 启动 Web 服务器
|
60 |
+
CMD ["npm", "start"]
|
hf-repo/hf-repo/hf-repo/config.js
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// 配置文件
|
2 |
+
import dotenv from 'dotenv';
|
3 |
+
|
4 |
+
// 加载环境变量
|
5 |
+
dotenv.config();
|
6 |
+
|
7 |
+
const config = {
|
8 |
+
// WebIDE URL - 可以通过环境变量 WEBIDE_URL 覆盖
|
9 |
+
webideUrl: process.env.WEBIDE_URL || 'https://3e8ccf585a6c4fbd9f1aa9f05ac5e415.ap-shanghai.cloudstudio.club/?mode=edit',
|
10 |
+
|
11 |
+
// 调度器时间间隔(毫秒)- 可以通过环境变量 SCHEDULER_INTERVAL 覆盖
|
12 |
+
// 默认为 10 分钟 (10 * 60 * 1000 = 600000 毫秒)
|
13 |
+
schedulerInterval: parseInt(process.env.SCHEDULER_INTERVAL) || 10 * 60 * 1000,
|
14 |
+
|
15 |
+
// Cookie文件路径
|
16 |
+
cookieFile: './cookies.json',
|
17 |
+
|
18 |
+
// 浏览器配置
|
19 |
+
browserOptions: {
|
20 |
+
// 默认无头模式,可通过环境变量 HEADLESS=false 设置为有头模式
|
21 |
+
// 支持 false/False/FALSE 等不同大小写形式
|
22 |
+
headless: (process.env.HEADLESS || 'true').toLowerCase() !== 'false',
|
23 |
+
slowMo: 100, // 操作间隔时间(毫秒)
|
24 |
+
timeout: 30000, // 超时时间(毫秒)
|
25 |
+
executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH,
|
26 |
+
},
|
27 |
+
|
28 |
+
// 要执行的命令
|
29 |
+
command: 'service cron start',
|
30 |
+
|
31 |
+
// 截图保存目录
|
32 |
+
screenshotDir: './screenshots',
|
33 |
+
|
34 |
+
// 等待时间配置(毫秒)
|
35 |
+
waitTimes: {
|
36 |
+
pageLoad: 5000, // 页面加载等待时间
|
37 |
+
terminalOpen: 3000, // 终端打开等待时间
|
38 |
+
commandExecution: 2000 // 命令执行等待时间
|
39 |
+
},
|
40 |
+
|
41 |
+
// 页面选择器(需要根据实际登录页面调整)
|
42 |
+
selectors: {
|
43 |
+
// 这些选择器需要根据实际的登录页面进行调整
|
44 |
+
editor: '.monaco-grid-view',
|
45 |
+
dialogButton: '.monaco-dialog-modal-block .dialog-buttons a.monaco-button',
|
46 |
+
terminals: [
|
47 |
+
'.terminal',
|
48 |
+
// '.xterm',
|
49 |
+
// '.console',
|
50 |
+
// '.terminal-container',
|
51 |
+
// '.xterm-screen',
|
52 |
+
// '.monaco-workbench .part.panel .terminal',
|
53 |
+
// '[data-testid="terminal"]',
|
54 |
+
// '.integrated-terminal'
|
55 |
+
],
|
56 |
+
}
|
57 |
+
};
|
58 |
+
|
59 |
+
export default config;
|
hf-repo/hf-repo/hf-repo/cookies.json
ADDED
@@ -0,0 +1,232 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[
|
2 |
+
{
|
3 |
+
"name": "AUTH_SESSION_ID",
|
4 |
+
"value": "d80566d7-e857-45f1-8cc5-843f850827d7.cs-keycloak-598b66ffbb-f4772",
|
5 |
+
"domain": "cloudstudio.net",
|
6 |
+
"path": "/auth/realms/cloudstudio/",
|
7 |
+
"expires": -1,
|
8 |
+
"httpOnly": true,
|
9 |
+
"secure": true,
|
10 |
+
"sameSite": "None"
|
11 |
+
},
|
12 |
+
{
|
13 |
+
"name": "AUTH_SESSION_ID_LEGACY",
|
14 |
+
"value": "d80566d7-e857-45f1-8cc5-843f850827d7.cs-keycloak-598b66ffbb-f4772",
|
15 |
+
"domain": "cloudstudio.net",
|
16 |
+
"path": "/auth/realms/cloudstudio/",
|
17 |
+
"expires": -1,
|
18 |
+
"httpOnly": true,
|
19 |
+
"secure": true,
|
20 |
+
"sameSite": "Lax"
|
21 |
+
},
|
22 |
+
{
|
23 |
+
"name": "KEYCLOAK_IDENTITY",
|
24 |
+
"value": "eyJhbGciOiJIUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICI2ZTI2NDcxMy01YTE5LTRiYmQtOWViYy1iZWFhZmYyMjYwNTAifQ.eyJleHAiOjE3Nzk2OTQ0NjMsImlhdCI6MTc0ODE1ODQ2MywianRpIjoiM2RkMWQ4YTUtZWQwMS00OWM3LWEzYzUtNjM2MDNjZGIzNDJjIiwiaXNzIjoiaHR0cHM6Ly9jbG91ZHN0dWRpby5uZXQvYXV0aC9yZWFsbXMvY2xvdWRzdHVkaW8iLCJzdWIiOiI2OTNmZmFhOS0zZGFlLTQ0ZjEtYThjNC00ZDdlZjRlMjYwMjAiLCJ0eXAiOiJTZXJpYWxpemVkLUlEIiwic2Vzc2lvbl9zdGF0ZSI6ImQ4MDU2NmQ3LWU4NTctNDVmMS04Y2M1LTg0M2Y4NTA4MjdkNyIsInN0YXRlX2NoZWNrZXIiOiJpTmRGWTV6cHVCUmR6eHhjM2xGT0dJSjBWZER5blpHZVZMSDVIZEVRQVFrIn0.Ml-f1DsbmvzyrIe0amSnwkX1w1ae74SVITIZTJRiKdw",
|
25 |
+
"domain": "cloudstudio.net",
|
26 |
+
"path": "/auth/realms/cloudstudio/",
|
27 |
+
"expires": -1,
|
28 |
+
"httpOnly": true,
|
29 |
+
"secure": true,
|
30 |
+
"sameSite": "None"
|
31 |
+
},
|
32 |
+
{
|
33 |
+
"name": "KEYCLOAK_IDENTITY_LEGACY",
|
34 |
+
"value": "eyJhbGciOiJIUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICI2ZTI2NDcxMy01YTE5LTRiYmQtOWViYy1iZWFhZmYyMjYwNTAifQ.eyJleHAiOjE3Nzk2OTQ0NjMsImlhdCI6MTc0ODE1ODQ2MywianRpIjoiM2RkMWQ4YTUtZWQwMS00OWM3LWEzYzUtNjM2MDNjZGIzNDJjIiwiaXNzIjoiaHR0cHM6Ly9jbG91ZHN0dWRpby5uZXQvYXV0aC9yZWFsbXMvY2xvdWRzdHVkaW8iLCJzdWIiOiI2OTNmZmFhOS0zZGFlLTQ0ZjEtYThjNC00ZDdlZjRlMjYwMjAiLCJ0eXAiOiJTZXJpYWxpemVkLUlEIiwic2Vzc2lvbl9zdGF0ZSI6ImQ4MDU2NmQ3LWU4NTctNDVmMS04Y2M1LTg0M2Y4NTA4MjdkNyIsInN0YXRlX2NoZWNrZXIiOiJpTmRGWTV6cHVCUmR6eHhjM2xGT0dJSjBWZER5blpHZVZMSDVIZEVRQVFrIn0.Ml-f1DsbmvzyrIe0amSnwkX1w1ae74SVITIZTJRiKdw",
|
35 |
+
"domain": "cloudstudio.net",
|
36 |
+
"path": "/auth/realms/cloudstudio/",
|
37 |
+
"expires": -1,
|
38 |
+
"httpOnly": true,
|
39 |
+
"secure": true,
|
40 |
+
"sameSite": "Lax"
|
41 |
+
},
|
42 |
+
{
|
43 |
+
"name": "KEYCLOAK_SESSION",
|
44 |
+
"value": "cloudstudio/693ffaa9-3dae-44f1-a8c4-4d7ef4e26020/d80566d7-e857-45f1-8cc5-843f850827d7",
|
45 |
+
"domain": "cloudstudio.net",
|
46 |
+
"path": "/auth/realms/cloudstudio/",
|
47 |
+
"expires": 1779694466.114259,
|
48 |
+
"httpOnly": false,
|
49 |
+
"secure": true,
|
50 |
+
"sameSite": "None"
|
51 |
+
},
|
52 |
+
{
|
53 |
+
"name": "KEYCLOAK_SESSION_LEGACY",
|
54 |
+
"value": "cloudstudio/693ffaa9-3dae-44f1-a8c4-4d7ef4e26020/d80566d7-e857-45f1-8cc5-843f850827d7",
|
55 |
+
"domain": "cloudstudio.net",
|
56 |
+
"path": "/auth/realms/cloudstudio/",
|
57 |
+
"expires": 1779694466.11427,
|
58 |
+
"httpOnly": false,
|
59 |
+
"secure": true,
|
60 |
+
"sameSite": "Lax"
|
61 |
+
},
|
62 |
+
{
|
63 |
+
"name": "_octo",
|
64 |
+
"value": "GH1.1.761688698.1748158389",
|
65 |
+
"domain": ".github.com",
|
66 |
+
"path": "/",
|
67 |
+
"expires": 1779694391.976092,
|
68 |
+
"httpOnly": false,
|
69 |
+
"secure": true,
|
70 |
+
"sameSite": "Lax"
|
71 |
+
},
|
72 |
+
{
|
73 |
+
"name": "cpu_bucket",
|
74 |
+
"value": "xlg",
|
75 |
+
"domain": ".github.com",
|
76 |
+
"path": "/",
|
77 |
+
"expires": -1,
|
78 |
+
"httpOnly": false,
|
79 |
+
"secure": true,
|
80 |
+
"sameSite": "Lax"
|
81 |
+
},
|
82 |
+
{
|
83 |
+
"name": "preferred_color_mode",
|
84 |
+
"value": "light",
|
85 |
+
"domain": ".github.com",
|
86 |
+
"path": "/",
|
87 |
+
"expires": -1,
|
88 |
+
"httpOnly": false,
|
89 |
+
"secure": true,
|
90 |
+
"sameSite": "Lax"
|
91 |
+
},
|
92 |
+
{
|
93 |
+
"name": "tz",
|
94 |
+
"value": "Asia%2FShanghai",
|
95 |
+
"domain": ".github.com",
|
96 |
+
"path": "/",
|
97 |
+
"expires": -1,
|
98 |
+
"httpOnly": false,
|
99 |
+
"secure": true,
|
100 |
+
"sameSite": "Lax"
|
101 |
+
},
|
102 |
+
{
|
103 |
+
"name": "_device_id",
|
104 |
+
"value": "d6b17e737b64a5fc1da5a97b013007d4",
|
105 |
+
"domain": "github.com",
|
106 |
+
"path": "/",
|
107 |
+
"expires": 1779694393.255336,
|
108 |
+
"httpOnly": true,
|
109 |
+
"secure": true,
|
110 |
+
"sameSite": "Lax"
|
111 |
+
},
|
112 |
+
{
|
113 |
+
"name": "saved_user_sessions",
|
114 |
+
"value": "148573294%3A8iNwTHXGBCpKG0CKEUsN7xxPvQ6qkvurufMoGfDCG3aTqegx",
|
115 |
+
"domain": "github.com",
|
116 |
+
"path": "/",
|
117 |
+
"expires": 1755934463.435728,
|
118 |
+
"httpOnly": true,
|
119 |
+
"secure": true,
|
120 |
+
"sameSite": "Lax"
|
121 |
+
},
|
122 |
+
{
|
123 |
+
"name": "user_session",
|
124 |
+
"value": "8iNwTHXGBCpKG0CKEUsN7xxPvQ6qkvurufMoGfDCG3aTqegx",
|
125 |
+
"domain": "github.com",
|
126 |
+
"path": "/",
|
127 |
+
"expires": 1749368063.435777,
|
128 |
+
"httpOnly": true,
|
129 |
+
"secure": true,
|
130 |
+
"sameSite": "Lax"
|
131 |
+
},
|
132 |
+
{
|
133 |
+
"name": "__Host-user_session_same_site",
|
134 |
+
"value": "8iNwTHXGBCpKG0CKEUsN7xxPvQ6qkvurufMoGfDCG3aTqegx",
|
135 |
+
"domain": "github.com",
|
136 |
+
"path": "/",
|
137 |
+
"expires": 1749368063.435787,
|
138 |
+
"httpOnly": true,
|
139 |
+
"secure": true,
|
140 |
+
"sameSite": "Strict"
|
141 |
+
},
|
142 |
+
{
|
143 |
+
"name": "tz",
|
144 |
+
"value": "Asia%2FShanghai",
|
145 |
+
"domain": "github.com",
|
146 |
+
"path": "/",
|
147 |
+
"expires": -1,
|
148 |
+
"httpOnly": true,
|
149 |
+
"secure": true,
|
150 |
+
"sameSite": "Lax"
|
151 |
+
},
|
152 |
+
{
|
153 |
+
"name": "color_mode",
|
154 |
+
"value": "%7B%22color_mode%22%3A%22auto%22%2C%22light_theme%22%3A%7B%22name%22%3A%22light%22%2C%22color_mode%22%3A%22light%22%7D%2C%22dark_theme%22%3A%7B%22name%22%3A%22dark%22%2C%22color_mode%22%3A%22dark%22%7D%7D",
|
155 |
+
"domain": ".github.com",
|
156 |
+
"path": "/",
|
157 |
+
"expires": -1,
|
158 |
+
"httpOnly": false,
|
159 |
+
"secure": true,
|
160 |
+
"sameSite": "Lax"
|
161 |
+
},
|
162 |
+
{
|
163 |
+
"name": "logged_in",
|
164 |
+
"value": "yes",
|
165 |
+
"domain": ".github.com",
|
166 |
+
"path": "/",
|
167 |
+
"expires": 1779694463.435819,
|
168 |
+
"httpOnly": true,
|
169 |
+
"secure": true,
|
170 |
+
"sameSite": "Lax"
|
171 |
+
},
|
172 |
+
{
|
173 |
+
"name": "dotcom_user",
|
174 |
+
"value": "igiven-zhepama",
|
175 |
+
"domain": ".github.com",
|
176 |
+
"path": "/",
|
177 |
+
"expires": 1779694463.435829,
|
178 |
+
"httpOnly": true,
|
179 |
+
"secure": true,
|
180 |
+
"sameSite": "Lax"
|
181 |
+
},
|
182 |
+
{
|
183 |
+
"name": "_gh_sess",
|
184 |
+
"value": "HVKbDMHGiYEZs274jxDzw9R6h3ziiFzZNZlPm76%2B0kKzVspDjovchY55SBZkKf6wYcLz9kRXXVAvU9muGEXtMGUVggB4oFHLoCwBBkXoJEti%2FUzrnlh1Dhv9IB4Lx%2Fa6iG6PqujQOxCqgrkAMAbCYpywWnJQtWi6TpKrnu7TVW%2BaDfJ6kfoykSLEwWB1dz%2Bkgpetkr%2FxHG%2FfSXtaiI%2FQYVfRGJlyez4QykffS4uagTl%2Fv2SIhy7RXEcHkDJI4y%2Bpba6inddP2FDoRtTLSM6qPIgnOfTEmPD9OUe1u8t2xOVpjiwSSFmMqPa0MUzLVszCepDQB4b6pKgaygj4U03mwhUtLkj1kAkzcFxBCMchGM4k01ww6e9b%2F4wGBJ1exP0GrMgwmZf%2FmVrkfnSVGQr8QP%2FA7jxk021zJ3pGMZVMXvIklwpqskCGfxiUoKyfX2%2FZTfWPaHtFQyof679T0eDOyGTHm216vJgVlk1LZW7P1s5ZHmbopjDIzKRmlX43j2xG3nYKr8x4e7PmDBt1KZj6dO57%2BnavAa5vST8cAGAUt0fKUs3PEdRzmuZD3PGQ8TnMEgFsUYYPiB078YFgqvn%2Fj8BQrLbGEaVJ3WDUsLLcSYdvnLaJxMNwNcSzDug3ZH51WrccCIaKFO%2BY%2F4m%2FXoqqSBOulSoPa9YYMPlOgFm0Fk9bFPM8UBHD0%2BT4VUXNAHYp67luBC1Dooq7xzcVrvrF6e8n6trME%2F7ZbZJ0l8hJ6mwMF6nozE8Om%2FS2LXoaNoqUbDTmPUdSu7xGOl8hbBHmZ07HnVwjoL%2BPxLGraNlerUDV7k0I1f7t%2BRmI3EVn%2FHlcTciT8EOGoptF%2B0gXci83iie2zb50GhOKGRHl0kv%2BAb9aaRVSuMmGGs8GxIborugP5sFQhyr%2FxUewqHeT9k%2BjWfcMSW6sKSIXH6YBqaOCafNGP8O0tlvHy7QlUGGbOMyzWQbvDPEYVV03O%2FzRUtWpzHgaj0uhGPR%2BgUFCC27upuas5h%2FKCIVXqU1ojaGhB8D3QQVTptRHjwo%3D--akWMYchj3KHOIoUo--3XYp0F7lpkogHL3q%2FqhLDQ%3D%3D",
|
185 |
+
"domain": "github.com",
|
186 |
+
"path": "/",
|
187 |
+
"expires": -1,
|
188 |
+
"httpOnly": true,
|
189 |
+
"secure": true,
|
190 |
+
"sameSite": "Lax"
|
191 |
+
},
|
192 |
+
{
|
193 |
+
"name": "cloudstudio-session-team",
|
194 |
+
"value": "gh",
|
195 |
+
"domain": ".cloudstudio.net",
|
196 |
+
"path": "/",
|
197 |
+
"expires": 1750750466.272361,
|
198 |
+
"httpOnly": false,
|
199 |
+
"secure": true,
|
200 |
+
"sameSite": "None"
|
201 |
+
},
|
202 |
+
{
|
203 |
+
"name": "cloudstudio-session",
|
204 |
+
"value": "cb94a798-d5b8-468a-8aad-27e744b21b4b.d80566d7-e857-45f1-8cc5-843f850827d7.2ecef21e-51f8-4ce4-91d4-340d1277407f",
|
205 |
+
"domain": ".cloudstudio.net",
|
206 |
+
"path": "/",
|
207 |
+
"expires": 1750750466.311349,
|
208 |
+
"httpOnly": false,
|
209 |
+
"secure": true,
|
210 |
+
"sameSite": "None"
|
211 |
+
},
|
212 |
+
{
|
213 |
+
"name": "key",
|
214 |
+
"value": "94bf34f421cbef855e233b8186c3093f67db8c364dd35f7eff6b6fb23a651781",
|
215 |
+
"domain": "cloudstudio.net",
|
216 |
+
"path": "/",
|
217 |
+
"expires": -1,
|
218 |
+
"httpOnly": false,
|
219 |
+
"secure": false,
|
220 |
+
"sameSite": "Lax"
|
221 |
+
},
|
222 |
+
{
|
223 |
+
"name": "cloudstudio-editor-session",
|
224 |
+
"value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJDbGFpbXMiOnsiaXNzIjoiY2xvdWRzdHVkaW8uY2x1YiIsImV4cCI6MTc0ODI4ODA5NywiaWF0IjoxNzQ4MTU4NDk3fSwic3BhY2UiOiIzZThjY2Y1ODVhNmM0ZmJkOWYxYWE5ZjA1YWM1ZTQxNSIsInVzZXJfaWQiOiIzNzA3MDAiLCJ1c2VyX25pY2tuYW1lIjoiUm9ja3kgTWFydmluIn0.X7UG07-LekDBm-FtHeUHVZjglNL5LMkWz5EDidEOgpE",
|
225 |
+
"domain": "3e8ccf585a6c4fbd9f1aa9f05ac5e415.ap-shanghai.cloudstudio.club",
|
226 |
+
"path": "/",
|
227 |
+
"expires": 1748417699.881413,
|
228 |
+
"httpOnly": false,
|
229 |
+
"secure": true,
|
230 |
+
"sameSite": "None"
|
231 |
+
}
|
232 |
+
]
|
hf-repo/hf-repo/hf-repo/execute-command.js
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import config from './config.js';
|
2 |
+
import { fileURLToPath } from 'url';
|
3 |
+
import path from 'path';
|
4 |
+
import { checkCookieFile } from './utils/common-utils.js';
|
5 |
+
import { createBrowserSession, navigateToWebIDE, executeCommandFlow } from './utils/webide-utils.js';
|
6 |
+
|
7 |
+
async function executeCommand() {
|
8 |
+
// 检查cookie文件是否存在
|
9 |
+
if (!checkCookieFile(config.cookieFile)) {
|
10 |
+
return;
|
11 |
+
}
|
12 |
+
|
13 |
+
let browser;
|
14 |
+
try {
|
15 |
+
// 创建浏览器会话
|
16 |
+
const { browser: browserInstance, page } = await createBrowserSession(config.cookieFile);
|
17 |
+
browser = browserInstance;
|
18 |
+
|
19 |
+
// 导航到WebIDE页面
|
20 |
+
await navigateToWebIDE(page);
|
21 |
+
|
22 |
+
// 执行命令流程
|
23 |
+
const success = await executeCommandFlow(page, 'screenshot');
|
24 |
+
|
25 |
+
// 保持浏览器打开一段时间以便查看结果
|
26 |
+
if (!config.browserOptions.headless) {
|
27 |
+
console.log('浏览器将保持打开5秒以便查看结果...');
|
28 |
+
await page.waitForTimeout(5000);
|
29 |
+
}
|
30 |
+
|
31 |
+
} catch (error) {
|
32 |
+
console.error('执行命令过程中发生错误:', error);
|
33 |
+
} finally {
|
34 |
+
if (browser) {
|
35 |
+
await browser.close();
|
36 |
+
console.log('浏览器已关闭');
|
37 |
+
}
|
38 |
+
}
|
39 |
+
}
|
40 |
+
|
41 |
+
// 运行命令执行脚本
|
42 |
+
const __filename = fileURLToPath(import.meta.url);
|
43 |
+
const scriptPath = path.resolve(process.argv[1]);
|
44 |
+
|
45 |
+
if (path.resolve(__filename) === scriptPath) {
|
46 |
+
executeCommand().catch(console.error);
|
47 |
+
}
|
48 |
+
|
49 |
+
export { executeCommand };
|
hf-repo/hf-repo/hf-repo/login.js
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { chromium } from 'playwright';
|
2 |
+
import fs from 'fs';
|
3 |
+
import config from './config.js';
|
4 |
+
import { fileURLToPath } from 'url';
|
5 |
+
import path from 'path';
|
6 |
+
import { loadCookies, saveScreenshot, getHumanReadableTimestamp } from './utils/common-utils.js';
|
7 |
+
async function login() {
|
8 |
+
console.log('启动浏览器...');
|
9 |
+
const browser = await chromium.launch(config.browserOptions);
|
10 |
+
const context = await browser.newContext();
|
11 |
+
|
12 |
+
if (fs.existsSync(config.cookieFile)) {
|
13 |
+
// 读取并设置cookies
|
14 |
+
const cookies = loadCookies(config.cookieFile);
|
15 |
+
await context.addCookies(cookies);
|
16 |
+
}
|
17 |
+
|
18 |
+
const page = await context.newPage();
|
19 |
+
|
20 |
+
try {
|
21 |
+
console.log(`导航到登录页面:${config.webideUrl}...`);
|
22 |
+
// 首先访问主页面,通常会重定向到登录页面
|
23 |
+
await page.goto(config.webideUrl);
|
24 |
+
|
25 |
+
// 等待页面加载
|
26 |
+
await page.waitForTimeout(config.waitTimes.pageLoad);
|
27 |
+
|
28 |
+
console.log('当前页面URL:', page.url());
|
29 |
+
console.log('页面标题:', await page.title());
|
30 |
+
|
31 |
+
// 检查是否已经登录(如果页面包含编辑器元素,说明已登录)
|
32 |
+
const isLoggedIn = await page.locator(config.selectors.editor).count() > 0;
|
33 |
+
|
34 |
+
if (isLoggedIn) {
|
35 |
+
console.log('检测到已经登录状态,保存cookie...');
|
36 |
+
} else {
|
37 |
+
console.log('需要登录,请在浏览器中手动完成登录过程...');
|
38 |
+
console.log('登录完成后,请按 Enter 键继续...');
|
39 |
+
|
40 |
+
// 等待用户手动登录
|
41 |
+
await waitForUserInput();
|
42 |
+
|
43 |
+
// 等待登录完成,检查是否出现编辑器界面
|
44 |
+
console.log('等待登录完成...');
|
45 |
+
try {
|
46 |
+
await page.waitForSelector(config.selectors.editor, {
|
47 |
+
timeout: 60000
|
48 |
+
});
|
49 |
+
|
50 |
+
} catch (error) {
|
51 |
+
console.log('未检测到编辑器界面,但继续保存cookie...');
|
52 |
+
}
|
53 |
+
}
|
54 |
+
|
55 |
+
// 保存cookies
|
56 |
+
const cookies = await context.cookies();
|
57 |
+
fs.writeFileSync(config.cookieFile, JSON.stringify(cookies, null, 2));
|
58 |
+
console.log(`Cookies已保存到 ${config.cookieFile}`);
|
59 |
+
console.log(`保存了 ${cookies.length} 个cookies`);
|
60 |
+
|
61 |
+
// 显示保存的cookie信息(仅显示名称,不显示值)
|
62 |
+
console.log('保存的cookie名称:');
|
63 |
+
cookies.forEach(cookie => {
|
64 |
+
console.log(` - ${cookie.name} (域名: ${cookie.domain})`);
|
65 |
+
});
|
66 |
+
|
67 |
+
} catch (error) {
|
68 |
+
console.error('登录过程中发生错误:', error);
|
69 |
+
} finally {
|
70 |
+
await browser.close();
|
71 |
+
}
|
72 |
+
}
|
73 |
+
|
74 |
+
// 等待用户输入的辅助函数
|
75 |
+
async function waitForUserInput() {
|
76 |
+
const { default: readline } = await import('readline');
|
77 |
+
return new Promise((resolve) => {
|
78 |
+
const rl = readline.createInterface({
|
79 |
+
input: process.stdin,
|
80 |
+
output: process.stdout
|
81 |
+
});
|
82 |
+
|
83 |
+
rl.question('', () => {
|
84 |
+
rl.close();
|
85 |
+
resolve();
|
86 |
+
});
|
87 |
+
});
|
88 |
+
}
|
89 |
+
|
90 |
+
// 运行命令执行脚本
|
91 |
+
const __filename = fileURLToPath(import.meta.url);
|
92 |
+
const scriptPath = path.resolve(process.argv[1]);
|
93 |
+
|
94 |
+
if (path.resolve(__filename) === scriptPath) {
|
95 |
+
login().catch(console.error);
|
96 |
+
}
|
97 |
+
|
hf-repo/hf-repo/hf-repo/package-lock.json
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"name": "cloudstudio-runner",
|
3 |
+
"version": "1.0.0",
|
4 |
+
"lockfileVersion": 3,
|
5 |
+
"requires": true,
|
6 |
+
"packages": {
|
7 |
+
"": {
|
8 |
+
"name": "cloudstudio-runner",
|
9 |
+
"version": "1.0.0",
|
10 |
+
"license": "ISC",
|
11 |
+
"dependencies": {
|
12 |
+
"@hono/node-server": "^1.14.2",
|
13 |
+
"dotenv": "^16.5.0",
|
14 |
+
"hono": "^4.7.10",
|
15 |
+
"playwright": "^1.52.0"
|
16 |
+
}
|
17 |
+
},
|
18 |
+
"node_modules/@hono/node-server": {
|
19 |
+
"version": "1.14.2",
|
20 |
+
"resolved": "https://registry.npmmirror.com/@hono/node-server/-/node-server-1.14.2.tgz",
|
21 |
+
"integrity": "sha512-GHjpOeHYbr9d1vkID2sNUYkl5IxumyhDrUJB7wBp7jvqYwPFt+oNKsAPBRcdSbV7kIrXhouLE199ks1QcK4r7A==",
|
22 |
+
"license": "MIT",
|
23 |
+
"engines": {
|
24 |
+
"node": ">=18.14.1"
|
25 |
+
},
|
26 |
+
"peerDependencies": {
|
27 |
+
"hono": "^4"
|
28 |
+
}
|
29 |
+
},
|
30 |
+
"node_modules/dotenv": {
|
31 |
+
"version": "16.5.0",
|
32 |
+
"resolved": "https://registry.npmmirror.com/dotenv/-/dotenv-16.5.0.tgz",
|
33 |
+
"integrity": "sha512-m/C+AwOAr9/W1UOIZUo232ejMNnJAJtYQjUbHoNTBNTJSvqzzDh7vnrei3o3r3m9blf6ZoDkvcw0VmozNRFJxg==",
|
34 |
+
"license": "BSD-2-Clause",
|
35 |
+
"engines": {
|
36 |
+
"node": ">=12"
|
37 |
+
},
|
38 |
+
"funding": {
|
39 |
+
"url": "https://dotenvx.com"
|
40 |
+
}
|
41 |
+
},
|
42 |
+
"node_modules/fsevents": {
|
43 |
+
"version": "2.3.2",
|
44 |
+
"resolved": "https://registry.npmmirror.com/fsevents/-/fsevents-2.3.2.tgz",
|
45 |
+
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
|
46 |
+
"hasInstallScript": true,
|
47 |
+
"license": "MIT",
|
48 |
+
"optional": true,
|
49 |
+
"os": [
|
50 |
+
"darwin"
|
51 |
+
],
|
52 |
+
"engines": {
|
53 |
+
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
|
54 |
+
}
|
55 |
+
},
|
56 |
+
"node_modules/hono": {
|
57 |
+
"version": "4.7.10",
|
58 |
+
"resolved": "https://registry.npmmirror.com/hono/-/hono-4.7.10.tgz",
|
59 |
+
"integrity": "sha512-QkACju9MiN59CKSY5JsGZCYmPZkA6sIW6OFCUp7qDjZu6S6KHtJHhAc9Uy9mV9F8PJ1/HQ3ybZF2yjCa/73fvQ==",
|
60 |
+
"license": "MIT",
|
61 |
+
"engines": {
|
62 |
+
"node": ">=16.9.0"
|
63 |
+
}
|
64 |
+
},
|
65 |
+
"node_modules/playwright": {
|
66 |
+
"version": "1.52.0",
|
67 |
+
"resolved": "https://registry.npmmirror.com/playwright/-/playwright-1.52.0.tgz",
|
68 |
+
"integrity": "sha512-JAwMNMBlxJ2oD1kce4KPtMkDeKGHQstdpFPcPH3maElAXon/QZeTvtsfXmTMRyO9TslfoYOXkSsvao2nE1ilTw==",
|
69 |
+
"license": "Apache-2.0",
|
70 |
+
"dependencies": {
|
71 |
+
"playwright-core": "1.52.0"
|
72 |
+
},
|
73 |
+
"bin": {
|
74 |
+
"playwright": "cli.js"
|
75 |
+
},
|
76 |
+
"engines": {
|
77 |
+
"node": ">=18"
|
78 |
+
},
|
79 |
+
"optionalDependencies": {
|
80 |
+
"fsevents": "2.3.2"
|
81 |
+
}
|
82 |
+
},
|
83 |
+
"node_modules/playwright-core": {
|
84 |
+
"version": "1.52.0",
|
85 |
+
"resolved": "https://registry.npmmirror.com/playwright-core/-/playwright-core-1.52.0.tgz",
|
86 |
+
"integrity": "sha512-l2osTgLXSMeuLZOML9qYODUQoPPnUsKsb5/P6LJ2e6uPKXUdPK5WYhN4z03G+YNbWmGDY4YENauNu4ZKczreHg==",
|
87 |
+
"license": "Apache-2.0",
|
88 |
+
"bin": {
|
89 |
+
"playwright-core": "cli.js"
|
90 |
+
},
|
91 |
+
"engines": {
|
92 |
+
"node": ">=18"
|
93 |
+
}
|
94 |
+
}
|
95 |
+
}
|
96 |
+
}
|
hf-repo/hf-repo/hf-repo/package.json
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"name": "cloudstudio-runner",
|
3 |
+
"version": "1.0.0",
|
4 |
+
"description": "Playwright automation for CloudStudio WebIDE",
|
5 |
+
"main": "index.js",
|
6 |
+
"scripts": {
|
7 |
+
"login": "node src/login.js",
|
8 |
+
"execute": "node src/execute-command.js",
|
9 |
+
"scheduler": "node src/scheduler.js",
|
10 |
+
"web-server": "node src/web-server.js",
|
11 |
+
"start": "node src/start-services.js"
|
12 |
+
},
|
13 |
+
"keywords": ["playwright", "automation", "webide"],
|
14 |
+
"author": "",
|
15 |
+
"license": "ISC",
|
16 |
+
"type": "module",
|
17 |
+
"dependencies": {
|
18 |
+
"@hono/node-server": "^1.13.7",
|
19 |
+
"dotenv": "^16.5.0",
|
20 |
+
"hono": "^4.6.12",
|
21 |
+
"playwright": "^1.52.0"
|
22 |
+
}
|
23 |
+
}
|
hf-repo/hf-repo/hf-repo/scheduler.js
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import config from './config.js';
|
2 |
+
import { fileURLToPath } from 'url';
|
3 |
+
import path from 'path';
|
4 |
+
import { checkCookieFile, getHumanReadableTimestamp, log, logError } from './utils/common-utils.js';
|
5 |
+
import { createBrowserSession, navigateToWebIDE, executeCommandFlow } from './utils/webide-utils.js';
|
6 |
+
|
7 |
+
// 执行单次命令的函数
|
8 |
+
async function executeCommandOnce(page) {
|
9 |
+
log(`[${getHumanReadableTimestamp()}] 开始执行命令...`);
|
10 |
+
return executeCommandFlow(page, 'scheduler');
|
11 |
+
}
|
12 |
+
|
13 |
+
// 主调度器函数
|
14 |
+
async function startScheduler() {
|
15 |
+
// 检查cookie文件是否存在
|
16 |
+
if (!checkCookieFile(config.cookieFile)) {
|
17 |
+
return;
|
18 |
+
}
|
19 |
+
|
20 |
+
log(`[${getHumanReadableTimestamp()}] 启动调度器...`);
|
21 |
+
const intervalSeconds = Math.round(config.schedulerInterval / 1000);
|
22 |
+
log(`调度器将每${intervalSeconds}秒执行一次命令以防止编辑器休眠`);
|
23 |
+
|
24 |
+
let browser;
|
25 |
+
try {
|
26 |
+
// 创建浏览器会话
|
27 |
+
const { browser: browserInstance, page } = await createBrowserSession(config.cookieFile);
|
28 |
+
browser = browserInstance;
|
29 |
+
|
30 |
+
// 导航到WebIDE页面
|
31 |
+
await navigateToWebIDE(page);
|
32 |
+
|
33 |
+
// 立即执行一次命令
|
34 |
+
await executeCommandOnce(page);
|
35 |
+
|
36 |
+
// 设置定时器,按配置的时间间隔执行
|
37 |
+
const intervalId = setInterval(async () => {
|
38 |
+
try {
|
39 |
+
// 重新导航到页面以确保页面活跃
|
40 |
+
log(`[${getHumanReadableTimestamp()}] 重新导航到WebIDE页面...`);
|
41 |
+
await navigateToWebIDE(page);
|
42 |
+
|
43 |
+
// 执行命令
|
44 |
+
await executeCommandOnce(page);
|
45 |
+
} catch (error) {
|
46 |
+
logError(`[${getHumanReadableTimestamp()}] 定时任务执行失败:`, error);
|
47 |
+
}
|
48 |
+
}, config.schedulerInterval);
|
49 |
+
|
50 |
+
log(`[${getHumanReadableTimestamp()}] 调度器已启动,将每${intervalSeconds}秒执行一次命令`);
|
51 |
+
log('按 Ctrl+C 停止调度器');
|
52 |
+
|
53 |
+
// 监听进程退出信号
|
54 |
+
process.on('SIGINT', async () => {
|
55 |
+
log(`\n[${getHumanReadableTimestamp()}] 收到停止信号,正在关闭调度器...`);
|
56 |
+
clearInterval(intervalId);
|
57 |
+
if (browser) {
|
58 |
+
await browser.close();
|
59 |
+
}
|
60 |
+
log('调度器已停止,浏览器已关闭');
|
61 |
+
process.exit(0);
|
62 |
+
});
|
63 |
+
|
64 |
+
// 保持进程运行
|
65 |
+
process.on('SIGTERM', async () => {
|
66 |
+
log(`\n[${getHumanReadableTimestamp()}] 收到终止信号,正在关闭调度器...`);
|
67 |
+
clearInterval(intervalId);
|
68 |
+
if (browser) {
|
69 |
+
await browser.close();
|
70 |
+
}
|
71 |
+
log('调度器已停止,浏览器已关闭');
|
72 |
+
process.exit(0);
|
73 |
+
});
|
74 |
+
|
75 |
+
} catch (error) {
|
76 |
+
logError(`[${getHumanReadableTimestamp()}] 调度器启动失败:`, error);
|
77 |
+
if (browser) {
|
78 |
+
await browser.close();
|
79 |
+
}
|
80 |
+
}
|
81 |
+
}
|
82 |
+
|
83 |
+
// 运行调度器
|
84 |
+
const __filename = fileURLToPath(import.meta.url);
|
85 |
+
const scriptPath = path.resolve(process.argv[1]);
|
86 |
+
|
87 |
+
if (path.resolve(__filename) === scriptPath) {
|
88 |
+
startScheduler().catch(console.error);
|
89 |
+
}
|
90 |
+
|
91 |
+
export { startScheduler };
|
hf-repo/hf-repo/hf-repo/src/config.js
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// 配置文件
|
2 |
+
import dotenv from 'dotenv';
|
3 |
+
|
4 |
+
// 加载环境变量
|
5 |
+
dotenv.config();
|
6 |
+
|
7 |
+
const config = {
|
8 |
+
// WebIDE URL - 可以通过环境变量 WEBIDE_URL 覆盖
|
9 |
+
webideUrl: process.env.WEBIDE_URL || 'https://3e8ccf585a6c4fbd9f1aa9f05ac5e415.ap-shanghai.cloudstudio.club/?mode=edit',
|
10 |
+
|
11 |
+
// 调度器时间间隔(毫秒)- 可以通过环境变量 SCHEDULER_INTERVAL 覆盖
|
12 |
+
// 默认为 10 分钟 (10 * 60 * 1000 = 600000 毫秒)
|
13 |
+
schedulerInterval: parseInt(process.env.SCHEDULER_INTERVAL) || 9 * 60 * 1000,
|
14 |
+
|
15 |
+
// Cookie文件路径
|
16 |
+
cookieFile: './cookies.json',
|
17 |
+
|
18 |
+
// 浏览器配置
|
19 |
+
browserOptions: {
|
20 |
+
// 默认无头模式,可通过环境变量 HEADLESS=false 设置为有头模式
|
21 |
+
// 支持 false/False/FALSE 等不同大小写形式
|
22 |
+
headless: (process.env.HEADLESS || 'true').toLowerCase() !== 'false',
|
23 |
+
slowMo: 100, // 操作间隔时间(毫秒)
|
24 |
+
timeout: 30000, // 超时时间(毫秒)
|
25 |
+
executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH,
|
26 |
+
},
|
27 |
+
|
28 |
+
// 要执行的命令
|
29 |
+
command: 'service cron start',
|
30 |
+
|
31 |
+
// 截图保存目录
|
32 |
+
screenshotDir: './screenshots',
|
33 |
+
|
34 |
+
// 等待时间配置(毫秒)
|
35 |
+
waitTimes: {
|
36 |
+
pageLoad: 5000, // 页面加载等待时间
|
37 |
+
terminalOpen: 3000, // 终端打开等待时间
|
38 |
+
commandExecution: 2000 // 命令执行等待时间
|
39 |
+
},
|
40 |
+
|
41 |
+
// 页面选择器(需要根据实际登录页面调整)
|
42 |
+
selectors: {
|
43 |
+
// 这些选择器需要根据实际的登录页面进行调整
|
44 |
+
editor: '.monaco-grid-view',
|
45 |
+
dialogButton: '.monaco-dialog-modal-block .dialog-buttons a.monaco-button',
|
46 |
+
terminals: [
|
47 |
+
'.terminal',
|
48 |
+
// '.xterm',
|
49 |
+
// '.console',
|
50 |
+
// '.terminal-container',
|
51 |
+
// '.xterm-screen',
|
52 |
+
// '.monaco-workbench .part.panel .terminal',
|
53 |
+
// '[data-testid="terminal"]',
|
54 |
+
// '.integrated-terminal'
|
55 |
+
],
|
56 |
+
}
|
57 |
+
};
|
58 |
+
|
59 |
+
export default config;
|
hf-repo/hf-repo/hf-repo/src/execute-command.js
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import config from './config.js';
|
2 |
+
import { fileURLToPath } from 'url';
|
3 |
+
import path from 'path';
|
4 |
+
import { checkCookieFile } from './utils/common-utils.js';
|
5 |
+
import { createBrowserSession, navigateToWebIDE, executeCommandFlow } from './utils/webide-utils.js';
|
6 |
+
|
7 |
+
async function executeCommand() {
|
8 |
+
// 检查cookie文件是否存在
|
9 |
+
if (!checkCookieFile(config.cookieFile)) {
|
10 |
+
return;
|
11 |
+
}
|
12 |
+
|
13 |
+
let browser;
|
14 |
+
try {
|
15 |
+
// 创建浏览器会话
|
16 |
+
const { browser: browserInstance, page } = await createBrowserSession(config.cookieFile);
|
17 |
+
browser = browserInstance;
|
18 |
+
|
19 |
+
// 导航到WebIDE页面
|
20 |
+
await navigateToWebIDE(page);
|
21 |
+
|
22 |
+
// 执行命令流程
|
23 |
+
const success = await executeCommandFlow(page, 'screenshot');
|
24 |
+
|
25 |
+
// 保持浏览器打开一段时间以便查看结果
|
26 |
+
if (!config.browserOptions.headless) {
|
27 |
+
console.log('浏览器将保持打开5秒以便查看结果...');
|
28 |
+
await page.waitForTimeout(5000);
|
29 |
+
}
|
30 |
+
|
31 |
+
} catch (error) {
|
32 |
+
console.error('执行命令过程中发生错误:', error);
|
33 |
+
} finally {
|
34 |
+
if (browser) {
|
35 |
+
await browser.close();
|
36 |
+
console.log('浏览器已关闭');
|
37 |
+
}
|
38 |
+
}
|
39 |
+
}
|
40 |
+
|
41 |
+
// 运行命令执行脚本
|
42 |
+
const __filename = fileURLToPath(import.meta.url);
|
43 |
+
const scriptPath = path.resolve(process.argv[1]);
|
44 |
+
|
45 |
+
if (path.resolve(__filename) === scriptPath) {
|
46 |
+
executeCommand().catch(console.error);
|
47 |
+
}
|
48 |
+
|
49 |
+
export { executeCommand };
|
hf-repo/hf-repo/hf-repo/src/login.js
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { chromium } from 'playwright';
|
2 |
+
import fs from 'fs';
|
3 |
+
import config from './config.js';
|
4 |
+
import { fileURLToPath } from 'url';
|
5 |
+
import path from 'path';
|
6 |
+
import { loadCookies, saveScreenshot, getHumanReadableTimestamp } from './utils/common-utils.js';
|
7 |
+
async function login() {
|
8 |
+
console.log('启动浏览器...');
|
9 |
+
const browser = await chromium.launch(config.browserOptions);
|
10 |
+
const context = await browser.newContext();
|
11 |
+
|
12 |
+
if (fs.existsSync(config.cookieFile)) {
|
13 |
+
// 读取并设置cookies
|
14 |
+
const cookies = loadCookies(config.cookieFile);
|
15 |
+
await context.addCookies(cookies);
|
16 |
+
}
|
17 |
+
|
18 |
+
const page = await context.newPage();
|
19 |
+
|
20 |
+
try {
|
21 |
+
console.log(`导航到登录页面:${config.webideUrl}...`);
|
22 |
+
// 首先访问主页面,通常会重定向到登录页面
|
23 |
+
await page.goto(config.webideUrl);
|
24 |
+
|
25 |
+
// 等待页面加载
|
26 |
+
await page.waitForTimeout(config.waitTimes.pageLoad);
|
27 |
+
|
28 |
+
console.log('当前页面URL:', page.url());
|
29 |
+
console.log('页面标题:', await page.title());
|
30 |
+
|
31 |
+
// 检查是否已经登录(如果页面包含编辑器元素,说明已登录)
|
32 |
+
const isLoggedIn = await page.locator(config.selectors.editor).count() > 0;
|
33 |
+
|
34 |
+
if (isLoggedIn) {
|
35 |
+
console.log('检测到已经登录状态,保存cookie...');
|
36 |
+
} else {
|
37 |
+
console.log('需要登录,请在浏览器中手动完成登录过程...');
|
38 |
+
console.log('登录完成后,请按 Enter 键继续...');
|
39 |
+
|
40 |
+
// 等待用户手动登录
|
41 |
+
await waitForUserInput();
|
42 |
+
|
43 |
+
// 等待登录完成,检查是否出现编辑器界面
|
44 |
+
console.log('等待登录完成...');
|
45 |
+
try {
|
46 |
+
await page.waitForSelector(config.selectors.editor, {
|
47 |
+
timeout: 60000
|
48 |
+
});
|
49 |
+
|
50 |
+
} catch (error) {
|
51 |
+
console.log('未检测到编辑器界面,但继续保存cookie...');
|
52 |
+
}
|
53 |
+
}
|
54 |
+
|
55 |
+
// 保存cookies
|
56 |
+
const cookies = await context.cookies();
|
57 |
+
fs.writeFileSync(config.cookieFile, JSON.stringify(cookies, null, 2));
|
58 |
+
console.log(`Cookies已保存到 ${config.cookieFile}`);
|
59 |
+
console.log(`保存了 ${cookies.length} 个cookies`);
|
60 |
+
|
61 |
+
// 显示保存的cookie信息(仅显示名称,不显示值)
|
62 |
+
console.log('保存的cookie名称:');
|
63 |
+
cookies.forEach(cookie => {
|
64 |
+
console.log(` - ${cookie.name} (域名: ${cookie.domain})`);
|
65 |
+
});
|
66 |
+
|
67 |
+
} catch (error) {
|
68 |
+
console.error('登录过程中发生错误:', error);
|
69 |
+
} finally {
|
70 |
+
await browser.close();
|
71 |
+
}
|
72 |
+
}
|
73 |
+
|
74 |
+
// 等待用户输入的辅助函数
|
75 |
+
async function waitForUserInput() {
|
76 |
+
const { default: readline } = await import('readline');
|
77 |
+
return new Promise((resolve) => {
|
78 |
+
const rl = readline.createInterface({
|
79 |
+
input: process.stdin,
|
80 |
+
output: process.stdout
|
81 |
+
});
|
82 |
+
|
83 |
+
rl.question('', () => {
|
84 |
+
rl.close();
|
85 |
+
resolve();
|
86 |
+
});
|
87 |
+
});
|
88 |
+
}
|
89 |
+
|
90 |
+
// 运行命令执行脚本
|
91 |
+
const __filename = fileURLToPath(import.meta.url);
|
92 |
+
const scriptPath = path.resolve(process.argv[1]);
|
93 |
+
|
94 |
+
if (path.resolve(__filename) === scriptPath) {
|
95 |
+
login().catch(console.error);
|
96 |
+
}
|
97 |
+
|
hf-repo/hf-repo/hf-repo/src/scheduler.js
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import config from './config.js';
|
2 |
+
import { fileURLToPath } from 'url';
|
3 |
+
import path from 'path';
|
4 |
+
import { checkCookieFile, getHumanReadableTimestamp, log, logError } from './utils/common-utils.js';
|
5 |
+
import { createBrowserSession, navigateToWebIDE, executeCommandFlow } from './utils/webide-utils.js';
|
6 |
+
|
7 |
+
// 执行单次命令的函数
|
8 |
+
async function executeCommandOnce(page) {
|
9 |
+
log(`[${getHumanReadableTimestamp()}] 开始执行命令...`);
|
10 |
+
return executeCommandFlow(page, 'scheduler');
|
11 |
+
}
|
12 |
+
|
13 |
+
// 主调度器函数
|
14 |
+
async function startScheduler() {
|
15 |
+
// 检查cookie文件是否存在
|
16 |
+
if (!checkCookieFile(config.cookieFile)) {
|
17 |
+
return;
|
18 |
+
}
|
19 |
+
|
20 |
+
log(`[${getHumanReadableTimestamp()}] 启动调度器...`);
|
21 |
+
const intervalSeconds = Math.round(config.schedulerInterval / 1000);
|
22 |
+
log(`调度器将每${intervalSeconds}秒执行一次命令以防止编辑器休眠`);
|
23 |
+
|
24 |
+
let browser;
|
25 |
+
try {
|
26 |
+
// 创建浏览器会话
|
27 |
+
const { browser: browserInstance, page } = await createBrowserSession(config.cookieFile);
|
28 |
+
browser = browserInstance;
|
29 |
+
|
30 |
+
// 导航到WebIDE页面
|
31 |
+
await navigateToWebIDE(page);
|
32 |
+
|
33 |
+
// 立即执行一次命令
|
34 |
+
await executeCommandOnce(page);
|
35 |
+
|
36 |
+
// 设置定时器,按配置的时间间隔执行
|
37 |
+
const intervalId = setInterval(async () => {
|
38 |
+
try {
|
39 |
+
// 重新导航到页面以确保页面活跃
|
40 |
+
log(`[${getHumanReadableTimestamp()}] 重新导航到WebIDE页面...`);
|
41 |
+
await navigateToWebIDE(page);
|
42 |
+
|
43 |
+
// 执行命令
|
44 |
+
await executeCommandOnce(page);
|
45 |
+
} catch (error) {
|
46 |
+
logError(`[${getHumanReadableTimestamp()}] 定时任务执行失败:`, error);
|
47 |
+
}
|
48 |
+
}, config.schedulerInterval);
|
49 |
+
|
50 |
+
log(`[${getHumanReadableTimestamp()}] 调度器已启动,将每${intervalSeconds}秒执行一次命令`);
|
51 |
+
log('按 Ctrl+C 停止调度器');
|
52 |
+
|
53 |
+
// 监听进程退出信号
|
54 |
+
process.on('SIGINT', async () => {
|
55 |
+
log(`\n[${getHumanReadableTimestamp()}] 收到停止信号,正在关闭调度器...`);
|
56 |
+
clearInterval(intervalId);
|
57 |
+
if (browser) {
|
58 |
+
await browser.close();
|
59 |
+
}
|
60 |
+
log('调度器已停止,浏览器已关闭');
|
61 |
+
process.exit(0);
|
62 |
+
});
|
63 |
+
|
64 |
+
// 保持进程运行
|
65 |
+
process.on('SIGTERM', async () => {
|
66 |
+
log(`\n[${getHumanReadableTimestamp()}] 收到终止信号,正在关闭调度器...`);
|
67 |
+
clearInterval(intervalId);
|
68 |
+
if (browser) {
|
69 |
+
await browser.close();
|
70 |
+
}
|
71 |
+
log('调度器已停止,浏览器已关闭');
|
72 |
+
process.exit(0);
|
73 |
+
});
|
74 |
+
|
75 |
+
} catch (error) {
|
76 |
+
logError(`[${getHumanReadableTimestamp()}] 调度器启动失败:`, error);
|
77 |
+
if (browser) {
|
78 |
+
await browser.close();
|
79 |
+
}
|
80 |
+
}
|
81 |
+
}
|
82 |
+
|
83 |
+
// 运行调度器
|
84 |
+
const __filename = fileURLToPath(import.meta.url);
|
85 |
+
const scriptPath = path.resolve(process.argv[1]);
|
86 |
+
|
87 |
+
if (path.resolve(__filename) === scriptPath) {
|
88 |
+
startScheduler().catch(console.error);
|
89 |
+
}
|
90 |
+
|
91 |
+
export { startScheduler };
|
hf-repo/hf-repo/hf-repo/src/start-services.js
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env node
|
2 |
+
|
3 |
+
/**
|
4 |
+
* 服务启动脚本
|
5 |
+
* 同时启动 Web 服务器和调度器
|
6 |
+
*/
|
7 |
+
|
8 |
+
import { spawn } from 'child_process';
|
9 |
+
import { log, logError } from './utils/common-utils.js';
|
10 |
+
|
11 |
+
// 存储子进程
|
12 |
+
const processes = [];
|
13 |
+
|
14 |
+
/**
|
15 |
+
* 启动子进程
|
16 |
+
*/
|
17 |
+
function startProcess(name, command, args = []) {
|
18 |
+
log(`启动 ${name}...`);
|
19 |
+
|
20 |
+
const child = spawn('node', [command, ...args], {
|
21 |
+
stdio: 'inherit',
|
22 |
+
cwd: process.cwd()
|
23 |
+
});
|
24 |
+
|
25 |
+
child.on('error', (error) => {
|
26 |
+
logError(`${name} 启动失败:`, error);
|
27 |
+
});
|
28 |
+
|
29 |
+
child.on('exit', (code, signal) => {
|
30 |
+
if (code !== 0) {
|
31 |
+
logError(`${name} 异常退出,代码: ${code}, 信号: ${signal}`);
|
32 |
+
} else {
|
33 |
+
log(`${name} 正常退出`);
|
34 |
+
}
|
35 |
+
});
|
36 |
+
|
37 |
+
processes.push({ name, process: child });
|
38 |
+
return child;
|
39 |
+
}
|
40 |
+
|
41 |
+
/**
|
42 |
+
* 优雅关闭所有进程
|
43 |
+
*/
|
44 |
+
function gracefulShutdown() {
|
45 |
+
log('收到关闭信号,正在停止所有服务...');
|
46 |
+
|
47 |
+
processes.forEach(({ name, process }) => {
|
48 |
+
if (!process.killed) {
|
49 |
+
log(`停止 ${name}...`);
|
50 |
+
process.kill('SIGTERM');
|
51 |
+
}
|
52 |
+
});
|
53 |
+
|
54 |
+
// 等待一段时间后强制关闭
|
55 |
+
setTimeout(() => {
|
56 |
+
processes.forEach(({ name, process }) => {
|
57 |
+
if (!process.killed) {
|
58 |
+
log(`强制停止 ${name}...`);
|
59 |
+
process.kill('SIGKILL');
|
60 |
+
}
|
61 |
+
});
|
62 |
+
process.exit(0);
|
63 |
+
}, 5000);
|
64 |
+
}
|
65 |
+
|
66 |
+
/**
|
67 |
+
* 主函数
|
68 |
+
*/
|
69 |
+
async function main() {
|
70 |
+
log('🚀 启动 CloudStudio Runner 服务');
|
71 |
+
log('='.repeat(50));
|
72 |
+
|
73 |
+
try {
|
74 |
+
// 启动 Web 服务器
|
75 |
+
const webServer = startProcess('Web 服务器', 'src/web-server.js');
|
76 |
+
|
77 |
+
// 等待一下确保 Web 服务器启动
|
78 |
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
79 |
+
|
80 |
+
// 启动调度器
|
81 |
+
const scheduler = startProcess('调度器', 'src/scheduler.js');
|
82 |
+
|
83 |
+
log('='.repeat(50));
|
84 |
+
log('✅ 所有服务已启动');
|
85 |
+
log('📊 Web 界面: http://localhost:7860');
|
86 |
+
log('⏰ 调度器: 每10分钟执行一次任务');
|
87 |
+
log('按 Ctrl+C 停止所有服务');
|
88 |
+
|
89 |
+
// 监听退出信号
|
90 |
+
process.on('SIGINT', gracefulShutdown);
|
91 |
+
process.on('SIGTERM', gracefulShutdown);
|
92 |
+
|
93 |
+
// 保持主进程运行
|
94 |
+
process.on('exit', () => {
|
95 |
+
log('主进程退出');
|
96 |
+
});
|
97 |
+
|
98 |
+
} catch (error) {
|
99 |
+
logError('启动服务失败:', error);
|
100 |
+
process.exit(1);
|
101 |
+
}
|
102 |
+
}
|
103 |
+
|
104 |
+
// 运行主函数
|
105 |
+
import { fileURLToPath } from 'url';
|
106 |
+
import path from 'path';
|
107 |
+
|
108 |
+
const __filename = fileURLToPath(import.meta.url);
|
109 |
+
const scriptPath = path.resolve(process.argv[1]);
|
110 |
+
|
111 |
+
if (path.resolve(__filename) === scriptPath) {
|
112 |
+
main().catch(error => {
|
113 |
+
logError('服务启动异常:', error);
|
114 |
+
process.exit(1);
|
115 |
+
});
|
116 |
+
}
|
117 |
+
|
118 |
+
export { main };
|
hf-repo/hf-repo/hf-repo/src/utils/common-utils.js
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import fs from 'fs';
|
2 |
+
import path from 'path';
|
3 |
+
|
4 |
+
// 日志文件路径
|
5 |
+
const LOG_FILE = './logs/app.log';
|
6 |
+
const LOG_DIR = './logs';
|
7 |
+
|
8 |
+
/**
|
9 |
+
* 确保日志目录存在
|
10 |
+
*/
|
11 |
+
function ensureLogDirectory() {
|
12 |
+
if (!fs.existsSync(LOG_DIR)) {
|
13 |
+
fs.mkdirSync(LOG_DIR, { recursive: true });
|
14 |
+
}
|
15 |
+
}
|
16 |
+
|
17 |
+
/**
|
18 |
+
* 写入日志到文件
|
19 |
+
* @param {string} level - 日志级别 (INFO, ERROR, WARN)
|
20 |
+
* @param {string} message - 日志消息
|
21 |
+
*/
|
22 |
+
export function writeLog(level, message) {
|
23 |
+
ensureLogDirectory();
|
24 |
+
const timestamp = new Date().toISOString();
|
25 |
+
const logEntry = `[${timestamp}] [${level}] ${message}\n`;
|
26 |
+
|
27 |
+
try {
|
28 |
+
fs.appendFileSync(LOG_FILE, logEntry);
|
29 |
+
} catch (error) {
|
30 |
+
console.error('写入日志文件失败:', error);
|
31 |
+
}
|
32 |
+
}
|
33 |
+
|
34 |
+
/**
|
35 |
+
* 增强的 console.log,同时输出到控制台和文件
|
36 |
+
* @param {...any} args - 要记录的参数
|
37 |
+
*/
|
38 |
+
export function log(...args) {
|
39 |
+
const message = args.map(arg =>
|
40 |
+
typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
|
41 |
+
).join(' ');
|
42 |
+
|
43 |
+
console.log(...args);
|
44 |
+
writeLog('INFO', message);
|
45 |
+
}
|
46 |
+
|
47 |
+
/**
|
48 |
+
* 增强的 console.error,同时输出到控制台和文件
|
49 |
+
* @param {...any} args - 要记录的参数
|
50 |
+
*/
|
51 |
+
export function logError(...args) {
|
52 |
+
const message = args.map(arg =>
|
53 |
+
typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
|
54 |
+
).join(' ');
|
55 |
+
|
56 |
+
console.error(...args);
|
57 |
+
writeLog('ERROR', message);
|
58 |
+
}
|
59 |
+
|
60 |
+
/**
|
61 |
+
* 读取最近的日志
|
62 |
+
* @param {number} lines - 要读取的行数,默认100行
|
63 |
+
* @returns {Array} 日志行数组
|
64 |
+
*/
|
65 |
+
export function getRecentLogs(lines = 100) {
|
66 |
+
try {
|
67 |
+
if (!fs.existsSync(LOG_FILE)) {
|
68 |
+
return [];
|
69 |
+
}
|
70 |
+
|
71 |
+
const content = fs.readFileSync(LOG_FILE, 'utf8');
|
72 |
+
const logLines = content.trim().split('\n').filter(line => line.length > 0);
|
73 |
+
|
74 |
+
// 返回最后 N 行
|
75 |
+
return logLines.slice(-lines);
|
76 |
+
} catch (error) {
|
77 |
+
console.error('读取日志文件失败:', error);
|
78 |
+
return [];
|
79 |
+
}
|
80 |
+
}
|
81 |
+
|
82 |
+
/**
|
83 |
+
* 清空日志文件
|
84 |
+
* @returns {boolean} 是否成功清空
|
85 |
+
*/
|
86 |
+
export function clearLogFile() {
|
87 |
+
try {
|
88 |
+
ensureLogDirectory();
|
89 |
+
fs.writeFileSync(LOG_FILE, '');
|
90 |
+
console.log('日志文件已清空');
|
91 |
+
return true;
|
92 |
+
} catch (error) {
|
93 |
+
console.error('清空日志文件失败:', error);
|
94 |
+
return false;
|
95 |
+
}
|
96 |
+
}
|
97 |
+
|
98 |
+
/**
|
99 |
+
* 创建人类可读的时间戳
|
100 |
+
* @returns {string} 格式化的时间戳 YYYY-MM-DD_HH-MM-SS
|
101 |
+
*/
|
102 |
+
export function getHumanReadableTimestamp() {
|
103 |
+
const now = new Date();
|
104 |
+
const year = now.getFullYear();
|
105 |
+
const month = String(now.getMonth() + 1).padStart(2, '0');
|
106 |
+
const day = String(now.getDate()).padStart(2, '0');
|
107 |
+
const hours = String(now.getHours()).padStart(2, '0');
|
108 |
+
const minutes = String(now.getMinutes()).padStart(2, '0');
|
109 |
+
const seconds = String(now.getSeconds()).padStart(2, '0');
|
110 |
+
|
111 |
+
return `${year}-${month}-${day}_${hours}-${minutes}-${seconds}`;
|
112 |
+
}
|
113 |
+
|
114 |
+
/**
|
115 |
+
* 确保截图目录存在
|
116 |
+
* @param {string} dir - 目录路径
|
117 |
+
*/
|
118 |
+
export function ensureScreenshotDirectory(dir) {
|
119 |
+
if (!fs.existsSync(dir)) {
|
120 |
+
fs.mkdirSync(dir, { recursive: true });
|
121 |
+
console.log(`创建截图目录: ${dir}`);
|
122 |
+
}
|
123 |
+
}
|
124 |
+
|
125 |
+
/**
|
126 |
+
* 检查 Cookie 文件是否存在
|
127 |
+
* @param {string} cookieFile - Cookie 文件路径
|
128 |
+
* @returns {boolean} 文件是否存在
|
129 |
+
*/
|
130 |
+
export function checkCookieFile(cookieFile) {
|
131 |
+
if (!fs.existsSync(cookieFile)) {
|
132 |
+
console.error(`Cookie文件不存在: ${cookieFile}`);
|
133 |
+
console.log('请先运行 npm run login 进行登录');
|
134 |
+
return false;
|
135 |
+
}
|
136 |
+
return true;
|
137 |
+
}
|
138 |
+
|
139 |
+
/**
|
140 |
+
* 读取并解析 Cookie 文件
|
141 |
+
* @param {string} cookieFile - Cookie 文件路径
|
142 |
+
* @returns {Array} Cookie 数组
|
143 |
+
*/
|
144 |
+
export function loadCookies(cookieFile) {
|
145 |
+
try {
|
146 |
+
const cookies = JSON.parse(fs.readFileSync(cookieFile, 'utf8'));
|
147 |
+
console.log(`已加载 ${cookies.length} 个cookies`);
|
148 |
+
return cookies;
|
149 |
+
} catch (error) {
|
150 |
+
console.error('读取 Cookie 文件失败:', error);
|
151 |
+
throw error;
|
152 |
+
}
|
153 |
+
}
|
154 |
+
|
155 |
+
/**
|
156 |
+
* 保存截图
|
157 |
+
* @param {Object} page - Playwright 页面对象
|
158 |
+
* @param {string} screenshotDir - 截图目录
|
159 |
+
* @param {string} prefix - 文件名前缀
|
160 |
+
* @returns {string} 截图文件路径
|
161 |
+
*/
|
162 |
+
export async function saveScreenshot(page, screenshotDir, prefix = 'screenshot') {
|
163 |
+
ensureScreenshotDirectory(screenshotDir);
|
164 |
+
const timestamp = getHumanReadableTimestamp();
|
165 |
+
const screenshotPath = path.join(screenshotDir, `${prefix}-${timestamp}.png`);
|
166 |
+
await page.screenshot({ path: screenshotPath });
|
167 |
+
console.log(`截图已保存: ${screenshotPath}`);
|
168 |
+
return screenshotPath;
|
169 |
+
}
|
hf-repo/hf-repo/hf-repo/src/utils/webide-utils.js
ADDED
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { chromium } from 'playwright';
|
2 |
+
import config from '../config.js';
|
3 |
+
import { loadCookies, saveScreenshot, getHumanReadableTimestamp } from './common-utils.js';
|
4 |
+
|
5 |
+
/**
|
6 |
+
* 创建浏览器实例和上下文
|
7 |
+
* @param {string} cookieFile - Cookie 文件路径
|
8 |
+
* @returns {Object} { browser, context, page }
|
9 |
+
*/
|
10 |
+
export async function createBrowserSession(cookieFile) {
|
11 |
+
console.log('启动浏览器...');
|
12 |
+
const browser = await chromium.launch(config.browserOptions);
|
13 |
+
const context = await browser.newContext();
|
14 |
+
|
15 |
+
// 读取并设置cookies
|
16 |
+
const cookies = loadCookies(cookieFile);
|
17 |
+
await context.addCookies(cookies);
|
18 |
+
|
19 |
+
const page = await context.newPage();
|
20 |
+
|
21 |
+
return { browser, context, page };
|
22 |
+
}
|
23 |
+
|
24 |
+
/**
|
25 |
+
* 导航到 WebIDE 页面并验证登录状态
|
26 |
+
* @param {Object} page - Playwright 页面对象
|
27 |
+
*/
|
28 |
+
export async function navigateToWebIDE(page) {
|
29 |
+
console.log('导航到WebIDE页面...');
|
30 |
+
await page.goto(config.webideUrl);
|
31 |
+
|
32 |
+
// 等待页面加载
|
33 |
+
await page.waitForTimeout(config.waitTimes.pageLoad);
|
34 |
+
|
35 |
+
console.log('当前页面URL:', page.url());
|
36 |
+
console.log('页面标题:', await page.title());
|
37 |
+
|
38 |
+
// 检查是否成功登录
|
39 |
+
try {
|
40 |
+
await page.waitForSelector(config.selectors.editor, {
|
41 |
+
timeout: 60000
|
42 |
+
});
|
43 |
+
console.log('成功进入WebIDE界面');
|
44 |
+
return true;
|
45 |
+
} catch (error) {
|
46 |
+
console.log('警告: 未检测到编辑器界面,可能需要重新登录');
|
47 |
+
return false;
|
48 |
+
}
|
49 |
+
}
|
50 |
+
|
51 |
+
/**
|
52 |
+
* 处理模态对话框
|
53 |
+
* @param {Object} page - Playwright 页面对象
|
54 |
+
*/
|
55 |
+
export async function handleModalDialog(page) {
|
56 |
+
try {
|
57 |
+
const dialogButton = await page.waitForSelector(config.selectors.dialogButton, { timeout: 30000 });
|
58 |
+
if (dialogButton && await dialogButton.isVisible()) {
|
59 |
+
console.log('发现模态对话框按钮,点击处理...');
|
60 |
+
await dialogButton.click();
|
61 |
+
await page.waitForTimeout(500);
|
62 |
+
return true;
|
63 |
+
}
|
64 |
+
} catch (error) {
|
65 |
+
// 没有找到对话框按钮,继续执行
|
66 |
+
console.log('未发现模态对话框,继续执行...');
|
67 |
+
}
|
68 |
+
return false;
|
69 |
+
}
|
70 |
+
|
71 |
+
/**
|
72 |
+
* 打开终端
|
73 |
+
* @param {Object} page - Playwright 页面对象
|
74 |
+
* @returns {Object|null} 终端元素或 null
|
75 |
+
*/
|
76 |
+
export async function openTerminal(page) {
|
77 |
+
console.log('尝试打开终端 (Ctrl+~)...');
|
78 |
+
|
79 |
+
// 确保页面获得焦点
|
80 |
+
await page.click('body');
|
81 |
+
await page.waitForTimeout(500);
|
82 |
+
|
83 |
+
// 按下 Ctrl+~ 打开终端
|
84 |
+
await page.keyboard.press('Control+`');
|
85 |
+
|
86 |
+
// 等待终端打开
|
87 |
+
await page.waitForTimeout(config.waitTimes.terminalOpen);
|
88 |
+
|
89 |
+
// 尝试多种方式查找终端
|
90 |
+
const terminalSelectors = config.selectors.terminals;
|
91 |
+
|
92 |
+
let terminalFound = false;
|
93 |
+
let terminalElement = null;
|
94 |
+
|
95 |
+
for (const selector of terminalSelectors) {
|
96 |
+
try {
|
97 |
+
terminalElement = await page.waitForSelector(selector, { timeout: 2000 });
|
98 |
+
if (terminalElement) {
|
99 |
+
console.log(`找到终端元素: ${selector}`);
|
100 |
+
terminalFound = true;
|
101 |
+
break;
|
102 |
+
}
|
103 |
+
} catch (error) {
|
104 |
+
// 继续尝试下一个选择器
|
105 |
+
}
|
106 |
+
}
|
107 |
+
|
108 |
+
if (!terminalFound) {
|
109 |
+
console.log('未找到终端元素,尝试直接输入命令...');
|
110 |
+
return null;
|
111 |
+
} else {
|
112 |
+
// 点击终端区域确保焦点
|
113 |
+
await terminalElement.click();
|
114 |
+
await page.waitForTimeout(500);
|
115 |
+
return terminalElement;
|
116 |
+
}
|
117 |
+
}
|
118 |
+
|
119 |
+
/**
|
120 |
+
* 在终端中执行命令
|
121 |
+
* @param {Object} page - Playwright 页面对象
|
122 |
+
* @param {string} command - 要执行的命令
|
123 |
+
*/
|
124 |
+
export async function executeTerminalCommand(page, command) {
|
125 |
+
console.log(`执行命令: ${command}`);
|
126 |
+
|
127 |
+
// 输入命令
|
128 |
+
await page.keyboard.type(command);
|
129 |
+
await page.waitForTimeout(500);
|
130 |
+
|
131 |
+
// 按回车执行命令
|
132 |
+
await page.keyboard.press('Enter');
|
133 |
+
|
134 |
+
// 等待命令执行
|
135 |
+
await page.waitForTimeout(config.waitTimes.commandExecution);
|
136 |
+
|
137 |
+
console.log('命令已执行');
|
138 |
+
}
|
139 |
+
|
140 |
+
/**
|
141 |
+
* 完整的命令执行流程
|
142 |
+
* @param {Object} page - Playwright 页面对象
|
143 |
+
* @param {string} screenshotPrefix - 截图文件名前缀
|
144 |
+
* @returns {boolean} 执行是否成功
|
145 |
+
*/
|
146 |
+
export async function executeCommandFlow(page, screenshotPrefix = 'screenshot') {
|
147 |
+
try {
|
148 |
+
// 处理模态对话框
|
149 |
+
await handleModalDialog(page);
|
150 |
+
|
151 |
+
// 打开终端
|
152 |
+
await openTerminal(page);
|
153 |
+
|
154 |
+
// 执行命令
|
155 |
+
await executeTerminalCommand(page, config.command);
|
156 |
+
|
157 |
+
// 截图保存执行结果
|
158 |
+
// const screenshotDir = config.screenshotDir || './screenshots';
|
159 |
+
// const screenshotPath = await saveScreenshot(page, screenshotDir, screenshotPrefix);
|
160 |
+
|
161 |
+
return true;
|
162 |
+
} catch (error) {
|
163 |
+
console.error(`[${getHumanReadableTimestamp()}] 执行命令时发生错误:`, error);
|
164 |
+
return false;
|
165 |
+
}
|
166 |
+
}
|
hf-repo/hf-repo/hf-repo/src/web-server.js
ADDED
@@ -0,0 +1,381 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { Hono } from 'hono';
|
2 |
+
import { serve } from '@hono/node-server';
|
3 |
+
import { getRecentLogs, log, logError, clearLogFile } from './utils/common-utils.js';
|
4 |
+
import config from './config.js';
|
5 |
+
|
6 |
+
const app = new Hono();
|
7 |
+
|
8 |
+
// 静态 HTML 页面
|
9 |
+
const indexHTML = `
|
10 |
+
<!DOCTYPE html>
|
11 |
+
<html lang="zh-CN">
|
12 |
+
<head>
|
13 |
+
<meta charset="UTF-8">
|
14 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
15 |
+
<title>CloudStudio Runner - 日志查看器</title>
|
16 |
+
<style>
|
17 |
+
body {
|
18 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
19 |
+
margin: 0;
|
20 |
+
padding: 20px;
|
21 |
+
background-color: #f5f5f5;
|
22 |
+
color: #333;
|
23 |
+
}
|
24 |
+
.container {
|
25 |
+
max-width: 1200px;
|
26 |
+
margin: 0 auto;
|
27 |
+
background: white;
|
28 |
+
border-radius: 8px;
|
29 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
30 |
+
overflow: hidden;
|
31 |
+
}
|
32 |
+
.header {
|
33 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
34 |
+
color: white;
|
35 |
+
padding: 20px;
|
36 |
+
text-align: center;
|
37 |
+
}
|
38 |
+
.header h1 {
|
39 |
+
margin: 0;
|
40 |
+
font-size: 2em;
|
41 |
+
}
|
42 |
+
.header p {
|
43 |
+
margin: 10px 0 0 0;
|
44 |
+
opacity: 0.9;
|
45 |
+
}
|
46 |
+
.controls {
|
47 |
+
padding: 20px;
|
48 |
+
border-bottom: 1px solid #eee;
|
49 |
+
display: flex;
|
50 |
+
gap: 10px;
|
51 |
+
align-items: center;
|
52 |
+
flex-wrap: wrap;
|
53 |
+
}
|
54 |
+
.controls label {
|
55 |
+
font-weight: bold;
|
56 |
+
}
|
57 |
+
.controls select, .controls button {
|
58 |
+
padding: 8px 12px;
|
59 |
+
border: 1px solid #ddd;
|
60 |
+
border-radius: 4px;
|
61 |
+
font-size: 14px;
|
62 |
+
}
|
63 |
+
.controls button {
|
64 |
+
background: #667eea;
|
65 |
+
color: white;
|
66 |
+
border: none;
|
67 |
+
cursor: pointer;
|
68 |
+
transition: background 0.3s;
|
69 |
+
}
|
70 |
+
.controls button:hover {
|
71 |
+
background: #5a6fd8;
|
72 |
+
}
|
73 |
+
.log-container {
|
74 |
+
padding: 20px;
|
75 |
+
max-height: 600px;
|
76 |
+
overflow-y: auto;
|
77 |
+
}
|
78 |
+
.log-entry {
|
79 |
+
margin-bottom: 8px;
|
80 |
+
padding: 8px;
|
81 |
+
border-radius: 4px;
|
82 |
+
font-family: 'Courier New', monospace;
|
83 |
+
font-size: 13px;
|
84 |
+
line-height: 1.4;
|
85 |
+
border-left: 3px solid #ddd;
|
86 |
+
}
|
87 |
+
.log-entry.info {
|
88 |
+
background-color: #f8f9fa;
|
89 |
+
border-left-color: #28a745;
|
90 |
+
}
|
91 |
+
.log-entry.error {
|
92 |
+
background-color: #fff5f5;
|
93 |
+
border-left-color: #dc3545;
|
94 |
+
color: #721c24;
|
95 |
+
}
|
96 |
+
.log-entry.warn {
|
97 |
+
background-color: #fffbf0;
|
98 |
+
border-left-color: #ffc107;
|
99 |
+
color: #856404;
|
100 |
+
}
|
101 |
+
.timestamp {
|
102 |
+
color: #6c757d;
|
103 |
+
font-weight: bold;
|
104 |
+
}
|
105 |
+
.level {
|
106 |
+
font-weight: bold;
|
107 |
+
margin: 0 8px;
|
108 |
+
}
|
109 |
+
.level.info { color: #28a745; }
|
110 |
+
.level.error { color: #dc3545; }
|
111 |
+
.level.warn { color: #ffc107; }
|
112 |
+
.message {
|
113 |
+
word-break: break-word;
|
114 |
+
}
|
115 |
+
.status {
|
116 |
+
padding: 10px 20px;
|
117 |
+
background: #e9ecef;
|
118 |
+
border-top: 1px solid #ddd;
|
119 |
+
font-size: 14px;
|
120 |
+
color: #6c757d;
|
121 |
+
}
|
122 |
+
.loading {
|
123 |
+
text-align: center;
|
124 |
+
padding: 40px;
|
125 |
+
color: #6c757d;
|
126 |
+
}
|
127 |
+
.empty {
|
128 |
+
text-align: center;
|
129 |
+
padding: 40px;
|
130 |
+
color: #6c757d;
|
131 |
+
font-style: italic;
|
132 |
+
}
|
133 |
+
</style>
|
134 |
+
</head>
|
135 |
+
<body>
|
136 |
+
<div class="container">
|
137 |
+
<div class="header">
|
138 |
+
<h1>🚀 CloudStudio Runner</h1>
|
139 |
+
<p>实时日志查看器 - 监控自动化任务执行状态</p>
|
140 |
+
</div>
|
141 |
+
|
142 |
+
<div class="controls">
|
143 |
+
<label for="lines">显示行数:</label>
|
144 |
+
<select id="lines">
|
145 |
+
<option value="50">50 行</option>
|
146 |
+
<option value="100" selected>100 行</option>
|
147 |
+
<option value="200">200 行</option>
|
148 |
+
<option value="500">500 行</option>
|
149 |
+
<option value="1000">1000 行</option>
|
150 |
+
</select>
|
151 |
+
|
152 |
+
<button onclick="refreshLogs()">🔄 刷新日志</button>
|
153 |
+
<button onclick="toggleAutoRefresh()">⏱️ 自动刷新</button>
|
154 |
+
<button onclick="clearLogs()">🗑️ 清空显示</button>
|
155 |
+
</div>
|
156 |
+
|
157 |
+
<div class="log-container" id="logContainer">
|
158 |
+
<div class="loading">正在加载日志...</div>
|
159 |
+
</div>
|
160 |
+
|
161 |
+
<div class="status" id="status">
|
162 |
+
准备就绪
|
163 |
+
</div>
|
164 |
+
</div>
|
165 |
+
|
166 |
+
<script>
|
167 |
+
let autoRefreshInterval = null;
|
168 |
+
let isAutoRefresh = false;
|
169 |
+
|
170 |
+
async function fetchLogs() {
|
171 |
+
try {
|
172 |
+
const lines = document.getElementById('lines').value;
|
173 |
+
const response = await fetch(\`/api/logs?lines=\${lines}\`);
|
174 |
+
const data = await response.json();
|
175 |
+
|
176 |
+
if (data.success) {
|
177 |
+
displayLogs(data.logs);
|
178 |
+
updateStatus(\`已加载 \${data.logs.length} 条日志 - \${new Date().toLocaleString()}\`);
|
179 |
+
} else {
|
180 |
+
updateStatus('获取日志失败: ' + data.error);
|
181 |
+
}
|
182 |
+
} catch (error) {
|
183 |
+
updateStatus('网络错误: ' + error.message);
|
184 |
+
}
|
185 |
+
}
|
186 |
+
|
187 |
+
function displayLogs(logs) {
|
188 |
+
const container = document.getElementById('logContainer');
|
189 |
+
|
190 |
+
if (logs.length === 0) {
|
191 |
+
container.innerHTML = '<div class="empty">暂无日志数据</div>';
|
192 |
+
return;
|
193 |
+
}
|
194 |
+
|
195 |
+
const logHTML = logs.map(log => {
|
196 |
+
const match = log.match(/\\[(.*?)\\]\\s*\\[(.*?)\\]\\s*(.*)/);
|
197 |
+
if (match) {
|
198 |
+
const [, timestamp, level, message] = match;
|
199 |
+
const levelClass = level.toLowerCase();
|
200 |
+
return \`
|
201 |
+
<div class="log-entry \${levelClass}">
|
202 |
+
<span class="timestamp">\${timestamp}</span>
|
203 |
+
<span class="level \${levelClass}">[\${level}]</span>
|
204 |
+
<span class="message">\${message}</span>
|
205 |
+
</div>
|
206 |
+
\`;
|
207 |
+
} else {
|
208 |
+
return \`
|
209 |
+
<div class="log-entry">
|
210 |
+
<span class="message">\${log}</span>
|
211 |
+
</div>
|
212 |
+
\`;
|
213 |
+
}
|
214 |
+
}).join('');
|
215 |
+
|
216 |
+
container.innerHTML = logHTML;
|
217 |
+
container.scrollTop = container.scrollHeight;
|
218 |
+
}
|
219 |
+
|
220 |
+
function updateStatus(message) {
|
221 |
+
document.getElementById('status').textContent = message;
|
222 |
+
}
|
223 |
+
|
224 |
+
function refreshLogs() {
|
225 |
+
fetchLogs();
|
226 |
+
}
|
227 |
+
|
228 |
+
function toggleAutoRefresh() {
|
229 |
+
const button = event.target;
|
230 |
+
|
231 |
+
if (isAutoRefresh) {
|
232 |
+
clearInterval(autoRefreshInterval);
|
233 |
+
autoRefreshInterval = null;
|
234 |
+
isAutoRefresh = false;
|
235 |
+
button.textContent = '⏱️ 自动刷新';
|
236 |
+
updateStatus('自动刷新已停止');
|
237 |
+
} else {
|
238 |
+
autoRefreshInterval = setInterval(fetchLogs, 5000);
|
239 |
+
isAutoRefresh = true;
|
240 |
+
button.textContent = '⏹️ 停止刷新';
|
241 |
+
updateStatus('自动刷新已启动 (每5秒)');
|
242 |
+
}
|
243 |
+
}
|
244 |
+
|
245 |
+
async function clearLogs() {
|
246 |
+
try {
|
247 |
+
updateStatus('正在清空日志...');
|
248 |
+
|
249 |
+
const response = await fetch('/api/clear-logs', {
|
250 |
+
method: 'POST',
|
251 |
+
headers: {
|
252 |
+
'Content-Type': 'application/json'
|
253 |
+
}
|
254 |
+
});
|
255 |
+
|
256 |
+
const data = await response.json();
|
257 |
+
|
258 |
+
if (data.success) {
|
259 |
+
document.getElementById('logContainer').innerHTML = '<div class="empty">日志已清空</div>';
|
260 |
+
updateStatus('日志文件已清空 - ' + new Date().toLocaleString());
|
261 |
+
} else {
|
262 |
+
updateStatus('清空日志失败: ' + data.error);
|
263 |
+
}
|
264 |
+
} catch (error) {
|
265 |
+
updateStatus('清空日志时发生网络错误: ' + error.message);
|
266 |
+
}
|
267 |
+
}
|
268 |
+
|
269 |
+
// 页面加载时获取日志
|
270 |
+
document.addEventListener('DOMContentLoaded', fetchLogs);
|
271 |
+
</script>
|
272 |
+
</body>
|
273 |
+
</html>
|
274 |
+
`;
|
275 |
+
|
276 |
+
// 路由定义
|
277 |
+
app.get('/', (c) => {
|
278 |
+
return c.html(indexHTML);
|
279 |
+
});
|
280 |
+
|
281 |
+
// API 路由 - 获取日志
|
282 |
+
app.get('/api/logs', (c) => {
|
283 |
+
try {
|
284 |
+
const lines = parseInt(c.req.query('lines') || '100');
|
285 |
+
const logs = getRecentLogs(lines);
|
286 |
+
|
287 |
+
return c.json({
|
288 |
+
success: true,
|
289 |
+
logs: logs,
|
290 |
+
count: logs.length,
|
291 |
+
timestamp: new Date().toISOString()
|
292 |
+
});
|
293 |
+
} catch (error) {
|
294 |
+
logError('获取日志API错误:', error);
|
295 |
+
return c.json({
|
296 |
+
success: false,
|
297 |
+
error: error.message
|
298 |
+
}, 500);
|
299 |
+
}
|
300 |
+
});
|
301 |
+
|
302 |
+
// API 路由 - 清空日志
|
303 |
+
app.post('/api/clear-logs', (c) => {
|
304 |
+
try {
|
305 |
+
const success = clearLogFile();
|
306 |
+
|
307 |
+
if (success) {
|
308 |
+
log('日志文件已通过Web界面清空');
|
309 |
+
return c.json({
|
310 |
+
success: true,
|
311 |
+
message: '日志文件已清空',
|
312 |
+
timestamp: new Date().toISOString()
|
313 |
+
});
|
314 |
+
} else {
|
315 |
+
return c.json({
|
316 |
+
success: false,
|
317 |
+
error: '清空日志文件失败'
|
318 |
+
}, 500);
|
319 |
+
}
|
320 |
+
} catch (error) {
|
321 |
+
logError('清空日志API错误:', error);
|
322 |
+
return c.json({
|
323 |
+
success: false,
|
324 |
+
error: error.message
|
325 |
+
}, 500);
|
326 |
+
}
|
327 |
+
});
|
328 |
+
|
329 |
+
// API 路由 - 系统状态
|
330 |
+
app.get('/api/status', (c) => {
|
331 |
+
return c.json({
|
332 |
+
success: true,
|
333 |
+
status: 'running',
|
334 |
+
uptime: process.uptime(),
|
335 |
+
memory: process.memoryUsage(),
|
336 |
+
timestamp: new Date().toISOString(),
|
337 |
+
config: {
|
338 |
+
webideUrl: config.webideUrl,
|
339 |
+
schedulerInterval: config.schedulerInterval,
|
340 |
+
headless: config.browserOptions.headless
|
341 |
+
}
|
342 |
+
});
|
343 |
+
});
|
344 |
+
|
345 |
+
// 健康检查
|
346 |
+
app.get('/health', (c) => {
|
347 |
+
return c.json({ status: 'ok', timestamp: new Date().toISOString() });
|
348 |
+
});
|
349 |
+
|
350 |
+
// 启动服务器
|
351 |
+
const port = process.env.PORT || 7860;
|
352 |
+
|
353 |
+
async function startServer() {
|
354 |
+
try {
|
355 |
+
log(`启动 Web 服务器,端口: ${port}`);
|
356 |
+
log(`访问地址: http://localhost:${port}`);
|
357 |
+
|
358 |
+
serve({
|
359 |
+
fetch: app.fetch,
|
360 |
+
port: port,
|
361 |
+
});
|
362 |
+
|
363 |
+
log('Web 服务器启动成功');
|
364 |
+
} catch (error) {
|
365 |
+
logError('启动 Web 服务器失败:', error);
|
366 |
+
process.exit(1);
|
367 |
+
}
|
368 |
+
}
|
369 |
+
|
370 |
+
// 如果直接运行此文件,启动服务器
|
371 |
+
import { fileURLToPath } from 'url';
|
372 |
+
import path from 'path';
|
373 |
+
|
374 |
+
const __filename = fileURLToPath(import.meta.url);
|
375 |
+
const scriptPath = path.resolve(process.argv[1]);
|
376 |
+
|
377 |
+
if (path.resolve(__filename) === scriptPath) {
|
378 |
+
startServer();
|
379 |
+
}
|
380 |
+
|
381 |
+
export { app, startServer };
|
hf-repo/hf-repo/hf-repo/start-services.js
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env node
|
2 |
+
|
3 |
+
/**
|
4 |
+
* 服务启动脚本
|
5 |
+
* 同时启动 Web 服务器和调度器
|
6 |
+
*/
|
7 |
+
|
8 |
+
import { spawn } from 'child_process';
|
9 |
+
import { log, logError } from './utils/common-utils.js';
|
10 |
+
|
11 |
+
// 存储子进程
|
12 |
+
const processes = [];
|
13 |
+
|
14 |
+
/**
|
15 |
+
* 启动子进程
|
16 |
+
*/
|
17 |
+
function startProcess(name, command, args = []) {
|
18 |
+
log(`启动 ${name}...`);
|
19 |
+
|
20 |
+
const child = spawn('node', [command, ...args], {
|
21 |
+
stdio: 'inherit',
|
22 |
+
cwd: process.cwd()
|
23 |
+
});
|
24 |
+
|
25 |
+
child.on('error', (error) => {
|
26 |
+
logError(`${name} 启动失败:`, error);
|
27 |
+
});
|
28 |
+
|
29 |
+
child.on('exit', (code, signal) => {
|
30 |
+
if (code !== 0) {
|
31 |
+
logError(`${name} 异常退出,代码: ${code}, 信号: ${signal}`);
|
32 |
+
} else {
|
33 |
+
log(`${name} 正常退出`);
|
34 |
+
}
|
35 |
+
});
|
36 |
+
|
37 |
+
processes.push({ name, process: child });
|
38 |
+
return child;
|
39 |
+
}
|
40 |
+
|
41 |
+
/**
|
42 |
+
* 优雅关闭所有进程
|
43 |
+
*/
|
44 |
+
function gracefulShutdown() {
|
45 |
+
log('收到关闭信号,正在停止所有服务...');
|
46 |
+
|
47 |
+
processes.forEach(({ name, process }) => {
|
48 |
+
if (!process.killed) {
|
49 |
+
log(`停止 ${name}...`);
|
50 |
+
process.kill('SIGTERM');
|
51 |
+
}
|
52 |
+
});
|
53 |
+
|
54 |
+
// 等待一段时间后强制关闭
|
55 |
+
setTimeout(() => {
|
56 |
+
processes.forEach(({ name, process }) => {
|
57 |
+
if (!process.killed) {
|
58 |
+
log(`强制停止 ${name}...`);
|
59 |
+
process.kill('SIGKILL');
|
60 |
+
}
|
61 |
+
});
|
62 |
+
process.exit(0);
|
63 |
+
}, 5000);
|
64 |
+
}
|
65 |
+
|
66 |
+
/**
|
67 |
+
* 主函数
|
68 |
+
*/
|
69 |
+
async function main() {
|
70 |
+
log('🚀 启动 CloudStudio Runner 服务');
|
71 |
+
log('='.repeat(50));
|
72 |
+
|
73 |
+
try {
|
74 |
+
// 启动 Web 服务器
|
75 |
+
const webServer = startProcess('Web 服务器', 'web-server.js');
|
76 |
+
|
77 |
+
// 等待一下确保 Web 服务器启动
|
78 |
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
79 |
+
|
80 |
+
// 启动调度器
|
81 |
+
const scheduler = startProcess('调度器', 'scheduler.js');
|
82 |
+
|
83 |
+
log('='.repeat(50));
|
84 |
+
log('✅ 所有服务已启动');
|
85 |
+
log('📊 Web 界面: http://localhost:7860');
|
86 |
+
log('⏰ 调度器: 每10分钟执行一次任务');
|
87 |
+
log('按 Ctrl+C 停止所有服务');
|
88 |
+
|
89 |
+
// 监听退出信号
|
90 |
+
process.on('SIGINT', gracefulShutdown);
|
91 |
+
process.on('SIGTERM', gracefulShutdown);
|
92 |
+
|
93 |
+
// 保持主进程运行
|
94 |
+
process.on('exit', () => {
|
95 |
+
log('主进程退出');
|
96 |
+
});
|
97 |
+
|
98 |
+
} catch (error) {
|
99 |
+
logError('启动服务失败:', error);
|
100 |
+
process.exit(1);
|
101 |
+
}
|
102 |
+
}
|
103 |
+
|
104 |
+
// 运行主函数
|
105 |
+
import { fileURLToPath } from 'url';
|
106 |
+
import path from 'path';
|
107 |
+
|
108 |
+
const __filename = fileURLToPath(import.meta.url);
|
109 |
+
const scriptPath = path.resolve(process.argv[1]);
|
110 |
+
|
111 |
+
if (path.resolve(__filename) === scriptPath) {
|
112 |
+
main().catch(error => {
|
113 |
+
logError('服务启动异常:', error);
|
114 |
+
process.exit(1);
|
115 |
+
});
|
116 |
+
}
|
117 |
+
|
118 |
+
export { main };
|
hf-repo/hf-repo/hf-repo/utils/common-utils.js
ADDED
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import fs from 'fs';
|
2 |
+
import path from 'path';
|
3 |
+
|
4 |
+
// 日志文件路径
|
5 |
+
const LOG_FILE = './logs/app.log';
|
6 |
+
const LOG_DIR = './logs';
|
7 |
+
|
8 |
+
/**
|
9 |
+
* 确保日志目录存在
|
10 |
+
*/
|
11 |
+
function ensureLogDirectory() {
|
12 |
+
if (!fs.existsSync(LOG_DIR)) {
|
13 |
+
fs.mkdirSync(LOG_DIR, { recursive: true });
|
14 |
+
}
|
15 |
+
}
|
16 |
+
|
17 |
+
/**
|
18 |
+
* 写入日志到文件
|
19 |
+
* @param {string} level - 日志级别 (INFO, ERROR, WARN)
|
20 |
+
* @param {string} message - 日志消息
|
21 |
+
*/
|
22 |
+
export function writeLog(level, message) {
|
23 |
+
ensureLogDirectory();
|
24 |
+
const timestamp = new Date().toISOString();
|
25 |
+
const logEntry = `[${timestamp}] [${level}] ${message}\n`;
|
26 |
+
|
27 |
+
try {
|
28 |
+
fs.appendFileSync(LOG_FILE, logEntry);
|
29 |
+
} catch (error) {
|
30 |
+
console.error('写入日志文件失败:', error);
|
31 |
+
}
|
32 |
+
}
|
33 |
+
|
34 |
+
/**
|
35 |
+
* 增强的 console.log,同时输出到控制台和文件
|
36 |
+
* @param {...any} args - 要记录的参数
|
37 |
+
*/
|
38 |
+
export function log(...args) {
|
39 |
+
const message = args.map(arg =>
|
40 |
+
typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
|
41 |
+
).join(' ');
|
42 |
+
|
43 |
+
console.log(...args);
|
44 |
+
writeLog('INFO', message);
|
45 |
+
}
|
46 |
+
|
47 |
+
/**
|
48 |
+
* 增强的 console.error,同时输出到控制台和文件
|
49 |
+
* @param {...any} args - 要记录的参数
|
50 |
+
*/
|
51 |
+
export function logError(...args) {
|
52 |
+
const message = args.map(arg =>
|
53 |
+
typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
|
54 |
+
).join(' ');
|
55 |
+
|
56 |
+
console.error(...args);
|
57 |
+
writeLog('ERROR', message);
|
58 |
+
}
|
59 |
+
|
60 |
+
/**
|
61 |
+
* 读取最近的日志
|
62 |
+
* @param {number} lines - 要读取的行数,默认100行
|
63 |
+
* @returns {Array} 日志行数组
|
64 |
+
*/
|
65 |
+
export function getRecentLogs(lines = 100) {
|
66 |
+
try {
|
67 |
+
if (!fs.existsSync(LOG_FILE)) {
|
68 |
+
return [];
|
69 |
+
}
|
70 |
+
|
71 |
+
const content = fs.readFileSync(LOG_FILE, 'utf8');
|
72 |
+
const logLines = content.trim().split('\n').filter(line => line.length > 0);
|
73 |
+
|
74 |
+
// 返回最后 N 行
|
75 |
+
return logLines.slice(-lines);
|
76 |
+
} catch (error) {
|
77 |
+
console.error('读取日志文件失败:', error);
|
78 |
+
return [];
|
79 |
+
}
|
80 |
+
}
|
81 |
+
|
82 |
+
/**
|
83 |
+
* 创建人类可读的时间戳
|
84 |
+
* @returns {string} 格式化的时间戳 YYYY-MM-DD_HH-MM-SS
|
85 |
+
*/
|
86 |
+
export function getHumanReadableTimestamp() {
|
87 |
+
const now = new Date();
|
88 |
+
const year = now.getFullYear();
|
89 |
+
const month = String(now.getMonth() + 1).padStart(2, '0');
|
90 |
+
const day = String(now.getDate()).padStart(2, '0');
|
91 |
+
const hours = String(now.getHours()).padStart(2, '0');
|
92 |
+
const minutes = String(now.getMinutes()).padStart(2, '0');
|
93 |
+
const seconds = String(now.getSeconds()).padStart(2, '0');
|
94 |
+
|
95 |
+
return `${year}-${month}-${day}_${hours}-${minutes}-${seconds}`;
|
96 |
+
}
|
97 |
+
|
98 |
+
/**
|
99 |
+
* 确保截图目录存在
|
100 |
+
* @param {string} dir - 目录路径
|
101 |
+
*/
|
102 |
+
export function ensureScreenshotDirectory(dir) {
|
103 |
+
if (!fs.existsSync(dir)) {
|
104 |
+
fs.mkdirSync(dir, { recursive: true });
|
105 |
+
console.log(`创建截图目录: ${dir}`);
|
106 |
+
}
|
107 |
+
}
|
108 |
+
|
109 |
+
/**
|
110 |
+
* 检查 Cookie 文件是否存在
|
111 |
+
* @param {string} cookieFile - Cookie 文件路径
|
112 |
+
* @returns {boolean} 文件是否存在
|
113 |
+
*/
|
114 |
+
export function checkCookieFile(cookieFile) {
|
115 |
+
if (!fs.existsSync(cookieFile)) {
|
116 |
+
console.error(`Cookie文件不存在: ${cookieFile}`);
|
117 |
+
console.log('请先运行 npm run login 进行登录');
|
118 |
+
return false;
|
119 |
+
}
|
120 |
+
return true;
|
121 |
+
}
|
122 |
+
|
123 |
+
/**
|
124 |
+
* 读取并解析 Cookie 文件
|
125 |
+
* @param {string} cookieFile - Cookie 文件路径
|
126 |
+
* @returns {Array} Cookie 数组
|
127 |
+
*/
|
128 |
+
export function loadCookies(cookieFile) {
|
129 |
+
try {
|
130 |
+
const cookies = JSON.parse(fs.readFileSync(cookieFile, 'utf8'));
|
131 |
+
console.log(`已加载 ${cookies.length} 个cookies`);
|
132 |
+
return cookies;
|
133 |
+
} catch (error) {
|
134 |
+
console.error('读取 Cookie 文件失败:', error);
|
135 |
+
throw error;
|
136 |
+
}
|
137 |
+
}
|
138 |
+
|
139 |
+
/**
|
140 |
+
* 保存截图
|
141 |
+
* @param {Object} page - Playwright 页面对象
|
142 |
+
* @param {string} screenshotDir - 截图目录
|
143 |
+
* @param {string} prefix - 文件名前缀
|
144 |
+
* @returns {string} 截图文件路径
|
145 |
+
*/
|
146 |
+
export async function saveScreenshot(page, screenshotDir, prefix = 'screenshot') {
|
147 |
+
ensureScreenshotDirectory(screenshotDir);
|
148 |
+
const timestamp = getHumanReadableTimestamp();
|
149 |
+
const screenshotPath = path.join(screenshotDir, `${prefix}-${timestamp}.png`);
|
150 |
+
await page.screenshot({ path: screenshotPath });
|
151 |
+
console.log(`截图已保存: ${screenshotPath}`);
|
152 |
+
return screenshotPath;
|
153 |
+
}
|
hf-repo/hf-repo/hf-repo/utils/webide-utils.js
ADDED
@@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { chromium } from 'playwright';
|
2 |
+
import config from '../config.js';
|
3 |
+
import { loadCookies, saveScreenshot, getHumanReadableTimestamp } from './common-utils.js';
|
4 |
+
|
5 |
+
/**
|
6 |
+
* 创建浏览器实例和上下文
|
7 |
+
* @param {string} cookieFile - Cookie 文件路径
|
8 |
+
* @returns {Object} { browser, context, page }
|
9 |
+
*/
|
10 |
+
export async function createBrowserSession(cookieFile) {
|
11 |
+
console.log('启动浏览器...');
|
12 |
+
const browser = await chromium.launch(config.browserOptions);
|
13 |
+
const context = await browser.newContext();
|
14 |
+
|
15 |
+
// 读取并设置cookies
|
16 |
+
const cookies = loadCookies(cookieFile);
|
17 |
+
await context.addCookies(cookies);
|
18 |
+
|
19 |
+
const page = await context.newPage();
|
20 |
+
|
21 |
+
return { browser, context, page };
|
22 |
+
}
|
23 |
+
|
24 |
+
/**
|
25 |
+
* 导航到 WebIDE 页面并验证登录状态
|
26 |
+
* @param {Object} page - Playwright 页面对象
|
27 |
+
*/
|
28 |
+
export async function navigateToWebIDE(page) {
|
29 |
+
console.log('导航到WebIDE页面...');
|
30 |
+
await page.goto(config.webideUrl);
|
31 |
+
|
32 |
+
// 等待页面加载
|
33 |
+
await page.waitForTimeout(config.waitTimes.pageLoad);
|
34 |
+
|
35 |
+
console.log('当前页面URL:', page.url());
|
36 |
+
console.log('页面标题:', await page.title());
|
37 |
+
|
38 |
+
// 检查是否成功登录
|
39 |
+
try {
|
40 |
+
await page.waitForSelector(config.selectors.editor, {
|
41 |
+
timeout: 60000
|
42 |
+
});
|
43 |
+
console.log('成功进入WebIDE界面');
|
44 |
+
return true;
|
45 |
+
} catch (error) {
|
46 |
+
console.log('警告: 未检测到编辑器界面,可能需要重新登录');
|
47 |
+
return false;
|
48 |
+
}
|
49 |
+
}
|
50 |
+
|
51 |
+
/**
|
52 |
+
* 处理模态对话框
|
53 |
+
* @param {Object} page - Playwright 页面对象
|
54 |
+
*/
|
55 |
+
export async function handleModalDialog(page) {
|
56 |
+
try {
|
57 |
+
const dialogButton = await page.waitForSelector(config.selectors.dialogButton, { timeout: 30000 });
|
58 |
+
if (dialogButton && await dialogButton.isVisible()) {
|
59 |
+
console.log('发现模态对话框按钮,点击处理...');
|
60 |
+
await dialogButton.click();
|
61 |
+
await page.waitForTimeout(500);
|
62 |
+
return true;
|
63 |
+
}
|
64 |
+
} catch (error) {
|
65 |
+
// 没有找到对话框按钮,继续执行
|
66 |
+
console.log('未发现模态对话框,继续执行...');
|
67 |
+
}
|
68 |
+
return false;
|
69 |
+
}
|
70 |
+
|
71 |
+
/**
|
72 |
+
* 打开终端
|
73 |
+
* @param {Object} page - Playwright 页面对象
|
74 |
+
* @returns {Object|null} 终端元素或 null
|
75 |
+
*/
|
76 |
+
export async function openTerminal(page) {
|
77 |
+
console.log('尝试打开终端 (Ctrl+~)...');
|
78 |
+
|
79 |
+
// 确保页面获得焦点
|
80 |
+
await page.click('body');
|
81 |
+
await page.waitForTimeout(500);
|
82 |
+
|
83 |
+
// 按下 Ctrl+~ 打开终端
|
84 |
+
await page.keyboard.press('Control+`');
|
85 |
+
|
86 |
+
// 等待终端打开
|
87 |
+
await page.waitForTimeout(config.waitTimes.terminalOpen);
|
88 |
+
|
89 |
+
// 尝试多种方式查找终端
|
90 |
+
const terminalSelectors = config.selectors.terminals;
|
91 |
+
|
92 |
+
let terminalFound = false;
|
93 |
+
let terminalElement = null;
|
94 |
+
|
95 |
+
for (const selector of terminalSelectors) {
|
96 |
+
try {
|
97 |
+
terminalElement = await page.waitForSelector(selector, { timeout: 2000 });
|
98 |
+
if (terminalElement) {
|
99 |
+
console.log(`找到终端元素: ${selector}`);
|
100 |
+
terminalFound = true;
|
101 |
+
break;
|
102 |
+
}
|
103 |
+
} catch (error) {
|
104 |
+
// 继续尝试下一个选择器
|
105 |
+
}
|
106 |
+
}
|
107 |
+
|
108 |
+
if (!terminalFound) {
|
109 |
+
console.log('未找到终端元素,尝试直接输入命令...');
|
110 |
+
return null;
|
111 |
+
} else {
|
112 |
+
// 点击终端区域确保焦点
|
113 |
+
await terminalElement.click();
|
114 |
+
await page.waitForTimeout(500);
|
115 |
+
return terminalElement;
|
116 |
+
}
|
117 |
+
}
|
118 |
+
|
119 |
+
/**
|
120 |
+
* 在终端中执行命令
|
121 |
+
* @param {Object} page - Playwright 页面对象
|
122 |
+
* @param {string} command - 要执行的命令
|
123 |
+
*/
|
124 |
+
export async function executeTerminalCommand(page, command) {
|
125 |
+
console.log(`执行命令: ${command}`);
|
126 |
+
|
127 |
+
// 输入命令
|
128 |
+
await page.keyboard.type(command);
|
129 |
+
await page.waitForTimeout(500);
|
130 |
+
|
131 |
+
// 按回车执行命令
|
132 |
+
await page.keyboard.press('Enter');
|
133 |
+
|
134 |
+
// 等待命令执行
|
135 |
+
await page.waitForTimeout(config.waitTimes.commandExecution);
|
136 |
+
|
137 |
+
console.log('命令已执行');
|
138 |
+
}
|
139 |
+
|
140 |
+
/**
|
141 |
+
* 完整的命令执行流程
|
142 |
+
* @param {Object} page - Playwright 页面对象
|
143 |
+
* @param {string} screenshotPrefix - 截图文件名前缀
|
144 |
+
* @returns {boolean} 执行是否成功
|
145 |
+
*/
|
146 |
+
export async function executeCommandFlow(page, screenshotPrefix = 'screenshot') {
|
147 |
+
try {
|
148 |
+
// 处理模态对话框
|
149 |
+
await handleModalDialog(page);
|
150 |
+
|
151 |
+
// 打开终端
|
152 |
+
await openTerminal(page);
|
153 |
+
|
154 |
+
// 执行命令
|
155 |
+
await executeTerminalCommand(page, config.command);
|
156 |
+
|
157 |
+
// 截图保存执行结果
|
158 |
+
// const screenshotDir = config.screenshotDir || './screenshots';
|
159 |
+
// const screenshotPath = await saveScreenshot(page, screenshotDir, screenshotPrefix);
|
160 |
+
|
161 |
+
return true;
|
162 |
+
} catch (error) {
|
163 |
+
console.error(`[${getHumanReadableTimestamp()}] 执行命令时发生错误:`, error);
|
164 |
+
return false;
|
165 |
+
}
|
166 |
+
}
|
hf-repo/hf-repo/hf-repo/web-server.js
ADDED
@@ -0,0 +1,335 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { Hono } from 'hono';
|
2 |
+
import { serve } from '@hono/node-server';
|
3 |
+
import { getRecentLogs, log, logError } from './utils/common-utils.js';
|
4 |
+
import config from './config.js';
|
5 |
+
|
6 |
+
const app = new Hono();
|
7 |
+
|
8 |
+
// 静态 HTML 页面
|
9 |
+
const indexHTML = `
|
10 |
+
<!DOCTYPE html>
|
11 |
+
<html lang="zh-CN">
|
12 |
+
<head>
|
13 |
+
<meta charset="UTF-8">
|
14 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
15 |
+
<title>CloudStudio Runner - 日志查看器</title>
|
16 |
+
<style>
|
17 |
+
body {
|
18 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
19 |
+
margin: 0;
|
20 |
+
padding: 20px;
|
21 |
+
background-color: #f5f5f5;
|
22 |
+
color: #333;
|
23 |
+
}
|
24 |
+
.container {
|
25 |
+
max-width: 1200px;
|
26 |
+
margin: 0 auto;
|
27 |
+
background: white;
|
28 |
+
border-radius: 8px;
|
29 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
30 |
+
overflow: hidden;
|
31 |
+
}
|
32 |
+
.header {
|
33 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
34 |
+
color: white;
|
35 |
+
padding: 20px;
|
36 |
+
text-align: center;
|
37 |
+
}
|
38 |
+
.header h1 {
|
39 |
+
margin: 0;
|
40 |
+
font-size: 2em;
|
41 |
+
}
|
42 |
+
.header p {
|
43 |
+
margin: 10px 0 0 0;
|
44 |
+
opacity: 0.9;
|
45 |
+
}
|
46 |
+
.controls {
|
47 |
+
padding: 20px;
|
48 |
+
border-bottom: 1px solid #eee;
|
49 |
+
display: flex;
|
50 |
+
gap: 10px;
|
51 |
+
align-items: center;
|
52 |
+
flex-wrap: wrap;
|
53 |
+
}
|
54 |
+
.controls label {
|
55 |
+
font-weight: bold;
|
56 |
+
}
|
57 |
+
.controls select, .controls button {
|
58 |
+
padding: 8px 12px;
|
59 |
+
border: 1px solid #ddd;
|
60 |
+
border-radius: 4px;
|
61 |
+
font-size: 14px;
|
62 |
+
}
|
63 |
+
.controls button {
|
64 |
+
background: #667eea;
|
65 |
+
color: white;
|
66 |
+
border: none;
|
67 |
+
cursor: pointer;
|
68 |
+
transition: background 0.3s;
|
69 |
+
}
|
70 |
+
.controls button:hover {
|
71 |
+
background: #5a6fd8;
|
72 |
+
}
|
73 |
+
.log-container {
|
74 |
+
padding: 20px;
|
75 |
+
max-height: 600px;
|
76 |
+
overflow-y: auto;
|
77 |
+
}
|
78 |
+
.log-entry {
|
79 |
+
margin-bottom: 8px;
|
80 |
+
padding: 8px;
|
81 |
+
border-radius: 4px;
|
82 |
+
font-family: 'Courier New', monospace;
|
83 |
+
font-size: 13px;
|
84 |
+
line-height: 1.4;
|
85 |
+
border-left: 3px solid #ddd;
|
86 |
+
}
|
87 |
+
.log-entry.info {
|
88 |
+
background-color: #f8f9fa;
|
89 |
+
border-left-color: #28a745;
|
90 |
+
}
|
91 |
+
.log-entry.error {
|
92 |
+
background-color: #fff5f5;
|
93 |
+
border-left-color: #dc3545;
|
94 |
+
color: #721c24;
|
95 |
+
}
|
96 |
+
.log-entry.warn {
|
97 |
+
background-color: #fffbf0;
|
98 |
+
border-left-color: #ffc107;
|
99 |
+
color: #856404;
|
100 |
+
}
|
101 |
+
.timestamp {
|
102 |
+
color: #6c757d;
|
103 |
+
font-weight: bold;
|
104 |
+
}
|
105 |
+
.level {
|
106 |
+
font-weight: bold;
|
107 |
+
margin: 0 8px;
|
108 |
+
}
|
109 |
+
.level.info { color: #28a745; }
|
110 |
+
.level.error { color: #dc3545; }
|
111 |
+
.level.warn { color: #ffc107; }
|
112 |
+
.message {
|
113 |
+
word-break: break-word;
|
114 |
+
}
|
115 |
+
.status {
|
116 |
+
padding: 10px 20px;
|
117 |
+
background: #e9ecef;
|
118 |
+
border-top: 1px solid #ddd;
|
119 |
+
font-size: 14px;
|
120 |
+
color: #6c757d;
|
121 |
+
}
|
122 |
+
.loading {
|
123 |
+
text-align: center;
|
124 |
+
padding: 40px;
|
125 |
+
color: #6c757d;
|
126 |
+
}
|
127 |
+
.empty {
|
128 |
+
text-align: center;
|
129 |
+
padding: 40px;
|
130 |
+
color: #6c757d;
|
131 |
+
font-style: italic;
|
132 |
+
}
|
133 |
+
</style>
|
134 |
+
</head>
|
135 |
+
<body>
|
136 |
+
<div class="container">
|
137 |
+
<div class="header">
|
138 |
+
<h1>🚀 CloudStudio Runner</h1>
|
139 |
+
<p>实时日志查看器 - 监控自动化任务执行状态</p>
|
140 |
+
</div>
|
141 |
+
|
142 |
+
<div class="controls">
|
143 |
+
<label for="lines">显示行数:</label>
|
144 |
+
<select id="lines">
|
145 |
+
<option value="50">50 行</option>
|
146 |
+
<option value="100" selected>100 行</option>
|
147 |
+
<option value="200">200 行</option>
|
148 |
+
<option value="500">500 行</option>
|
149 |
+
<option value="1000">1000 行</option>
|
150 |
+
</select>
|
151 |
+
|
152 |
+
<button onclick="refreshLogs()">🔄 刷新日志</button>
|
153 |
+
<button onclick="toggleAutoRefresh()">⏱️ 自动刷新</button>
|
154 |
+
<button onclick="clearLogs()">🗑️ 清空显示</button>
|
155 |
+
</div>
|
156 |
+
|
157 |
+
<div class="log-container" id="logContainer">
|
158 |
+
<div class="loading">正在加载日志...</div>
|
159 |
+
</div>
|
160 |
+
|
161 |
+
<div class="status" id="status">
|
162 |
+
准备就绪
|
163 |
+
</div>
|
164 |
+
</div>
|
165 |
+
|
166 |
+
<script>
|
167 |
+
let autoRefreshInterval = null;
|
168 |
+
let isAutoRefresh = false;
|
169 |
+
|
170 |
+
async function fetchLogs() {
|
171 |
+
try {
|
172 |
+
const lines = document.getElementById('lines').value;
|
173 |
+
const response = await fetch(\`/api/logs?lines=\${lines}\`);
|
174 |
+
const data = await response.json();
|
175 |
+
|
176 |
+
if (data.success) {
|
177 |
+
displayLogs(data.logs);
|
178 |
+
updateStatus(\`已加载 \${data.logs.length} 条日志 - \${new Date().toLocaleString()}\`);
|
179 |
+
} else {
|
180 |
+
updateStatus('获取日志失败: ' + data.error);
|
181 |
+
}
|
182 |
+
} catch (error) {
|
183 |
+
updateStatus('网络错误: ' + error.message);
|
184 |
+
}
|
185 |
+
}
|
186 |
+
|
187 |
+
function displayLogs(logs) {
|
188 |
+
const container = document.getElementById('logContainer');
|
189 |
+
|
190 |
+
if (logs.length === 0) {
|
191 |
+
container.innerHTML = '<div class="empty">暂无日志数据</div>';
|
192 |
+
return;
|
193 |
+
}
|
194 |
+
|
195 |
+
const logHTML = logs.map(log => {
|
196 |
+
const match = log.match(/\\[(.*?)\\]\\s*\\[(.*?)\\]\\s*(.*)/);
|
197 |
+
if (match) {
|
198 |
+
const [, timestamp, level, message] = match;
|
199 |
+
const levelClass = level.toLowerCase();
|
200 |
+
return \`
|
201 |
+
<div class="log-entry \${levelClass}">
|
202 |
+
<span class="timestamp">\${timestamp}</span>
|
203 |
+
<span class="level \${levelClass}">[\${level}]</span>
|
204 |
+
<span class="message">\${message}</span>
|
205 |
+
</div>
|
206 |
+
\`;
|
207 |
+
} else {
|
208 |
+
return \`
|
209 |
+
<div class="log-entry">
|
210 |
+
<span class="message">\${log}</span>
|
211 |
+
</div>
|
212 |
+
\`;
|
213 |
+
}
|
214 |
+
}).join('');
|
215 |
+
|
216 |
+
container.innerHTML = logHTML;
|
217 |
+
container.scrollTop = container.scrollHeight;
|
218 |
+
}
|
219 |
+
|
220 |
+
function updateStatus(message) {
|
221 |
+
document.getElementById('status').textContent = message;
|
222 |
+
}
|
223 |
+
|
224 |
+
function refreshLogs() {
|
225 |
+
fetchLogs();
|
226 |
+
}
|
227 |
+
|
228 |
+
function toggleAutoRefresh() {
|
229 |
+
const button = event.target;
|
230 |
+
|
231 |
+
if (isAutoRefresh) {
|
232 |
+
clearInterval(autoRefreshInterval);
|
233 |
+
autoRefreshInterval = null;
|
234 |
+
isAutoRefresh = false;
|
235 |
+
button.textContent = '⏱️ 自动刷新';
|
236 |
+
updateStatus('自动刷新已停止');
|
237 |
+
} else {
|
238 |
+
autoRefreshInterval = setInterval(fetchLogs, 5000);
|
239 |
+
isAutoRefresh = true;
|
240 |
+
button.textContent = '⏹️ 停止刷新';
|
241 |
+
updateStatus('自动刷新已启动 (每5秒)');
|
242 |
+
}
|
243 |
+
}
|
244 |
+
|
245 |
+
function clearLogs() {
|
246 |
+
document.getElementById('logContainer').innerHTML = '<div class="empty">日志显示已清空</div>';
|
247 |
+
updateStatus('显示已清空');
|
248 |
+
}
|
249 |
+
|
250 |
+
// 页面加载时获取日志
|
251 |
+
document.addEventListener('DOMContentLoaded', fetchLogs);
|
252 |
+
</script>
|
253 |
+
</body>
|
254 |
+
</html>
|
255 |
+
`;
|
256 |
+
|
257 |
+
// 路由定义
|
258 |
+
app.get('/', (c) => {
|
259 |
+
return c.html(indexHTML);
|
260 |
+
});
|
261 |
+
|
262 |
+
// API 路由 - 获取日志
|
263 |
+
app.get('/api/logs', (c) => {
|
264 |
+
try {
|
265 |
+
const lines = parseInt(c.req.query('lines') || '100');
|
266 |
+
const logs = getRecentLogs(lines);
|
267 |
+
|
268 |
+
return c.json({
|
269 |
+
success: true,
|
270 |
+
logs: logs,
|
271 |
+
count: logs.length,
|
272 |
+
timestamp: new Date().toISOString()
|
273 |
+
});
|
274 |
+
} catch (error) {
|
275 |
+
logError('获取日志API错误:', error);
|
276 |
+
return c.json({
|
277 |
+
success: false,
|
278 |
+
error: error.message
|
279 |
+
}, 500);
|
280 |
+
}
|
281 |
+
});
|
282 |
+
|
283 |
+
// API 路由 - 系统状态
|
284 |
+
app.get('/api/status', (c) => {
|
285 |
+
return c.json({
|
286 |
+
success: true,
|
287 |
+
status: 'running',
|
288 |
+
uptime: process.uptime(),
|
289 |
+
memory: process.memoryUsage(),
|
290 |
+
timestamp: new Date().toISOString(),
|
291 |
+
config: {
|
292 |
+
webideUrl: config.webideUrl,
|
293 |
+
schedulerInterval: config.schedulerInterval,
|
294 |
+
headless: config.browserOptions.headless
|
295 |
+
}
|
296 |
+
});
|
297 |
+
});
|
298 |
+
|
299 |
+
// 健康检查
|
300 |
+
app.get('/health', (c) => {
|
301 |
+
return c.json({ status: 'ok', timestamp: new Date().toISOString() });
|
302 |
+
});
|
303 |
+
|
304 |
+
// 启动服务器
|
305 |
+
const port = process.env.PORT || 7860;
|
306 |
+
|
307 |
+
async function startServer() {
|
308 |
+
try {
|
309 |
+
log(`启动 Web 服务器,端口: ${port}`);
|
310 |
+
log(`访问地址: http://localhost:${port}`);
|
311 |
+
|
312 |
+
serve({
|
313 |
+
fetch: app.fetch,
|
314 |
+
port: port,
|
315 |
+
});
|
316 |
+
|
317 |
+
log('Web 服务器启动成功');
|
318 |
+
} catch (error) {
|
319 |
+
logError('启动 Web 服务器失败:', error);
|
320 |
+
process.exit(1);
|
321 |
+
}
|
322 |
+
}
|
323 |
+
|
324 |
+
// 如果直接运行此文件,启动服务器
|
325 |
+
import { fileURLToPath } from 'url';
|
326 |
+
import path from 'path';
|
327 |
+
|
328 |
+
const __filename = fileURLToPath(import.meta.url);
|
329 |
+
const scriptPath = path.resolve(process.argv[1]);
|
330 |
+
|
331 |
+
if (path.resolve(__filename) === scriptPath) {
|
332 |
+
startServer();
|
333 |
+
}
|
334 |
+
|
335 |
+
export { app, startServer };
|
hf-repo/hf-repo/src/config.js
CHANGED
@@ -6,7 +6,7 @@ dotenv.config();
|
|
6 |
|
7 |
const config = {
|
8 |
// WebIDE URL - 可以通过环境变量 WEBIDE_URL 覆盖
|
9 |
-
webideUrl: process.env.WEBIDE_URL || '
|
10 |
|
11 |
// 调度器时间间隔(毫秒)- 可以通过环境变量 SCHEDULER_INTERVAL 覆盖
|
12 |
// 默认为 10 分钟 (10 * 60 * 1000 = 600000 毫秒)
|
@@ -15,6 +15,9 @@ const config = {
|
|
15 |
// Cookie文件路径
|
16 |
cookieFile: './cookies.json',
|
17 |
|
|
|
|
|
|
|
18 |
// 浏览器配置
|
19 |
browserOptions: {
|
20 |
// 默认无头模式,可通过环境变量 HEADLESS=false 设置为有头模式
|
|
|
6 |
|
7 |
const config = {
|
8 |
// WebIDE URL - 可以通过环境变量 WEBIDE_URL 覆盖
|
9 |
+
webideUrl: process.env.WEBIDE_URL || '',
|
10 |
|
11 |
// 调度器时间间隔(毫秒)- 可以通过环境变量 SCHEDULER_INTERVAL 覆盖
|
12 |
// 默认为 10 分钟 (10 * 60 * 1000 = 600000 毫秒)
|
|
|
15 |
// Cookie文件路径
|
16 |
cookieFile: './cookies.json',
|
17 |
|
18 |
+
// Cookie环境变量 - 如果设置了此环境变量,将优先使用环境变量中的cookies
|
19 |
+
cookiesFromEnv: process.env.COOKIES,
|
20 |
+
|
21 |
// 浏览器配置
|
22 |
browserOptions: {
|
23 |
// 默认无头模式,可通过环境变量 HEADLESS=false 设置为有头模式
|
hf-repo/hf-repo/src/execute-command.js
CHANGED
@@ -1,19 +1,19 @@
|
|
1 |
import config from './config.js';
|
2 |
import { fileURLToPath } from 'url';
|
3 |
import path from 'path';
|
4 |
-
import {
|
5 |
import { createBrowserSession, navigateToWebIDE, executeCommandFlow } from './utils/webide-utils.js';
|
6 |
|
7 |
async function executeCommand() {
|
8 |
-
// 检查cookie
|
9 |
-
if (!
|
10 |
return;
|
11 |
}
|
12 |
|
13 |
let browser;
|
14 |
try {
|
15 |
// 创建浏览器会话
|
16 |
-
const { browser: browserInstance, page } = await createBrowserSession(config.cookieFile);
|
17 |
browser = browserInstance;
|
18 |
|
19 |
// 导航到WebIDE页面
|
|
|
1 |
import config from './config.js';
|
2 |
import { fileURLToPath } from 'url';
|
3 |
import path from 'path';
|
4 |
+
import { checkCookieAvailability } from './utils/common-utils.js';
|
5 |
import { createBrowserSession, navigateToWebIDE, executeCommandFlow } from './utils/webide-utils.js';
|
6 |
|
7 |
async function executeCommand() {
|
8 |
+
// 检查cookie是否可用(环境变量或文件)
|
9 |
+
if (!checkCookieAvailability(config.cookieFile, config.cookiesFromEnv)) {
|
10 |
return;
|
11 |
}
|
12 |
|
13 |
let browser;
|
14 |
try {
|
15 |
// 创建浏览器会话
|
16 |
+
const { browser: browserInstance, page } = await createBrowserSession(config.cookieFile, config.cookiesFromEnv);
|
17 |
browser = browserInstance;
|
18 |
|
19 |
// 导航到WebIDE页面
|
hf-repo/hf-repo/src/scheduler.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import config from './config.js';
|
2 |
import { fileURLToPath } from 'url';
|
3 |
import path from 'path';
|
4 |
-
import {
|
5 |
import { createBrowserSession, navigateToWebIDE, executeCommandFlow } from './utils/webide-utils.js';
|
6 |
|
7 |
// 执行单次命令的函数
|
@@ -12,8 +12,8 @@ async function executeCommandOnce(page) {
|
|
12 |
|
13 |
// 主调度器函数
|
14 |
async function startScheduler() {
|
15 |
-
// 检查cookie
|
16 |
-
if (!
|
17 |
return;
|
18 |
}
|
19 |
|
@@ -24,7 +24,7 @@ async function startScheduler() {
|
|
24 |
let browser;
|
25 |
try {
|
26 |
// 创建浏览器会话
|
27 |
-
const { browser: browserInstance, page } = await createBrowserSession(config.cookieFile);
|
28 |
browser = browserInstance;
|
29 |
|
30 |
// 导航到WebIDE页面
|
|
|
1 |
import config from './config.js';
|
2 |
import { fileURLToPath } from 'url';
|
3 |
import path from 'path';
|
4 |
+
import { checkCookieAvailability, getHumanReadableTimestamp, log, logError } from './utils/common-utils.js';
|
5 |
import { createBrowserSession, navigateToWebIDE, executeCommandFlow } from './utils/webide-utils.js';
|
6 |
|
7 |
// 执行单次命令的函数
|
|
|
12 |
|
13 |
// 主调度器函数
|
14 |
async function startScheduler() {
|
15 |
+
// 检查cookie是否可用(环境变量或文件)
|
16 |
+
if (!checkCookieAvailability(config.cookieFile, config.cookiesFromEnv)) {
|
17 |
return;
|
18 |
}
|
19 |
|
|
|
24 |
let browser;
|
25 |
try {
|
26 |
// 创建浏览器会话
|
27 |
+
const { browser: browserInstance, page } = await createBrowserSession(config.cookieFile, config.cookiesFromEnv);
|
28 |
browser = browserInstance;
|
29 |
|
30 |
// 导航到WebIDE页面
|
hf-repo/hf-repo/src/utils/common-utils.js
CHANGED
@@ -123,7 +123,35 @@ export function ensureScreenshotDirectory(dir) {
|
|
123 |
}
|
124 |
|
125 |
/**
|
126 |
-
* 检查 Cookie
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
* @param {string} cookieFile - Cookie 文件路径
|
128 |
* @returns {boolean} 文件是否存在
|
129 |
*/
|
@@ -137,17 +165,28 @@ export function checkCookieFile(cookieFile) {
|
|
137 |
}
|
138 |
|
139 |
/**
|
140 |
-
* 读取并解析 Cookie
|
141 |
* @param {string} cookieFile - Cookie 文件路径
|
|
|
142 |
* @returns {Array} Cookie 数组
|
143 |
*/
|
144 |
-
export function loadCookies(cookieFile) {
|
145 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
const cookies = JSON.parse(fs.readFileSync(cookieFile, 'utf8'));
|
147 |
-
console.log(
|
148 |
return cookies;
|
149 |
} catch (error) {
|
150 |
-
console.error('读取 Cookie
|
151 |
throw error;
|
152 |
}
|
153 |
}
|
|
|
123 |
}
|
124 |
|
125 |
/**
|
126 |
+
* 检查 Cookie 是否可用(环境变量或文件)
|
127 |
+
* @param {string} cookieFile - Cookie 文件路径
|
128 |
+
* @param {string} cookiesFromEnv - 环境变量中的cookies
|
129 |
+
* @returns {boolean} Cookie是否可用
|
130 |
+
*/
|
131 |
+
export function checkCookieAvailability(cookieFile, cookiesFromEnv) {
|
132 |
+
// 优先检查环境变量
|
133 |
+
if (cookiesFromEnv) {
|
134 |
+
console.log('发现环境变量COOKIES,将使用环境变量中的cookies');
|
135 |
+
try {
|
136 |
+
JSON.parse(cookiesFromEnv);
|
137 |
+
return true;
|
138 |
+
} catch (error) {
|
139 |
+
console.error('环境变量COOKIES格式无效:', error.message);
|
140 |
+
console.log('将尝试使用cookie文件...');
|
141 |
+
}
|
142 |
+
}
|
143 |
+
|
144 |
+
// 检查cookie文件
|
145 |
+
if (!fs.existsSync(cookieFile)) {
|
146 |
+
console.error(`Cookie文件不存在: ${cookieFile}`);
|
147 |
+
console.log('请先运行 npm run login 进行登录,或设置环境变量COOKIES');
|
148 |
+
return false;
|
149 |
+
}
|
150 |
+
return true;
|
151 |
+
}
|
152 |
+
|
153 |
+
/**
|
154 |
+
* 检查 Cookie 文件是否存在(保持向后兼容)
|
155 |
* @param {string} cookieFile - Cookie 文件路径
|
156 |
* @returns {boolean} 文件是否存在
|
157 |
*/
|
|
|
165 |
}
|
166 |
|
167 |
/**
|
168 |
+
* 读取并解析 Cookie(优先使用环境变量)
|
169 |
* @param {string} cookieFile - Cookie 文件路径
|
170 |
+
* @param {string} cookiesFromEnv - 环境变量中的cookies
|
171 |
* @returns {Array} Cookie 数组
|
172 |
*/
|
173 |
+
export function loadCookies(cookieFile, cookiesFromEnv) {
|
174 |
try {
|
175 |
+
// 优先使用环境变量
|
176 |
+
if (cookiesFromEnv) {
|
177 |
+
console.log('从环境变量COOKIES加载cookies...');
|
178 |
+
const cookies = JSON.parse(cookiesFromEnv);
|
179 |
+
console.log(`已从环境变量加载 ${cookies.length} 个cookies`);
|
180 |
+
return cookies;
|
181 |
+
}
|
182 |
+
|
183 |
+
// 使用文件
|
184 |
+
console.log(`从文件加载cookies: ${cookieFile}`);
|
185 |
const cookies = JSON.parse(fs.readFileSync(cookieFile, 'utf8'));
|
186 |
+
console.log(`已从文件加载 ${cookies.length} 个cookies`);
|
187 |
return cookies;
|
188 |
} catch (error) {
|
189 |
+
console.error('读取 Cookie 失败:', error);
|
190 |
throw error;
|
191 |
}
|
192 |
}
|
hf-repo/hf-repo/src/utils/webide-utils.js
CHANGED
@@ -5,15 +5,16 @@ import { loadCookies, saveScreenshot, getHumanReadableTimestamp } from './common
|
|
5 |
/**
|
6 |
* 创建浏览器实例和上下文
|
7 |
* @param {string} cookieFile - Cookie 文件路径
|
|
|
8 |
* @returns {Object} { browser, context, page }
|
9 |
*/
|
10 |
-
export async function createBrowserSession(cookieFile) {
|
11 |
console.log('启动浏览器...');
|
12 |
const browser = await chromium.launch(config.browserOptions);
|
13 |
const context = await browser.newContext();
|
14 |
|
15 |
-
// 读取并设置cookies
|
16 |
-
const cookies = loadCookies(cookieFile);
|
17 |
await context.addCookies(cookies);
|
18 |
|
19 |
const page = await context.newPage();
|
|
|
5 |
/**
|
6 |
* 创建浏览器实例和上下文
|
7 |
* @param {string} cookieFile - Cookie 文件路径
|
8 |
+
* @param {string} cookiesFromEnv - 环境变量中的cookies
|
9 |
* @returns {Object} { browser, context, page }
|
10 |
*/
|
11 |
+
export async function createBrowserSession(cookieFile, cookiesFromEnv) {
|
12 |
console.log('启动浏览器...');
|
13 |
const browser = await chromium.launch(config.browserOptions);
|
14 |
const context = await browser.newContext();
|
15 |
|
16 |
+
// 读取并设置cookies(优先使用环境变量)
|
17 |
+
const cookies = loadCookies(cookieFile, cookiesFromEnv);
|
18 |
await context.addCookies(cookies);
|
19 |
|
20 |
const page = await context.newPage();
|
hf-repo/src/execute-command.js
CHANGED
@@ -1,14 +1,10 @@
|
|
1 |
import config from './config.js';
|
2 |
import { fileURLToPath } from 'url';
|
3 |
import path from 'path';
|
4 |
-
|
5 |
import { createBrowserSession, navigateToWebIDE, executeCommandFlow } from './utils/webide-utils.js';
|
6 |
|
7 |
async function executeCommand() {
|
8 |
-
// 检查cookie是否可用(环境变量或文件)
|
9 |
-
if (!checkCookieAvailability(config.cookieFile, config.cookiesFromEnv)) {
|
10 |
-
return;
|
11 |
-
}
|
12 |
|
13 |
let browser;
|
14 |
try {
|
|
|
1 |
import config from './config.js';
|
2 |
import { fileURLToPath } from 'url';
|
3 |
import path from 'path';
|
4 |
+
|
5 |
import { createBrowserSession, navigateToWebIDE, executeCommandFlow } from './utils/webide-utils.js';
|
6 |
|
7 |
async function executeCommand() {
|
|
|
|
|
|
|
|
|
8 |
|
9 |
let browser;
|
10 |
try {
|
hf-repo/src/login.js
CHANGED
@@ -9,11 +9,8 @@ async function login() {
|
|
9 |
const browser = await chromium.launch(config.browserOptions);
|
10 |
const context = await browser.newContext();
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
const cookies = loadCookies(config.cookieFile);
|
15 |
-
await context.addCookies(cookies);
|
16 |
-
}
|
17 |
|
18 |
const page = await context.newPage();
|
19 |
|
|
|
9 |
const browser = await chromium.launch(config.browserOptions);
|
10 |
const context = await browser.newContext();
|
11 |
|
12 |
+
const cookies = loadCookies(config.cookieFile, config.cookiesFromEnv);
|
13 |
+
await context.addCookies(cookies);
|
|
|
|
|
|
|
14 |
|
15 |
const page = await context.newPage();
|
16 |
|
hf-repo/src/scheduler.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import config from './config.js';
|
2 |
import { fileURLToPath } from 'url';
|
3 |
import path from 'path';
|
4 |
-
import {
|
5 |
import { createBrowserSession, navigateToWebIDE, executeCommandFlow } from './utils/webide-utils.js';
|
6 |
|
7 |
// 执行单次命令的函数
|
@@ -12,10 +12,6 @@ async function executeCommandOnce(page) {
|
|
12 |
|
13 |
// 主调度器函数
|
14 |
async function startScheduler() {
|
15 |
-
// 检查cookie是否可用(环境变量或文件)
|
16 |
-
if (!checkCookieAvailability(config.cookieFile, config.cookiesFromEnv)) {
|
17 |
-
return;
|
18 |
-
}
|
19 |
|
20 |
log(`[${getHumanReadableTimestamp()}] 启动调度器...`);
|
21 |
const intervalSeconds = Math.round(config.schedulerInterval / 1000);
|
|
|
1 |
import config from './config.js';
|
2 |
import { fileURLToPath } from 'url';
|
3 |
import path from 'path';
|
4 |
+
import { getHumanReadableTimestamp, log, logError } from './utils/common-utils.js';
|
5 |
import { createBrowserSession, navigateToWebIDE, executeCommandFlow } from './utils/webide-utils.js';
|
6 |
|
7 |
// 执行单次命令的函数
|
|
|
12 |
|
13 |
// 主调度器函数
|
14 |
async function startScheduler() {
|
|
|
|
|
|
|
|
|
15 |
|
16 |
log(`[${getHumanReadableTimestamp()}] 启动调度器...`);
|
17 |
const intervalSeconds = Math.round(config.schedulerInterval / 1000);
|
hf-repo/src/utils/common-utils.js
CHANGED
@@ -181,10 +181,13 @@ export function loadCookies(cookieFile, cookiesFromEnv) {
|
|
181 |
}
|
182 |
|
183 |
// 使用文件
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
|
|
|
|
|
|
188 |
} catch (error) {
|
189 |
console.error('读取 Cookie 失败:', error);
|
190 |
throw error;
|
|
|
181 |
}
|
182 |
|
183 |
// 使用文件
|
184 |
+
if (fs.existsSync(cookieFile)) {
|
185 |
+
console.log(`从文件加载cookies: ${cookieFile}`);
|
186 |
+
const cookies = JSON.parse(fs.readFileSync(cookieFile, 'utf8'));
|
187 |
+
console.log(`已从文件加载 ${cookies.length} 个cookies`);
|
188 |
+
return cookies;
|
189 |
+
}
|
190 |
+
return [];
|
191 |
} catch (error) {
|
192 |
console.error('读取 Cookie 失败:', error);
|
193 |
throw error;
|
src/login.js
CHANGED
@@ -40,6 +40,7 @@ async function login() {
|
|
40 |
// 等待登录完成,检查是否出现编辑器界面
|
41 |
console.log('等待登录完成...');
|
42 |
try {
|
|
|
43 |
await page.waitForSelector(config.selectors.editor, {
|
44 |
timeout: 60000
|
45 |
});
|
|
|
40 |
// 等待登录完成,检查是否出现编辑器界面
|
41 |
console.log('等待登录完成...');
|
42 |
try {
|
43 |
+
await page.goto(config.webideUrl);
|
44 |
await page.waitForSelector(config.selectors.editor, {
|
45 |
timeout: 60000
|
46 |
});
|