@h-ai/ai 1.0.0-beta.22 → 1.0.0-beta.24

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 (41) hide show
  1. package/README.en.md +145 -141
  2. package/README.ja.md +145 -141
  3. package/README.md +143 -141
  4. package/dist/{ai-acp-types-BNeA1j0P.d.ts → ai-acp-types-B99Oa7WD.d.ts} +2242 -233
  5. package/dist/browser.d.ts +3 -1
  6. package/dist/browser.js +3 -3
  7. package/dist/{chunk-55A3S4AH.js → chunk-3G4ZLXH4.js} +154 -16
  8. package/dist/chunk-3G4ZLXH4.js.map +1 -0
  9. package/dist/{chunk-WSH2246H.js → chunk-3R5SMLSJ.js} +3 -3
  10. package/dist/{chunk-WSH2246H.js.map → chunk-3R5SMLSJ.js.map} +1 -1
  11. package/dist/{chunk-M77C3VAP.js → chunk-NRDLMUSY.js} +4 -2
  12. package/dist/chunk-NRDLMUSY.js.map +1 -0
  13. package/dist/client/index.js +2 -2
  14. package/dist/index.d.ts +4 -2
  15. package/dist/index.js +2492 -599
  16. package/dist/index.js.map +1 -1
  17. package/models/agents/README.md +30 -18
  18. package/models/agents/build.mjs +158 -55
  19. package/models/agents/codex-acp/Dockerfile +8 -13
  20. package/models/agents/deepseek-harness/Dockerfile +8 -13
  21. package/models/agents/goose/Dockerfile +8 -13
  22. package/models/agents/goose/agent.json +1 -1
  23. package/package.json +11 -8
  24. package/tools/.dockerignore +4 -0
  25. package/tools/README.md +74 -0
  26. package/tools/archives.py +56 -0
  27. package/tools/browser/Dockerfile +19 -0
  28. package/tools/browser/operation.py +50 -0
  29. package/tools/browser/render.mjs +40 -0
  30. package/tools/documents/Dockerfile +20 -0
  31. package/tools/documents/operation.py +80 -0
  32. package/tools/ocr/Dockerfile +19 -0
  33. package/tools/ocr/operation.py +18 -0
  34. package/tools/office/Dockerfile +21 -0
  35. package/tools/office/office_job.py +65 -0
  36. package/tools/office/operation.py +43 -0
  37. package/tools/tests/image-smoke.mjs +83 -0
  38. package/tools/tests/test_hai_tool.py +82 -0
  39. package/tools/tool.py +145 -0
  40. package/dist/chunk-55A3S4AH.js.map +0 -1
  41. package/dist/chunk-M77C3VAP.js.map +0 -1
package/tools/tool.py ADDED
@@ -0,0 +1,145 @@
1
+ #!/usr/bin/python3
2
+ """固定工具镜像的单请求入口。"""
3
+ import json
4
+ import os
5
+ import pathlib
6
+ import re
7
+ import shutil
8
+ import signal
9
+ import stat
10
+ import subprocess
11
+ import sys
12
+ import tempfile
13
+
14
+ REQUEST_LIMIT = 64 * 1024
15
+ FILE_LIMIT = 50 * 1024 * 1024
16
+ TEXT_LIMIT = 256 * 1024
17
+ NAME = re.compile(r'[A-Za-z0-9][A-Za-z0-9_.-]{0,127}\Z', re.ASCII)
18
+
19
+
20
+ class ToolError(Exception):
21
+ def __init__(self, code, message):
22
+ super().__init__(message)
23
+ self.code = code
24
+
25
+
26
+ def require_name(name, code):
27
+ if not isinstance(name, str) or not NAME.fullmatch(name):
28
+ raise ToolError(code, 'A simple file name is required')
29
+ return name
30
+
31
+
32
+ def input_file(root, name):
33
+ path = root / require_name(name, 'INVALID_INPUT')
34
+ try:
35
+ info = path.lstat()
36
+ except OSError:
37
+ raise ToolError('INVALID_INPUT', 'Input file is unavailable') from None
38
+ if not stat.S_ISREG(info.st_mode) or not 0 < info.st_size <= FILE_LIMIT:
39
+ raise ToolError('INVALID_INPUT', 'Input is not a regular file or exceeds the size limit')
40
+ return path
41
+
42
+
43
+ def output_file(root, name):
44
+ path = root / require_name(name, 'INVALID_OUTPUT')
45
+ if os.path.lexists(path):
46
+ raise ToolError('INVALID_OUTPUT', 'Output already exists')
47
+ return path
48
+
49
+
50
+ def text_result(text):
51
+ if len(text.encode('utf-8')) > TEXT_LIMIT:
52
+ raise ToolError('OUTPUT_LIMIT', 'Extracted text exceeds the size limit')
53
+ return {'text': text}
54
+
55
+
56
+ def read_text(path):
57
+ with path.open('rb') as source:
58
+ data = source.read(TEXT_LIMIT + 1)
59
+ if len(data) > TEXT_LIMIT:
60
+ raise ToolError('OUTPUT_LIMIT', 'Extracted text exceeds the size limit')
61
+ return text_result(data.decode('utf-8'))
62
+
63
+
64
+ def publish(source, output, signature):
65
+ info = source.lstat()
66
+ if not stat.S_ISREG(info.st_mode) or not 0 < info.st_size <= FILE_LIMIT:
67
+ raise ToolError('INVALID_OUTPUT', 'Produced artifact is not a bounded regular file')
68
+ with source.open('rb') as stream:
69
+ if not stream.read(len(signature)).startswith(signature):
70
+ raise ToolError('INVALID_OUTPUT', 'Produced artifact has an unexpected format')
71
+ stream.seek(0)
72
+ try:
73
+ with output.open('xb') as destination:
74
+ try:
75
+ shutil.copyfileobj(stream, destination)
76
+ except OSError:
77
+ output.unlink(missing_ok=True)
78
+ raise
79
+ except FileExistsError:
80
+ raise ToolError('INVALID_OUTPUT', 'Output already exists') from None
81
+ return {'artifact': {'name': output.name, 'bytes': info.st_size}}
82
+
83
+
84
+ def run_command(args, timeout, workdir):
85
+ # 不继承代理、工具凭据或外部程序配置。诊断输出不能混入协议。
86
+ env = {'PATH': '/usr/local/bin:/usr/bin:/bin', 'HOME': str(workdir), 'TMPDIR': str(workdir), 'LANG': 'C.UTF-8'}
87
+ with subprocess.Popen(args, cwd=workdir, env=env, stdin=subprocess.DEVNULL,
88
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True) as child:
89
+ try:
90
+ code = child.wait(timeout=timeout)
91
+ except BaseException:
92
+ try:
93
+ os.killpg(child.pid, signal.SIGKILL)
94
+ except ProcessLookupError:
95
+ pass
96
+ child.wait()
97
+ raise
98
+ if code != 0:
99
+ raise ToolError('TOOL_FAILED', 'The selected operation failed')
100
+
101
+
102
+ def no_duplicates(pairs):
103
+ values = {}
104
+ for key, value in pairs:
105
+ if key in values:
106
+ raise ToolError('INVALID_REQUEST', 'Duplicate request field')
107
+ values[key] = value
108
+ return values
109
+
110
+
111
+ def serve(source, response, commands, inputs, outputs):
112
+ try:
113
+ raw = source.read(REQUEST_LIMIT + 1)
114
+ if len(raw) > REQUEST_LIMIT:
115
+ raise ToolError('INVALID_REQUEST', 'Request exceeds the size limit')
116
+ request = json.loads(raw.decode('utf-8'), object_pairs_hook=no_duplicates)
117
+ operation = request.get('operation') if isinstance(request, dict) else None
118
+ if not isinstance(operation, str) or operation not in commands:
119
+ raise ToolError('INVALID_REQUEST', 'Unknown operation')
120
+ required, optional, handler = commands[operation]
121
+ keys = set(request) - {'operation'}
122
+ if not required <= keys <= required | optional:
123
+ raise ToolError('INVALID_REQUEST', 'Missing or unsupported request fields')
124
+ item = input_file(inputs, request['input']) if 'input' in request else None
125
+ artifact = output_file(outputs, request['output']) if 'output' in request else None
126
+ with tempfile.TemporaryDirectory(prefix='hai-tool-') as workspace:
127
+ data = handler(request, item, artifact, pathlib.Path(workspace))
128
+ result = {'success': True, 'data': data}
129
+ except ToolError as error:
130
+ result = {'success': False, 'error': {'code': error.code, 'message': str(error)}}
131
+ except (json.JSONDecodeError, UnicodeDecodeError, RecursionError):
132
+ result = {'success': False, 'error': {'code': 'INVALID_REQUEST', 'message': 'Malformed JSON request'}}
133
+ except subprocess.TimeoutExpired:
134
+ result = {'success': False, 'error': {'code': 'TIMEOUT', 'message': 'The selected operation timed out'}}
135
+ except (OSError, ValueError):
136
+ result = {'success': False, 'error': {'code': 'TOOL_FAILED', 'message': 'The selected operation failed'}}
137
+ response.write(json.dumps(result, ensure_ascii=True, allow_nan=False) + '\n')
138
+
139
+
140
+ if __name__ == '__main__':
141
+ # 可执行文件只加载构建时随镜像复制的固定操作。
142
+ sys.path.insert(0, '/opt/hai-tools')
143
+ sys.modules['tool'] = sys.modules[__name__]
144
+ from operation import commands
145
+ serve(sys.stdin.buffer, sys.stdout, commands, pathlib.Path('/input'), pathlib.Path('/output'))