@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.
Files changed (49) hide show
  1. package/LICENSE +202 -0
  2. package/README.en.md +37 -0
  3. package/README.ja.md +37 -0
  4. package/README.md +36 -2
  5. package/dist/chunk-UA7MOYTT.js +413 -0
  6. package/dist/chunk-UA7MOYTT.js.map +1 -0
  7. package/dist/index.d.ts +16 -0
  8. package/dist/index.js +3 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/server/index.d.ts +306 -0
  11. package/dist/server/index.js +1051 -0
  12. package/dist/server/index.js.map +1 -0
  13. package/dist/tools-types-CM0lGuJr.d.ts +199 -0
  14. package/jobs/.dockerignore +4 -0
  15. package/jobs/README.md +223 -0
  16. package/jobs/archives.py +56 -0
  17. package/jobs/browser/Dockerfile +20 -0
  18. package/jobs/browser/operation.py +17 -0
  19. package/jobs/browser/render.mjs +90 -0
  20. package/jobs/browser_policy.py +36 -0
  21. package/jobs/documents/Dockerfile +23 -0
  22. package/jobs/documents/generate.py +165 -0
  23. package/jobs/documents/operation.py +84 -0
  24. package/jobs/documents/requirements.txt +3 -0
  25. package/jobs/hyperframes-runtime.mjs +73 -0
  26. package/jobs/media_files.py +78 -0
  27. package/jobs/ocr/Dockerfile +19 -0
  28. package/jobs/ocr/operation.py +18 -0
  29. package/jobs/office/Dockerfile +21 -0
  30. package/jobs/office/office_job.py +65 -0
  31. package/jobs/office/operation.py +43 -0
  32. package/jobs/preview_build/Dockerfile +19 -0
  33. package/jobs/preview_build/build.mjs +552 -0
  34. package/jobs/preview_build/operation.py +62 -0
  35. package/jobs/preview_build/package.json +11 -0
  36. package/jobs/tests/image-smoke.mjs +83 -0
  37. package/jobs/tests/requirements.txt +4 -0
  38. package/jobs/tests/test_documents_generate.py +107 -0
  39. package/jobs/tests/test_hai_tool.py +82 -0
  40. package/jobs/tests/test_media_jobs.py +449 -0
  41. package/jobs/tests/test_preview_build.py +213 -0
  42. package/jobs/tool.py +145 -0
  43. package/jobs/video_render/Dockerfile +20 -0
  44. package/jobs/video_render/operation.py +109 -0
  45. package/jobs/video_render/render.mjs +36 -0
  46. package/jobs/web_capture/Dockerfile +20 -0
  47. package/jobs/web_capture/capture.mjs +122 -0
  48. package/jobs/web_capture/operation.py +65 -0
  49. package/package.json +62 -3
@@ -0,0 +1,83 @@
1
+ import { spawnSync } from 'node:child_process'
2
+ import { chmod, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
3
+ import { tmpdir } from 'node:os'
4
+ import { join } from 'node:path'
5
+ import process from 'node:process'
6
+
7
+ async function smoke() {
8
+ const engine = process.env.HAI_TOOL_IMAGE_ENGINE ?? 'podman'
9
+ if (!['podman', 'docker'].includes(engine))
10
+ throw new Error('HAI_TOOL_IMAGE_ENGINE must be podman or docker')
11
+ const directory = await mkdtemp(join(tmpdir(), 'hai-tool-images-'))
12
+ const input = join(directory, 'input')
13
+ const output = join(directory, 'output')
14
+ try {
15
+ await mkdir(input, { mode: 0o755 })
16
+ await mkdir(output)
17
+ await chmod(output, 0o777)
18
+ await writeFile(join(input, 'input.txt'), 'HAI tool image fixture')
19
+ for (const [name, request, check] of [
20
+ [
21
+ 'documents',
22
+ { operation: 'documents.extract', input: 'input.txt' },
23
+ result => result.data?.text === 'HAI tool image fixture',
24
+ ],
25
+ [
26
+ 'office',
27
+ { operation: 'office.convert', input: 'input.txt', output: 'office.pdf' },
28
+ result => result.error?.code === 'INVALID_REQUEST',
29
+ ],
30
+ [
31
+ 'ocr',
32
+ { operation: 'ocr.recognize', input: 'input.txt' },
33
+ result => result.error?.code === 'INVALID_INPUT',
34
+ ],
35
+ [
36
+ 'browser',
37
+ { operation: 'browser.navigate', url: 'https://example.com/', output: 'page.png' },
38
+ result => result.error?.code === 'NETWORK_POLICY_REQUIRED',
39
+ ],
40
+ ]) {
41
+ const image = process.env[`HAI_TOOL_IMAGE_${name.toUpperCase()}`]
42
+ if (!image)
43
+ throw new Error(`Missing image for ${name}`)
44
+ const task = spawnSync(engine, [
45
+ 'run',
46
+ '--rm',
47
+ '--pull=never',
48
+ '-i',
49
+ '--network=none',
50
+ '--read-only',
51
+ '--tmpfs=/tmp:rw,nosuid,nodev,size=128m',
52
+ ...(engine === 'podman'
53
+ ? ['--userns=keep-id:uid=1000,gid=1000']
54
+ : []),
55
+ '--mount',
56
+ `type=bind,src=${input},dst=/input,ro=true`,
57
+ '--mount',
58
+ `type=bind,src=${output},dst=/output`,
59
+ image,
60
+ ], {
61
+ input: JSON.stringify(request),
62
+ encoding: 'utf8',
63
+ timeout: 30_000,
64
+ })
65
+ if (task.status !== 0)
66
+ throw new Error(`${name} failed to start: ${task.stderr ?? task.error}`)
67
+ const result = JSON.parse(task.stdout)
68
+ if (!check(result))
69
+ throw new Error(`${name} returned unexpected result: ${JSON.stringify(result)}`)
70
+ process.stdout.write(`${name} image smoke passed\n`)
71
+ }
72
+ if (await readFile(join(input, 'input.txt'), 'utf8') !== 'HAI tool image fixture')
73
+ throw new Error('Tool modified read-only input')
74
+ }
75
+ finally {
76
+ await rm(directory, { recursive: true, force: true })
77
+ }
78
+ }
79
+
80
+ smoke().catch((error) => {
81
+ process.stderr.write(`${error.message}\n`)
82
+ process.exitCode = 1
83
+ })
@@ -0,0 +1,4 @@
1
+ # 用于测试进程在容器外生成真实的 Office 文件;生产镜像单独安装依赖。
2
+ openpyxl==3.1.5
3
+ python-docx==1.2.0
4
+ python-pptx==1.0.2
@@ -0,0 +1,107 @@
1
+ import io
2
+ import json
3
+ import pathlib
4
+ import sys
5
+ import tempfile
6
+ import unittest
7
+ import xml.etree.ElementTree as ET
8
+ import zipfile
9
+
10
+ ROOT = pathlib.Path(__file__).resolve().parents[1]
11
+ sys.path.insert(0, str(ROOT))
12
+ from tool import serve
13
+ from test_hai_tool import load
14
+
15
+
16
+ class GenerateDocumentsTests(unittest.TestCase):
17
+ def setUp(self):
18
+ self.temporary = tempfile.TemporaryDirectory()
19
+ self.addCleanup(self.temporary.cleanup)
20
+ self.root = pathlib.Path(self.temporary.name)
21
+ self.inputs = self.root / 'input'
22
+ self.outputs = self.root / 'output'
23
+ self.inputs.mkdir()
24
+ self.outputs.mkdir()
25
+
26
+ def invoke(self, specification, output):
27
+ (self.inputs / 'source.json').write_text(json.dumps(specification, ensure_ascii=False), encoding='utf-8')
28
+ response = io.StringIO()
29
+ serve(io.BytesIO(json.dumps({'operation': 'documents.generate', 'input': 'source.json', 'output': output}).encode()),
30
+ response, load('documents'), self.inputs, self.outputs)
31
+ return json.loads(response.getvalue())
32
+
33
+ def test_docx_contains_title_paragraphs_and_real_table(self):
34
+ result = self.invoke({'title': '发布说明', 'paragraphs': ['工具可以独立调用。'],
35
+ 'table': {'headers': ['模块', '状态'], 'rows': [['Tools', '完成']]}}, 'report.docx')
36
+ self.assertTrue(result['success'], result)
37
+ with zipfile.ZipFile(self.outputs / 'report.docx') as archive:
38
+ document = ET.fromstring(archive.read('word/document.xml'))
39
+ texts = [node.text for node in document.iter('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}t')]
40
+ self.assertIn('发布说明', texts)
41
+ self.assertIn('工具可以独立调用。', texts)
42
+ self.assertIn('Tools', texts)
43
+ self.assertEqual(len(list(document.iter('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}tbl'))), 1)
44
+
45
+ def test_xlsx_writes_user_content_as_strings_without_formulas(self):
46
+ values = ['=1+1', '=WEBSERVICE("https://example.com")', '+2+3', '@SUM(A1:A2)']
47
+ result = self.invoke({'title': '安全表格', 'table': {'headers': ['输入'], 'rows': [[value] for value in values]}}, 'report.xlsx')
48
+ self.assertTrue(result['success'], result)
49
+ with zipfile.ZipFile(self.outputs / 'report.xlsx') as archive:
50
+ sheet = ET.fromstring(archive.read('xl/worksheets/sheet1.xml'))
51
+ namespace = '{http://schemas.openxmlformats.org/spreadsheetml/2006/main}'
52
+ self.assertEqual(list(sheet.iter(namespace + 'f')), [])
53
+ texts = [node.text for node in sheet.iter(namespace + 't')]
54
+ for value in values:
55
+ self.assertIn(value, texts)
56
+
57
+ def test_pptx_contains_real_slides_and_table(self):
58
+ result = self.invoke({'title': '工具平台', 'slides': [
59
+ {'title': '一个入口', 'paragraphs': ['复用同一工具实现。']},
60
+ {'title': '能力清单', 'table': {'headers': ['能力', '用途'], 'rows': [['网页', '采集素材']]}},
61
+ ]}, 'report.pptx')
62
+ self.assertTrue(result['success'], result)
63
+ with zipfile.ZipFile(self.outputs / 'report.pptx') as archive:
64
+ slides = [name for name in archive.namelist() if name.startswith('ppt/slides/slide') and name.endswith('.xml')]
65
+ self.assertEqual(len(slides), 2)
66
+ drawing = '{http://schemas.openxmlformats.org/drawingml/2006/main}'
67
+ first = ET.fromstring(archive.read('ppt/slides/slide1.xml'))
68
+ second = ET.fromstring(archive.read('ppt/slides/slide2.xml'))
69
+ self.assertIn('一个入口', [node.text for node in first.iter(drawing + 't')])
70
+ self.assertEqual(len(list(second.iter(drawing + 'tbl'))), 1)
71
+
72
+ def test_rejects_unknown_fields_controls_and_excessive_or_ragged_tables(self):
73
+ invalid = [
74
+ {'title': 'Report', 'command': 'execute'},
75
+ {'title': 'Report\u0000'},
76
+ {'title': 'x' * 201},
77
+ {'title': 'Report', 'paragraphs': ['item'] * 101},
78
+ {'title': 'Report', 'table': {'headers': ['a', 'b'], 'rows': [['only one']]}},
79
+ {'title': 'Report', 'table': {'headers': ['a'] * 21, 'rows': []}},
80
+ {'title': 'Report', 'table': {'headers': ['a'], 'rows': [['item']] * 201}},
81
+ {'title': 'Report', 'slides': [{'title': 'Only slides'}]},
82
+ ]
83
+ for index, specification in enumerate(invalid):
84
+ with self.subTest(specification=index):
85
+ result = self.invoke(specification, f'invalid{index}.docx')
86
+ self.assertEqual(result['error']['code'], 'INVALID_REQUEST')
87
+ self.assertEqual(list(self.outputs.iterdir()), [])
88
+
89
+ def test_rejects_oversized_specs_and_non_office_output(self):
90
+ result = self.invoke({'title': 'Report', 'paragraphs': ['x' * (256 * 1024)]}, 'large.docx')
91
+ self.assertEqual(result['error']['code'], 'INVALID_INPUT')
92
+ result = self.invoke({'title': 'Report'}, 'report.pdf')
93
+ self.assertEqual(result['error']['code'], 'INVALID_REQUEST')
94
+
95
+ def test_pptx_rejects_ambiguous_or_unbounded_slide_layout(self):
96
+ for specification in [
97
+ {'title': 'Report', 'paragraphs': ['summary'], 'slides': [{'title': 'Slide'}]},
98
+ {'title': 'Report', 'slides': [{'title': 'Slide'}] * 21},
99
+ {'title': 'Report', 'table': {'headers': ['Column'], 'rows': [['text' * 20]]}},
100
+ ]:
101
+ result = self.invoke(specification, 'large.pptx')
102
+ self.assertEqual(result['error']['code'], 'INVALID_REQUEST')
103
+ self.assertEqual(list(self.outputs.iterdir()), [])
104
+
105
+
106
+ if __name__ == '__main__':
107
+ unittest.main()
@@ -0,0 +1,82 @@
1
+ import importlib.util
2
+ import io
3
+ import json
4
+ import pathlib
5
+ import sys
6
+ import tempfile
7
+ import unittest
8
+ import zipfile
9
+
10
+ ROOT = pathlib.Path(__file__).resolve().parents[1]
11
+ sys.path.insert(0, str(ROOT))
12
+ from tool import serve
13
+
14
+
15
+ def load(kind):
16
+ spec = importlib.util.spec_from_file_location(kind, ROOT / kind / 'operation.py')
17
+ module = importlib.util.module_from_spec(spec)
18
+ spec.loader.exec_module(module)
19
+ return module.commands
20
+
21
+
22
+ class ToolTests(unittest.TestCase):
23
+ def setUp(self):
24
+ self.temp = tempfile.TemporaryDirectory()
25
+ self.addCleanup(self.temp.cleanup)
26
+ self.root = pathlib.Path(self.temp.name)
27
+ self.inputs = self.root / 'input'
28
+ self.outputs = self.root / 'output'
29
+ self.inputs.mkdir()
30
+ self.outputs.mkdir()
31
+
32
+ def run_tool(self, kind, request):
33
+ result = io.StringIO()
34
+ serve(io.BytesIO(json.dumps(request).encode()), result, load(kind), self.inputs, self.outputs)
35
+ return json.loads(result.getvalue())
36
+
37
+ def test_extract_text_and_reject_link_escape(self):
38
+ (self.inputs / 'plain.txt').write_text('Hello, 文件。', encoding='utf-8')
39
+ args = {'operation': 'documents.extract', 'input': 'plain.txt'}
40
+ self.assertEqual(self.run_tool('documents', args), {'success': True, 'data': {'text': 'Hello, 文件。'}})
41
+ (self.inputs / 'link.txt').symlink_to(self.inputs / 'plain.txt')
42
+ for name in ['../plain.txt', '/etc/passwd', 'link.txt']:
43
+ self.assertFalse(self.run_tool('documents', {**args, 'input': name})['success'])
44
+
45
+ def test_reject_unknown_fields_and_oversized_json(self):
46
+ bad = self.run_tool('documents', {'operation': 'documents.extract', 'input': 'plain.txt', 'command': 'wrong'})
47
+ self.assertEqual(bad['error']['code'], 'INVALID_REQUEST')
48
+ result = io.StringIO()
49
+ serve(io.BytesIO(b' ' * 70_000), result, load('documents'), self.inputs, self.outputs)
50
+ self.assertEqual(json.loads(result.getvalue())['error']['code'], 'INVALID_REQUEST')
51
+
52
+ def test_office_rejects_external_document_relationships(self):
53
+ with zipfile.ZipFile(self.inputs / 'linked.docx', 'w') as archive:
54
+ archive.writestr('[Content_Types].xml', '<Types/>')
55
+ archive.writestr('word/document.xml', '<document/>')
56
+ archive.writestr('word/_rels/document.xml.rels', '<Relationships><Relationship TargetMode="External" Target="https://example.test/file"/></Relationships>')
57
+ result = self.run_tool('office', {'operation': 'office.convert', 'input': 'linked.docx', 'output': 'result.pdf'})
58
+ self.assertEqual(result['error']['code'], 'UNSAFE_DOCUMENT')
59
+ self.assertEqual(list(self.outputs.iterdir()), [])
60
+
61
+ def test_ocr_rejects_unsupported_input_and_language(self):
62
+ (self.inputs / 'sample.png').write_bytes(b'invalid image data')
63
+ args = {'operation': 'ocr.recognize', 'input': 'sample.png'}
64
+ self.assertEqual(self.run_tool('ocr', {**args, 'language': '-bad'})['error']['code'], 'INVALID_REQUEST')
65
+ self.assertEqual(self.run_tool('ocr', {**args, 'language': 'eng'})['error']['code'], 'INVALID_INPUT')
66
+
67
+ def test_browser_rejects_invalid_url_and_missing_deployment_proxy(self):
68
+ args = {'operation': 'browser.navigate', 'output': 'screen.png'}
69
+ for url in ['file:///etc/passwd', 'http://example.test', 'https://person:token@example.test', 'https://127.0.0.1/']:
70
+ self.assertEqual(self.run_tool('browser', {**args, 'url': url})['error']['code'], 'INVALID_REQUEST')
71
+ self.assertEqual(self.run_tool('browser', {**args, 'url': 'https://example.test'})['error']['code'], 'NETWORK_POLICY_REQUIRED')
72
+
73
+ def test_tool_cannot_overwrite_existing_outputs(self):
74
+ artifact = self.outputs / 'screen.png'
75
+ artifact.write_text('existing')
76
+ args = {'operation': 'browser.navigate', 'output': artifact.name, 'url': 'https://example.test'}
77
+ self.assertEqual(self.run_tool('browser', args)['error']['code'], 'INVALID_OUTPUT')
78
+ self.assertEqual(artifact.read_text(), 'existing')
79
+
80
+
81
+ if __name__ == '__main__':
82
+ unittest.main()