@h-ai/tools 0.0.0-stage → 1.0.0-beta.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.en.md +37 -0
- package/README.ja.md +37 -0
- package/README.md +36 -2
- package/dist/chunk-UA7MOYTT.js +413 -0
- package/dist/chunk-UA7MOYTT.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/server/index.d.ts +306 -0
- package/dist/server/index.js +1051 -0
- package/dist/server/index.js.map +1 -0
- package/dist/tools-types-CM0lGuJr.d.ts +199 -0
- package/jobs/.dockerignore +4 -0
- package/jobs/README.md +223 -0
- package/jobs/archives.py +56 -0
- package/jobs/browser/Dockerfile +20 -0
- package/jobs/browser/operation.py +17 -0
- package/jobs/browser/render.mjs +90 -0
- package/jobs/browser_policy.py +36 -0
- package/jobs/documents/Dockerfile +23 -0
- package/jobs/documents/generate.py +165 -0
- package/jobs/documents/operation.py +84 -0
- package/jobs/documents/requirements.txt +3 -0
- package/jobs/hyperframes-runtime.mjs +73 -0
- package/jobs/media_files.py +78 -0
- package/jobs/ocr/Dockerfile +19 -0
- package/jobs/ocr/operation.py +18 -0
- package/jobs/office/Dockerfile +21 -0
- package/jobs/office/office_job.py +65 -0
- package/jobs/office/operation.py +43 -0
- package/jobs/preview_build/Dockerfile +19 -0
- package/jobs/preview_build/build.mjs +552 -0
- package/jobs/preview_build/operation.py +62 -0
- package/jobs/preview_build/package.json +11 -0
- package/jobs/tests/image-smoke.mjs +83 -0
- package/jobs/tests/requirements.txt +4 -0
- package/jobs/tests/test_documents_generate.py +107 -0
- package/jobs/tests/test_hai_tool.py +82 -0
- package/jobs/tests/test_media_jobs.py +449 -0
- package/jobs/tests/test_preview_build.py +213 -0
- package/jobs/tool.py +145 -0
- package/jobs/video_render/Dockerfile +20 -0
- package/jobs/video_render/operation.py +109 -0
- package/jobs/video_render/render.mjs +36 -0
- package/jobs/web_capture/Dockerfile +20 -0
- package/jobs/web_capture/capture.mjs +122 -0
- package/jobs/web_capture/operation.py +65 -0
- package/package.json +62 -3
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import xml.etree.ElementTree as ET
|
|
2
|
+
import zipfile
|
|
3
|
+
from archives import OfficeArchive, office_archive_error
|
|
4
|
+
from documents.generate import generate
|
|
5
|
+
from tool import TEXT_LIMIT, ToolError, read_text, run_command, text_result
|
|
6
|
+
|
|
7
|
+
WORD = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
|
|
8
|
+
SHEET = '{http://schemas.openxmlformats.org/spreadsheetml/2006/main}'
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def collect_lines(lines):
|
|
12
|
+
result = []
|
|
13
|
+
size = 0
|
|
14
|
+
for line in lines:
|
|
15
|
+
size += len(line.encode('utf-8')) + 1
|
|
16
|
+
if size > TEXT_LIMIT:
|
|
17
|
+
raise ToolError('OUTPUT_LIMIT', 'Extracted text exceeds the size limit')
|
|
18
|
+
result.append(line)
|
|
19
|
+
return text_result('\n'.join(result))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def word_lines(document):
|
|
23
|
+
for paragraph in document.xml('word/document.xml').iter(WORD + 'p'):
|
|
24
|
+
yield ''.join(value.text or '' for value in paragraph.iter(WORD + 't'))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def sheet_lines(document):
|
|
28
|
+
names = document.archive.namelist()
|
|
29
|
+
strings = []
|
|
30
|
+
if 'xl/sharedStrings.xml' in names:
|
|
31
|
+
for item in document.xml('xl/sharedStrings.xml').iter(SHEET + 'si'):
|
|
32
|
+
strings.append(''.join(value.text or '' for value in item.iter(SHEET + 't')))
|
|
33
|
+
worksheets = sorted(name for name in names if name.startswith('xl/worksheets/') and name.rsplit('/', 1)[1].startswith('sheet') and name.endswith('.xml'))
|
|
34
|
+
if not worksheets:
|
|
35
|
+
raise office_archive_error()
|
|
36
|
+
for name in worksheets:
|
|
37
|
+
yield name.rsplit('/', 1)[1]
|
|
38
|
+
for row in document.xml(name).iter(SHEET + 'row'):
|
|
39
|
+
values = []
|
|
40
|
+
for cell in row.findall(SHEET + 'c'):
|
|
41
|
+
value = cell.findtext(SHEET + 'v') or ''
|
|
42
|
+
cell_type = cell.get('t')
|
|
43
|
+
if cell_type == 's':
|
|
44
|
+
index = int(value)
|
|
45
|
+
if index < 0 or index >= len(strings):
|
|
46
|
+
raise office_archive_error()
|
|
47
|
+
value = strings[index]
|
|
48
|
+
elif cell_type == 'inlineStr':
|
|
49
|
+
value = ''.join(item.text or '' for item in cell.iter(SHEET + 't'))
|
|
50
|
+
values.append(value)
|
|
51
|
+
yield '\t'.join(values)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def extract(_request, source, _artifact, workspace):
|
|
55
|
+
suffix = source.suffix.lower()
|
|
56
|
+
if suffix in ('.txt', '.md', '.csv', '.tsv'):
|
|
57
|
+
try:
|
|
58
|
+
return read_text(source)
|
|
59
|
+
except UnicodeDecodeError:
|
|
60
|
+
raise ToolError('INVALID_INPUT', 'Expected UTF-8 document text') from None
|
|
61
|
+
if suffix == '.pdf':
|
|
62
|
+
with source.open('rb') as stream:
|
|
63
|
+
if not stream.read(5) == b'%PDF-':
|
|
64
|
+
raise ToolError('INVALID_INPUT', 'Invalid PDF document')
|
|
65
|
+
result = workspace / 'extracted.txt'
|
|
66
|
+
# 额外读取一页检测超限,避免把较长文档报告为完整输出。
|
|
67
|
+
run_command(['pdftotext', '-l', '101', '-enc', 'UTF-8', str(source), str(result)], 30, workspace)
|
|
68
|
+
extracted = read_text(result)
|
|
69
|
+
if extracted['text'].count('\f') > 100:
|
|
70
|
+
raise ToolError('OUTPUT_LIMIT', 'PDF exceeds the page limit')
|
|
71
|
+
return extracted
|
|
72
|
+
if suffix in ('.docx', '.xlsx'):
|
|
73
|
+
try:
|
|
74
|
+
with OfficeArchive(source) as document:
|
|
75
|
+
return collect_lines(word_lines(document) if suffix == '.docx' else sheet_lines(document))
|
|
76
|
+
except (zipfile.BadZipFile, KeyError, ValueError, ET.ParseError):
|
|
77
|
+
raise office_archive_error() from None
|
|
78
|
+
raise ToolError('INVALID_INPUT', 'Unsupported document format')
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
commands = {
|
|
82
|
+
'documents.extract': ({'input'}, set(), extract),
|
|
83
|
+
'documents.generate': ({'input', 'output'}, set(), generate),
|
|
84
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process'
|
|
2
|
+
import { readFile } from 'node:fs/promises'
|
|
3
|
+
import { createRequire } from 'node:module'
|
|
4
|
+
import { dirname, join } from 'node:path'
|
|
5
|
+
import process from 'node:process'
|
|
6
|
+
import { promisify } from 'node:util'
|
|
7
|
+
|
|
8
|
+
const execute = promisify(execFile)
|
|
9
|
+
const require = createRequire(import.meta.url)
|
|
10
|
+
|
|
11
|
+
export async function runHyperframes(args, directory, extra = {}, timeout = 280_000) {
|
|
12
|
+
// 从镜像自身解析 CLI,不执行输入项目中的命令或依赖。
|
|
13
|
+
const metadataPath = require.resolve('hyperframes/package.json')
|
|
14
|
+
const metadata = JSON.parse(await readFile(metadataPath, 'utf8'))
|
|
15
|
+
if (metadata.version !== '0.9.56')
|
|
16
|
+
throw new Error('The pinned HyperFrames version is unavailable')
|
|
17
|
+
const environment = {
|
|
18
|
+
PATH: '/usr/local/bin:/usr/bin:/bin',
|
|
19
|
+
HOME: directory,
|
|
20
|
+
TMPDIR: directory,
|
|
21
|
+
LANG: 'C.UTF-8',
|
|
22
|
+
HYPERFRAMES_BROWSER_PATH: process.env.HAI_TOOL_BROWSER_PATH ?? '/usr/bin/chromium',
|
|
23
|
+
HYPERFRAMES_FFMPEG_PATH: process.env.HAI_TOOL_FFMPEG_PATH ?? '/usr/bin/ffmpeg',
|
|
24
|
+
HYPERFRAMES_FFPROBE_PATH: process.env.HAI_TOOL_FFPROBE_PATH ?? '/usr/bin/ffprobe',
|
|
25
|
+
HYPERFRAMES_NO_UPDATE_CHECK: '1',
|
|
26
|
+
HYPERFRAMES_NO_OPTIONAL_INSTALL: '1',
|
|
27
|
+
HYPERFRAMES_NO_CAPTURE_DOTENV: '1',
|
|
28
|
+
HYPERFRAMES_NO_ORPHAN_CLEANUP: '1',
|
|
29
|
+
HYPERFRAMES_RENDER_DETACHED: '0',
|
|
30
|
+
HYPERFRAMES_CAPTURE_DEADLINE_MS: '120000',
|
|
31
|
+
HYPERFRAMES_MAX_DOWNLOAD_BYTES: String(20 * 1024 * 1024),
|
|
32
|
+
PRODUCER_RESTRICT_PAGE_NETWORK: '1',
|
|
33
|
+
DO_NOT_TRACK: '1',
|
|
34
|
+
...extra,
|
|
35
|
+
}
|
|
36
|
+
return execute(process.execPath, [
|
|
37
|
+
join(dirname(metadataPath), 'bin/hyperframes.mjs'),
|
|
38
|
+
...args,
|
|
39
|
+
], {
|
|
40
|
+
cwd: directory,
|
|
41
|
+
env: environment,
|
|
42
|
+
timeout,
|
|
43
|
+
maxBuffer: 2 * 1024 * 1024,
|
|
44
|
+
windowsHide: true,
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function probeVideo(file, directory, expected) {
|
|
49
|
+
const { stdout } = await execute(process.env.HAI_TOOL_FFPROBE_PATH ?? '/usr/bin/ffprobe', [
|
|
50
|
+
'-v',
|
|
51
|
+
'error',
|
|
52
|
+
'-show_format',
|
|
53
|
+
'-show_streams',
|
|
54
|
+
'-of',
|
|
55
|
+
'json',
|
|
56
|
+
file,
|
|
57
|
+
], {
|
|
58
|
+
cwd: directory,
|
|
59
|
+
env: { PATH: '/usr/local/bin:/usr/bin:/bin', LANG: 'C.UTF-8' },
|
|
60
|
+
timeout: 15_000,
|
|
61
|
+
maxBuffer: 256 * 1024,
|
|
62
|
+
})
|
|
63
|
+
const result = JSON.parse(stdout)
|
|
64
|
+
const video = result.streams?.find(stream => stream.codec_type === 'video')
|
|
65
|
+
const duration = Number(result.format?.duration)
|
|
66
|
+
if (!video || video.codec_name !== 'h264' || !(duration > 0 && duration <= 30.25)
|
|
67
|
+
|| !(video.width > 0 && video.height > 0 && video.width * video.height <= 1920 * 1080)
|
|
68
|
+
|| video.width !== expected.width || video.height !== expected.height
|
|
69
|
+
|| Math.abs(duration - expected.duration) > 1 / expected.fps + 0.05) {
|
|
70
|
+
throw new Error('Rendered video failed media validation')
|
|
71
|
+
}
|
|
72
|
+
return { duration, width: video.width, height: video.height }
|
|
73
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""媒体项目和采集素材的有界归档读写。"""
|
|
2
|
+
import pathlib
|
|
3
|
+
import shutil
|
|
4
|
+
import stat
|
|
5
|
+
import zipfile
|
|
6
|
+
from tool import FILE_LIMIT, ToolError
|
|
7
|
+
|
|
8
|
+
ENTRY_LIMIT = 256
|
|
9
|
+
EXPANDED_LIMIT = 100 * 1024 * 1024
|
|
10
|
+
CAPTURE_INSTRUCTION_FILES = frozenset({'AGENTS.md', 'CLAUDE.md', '.cursorrules'})
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def archive_path(name):
|
|
14
|
+
if not isinstance(name, str) or not name or len(name) > 512 or '\\' in name or '\x00' in name:
|
|
15
|
+
raise ToolError('UNSAFE_PROJECT', 'Archive entry has an unsafe path')
|
|
16
|
+
path = pathlib.PurePosixPath(name)
|
|
17
|
+
if (path.is_absolute() or any(part in ('', '.', '..') for part in name.rstrip('/').split('/'))
|
|
18
|
+
or any(':' in part or part.startswith('.') for part in path.parts)):
|
|
19
|
+
raise ToolError('UNSAFE_PROJECT', 'Archive entry escapes the project')
|
|
20
|
+
return path
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def extract_project(source, destination):
|
|
24
|
+
try:
|
|
25
|
+
with zipfile.ZipFile(source) as archive:
|
|
26
|
+
entries = archive.infolist()
|
|
27
|
+
seen = set()
|
|
28
|
+
if len(entries) > ENTRY_LIMIT or sum(entry.file_size for entry in entries) > EXPANDED_LIMIT:
|
|
29
|
+
raise ToolError('UNSAFE_PROJECT', 'Project archive exceeds the extraction limits')
|
|
30
|
+
for entry in entries:
|
|
31
|
+
path = archive_path(entry.filename)
|
|
32
|
+
identity = str(path).casefold()
|
|
33
|
+
mode = entry.external_attr >> 16
|
|
34
|
+
if (identity in seen or entry.flag_bits & 1 or entry.file_size > FILE_LIMIT
|
|
35
|
+
or stat.S_ISLNK(mode) or (stat.S_IFMT(mode) not in (0, stat.S_IFREG, stat.S_IFDIR))):
|
|
36
|
+
raise ToolError('UNSAFE_PROJECT', 'Project contains duplicate, linked or unsupported entries')
|
|
37
|
+
seen.add(identity)
|
|
38
|
+
target = destination.joinpath(*path.parts)
|
|
39
|
+
if entry.is_dir():
|
|
40
|
+
target.mkdir(parents=True, exist_ok=True)
|
|
41
|
+
continue
|
|
42
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
43
|
+
with archive.open(entry) as incoming, target.open('xb') as output:
|
|
44
|
+
shutil.copyfileobj(incoming, output)
|
|
45
|
+
if target.stat().st_size != entry.file_size:
|
|
46
|
+
raise ToolError('UNSAFE_PROJECT', 'Project entry size is inconsistent')
|
|
47
|
+
except (zipfile.BadZipFile, RuntimeError, EOFError, NotImplementedError):
|
|
48
|
+
raise ToolError('UNSAFE_PROJECT', 'Invalid or unsupported project archive') from None
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def collect_files(directory, excluded_root_files=frozenset()):
|
|
52
|
+
files = []
|
|
53
|
+
total = 0
|
|
54
|
+
for path in sorted(directory.rglob('*')):
|
|
55
|
+
# 仅排除调用方指定的根文件,不放宽素材或用户项目的路径规则。
|
|
56
|
+
if path.parent == directory and path.name in excluded_root_files:
|
|
57
|
+
continue
|
|
58
|
+
info = path.lstat()
|
|
59
|
+
if stat.S_ISDIR(info.st_mode):
|
|
60
|
+
continue
|
|
61
|
+
if not stat.S_ISREG(info.st_mode) or info.st_size > FILE_LIMIT:
|
|
62
|
+
raise ToolError('INVALID_OUTPUT', 'Capture contains linked or oversized materials')
|
|
63
|
+
relative = path.relative_to(directory).as_posix()
|
|
64
|
+
archive_path(relative)
|
|
65
|
+
total += info.st_size
|
|
66
|
+
files.append((relative, path))
|
|
67
|
+
if len(files) > ENTRY_LIMIT or total > EXPANDED_LIMIT:
|
|
68
|
+
raise ToolError('OUTPUT_LIMIT', 'Capture exceeds the artifact limits')
|
|
69
|
+
return files
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def bundle_capture(directory, output):
|
|
73
|
+
files = collect_files(directory, CAPTURE_INSTRUCTION_FILES)
|
|
74
|
+
with zipfile.ZipFile(output, 'x', compression=zipfile.ZIP_DEFLATED) as archive:
|
|
75
|
+
for relative, source in files:
|
|
76
|
+
archive.write(source, relative)
|
|
77
|
+
if output.stat().st_size > FILE_LIMIT:
|
|
78
|
+
raise ToolError('OUTPUT_LIMIT', 'Capture archive exceeds the artifact limit')
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
FROM docker.io/library/node:24.22.1-bookworm-slim@sha256:be8204d4bd327363357f532187b752ff62759c31bedf98e9e18fc08c9cbd605c AS trust
|
|
2
|
+
RUN mkdir -p /etc/ssl/certs && node -e "require('node:fs').writeFileSync('/etc/ssl/certs/ca-certificates.crt', require('node:tls').rootCertificates.join('\\n'))"
|
|
3
|
+
FROM mcr.microsoft.com/mirror/docker/library/debian:bookworm-slim@sha256:b4aa902587c2e61ce789849cb54c332b0400fe27b1ee33af4669e1f7e7c3e22f
|
|
4
|
+
COPY --from=trust /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
|
5
|
+
ARG DEBIAN_MIRROR=https://deb.debian.org/debian
|
|
6
|
+
ARG DEBIAN_SECURITY_MIRROR=https://deb.debian.org/debian-security
|
|
7
|
+
RUN set -eu; \
|
|
8
|
+
sed -i -e "s|http://deb.debian.org/debian-security|${DEBIAN_SECURITY_MIRROR}|g" -e "s|http://deb.debian.org/debian|${DEBIAN_MIRROR}|g" /etc/apt/sources.list.d/debian.sources; \
|
|
9
|
+
apt-get -o Acquire::Retries=2 -o Acquire::https::Timeout=20 update; \
|
|
10
|
+
apt-get install -y --no-install-recommends python3 tesseract-ocr tesseract-ocr-eng tesseract-ocr-chi-sim tesseract-ocr-jpn; \
|
|
11
|
+
rm -rf /var/lib/apt/lists/*
|
|
12
|
+
RUN mkdir -p /opt/hai-tools /input /output && chown 1000:1000 /output
|
|
13
|
+
COPY tool.py /usr/local/bin/hai-tool
|
|
14
|
+
COPY ocr/operation.py /opt/hai-tools/operation.py
|
|
15
|
+
RUN chmod 0555 /usr/local/bin/hai-tool
|
|
16
|
+
USER 1000:1000
|
|
17
|
+
ENV HOME=/tmp PYTHONDONTWRITEBYTECODE=1
|
|
18
|
+
WORKDIR /tmp
|
|
19
|
+
ENTRYPOINT ["/usr/local/bin/hai-tool"]
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from tool import ToolError, read_text, run_command
|
|
2
|
+
|
|
3
|
+
LANGUAGES = {'eng', 'chi_sim', 'jpn', 'eng+chi_sim', 'eng+jpn'}
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def recognize(request, source, _artifact, workspace):
|
|
7
|
+
language = request.get('language', 'eng')
|
|
8
|
+
if not isinstance(language, str) or language not in LANGUAGES:
|
|
9
|
+
raise ToolError('INVALID_REQUEST', 'Unsupported OCR language')
|
|
10
|
+
with source.open('rb') as stream:
|
|
11
|
+
header = stream.read(8)
|
|
12
|
+
if not (header.startswith(b'\x89PNG\r\n\x1a\n') or header.startswith(b'\xff\xd8\xff') or header.startswith((b'II*\0', b'MM\0*'))):
|
|
13
|
+
raise ToolError('INVALID_INPUT', 'Expected a PNG, JPEG or TIFF image')
|
|
14
|
+
run_command(['/usr/bin/tesseract', str(source), str(workspace / 'result'), '-l', language], 60, workspace)
|
|
15
|
+
return read_text(workspace / 'result.txt')
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
commands = {'ocr.recognize': ({'input'}, {'language'}, recognize)}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
FROM docker.io/library/node:24.22.1-bookworm-slim@sha256:be8204d4bd327363357f532187b752ff62759c31bedf98e9e18fc08c9cbd605c AS trust
|
|
2
|
+
RUN mkdir -p /etc/ssl/certs && node -e "require('node:fs').writeFileSync('/etc/ssl/certs/ca-certificates.crt', require('node:tls').rootCertificates.join('\\n'))"
|
|
3
|
+
FROM mcr.microsoft.com/mirror/docker/library/debian:bookworm-slim@sha256:b4aa902587c2e61ce789849cb54c332b0400fe27b1ee33af4669e1f7e7c3e22f
|
|
4
|
+
COPY --from=trust /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
|
5
|
+
ARG DEBIAN_MIRROR=https://deb.debian.org/debian
|
|
6
|
+
ARG DEBIAN_SECURITY_MIRROR=https://deb.debian.org/debian-security
|
|
7
|
+
RUN set -eu; \
|
|
8
|
+
sed -i -e "s|http://deb.debian.org/debian-security|${DEBIAN_SECURITY_MIRROR}|g" -e "s|http://deb.debian.org/debian|${DEBIAN_MIRROR}|g" /etc/apt/sources.list.d/debian.sources; \
|
|
9
|
+
apt-get -o Acquire::Retries=2 -o Acquire::https::Timeout=20 update; \
|
|
10
|
+
apt-get install -y --no-install-recommends python3 python3-uno libreoffice-writer libreoffice-calc libreoffice-impress fonts-liberation fonts-noto-cjk; \
|
|
11
|
+
rm -rf /var/lib/apt/lists/*
|
|
12
|
+
RUN mkdir -p /opt/hai-tools /input /output && chown 1000:1000 /output
|
|
13
|
+
COPY tool.py /usr/local/bin/hai-tool
|
|
14
|
+
COPY office/operation.py /opt/hai-tools/operation.py
|
|
15
|
+
COPY archives.py /opt/hai-tools/archives.py
|
|
16
|
+
COPY office/office_job.py /opt/hai-tools/office_job.py
|
|
17
|
+
RUN chmod 0555 /usr/local/bin/hai-tool
|
|
18
|
+
USER 1000:1000
|
|
19
|
+
ENV HOME=/tmp PYTHONDONTWRITEBYTECODE=1
|
|
20
|
+
WORKDIR /tmp
|
|
21
|
+
ENTRYPOINT ["/usr/local/bin/hai-tool"]
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""通过独立 LibreOffice profile 和本地 UNO 管道运行单个作业。"""
|
|
2
|
+
import pathlib
|
|
3
|
+
import subprocess
|
|
4
|
+
import sys
|
|
5
|
+
import time
|
|
6
|
+
import uuid
|
|
7
|
+
import uno
|
|
8
|
+
from com.sun.star.beans import PropertyValue
|
|
9
|
+
from com.sun.star.connection import NoConnectException
|
|
10
|
+
from com.sun.star.document.MacroExecMode import NEVER_EXECUTE
|
|
11
|
+
from com.sun.star.document.UpdateDocMode import NO_UPDATE
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def property_value(name, value):
|
|
15
|
+
item = PropertyValue()
|
|
16
|
+
item.Name = name
|
|
17
|
+
item.Value = value
|
|
18
|
+
return item
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def main():
|
|
22
|
+
source, result, output_filter, operation = sys.argv[1:]
|
|
23
|
+
context = uno.getComponentContext()
|
|
24
|
+
resolver = context.ServiceManager.createInstanceWithContext('com.sun.star.bridge.UnoUrlResolver', context)
|
|
25
|
+
pipe = 'hai_' + uuid.uuid4().hex
|
|
26
|
+
address = 'pipe,name=' + pipe + ';urp;StarOffice.ComponentContext'
|
|
27
|
+
profile = (pathlib.Path.cwd() / 'profile').as_uri()
|
|
28
|
+
with subprocess.Popen([
|
|
29
|
+
'/usr/bin/libreoffice', '-env:UserInstallation=' + profile,
|
|
30
|
+
'--headless', '--nologo', '--norestore', '--nodefault', '--nofirststartwizard', '--accept=' + address,
|
|
31
|
+
]) as process:
|
|
32
|
+
try:
|
|
33
|
+
deadline = time.monotonic() + 10
|
|
34
|
+
while True:
|
|
35
|
+
try:
|
|
36
|
+
remote = resolver.resolve('uno:' + address)
|
|
37
|
+
break
|
|
38
|
+
except NoConnectException:
|
|
39
|
+
if time.monotonic() >= deadline or process.poll() is not None:
|
|
40
|
+
raise
|
|
41
|
+
time.sleep(0.05)
|
|
42
|
+
desktop = remote.ServiceManager.createInstanceWithContext('com.sun.star.frame.Desktop', remote)
|
|
43
|
+
document = desktop.loadComponentFromURL(pathlib.Path(source).as_uri(), '_blank', 0, (
|
|
44
|
+
property_value('Hidden', True),
|
|
45
|
+
property_value('ReadOnly', True),
|
|
46
|
+
property_value('MacroExecutionMode', NEVER_EXECUTE),
|
|
47
|
+
property_value('UpdateDocMode', NO_UPDATE),
|
|
48
|
+
))
|
|
49
|
+
if document is None:
|
|
50
|
+
raise RuntimeError('Unable to load office document')
|
|
51
|
+
try:
|
|
52
|
+
if operation == 'recalculate':
|
|
53
|
+
document.calculateAll()
|
|
54
|
+
document.storeToURL(pathlib.Path(result).as_uri(), (property_value('FilterName', output_filter),))
|
|
55
|
+
finally:
|
|
56
|
+
document.dispose()
|
|
57
|
+
desktop.terminate()
|
|
58
|
+
process.wait(timeout=3)
|
|
59
|
+
finally:
|
|
60
|
+
if process.poll() is None:
|
|
61
|
+
process.kill()
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
if __name__ == '__main__':
|
|
65
|
+
main()
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import pathlib
|
|
2
|
+
import xml.etree.ElementTree as ET
|
|
3
|
+
import zipfile
|
|
4
|
+
from archives import OfficeArchive, office_archive_error
|
|
5
|
+
from tool import ToolError, publish, run_command
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
PDF_FILTERS = {
|
|
9
|
+
'.docx': 'writer_pdf_Export', '.odt': 'writer_pdf_Export',
|
|
10
|
+
'.xlsx': 'calc_pdf_Export', '.ods': 'calc_pdf_Export',
|
|
11
|
+
'.pptx': 'impress_pdf_Export', '.odp': 'impress_pdf_Export',
|
|
12
|
+
}
|
|
13
|
+
SHEET_FILTERS = {'.xlsx': 'Calc MS Excel 2007 XML', '.ods': 'calc8'}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def process(request, source, artifact, workspace):
|
|
17
|
+
suffix = source.suffix.lower()
|
|
18
|
+
convert = request['operation'] == 'office.convert'
|
|
19
|
+
if convert:
|
|
20
|
+
if suffix not in PDF_FILTERS or artifact.suffix.lower() != '.pdf':
|
|
21
|
+
raise ToolError('INVALID_REQUEST', 'Conversion requires a supported office document and PDF output')
|
|
22
|
+
output_filter = PDF_FILTERS[suffix]
|
|
23
|
+
signature = b'%PDF-'
|
|
24
|
+
else:
|
|
25
|
+
if suffix not in SHEET_FILTERS or artifact.suffix.lower() != suffix:
|
|
26
|
+
raise ToolError('INVALID_REQUEST', 'Recalculation requires matching XLSX or ODS input and output')
|
|
27
|
+
output_filter = SHEET_FILTERS[suffix]
|
|
28
|
+
signature = b'PK\x03\x04'
|
|
29
|
+
try:
|
|
30
|
+
with OfficeArchive(source) as document:
|
|
31
|
+
document.reject_external_links()
|
|
32
|
+
except (zipfile.BadZipFile, KeyError, ValueError, ET.ParseError):
|
|
33
|
+
raise office_archive_error() from None
|
|
34
|
+
result = workspace / artifact.name
|
|
35
|
+
worker = pathlib.Path(__file__).with_name('office_job.py')
|
|
36
|
+
run_command(['/usr/bin/python3', str(worker), str(source), str(result), output_filter, 'convert' if convert else 'recalculate'], 90, workspace)
|
|
37
|
+
return publish(result, artifact, signature)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
commands = {
|
|
41
|
+
'office.convert': ({'input', 'output'}, set(), process),
|
|
42
|
+
'office.recalculate': ({'input', 'output'}, set(), process),
|
|
43
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
FROM docker.io/library/node:24.22.1-bookworm-slim@sha256:be8204d4bd327363357f532187b752ff62759c31bedf98e9e18fc08c9cbd605c
|
|
2
|
+
RUN mkdir -p /etc/ssl/certs && node -e "require('node:fs').writeFileSync('/etc/ssl/certs/ca-certificates.crt', require('node:tls').rootCertificates.join('\\n'))"
|
|
3
|
+
ARG DEBIAN_MIRROR=https://deb.debian.org/debian
|
|
4
|
+
ARG DEBIAN_SECURITY_MIRROR=https://deb.debian.org/debian-security
|
|
5
|
+
RUN set -eu; \
|
|
6
|
+
sed -i -e "s|http://deb.debian.org/debian-security|${DEBIAN_SECURITY_MIRROR}|g" -e "s|http://deb.debian.org/debian|${DEBIAN_MIRROR}|g" /etc/apt/sources.list.d/debian.sources; \
|
|
7
|
+
apt-get -o Acquire::Retries=2 -o Acquire::https::Timeout=20 update; \
|
|
8
|
+
apt-get install -y --no-install-recommends python3; \
|
|
9
|
+
rm -rf /var/lib/apt/lists/*
|
|
10
|
+
RUN mkdir -p /opt/hai-tools/preview_build /input /output && chown 1000:1000 /output
|
|
11
|
+
COPY tool.py /usr/local/bin/hai-tool
|
|
12
|
+
COPY media_files.py /opt/hai-tools/media_files.py
|
|
13
|
+
COPY preview_build /opt/hai-tools/preview_build
|
|
14
|
+
RUN npm ci --prefix /opt/hai-tools/preview_build --ignore-scripts --no-audit --no-fund
|
|
15
|
+
RUN printf 'from preview_build.operation import commands\n' > /opt/hai-tools/operation.py && chmod 0555 /usr/local/bin/hai-tool
|
|
16
|
+
USER 1000:1000
|
|
17
|
+
ENV HOME=/tmp PYTHONDONTWRITEBYTECODE=1
|
|
18
|
+
WORKDIR /tmp
|
|
19
|
+
ENTRYPOINT ["/usr/local/bin/hai-tool"]
|